src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
changeset 57724 447d48371b41
parent 55561 4c0a7916d3cd
child 58109 ee07de0d2c16
child 58679 9c3209ff7550
child 58713 ad69fd32778e
equal deleted inserted replaced
57723:54a04db114d8 57724:447d48371b41
  3754      *                  [ ";" {ClassBodyDeclaration} ] "}"
  3754      *                  [ ";" {ClassBodyDeclaration} ] "}"
  3755      */
  3755      */
  3756     List<JCTree> enumBody(Name enumName) {
  3756     List<JCTree> enumBody(Name enumName) {
  3757         accept(LBRACE);
  3757         accept(LBRACE);
  3758         ListBuffer<JCTree> defs = new ListBuffer<>();
  3758         ListBuffer<JCTree> defs = new ListBuffer<>();
       
  3759         boolean wasSemi = false;
       
  3760         boolean hasStructuralErrors = false;
       
  3761         boolean wasError = false;
  3759         if (token.kind == COMMA) {
  3762         if (token.kind == COMMA) {
  3760             nextToken();
  3763             nextToken();
  3761         } else if (token.kind != RBRACE && token.kind != SEMI) {
  3764             if (token.kind == SEMI) {
  3762             defs.append(enumeratorDeclaration(enumName));
  3765                 wasSemi = true;
  3763             while (token.kind == COMMA) {
       
  3764                 nextToken();
  3766                 nextToken();
  3765                 if (token.kind == RBRACE || token.kind == SEMI) break;
  3767             } else if (token.kind != RBRACE) {
       
  3768                 reportSyntaxError(S.prevToken().endPos,
       
  3769                                   Errors.Expected2(RBRACE, SEMI));
       
  3770                 wasError = true;
       
  3771             }
       
  3772         }
       
  3773         while (token.kind != RBRACE && token.kind != EOF) {
       
  3774             if (token.kind == SEMI) {
       
  3775                 accept(SEMI);
       
  3776                 wasSemi = true;
       
  3777                 if (token.kind == RBRACE || token.kind == EOF) break;
       
  3778             }
       
  3779             EnumeratorEstimate memberType = estimateEnumeratorOrMember(enumName);
       
  3780             if (memberType == EnumeratorEstimate.UNKNOWN) {
       
  3781                 memberType = wasSemi ? EnumeratorEstimate.MEMBER
       
  3782                                      : EnumeratorEstimate.ENUMERATOR;
       
  3783             }
       
  3784             if (memberType == EnumeratorEstimate.ENUMERATOR) {
       
  3785                 wasError = false;
       
  3786                 if (wasSemi && !hasStructuralErrors) {
       
  3787                     reportSyntaxError(token.pos, Errors.EnumConstantNotExpected);
       
  3788                     hasStructuralErrors = true;
       
  3789                 }
  3766                 defs.append(enumeratorDeclaration(enumName));
  3790                 defs.append(enumeratorDeclaration(enumName));
  3767             }
  3791                 if (token.pos <= endPosTable.errorEndPos) {
  3768             if (token.kind != SEMI && token.kind != RBRACE) {
  3792                     // error recovery
  3769                 defs.append(syntaxError(token.pos, Errors.Expected3(COMMA, RBRACE, SEMI)));
  3793                    skip(false, true, true, false);
  3770                 nextToken();
  3794                 } else {
  3771             }
  3795                     if (token.kind != RBRACE && token.kind != SEMI && token.kind != EOF) {
  3772         }
  3796                         if (token.kind == COMMA) {
  3773         if (token.kind == SEMI) {
  3797                             nextToken();
  3774             nextToken();
  3798                         } else {
  3775             while (token.kind != RBRACE && token.kind != EOF) {
  3799                             setErrorEndPos(token.pos);
       
  3800                             reportSyntaxError(S.prevToken().endPos,
       
  3801                                               Errors.Expected3(COMMA, RBRACE, SEMI));
       
  3802                             wasError = true;
       
  3803                         }
       
  3804                     }
       
  3805                 }
       
  3806             } else {
       
  3807                 if (!wasSemi && !hasStructuralErrors && !wasError) {
       
  3808                     reportSyntaxError(token.pos, Errors.EnumConstantExpected);
       
  3809                     hasStructuralErrors = true;
       
  3810                 }
       
  3811                 wasError = false;
  3776                 defs.appendList(classOrInterfaceBodyDeclaration(enumName,
  3812                 defs.appendList(classOrInterfaceBodyDeclaration(enumName,
  3777                                                                 false));
  3813                                                                 false));
  3778                 if (token.pos <= endPosTable.errorEndPos) {
  3814                 if (token.pos <= endPosTable.errorEndPos) {
  3779                     // error recovery
  3815                     // error recovery
  3780                    skip(false, true, true, false);
  3816                    skip(false, true, true, false);
  3781                 }
  3817                 }
  3782             }
  3818             }
  3783         }
  3819         }
  3784         accept(RBRACE);
  3820         accept(RBRACE);
  3785         return defs.toList();
  3821         return defs.toList();
       
  3822     }
       
  3823 
       
  3824     private EnumeratorEstimate estimateEnumeratorOrMember(Name enumName) {
       
  3825         if (token.kind == TokenKind.IDENTIFIER && token.name() != enumName) {
       
  3826             Token next = S.token(1);
       
  3827             switch (next.kind) {
       
  3828                 case LPAREN: case LBRACE: case COMMA: case SEMI:
       
  3829                     return EnumeratorEstimate.ENUMERATOR;
       
  3830             }
       
  3831         }
       
  3832         switch (token.kind) {
       
  3833             case IDENTIFIER: case MONKEYS_AT: case LT:
       
  3834                 return EnumeratorEstimate.UNKNOWN;
       
  3835             default:
       
  3836                 return EnumeratorEstimate.MEMBER;
       
  3837         }
       
  3838     }
       
  3839 
       
  3840     private enum EnumeratorEstimate {
       
  3841         ENUMERATOR,
       
  3842         MEMBER,
       
  3843         UNKNOWN;
  3786     }
  3844     }
  3787 
  3845 
  3788     /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
  3846     /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
  3789      */
  3847      */
  3790     JCTree enumeratorDeclaration(Name enumName) {
  3848     JCTree enumeratorDeclaration(Name enumName) {