Blame view

src/expressionParser/ParserGrammar.hh 10.3 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
    {
8a7bf6d4   Benjamin Renard   Fix operation pri...
29
        expr_ = equal_.alias();
a7f2648e   Benjamin Renard   Parser: First imp...
30

8a7bf6d4   Benjamin Renard   Fix operation pri...
31
32
33
34
35
36
        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,   greater_,        lowerOrEqualOperator_);
	greater_        = PARSERGRAMMAR_ADD_OPERATOR(GreaterOperation,        lower_,          greaterOperator_);
	lower_          = PARSERGRAMMAR_ADD_OPERATOR(LowerOperation,          or_,             lowerOperator_);
c1f4db8e   Benjamin Renard   Add process to te...
37

8a7bf6d4   Benjamin Renard   Fix operation pri...
38
39
40
41
42
43
	or_             = PARSERGRAMMAR_ADD_OPERATOR(OrOperation,             sum_,            orOperator_);
	sum_            = PARSERGRAMMAR_ADD_OPERATOR(SumOperation,            difference_,     sumOperator_);
	difference_     = PARSERGRAMMAR_ADD_OPERATOR(DifferenceOperation,     and_,            differenceOperator_);
	and_            = PARSERGRAMMAR_ADD_OPERATOR(AndOperation,            factor_,         andOperator_);
        factor_         = PARSERGRAMMAR_ADD_OPERATOR(FactorOperation,         division_,       factorOperator_);
        division_       = PARSERGRAMMAR_ADD_OPERATOR(DivisionOperation,       powerTen_,       divisionOperator_);
c0bbbeb7   Benjamin Renard   Fix some bug with...
44

82a9bc9e   Benjamin Renard   Support d operator
45
46
	powerTen_       = PARSERGRAMMAR_ADD_OPERATOR(PowerTenOperation,       powerTenD_,      powerTenOperator_);
	powerTenD_      = PARSERGRAMMAR_ADD_OPERATOR(PowerTenDOperation,      pow_,            powerTenDOperator_);
02869c99   Benjamin Renard   Improve parse of ...
47
	pow_            = PARSERGRAMMAR_ADD_OPERATOR(PowerOperation,          plusSign_,       powOperator_);
e33239bf   Benjamin Renard   Parse parameters
48

36749bea   Benjamin Renard   Use a common cont...
49
50
        plusSign_ = ((sumOperator_ >>-*sumOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] ||
                ((differenceOperator_ >>differenceOperator_)> minusSign_ ) [_val = boost::phoenix::construct<PlusSignOperation>(_1)] |minusSign_ [_val = _1];
02869c99   Benjamin Renard   Improve parse of ...
51
        minusSign_ = (((-*sumOperator_>>differenceOperator_ >>-*sumOperator_) ) > not_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | not_[_val = _1];
8a7bf6d4   Benjamin Renard   Fix operation pri...
52
	not_ = (notOperator_ > functions_) [_val = boost::phoenix::construct<NotOperation>(_1)] | functions_[_val = _1];
a7f2648e   Benjamin Renard   Parser: First imp...
53

e33239bf   Benjamin Renard   Parse parameters
54

a7f2648e   Benjamin Renard   Parser: First imp...
55

66aeb1bf   Benjamin Renard   First step to sup...
56
57
        functions_ = (fourArgsFunction>>"(">>funArgs_>>argumentSeparator_>>funArgs_>>argumentSeparator_>>funArgs_>>argumentSeparator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4,_5)] ||
        (threeArgsFunction>>"(">>funArgs_>>argumentSeparator_>>funArgs_>>argumentSeparator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3,_4)] ||
02869c99   Benjamin Renard   Improve parse of ...
58
        (twoArgsFunction>>"(">>funArgs_>>argumentSeparator_>>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2,_3)]||
