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.
13 #include <ucw/trans.h>
14 #include <ucw/ff-unicode.h>
15 #include <ucw/unicode.h>
16 #include <ucw-json/json.h>
21 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 // FIXME: Report column as well as line?
33 static void NONRET json_parse_error(struct json_context *js, const char *msg)
35 trans_throw("ucw.js.parse", js, "%s at line %u", msg, js->in_line);
38 static int json_get_char(struct json_context *js)
40 int c = bget_utf8_32_repl(js->in_fb, -2);
44 json_parse_error(js, "Malformed UTF-8 character");
46 // 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 if (!(c >= '0' && c <= '9'))
100 json_parse_error(js, "Malformed number: no digits after decimal point");
101 while (c >= '0' && c <= '9')
103 p = mp_append_char(js->pool, p, c);
104 c = json_get_char(js);
109 if (c == 'e' || c == 'E')
111 p = mp_append_char(js->pool, p, c);
112 c = json_get_char(js);
113 if (c == '+' || c == '-')
115 p = mp_append_char(js->pool, p, c);
116 c = json_get_char(js);
118 if (!(c >= '0' && c <= '9'))
119 json_parse_error(js, "Malformed number: empty exponent");
120 while (c >= '0' && c <= '9')
122 p = mp_append_char(js->pool, p, c);
123 c = json_get_char(js);
127 json_unget_char(js, c);
129 p = mp_end_string(js->pool, p);
131 double val = strtod(p, NULL);
133 json_parse_error(js, "Number out of range");
136 return json_new_number(js, val);
139 static struct json_node *json_parse_name(struct json_context *js, int c)
142 char *p = mp_start_noalign(js->pool, 0);
144 while (c >= 'a' && c <= 'z')
146 p = mp_append_char(js->pool, p, c);
147 c = json_get_char(js);
149 json_unget_char(js, c);
151 p = mp_end_string(js->pool, p);
153 if (!strcmp(p, "null"))
154 n = json_new_null(js);
155 else if (!strcmp(p, "false"))
156 n = json_new_bool(js, 0);
157 else if (!strcmp(p, "true"))
158 n = json_new_bool(js, 1);
160 json_parse_error(js, "Invalid literal name");
166 static uint json_parse_hex4(struct json_context *js)
169 for (int i=0; i<4; i++)
172 int c = json_get_char(js);
173 if (c >= '0' && c <= '9')
175 else if (c >= 'a' && c <= 'f')
177 else if (c >= 'A' && c <= 'F')
180 json_parse_error(js, "Invalid Unicode escape sequence");
185 static struct json_node *json_parse_string(struct json_context *js, int c)
187 char *p = mp_start_noalign(js->pool, 0);
189 c = json_get_char(js);
192 if (unlikely(c < 0x20))
194 if (c < 0 || c == 0x0d || c == 0x0a)
195 json_parse_error(js, "Unterminated string");
197 json_parse_error(js, "Invalid control character in string");
199 if (unlikely(c >= 0xd800 && c < 0xf900))
202 json_parse_error(js, "Invalid surrogate character in string");
204 json_parse_error(js, "Invalid private-use character in string");
206 if (unlikely(c > 0xf0000))
209 json_parse_error(js, "Invalid non-Unicode character in string");
211 json_parse_error(js, "Invalid private-use character in string");
215 c = json_get_char(js);
239 uint x = json_parse_hex4(js);
241 json_parse_error(js, "Zero bytes in strings are not supported");
242 if (x >= 0xd800 && x < 0xf900)
246 // High surrogate: low surrogate must follow
248 if (json_get_char(js) == '\\' && json_get_char(js) == 'u')
249 y = json_parse_hex4(js);
250 if (!(y >= 0xdc00 && y < 0xe000))
251 json_parse_error(js, "Escaped high surrogate codepoint must be followed by a low surrogate codepoint");
252 c = 0x10000 | ((x & 0x03ff) << 10) | (y & 0x03ff);
254 json_parse_error(js, "Invalid escaped private-use character");
259 json_parse_error(js, "Invalid escaped surrogate codepoint");
262 json_parse_error(js, "Invalid escaped private-use character");
267 json_parse_error(js, "Invalid backslash sequence in string");
270 p = mp_append_utf8_32(js->pool, p, c);
271 c = json_get_char(js);
274 p = mp_end_string(js->pool, p);
275 return json_new_string_ref(js, p);
278 struct json_node *json_peek_token(struct json_context *js)
280 if (unlikely(js->in_eof))
281 return json_triv_token(js, JSON_EOF);
283 int c = js->next_char;
287 c = json_get_char(js);
289 while (c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d)
293 c = json_get_char(js);
296 return json_triv_token(js, JSON_EOF);
298 if (c >= '0' && c <= '9' || c == '-')
299 return json_parse_number(js, c);
301 if (c >= 'a' && c <= 'z')
302 return json_parse_name(js, c);
305 return json_parse_string(js, c);
310 return json_triv_token(js, JSON_BEGIN_ARRAY);
312 return json_triv_token(js, JSON_END_ARRAY);
314 return json_triv_token(js, JSON_BEGIN_OBJECT);
316 return json_triv_token(js, JSON_END_OBJECT);
318 return json_triv_token(js, JSON_NAME_SEP);
320 return json_triv_token(js, JSON_VALUE_SEP);
322 json_parse_error(js, "Invalid character");
326 struct json_node *json_next_token(struct json_context *js)
330 struct json_node *t = js->next_token;
331 js->next_token = NULL;
335 struct json_node *json_next_object(struct json_context *js)
337 struct json_node *t = json_next_token(js);
352 case JSON_BEGIN_ARRAY:
354 struct json_node *a = json_new_array(js);
355 if (json_peek_token(js)->type == JSON_END_ARRAY)
359 struct json_node *v = json_next_object(js);
361 json_parse_error(js, "Unterminated array");
362 json_array_append(a, v);
364 t = json_next_token(js);
365 if (t->type == JSON_END_ARRAY)
367 if (t->type != JSON_VALUE_SEP)
368 json_parse_error(js, "Comma expected");
374 case JSON_BEGIN_OBJECT:
376 struct json_node *o = json_new_object(js);
377 if (json_peek_token(js)->type == JSON_END_OBJECT)
381 struct json_node *k = json_next_object(js);
383 json_parse_error(js, "Unterminated object");
384 if (k->type != JSON_STRING)
385 json_parse_error(js, "Object key must be a string");
387 t = json_next_token(js);
388 if (t->type != JSON_NAME_SEP)
389 json_parse_error(js, "Colon expected");
391 struct json_node *v = json_next_object(js);
393 json_parse_error(js, "Unterminated object");
394 if (json_object_get(o, k->string)) // FIXME: Optimize
395 json_parse_error(js, "Key already set");
396 json_object_set(o, k->string, v);
398 t = json_next_token(js);
399 if (t->type == JSON_END_OBJECT)
401 if (t->type != JSON_VALUE_SEP)
402 json_parse_error(js, "Comma expected");
407 // Misplaced characters
409 json_parse_error(js, "Misplaced end of array");
410 case JSON_END_OBJECT:
411 json_parse_error(js, "Misplaced end of object");
413 json_parse_error(js, "Misplaced colon");
415 json_parse_error(js, "Misplaced comma");
421 struct json_node *json_parse(struct json_context *js, struct fastbuf *fb)
423 json_set_input(js, fb);
425 struct json_node *n = json_next_object(js);
427 json_parse_error(js, "Empty input");
429 struct json_node *t = json_next_token(js);
430 if (t->type != JSON_EOF)
431 json_parse_error(js, "Only one top-level value allowed");