2 * UCW JSON Library -- Parser
4 * (c) 2015 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/fastbuf.h>
12 #include <ucw/ff-unicode.h>
13 #include <ucw/trans.h>
14 #include <ucw/unicode.h>
15 #include <ucw-json/json.h>
20 void json_set_input(struct json_context *js, struct fastbuf *in)
26 js->next_token = NULL;
28 if (!js->trivial_token)
29 js->trivial_token = json_new_node(js, JSON_INVALID);
32 static void NONRET json_parse_error(struct json_context *js, const char *msg)
34 trans_throw("ucw.js.parse", js, "%s at line %u:%u", msg, js->in_line, js->in_column);
37 static int json_get_char(struct json_context *js)
39 int c = bget_utf8_32_repl(js->in_fb, -2);
43 json_parse_error(js, "Malformed UTF-8 character");
45 // FIXME: Reject alternative sequences
52 static void json_unget_char(struct json_context *js, int c)
57 static struct json_node *json_triv_token(struct json_context *js, enum json_node_type type)
59 js->trivial_token->type = type;
60 return js->trivial_token;
63 static struct json_node *json_parse_number(struct json_context *js, int c)
66 char *p = mp_start_noalign(js->pool, 0);
71 p = mp_append_char(js->pool, p, c);
72 c = json_get_char(js);
73 if (!(c >= '0' && c <= '9'))
74 json_parse_error(js, "Malformed number: just minus");
80 // Leading zeroes are forbidden by RFC 7159
81 p = mp_append_char(js->pool, p, c);
82 c = json_get_char(js);
83 if (c >= '0' && c <= '9')
84 json_parse_error(js, "Malformed number: leading zero");
88 while (c >= '0' && c <= '9')
90 p = mp_append_char(js->pool, p, c);
91 c = json_get_char(js);
98 p = mp_append_char(js->pool, p, c);
99 c = json_get_char(js);
100 if (!(c >= '0' && c <= '9'))
101 json_parse_error(js, "Malformed number: no digits after decimal point");
102 while (c >= '0' && c <= '9')
104 p = mp_append_char(js->pool, p, c);
105 c = json_get_char(js);
110 if (c == 'e' || c == 'E')
112 p = mp_append_char(js->pool, p, c);
113 c = json_get_char(js);
114 if (c == '+' || c == '-')
116 p = mp_append_char(js->pool, p, c);
117 c = json_get_char(js);
119 if (!(c >= '0' && c <= '9'))
120 json_parse_error(js, "Malformed number: empty exponent");
121 while (c >= '0' && c <= '9')
123 p = mp_append_char(js->pool, p, c);
124 c = json_get_char(js);
128 json_unget_char(js, c);
130 p = mp_end_string(js->pool, p);
132 double val = strtod(p, NULL);
134 json_parse_error(js, "Number out of range");
137 return json_new_number(js, val);
140 static struct json_node *json_parse_name(struct json_context *js, int c)
143 char *p = mp_start_noalign(js->pool, 0);
145 while (c >= 'a' && c <= 'z')
147 p = mp_append_char(js->pool, p, c);
148 c = json_get_char(js);
150 json_unget_char(js, c);
152 p = mp_end_string(js->pool, p);
154 if (!strcmp(p, "null"))
155 n = json_new_null(js);
156 else if (!strcmp(p, "false"))
157 n = json_new_bool(js, 0);
158 else if (!strcmp(p, "true"))
159 n = json_new_bool(js, 1);
161 json_parse_error(js, "Invalid literal name");
167 static uint json_parse_hex4(struct json_context *js)
170 for (int i=0; i<4; i++)
173 int c = json_get_char(js);
174 if (c >= '0' && c <= '9')
176 else if (c >= 'a' && c <= 'f')
178 else if (c >= 'A' && c <= 'F')
181 json_parse_error(js, "Invalid Unicode escape sequence");
186 static struct json_node *json_parse_string(struct json_context *js, int c)
188 char *p = mp_start_noalign(js->pool, 0);
190 c = json_get_char(js);
193 if (unlikely(c < 0x20))
195 if (c < 0 || c == 0x0d || c == 0x0a)
196 json_parse_error(js, "Unterminated string");
198 json_parse_error(js, "Invalid control character in string");
200 if (unlikely(c >= 0xd800 && c < 0xf900))
203 json_parse_error(js, "Invalid surrogate character in string");
205 json_parse_error(js, "Invalid private-use character in string");
207 if (unlikely(c > 0xf0000))
210 json_parse_error(js, "Invalid non-Unicode character in string");
212 json_parse_error(js, "Invalid private-use character in string");
216 c = json_get_char(js);
240 uint x = json_parse_hex4(js);
242 json_parse_error(js, "Zero bytes in strings are not supported");
243 if (x >= 0xd800 && x < 0xf900)
247 // High surrogate: low surrogate must follow
249 if (json_get_char(js) == '\\' && json_get_char(js) == 'u')
250 y = json_parse_hex4(js);
251 if (!(y >= 0xdc00 && y < 0xe000))
252 json_parse_error(js, "Escaped high surrogate codepoint must be followed by a low surrogate codepoint");
253 c = 0x10000 + ((x & 0x03ff) << 10) | (y & 0x03ff);
255 json_parse_error(js, "Invalid escaped private-use character");
260 json_parse_error(js, "Invalid escaped surrogate codepoint");
263 json_parse_error(js, "Invalid escaped private-use character");
270 json_parse_error(js, "Invalid backslash sequence in string");
273 p = mp_append_utf8_32(js->pool, p, c);
274 c = json_get_char(js);
277 p = mp_end_string(js->pool, p);
278 return json_new_string_ref(js, p);
281 static struct json_node *json_read_token(struct json_context *js)
283 if (unlikely(js->in_eof))
284 return json_triv_token(js, JSON_EOF);
286 int c = js->next_char;
290 c = json_get_char(js);
292 while (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d)
299 c = json_get_char(js);
302 return json_triv_token(js, JSON_EOF);
304 if (c >= '0' && c <= '9' || c == '-')
305 return json_parse_number(js, c);
307 if (c >= 'a' && c <= 'z')
308 return json_parse_name(js, c);
311 return json_parse_string(js, c);
316 return json_triv_token(js, JSON_BEGIN_ARRAY);
318 return json_triv_token(js, JSON_END_ARRAY);
320 return json_triv_token(js, JSON_BEGIN_OBJECT);
322 return json_triv_token(js, JSON_END_OBJECT);
324 return json_triv_token(js, JSON_NAME_SEP);
326 return json_triv_token(js, JSON_VALUE_SEP);
328 json_parse_error(js, "Invalid character");
332 struct json_node *json_peek_token(struct json_context *js)
335 js->next_token = json_read_token(js);
336 return js->next_token;
339 struct json_node *json_next_token(struct json_context *js)
341 struct json_node *t = js->next_token;
344 js->next_token = NULL;
347 return json_read_token(js);
350 struct json_node *json_next_value(struct json_context *js)
352 struct json_node *t = json_next_token(js);
367 case JSON_BEGIN_ARRAY:
369 struct json_node *a = json_new_array(js);
370 if (json_peek_token(js)->type == JSON_END_ARRAY)
374 struct json_node *v = json_next_value(js);
376 json_parse_error(js, "Unterminated array");
377 json_array_append(a, v);
379 t = json_next_token(js);
380 if (t->type == JSON_END_ARRAY)
382 if (t->type != JSON_VALUE_SEP)
383 json_parse_error(js, "Comma expected");
389 case JSON_BEGIN_OBJECT:
391 struct json_node *o = json_new_object(js);
392 if (json_peek_token(js)->type == JSON_END_OBJECT)
396 struct json_node *k = json_next_value(js);
398 json_parse_error(js, "Unterminated object");
399 if (k->type != JSON_STRING)
400 json_parse_error(js, "Object key must be a string");
402 t = json_next_token(js);
403 if (t->type != JSON_NAME_SEP)
404 json_parse_error(js, "Colon expected");
406 struct json_node *v = json_next_value(js);
408 json_parse_error(js, "Unterminated object");
409 if (json_object_get(o, k->string)) // FIXME: Optimize
410 json_parse_error(js, "Key already set");
411 json_object_set(o, k->string, v);
413 t = json_next_token(js);
414 if (t->type == JSON_END_OBJECT)
416 if (t->type != JSON_VALUE_SEP)
417 json_parse_error(js, "Comma expected");
422 // Misplaced characters
424 json_parse_error(js, "Misplaced end of array");
425 case JSON_END_OBJECT:
426 json_parse_error(js, "Misplaced end of object");
428 json_parse_error(js, "Misplaced colon");
430 json_parse_error(js, "Misplaced comma");
436 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb)
438 json_set_input(js, fb);
440 struct json_node *n = json_next_value(js);
442 json_parse_error(js, "Empty input");
444 struct json_node *t = json_next_token(js);
445 if (t->type != JSON_EOF)
446 json_parse_error(js, "Only one top-level value allowed");