Hyphenated keywords and switch expressions
Tagir Valeev
amaembo at gmail.com
Sun Jan 13 10:53:35 UTC 2019
Hello!
> 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.
Ah, ok, we moved away slightly from the spec draft [1]. I was not
aware, because I haven't wrote parser by myself. The draft says:
SwitchLabeledRule:
SwitchLabeledExpression
SwitchLabeledBlock
SwitchLabeledThrowStatement
SwitchLabeledExpression:
SwitchLabel -> Expression ;
SwitchLabeledBlock:
SwitchLabel -> Block ;
SwitchLabeledThrowStatement:
SwitchLabel -> ThrowStatement ;
(by the way I think that ; after block and throw should not be
present: current implementation does not require it after the block
and throw statement already includes a ; inside it).
Instead we implement it like:
SwitchLabeledRule:
SwitchLabel -> SwitchLabeledRuleStatement
SwitchLabeledRuleStatement:
ExpressionStatement
Block
ThrowStatement
So we assume that the right part of SwitchLabeledRule is always a
statement and reused ExpressionStatement to express Expression plus
semicolon, because syntactically it looks the same. Strictly following
a spec draft here looks even more ugly, because it requires more
object types in our code model and reduces the flexibility when we
need to perform code transformation. E.g. if we want to wrap
expression into block, currently we just need to replace an
ExpressionStatement with a Block not touching a SwitchLabel at all.
Had we mirrored the spec in our code model, we would need to replace
SwitchLabeledExpression with SwitchLabeledBlock which looks more
annoying.
With best regards,
Tagir Valeev
[1] http://cr.openjdk.java.net/~gbierman/switch-expressions.html#jep325-14.11
More information about the amber-spec-experts
mailing list