statement_def.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. using qi::on_error;
  24. using qi::on_success;
  25. using qi::fail;
  26. using boost::phoenix::function;
  27. typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
  28. error_handler_type;
  29. typedef function<error_handler_type> error_handler_function;
  30. typedef function<client::annotation<Iterator> > annotation_function;
  31. statement_list =
  32. +statement_
  33. ;
  34. statement_ =
  35. variable_declaration
  36. | assignment
  37. | compound_statement
  38. | if_statement
  39. | while_statement
  40. | return_statement
  41. ;
  42. variable_declaration =
  43. l("int")
  44. > expr.identifier
  45. > -('=' > expr)
  46. > ';'
  47. ;
  48. assignment =
  49. expr.identifier
  50. > '='
  51. > expr
  52. > ';'
  53. ;
  54. if_statement =
  55. l("if")
  56. > '('
  57. > expr
  58. > ')'
  59. > statement_
  60. >
  61. -(
  62. l("else")
  63. > statement_
  64. )
  65. ;
  66. while_statement =
  67. l("while")
  68. > '('
  69. > expr
  70. > ')'
  71. > statement_
  72. ;
  73. compound_statement =
  74. '{' >> -statement_list >> '}'
  75. ;
  76. return_statement =
  77. l("return")
  78. > -expr
  79. > ';'
  80. ;
  81. // Debugging and error handling and reporting support.
  82. BOOST_SPIRIT_DEBUG_NODES(
  83. (statement_list)
  84. (statement_)
  85. (variable_declaration)
  86. (assignment)
  87. (if_statement)
  88. (while_statement)
  89. (compound_statement)
  90. (return_statement)
  91. );
  92. // Error handling: on error in statement_list, call error_handler.
  93. on_error<fail>(statement_list,
  94. error_handler_function(error_handler)(
  95. "Error! Expecting ", _4, _3));
  96. // Annotation: on success in variable_declaration,
  97. // assignment and return_statement, call annotation.
  98. on_success(variable_declaration,
  99. annotation_function(error_handler.iters)(_val, _1));
  100. on_success(assignment,
  101. annotation_function(error_handler.iters)(_val, _1));
  102. on_success(return_statement,
  103. annotation_function(error_handler.iters)(_val, _1));
  104. }
  105. }}