The Gaudi Framework  v29r0 (ff2e7097)
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> {
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  //---------------------------------------------------------------------------
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  //-----------------------------------------------------------------------------
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;
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;
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;
204  ph::function<NodeOperations> op;
205  };
206  // ============================================================================
207  } /* Gaudi */
208 } /* Parsers */
209 // ============================================================================
210 #endif // JOBOPTIONSVC_GRAMMAR_H_
qi::rule< Iterator, Node(), qi::locals< std::string > > shell
Definition: Grammar.h:194
qi::rule< Iterator, void(char)> end_vector
Definition: Grammar.h:199
qi::rule< Iterator, Node(), qi::locals< Node, Node >, Skipper > condition
Definition: Grammar.h:197
qi::rule< Iterator > comments
Definition: Grammar.h:54
qi::rule< Iterator, std::string(), Skipper > str
Definition: Grammar.h:94
IdentifierGrammar< Iterator, Skipper > gidentifier
Definition: Grammar.h:203
Gaudi::Details::PropertyBase * property(const std::string &name) const
RealGrammar< Iterator, Skipper > greal
Definition: Grammar.h:202
qi::rule< Iterator, void(char)> quote
Definition: Grammar.h:75
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:124
STL class.
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > value
Definition: Grammar.h:195
ph::function< NodeOperations > op
Definition: Grammar.h:96
qi::rule< Iterator, Node(), Skipper > real
Definition: Grammar.h:123
BoolGrammar< Iterator, Skipper > gbool
Definition: Grammar.h:201
qi::rule< Iterator, Node(), Skipper > val
Definition: Grammar.h:141
ph::function< NodeOperations > op
Definition: Grammar.h:204
qi::rule< Iterator, char()> begin_vector
Definition: Grammar.h:198
qi::rule< Iterator, Node(), qi::locals< char >, Skipper > vector_value
Definition: Grammar.h:196
qi::rule< Iterator, Node(), Skipper > units
Definition: Grammar.h:192
qi::rule< Iterator, Node(), Skipper > ident
Definition: Grammar.h:93
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:142
qi::rule< Iterator, std::string()> inner
Definition: Grammar.h:95
qi::rule< Iterator, bool(), Skipper > boolean
Definition: Grammar.h:109
qi::rule< Iterator, std::string(), qi::locals< char >, Skipper > str
Definition: Grammar.h:73
StringGrammar< Iterator, Skipper > gstring
Definition: Grammar.h:200
ph::function< NodeOperations > op
Definition: Grammar.h:143
ph::function< NodeOperations > op
Definition: Grammar.h:125
Helper functions to set/get the application return code.
Definition: __init__.py:1
qi::rule< Iterator, char()> begin_quote
Definition: Grammar.h:74