]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/compiler_tutorial/calc8/statement_def.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / compiler_tutorial / calc8 / statement_def.hpp
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 "statement.hpp"
8 #include "error_handler.hpp"
9 #include "annotation.hpp"
10
11 namespace client { namespace parser
12 {
13 template <typename Iterator>
14 statement<Iterator>::statement(error_handler<Iterator>& error_handler)
15 : statement::base_type(statement_list), expr(error_handler)
16 {
17 qi::_1_type _1;
18 qi::_2_type _2;
19 qi::_3_type _3;
20 qi::_4_type _4;
21
22 qi::_val_type _val;
23 qi::raw_type raw;
24 qi::lexeme_type lexeme;
25 qi::alpha_type alpha;
26 qi::alnum_type alnum;
27 qi::lit_type lit;
28
29 using qi::on_error;
30 using qi::on_success;
31 using qi::fail;
32 using boost::phoenix::function;
33
34 typedef function<client::error_handler<Iterator> > error_handler_function;
35 typedef function<client::annotation<Iterator> > annotation_function;
36
37 statement_list =
38 +statement_
39 ;
40
41 statement_ =
42 variable_declaration
43 | assignment
44 | compound_statement
45 | if_statement
46 | while_statement
47 ;
48
49 identifier =
50 !expr.keywords
51 >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
52 ;
53
54 variable_declaration =
55 lexeme["var" >> !(alnum | '_')] // make sure we have whole words
56 > &identifier // expect an identifier
57 > assignment
58 ;
59
60 assignment =
61 identifier
62 > '='
63 > expr
64 > ';'
65 ;
66
67 if_statement =
68 lit("if")
69 > '('
70 > expr
71 > ')'
72 > statement_
73 >
74 -(
75 lexeme["else" >> !(alnum | '_')] // make sure we have whole words
76 > statement_
77 )
78 ;
79
80 while_statement =
81 lit("while")
82 > '('
83 > expr
84 > ')'
85 > statement_
86 ;
87
88 compound_statement =
89 '{' >> -statement_list >> '}'
90 ;
91
92 // Debugging and error handling and reporting support.
93 BOOST_SPIRIT_DEBUG_NODES(
94 (statement_list)
95 (identifier)
96 (variable_declaration)
97 (assignment)
98 );
99
100 // Error handling: on error in statement_list, call error_handler.
101 on_error<fail>(statement_list,
102 error_handler_function(error_handler)(
103 "Error! Expecting ", _4, _3));
104
105 // Annotation: on success in assignment, call annotation.
106 on_success(assignment,
107 annotation_function(error_handler.iters)(_val, _1));
108 }
109 }}
110
111