1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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 #if !defined(BOOST_SPIRIT_CONJURE_ANNOTATION_HPP)
8 #define BOOST_SPIRIT_CONJURE_ANNOTATION_HPP
11 #include <boost/variant/apply_visitor.hpp>
12 #include <boost/type_traits/is_base_of.hpp>
13 #include <boost/mpl/bool.hpp>
18 ///////////////////////////////////////////////////////////////////////////////
19 // The annotation handler links the AST to a map of iterator positions
20 // for the purpose of subsequent semantic error handling when the
21 // program is being compiled.
22 ///////////////////////////////////////////////////////////////////////////////
23 struct set_annotation_id
25 typedef void result_type;
28 set_annotation_id(int id) : id(id) {}
30 void operator()(ast::function_call& x) const
32 x.function_name.id = id;
36 void dispatch(T& x, boost::mpl::true_) const
42 void dispatch(T& x, boost::mpl::false_) const
48 void operator()(T& x) const
50 typename boost::is_base_of<ast::tagged, T> is_tagged;
51 dispatch(x, is_tagged);
55 struct get_annotation_id
57 typedef int result_type;
59 int operator()(ast::function_call& x) const
61 return x.function_name.id;
65 int dispatch(T& x, boost::mpl::true_) const
71 int dispatch(T& x, boost::mpl::false_) const
77 int operator()(T& x) const
79 typename boost::is_base_of<ast::tagged, T> is_tagged;
80 return dispatch(x, is_tagged);
84 template <typename Iterator>
87 template <typename, typename>
88 struct result { typedef void type; };
90 std::vector<Iterator>& iters;
91 annotation(std::vector<Iterator>& iters)
94 void operator()(ast::operand& ast, Iterator pos) const
96 int id = iters.size();
98 boost::apply_visitor(set_annotation_id(id), ast);
102 void operator()(ast::primary_expr& ast, Iterator pos) const
104 int id = iters.size();
105 iters.push_back(pos);
106 boost::apply_visitor(set_annotation_id(id), ast);
110 void operator()(ast::variable_declaration& ast, Iterator pos) const
112 int id = iters.size();
113 iters.push_back(pos);
117 void operator()(ast::assignment& ast, Iterator pos) const
119 int id = iters.size();
120 iters.push_back(pos);
124 void operator()(ast::return_statement& ast, Iterator pos) const
126 int id = iters.size();
127 iters.push_back(pos);
131 void operator()(ast::identifier& ast, Iterator pos) const
133 int id = iters.size();
134 iters.push_back(pos);