From 013f28cb001b30a3d9d47758cf1a9e6456d5356e Mon Sep 17 00:00:00 2001 From: Henri Menke Date: Fri, 1 Nov 2019 09:34:48 +1300 Subject: [PATCH] BOOST_SPIRIT_NO_PREDEFINED_TERMINALS and formatting --- src/qi/ast.hpp | 3 - src/qi/parser.hpp | 1 + src/qi/parser_def.hpp | 147 +++++++++++++++++++++++-------------- src/x3/parser_def.hpp | 164 ++++++++++++++++++++++++++---------------- 4 files changed, 196 insertions(+), 119 deletions(-) diff --git a/src/qi/ast.hpp b/src/qi/ast.hpp index 4ca56cf386d..d2e161e0685 100644 --- a/src/qi/ast.hpp +++ b/src/qi/ast.hpp @@ -4,7 +4,6 @@ #pragma once -#include #include #include @@ -12,8 +11,6 @@ namespace matheval { -namespace qi = boost::spirit::qi; - namespace ast { struct nil {}; diff --git a/src/qi/parser.hpp b/src/qi/parser.hpp index 7a4846f5331..942e8d4d8e3 100644 --- a/src/qi/parser.hpp +++ b/src/qi/parser.hpp @@ -6,6 +6,7 @@ #include "ast.hpp" +#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS #include #include diff --git a/src/qi/parser_def.hpp b/src/qi/parser_def.hpp index c2e6a341e1f..a179a096c0d 100644 --- a/src/qi/parser_def.hpp +++ b/src/qi/parser_def.hpp @@ -10,6 +10,7 @@ #include "parser.hpp" #include +#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS #include #include @@ -26,54 +27,92 @@ namespace parser { template grammar::grammar() : grammar::base_type(expression) { - constant.add("e", boost::math::constants::e())( - "epsilon", std::numeric_limits::epsilon())( - "phi", boost::math::constants::phi())( - "pi", boost::math::constants::pi()); - ufunc.add("abs", static_cast(&std::abs))( - "acos", static_cast(&std::acos))( - "asin", static_cast(&std::asin))( - "atan", static_cast(&std::atan))( - "ceil", static_cast(&std::ceil))( - "cos", static_cast(&std::cos))( - "cosh", static_cast(&std::cosh))( - "deg", static_cast(&math::deg))( - "exp", static_cast(&std::exp))( - "floor", static_cast(&std::floor))( - "isinf", static_cast(&math::isinf))( - "isnan", static_cast(&math::isnan))( - "log", static_cast(&std::log))( - "log10", static_cast(&std::log10))( - "rad", static_cast(&math::rad))( - "sgn", static_cast(&math::sgn))( - "sin", static_cast(&std::sin))( - "sinh", static_cast(&std::sinh))( - "sqrt", static_cast(&std::sqrt))( - "tan", static_cast(&std::tan))( - "tanh", static_cast(&std::tanh)); - bfunc.add("atan2", static_cast(&std::atan2))( - "pow", static_cast(&std::pow)); - unary_op.add("+", static_cast(&math::plus))( - "-", static_cast(&math::minus))( - "!", static_cast(&math::unary_not)); - additive_op.add("+", static_cast(&math::plus))( - "-", static_cast(&math::minus)); - multiplicative_op.add("*", static_cast(&math::multiplies))( - "/", static_cast(&math::divides))( - "%", static_cast(&std::fmod)); - logical_op.add("&&", - static_cast(&math::logical_and))( - "||", static_cast(&math::logical_or)); - relational_op.add("<", static_cast(&math::less))( - "<=", static_cast(&math::less_equals))( - ">", static_cast(&math::greater))( - ">=", static_cast(&math::greater_equals)); - equality_op.add("==", static_cast(&math::equals))( - "!=", static_cast(&math::not_equals)); - power.add("**", static_cast(&std::pow)); + qi::_2_type _2; + qi::_3_type _3; + qi::_4_type _4; + + qi::alnum_type alnum; + qi::alpha_type alpha; + qi::double_type double_; + qi::lexeme_type lexeme; + qi::raw_type raw; // clang-format off + constant.add + ("e" , boost::math::constants::e()) + ("epsilon", std::numeric_limits::epsilon()) + ("phi" , boost::math::constants::phi()) + ("pi" , boost::math::constants::pi()) + ; + + ufunc.add + ("abs" , static_cast(&std::abs)) + ("acos" , static_cast(&std::acos)) + ("asin" , static_cast(&std::asin)) + ("atan" , static_cast(&std::atan)) + ("ceil" , static_cast(&std::ceil)) + ("cos" , static_cast(&std::cos)) + ("cosh" , static_cast(&std::cosh)) + ("deg" , static_cast(&math::deg)) + ("exp" , static_cast(&std::exp)) + ("floor", static_cast(&std::floor)) + ("isinf", static_cast(&math::isinf)) + ("isnan", static_cast(&math::isnan)) + ("log" , static_cast(&std::log)) + ("log10", static_cast(&std::log10)) + ("rad" , static_cast(&math::rad)) + ("sgn" , static_cast(&math::sgn)) + ("sin" , static_cast(&std::sin)) + ("sinh" , static_cast(&std::sinh)) + ("sqrt" , static_cast(&std::sqrt)) + ("tan" , static_cast(&std::tan)) + ("tanh" , static_cast(&std::tanh)) + ; + + bfunc.add + ("atan2", static_cast(&std::atan2)) + ("pow" , static_cast(&std::pow)) + ; + + unary_op.add + ("+", static_cast(&math::plus)) + ("-", static_cast(&math::minus)) + ("!", static_cast(&math::unary_not)) + ; + + additive_op.add + ("+", static_cast(&math::plus)) + ("-", static_cast(&math::minus)) + ; + + multiplicative_op.add + ("*", static_cast(&math::multiplies)) + ("/", static_cast(&math::divides)) + ("%", static_cast(&std::fmod)) + ; + + logical_op.add + ("&&", static_cast(&math::logical_and)) + ("||", static_cast(&math::logical_or)) + ; + + relational_op.add + ("<" , static_cast(&math::less)) + ("<=", static_cast(&math::less_equals)) + (">" , static_cast(&math::greater)) + (">=", static_cast(&math::greater_equals)) + ; + + equality_op.add + ("==", static_cast(&math::equals)) + ("!=", static_cast(&math::not_equals)) + ; + + power.add + ("**", static_cast(&std::pow)) + ; + expression = logical.alias() ; @@ -97,25 +136,25 @@ grammar::grammar() : grammar::base_type(expression) { multiplicative = factor >> *(multiplicative_op > factor) ; - + factor = primary >> *( power > factor ) ; - + unary = ufunc > '(' > expression > ')' ; - + binary = bfunc > '(' > expression > ',' > expression > ')' ; - + variable = - qi::raw[qi::lexeme[qi::alpha >> *(qi::alnum | '_')]] + raw[lexeme[alpha >> *(alnum | '_')]] ; - + primary = - qi::double_ + double_ | ('(' > expression > ')') | (unary_op > primary) | binary @@ -143,8 +182,8 @@ grammar::grammar() : grammar::base_type(expression) { // error_handler_function(error_handler())( // "Error! Expecting ", qi::_4, qi::_3)); qi::on_error( - expression, boost::phoenix::bind(boost::phoenix::ref(err_handler), - qi::_3, qi::_2, qi::_4)); + expression, + boost::phoenix::bind(boost::phoenix::ref(err_handler), _3, _2, _4)); } } // namespace parser diff --git a/src/x3/parser_def.hpp b/src/x3/parser_def.hpp index 113fef6e852..b56a60aa90b 100644 --- a/src/x3/parser_def.hpp +++ b/src/x3/parser_def.hpp @@ -27,106 +27,148 @@ namespace parser { struct constant_ : x3::symbols { constant_() { - add("e", boost::math::constants::e())( - "epsilon", std::numeric_limits::epsilon())( - "phi", boost::math::constants::phi())( - "pi", boost::math::constants::pi()); + // clang-format off + add + ("e" , boost::math::constants::e()) + ("epsilon", std::numeric_limits::epsilon()) + ("phi" , boost::math::constants::phi()) + ("pi" , boost::math::constants::pi()) + ; + // clang-format on } } constant; struct ufunc_ : x3::symbols { ufunc_() { - add("abs", static_cast(&std::abs))( - "acos", static_cast(&std::acos))( - "acosh", static_cast(&std::acosh))( - "asin", static_cast(&std::asin))( - "asinh", static_cast(&std::asinh))( - "atan", static_cast(&std::atan))( - "atanh", static_cast(&std::atanh))( - "cbrt", static_cast(&std::cbrt))( - "ceil", static_cast(&std::ceil))( - "cos", static_cast(&std::cos))( - "cosh", static_cast(&std::cosh))( - "deg", static_cast(&math::deg))( - "erf", static_cast(&std::erf))( - "erfc", static_cast(&std::erfc))( - "exp", static_cast(&std::exp))( - "exp2", static_cast(&std::exp2))( - "floor", static_cast(&std::floor))( - "isinf", static_cast(&math::isinf))( - "isnan", static_cast(&math::isnan))( - "log", static_cast(&std::log))( - "log2", static_cast(&std::log2))( - "log10", static_cast(&std::log10))( - "rad", static_cast(&math::rad))( - "round", static_cast(&std::round))( - "sgn", static_cast(&math::sgn))( - "sin", static_cast(&std::sin))( - "sinh", static_cast(&std::sinh))( - "sqrt", static_cast(&std::sqrt))( - "tan", static_cast(&std::tan))( - "tanh", static_cast(&std::tanh))( - "tgamma", static_cast(&std::tgamma)); + // clang-format off + add + ("abs" , static_cast(&std::abs)) + ("acos" , static_cast(&std::acos)) + ("acosh" , static_cast(&std::acosh)) + ("asin" , static_cast(&std::asin)) + ("asinh" , static_cast(&std::asinh)) + ("atan" , static_cast(&std::atan)) + ("atanh" , static_cast(&std::atanh)) + ("cbrt" , static_cast(&std::cbrt)) + ("ceil" , static_cast(&std::ceil)) + ("cos" , static_cast(&std::cos)) + ("cosh" , static_cast(&std::cosh)) + ("deg" , static_cast(&math::deg)) + ("erf" , static_cast(&std::erf)) + ("erfc" , static_cast(&std::erfc)) + ("exp" , static_cast(&std::exp)) + ("exp2" , static_cast(&std::exp2)) + ("floor" , static_cast(&std::floor)) + ("isinf" , static_cast(&math::isinf)) + ("isnan" , static_cast(&math::isnan)) + ("log" , static_cast(&std::log)) + ("log2" , static_cast(&std::log2)) + ("log10" , static_cast(&std::log10)) + ("rad" , static_cast(&math::rad)) + ("round" , static_cast(&std::round)) + ("sgn" , static_cast(&math::sgn)) + ("sin" , static_cast(&std::sin)) + ("sinh" , static_cast(&std::sinh)) + ("sqrt" , static_cast(&std::sqrt)) + ("tan" , static_cast(&std::tan)) + ("tanh" , static_cast(&std::tanh)) + ("tgamma", static_cast(&std::tgamma)) + ; + // clang-format on } } ufunc; struct bfunc_ : x3::symbols { bfunc_() { - add("atan2", static_cast(&std::atan2))( - "max", static_cast(&std::fmax))( - "min", static_cast(&std::fmin))( - "pow", static_cast(&std::pow)); + // clang-format off + add + ("atan2", static_cast(&std::atan2)) + ("max" , static_cast(&std::fmax)) + ("min" , static_cast(&std::fmin)) + ("pow" , static_cast(&std::pow)) + ; + // clang-format on } } bfunc; struct unary_op_ : x3::symbols { unary_op_() { - add("+", static_cast(&math::plus))( - "-", static_cast(&math::minus))( - "!", static_cast(&math::unary_not)); + // clang-format off + add + ("+", static_cast(&math::plus)) + ("-", static_cast(&math::minus)) + ("!", static_cast(&math::unary_not)) + ; + // clang-format on } } unary_op; struct additive_op_ : x3::symbols { additive_op_() { - add("+", static_cast(&math::plus))( - "-", static_cast(&math::minus)); + // clang-format off + add + ("+", static_cast(&math::plus)) + ("-", static_cast(&math::minus)) + ; + // clang-format on } } additive_op; struct multiplicative_op_ : x3::symbols { multiplicative_op_() { - add("*", static_cast(&math::multiplies))( - "/", static_cast(&math::divides))( - "%", static_cast(&std::fmod)); + // clang-format off + add + ("*", static_cast(&math::multiplies)) + ("/", static_cast(&math::divides)) + ("%", static_cast(&std::fmod)) + ; + // clang-format on } } multiplicative_op; struct logical_op_ : x3::symbols { logical_op_() { - add("&&", static_cast(&math::logical_and))( - "||", static_cast(&math::logical_or)); + // clang-format off + add + ("&&", static_cast(&math::logical_and)) + ("||", static_cast(&math::logical_or)) + ; + // clang-format on } } logical_op; struct relational_op_ : x3::symbols { relational_op_() { - add("<", static_cast(&math::less))( - "<=", static_cast(&math::less_equals))( - ">", static_cast(&math::greater))( - ">=", static_cast(&math::greater_equals)); + // clang-format off + add + ("<" , static_cast(&math::less)) + ("<=", static_cast(&math::less_equals)) + (">" , static_cast(&math::greater)) + (">=", static_cast(&math::greater_equals)) + ; + // clang-format on } } relational_op; struct equality_op_ : x3::symbols { equality_op_() { - add("==", static_cast(&math::equals))( - "!=", static_cast(&math::not_equals)); + // clang-format off + add + ("==", static_cast(&math::equals)) + ("!=", static_cast(&math::not_equals)) + ; + // clang-format on } } equality_op; struct power_ : x3::symbols { - power_() { add("**", static_cast(&std::pow)); } + power_() { + // clang-format off + add + ("**", static_cast(&std::pow)) + ; + // clang-format on + } } power; // ADL markers @@ -143,9 +185,10 @@ struct unary_class; struct binary_class; struct variable_class; +// clang-format off + // Rule declarations -// clang-format off auto const expression = x3::rule{"expression"}; auto const logical = x3::rule{"logical"}; auto const equality = x3::rule{"equality"}; @@ -157,12 +200,9 @@ auto const primary = x3::rule{"pri auto const unary = x3::rule{"unary"}; auto const binary = x3::rule{"binary"}; auto const variable = x3::rule{"variable"}; -// clang-format on // Rule defintions -// clang-format off - auto const expression_def = logical ; @@ -213,8 +253,6 @@ auto const primary_def = | variable ; -// clang-format on - BOOST_SPIRIT_DEFINE( expression, logical, @@ -229,6 +267,8 @@ BOOST_SPIRIT_DEFINE( variable ) +// clang-format on + struct expression_class { template x3::error_handler_result on_error(Iterator &, Iterator const &last,