Commit d2130573 authored by Robert Griesemer's avatar Robert Griesemer

go/scanner: don't return previous comment as literal value if none is expected

Fixes #10213.

Change-Id: Ia587dd51eea702058da926717ad305792c9fc42b
Reviewed-on: https://go-review.googlesource.com/10081Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 92bdbb8a
......@@ -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)
}
......
......@@ -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()
......
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