The Gaudi Framework  v30r3 (a5ef0a68)
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> {
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  //---------------------------------------------------------------------------
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  //-----------------------------------------------------------------------------
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;
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;
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;
199  ph::function<NodeOperations> op;
200  };
201  // ============================================================================
202  } /* Gaudi */
203 } /* Parsers */
204 // ============================================================================
205 #endif // JOBOPTIONSVC_GRAMMAR_H_
qi::rule< Iterator, Node(), qi::locals< std::string > > shell
Definition: Grammar.h:189
qi::rule< Iterator, void(char)> end_vector
Definition: Grammar.h:194
qi::rule< Iterator, Node(), qi::locals< Node, Node >, Skipper > condition
Definition: Grammar.h:192
qi::rule< Iterator > comments
Definition: Grammar.h:49
qi::rule< Iterator, std::string(), Skipper > str
Definition: Grammar.h:89
IdentifierGrammar< Iterator, Skipper > gidentifier
Definition: Grammar.h:198
Gaudi::Details::PropertyBase * property(const std::string &name) const
RealGrammar< Iterator, Skipper > greal
Definition: Grammar.h:197
qi::rule< Iterator, void(char)> quote
Definition: Grammar.h:70
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:119
STL class.
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > value
Definition: Grammar.h:190
ph::function< NodeOperations > op
Definition: Grammar.h:91
qi::rule< Iterator, Node(), Skipper > real
Definition: Grammar.h:118
BoolGrammar< Iterator, Skipper > gbool
Definition: Grammar.h:196
qi::rule< Iterator, Node(), Skipper > val
Definition: Grammar.h:136
ph::function< NodeOperations > op
Definition: Grammar.h:199
qi::rule< Iterator, char()> begin_vector
Definition: Grammar.h:193
qi::rule< Iterator, Node(), qi::locals< char >, Skipper > vector_value
Definition: Grammar.h:191
qi::rule< Iterator, Node(), Skipper > units
Definition: Grammar.h:187
qi::rule< Iterator, Node(), Skipper > ident
Definition: Grammar.h:88
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:137
qi::rule< Iterator, std::string()> inner
Definition: Grammar.h:90
qi::rule< Iterator, bool(), Skipper > boolean
Definition: Grammar.h:104
qi::rule< Iterator, std::string(), qi::locals< char >, Skipper > str
Definition: Grammar.h:68
StringGrammar< Iterator, Skipper > gstring
Definition: Grammar.h:195
ph::function< NodeOperations > op
Definition: Grammar.h:138
ph::function< NodeOperations > op
Definition: Grammar.h:120
Helper functions to set/get the application return code.
Definition: __init__.py:1
qi::rule< Iterator, char()> begin_quote
Definition: Grammar.h:69