| The Gaudi Framework v32r1 (f65d50dc)

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