CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/HLTrigger/HLTcore/interface/TriggerExpressionParser.h

Go to the documentation of this file.
00001 #ifndef HLTrigger_HLTfilters_TriggerExpressionParser_h
00002 #define HLTrigger_HLTfilters_TriggerExpressionParser_h
00003 
00004 // Note: this requires Boost 1.41 or higher, for Spirit 2.1 or higher
00005 #include <boost/spirit/include/phoenix.hpp>
00006 #include <boost/spirit/include/qi.hpp>
00007 
00008 #include "HLTrigger/HLTcore/interface/TriggerExpressionHLTReader.h"
00009 #include "HLTrigger/HLTcore/interface/TriggerExpressionL1Reader.h"
00010 #include "HLTrigger/HLTcore/interface/TriggerExpressionL1TechReader.h"
00011 #include "HLTrigger/HLTcore/interface/TriggerExpressionOperators.h"
00012 #include "HLTrigger/HLTcore/interface/TriggerExpressionPrescaler.h"
00013 #include "HLTrigger/HLTcore/interface/TriggerExpressionConstant.h"
00014 
00015 namespace triggerExpression {
00016 
00017 namespace qi = boost::spirit::qi;
00018 namespace ascii = boost::spirit::ascii;
00019 
00020 using boost::phoenix::new_;
00021 using boost::spirit::unused_type;
00022 
00023 template <typename Iterator>
00024 class Parser : public qi::grammar<Iterator, Evaluator*(), ascii::space_type>
00025 {
00026 public:
00027   Parser() : 
00028     Parser::base_type(expression)
00029   {
00030     token_hlt       %= qi::raw[qi::lexeme["HLT_"    >> +(qi::char_("a-zA-Z0-9_*?"))]];
00031     token_alca      %= qi::raw[qi::lexeme["AlCa_"   >> +(qi::char_("a-zA-Z0-9_*?"))]];
00032     token_l1        %= qi::raw[qi::lexeme["L1_"     >> +(qi::char_("a-zA-Z0-9_*?"))]];
00033     token_l1tech    %= qi::raw[qi::lexeme["L1Tech_" >> +(qi::char_("a-zA-Z0-9_*?"))]];
00034 
00035     token            = ( token_hlt                      [qi::_val = new_<HLTReader>(qi::_1)]
00036                        | token_alca                     [qi::_val = new_<HLTReader>(qi::_1)]
00037                        | token_l1                       [qi::_val = new_<L1Reader>(qi::_1)]
00038                        | token_l1tech                   [qi::_val = new_<L1TechReader>(qi::_1)]
00039                        | qi::lit("TRUE")                [qi::_val = new_<Constant>(true)]
00040                        | qi::lit("FALSE")               [qi::_val = new_<Constant>(false)]
00041                        );
00042 
00043     parenthesis     %= ('(' >> expression >> ')');
00044 
00045     element         %= (token | parenthesis);
00046 
00047     prescale         = (element >> '/' >> qi::uint_)    [qi::_val = new_<Prescaler> (qi::_1, qi::_2)];
00048 
00049     operand         %= (prescale | element);
00050 
00051     unary            = ( operand                        [qi::_val = qi::_1]
00052                        | (qi::lit("NOT") >> operand)    [qi::_val = new_<OperatorNot> (qi::_1)]
00053                        );
00054 
00055     expression       = unary                            [qi::_val = qi::_1] 
00056                        >> *(
00057                               (qi::lit("AND") >> unary) [qi::_val = new_<OperatorAnd> (qi::_val, qi::_1)] 
00058                             | (qi::lit("OR")  >> unary) [qi::_val = new_<OperatorOr>  (qi::_val, qi::_1)] 
00059                        );
00060   }
00061 
00062 private:
00063   typedef qi::rule<Iterator, unused_type(), ascii::space_type> void_rule;
00064   typedef qi::rule<Iterator, std::string(), ascii::space_type> name_rule;
00065   typedef qi::rule<Iterator, Evaluator*(),  ascii::space_type> rule;
00066 
00067   name_rule token_hlt;
00068   name_rule token_alca;
00069   name_rule token_l1;
00070   name_rule token_l1tech;
00071 
00072   rule token;
00073   rule parenthesis;
00074   rule element;
00075   rule prescale;
00076   rule operand;
00077   rule unary;
00078   rule expression;
00079 };
00080 
00081 
00082 // generic interface for string-like objects
00083 template <class T>
00084 Evaluator * parse(const T & text) {
00085   typedef typename T::const_iterator Iterator;
00086   Parser<Iterator> parser;
00087   Evaluator * evaluator = 0;
00088 
00089   Iterator begin = text.begin();
00090   Iterator end   = text.end();
00091 
00092   // the interface of qi::phrase_parse has changed between Boost 1.40 (Spirit 2.0) and Boost 1.41 (Spirit 2.1)
00093   bool result = qi::phrase_parse( begin, end, parser, ascii::space, evaluator );
00094 
00095   if (not result or begin != end) {
00096     delete evaluator;
00097     return 0;
00098   }
00099 
00100   return evaluator;
00101 }
00102 
00103 // overloaded interface for null-terminated strings
00104 inline Evaluator * parse(const char * text) {
00105   Parser<const char *> parser;
00106   Evaluator * evaluator = 0;
00107 
00108   const char * begin = text;
00109   const char * end   = text + strlen(text);
00110 
00111   // the interface of qi::phrase_parse has changed between Boost 1.40 (Spirit 2.0) and Boost 1.41 (Spirit 2.1)
00112   bool result = qi::phrase_parse( begin, end, parser, ascii::space, evaluator );
00113 
00114   if (not result or begin != end) {
00115     delete evaluator;
00116     return 0;
00117   }
00118 
00119   return evaluator;
00120 }
00121 
00122 } // namespace triggerExpression
00123 
00124 #endif // HLTrigger_HLTfilters_TriggerExpressionParser_h