8a7bf6d4   Benjamin Renard   Fix operation pri...
59
        (oneArgsFunction>>"(">>funArgs_>>")")[_val= boost::phoenix::construct<FunctionOperation>(_1,_2)]|const_[_val=_1];
a7f2648e   Benjamin Renard   Parser: First imp...
60
	// UNARY OPERATIONS
e33239bf   Benjamin Renard   Parse parameters
61
 
36749bea   Benjamin Renard   Use a common cont...
62
	const_ = (definedConstants) [_val = boost::phoenix::construct<ConstantOperation>(_1)] | param_[_val = _1];
93f280a8   Benjamin Renard   Implements templa...
63
64
65
        param_= ((localParams_ | derivedParam_ | uploadedParam_ | impexParam_ | themisParam_ | cdaWebParam_ | templatedParam_) >>('(' >> componentValue_ >> componentSeparator_ >> componentValue_ >>')'))[_val=boost::phoenix::construct<ParameterDef>(_1,_2,_3)] ||
		((localParams_ | derivedParam_ | uploadedParam_ | impexParam_ | themisParam_ | cdaWebParam_ | templatedParam_) >>('(' >> componentValue_ >> ')'))[_val=boost::phoenix::construct<ParameterDef>(_1,_2)] ||
		((localParams_ | derivedParam_ | uploadedParam_ | impexParam_ | themisParam_ | cdaWebParam_ | templatedParam_))[_val=boost::phoenix::construct<ParameterDef>(_1)] | simple[_val = _1];
e33239bf   Benjamin Renard   Parse parameters
66

ea6be348   Benjamin Renard   Support arguments...
67
        funArgs_=((expr_   |var_ | definedArgsListValues) |functions_); 
a7f2648e   Benjamin Renard   Parser: First imp...
68
        simple = (('(' > expr_ > ')') | var_);
04d84599   Benjamin Renard   Parser: Add suppo...
69

e33239bf   Benjamin Renard   Parse parameters
70
	var_ = (+qi::char_('0','9') >> -qi::char_('.') >> -(+qi::char_('0','9'))) | ((qi::char_('.') >> +qi::char_('0','9')));
04d84599   Benjamin Renard   Parser: Add suppo...
71
72
	componentValue_ = (+qi::char_('0','9') | qi::char_('*') | (qi::string("range[") >> var_ >> qi::char_(',') >> var_ >> qi::string("]")) | (qi::string("indexes[") >> indexValue_ >> qi::char_(',') >> indexValue_ >> qi::string("]")));
	indexValue_ = +qi::char_('0','9');
c1f4db8e   Benjamin Renard   Add process to te...
73

9d0730bf   Benjamin Renard   Parser: add suppo...
74
75
76
77
78
79
	derivedParam_ = (qi::string("ws_") >> +(qi::alnum | qi::char_('_')));
	uploadedParam_ = (qi::string("wsd_") >> +(qi::alnum | qi::char_('_')));
	impexParam_ = (qi::string("spase___IMPEX_") >> +(qi::alnum | qi::char_('_')));
	themisParam_ = (qi::string("th") >> qi::char_('a','e') >> qi::char_('_') >> +(qi::alnum | qi::char_('_')));
	cdaWebParam_ = (qi::string("cdaweb_") >> +(qi::alnum | qi::char_('_')));

e33239bf   Benjamin Renard   Parse parameters
80
	powOperator_        = qi::char_('^');
36749bea   Benjamin Renard   Use a common cont...
81
	powerTenOperator_   = qi::char_('e');
82a9bc9e   Benjamin Renard   Support d operator
82
	powerTenDOperator_  = qi::char_('d');
a7f2648e   Benjamin Renard   Parser: First imp...
83
        notOperator_        = qi::char_('!');
f5ef5545   Benjamin Renard   Fix operators sym...
84
85
        andOperator_        = qi::string("&");
        orOperator_         = qi::string("|");
f5ef5545   Benjamin Renard   Fix operators sym...
86
        equalOperator_      = qi::string("=");
