6 typedef struct Lex Lex;
10 TSTRING = (1<<(8*sizeof(Rune)))+1,
38 l->s += chartorune(&r, l->s);
58 if(r != 0x20 && r != 0x09 && r != 0x0A && r != 0x0D)
63 if(r == ']' && l->canjmp)
66 if(r == 0 || r == '{' || r == '[' || r == ']' || r == '}' || r == ':' || r == ','){
70 if(r >= 0x80 || isalpha(r)){
73 t += runetochar(t, &r);
74 if(t >= l->buf + sizeof(l->buf)){
75 werrstr("json: literal too long");
79 if(r < 0x80 && !isalpha(r))
84 if(strcmp(l->buf, "true") == 0)
86 else if(strcmp(l->buf, "false") == 0)
88 else if(strcmp(l->buf, "null") == 0)
91 werrstr("json: invalid literal");
96 if(isdigit(r) || r == '-'){
97 l->n = strtod(l->s-1, &l->s);
108 werrstr("json: invalid char in string %x", r);
129 case '"': case '/': case '\\':
132 werrstr("json: invalid escape sequence \\%C", r);
136 t += runetochar(t, &r);
137 if(t >= l->buf + sizeof(l->buf)){
138 werrstr("json: string too long");
146 werrstr("json: invalid char %C", peekch(l));
158 j = mallocz(sizeof(*j), 1);
168 werrstr("json: unexpected eof");
183 j->s = strdup(l->buf);
206 if(setjmp(l->jmp) > 0){
217 werrstr("json: syntax error, not string");
220 e = mallocz(sizeof(*e), 1);
223 e->name = strdup(l->buf);
224 if(e->name == nil || lex(l) < 0){
229 werrstr("json: syntax error, not colon");
234 e = mallocz(sizeof(*e), 1);
247 if(l->t == (obj ? '}' : ']'))
250 werrstr("json: syntax error, neither comma nor ending paren");
258 case ']': case '}': case ',': case ':':
259 werrstr("json: unexpected %C", l->t);
262 werrstr("json: the front fell off");
273 memset(&l, 0, sizeof(l));
288 case JSONArray: case JSONObject:
289 for(e = j->first; e != nil; e = f){
301 jsonbyname(JSON *j, char *n)
305 if(j->t != JSONObject){
306 werrstr("not an object");
309 for(e = j->first; e != nil; e = e->next)
310 if(strcmp(e->name, n) == 0)
312 werrstr("key '%s' not found", n);
321 if(j->t != JSONString){
322 werrstr("not a string");