| The Gaudi Framework v30r0 (c919700c)

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