a7f2648e   Benjamin Renard   Parser: First imp...
87
        unequalOperator_    = qi::string("!=");
e33239bf   Benjamin Renard   Parse parameters
88
89
        sumOperator_        = qi::char_('+');
        differenceOperator_ = qi::char_('-');
a7f2648e   Benjamin Renard   Parser: First imp...
90
91
92
93
94
95
        factorOperator_     = qi::char_("*");
        divisionOperator_   = qi::char_("/");
        greaterOperator_   = qi::char_(">");
        greaterOrEqualOperator_   = qi::string(">=");
        lowerOrEqualOperator_   = qi::string("<=");
        lowerOperator_   = qi::char_("<");
02869c99   Benjamin Renard   Improve parse of ...
96
97
	argumentSeparator_    = qi::char_(",");
        componentSeparator_   = qi::char_(",");
e58d7be4   Benjamin Renard   Parser: Add const...
98
99
100
101

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

9d0730bf   Benjamin Renard   Parser: add suppo...
105
	// Define available local params
e33239bf   Benjamin Renard   Parse parameters
106
	std::vector<std::string> existingParams = ParserToolbox::getParameters(lProperties);
8048fba8   Hacene SI HADJ MOHAND   8893
107
                    const char *otherPatterns[5] = { "ws_", "wsd", "spase___IMPEX_", "th", "cdaweb_"};
e33239bf   Benjamin Renard   Parse parameters
108
	for(std::vector<std::string>::iterator it = existingParams.begin(); it != existingParams.end(); ++it) {
8048fba8   Hacene SI HADJ MOHAND   8893
109
110
111
112
113
114
                               bool isLocal = true; 
                               for (auto patt : otherPatterns){
                                             if (it->rfind(patt, 0) == 0) 
                                                 isLocal = false;
                                         }
                              if(isLocal)
9d0730bf   Benjamin Renard   Parser: add suppo...
115
		localParams_.add(*it,*it);
e33239bf   Benjamin Renard   Parse parameters
116
	}
36749bea   Benjamin Renard   Use a common cont...
117
118
119
120
       
        // Defined functions available 
        std::map<std::string,FunctionInfo> existingFunctions = ParserToolbox::getFunctions(lProperties);
        for(auto const& x : existingFunctions ) {
66aeb1bf   Benjamin Renard   First step to sup...
121
122
		int nbAllArgs = x.second.getNbArgs() + x.second.getNbPromptedArgs();
                if (nbAllArgs == 1) {
36749bea   Benjamin Renard   Use a common cont...
123
                      oneArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
66aeb1bf   Benjamin Renard   First step to sup...
124
                }else if (nbAllArgs == 2) {
36749bea   Benjamin Renard   Use a common cont...
125
                      twoArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
66aeb1bf   Benjamin Renard   First step to sup...
126
                }else if (nbAllArgs == 3) {
36749bea   Benjamin Renard   Use a common cont...
127
                      threeArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
66aeb1bf   Benjamin Renard   First step to sup...
128
129
130
                }else if (nbAllArgs == 4) {
                      fourArgsFunction.add(x.second.getIHMName(), x.second.getKernelName());
		}
36749bea   Benjamin Renard   Use a common cont...
131
132
        } 

ea6be348   Benjamin Renard   Support arguments...
133
134
135
136
137
138
139
140
        // Defined available values for functions args list
        std::map<std::string,ArgListInfo> existingArgsLists = ParserToolbox::getArgsLists(lProperties);
	for(auto const& x : existingArgsLists ) {
		for(auto const& y : x.second.getValues()) {
			definedArgsListValues.add(y.first,y.first);
		}
	}

93f280a8   Benjamin Renard   Implements templa...
141
142
143
144
145
146
	// Defined templated parameters
	TemplateParamsList existingTemplateParams = ParserToolbox::getTemplateParams(lProperties);
	templatedParam_ = (qi::string("fakeparam"));
	for(auto const& t : existingTemplateParams ) {
		templatedParam_ = (templatedParam_.copy() | (qi::string(t.getParamId()) >> +(qi::alnum | qi::char_('_'))));;
	}
