--- JavacParser.old.java 2022-11-03 14:46:02.000000000 -0300 +++ JavacParser.new.java 2022-11-03 14:46:29.000000000 -0300 @@ -238,23 +238,39 @@ * mode = TYPE : a type * mode = NOPARAMS : no parameters allowed for type * mode = TYPEARG : type argument * mode |= NOLAMBDA : lambdas are not allowed */ - protected static final int EXPR = 0x1; - protected static final int TYPE = 0x2; - protected static final int NOPARAMS = 0x4; - protected static final int TYPEARG = 0x8; - protected static final int DIAMOND = 0x10; - protected static final int NOLAMBDA = 0x20; + protected static final int EXPR = 1 << 0; + protected static final int TYPE = 1 << 1; + protected static final int NOPARAMS = 1 << 2; + protected static final int TYPEARG = 1 << 3; + protected static final int DIAMOND = 1 << 4; + protected static final int NOLAMBDA = 1 << 5; + + protected void setMode(int mode) { + this.mode = mode; + } + + protected void setLastMode(int mode) { + lastmode = mode; + } + + protected boolean isMode(int mode) { + return (this.mode & mode) != 0; + } + + protected boolean wasTypeMode() { + return (lastmode & TYPE) != 0; + } protected void selectExprMode() { - mode = (mode & NOLAMBDA) | EXPR; + setMode((mode & NOLAMBDA) | EXPR); } protected void selectTypeMode() { - mode = (mode & NOLAMBDA) | TYPE; + setMode((mode & NOLAMBDA) | TYPE); } /** The current mode. */ protected int mode = 0; @@ -474,11 +490,11 @@ /** Report an illegal start of expression/type error at given position. */ JCExpression illegal(int pos) { setErrorEndPos(pos); - if ((mode & EXPR) != 0) + if (isMode(EXPR)) return syntaxError(pos, Errors.IllegalStartOfExpr); else return syntaxError(pos, Errors.IllegalStartOfType); } @@ -866,14 +882,14 @@ protected JCExpression term(int newmode) { int prevmode = mode; - mode = newmode; + setMode(newmode); JCExpression t = term(); - lastmode = mode; - mode = prevmode; + setLastMode(mode); + setMode(prevmode); return t; } /** * {@literal @@ -888,11 +904,11 @@ * ConstantExpression = Expression * } */ JCExpression term() { JCExpression t = term1(); - if ((mode & EXPR) != 0 && + if (isMode(EXPR) && (token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0)) return termRest(t); else return t; } @@ -932,11 +948,11 @@ * Type1 = Type2 * TypeNoParams1 = TypeNoParams2 */ JCExpression term1() { JCExpression t = term2(); - if ((mode & EXPR) != 0 && token.kind == QUES) { + if (isMode(EXPR) && token.kind == QUES) { selectExprMode(); return term1Rest(t); } else { return t; } @@ -961,11 +977,11 @@ * Type2 = Type3 * TypeNoParams2 = TypeNoParams3 */ JCExpression term2() { JCExpression t = term3(); - if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) { + if (isMode(EXPR) && prec(token.kind) >= TreeInfo.orPrec) { selectExprMode(); return term2Rest(t, TreeInfo.orPrec); } else { return t; } @@ -1186,17 +1202,17 @@ int pos = token.pos; JCExpression t; List typeArgs = typeArgumentsOpt(EXPR); switch (token.kind) { case QUES: - if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) { + if (isMode(TYPE) && isMode(TYPEARG) && !isMode(NOPARAMS)) { selectTypeMode(); return typeArgument(); } else return illegal(); case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB: - if (typeArgs == null && (mode & EXPR) != 0) { + if (typeArgs == null && isMode(EXPR)) { TokenKind tk = token.kind; nextToken(); selectExprMode(); if (tk == SUB && (token.kind == INTLITERAL || token.kind == LONGLITERAL) && @@ -1208,11 +1224,11 @@ return F.at(pos).Unary(unoptag(tk), t); } } else return illegal(); break; case LPAREN: - if (typeArgs == null && (mode & EXPR) != 0) { + if (typeArgs == null && isMode(EXPR)) { ParensResult pres = analyzeParens(); switch (pres) { case CAST: accept(LPAREN); selectTypeMode(); @@ -1244,11 +1260,11 @@ } else { return illegal(); } break; case THIS: - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { selectExprMode(); t = to(F.at(pos).Ident(names._this)); nextToken(); if (typeArgs == null) t = argumentsOpt(null, t); @@ -1256,28 +1272,28 @@ t = arguments(typeArgs, t); typeArgs = null; } else return illegal(); break; case SUPER: - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { selectExprMode(); t = to(F.at(pos).Ident(names._super)); t = superSuffix(typeArgs, t); typeArgs = null; } else return illegal(); break; case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL: case TRUE: case FALSE: case NULL: - if (typeArgs == null && (mode & EXPR) != 0) { + if (typeArgs == null && isMode(EXPR)) { selectExprMode(); t = literal(names.empty); } else return illegal(); break; case NEW: if (typeArgs != null) return illegal(); - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { selectExprMode(); nextToken(); if (token.kind == LT) typeArgs = typeArguments(false); t = creator(pos, typeArgs); typeArgs = null; @@ -1291,11 +1307,11 @@ throw new AssertionError("Expected type annotations, but found none!"); } JCExpression expr = term3(); - if ((mode & TYPE) == 0) { + if (!isMode(TYPE)) { // Type annotations on class literals no longer legal switch (expr.getTag()) { case REFERENCE: { JCMemberReference mref = (JCMemberReference) expr; mref.expr = toP(F.at(pos).AnnotatedType(typeAnnos, mref.expr)); @@ -1321,11 +1337,11 @@ t = insertAnnotationsToMostInner(expr, typeAnnos, false); } break; case UNDERSCORE: case IDENTIFIER: case ASSERT: case ENUM: if (typeArgs != null) return illegal(); - if ((mode & EXPR) != 0 && (mode & NOLAMBDA) == 0 && peekToken(ARROW)) { + if (isMode(EXPR) && !isMode(NOLAMBDA) && peekToken(ARROW)) { t = lambdaExpressionOrStatement(false, false, pos); } else { t = toP(F.at(token.pos).Ident(ident())); loop: while (true) { pos = token.pos; @@ -1346,21 +1362,21 @@ if (annos.nonEmpty()) { t = toP(F.at(pos).AnnotatedType(annos, t)); } t = bracketsSuffix(t); } else { - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { selectExprMode(); JCExpression t1 = term(); if (!annos.isEmpty()) t = illegal(annos.head.pos); t = to(F.at(pos).Indexed(t, t1)); } accept(RBRACKET); } break loop; case LPAREN: - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { selectExprMode(); t = arguments(typeArgs, t); if (!annos.isEmpty()) t = illegal(annos.head.pos); typeArgs = null; } @@ -1368,15 +1384,15 @@ case DOT: nextToken(); if (token.kind == TokenKind.IDENTIFIER && typeArgs != null) { return illegal(); } - int oldmode = mode; + int prevmode = mode; mode &= ~NOPARAMS; typeArgs = typeArgumentsOpt(EXPR); - mode = oldmode; - if ((mode & EXPR) != 0) { + setMode(prevmode); + if (isMode(EXPR)) { switch (token.kind) { case CLASS: if (typeArgs != null) return illegal(); selectExprMode(); t = to(F.at(pos).Select(t, names._class)); @@ -1405,11 +1421,11 @@ break loop; } } List tyannos = null; - if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) { + if (isMode(TYPE) && token.kind == MONKEYS_AT) { tyannos = typeAnnotationsOpt(); } // typeArgs saved for next loop iteration. t = toP(F.at(pos).Select(t, ident())); if (token.pos <= endPosTable.errorEndPos && @@ -1431,11 +1447,11 @@ // Don't return here -- error recovery attempt illegal(annos.head.pos); } break loop; case LT: - if ((mode & TYPE) == 0 && isUnboundMemberRef()) { + if (!isMode(TYPE) && isUnboundMemberRef()) { //this is an unbound method reference whose qualifier //is a generic type i.e. A::m int pos1 = token.pos; accept(LT); ListBuffer args = new ListBuffer<>(); @@ -1474,11 +1490,11 @@ if (typeArgs != null) illegal(); t = bracketsSuffix(bracketsOpt(basicType())); break; case VOID: if (typeArgs != null) illegal(); - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { nextToken(); if (token.kind == DOT) { JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTag.VOID)); t = bracketsSuffix(ti); } else { @@ -1518,10 +1534,11 @@ default: nextToken(); // to ensure progress syntaxError(pos, Errors.Expected3(CASE, DEFAULT, RBRACE)); } } + // Not reachable. default: return illegal(); } return term3Rest(t, typeArgs); } @@ -1579,12 +1596,12 @@ int pos1 = token.pos; final List annos = typeAnnotationsOpt(); if (token.kind == LBRACKET) { nextToken(); - if ((mode & TYPE) != 0) { - int oldmode = mode; + if (isMode(TYPE)) { + int prevmode = mode; selectTypeMode(); if (token.kind == RBRACKET) { nextToken(); t = bracketsOpt(t); t = toP(F.at(pos1).TypeArray(t)); @@ -1595,38 +1612,38 @@ if (annos.nonEmpty()) { t = toP(F.at(pos1).AnnotatedType(annos, t)); } return t; } - mode = oldmode; + setMode(prevmode); } - if ((mode & EXPR) != 0) { + if (isMode(EXPR)) { selectExprMode(); JCExpression t1 = term(); t = to(F.at(pos1).Indexed(t, t1)); } accept(RBRACKET); } else if (token.kind == DOT) { nextToken(); typeArgs = typeArgumentsOpt(EXPR); - if (token.kind == SUPER && (mode & EXPR) != 0) { + if (token.kind == SUPER && isMode(EXPR)) { selectExprMode(); t = to(F.at(pos1).Select(t, names._super)); nextToken(); t = arguments(typeArgs, t); typeArgs = null; - } else if (token.kind == NEW && (mode & EXPR) != 0) { + } else if (token.kind == NEW && isMode(EXPR)) { if (typeArgs != null) return illegal(); selectExprMode(); int pos2 = token.pos; nextToken(); if (token.kind == LT) typeArgs = typeArguments(false); t = innerCreator(pos2, typeArgs, t); typeArgs = null; } else { List tyannos = null; - if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) { + if (isMode(TYPE) && token.kind == MONKEYS_AT) { // is the mode check needed? tyannos = typeAnnotationsOpt(); } t = toP(F.at(pos1).Select(t, ident(true))); if (token.pos <= endPosTable.errorEndPos && @@ -1640,11 +1657,11 @@ t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); } t = argumentsOpt(typeArgs, typeArgumentsOpt(t)); typeArgs = null; } - } else if ((mode & EXPR) != 0 && token.kind == COLCOL) { + } else if (isMode(EXPR) && token.kind == COLCOL) { selectExprMode(); if (typeArgs != null) return illegal(); accept(COLCOL); t = memberReferenceSuffix(pos1, t); } else { @@ -1655,11 +1672,11 @@ return illegal(annos.head.pos); } break; } } - while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && (mode & EXPR) != 0) { + while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && isMode(EXPR)) { selectExprMode(); t = to(F.at(token.pos).Unary( token.kind == PLUSPLUS ? POSTINC : POSTDEC, t)); nextToken(); } @@ -1797,12 +1814,12 @@ if (peekToken(lookahead, LAX_IDENTIFIER)) { // Identifier, Identifier/'_'/'assert'/'enum' -> explicit lambda return ParensResult.EXPLICIT_LAMBDA; } else if (peekToken(lookahead, RPAREN, ARROW)) { // Identifier, ')' '->' -> implicit lambda - return (mode & NOLAMBDA) == 0 ? ParensResult.IMPLICIT_LAMBDA - : ParensResult.PARENS; + return !isMode(NOLAMBDA) ? ParensResult.IMPLICIT_LAMBDA + : ParensResult.PARENS; } else if (depth == 0 && peekToken(lookahead, COMMA)) { defaultResult = ParensResult.IMPLICIT_LAMBDA; } type = false; break; @@ -2047,11 +2064,11 @@ } /** ArgumentsOpt = [ Arguments ] */ JCExpression argumentsOpt(List typeArgs, JCExpression t) { - if ((mode & EXPR) != 0 && token.kind == LPAREN || typeArgs != null) { + if (isMode(EXPR) && token.kind == LPAREN || typeArgs != null) { selectExprMode(); return arguments(typeArgs, t); } else { return t; } @@ -2102,12 +2119,12 @@ /** TypeArgumentsOpt = [ TypeArguments ] */ JCExpression typeArgumentsOpt(JCExpression t) { if (token.kind == LT && - (mode & TYPE) != 0 && - (mode & NOPARAMS) == 0) { + isMode(TYPE) && + !isMode(NOPARAMS)) { selectTypeMode(); return typeArguments(t, false); } else { return t; } @@ -2116,15 +2133,15 @@ return typeArgumentsOpt(TYPE); } List typeArgumentsOpt(int useMode) { if (token.kind == LT) { - if ((mode & useMode) == 0 || - (mode & NOPARAMS) != 0) { + if (!isMode(useMode) || + isMode(NOPARAMS)) { illegal(); } - mode = useMode; + setMode(useMode); return typeArguments(false); } return null; } @@ -2140,14 +2157,14 @@ mode |= DIAMOND; nextToken(); return List.nil(); } else { ListBuffer args = new ListBuffer<>(); - args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); + args.append(!isMode(EXPR) ? typeArgument() : parseType()); while (token.kind == COMMA) { nextToken(); - args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); + args.append(!isMode(EXPR) ? typeArgument() : parseType()); } switch (token.kind) { case GTGTGTEQ: case GTGTEQ: case GTEQ: case GTGTGT: case GTGT: @@ -2264,11 +2281,11 @@ /** BracketsSuffixExpr = "." CLASS * BracketsSuffixType = */ JCExpression bracketsSuffix(JCExpression t) { - if ((mode & EXPR) != 0 && token.kind == DOT) { + if (isMode(EXPR) && token.kind == DOT) { selectExprMode(); int pos = token.pos; nextToken(); accept(CLASS); if (token.pos == endPosTable.errorEndPos) { @@ -2288,11 +2305,11 @@ // taking care to handle some interior dimension(s) being annotated. if ((tag == TYPEARRAY && TreeInfo.containsTypeAnnotation(t)) || tag == ANNOTATED_TYPE) syntaxError(token.pos, Errors.NoAnnotationsOnDotClass); t = toP(F.at(pos).Select(t, names._class)); } - } else if ((mode & TYPE) != 0) { + } else if (isMode(TYPE)) { if (token.kind != COLCOL) { selectTypeMode(); } } else if (token.kind != COLCOL) { syntaxError(token.pos, Errors.DotClassExpected); @@ -2347,18 +2364,18 @@ break; default: } JCExpression t = qualident(true); - int oldmode = mode; + int prevmode = mode; selectTypeMode(); boolean diamondFound = false; int lastTypeargsPos = -1; if (token.kind == LT) { lastTypeargsPos = token.pos; t = typeArguments(t, true); - diamondFound = (mode & DIAMOND) != 0; + diamondFound = isMode(DIAMOND); } while (token.kind == DOT) { if (diamondFound) { //cannot select after a diamond illegal(); @@ -2373,14 +2390,14 @@ } if (token.kind == LT) { lastTypeargsPos = token.pos; t = typeArguments(t, true); - diamondFound = (mode & DIAMOND) != 0; + diamondFound = isMode(DIAMOND); } } - mode = oldmode; + setMode(prevmode); if (token.kind == LBRACKET || token.kind == MONKEYS_AT) { // handle type annotations for non primitive arrays if (newAnnotations.nonEmpty()) { t = insertAnnotationsToMostInner(t, newAnnotations, false); } @@ -2428,13 +2445,13 @@ if (newAnnotations.nonEmpty()) { t = toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, t)); } if (token.kind == LT) { - int oldmode = mode; + int prevmode = mode; t = typeArguments(t, true); - mode = oldmode; + setMode(prevmode); } return classCreatorRest(newpos, encl, typeArgs, t); } /** ArrayCreatorRest = [Annotations] "[" ( "]" BracketsOpt ArrayInitializer @@ -2763,11 +2780,11 @@ JCExpression t = term(EXPR | TYPE); if (token.kind == COLON && t.hasTag(IDENT)) { nextToken(); JCStatement stat = parseStatementAsBlock(); return List.of(F.at(pos).Labelled(prevToken.name(), stat)); - } else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) { + } else if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) { pos = token.pos; JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); F.at(pos); return localVariableDeclarations(mods, t); } else { @@ -3211,13 +3228,13 @@ int pos = token.pos; if (token.kind == FINAL || token.kind == MONKEYS_AT) { return variableDeclarators(optFinal(0), parseType(true), stats, true).toList(); } else { JCExpression t = term(EXPR | TYPE); - if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) { + if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) { return variableDeclarators(modifiersOpt(), t, stats, true).toList(); - } else if ((lastmode & TYPE) != 0 && token.kind == COLON) { + } else if (wasTypeMode() && token.kind == COLON) { log.error(DiagnosticFlag.SYNTAX, pos, Errors.BadInitializer("for-loop")); return List.of((JCStatement)F.at(pos).VarDef(modifiersOpt(), names.error, t, null)); } else { return moreStatementExpressions(pos, t, stats).toList(); } @@ -3243,12 +3260,12 @@ while (token.kind == MONKEYS_AT) { int pos = token.pos; nextToken(); buf.append(annotation(pos, kind)); } - lastmode = mode; - mode = prevmode; + setLastMode(mode); + setMode(prevmode); List annotations = buf.toList(); return annotations; } @@ -3666,11 +3683,11 @@ JCModifiers mods = optFinal(0); JCExpression t = parseType(true); return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false); } JCExpression t = term(EXPR | TYPE); - if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) { + if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) { JCModifiers mods = F.Modifiers(0); return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false); } else { checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES); if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) {