]> Cypherpunks repositories - gostls13.git/commitdiff
allow trailing comma in braced initialized list
authorRuss Cox <rsc@golang.org>
Wed, 15 Oct 2008 00:10:39 +0000 (17:10 -0700)
committerRuss Cox <rsc@golang.org>
Wed, 15 Oct 2008 00:10:39 +0000 (17:10 -0700)
R=ken
OCL=17141
CL=17143

src/cmd/gc/go.y
test/initcomma.go [new file with mode: 0644]

index b7eba4a47072e883385f45164d9bfcf2e926daad..25df8d3050d4a74ceef983b57984fa946b142df1 100644 (file)
@@ -60,7 +60,7 @@
 %type  <node>          interfacedcl_list_r interfacedcl
 %type  <node>          structdcl_list_r structdcl
 %type  <node>          fnres Afnres Bfnres fnliteral xfndcl fndcl fnbody
-%type  <node>          keyexpr_list keyval_list_r keyval
+%type  <node>          keyexpr_list braced_keyexpr_list keyval_list_r keyval
 
 %type  <type>          typedclname new_type
 %type  <type>          type Atype Btype
@@ -871,7 +871,7 @@ pexpr:
                $$ = nod(OCONV, $3, N);
                $$->type = oldtype($1);
        }
-|      convtype '{' keyexpr_list '}'
+|      convtype '{' braced_keyexpr_list '}'
        {
                // composite literal
                $$ = rev($3);
@@ -1598,7 +1598,32 @@ keyexpr_list:
        {
                $$ = rev($1);
        }
-|      oexpr_list
+|      expr_list
+
+/*
+ * have to spell this out using _r lists to avoid yacc conflict
+ */
+braced_keyexpr_list:
+       {
+               $$ = N;
+       }
+|      keyval_list_r
+       {
+               $$ = rev($1);
+       }
+|      keyval_list_r ','
+       {
+               $$ = rev($1);
+       }
+|      expr_list_r
+       {
+               $$ = rev($1);
+       }
+|      expr_list_r ','
+       {
+               $$ = rev($1);
+       }
+
 
 /*
  * the one compromise of a
diff --git a/test/initcomma.go b/test/initcomma.go
new file mode 100644 (file)
index 0000000..d4bff2a
--- /dev/null
@@ -0,0 +1,17 @@
+// $G $F.go && $L $F.$A && ./$A.out
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+var a = []int { 1, 2, }
+var b = [5]int { }
+var c = []int { 1 }
+
+func main() {
+       if len(a) != 2 { panicln("len a", len(a)) }
+       if len(b) != 5 { panicln("len b", len(b)) }
+       if len(c) != 1 { panicln("len a", len(a)) }
+}