]> Cypherpunks repositories - gostls13.git/commitdiff
- allow parenthesized [...]T types as in: ([...]int){}
authorRobert Griesemer <gri@golang.org>
Fri, 2 Oct 2009 17:03:25 +0000 (10:03 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 2 Oct 2009 17:03:25 +0000 (10:03 -0700)
- added extra tests

R=rsc
DELTA=55  (37 added, 0 deleted, 18 changed)
OCL=35250
CL=35276

src/pkg/go/parser/parser.go
src/pkg/go/printer/testdata/expressions.go
src/pkg/go/printer/testdata/expressions.golden

index 034ee80994861fe4bdb6482bda8d3f522263df75..723f40f0110338706b8bd580794ad2961ec295c2 100644 (file)
@@ -439,7 +439,7 @@ func (p *parser) parseFieldDecl() *ast.Field {
        // a list of identifiers looks like a list of type names
        list := vector.New(0);
        for {
-               // TODO do not allow ()'s here
+               // TODO(gri): do not allow ()'s here
                list.Push(p.parseType());
                if p.tok == token.COMMA {
                        p.next();
@@ -465,7 +465,7 @@ func (p *parser) parseFieldDecl() *ast.Field {
        } else {
                // Type (anonymous field)
                if list.Len() == 1 {
-                       // TODO check that this looks like a type
+                       // TODO(gri): check that this looks like a type
                        typ = list.At(0).(ast.Expr);
                } else {
                        p.errorExpected(p.pos, "anonymous field");
@@ -554,7 +554,7 @@ func (p *parser) parseParameterDecl(ellipsisOk bool) (*vector.Vector, ast.Expr)
        // a list of identifiers looks like a list of type names
        list := vector.New(0);
        for {
-               // TODO do not allow ()'s here
+               // TODO(gri): do not allow ()'s here
                list.Push(p.parseParameterType(ellipsisOk));
                if p.tok == token.COMMA {
                        p.next();
@@ -1050,14 +1050,14 @@ func (p *parser) parseCompositeLit(typ ast.Expr) ast.Expr {
 }
 
 
-// TODO Consider different approach to checking syntax after parsing:
-//      Provide a arguments (set of flags) to parsing functions
-//      restricting what they are supposed to accept depending
-//      on context.
+// TODO(gri): Consider different approach to checking syntax after parsing:
+//            Provide a arguments (set of flags) to parsing functions
+//            restricting what they are supposed to accept depending
+//            on context.
 
 // checkExpr checks that x is an expression (and not a type).
 func (p *parser) checkExpr(x ast.Expr) ast.Expr {
-       // TODO should provide predicate in AST nodes
+       // TODO(gri): should provide predicate in AST nodes
        switch t := x.(type) {
        case *ast.BadExpr:
        case *ast.Ident:
@@ -1094,11 +1094,11 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr {
 
 // isTypeName returns true iff x is type name.
 func isTypeName(x ast.Expr) bool {
-       // TODO should provide predicate in AST nodes
+       // TODO(gri): should provide predicate in AST nodes
        switch t := x.(type) {
        case *ast.BadExpr:
        case *ast.Ident:
-       case *ast.ParenExpr: return isTypeName(t.X);  // TODO should (TypeName) be illegal?
+       case *ast.ParenExpr: return isTypeName(t.X);  // TODO(gri): should (TypeName) be illegal?
        case *ast.SelectorExpr: return isTypeName(t.X);
        default: return false;  // all other nodes are not type names
        }
@@ -1108,7 +1108,7 @@ func isTypeName(x ast.Expr) bool {
 
 // isCompositeLitType returns true iff x is a legal composite literal type.
 func isCompositeLitType(x ast.Expr) bool {
-       // TODO should provide predicate in AST nodes
+       // TODO(gri): should provide predicate in AST nodes
        switch t := x.(type) {
        case *ast.BadExpr:
        case *ast.Ident:
@@ -1127,7 +1127,7 @@ func isCompositeLitType(x ast.Expr) bool {
 // (and not a raw type such as [...]T).
 //
 func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
-       // TODO should provide predicate in AST nodes
+       // TODO(gri): should provide predicate in AST nodes
        switch t := x.(type) {
        case *ast.UnaryExpr:
                if t.Op == token.RANGE {
@@ -1169,7 +1169,7 @@ L:        for {
                }
        }
 
-       return p.checkExprOrType(x);
+       return x;
 }
 
 
@@ -1216,6 +1216,8 @@ func (p *parser) parseBinaryExpr(prec1 int) ast.Expr {
 }
 
 
+// TODO(gri): parseExpr may return a type or even a raw type ([..]int) -
+//            should reject when a type/raw type is obviously not allowed
 func (p *parser) parseExpr() ast.Expr {
        if p.trace {
                defer un(trace(p, "Expression"));
@@ -1523,7 +1525,7 @@ func (p *parser) parseSwitchStmt() ast.Stmt {
        }
 
        // type switch
-       // TODO do all the checks!
+       // TODO(gri): do all the checks!
        lbrace := p.expect(token.LBRACE);
        cases := vector.New(0);
        for p.tok == token.CASE || p.tok == token.DEFAULT {
index 3d1b4f295132178ffc338669bbfc08b812338911..c5e309b8bae64badbed881f43944d11a39c330e7 100644 (file)
@@ -13,9 +13,11 @@ var (
        longIdentifier1, longIdentifier2, longIdentifier3 int;
        t0, t1, t2 T;
        s string;
+       p *int;
 )
 
-func main() {
+
+func _() {
        // no spaces around simple or parenthesized expressions
        _ = a+b;
        _ = a+b+c;
@@ -36,7 +38,7 @@ func main() {
        // spaces around expressions of different precedence or expressions containing spaces
        _ = a + -b;
        _ = a - ^b;
-       _ = a / *b;
+       _ = a / *p;
        _ = a + b*c;
        _ = 1 + b*c;
        _ = a + 2*c;
@@ -72,3 +74,20 @@ func main() {
        _ = (a+b+c)*2;
        _ = a - b + c - d + (a+b+c) + d&e;
 }
+
+
+func _() {
+       _ = T{};
+       _ = struct{}{};
+       _ = [10]T{};
+       _ = [...]T{};
+       _ = []T{};
+       _ = map[int]T{};
+
+       _ = (T){};
+       _ = (struct{}){};
+       _ = ([10]T){};
+       _ = ([...]T){};
+       _ = ([]T){};
+       _ = (map[int]T){};
+}
index 1bd3c6e3c325b1d90260116cc15090e924a3ceec..f772953fce89e32e735505406cd2ebf01872648c 100644 (file)
@@ -13,9 +13,10 @@ var (
        longIdentifier1, longIdentifier2, longIdentifier3       int;
        t0, t1, t2                                                                                      T;
        s                                                                                                       string;
+       p                                                                                                       *int;
 )
 
-func main() {
+func _() {
        // no spaces around simple or parenthesized expressions
        _ = a+b;
        _ = a+b+c;
@@ -36,7 +37,7 @@ func main() {
        // spaces around expressions of different precedence or expressions containing spaces
        _ = a + -b;
        _ = a - ^b;
-       _ = a / *b;
+       _ = a / *p;
        _ = a + b*c;
        _ = 1 + b*c;
        _ = a + 2*c;
@@ -72,3 +73,18 @@ func main() {
        _ = (a+b+c)*2;
        _ = a - b + c - d + (a+b+c) + d&e;
 }
+
+func _() {
+       _ = T{};
+       _ = struct{}{};
+       _ = [10]T{};
+       _ = [...]T{};
+       _ = []T{};
+       _ = map[int]T{};
+       _ = (T){};
+       _ = (struct{}){};
+       _ = ([10]T){};
+       _ = ([...]T){};
+       _ = ([]T){};
+       _ = (map[int]T){};
+}