Repository navigation
Conversation
Lexer.New documents a reusable lexer, but Lexer.Reset only updated source, tokens, and state while leaving err, start, end, and eof from the previous input intact. Reusing a Lexer via Reset therefore skipped input or emitted EOF immediately on subsequent valid sources, and kept returning a stale error after a failed lex. Clear err, start, end, and eof in Reset while preserving caller options such as DisableIfOperator.
|
I encountered the same issue and this is preventing me from properly re-using |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
(*Lexer).Resetinparser/lexer/lexer.goso reusable*Lexerinstances returned bylexer.New()actually reset all per-input scan state.Root Cause
lexer.New()returns a reusable*Lexer, but(*Lexer).Reset(source)only resetl.source,l.tokens, andl.state:l.startandl.endretained the byte and rune offsets of the previous input, so subsequentResetcalls either emittedEOFimmediately (when the new source was not longer than the oldl.end.byte) or skipped the prefix and produced shiftedfile.Locationspans.l.errremained non-nil after a lexing error, causing subsequentReset(validSource)+Next()calls to fail with the previous error bound to the new source.l.eofremainedtruefrom the previous input.Fix
Reset
l.err = nil,l.start = struct{ byte, rune int }{},l.end = struct{ byte, rune int }{}, andl.eof = falsein(*Lexer).Reset, while preserving caller-configured options such asDisableIfOperator.Verification
TestLexer_Resetinparser/lexer/lexer_test.gocovering reuse across shorter/longer valid sources (with exact token values andfile.Locationspans), recovery after a failed lex, and preservation ofDisableIfOperator; verified all 3 subtests fail onmasterand pass with the fix.gofmt -l parser/lexer/lexer.go parser/lexer/lexer_test.go(clean)go vet ./...(clean)go test -v -race -count=1 -run TestLexer_Reset ./parser/lexer(pass)go test -race -count=1 ./...(pass)go test -tags=expr_debug -run=TestDebugger -v ./vm(pass)