expression_def.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  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 "expression.hpp"
  8. #include "error_handler.hpp"
  9. #include "annotation.hpp"
  10. #include <boost/spirit/include/phoenix_function.hpp>
  11. #include <boost/spirit/include/lex_plain_token.hpp>
  12. namespace client { namespace parser
  13. {
  14. template <typename Iterator, typename Lexer>
  15. expression<Iterator, Lexer>::expression(
  16. error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
  17. , Lexer const& l)
  18. : expression::base_type(expr), lexer(l)
  19. {
  20. qi::_1_type _1;
  21. qi::_2_type _2;
  22. qi::_3_type _3;
  23. qi::_4_type _4;
  24. qi::_val_type _val;
  25. qi::tokenid_mask_type tokenid_mask;
  26. using qi::on_error;
  27. using qi::on_success;
  28. using qi::fail;
  29. using boost::phoenix::function;
  30. typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
  31. error_handler_type;
  32. typedef function<error_handler_type> error_handler_function;
  33. typedef function<client::annotation<Iterator> > annotation_function;
  34. ///////////////////////////////////////////////////////////////////////
  35. // Main expression grammar
  36. expr =
  37. unary_expr
  38. >> *(tokenid_mask(token_ids::op_binary) > unary_expr)
  39. ;
  40. unary_expr =
  41. primary_expr
  42. | (tokenid_mask(token_ids::op_unary) > unary_expr)
  43. ;
  44. primary_expr =
  45. lexer.lit_uint
  46. | function_call
  47. | identifier
  48. | lexer.true_or_false
  49. | '(' > expr > ')'
  50. ;
  51. function_call =
  52. (identifier >> '(')
  53. > argument_list
  54. > ')'
  55. ;
  56. argument_list = -(expr % ',');
  57. identifier = lexer.identifier;
  58. ///////////////////////////////////////////////////////////////////////
  59. // Debugging and error handling and reporting support.
  60. BOOST_SPIRIT_DEBUG_NODES(
  61. (expr)
  62. (unary_expr)
  63. (primary_expr)
  64. (function_call)
  65. (argument_list)
  66. (identifier)
  67. );
  68. ///////////////////////////////////////////////////////////////////////
  69. // Error handling: on error in expr, call error_handler.
  70. on_error<fail>(expr,
  71. error_handler_function(error_handler)(
  72. "Error! Expecting ", _4, _3));
  73. ///////////////////////////////////////////////////////////////////////
  74. // Annotation: on success in primary_expr, call annotation.
  75. on_success(primary_expr,
  76. annotation_function(error_handler.iters)(_val, _1));
  77. }
  78. }}