File: error_handler.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (92 lines) | stat: -rw-r--r-- 3,234 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*=============================================================================
    Copyright (c) 2001-2015 Joel de Guzman

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>

#include <boost/core/lightweight_test.hpp>
#include <string>
#include <sstream>

namespace x3 = boost::spirit::x3;

struct error_handler_base
{
    template <typename Iterator, typename Exception, typename Context>
    x3::error_handler_result on_error(
        Iterator&, Iterator const&
      , Exception const& x, Context const& context) const
    {
        std::string message = "Error! Expecting: " + x.which() + " here:";
        auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
        error_handler(x.where(), message);
        return x3::error_handler_result::fail;
    }
};

struct test_inner_rule_class;
struct test_rule_class : x3::annotate_on_success, error_handler_base {};

x3::rule<test_inner_rule_class> const test_inner_rule = "\"bar\"";
x3::rule<test_rule_class> const test_rule;
auto const test_inner_rule_def = x3::lit("bar");
auto const test_rule_def = x3::lit("foo") > test_inner_rule > x3::lit("git");

BOOST_SPIRIT_DEFINE(test_inner_rule, test_rule)

void test(std::string const& line_break) {
    std::string const input("foo" + line_break + "  foo" + line_break + "git");
    auto const begin = std::begin(input);
    auto const end = std::end(input);

{
    std::stringstream stream;
    x3::error_handler<std::string::const_iterator> error_handler{begin, end, stream};

    auto const parser = x3::with<x3::error_handler_tag>(std::ref(error_handler))[test_rule];
    x3::phrase_parse(begin, end, parser, x3::space);

    BOOST_TEST_EQ(stream.str(), "In line 2:\nError! Expecting: \"bar\" here:\n  foo\n__^_\n");
}

{ // TODO: cleanup when error_handler is reenterable
    std::stringstream stream;
    x3::error_handler<std::string::const_iterator> error_handler{ begin, end, stream };

    auto const parser = x3::with<x3::error_handler_tag>(std::ref(error_handler))[test_rule];
    x3::parse(begin, end, parser);

    BOOST_TEST_CSTR_EQ(stream.str().c_str(), "In line 1:\nError! Expecting: \"bar\" here:\nfoo\n___^_\n");
}
}

void test_line_break_first(std::string const& line_break) {
    std::string const input(line_break + "foo  foo" + line_break + "git");
    auto const begin = std::begin(input);
    auto const end = std::end(input);

    std::stringstream stream;
    x3::error_handler<std::string::const_iterator> error_handler{begin, end, stream};

    auto const parser = x3::with<x3::error_handler_tag>(std::ref(error_handler))[test_rule];
    x3::phrase_parse(begin, end, parser, x3::space);

    BOOST_TEST_EQ(stream.str(), "In line 2:\nError! Expecting: \"bar\" here:\nfoo  foo\n_____^_\n");
}

int main() {
    test("\n");
    test("\r");
    test("\r\n");

    test_line_break_first("\n");
    test_line_break_first("\r");
    test_line_break_first("\r\n");


    return boost::report_errors();
}