ParserGrammar.hh 7.27 KB
#ifndef PARSERGRAMMAR_HH_
#define PARSERGRAMMAR_HH_

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

#include "ExpressionContainer.hh"
#include "CustomFoldDirective.hh"
#include "ParserToolbox.hh"
#include "Properties.hh"
#include "FunctionInfo.hh"

namespace AMDA {
namespace parser {

namespace qi = boost::spirit::qi;

using namespace qi;
using namespace Expression;

#define PARSERGRAMMAR_ADD_OPERATOR(operation, next, operator) fold<operation>(next.alias())[operator >> next]

template <typename It, typename Skipper = boost::spirit::standard_wide::space_type>
class ParserGrammar : public qi::grammar<It, Expression::ExpressionContainer(), Skipper>
{
public:
    ParserGrammar(AMDA::helpers::Properties& lProperties) : ParserGrammar::base_type(expr_)
    {
        expr_ = or_.alias();

	// Logical Operators
        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_];
	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_];

        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_) | (-factorOperator_>>differenceOperator_>>-factorOperator_)) > pow_) [_val = boost::phoenix::construct<MinusSignOperation>(_1)] | pow_[_val = _1];

	// Numerical Operators

	pow_ = fold<funcop<op_power> >(greater_.alias())[powOperator_ >> greater_];

        greater_=  fold<GreaterOperation>(lower_.alias())[greaterOperator_ >> lower_];
        lower_ = fold<LowerOperation>( functions_.alias())[lowerOperator_ >> functions_];

        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];
	// UNARY OPERATIONS
	not_ = (notOperator_ > const_) [_val = boost::phoenix::construct<NotOperation>(_1)] | const_[_val = _1];
 
	const_ = (definedConstants) [_val = boost::phoenix::construct<ConstantOperation>(_1)] | param_[_val = _1];
 	param_= (definedParams >>('(' >> (spectArgs_|vectorArgs_)>>')'))[_val='$'+_1+"["+qi::_2+"]"] ||
                definedParams[_val='$'+_1]| simple[_val = _1];

        funArgs_=((expr_   |var_) |functions_); 
        simple = (('(' > expr_ > ')') | var_);
	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_('^');
	powerTenOperator_   = qi::char_('e');
        notOperator_        = qi::char_('!');
        andOperator_        = qi::string("&");
        orOperator_         = qi::string("|");
        equalOperator_      = qi::string("=");
        unequalOperator_    = qi::string("!=");
        sumOperator_        = qi::char_('+');
        differenceOperator_ = qi::char_('-');
        factorOperator_     = qi::char_("*");
        divisionOperator_   = qi::char_("/");
        greaterOperator_   = qi::char_(">");
        greaterOrEqualOperator_   = qi::string(">=");
        lowerOrEqualOperator_   = qi::string("<=");
        lowerOperator_   = qi::char_("<");
        componentOperator_   = qi::char_(",");

	// 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) {
		definedConstants.add (it->first, it->second);
        }

	// 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);
	}
       
        // 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());
                }
        } 


        BOOST_SPIRIT_DEBUG_NODES((expr_)(or_)(and_)(equal_)(unequal_)(greaterOrEqual_)(lowerOrEqual_)(lower_)(sum_)
                (difference_)(factor_)(division_)(simple)(notOperator_)(andOperator_)(orOperator_)(equalOperator_)(unequalOperator_)
                (sumOperator_)(differenceOperator_)(factorOperator_)(divisionOperator_)(greater_)(lower_)(functions_));
    }

private:
    qi::rule<It, Expression::var(), Skipper> var_, vectorArgs_, spectArgs_;;
    qi::rule<It, Expression::ExpressionContainer(), Skipper> not_
        , pow_
	, powerTen_
        , and_
        , or_
        , equal_
        , unequal_
        , sum_
        , difference_
        , factor_
        , division_
        , simple
        , expr_
        ,plusSign_
        ,minusSign_
        ,greater_
        ,greaterOrEqual_
        ,lowerOrEqual_
        ,lower_
	,const_
	,param_
        ,functions_
        ,funArgs_;


    qi::rule<It, Skipper> notOperator_
	, powOperator_
	, powerTenOperator_
        , andOperator_
        , orOperator_
        , equalOperator_
        , unequalOperator_
        , sumOperator_
        , differenceOperator_
        , factorOperator_
        , divisionOperator_
        , greaterOperator_ 
        , greaterOrEqualOperator_ 
        ,lowerOrEqualOperator_
        ,componentOperator_
        ,lowerOperator_  ;

    qi::symbols<char, std::string> definedConstants;
    qi::symbols<char, std::string> definedParams;
    qi::symbols<char, std::string> twoArgsFunction;
    qi::symbols<char, std::string> oneArgsFunction;
        qi::symbols<char, std::string> threeArgsFunction;
};

}
}

#endif