expression_def.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "expression.hpp"
  7. #include "error_handler.hpp"
  8. #include "annotation.hpp"
  9. #include <boost/spirit/include/phoenix_function.hpp>
  10. namespace client { namespace parser
  11. {
  12. template <typename Iterator>
  13. expression<Iterator>::expression(error_handler<Iterator>& error_handler)
  14. : expression::base_type(expr)
  15. {
  16. qi::_1_type _1;
  17. qi::_2_type _2;
  18. qi::_3_type _3;
  19. qi::_4_type _4;
  20. qi::char_type char_;
  21. qi::uint_type uint_;
  22. qi::_val_type _val;
  23. qi::raw_type raw;
  24. qi::lexeme_type lexeme;
  25. qi::alpha_type alpha;
  26. qi::alnum_type alnum;
  27. qi::bool_type bool_;
  28. using qi::on_error;
  29. using qi::on_success;
  30. using qi::fail;
  31. using boost::phoenix::function;
  32. typedef function<client::error_handler<Iterator> > error_handler_function;
  33. typedef function<client::annotation<Iterator> > annotation_function;
  34. ///////////////////////////////////////////////////////////////////////
  35. // Tokens
  36. logical_or_op.add
  37. ("||", ast::op_or)
  38. ;
  39. logical_and_op.add
  40. ("&&", ast::op_and)
  41. ;
  42. equality_op.add
  43. ("==", ast::op_equal)
  44. ("!=", ast::op_not_equal)
  45. ;
  46. relational_op.add
  47. ("<", ast::op_less)
  48. ("<=", ast::op_less_equal)
  49. (">", ast::op_greater)
  50. (">=", ast::op_greater_equal)
  51. ;
  52. additive_op.add
  53. ("+", ast::op_plus)
  54. ("-", ast::op_minus)
  55. ;
  56. multiplicative_op.add
  57. ("*", ast::op_times)
  58. ("/", ast::op_divide)
  59. ;
  60. unary_op.add
  61. ("+", ast::op_positive)
  62. ("-", ast::op_negative)
  63. ("!", ast::op_not)
  64. ;
  65. keywords.add
  66. ("true")
  67. ("false")
  68. ("if")
  69. ("else")
  70. ("while")
  71. ("int")
  72. ("void")
  73. ("return")
  74. ;
  75. ///////////////////////////////////////////////////////////////////////
  76. // Main expression grammar
  77. expr =
  78. logical_or_expr.alias()
  79. ;
  80. logical_or_expr =
  81. logical_and_expr
  82. >> *(logical_or_op > logical_and_expr)
  83. ;
  84. logical_and_expr =
  85. equality_expr
  86. >> *(logical_and_op > equality_expr)
  87. ;
  88. equality_expr =
  89. relational_expr
  90. >> *(equality_op > relational_expr)
  91. ;
  92. relational_expr =
  93. additive_expr
  94. >> *(relational_op > additive_expr)
  95. ;
  96. additive_expr =
  97. multiplicative_expr
  98. >> *(additive_op > multiplicative_expr)
  99. ;
  100. multiplicative_expr =
  101. unary_expr
  102. >> *(multiplicative_op > unary_expr)
  103. ;
  104. unary_expr =
  105. primary_expr
  106. | (unary_op > unary_expr)
  107. ;
  108. primary_expr =
  109. uint_
  110. | function_call
  111. | identifier
  112. | bool_
  113. | '(' > expr > ')'
  114. ;
  115. function_call =
  116. (identifier >> '(')
  117. > argument_list
  118. > ')'
  119. ;
  120. argument_list = -(expr % ',');
  121. identifier =
  122. !lexeme[keywords >> !(alnum | '_')]
  123. >> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
  124. ;
  125. ///////////////////////////////////////////////////////////////////////
  126. // Debugging and error handling and reporting support.
  127. BOOST_SPIRIT_DEBUG_NODES(
  128. (expr)
  129. (logical_or_expr)
  130. (logical_and_expr)
  131. (equality_expr)
  132. (relational_expr)
  133. (additive_expr)
  134. (multiplicative_expr)
  135. (unary_expr)
  136. (primary_expr)
  137. (function_call)
  138. (argument_list)
  139. (identifier)
  140. );
  141. ///////////////////////////////////////////////////////////////////////
  142. // Error handling: on error in expr, call error_handler.
  143. on_error<fail>(expr,
  144. error_handler_function(error_handler)(
  145. "Error! Expecting ", _4, _3));
  146. ///////////////////////////////////////////////////////////////////////
  147. // Annotation: on success in primary_expr, call annotation.
  148. on_success(primary_expr,
  149. annotation_function(error_handler.iters)(_val, _1));
  150. }
  151. }}