annotation.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #if !defined(BOOST_SPIRIT_CONJURE_ANNOTATION_HPP)
  7. #define BOOST_SPIRIT_CONJURE_ANNOTATION_HPP
  8. #include <map>
  9. #include <boost/variant/apply_visitor.hpp>
  10. #include <boost/type_traits/is_base_of.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include "ast.hpp"
  13. namespace client
  14. {
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // The annotation handler links the AST to a map of iterator positions
  17. // for the purpose of subsequent semantic error handling when the
  18. // program is being compiled.
  19. ///////////////////////////////////////////////////////////////////////////////
  20. struct set_annotation_id
  21. {
  22. typedef void result_type;
  23. int id;
  24. set_annotation_id(int id) : id(id) {}
  25. void operator()(ast::function_call& x) const
  26. {
  27. x.function_name.id = id;
  28. }
  29. template <typename T>
  30. void dispatch(T& x, boost::mpl::true_) const
  31. {
  32. x.id = id;
  33. }
  34. template <typename T>
  35. void dispatch(T& x, boost::mpl::false_) const
  36. {
  37. // no-op
  38. }
  39. template <typename T>
  40. void operator()(T& x) const
  41. {
  42. typename boost::is_base_of<ast::tagged, T> is_tagged;
  43. dispatch(x, is_tagged);
  44. }
  45. };
  46. struct get_annotation_id
  47. {
  48. typedef int result_type;
  49. int operator()(ast::function_call& x) const
  50. {
  51. return x.function_name.id;
  52. }
  53. template <typename T>
  54. int dispatch(T& x, boost::mpl::true_) const
  55. {
  56. return x.id;
  57. }
  58. template <typename T>
  59. int dispatch(T& x, boost::mpl::false_) const
  60. {
  61. return -1;
  62. }
  63. template <typename T>
  64. int operator()(T& x) const
  65. {
  66. typename boost::is_base_of<ast::tagged, T> is_tagged;
  67. return dispatch(x, is_tagged);
  68. }
  69. };
  70. template <typename Iterator>
  71. struct annotation
  72. {
  73. template <typename, typename>
  74. struct result { typedef void type; };
  75. std::vector<Iterator>& iters;
  76. annotation(std::vector<Iterator>& iters)
  77. : iters(iters) {}
  78. void operator()(ast::operand& ast, Iterator pos) const
  79. {
  80. int id = iters.size();
  81. iters.push_back(pos);
  82. boost::apply_visitor(set_annotation_id(id), ast);
  83. ast.id = id;
  84. }
  85. void operator()(ast::primary_expr& ast, Iterator pos) const
  86. {
  87. int id = iters.size();
  88. iters.push_back(pos);
  89. boost::apply_visitor(set_annotation_id(id), ast);
  90. ast.id = id;
  91. }
  92. void operator()(ast::variable_declaration& ast, Iterator pos) const
  93. {
  94. int id = iters.size();
  95. iters.push_back(pos);
  96. ast.lhs.id = id;
  97. }
  98. void operator()(ast::assignment& ast, Iterator pos) const
  99. {
  100. int id = iters.size();
  101. iters.push_back(pos);
  102. ast.lhs.id = id;
  103. }
  104. void operator()(ast::return_statement& ast, Iterator pos) const
  105. {
  106. int id = iters.size();
  107. iters.push_back(pos);
  108. ast.id = id;
  109. }
  110. void operator()(ast::identifier& ast, Iterator pos) const
  111. {
  112. int id = iters.size();
  113. iters.push_back(pos);
  114. ast.id = id;
  115. }
  116. };
  117. }
  118. #endif