statement_def.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "statement.hpp"
  7. #include "error_handler.hpp"
  8. #include "annotation.hpp"
  9. namespace client { namespace parser
  10. {
  11. template <typename Iterator>
  12. statement<Iterator>::statement(error_handler<Iterator>& error_handler)
  13. : statement::base_type(statement_list), expr(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::lit_type lit;
  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. 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. identifier =
  43. !expr.keywords
  44. >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
  45. ;
  46. variable_declaration =
  47. lexeme["int" >> !(alnum | '_')] // make sure we have whole words
  48. > identifier
  49. > -('=' > expr)
  50. > ';'
  51. ;
  52. assignment =
  53. identifier
  54. > '='
  55. > expr
  56. > ';'
  57. ;
  58. if_statement =
  59. lit("if")
  60. > '('
  61. > expr
  62. > ')'
  63. > statement_
  64. >
  65. -(
  66. lexeme["else" >> !(alnum | '_')] // make sure we have whole words
  67. > statement_
  68. )
  69. ;
  70. while_statement =
  71. lit("while")
  72. > '('
  73. > expr
  74. > ')'
  75. > statement_
  76. ;
  77. compound_statement =
  78. '{' >> -statement_list >> '}'
  79. ;
  80. return_statement =
  81. lexeme["return" >> !(alnum | '_')] // make sure we have whole words
  82. > -expr
  83. > ';'
  84. ;
  85. // Debugging and error handling and reporting support.
  86. BOOST_SPIRIT_DEBUG_NODES(
  87. (statement_list)
  88. (identifier)
  89. (variable_declaration)
  90. (assignment)
  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. }}