Blame view

src/expressionParser/ParserGrammar.hh 7.15 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
21

namespace AMDA {
namespace parser {

namespace qi = boost::spirit::qi;

using namespace qi;
using namespace Expression;

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

	// Logical Operators
36749bea   Benjamin Renard   Use a common cont...
30
31
32
33
34
35
        or_ = fold<OrOperation>(and_.alias())[orOperator_ >> and_];
        and_ = fold<AndOperation>(equal_.alias())[andOperator_ >> equal_];
        equal_ = fold<EqualOperation>(unequal_.alias())[equalOperator_ >> unequal_]; 
        unequal_ = fold<UnequalOperation>(greaterOrEqual_.alias())[unequalOperator_ >>greaterOrEqual_];
        greaterOrEqual_ = fold<GreaterOrEqualOperation>(lowerOrEqual_.alias())[greaterOrEqualOperator_ >> lowerOrEqual_];
        lowerOrEqual_ = fold<LowerOrEqualOperation>(plusSign_.alias())[lowerOrEqualOperator_ >> plusSign_];                        
e33239bf   Benjamin Renard   Parse parameters
36

36749bea   Benjamin Renard   Use a common cont...
37
38
39
        plusSign_ = ((sumOperator_ >>-*sumOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] ||
                ((differenceOperator_ >>differenceOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] |minusSign_ [_val = _1];
        minusSign_ = (-*sumOperator_>>differenceOperator_ >>-*sumOperator_> sum_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | sum_[_val = _1];
a7f2648e   Benjamin Renard   Parser: First imp...
40

a7f2648e   Benjamin Renard   Parser: First imp...
41
	// Numerical Operators
36749bea   Benjamin Renard   Use a common cont...
42
43
44
45
46
	sum_ = fold<SumOperation>(difference_.alias())[sumOperator_ >> difference_];
        difference_ = fold<DifferenceOperation>(powerTen_.alias())[differenceOperator_ >> powerTen_];
	powerTen_ = fold<PowerTenOperation>(division_.alias())[powerTenOperator_ >> division_];
        factor_ = fold<FactorOperation>(division_.alias())[factorOperator_ >> division_]; 
        division_ = fold<DivisionOperation>(pow_.alias())[divisionOperator_ >> pow_];
e33239bf   Benjamin Renard   Parse parameters
47
48

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

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

36749bea   Benjamin Renard   Use a common cont...
53
54
55
        functions_ = (threeArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<Expression::forop <fun_three>>(_1,_2,_3,_4)] ||
        (twoArgsFunction>>"(">>funArgs_>>componentOperator_>>funArgs_>>")")[_val= boost::phoenix::construct<Expression::triop <fun_two>>(_1,_2,_3)]||
        (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<Expression::binop <fun_one>>(_1,_2)]|not_[_val=_1];
a7f2648e   Benjamin Renard   Parser: First imp...
56
	// UNARY OPERATIONS
36749bea   Benjamin Renard   Use a common cont...
57
	not_ = (notOperator_ > const_) [_val = boost::phoenix::construct<NotOperation>(_1)] | const_[_val = _1];
e33239bf   Benjamin Renard   Parse parameters
58
 
36749bea   Benjamin Renard   Use a common cont...
59
	const_ = (definedConstants) [_val = boost::phoenix::construct<ConstantOperation>(_1)] | param_[_val = _1];
e33239bf   Benjamin Renard   Parse parameters
60
61
62
 	param_= (definedParams >>('(' >> (spectArgs_|vectorArgs_)>>')'))[_val='$'+_1+"["+qi::_2+"]"] ||
                definedParams[_val='$'+_1]| simple[_val = _1];

36749bea   Benjamin Renard   Use a common cont...
63
        funArgs_=((expr_   |var_) |functions_); 
a7f2648e   Benjamin Renard   Parser: First imp...
64
        simple = (('(' > expr_ > ')') | var_);
e33239bf   Benjamin Renard   Parse parameters
65
66
67
68
	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_)];
	powOperator_        = qi::char_('^');
36749bea   Benjamin Renard   Use a common cont...
69
	powerTenOperator_   = qi::char_('e');
a7f2648e   Benjamin Renard   Parser: First imp...
70
        notOperator_        = qi::char_('!');
f5ef5545   Benjamin Renard   Fix operators sym...
71
72
        andOperator_        = qi::string("&");
        orOperator_         = qi::string("|");
f5ef5545   Benjamin Renard   Fix operators sym...
73
        equalOperator_      = qi::string("=");
a7f2648e   Benjamin Renard   Parser: First imp...
74
        unequalOperator_    = qi::string("!=");
e33239bf   Benjamin Renard   Parse parameters
75
76
        sumOperator_        = qi::char_('+');
        differenceOperator_ = qi::char_('-');
a7f2648e   Benjamin Renard   Parser: First imp...
77
78
79
80
81
82
        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...
83
        componentOperator_   = qi::char_(",");
e58d7be4   Benjamin Renard   Parser: Add const...
84
85
86
87

	// 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
88
		definedConstants.add (it->first, it->second);
e58d7be4   Benjamin Renard   Parser: Add const...
89
        }
a7f2648e   Benjamin Renard   Parser: First imp...
90

e33239bf   Benjamin Renard   Parse parameters
91
92
93
94
95
	// 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...
96
97
98
99
100
101
102
103
104
105
106
107
108
       
        // 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
109
110
111

        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...
112
                (sumOperator_)(differenceOperator_)(factorOperator_)(divisionOperator_)(greater_)(lower_)(functions_));
a7f2648e   Benjamin Renard   Parser: First imp...
113
114
115
    }

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


    qi::rule<It, Skipper> notOperator_
e33239bf   Benjamin Renard   Parse parameters
143
	, powOperator_
36749bea   Benjamin Renard   Use a common cont...
144
	, powerTenOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
145
146
        , andOperator_
        , orOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
147
148
149
150
151
152
153
154
155
        , equalOperator_
        , unequalOperator_
        , sumOperator_
        , differenceOperator_
        , factorOperator_
        , divisionOperator_
        , greaterOperator_ 
        , greaterOrEqualOperator_ 
        ,lowerOrEqualOperator_
36749bea   Benjamin Renard   Use a common cont...
156
        ,componentOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
157
158
        ,lowerOperator_  ;

e33239bf   Benjamin Renard   Parse parameters
159
    qi::symbols<char, std::string> definedConstants;
e33239bf   Benjamin Renard   Parse parameters
160
    qi::symbols<char, std::string> definedParams;
36749bea   Benjamin Renard   Use a common cont...
161
162
163
    qi::symbols<char, std::string> twoArgsFunction;
    qi::symbols<char, std::string> oneArgsFunction;
        qi::symbols<char, std::string> threeArgsFunction;
a7f2648e   Benjamin Renard   Parser: First imp...
164
165
166
167
168
169
};

}
}

#endif