From 6f418c17a92a3f2d6e24e63752f19e1c13c13d2f Mon Sep 17 00:00:00 2001 From: xgbtxy <115146404+xgbtxy@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:44:09 +0800 Subject: [PATCH] parser/lexer: reset offsets, error, and EOF flag in Lexer.Reset 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. --- parser/lexer/lexer.go | 4 ++ parser/lexer/lexer_test.go | 91 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/parser/lexer/lexer.go b/parser/lexer/lexer.go index 0e75942d0..22c22ff47 100644 --- a/parser/lexer/lexer.go +++ b/parser/lexer/lexer.go @@ -55,6 +55,10 @@ func (l *Lexer) Reset(source file.Source) { l.source = source l.tokens.Reset() l.state = root + l.err = nil + l.start = struct{ byte, rune int }{} + l.end = struct{ byte, rune int }{} + l.eof = false } func (l *Lexer) Next() (Token, error) { diff --git a/parser/lexer/lexer_test.go b/parser/lexer/lexer_test.go index 6212d066f..a50f303db 100644 --- a/parser/lexer/lexer_test.go +++ b/parser/lexer/lexer_test.go @@ -2,6 +2,7 @@ package lexer_test import ( "fmt" + "io" "strings" "testing" @@ -456,3 +457,93 @@ func TestLex_error(t *testing.T) { assert.Equal(t, input[1], err.Error(), input[0]) } } + +func lexAll(l *Lexer, input string) ([]Token, error) { + l.Reset(file.NewSource(input)) + var tokens []Token + for { + tok, err := l.Next() + if err == io.EOF { + return tokens, nil + } + if err != nil { + return nil, err + } + tokens = append(tokens, tok) + } +} + +func TestLexer_Reset(t *testing.T) { + t.Run("reuses lexer across shorter and longer valid sources", func(t *testing.T) { + l := New() + + first, err := lexAll(l, "foo + bar") + require.NoError(t, err) + require.Equal(t, []Token{ + {Location: file.Location{From: 0, To: 3}, Kind: Identifier, Value: "foo"}, + {Location: file.Location{From: 4, To: 5}, Kind: Operator, Value: "+"}, + {Location: file.Location{From: 6, To: 9}, Kind: Identifier, Value: "bar"}, + {Location: file.Location{From: 8, To: 9}, Kind: EOF}, + }, first) + + shorter, err := lexAll(l, "1 + 2") + require.NoError(t, err) + require.Equal(t, []Token{ + {Location: file.Location{From: 0, To: 1}, Kind: Number, Value: "1"}, + {Location: file.Location{From: 2, To: 3}, Kind: Operator, Value: "+"}, + {Location: file.Location{From: 4, To: 5}, Kind: Number, Value: "2"}, + {Location: file.Location{From: 4, To: 5}, Kind: EOF}, + }, shorter) + + longer, err := lexAll(l, "alpha * beta + gamma") + require.NoError(t, err) + require.Equal(t, []Token{ + {Location: file.Location{From: 0, To: 5}, Kind: Identifier, Value: "alpha"}, + {Location: file.Location{From: 6, To: 7}, Kind: Operator, Value: "*"}, + {Location: file.Location{From: 8, To: 12}, Kind: Identifier, Value: "beta"}, + {Location: file.Location{From: 13, To: 14}, Kind: Operator, Value: "+"}, + {Location: file.Location{From: 15, To: 20}, Kind: Identifier, Value: "gamma"}, + {Location: file.Location{From: 19, To: 20}, Kind: EOF}, + }, longer) + }) + + t.Run("clears error state after failed lex", func(t *testing.T) { + l := New() + + _, err := lexAll(l, `"unterminated`) + require.Error(t, err) + + tokens, err := lexAll(l, "x == 1") + require.NoError(t, err) + require.Equal(t, []Token{ + {Location: file.Location{From: 0, To: 1}, Kind: Identifier, Value: "x"}, + {Location: file.Location{From: 2, To: 4}, Kind: Operator, Value: "=="}, + {Location: file.Location{From: 5, To: 6}, Kind: Number, Value: "1"}, + {Location: file.Location{From: 5, To: 6}, Kind: EOF}, + }, tokens) + }) + + t.Run("preserves DisableIfOperator option", func(t *testing.T) { + l := New() + l.DisableIfOperator = true + + first, err := lexAll(l, "if(a, b, c)") + require.NoError(t, err) + require.Equal(t, Identifier, first[0].Kind) + require.Equal(t, "if", first[0].Value) + + second, err := lexAll(l, "if(x, y, z)") + require.NoError(t, err) + require.Equal(t, []Token{ + {Location: file.Location{From: 0, To: 2}, Kind: Identifier, Value: "if"}, + {Location: file.Location{From: 2, To: 3}, Kind: Bracket, Value: "("}, + {Location: file.Location{From: 3, To: 4}, Kind: Identifier, Value: "x"}, + {Location: file.Location{From: 4, To: 5}, Kind: Operator, Value: ","}, + {Location: file.Location{From: 6, To: 7}, Kind: Identifier, Value: "y"}, + {Location: file.Location{From: 7, To: 8}, Kind: Operator, Value: ","}, + {Location: file.Location{From: 9, To: 10}, Kind: Identifier, Value: "z"}, + {Location: file.Location{From: 10, To: 11}, Kind: Bracket, Value: ")"}, + {Location: file.Location{From: 10, To: 11}, Kind: EOF}, + }, second) + }) +}