expression_def.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. postfix_expr
  42. | (tokenid_mask(token_ids::op_unary) > unary_expr)
  43. ;
  44. postfix_expr =
  45. function_call
  46. | primary_expr
  47. ;
  48. primary_expr =
  49. lexer.lit_uint
  50. | lexer.true_or_false
  51. | identifier
  52. | '(' > expr > ')'
  53. ;
  54. function_call =
  55. (identifier >> '(')
  56. > argument_list
  57. > ')'
  58. ;
  59. argument_list = -(expr % ',');
  60. identifier = lexer.identifier;
  61. ///////////////////////////////////////////////////////////////////////
  62. // Debugging and error handling and reporting support.
  63. BOOST_SPIRIT_DEBUG_NODES(
  64. (expr)
  65. (unary_expr)
  66. (postfix_expr)
  67. (primary_expr)
  68. (function_call)
  69. (argument_list)
  70. (identifier)
  71. );
  72. ///////////////////////////////////////////////////////////////////////
  73. // Error handling: on error in expr, call error_handler.
  74. on_error<fail>(expr,
  75. error_handler_function(error_handler)(
  76. "Error! Expecting ", _4, _3));
  77. ///////////////////////////////////////////////////////////////////////
  78. // Annotation: on success in unary_expr, postfix_expr,
  79. // and primary_expr call annotation.
  80. on_success(unary_expr,
  81. annotation_function(error_handler.iters)(_val, _1));
  82. on_success(postfix_expr,
  83. annotation_function(error_handler.iters)(_val, _1));
  84. on_success(primary_expr,
  85. annotation_function(error_handler.iters)(_val, _1));
  86. }
  87. }}