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;
30 static void NONRET json_parse_error(struct json_context *js, const char *msg)
32 trans_throw("ucw.js.parse", js, "%s at line %u:%u", msg, js->in_line, js->in_column);
35 static int json_get_char(struct json_context *js)
37 int c = bget_utf8_32_repl(js->in_fb, -2);
41 json_parse_error(js, "Malformed UTF-8 character");
43 // FIXME: Reject alternative sequences
50 static void json_unget_char(struct json_context *js, int c)
55 static struct json_node *json_triv_token(struct json_context *js, enum json_node_type type)
57 js->trivial_token->type = type;
58 return js->trivial_token;
61 static struct json_node *json_parse_number(struct json_context *js, int c)
64 char *p = mp_start_noalign(js->pool, 0);
69 p = mp_append_char(js->pool, p, c);
70 c = json_get_char(js);
71 if (!(c >= '0' && c <= '9'))
72 json_parse_error(js, "Malformed number: just minus");
78 // Leading zeroes are forbidden by RFC 7159
79 p = mp_append_char(js->pool, p, c);
80 c = json_get_char(js);
81 if (c >= '0' && c <= '9')
82 json_parse_error(js, "Malformed number: leading zero");
86 while (c >= '0' && c <= '9')
88 p = mp_append_char(js->pool, p, c);
89 c = json_get_char(js);
96 p = mp_append_char(js->pool, p, c);
97 c = json_get_char(js);
98 if (!(c >= '0' && c <= '9'))
99 json_parse_error(js, "Malformed number: no digits after decimal point");
100 while (c >= '0' && c <= '9')
102 p = mp_append_char(js->pool, p, c);
103 c = json_get_char(js);
108 if (c == 'e' || c == 'E')
110 p = mp_append_char(js->pool, p, c);
111 c = json_get_char(js);
112 if (c == '+' || c == '-')
114 p = mp_append_char(js->pool, p, c);
115 c = json_get_char(js);
117 if (!(c >= '0' && c <= '9'))
118 json_parse_error(js, "Malformed number: empty exponent");
119 while (c >= '0' && c <= '9')
121 p = mp_append_char(js->pool, p, c);
122 c = json_get_char(js);
126 json_unget_char(js, c);
128 p = mp_end_string(js->pool, p);
130 double val = strtod(p, NULL);
132 json_parse_error(js, "Number out of range");
135 return json_new_number(js, val);
138 static struct json_node *json_parse_name(struct json_context *js, int c)
143 while (c >= 'a' && c <= 'z')
145 if (i < sizeof(name) - 1)
147 c = json_get_char(js);
149 if (i >= sizeof(name) - 1)
150 json_parse_error(js, "Invalid literal name");
152 json_unget_char(js, c);
155 if (!strcmp(name, "null"))
156 n = json_new_null(js);
157 else if (!strcmp(name, "false"))
158 n = json_new_bool(js, 0);
159 else if (!strcmp(name, "true"))
160 n = json_new_bool(js, 1);
162 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, "Numbers must start with a digit");
330 json_parse_error(js, "Misplaced byte-order mark, complain in Redmond");
332 json_parse_error(js, "Invalid character");
336 struct json_node *json_peek_token(struct json_context *js)
339 js->next_token = json_read_token(js);
340 return js->next_token;
343 struct json_node *json_next_token(struct json_context *js)
345 struct json_node *t = js->next_token;
348 js->next_token = NULL;
351 return json_read_token(js);
354 struct json_node *json_next_value(struct json_context *js)
356 struct json_node *t = json_next_token(js);
371 case JSON_BEGIN_ARRAY:
373 struct json_node *a = json_new_array(js);
374 if (json_peek_token(js)->type == JSON_END_ARRAY)
378 struct json_node *v = json_next_value(js);
380 json_parse_error(js, "Unterminated array");
381 json_array_append(a, v);
383 t = json_next_token(js);
384 if (t->type == JSON_END_ARRAY)
386 if (t->type != JSON_VALUE_SEP)
387 json_parse_error(js, "Comma or right bracket expected");
393 case JSON_BEGIN_OBJECT:
395 struct json_node *o = json_new_object(js);
396 if (json_peek_token(js)->type == JSON_END_OBJECT)
400 struct json_node *k = json_next_value(js);
402 json_parse_error(js, "Unterminated object");
403 if (k->type != JSON_STRING)
404 json_parse_error(js, "Object key must be a string");
406 t = json_next_token(js);
407 if (t->type != JSON_NAME_SEP)
408 json_parse_error(js, "Colon expected");
410 struct json_node *v = json_next_value(js);
412 json_parse_error(js, "Unterminated object");
413 if (json_object_get(o, k->string)) // FIXME: Optimize
414 json_parse_error(js, "Key already set");
415 json_object_set(o, k->string, v);
417 t = json_next_token(js);
418 if (t->type == JSON_END_OBJECT)
420 if (t->type != JSON_VALUE_SEP)
421 json_parse_error(js, "Comma expected");
426 // Misplaced characters
428 json_parse_error(js, "Misplaced end of array");
429 case JSON_END_OBJECT:
430 json_parse_error(js, "Misplaced end of object");
432 json_parse_error(js, "Misplaced colon");
434 json_parse_error(js, "Misplaced comma");
440 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb)
442 json_set_input(js, fb);
444 struct json_node *n = json_next_value(js);
446 json_parse_error(js, "Empty input");
448 struct json_node *t = json_next_token(js);
449 if (t->type != JSON_EOF)
450 json_parse_error(js, "Only one top-level value allowed");