File tree 2 files changed +33
-9
lines changed
2 files changed +33
-9
lines changed Original file line number Diff line number Diff line change @@ -314,7 +314,9 @@ func (s *Scanner) Next() rune {
314
314
s .tokPos = - 1 // don't collect token text
315
315
s .Line = 0 // invalidate token position
316
316
ch := s .Peek ()
317
- s .ch = s .next ()
317
+ if ch != EOF {
318
+ s .ch = s .next ()
319
+ }
318
320
return ch
319
321
}
320
322
@@ -597,6 +599,8 @@ redo:
597
599
}
598
600
default :
599
601
switch ch {
602
+ case EOF :
603
+ break
600
604
case '"' :
601
605
if s .Mode & ScanStrings != 0 {
602
606
s .scanString ('"' )
Original file line number Diff line number Diff line change @@ -619,29 +619,49 @@ func TestPos(t *testing.T) {
619
619
620
620
type countReader int
621
621
622
- func (c * countReader ) Read ([]byte ) (int , error ) {
623
- * c ++
624
-
622
+ func (r * countReader ) Read ([]byte ) (int , error ) {
623
+ * r ++
625
624
return 0 , io .EOF
626
625
}
627
626
628
- func TestPeekEOFHandling (t * testing.T ) {
627
+ func TestNextEOFHandling (t * testing.T ) {
629
628
var r countReader
630
629
631
630
// corner case: empty source
632
631
s := new (Scanner ).Init (& r )
633
632
634
633
tok := s .Next ()
635
634
if tok != EOF {
636
- t .Errorf ("EOF not reported" )
635
+ t .Error ("1) EOF not reported" )
636
+ }
637
+
638
+ tok = s .Peek ()
639
+ if tok != EOF {
640
+ t .Error ("2) EOF not reported" )
641
+ }
642
+
643
+ if r != 1 {
644
+ t .Errorf ("scanner called Read %d times, not once" , r )
645
+ }
646
+ }
647
+
648
+ func TestScanEOFHandling (t * testing.T ) {
649
+ var r countReader
650
+
651
+ // corner case: empty source
652
+ s := new (Scanner ).Init (& r )
653
+
654
+ tok := s .Scan ()
655
+ if tok != EOF {
656
+ t .Error ("1) EOF not reported" )
637
657
}
638
658
639
659
tok = s .Peek ()
640
660
if tok != EOF {
641
- t .Errorf ( " EOF not reported" )
661
+ t .Error ( "2) EOF not reported" )
642
662
}
643
663
644
- if r != 2 {
645
- t .Errorf ("scanner called Read %d times, not twice " , r )
664
+ if r != 1 {
665
+ t .Errorf ("scanner called Read %d times, not once " , r )
646
666
}
647
667
}
You can’t perform that action at this time.
0 commit comments