]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: better error message for missing ',' in lists
authorRobert Griesemer <gri@golang.org>
Thu, 14 May 2015 23:47:53 +0000 (16:47 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 15 May 2015 17:58:56 +0000 (17:58 +0000)
Fixes #8940.

Change-Id: Ie9e5149983518ba8d56ddd82ac8f4cde6b644167
Reviewed-on: https://go-review.googlesource.com/10089
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/parser/parser.go
src/go/parser/short_test.go

index 0095d7facf3f2f5e6821fdad1ec2dfb314cd7c89..fb6ca76a77515604b9594217dbe8a1cb1dc3bbf6 100644 (file)
@@ -412,14 +412,17 @@ func (p *parser) expectSemi() {
        }
 }
 
-func (p *parser) atComma(context string) bool {
+func (p *parser) atComma(context string, follow token.Token) bool {
        if p.tok == token.COMMA {
                return true
        }
-       if p.tok == token.SEMICOLON && p.lit == "\n" {
-               p.error(p.pos, "missing ',' before newline in "+context)
-               return true // "insert" the comma and continue
-
+       if p.tok != follow {
+               msg := "missing ','"
+               if p.tok == token.SEMICOLON && p.lit == "\n" {
+                       msg += " before newline"
+               }
+               p.error(p.pos, msg+" in "+context)
+               return true // "insert" comma and continue
        }
        return false
 }
@@ -825,7 +828,7 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
                // parameter or result variable is the function body.
                p.declare(field, nil, scope, ast.Var, idents...)
                p.resolve(typ)
-               if !p.atComma("parameter list") {
+               if !p.atComma("parameter list", token.RPAREN) {
                        return
                }
                p.next()
@@ -838,7 +841,7 @@ func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params [
                        // parameter or result variable is the function body.
                        p.declare(field, nil, scope, ast.Var, idents...)
                        p.resolve(typ)
-                       if !p.atComma("parameter list") {
+                       if !p.atComma("parameter list", token.RPAREN) {
                                break
                        }
                        p.next()
@@ -1248,7 +1251,7 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
                        ellipsis = p.pos
                        p.next()
                }
-               if !p.atComma("argument list") {
+               if !p.atComma("argument list", token.RPAREN) {
                        break
                }
                p.next()
@@ -1323,7 +1326,7 @@ func (p *parser) parseElementList() (list []ast.Expr) {
 
        for p.tok != token.RBRACE && p.tok != token.EOF {
                list = append(list, p.parseElement())
-               if !p.atComma("composite literal") {
+               if !p.atComma("composite literal", token.RBRACE) {
                        break
                }
                p.next()
index 970ef2d3fae5679c30e59bf21afbce1cf71d3b4c..ef2ffadbd981c09400d5bd4c2e92fcdbf4164b18 100644 (file)
@@ -99,8 +99,9 @@ var invalids = []string{
        `package p; func f() { for i /* ERROR "boolean or range expression" */ , x := []string {} }`,
        `package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`,
        `package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`,
-       `package p; func f() { go func() { func() { f(x func /* ERROR "expected '\)'" */ (){}) } } }`,
-       `package p; func f() (a b string /* ERROR "expected '\)'" */ , ok bool)`,         // issue 8656
+       `package p; func f() { go func() { func() { f(x func /* ERROR "missing ','" */ (){}) } } }`,
+       `package p; func f(x func(), u v func /* ERROR "missing ','" */ ()){}`,
+       `package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`,           // issue 8656
        `package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`, // issue 9639
        `package p; const x /* ERROR "missing constant value" */ ;`,                      // issue 9639
        `package p; const x /* ERROR "missing constant value" */ int;`,                   // issue 9639