aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2024-08-27 23:44:32 +1000
committerSteve Bennett <steveb@workware.net.au>2024-08-28 11:59:05 +1000
commitc5188c3323985a659cc03ed2d02eb98a2bfa68aa (patch)
treef2f520edcec1381dd94a28389270d96b1ecfade8
parent0bab3745bb1abcea2b1c3210ac7ddc97975608d1 (diff)
downloadjimtcl-c5188c3323985a659cc03ed2d02eb98a2bfa68aa.zip
jimtcl-c5188c3323985a659cc03ed2d02eb98a2bfa68aa.tar.gz
jimtcl-c5188c3323985a659cc03ed2d02eb98a2bfa68aa.tar.bz2
jsmn: add support for recording source line numbers
Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--jsmn/jsmn.c20
-rw-r--r--jsmn/jsmn.h2
2 files changed, 16 insertions, 6 deletions
diff --git a/jsmn/jsmn.c b/jsmn/jsmn.c
index 19d48e0..2174df9 100644
--- a/jsmn/jsmn.c
+++ b/jsmn/jsmn.c
@@ -18,6 +18,7 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
tok = &tokens[parser->toknext++];
tok->start = tok->end = -1;
tok->size = 0;
+ tok->line = 0;
#ifdef JSMN_PARENT_LINKS
tok->parent = -1;
#endif
@@ -28,11 +29,12 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
* Fills token type and boundaries.
*/
static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
- int start, int end) {
+ int start, int end, int line) {
token->type = type;
token->start = start;
token->end = end;
token->size = 0;
+ token->line = line;
}
/**
@@ -47,11 +49,13 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
switch (js[parser->pos]) {
+ case '\n' :
+ parser->line++;
+ /* fall-thru */
#ifndef JSMN_STRICT
- /* In strict mode primitive must be followed by "," or "}" or "]" */
case ':':
#endif
- case '\t' : case '\r' : case '\n' : case ' ' :
+ case '\t' : case '\r' : case ' ' :
case ',' : case ']' : case '}' :
goto found;
}
@@ -76,7 +80,7 @@ found:
parser->pos = start;
return JSMN_ERROR_NOMEM;
}
- jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
+ jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos, parser->line);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
@@ -109,7 +113,7 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
parser->pos = start;
return JSMN_ERROR_NOMEM;
}
- jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
+ jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos, parser->line);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
@@ -242,7 +246,10 @@ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
if (parser->toksuper != -1 && tokens != NULL)
tokens[parser->toksuper].size++;
break;
- case '\t' : case '\r' : case '\n' : case ' ':
+ case '\n' :
+ parser->line++;
+ /* fall-thru */
+ case '\t' : case '\r' : case ' ':
break;
case ':':
parser->toksuper = parser->toknext - 1;
@@ -322,5 +329,6 @@ void jsmn_init(jsmn_parser *parser) {
parser->toknext = 0;
parser->toksuper = -1;
parser->count = 0;
+ parser->line = 1;
}
diff --git a/jsmn/jsmn.h b/jsmn/jsmn.h
index 9c20657..e6ee22c 100644
--- a/jsmn/jsmn.h
+++ b/jsmn/jsmn.h
@@ -42,6 +42,7 @@ typedef struct {
int start;
int end;
int size;
+ int line;
#ifdef JSMN_PARENT_LINKS
int parent;
#endif
@@ -56,6 +57,7 @@ typedef struct {
unsigned int toknext; /* next token to allocate */
unsigned int count; /* number of tokens parsed */
int toksuper; /* superior token node, e.g parent object or array */
+ int line; /* current line number */
} jsmn_parser;
/**