Commit e4db08d2 authored by Robert Griesemer's avatar Robert Griesemer

fix scanner initialization, add test

R=r
DELTA=27  (25 added, 0 deleted, 2 changed)
OCL=26798
CL=26798
parent eeddc8e7
......@@ -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();
}
......
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment