statement_def.hpp 3.5 KB

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