Commit e3f11b3f authored by Robert Griesemer's avatar Robert Griesemer

go/parser: better error messages for missing commas

Fixes #3008.

R=rsc
CC=golang-dev
https://golang.org/cl/5660046
parent 982e6c44
...@@ -335,7 +335,7 @@ func (p *parser) errorExpected(pos token.Pos, msg string) { ...@@ -335,7 +335,7 @@ func (p *parser) errorExpected(pos token.Pos, msg string) {
if pos == p.pos { if pos == p.pos {
// the error happened at the current position; // the error happened at the current position;
// make the error message more specific // make the error message more specific
if p.tok == token.SEMICOLON && p.lit[0] == '\n' { if p.tok == token.SEMICOLON && p.lit == "\n" {
msg += ", found newline" msg += ", found newline"
} else { } else {
msg += ", found '" + p.tok.String() + "'" msg += ", found '" + p.tok.String() + "'"
...@@ -356,6 +356,17 @@ func (p *parser) expect(tok token.Token) token.Pos { ...@@ -356,6 +356,17 @@ func (p *parser) expect(tok token.Token) token.Pos {
return pos return pos
} }
// expectClosing is like expect but provides a better error message
// for the common case of a missing comma before a newline.
//
func (p *parser) expectClosing(tok token.Token, construct string) token.Pos {
if p.tok != tok && p.tok == token.SEMICOLON && p.lit == "\n" {
p.error(p.pos, "missing ',' before newline in "+construct)
p.next()
}
return p.expect(tok)
}
func (p *parser) expectSemi() { func (p *parser) expectSemi() {
if p.tok != token.RPAREN && p.tok != token.RBRACE { if p.tok != token.RPAREN && p.tok != token.RBRACE {
p.expect(token.SEMICOLON) p.expect(token.SEMICOLON)
...@@ -1056,7 +1067,7 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr { ...@@ -1056,7 +1067,7 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
p.next() p.next()
} }
p.exprLev-- p.exprLev--
rparen := p.expect(token.RPAREN) rparen := p.expectClosing(token.RPAREN, "argument list")
return &ast.CallExpr{fun, lparen, list, ellipsis, rparen} return &ast.CallExpr{fun, lparen, list, ellipsis, rparen}
} }
...@@ -1111,7 +1122,7 @@ func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr { ...@@ -1111,7 +1122,7 @@ func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr {
elts = p.parseElementList() elts = p.parseElementList()
} }
p.exprLev-- p.exprLev--
rbrace := p.expect(token.RBRACE) rbrace := p.expectClosing(token.RBRACE, "composite literal")
return &ast.CompositeLit{typ, lbrace, elts, rbrace} return &ast.CompositeLit{typ, lbrace, elts, rbrace}
} }
......
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