1
0
mirror of https://github.com/golang/go synced 2024-11-22 21:30:02 -07:00

fix scanner initialization, add test

R=r
DELTA=27  (25 added, 0 deleted, 2 changed)
OCL=26798
CL=26798
This commit is contained in:
Robert Griesemer 2009-03-26 17:40:51 -07:00
parent eeddc8e73b
commit e4db08d26d
2 changed files with 27 additions and 2 deletions

View File

@ -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();
}

View File

@ -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);
}
}