s.tokPos = -1 // don't collect token text
s.Line = 0 // invalidate token position
ch := s.Peek()
- s.ch = s.next()
+ if ch != EOF {
+ s.ch = s.next()
+ }
return ch
}
}
default:
switch ch {
+ case EOF:
+ break
case '"':
if s.Mode&ScanStrings != 0 {
s.scanString('"')
type countReader int
-func (c *countReader) Read([]byte) (int, error) {
- *c++
-
+func (r *countReader) Read([]byte) (int, error) {
+ *r++
return 0, io.EOF
}
-func TestPeekEOFHandling(t *testing.T) {
+func TestNextEOFHandling(t *testing.T) {
var r countReader
// corner case: empty source
tok := s.Next()
if tok != EOF {
- t.Errorf("EOF not reported")
+ t.Error("1) EOF not reported")
+ }
+
+ tok = s.Peek()
+ if tok != EOF {
+ t.Error("2) EOF not reported")
+ }
+
+ if r != 1 {
+ t.Errorf("scanner called Read %d times, not once", r)
+ }
+}
+
+func TestScanEOFHandling(t *testing.T) {
+ var r countReader
+
+ // corner case: empty source
+ s := new(Scanner).Init(&r)
+
+ tok := s.Scan()
+ if tok != EOF {
+ t.Error("1) EOF not reported")
}
tok = s.Peek()
if tok != EOF {
- t.Errorf("EOF not reported")
+ t.Error("2) EOF not reported")
}
- if r != 2 {
- t.Errorf("scanner called Read %d times, not twice", r)
+ if r != 1 {
+ t.Errorf("scanner called Read %d times, not once", r)
}
}