The Gaudi Framework  master (37c0b60a)
Grammar.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef JOBOPTIONSVC_GRAMMAR_H_
12 #define JOBOPTIONSVC_GRAMMAR_H_
13 // ============================================================================
14 // Includes:
15 // ============================================================================
16 // STD & STL:
17 // ============================================================================
18 #include <map>
19 #include <string>
20 #include <vector>
21 // ============================================================================
22 // Boost:
23 // ============================================================================
24 #include <boost/phoenix/core.hpp>
25 #include <boost/phoenix/fusion.hpp>
26 #include <boost/phoenix/operator.hpp>
27 #include <boost/phoenix/stl.hpp>
28 #include <boost/spirit/include/qi.hpp>
29 #include <boost/spirit/repository/include/qi_confix.hpp>
30 #include <boost/spirit/repository/include/qi_iter_pos.hpp>
31 
32 // ============================================================================
33 // Project:
34 // ============================================================================
35 #include "Node.h"
36 //============================================================================
37 namespace Gaudi {
38  namespace Parsers {
39  // ============================================================================
40  // Namespace aliases:
41  // ============================================================================
42  namespace sp = boost::spirit;
43  namespace ph = boost::phoenix;
44  namespace qi = sp::qi;
45  namespace enc = sp::ascii;
46  namespace rep = sp::repository;
47  //=============================================================================
48  // Grammars
49  //=============================================================================
50  template <typename Iterator>
51  struct SkipperGrammar : qi::grammar<Iterator> {
53  comments = enc::space | rep::confix( "/*", "*/" )[*( qi::char_ - "*/" )] |
54  rep::confix( "//", ( sp::eol | sp::eoi ) )[*( qi::char_ - ( sp::eol | sp::eoi ) )];
55  }
56  qi::rule<Iterator> comments;
57  };
58  // ============================================================================
59  template <typename Iterator, typename Skipper>
60  struct StringGrammar : qi::grammar<Iterator, std::string(), qi::locals<char>, Skipper> {
61  //---------------------------------------------------------------------------
63  //---------------------------------------------------------------------
64  StringGrammar() : StringGrammar::base_type( str ) {
65  begin_quote = enc::char_( "\"'" );
66  quote = enc::char_( qi::_r1 );
67 
68  str = qi::lexeme[begin_quote[qi::_a = qi::_1] >>
69  *( ( enc::char_( '\\' ) >> quote( qi::_a ) )[qi::_val += qi::_a] |
70  ( enc::char_[qi::_val += qi::_1] - quote( qi::_a ) ) ) >>
71  quote( qi::_a )];
72  }
73  //-----------------------------------------------------------------------------
74  qi::rule<Iterator, std::string(), qi::locals<char>, Skipper> str;
75  qi::rule<Iterator, char()> begin_quote;
76  qi::rule<Iterator, void( char )> quote;
77  //-----------------------------------------------------------------------------
78  };
79  // ============================================================================
80  template <typename Iterator, typename Skipper>
81  struct IdentifierGrammar : qi::grammar<Iterator, Node(), Skipper> {
82  //-----------------------------------------------------------------------------
84  //-----------------------------------------------------------------------------
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;
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  boolean = enc::no_case[qi::lit( "true" )[qi::_val = true] | qi::lit( "false" )[qi::_val = false]];
106  }
107  // ----------------------------------------------------------------------------
108  qi::rule<Iterator, bool(), Skipper> boolean;
109  };
110  // ============================================================================
111  template <typename Iterator, typename Skipper>
112  struct RealGrammar : qi::grammar<Iterator, Node(), Skipper> {
113  // ----------------------------------------------------------------------------
114  typedef bool ResultT;
115  //---------------------------------------------------------------------
116  RealGrammar() : RealGrammar::base_type( real ) {
117  real = qi::raw[qi::double_][op( qi::_val, qi::_1 )][op( qi::_val, Node::kReal )] >> -qi::char_( 'L' ) >>
118  -( -qi::char_( '*' ) >> gunit[op( qi::_val, qi::_1 )] );
119  }
120  // ----------------------------------------------------------------------------
121  qi::rule<Iterator, Node(), Skipper> real;
123  ph::function<NodeOperations> op;
124  };
125  // ============================================================================
126  template <typename Iterator, typename Skipper>
127  struct UnitsGrammar : qi::grammar<Iterator, Node(), Skipper> {
128  // ----------------------------------------------------------------------------
129  UnitsGrammar() : UnitsGrammar::base_type( units ) {
130  units = *unit[op( qi::_val, qi::_1 )];
131  unit = rep::qi::iter_pos[op( qi::_val, qi::_1 )] >> val[op( qi::_val, qi::_1 )] >> -qi::lit( '*' ) >>
132  gunit[op( qi::_val, qi::_1 )] >> qi::lit( '=' ) >>
133  val[op( qi::_val, qi::_1 )][op( qi::_val, Node::kUnit )];
134  ;
135  val = qi::raw[qi::double_][op( qi::_val, qi::_1 )][op( qi::_val, Node::kReal )];
136  }
137  // ----------------------------------------------------------------------------
138  qi::rule<Iterator, Node(), Skipper> units, unit, val;
140  ph::function<NodeOperations> op;
141  };
142  // ============================================================================
143  template <typename Iterator, typename Skipper>
144  struct FileGrammar : qi::grammar<Iterator, Node(), Skipper> {
145  FileGrammar() : FileGrammar::base_type( file ) {
146  file = -shell[op( qi::_val, qi::_1 )] >> *( statement[op( qi::_val, qi::_1 )] )[op( qi::_val, Node::kRoot )];
147  shell = rep::confix(
148  "#!",
149  qi::eol )[*( qi::char_[qi::_a += qi::_1] - qi::eol )][op( qi::_val, Node::kShell )][op( qi::_val, qi::_a )];
150  statement = rep::qi::iter_pos[qi::_a = qi::_1] >> ( include | assign | units | print_options | pragma |
151  condition )[qi::_val = qi::_1][op( qi::_val, qi::_a )];
152  condition =
153  ( qi::lit( "#ifdef" )[op( qi::_a, Node::kIfdef )] | qi::lit( "#ifndef" )[op( qi::_a, Node::kIfndef )] ) >>
154  property[op( qi::_val, qi::_1 )] >> ( *statement[op( qi::_a, qi::_1 )] )[op( qi::_val, qi::_a )] >>
155  -( qi::lit( "#else" )[op( qi::_b, Node::kElse )] >>
156  *statement[op( qi::_b, qi::_1 )] )[op( qi::_val, qi::_b )] >>
157  qi::lit( "#endif" )[op( qi::_val, Node::kCondition )];
158  include = qi::lit( "#include" ) >> gstring[op( qi::_val, qi::_1 )][op( qi::_val, Node::kInclude )];
159  units = qi::lit( "#units" ) >> gstring[op( qi::_val, qi::_1 )][op( qi::_val, Node::kUnits )];
160  print_options = qi::lit( "#printOptions" ) >> qi::lit( "full" )[op( qi::_val, Node::kPrintOptions )];
161  pragma = qi::lit( "#pragma" ) >> ( pragma_print | pragma_tree | pragma_dump_file );
162  pragma_print = qi::lit( "print" ) >> enc::no_case[qi::lit( "on" )[op( qi::_val, Node::kPrintOn )] |
163  qi::lit( "off" )[op( qi::_val, Node::kPrintOff )]];
164  pragma_tree = enc::no_case[qi::lit( "printtree" )[op( qi::_val, Node::kPrintTree )]];
165  pragma_dump_file = qi::lit( "dumpfile" ) >> gstring[op( qi::_val, qi::_1 )][op( qi::_val, Node::kDumpFile )];
166  assign = property[op( qi::_val, qi::_1 )][op( qi::_val, Node::kAssign )] >> oper[op( qi::_val, qi::_1 )] >>
167  value[op( qi::_val, qi::_1 )] >> ';';
168  property = rep::qi::iter_pos[op( qi::_val, qi::_1 )] >>
169  ( gidentifier[op( qi::_val, qi::_1 )] % '.' )[op( qi::_val, Node::kProperty )];
170  property_ref %= -qi::lit( '@' ) >> property[op( qi::_val, Node::kPropertyRef )];
171  oper = rep::qi::iter_pos[op( qi::_val, qi::_1 )] >>
172  ( qi::lit( "=" )[op( qi::_val, Node::kEqual )] | qi::lit( "+=" )[op( qi::_val, Node::kPlusEqual )] |
173  qi::lit( "-=" )[op( qi::_val, Node::kMinusEqual )] );
174  value = rep::qi::iter_pos[qi::_a = qi::_1] >> ( map_value | vector_value | simple_value | property |
175  property_ref )[qi::_val = qi::_1][op( qi::_val, qi::_a )];
176  begin_vector =
177  enc::char_( '(' )[qi::_val = ')'] | enc::char_( '[' )[qi::_val = ']'] | enc::char_( '{' )[qi::_val = '}'];
178  end_vector = qi::char_( qi::_r1 );
179  vector_value = ( begin_vector[qi::_a = qi::_1] >> -( value[op( qi::_val, qi::_1 )] % ',' ) >>
180  end_vector( qi::_a ) )[op( qi::_val, Node::kVector )];
181  map_value = ( enc::char_( '{' ) >> -( pair[op( qi::_val, qi::_1 )] % ',' ) >>
182  enc::char_( '}' ) )[op( qi::_val, Node::kMap )];
183  pair =
184  simple_value[op( qi::_val, qi::_1 )] >> ':' >> value[op( qi::_val, qi::_1 )][op( qi::_val, Node::kPair )];
185  simple_value = ( gstring[op( qi::_val, qi::_1 )][op( qi::_val, Node::kString )] ) |
186  ( gbool[op( qi::_val, qi::_1 )][op( qi::_val, Node::kBool )] ) | ( greal[qi::_val = qi::_1] );
187  }
190  qi::rule<Iterator, Node(), qi::locals<std::string>> shell;
191  qi::rule<Iterator, Node(), qi::locals<Iterator>, Skipper> statement, value;
192  qi::rule<Iterator, Node(), qi::locals<char>, Skipper> vector_value;
193  qi::rule<Iterator, Node(), qi::locals<Node, Node>, Skipper> condition;
194  qi::rule<Iterator, char()> begin_vector;
195  qi::rule<Iterator, void( char )> end_vector;
200  ph::function<NodeOperations> op;
201  };
202  // ============================================================================
203  } // namespace Parsers
204 } // namespace Gaudi
205 // ============================================================================
206 #endif // JOBOPTIONSVC_GRAMMAR_H_
Gaudi::Parsers::UnitsGrammar::val
qi::rule< Iterator, Node(), Skipper > val
Definition: Grammar.h:138
Gaudi::Parsers::UnitsGrammar::units
qi::rule< Iterator, Node(), Skipper > units
Definition: Grammar.h:138
Gaudi::Parsers::RealGrammar::RealGrammar
RealGrammar()
Definition: Grammar.h:116
Gaudi::Parsers::StringGrammar::begin_quote
qi::rule< Iterator, char()> begin_quote
Definition: Grammar.h:75
Gaudi::Parsers::Node::kPrintOptions
@ kPrintOptions
Definition: Node.h:60
Gaudi::Parsers::RealGrammar::gunit
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:122
Gaudi::Parsers::Node::kBool
@ kBool
Definition: Node.h:53
std::string
STL class.
Gaudi::Parsers::FileGrammar::oper
qi::rule< Iterator, Node(), Skipper > oper
Definition: Grammar.h:188
Gaudi::Parsers::BoolGrammar::BoolGrammar
BoolGrammar()
Definition: Grammar.h:104
Gaudi::Parsers::Node::kEqual
@ kEqual
Definition: Node.h:44
Gaudi::Parsers::UnitsGrammar::UnitsGrammar
UnitsGrammar()
Definition: Grammar.h:129
Gaudi::Parsers::FileGrammar::file
qi::rule< Iterator, Node(), Skipper > file
Definition: Grammar.h:188
Gaudi::Parsers::Node::kInclude
@ kInclude
Definition: Node.h:38
Gaudi::Parsers::Node::kIdentifier
@ kIdentifier
Definition: Node.h:39
Gaudi::Parsers::Node::kMinusEqual
@ kMinusEqual
Definition: Node.h:46
Gaudi::Parsers::Node::kCondition
@ kCondition
Definition: Node.h:56
Gaudi::Parsers::FileGrammar::simple_value
qi::rule< Iterator, Node(), Skipper > simple_value
Definition: Grammar.h:189
Gaudi::Parsers::FileGrammar::end_vector
qi::rule< Iterator, void(char)> end_vector
Definition: Grammar.h:195
Gaudi::Parsers::FileGrammar::gidentifier
IdentifierGrammar< Iterator, Skipper > gidentifier
Definition: Grammar.h:199
Gaudi::Parsers::Node::kAssign
@ kAssign
Definition: Node.h:43
Gaudi::Parsers::FileGrammar::pair
qi::rule< Iterator, Node(), Skipper > pair
Definition: Grammar.h:189
Gaudi::Parsers::FileGrammar
Definition: Grammar.h:144
Gaudi::Parsers::Node::kPropertyRef
@ kPropertyRef
Definition: Node.h:66
Gaudi::Parsers::IdentifierGrammar::inner
qi::rule< Iterator, std::string()> inner
Definition: Grammar.h:95
GaudiPartProp.Nodes.Node
Node
Definition: Nodes.py:247
Gaudi::Parsers::Node::kProperty
@ kProperty
Definition: Node.h:40
Gaudi::Parsers::FileGrammar::greal
RealGrammar< Iterator, Skipper > greal
Definition: Grammar.h:198
Gaudi::Parsers::Node::kPrintOn
@ kPrintOn
Definition: Node.h:61
Gaudi::Parsers::StringGrammar::StringGrammar
StringGrammar()
Definition: Grammar.h:64
Gaudi::Parsers::Node::kIfdef
@ kIfdef
Definition: Node.h:57
Gaudi::Parsers::Node::kString
@ kString
Definition: Node.h:51
Gaudi::Parsers::FileGrammar::property_ref
qi::rule< Iterator, Node(), Skipper > property_ref
Definition: Grammar.h:188
Gaudi::Parsers::Node::kShell
@ kShell
Definition: Node.h:63
Gaudi::Parsers::SkipperGrammar::SkipperGrammar
SkipperGrammar()
Definition: Grammar.h:52
compareOutputFiles.sp
sp
Definition: compareOutputFiles.py:506
Gaudi::Parsers::FileGrammar::pragma_dump_file
qi::rule< Iterator, Node(), Skipper > pragma_dump_file
Definition: Grammar.h:189
Gaudi::Parsers::FileGrammar::statement
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > statement
Definition: Grammar.h:191
Gaudi::Parsers::Node::kPlusEqual
@ kPlusEqual
Definition: Node.h:45
Gaudi::Parsers::RealGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:123
Gaudi::Parsers::FileGrammar::assign
qi::rule< Iterator, Node(), Skipper > assign
Definition: Grammar.h:188
Gaudi::Parsers::FileGrammar::pragma_tree
qi::rule< Iterator, Node(), Skipper > pragma_tree
Definition: Grammar.h:189
Gaudi::Parsers::RealGrammar::real
qi::rule< Iterator, Node(), Skipper > real
Definition: Grammar.h:121
Gaudi::Parsers::FileGrammar::map_value
qi::rule< Iterator, Node(), Skipper > map_value
Definition: Grammar.h:188
Gaudi::Parsers::StringGrammar
Definition: Grammar.h:60
Gaudi::Parsers::FileGrammar::condition
qi::rule< Iterator, Node(), qi::locals< Node, Node >, Skipper > condition
Definition: Grammar.h:193
Gaudi::Parsers::FileGrammar::vector_value
qi::rule< Iterator, Node(), qi::locals< char >, Skipper > vector_value
Definition: Grammar.h:192
Gaudi::Parsers::Node::kUnit
@ kUnit
Definition: Node.h:55
Gaudi::Parsers::StringGrammar::quote
qi::rule< Iterator, void(char)> quote
Definition: Grammar.h:76
Gaudi::Parsers::FileGrammar::begin_vector
qi::rule< Iterator, char()> begin_vector
Definition: Grammar.h:194
Gaudi::Parsers::FileGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:200
Gaudi::Parsers::SkipperGrammar::comments
qi::rule< Iterator > comments
Definition: Grammar.h:56
Gaudi::Parsers::BoolGrammar
Definition: Grammar.h:100
Gaudi::Parsers::SkipperGrammar
Definition: Grammar.h:51
Gaudi::Parsers::FileGrammar::units
qi::rule< Iterator, Node(), Skipper > units
Definition: Grammar.h:189
Gaudi::Parsers::IdentifierGrammar::IdentifierGrammar
IdentifierGrammar()
Definition: Grammar.h:85
Gaudi::Parsers::FileGrammar::property
qi::rule< Iterator, Node(), Skipper > property
Definition: Grammar.h:188
Gaudi::Parsers::Node::kDumpFile
@ kDumpFile
Definition: Node.h:65
Gaudi::Parsers::FileGrammar::include
qi::rule< Iterator, Node(), Skipper > include
Definition: Grammar.h:188
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Parsers::UnitsGrammar::gunit
IdentifierGrammar< Iterator, Skipper > gunit
Definition: Grammar.h:139
Gaudi::Parsers::Node::kVector
@ kVector
Definition: Node.h:47
Gaudi::Parsers::Node::kRoot
@ kRoot
Definition: Node.h:37
Gaudi::Parsers::BoolGrammar::ResultT
bool ResultT
Definition: Grammar.h:102
Gaudi::Parsers::Node::kElse
@ kElse
Definition: Node.h:59
Gaudi::Parsers::Node::kUnits
@ kUnits
Definition: Node.h:54
Gaudi::Parsers::BoolGrammar::boolean
qi::rule< Iterator, bool(), Skipper > boolean
Definition: Grammar.h:108
Gaudi::Parsers::Node::kPrintTree
@ kPrintTree
Definition: Node.h:64
Gaudi::Parsers::UnitsGrammar
Definition: Grammar.h:127
Gaudi::Parsers::IdentifierGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:96
Node.h
Gaudi::Parsers::StringGrammar::ResultT
std::string ResultT
Definition: Grammar.h:62
Gaudi::Parsers::UnitsGrammar::unit
qi::rule< Iterator, Node(), Skipper > unit
Definition: Grammar.h:138
Gaudi::Parsers::IdentifierGrammar::str
qi::rule< Iterator, std::string(), Skipper > str
Definition: Grammar.h:94
Gaudi::Parsers::FileGrammar::FileGrammar
FileGrammar()
Definition: Grammar.h:145
Gaudi::Parsers::FileGrammar::print_options
qi::rule< Iterator, Node(), Skipper > print_options
Definition: Grammar.h:189
Gaudi::Parsers::IdentifierGrammar
Definition: Grammar.h:81
Gaudi::Parsers::Node::kPair
@ kPair
Definition: Node.h:49
Gaudi::Parsers::FileGrammar::gstring
StringGrammar< Iterator, Skipper > gstring
Definition: Grammar.h:196
Gaudi::Parsers::Node::kIfndef
@ kIfndef
Definition: Node.h:58
Gaudi::Parsers::Node::kMap
@ kMap
Definition: Node.h:48
Gaudi::Parsers::Node::kReal
@ kReal
Definition: Node.h:52
Gaudi::Parsers::UnitsGrammar::op
ph::function< NodeOperations > op
Definition: Grammar.h:140
Gaudi::Parsers::FileGrammar::pragma_print
qi::rule< Iterator, Node(), Skipper > pragma_print
Definition: Grammar.h:189
Gaudi::Parsers::FileGrammar::pair_value
qi::rule< Iterator, Node(), Skipper > pair_value
Definition: Grammar.h:188
Gaudi::Parsers::RealGrammar::ResultT
bool ResultT
Definition: Grammar.h:114
Gaudi::Parsers::FileGrammar::gbool
BoolGrammar< Iterator, Skipper > gbool
Definition: Grammar.h:197
Gaudi::Parsers::IdentifierGrammar::ident
qi::rule< Iterator, Node(), Skipper > ident
Definition: Grammar.h:93
Gaudi::Parsers::IdentifierGrammar::ResultT
std::string ResultT
Definition: Grammar.h:83
Gaudi::Parsers::FileGrammar::pragma
qi::rule< Iterator, Node(), Skipper > pragma
Definition: Grammar.h:189
Gaudi::Parsers::FileGrammar::shell
qi::rule< Iterator, Node(), qi::locals< std::string > > shell
Definition: Grammar.h:190
Gaudi::Parsers::RealGrammar
Definition: Grammar.h:112
Iterator
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:28
Gaudi::Parsers::Node::kPrintOff
@ kPrintOff
Definition: Node.h:62
Gaudi::Parsers::StringGrammar::str
qi::rule< Iterator, std::string(), qi::locals< char >, Skipper > str
Definition: Grammar.h:74
Gaudi::Parsers::FileGrammar::value
qi::rule< Iterator, Node(), qi::locals< Iterator >, Skipper > value
Definition: Grammar.h:191