langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
changeset 42828 cce89649f958
parent 42827 36468b5fa7f4
child 43030 a3d5b201d899
equal deleted inserted replaced
42827:36468b5fa7f4 42828:cce89649f958
   416             nextToken();
   416             nextToken();
   417         }
   417         }
   418     }
   418     }
   419 
   419 
   420     protected JCErroneous syntaxError(int pos, String key, TokenKind... args) {
   420     protected JCErroneous syntaxError(int pos, String key, TokenKind... args) {
   421         return syntaxError(pos, List.<JCTree>nil(), key, args);
   421         return syntaxError(pos, List.nil(), key, args);
   422     }
   422     }
   423 
   423 
   424     protected JCErroneous syntaxError(int pos, List<JCTree> errs, String key, TokenKind... args) {
   424     protected JCErroneous syntaxError(int pos, List<JCTree> errs, String key, TokenKind... args) {
   425         setErrorEndPos(pos);
   425         setErrorEndPos(pos);
   426         JCErroneous err = F.at(pos).Erroneous(errs);
   426         JCErroneous err = F.at(pos).Erroneous(errs);
  1880                     break;
  1880                     break;
  1881                 }
  1881                 }
  1882                 return args.toList();
  1882                 return args.toList();
  1883             }
  1883             }
  1884         } else {
  1884         } else {
  1885             return List.<JCExpression>of(syntaxError(token.pos, "expected", LT));
  1885             return List.of(syntaxError(token.pos, "expected", LT));
  1886         }
  1886         }
  1887     }
  1887     }
  1888 
  1888 
  1889     /**
  1889     /**
  1890      *  {@literal
  1890      *  {@literal
  1965     }
  1965     }
  1966 
  1966 
  1967     /** BracketsOpt = [ "[" "]" { [Annotations] "[" "]"} ]
  1967     /** BracketsOpt = [ "[" "]" { [Annotations] "[" "]"} ]
  1968      */
  1968      */
  1969     private JCExpression bracketsOpt(JCExpression t) {
  1969     private JCExpression bracketsOpt(JCExpression t) {
  1970         return bracketsOpt(t, List.<JCAnnotation>nil());
  1970         return bracketsOpt(t, List.nil());
  1971     }
  1971     }
  1972 
  1972 
  1973     private JCExpression bracketsOptCont(JCExpression t, int pos,
  1973     private JCExpression bracketsOptCont(JCExpression t, int pos,
  1974             List<JCAnnotation> annotations) {
  1974             List<JCAnnotation> annotations) {
  1975         accept(RBRACKET);
  1975         accept(RBRACKET);
  2145             }
  2145             }
  2146             return newClass;
  2146             return newClass;
  2147         } else {
  2147         } else {
  2148             setErrorEndPos(token.pos);
  2148             setErrorEndPos(token.pos);
  2149             reportSyntaxError(token.pos, "expected2", LPAREN, LBRACKET);
  2149             reportSyntaxError(token.pos, "expected2", LPAREN, LBRACKET);
  2150             t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null));
  2150             t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.nil(), null));
  2151             return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
  2151             return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
  2152         }
  2152         }
  2153     }
  2153     }
  2154 
  2154 
  2155     /** InnerCreator = [Annotations] Ident [TypeArguments] ClassCreatorRest
  2155     /** InnerCreator = [Annotations] Ident [TypeArguments] ClassCreatorRest
  2195                     na.annotations = annotated.annotations;
  2195                     na.annotations = annotated.annotations;
  2196                     na.elemtype = annotated.underlyingType;
  2196                     na.elemtype = annotated.underlyingType;
  2197                 }
  2197                 }
  2198                 return na;
  2198                 return na;
  2199             } else {
  2199             } else {
  2200                 JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.<JCExpression>nil(), null));
  2200                 JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.nil(), null));
  2201                 return syntaxError(token.pos, List.<JCTree>of(t), "array.dimension.missing");
  2201                 return syntaxError(token.pos, List.of(t), "array.dimension.missing");
  2202             }
  2202             }
  2203         } else {
  2203         } else {
  2204             ListBuffer<JCExpression> dims = new ListBuffer<>();
  2204             ListBuffer<JCExpression> dims = new ListBuffer<>();
  2205 
  2205 
  2206             // maintain array dimension type annotations
  2206             // maintain array dimension type annotations
  2265                 if (token.kind == RBRACE) break;
  2265                 if (token.kind == RBRACE) break;
  2266                 elems.append(variableInitializer());
  2266                 elems.append(variableInitializer());
  2267             }
  2267             }
  2268         }
  2268         }
  2269         accept(RBRACE);
  2269         accept(RBRACE);
  2270         return toP(F.at(newpos).NewArray(t, List.<JCExpression>nil(), elems.toList()));
  2270         return toP(F.at(newpos).NewArray(t, List.nil(), elems.toList()));
  2271     }
  2271     }
  2272 
  2272 
  2273     /** VariableInitializer = ArrayInitializer | Expression
  2273     /** VariableInitializer = ArrayInitializer | Expression
  2274      */
  2274      */
  2275     public JCExpression variableInitializer() {
  2275     public JCExpression variableInitializer() {
  2418             Token prevToken = token;
  2418             Token prevToken = token;
  2419             JCExpression t = term(EXPR | TYPE);
  2419             JCExpression t = term(EXPR | TYPE);
  2420             if (token.kind == COLON && t.hasTag(IDENT)) {
  2420             if (token.kind == COLON && t.hasTag(IDENT)) {
  2421                 nextToken();
  2421                 nextToken();
  2422                 JCStatement stat = parseStatementAsBlock();
  2422                 JCStatement stat = parseStatementAsBlock();
  2423                 return List.<JCStatement>of(F.at(pos).Labelled(prevToken.name(), stat));
  2423                 return List.of(F.at(pos).Labelled(prevToken.name(), stat));
  2424             } else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
  2424             } else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
  2425                 pos = token.pos;
  2425                 pos = token.pos;
  2426                 JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
  2426                 JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
  2427                 F.at(pos);
  2427                 F.at(pos);
  2428                 ListBuffer<JCStatement> stats =
  2428                 ListBuffer<JCStatement> stats =
  2434             } else {
  2434             } else {
  2435                 // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
  2435                 // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
  2436                 t = checkExprStat(t);
  2436                 t = checkExprStat(t);
  2437                 accept(SEMI);
  2437                 accept(SEMI);
  2438                 JCExpressionStatement expr = toP(F.at(pos).Exec(t));
  2438                 JCExpressionStatement expr = toP(F.at(pos).Exec(t));
  2439                 return List.<JCStatement>of(expr);
  2439                 return List.of(expr);
  2440             }
  2440             }
  2441         }
  2441         }
  2442     }
  2442     }
  2443 
  2443 
  2444     /** Statement =
  2444     /** Statement =
  2476             return F.at(pos).If(cond, thenpart, elsepart);
  2476             return F.at(pos).If(cond, thenpart, elsepart);
  2477         }
  2477         }
  2478         case FOR: {
  2478         case FOR: {
  2479             nextToken();
  2479             nextToken();
  2480             accept(LPAREN);
  2480             accept(LPAREN);
  2481             List<JCStatement> inits = token.kind == SEMI ? List.<JCStatement>nil() : forInit();
  2481             List<JCStatement> inits = token.kind == SEMI ? List.nil() : forInit();
  2482             if (inits.length() == 1 &&
  2482             if (inits.length() == 1 &&
  2483                 inits.head.hasTag(VARDEF) &&
  2483                 inits.head.hasTag(VARDEF) &&
  2484                 ((JCVariableDecl) inits.head).init == null &&
  2484                 ((JCVariableDecl) inits.head).init == null &&
  2485                 token.kind == COLON) {
  2485                 token.kind == COLON) {
  2486                 JCVariableDecl var = (JCVariableDecl)inits.head;
  2486                 JCVariableDecl var = (JCVariableDecl)inits.head;
  2491                 return F.at(pos).ForeachLoop(var, expr, body);
  2491                 return F.at(pos).ForeachLoop(var, expr, body);
  2492             } else {
  2492             } else {
  2493                 accept(SEMI);
  2493                 accept(SEMI);
  2494                 JCExpression cond = token.kind == SEMI ? null : parseExpression();
  2494                 JCExpression cond = token.kind == SEMI ? null : parseExpression();
  2495                 accept(SEMI);
  2495                 accept(SEMI);
  2496                 List<JCExpressionStatement> steps = token.kind == RPAREN ? List.<JCExpressionStatement>nil() : forUpdate();
  2496                 List<JCExpressionStatement> steps = token.kind == RPAREN ? List.nil() : forUpdate();
  2497                 accept(RPAREN);
  2497                 accept(RPAREN);
  2498                 JCStatement body = parseStatementAsBlock();
  2498                 JCStatement body = parseStatementAsBlock();
  2499                 return F.at(pos).ForLoop(inits, cond, steps, body);
  2499                 return F.at(pos).ForLoop(inits, cond, steps, body);
  2500             }
  2500             }
  2501         }
  2501         }
  2514             JCDoWhileLoop t = toP(F.at(pos).DoLoop(body, cond));
  2514             JCDoWhileLoop t = toP(F.at(pos).DoLoop(body, cond));
  2515             return t;
  2515             return t;
  2516         }
  2516         }
  2517         case TRY: {
  2517         case TRY: {
  2518             nextToken();
  2518             nextToken();
  2519             List<JCTree> resources = List.<JCTree>nil();
  2519             List<JCTree> resources = List.nil();
  2520             if (token.kind == LPAREN) {
  2520             if (token.kind == LPAREN) {
  2521                 checkTryWithResources();
  2521                 checkTryWithResources();
  2522                 nextToken();
  2522                 nextToken();
  2523                 resources = resources();
  2523                 resources = resources();
  2524                 accept(RPAREN);
  2524                 accept(RPAREN);
  2624 
  2624 
  2625     private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) {
  2625     private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) {
  2626         int errPos = S.errPos();
  2626         int errPos = S.errPos();
  2627         JCTree stm = action.doRecover(this);
  2627         JCTree stm = action.doRecover(this);
  2628         S.errPos(errPos);
  2628         S.errPos(errPos);
  2629         return toP(F.Exec(syntaxError(startPos, List.<JCTree>of(stm), key)));
  2629         return toP(F.Exec(syntaxError(startPos, List.of(stm), key)));
  2630     }
  2630     }
  2631 
  2631 
  2632     /** CatchClause     = CATCH "(" FormalParameter ")" Block
  2632     /** CatchClause     = CATCH "(" FormalParameter ")" Block
  2633      * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below.
  2633      * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below.
  2634      */
  2634      */
  2880         storeEnd(ann, S.prevToken().endPos);
  2880         storeEnd(ann, S.prevToken().endPos);
  2881         return ann;
  2881         return ann;
  2882     }
  2882     }
  2883 
  2883 
  2884     List<JCExpression> annotationFieldValuesOpt() {
  2884     List<JCExpression> annotationFieldValuesOpt() {
  2885         return (token.kind == LPAREN) ? annotationFieldValues() : List.<JCExpression>nil();
  2885         return (token.kind == LPAREN) ? annotationFieldValues() : List.nil();
  2886     }
  2886     }
  2887 
  2887 
  2888     /** AnnotationFieldValues   = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */
  2888     /** AnnotationFieldValues   = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */
  2889     List<JCExpression> annotationFieldValues() {
  2889     List<JCExpression> annotationFieldValues() {
  2890         accept(LPAREN);
  2890         accept(LPAREN);
  2943                     if (token.kind == RBRACE) break;
  2943                     if (token.kind == RBRACE) break;
  2944                     buf.append(annotationValue());
  2944                     buf.append(annotationValue());
  2945                 }
  2945                 }
  2946             }
  2946             }
  2947             accept(RBRACE);
  2947             accept(RBRACE);
  2948             return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList()));
  2948             return toP(F.at(pos).NewArray(null, List.nil(), buf.toList()));
  2949         default:
  2949         default:
  2950             mode = EXPR;
  2950             mode = EXPR;
  2951             return term1();
  2951             return term1();
  2952         }
  2952         }
  2953     }
  2953     }
  3340             return enumDeclaration(mods, dc);
  3340             return enumDeclaration(mods, dc);
  3341         } else {
  3341         } else {
  3342             int pos = token.pos;
  3342             int pos = token.pos;
  3343             List<JCTree> errs;
  3343             List<JCTree> errs;
  3344             if (LAX_IDENTIFIER.accepts(token.kind)) {
  3344             if (LAX_IDENTIFIER.accepts(token.kind)) {
  3345                 errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident())));
  3345                 errs = List.of(mods, toP(F.at(pos).Ident(ident())));
  3346                 setErrorEndPos(token.pos);
  3346                 setErrorEndPos(token.pos);
  3347             } else {
  3347             } else {
  3348                 errs = List.<JCTree>of(mods);
  3348                 errs = List.of(mods);
  3349             }
  3349             }
  3350             return toP(F.Exec(syntaxError(pos, errs, "expected3",
  3350             return toP(F.Exec(syntaxError(pos, errs, "expected3",
  3351                                           CLASS, INTERFACE, ENUM)));
  3351                                           CLASS, INTERFACE, ENUM)));
  3352         }
  3352         }
  3353     }
  3353     }
  3421         }
  3421         }
  3422 
  3422 
  3423         List<JCTree> defs = enumBody(name);
  3423         List<JCTree> defs = enumBody(name);
  3424         mods.flags |= Flags.ENUM;
  3424         mods.flags |= Flags.ENUM;
  3425         JCClassDecl result = toP(F.at(pos).
  3425         JCClassDecl result = toP(F.at(pos).
  3426             ClassDef(mods, name, List.<JCTypeParameter>nil(),
  3426             ClassDef(mods, name, List.nil(),
  3427                      null, implementing, defs));
  3427                      null, implementing, defs));
  3428         attach(result, dc);
  3428         attach(result, dc);
  3429         return result;
  3429         return result;
  3430     }
  3430     }
  3431 
  3431 
  3479         List<JCExpression> typeArgs = typeArgumentsOpt();
  3479         List<JCExpression> typeArgs = typeArgumentsOpt();
  3480         int identPos = token.pos;
  3480         int identPos = token.pos;
  3481         Name name = ident();
  3481         Name name = ident();
  3482         int createPos = token.pos;
  3482         int createPos = token.pos;
  3483         List<JCExpression> args = (token.kind == LPAREN)
  3483         List<JCExpression> args = (token.kind == LPAREN)
  3484             ? arguments() : List.<JCExpression>nil();
  3484             ? arguments() : List.nil();
  3485         JCClassDecl body = null;
  3485         JCClassDecl body = null;
  3486         if (token.kind == LBRACE) {
  3486         if (token.kind == LBRACE) {
  3487             JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM);
  3487             JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM);
  3488             List<JCTree> defs = classOrInterfaceBody(names.empty, false);
  3488             List<JCTree> defs = classOrInterfaceBody(names.empty, false);
  3489             body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
  3489             body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
  3565      *
  3565      *
  3566      */
  3566      */
  3567     protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
  3567     protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
  3568         if (token.kind == SEMI) {
  3568         if (token.kind == SEMI) {
  3569             nextToken();
  3569             nextToken();
  3570             return List.<JCTree>nil();
  3570             return List.nil();
  3571         } else {
  3571         } else {
  3572             Comment dc = token.comment(CommentStyle.JAVADOC);
  3572             Comment dc = token.comment(CommentStyle.JAVADOC);
  3573             int pos = token.pos;
  3573             int pos = token.pos;
  3574             JCModifiers mods = modifiersOpt();
  3574             JCModifiers mods = modifiersOpt();
  3575             if (token.kind == CLASS ||
  3575             if (token.kind == CLASS ||
  3576                 token.kind == INTERFACE ||
  3576                 token.kind == INTERFACE ||
  3577                 token.kind == ENUM) {
  3577                 token.kind == ENUM) {
  3578                 return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc));
  3578                 return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
  3579             } else if (token.kind == LBRACE &&
  3579             } else if (token.kind == LBRACE &&
  3580                        (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
  3580                        (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
  3581                        mods.annotations.isEmpty()) {
  3581                        mods.annotations.isEmpty()) {
  3582                 if (isInterface) {
  3582                 if (isInterface) {
  3583                     error(token.pos, "initializer.not.allowed");
  3583                     error(token.pos, "initializer.not.allowed");
  3584                 }
  3584                 }
  3585                 return List.<JCTree>of(block(pos, mods.flags));
  3585                 return List.of(block(pos, mods.flags));
  3586             } else {
  3586             } else {
  3587                 pos = token.pos;
  3587                 pos = token.pos;
  3588                 List<JCTypeParameter> typarams = typeParametersOpt();
  3588                 List<JCTypeParameter> typarams = typeParametersOpt();
  3589                 // if there are type parameters but no modifiers, save the start
  3589                 // if there are type parameters but no modifiers, save the start
  3590                 // position of the method in the modifiers.
  3590                 // position of the method in the modifiers.
  3635                         storeEnd(defs.last(), S.prevToken().endPos);
  3635                         storeEnd(defs.last(), S.prevToken().endPos);
  3636                         return defs;
  3636                         return defs;
  3637                     } else {
  3637                     } else {
  3638                         pos = token.pos;
  3638                         pos = token.pos;
  3639                         List<JCTree> err = isVoid
  3639                         List<JCTree> err = isVoid
  3640                             ? List.<JCTree>of(toP(F.at(pos).MethodDef(mods, name, type, typarams,
  3640                             ? List.of(toP(F.at(pos).MethodDef(mods, name, type, typarams,
  3641                                 List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null)))
  3641                                 List.nil(), List.nil(), null, null)))
  3642                             : null;
  3642                             : null;
  3643                         return List.<JCTree>of(syntaxError(token.pos, err, "expected", LPAREN));
  3643                         return List.of(syntaxError(token.pos, err, "expected", LPAREN));
  3644                     }
  3644                     }
  3645                 }
  3645                 }
  3646             }
  3646             }
  3647         }
  3647         }
  3648     }
  3648     }