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

changeset 1400
19d6ba779759
parent 1374
c002fdee76fd
child 1409
33abf479f202
equal deleted inserted replaced
1399:6dc8616cea9b 1400:19d6ba779759
85 /** The name table. */ 85 /** The name table. */
86 private Names names; 86 private Names names;
87 87
88 /** End position mappings container */ 88 /** End position mappings container */
89 private final AbstractEndPosTable endPosTable; 89 private final AbstractEndPosTable endPosTable;
90
91 interface ErrorRecoveryAction {
92 JCTree doRecover(JavacParser parser);
93 }
94
95 enum BasicErrorRecoveryAction implements ErrorRecoveryAction {
96 BLOCK_STMT {public JCTree doRecover(JavacParser parser) { return parser.parseStatementAsBlock(); }},
97 CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }}
98 }
90 99
91 /** Construct a parser from a given scanner, tree factory and log. 100 /** Construct a parser from a given scanner, tree factory and log.
92 */ 101 */
93 protected JavacParser(ParserFactory fac, 102 protected JavacParser(ParserFactory fac,
94 Lexer S, 103 Lexer S,
2100 } 2109 }
2101 case SEMI: 2110 case SEMI:
2102 nextToken(); 2111 nextToken();
2103 return toP(F.at(pos).Skip()); 2112 return toP(F.at(pos).Skip());
2104 case ELSE: 2113 case ELSE:
2105 return toP(F.Exec(syntaxError("else.without.if"))); 2114 int elsePos = token.pos;
2115 nextToken();
2116 return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if");
2106 case FINALLY: 2117 case FINALLY:
2107 return toP(F.Exec(syntaxError("finally.without.try"))); 2118 int finallyPos = token.pos;
2119 nextToken();
2120 return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try");
2108 case CATCH: 2121 case CATCH:
2109 return toP(F.Exec(syntaxError("catch.without.try"))); 2122 return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try");
2110 case ASSERT: { 2123 case ASSERT: {
2111 if (allowAsserts && token.kind == ASSERT) { 2124 if (allowAsserts && token.kind == ASSERT) {
2112 nextToken(); 2125 nextToken();
2113 JCExpression assertion = parseExpression(); 2126 JCExpression assertion = parseExpression();
2114 JCExpression message = null; 2127 JCExpression message = null;
2135 JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr))); 2148 JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr)));
2136 accept(SEMI); 2149 accept(SEMI);
2137 return stat; 2150 return stat;
2138 } 2151 }
2139 } 2152 }
2153 }
2154
2155 private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) {
2156 int errPos = S.errPos();
2157 JCTree stm = action.doRecover(this);
2158 S.errPos(errPos);
2159 return toP(F.Exec(syntaxError(startPos, List.<JCTree>of(stm), key)));
2140 } 2160 }
2141 2161
2142 /** CatchClause = CATCH "(" FormalParameter ")" Block 2162 /** CatchClause = CATCH "(" FormalParameter ")" Block
2143 */ 2163 */
2144 protected JCCatch catchClause() { 2164 protected JCCatch catchClause() {

mercurial