public static JCTree.JCExpression parseString(String guardedByString, Context context) { JavacParser parser = ParserFactory.instance(context) .newParser( guardedByString, /* keepDocComments= */ false, /* keepEndPos= */ true, /* keepLineMap= */ false); JCTree.JCExpression exp; try { exp = parser.parseExpression(); } catch (Throwable e) { throw new IllegalGuardedBy(e.getMessage()); } int len = (parser.getEndPos(exp) - exp.getStartPosition()); if (len != guardedByString.length()) { throw new IllegalGuardedBy("Didn't parse entire string."); } return exp; }
JCExpression lambdaExpressionOrStatementRest(List<JCVariableDecl> args, int pos) { checkLambda(); accept(ARROW); return token.kind == LBRACE ? lambdaStatement(args, pos, pos) : lambdaExpression(args, pos); }
/** VariableInitializer = ArrayInitializer | Expression */ public JCExpression variableInitializer() { return token.kind == LBRACE ? arrayInitializer(token.pos, null) : parseExpression(); }
int pos = token.pos; JCExpression t; List<JCExpression> typeArgs = typeArgumentsOpt(EXPR); switch (token.kind) { case QUES: if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) { mode = TYPE; return typeArgument(); } else return illegal(); case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB: if (typeArgs == null && (mode & EXPR) != 0) { TokenKind tk = token.kind; nextToken(); mode = EXPR; if (tk == SUB && token.radix() == 10) { mode = EXPR; t = literal(names.hyphen, pos); } else { t = term3(); return F.at(pos).Unary(unoptag(tk), t); } else return illegal(); break; case LPAREN: if (typeArgs == null && (mode & EXPR) != 0) { ParensResult pres = analyzeParens();
switch (token.kind) { case LBRACE: return block(); case IF: { nextToken(); JCExpression cond = parExpression(); JCStatement thenpart = parseStatementAsBlock(); JCStatement elsepart = null; if (token.kind == ELSE) { nextToken(); elsepart = parseStatementAsBlock(); nextToken(); accept(LPAREN); List<JCStatement> inits = token.kind == SEMI ? List.<JCStatement>nil() : forInit(); if (inits.length() == 1 && inits.head.hasTag(VARDEF) && ((JCVariableDecl) inits.head).init == null && token.kind == COLON) { checkForeach(); JCVariableDecl var = (JCVariableDecl)inits.head; accept(COLON); JCExpression expr = parseExpression(); accept(RPAREN); JCStatement body = parseStatementAsBlock(); return F.at(pos).ForeachLoop(var, expr, body); } else { accept(SEMI); JCExpression cond = token.kind == SEMI ? null : parseExpression();
nextToken(); return List.<JCTree>nil(); } else { Comment dc = token.comment(CommentStyle.JAVADOC); int pos = token.pos; JCModifiers mods = modifiersOpt(); if (token.kind == CLASS || token.kind == INTERFACE || allowEnums && token.kind == ENUM) { return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc)); } else if (token.kind == LBRACE && !isInterface && (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 && mods.annotations.isEmpty()) { return List.<JCTree>of(block(pos, mods.flags)); } else { pos = token.pos; List<JCTypeParameter> typarams = typeParametersOpt(); storeEnd(mods, pos); List<JCAnnotation> annosAfterParams = annotationsOpt(Tag.ANNOTATION); if (isVoid) { if (annosAfterParams.nonEmpty()) illegal(annosAfterParams.head.pos); type = to(F.at(pos).TypeIdent(TypeTag.VOID)); nextToken(); } else { if (annosAfterParams.nonEmpty()) {
JCExpression term3Rest(JCExpression t, List<JCExpression> typeArgs) { if (typeArgs != null) illegal(); while (true) { int pos1 = token.pos; final List<JCAnnotation> annos = typeAnnotationsOpt(); nextToken(); if ((mode & TYPE) != 0) { int oldmode = mode; mode = TYPE; if (token.kind == RBRACKET) { nextToken(); t = bracketsOpt(t); t = toP(F.at(pos1).TypeArray(t)); if (token.kind == COLCOL) { mode = EXPR; t = toP(F.at(pos1).AnnotatedType(annos, t)); 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) { mode = EXPR; t = to(F.at(pos1).Select(t, names._super)); nextToken();
/** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt * [EXTENDS TypeList] InterfaceBody * @param mods The modifiers starting the interface declaration * @param dc The documentation comment for the interface, or null. */ protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) { int pos = token.pos; accept(INTERFACE); Name name = ident(); List<JCTypeParameter> typarams = typeParametersOpt(); List<JCExpression> extending = List.nil(); if (token.kind == EXTENDS) { nextToken(); extending = typeList(); } List<JCTree> defs = classOrInterfaceBody(name, true); JCClassDecl result = toP(F.at(pos).ClassDef( mods, name, typarams, null, extending, defs)); attach(result, dc); return result; }
case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK: case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH: return List.of(parseStatement()); case MONKEYS_AT: case FINAL: { Comment dc = token.comment(CommentStyle.JAVADOC); JCModifiers mods = modifiersOpt(); if (token.kind == INTERFACE || token.kind == CLASS || allowEnums && token.kind == ENUM) { return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); } else { JCExpression t = parseType(); ListBuffer<JCStatement> stats = variableDeclarators(mods, t, new ListBuffer<JCStatement>()); storeEnd(stats.last(), token.endPos); accept(SEMI); return stats.toList(); JCModifiers mods = modifiersOpt(); return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc)); case ENUM: case ASSERT: if (allowEnums && token.kind == ENUM) { error(token.pos, "local.enum"); dc = token.comment(CommentStyle.JAVADOC);
Comment dc) { if (isInterface && (mods.flags & Flags.STATIC) != 0) { checkStaticInterfaceMethods(); List<JCVariableDecl> params = formalParameters(); if (!isVoid) type = bracketsOpt(type); List<JCExpression> thrown = List.nil(); if (token.kind == THROWS) { nextToken(); thrown = qualidentList(); body = block(); defaultValue = null; } else { if (token.kind == DEFAULT) { accept(DEFAULT); defaultValue = annotationValue(); } else { defaultValue = null; accept(SEMI); if (token.pos <= endPosTable.errorEndPos) { skip(false, true, false, false); if (token.kind == LBRACE) { body = block(); toP(F.at(pos).MethodDef(mods, name, type, typarams, receiverParam, params, thrown,
List<JCAnnotation> packageAnnotations = List.nil(); if (token.kind == MONKEYS_AT) mods = modifiersOpt(); checkNoMods(mods.flags); packageAnnotations = mods.annotations; mods = null; nextToken(); pid = qualident(false); accept(SEMI); skip(checkForImports, false, false, false); if (token.kind == EOF) break; defs.append(importDeclaration()); } else { Comment docComment = token.comment(CommentStyle.JAVADOC); JCTree def = typeDeclaration(mods, docComment); if (def instanceof JCExpressionStatement) def = ((JCExpressionStatement)def).expr; attach(toplevel, firstToken.comment(CommentStyle.JAVADOC)); if (defs.isEmpty()) storeEnd(toplevel, S.prevToken().endPos); if (keepDocComments) toplevel.docComments = docComments;
List<JCAnnotation> annos = typeAnnotationsOpt(); accept(LBRACKET); if (token.kind == RBRACKET) { accept(RBRACKET); elemtype = bracketsOpt(elemtype, annos); if (token.kind == LBRACE) { JCNewArray na = (JCNewArray)arrayInitializer(newpos, elemtype); if (annos.nonEmpty()) { JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.<JCExpression>nil(), null)); return syntaxError(token.pos, List.<JCTree>of(t), "array.dimension.missing"); dims.append(parseExpression()); accept(RBRACKET); while (token.kind == LBRACKET || token.kind == MONKEYS_AT) { List<JCAnnotation> maybeDimAnnos = typeAnnotationsOpt(); int pos = token.pos; nextToken(); if (token.kind == RBRACKET) { elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos); } else { if (token.kind == RBRACKET) { // no dimension elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos); } else { dimAnnotations.append(maybeDimAnnos); dims.append(parseExpression()); accept(RBRACKET);
/** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments] */ JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) { nextToken(); if (token.kind == LPAREN || typeArgs != null) { t = arguments(typeArgs, t); } else if (token.kind == COLCOL) { if (typeArgs != null) return illegal(); t = memberReferenceSuffix(t); } else { int pos = token.pos; accept(DOT); typeArgs = (token.kind == LT) ? typeArguments(false) : null; t = toP(F.at(pos).Select(t, ident())); t = argumentsOpt(typeArgs, t); } return t; }
/** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody * @param mods The modifiers starting the enum declaration * @param dc The documentation comment for the enum, or null. */ protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) { int pos = token.pos; accept(ENUM); Name name = ident(); List<JCExpression> implementing = List.nil(); if (token.kind == IMPLEMENTS) { nextToken(); implementing = typeList(); } List<JCTree> defs = enumBody(name); mods.flags |= Flags.ENUM; JCClassDecl result = toP(F.at(pos). ClassDef(mods, name, List.<JCTypeParameter>nil(), null, implementing, defs)); attach(result, dc); return result; }
/** CatchClause = CATCH "(" FormalParameter ")" Block * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below. */ protected JCCatch catchClause() { int pos = token.pos; accept(CATCH); accept(LPAREN); JCModifiers mods = optFinal(Flags.PARAMETER); List<JCExpression> catchTypes = catchTypes(); JCExpression paramType = catchTypes.size() > 1 ? toP(F.at(catchTypes.head.getStartPosition()).TypeUnion(catchTypes)) : catchTypes.head; JCVariableDecl formal = variableDeclaratorId(mods, paramType); accept(RPAREN); JCBlock body = block(); return F.at(pos).Catch(formal, body); }
/** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";" */ JCTree importDeclaration() { int pos = token.pos; nextToken(); boolean importStatic = false; if (token.kind == STATIC) { checkStaticImports(); importStatic = true; nextToken(); } JCExpression pid = toP(F.at(token.pos).Ident(ident())); do { int pos1 = token.pos; accept(DOT); if (token.kind == STAR) { pid = to(F.at(pos1).Select(pid, names.asterisk)); nextToken(); break; } else { pid = toP(F.at(pos1).Select(pid, ident())); } } while (token.kind == DOT); accept(SEMI); return toP(F.at(pos).Import(pid, importStatic)); }
JCExpression annotationValue() { int pos; switch (token.kind) { case MONKEYS_AT: pos = token.pos; nextToken(); return annotation(pos, Tag.ANNOTATION); case LBRACE: pos = token.pos; accept(LBRACE); ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>(); if (token.kind == COMMA) { nextToken(); } else if (token.kind != RBRACE) { buf.append(annotationValue()); while (token.kind == COMMA) { nextToken(); if (token.kind == RBRACE) break; buf.append(annotationValue()); } } accept(RBRACE); return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList())); default: mode = EXPR; return term1(); } }
accept(LBRACE); ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); if (token.kind == COMMA) { nextToken(); } else if (token.kind != RBRACE && token.kind != SEMI) { defs.append(enumeratorDeclaration(enumName)); while (token.kind == COMMA) { nextToken(); if (token.kind == RBRACE || token.kind == SEMI) break; defs.append(enumeratorDeclaration(enumName)); defs.append(syntaxError(token.pos, "expected3", COMMA, RBRACE, SEMI)); nextToken(); nextToken(); while (token.kind != RBRACE && token.kind != EOF) { defs.appendList(classOrInterfaceBodyDeclaration(enumName, false)); if (token.pos <= endPosTable.errorEndPos) { skip(false, true, true, false); accept(RBRACE); return defs.toList();
case STRICTFP : flag = Flags.STRICTFP; break; case MONKEYS_AT : flag = Flags.ANNOTATION; break; case DEFAULT : checkDefaultMethods(); flag = Flags.DEFAULT; break; case ERROR : flag = 0; nextToken(); break; default: break loop; if ((flags & flag) != 0) error(token.pos, "repeated.modifier"); lastPos = token.pos; nextToken(); if (flag == Flags.ANNOTATION) { checkAnnotations(); if (token.kind != INTERFACE) { JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION); storeEnd(mods, S.prevToken().endPos); return mods;
protected JCCase switchBlockStatementGroup() { int pos = token.pos; List<JCStatement> stats; JCCase c; switch (token.kind) { case CASE: nextToken(); JCExpression pat = parseExpression(); accept(COLON); stats = blockStatements(); c = F.at(pos).Case(pat, stats); if (stats.isEmpty()) storeEnd(c, S.prevToken().endPos); return c; case DEFAULT: nextToken(); accept(COLON); stats = blockStatements(); c = F.at(pos).Case(null, stats); if (stats.isEmpty()) storeEnd(c, S.prevToken().endPos); return c; } throw new AssertionError("should not reach here"); }