[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] arrays: Allow trailing comma
From: |
Eric Blake |
Subject: |
[PATCH] arrays: Allow trailing comma |
Date: |
Mon, 24 Feb 2020 16:51:59 -0600 |
If I want to write a program that generates Poke output, it's easier
to generate [1,2,] than it is to generate [1,2] having to special-case
the last element. After all, C99 allows trailing commas in enums to
correct a shortcoming in C89's syntax.
* src/pkl-tab.y (array): Allow trailing comma in arrays.
* doc/poke.texi (Array Literals): Document it.
* testsuite/poke.pkl/arrays-9.pk: New file, to test it.
---
ChangeLog | 6 ++++++
doc/poke.texi | 5 +++--
src/pkl-tab.y | 8 ++++++++
testsuite/poke.pkl/arrays-9.pk | 4 ++++
4 files changed, 21 insertions(+), 2 deletions(-)
create mode 100644 testsuite/poke.pkl/arrays-9.pk
diff --git a/ChangeLog b/ChangeLog
index 63db3969..64854bfb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-02-24 Eric Blake <address@hidden>
+
+ * src/pkl-tab.y (array): Allow trailing comma in arrays.
+ * doc/poke.texi (Array Literals): Document it.
+ * testsuite/poke.pkl/arrays-9.pk: New file, to test it.
+
2020-02-24 Eric Blake <address@hidden>
* .gitignore: Ignore files created during 'make check'.
diff --git a/doc/poke.texi b/doc/poke.texi
index 933b3924..f4866d33 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -2047,7 +2047,8 @@ Where @var{exp} is an arbitrary expression.
For example, @code{[1,2,3]} constructs an array of three signed 32-bit
integers. Likewise, @code{['a','b','c']} constructs an array of three
-unsigned 8-bit integers (characters).
+unsigned 8-bit integers (characters). For convenience, a trailing
+comma is accepted but not required.
The type of the array literal is inferred from the type of its
elements. Accordingly, all the elements in an array literal must be
@@ -2068,7 +2069,7 @@ This is how a @code{3x3} matrix could be constructed
using an array of
arrays:
@example
-[[1,2,3],[4,5,6],[7,8,9]]
+[[1,2,3],[4,5,6],[7,8,9],]
@end example
It is possible to refer to specific elements when constructing array
diff --git a/src/pkl-tab.y b/src/pkl-tab.y
index f6f48769..52fa7be9 100644
--- a/src/pkl-tab.y
+++ b/src/pkl-tab.y
@@ -975,6 +975,14 @@ array:
$2);
PKL_AST_LOC ($$) = @$;
}
+ | '[' array_initializer_list ',' ']'
+ {
+ $$ = pkl_ast_make_array (pkl_parser->ast,
+ 0 /* nelem */,
+ 0 /* ninitializer */,
+ $2);
+ PKL_AST_LOC ($$) = @$;
+ }
;
array_initializer_list:
diff --git a/testsuite/poke.pkl/arrays-9.pk b/testsuite/poke.pkl/arrays-9.pk
new file mode 100644
index 00000000..dbc90d24
--- /dev/null
+++ b/testsuite/poke.pkl/arrays-9.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { [1,2,] } } */
+/* { dg-output "\\\[1,2\\\]" } */
--
2.24.1
- [PATCH] arrays: Allow trailing comma,
Eric Blake <=