| The Gaudi Framework v29r0 (ff2e7097)

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