1 /*=============================================================================
2     Copyright (c) 2001-2014 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 #if !defined(BOOST_SPIRIT_X3_CALC8_STATEMENT_DEF_HPP)
8 #define BOOST_SPIRIT_X3_CALC8_STATEMENT_DEF_HPP
9 
10 #include <boost/spirit/home/x3.hpp>
11 #include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
12 #include "ast.hpp"
13 #include "ast_adapted.hpp"
14 #include "statement.hpp"
15 #include "expression.hpp"
16 #include "common.hpp"
17 #include "error_handler.hpp"
18 
19 namespace client { namespace parser
20 {
21     using x3::raw;
22     using x3::lexeme;
23     using namespace x3::ascii;
24 
25     struct statement_list_class;
26     struct variable_declaration_class;
27     struct assignment_class;
28     struct variable_class;
29 
30     typedef x3::rule<statement_list_class, ast::statement_list> statement_list_type;
31     typedef x3::rule<variable_declaration_class, ast::variable_declaration> variable_declaration_type;
32     typedef x3::rule<assignment_class, ast::assignment> assignment_type;
33     typedef x3::rule<variable_class, ast::variable> variable_type;
34 
35     statement_type const statement = "statement";
36     statement_list_type const statement_list = "statement_list";
37     variable_declaration_type const variable_declaration = "variable_declaration";
38     assignment_type const assignment = "assignment";
39     variable_type const variable = "variable";
40 
41     // Import the expression rule
42     namespace { auto const& expression = client::expression(); }
43 
44     auto const statement_list_def =
45         +(variable_declaration | assignment)
46         ;
47 
48     auto const variable_declaration_def =
49             lexeme["var" >> !(alnum | '_')] // make sure we have whole words
50         >   assignment
51         ;
52 
53     auto const assignment_def =
54             variable
55         >   '='
56         >   expression
57         >   ';'
58         ;
59 
60     auto const variable_def = identifier;
61     auto const statement_def = statement_list;
62 
63     BOOST_SPIRIT_DEFINE(
64         statement
65       , statement_list
66       , variable_declaration
67       , assignment
68       , variable
69     );
70 
71     struct statement_class : error_handler_base, x3::annotate_on_success {};
72     struct assignment_class : x3::annotate_on_success {};
73     struct variable_class : x3::annotate_on_success {};
74 }}
75 
76 namespace client
77 {
statement()78     parser::statement_type const& statement()
79     {
80         return parser::statement;
81     }
82 }
83 
84 #endif
85