};
// A CompositeLit node represents a composite literal.
- // A pair (x : y) in a CompositeLit is represented by
- // a binary expression with the Colon operator.
- // TODO decide if better to use a Pair node instead.
//
CompositeLit struct {
Type Expr; // literal type
};
// A BinaryExpr node represents a binary expression.
- // A pair (x : y) in a CompositeLit is represented by
- // a binary expression with the Colon operator.
- // TODO decide if better to use a Pair node instead.
//
BinaryExpr struct {
X Expr; // left operand
Op token.Token; // operator
Y Expr; // right operand
};
+
+ // A KeyValueExpr node represents (key : value) pairs
+ // in composite literals.
+ //
+ KeyValueExpr struct {
+ Key Expr;
+ Colon token.Position; // position of ":"
+ Value Expr;
+ };
)
func (x *TypeAssertExpr) Pos() token.Position { return x.X.Pos(); }
func (x *CallExpr) Pos() token.Position { return x.Fun.Pos(); }
func (x *BinaryExpr) Pos() token.Position { return x.X.Pos(); }
+func (x *KeyValueExpr) Pos() token.Position { return x.Key.Pos(); }
// All expression/type nodes implement a Visit method which takes
DoStarExpr(x *StarExpr);
DoUnaryExpr(x *UnaryExpr);
DoBinaryExpr(x *BinaryExpr);
+ DoKeyValueExpr(x *KeyValueExpr);
// Type expressions
DoEllipsis(x *Ellipsis);
func (x *StarExpr) Visit(v ExprVisitor) { v.DoStarExpr(x); }
func (x *UnaryExpr) Visit(v ExprVisitor) { v.DoUnaryExpr(x); }
func (x *BinaryExpr) Visit(v ExprVisitor) { v.DoBinaryExpr(x); }
+func (x *KeyValueExpr) Visit(v ExprVisitor) { v.DoKeyValueExpr(x); }
func (x *ArrayType) Visit(v ExprVisitor) { v.DoArrayType(x); }
func (x *SliceType) Visit(v ExprVisitor) { v.DoSliceType(x); }
// A set of constants for precedence-based expression parsing.
// Non-operators have lowest precedence, followed by operators
-// starting with precedence 0 up to unary operators. The highest
+// starting with precedence 1 up to unary operators. The highest
// precedence corresponds serves as "catch-all" precedence for
// selector, indexing, and other operator and delimiter tokens.
//
const (
- LowestPrec = -1; // non-operators
+ LowestPrec = 0; // non-operators
UnaryPrec = 7;
HighestPrec = 8;
)
-// Precedence returns the syntax precedence of the operator
-// token op or LowestPrecedence if op is not an operator.
+// Precedence returns the operator precedence of the binary
+// operator op. If op is not a binary operator, the result
+// is LowestPrecedence.
//
func (op Token) Precedence() int {
switch op {
- case COLON:
- return 0;
case LOR:
return 1;
case LAND: