annotate_on_success.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 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_X3__ANNOTATE_ON_SUCCESS_HPP)
  7. #define BOOST_SPIRIT_X3__ANNOTATE_ON_SUCCESS_HPP
  8. #include <boost/spirit/home/x3/support/ast/variant.hpp>
  9. #include <boost/spirit/home/x3/support/context.hpp>
  10. #include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
  11. #include <boost/spirit/home/x3/support/utility/lambda_visitor.hpp>
  12. namespace boost { namespace spirit { namespace x3
  13. {
  14. ///////////////////////////////////////////////////////////////////////////
  15. // The on_success handler tags the AST with the iterator position
  16. // for error handling.
  17. //
  18. // The on_success handler also ties the AST to a vector of iterator
  19. // positions for the purpose of subsequent semantic error handling
  20. // when the program is being compiled. See x3::position_cache in
  21. // x3/support/ast.
  22. //
  23. // We'll ask the X3's error_handler utility to do these.
  24. ///////////////////////////////////////////////////////////////////////////
  25. struct annotate_on_success
  26. {
  27. template <typename Iterator, typename Context, typename... Types>
  28. inline void on_success(Iterator const& first, Iterator const& last
  29. , variant<Types...>& ast, Context const& context)
  30. {
  31. ast.apply_visitor(x3::make_lambda_visitor<void>([&](auto& node)
  32. {
  33. this->on_success(first, last, node, context);
  34. }));
  35. }
  36. template <typename T, typename Iterator, typename Context>
  37. inline void on_success(Iterator const& first, Iterator const& last
  38. , T& ast, Context const& context)
  39. {
  40. auto& error_handler = get<error_handler_tag>(context).get();
  41. error_handler.tag(ast, first, last);
  42. }
  43. };
  44. }}}
  45. #endif