Backport from xs.

This commit is contained in:
default 2023-08-21 09:32:05 +02:00
parent cf0aa96a35
commit 3495c69b35
2 changed files with 21 additions and 35 deletions

View file

@ -323,28 +323,24 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
} }
static xs_list *_xs_json_load_array(FILE *f, js_type *t); static xs_list *_xs_json_load_array(FILE *f);
static xs_dict *_xs_json_load_object(FILE *f, js_type *t); static xs_dict *_xs_json_load_object(FILE *f);
static xs_list *_xs_json_load_array(FILE *f, js_type *t) static xs_list *_xs_json_load_array(FILE *f)
/* parses a JSON array */ /* parses a JSON array */
{ {
xs_list *l = xs_list_new(); xs_list *l = xs_list_new();
int c = 0; int c = 0;
*t = JS_INCOMPLETE; for (;;) {
while (*t == JS_INCOMPLETE) {
js_type tt; js_type tt;
xs *v = _xs_json_load_lexer(f, &tt); xs *v = _xs_json_load_lexer(f, &tt);
if (tt == JS_ERROR) if (tt == JS_ERROR)
break; break;
if (tt == JS_CBRACK) { if (tt == JS_CBRACK)
*t = JS_ARRAY; return l;
break;
}
if (c > 0) { if (c > 0) {
if (tt == JS_COMMA) if (tt == JS_COMMA)
@ -354,26 +350,23 @@ static xs_list *_xs_json_load_array(FILE *f, js_type *t)
} }
if (tt == JS_OBRACK) if (tt == JS_OBRACK)
v = _xs_json_load_array(f, &tt); v = _xs_json_load_array(f);
else else
if (tt == JS_OCURLY) if (tt == JS_OCURLY)
v = _xs_json_load_object(f, &tt); v = _xs_json_load_object(f);
if (tt < JS_VALUE) if (v == NULL)
break; break;
l = xs_list_append(l, v); l = xs_list_append(l, v);
c++; c++;
} }
if (*t == JS_INCOMPLETE || *t == JS_ERROR) return xs_free(l);
l = xs_free(l);
return l;
} }
static xs_dict *_xs_json_load_object(FILE *f, js_type *t) static xs_dict *_xs_json_load_object(FILE *f)
/* parses a JSON object */ /* parses a JSON object */
{ {
xs_dict *d = xs_dict_new(); xs_dict *d = xs_dict_new();
@ -381,9 +374,7 @@ static xs_dict *_xs_json_load_object(FILE *f, js_type *t)
d = xs_dict_new(); d = xs_dict_new();
*t = JS_INCOMPLETE; for (;;) {
while (*t == JS_INCOMPLETE) {
js_type tt; js_type tt;
xs *k = _xs_json_load_lexer(f, &tt); xs *k = _xs_json_load_lexer(f, &tt);
xs *v = NULL; xs *v = NULL;
@ -391,10 +382,8 @@ static xs_dict *_xs_json_load_object(FILE *f, js_type *t)
if (tt == JS_ERROR) if (tt == JS_ERROR)
break; break;
if (tt == JS_CCURLY) { if (tt == JS_CCURLY)
*t = JS_OBJECT; return d;
break;
}
if (c > 0) { if (c > 0) {
if (tt == JS_COMMA) if (tt == JS_COMMA)
@ -414,22 +403,19 @@ static xs_dict *_xs_json_load_object(FILE *f, js_type *t)
v = _xs_json_load_lexer(f, &tt); v = _xs_json_load_lexer(f, &tt);
if (tt == JS_OBRACK) if (tt == JS_OBRACK)
v = _xs_json_load_array(f, &tt); v = _xs_json_load_array(f);
else else
if (tt == JS_OCURLY) if (tt == JS_OCURLY)
v = _xs_json_load_object(f, &tt); v = _xs_json_load_object(f);
if (tt < JS_VALUE) if (v == NULL)
break; break;
d = xs_dict_append(d, k, v); d = xs_dict_append(d, k, v);
c++; c++;
} }
if (*t == JS_INCOMPLETE || *t == JS_ERROR) return xs_free(d);
d = xs_free(d);
return d;
} }
@ -457,10 +443,10 @@ xs_val *xs_json_load(FILE *f)
xs_free(_xs_json_load_lexer(f, &t)); xs_free(_xs_json_load_lexer(f, &t));
if (t == JS_OBRACK) if (t == JS_OBRACK)
v = _xs_json_load_array(f, &t); v = _xs_json_load_array(f);
else else
if (t == JS_OCURLY) if (t == JS_OCURLY)
v = _xs_json_load_object(f, &t); v = _xs_json_load_object(f);
return v; return v;
} }

View file

@ -1 +1 @@
/* ca82377df89097bd3965875fd63286d6c7af26eb */ /* f42504297420abef44904644524207f8a1440494 */