2 #ifndef GAUDIKERNEL_GRAMMARS_H
3 #define GAUDIKERNEL_GRAMMARS_H 1
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.
19 #include <boost/version.hpp>
20 #if BOOST_VERSION >= 103800
22 #if !defined(BOOST_SPIRIT_USE_OLD_NAMESPACE)
23 #define BOOST_SPIRIT_USE_OLD_NAMESPACE
25 #include <boost/spirit/include/classic.hpp>
26 #include <boost/spirit/include/phoenix1.hpp>
28 #include <boost/spirit.hpp>
29 #include <boost/spirit/phoenix.hpp>
31 #include <boost/bind.hpp>
63 struct ClosureGrammar :
public boost::spirit::closure < ClosureGrammar<T>,T >
65 typedef boost::spirit::closure<ClosureGrammar, T> closure;
66 typename closure::member1 val;
79 template <
typename T1,
typename T2>
80 struct AttributesClosureGrammar
81 :
public boost::spirit::closure<AttributesClosureGrammar<T1,T2>,T1,T2>
83 typedef boost::spirit::closure<AttributesClosureGrammar, T1,T2> closure;
84 typename closure::member1 val;
85 typename closure::member2 attrs;
99 class BoolGrammar :
public grammar
102 ClosureGrammar<bool>::context_t
106 typedef bool ResultT;
108 template <
typename ScannerT>
111 definition( BoolGrammar
const &
self)
114 = true_literal[
self.val =
true] | false_literal[
self.val =
false];
116 = str_p(
"true" ) | str_p(
"True" ) | str_p(
"TRUE" ) | str_p(
"1");
118 = str_p(
"false") | str_p(
"False") | str_p(
"FALSE") | str_p(
"0");
120 rule<ScannerT>
const&
start()
const
121 {
return boolean_literal;}
122 rule<ScannerT> boolean_literal,true_literal,false_literal;
136 template<
typename RT=
char>
137 class CharGrammar :
public grammar
139 CharGrammar<RT> , typename ClosureGrammar<RT>::context_t
145 template <
typename ScannerT>
148 definition( CharGrammar<RT>
const &
self)
151 = int_parser<RT>()[
self.val=arg1]
153 >> ( str_p(
"\\'")[
self.val=
'\'']
154 | (anychar_p[
self.val=arg1]-
'\'') )>>
'\'');
156 rule<ScannerT>
const&
start()
const
157 {
return char_literal; }
158 rule<ScannerT> char_literal;
173 template<
typename RT=
int>
174 class IntGrammar :
public grammar
177 typename ClosureGrammar<RT>::context_t
183 template <
typename ScannerT>
186 definition( IntGrammar<RT>
const &
self)
188 int_literal = lexeme_d[int_parser<RT>()[
self.val=arg1]
189 >> !(ch_p(
'u') | ch_p(
'U') | ch_p(
'l') | ch_p(
'L'))];
191 rule<ScannerT>
const&
start()
const {
return int_literal; }
192 rule<ScannerT> int_literal;
207 template<
typename RT=
double>
208 class RealGrammar :
public grammar
210 RealGrammar<RT>,typename ClosureGrammar<RT>::context_t
216 template <
typename ScannerT>
219 definition( RealGrammar
const &
self)
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'))];
226 rule<ScannerT>
const&
start()
const
227 {
return real_literal; }
228 rule<ScannerT> real_literal;
246 class StringGrammar :
public grammar
248 StringGrammar, ClosureGrammar<std::string>::context_t
252 typedef std::string ResultT;
261 void matchString()
const
263 for ( std::string::iterator cur=this->val().begin();
264 cur!=this->val().end();cur++)
265 {
if(std::isspace(*cur) ) { *cur =
' '; } }
268 template <
typename ScannerT>
271 definition( StringGrammar
const &
self )
273 string_literal = (lexeme_d
275 (
'"' >> (*( str_p(
"\\\"")
278 [
self.val = construct_<std::string>
282 (
'\'' >> (*( str_p(
"\\'")
285 [
self.val = construct_<std::string>
289 rule<ScannerT>
const&
start()
const {
return string_literal; }
290 rule<ScannerT> string_literal;
306 class SkipperGrammar :
public grammar<SkipperGrammar>
312 SkipperGrammar (
const bool skipnewline =
true )
313 : m_skipnewline(skipnewline){}
316 bool skipnewline()
const{
return m_skipnewline;}
318 template <
typename ScannerT>
321 definition( SkipperGrammar
const&
self)
323 if (
self.skipnewline() )
328 | comment_p(
"/*",
"*/")
336 | comment_p(
"/*",
"*/")
341 rule<ScannerT>
const&
start()
const {
return skip; }
357 template <
typename KeyGrammarT,
typename ValueGrammarT>
358 class PairGrammar :
public grammar
360 PairGrammar<KeyGrammarT,ValueGrammarT>,
361 typename ClosureGrammar<
362 std::pair<typename KeyGrammarT::ResultT,
363 typename ValueGrammarT::ResultT> >::context_t
367 typedef typename KeyGrammarT::ResultT KeyT;
368 typedef typename ValueGrammarT::ResultT ValueT;
369 typedef std::pair<KeyT,ValueT> ResultT;
374 PairGrammar (
const std::string& delim =
"," )
378 void matchFirst (
const KeyT& first )
const { this->val().first = first; }
380 void matchSecond (
const ValueT&
second )
const { this->val().second =
second; }
382 template <
typename ScannerT>
385 definition( PairGrammar
const &
self)
391 >>
self.delim().c_str()
396 rule<ScannerT>
const&
start()
const {
return para; }
399 ValueGrammarT grvalue;
403 const std::string& delim()
const {
return m_delim ; }
407 void setDelim (
const std::string& delim ) { m_delim = delim;}
423 template <
typename GrammarT>
424 class VectorGrammar :
public grammar
426 VectorGrammar<GrammarT> ,
427 typename ClosureGrammar<std::vector<typename GrammarT::ResultT> >::context_t
431 typedef typename GrammarT::ResultT ValueT;
432 typedef std::vector<ValueT> ResultT;
433 typedef VectorGrammar<GrammarT> SelfT;
436 void matchItem(
const ValueT&
value)
const { this->val().push_back(value); }
438 template <
typename ScannerT>
441 definition(SelfT
const &
self)
447 '[' >> inner >>
']' |
448 '(' >> inner >>
')' |
449 '{' >> inner >>
'}' ;
451 rule<ScannerT>
const&
start()
const {
return vec; }
452 rule<ScannerT> vec,inner;
471 template <
typename KeyGrammarT,
typename ValueGrammarT>
472 class MapGrammar :
public grammar
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
483 typedef typename KeyGrammarT::ResultT KeyT;
484 typedef typename ValueGrammarT::ResultT ValueT;
485 typedef std::map<KeyT,ValueT> ResultT;
488 void matchItem ()
const
491 this->val()[this->attrs().first] = this->attrs().second ;
494 void matchFirst (
const KeyT&
value )
const { this->attrs().first =
value ; }
496 void matchSecond(
const ValueT&
value )
const { this->attrs().second =
value ; }
498 template <
typename ScannerT>
501 definition( MapGrammar
const &
self)
504 = (
'{'>> inner_list >>
'}') | (
'['>>inner_list>>
']');
514 >> ( ch_p(
'=') | ch_p(
':'))
518 ValueGrammarT grValue;
519 rule<ScannerT>
const&
start()
const {
return vec; }
520 rule<ScannerT> vec,inner, inner_list ;
529 #endif // GAUDIKERNEL_GRAMMARS_H
void matchSecond(const ValueT &second) const
callback. Action when we match second value
void matchFirst(const KeyT &value) const
call backs. Action when we match key of pair
void matchSecond(const ValueT &value) const
call backs. Action when we match value pf pair
void matchFirst(const KeyT &first) const
callback. Action when we match first value
void matchString() const
remove CR/LF symbols form the parsed strings
void matchItem(const ValueT &value) const
callback. Action when we match inner value
Helper functions to set/get the application return code.
void matchItem() const
call backs. Action when we match pair in map