function_def.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include "function.hpp"
  7. #include "error_handler.hpp"
  8. #include "annotation.hpp"
  9. namespace client { namespace parser
  10. {
  11. template <typename Iterator>
  12. function<Iterator>::function(error_handler<Iterator>& error_handler)
  13. : function::base_type(start), body(error_handler)
  14. {
  15. qi::_1_type _1;
  16. qi::_2_type _2;
  17. qi::_3_type _3;
  18. qi::_4_type _4;
  19. qi::_val_type _val;
  20. qi::raw_type raw;
  21. qi::lexeme_type lexeme;
  22. qi::alpha_type alpha;
  23. qi::alnum_type alnum;
  24. qi::string_type string;
  25. using qi::on_error;
  26. using qi::on_success;
  27. using qi::fail;
  28. using boost::phoenix::function;
  29. typedef function<client::error_handler<Iterator> > error_handler_function;
  30. typedef function<client::annotation<Iterator> > annotation_function;
  31. name =
  32. !body.expr.keywords
  33. >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
  34. ;
  35. identifier = name;
  36. argument_list = -(identifier % ',');
  37. start =
  38. lexeme[(string("void") | string("int"))
  39. >> !(alnum | '_')] // make sure we have whole words
  40. > identifier
  41. > '(' > argument_list > ')'
  42. > '{' > body > '}'
  43. ;
  44. // Debugging and error handling and reporting support.
  45. BOOST_SPIRIT_DEBUG_NODES(
  46. (identifier)
  47. (argument_list)
  48. (start)
  49. );
  50. // Error handling: on error in start, call error_handler.
  51. on_error<fail>(start,
  52. error_handler_function(error_handler)(
  53. "Error! Expecting ", _4, _3));
  54. // Annotation: on success in start, call annotation.
  55. on_success(identifier,
  56. annotation_function(error_handler.iters)(_val, _1));
  57. }
  58. }}