Skip to content

parser/lexer: reset offsets, error, and EOF flag in Lexer.Reset - #1034

Open
xgbtxy wants to merge 1 commit into
expr-lang:masterfrom
xgbtxy:fix/lexer-reset-state
Open

xgbtxy wants to merge 1 commit into
expr-lang:masterfrom
xgbtxy:fix/lexer-reset-state

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 27, 2026

Copy link
Copy Markdown

Summary

Fixes (*Lexer).Reset in parser/lexer/lexer.go so reusable *Lexer instances returned by lexer.New() actually reset all per-input scan state.

Root Cause

lexer.New() returns a reusable *Lexer, but (*Lexer).Reset(source) only reset l.source, l.tokens, and l.state:

  • l.start and l.end retained the byte and rune offsets of the previous input, so subsequent Reset calls either emitted EOF immediately (when the new source was not longer than the old l.end.byte) or skipped the prefix and produced shifted file.Location spans.
  • l.err remained non-nil after a lexing error, causing subsequent Reset(validSource) + Next() calls to fail with the previous error bound to the new source.
  • l.eof remained true from the previous input.

Fix

Reset l.err = nil, l.start = struct{ byte, rune int }{}, l.end = struct{ byte, rune int }{}, and l.eof = false in (*Lexer).Reset, while preserving caller-configured options such as DisableIfOperator.

Verification

  • Added TestLexer_Reset in parser/lexer/lexer_test.go covering reuse across shorter/longer valid sources (with exact token values and file.Location spans), recovery after a failed lex, and preservation of DisableIfOperator; verified all 3 subtests fail on master and pass with the fix.
  • Ran:
    • 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)

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.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 06:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@tkw1536

tkw1536 commented Dec 1, 2026

Copy link
Copy Markdown

I encountered the same issue and this is preventing me from properly re-using *Parser.Parse.

This branch has not been deployed

No deployments
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.

3 participants