1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:54:42 -07:00

adjustments for relaxed composite literal syntax

R=r
DELTA=41  (0 added, 21 deleted, 20 changed)
OCL=29219
CL=29219
This commit is contained in:
Robert Griesemer 2009-05-21 19:50:25 -07:00
parent f4d3d22a94
commit ad8527c4dc

View File

@ -984,51 +984,30 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
} }
func (p *parser) parseKeyValueExpr() ast.Expr { func (p *parser) parseElement() ast.Expr {
if p.trace { if p.trace {
defer un(trace(p, "KeyValueExpr")); defer un(trace(p, "Element"));
} }
key := p.parseExpression(); x := p.parseExpression();
if p.tok == token.COLON { if p.tok == token.COLON {
colon := p.pos; colon := p.pos;
p.next(); p.next();
value := p.parseExpression(); x = &ast.KeyValueExpr{x, colon, p.parseExpression()};
return &ast.KeyValueExpr{key, colon, value};
} }
return key; return x;
} }
func isPair(x ast.Expr) bool { func (p *parser) parseElementList() []ast.Expr {
tmp, is_pair := x.(*ast.KeyValueExpr);
return is_pair;
}
func (p *parser) parseExpressionOrKeyValueList() []ast.Expr {
if p.trace { if p.trace {
defer un(trace(p, "ExpressionOrKeyValueList")); defer un(trace(p, "ElementList"));
} }
var pairs bool;
list := vector.New(0); list := vector.New(0);
for p.tok != token.RBRACE && p.tok != token.EOF { for p.tok != token.RBRACE && p.tok != token.EOF {
x := p.parseKeyValueExpr(); list.Push(p.parseElement());
if list.Len() == 0 {
pairs = isPair(x);
} else {
// not the first element - check syntax
if pairs != isPair(x) {
p.error_expected(x.Pos(), "all single expressions or all key-value pairs");
}
}
list.Push(x);
if p.tok == token.COMMA { if p.tok == token.COMMA {
p.next(); p.next();
} else { } else {
@ -1054,7 +1033,7 @@ func (p *parser) parseCompositeLit(typ ast.Expr) ast.Expr {
lbrace := p.expect(token.LBRACE); lbrace := p.expect(token.LBRACE);
var elts []ast.Expr; var elts []ast.Expr;
if p.tok != token.RBRACE { if p.tok != token.RBRACE {
elts = p.parseExpressionOrKeyValueList(); elts = p.parseElementList();
} }
rbrace := p.expect(token.RBRACE); rbrace := p.expect(token.RBRACE);
return &ast.CompositeLit{typ, lbrace, elts, rbrace}; return &ast.CompositeLit{typ, lbrace, elts, rbrace};