All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Grammars.h
Go to the documentation of this file.
1 // ============================================================================
2 #ifndef GAUDIKERNEL_GRAMMARS_H
3 #define GAUDIKERNEL_GRAMMARS_H 1
4 #ifdef __GNUC__
5 #warning \
6  The headers GaudiKernel/Grammars.h and GaudiKernel/Parsers.icpp are deprecated \
7  and will be removed from the next release of Gaudi. You should migrate your \
8  code the new pasers based on Boost.Spirit 2.
9 #endif
10 // ============================================================================
11 // Include files
12 // ============================================================================
13 // STD & STL
14 // ============================================================================
15 #include <cctype>
16 // ============================================================================
17 // Boost.Spirit
18 // ============================================================================
19 #include <boost/version.hpp>
20 #if BOOST_VERSION >= 103800
21 // FIXME: Move to the new boost::spirit::classic namespace
22 #if !defined(BOOST_SPIRIT_USE_OLD_NAMESPACE)
23 #define BOOST_SPIRIT_USE_OLD_NAMESPACE
24 #endif
25 #include <boost/spirit/include/classic.hpp>
26 #include <boost/spirit/include/phoenix1.hpp>
27 #else
28 #include <boost/spirit.hpp>
29 #include <boost/spirit/phoenix.hpp>
30 #endif
31 #include <boost/bind.hpp>
32 
33 // ============================================================================
44 // ============================================================================
45 namespace Gaudi
46 {
47  namespace Parsers
48  {
49  // ========================================================================
50  using namespace boost::spirit ;
51  // ========================================================================
52  using namespace phoenix ;
53  // ========================================================================
62  template <typename T>
63  struct ClosureGrammar : public boost::spirit::closure < ClosureGrammar<T>,T >
64  {
65  typedef boost::spirit::closure<ClosureGrammar, T> closure;
66  typename closure::member1 val;
67  };
68  // ========================================================================
79  template <typename T1,typename T2>
81  : public boost::spirit::closure<AttributesClosureGrammar<T1,T2>,T1,T2>
82  {
83  typedef boost::spirit::closure<AttributesClosureGrammar, T1,T2> closure;
84  typename closure::member1 val;
85  typename closure::member2 attrs;
86  };
87  // ========================================================================
99  class BoolGrammar : public grammar
100  <
101  BoolGrammar,
102  ClosureGrammar<bool>::context_t
103  >
104  {
105  public:
106  typedef bool ResultT;
107  public:
108  template <typename ScannerT>
109  struct definition
110  {
111  definition( BoolGrammar const &self)
112  {
113  boolean_literal
114  = true_literal[self.val = true] | false_literal[self.val = false];
115  true_literal
116  = str_p("true" ) | str_p("True" ) | str_p("TRUE" ) | str_p("1");
117  false_literal
118  = str_p("false") | str_p("False") | str_p("FALSE") | str_p("0");
119  }
120  rule<ScannerT> const& start() const
121  { return boolean_literal;}
122  rule<ScannerT> boolean_literal,true_literal,false_literal;
123  };
124  };
125  // ========================================================================
136  template<typename RT=char>
137  class CharGrammar : public grammar
138  <
139  CharGrammar<RT> , typename ClosureGrammar<RT>::context_t
140  >
141  {
142  public:
143  typedef RT ResultT;
144  public:
145  template <typename ScannerT>
146  struct definition
147  {
149  {
150  char_literal
151  = int_parser<RT>()[self.val=arg1]
152  | ('\''
153  >> ( str_p("\\'")[self.val='\'']
154  | (anychar_p[self.val=arg1]-'\'') )>>'\'');
155  }
156  rule<ScannerT> const& start() const
157  { return char_literal; }
158  rule<ScannerT> char_literal;
159  };
160  };
161  // ========================================================================
173  template<typename RT=int>
174  class IntGrammar : public grammar
175  <
176  IntGrammar<RT>,
177  typename ClosureGrammar<RT>::context_t
178  >
179  {
180  public:
181  typedef RT ResultT;
182  public:
183  template <typename ScannerT>
184  struct definition
185  {
187  {
188  int_literal = lexeme_d[int_parser<RT>()[self.val=arg1]
189  >> !(ch_p('u') | ch_p('U') | ch_p('l') | ch_p('L'))];
190  }
191  rule<ScannerT> const& start() const { return int_literal; }
192  rule<ScannerT> int_literal;
193  };
194  };
195  // ========================================================================
207  template<typename RT=double>
208  class RealGrammar : public grammar
209  <
210  RealGrammar<RT>,typename ClosureGrammar<RT>::context_t
211  >
212  {
213  public:
214  typedef RT ResultT;
215  public:
216  template <typename ScannerT>
217  struct definition
218  {
219  definition( RealGrammar const &self)
220  {
221  real_literal
222  = lexeme_d[real_parser<RT,
223  real_parser_policies<RT> >()[self.val = arg1]
224  >> !(ch_p('f') | ch_p('F') | ch_p('l') | ch_p('L'))];
225  }
226  rule<ScannerT> const& start() const
227  { return real_literal; }
228  rule<ScannerT> real_literal;
229  };
230  };
231  // ========================================================================
246  class StringGrammar : public grammar
247  <
248  StringGrammar, ClosureGrammar<std::string>::context_t
249  >
250  {
251  public:
252  typedef std::string ResultT;
261  void matchString() const
262  {
263  for ( std::string::iterator cur=this->val().begin();
264  cur!=this->val().end();cur++)
265  { if(std::isspace(*cur) ) { *cur = ' '; } }
266  }
267  public:
268  template <typename ScannerT>
269  struct definition
270  {
271  definition( StringGrammar const &self )
272  {
273  string_literal = (lexeme_d
274  [
275  ('"' >> (*( str_p("\\\"")
276  |
277  (anychar_p-'"') ))
278  [self.val = construct_<std::string>
279  (arg1,arg2)] >>
280  '"')
281  |
282  ('\'' >> (*( str_p("\\'")
283  |
284  (anychar_p-'\'') ))
285  [self.val = construct_<std::string>
286  (arg1,arg2)]>>
287  '\'')])[boost::bind(&StringGrammar::matchString,&self)];
288  }
289  rule<ScannerT> const& start() const { return string_literal; }
290  rule<ScannerT> string_literal;
291  };
292  };
293  // ========================================================================
306  class SkipperGrammar : public grammar<SkipperGrammar>
307  {
308  public:
312  SkipperGrammar ( const bool skipnewline = true )
313  : m_skipnewline(skipnewline){}
314  public:
316  bool skipnewline() const{return m_skipnewline;}
317  public:
318  template <typename ScannerT>
319  struct definition
320  {
322  {
323  if ( self.skipnewline() )
324  {
325  skip
326  = space_p
327  | comment_p("//") // C++ comment
328  | comment_p("/*", "*/") // C comment
329  ;
330  }
331  else
332  {
333  skip
334  = (space_p-eol_p)
335  | comment_p("//") // C++ comment
336  | comment_p("/*", "*/") // C comment
337  ;
338  }
339  }
340  rule<ScannerT> skip;
341  rule<ScannerT> const& start() const { return skip; }
342  };
343  private:
345  };
346  // ========================================================================
357  template <typename KeyGrammarT, typename ValueGrammarT>
358  class PairGrammar : public grammar
359  <
360  PairGrammar<KeyGrammarT,ValueGrammarT>,
361  typename ClosureGrammar<
362  std::pair<typename KeyGrammarT::ResultT,
363  typename ValueGrammarT::ResultT> >::context_t
364  >
365  {
366  public:
367  typedef typename KeyGrammarT::ResultT KeyT;
368  typedef typename ValueGrammarT::ResultT ValueT;
369  typedef std::pair<KeyT,ValueT> ResultT;
370  public:
374  PairGrammar ( const std::string& delim = "," )
375  : m_delim(delim) {}
376  public:
378  void matchFirst ( const KeyT& first ) const { this->val().first = first; }
380  void matchSecond ( const ValueT& second ) const { this->val().second = second; }
381  public:
382  template <typename ScannerT>
383  struct definition
384  {
385  definition( PairGrammar const &self)
386  {
387  para
388  = (
389  str_p("(")
390  >> (grkey[boost::bind(&PairGrammar::matchFirst,&self,_1)])
391  >> self.delim().c_str()
392  >> (grvalue[boost::bind(&PairGrammar::matchSecond,&self,_1)])
393  >> str_p(")")
394  ) ;
395  }
396  rule<ScannerT> const& start() const { return para; }
397  rule<ScannerT> para;
398  KeyGrammarT grkey;
399  ValueGrammarT grvalue;
400  };
401  public:
403  const std::string& delim() const { return m_delim ; }
407  void setDelim ( const std::string& delim ) { m_delim = delim;}
408  private:
409  std::string m_delim;
410  };
411  // ========================================================================
423  template <typename GrammarT>
424  class VectorGrammar : public grammar
425  <
426  VectorGrammar<GrammarT> ,
427  typename ClosureGrammar<std::vector<typename GrammarT::ResultT> >::context_t
428  >
429  {
430  public:
431  typedef typename GrammarT::ResultT ValueT;
432  typedef std::vector<ValueT> ResultT;
434  public:
436  void matchItem(const ValueT& value) const { this->val().push_back(value); }
437  public:
438  template <typename ScannerT>
439  struct definition
440  {
441  definition(SelfT const &self)
442  {
443  inner =
444  !(gr[boost::bind(&VectorGrammar::matchItem,&self,_1)]
445  >> *(','>>gr[boost::bind(&VectorGrammar::matchItem,&self,_1)]));
446  vec =
447  '[' >> inner >> ']' | // a'la python list
448  '(' >> inner >> ')' | // a'la python tuple
449  '{' >> inner >> '}' ; // like obsolete list from opts-grammar
450  }
451  rule<ScannerT> const& start() const { return vec; }
452  rule<ScannerT> vec,inner;
453  GrammarT gr;
454  };
455  };
456  // ========================================================================
471  template <typename KeyGrammarT, typename ValueGrammarT>
472  class MapGrammar : public grammar
473  <
474  MapGrammar<KeyGrammarT,ValueGrammarT>,
475  typename AttributesClosureGrammar
476  < std::map<typename KeyGrammarT::ResultT,
477  typename ValueGrammarT::ResultT>,
478  std::pair<typename KeyGrammarT::ResultT,
479  typename ValueGrammarT::ResultT> >::context_t
480  >
481  {
482  public:
483  typedef typename KeyGrammarT::ResultT KeyT;
484  typedef typename ValueGrammarT::ResultT ValueT;
485  typedef std::map<KeyT,ValueT> ResultT;
486  public:
488  void matchItem () const
489  {
490  //this->val().insert(this->attrs());
491  this->val()[this->attrs().first] = this->attrs().second ;
492  }
494  void matchFirst ( const KeyT& value ) const { this->attrs().first = value ; }
496  void matchSecond( const ValueT& value ) const { this->attrs().second = value ; }
497  public:
498  template <typename ScannerT>
499  struct definition
500  {
501  definition( MapGrammar const &self)
502  {
503  vec
504  = ('{'>> inner_list >> '}') | ('['>>inner_list>>']');
505  inner_list
506  =
507  !( inner[boost::bind(&MapGrammar::matchItem,&self)]
508  >> *( ch_p(',') >>
509  inner[boost::bind(&MapGrammar::matchItem,&self)] )
510  );
511  inner
512  =
513  grKey[boost ::bind(&MapGrammar::matchFirst,&self,_1)]
514  >> ( ch_p('=') | ch_p(':'))
515  >> grValue[boost::bind(&MapGrammar::matchSecond,&self,_1)] ;
516  }
517  KeyGrammarT grKey;
518  ValueGrammarT grValue;
519  rule<ScannerT> const& start() const { return vec; }
520  rule<ScannerT> vec,inner, inner_list ;
521  };
522  };
523  // ========================================================================
524  } // end of namespace Gaudi::Parsers
525 } // end of namespace Gaudi
526 // ============================================================================
527 // The END
528 // ============================================================================
529 #endif // GAUDIKERNEL_GRAMMARS_H
530 // ============================================================================
definition(MapGrammar const &self)
Definition: Grammars.h:501
The valid represenation of boolean values are:
Definition: Grammar.h:95
SkipperGrammar(const bool skipnewline=true)
Constructor.
Definition: Grammars.h:312
The valid represenation of char values are:
Definition: Grammars.h:137
Grammar or grammar rule which derive from this struct will have attribute of type T and name val ...
Definition: Grammars.h:63
void matchSecond(const ValueT &second) const
callback. Action when we match second value
Definition: Grammars.h:380
std::map< KeyT, ValueT > ResultT
Definition: Grammars.h:485
The valid represenation of real values are:
Definition: Grammar.h:111
The valid represenation of pairs are: ("abc",123) or ("abc","def") Inner types of pair depends on Key...
Definition: Grammars.h:358
rule< ScannerT > const & start() const
Definition: Grammars.h:341
definition(IntGrammar< RT > const &self)
Definition: Grammars.h:186
KeyGrammarT::ResultT KeyT
Definition: Grammars.h:483
GrammarT::ResultT ValueT
Definition: Grammars.h:431
definition(CharGrammar< RT > const &self)
Definition: Grammars.h:148
std::pair< KeyT, ValueT > ResultT
Definition: Grammars.h:369
VectorGrammar< GrammarT > SelfT
Definition: Grammars.h:433
void setDelim(const std::string &delim)
Set delimiters for pair values.
Definition: Grammars.h:407
PairGrammar(const std::string &delim=",")
Constructor.
Definition: Grammars.h:374
The valid represenation of string values are:
Definition: Grammar.h:55
rule< ScannerT > const & start() const
Definition: Grammars.h:396
void matchFirst(const KeyT &value) const
call backs. Action when we match key of pair
Definition: Grammars.h:494
boost::spirit::closure< AttributesClosureGrammar, T1, T2 > closure
Definition: Grammars.h:83
Grammar or grammar rule which derive from this struct will have two attributes: type T1 and name val...
Definition: Grammars.h:80
definition(PairGrammar const &self)
Definition: Grammars.h:385
void matchSecond(const ValueT &value) const
call backs. Action when we match value pf pair
Definition: Grammars.h:496
rule< ScannerT > const & start() const
Definition: Grammars.h:451
rule< ScannerT > const & start() const
Definition: Grammars.h:191
The valid representation of integers values are:
Definition: Grammars.h:174
void matchFirst(const KeyT &first) const
callback. Action when we match first value
Definition: Grammars.h:378
The valid represenation of vector are:
Definition: Grammars.h:424
closure::member1 val
Definition: Grammars.h:66
Skipping spaces and comments.
Definition: Grammar.h:45
const std::string & delim() const
Definition: Grammars.h:403
rule< ScannerT > const & start() const
Definition: Grammars.h:289
rule< ScannerT > const & start() const
Definition: Grammars.h:120
std::vector< ValueT > ResultT
Definition: Grammars.h:432
The valid represenation of map are:
Definition: Grammars.h:472
rule< ScannerT > const & start() const
Definition: Grammars.h:226
ValueGrammarT::ResultT ValueT
Definition: Grammars.h:368
void matchString() const
remove CR/LF symbols form the parsed strings
Definition: Grammars.h:261
rule< ScannerT > const & start() const
Definition: Grammars.h:156
definition(SkipperGrammar const &self)
Definition: Grammars.h:321
void matchItem(const ValueT &value) const
callback. Action when we match inner value
Definition: Grammars.h:436
definition(StringGrammar const &self)
Definition: Grammars.h:271
KeyGrammarT::ResultT KeyT
Definition: Grammars.h:367
rule< ScannerT > const & start() const
Definition: Grammars.h:519
definition(RealGrammar const &self)
Definition: Grammars.h:219
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:15
boost::spirit::closure< ClosureGrammar, T > closure
Definition: Grammars.h:65
void matchItem() const
call backs. Action when we match pair in map
Definition: Grammars.h:488
ValueGrammarT::ResultT ValueT
Definition: Grammars.h:484
definition(BoolGrammar const &self)
Definition: Grammars.h:111