| 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 "statement.hpp" |
| 9 | #include "error_handler.hpp" |
| 10 | #include "annotation.hpp" |
| 11 | |
| 12 | namespace client { namespace parser |
| 13 | { |
| 14 | template <typename Iterator, typename Lexer> |
| 15 | statement<Iterator, Lexer>::statement( |
| 16 | error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler |
| 17 | , Lexer const& l) |
| 18 | : statement::base_type(statement_list), expr(error_handler, l) |
| 19 | { |
| 20 | qi::_1_type _1; |
| 21 | qi::_2_type _2; |
| 22 | qi::_3_type _3; |
| 23 | qi::_4_type _4; |
| 24 | |
| 25 | qi::_val_type _val; |
| 26 | |
| 27 | using qi::on_error; |
| 28 | using qi::on_success; |
| 29 | using qi::fail; |
| 30 | using boost::phoenix::function; |
| 31 | |
| 32 | typedef client::error_handler<typename Lexer::base_iterator_type, Iterator> |
| 33 | error_handler_type; |
| 34 | typedef function<error_handler_type> 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 | variable_declaration = |
| 51 | l("int" ) |
| 52 | > expr.identifier |
| 53 | > -('=' > expr) |
| 54 | > ';' |
| 55 | ; |
| 56 | |
| 57 | assignment = |
| 58 | expr.identifier |
| 59 | > '=' |
| 60 | > expr |
| 61 | > ';' |
| 62 | ; |
| 63 | |
| 64 | if_statement = |
| 65 | l("if" ) |
| 66 | > '(' |
| 67 | > expr |
| 68 | > ')' |
| 69 | > statement_ |
| 70 | > |
| 71 | -( |
| 72 | l("else" ) |
| 73 | > statement_ |
| 74 | ) |
| 75 | ; |
| 76 | |
| 77 | while_statement = |
| 78 | l("while" ) |
| 79 | > '(' |
| 80 | > expr |
| 81 | > ')' |
| 82 | > statement_ |
| 83 | ; |
| 84 | |
| 85 | compound_statement = |
| 86 | '{' >> -statement_list >> '}' |
| 87 | ; |
| 88 | |
| 89 | return_statement = |
| 90 | l("return" ) |
| 91 | > -expr |
| 92 | > ';' |
| 93 | ; |
| 94 | |
| 95 | // Debugging and error handling and reporting support. |
| 96 | BOOST_SPIRIT_DEBUG_NODES( |
| 97 | (statement_list) |
| 98 | (statement_) |
| 99 | (variable_declaration) |
| 100 | (assignment) |
| 101 | (if_statement) |
| 102 | (while_statement) |
| 103 | (compound_statement) |
| 104 | (return_statement) |
| 105 | ); |
| 106 | |
| 107 | // Error handling: on error in statement_list, call error_handler. |
| 108 | on_error<fail>(statement_list, |
| 109 | error_handler_function(error_handler)( |
| 110 | "Error! Expecting " , _4, _3)); |
| 111 | |
| 112 | // Annotation: on success in variable_declaration, |
| 113 | // assignment and return_statement, call annotation. |
| 114 | on_success(variable_declaration, |
| 115 | annotation_function(error_handler.iters)(_val, _1)); |
| 116 | on_success(assignment, |
| 117 | annotation_function(error_handler.iters)(_val, _1)); |
| 118 | on_success(return_statement, |
| 119 | annotation_function(error_handler.iters)(_val, _1)); |
| 120 | } |
| 121 | }} |
| 122 | |
| 123 | |
| 124 | |