statement_def.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ;
  41. identifier =
  42. !expr.keywords
  43. >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
  44. ;
  45. variable_declaration =
  46. lexeme["var" >> !(alnum | '_')] // make sure we have whole words
  47. > &identifier // expect an identifier
  48. > assignment
  49. ;
  50. assignment =
  51. identifier
  52. > '='
  53. > expr
  54. > ';'
  55. ;
  56. if_statement =
  57. lit("if")
  58. > '('
  59. > expr
  60. > ')'
  61. > statement_
  62. >
  63. -(
  64. lexeme["else" >> !(alnum | '_')] // make sure we have whole words
  65. > statement_
  66. )
  67. ;
  68. while_statement =
  69. lit("while")
  70. > '('
  71. > expr
  72. > ')'
  73. > statement_
  74. ;
  75. compound_statement =
  76. '{' >> -statement_list >> '}'
  77. ;
  78. // Debugging and error handling and reporting support.
  79. BOOST_SPIRIT_DEBUG_NODES(
  80. (statement_list)
  81. (identifier)
  82. (variable_declaration)
  83. (assignment)
  84. );
  85. // Error handling: on error in statement_list, call error_handler.
  86. on_error<fail>(statement_list,
  87. error_handler_function(error_handler)(
  88. "Error! Expecting ", _4, _3));
  89. // Annotation: on success in assignment, call annotation.
  90. on_success(assignment,
  91. annotation_function(error_handler.iters)(_val, _1));
  92. }
  93. }}