]> Cypherpunks repositories - gostls13.git/commitdiff
go/scanner: don't return previous comment as literal value if none is expected
authorRobert Griesemer <gri@golang.org>
Thu, 14 May 2015 18:20:08 +0000 (11:20 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 14 May 2015 18:39:04 +0000 (18:39 +0000)
Fixes #10213.

Change-Id: Ia587dd51eea702058da926717ad305792c9fc42b
Reviewed-on: https://go-review.googlesource.com/10081
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/scanner/scanner.go
src/go/scanner/scanner_test.go

index cec82ea10efa52bde92a290062f57cfeb0418974..e9476c4dee452eb3aece27e1da60a4b4c6ee471b 100644 (file)
@@ -706,13 +706,14 @@ scanAgain:
                                        s.insertSemi = false // newline consumed
                                        return pos, token.SEMICOLON, "\n"
                                }
-                               lit = s.scanComment()
+                               comment := s.scanComment()
                                if s.mode&ScanComments == 0 {
                                        // skip comment
                                        s.insertSemi = false // newline consumed
                                        goto scanAgain
                                }
                                tok = token.COMMENT
+                               lit = comment
                        } else {
                                tok = s.switch2(token.QUO, token.QUO_ASSIGN)
                        }
index fc450d8a6eb0db6dd854f116c3800d5ccfcc7ffa..0d21905166c286eba4d913e6ba7181fac3827598 100644 (file)
@@ -734,6 +734,41 @@ func TestScanErrors(t *testing.T) {
        }
 }
 
+// Verify that no comments show up as literal values when skipping comments.
+func TestIssue10213(t *testing.T) {
+       var src = `
+               var (
+                       A = 1 // foo
+               )
+
+               var (
+                       B = 2
+                       // foo
+               )
+
+               var C = 3 // foo
+
+               var D = 4
+               // foo
+
+               func anycode() {
+               // foo
+               }
+       `
+       var s Scanner
+       s.Init(fset.AddFile("", fset.Base(), len(src)), []byte(src), nil, 0)
+       for {
+               pos, tok, lit := s.Scan()
+               class := tokenclass(tok)
+               if lit != "" && class != keyword && class != literal && tok != token.SEMICOLON {
+                       t.Errorf("%s: tok = %s, lit = %q", fset.Position(pos), tok, lit)
+               }
+               if tok <= token.EOF {
+                       break
+               }
+       }
+}
+
 func BenchmarkScan(b *testing.B) {
        b.StopTimer()
        fset := token.NewFileSet()