-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_parser.hpp
271 lines (234 loc) · 7.64 KB
/
json_parser.hpp
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
#pragma once
#include "fly/parser/parser.hpp"
#include "fly/types/json/json.hpp"
#include "fly/types/string/string.hpp"
#include <cstdint>
#include <istream>
#include <limits>
#include <optional>
#include <string>
namespace fly::parser {
/**
* Implementation of the Parser interface for the .json format.
*
* @author Timothy Flynn (trflynn89@pm.me)
* @version May 13, 2020
*/
class JsonParser : public Parser
{
public:
/**
* Optional parsing features. May be combined with bitwise and/or operators.
*/
enum class Features : std::uint8_t
{
// Strict compliance with https://www.json.org.
Strict = 0,
// Allows single-line (//) and multi-line (/* */) comments.
AllowComments = 1 << 0,
// Allows the last value in an object/array to have one trailing comma.
AllowTrailingComma = 1 << 1,
// Allow parsing any JSON type, rather than only objects and arrays.
AllowAnyType = 1 << 2,
// Allows all of the above features.
AllFeatures = std::numeric_limits<std::underlying_type_t<Features>>::max()
};
/**
* ASCII codes for special JSON tokens.
*/
enum class Token : std::char_traits<fly::json_char_type>::int_type
{
EndOfFile = std::char_traits<fly::json_char_type>::eof(),
Tab = 0x09, // \t
NewLine = 0x0a, // \n
VerticalTab = 0x0b, // \v
CarriageReturn = 0x0d, // \r
Space = 0x20, // <space>
Quote = 0x22, // "
Asterisk = 0x2a, // *
Comma = 0x2c, // ,
Hyphen = 0x2d, // -
Solidus = 0x2f, // /
Colon = 0x3a, // :
ReverseSolidus = 0x5c, /* \ */
StartBracket = 0x5b, // [
CloseBracket = 0x5d, // ]
StartBrace = 0x7b, // {
CloseBrace = 0x7d, // }
};
/**
* Constructor. Create a parser with strict compliance.
*/
JsonParser() = default;
/**
* Constructor. Create a parser with the specified features.
*
* @param features The extra features to allow.
*/
explicit JsonParser(Features features) noexcept;
protected:
/**
* Parse a single complete JSON value from the stream.
*
* @return If successful, the parsed JSON value. Otherwise, an uninitialized value.
*/
std::optional<fly::Json> parse_internal() override;
private:
/**
* Enumeration to indicate the type of a JSON number to parse.
*/
enum class NumberType : std::uint8_t
{
Invalid,
SignedInteger,
UnsignedInteger,
FloatingPoint,
};
/**
* Enumeration to indicate the current status of parsing the JSON value.
*/
enum class ParseState : std::uint8_t
{
Invalid,
StopParsing,
KeepParsing,
};
/**
* Parse a complete JSON value from the stream. May be called recursively for nested values.
*
* @return If successful, the parsed JSON value. Otherwise, an uninitialized value.
*/
std::optional<fly::Json> parse_json();
/**
* Parse a JSON object from the stream.
*
* @return If successful, the parsed JSON object. Otherwise, an uninitialized value.
*/
std::optional<fly::Json> parse_object();
/**
* Parse a JSON array from the stream.
*
* @return If successful, the parsed JSON array. Otherwise, an uninitialized value.
*/
std::optional<fly::Json> parse_array();
/**
* Determine whether parsing a JSON object or array is complete.
*
* @param end_token Token indicating the end of the JSON object (}) or array (]).
*
* @return The new parsing state.
*/
ParseState state_for_object_or_array(Token end_token);
/**
* Parse a JSON string from the stream. Escaped symbols are preserved in the string, and the
* returned value does not contain its surrounding quotes.
*
* This returns an actual string rather than a JSON value because some callers prefer the string
* type (e.g. to pass the string as the key of a JSON object).
*
* @return If successful, the parsed JSON string. Otherwise, an uninitialized value.
*/
std::optional<fly::json_string_type> parse_quoted_string();
/**
* Parse a JSON number, boolean, or null value from the stream.
*
* @return If successful, the parsed JSON value. Otherwise, an uninitialized value.
*/
std::optional<fly::Json> parse_value();
/**
* Extract a single symbol from the stream. Ensure that symbol is equal to an expected token.
*
* @return The new parsing state.
*/
ParseState consume_token(Token token);
/**
* Extract a comma from the stream. Handles any trailing commas, allowing a single trailing
* comma if enabled in the feature set.
*
* @param end_token Token indicating the end of the JSON object (}) or array (]).
*
* @return The new parsing state.
*/
ParseState consume_comma(Token end_token);
/**
* Extract a number, boolean, or null value from the stream.
*
* @return The JSON value that was parsed as a string.
*/
fly::json_string_type consume_value();
/**
* Extract all consecutive whitespace symbols and comments (if enabled in the feature set) from
* the stream. The first non-whitespace, non-comment symbol is left on the stream.
*
* @return The new parsing state.
*/
ParseState consume_whitespace_and_comments();
/**
* Extract all consecutive whitespace symbols from the stream until a non- whitespace symbol is
* encountered. The non-whitespace symbol is left on the stream.
*/
void consume_whitespace();
/**
* Extract a single- or multi-line comment from the stream, if enabled in the feature set.
*
* @return The new parsing state.
*/
ParseState consume_comment();
/**
* Validate that a parsed number is valid and interpret its numeric JSON type.
*
* @param value String storing the parsed number to validate.
*
* @return The interpreted JSON value type.
*/
NumberType validate_number(fly::json_string_type const &value) const;
/**
* Check if a symbol is a whitespace symbol.
*
* @param token The token to inspect.
*
* @return True if the symbol is whitespace.
*/
bool is_whitespace(Token token) const;
bool const m_allow_comments {false};
bool const m_allow_trailing_comma {false};
bool const m_allow_any_type {false};
};
/**
* Combine two Features instances into a single instance via bitwise-and.
*/
JsonParser::Features operator&(JsonParser::Features a, JsonParser::Features b);
/**
* Combine two Features instances into a single instance via bitwise-or.
*/
JsonParser::Features operator|(JsonParser::Features a, JsonParser::Features b);
} // namespace fly::parser
//==================================================================================================
template <>
struct fly::string::Formatter<fly::parser::JsonParser::Token>
{
/**
* Format a JSON parser token.
*
* @tparam FormatContext The type of the formatting context.
*
* @param token The JSON parser token to format.
* @param context The context holding the formatting state.
*/
template <typename FormatContext>
void format(fly::parser::JsonParser::Token token, FormatContext &context)
{
if (token == fly::parser::JsonParser::Token::EndOfFile)
{
context.out()++ = 'E';
context.out()++ = 'O';
context.out()++ = 'F';
}
else
{
context.out()++ = '\'';
context.out()++ = static_cast<char>(token);
context.out()++ = '\'';
}
}
};