diff options
author | Serge A. Zaitsev <devnull@localhost> | 2010-11-16 11:45:10 +0200 |
---|---|---|
committer | Serge A. Zaitsev <devnull@localhost> | 2010-11-16 11:45:10 +0200 |
commit | 30370e37f879a0871ccbc078f8d3c86938b3a063 (patch) | |
tree | b192953a2c75943ee0c144e127200a767761ee09 /jsmn.c | |
parent | b99663079c1542e514f0ca8056cd87cb3683bd7e (diff) |
Design: Added asserts in parser. Increased number of tokens in demo.
Diffstat (limited to 'jsmn.c')
-rw-r--r-- | jsmn.c | 22 |
1 files changed, 13 insertions, 9 deletions
@@ -100,6 +100,12 @@ static jsontok_t *jsmn_token_end(struct jsmn_params *params, jsontype_t type, in int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, int *errpos) { +#define jsmn_assert(cond, pos, err) \ + if (!(cond)) { \ + jsmn_error(¶ms, pos); \ + return (err); \ + } + struct jsmn_params params; int r; @@ -122,11 +128,13 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in case '{': case '[': type = (*p == '{' ? JSON_OBJECT : JSON_ARRAY); cur_token = jsmn_token_start(¶ms, type, p - js); + jsmn_assert(cur_token != NULL, p - js, -1); cur_token->start = 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; |