|
|
@ -11,6 +11,7 @@ static jsmntok_t *jsmn_get_token(jsmn_parser *parser) {
|
|
|
|
for (i = parser->curtoken; i<parser->num_tokens; i++) {
|
|
|
|
for (i = parser->curtoken; i<parser->num_tokens; i++) {
|
|
|
|
if (tokens[i].start == -1 && tokens[i].end == -1) {
|
|
|
|
if (tokens[i].start == -1 && tokens[i].end == -1) {
|
|
|
|
parser->curtoken = i;
|
|
|
|
parser->curtoken = i;
|
|
|
|
|
|
|
|
tokens[i].size = 0;
|
|
|
|
return &tokens[i];
|
|
|
|
return &tokens[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -40,6 +41,7 @@ void jsmn_init_parser(jsmn_parser *parser, const char *js,
|
|
|
|
parser->tokens = tokens;
|
|
|
|
parser->tokens = tokens;
|
|
|
|
parser->num_tokens = num_tokens;
|
|
|
|
parser->num_tokens = num_tokens;
|
|
|
|
parser->curtoken = 0;
|
|
|
|
parser->curtoken = 0;
|
|
|
|
|
|
|
|
parser->cursize = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < parser->num_tokens; i++) {
|
|
|
|
for (i = 0; i < parser->num_tokens; i++) {
|
|
|
|
jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
|
|
|
|
jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
|
|
|
@ -132,7 +134,7 @@ static int jsmn_parse_string(jsmn_parser *parser) {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
jsmnerr_t jsmn_parse(jsmn_parser *parser) {
|
|
|
|
jsmnerr_t jsmn_parse(jsmn_parser *parser) {
|
|
|
|
int r;
|
|
|
|
int r;
|
|
|
|
unsigned int i;
|
|
|
|
int i;
|
|
|
|
const char *js;
|
|
|
|
const char *js;
|
|
|
|
jsmntype_t type;
|
|
|
|
jsmntype_t type;
|
|
|
|
jsmntok_t *token;
|
|
|
|
jsmntok_t *token;
|
|
|
@ -147,8 +149,11 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) {
|
|
|
|
token = jsmn_get_token(parser);
|
|
|
|
token = jsmn_get_token(parser);
|
|
|
|
if (token == NULL)
|
|
|
|
if (token == NULL)
|
|
|
|
return JSMN_ERROR_NOMEM;
|
|
|
|
return JSMN_ERROR_NOMEM;
|
|
|
|
|
|
|
|
if (parser->cursize != NULL)
|
|
|
|
|
|
|
|
(*parser->cursize)++;
|
|
|
|
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
|
|
|
|
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
|
|
|
|
token->start = parser->pos;
|
|
|
|
token->start = parser->pos;
|
|
|
|
|
|
|
|
parser->cursize = &token->size;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case '}': case ']':
|
|
|
|
case '}': case ']':
|
|
|
|
type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
|
|
|
|
type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
|
|
|
@ -158,20 +163,32 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) {
|
|
|
|
if (token->type != type) {
|
|
|
|
if (token->type != type) {
|
|
|
|
return JSMN_ERROR_INVAL;
|
|
|
|
return JSMN_ERROR_INVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parser->cursize = NULL;
|
|
|
|
token->end = parser->pos + 1;
|
|
|
|
token->end = parser->pos + 1;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i >= 0; i--) {
|
|
|
|
|
|
|
|
token = &parser->tokens[i];
|
|
|
|
|
|
|
|
if (token->start != -1 && token->end == -1) {
|
|
|
|
|
|
|
|
parser->cursize = &token->size;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
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' :
|
|
|
|
r = jsmn_parse_primitive(parser);
|
|
|
|
r = jsmn_parse_primitive(parser);
|
|
|
|
if (r < 0) return r;
|
|
|
|
if (r < 0) return r;
|
|
|
|
|
|
|
|
if (parser->cursize != NULL)
|
|
|
|
|
|
|
|
(*parser->cursize)++;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
case '\"':
|
|
|
|
r = jsmn_parse_string(parser);
|
|
|
|
r = jsmn_parse_string(parser);
|
|
|
|
if (r < 0) return r;
|
|
|
|
if (r < 0) return r;
|
|
|
|
|
|
|
|
if (parser->cursize != NULL)
|
|
|
|
|
|
|
|
(*parser->cursize)++;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
|
|
|
|
case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ':
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|