Newer
Older
// ============================================================================
#ifndef DD4HEPKERNEL_GRAMMARS_H
#define DD4HEPKERNEL_GRAMMARS_H 1
#ifdef __GNUC__
#warning \
The headers Grammars.h and Parsers.icpp are deprecated \
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
and will be removed from the next release of Gaudi. You should migrate your \
code the new pasers based on Boost.Spirit 2.
#endif
// ============================================================================
// Include files
// ============================================================================
// STD & STL
// ============================================================================
#include <cctype>
// ============================================================================
// Boost.Spirit
// ============================================================================
#include <boost/version.hpp>
#if BOOST_VERSION >= 103800
// FIXME: Move to the new boost::spirit::classic namespace
#if !defined(BOOST_SPIRIT_USE_OLD_NAMESPACE)
#define BOOST_SPIRIT_USE_OLD_NAMESPACE
#endif
#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/phoenix1.hpp>
#else
#include <boost/spirit.hpp>
#include <boost/spirit/phoenix.hpp>
#endif
#include <boost/bind.hpp>
// ============================================================================
/** @file
* Collection of grammars for property types
*
* @see Dd4hep::Parsers::parse
* @see Property
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-12
*/
// ============================================================================
namespace DD4hep
{
namespace Parsers
{
// ========================================================================
using namespace boost::spirit ;
// ========================================================================
using namespace phoenix ;
// ========================================================================
/** @struct ClosureGrammar
* Grammar or grammar rule which derive from this struct will have
* attribute of type <c>T</c> and name <c>val</c>
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template <typename T>
struct ClosureGrammar : public boost::spirit::closure < ClosureGrammar<T>,T >
{
typedef boost::spirit::closure<ClosureGrammar, T> closure;
typename closure::member1 val;
};
// ========================================================================
/** @struct AttributesClosureGrammar
*
* Grammar or grammar rule which derive from this struct will have
* two attributes: type <c>T1</c> and name <c>val</c>, type <c>T2</c>
* and name <c>attrs</c>
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template <typename T1,typename T2>
struct AttributesClosureGrammar
: public boost::spirit::closure<AttributesClosureGrammar<T1,T2>,T1,T2>
{
typedef boost::spirit::closure<AttributesClosureGrammar, T1,T2> closure;
typename closure::member1 val;
typename closure::member2 attrs;
};
// ========================================================================
/** @class BoolGrammar
*
* The valid represenation of boolean values are:
*
* - true , True , TRUE or 1
* - false , False , FALSE or 0
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
class BoolGrammar : public grammar
<
BoolGrammar,
ClosureGrammar<bool>::context_t
>
{
public:
typedef bool ResultT;
public:
template <typename ScannerT>
struct definition
{
definition( BoolGrammar const &self)
{
boolean_literal
= true_literal[self.val = true] | false_literal[self.val = false];
true_literal
= str_p("true" ) | str_p("True" ) | str_p("TRUE" ) | str_p("1");
false_literal
= str_p("false") | str_p("False") | str_p("FALSE") | str_p("0");
}
rule<ScannerT> const& start() const
{ return boolean_literal;}
rule<ScannerT> boolean_literal,true_literal,false_literal;
};
};
// ========================================================================
/** @class CharGrammar
*
* The valid represenation of char values are:
*
* - 'a', 'b','\''
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template<typename RT=char>
class CharGrammar : public grammar
<
CharGrammar<RT> , typename ClosureGrammar<RT>::context_t
>
{
public:
typedef RT ResultT;
public:
template <typename ScannerT>
struct definition
{
definition( CharGrammar<RT> const &self)
{
char_literal
= int_parser<RT>()[self.val=arg1]
| ('\''
>> ( str_p("\\'")[self.val='\'']
| (anychar_p[self.val=arg1]-'\'') )>>'\'');
}
rule<ScannerT> const& start() const
{ return char_literal; }
rule<ScannerT> char_literal;
};
};
// ========================================================================
/** @class IntGrammar
*
* The valid representation of integers values are:
*
* - 1, 100, 123
*
* @todo implement suffixes u U l L
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template<typename RT=int>
class IntGrammar : public grammar
<
IntGrammar<RT>,
typename ClosureGrammar<RT>::context_t
>
{
public:
typedef RT ResultT;
public:
template <typename ScannerT>
struct definition
{
definition( IntGrammar<RT> const &self)
{
int_literal = lexeme_d[int_parser<RT>()[self.val=arg1]
>> !(ch_p('u') | ch_p('U') | ch_p('l') | ch_p('L'))];
}
rule<ScannerT> const& start() const { return int_literal; }
rule<ScannerT> int_literal;
};
};
// ========================================================================
/** @class RealGrammar
*
* The valid represenation of real values are:
*
* - 1, 1.0 ,1.123, 1E+2, 0.5e-2
*
* @todo implement suffixes f l F L
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template<typename RT=double>
class RealGrammar : public grammar
<
RealGrammar<RT>,typename ClosureGrammar<RT>::context_t
>
{
public:
typedef RT ResultT;
public:
template <typename ScannerT>
struct definition
{
definition( RealGrammar const &self)
{
real_literal
= lexeme_d[real_parser<RT,
real_parser_policies<RT> >()[self.val = arg1]
>> !(ch_p('f') | ch_p('F') | ch_p('l') | ch_p('L'))];
}
rule<ScannerT> const& start() const
{ return real_literal; }
rule<ScannerT> real_literal;
};
};
// ========================================================================
/** @class StringGrammar
*
* The valid represenation of string values are:
*
* - "abc" , "\"abc\""
* - 'abs' , '\'abc\''
*
* @todo implement not ASCII chars in strings
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
class StringGrammar : public grammar
<
StringGrammar, ClosureGrammar<std::string>::context_t
>
{
public:
typedef std::string ResultT;
/** remove CR/LF symbols form the parsed strings
* @attention it is a bit dangerous operation
* The operation allows to write "very long" input strings
* for opts-files (it is actual e.g. for DataOnDemandSvc configuration)
* by splitting the strings into few lines
* All new-line symbols (as well as '\n', '\t', CR/LF etc
* are substituted by ordinary blanks.
*/
void matchString() const
{
for ( std::string::iterator cur=this->val().begin();
cur!=this->val().end();cur++)
{ if(std::isspace(*cur) ) { *cur = ' '; } }
}
public:
template <typename ScannerT>
struct definition
{
definition( StringGrammar const &self )
{
string_literal = (lexeme_d
[
('"' >> (*( str_p("\\\"")
|
(anychar_p-'"') ))
[self.val = construct_<std::string>
(arg1,arg2)] >>
'"')
|
('\'' >> (*( str_p("\\'")
|
(anychar_p-'\'') ))
[self.val = construct_<std::string>
(arg1,arg2)]>>
'\'')])[boost::bind(&StringGrammar::matchString,&self)];
}
rule<ScannerT> const& start() const { return string_literal; }
rule<ScannerT> string_literal;
};
};
// ========================================================================
/** @class SkipperGrammar
*
* Skipping spaces and comments. Comments can be
*
* - // ... - one line
* - \/\* ... \*\/ - multiline
*
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
class SkipperGrammar : public grammar<SkipperGrammar>
{
public:
/** Constructor
* @param skipnewline Skip new line symbols or not
*/
SkipperGrammar ( const bool skipnewline = true )
: m_skipnewline(skipnewline){}
public:
/// @return true - skip new line symbols, false - not skip
bool skipnewline() const{return m_skipnewline;}
public:
template <typename ScannerT>
struct definition
{
definition( SkipperGrammar const& self)
{
if ( self.skipnewline() )
{
skip
= space_p
| comment_p("//") // C++ comment
| comment_p("/*", "*/") // C comment
;
}
else
{
skip
= (space_p-eol_p)
| comment_p("//") // C++ comment
| comment_p("/*", "*/") // C comment
;
}
}
rule<ScannerT> skip;
rule<ScannerT> const& start() const { return skip; }
};
private:
bool m_skipnewline;
};
// ========================================================================
/** @class PairGrammar
*
* The valid represenation of pairs are:
* ("abc",123) or ("abc","def")
* Inner types of pair depends on KeyGrammarT and ValueGrammarT grammars
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template <typename KeyGrammarT, typename ValueGrammarT>
class PairGrammar : public grammar
<
PairGrammar<KeyGrammarT,ValueGrammarT>,
typename ClosureGrammar<
std::pair<typename KeyGrammarT::ResultT,
typename ValueGrammarT::ResultT> >::context_t
>
{
public:
typedef typename KeyGrammarT::ResultT KeyT;
typedef typename ValueGrammarT::ResultT ValueT;
typedef std::pair<KeyT,ValueT> ResultT;
public:
/** Constructor
* @param delim Delimiter for pair values
*/
PairGrammar ( const std::string& delim = "," )
: m_delim(delim) {}
public:
/// callback. Action when we match first value
void matchFirst ( const KeyT& first ) const { this->val().first = first; }
/// callback. Action when we match second value
void matchSecond ( const ValueT& second ) const { this->val().second = second; }
public:
template <typename ScannerT>
struct definition
{
definition( PairGrammar const &self)
{
para
= (
str_p("(")
>> (grkey[boost::bind(&PairGrammar::matchFirst,&self,_1)])
>> self.delim().c_str()
>> (grvalue[boost::bind(&PairGrammar::matchSecond,&self,_1)])
>> str_p(")")
) ;
}
rule<ScannerT> const& start() const { return para; }
rule<ScannerT> para;
KeyGrammarT grkey;
ValueGrammarT grvalue;
};
public:
/// @return Delimiter for pair values
const std::string& delim() const { return m_delim ; }
/** Set delimiters for pair values
* @param delim Delimiter
*/
void setDelim ( const std::string& delim ) { m_delim = delim;}
private:
std::string m_delim;
};
// ========================================================================
/** @class VectorGrammar
*
* The valid represenation of vector are:
* - {"abc","defj","i"} or {1,2,3,4,5}
* - ["abc","defj","i"] or [1,2,3,4,5]
* Inner type depends on GrammarT grammar
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template <typename GrammarT>
class VectorGrammar : public grammar
<
VectorGrammar<GrammarT> ,
typename ClosureGrammar<std::vector<typename GrammarT::ResultT> >::context_t
>
{
public:
typedef typename GrammarT::ResultT ValueT;
typedef std::vector<ValueT> ResultT;
typedef VectorGrammar<GrammarT> SelfT;
public:
/// callback. Action when we match inner value
void matchItem(const ValueT& value) const { this->val().push_back(value); }
public:
template <typename ScannerT>
struct definition
{
definition(SelfT const &self)
{
inner =
!(gr[boost::bind(&VectorGrammar::matchItem,&self,_1)]
>> *(','>>gr[boost::bind(&VectorGrammar::matchItem,&self,_1)]));
vec =
'[' >> inner >> ']' | // a'la python list
'(' >> inner >> ')' | // a'la python tuple
'{' >> inner >> '}' ; // like obsolete list from opts-grammar
}
rule<ScannerT> const& start() const { return vec; }
rule<ScannerT> vec,inner;
GrammarT gr;
};
};
// ========================================================================
/** @class MapGrammar
*
* The valid represenation of map are:
* - {"file1":"path1","something":"nothing"}
* - {"file1"="path1","something"="nothing"}
* - ["file1":10,"something":20]
* - ["file1"=30,"something"=40]
* Inner key type depends on KeyGrammarT grammar
* Inner value type depends on ValueGrammarT grammar
*
* @author Alexander MAZUROV Alexander.Mazurov@gmail.com
* @author Vanya BELYAEV ibelyaev@physics.syr.edu
* @date 2006-05-14
*/
template <typename KeyGrammarT, typename ValueGrammarT>
class MapGrammar : public grammar
<
MapGrammar<KeyGrammarT,ValueGrammarT>,
typename AttributesClosureGrammar
< std::map<typename KeyGrammarT::ResultT,
typename ValueGrammarT::ResultT>,
std::pair<typename KeyGrammarT::ResultT,
typename ValueGrammarT::ResultT> >::context_t
>
{
public:
typedef typename KeyGrammarT::ResultT KeyT;
typedef typename ValueGrammarT::ResultT ValueT;
typedef std::map<KeyT,ValueT> ResultT;
public:
/// call backs. Action when we match pair in map
void matchItem () const
{
//this->val().insert(this->attrs());
this->val()[this->attrs().first] = this->attrs().second ;
}
/// call backs. Action when we match key of pair
void matchFirst ( const KeyT& value ) const { this->attrs().first = value ; }
/// call backs. Action when we match value pf pair
void matchSecond( const ValueT& value ) const { this->attrs().second = value ; }
public:
template <typename ScannerT>
struct definition
{
definition( MapGrammar const &self)
{
vec
= ('{'>> inner_list >> '}') | ('['>>inner_list>>']');
inner_list
=
!( inner[boost::bind(&MapGrammar::matchItem,&self)]
>> *( ch_p(',') >>
inner[boost::bind(&MapGrammar::matchItem,&self)] )
);
inner
=
grKey[boost ::bind(&MapGrammar::matchFirst,&self,_1)]
>> ( ch_p('=') | ch_p(':'))
>> grValue[boost::bind(&MapGrammar::matchSecond,&self,_1)] ;
}
KeyGrammarT grKey;
ValueGrammarT grValue;
rule<ScannerT> const& start() const { return vec; }
rule<ScannerT> vec,inner, inner_list ;
};
};
// ========================================================================
} // end of namespace Dd4hep::Parsers
} // end of namespace Dd4hep
// ============================================================================
// The END
// ============================================================================
#endif // DD4HEPKERNEL_GRAMMARS_H
// ============================================================================