Commit e33239bfd854a3d6f868caae9bd38344f0675b2f
1 parent
24ca3eda
Exists in
master
and in
89 other branches
Parse parameters
Showing
3 changed files
with
47 additions
and
26 deletions
Show diff stats
src/expressionParser/ExpressionContainer.hh
... | ... | @@ -11,7 +11,6 @@ struct op_greaterOrEqual{}; |
11 | 11 | struct op_lowerOrEqual{}; |
12 | 12 | struct op_lower{}; |
13 | 13 | struct op_and {}; |
14 | -struct op_xor {}; | |
15 | 14 | struct op_equal {}; |
16 | 15 | struct op_unequal {}; |
17 | 16 | struct op_sum {}; |
... | ... | @@ -30,6 +29,7 @@ typedef std::string var; |
30 | 29 | template <typename tag> struct binop; |
31 | 30 | template <typename tag> struct unop; |
32 | 31 | template <typename tag> struct comop; |
32 | +template <typename tag> struct funcop; | |
33 | 33 | |
34 | 34 | typedef boost::variant<var, |
35 | 35 | boost::recursive_wrapper<unop <op_not> >, |
... | ... | @@ -40,13 +40,12 @@ typedef boost::variant<var, |
40 | 40 | boost::recursive_wrapper<comop<op_lower> >, |
41 | 41 | boost::recursive_wrapper<binop<op_unequal> >, |
42 | 42 | boost::recursive_wrapper<binop<op_and> >, |
43 | - boost::recursive_wrapper<binop<op_xor> >, | |
44 | 43 | boost::recursive_wrapper<binop<op_or> >, |
45 | 44 | boost::recursive_wrapper<binop<op_difference> >, |
46 | 45 | boost::recursive_wrapper<binop<op_sum> >, |
47 | 46 | boost::recursive_wrapper<binop<op_factor> >, |
48 | 47 | boost::recursive_wrapper<binop<op_division> >, |
49 | - boost::recursive_wrapper<binop<op_power> >, | |
48 | + boost::recursive_wrapper<funcop<op_power> >, | |
50 | 49 | boost::recursive_wrapper<binop<op_powerTen> >, |
51 | 50 | boost::recursive_wrapper<unop<op_minusSign> >, |
52 | 51 | boost::recursive_wrapper<unop<op_plusSign> >, |
... | ... | @@ -66,6 +65,12 @@ template <typename tag> struct comop |
66 | 65 | ExpressionContainer oper1, oper2; |
67 | 66 | }; |
68 | 67 | |
68 | +template <typename tag> struct funcop | |
69 | +{ | |
70 | + explicit funcop(const ExpressionContainer& l, const ExpressionContainer& r) : oper1(l), oper2(r) { } | |
71 | + ExpressionContainer oper1, oper2; | |
72 | +}; | |
73 | + | |
69 | 74 | template <typename tag> struct unop |
70 | 75 | { |
71 | 76 | explicit unop(const ExpressionContainer& o) : oper1(o) { } | ... | ... |
src/expressionParser/ExpressionPrinter.hh
... | ... | @@ -19,7 +19,6 @@ public: |
19 | 19 | // Logical |
20 | 20 | void operator()(const binop<op_and>& b) const { print(" && ", b.oper1, b.oper2); } |
21 | 21 | void operator()(const binop<op_or >& b) const { print(" || ", b.oper1, b.oper2); } |
22 | - void operator()(const binop<op_xor>& b) const { print(" | ", b.oper1, b.oper2); } | |
23 | 22 | void operator()(const binop<op_equal>& b) const { print(" == ", b.oper1, b.oper2); } |
24 | 23 | void operator()(const binop<op_unequal>& b) const { print(" != ", b.oper1, b.oper2); } |
25 | 24 | void operator()(const comop<op_lower>& b) const { print("<", b.oper1, b.oper2); } |
... | ... | @@ -34,8 +33,8 @@ public: |
34 | 33 | void operator()(const binop<op_division>& b) const { print("/", b.oper1, b.oper2); } |
35 | 34 | |
36 | 35 | // Power Math operators |
37 | - void operator()(const binop<op_power>& b) const { print("pow", b.oper1, b.oper2); } | |
38 | - void operator()(const binop<op_powerTen>& b) const { printPower("e", b.oper1, b.oper2); } | |
36 | + void operator()(const funcop<op_power>& b) const { printFunc("pow", b.oper1, b.oper2); } | |
37 | + void operator()(const binop<op_powerTen>& b) const { printPowerTen("e", b.oper1, b.oper2); } | |
39 | 38 | |
40 | 39 | // unique operators |
41 | 40 | void operator()(const unop<op_not>& u) const{printUnique("!",u.oper1);} |
... | ... | @@ -60,13 +59,14 @@ public: |
60 | 59 | _os << op; |
61 | 60 | boost::apply_visitor(*this, l); |
62 | 61 | } |
63 | - void printPower(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const | |
62 | + void printPowerTen(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const | |
64 | 63 | { |
65 | 64 | boost::apply_visitor(*this, l); |
66 | 65 | _os << op; |
67 | 66 | boost::apply_visitor(*this, r); |
68 | 67 | } |
69 | - void printOutSide(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const | |
68 | + | |
69 | + void printFunc(const std::string& op, const ExpressionContainer& l, const ExpressionContainer& r) const | |
70 | 70 | { |
71 | 71 | _os << op; |
72 | 72 | _os << "("; | ... | ... |
src/expressionParser/ParserGrammar.hh
... | ... | @@ -26,41 +26,48 @@ public: |
26 | 26 | expr_ = or_.alias(); |
27 | 27 | |
28 | 28 | // Logical Operators |
29 | - or_ = fold<binop<op_or> >(xor_.alias())[orOperator_ >> xor_]; | |
30 | - xor_ = fold<binop<op_xor> >(and_.alias())[xorOperator_ >> and_]; | |
29 | + or_ = fold<binop<op_or> >(and_.alias())[orOperator_ >> and_]; | |
31 | 30 | and_ = fold<binop<op_and> >(equal_.alias())[andOperator_ >> equal_]; |
32 | 31 | equal_ = fold<binop<op_equal> >(unequal_.alias())[equalOperator_ >> unequal_]; |
33 | 32 | unequal_ = fold<binop<op_unequal> >(greaterOrEqual_.alias())[unequalOperator_ >>greaterOrEqual_]; |
34 | 33 | greaterOrEqual_ = fold<binop<op_greaterOrEqual> >(lowerOrEqual_.alias())[greaterOrEqualOperator_ >> lowerOrEqual_]; |
35 | 34 | lowerOrEqual_ = fold<binop<op_lowerOrEqual> >(plusSign_.alias())[lowerOrEqualOperator_ >> plusSign_]; |
35 | + | |
36 | 36 | plusSign_ = ((sumOperator_ >>-*sumOperator_)> minusSign_ ) [_val = boost::phoenix::construct<Expression::unop <op_plusSign>>(_1)] || |
37 | 37 | ((differenceOperator_ >>differenceOperator_)> minusSign_ ) [_val = boost::phoenix::construct<Expression::unop <op_plusSign>>(_1)] |minusSign_ [_val = _1]; |
38 | 38 | minusSign_ = (-*sumOperator_>>differenceOperator_ >>-*sumOperator_> sum_) [_val = boost::phoenix::construct<Expression::unop <op_minusSign>>(_1)] | sum_[_val = _1]; |
39 | 39 | |
40 | - | |
41 | 40 | // Numerical Operators |
42 | 41 | sum_ = fold<binop<op_sum> >(difference_.alias())[sumOperator_ >> difference_]; |
43 | 42 | difference_ = fold<binop<op_difference> >(factor_.alias())[differenceOperator_ >> factor_]; |
44 | 43 | factor_ = fold<binop<op_factor> >(division_.alias())[factorOperator_ >> division_]; |
45 | - division_ = fold<binop<op_division> >(greater_.alias())[divisionOperator_ >> greater_]; | |
44 | + division_ = fold<binop<op_division> >(pow_.alias())[divisionOperator_ >> pow_]; | |
45 | + | |
46 | + pow_ = fold<funcop<op_power> >(greater_.alias())[powOperator_ >> greater_]; | |
46 | 47 | |
47 | 48 | greater_= fold<comop<op_greater> >(lower_.alias())[greaterOperator_ >> lower_]; |
48 | 49 | lower_ = fold<comop<op_lower> >( not_.alias())[lowerOperator_ >> not_]; |
49 | 50 | |
50 | 51 | // UNARY OPERATIONS |
51 | - not_ = (notOperator_ > simple) [_val = boost::phoenix::construct<Expression::unop <op_not>>(_1)] | const_[_val = _1]; | |
52 | - const_ = (definedFunctions ) [_val = boost::phoenix::construct<Expression::unop <op_const>>(_1)] | simple[_val = _1]; | |
52 | + not_ = (notOperator_ > const_) [_val = boost::phoenix::construct<Expression::unop <op_not>>(_1)] | const_[_val = _1]; | |
53 | + | |
54 | + const_ = (definedConstants) [_val = boost::phoenix::construct<Expression::unop <op_const>>(_1)] | param_[_val = _1]; | |
55 | + param_= (definedParams >>('(' >> (spectArgs_|vectorArgs_)>>')'))[_val='$'+_1+"["+qi::_2+"]"] || | |
56 | + definedParams[_val='$'+_1]| simple[_val = _1]; | |
57 | + | |
53 | 58 | |
54 | 59 | simple = (('(' > expr_ > ')') | var_); |
55 | - var_ = qi::lexeme[+alnum]; | |
60 | + var_ = (+qi::char_('0','9') >> -qi::char_('.') >> -(+qi::char_('0','9'))) | ((qi::char_('.') >> +qi::char_('0','9'))); | |
61 | + vectorArgs_%=qi::raw[qi::int_ > -(qi::char_(',')>>qi::int_) ]; | |
62 | + spectArgs_ %=qi::raw[(qi::int_>>qi::char_(',')>>'*')|(qi::char_('*')>>qi::char_(',')>>qi::int_)]; | |
63 | + powOperator_ = qi::char_('^'); | |
56 | 64 | notOperator_ = qi::char_('!'); |
57 | 65 | andOperator_ = qi::string("&"); |
58 | 66 | orOperator_ = qi::string("|"); |
59 | - xorOperator_ = qi::char_("^"); | |
60 | 67 | equalOperator_ = qi::string("="); |
61 | 68 | unequalOperator_ = qi::string("!="); |
62 | - sumOperator_ = qi::char_("+"); | |
63 | - differenceOperator_ = qi::char_("-"); | |
69 | + sumOperator_ = qi::char_('+'); | |
70 | + differenceOperator_ = qi::char_('-'); | |
64 | 71 | factorOperator_ = qi::char_("*"); |
65 | 72 | divisionOperator_ = qi::char_("/"); |
66 | 73 | greaterOperator_ = qi::char_(">"); |
... | ... | @@ -71,19 +78,25 @@ public: |
71 | 78 | // Define available constants |
72 | 79 | std::map<std::string, std::string> existingConstants = ParserToolbox::getConstants(lProperties); |
73 | 80 | for(std::map<std::string, std::string>::iterator it = existingConstants.begin(); it != existingConstants.end(); ++it) { |
74 | - definedFunctions.add (it->first, it->second); | |
81 | + definedConstants.add (it->first, it->second); | |
75 | 82 | } |
76 | 83 | |
77 | - BOOST_SPIRIT_DEBUG_NODES((expr_)(or_)(xor_)(and_)(equal_)(unequal_)(greaterOrEqual_)(lowerOrEqual_)(lower_)(sum_) | |
78 | - (difference_)(factor_)(division_)(simple)(notOperator_)(andOperator_)(orOperator_)(xorOperator_)(equalOperator_)(unequalOperator_) | |
84 | + // Define available params | |
85 | + std::vector<std::string> existingParams = ParserToolbox::getParameters(lProperties); | |
86 | + for(std::vector<std::string>::iterator it = existingParams.begin(); it != existingParams.end(); ++it) { | |
87 | + definedParams.add(*it,*it); | |
88 | + } | |
89 | + | |
90 | + BOOST_SPIRIT_DEBUG_NODES((expr_)(or_)(and_)(equal_)(unequal_)(greaterOrEqual_)(lowerOrEqual_)(lower_)(sum_) | |
91 | + (difference_)(factor_)(division_)(simple)(notOperator_)(andOperator_)(orOperator_)(equalOperator_)(unequalOperator_) | |
79 | 92 | (sumOperator_)(differenceOperator_)(factorOperator_)(divisionOperator_)(greater_)(lower_)); |
80 | 93 | } |
81 | 94 | |
82 | 95 | private: |
83 | - qi::rule<It, Expression::var(), Skipper> var_; | |
96 | + qi::rule<It, Expression::var(), Skipper> var_, vectorArgs_, spectArgs_;; | |
84 | 97 | qi::rule<It, Expression::ExpressionContainer(), Skipper> not_ |
98 | + , pow_ | |
85 | 99 | , and_ |
86 | - , xor_ | |
87 | 100 | , or_ |
88 | 101 | , equal_ |
89 | 102 | , unequal_ |
... | ... | @@ -99,13 +112,14 @@ private: |
99 | 112 | ,greaterOrEqual_ |
100 | 113 | ,lowerOrEqual_ |
101 | 114 | ,lower_ |
102 | - ,const_; | |
115 | + ,const_ | |
116 | + ,param_; | |
103 | 117 | |
104 | 118 | |
105 | 119 | qi::rule<It, Skipper> notOperator_ |
120 | + , powOperator_ | |
106 | 121 | , andOperator_ |
107 | 122 | , orOperator_ |
108 | - , xorOperator_ | |
109 | 123 | , equalOperator_ |
110 | 124 | , unequalOperator_ |
111 | 125 | , sumOperator_ |
... | ... | @@ -117,7 +131,9 @@ private: |
117 | 131 | ,lowerOrEqualOperator_ |
118 | 132 | ,lowerOperator_ ; |
119 | 133 | |
120 | - qi::symbols<char, std::string> definedFunctions; | |
134 | + qi::symbols<char, std::string> definedConstants; | |
135 | + | |
136 | + qi::symbols<char, std::string> definedParams; | |
121 | 137 | }; |
122 | 138 | |
123 | 139 | } | ... | ... |