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
8#include <boost/spirit/home/x3.hpp>
9#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
10
11#include <boost/core/lightweight_test.hpp>
12#include <string>
13#include <sstream>
14
15namespace x3 = boost::spirit::x3;
16
17struct error_handler_base
18{
19 template <typename Iterator, typename Exception, typename Context>
20 x3::error_handler_result on_error(
21 Iterator&, Iterator const&
22 , Exception const& x, Context const& context) const
23 {
24 std::string message = "Error! Expecting: " + x.which() + " here:";
25 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
26 error_handler(x.where(), message);
27 return x3::error_handler_result::fail;
28 }
29};
30
31struct test_inner_rule_class;
32struct test_rule_class : x3::annotate_on_success, error_handler_base {};
33
34x3::rule<test_inner_rule_class> const test_inner_rule = "\"bar\"";
35x3::rule<test_rule_class> const test_rule;
36auto const test_inner_rule_def = x3::lit(s: "bar");
37auto const test_rule_def = x3::lit(s: "foo") > test_inner_rule > x3::lit(s: "git");
38
39BOOST_SPIRIT_DEFINE(test_inner_rule, test_rule)
40
41void test(std::string const& line_break) {
42 std::string const input("foo" + line_break + " foo" + line_break + "git");
43 auto const begin = std::begin(cont: input);
44 auto const end = std::end(cont: input);
45
46{
47 std::stringstream stream;
48 x3::error_handler<std::string::const_iterator> error_handler{begin, end, stream};
49
50 auto const parser = x3::with<x3::error_handler_tag>(val: std::ref(t&: error_handler))[test_rule];
51 x3::phrase_parse(first_: begin, last: end, p: parser, s: x3::space);
52
53 BOOST_TEST_EQ(stream.str(), "In line 2:\nError! Expecting: \"bar\" here:\n foo\n__^_\n");
54}
55
56{ // TODO: cleanup when error_handler is reenterable
57 std::stringstream stream;
58 x3::error_handler<std::string::const_iterator> error_handler{ begin, end, stream };
59
60 auto const parser = x3::with<x3::error_handler_tag>(val: std::ref(t&: error_handler))[test_rule];
61 x3::parse(first_: begin, last: end, p: parser);
62
63 BOOST_TEST_CSTR_EQ(stream.str().c_str(), "In line 1:\nError! Expecting: \"bar\" here:\nfoo\n___^_\n");
64}
65}
66
67void test_line_break_first(std::string const& line_break) {
68 std::string const input(line_break + "foo foo" + line_break + "git");
69 auto const begin = std::begin(cont: input);
70 auto const end = std::end(cont: input);
71
72 std::stringstream stream;
73 x3::error_handler<std::string::const_iterator> error_handler{begin, end, stream};
74
75 auto const parser = x3::with<x3::error_handler_tag>(val: std::ref(t&: error_handler))[test_rule];
76 x3::phrase_parse(first_: begin, last: end, p: parser, s: x3::space);
77
78 BOOST_TEST_EQ(stream.str(), "In line 2:\nError! Expecting: \"bar\" here:\nfoo foo\n_____^_\n");
79}
80
81int main() {
82 test(line_break: "\n");
83 test(line_break: "\r");
84 test(line_break: "\r\n");
85
86 test_line_break_first(line_break: "\n");
87 test_line_break_first(line_break: "\r");
88 test_line_break_first(line_break: "\r\n");
89
90
91 return boost::report_errors();
92}
93

source code of boost/libs/spirit/test/x3/error_handler.cpp