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
11namespace 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 | return_statement
48 ;
49
50 identifier =
51 !expr.keywords
52 >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
53 ;
54
55 variable_declaration =
56 lexeme["int" >> !(alnum | '_')] // make sure we have whole words
57 > identifier
58 > -('=' > expr)
59 > ';'
60 ;
61
62 assignment =
63 identifier
64 > '='
65 > expr
66 > ';'
67 ;
68
69 if_statement =
70 lit("if")
71 > '('
72 > expr
73 > ')'
74 > statement_
75 >
76 -(
77 lexeme["else" >> !(alnum | '_')] // make sure we have whole words
78 > statement_
79 )
80 ;
81
82 while_statement =
83 lit("while")
84 > '('
85 > expr
86 > ')'
87 > statement_
88 ;
89
90 compound_statement =
91 '{' >> -statement_list >> '}'
92 ;
93
94 return_statement =
95 lexeme["return" >> !(alnum | '_')] // make sure we have whole words
96 > -expr
97 > ';'
98 ;
99
100 // Debugging and error handling and reporting support.
101 BOOST_SPIRIT_DEBUG_NODES(
102 (statement_list)
103 (statement_)
104 (variable_declaration)
105 (assignment)
106 (if_statement)
107 (while_statement)
108 (compound_statement)
109 (return_statement)
110 );
111
112 // Error handling: on error in statement_list, call error_handler.
113 on_error<fail>(statement_list,
114 error_handler_function(error_handler)(
115 "Error! Expecting ", _4, _3));
116
117 // Annotation: on success in variable_declaration,
118 // assignment and return_statement, call annotation.
119 on_success(variable_declaration,
120 annotation_function(error_handler.iters)(_val, _1));
121 on_success(assignment,
122 annotation_function(error_handler.iters)(_val, _1));
123 on_success(return_statement,
124 annotation_function(error_handler.iters)(_val, _1));
125 }
126}}
127
128
129

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