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/detail/lightweight_test.hpp>
9 #include <boost/spirit/home/x3.hpp>
10 #include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
11 #include <string>
12 #include <sstream>
13
14 namespace x3 = boost::spirit::x3;
15
16 struct error_handler_base
17 {
18 template <typename Iterator, typename Exception, typename Context>
on_errorerror_handler_base19 x3::error_handler_result on_error(
20 Iterator&, Iterator const&
21 , Exception const& x, Context const& context) const
22 {
23 std::string message = "Error! Expecting: " + x.which() + " here:";
24 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
25 error_handler(x.where(), message);
26 return x3::error_handler_result::fail;
27 }
28 };
29
30 struct test_rule_class : x3::annotate_on_success, error_handler_base {};
31
32 x3::rule<test_rule_class> const test_rule;
33 auto const test_rule_def = x3::lit("foo") > x3::lit("bar") > x3::lit("git");
34
BOOST_SPIRIT_DEFINE(test_rule)35 BOOST_SPIRIT_DEFINE(test_rule)
36
37 void test(std::string const& line_break) {
38 std::string const input("foo" + line_break + " foo" + line_break + "git");
39 auto const begin = std::begin(input);
40 auto const end = std::end(input);
41
42 std::stringstream stream;
43 x3::error_handler<std::string::const_iterator> error_handler{begin, end, stream};
44
45 auto const parser = x3::with<x3::error_handler_tag>(std::ref(error_handler))[test_rule];
46 x3::phrase_parse(begin, end, parser, x3::space);
47
48 BOOST_TEST_EQ(stream.str(), "In line 2:\nError! Expecting: \"bar\" here:\n foo\n__^_\n");
49 }
50
main()51 int main() {
52 test("\n");
53 test("\r");
54 test("\r\n");
55
56 return boost::report_errors();
57 }
58