1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(BOOST_SPIRIT_X3_CALC9_EXPRESSION_DEF_HPP)
8 #define BOOST_SPIRIT_X3_CALC9_EXPRESSION_DEF_HPP
10 #include <boost/spirit/home/x3.hpp>
11 #include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
13 #include "ast_adapted.hpp"
14 #include "expression.hpp"
16 #include "error_handler.hpp"
18 namespace client { namespace parser
25 using namespace x3::ascii;
27 ////////////////////////////////////////////////////////////////////////////
29 ////////////////////////////////////////////////////////////////////////////
31 x3::symbols<ast::optoken> equality_op;
32 x3::symbols<ast::optoken> relational_op;
33 x3::symbols<ast::optoken> logical_op;
34 x3::symbols<ast::optoken> additive_op;
35 x3::symbols<ast::optoken> multiplicative_op;
36 x3::symbols<ast::optoken> unary_op;
37 x3::symbols<> keywords;
41 static bool once = false;
53 ("!=", ast::op_not_equal)
58 ("<=", ast::op_less_equal)
59 (">", ast::op_greater)
60 (">=", ast::op_greater_equal)
74 ("+", ast::op_positive)
75 ("-", ast::op_negative)
89 ////////////////////////////////////////////////////////////////////////////
90 // Main expression grammar
91 ////////////////////////////////////////////////////////////////////////////
93 struct equality_expr_class;
94 struct relational_expr_class;
95 struct logical_expr_class;
96 struct additive_expr_class;
97 struct multiplicative_expr_class;
98 struct unary_expr_class;
99 struct primary_expr_class;
101 typedef x3::rule<equality_expr_class, ast::expression> equality_expr_type;
102 typedef x3::rule<relational_expr_class, ast::expression> relational_expr_type;
103 typedef x3::rule<logical_expr_class, ast::expression> logical_expr_type;
104 typedef x3::rule<additive_expr_class, ast::expression> additive_expr_type;
105 typedef x3::rule<multiplicative_expr_class, ast::expression> multiplicative_expr_type;
106 typedef x3::rule<unary_expr_class, ast::operand> unary_expr_type;
107 typedef x3::rule<primary_expr_class, ast::operand> primary_expr_type;
109 expression_type const expression = "expression";
110 equality_expr_type const equality_expr = "equality_expr";
111 relational_expr_type const relational_expr = "relational_expr";
112 logical_expr_type const logical_expr = "logical_expr";
113 additive_expr_type const additive_expr = "additive_expr";
114 multiplicative_expr_type const multiplicative_expr = "multiplicative_expr";
115 unary_expr_type const unary_expr = "unary_expr";
116 primary_expr_type const primary_expr = "primary_expr";
118 auto const logical_expr_def =
120 >> *(logical_op > equality_expr)
123 auto const equality_expr_def =
125 >> *(equality_op > relational_expr)
128 auto const relational_expr_def =
130 >> *(relational_op > additive_expr)
133 auto const additive_expr_def =
135 >> *(additive_op > multiplicative_expr)
138 auto const multiplicative_expr_def =
140 >> *(multiplicative_op > unary_expr)
143 auto const unary_expr_def =
145 | (unary_op > primary_expr)
148 auto const primary_expr_def =
151 | (!keywords >> identifier)
152 | '(' > expression > ')'
155 auto const expression_def = logical_expr;
163 , multiplicative_expr
168 struct unary_expr_class : x3::annotate_on_success {};
169 struct primary_expr_class : x3::annotate_on_success {};
175 parser::expression_type const& expression()
177 parser::add_keywords();
178 return parser::expression;