| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Grammar.h
Go to the documentation of this file.
1 #ifndef JOBOPTIONSVC_GRAMMAR_H_
2 #define JOBOPTIONSVC_GRAMMAR_H_
3 // ============================================================================
4 // Includes:
5 // ============================================================================
6 // STD & STL:
7 // ============================================================================
8 #include <string>
9 #include <vector>
10 #include <map>
11 // ============================================================================
12 // Boost:
13 // ============================================================================
14 #include <boost/spirit/include/qi.hpp>
15 #include <boost/spirit/include/phoenix_core.hpp>
16 #include <boost/spirit/include/phoenix_fusion.hpp>
17 #include <boost/spirit/include/phoenix_operator.hpp>
18 #include <boost/spirit/include/phoenix_stl.hpp>
19 #include <boost/spirit/repository/include/qi_confix.hpp>
20
21 #if BOOST_VERSION <= 104400
22 #include "iter_pos.hpp"
23 #else
24 #include <boost/spirit/repository/include/qi_iter_pos.hpp>
25 #endif
26
27 // ============================================================================
28 // Project:
29 // ============================================================================
30 #include "Node.h"
31 //============================================================================
32 namespace Gaudi { namespace Parsers {
33 // ============================================================================
34 // Namespace aliases:
35 // ============================================================================
36 namespace sp = boost::spirit;
37 namespace ph = boost::phoenix;
38 namespace qi = sp::qi;
39 namespace enc = sp::ascii;
40 namespace rep = sp::repository;
41 //=============================================================================
42 // Grammars
43 //=============================================================================
44 template<typename Iterator>
45 struct SkipperGrammar: qi::grammar<Iterator> {
46 SkipperGrammar() : SkipperGrammar::base_type(comments) {
47 comments = enc::space
48 | rep::confix("/*", "*/")[*(qi::char_ - "*/")]
49 | rep::confix("//", (sp::eol | sp::eoi))[*(qi::char_ - (sp::eol | sp::eoi))];
50 }
51 qi::rule<Iterator> comments;
52 };
53 // ============================================================================
54 template<typename Iterator, typename Skipper>
55 struct StringGrammar: qi::grammar<Iterator, std::string(), qi::locals<char>,
56 Skipper> {
57 //---------------------------------------------------------------------------
58 typedef std::string ResultT;
59 //---------------------------------------------------------------------
60 StringGrammar() : StringGrammar::base_type(str) {
61 begin_quote = enc::char_("\"'");
62 quote = enc::char_(qi::_r1);
63
64 str = qi::lexeme[begin_quote[qi::_a = qi::_1] >> *((enc::char_('\\')
65 >> quote(qi::_a))[qi::_val += qi::_a]
66 | (enc::char_[qi::_val += qi::_1] - quote(qi::_a)))
67 >> quote(qi::_a)];
68 }
69 //-----------------------------------------------------------------------------
70 qi::rule<Iterator, std::string(), qi::locals<char>, Skipper> str;
71 qi::rule<Iterator, char()> begin_quote;
72 qi::rule<Iterator, void(char)> quote;
73 //-----------------------------------------------------------------------------
74 };
75 // ============================================================================
76 template<typename Iterator, typename Skipper>
77 struct IdentifierGrammar: qi::grammar<Iterator, Node(), Skipper> {
78 //-----------------------------------------------------------------------------
79 typedef std::string ResultT;
80 //-----------------------------------------------------------------------------
81 IdentifierGrammar() : IdentifierGrammar::base_type(ident) {
82 ident = rep::qi::iter_pos[op(qi::_val, qi::_1)] >> str[op(qi::_val, qi::_1)][op(qi::_val, Node::kIdentifier)];
83 str = -qi::lit("::")[qi::_val += "::"] >> inner[qi::_val += qi::_1]
84 >> *(qi::lit("::") >> inner[qi::_val += ("::"+qi::_1)]);
85 inner = qi::alpha >> *(qi::alnum | qi::char_('_'));
86 }
87 // ----------------------------------------------------------------------------
88 qi::rule<Iterator, Node(), Skipper> ident;
89 qi::rule<Iterator, std::string(), Skipper> str;
90 qi::rule<Iterator, std::string()> inner;
91 ph::function<NodeOperations> op;
92 };
93 // ============================================================================
94 template<typename Iterator, typename Skipper>
95 struct BoolGrammar: qi::grammar<Iterator, bool(), Skipper> {
96 // ----------------------------------------------------------------------------
97 typedef bool ResultT;
98 // ----------------------------------------------------------------------------
99 BoolGrammar() : BoolGrammar::base_type(boolean) {
100 boolean = enc::no_case[
101 qi::lit("true")[qi::_val=true]
102 |
103 qi::lit("false")[qi::_val=false]
104 ];
105 }
106 // ----------------------------------------------------------------------------
107 qi::rule<Iterator, bool(), Skipper> boolean;
108 };
109 // ============================================================================
110 template<typename Iterator, typename Skipper>
111 struct RealGrammar: qi::grammar<Iterator, Node(), Skipper> {
112 // ----------------------------------------------------------------------------
113 typedef bool ResultT;
114 //---------------------------------------------------------------------
115 RealGrammar() : RealGrammar::base_type(real) {
116 real = qi::raw[qi::double_][op(qi::_val, qi::_1)]
117 [op(qi::_val, Node::kReal)]
118 >> -qi::char_('L') >> -(-qi::char_('*') >> gunit[op(qi::_val,qi::_1)]);
119
120 }
121 // ----------------------------------------------------------------------------
122 qi::rule<Iterator, Node(), Skipper> real;
123 IdentifierGrammar<Iterator,Skipper> gunit;
124 ph::function<NodeOperations> op;
125 };
126 // ============================================================================
127 template<typename Iterator, typename Skipper>
128 struct UnitsGrammar: qi::grammar<Iterator, Node(), Skipper> {
129 // ----------------------------------------------------------------------------
130 UnitsGrammar() : UnitsGrammar::base_type(units) {
131 units = *unit[op(qi::_val, qi::_1)];
132 unit = rep::qi::iter_pos[op(qi::_val, qi::_1)] >>
133 val[op(qi::_val, qi::_1)]
134 >> -qi::lit('*') >> gunit[op(qi::_val, qi::_1)]
135 >> qi::lit('=') >> val[op(qi::_val, qi::_1)]
136 [op(qi::_val, Node::kUnit)];;
137 val = qi::raw[qi::double_]
138 [op(qi::_val, qi::_1)][op(qi::_val, Node::kReal)];
139
140 }
141 // ----------------------------------------------------------------------------
142 qi::rule<Iterator, Node(), Skipper> units, unit, val;
143 IdentifierGrammar<Iterator,Skipper> gunit;
144 ph::function<NodeOperations> op;
145 };
146 // ============================================================================
147 template<typename Iterator, typename Skipper>
148 struct FileGrammar: qi::grammar<Iterator, Node(), Skipper> {
149 FileGrammar() : FileGrammar::base_type(file) {
150 file = -shell[op(qi::_val,qi::_1)] >>
151 *(statement[op(qi::_val, qi::_1)])
152 [op(qi::_val, Node::kRoot)];
153 shell = rep::confix("#!", qi::eol)[*(qi::char_[qi::_a += qi::_1]
154 - qi::eol)][op(qi::_val,Node::kShell)]
155 [op(qi::_val,qi::_a)];
156 statement = rep::qi::iter_pos[qi::_a = qi::_1]
157 >> (include | assign | units | print_options | pragma
158 | condition)[qi::_val = qi::_1][op(qi::_val,qi::_a)];
159 condition = (qi::lit("#ifdef")[op(qi::_a,Node::kIfdef)]
160 | qi::lit("#ifndef")[op(qi::_a,Node::kIfndef)])
161 >> property[op(qi::_val,qi::_1)]
162 >> (*statement[op(qi::_a,qi::_1)])[op(qi::_val, qi::_a)]
163 >> -(qi::lit("#else")[op(qi::_b,Node::kElse)]
164 >> *statement[op(qi::_b,qi::_1)])[op(qi::_val,qi::_b)]
165 >> qi::lit("#endif")[op(qi::_val, Node::kCondition)];
166 include = qi::lit("#include")
167 >> gstring[op(qi::_val, qi::_1)]
168 [op(qi::_val, Node::kInclude)];
169 units = qi::lit("#units")
170 >> gstring[op(qi::_val, qi::_1)]
171 [op(qi::_val, Node::kUnits)];
172 print_options = qi::lit("#printOptions") >> qi::lit("full")
173 [op(qi::_val, Node::kPrintOptions)];
174 pragma = qi::lit("#pragma") >> (pragma_print | pragma_tree |
175 pragma_dump_file);
176 pragma_print = qi::lit("print") >> enc::no_case[
177 qi::lit("on")[op(qi::_val, Node::kPrintOn)]
178 | qi::lit("off")[op(qi::_val, Node::kPrintOff)]
179 ];
180 pragma_tree = enc::no_case[
181 qi::lit("printtree")[op(qi::_val, Node::kPrintTree)]
182 ];
183 pragma_dump_file = qi::lit("dumpfile") >> gstring[op(qi::_val, qi::_1)]
184 [op(qi::_val, Node::kDumpFile)];
185 assign = property[op(qi::_val, qi::_1)]
186 [op(qi::_val, Node::kAssign)]
187 >> oper[op(qi::_val, qi::_1)]
188 >> value[op(qi::_val, qi::_1)]
189 >> ';';
190 property = rep::qi::iter_pos[op(qi::_val, qi::_1)]
191 >> (gidentifier[op(qi::_val, qi::_1)] % '.')
192 [op(qi::_val, Node::kProperty)];
193 property_ref %= -qi::lit('@') >>
194 property[op(qi::_val, Node::kPropertyRef)];
195 oper = rep::qi::iter_pos[op(qi::_val, qi::_1)]
196 >> (qi::lit("=")[op(qi::_val, Node::kEqual)]
197 |
198 qi::lit("+=")[op(qi::_val, Node::kPlusEqual)]
199 |
200 qi::lit("-=")[op(qi::_val, Node::kMinusEqual)]);
201 value = rep::qi::iter_pos[qi::_a = qi::_1]
202 >>
203 (map_value | vector_value | simple_value | property |
204 property_ref)
205 [qi::_val = qi::_1][op(qi::_val, qi::_a)]
206 ;
207 begin_vector = enc::char_('(')[qi::_val=')']
208 |
209 enc::char_('[')[qi::_val=']']
210 |
211 enc::char_('{')[qi::_val='}'];
212 end_vector = qi::char_(qi::_r1);
213 vector_value = (begin_vector[qi::_a = qi::_1]
214 >> -(value[op(qi::_val,qi::_1)] % ',')
215 >> end_vector(qi::_a)
216 )[op(qi::_val,Node::kVector)];
217 map_value = (enc::char_('{')
218 >> -(pair[op(qi::_val, qi::_1)] % ',')
219 >> enc::char_('}'))
220 [op(qi::_val, Node::kMap)];
221 pair = simple_value[op(qi::_val,qi::_1)] >> ':'
222 >> value[op(qi::_val,qi::_1)]
223 [op(qi::_val,Node::kPair)];
224 simple_value =
225 (gstring[op(qi::_val, qi::_1)][op(qi::_val, Node::kString)])
226 |
227 (gbool[op(qi::_val, qi::_1)][op(qi::_val, Node::kBool)])
228 |
229 (greal[qi::_val = qi::_1]);
230 }
231 qi::rule<Iterator, Node(), Skipper> file, include, assign, property,
232 property_ref, oper, map_value, pair_value, simple_value, pair, units,
233 print_options, pragma, pragma_print, pragma_tree, pragma_dump_file;
234 qi::rule<Iterator, Node(),qi::locals<std::string> > shell;
235 qi::rule<Iterator, Node(), qi::locals<Iterator>, Skipper> statement, value;
236 qi::rule<Iterator, Node(), qi::locals<char>, Skipper> vector_value;
237 qi::rule<Iterator, Node(), qi::locals<Node, Node>, Skipper> condition;
238 qi::rule<Iterator, char()> begin_vector;
239 qi::rule<Iterator, void(char)> end_vector;
240 StringGrammar<Iterator, Skipper> gstring;
241 BoolGrammar<Iterator, Skipper> gbool;
242 RealGrammar<Iterator, Skipper> greal;
243 IdentifierGrammar<Iterator, Skipper> gidentifier;
244 ph::function<NodeOperations> op;
245 };
246 // ============================================================================
247 } /* Gaudi */ } /* Parsers */
248 // ============================================================================
249 #endif // JOBOPTIONSVC_GRAMMAR_H_
Gaudi::Parsers::Node::kString
Definition: Node.h:28
Gaudi::Parsers::Node::kRoot
Definition: Node.h:26
Gaudi::Parsers::BoolGrammar
Definition: Grammar.h:95
Gaudi::Parsers::Node::kMap
Definition: Node.h:27
Gaudi::Parsers::Node::kPlusEqual
Definition: Node.h:27
Gaudi::Parsers::BoolGrammar::ResultT
bool ResultT
Definition: Grammar.h:97
Gaudi::Parsers::RealGrammar
Definition: Grammar.h:111
Gaudi::Parsers::Node::kShell
Definition: Node.h:29
Gaudi::Parsers::Node::kPair
Definition: Node.h:27
Gaudi::Parsers::FileGrammar::condition
qi::rule< Iterator, Node(), qi::locals< Node, Node >, Skipper > condition
Definition: Grammar.h:237
Gaudi::Parsers::StringGrammar::ResultT
std::string ResultT
Definition: Grammar.h:58
Gaudi::Parsers::FileGrammar::shell
qi::rule< Iterator, Node(), qi::locals< std::string > > shell
Definition: Grammar.h:234
Gaudi::Parsers::SkipperGrammar::comments
qi::rule< Iterator > comments
Definition: Grammar.h:51
Gaudi::Parsers::IdentifierGrammar::str
qi::rule< Iterator, std::string(), Skipper > str
Definition: Grammar.h:89
Gaudi::Parsers::FileGrammar::gidentifier
IdentifierGrammar< Iterator, Skipper > gidentifier
Definition: Grammar.h:243
Gaudi::Parsers::Node::kUnit
Definition: Node.h:28
Gaudi::Parsers::UnitsGrammar::gunit
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:143
Gaudi::Parsers::SkipperGrammar::SkipperGrammar
SkipperGrammar()
Definition: Grammar.h:46
property
Gaudi::Details::PropertyBase * property(const std::string &name) const
Gaudi::Parsers::Node::kIdentifier
Definition: Node.h:26
Gaudi::Parsers::Node::kIfdef
Definition: Node.h:28
Gaudi::Parsers::Node::kMinusEqual
Definition: Node.h:27
Gaudi::Parsers::Node::kElse
Definition: Node.h:29
Gaudi::Parsers::FileGrammar::greal
RealGrammar< Iterator, Skipper > greal
Definition: Grammar.h:242
boost::spirit
Definition: iter_pos.hpp:13
Gaudi::Parsers::StringGrammar
Definition: Grammar.h:55
Gaudi::Parsers::Node::kVector
Definition: Node.h:27
Gaudi::Parsers::FileGrammar::end_vector
qi::rule< Iterator, void(char)> end_vector
Definition: Grammar.h:239
Gaudi::Parsers::Node::kEqual
Definition: Node.h:27
Gaudi::Parsers::IdentifierGrammar::ResultT
std::string ResultT
Definition: Grammar.h:79
Gaudi::Parsers::StringGrammar::quote
qi::rule< Iterator, void(char)> quote
Definition: Grammar.h:72
Gaudi::Parsers::FileGrammar::FileGrammar
FileGrammar()
Definition: Grammar.h:149
Gaudi::Parsers::Node::kPrintOff
Definition: Node.h:29
std::string
STL class.
Gaudi::Parsers::FileGrammar::value
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > value
Definition: Grammar.h:235
Gaudi::Parsers::BoolGrammar::BoolGrammar
BoolGrammar()
Definition: Grammar.h:99
Gaudi::Parsers::Node::kAssign
Definition: Node.h:27
Gaudi::Parsers::IdentifierGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:91
Gaudi::Parsers::RealGrammar::real
qi::rule< Iterator, Node(), Skipper > real
Definition: Grammar.h:122
Gaudi::Parsers::FileGrammar::gbool
BoolGrammar< Iterator, Skipper > gbool
Definition: Grammar.h:241
Gaudi::Parsers::UnitsGrammar::val
qi::rule< Iterator, Node(), Skipper > val
Definition: Grammar.h:142
Gaudi::Parsers::IdentifierGrammar
Definition: Grammar.h:77
Gaudi::Parsers::FileGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:244
Gaudi::Parsers::FileGrammar
Definition: Grammar.h:148
Gaudi::Parsers::Node::kCondition
Definition: Node.h:28
Gaudi::Parsers::FileGrammar::begin_vector
qi::rule< Iterator, char()> begin_vector
Definition: Grammar.h:238
Gaudi::Parsers::FileGrammar::vector_value
qi::rule< Iterator, Node(), qi::locals< char >, Skipper > vector_value
Definition: Grammar.h:236
Node.h
Gaudi::Parsers::FileGrammar::units
qi::rule< Iterator, Node(), Skipper > units
Definition: Grammar.h:231
Gaudi::Parsers::IdentifierGrammar::ident
qi::rule< Iterator, Node(), Skipper > ident
Definition: Grammar.h:88
Gaudi::Parsers::SkipperGrammar
Definition: Grammar.h:45
Gaudi::Parsers::RealGrammar::ResultT
bool ResultT
Definition: Grammar.h:113
Gaudi::Parsers::Node::kBool
Definition: Node.h:28
Gaudi::Parsers::IdentifierGrammar::inner
qi::rule< Iterator, std::string()> inner
Definition: Grammar.h:90
Gaudi::Parsers::StringGrammar::StringGrammar
StringGrammar()
Definition: Grammar.h:60
Gaudi::Parsers::IdentifierGrammar::IdentifierGrammar
IdentifierGrammar()
Definition: Grammar.h:81
Gaudi::Parsers::UnitsGrammar
Definition: Grammar.h:128
Gaudi::Parsers::RealGrammar::RealGrammar
RealGrammar()
Definition: Grammar.h:115
Gaudi::Parsers::BoolGrammar::boolean
qi::rule< Iterator, bool(), Skipper > boolean
Definition: Grammar.h:107
Gaudi::Parsers::Node::kPrintOptions
Definition: Node.h:29
Gaudi::Parsers::StringGrammar::str
qi::rule< Iterator, std::string(), qi::locals< char >, Skipper > str
Definition: Grammar.h:70
Gaudi::Parsers::Node::kPrintTree
Definition: Node.h:29
Gaudi::Parsers::Node::kInclude
Definition: Node.h:26
Gaudi::Parsers::FileGrammar::gstring
StringGrammar< Iterator, Skipper > gstring
Definition: Grammar.h:240
Gaudi::Parsers::UnitsGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:144
Gaudi::Parsers::Node::kPrintOn
Definition: Node.h:29
Gaudi::Parsers::RealGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:124
Gaudi::Parsers::RealGrammar::gunit
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:123
Gaudi::Parsers::Node::kUnits
Definition: Node.h:28
Gaudi::Parsers::Node::kIfndef
Definition: Node.h:28
Gaudi::Parsers::Node::kDumpFile
Definition: Node.h:29
Gaudi::Parsers::UnitsGrammar::UnitsGrammar
UnitsGrammar()
Definition: Grammar.h:130
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::Parsers::Node::kProperty
Definition: Node.h:26
Gaudi::Parsers::StringGrammar::begin_quote
qi::rule< Iterator, char()> begin_quote
Definition: Grammar.h:71
iter_pos.hpp
Gaudi::Parsers::Node::kPropertyRef
Definition: Node.h:30
Gaudi::Parsers::Node::kReal
Definition: Node.h:28