]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: handle last line comments
authorHiroshi Ioka <hirochachacha@gmail.com>
Sat, 10 Jun 2017 01:36:28 +0000 (10:36 +0900)
committerRobert Griesemer <gri@golang.org>
Mon, 12 Jun 2017 19:07:38 +0000 (19:07 +0000)
Fixes #20636

Change-Id: Icea0012fecb73944c95f6037922505c63b57b245
Reviewed-on: https://go-review.googlesource.com/45295
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/go/parser/parser.go
src/go/parser/parser_test.go

index 40c4a3e58d9cd0dce6b2ab781f6c424a70caa74b..1b4309b5da739a870056299056123719d8efdeee 100644 (file)
@@ -327,7 +327,7 @@ func (p *parser) next() {
                        // The comment is on same line as the previous token; it
                        // cannot be a lead comment but may be a line comment.
                        comment, endline = p.consumeCommentGroup(0)
-                       if p.file.Line(p.pos) != endline {
+                       if p.file.Line(p.pos) != endline || p.tok == token.EOF {
                                // The next token is on a different line, thus
                                // the last comment group is a line comment.
                                p.lineComment = comment
index c7bb36d789d551543d02cf42a81edeaa5110cbd1..fb35a88ba1aa5565614762df24193cb237584dff 100644 (file)
@@ -531,3 +531,18 @@ func TestIncompleteSelection(t *testing.T) {
                }
        }
 }
+
+func TestLastLineComment(t *testing.T) {
+       const src = `package main
+type x int // comment
+`
+       fset := token.NewFileSet()
+       f, err := ParseFile(fset, "", src, ParseComments)
+       if err != nil {
+               t.Fatal(err)
+       }
+       comment := f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.TypeSpec).Comment.List[0].Text
+       if comment != "// comment" {
+               t.Errorf("got %q, want %q", comment, "// comment")
+       }
+}