1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "expression.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
#include <boost/phoenix/function.hpp>
namespace client { namespace parser
{
template <typename Iterator>
expression<Iterator>::expression(error_handler<Iterator>& error_handler)
: expression::base_type(expr)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::char_type char_;
qi::uint_type uint_;
qi::_val_type _val;
qi::raw_type raw;
qi::lexeme_type lexeme;
qi::alpha_type alpha;
qi::alnum_type alnum;
qi::bool_type bool_;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef function<client::error_handler<Iterator> > error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
///////////////////////////////////////////////////////////////////////
// Tokens
logical_or_op.add
("||", ast::op_or)
;
logical_and_op.add
("&&", ast::op_and)
;
equality_op.add
("==", ast::op_equal)
("!=", ast::op_not_equal)
;
relational_op.add
("<", ast::op_less)
("<=", ast::op_less_equal)
(">", ast::op_greater)
(">=", ast::op_greater_equal)
;
additive_op.add
("+", ast::op_plus)
("-", ast::op_minus)
;
multiplicative_op.add
("*", ast::op_times)
("/", ast::op_divide)
;
unary_op.add
("+", ast::op_positive)
("-", ast::op_negative)
("!", ast::op_not)
;
keywords.add
("true")
("false")
("if")
("else")
("while")
("int")
("void")
("return")
;
///////////////////////////////////////////////////////////////////////
// Main expression grammar
expr =
logical_or_expr.alias()
;
logical_or_expr =
logical_and_expr
>> *(logical_or_op > logical_and_expr)
;
logical_and_expr =
equality_expr
>> *(logical_and_op > equality_expr)
;
equality_expr =
relational_expr
>> *(equality_op > relational_expr)
;
relational_expr =
additive_expr
>> *(relational_op > additive_expr)
;
additive_expr =
multiplicative_expr
>> *(additive_op > multiplicative_expr)
;
multiplicative_expr =
unary_expr
>> *(multiplicative_op > unary_expr)
;
unary_expr =
primary_expr
| (unary_op > unary_expr)
;
primary_expr =
uint_
| function_call
| identifier
| bool_
| '(' > expr > ')'
;
function_call =
(identifier >> '(')
> argument_list
> ')'
;
argument_list = -(expr % ',');
identifier =
!lexeme[keywords >> !(alnum | '_')]
>> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
///////////////////////////////////////////////////////////////////////
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
(logical_or_expr)
(logical_and_expr)
(equality_expr)
(relational_expr)
(additive_expr)
(multiplicative_expr)
(unary_expr)
(primary_expr)
(function_call)
(argument_list)
(identifier)
);
///////////////////////////////////////////////////////////////////////
// Error handling: on error in expr, call error_handler.
on_error<fail>(expr,
error_handler_function(error_handler)(
"Error! Expecting ", _4, _3));
///////////////////////////////////////////////////////////////////////
// Annotation: on success in primary_expr, call annotation.
on_success(primary_expr,
annotation_function(error_handler.iters)(_val, _1));
}
}}