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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <orcus/json_parser_thread.hpp>
#include <orcus/json_parser.hpp>
#include <orcus/string_pool.hpp>
#include <orcus/detail/parser_token_buffer.hpp>
#include <sstream>
#include <string_view>
#include <algorithm>
#include <limits>
namespace orcus { namespace json {
parse_token::parse_token() : type(parse_token_t::unknown), value(0.0) {}
parse_token::parse_token(parse_token_t _type) : type(_type), value(0.0) {}
parse_token::parse_token(parse_token_t _type, std::string_view s) :
type(_type), value(s)
{
}
parse_token::parse_token(std::string_view s, std::ptrdiff_t offset) :
type(parse_token_t::parse_error), value(parse_error_value_t{s, offset})
{
assert(type == parse_token_t::parse_error);
}
parse_token::parse_token(double v) :
type(parse_token_t::number), value(v)
{
}
parse_token::parse_token(const parse_token& other) :
type(other.type), value(other.value)
{
}
bool parse_token::operator== (const parse_token& other) const
{
return type == other.type && value == other.value;
}
bool parse_token::operator!= (const parse_token& other) const
{
return !operator== (other);
}
/**
* This impl class also acts as a handler for the parser.
*
*/
struct parser_thread::impl
{
detail::thread::parser_token_buffer<parse_tokens_t> m_token_buffer;
string_pool m_pool;
parse_tokens_t m_parser_tokens; // token buffer for the parser thread.
const char* mp_char;
size_t m_size;
impl(const char* p, size_t n, size_t min_token_size, size_t max_token_size) :
m_token_buffer(min_token_size, max_token_size),
mp_char(p), m_size(n)
{
m_parser_tokens.reserve(min_token_size);
}
void start()
{
try
{
json_parser<parser_thread::impl> parser({mp_char, m_size}, *this);
parser.parse();
}
catch (const parse_error& e)
{
std::string_view s = m_pool.intern(e.what()).first;
m_parser_tokens.emplace_back(s, e.offset());
}
notify_and_finish();
}
void begin_parse()
{
m_parser_tokens.emplace_back(parse_token_t::begin_parse);
check_and_notify();
}
void end_parse()
{
m_parser_tokens.emplace_back(parse_token_t::end_parse);
check_and_notify();
}
void begin_array()
{
m_parser_tokens.emplace_back(parse_token_t::begin_array);
check_and_notify();
}
void end_array()
{
m_parser_tokens.emplace_back(parse_token_t::end_array);
check_and_notify();
}
void begin_object()
{
m_parser_tokens.emplace_back(parse_token_t::begin_object);
check_and_notify();
}
void object_key(std::string_view s, bool transient)
{
if (transient)
s = m_pool.intern(s).first;
m_parser_tokens.emplace_back(parse_token_t::object_key, s);
check_and_notify();
}
void end_object()
{
m_parser_tokens.emplace_back(parse_token_t::end_object);
check_and_notify();
}
void boolean_true()
{
m_parser_tokens.emplace_back(parse_token_t::boolean_true);
check_and_notify();
}
void boolean_false()
{
m_parser_tokens.emplace_back(parse_token_t::boolean_false);
check_and_notify();
}
void null()
{
m_parser_tokens.emplace_back(parse_token_t::null);
check_and_notify();
}
void string(std::string_view s, bool transient)
{
if (transient)
s = m_pool.intern(s).first;
m_parser_tokens.emplace_back(parse_token_t::string, s);
check_and_notify();
}
void number(double val)
{
m_parser_tokens.emplace_back(val);
check_and_notify();
}
void check_and_notify()
{
m_token_buffer.check_and_notify(m_parser_tokens);
}
void notify_and_finish()
{
m_token_buffer.notify_and_finish(m_parser_tokens);
}
bool next_tokens(parse_tokens_t& tokens)
{
return m_token_buffer.next_tokens(tokens);
}
parser_stats get_stats() const
{
parser_stats stats;
stats.token_buffer_size_threshold = m_token_buffer.token_size_threshold();
return stats;
}
void swap_string_pool(string_pool& pool)
{
m_pool.swap(pool);
}
};
std::ostream& operator<< (std::ostream& os, const parse_tokens_t& tokens)
{
using std::endl;
os << "token size: " << tokens.size() << endl;
std::for_each(tokens.begin(), tokens.end(),
[&](const parse_token& t)
{
switch (t.type)
{
case parse_token_t::begin_array:
os << "- begin_array" << endl;
break;
case parse_token_t::begin_object:
os << "- begin_object" << endl;
break;
case parse_token_t::begin_parse:
os << "- begin_parse" << endl;
break;
case parse_token_t::boolean_false:
os << "- boolean_false" << endl;
break;
case parse_token_t::boolean_true:
os << "- boolean_true" << endl;
break;
case parse_token_t::end_array:
os << "- end_array" << endl;
break;
case parse_token_t::end_object:
os << "- end_object" << endl;
break;
case parse_token_t::end_parse:
os << "- end_parse" << endl;
break;
case parse_token_t::null:
os << "- null" << endl;
break;
case parse_token_t::number:
os << "- number (v=" << std::get<double>(t.value) << ")" << endl;
break;
case parse_token_t::object_key:
os << "- object_key (v=" << std::get<std::string_view>(t.value) << ")" << endl;
break;
case parse_token_t::parse_error:
{
auto v = std::get<parse_error_value_t>(t.value);
os << "- parse_error (v=" << v.str << ", offset=" << v.offset << ")" << endl;
break;
}
case parse_token_t::string:
os << "- string (" << std::get<std::string_view>(t.value) << ")" << endl;
break;
case parse_token_t::unknown:
os << "- unknown" << endl;
break;
default:
;
}
}
);
return os;
}
parser_thread::parser_thread(const char* p, size_t n, size_t min_token_size) :
mp_impl(std::make_unique<parser_thread::impl>(
p, n, min_token_size, std::numeric_limits<size_t>::max()/2)) {}
parser_thread::parser_thread(const char* p, size_t n, size_t min_token_size, size_t max_token_size) :
mp_impl(std::make_unique<parser_thread::impl>(
p, n, min_token_size, max_token_size)) {}
parser_thread::~parser_thread() {}
void parser_thread::start()
{
mp_impl->start();
}
bool parser_thread::next_tokens(parse_tokens_t& tokens)
{
return mp_impl->next_tokens(tokens);
}
parser_stats parser_thread::get_stats() const
{
return mp_impl->get_stats();
}
void parser_thread::swap_string_pool(string_pool& pool)
{
mp_impl->swap_string_pool(pool);
}
}}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|