6 typedef struct Lex Lex;
38 l->s += chartorune(&r, l->s);
51 fixsurrogate(Rune *rp, Rune r2)
56 if(r1 >= 0xD800 && r1 <= 0xDBFF){
57 if(r2 >= 0xDC00 && r2 <= 0xDFFF){
58 *rp = 0x10000 + (((r1 - 0xD800)<<10) | (r2 - 0xDC00));
63 if(r1 >= 0xDC00 && r1 <= 0xDFFF){
64 if(r2 >= 0xD800 && r2 <= 0xDBFF){
65 *rp = 0x10000 + (((r2 - 0xD800)<<10) | (r1 - 0xDC00));
83 if(r != 0x20 && r != 0x09 && r != 0x0A && r != 0x0D)
88 if(r == ']' && l->canjmp)
91 if(r == 0 || r == '{' || r == '[' || r == ']' || r == '}' || r == ':' || r == ','){
95 if(r >= 0x80 || isalpha(r)){
98 t += runetochar(t, &r);
99 if(t >= l->buf + sizeof(l->buf)){
100 werrstr("json: literal too long");
104 if(r < 0x80 && !isalpha(r))
109 if(strcmp(l->buf, "true") == 0)
111 else if(strcmp(l->buf, "false") == 0)
113 else if(strcmp(l->buf, "null") == 0)
116 werrstr("json: invalid literal");
121 if(isdigit(r) || r == '-'){
122 l->n = strtod(l->s-1, &l->s);
134 werrstr("json: invalid char in string %x", r);
148 for(i = 0; i < 4; i++){
149 if(!isxdigit(peekch(l)))
154 if(c >= '0' && c <= '9')
156 if(c >= 'a' && c <= 'f')
158 else if(c >= 'A' && c <= 'F')
161 if(fixsurrogate(&r, r2)){
175 case '"': case '/': case '\\':
178 werrstr("json: invalid escape sequence \\%C", r);
183 t += runetochar(t, &r);
184 if(t >= l->buf + sizeof(l->buf)){
185 werrstr("json: string too long");
193 werrstr("json: invalid char %C", peekch(l));
205 j = mallocz(sizeof(*j), 1);
215 werrstr("json: unexpected eof");
230 j->s = strdup(l->buf);
253 if(setjmp(l->jmp) > 0){
264 werrstr("json: syntax error, not string");
267 e = mallocz(sizeof(*e), 1);
270 e->name = strdup(l->buf);
271 if(e->name == nil || lex(l) < 0){
276 werrstr("json: syntax error, not colon");
281 e = mallocz(sizeof(*e), 1);
294 if(l->t == (obj ? '}' : ']'))
297 werrstr("json: syntax error, neither comma nor ending paren");
305 case ']': case '}': case ',': case ':':
306 werrstr("json: unexpected %C", l->t);
309 werrstr("json: the front fell off");
320 memset(&l, 0, sizeof(l));
335 case JSONArray: case JSONObject:
336 for(e = j->first; e != nil; e = f){
348 jsonbyname(JSON *j, char *n)
352 if(j->t != JSONObject){
353 werrstr("not an object");
356 for(e = j->first; e != nil; e = e->next)
357 if(strcmp(e->name, n) == 0)
359 werrstr("key '%s' not found", n);
368 if(j->t != JSONString){
369 werrstr("not a string");