Switch expressions: parsing / syntax

Robert Field robert.field at oracle.com
Tue Sep 25 02:14:55 UTC 2018


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