1/*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7=============================================================================*/
8#include "expression.hpp"
9#include "error_handler.hpp"
10#include "annotation.hpp"
11#include <boost/phoenix/function.hpp>
12#include <boost/spirit/include/lex_plain_token.hpp>
13
14namespace client { namespace parser
15{
16 template <typename Iterator, typename Lexer>
17 expression<Iterator, Lexer>::expression(
18 error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
19 , Lexer const& l)
20 : expression::base_type(expr), lexer(l)
21 {
22 qi::_1_type _1;
23 qi::_2_type _2;
24 qi::_3_type _3;
25 qi::_4_type _4;
26
27 qi::_val_type _val;
28 qi::tokenid_mask_type tokenid_mask;
29
30 using qi::on_error;
31 using qi::on_success;
32 using qi::fail;
33 using boost::phoenix::function;
34
35 typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
36 error_handler_type;
37 typedef function<error_handler_type> error_handler_function;
38 typedef function<client::annotation<Iterator> > annotation_function;
39
40 ///////////////////////////////////////////////////////////////////////
41 // Main expression grammar
42 expr =
43 unary_expr
44 >> *(tokenid_mask(token_ids::op_binary) > unary_expr)
45 ;
46
47 unary_expr =
48 primary_expr
49 | (tokenid_mask(token_ids::op_unary) > unary_expr)
50 ;
51
52 primary_expr =
53 lexer.lit_uint
54 | function_call
55 | identifier
56 | lexer.true_or_false
57 | '(' > expr > ')'
58 ;
59
60 function_call =
61 (identifier >> '(')
62 > argument_list
63 > ')'
64 ;
65
66 argument_list = -(expr % ',');
67
68 identifier = lexer.identifier;
69
70 ///////////////////////////////////////////////////////////////////////
71 // Debugging and error handling and reporting support.
72 BOOST_SPIRIT_DEBUG_NODES(
73 (expr)
74 (unary_expr)
75 (primary_expr)
76 (function_call)
77 (argument_list)
78 (identifier)
79 );
80
81 ///////////////////////////////////////////////////////////////////////
82 // Error handling: on error in expr, call error_handler.
83 on_error<fail>(expr,
84 error_handler_function(error_handler)(
85 "Error! Expecting ", _4, _3));
86
87 ///////////////////////////////////////////////////////////////////////
88 // Annotation: on success in primary_expr, call annotation.
89 on_success(primary_expr,
90 annotation_function(error_handler.iters)(_val, _1));
91 }
92}}
93
94
95

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