CMS 3D CMS Logo

Grammar.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_Grammar_h
2 #define CommonTools_Utils_Grammar_h
3 /* \class reco::parser::Grammar
4  *
5  * parser grammar
6  *
7  * \author original version: Chris Jones, Cornell,
8  * extended by Luca Lista, INFN
9  *
10  * \version $Revision: 1.13 $
11  *
12  */
13 #include "boost/spirit/include/classic_core.hpp"
14 #include "boost/spirit/include/classic_grammar_def.hpp"
15 #include "boost/spirit/include/classic_chset.hpp"
16 #include <functional>
41 // #include "CommonTools/Utils/src/Abort.h"
42 
43 namespace reco {
44  namespace parser {
45  struct Grammar : public boost::spirit::classic::grammar<Grammar> {
50  bool lazy_;
59  mutable IntStack intStack;
60 
61  Grammar(SelectorPtr& sel, const edm::TypeWithDict& iType, bool lazy = false)
62  : sel_(&sel), expr_(&dummyExpr_), lazy_(lazy) {
63  typeStack.push_back(iType);
64  }
65 
66  Grammar(ExpressionPtr& expr, const edm::TypeWithDict& iType, bool lazy = false)
67  : sel_(&dummySel_), expr_(&expr), lazy_(lazy) {
68  typeStack.push_back(iType);
69  }
70 
71  template <typename ScannerT>
72  struct definition : public boost::spirit::classic::grammar_def<boost::spirit::classic::rule<ScannerT>,
73  boost::spirit::classic::same,
74  boost::spirit::classic::same> {
75  typedef boost::spirit::classic::rule<ScannerT> rule;
79  definition(const Grammar& self) {
80  using namespace boost::spirit::classic;
81  using namespace std;
82 
83  ExpressionNumberSetter number_s(self.exprStack);
84  IntSetter int_s(self.intStack);
85  ExpressionVarSetter var_s(self.exprStack, self.methStack, self.lazyMethStack, self.typeStack);
86  ExpressionConditionSetter cond_s(self.exprStack, self.selStack);
87  MethodArgumentSetter methodArg_s(self.methArgStack);
88  MethodSetter method_s(self.methStack, self.lazyMethStack, self.typeStack, self.methArgStack, self.lazy_);
89  ComparisonSetter<less_equal<double> > less_equal_s(self.cmpStack);
92  ComparisonSetter<greater_equal<double> > greater_equal_s(self.cmpStack);
94  ComparisonSetter<not_equal_to<double> > not_equal_to_s(self.cmpStack);
95  FunctionSetter abs_s(kAbs, self.funStack), acos_s(kAcos, self.funStack), asin_s(kAsin, self.funStack),
96  atan2_s(kAtan, self.funStack), atan_s(kAtan, self.funStack), chi2prob_s(kChi2Prob, self.funStack),
97  cosh_s(kCosh, self.funStack), cos_s(kCos, self.funStack), exp_s(kExp, self.funStack),
98  hypot_s(kHypot, self.funStack), log_s(kLog, self.funStack), log10_s(kLog10, self.funStack),
99  max_s(kMax, self.funStack), min_s(kMin, self.funStack), pow_s(kPow, self.funStack),
100  sinh_s(kSinh, self.funStack), sin_s(kSin, self.funStack), sqrt_s(kSqrt, self.funStack),
101  tanh_s(kTanh, self.funStack), tan_s(kTan, self.funStack), deltaPhi_s(kDeltaPhi, self.funStack),
102  deltaR_s(kDeltaR, self.funStack), test_bit_s(kTestBit, self.funStack);
103  FunctionSetterCommit funOk_s(self.funStack, self.finalFunStack);
104  TrinarySelectorSetter trinary_s(self.selStack, self.cmpStack, self.exprStack);
105  BinarySelectorSetter binary_s(self.selStack, self.cmpStack, self.exprStack);
106  ExpressionSelectorSetter expr_sel_s(self.selStack, self.exprStack);
110  CutSetter cut_s(*self.sel_, self.selStack);
111  ExpressionSetter expr_s(*self.expr_, self.exprStack);
119  // Abort abort_s;
120  BOOST_SPIRIT_DEBUG_RULE(var);
121  BOOST_SPIRIT_DEBUG_RULE(arrayAccess);
122  BOOST_SPIRIT_DEBUG_RULE(method);
123  BOOST_SPIRIT_DEBUG_RULE(logical_expression);
124  BOOST_SPIRIT_DEBUG_RULE(cond_expression);
125  BOOST_SPIRIT_DEBUG_RULE(logical_term);
126  BOOST_SPIRIT_DEBUG_RULE(logical_factor);
127  BOOST_SPIRIT_DEBUG_RULE(number);
128  BOOST_SPIRIT_DEBUG_RULE(metharg);
129  BOOST_SPIRIT_DEBUG_RULE(function1);
130  BOOST_SPIRIT_DEBUG_RULE(function2);
131  BOOST_SPIRIT_DEBUG_RULE(function4);
132  BOOST_SPIRIT_DEBUG_RULE(expression);
133  BOOST_SPIRIT_DEBUG_RULE(term);
134  BOOST_SPIRIT_DEBUG_RULE(power);
135  BOOST_SPIRIT_DEBUG_RULE(factor);
136  BOOST_SPIRIT_DEBUG_RULE(or_op);
137  BOOST_SPIRIT_DEBUG_RULE(and_op);
138  BOOST_SPIRIT_DEBUG_RULE(comparison_op);
139  BOOST_SPIRIT_DEBUG_RULE(binary_comp);
140  BOOST_SPIRIT_DEBUG_RULE(trinary_comp);
141  BOOST_SPIRIT_DEBUG_RULE(cut);
142  BOOST_SPIRIT_DEBUG_RULE(fun);
143 
144  boost::spirit::classic::assertion<SyntaxErrors> expectParenthesis(kMissingClosingParenthesis);
145  boost::spirit::classic::assertion<SyntaxErrors> expect(kSyntaxError);
146 
147  number = real_p[number_s];
148  metharg = (strict_real_p[methodArg_s]) | (int_p[methodArg_s]) |
149  (ch_p('"') >> *(~ch_p('"')) >> ch_p('"'))[methodArg_s] |
150  (ch_p('\'') >> *(~ch_p('\'')) >> ch_p('\''))[methodArg_s];
151  var = // alnum_p doesn't accept underscores, so we use chset<>; lexeme_d needed to avoid whitespace skipping within method names
152  (lexeme_d[alpha_p >> *chset<>("a-zA-Z0-9_")] >> ch_p('(') >> metharg >> *(ch_p(',') >> metharg) >>
153  expectParenthesis(ch_p(')')))[method_s] |
154  ((lexeme_d[alpha_p >> *chset<>("a-zA-Z0-9_")])[method_s] >> !(ch_p('(') >> ch_p(')')));
155  arrayAccess = (ch_p('[') >> metharg >> *(ch_p(',') >> metharg) >> expectParenthesis(ch_p(']')))[method_s];
156  method = (var >> *(arrayAccess | (ch_p('.') >> expect(var))))[var_s];
157  function1 = chseq_p("abs")[abs_s] | chseq_p("acos")[acos_s] | chseq_p("asin")[asin_s] |
158  chseq_p("atan")[atan_s] | chseq_p("cosh")[cosh_s] | chseq_p("cos")[cos_s] |
159  chseq_p("exp")[exp_s] | chseq_p("log")[log_s] | chseq_p("log10")[log10_s] |
160  chseq_p("sinh")[sinh_s] | chseq_p("sin")[sin_s] | chseq_p("sqrt")[sqrt_s] |
161  chseq_p("tanh")[tanh_s] | chseq_p("tan")[tan_s];
162  function2 = chseq_p("atan2")[atan2_s] | chseq_p("chi2prob")[chi2prob_s] | chseq_p("pow")[pow_s] |
163  chseq_p("min")[min_s] | chseq_p("max")[max_s] | chseq_p("deltaPhi")[deltaPhi_s] |
164  chseq_p("hypot")[hypot_s] | chseq_p("test_bit")[test_bit_s];
165  function4 = chseq_p("deltaR")[deltaR_s];
167  nocond_expression = term >> (*(('+' >> expect(term))[plus_s] | ('-' >> expect(term))[minus_s]));
168  cond_expression = (ch_p('?') >> logical_expression >> ch_p('?') >> expect(expression) >> ch_p(":") >>
169  expect(expression))[cond_s];
170  term = power >> *(('*' >> expect(power))[multiplies_s] | ('/' >> expect(power))[divides_s]);
171  power = factor >> *(('^' >> expect(factor))[power_of_s]);
172  factor =
173  number | (function1 >> ch_p('(')[funOk_s] >> expect(expression) >> expectParenthesis(ch_p(')')))[fun_s] |
174  (function2 >> ch_p('(')[funOk_s] >> expect(expression) >> expect(ch_p(',')) >> expect(expression) >>
175  expectParenthesis(ch_p(')')))[fun_s] |
176  (function4 >> ch_p('(')[funOk_s] >> expect(expression) >> expect(ch_p(',')) >> expect(expression) >>
177  expect(ch_p(',')) >> expect(expression) >> expect(ch_p(',')) >> expect(expression) >>
178  expectParenthesis(ch_p(')')))[fun_s] |
179  //NOTE: no expect around the first ch_p('(') otherwise it can't parse a method that starts like a function name (i.e. maxSomething)
180  method |
181  //NOTE: no 'expectedParenthesis around ending ')' because at this point the partial phrase
182  // "(a"
183  //could refer to an expression, e.g., "(a+b)*c" or a logical expression "(a<1) &&"
184  //so we need to allow the parser to 'backup' and try a different approach.
185  //NOTE: if the parser were changed so a logical expression could be used as an expression,e.g.
186  // (a<b)+1 <2
187  // then we could remove such an ambiguity.
188  ch_p('(') >> expression >> ch_p(')') | (ch_p('-') >> factor)[negate_s] | (ch_p('+') >> factor);
189  comparison_op = (ch_p('<') >> ch_p('=')[less_equal_s]) | (ch_p('<')[less_s]) |
190  (ch_p('=') >> ch_p('=')[equal_to_s]) | (ch_p('=')[equal_to_s]) |
191  (ch_p('>') >> ch_p('=')[greater_equal_s]) | (ch_p('>')[greater_s]) |
192  (ch_p('!') >> ch_p('=')[not_equal_to_s]);
193  binary_comp = (expression >> comparison_op >> expect(expression))[binary_s];
194  trinary_comp =
195  (expression >> comparison_op >> expect(expression) >> comparison_op >> expect(expression))[trinary_s];
196  or_op = ch_p('|') >> ch_p('|') | ch_p('|');
197  and_op = ch_p('&') >> ch_p('&') | ch_p('&');
198  logical_expression = logical_term >> *(or_op >> expect(logical_term))[or_s];
199  logical_term = logical_factor >> *(and_op >> expect(logical_factor))[and_s];
201  ch_p('(') >> logical_expression >> expectParenthesis(ch_p(')')) |
202  (ch_p('!') >> expect(logical_factor))[not_s] | expression[expr_sel_s];
203  ;
204  cut = logical_expression[cut_s];
205  fun = expression[expr_s];
206  this->start_parsers(cut, fun);
207  }
208  };
209  };
210  } // namespace parser
211 } // namespace reco
212 
213 #endif
reco::parser::kLog
Definition: Function.h:29
reco::parser::kSin
Definition: Function.h:34
reco::parser::ExpressionVarSetter
Definition: ExpressionVarSetter.h:19
ExpressionNumberSetter.h
reco::parser::Grammar::methArgStack
MethodArgumentStack methArgStack
Definition: Grammar.h:57
reco::parser::kCosh
Definition: Function.h:26
reco::parser::Grammar::definition::logical_expression
rule logical_expression
Definition: Grammar.h:76
reco::parser::Grammar::definition::var
rule var
Definition: Grammar.h:76
reco::parser::kTanh
Definition: Function.h:38
reco::parser::Grammar::definition::or_op
rule or_op
Definition: Grammar.h:76
reco::parser::Grammar::dummyExpr_
ExpressionPtr dummyExpr_
Definition: Grammar.h:47
reco::parser::kAcos
Definition: Function.h:20
reco::parser::Grammar::definition::expression
rule expression
Definition: Grammar.h:76
reco::parser::FunctionSetter
Definition: FunctionSetter.h:17
BinarySelectorSetter.h
reco::parser::IntStack
std::vector< int > IntStack
Definition: IntStack.h:16
reco::parser::Grammar::expr_
ExpressionPtr * expr_
Definition: Grammar.h:49
boost::spirit::classic
Definition: DDPartSelection.cc:10
TrinarySelectorSetter.h
reco::parser::Grammar::methStack
MethodStack methStack
Definition: Grammar.h:55
reco::parser::Grammar::definition
Definition: Grammar.h:72
ExpressionSetter.h
reco::parser::kSyntaxError
Definition: Exception.h:31
reco::parser::Grammar::definition::cut
rule cut
Definition: Grammar.h:76
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
reco::parser::ExpressionSelectorSetter
Definition: ExpressionSelectorSetter.h:24
reco::parser::TrinarySelectorSetter
Definition: TrinarySelectorSetter.h:20
reco::parser::kHypot
Definition: Function.h:28
TypeWithDict.h
reco::parser::Grammar::typeStack
TypeStack typeStack
Definition: Grammar.h:58
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
reco::parser::Grammar::selStack
SelectorStack selStack
Definition: Grammar.h:53
reco::parser::Grammar::intStack
IntStack intStack
Definition: Grammar.h:59
reco::parser::Grammar::definition::factor
rule factor
Definition: Grammar.h:76
reco::parser::ExpressionConditionSetter
Definition: ExpressionConditionSetter.h:17
reco::parser::MethodArgumentSetter
Definition: MethodArgumentSetter.h:17
reco::parser::Grammar::definition::logical_term
rule logical_term
Definition: Grammar.h:76
reco::parser::ExpressionSetter
Definition: ExpressionSetter.h:18
MethodStack.h
IntStack.h
reco::parser::Grammar::exprStack
ExpressionStack exprStack
Definition: Grammar.h:51
reco::parser::kMax
Definition: Function.h:31
reco::parser::Grammar::definition::fun
rule fun
Definition: Grammar.h:76
reco::parser::kCos
Definition: Function.h:25
reco::parser::Grammar::definition::function1
rule function1
Definition: Grammar.h:76
CutSetter.h
reco::parser::kAsin
Definition: Function.h:21
Exception.h
reco::parser::ExpressionFunctionSetter
Definition: ExpressionFunctionSetter.h:17
reco::parser::FunctionStack
std::vector< Function > FunctionStack
Definition: FunctionStack.h:17
reco::parser::kDeltaR
Definition: Function.h:39
reco::parser::MethodStack
std::vector< MethodInvoker > MethodStack
Definition: MethodStack.h:17
reco::parser::Grammar::sel_
SelectorPtr * sel_
Definition: Grammar.h:48
reco::parser::IntSetter
Definition: IntSetter.h:16
reco::parser::kDeltaPhi
Definition: Function.h:40
FunctionSetter.h
reco::parser::Grammar::definition::cond_expression
rule cond_expression
Definition: Grammar.h:76
reco::parser::Grammar::Grammar
Grammar(SelectorPtr &sel, const edm::TypeWithDict &iType, bool lazy=false)
Definition: Grammar.h:61
reco::parser::Grammar::dummySel_
SelectorPtr dummySel_
Definition: Grammar.h:46
ExpressionVarSetter.h
ComparisonSetter.h
reco::parser::Grammar::definition::definition
definition(const Grammar &self)
Definition: Grammar.h:79
reco::parser::Grammar::definition::arrayAccess
rule arrayAccess
Definition: Grammar.h:76
reco::parser::FunctionSetterCommit
Definition: FunctionSetter.h:32
reco::parser::CutSetter
Definition: CutSetter.h:19
reco::parser::kMissingClosingParenthesis
Definition: Exception.h:31
reco::parser::Grammar::definition::and_op
rule and_op
Definition: Grammar.h:76
reco::parser::kAtan
Definition: Function.h:22
reco::parser::Grammar::funStack
FunctionStack funStack
Definition: Grammar.h:54
edm::TypeWithDict
Definition: TypeWithDict.h:38
reco::parser::BinaryCutSetter
Definition: BinaryCutSetter.h:19
reco::parser::kSqrt
Definition: Function.h:36
reco::parser::Grammar::definition::function4
rule function4
Definition: Grammar.h:76
reco::parser::Grammar::definition::metharg
rule metharg
Definition: Grammar.h:76
reco::parser::Grammar::definition::logical_factor
rule logical_factor
Definition: Grammar.h:76
reco::parser::Grammar::definition::power
rule power
Definition: Grammar.h:76
ExpressionBinaryOperatorSetter.h
reco::parser::ExpressionNumberSetter
Definition: ExpressionNumberSetter.h:18
MethodArgumentSetter.h
reco::parser::ExpressionPtr
std::shared_ptr< ExpressionBase > ExpressionPtr
Definition: ExpressionBase.h:25
reco::parser::TypeStack
std::vector< edm::TypeWithDict > TypeStack
Definition: TypeStack.h:17
ExpressionFunctionSetter.h
BinaryCutSetter.h
reco::parser::Grammar
Definition: Grammar.h:45
reco::parser::kChi2Prob
Definition: Function.h:24
reco::parser::BinarySelectorSetter
Definition: BinarySelectorSetter.h:21
reco::parser::Grammar::definition::number
rule number
Definition: Grammar.h:76
reco::parser::kAbs
Definition: Function.h:19
reco::parser::Grammar::definition::method
rule method
Definition: Grammar.h:76
reco::parser::ExpressionBinaryOperatorSetter
Definition: ExpressionBinaryOperatorSetter.h:26
reco::parser::Grammar::definition::trinary_comp
rule trinary_comp
Definition: Grammar.h:76
std
Definition: JetResolutionObject.h:76
reco::parser::Grammar::definition::term
rule term
Definition: Grammar.h:76
reco::parser::MethodSetter
Definition: MethodSetter.h:17
reco::parser::Grammar::definition::rule
boost::spirit::classic::rule< ScannerT > rule
Definition: Grammar.h:75
jets_cff.expr
expr
Definition: jets_cff.py:485
TypeStack.h
reco::parser::LazyMethodStack
std::vector< LazyInvoker > LazyMethodStack
Definition: MethodStack.h:18
reco::parser::SelectorPtr
std::shared_ptr< SelectorBase > SelectorPtr
Definition: SelectorPtr.h:18
reco::parser::kTan
Definition: Function.h:37
reco::parser::Grammar::definition::nocond_expression
rule nocond_expression
Definition: Grammar.h:76
MethodArgumentStack.h
reco::parser::kMin
Definition: Function.h:32
reco::parser::ExpressionStack
std::vector< std::shared_ptr< ExpressionBase > > ExpressionStack
Definition: ExpressionStack.h:19
reco::parser::ComparisonStack
std::vector< std::shared_ptr< ComparisonBase > > ComparisonStack
Definition: ComparisonStack.h:19
IntSetter.h
reco::parser::Grammar::finalFunStack
FunctionStack finalFunStack
Definition: Grammar.h:54
reco::parser::Grammar::lazyMethStack
LazyMethodStack lazyMethStack
Definition: Grammar.h:56
reco::parser::Grammar::definition::binary_comp
rule binary_comp
Definition: Grammar.h:76
reco::parser::Grammar::lazy_
bool lazy_
Definition: Grammar.h:50
reco::parser::ExpressionUnaryOperatorSetter
Definition: ExpressionUnaryOperatorSetter.h:30
reco::parser::Grammar::Grammar
Grammar(ExpressionPtr &expr, const edm::TypeWithDict &iType, bool lazy=false)
Definition: Grammar.h:66
reco::parser::UnaryCutSetter
Definition: UnaryCutSetter.h:19
ExpressionSelectorSetter.h
reco::parser::MethodArgumentStack
std::vector< AnyMethodArgument > MethodArgumentStack
Definition: MethodArgumentStack.h:17
reco::parser::kExp
Definition: Function.h:27
reco::parser::kTestBit
Definition: Function.h:41
EgammaValidation_Wenu_cff.sel
sel
Definition: EgammaValidation_Wenu_cff.py:33
reco::parser::Grammar::cmpStack
ComparisonStack cmpStack
Definition: Grammar.h:52
reco::parser::kLog10
Definition: Function.h:30
reco::parser::kPow
Definition: Function.h:33
UnaryCutSetter.h
ExpressionConditionSetter.h
reco::parser::Grammar::definition::logical_combiner
rule logical_combiner
Definition: Grammar.h:76
reco::parser::ComparisonSetter
Definition: ComparisonSetter.h:31
reco::parser::kSinh
Definition: Function.h:35
reco::parser::Grammar::definition::function2
rule function2
Definition: Grammar.h:76
ExpressionUnaryOperatorSetter.h
reco::parser::SelectorStack
std::vector< SelectorPtr > SelectorStack
Definition: SelectorStack.h:18
reco::parser::Grammar::definition::comparison_op
rule comparison_op
Definition: Grammar.h:76
MethodSetter.h