Design: unrolled reference tables. Nested functions moved outside. Got smaller code.

master
Serge A. Zaitsev 15 years ago
parent aa97d8b599
commit a70dab5cf9

@ -49,6 +49,8 @@ static void json_dump_obj(jsontok_t *obj, const char *js) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i; int i;
int r;
int errpos;
jsontok_t tokens[NUM_TOKENS]; jsontok_t tokens[NUM_TOKENS];
FILE *f; FILE *f;
int filesize = 0; int filesize = 0;
@ -66,7 +68,6 @@ int main(int argc, char *argv[]) {
} }
while (1) { while (1) {
int r;
char buf[BUFSIZ]; char buf[BUFSIZ];
r = fread(buf, 1, BUFSIZ, f); r = fread(buf, 1, BUFSIZ, f);
if (r <= 0) { if (r <= 0) {
@ -84,7 +85,11 @@ int main(int argc, char *argv[]) {
fclose(f); fclose(f);
jsmn_parse((unsigned char *) js, tokens, NUM_TOKENS, NULL); r = jsmn_parse((unsigned char *) js, tokens, NUM_TOKENS, &errpos);
if (r < 0) {
printf("error at pos %d: %s\n", errpos, &js[errpos]);
exit(EXIT_FAILURE);
}
for (i = 0; i<NUM_TOKENS; i++) { for (i = 0; i<NUM_TOKENS; i++) {
json_dump_obj(&tokens[i], js); json_dump_obj(&tokens[i], js);

224
jsmn.c

@ -2,97 +2,113 @@
#include "jsmn.h" #include "jsmn.h"
enum { struct jsmn_params {
JSON_SKIP = 0, jsontok_t *tokens;
JSON_OPEN = 1, size_t num_tokens;
JSON_CLOSE = 2, int **errpos;
JSON_BARE = 3,
JSON_UNBARE = 4,
JSON_QUOTE = 5,
JSON_UNQUOTE = 6,
JSON_ERROR = 7
}; };
#define JSON_SYM_SKIP(sym) \ /**
[sym] = JSON_SKIP * Read the string from JSON data. Store string
*/
#define JSON_SYM_ERROR(sym) \ static int jsmn_parse_string(const unsigned char *js, jsontok_t *token) {
[sym] = JSON_ERROR const unsigned char *p;
#define JSON_SYM_OPEN(sym) \ /* Check if string begins from a quote */
[sym] = JSON_OPEN if (js[token->start] != '\"') {
return -1;
}
#define JSON_SYM_CLOSE(sym) \ /* Skip starting quote */
[sym] = JSON_CLOSE token->start++;
#define JSON_SYM_BARE(sym) \ for (p = &js[token->start]; *p != '\0'; p++) {
[sym] = JSON_BARE /* Quote: end of string */
if (*p == '\"') {
token->end = p - js;
return 0;
}
#define JSON_SYM_UNBARE(sym) \ /* Backslash: Quoted symbol expected */
[sym] = JSON_UNBARE if (*p == '\\') {
p++;
switch (*p) {
/* Allowed escaped symbols */
case '\"': case '/' : case '\\' : case 'b' :
case 'f' : case 'r' : case 'n' : case 't' :
break;
/* Allows escaped symbol \uXXXX */
case 'u':
/* TODO */
break;
/* Unexpected symbol */
default:
return -1;
}
}
}
return -1;
}
#define JSON_SYM_QUOTE(sym) \ static int jsmn_parse_primitive(const unsigned char *js, jsontok_t *token) {
[sym] = JSON_QUOTE const unsigned char *p;
#define JSON_SYM_UNQUOTE(sym) \ for (p = &js[token->start]; *p != '\0'; p++) {
[sym] = JSON_UNQUOTE switch (*p) {
case '\t' : case '\r' : case '\n' : case ' ' :
case ',' : case ']' : case '}' :
token->end = p - js;
return 0;
}
if (*p < 32 || *p >= 127) {
return -1;
}
}
return -1;
}
int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, int **errpos) { static void jsmn_error(struct jsmn_params *params, int pos) {
if (params->errpos != NULL) {
*params->errpos = pos;
}
}
int jsmn_token_start(jsontype_t type, int pos) { static jsontok_t *jsmn_token_start(struct jsmn_params *params, jsontype_t type, int pos) {
unsigned int i; unsigned int i;
for (i = 0; i<num_tokens; i++) { jsontok_t *tokens = params->tokens;
if (tokens[i].start == -1 && tokens[i].end == -1) { for (i = 0; i<params->num_tokens; i++) {
tokens[i].start = pos; if (tokens[i].start == -1 && tokens[i].end == -1) {
tokens[i].type = type; tokens[i].start = pos;
return 0; tokens[i].type = type;
} return &tokens[i];
} }
return -1;
} }
return NULL;
}
int jsmn_token_end(jsontype_t type, int pos) { static jsontok_t *jsmn_token_end(struct jsmn_params *params, jsontype_t type, int pos) {
int i; int i;
for (i = num_tokens - 1; i>= 0; i--) { jsontok_t *tokens = params->tokens;
if (tokens[i].type == type && tokens[i].start != -1 && tokens[i].end == -1) { for (i = params->num_tokens - 1; i>= 0; i--) {
tokens[i].end = pos; if (tokens[i].type == type && tokens[i].start != -1 && tokens[i].end == -1) {
return 0; tokens[i].end = pos;
} return &tokens[i];
} }
return -1;
} }
return NULL;
}
int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, int **errpos) {
struct jsmn_params params;
int r;
const unsigned char *p; const unsigned char *p;
jsontok_t *cur_token;
int obj_common[] = { params.num_tokens = num_tokens;
JSON_SYM_ERROR(0 ... 255), params.tokens = tokens;
JSON_SYM_SKIP('\t'), JSON_SYM_SKIP('\r'),JSON_SYM_SKIP('\n'), params.errpos = errpos;
JSON_SYM_SKIP(':'), JSON_SYM_SKIP(','), JSON_SYM_SKIP(' '),
JSON_SYM_QUOTE('\"'),
JSON_SYM_OPEN('['), JSON_SYM_CLOSE(']'),
JSON_SYM_OPEN('{'), JSON_SYM_CLOSE('}'),
JSON_SYM_BARE('-'), JSON_SYM_BARE('0'...'9'),
JSON_SYM_BARE('t'), JSON_SYM_BARE('f'), JSON_SYM_BARE('n') /* true false null */
};
int obj_bare[] = {
JSON_SYM_ERROR(0 ... 31),
JSON_SYM_ERROR(127 ... 255),
JSON_SYM_SKIP(32 ... 126),
JSON_SYM_UNBARE('\t'), JSON_SYM_UNBARE(' '),
JSON_SYM_UNBARE('\r'), JSON_SYM_UNBARE('\n'),
JSON_SYM_UNBARE(','), JSON_SYM_UNBARE(']'),
JSON_SYM_UNBARE('}')
};
int obj_string[] = {
JSON_SYM_ERROR(0 ... 31), JSON_SYM_ERROR(127),
JSON_SYM_SKIP(32 ... 126),
JSON_SYM_UNQUOTE('\"'),
JSON_SYM_ERROR(248 ... 255),
};
int *obj_state = obj_common;
unsigned int i; unsigned int i;
for (i = 0; i<num_tokens; i++) { for (i = 0; i<num_tokens; i++) {
@ -101,42 +117,48 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in
} }
for (p = js; *p != '\0'; ) { for (p = js; *p != '\0'; ) {
switch (obj_state[*p]) { switch (*p) {
case JSON_ERROR: case '{': case '[':
if (errpos != NULL) { cur_token = jsmn_token_start(&params, JSON_OBJECT, p - js);
*errpos = p - js; cur_token->start = p - js;
}
return -1;
case JSON_OPEN:
jsmn_token_start(JSON_OBJECT, p - js);
break; break;
case JSON_CLOSE: case '}' : case ']':
jsmn_token_end(JSON_OBJECT, p - js + 1); cur_token = jsmn_token_end(&params, JSON_OBJECT, p - js + 1);
cur_token->end = p - js + 1;
break; break;
case JSON_BARE: case '-': case '0': case '1' : case '2': case '3' : case '4':
jsmn_token_start(JSON_OTHER, p - js); case '5': case '6': case '7' : case '8': case '9':
obj_state = obj_bare; case 't': case 'f': case 'n' :
break; cur_token = jsmn_token_start(&params, JSON_OTHER, p - js);
case JSON_UNBARE: r = jsmn_parse_primitive(js, cur_token);
jsmn_token_end(JSON_OTHER, p - js); if (r < 0) {
obj_state = obj_common; jsmn_error(&params, p - js);
continue; return -1;
}
case JSON_QUOTE: p = &js[cur_token->end];
jsmn_token_start(JSON_STRING, p - js + 1);
obj_state = obj_string;
break; break;
case JSON_UNQUOTE:
jsmn_token_end(JSON_STRING, p - js); case '\"':
obj_state = obj_common; cur_token = jsmn_token_start(&params, JSON_STRING, p - js);
r = jsmn_parse_string(js, cur_token);
if (r < 0) {
jsmn_error(&params, p - js);
return -1;
}
p = &js[cur_token->end];
break; break;
case JSON_SKIP:
case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
break; break;
default:
jsmn_error(&params, p - js);
return -1;
} }
p++; p++;
} }
jsmn_error(&params, 0);
return 0; return 0;
} }

Loading…
Cancel
Save