// 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();
}
}
-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 {
}
);
}
+
+
+// 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);
+ }
+}