summaryrefslogtreecommitdiff
path: root/vendor/jsoncons-0.99.2/jsoncons/parse_error_handler.hpp
blob: 9081fc95872a2f2f345ca993bb573f1f94a985ee (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/// Copyright 2013 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_PARSE_ERROR_HANDLER_HPP
#define JSONCONS_PARSE_ERROR_HANDLER_HPP

#include "jsoncons/jsoncons.hpp"
#include <system_error>

namespace jsoncons {

class parse_exception : public std::exception, public virtual json_exception
{
public:
    parse_exception(std::error_code ec,
                    size_t line,
                    size_t column)
        : error_code_(ec),
          line_number_(line),
          column_number_(column)
    {
    }
    parse_exception(const parse_exception& other)
        : error_code_(other.error_code_),
          line_number_(other.line_number_),
          column_number_(other.column_number_)
    {
    }
    const char* what() const JSONCONS_NOEXCEPT
    {
        std::ostringstream os;
        os << error_code_.message() << " at line " << line_number_ << " and column " << column_number_;
        const_cast<std::string&>(buffer_) = os.str();
        return buffer_.c_str();
    }

    const std::error_code code() const
    {
        return error_code_;
    }

    size_t line_number() const
    {
        return line_number_;
    }

    size_t column_number() const
    {
        return column_number_;
    }
private:
    std::error_code error_code_;
    std::string buffer_;
    size_t line_number_;
    size_t column_number_;
};

typedef parse_exception json_parse_exception;

template<typename CharT>
class basic_parsing_context
{
public:
    virtual ~basic_parsing_context() {}

    size_t line_number() const
    {
        return do_line_number();
    }
    size_t column_number() const 
    {
        return do_column_number();
    }
    CharT current_char() const
    {
        return do_current_char();
    }

#if !defined(JSONCONS_NO_DEPRECATED)
    CharT last_char() const
    {
        return do_current_char();
    }
#endif

private:
    virtual size_t do_line_number() const = 0;
    virtual size_t do_column_number() const = 0;
    virtual CharT do_current_char() const = 0;
};

typedef basic_parsing_context<char> parsing_context;
typedef basic_parsing_context<wchar_t> wparsing_context;

template <typename CharT>
class basic_parse_error_handler
{
public:
    virtual ~basic_parse_error_handler()
    {
    }

    void warning(std::error_code ec,
                 const basic_parsing_context<CharT>& context) throw (parse_exception) 
    {
        do_warning(ec,context);
    }

    void error(std::error_code ec,
               const basic_parsing_context<CharT>& context) throw (parse_exception) 
    {
        do_error(ec,context);
    }

    void fatal_error(std::error_code ec,
                     const basic_parsing_context<CharT>& context) throw (parse_exception) 
    {
        do_fatal_error(ec,context);
        throw parse_exception(ec,context.line_number(),context.column_number());
    }

private:
    virtual void do_warning(std::error_code,
                            const basic_parsing_context<CharT>& context) throw (parse_exception) = 0;

    virtual void do_error(std::error_code,
                          const basic_parsing_context<CharT>& context) throw (parse_exception) = 0;

    virtual void do_fatal_error(std::error_code,
                                const basic_parsing_context<CharT>& context) throw (parse_exception)
    {
        (void)context;
    }
};

template <typename CharT>
class basic_default_parse_error_handler : public basic_parse_error_handler<CharT>
{
public:
    static basic_parse_error_handler<CharT>& instance()
    {
        static basic_default_parse_error_handler<CharT> instance;
        return instance;
    }
private:
    virtual void do_warning(std::error_code,
                            const basic_parsing_context<CharT>& context) throw (parse_exception) 
    {
        (void)context;
    }

    virtual void do_error(std::error_code ec,
                          const basic_parsing_context<CharT>& context) throw (parse_exception)
    {
        throw parse_exception(ec,context.line_number(),context.column_number());
    }
};

typedef basic_parse_error_handler<char> parse_error_handler;
typedef basic_parse_error_handler<wchar_t> wparse_error_handler;

typedef basic_default_parse_error_handler<char> default_parse_error_handler;
typedef basic_default_parse_error_handler<wchar_t> wdefault_parse_error_handler;

typedef basic_parsing_context<char> parsing_context;
typedef basic_parsing_context<wchar_t> wparsing_context;

}
#endif