From 30370e37f879a0871ccbc078f8d3c86938b3a063 Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Tue, 16 Nov 2010 11:45:10 +0200 Subject: [PATCH] Design: Added asserts in parser. Increased number of tokens in demo. --- demo.c | 8 ++++---- jsmn.c | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/demo.c b/demo.c index 240d8a5..a9dfa41 100644 --- a/demo.c +++ b/demo.c @@ -8,9 +8,9 @@ #include "jsmn.h" -#define NUM_TOKENS 20 +#define NUM_TOKENS 30 -static void json_dump_obj(jsontok_t *obj, const char *js) { +static void jsmn_dump_obj(jsontok_t *obj, const char *js) { size_t len; if (obj->end < 0 || obj->start < 0) { @@ -87,12 +87,12 @@ int main(int argc, char *argv[]) { r = jsmn_parse((unsigned char *) js, tokens, NUM_TOKENS, &errpos); if (r < 0) { - printf("error at pos %d: %s\n", errpos, &js[errpos]); + printf("error %d at pos %d: %s\n", r, errpos, &js[errpos]); exit(EXIT_FAILURE); } for (i = 0; istart = p - js; break; case '}' : case ']': type = (*p == '}' ? JSON_OBJECT : JSON_ARRAY); cur_token = jsmn_token_end(¶ms, type, p - js + 1); + jsmn_assert(cur_token != NULL, p - js, -1); cur_token->end = p - js + 1; break; @@ -134,21 +142,17 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in case '5': case '6': case '7' : case '8': case '9': case 't': case 'f': case 'n' : cur_token = jsmn_token_start(¶ms, JSON_OTHER, p - js); + jsmn_assert(cur_token != NULL, p - js, -1); r = jsmn_parse_primitive(js, cur_token); - if (r < 0) { - jsmn_error(¶ms, p - js); - return -1; - } - p = &js[cur_token->end]; + jsmn_assert(r == 0, p - js, -2); + p = &js[cur_token->end] - 1; break; case '\"': cur_token = jsmn_token_start(¶ms, JSON_STRING, p - js); + jsmn_assert(cur_token != NULL, p - js, -1); r = jsmn_parse_string(js, cur_token); - if (r < 0) { - jsmn_error(¶ms, p - js); - return -1; - } + jsmn_assert(r == 0, p - js, -2); p = &js[cur_token->end]; break;