]> Cypherpunks repositories - gostls13.git/commitdiff
go/scanner: line comments may end in EOF
authorRobert Griesemer <gri@golang.org>
Thu, 4 Nov 2010 20:15:42 +0000 (13:15 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 4 Nov 2010 20:15:42 +0000 (13:15 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/2908041

src/pkg/go/scanner/scanner.go
src/pkg/go/scanner/scanner_test.go

index ab117147059a2119b5ba72cc22e89a5e60d25a2d..64ff127750d82bbbd70b09764626796344113944 100644 (file)
@@ -183,29 +183,25 @@ func (S *Scanner) scanComment() {
 
        if S.ch == '/' {
                //-style comment
-               for S.ch >= 0 {
+               S.next()
+               for S.ch != '\n' && S.ch >= 0 {
                        S.next()
-                       if S.ch == '\n' {
-                               // '\n' is not part of the comment for purposes of scanning
-                               // (the comment ends on the same line where it started)
-                               if col == 1 {
-                                       // comment starts at the beginning of the current line
-                                       S.interpretLineComment(S.src[offs:S.offset])
-                               }
-                               return
-                       }
                }
+               if col == 1 {
+                       // comment starts at the beginning of the current line
+                       S.interpretLineComment(S.src[offs:S.offset])
+               }
+               return
+       }
 
-       } else {
-               /*-style comment */
+       /*-style comment */
+       S.next()
+       for S.ch >= 0 {
+               ch := S.ch
                S.next()
-               for S.ch >= 0 {
-                       ch := S.ch
+               if ch == '*' && S.ch == '/' {
                        S.next()
-                       if ch == '*' && S.ch == '/' {
-                               S.next()
-                               return
-                       }
+                       return
                }
        }
 
index c40753fb03b68f244dc8cd223768231a356d8394..dbec8f71474e5277558f5a47a3b4974f327db18b 100644 (file)
@@ -395,12 +395,14 @@ var lines = []string{
        "var\n",
 
        "foo$//comment\n",
+       "foo$//comment",
        "foo$/*comment*/\n",
        "foo$/*\n*/",
        "foo$/*comment*/    \n",
        "foo$/*\n*/    ",
 
        "foo    $// comment\n",
+       "foo    $// comment",
        "foo    $/*comment*/\n",
        "foo    $/*\n*/",
        "foo    $/*  */ /* \n */ bar$/**/\n",
@@ -410,7 +412,8 @@ var lines = []string{
        "foo    $/*0*/ /*1*/ /*2*/    \n",
        "foo    $/**/ /*-------------*/       /*----\n*/bar       $/*  \n*/baa$\n",
        "foo    $/* an EOF terminates a line */",
-       "foo    $/* an EOF terminates a line *//*",
+       "foo    $/* an EOF terminates a line */ /*",
+       "foo    $/* an EOF terminates a line */ //",
 
        "package main$\n\nfunc main() {\n\tif {\n\t\treturn /* */ }$\n}$\n",
        "package main$",
@@ -626,8 +629,6 @@ var errors = []struct {
        {"`", token.STRING, 0, "string not terminated"},
        {"/**/", token.COMMENT, 0, ""},
        {"/*", token.COMMENT, 0, "comment not terminated"},
-       {"//\n", token.COMMENT, 0, ""},
-       {"//", token.COMMENT, 0, "comment not terminated"},
        {"077", token.INT, 0, ""},
        {"078.", token.FLOAT, 0, ""},
        {"07801234567.", token.FLOAT, 0, ""},