@@ -772,8 +772,8 @@ public JCPattern parsePattern(int pos, JCModifiers mods, JCExpression parsedType
772772 accept (RPAREN );
773773 pattern = toP (F .at (startPos ).ParenthesizedPattern (p ));
774774 } else {
775- JCExpression e = parsedType == null ? term ( EXPR | TYPE | NOLAMBDA ) : parsedType ;
776- mods = mods != null ? mods : F . at ( token . pos ). Modifiers ( 0 ) ;
775+ mods = mods != null ? mods : optFinal ( 0 ) ;
776+ JCExpression e = parsedType == null ? term ( TYPE | NOLAMBDA ) : parsedType ;
777777 JCVariableDecl var = toP (F .at (token .pos ).VarDef (mods , ident (), e , null ));
778778 pattern = toP (F .at (pos ).BindingPattern (var ));
779779 }
@@ -1694,16 +1694,12 @@ boolean isUnboundMemberRef() {
16941694 * method reference or a binary expression. To disambiguate, look for a
16951695 * matching '>' and see if the subsequent terminal is either '.' or '::'.
16961696 */
1697- ParensResult analyzeParens () {
1698- return analyzeParens (0 );
1699- }
1700-
17011697 @ SuppressWarnings ("fallthrough" )
1702- ParensResult analyzeParens (int startLookahead ) {
1698+ ParensResult analyzeParens () {
17031699 int depth = 0 ;
17041700 boolean type = false ;
17051701 ParensResult defaultResult = ParensResult .PARENS ;
1706- outer : for (int lookahead = startLookahead ; ; lookahead ++) {
1702+ outer : for (int lookahead = 0 ; ; lookahead ++) {
17071703 TokenKind tk = S .token (lookahead ).kind ;
17081704 switch (tk ) {
17091705 case COMMA :
@@ -1729,7 +1725,7 @@ ParensResult analyzeParens(int startLookahead) {
17291725 }
17301726 break ;
17311727 case LPAREN :
1732- if (lookahead != startLookahead ) {
1728+ if (lookahead != 0 ) {
17331729 // '(' in a non-starting position -> parens
17341730 return ParensResult .PARENS ;
17351731 } else if (peekToken (lookahead , RPAREN )) {
@@ -1780,31 +1776,7 @@ ParensResult analyzeParens(int startLookahead) {
17801776 return ParensResult .EXPLICIT_LAMBDA ;
17811777 case MONKEYS_AT :
17821778 type = true ;
1783- lookahead += 1 ; //skip '@'
1784- while (peekToken (lookahead , DOT )) {
1785- lookahead += 2 ;
1786- }
1787- if (peekToken (lookahead , LPAREN )) {
1788- lookahead ++;
1789- //skip annotation values
1790- int nesting = 0 ;
1791- for (; ; lookahead ++) {
1792- TokenKind tk2 = S .token (lookahead ).kind ;
1793- switch (tk2 ) {
1794- case EOF :
1795- return ParensResult .PARENS ;
1796- case LPAREN :
1797- nesting ++;
1798- break ;
1799- case RPAREN :
1800- nesting --;
1801- if (nesting == 0 ) {
1802- continue outer ;
1803- }
1804- break ;
1805- }
1806- }
1807- }
1779+ lookahead = skipAnnotation (lookahead );
18081780 break ;
18091781 case LBRACKET :
18101782 if (peekToken (lookahead , RBRACKET , LAX_IDENTIFIER )) {
@@ -1861,6 +1833,35 @@ ParensResult analyzeParens(int startLookahead) {
18611833 }
18621834 }
18631835
1836+ private int skipAnnotation (int lookahead ) {
1837+ lookahead += 1 ; //skip '@'
1838+ while (peekToken (lookahead , DOT )) {
1839+ lookahead += 2 ;
1840+ }
1841+ if (peekToken (lookahead , LPAREN )) {
1842+ lookahead ++;
1843+ //skip annotation values
1844+ int nesting = 0 ;
1845+ for (; ; lookahead ++) {
1846+ TokenKind tk2 = S .token (lookahead ).kind ;
1847+ switch (tk2 ) {
1848+ case EOF :
1849+ return lookahead ;
1850+ case LPAREN :
1851+ nesting ++;
1852+ break ;
1853+ case RPAREN :
1854+ nesting --;
1855+ if (nesting == 0 ) {
1856+ return lookahead ;
1857+ }
1858+ break ;
1859+ }
1860+ }
1861+ }
1862+ return lookahead ;
1863+ }
1864+
18641865 /** Accepts all identifier-like tokens */
18651866 protected Predicate <TokenKind > LAX_IDENTIFIER = t -> t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM ;
18661867
@@ -3067,34 +3068,69 @@ private JCCaseLabel parseCaseLabel() {
30673068 nextToken ();
30683069 label = toP (F .at (patternPos ).DefaultCaseLabel ());
30693070 } else {
3070- if (token .kind == LPAREN ) {
3071- int lookahead = 0 ;
3072- while (S .token (lookahead + 1 ).kind == LPAREN ) {
3073- lookahead ++;
3074- }
3075- boolean pattern = analyzeParens (lookahead ) == ParensResult .EXPLICIT_LAMBDA ;
3076- if (pattern ) {
3077- checkSourceLevel (token .pos , Feature .PATTERN_SWITCH );
3078- return parsePattern (token .pos , null , null , false );
3079- } else {
3080- return term (EXPR | TYPE | NOLAMBDA );
3081- }
3071+ int lookahead = 0 ;
3072+ while (S .token (lookahead ).kind == LPAREN ) {
3073+ lookahead ++;
3074+ }
3075+ JCModifiers mods = optFinal (0 );
3076+ boolean pattern = mods .flags != 0 || mods .annotations .nonEmpty () ||
3077+ analyzePattern (lookahead ) == PatternResult .PATTERN ;
3078+ if (pattern ) {
3079+ checkSourceLevel (token .pos , Feature .PATTERN_SWITCH );
3080+ return parsePattern (patternPos , mods , null , false );
30823081 } else {
3083- JCModifiers mods = optFinal (0 );
3084- JCExpression e = term (EXPR | TYPE | NOLAMBDA );
3085-
3086- if (token .kind == IDENTIFIER || mods .flags != 0 || mods .annotations .nonEmpty ()) {
3087- checkSourceLevel (token .pos , Feature .PATTERN_SWITCH );
3088- return parsePattern (patternPos , null , e , false );
3089- } else {
3090- return e ;
3091- }
3082+ return term (EXPR | NOLAMBDA );
30923083 }
30933084 }
30943085
30953086 return label ;
30963087 }
30973088
3089+ @ SuppressWarnings ("fallthrough" )
3090+ PatternResult analyzePattern (int lookahead ) {
3091+ int depth = 0 ;
3092+ while (true ) {
3093+ TokenKind token = S .token (lookahead ).kind ;
3094+ switch (token ) {
3095+ case BYTE : case SHORT : case INT : case LONG : case FLOAT :
3096+ case DOUBLE : case BOOLEAN : case CHAR : case VOID :
3097+ case ASSERT , ENUM , IDENTIFIER , UNDERSCORE :
3098+ if (depth == 0 && peekToken (lookahead , LAX_IDENTIFIER )) return PatternResult .PATTERN ;
3099+ break ;
3100+ case DOT , QUES , EXTENDS , SUPER , COMMA : break ;
3101+ case LT : depth ++; break ;
3102+ case GTGTGT : depth --;
3103+ case GTGT : depth --;
3104+ case GT :
3105+ depth --;
3106+ if (depth == 0 ) {
3107+ return peekToken (lookahead , LAX_IDENTIFIER ) ? PatternResult .PATTERN
3108+ : PatternResult .EXPRESSION ;
3109+ } else if (depth < 0 ) return PatternResult .EXPRESSION ;
3110+ break ;
3111+ case MONKEYS_AT :
3112+ lookahead = skipAnnotation (lookahead );
3113+ break ;
3114+ case LBRACKET :
3115+ if (peekToken (lookahead , RBRACKET , LAX_IDENTIFIER )) {
3116+ return PatternResult .PATTERN ;
3117+ } else if (peekToken (lookahead , RBRACKET )) {
3118+ lookahead ++;
3119+ break ;
3120+ } else {
3121+ return PatternResult .EXPRESSION ;
3122+ }
3123+ default : return PatternResult .EXPRESSION ;
3124+ }
3125+ lookahead ++;
3126+ }
3127+ }
3128+
3129+ private enum PatternResult {
3130+ EXPRESSION ,
3131+ PATTERN ;
3132+ }
3133+
30983134 /** MoreStatementExpressions = { COMMA StatementExpression }
30993135 */
31003136 <T extends ListBuffer <? super JCExpressionStatement >> T moreStatementExpressions (int pos ,
0 commit comments