]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / compiler_tutorial / conjure3 / expression_def.hpp
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/spirit/include/phoenix_function.hpp>
12 #include <boost/spirit/include/lex_plain_token.hpp>
13
14 namespace 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 postfix_expr
49 | (tokenid_mask(token_ids::op_unary) > unary_expr)
50 ;
51
52 postfix_expr =
53 function_call
54 | primary_expr
55 ;
56
57 primary_expr =
58 lexer.lit_uint
59 | lexer.true_or_false
60 | identifier
61 | '(' > expr > ')'
62 ;
63
64 function_call =
65 (identifier >> '(')
66 > argument_list
67 > ')'
68 ;
69
70 argument_list = -(expr % ',');
71
72 identifier = lexer.identifier;
73
74 ///////////////////////////////////////////////////////////////////////
75 // Debugging and error handling and reporting support.
76 BOOST_SPIRIT_DEBUG_NODES(
77 (expr)
78 (unary_expr)
79 (postfix_expr)
80 (primary_expr)
81 (function_call)
82 (argument_list)
83 (identifier)
84 );
85
86 ///////////////////////////////////////////////////////////////////////
87 // Error handling: on error in expr, call error_handler.
88 on_error<fail>(expr,
89 error_handler_function(error_handler)(
90 "Error! Expecting ", _4, _3));
91
92 ///////////////////////////////////////////////////////////////////////
93 // Annotation: on success in unary_expr, postfix_expr,
94 // and primary_expr call annotation.
95 on_success(unary_expr,
96 annotation_function(error_handler.iters)(_val, _1));
97 on_success(postfix_expr,
98 annotation_function(error_handler.iters)(_val, _1));
99 on_success(primary_expr,
100 annotation_function(error_handler.iters)(_val, _1));
101 }
102 }}
103
104