e33239bf   Benjamin Renard   Parse parameters
147
148
149

        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...
150
                (sumOperator_)(differenceOperator_)(factorOperator_)(divisionOperator_)(greater_)(lower_)(functions_));
a7f2648e   Benjamin Renard   Parser: First imp...
151
152
153
    }

private:
93f280a8   Benjamin Renard   Implements templa...
154
    qi::rule<It, Expression::var(), Skipper> var_, componentValue_, indexValue_, derivedParam_, uploadedParam_, impexParam_, themisParam_, cdaWebParam_, templatedParam_;
47755d13   Benjamin Renard   Minor changes
155
    qi::rule<It, Expression::ExpressionContainer(), Skipper> not_
e33239bf   Benjamin Renard   Parse parameters
156
        , pow_
36749bea   Benjamin Renard   Use a common cont...
157
	, powerTen_
82a9bc9e   Benjamin Renard   Support d operator
158
	, powerTenD_
a7f2648e   Benjamin Renard   Parser: First imp...
159
        , and_
a7f2648e   Benjamin Renard   Parser: First imp...
160
161
162
163
164
165
166
167
168
169
170
        , or_
        , equal_
        , unequal_
        , sum_
        , difference_
        , factor_
        , division_
        , simple
        , expr_
        ,plusSign_
        ,minusSign_
e58d7be4   Benjamin Renard   Parser: Add const...
171
172
173
174
        ,greater_
        ,greaterOrEqual_
        ,lowerOrEqual_
        ,lower_
e33239bf   Benjamin Renard   Parse parameters
175
	,const_
66aeb1bf   Benjamin Renard   First step to sup...
176
	,frame_
36749bea   Benjamin Renard   Use a common cont...
177
178
179
	,param_
        ,functions_
        ,funArgs_;
a7f2648e   Benjamin Renard   Parser: First imp...
180
181
182


    qi::rule<It, Skipper> notOperator_
e33239bf   Benjamin Renard   Parse parameters
183
	, powOperator_
36749bea   Benjamin Renard   Use a common cont...
184
	, powerTenOperator_
82a9bc9e   Benjamin Renard   Support d operator
185
	, powerTenDOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
186
187
        , andOperator_
        , orOperator_
a7f2648e   Benjamin Renard   Parser: First imp...
188
189
190
191
192
193
194
195
        , equalOperator_
        , unequalOperator_
        , sumOperator_
        , differenceOperator_
        , factorOperator_
        , divisionOperator_
        , greaterOperator_ 
        , greaterOrEqualOperator_ 
02869c99   Benjamin Renard   Improve parse of ...
196
197
198
199
        , lowerOrEqualOperator_
	, lowerOperator_
	, argumentSeparator_
        , componentSeparator_;
a7f2648e   Benjamin Renard   Parser: First imp...
200

e33239bf   Benjamin Renard   Parse parameters
201
    qi::symbols<char, std::string> definedConstants;
ea6be348   Benjamin Renard   Support arguments...
202
    qi::symbols<char, std::string> definedArgsListValues;
9d0730bf   Benjamin Renard   Parser: add suppo...
203
    qi::symbols<char, std::string> localParams_;
36749bea   Benjamin Renard   Use a common cont...
204
205
    qi::symbols<char, std::string> twoArgsFunction;
    qi::symbols<char, std::string> oneArgsFunction;
e8489ddc   Benjamin Renard   Fix printer for a...
206
    qi::symbols<char, std::string> threeArgsFunction;
66aeb1bf   Benjamin Renard   First step to sup...
207
    qi::symbols<char, std::string> fourArgsFunction;
a7f2648e   Benjamin Renard   Parser: First imp...
208
209
210
211
212
213
};

}
}

#endif