6 typedef struct Lex Lex;
39 l->s += chartorune(&r, l->s);
58 if(r != 0x20 && r != 0x09 && r != 0x0A && r != 0x0D)
66 fixsurrogate(Rune *rp, Rune r2)
71 if(r1 >= 0xD800 && r1 <= 0xDBFF){
72 if(r2 >= 0xDC00 && r2 <= 0xDFFF){
73 *rp = 0x10000 + (((r1 - 0xD800)<<10) | (r2 - 0xDC00));
78 if(r1 >= 0xDC00 && r1 <= 0xDFFF){
79 if(r2 >= 0xD800 && r2 <= 0xDBFF){
80 *rp = 0x10000 + (((r2 - 0xD800)<<10) | (r1 - 0xDC00));
98 if(r == 0 || r == '{' || r == '[' || r == ']' || r == '}' || r == ':' || r == ','){
102 if(r >= 0x80 || isalpha(r)){
105 t += runetochar(t, &r);
106 if(t >= l->buf + l->slen){
107 werrstr("json: literal too long");
111 if(r < 0x80 && !isalpha(r))
116 if(strcmp(l->buf, "true") == 0)
118 else if(strcmp(l->buf, "false") == 0)
120 else if(strcmp(l->buf, "null") == 0)
123 werrstr("json: invalid literal");
128 if(isdigit(r) || r == '-'){
129 l->n = strtod(l->s-1, &l->s);
141 werrstr("json: invalid char in string %x", r);
155 for(i = 0; i < 4; i++){
156 if(!isxdigit(peekch(l)))
161 if(c >= '0' && c <= '9')
163 else if(c >= 'a' && c <= 'f')
165 else if(c >= 'A' && c <= 'F')
168 if(fixsurrogate(&r, r2)){
182 case '"': case '/': case '\\':
185 werrstr("json: invalid escape sequence \\%C", r);
190 t += runetochar(t, &r);
191 if(t >= l->buf + l->slen){
192 werrstr("json: string too long");
200 werrstr("json: invalid char %C", peekch(l));
212 if((j = mallocz(sizeof(*j), 1)) == nil)
222 werrstr("json: unexpected eof");
237 if((j->s = strdup(l->buf)) == nil)
257 if(peeknonspace(l) == ']'){
268 werrstr("json: syntax error, not string");
271 if((e = mallocz(sizeof(*e), 1)) == nil)
273 e->name = strdup(l->buf);
274 if(e->name == nil || lex(l) < 0){
279 werrstr("json: syntax error, not colon");
284 if((e = mallocz(sizeof(*e), 1)) == nil)
296 if(l->t == (obj ? '}' : ']'))
299 werrstr("json: syntax error, neither comma nor ending paren");
307 case ']': case '}': case ',': case ':':
308 werrstr("json: unexpected %C", l->t);
311 werrstr("json: the front fell off");
323 memset(&l, 0, sizeof(l));
326 if((l.buf = mallocz(l.slen+UTFmax+1, 1)) == nil)
346 case JSONArray: case JSONObject:
347 for(e = j->first; e != nil; e = f){
359 jsonbyname(JSON *j, char *n)
363 if(j->t != JSONObject){
364 werrstr("not an object");
367 for(e = j->first; e != nil; e = e->next)
368 if(strcmp(e->name, n) == 0)
370 werrstr("key '%s' not found", n);
379 if(j->t != JSONString){
380 werrstr("not a string");