1 #ifndef CommonTools_Utils_Grammar_h 2 #define CommonTools_Utils_Grammar_h 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" 45 struct Grammar :
public boost::spirit::classic::grammar<Grammar> {
62 : sel_(&sel), expr_(&dummyExpr_), lazy_(lazy) {
63 typeStack.push_back(iType);
67 : sel_(&dummySel_), expr_(&expr), lazy_(lazy) {
68 typeStack.push_back(iType);
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;
76 rule
number,
var,
arrayAccess,
metharg,
method,
term,
power,
factor,
function1,
function2,
function4,
85 ExpressionVarSetter var_s(
self.exprStack,
self.methStack,
self.lazyMethStack,
self.typeStack);
88 MethodSetter method_s(
self.methStack,
self.lazyMethStack,
self.typeStack,
self.methArgStack,
self.lazy_);
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),
110 CutSetter cut_s(*
self.sel_,
self.selStack);
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);
145 boost::spirit::classic::assertion<SyntaxErrors> expect(
kSyntaxError);
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];
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]);
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] |
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];
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];
200 logical_factor = trinary_comp | binary_comp |
201 ch_p(
'(') >> logical_expression >> expectParenthesis(ch_p(
')')) |
202 (ch_p(
'!') >> expect(logical_factor))[not_s] | expression[expr_sel_s];
204 cut = logical_expression[cut_s];
205 fun = expression[expr_s];
206 this->start_parsers(cut, fun);
std::vector< std::shared_ptr< ComparisonBase > > ComparisonStack
Grammar(SelectorPtr &sel, const edm::TypeWithDict &iType, bool lazy=false)
LazyMethodStack lazyMethStack
std::shared_ptr< ExpressionBase > ExpressionPtr
std::vector< edm::TypeWithDict > TypeStack
FunctionStack finalFunStack
Grammar(ExpressionPtr &expr, const edm::TypeWithDict &iType, bool lazy=false)
std::vector< std::shared_ptr< ExpressionBase > > ExpressionStack
std::vector< AnyMethodArgument > MethodArgumentStack
boost::spirit::classic::rule< ScannerT > rule
std::vector< SelectorPtr > SelectorStack
std::vector< LazyInvoker > LazyMethodStack
std::vector< int > IntStack
MethodArgumentStack methArgStack
definition(const Grammar &self)
std::vector< MethodInvoker > MethodStack
ExpressionStack exprStack
std::vector< Function > FunctionStack
std::shared_ptr< SelectorBase > SelectorPtr