Switch expressions: parsing / syntax

Robert Field robert.field at oracle.com
Tue Sep 25 03:11:53 UTC 2018


Dan sent me a the spec changes for switch-expressions (which do seem 
like they should be associated with the JEP) -- thanks Dan!

So, switch expressions are UnaryExpressionNotPlusMinus -- which seems a 
bit strange for a self-contained thing, and which correlates with its 
odd handling in the parser.

-Robert



On 09/24/18 19:14, Robert Field wrote:
> I'm working on getting all the corners of switch expressions to work 
> in JShell.
>
> I found myself surprised at an asymmetry which is perhaps intentional 
> and reflected in the syntax (pointers please). However, I don't 
> understand why it would diverge.
>
> Switch expressions are in almost all cases treated as 
> PrimaryNoNewArray (in JLS lingo), however, field access ( 
> SwitchExpression . Identifier ), and array access ( SwitchExpression [ 
> Expression ] ) give syntax errors.
>
> In JavacParser, switch expressions are handled in term3(), which 
> appears to align with UnaryExpression in the JLS.  The handling of 
> switch expression diverges from the handling of all other cases in 
> term3() in that it returns rather than falling out of the switch into 
> a call to term3Rest() (which handles field and array access).  Other 
> cases do not return from term3() -- except for failure cases and 
> recursive cases (which call term3Rest() indirectly).
>
> -Robert
>
>



More information about the compiler-dev mailing list