6 typedef struct Lex Lex;
41 l->s += chartorune(&r, l->s);
54 fixsurrogate(Rune *rp, Rune r2)
59 if(r1 >= 0xD800 && r1 <= 0xDBFF){
60 if(r2 >= 0xDC00 && r2 <= 0xDFFF){
61 *rp = 0x10000 + (((r1 - 0xD800)<<10) | (r2 - 0xDC00));
66 if(r1 >= 0xDC00 && r1 <= 0xDFFF){
67 if(r2 >= 0xD800 && r2 <= 0xDBFF){
68 *rp = 0x10000 + (((r2 - 0xD800)<<10) | (r1 - 0xDC00));
86 if(r != 0x20 && r != 0x09 && r != 0x0A && r != 0x0D)
91 if(r == ']' && l->canjmp)
94 if(r == 0 || r == '{' || r == '[' || r == ']' || r == '}' || r == ':' || r == ','){
98 if(r >= 0x80 || isalpha(r)){
101 t += runetochar(t, &r);
102 if(t >= l->buf + l->slen){
103 werrstr("json: literal too long");
107 if(r < 0x80 && !isalpha(r))
112 if(strcmp(l->buf, "true") == 0)
114 else if(strcmp(l->buf, "false") == 0)
116 else if(strcmp(l->buf, "null") == 0)
119 werrstr("json: invalid literal");
124 if(isdigit(r) || r == '-'){
125 l->n = strtod(l->s-1, &l->s);
137 werrstr("json: invalid char in string %x", r);
151 for(i = 0; i < 4; i++){
152 if(!isxdigit(peekch(l)))
157 if(c >= '0' && c <= '9')
159 else if(c >= 'a' && c <= 'f')
161 else if(c >= 'A' && c <= 'F')
164 if(fixsurrogate(&r, r2)){
178 case '"': case '/': case '\\':
181 werrstr("json: invalid escape sequence \\%C", r);
186 t += runetochar(t, &r);
187 if(t >= l->buf + l->slen){
188 werrstr("json: string too long");
196 werrstr("json: invalid char %C", peekch(l));
208 if((j = mallocz(sizeof(*j), 1)) == nil)
218 werrstr("json: unexpected eof");
233 if((j->s = strdup(l->buf)) == nil)
255 if(setjmp(l->jmp) > 0){
266 werrstr("json: syntax error, not string");
269 if((e = mallocz(sizeof(*e), 1)) == nil)
271 e->name = strdup(l->buf);
272 if(e->name == nil || lex(l) < 0){
277 werrstr("json: syntax error, not colon");
282 if((e = mallocz(sizeof(*e), 1)) == nil)
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");
321 memset(&l, 0, sizeof(l));
324 if((l.buf = mallocz(l.slen+1, 1)) == nil)
342 case JSONArray: case JSONObject:
343 for(e = j->first; e != nil; e = f){
355 jsonbyname(JSON *j, char *n)
359 if(j->t != JSONObject){
360 werrstr("not an object");
363 for(e = j->first; e != nil; e = e->next)
364 if(strcmp(e->name, n) == 0)
366 werrstr("key '%s' not found", n);
375 if(j->t != JSONString){
376 werrstr("not a string");