1/*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3
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
9
10#include <map>
11#include <boost/variant/apply_visitor.hpp>
12#include <boost/type_traits/is_base_of.hpp>
13#include <boost/mpl/bool.hpp>
14#include "ast.hpp"
15
16namespace client
17{
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 template <typename Iterator>
24 struct annotation
25 {
26 template <typename, typename>
27 struct result { typedef void type; };
28
29 std::vector<Iterator>& iters;
30 annotation(std::vector<Iterator>& iters)
31 : iters(iters) {}
32
33 struct set_id
34 {
35 typedef void result_type;
36
37 int id;
38 set_id(int id) : id(id) {}
39
40 void operator()(ast::function_call& x) const
41 {
42 x.function_name.id = id;
43 }
44
45 void operator()(ast::identifier& x) const
46 {
47 x.id = id;
48 }
49
50 template <typename T>
51 void operator()(T& x) const
52 {
53 // no-op
54 }
55 };
56
57 void operator()(ast::operand& ast, Iterator pos) const
58 {
59 int id = iters.size();
60 iters.push_back(pos);
61 boost::apply_visitor(set_id(id), ast);
62 }
63
64 void operator()(ast::variable_declaration& ast, Iterator pos) const
65 {
66 int id = iters.size();
67 iters.push_back(pos);
68 ast.lhs.id = id;
69 }
70
71 void operator()(ast::assignment& ast, Iterator pos) const
72 {
73 int id = iters.size();
74 iters.push_back(pos);
75 ast.lhs.id = id;
76 }
77
78 void operator()(ast::return_statement& ast, Iterator pos) const
79 {
80 int id = iters.size();
81 iters.push_back(pos);
82 ast.id = id;
83 }
84
85 void operator()(ast::identifier& ast, Iterator pos) const
86 {
87 int id = iters.size();
88 iters.push_back(pos);
89 ast.id = id;
90 }
91 };
92}
93
94#endif
95
96

source code of boost/libs/spirit/example/qi/compiler_tutorial/conjure2/annotation.hpp