Commit 02869c99902df46757673d79722a7bbdfca2a2ce
1 parent
8a7bf6d4
Exists in
master
and in
87 other branches
Improve parse of parameters & components
Showing
4 changed files
with
68 additions
and
53 deletions
Show diff stats
src/expressionParser/ExpressionContainer.hh
... | ... | @@ -4,9 +4,6 @@ |
4 | 4 | namespace AMDA { |
5 | 5 | namespace parser { |
6 | 6 | |
7 | -struct op_power{}; | |
8 | -struct op_powerTen{}; | |
9 | - | |
10 | 7 | struct Operator { |
11 | 8 | explicit Operator(std::string ihmSymbol, std::string kernelSymbol) : ihmSymbol(ihmSymbol), kernelSymbol(kernelSymbol) {}; |
12 | 9 | |
... | ... | @@ -18,21 +15,19 @@ namespace Expression{ |
18 | 15 | |
19 | 16 | typedef std::string var; |
20 | 17 | |
21 | -template <typename tag> struct binop; | |
22 | -template <typename tag> struct funcop; | |
23 | 18 | struct BinaryOperation; |
24 | 19 | struct UnaryOperation; |
25 | 20 | struct FunctionOperation; |
21 | +struct PowerOperation; | |
22 | +struct ParameterDef; | |
26 | 23 | |
27 | 24 | typedef boost::variant< |
28 | 25 | var, |
29 | 26 | boost::recursive_wrapper<UnaryOperation>, |
30 | 27 | boost::recursive_wrapper<BinaryOperation>, |
31 | - | |
28 | + boost::recursive_wrapper<PowerOperation>, | |
32 | 29 | boost::recursive_wrapper<FunctionOperation>, |
33 | - | |
34 | - boost::recursive_wrapper<funcop<op_power> >, | |
35 | - boost::recursive_wrapper<binop<op_powerTen> > | |
30 | + boost::recursive_wrapper<ParameterDef> | |
36 | 31 | > ExpressionContainer; |
37 | 32 | |
38 | 33 | struct FunctionOperation |
... | ... | @@ -56,6 +51,23 @@ struct FunctionOperation |
56 | 51 | std::vector<ExpressionContainer> args; |
57 | 52 | }; |
58 | 53 | |
54 | +struct ParameterDef | |
55 | +{ | |
56 | + explicit ParameterDef(std::string param, const std::string& comp1, const std::string& comp2) : param(param) { | |
57 | + components.push_back(comp1); | |
58 | + components.push_back(comp2); | |
59 | + } | |
60 | + | |
61 | + explicit ParameterDef(std::string param, const std::string& comp1) : param(param) { | |
62 | + components.push_back(comp1); | |
63 | + } | |
64 | + | |
65 | + explicit ParameterDef(std::string param) : param(param) {} | |
66 | + | |
67 | + std::string param; | |
68 | + std::vector<std::string> components; | |
69 | +}; | |
70 | + | |
59 | 71 | struct UnaryOperation |
60 | 72 | { |
61 | 73 | explicit UnaryOperation(Operator op, const ExpressionContainer& e) : op(op), e(e) {} |
... | ... | @@ -71,6 +83,13 @@ struct BinaryOperation |
71 | 83 | ExpressionContainer r; |
72 | 84 | }; |
73 | 85 | |
86 | +struct PowerOperation | |
87 | +{ | |
88 | + explicit PowerOperation(const ExpressionContainer& l, const ExpressionContainer& r) : l(l), r(r) {} | |
89 | + ExpressionContainer l; | |
90 | + ExpressionContainer r; | |
91 | +}; | |
92 | + | |
74 | 93 | struct NotOperation : UnaryOperation |
75 | 94 | { |
76 | 95 | explicit NotOperation(const ExpressionContainer& e) : UnaryOperation(Operator("!","!"), e) {} |
... | ... | @@ -156,18 +175,6 @@ struct OrOperation : BinaryOperation |
156 | 175 | explicit OrOperation(const ExpressionContainer& l, const ExpressionContainer& r) : BinaryOperation(l, Operator("|","||"), r) { } |
157 | 176 | }; |
158 | 177 | |
159 | -template <typename tag> struct binop | |
160 | -{ | |
161 | - explicit binop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { } | |
162 | - ExpressionContainer oper1, oper2; | |
163 | -}; | |
164 | - | |
165 | -template <typename tag> struct funcop | |
166 | -{ | |
167 | - explicit funcop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { } | |
168 | - ExpressionContainer oper1, oper2; | |
169 | -}; | |
170 | - | |
171 | 178 | } /* namespace Expression */ |
172 | 179 | |
173 | 180 | } /* namespace parser */ | ... | ... |
src/expressionParser/ExpressionPrinter.hh
... | ... | @@ -41,6 +41,17 @@ public: |
41 | 41 | boost::apply_visitor(*this, u.e); |
42 | 42 | } |
43 | 43 | |
44 | + void operator()(const PowerOperation& p) const{ | |
45 | + #ifdef ADD_DEBUG_TAG | |
46 | + _os << "@POWER@"; | |
47 | + #endif | |
48 | + _os << "pow("; | |
49 | + boost::apply_visitor(*this, p.l); | |
50 | + _os << ","; | |
51 | + boost::apply_visitor(*this, p.r); | |
52 | + _os << ")"; | |
53 | + } | |
54 | + | |
44 | 55 | void operator()(const FunctionOperation& f) const{ |
45 | 56 | #ifdef ADD_DEBUG_TAG |
46 | 57 | _os << "@FUNC@"; |
... | ... | @@ -53,24 +64,20 @@ public: |
53 | 64 | boost::apply_visitor(*this, f.args[i]); |
54 | 65 | } |
55 | 66 | _os << ")"; |
56 | - } | |
57 | - | |
58 | - // Logical | |
59 | - | |
60 | - // Power Math operators | |
61 | - void operator()(const funcop<op_power>& b) const { _os << "power"; } | |
62 | - void operator()(const binop<op_powerTen>& b) const { printPowerTen("e", b.oper1, b.oper2); } | |
63 | - | |
64 | - // Functions | |
65 | - // printer | |
66 | - | |
67 | - void printPowerTen(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const | |
68 | - { | |
69 | - boost::apply_visitor(*this, l); | |
70 | - _os << op; | |
71 | - boost::apply_visitor(*this, r); | |
72 | - } | |
67 | + } | |
73 | 68 | |
69 | + void operator()(const ParameterDef& p) const{ | |
70 | + #ifdef ADD_DEBUG_TAG | |
71 | + _os << "@PARAM@"; | |
72 | + #endif | |
73 | + _os << "$"; | |
74 | + _os << p.param; | |
75 | + for (unsigned int i = 0; i < p.components.size(); ++i) { | |
76 | + _os << "["; | |
77 | + _os << p.components[i]; | |
78 | + _os << "]"; | |
79 | + } | |
80 | + } | |
74 | 81 | }; |
75 | 82 | |
76 | 83 | std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e) | ... | ... |
src/expressionParser/ParserGrammar.hh
... | ... | @@ -43,31 +43,29 @@ public: |
43 | 43 | division_ = PARSERGRAMMAR_ADD_OPERATOR(DivisionOperation, powerTen_, divisionOperator_); |
44 | 44 | |
45 | 45 | powerTen_ = PARSERGRAMMAR_ADD_OPERATOR(PowerTenOperation, pow_, powerTenOperator_); |
46 | - pow_ = PARSERGRAMMAR_ADD_OPERATOR(funcop<op_power>, plusSign_, powOperator_); | |
46 | + pow_ = PARSERGRAMMAR_ADD_OPERATOR(PowerOperation, plusSign_, powOperator_); | |
47 | 47 | |
48 | 48 | plusSign_ = ((sumOperator_ >>-*sumOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] || |
49 | 49 | ((differenceOperator_ >>differenceOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] |minusSign_ [_val = _1]; |
50 | - minusSign_ = (((-*sumOperator_>>differenceOperator_ >>-*sumOperator_) | (-factorOperator_>>differenceOperator_>>-factorOperator_)) > not_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | not_[_val = _1]; | |
50 | + minusSign_ = (((-*sumOperator_>>differenceOperator_ >>-*sumOperator_) ) > not_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | not_[_val = _1]; | |
51 | 51 | not_ = (notOperator_ > functions_) [_val = boost::phoenix::construct<NotOperation>(_1)] | functions_[_val = _1]; |
52 | 52 | |
53 | 53 | |
54 | 54 | |
55 | - functions_ = (threeArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4)] || | |
56 | - (twoArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3)]|| | |
55 | + functions_ = (threeArgsFunction>>"(">>funArgs_>>argumentSeparator_>>funArgs_>>argumentSeparator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4)] || | |
56 | + (twoArgsFunction>>"(">>funArgs_>>argumentSeparator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3)]|| | |
57 | 57 | (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2)]|const_[_val=_1]; |
58 | 58 | // UNARY OPERATIONS |
59 | 59 | |
60 | 60 | const_ = (definedConstants) [_val = boost::phoenix::construct<ConstantOperation>(_1)] | param_[_val = _1]; |
61 | - param_= (definedParams >>('(' >> (spectArgs_|vectorArgs_)>>')'))[_val='$'+_1+"["+qi::_2+"]"] || | |
62 | - definedParams[_val='$'+_1]| simple[_val = _1]; | |
61 | + param_= (definedParams >>('(' >> componentValue_ >> componentSeparator_ >> componentValue_ >>')'))[_val=boost::phoenix::construct<ParameterDef>(_1,_2,_3)] || | |
62 | + (definedParams >>('(' >> componentValue_ >> ')'))[_val=boost::phoenix::construct<ParameterDef>(_1,_2)] || | |
63 | + (definedParams)[_val=boost::phoenix::construct<ParameterDef>(_1)] | simple[_val = _1]; | |
63 | 64 | |
64 | 65 | funArgs_=((expr_ |var_) |functions_); |
65 | 66 | simple = (('(' > expr_ > ')') | var_); |
66 | 67 | var_ = (+qi::char_('0','9') >> -qi::char_('.') >> -(+qi::char_('0','9'))) | ((qi::char_('.') >> +qi::char_('0','9'))); |
67 | - vectorArgs_%=qi::raw[qi::int_ > -(qi::char_(',')>>qi::int_) ]; | |
68 | - spectArgs_ %=qi::raw[(qi::int_>>qi::char_(',')>>'*')|(qi::char_('*')>>qi::char_(',')>>qi::int_)]; | |
69 | - | |
70 | - | |
68 | + componentValue_ = (+qi::char_('0','9') | qi::char_('*')); | |
71 | 69 | |
72 | 70 | powOperator_ = qi::char_('^'); |
73 | 71 | powerTenOperator_ = qi::char_('e'); |
... | ... | @@ -84,7 +82,8 @@ public: |
84 | 82 | greaterOrEqualOperator_ = qi::string(">="); |
85 | 83 | lowerOrEqualOperator_ = qi::string("<="); |
86 | 84 | lowerOperator_ = qi::char_("<"); |
87 | - componentOperator_ = qi::char_(","); | |
85 | + argumentSeparator_ = qi::char_(","); | |
86 | + componentSeparator_ = qi::char_(","); | |
88 | 87 | |
89 | 88 | // Define available constants |
90 | 89 | std::map<std::string, std::string> existingConstants = ParserToolbox::getConstants(lProperties); |
... | ... | @@ -117,7 +116,7 @@ public: |
117 | 116 | } |
118 | 117 | |
119 | 118 | private: |
120 | - qi::rule<It, Expression::var(), Skipper> var_, vectorArgs_, spectArgs_;; | |
119 | + qi::rule<It, Expression::var(), Skipper> var_, componentValue_; | |
121 | 120 | qi::rule<It, Expression::ExpressionContainer(), Skipper> not_ |
122 | 121 | , pow_ |
123 | 122 | , powerTen_ |
... | ... | @@ -156,9 +155,10 @@ private: |
156 | 155 | , divisionOperator_ |
157 | 156 | , greaterOperator_ |
158 | 157 | , greaterOrEqualOperator_ |
159 | - ,lowerOrEqualOperator_ | |
160 | - ,componentOperator_ | |
161 | - ,lowerOperator_ ; | |
158 | + , lowerOrEqualOperator_ | |
159 | + , lowerOperator_ | |
160 | + , argumentSeparator_ | |
161 | + , componentSeparator_; | |
162 | 162 | |
163 | 163 | qi::symbols<char, std::string> definedConstants; |
164 | 164 | qi::symbols<char, std::string> definedParams; | ... | ... |
test/parser/amda_test_parser.csv