/*============================================================================= Copyright (c) 2001-2011 Joel de Guzman Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #if !defined(BOOST_SPIRIT_CONJURE_ANNOTATION_HPP) #define BOOST_SPIRIT_CONJURE_ANNOTATION_HPP #include #include #include #include #include "ast.hpp" namespace client { /////////////////////////////////////////////////////////////////////////////// // The annotation handler links the AST to a map of iterator positions // for the purpose of subsequent semantic error handling when the // program is being compiled. /////////////////////////////////////////////////////////////////////////////// template struct annotation { template struct result { typedef void type; }; std::vector& iters; annotation(std::vector& iters) : iters(iters) {} struct set_id { typedef void result_type; int id; set_id(int id) : id(id) {} template void operator()(T& x) const { this->dispatch(x, boost::is_base_of()); } // This will catch all nodes except those inheriting from ast::tagged template void dispatch(T& x, boost::mpl::false_) const { // (no-op) no need for tags } // This will catch all nodes inheriting from ast::tagged template void dispatch(T& x, boost::mpl::true_) const { x.id = id; } }; void operator()(ast::operand& ast, Iterator pos) const { int id = iters.size(); iters.push_back(pos); boost::apply_visitor(set_id(id), ast); } void operator()(ast::assignment& ast, Iterator pos) const { int id = iters.size(); iters.push_back(pos); ast.lhs.id = id; } void operator()(ast::return_statement& ast, Iterator pos) const { int id = iters.size(); iters.push_back(pos); ast.id = id; } void operator()(ast::identifier& ast, Iterator pos) const { int id = iters.size(); iters.push_back(pos); ast.id = id; } }; } #endif