Skip to content

Fix parsing stream with first token size > parser's chunk size-4#45

Open
n-peugnet wants to merge 3 commits into
bzick:masterfrom
n-peugnet:issue-44-long-first-token
Open

Fix parsing stream with first token size > parser's chunk size-4#45
n-peugnet wants to merge 3 commits into
bzick:masterfrom
n-peugnet:issue-44-long-first-token

Conversation

@n-peugnet

@n-peugnet n-peugnet commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

When (first token size) > (parser's chunk size-4), the next chunks are
loaded during the parsing of the first token. This triggers the end of
the parsing loop, leaving the frist token as the only one parsed, and
thus having its "next" field set to nil. This in turn, lead
Stream.GoNext() to detect the end of the stream.

To fix this, Stream.GoNext() now starts by checking the next field of
the current token, parsing the current chunk if needed, before deciding
if the end of the stream is reached.

NextToken() also assumed that the next token was always parsed, which is
not the case. Add a new ensureParseNext() function that can be used in
both GoNext() and NextToken(), and that makes sure the next token is
parsed if the end of the stream has not been reached yet.

Tests have been added to prevent regressions.

Fixes #44

@n-peugnet n-peugnet force-pushed the issue-44-long-first-token branch 3 times, most recently from 84c07ed to eab3a71 Compare July 7, 2026 13:47
@n-peugnet n-peugnet changed the title Failing test for issue 44 Fix parsing stream with first token >= to parser's buffer Jul 7, 2026
@n-peugnet n-peugnet marked this pull request as ready for review July 7, 2026 16:30
@n-peugnet n-peugnet changed the title Fix parsing stream with first token >= to parser's buffer Fix parsing stream with first token >= to parser's chunk size-4 Jul 7, 2026
@n-peugnet n-peugnet changed the title Fix parsing stream with first token >= to parser's chunk size-4 Fix parsing stream with first token size > parser's chunk size-4 Jul 7, 2026
@n-peugnet n-peugnet force-pushed the issue-44-long-first-token branch from 2e9b9e9 to 99801b8 Compare July 12, 2026 11:22
Comment thread stream_test.go Outdated
defer stream.Close()

require.True(t, stream.IsValid())
assert.True(t, stream.CurrentToken().IsKeyword())

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

please use require instead of assert

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.

Done

When (first token size) > (parser's chunk size-4), the next chunks are
loaded during the parsing of the first token. This triggers the end of
the parsing loop, leaving the frist token as the only one parsed, and
thus having its "next" field set to nil. This in turn, lead
Stream.GoNext() to detect the end of the stream.

To fix this, Stream.GoNext() now starts by checking the next field of
the current token, parsing the current chunk if needed, before deciding
if the end of the stream is reached.

Fixes bzick#44
NextToken() also assumed that the next token was always parsed, which is
not the case. Add a new ensureParseNext() function that can be used in
both GoNext() and NextToken(), and that makes sure the next token is
parsed if the end of the stream has not been reached yet.
@n-peugnet n-peugnet force-pushed the issue-44-long-first-token branch from 99801b8 to fda5579 Compare July 13, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stops parsing after first token if its size is very close to the size of the parser's buffer

2 participants