Design: added assert macro and return macro.

master
Serge A. Zaitsev 14 years ago
parent 30370e37f8
commit 470c77fa63

@ -67,12 +67,6 @@ static int jsmn_parse_primitive(const unsigned char *js, jsontok_t *token) {
return -1; return -1;
} }
static void jsmn_error(struct jsmn_params *params, int pos) {
if (params->errpos != NULL) {
*params->errpos = pos;
}
}
static jsontok_t *jsmn_token_start(struct jsmn_params *params, 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;
jsontok_t *tokens = params->tokens; jsontok_t *tokens = params->tokens;
@ -100,10 +94,17 @@ 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) { int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, int *errpos) {
#define jsmn_assert(cond, pos, err) \ #define jsmn_return(error) \
if (!(cond)) { \ do { \
jsmn_error(&params, pos); \ if ((errpos) != NULL) { \
return (err); \ *(errpos) = (p - js); \
} \
return (error); \
} while (0)
#define jsmn_assert(cond, error) \
if (!(cond)) { \
jsmn_return(error); \
} }
struct jsmn_params params; struct jsmn_params params;
@ -128,31 +129,29 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in
case '{': case '[': case '{': case '[':
type = (*p == '{' ? JSON_OBJECT : JSON_ARRAY); type = (*p == '{' ? JSON_OBJECT : JSON_ARRAY);
cur_token = jsmn_token_start(&params, type, p - js); cur_token = jsmn_token_start(&params, type, p - js);
jsmn_assert(cur_token != NULL, p - js, -1); jsmn_assert(cur_token != NULL, -1);
cur_token->start = p - js;
break; break;
case '}' : case ']': case '}' : case ']':
type = (*p == '}' ? JSON_OBJECT : JSON_ARRAY); type = (*p == '}' ? JSON_OBJECT : JSON_ARRAY);
cur_token = jsmn_token_end(&params, type, p - js + 1); cur_token = jsmn_token_end(&params, type, p - js + 1);
jsmn_assert(cur_token != NULL, p - js, -1); jsmn_assert(cur_token != NULL, -1);
cur_token->end = p - js + 1;
break; break;
case '-': case '0': case '1' : case '2': case '3' : case '4': case '-': case '0': case '1' : case '2': case '3' : case '4':
case '5': case '6': case '7' : case '8': case '9': case '5': case '6': case '7' : case '8': case '9':
case 't': case 'f': case 'n' : case 't': case 'f': case 'n' :
cur_token = jsmn_token_start(&params, JSON_OTHER, p - js); cur_token = jsmn_token_start(&params, JSON_OTHER, p - js);
jsmn_assert(cur_token != NULL, p - js, -1); jsmn_assert(cur_token != NULL, -1);
r = jsmn_parse_primitive(js, cur_token); r = jsmn_parse_primitive(js, cur_token);
jsmn_assert(r == 0, p - js, -2); jsmn_assert(r == 0, -2);
p = &js[cur_token->end] - 1; p = &js[cur_token->end] - 1;
break; break;
case '\"': case '\"':
cur_token = jsmn_token_start(&params, JSON_STRING, p - js); cur_token = jsmn_token_start(&params, JSON_STRING, p - js);
jsmn_assert(cur_token != NULL, p - js, -1); jsmn_assert(cur_token != NULL, -1);
r = jsmn_parse_string(js, cur_token); r = jsmn_parse_string(js, cur_token);
jsmn_assert(r == 0, p - js, -2); jsmn_assert(r == 0, -2);
p = &js[cur_token->end]; p = &js[cur_token->end];
break; break;
@ -160,12 +159,13 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in
break; break;
default: default:
jsmn_error(&params, p - js); jsmn_assert(0, -1); /* Assert always fails */
return -1;
} }
p++; p++;
} }
jsmn_error(&params, 0); if (errpos != NULL) *errpos = 0;
return 0; return 0;
#undef jsmn_return
#undef jsmn_assert
} }

Loading…
Cancel
Save