1 /*=============================================================================
2 Copyright (c) 2001-2015 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_X3_REPR_ERROR_HANDLER_HPP)
8 #define BOOST_SPIRIT_X3_REPR_ERROR_HANDLER_HPP
12 #include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
13 #include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
17 namespace rexpr { namespace parser
19 namespace x3 = boost::spirit::x3;
21 ////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////
24 // X3 Error Handler Utility
25 template <typename Iterator>
26 using error_handler = x3::error_handler<Iterator>;
28 // tag used to get our error handler from the context
29 using error_handler_tag = x3::error_handler_tag;
31 struct error_handler_base
35 template <typename Iterator, typename Exception, typename Context>
36 x3::error_handler_result on_error(
37 Iterator& first, Iterator const& last
38 , Exception const& x, Context const& context);
40 std::map<std::string, std::string> id_map;
43 ////////////////////////////////////////////////////////////////////////////
45 ////////////////////////////////////////////////////////////////////////////
47 inline error_handler_base::error_handler_base()
49 id_map["rexpr"] = "RExpression";
50 id_map["rexpr_value"] = "Value";
51 id_map["rexpr_key_value"] = "Key value pair";
54 template <typename Iterator, typename Exception, typename Context>
55 inline x3::error_handler_result
56 error_handler_base::on_error(
57 Iterator& first, Iterator const& last
58 , Exception const& x, Context const& context)
60 std::string which = x.which();
61 auto iter = id_map.find(which);
62 if (iter != id_map.end())
65 std::string message = "Error! Expecting: " + which + " here:";
66 auto& error_handler = x3::get<error_handler_tag>(context).get();
67 error_handler(x.where(), message);
68 return x3::error_handler_result::fail;