diff -r 6dc8616cea9b -r 19d6ba779759 src/share/classes/com/sun/tools/javac/parser/JavacParser.java --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Nov 06 18:41:56 2012 -0800 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Nov 05 16:26:09 2012 +0000 @@ -88,6 +88,15 @@ /** End position mappings container */ private final AbstractEndPosTable endPosTable; + interface ErrorRecoveryAction { + JCTree doRecover(JavacParser parser); + } + + enum BasicErrorRecoveryAction implements ErrorRecoveryAction { + BLOCK_STMT {public JCTree doRecover(JavacParser parser) { return parser.parseStatementAsBlock(); }}, + CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }} + } + /** Construct a parser from a given scanner, tree factory and log. */ protected JavacParser(ParserFactory fac, @@ -2102,11 +2111,15 @@ nextToken(); return toP(F.at(pos).Skip()); case ELSE: - return toP(F.Exec(syntaxError("else.without.if"))); + int elsePos = token.pos; + nextToken(); + return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if"); case FINALLY: - return toP(F.Exec(syntaxError("finally.without.try"))); + int finallyPos = token.pos; + nextToken(); + return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try"); case CATCH: - return toP(F.Exec(syntaxError("catch.without.try"))); + return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try"); case ASSERT: { if (allowAsserts && token.kind == ASSERT) { nextToken(); @@ -2139,6 +2152,13 @@ } } + private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) { + int errPos = S.errPos(); + JCTree stm = action.doRecover(this); + S.errPos(errPos); + return toP(F.Exec(syntaxError(startPos, List.of(stm), key))); + } + /** CatchClause = CATCH "(" FormalParameter ")" Block */ protected JCCatch catchClause() {