1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:44:39 -07:00

go/parser: better error messages for missing commas

Fixes #3008.

R=rsc
CC=golang-dev
https://golang.org/cl/5660046
This commit is contained in:
Robert Griesemer 2012-02-13 19:48:27 -08:00
parent 982e6c44a0
commit e3f11b3f3c

View File

@ -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 {
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 {
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 {
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}
} }