| 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 "function.hpp" |
| 8 | #include "error_handler.hpp" |
| 9 | #include "annotation.hpp" |
| 10 | |
| 11 | namespace client { namespace parser |
| 12 | { |
| 13 | template <typename Iterator> |
| 14 | function<Iterator>::function(error_handler<Iterator>& error_handler) |
| 15 | : function::base_type(start), body(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::string_type string; |
| 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 | name = |
| 38 | !body.expr.keywords |
| 39 | >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]] |
| 40 | ; |
| 41 | |
| 42 | identifier = name; |
| 43 | argument_list = -(identifier % ','); |
| 44 | |
| 45 | start = |
| 46 | lexeme[(string("void" ) | string("int" )) |
| 47 | >> !(alnum | '_')] // make sure we have whole words |
| 48 | > identifier |
| 49 | > '(' > argument_list > ')' |
| 50 | > '{' > body > '}' |
| 51 | ; |
| 52 | |
| 53 | // Debugging and error handling and reporting support. |
| 54 | BOOST_SPIRIT_DEBUG_NODES( |
| 55 | (identifier) |
| 56 | (argument_list) |
| 57 | (start) |
| 58 | ); |
| 59 | |
| 60 | // Error handling: on error in start, call error_handler. |
| 61 | on_error<fail>(start, |
| 62 | error_handler_function(error_handler)( |
| 63 | "Error! Expecting " , _4, _3)); |
| 64 | |
| 65 | // Annotation: on success in start, call annotation. |
| 66 | on_success(identifier, |
| 67 | annotation_function(error_handler.iters)(_val, _1)); |
| 68 | } |
| 69 | }} |
| 70 | |
| 71 | |
| 72 | |