Blame view

src/expressionParser/ParserGrammar.hh 7.27 KB
a7f2648e   Benjamin Renard   Parser: First imp...
1
2
3
4
5
6
7
8
#ifndef PARSERGRAMMAR_HH_
#define PARSERGRAMMAR_HH_

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

#include "ExpressionContainer.hh"
#include "CustomFoldDirective.hh"
e58d7be4   Benjamin Renard   Parser: Add const...
9
10
#include "ParserToolbox.hh"
#include "Properties.hh"
36749bea   Benjamin Renard   Use a common cont...
11
#include "FunctionInfo.hh"
a7f2648e   Benjamin Renard   Parser: First imp...
12
13
14
15
16
17
18
19
20

namespace AMDA {
namespace parser {

namespace qi = boost::spirit::qi;

using namespace qi;
using namespace Expression;

c1f4db8e   Benjamin Renard   Add process to te...
21
22
#define PARSERGRAMMAR_ADD_OPERATOR(operation, next, operator) fold<operation>(next.alias())[operator >> next]

a7f2648e   Benjamin Renard   Parser: First imp...
23
template <typename It, typename Skipper = boost::spirit::standard_wide::space_type>
47755d13   Benjamin Renard   Minor changes
24
class ParserGrammar : public qi::grammar<It, Expression::ExpressionContainer(), Skipper>
a7f2648e   Benjamin Renard   Parser: First imp...
25
{
47755d13   Benjamin Renard   Minor changes
26
public:
e58d7be4   Benjamin Renard   Parser: Add const...
27
    ParserGrammar(AMDA::helpers::Properties& lProperties) : ParserGrammar::base_type(expr_)
a7f2648e   Benjamin Renard   Parser: First imp...
28
29
30
31
    {
        expr_ = or_.alias();

	// Logical Operators
c1f4db8e   Benjamin Renard   Add process to te...
32
33
34
35
36
37
38
39
        or_ = PARSERGRAMMAR_ADD_OPERATOR(OrOperation, and_, orOperator_);
        and_ = PARSERGRAMMAR_ADD_OPERATOR(AndOperation, equal_, andOperator_);
        equal_ = PARSERGRAMMAR_ADD_OPERATOR(EqualOperation, unequal_, equalOperator_); 
        unequal_ = PARSERGRAMMAR_ADD_OPERATOR(UnequalOperation, greaterOrEqual_, unequalOperator_);
        greaterOrEqual_ = PARSERGRAMMAR_ADD_OPERATOR(GreaterOrEqualOperation, lowerOrEqual_, greaterOrEqualOperator_);
        lowerOrEqual_ = PARSERGRAMMAR_ADD_OPERATOR(LowerOrEqualOperation, sum_, lowerOrEqualOperator_);

	sum_ = fold<SumOperation>(difference_.alias())[sumOperator_ >> difference_];
c0bbbeb7   Benjamin Renard   Fix some bug with...
40
41
42
43
44
	difference_ = fold<DifferenceOperation>(factor_.alias())[differenceOperator_ >> factor_];

        factor_ = fold<FactorOperation>(division_.alias())[factorOperator_ >> division_];
        division_ = fold<DivisionOperation>(powerTen_.alias())[divisionOperator_ >> powerTen_];
	powerTen_ = fold<PowerTenOperation>(plusSign_.alias())[powerTenOperator_ >> plusSign_];
e33239bf   Benjamin Renard   Parse parameters
45

36749bea   Benjamin Renard   Use a common cont...
46
47
        plusSign_ = ((sumOperator_ >>-*sumOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] ||
                ((differenceOperator_ >>differenceOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] |minusSign_ [_val = _1];
c0bbbeb7   Benjamin Renard   Fix some bug with...
48
        minusSign_ = (((-*sumOperator_>>differenceOperator_ >>-*sumOperator_) | (-factorOperator_>>differenceOperator_>>-factorOperator_)) > pow_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | pow_[_val = _1];
a7f2648e   Benjamin Renard   Parser: First imp...
49

a7f2648e   Benjamin Renard   Parser: First imp...
50
	// Numerical Operators
e33239bf   Benjamin Renard   Parse parameters
51
52

	pow_ = fold<funcop<op_power> >(greater_.alias())[powOperator_ >> greater_];
a7f2648e   Benjamin Renard   Parser: First imp...
53

36749bea   Benjamin Renard   Use a common cont...
54
55
        greater_=  fold<GreaterOperation>(lower_.alias())[greaterOperator_ >> lower_];
        lower_ = fold<LowerOperation>( functions_.alias())[lowerOperator_ >> functions_];
a7f2648e   Benjamin Renard   Parser: First imp...
56

ef417f05   Benjamin Renard   Improve function ...
57
58
59
        functions_ = (threeArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4)] ||
        (twoArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3)]||
        (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2)]|not_[_val=_1];
a7f2648e   Benjamin Renard   Parser: First imp...
60
	// UNARY OPERATIONS
36749bea   Benjamin Renard   Use a common cont...
61
	not_ = (notOperator_ > const_) [_val = boost::phoenix::construct<NotOperation>(_1)] | const_[_val = _1];
e33239bf   Benjamin Renard   Parse parameters
62
 
36749bea   Benjamin Renard   Use a common cont...
63
	const_ = (definedConstants) [_val = boost::phoenix::construct<ConstantOperation>(_1)] | param_[_val = _1];
e33239bf   Benjamin Renard   Parse parameters
64
65
66
 	param_= (definedParams >>('(' >> (spectArgs_|vectorArgs_)>>')'))[_val='$'+_1+"["+qi::_2+"]"] ||
                definedParams[_val='$'+_1]| simple[_val = _1];

36749bea   Benjamin Renard   Use a common cont...
67
        funArgs_=((expr_   |var_) |functions_); 
a7f2648e   Benjamin Renard   Parser: First imp...
68
        simple = (('(' > expr_ > ')') | var_);
e33239bf   Benjamin Renard   Parse parameters
69
70
71
	var_ = (+qi::char_('0','9') >> -qi::char_('.') >> -(+qi::char_('0','9'))) | ((qi::char_('.') >> +qi::char_('0','9')));
	vectorArgs_%=qi::raw[qi::int_ > -(qi::char_(',')>>qi::int_) ];
        spectArgs_ %=qi::raw[(qi::int_>>qi::char_(',')>>'*')|(qi::char_('*')>>qi::char_(',')>>qi::int_)];
c1f4db8e   Benjamin Renard   Add process to te...
72
73
74



e33239bf   Benjamin Renard   Parse parameters
75
	powOperator_        = qi::char_('^');
36749bea   Benjamin Renard   Use a common cont...
76
	powerTenOperator_   = qi::char_('e');
a7f2648e   Benjamin Renard   Parser: First imp...
77
        notOperator_        = qi::char_('!');
f5ef5545   Benjamin Renard   Fix operators sym...
78
79
        andOperator_        = qi::string("&");
        orOperator_         = qi::string("|");
f5ef5545   Benjamin Renard   Fix operators sym...
80
        equalOperator_      = qi::string("=");
a7f2648e   Benjamin Renard   Parser: First imp...
81
        unequalOperator_    = qi::string("!=");
e33239bf   Benjamin Renard   Parse parameters
82
83
        sumOperator_        = qi::char_('+');
        differenceOperator_ = qi::char_('-');
a7f2648e   Benjamin Renard   Parser: First imp...
84
85
86
87
88
89
        factorOperator_     = qi::char_("*");
        divisionOperator_   = qi::char_("/");
        greaterOperator_   = qi::char_(">");
        greaterOrEqualOperator_   = qi::string(">=");
        lowerOrEqualOperator_   = qi::string("<=");
        lowerOperator_   = qi::char_("<");
36749bea   Benjamin Renard   Use a common cont...
90
        componentOperator_   = qi::char_(",");
e58d7be4   Benjamin Renard   Parser: Add const...
91
92
93
94

	// Define available constants
	std::map<std::string, std::string> existingConstants = ParserToolbox::getConstants(lProperties);
	for(std::map<std::string, std::string>::iterator it = existingConstants.begin(); it != existingConstants.end(); ++it) {
e33239bf   Benjamin Renard   Parse parameters
95
		definedConstants.add (it->first, it->second);
e58d7be4   Benjamin Renard   Parser: Add const...
96
        }
a7f2648e   Benjamin Renard   Parser: First imp...
97

e33239bf   Benjamin Renard   Parse parameters
98
99
100
101
102
	// Define available params
	std::vector<std::string> existingParams = ParserToolbox::getParameters(lProperties);
	for(std::vector<std::string>::iterator it = existingParams.begin(); it != existingParams.end(); ++it) {
		definedParams.add(*it,*it);
	}
36749bea   Benjamin Renard   Use a common cont...
103
104
105
106
107
108
109
110
111
112
113
114
115
       
        // Defined functions available 
        std::map<std::string,FunctionInfo> existingFunctions = ParserToolbox::getFunctions(lProperties);
        for(auto const& x : existingFunctions ) {
                if (x.second.getNbArgs()==1) {
                      oneArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
                }else if (x.second.getNbArgs()==2) {
                      twoArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
                }else {
                      threeArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
                }
        } 

e33239bf   Benjamin Renard   Parse parameters
116
117
118

        BOOST_SPIRIT_DEBUG_NODES((expr_)(or_)(and_)(equal_)(unequal_)(greaterOrEqual_)(lowerOrEqual_)(lower_)(sum_)
                (difference_)(factor_)(division_)(simple)(notOperator_)(andOperator_)(orOperator_)(equalOperator_)(unequalOperator_)
36749bea   Benjamin Renard   Use a common cont...
119
                (sumOperator_)(differenceOperator_)(factorOperator_)(divisionOperator_)(greater_)(lower_)(functions_));
a7f2648e   Benjamin Renard   Parser: First imp...
120
121
122
    }

private:
e33239bf   Benjamin Renard   Parse parameters
123
    qi::rule<It, Expression::var(), Skipper> var_, vectorArgs_, spectArgs_;;
47755d13   Benjamin Renard   Minor changes
124
    qi::rule<It, Expression::ExpressionContainer(), Skipper> not_
e33239bf   Benjamin Renard   Parse parameters
125
        , pow_
36749bea   Benjamin Renard   Use a common cont...
126
	, powerTen_
a7f2648e   Benjamin Renard   Parser: First imp...
127
        , and_
a7f2648e   Benjamin Renard   Parser: First imp...
128
129
130
131
132
133
134
135
136
137
138
        , or_
        , equal_
        , unequal_
        , sum_
        , difference_
        , factor_
        , division_
        , simple
        , expr_
        ,plusSign_
        ,minusSign_
e58d7be4   Benjamin Renard   Parser: Add const...
139
140
141
142
        ,greater_
        ,greaterOrEqual_
        ,lowerOrEqual_
        ,lower_
e33239bf   Benjamin Renard   Parse parameters
143
	,const_
36749bea   Benjamin Renard   Use a common cont...
144
145
146
	,param_
        ,functions_
        ,funArgs_;
a7f2648e   Benjamin Renard   Parser: First imp...
147
148
149


    qi::rule<It, Skipper> notOperator_
e33239bf   Benjamin Renard   Parse parameters
150
	, powOperator_
36749bea   Benjamin Renard   Use a common cont...
151
	, powerTenOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
152
153
        , andOperator_
        , orOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
154
155
156
157
158
159
160
161
162
        , equalOperator_
        , unequalOperator_
        , sumOperator_
        , differenceOperator_
        , factorOperator_
        , divisionOperator_
        , greaterOperator_ 
        , greaterOrEqualOperator_ 
        ,lowerOrEqualOperator_
36749bea   Benjamin Renard   Use a common cont...
163
        ,componentOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
164
165
        ,lowerOperator_  ;

e33239bf   Benjamin Renard   Parse parameters
166
    qi::symbols<char, std::string> definedConstants;
e33239bf   Benjamin Renard   Parse parameters
167
    qi::symbols<char, std::string> definedParams;
36749bea   Benjamin Renard   Use a common cont...
168
169
170
    qi::symbols<char, std::string> twoArgsFunction;
    qi::symbols<char, std::string> oneArgsFunction;
        qi::symbols<char, std::string> threeArgsFunction;
a7f2648e   Benjamin Renard   Parser: First imp...
171
172
173
174
175
176
};

}
}

#endif