1 /*=============================================================================
2     Copyright (c) 2001-2014 Joel de Guzman
3 
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_CALC8_EXPRESSION_DEF_HPP)
8 #define BOOST_SPIRIT_X3_CALC8_EXPRESSION_DEF_HPP
9 
10 #include <boost/spirit/home/x3.hpp>
11 #include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
12 #include "ast.hpp"
13 #include "ast_adapted.hpp"
14 #include "expression.hpp"
15 #include "common.hpp"
16 #include "error_handler.hpp"
17 
18 namespace client { namespace parser
19 {
20     using x3::uint_;
21     using x3::char_;
22     using x3::raw;
23     using x3::lexeme;
24     using namespace x3::ascii;
25 
26     struct additive_expr_class;
27     struct multiplicative_expr_class;
28     struct unary_expr_class;
29     struct primary_expr_class;
30 
31     typedef x3::rule<additive_expr_class, ast::expression> additive_expr_type;
32     typedef x3::rule<multiplicative_expr_class, ast::expression> multiplicative_expr_type;
33     typedef x3::rule<unary_expr_class, ast::operand> unary_expr_type;
34     typedef x3::rule<primary_expr_class, ast::operand> primary_expr_type;
35 
36     expression_type const expression = "expression";
37     additive_expr_type const additive_expr = "additive_expr";
38     multiplicative_expr_type const multiplicative_expr = "multiplicative_expr";
39     unary_expr_type unary_expr = "unary_expr";
40     primary_expr_type primary_expr = "primary_expr";
41 
42     auto const additive_expr_def =
43         multiplicative_expr
44         >> *(   (char_('+') > multiplicative_expr)
45             |   (char_('-') > multiplicative_expr)
46             )
47         ;
48 
49     auto const multiplicative_expr_def =
50         unary_expr
51         >> *(   (char_('*') > unary_expr)
52             |   (char_('/') > unary_expr)
53             )
54         ;
55 
56     auto const unary_expr_def =
57             primary_expr
58         |   (char_('-') > primary_expr)
59         |   (char_('+') > primary_expr)
60         ;
61 
62     auto const primary_expr_def =
63             uint_
64         |   identifier
65         |   '(' > expression > ')'
66         ;
67 
68     auto const expression_def = additive_expr;
69 
70     BOOST_SPIRIT_DEFINE(
71         expression
72       , additive_expr
73       , multiplicative_expr
74       , unary_expr
75       , primary_expr
76     );
77 
78     struct unary_expr_class : x3::annotate_on_success {};
79     struct primary_expr_class : x3::annotate_on_success {};
80 
81 }}
82 
83 namespace client
84 {
expression()85     parser::expression_type const& expression()
86     {
87         return parser::expression;
88     }
89 }
90 
91 #endif
92