Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions lib/pure/parsejson.nim
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,33 @@ proc parseNumber(my: var JsonParser) =
inc(pos)
my.bufpos = pos

proc parseName(my: var JsonParser) =
var pos = my.bufpos
if my.buf[pos] in IdentStartChars:
while my.buf[pos] in IdentChars:
add(my.a, my.buf[pos])
inc(pos)
my.bufpos = pos
proc parseKeyword(my: var JsonParser): TokKind =
let pos = my.bufpos
case my.buf[pos]
Copy link
Copy Markdown
Contributor

@arnetheduck arnetheduck Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when my.buf.len <= pos + len(match)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I suppose it reads the terminating character and works correctly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or, more likely, [] raises a defect?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way this parser is built and also looking at lexbase.nim, it makes the assumption that [] doesnt perform range checks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see 👍 there's an extra non-ident character injected always, it seems so although [] performs a range check it should not be violated.

of 'n':
if my.buf[pos + 1] == 'u' and my.buf[pos + 2] == 'l' and
my.buf[pos + 3] == 'l' and my.buf[pos + 4] notin IdentChars:
my.bufpos = pos + 4
return tkNull
of 't':
if my.buf[pos + 1] == 'r' and my.buf[pos + 2] == 'u' and
my.buf[pos + 3] == 'e' and my.buf[pos + 4] notin IdentChars:
my.bufpos = pos + 4
return tkTrue
of 'f':
if my.buf[pos + 1] == 'a' and my.buf[pos + 2] == 'l' and
my.buf[pos + 3] == 's' and my.buf[pos + 4] == 'e' and
my.buf[pos + 5] notin IdentChars:
my.bufpos = pos + 5
return tkFalse
else:
discard

var endPos = pos
while my.buf[endPos] in IdentChars:
inc(endPos)
my.bufpos = endPos
result = tkError

proc getTok*(my: var JsonParser): TokKind =
setLen(my.a, 0)
Expand Down Expand Up @@ -384,12 +404,7 @@ proc getTok*(my: var JsonParser): TokKind =
of '\0':
result = tkEof
of 'a'..'z', 'A'..'Z', '_':
parseName(my)
case my.a
of "null": result = tkNull
of "true": result = tkTrue
of "false": result = tkFalse
else: result = tkError
result = parseKeyword(my)
else:
inc(my.bufpos)
result = tkError
Expand Down
Loading