Hyphenated keywords and switch expressions
Alex Buckley
alex.buckley at oracle.com
Sat Jan 12 00:42:47 UTC 2019
Hi Tagir,
On 1/11/2019 5:32 AM, Tagir Valeev wrote:
> On the other hand, from IDE developer point of view, having expression
> and statement with so similar syntax definitely adds a confusion to
> the parsing (and probably to users). E.g. suppose we want to parse a
> fragment which consists of a number of statements, isolated from other
> code:
>
> switch(0) { default -> throw new Exception(); };
>
> In normal context it's two statements: switch-statement followed by an
> empty statement. However inside switch expression rule it's one
> statement: an expression statement containing a switch expression:
>
> int x = switch(0) { default -> switch(0) { default -> throw new
> Exception(); }; };
>
> Normally if we take a textual representation of single statement, it
> could be parsed back into the same single statement, when isolated
> from other code (the same works for expressions). Here this rule is
> violated: the expression statement taken from switch expression rule
> could be reparsed in isolation as two statements.
I'm concerned about any claim of ambiguity in the grammar, though I'm
not sure I'm following you correctly. I agree that your first fragment
is parsed as two statements -- a switch statement and an empty statement
-- but I don't know what you mean about "inside switch expression rule"
for your second fragment. A switch expression is not an expression
statement (JLS 14.8). In your second fragment, the leftmost default
label is followed not by a block or a throw statement but by an
expression (`switch (0) {...}`, a unary expression) and a semicolon.
Yes, the phrase `switch (0) {...}` is parsed as a switch statement in
one context and as a unary expression in another context. Is that the
ambiguity you wished to highlight?
Alex
More information about the amber-spec-experts
mailing list