From e4db08d26dd1e6fe47e8c7e6b2547b81683b2a15 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 26 Mar 2009 17:40:51 -0700 Subject: [PATCH] fix scanner initialization, add test R=r DELTA=27 (25 added, 0 deleted, 2 changed) OCL=26798 CL=26798 --- src/lib/go/scanner.go | 4 +++- src/lib/go/scanner_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) 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); + } +}