diff --git a/src/lib/go/scanner.go b/src/lib/go/scanner.go index 41ce78218b0..8b51a3e4980 100644 --- a/src/lib/go/scanner.go +++ b/src/lib/go/scanner.go @@ -76,10 +76,12 @@ func (S *Scanner) next() { // white space and ignored. // func (S *Scanner) Init(src []byte, err ErrorHandler, scan_comments bool) { + // Explicitly initialize all fields since a scanner may be reused. S.src = src; S.err = err; S.scan_comments = scan_comments; - S.pos.Line = 1; + S.pos = token.Position{0, 1, 0}; + S.offset = 0; S.next(); } diff --git a/src/lib/go/scanner_test.go b/src/lib/go/scanner_test.go index 2042b191f82..bbe0b7602d9 100644 --- a/src/lib/go/scanner_test.go +++ b/src/lib/go/scanner_test.go @@ -176,7 +176,8 @@ func NewlineCount(s string) int { } -func Test(t *testing.T) { +// Verify that calling Scan() provides the correct results. +func TestScan(t *testing.T) { // make source var src string; for i, e := range tokens { @@ -223,3 +224,25 @@ func Test(t *testing.T) { } ); } + + +// Verify that initializing the same scanner more then once works correctly. +func TestInit(t *testing.T) { + var s scanner.Scanner; + + // 1st init + s.Init(io.StringBytes("if true { }"), &TestErrorHandler{t}, false); + s.Scan(); // if + s.Scan(); // true + pos, tok, lit := s.Scan(); // { + if tok != token.LBRACE { + t.Errorf("bad token: got %s, expected %s", tok.String(), token.LBRACE); + } + + // 2nd init + s.Init(io.StringBytes("go true { ]"), &TestErrorHandler{t}, false); + pos, tok, lit = s.Scan(); // go + if tok != token.GO { + t.Errorf("bad token: got %s, expected %s", tok.String(), token.GO); + } +}