< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

Print this page
rev 60625 : 8237041: AssertionError in parsing
Summary: Avoid parser crash for deeply nested classes without closing braces, improve error recovery for classes without an opening brace.
Reviewed-by: TBD


 427      */
 428     protected void reportSyntaxError(int pos, Error errorKey) {
 429         JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
 430         reportSyntaxError(diag, errorKey);
 431     }
 432 
 433     /**
 434      * Report a syntax error using the given DiagnosticPosition object and
 435      * arguments, unless one was already reported at the same position.
 436      */
 437     protected void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, Error errorKey) {
 438         int pos = diagPos.getPreferredPosition();
 439         if (pos > S.errPos() || pos == Position.NOPOS) {
 440             if (token.kind == EOF) {
 441                 log.error(DiagnosticFlag.SYNTAX, diagPos, Errors.PrematureEof);
 442             } else {
 443                 log.error(DiagnosticFlag.SYNTAX, diagPos, errorKey);
 444             }
 445         }
 446         S.errPos(pos);
 447         if (token.pos == errorPos) {
 448             //check for a possible infinite loop in parsing:
 449             Assert.check(count++ < RECOVERY_THRESHOLD);
 450         } else {
 451             count = 0;
 452             errorPos = token.pos;
 453         }
 454     }
 455 
 456     /** If next input token matches given token, skip it, otherwise report
 457      *  an error.
 458      */
 459     public void accept(TokenKind tk) {
 460         accept(tk, Errors::Expected);
 461     }
 462 
 463     /** If next input token matches given token, skip it, otherwise report
 464      *  an error.
 465      */
 466     public void accept(TokenKind tk, Function<TokenKind, Error> errorProvider) {
 467         if (token.kind == tk) {


4031     List<JCExpression> typeList() {
4032         ListBuffer<JCExpression> ts = new ListBuffer<>();
4033         ts.append(parseType());
4034         while (token.kind == COMMA) {
4035             nextToken();
4036             ts.append(parseType());
4037         }
4038         return ts.toList();
4039     }
4040 
4041     /** ClassBody     = "{" {ClassBodyDeclaration} "}"
4042      *  InterfaceBody = "{" {InterfaceBodyDeclaration} "}"
4043      */
4044     List<JCTree> classInterfaceOrRecordBody(Name className, boolean isInterface, boolean isRecord) {
4045         accept(LBRACE);
4046         if (token.pos <= endPosTable.errorEndPos) {
4047             // error recovery
4048             skip(false, true, false, false);
4049             if (token.kind == LBRACE)
4050                 nextToken();


4051         }
4052         ListBuffer<JCTree> defs = new ListBuffer<>();
4053         while (token.kind != RBRACE && token.kind != EOF) {
4054             defs.appendList(classOrInterfaceOrRecordBodyDeclaration(className, isInterface, isRecord));
4055             if (token.pos <= endPosTable.errorEndPos) {
4056                // error recovery
4057                skip(false, true, true, false);
4058            }
4059         }
4060         accept(RBRACE);
4061         return defs.toList();
4062     }
4063 
4064     /** ClassBodyDeclaration =
4065      *      ";"
4066      *    | [STATIC] Block
4067      *    | ModifiersOpt
4068      *      ( Type Ident
4069      *        ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
4070      *      | VOID Ident VoidMethodDeclaratorRest




 427      */
 428     protected void reportSyntaxError(int pos, Error errorKey) {
 429         JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
 430         reportSyntaxError(diag, errorKey);
 431     }
 432 
 433     /**
 434      * Report a syntax error using the given DiagnosticPosition object and
 435      * arguments, unless one was already reported at the same position.
 436      */
 437     protected void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, Error errorKey) {
 438         int pos = diagPos.getPreferredPosition();
 439         if (pos > S.errPos() || pos == Position.NOPOS) {
 440             if (token.kind == EOF) {
 441                 log.error(DiagnosticFlag.SYNTAX, diagPos, Errors.PrematureEof);
 442             } else {
 443                 log.error(DiagnosticFlag.SYNTAX, diagPos, errorKey);
 444             }
 445         }
 446         S.errPos(pos);
 447         if (token.pos == errorPos && token.kind != EOF) {
 448             //check for a possible infinite loop in parsing:
 449             Assert.check(count++ < RECOVERY_THRESHOLD);
 450         } else {
 451             count = 0;
 452             errorPos = token.pos;
 453         }
 454     }
 455 
 456     /** If next input token matches given token, skip it, otherwise report
 457      *  an error.
 458      */
 459     public void accept(TokenKind tk) {
 460         accept(tk, Errors::Expected);
 461     }
 462 
 463     /** If next input token matches given token, skip it, otherwise report
 464      *  an error.
 465      */
 466     public void accept(TokenKind tk, Function<TokenKind, Error> errorProvider) {
 467         if (token.kind == tk) {


4031     List<JCExpression> typeList() {
4032         ListBuffer<JCExpression> ts = new ListBuffer<>();
4033         ts.append(parseType());
4034         while (token.kind == COMMA) {
4035             nextToken();
4036             ts.append(parseType());
4037         }
4038         return ts.toList();
4039     }
4040 
4041     /** ClassBody     = "{" {ClassBodyDeclaration} "}"
4042      *  InterfaceBody = "{" {InterfaceBodyDeclaration} "}"
4043      */
4044     List<JCTree> classInterfaceOrRecordBody(Name className, boolean isInterface, boolean isRecord) {
4045         accept(LBRACE);
4046         if (token.pos <= endPosTable.errorEndPos) {
4047             // error recovery
4048             skip(false, true, false, false);
4049             if (token.kind == LBRACE)
4050                 nextToken();
4051             else
4052                 return List.nil();
4053         }
4054         ListBuffer<JCTree> defs = new ListBuffer<>();
4055         while (token.kind != RBRACE && token.kind != EOF) {
4056             defs.appendList(classOrInterfaceOrRecordBodyDeclaration(className, isInterface, isRecord));
4057             if (token.pos <= endPosTable.errorEndPos) {
4058                // error recovery
4059                skip(false, true, true, false);
4060            }
4061         }
4062         accept(RBRACE);
4063         return defs.toList();
4064     }
4065 
4066     /** ClassBodyDeclaration =
4067      *      ";"
4068      *    | [STATIC] Block
4069      *    | ModifiersOpt
4070      *      ( Type Ident
4071      *        ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
4072      *      | VOID Ident VoidMethodDeclaratorRest


< prev index next >