-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_def.h
100 lines (82 loc) · 2.63 KB
/
statement_def.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef STATEMENT_DEF_H
#define STATEMENT_DEF_H
#include <statement.h>
namespace parser {
statement::statement(error_handler& error)
: statement::base_type(statement_list), expr(error), types(expr.types)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
qi::lexeme_type lexeme;
qi::alnum_type alnum;
qi::lit_type lit;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef function<error_handler> error_handler_function;
typedef function<annotation> annotation_function;
statement_list =
+statement_;
statement_ =
declaration
| (expr > ';')
| if_statement
| while_statement
| return_statement
| compound_statement
;
identifier = expr.identifier
;
declaration = expr.type_specifier > -init_declarator > ';';
init_declarator = expr.declarator > -("=" > (expr.allocation_expression | expr.logical_OR_expression));
if_statement =
lit("if")
> '('
> expr.logical_OR_expression
> ')'
> statement_
;
while_statement =
lit("while")
> '('
> expr.logical_OR_expression
> ')'
> statement_
;
compound_statement =
'{' >> -statement_list >> '}'
;
return_statement =
lexeme["return" >> !(alnum | '_')] // make sure we have whole words
> -expr.logical_OR_expression
> ';'
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(statement_list)
(identifier)
(declaration)
(init_declarator)
);
// Error handling
on_error<qi::fail>(statement_list,
error_handler_function(error)(
"Error! Expecting ", _4, _3));
on_error<fail>(declaration,
error_handler_function(error)(
"Error! Expecting ", _4, _3));
on_error<fail>(init_declarator,
error_handler_function(error)(
"Error! Expecting ", _4, _3));
// Annotation on success
SUCCESS_ANNOTATE(identifier);
SUCCESS_ANNOTATE(declaration);
SUCCESS_ANNOTATE(return_statement);
SUCCESS_ANNOTATE(init_declarator);
}
}
#endif // STATEMENT_DEF_H