poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/4] pkl, testsuite, doc: Allow trailing comma in struct literals


From: Eric Blake
Subject: [PATCH 3/4] pkl, testsuite, doc: Allow trailing comma in struct literals
Date: Tue, 25 Feb 2020 22:11:51 -0600

* src/pkl-tab.y (opt_comma): New rule.
(array): Use it to simplify trailing comma in arrays.
(struct, expression): Allow trailing comma in struct literals.
* doc/poke.texi (Struct Literals): Document it.
* testsuite/poke.pkl/structs-5.pk: Test it.
* etc/poke.g4 (array, struct): Match the real grammar.
---
 ChangeLog                       |  9 +++++++++
 doc/poke.texi                   |  3 ++-
 etc/poke.g4                     | 11 ++++++++---
 src/pkl-tab.y                   | 19 ++++++++-----------
 testsuite/poke.pkl/structs-5.pk |  4 ++++
 5 files changed, 31 insertions(+), 15 deletions(-)
 create mode 100644 testsuite/poke.pkl/structs-5.pk

diff --git a/ChangeLog b/ChangeLog
index 1323835d..fa8bb600 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-02-25  Eric Blake  <address@hidden>
+
+       * src/pkl-tab.y (opt_comma): New rule.
+       (array): Use it to simplify trailing comma in arrays.
+       (struct, expression): Allow trailing comma in struct literals.
+       * doc/poke.texi (Struct Literals): Document it.
+       * testsuite/poke.pkl/structs-5.pk: Test it.
+       * etc/poke.g4 (array, struct): Match the real grammar.
+
 2020-02-25  Eric Blake  <address@hidden>

        maint: Fix syntax-check
diff --git a/doc/poke.texi b/doc/poke.texi
index 5b729262..6951db15 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -2389,7 +2389,8 @@ Struct Literals

 The contents of the struct literal is a possibly empty set of fields.
 Each field has an optional name @var{field_name} and a value
-@var{exp}.  The expression for the value can be of any type.
+@var{exp}.  The expression for the value can be of any type.  For
+convenience, a trailing comma is accepted but not required.

 @c XXX document struct constructors once they are supported.

diff --git a/etc/poke.g4 b/etc/poke.g4
index 23cd046d..3aebbecb 100644
--- a/etc/poke.g4
+++ b/etc/poke.g4
@@ -113,7 +113,7 @@ expression:
        | expression 'in' expression
        | expression 'as' simple_type_specifier
        | expression 'isa' simple_type_specifier
-    | TYPENAME '{' struct_field_list '}'
+    | TYPENAME '{' struct_field_list opt_comma '}'
     | UNIT
     | expression UNIT
        | struct
@@ -161,8 +161,13 @@ funcall_arg:
         expression
     ;

+opt_comma:
+        /* Empty */
+       | ','
+    ;
+
 struct:
-        'struct' '{' struct_field_list '}'
+        'struct' '{' struct_field_list opt_comma '}'
        ;

 struct_field_list:
@@ -177,7 +182,7 @@ struct_field:
     ;

 array:
-        '[' array_initializer_list ']'
+        '[' array_initializer_list opt_comma ']'
        ;

 array_initializer_list:
diff --git a/src/pkl-tab.y b/src/pkl-tab.y
index d06500d7..cf63b3b7 100644
--- a/src/pkl-tab.y
+++ b/src/pkl-tab.y
@@ -724,7 +724,7 @@ expression:
                                               $1, $3, $5);
                   PKL_AST_LOC ($$) = @$;
                 }
-        | TYPENAME '{' struct_field_list '}'
+        | TYPENAME '{' struct_field_list opt_comma '}'
                {
                   pkl_ast_node type;
                   pkl_ast_node astruct;
@@ -929,8 +929,13 @@ funcall_arg:
                 }
         ;

+opt_comma:
+         %empty
+       | ','
+        ;
+
 struct:
-         STRUCT '{' struct_field_list '}'
+         STRUCT '{' struct_field_list opt_comma '}'
                {
                     $$ = pkl_ast_make_struct (pkl_parser->ast,
                                               0 /* nelem */, $3);
@@ -967,15 +972,7 @@ struct_field:
         ;

 array:
-         '[' array_initializer_list ']'
-               {
-                    $$ = pkl_ast_make_array (pkl_parser->ast,
-                                             0 /* nelem */,
-                                             0 /* ninitializer */,
-                                             $2);
-                    PKL_AST_LOC ($$) = @$;
-                }
-        | '[' array_initializer_list ',' ']'
+         '[' array_initializer_list opt_comma ']'
                {
                     $$ = pkl_ast_make_array (pkl_parser->ast,
                                              0 /* nelem */,
diff --git a/testsuite/poke.pkl/structs-5.pk b/testsuite/poke.pkl/structs-5.pk
new file mode 100644
index 00000000..fdf73424
--- /dev/null
+++ b/testsuite/poke.pkl/structs-5.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { struct { 1, 2, } } } */
+/* { dg-output "struct {1,2}" } */
-- 
2.24.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]