File indexing completed on 2025-01-18 09:14:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef PARSERS_SPIRIT_GRAMMARSV2_H
0014 #define PARSERS_SPIRIT_GRAMMARSV2_H 1
0015 #ifdef __GNUC__
0016 #pragma GCC system_header
0017 #endif
0018
0019
0020
0021
0022
0023 #include <string>
0024 #include <vector>
0025 #include <deque>
0026 #include <list>
0027 #include <set>
0028 #include <map>
0029
0030
0031
0032 #include <boost/spirit/include/qi.hpp>
0033 #include <boost/fusion/include/unused.hpp>
0034 #include <boost/fusion/include/std_pair.hpp>
0035
0036
0037 #if BOOST_VERSION >= 107800
0038
0039 #include <boost/phoenix/core.hpp>
0040 #include <boost/phoenix/operator.hpp>
0041
0042 #else
0043
0044 #include <boost/spirit/include/phoenix_core.hpp>
0045 #include <boost/spirit/include/phoenix_operator.hpp>
0046
0047 #endif
0048
0049 #include <boost/utility/enable_if.hpp>
0050 #include <boost/type_traits.hpp>
0051
0052 #include <boost/spirit/repository/include/qi_confix.hpp>
0053
0054
0055 namespace dd4hep { namespace Parsers {
0056
0057
0058
0059 namespace sp = boost::spirit;
0060 namespace ph = boost::phoenix;
0061 namespace qi = sp::qi;
0062 namespace enc = sp::ascii;
0063 namespace rep = sp::repository;
0064
0065 template <typename T> T evaluate_string(const std::string& value);
0066
0067
0068
0069
0070 typedef std::string::const_iterator DefaultIterator;
0071 typedef enc::space_type DefaultSkipper;
0072
0073 template <typename Iterator, typename T, typename Skipper, class Enable=void>
0074 struct Grammar_ {
0075
0076
0077
0078
0079
0080
0081 BOOST_MPL_ASSERT_MSG(false, GRAMMAR_FOR_TYPE_DOES_NOT_EXISTS, (T));
0082 };
0083
0084 #define REGISTER_GRAMMAR(ResultType, GrammarName) \
0085 template <typename Iterator, typename Skipper> \
0086 struct Grammar_<Iterator, ResultType, Skipper> \
0087 { \
0088 typedef GrammarName<Iterator, Skipper> Grammar; \
0089 }
0090
0091 template< typename Iterator> struct SkipperGrammar : qi::grammar<Iterator>
0092 {
0093 SkipperGrammar() : SkipperGrammar::base_type(comments) {
0094 comments = enc::space | rep::confix("/*", "*/")[*(qi::char_ - "*/")]
0095 |
0096 rep::confix("//", (sp::eol | sp::eoi))[*(qi::char_ - (sp::eol|sp::eoi))];
0097 }
0098 qi::rule<Iterator> comments;
0099 };
0100
0101 template< typename Iterator, typename Skipper>
0102 struct StringGrammar : qi::grammar<Iterator, std::string(), qi::locals<char>,Skipper>
0103 {
0104
0105 typedef std::string ResultT;
0106
0107 StringGrammar() : StringGrammar::base_type( str ) {
0108 begin_quote = enc::char_("\"'");
0109 quote = enc::char_(qi::_r1);
0110
0111 str = qi::lexeme[begin_quote[qi::_a = qi::_1]
0112 > *( (enc::char_('\\') >> quote(qi::_a))[qi::_val += qi::_a]
0113 | (enc::char_[qi::_val += qi::_1] - quote(qi::_a))) >
0114 quote(qi::_a)];
0115 }
0116
0117 qi::rule<Iterator, std::string(), qi::locals<char>, Skipper> str;
0118 qi::rule<Iterator, char()> begin_quote;
0119 qi::rule<Iterator, void(char)> quote;
0120
0121 };
0122 REGISTER_GRAMMAR(std::string, StringGrammar);
0123
0124 template< typename Iterator, typename Skipper>
0125 struct CharGrammar : qi::grammar<Iterator, char(), Skipper> {
0126 typedef char ResultT;
0127 CharGrammar() : CharGrammar::base_type( ch ) {
0128 ch = qi::int_parser<char>()
0129 |
0130 '\'' >> (qi::char_-'\'') >> '\'';
0131 }
0132 qi::rule<Iterator, char(), Skipper> ch;
0133 };
0134 REGISTER_GRAMMAR(char, CharGrammar);
0135
0136 template< typename Iterator, typename Skipper>
0137 struct BoolGrammar : qi::grammar<Iterator, bool(), Skipper>
0138 {
0139 typedef bool ResultT;
0140 BoolGrammar() : BoolGrammar::base_type( boolean_literal ) {
0141 boolean_literal =
0142 (qi::lit("true") | qi::lit("True") | qi::lit("TRUE") | "1")[qi::_val=true]
0143 |
0144 (qi::lit("false") | qi::lit("False") | qi::lit("FALSE") | "0")[qi::_val=false];
0145 }
0146 qi::rule<Iterator, bool(), Skipper> boolean_literal;
0147 };
0148 REGISTER_GRAMMAR(bool, BoolGrammar);
0149
0150 template< typename Iterator, typename RT , typename Skipper>
0151 struct IntGrammar : qi::grammar<Iterator, RT(), Skipper> {
0152 typedef RT ResultT;
0153 IntGrammar() : IntGrammar::base_type( integer ) {
0154 integer = qi::int_parser<RT>()[qi::_val = qi::_1]
0155 >> -qi::no_case[qi::char_('L')];
0156 }
0157 qi::rule<Iterator, RT(), Skipper> integer;
0158 };
0159
0160
0161
0162 template <typename Iterator, typename T, typename Skipper>
0163 struct Grammar_<Iterator, T, Skipper,
0164 typename boost::enable_if<boost::is_integral<T> >::type>
0165 {
0166 typedef IntGrammar<Iterator, T, Skipper> Grammar;
0167 };
0168
0169 template< typename Iterator, typename RT, typename Skipper>
0170 struct RealGrammar : qi::grammar<Iterator, RT(), Skipper> {
0171 typedef RT ResultT;
0172 RealGrammar() : RealGrammar::base_type(real) {
0173 real = qi::real_parser<RT>();
0174 }
0175 qi::rule<Iterator, RT(), Skipper> real;
0176 };
0177
0178
0179
0180 template <typename Iterator, typename T, typename Skipper >
0181 struct Grammar_<Iterator, T, Skipper,
0182 typename boost::enable_if<boost::is_floating_point<T> >::type > {
0183 typedef RealGrammar<Iterator, T, Skipper> Grammar;
0184 };
0185
0186 template< typename Iterator, typename VectorT, typename Skipper>
0187 struct VectorGrammar : qi::grammar<Iterator, VectorT(), qi::locals<char>,Skipper>
0188 {
0189
0190 typedef VectorT ResultT;
0191
0192 VectorGrammar() : VectorGrammar::base_type(vec) {
0193 begin = enc::char_('[')[qi::_val=']'] | enc::char_('{')[qi::_val='}']
0194 | enc::char_('(')[qi::_val=')'];
0195 end = enc::char_(qi::_r1);
0196 list = elementGrammar % ',';
0197 vec = begin[qi::_a = qi::_1] >> -list[qi::_val=qi::_1] >> end(qi::_a);
0198 }
0199
0200 typename
0201 Grammar_<Iterator, typename VectorT::value_type, Skipper>::Grammar
0202 elementGrammar;
0203 qi::rule<Iterator, char()> begin;
0204 qi::rule<Iterator, void(char)> end;
0205
0206 qi::rule<Iterator, ResultT(), qi::locals<char>,Skipper> vec;
0207 qi::rule<Iterator, ResultT(), Skipper> list;
0208
0209 };
0210
0211
0212
0213 template <typename Iterator,typename InnerT,typename AllocatorT,typename Skipper>
0214 struct Grammar_<Iterator, std::vector<InnerT, AllocatorT>, Skipper > {
0215 typedef
0216 VectorGrammar<Iterator, std::vector<InnerT, AllocatorT>, Skipper>
0217 Grammar;
0218 };
0219
0220
0221
0222 template <typename Iterator, typename InnerT, typename AllocatorT,typename Skipper>
0223 struct Grammar_<Iterator, std::list<InnerT, AllocatorT>, Skipper > {
0224 typedef
0225 VectorGrammar<Iterator, std::list<InnerT, AllocatorT>, Skipper>
0226 Grammar;
0227 };
0228
0229
0230
0231 template <typename Iterator, typename InnerT, typename CompareT, typename AllocatorT, typename Skipper>
0232 struct Grammar_<Iterator, std::set<InnerT, CompareT, AllocatorT>, Skipper > {
0233 typedef
0234 VectorGrammar<Iterator, std::set<InnerT, CompareT, AllocatorT>, Skipper>
0235 Grammar;
0236 };
0237 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0238
0239
0240
0241 template <typename Iterator, typename InnerT, typename AllocatorT, typename Skipper>
0242 struct Grammar_<Iterator, std::deque<InnerT, AllocatorT>, Skipper > {
0243 typedef
0244 VectorGrammar<Iterator, std::deque<InnerT, AllocatorT>, Skipper>
0245 Grammar;
0246 };
0247 #endif
0248
0249
0250 template< typename Iterator, typename PairT, typename Skipper>
0251 struct PairGrammar :
0252 qi::grammar<Iterator,PairT(), qi::locals<char>, Skipper> {
0253
0254 typedef PairT ResultT;
0255 typedef typename PairT::first_type first_type;
0256 typedef typename PairT::second_type second_type;
0257
0258 PairGrammar() : PairGrammar::base_type(pair) {
0259 init(",");
0260 }
0261
0262 PairGrammar(const std::string& delimeter) : PairGrammar::base_type(pair) {
0263 init(delimeter);
0264 }
0265
0266 struct first {};
0267 struct second {};
0268 void init(const std::string& delimeter) {
0269 begin = enc::char_('(')[qi::_val=')']
0270 |
0271 enc::char_('[')[qi::_val=']'];
0272 end = qi::char_(qi::_r1);
0273 pair = begin[qi::_a = qi::_1] >> pair_in[qi::_val = qi::_1] >> end(qi::_a);
0274 pair_in = key >> qi::lit(delimeter) >> value;
0275 }
0276
0277 typename
0278 Grammar_<Iterator, typename PairT::first_type, Skipper>::Grammar key;
0279 typename
0280 Grammar_<Iterator, typename PairT::second_type, Skipper>::Grammar
0281 value;
0282 qi::rule<Iterator, char()> begin;
0283 qi::rule<Iterator, void(char)> end;
0284 qi::rule<Iterator, ResultT(), qi::locals<char>, Skipper> pair;
0285 qi::rule<Iterator, ResultT(), Skipper> pair_in;
0286
0287
0288 };
0289
0290
0291
0292
0293 template <typename Iterator, typename KeyT, typename ValueT,
0294 typename Skipper>
0295 struct Grammar_<Iterator, std::pair<KeyT, ValueT>, Skipper >
0296 {
0297 typedef PairGrammar<Iterator, std::pair<KeyT, ValueT>, Skipper> Grammar;
0298 };
0299
0300
0301 template< typename Iterator, typename MapT, typename Skipper>
0302 struct MapGrammar : qi::grammar<Iterator,MapT(), Skipper> {
0303
0304 typedef MapT ResultT;
0305 typedef typename MapT::key_type KeyT;
0306 typedef typename MapT::mapped_type MappedT;
0307 typedef std::pair<KeyT, MappedT> PairT;
0308
0309 typedef std::vector<PairT> VectorPairT;
0310
0311 struct tag_key{};
0312 struct tag_mapped{};
0313 struct Operations {
0314 template <typename A, typename B = boost::fusion::unused_type,
0315 typename C = boost::fusion::unused_type,
0316 typename D = boost::fusion::unused_type>
0317 struct result { typedef void type; };
0318
0319 void operator()(ResultT& res, const VectorPairT& vec) const{
0320 for(typename VectorPairT::const_iterator cur = vec.begin();
0321 cur != vec.end(); cur++){
0322 res.insert(*cur);
0323 }
0324 }
0325 void operator()(PairT& res, const KeyT& key, tag_key) const{
0326 res.first = key;
0327 }
0328 void operator()(PairT& res, const MappedT& value, tag_mapped) const{
0329 res.second = value;
0330 }
0331
0332 };
0333
0334 MapGrammar() : MapGrammar::base_type(map) {
0335 pair = key[op(qi::_val,qi::_1, tag_key())] > (qi::lit(':') | '=') >
0336 value[op(qi::_val,qi::_1, tag_mapped())];
0337 list = -(pair % enc::char_(','));
0338 map = (('[' >> list >> ']')
0339 | ('{' >> list >> '}'))[op(qi::_val,qi::_1)];
0340 }
0341
0342 typename
0343 Grammar_<Iterator, typename MapT::key_type, Skipper>::Grammar key;
0344 typename
0345 Grammar_<Iterator, typename MapT::mapped_type, Skipper>::Grammar value;
0346 qi::rule<Iterator, PairT(), Skipper> pair;
0347 qi::rule<Iterator, VectorPairT(), Skipper> list;
0348 qi::rule<Iterator, ResultT(), Skipper> map;
0349 ph::function<Operations> op;
0350
0351 };
0352
0353
0354
0355 template <typename Iterator, typename KeyT, typename ValueT,
0356 typename KeyCompareT, typename AllocatorT, typename Skipper>
0357 struct Grammar_<Iterator, std::map<KeyT, ValueT, KeyCompareT, AllocatorT>,
0358 Skipper > {
0359 typedef MapGrammar<Iterator,
0360 std::map<KeyT, ValueT, KeyCompareT, AllocatorT>, Skipper> Grammar;
0361 };
0362
0363
0364 template< typename Iterator, typename Skipper>
0365 struct KeyValueGrammar :
0366 qi::grammar<Iterator, std::pair<std::string, std::string>(), Skipper> {
0367
0368 typedef std::pair<std::string, std::string> ResultT;
0369
0370 struct first {};
0371 struct second {};
0372
0373 KeyValueGrammar() : KeyValueGrammar::base_type(pair) {
0374
0375 pair = gstring >> ":" >> +enc::char_;
0376 }
0377
0378 StringGrammar<Iterator, Skipper> gstring;
0379 qi::rule<Iterator, ResultT(), Skipper> pair;
0380
0381 };
0382
0383
0384 }}
0385
0386
0387
0388 #ifndef DD4HEP_PARSERS_NO_ROOT
0389 #include "Math/Point3D.h"
0390 #include "Math/Vector3D.h"
0391 #include "Math/Vector4D.h"
0392 #include "Math/RotationZYX.h"
0393
0394 namespace dd4hep { namespace Parsers {
0395
0396
0397 template< typename Iterator, typename PointT, typename Skipper>
0398 struct Pnt3DGrammar : qi::grammar<Iterator, PointT(), Skipper> {
0399 typedef PointT ResultT;
0400 typedef std::string Scalar;
0401
0402 struct Operations {
0403 template <typename A, typename B = boost::fusion::unused_type,
0404 typename C = boost::fusion::unused_type,
0405 typename D = boost::fusion::unused_type>
0406 struct result { typedef void type; };
0407 void operator()(ResultT& res, const Scalar& value,const char xyz) const{
0408 typename PointT::Scalar val = evaluate_string<typename PointT::Scalar>(value);
0409 switch(xyz) {
0410 case 'x': res.SetX(val); break;
0411 case 'y': res.SetY(val); break;
0412 case 'z': res.SetZ(val); break;
0413 default: break;
0414 }
0415 }
0416 };
0417
0418 Pnt3DGrammar() : Pnt3DGrammar::base_type(point) {
0419 point = list | ('(' >> list >> ')') | ('[' >> list >> ']');
0420 list = -(enc::no_case[qi::lit("x") | qi::lit("px")] >> ':')
0421 >> scalar[op(qi::_val,qi::_1,'x')] >>
0422 ',' >> -(enc::no_case[qi::lit("y") | qi::lit("py")] >> ':')
0423 >> scalar[op(qi::_val,qi::_1,'y')] >>
0424 ',' >> -(enc::no_case[qi::lit("z") | qi::lit("pz")] >> ':')
0425 >> scalar[op(qi::_val,qi::_1,'z')];
0426 }
0427
0428 qi::rule<Iterator, ResultT(), Skipper> point, list;
0429 typename Grammar_<Iterator, Scalar, Skipper>::Grammar scalar;
0430 ph::function<Operations> op;
0431
0432 };
0433
0434
0435
0436 template <typename Iterator, typename T1, typename T2, typename Skipper>
0437 struct Grammar_<Iterator, ROOT::Math::PositionVector3D<T1,T2>, Skipper>{
0438 typedef Pnt3DGrammar<Iterator, ROOT::Math::PositionVector3D<T1,T2>, Skipper> Grammar;
0439 };
0440
0441
0442
0443 template <typename Iterator, typename T1, typename T2, typename Skipper>
0444 struct Grammar_<Iterator, ROOT::Math::DisplacementVector3D<T1,T2>, Skipper>{
0445 typedef Pnt3DGrammar<Iterator,ROOT::Math::DisplacementVector3D<T1,T2>, Skipper> Grammar;
0446 };
0447
0448 template< typename Iterator, typename PointT, typename Skipper>
0449 struct Pnt4DGrammar : qi::grammar<Iterator, PointT(), Skipper> {
0450 typedef PointT ResultT;
0451 typedef std::string ScalarT;
0452
0453 struct Operations {
0454 template <typename A, typename B = boost::fusion::unused_type,
0455 typename C = boost::fusion::unused_type,
0456 typename D = boost::fusion::unused_type>
0457 struct result { typedef void type; };
0458
0459 void operator()(ResultT& res, const ScalarT& value,const char xyz) const{
0460 typename PointT::Scalar val = evaluate_string<typename PointT::Scalar>(value);
0461 switch(xyz){
0462 case 'x': res.SetPx(val); break;
0463 case 'y': res.SetPy(val); break;
0464 case 'z': res.SetPz(val); break;
0465 case 'e': res.SetE(val); break;
0466 default: break;
0467 }
0468 }
0469 void operator()(ResultT& res, const ResultT& xyz) const{
0470 res.SetPx(xyz.Px());
0471 res.SetPy(xyz.Py());
0472 res.SetPz(xyz.Pz());
0473 }
0474 };
0475
0476 Pnt4DGrammar() : Pnt4DGrammar::base_type(point4d) {
0477 point4d = list4d | ('(' >> list4d >> ')') | ('[' >> list4d >> ']');
0478 list4d = (point3d[op(qi::_val,qi::_1)] >> enc::char_(";,")
0479 >> e[op(qi::_val, qi::_1, 'e')])
0480 |
0481 (e[op(qi::_val,qi::_1, 'e')] >> enc::char_(";,")
0482 >> point3d[op(qi::_val, qi::_1)]);
0483 e = -(enc::no_case[enc::char_("te")] >> ':')
0484 >> scalar[qi::_val = qi::_1];
0485
0486 point3d = list3d | ('(' >> list3d >> ')') | ('[' >> list3d >> ']');
0487 list3d = -(enc::no_case[qi::lit("x") | qi::lit("px")] >> ':')
0488 >> scalar[op(qi::_val, qi::_1,'x')] >>
0489 ',' >> -(enc::no_case[qi::lit("y") | qi::lit("py")] >> ':')
0490 >> scalar[op(qi::_val, qi::_1,'y')] >>
0491 ',' >> -(enc::no_case[qi::lit("z") | qi::lit("pz")] >> ':')
0492 >> scalar[op(qi::_val, qi::_1,'z')];
0493 }
0494
0495 qi::rule<Iterator, ResultT(), Skipper> point3d, point4d, list3d,
0496 list4d;
0497 qi::rule<Iterator, ScalarT(), Skipper> e;
0498 typename Grammar_<Iterator, ScalarT, Skipper>::Grammar scalar;
0499 ph::function<Operations> op;
0500
0501 };
0502
0503
0504
0505 template <typename Iterator, typename T1, typename Skipper>
0506 struct Grammar_<Iterator, ROOT::Math::LorentzVector<T1>, Skipper > {
0507 typedef Pnt4DGrammar<Iterator, ROOT::Math::LorentzVector<T1>, Skipper> Grammar;
0508 };
0509
0510 template< typename Iterator, typename PointT, typename Skipper>
0511 struct Rot3DGrammar : qi::grammar<Iterator, PointT(), Skipper> {
0512 typedef PointT ResultT;
0513 typedef std::string Scalar;
0514
0515 struct Operations {
0516 template <typename A, typename B = boost::fusion::unused_type,
0517 typename C = boost::fusion::unused_type,
0518 typename D = boost::fusion::unused_type>
0519 struct result { typedef void type; };
0520 void operator()(ResultT& res, const Scalar& value,const char xyz) const{
0521 typename PointT::Scalar val = evaluate_string<typename PointT::Scalar>(value);
0522 switch(xyz) {
0523 case 'x': res.SetPsi(val); break;
0524 case 'y': res.SetPhi(val); break;
0525 case 'z': res.SetTheta(val); break;
0526 default: break;
0527 }
0528 }
0529 };
0530
0531 Rot3DGrammar() : Rot3DGrammar::base_type(point) {
0532 point = list | ('(' >> list >> ')') | ('[' >> list >> ']');
0533 list = -(enc::no_case[qi::lit("x")] >> ':')
0534 >> scalar[op(qi::_val,qi::_1,'x')] >>
0535 ',' >> -(enc::no_case[qi::lit("y")] >> ':')
0536 >> scalar[op(qi::_val,qi::_1,'y')] >>
0537 ',' >> -(enc::no_case[qi::lit("z")] >> ':')
0538 >> scalar[op(qi::_val,qi::_1,'z')];
0539 }
0540
0541 qi::rule<Iterator, ResultT(), Skipper> point, list;
0542 typename Grammar_<Iterator, Scalar, Skipper>::Grammar scalar;
0543 ph::function<Operations> op;
0544
0545 };
0546
0547
0548
0549 template <typename Iterator, typename Skipper>
0550 struct Grammar_<Iterator, ROOT::Math::RotationZYX, Skipper>{
0551 typedef Rot3DGrammar<Iterator, ROOT::Math::RotationZYX, Skipper> Grammar;
0552 };
0553
0554
0555 }}
0556 #endif
0557
0558 #endif