CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/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_dqm       %= qi::raw[qi::lexeme["DQM_"    >> +(qi::char_("a-zA-Z0-9_*?"))]];
00033     token_dst       %= qi::raw[qi::lexeme["DST_"    >> +(qi::char_("a-zA-Z0-9_*?"))]];
00034     token_l1        %= qi::raw[qi::lexeme["L1_"     >> +(qi::char_("a-zA-Z0-9_*?"))]];
00035     token_l1tech    %= qi::raw[qi::lexeme["L1Tech_" >> +(qi::char_("a-zA-Z0-9_*?"))]];
00036 
00037     token            = ( token_hlt                      [qi::_val = new_<HLTReader>(qi::_1)]
00038                        | token_alca                     [qi::_val = new_<HLTReader>(qi::_1)]
00039                        | token_dqm                      [qi::_val = new_<HLTReader>(qi::_1)]
00040                        | token_dst                      [qi::_val = new_<HLTReader>(qi::_1)]
00041                        | token_l1                       [qi::_val = new_<L1Reader>(qi::_1)]
00042                        | token_l1tech                   [qi::_val = new_<L1TechReader>(qi::_1)]
00043                        | qi::lit("TRUE")                [qi::_val = new_<Constant>(true)]
00044                        | qi::lit("FALSE")               [qi::_val = new_<Constant>(false)]
00045                        );
00046 
00047     parenthesis     %= ('(' >> expression >> ')');
00048 
00049     element         %= (token | parenthesis);
00050 
00051     prescale         = (element >> '/' >> qi::uint_)    [qi::_val = new_<Prescaler> (qi::_1, qi::_2)];
00052 
00053     operand         %= (prescale | element);
00054 
00055     unary            = ( operand                        [qi::_val = qi::_1]
00056                        | (qi::lit("NOT") >> operand)    [qi::_val = new_<OperatorNot> (qi::_1)]
00057                        );
00058 
00059     expression       = unary                            [qi::_val = qi::_1] 
00060                        >> *(
00061                               (qi::lit("AND") >> unary) [qi::_val = new_<OperatorAnd> (qi::_val, qi::_1)] 
00062                             | (qi::lit("OR")  >> unary) [qi::_val = new_<OperatorOr>  (qi::_val, qi::_1)] 
00063                        );
00064   }
00065 
00066 private:
00067   typedef qi::rule<Iterator, unused_type(), ascii::space_type> void_rule;
00068   typedef qi::rule<Iterator, std::string(), ascii::space_type> name_rule;
00069   typedef qi::rule<Iterator, Evaluator*(),  ascii::space_type> rule;
00070 
00071   name_rule token_hlt;
00072   name_rule token_alca;
00073   name_rule token_dqm;
00074   name_rule token_dst;
00075   name_rule token_l1;
00076   name_rule token_l1tech;
00077 
00078   rule token;
00079   rule parenthesis;
00080   rule element;
00081   rule prescale;
00082   rule operand;
00083   rule unary;
00084   rule expression;
00085 };
00086 
00087 
00088 // generic interface for string-like objects
00089 template <class T>
00090 Evaluator * parse(const T & text) {
00091   typedef typename T::const_iterator Iterator;
00092   Parser<Iterator> parser;
00093   Evaluator * evaluator = 0;
00094 
00095   Iterator begin = text.begin();
00096   Iterator end   = text.end();
00097 
00098   // the interface of qi::phrase_parse has changed between Boost 1.40 (Spirit 2.0) and Boost 1.41 (Spirit 2.1)
00099   bool result = qi::phrase_parse( begin, end, parser, ascii::space, evaluator );
00100 
00101   if (not result or begin != end) {
00102     delete evaluator;
00103     return 0;
00104   }
00105 
00106   return evaluator;
00107 }
00108 
00109 // overloaded interface for null-terminated strings
00110 inline Evaluator * parse(const char * text) {
00111   Parser<const char *> parser;
00112   Evaluator * evaluator = 0;
00113 
00114   const char * begin = text;
00115   const char * end   = text + strlen(text);
00116 
00117   // the interface of qi::phrase_parse has changed between Boost 1.40 (Spirit 2.0) and Boost 1.41 (Spirit 2.1)
00118   bool result = qi::phrase_parse( begin, end, parser, ascii::space, evaluator );
00119 
00120   if (not result or begin != end) {
00121     delete evaluator;
00122     return 0;
00123   }
00124 
00125   return evaluator;
00126 }
00127 
00128 } // namespace triggerExpression
00129 
00130 #endif // HLTrigger_HLTfilters_TriggerExpressionParser_h