CMS 3D CMS Logo

TriggerExpressionParser.h
Go to the documentation of this file.
1 #ifndef HLTrigger_HLTfilters_TriggerExpressionParser_h
2 #define HLTrigger_HLTfilters_TriggerExpressionParser_h
3 
4 // Note: this requires Boost 1.41 or higher, for Spirit 2.1 or higher
5 #include <boost/spirit/include/phoenix.hpp>
6 #include <boost/spirit/include/qi.hpp>
7 
13 
14 namespace triggerExpression {
15 
16  namespace qi = boost::spirit::qi;
17  namespace ascii = boost::spirit::ascii;
18 
19  using boost::phoenix::new_;
20  using boost::spirit::unused_type;
21 
22  template <typename Iterator>
23  class Parser : public qi::grammar<Iterator, Evaluator *(), ascii::space_type> {
24  public:
25  Parser() : Parser::base_type(expression) {
26  token_l1algo %= qi::raw[qi::lexeme["L1_" >> +(qi::char_("a-zA-Z0-9_*?"))]];
27  token_path %= qi::raw[qi::lexeme[+(qi::char_("a-zA-Z0-9_*?"))]];
28 
29  token = (qi::lit("TRUE")[qi::_val = new_<Constant>(true)] | qi::lit("FALSE")[qi::_val = new_<Constant>(false)] |
30  token_l1algo[qi::_val = new_<L1uGTReader>(qi::_1)] | token_path[qi::_val = new_<PathReader>(qi::_1)]);
31 
32  parenthesis %= ('(' >> expression >> ')');
33 
34  element %= (token | parenthesis);
35 
36  prescale = (element >> '/' >> qi::uint_)[qi::_val = new_<Prescaler>(qi::_1, qi::_2)];
37 
38  operand %= (prescale | element);
39 
40  unary = (operand[qi::_val = qi::_1] | (qi::lit("NOT") >> operand)[qi::_val = new_<OperatorNot>(qi::_1)]);
41 
42  expression =
43  unary[qi::_val = qi::_1] >> *((qi::lit("AND") >> unary)[qi::_val = new_<OperatorAnd>(qi::_val, qi::_1)] |
44  (qi::lit("OR") >> unary)[qi::_val = new_<OperatorOr>(qi::_val, qi::_1)]);
45  }
46 
47  private:
48  typedef qi::rule<Iterator, std::string(), ascii::space_type> name_rule;
49  typedef qi::rule<Iterator, Evaluator *(), ascii::space_type> rule;
50 
53 
61  };
62 
63  // generic interface for string-like objects
64  template <class T>
65  Evaluator *parse(const T &text) {
66  typedef typename T::const_iterator Iterator;
68  Evaluator *evaluator = nullptr;
69 
70  Iterator begin = text.begin();
71  Iterator end = text.end();
72 
73  // the interface of qi::phrase_parse has changed between Boost 1.40 (Spirit 2.0) and Boost 1.41 (Spirit 2.1)
74  bool result = qi::phrase_parse(begin, end, parser, ascii::space, evaluator);
75 
76  if (not result or begin != end) {
77  delete evaluator;
78  return nullptr;
79  }
80 
81  return evaluator;
82  }
83 
84  // overloaded interface for null-terminated strings
85  inline Evaluator *parse(const char *text) {
87  Evaluator *evaluator = nullptr;
88 
89  const char *begin = text;
90  const char *end = text + strlen(text);
91 
92  // the interface of qi::phrase_parse has changed between Boost 1.40 (Spirit 2.0) and Boost 1.41 (Spirit 2.1)
93  bool result = qi::phrase_parse(begin, end, parser, ascii::space, evaluator);
94 
95  if (not result or begin != end) {
96  delete evaluator;
97  return nullptr;
98  }
99 
100  return evaluator;
101  }
102 
103 } // namespace triggerExpression
104 
105 #endif // HLTrigger_HLTfilters_TriggerExpressionParser_h
Evaluator * parse(const T &text)
TGeoIterator Iterator
qi::rule< Iterator, Evaluator *(), ascii::space_type > rule
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
qi::rule< Iterator, std::string(), ascii::space_type > name_rule
long double T
dd4hep::tools::Evaluator & evaluator()