Description
Currently list literal element expressions are either if ...
or for ...
or plain expressions. We do not allow parentheses around an if ...
element, so the following:
var l = [ if (cond1) (if (cond2) 0) else if (cond3) 1 ];
is not allowed. That is inconvenient because we also do not have a "no element" element that is briefer than ...[]
, so the way to write the above logic today is:
var l = [ if (cond1) if (cond2) 0 else ...[] else if (cond3) 1 ];
We should consider allowing parentheses around all element expressions. The only place it actually matters is delimiting an if
with no else, but that is an annoying surprise in the current grammar.
The change would be adding the following elements
production:
<element> ::= `(' <element> `)' | ...
which is ambiguous with parenthesized expressions, but since any ambiguous element/expression means the same thing whether it's an element or an expression, it doesn't matter how we resolve the ambiguity.
Optionally, also allow multiple values, comma separated, inside an "elements parenthesis":
var l = [if (cond1) (value1, value2, value3)];
which is currently not something we can write shorter than:
var l = [if (cond1) ...[value1, value2, value3]];
The spread-literal does work, so it's not strictly necessary, but could be convenient. Effectively we would instead change the grammar to:
<element> ::= `(' <elements> `)' | ...
(where the <elements>
production allows trailing commas).
That grammar is no more ambiguous than the one above, any presence of a comma will definitely disambiguate towards the element production.
(Edit: Added multiple-values option and grammar).