• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2015 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_X3_REPR_ERROR_HANDLER_HPP)
8 #define BOOST_SPIRIT_X3_REPR_ERROR_HANDLER_HPP
9 
10 #include "rexpr.hpp"
11 
12 #include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
13 #include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
14 
15 #include <map>
16 
17 namespace rexpr { namespace parser
18 {
19     namespace x3 = boost::spirit::x3;
20 
21     ////////////////////////////////////////////////////////////////////////////
22     //  Our error handler
23     ////////////////////////////////////////////////////////////////////////////
24     // X3 Error Handler Utility
25     template <typename Iterator>
26     using error_handler = x3::error_handler<Iterator>;
27 
28     // tag used to get our error handler from the context
29     using error_handler_tag = x3::error_handler_tag;
30 
31     struct error_handler_base
32     {
33         error_handler_base();
34 
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);
39 
40         std::map<std::string, std::string> id_map;
41     };
42 
43     ////////////////////////////////////////////////////////////////////////////
44     // Implementation
45     ////////////////////////////////////////////////////////////////////////////
46 
error_handler_base()47     inline error_handler_base::error_handler_base()
48     {
49         id_map["rexpr"] = "RExpression";
50         id_map["rexpr_value"] = "Value";
51         id_map["rexpr_key_value"] = "Key value pair";
52     }
53 
54     template <typename Iterator, typename Exception, typename Context>
55     inline x3::error_handler_result
on_error(Iterator & first,Iterator const & last,Exception const & x,Context const & context)56     error_handler_base::on_error(
57         Iterator& first, Iterator const& last
58       , Exception const& x, Context const& context)
59     {
60         std::string which = x.which();
61         auto iter = id_map.find(which);
62         if (iter != id_map.end())
63             which = iter->second;
64 
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;
69     }
70 }}
71 
72 #endif
73