1/*=============================================================================
2 Copyright (c) 2001-2011 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#include "expression.hpp"
8#include "error_handler.hpp"
9#include "annotation.hpp"
10#include <boost/phoenix/function.hpp>
11
12namespace client { namespace parser
13{
14 template <typename Iterator>
15 expression<Iterator>::expression(error_handler<Iterator>& error_handler)
16 : expression::base_type(expr)
17 {
18 qi::_1_type _1;
19 qi::_2_type _2;
20 qi::_3_type _3;
21 qi::_4_type _4;
22
23 qi::char_type char_;
24 qi::uint_type uint_;
25 qi::_val_type _val;
26 qi::raw_type raw;
27 qi::lexeme_type lexeme;
28 qi::alpha_type alpha;
29 qi::alnum_type alnum;
30 qi::bool_type bool_;
31
32 using qi::on_error;
33 using qi::on_success;
34 using qi::fail;
35 using boost::phoenix::function;
36
37 typedef function<client::error_handler<Iterator> > error_handler_function;
38 typedef function<client::annotation<Iterator> > annotation_function;
39
40 ///////////////////////////////////////////////////////////////////////
41 // Tokens
42 binary_op.add
43 ("||", ast::op_logical_or)
44 ("&&", ast::op_logical_and)
45 ("==", ast::op_equal)
46 ("!=", ast::op_not_equal)
47 ("<", ast::op_less)
48 ("<=", ast::op_less_equal)
49 (">", ast::op_greater)
50 (">=", ast::op_greater_equal)
51 ("+", ast::op_plus)
52 ("-", ast::op_minus)
53 ("*", ast::op_times)
54 ("/", ast::op_divide)
55 ;
56
57 unary_op.add
58 ("+", ast::op_positive)
59 ("-", ast::op_negative)
60 ("!", ast::op_not)
61 ;
62
63 keywords.add
64 ("true")
65 ("false")
66 ("if")
67 ("else")
68 ("while")
69 ("int")
70 ("void")
71 ("return")
72 ;
73
74 ///////////////////////////////////////////////////////////////////////
75 // Main expression grammar
76 expr =
77 unary_expr
78 >> *(binary_op > unary_expr)
79 ;
80
81 unary_expr =
82 primary_expr
83 | (unary_op > unary_expr)
84 ;
85
86 primary_expr =
87 uint_
88 | function_call
89 | identifier
90 | bool_
91 | '(' > expr > ')'
92 ;
93
94 function_call =
95 (identifier >> '(')
96 > argument_list
97 > ')'
98 ;
99
100 argument_list = -(expr % ',');
101
102 identifier =
103 !lexeme[keywords >> !(alnum | '_')]
104 >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
105 ;
106
107 ///////////////////////////////////////////////////////////////////////
108 // Debugging and error handling and reporting support.
109 BOOST_SPIRIT_DEBUG_NODES(
110 (expr)
111 (unary_expr)
112 (primary_expr)
113 (function_call)
114 (argument_list)
115 (identifier)
116 );
117
118 ///////////////////////////////////////////////////////////////////////
119 // Error handling: on error in expr, call error_handler.
120 on_error<fail>(expr,
121 error_handler_function(error_handler)(
122 "Error! Expecting ", _4, _3));
123
124 ///////////////////////////////////////////////////////////////////////
125 // Annotation: on success in primary_expr, call annotation.
126 on_success(primary_expr,
127 annotation_function(error_handler.iters)(_val, _1));
128 }
129}}
130
131
132

source code of boost/libs/spirit/example/qi/compiler_tutorial/conjure1/expression_def.hpp