/** 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. */ JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(INTERFACE); Name name = ident(); List<JCTypeParameter> typarams = typeParametersOpt(); List<JCExpression> extending = List.nil(); if (S.token() == EXTENDS) { S.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; }
/** * Create a new Parser. * @param S Lexer for getting tokens while parsing * @param keepDocComments true if javadoc comments should be kept * @param genEndPos true if end positions should be generated */ public Parser newParser(Lexer S, boolean keepDocComments, boolean genEndPos) { if (!genEndPos) return new Parser(this, S, keepDocComments); else return new EndPosParser(this, S, keepDocComments); } }
/** AnnotationFieldValues = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */ List<JCExpression> annotationFieldValues() { accept(LPAREN); ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>(); if (S.token() != RPAREN) { buf.append(annotationFieldValue()); while (S.token() == COMMA) { S.nextToken(); buf.append(annotationFieldValue()); } } accept(RPAREN); return buf.toList(); }
/** ParExpression = "(" Expression ")" */ JCExpression parExpression() { accept(LPAREN); JCExpression t = expression(); accept(RPAREN); return t; }
private JCArrayTypeTree bracketsOptCont(JCExpression t, int pos) { accept(RBRACKET); t = bracketsOpt(t); return toP(F.at(pos).TypeArray(t)); }
int pos = S.pos(); JCExpression t; List<JCExpression> typeArgs = typeArgumentsOpt(EXPR); switch (S.token()) { 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) { S.radix() == 10) { mode = EXPR; t = literal(names.hyphen); } else { t = term3(); return F.at(pos).Unary(unoptag(token), t); } else return illegal(); break; case LPAREN: S.nextToken(); mode = EXPR | TYPE | NOPARAMS; t = term3(); if ((mode & TYPE) != 0 && S.token() == LT) {
switch (S.token()) { case LBRACE: return block(); case IF: { S.nextToken(); JCExpression cond = parExpression(); JCStatement thenpart = statement(); JCStatement elsepart = null; if (S.token() == ELSE) { S.nextToken(); elsepart = statement(); accept(LPAREN); List<JCStatement> inits = S.token() == SEMI ? List.<JCStatement>nil() : forInit(); if (inits.length() == 1 && inits.head.getTag() == JCTree.VARDEF && ((JCVariableDecl) inits.head).init == null && S.token() == COLON) { checkForeach(); JCVariableDecl var = (JCVariableDecl)inits.head; accept(COLON); JCExpression expr = expression(); accept(RPAREN); JCStatement body = statement(); return F.at(pos).ForeachLoop(var, expr, body); } else { accept(SEMI); JCExpression cond = S.token() == SEMI ? null : expression(); accept(SEMI);
String dc = S.docComment(); int pos = S.pos(); JCModifiers mods = modifiersOpt(); if (S.token() == CLASS || S.token() == INTERFACE || allowEnums && S.token() == ENUM) { return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc)); } else if (S.token() == LBRACE && !isInterface && (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 && mods.annotations.isEmpty()) { return List.<JCTree>of(block(pos, mods.flags)); } else { pos = S.pos(); List<JCTypeParameter> typarams = typeParametersOpt(); boolean isVoid = S.token() == VOID; if (isVoid) { type = to(F.at(pos).TypeIdent(TypeTags.VOID)); S.nextToken(); } else { type = type(); return List.of(methodDeclaratorRest( pos, mods, null, names.init, typarams, isInterface, true, dc)); } else { pos = S.pos(); name = ident(); if (S.token() == LPAREN) {
case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK: case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH: stats.append(statement()); break; case MONKEYS_AT: case FINAL: { String dc = S.docComment(); JCModifiers mods = modifiersOpt(); if (S.token() == INTERFACE || S.token() == CLASS || allowEnums && S.token() == ENUM) { stats.append(classOrInterfaceOrEnumDeclaration(mods, dc)); } else { JCExpression t = type(); stats.appendList(variableDeclarators(mods, t, new ListBuffer<JCStatement>())); storeEnd(stats.elems.last(), S.endPos()); accept(SEMI); JCModifiers mods = modifiersOpt(); stats.append(classOrInterfaceOrEnumDeclaration(mods, dc)); break; stats.append(classOrInterfaceOrEnumDeclaration(modifiersOpt(), S.docComment())); break; log.error(S.pos(), "local.enum");
boolean isInterface, boolean isVoid, String dc) { List<JCVariableDecl> params = formalParameters(); if (!isVoid) type = bracketsOpt(type); List<JCExpression> thrown = List.nil(); if (S.token() == THROWS) { S.nextToken(); thrown = qualidentList(); body = block(); defaultValue = null; } else { if (S.token() == DEFAULT) { accept(DEFAULT); defaultValue = annotationValue(); } else { defaultValue = null; accept(SEMI); if (S.pos() <= errorEndPos) { skip(false, true, false, false); if (S.token() == LBRACE) { body = block(); toP(F.at(pos).MethodDef(mods, name, type, typarams, params, thrown, body, defaultValue)); attach(result, dc);
List<JCAnnotation> packageAnnotations = List.nil(); if (S.token() == MONKEYS_AT) mods = modifiersOpt(); checkNoMods(mods.flags); packageAnnotations = mods.annotations; mods = null; pid = qualident(); accept(SEMI); if (S.pos() <= errorEndPos) { skip(checkForImports, false, false, false); if (S.token() == EOF) break; defs.append(importDeclaration()); } else { JCTree def = typeDeclaration(mods); if (def instanceof JCExpressionStatement) def = ((JCExpressionStatement)def).expr; attach(toplevel, dc); if (defs.elems.isEmpty()) storeEnd(toplevel, S.prevEndPos()); if (keepDocComments) toplevel.docComments = docComments; return toplevel;
List<JCAnnotation> annotations = annotationsOpt(); JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations); List<JCExpression> typeArgs = typeArgumentsOpt(); int identPos = S.pos(); Name name = ident(); int createPos = S.pos(); List<JCExpression> args = (S.token() == LPAREN) ? arguments() : List.<JCExpression>nil(); JCClassDecl body = null; if (S.token() == LBRACE) { JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC); List<JCTree> defs = classOrInterfaceBody(names.empty, false); body = toP(F.at(identPos).AnonymousClassDef(mods1, defs)); JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body); if (createPos != Position.NOPOS) storeEnd(create, S.prevEndPos()); ident = F.at(Position.NOPOS).Ident(enumName); JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create)); attach(result, dc); return result;
/** AnnotationFieldValue = AnnotationValue * | Identifier "=" AnnotationValue */ JCExpression annotationFieldValue() { if (S.token() == IDENTIFIER) { mode = EXPR; JCExpression t1 = term1(); if (t1.tag == JCTree.IDENT && S.token() == EQ) { int pos = S.pos(); accept(EQ); return toP(F.at(pos).Assign(t1, annotationValue())); } else { return t1; } } return annotationValue(); }
/** ArrayCreatorRest = "[" ( "]" BracketsOpt ArrayInitializer * | Expression "]" {"[" Expression "]"} BracketsOpt ) */ JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) { accept(LBRACKET); if (S.token() == RBRACKET) { accept(RBRACKET); elemtype = bracketsOpt(elemtype); if (S.token() == LBRACE) { return arrayInitializer(newpos, elemtype); } else { return syntaxError(S.pos(), "array.dimension.missing"); } } else { ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>(); dims.append(expression()); accept(RBRACKET); while (S.token() == LBRACKET) { int pos = S.pos(); S.nextToken(); if (S.token() == RBRACKET) { elemtype = bracketsOptCont(elemtype, pos); } else { dims.append(expression()); accept(RBRACKET); } } return toP(F.at(newpos).NewArray(elemtype, dims.toList(), null)); } }
/** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments] */ JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) { S.nextToken(); if (S.token() == LPAREN || typeArgs != null) { t = arguments(typeArgs, t); } else { int pos = S.pos(); accept(DOT); typeArgs = (S.token() == LT) ? typeArguments() : 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. */ JCClassDecl enumDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(ENUM); Name name = ident(); List<JCExpression> implementing = List.nil(); if (S.token() == IMPLEMENTS) { S.nextToken(); implementing = typeList(); } List<JCTree> defs = enumBody(name); JCModifiers newMods = F.at(mods.pos).Modifiers(mods.flags|Flags.ENUM, mods.annotations); JCClassDecl result = toP(F.at(pos). ClassDef(newMods, name, List.<JCTypeParameter>nil(), null, implementing, defs)); attach(result, dc); return result; }
/** Block = "{" BlockStatements "}" */ JCBlock block(int pos, long flags) { accept(LBRACE); List<JCStatement> stats = blockStatements(); JCBlock t = F.at(pos).Block(flags, stats); while (S.token() == CASE || S.token() == DEFAULT) { syntaxError("orphaned", keywords.token2string(S.token())); switchBlockStatementGroups(); } // the Block node has a field "endpos" for first char of last token, which is // usually but not necessarily the last char of the last token. t.endpos = S.pos(); accept(RBRACE); return toP(t); }
/** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";" */ JCTree importDeclaration() { int pos = S.pos(); S.nextToken(); boolean importStatic = false; if (S.token() == STATIC) { checkStaticImports(); importStatic = true; S.nextToken(); } JCExpression pid = toP(F.at(S.pos()).Ident(ident())); do { int pos1 = S.pos(); accept(DOT); if (S.token() == STAR) { pid = to(F.at(pos1).Select(pid, names.asterisk)); S.nextToken(); break; } else { pid = toP(F.at(pos1).Select(pid, ident())); } } while (S.token() == DOT); accept(SEMI); return toP(F.at(pos).Import(pid, importStatic)); }
accept(LBRACE); ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); if (S.token() == COMMA) { S.nextToken(); } else if (S.token() != RBRACE && S.token() != SEMI) { defs.append(enumeratorDeclaration(enumName)); while (S.token() == COMMA) { S.nextToken(); if (S.token() == RBRACE || S.token() == SEMI) break; defs.append(enumeratorDeclaration(enumName)); defs.append(syntaxError(S.pos(), "expected3", keywords.token2string(COMMA), keywords.token2string(RBRACE), S.nextToken(); while (S.token() != RBRACE && S.token() != EOF) { defs.appendList(classOrInterfaceBodyDeclaration(enumName, false)); if (S.pos() <= errorEndPos) { skip(false, true, true, false); accept(RBRACE); return defs.toList();