Set stop token even when RecognitionException occurs. Add ParserRuleContext.exception field to hold exception if one occurs. Resolves antlr/antlr4#49.

This commit is contained in:
Sam Harwell 2012-03-25 07:43:54 -05:00
parent 542e700644
commit bdda174af6
3 changed files with 10 additions and 2 deletions

View File

@ -386,6 +386,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
} }
public void exitRule() { public void exitRule() {
_ctx.stop = _input.LT(-1);
// trigger event on _ctx, before it reverts to parent // trigger event on _ctx, before it reverts to parent
if ( _parseListeners != null) triggerExitRuleEvent(); if ( _parseListeners != null) triggerExitRuleEvent();
_ctx = (ParserRuleContext<Token>)_ctx.parent; _ctx = (ParserRuleContext<Token>)_ctx.parent;
@ -411,6 +412,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
} }
public void unrollRecursionContexts(ParserRuleContext<Token> _parentctx) { public void unrollRecursionContexts(ParserRuleContext<Token> _parentctx) {
_ctx.stop = _input.LT(-1);
ParserRuleContext<Token> retctx = _ctx; // save current ctx (return value) ParserRuleContext<Token> retctx = _ctx; // save current ctx (return value)
// unroll so _ctx is as it was before call to recursive method // unroll so _ctx is as it was before call to recursive method

View File

@ -109,6 +109,12 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
/** Set during parsing to identify which alt of rule parser is in. */ /** Set during parsing to identify which alt of rule parser is in. */
public int altNum; public int altNum;
/**
* The exception which forced this rule to return. If the rule successfully
* completed, this is {@code null}.
*/
public RecognitionException exception;
public ParserRuleContext() { } public ParserRuleContext() { }
/** COPY a ctx (I'm deliberately not using copy constructor) */ /** COPY a ctx (I'm deliberately not using copy constructor) */

View File

@ -229,11 +229,11 @@ RuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAc
int _alt; int _alt;
<endif> <endif>
<code> <code>
_localctx.stop = _input.LT(-1);
<postamble; separator="\n"> <postamble; separator="\n">
<namedActions.after> <namedActions.after>
} }
catch (RecognitionException re) { catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re); _errHandler.reportError(this, re);
_errHandler.recover(this, re); _errHandler.recover(this, re);
} }
@ -265,11 +265,11 @@ LeftRecursiveRuleFunction(currentRule,code,locals,ruleCtx,altLabelCtxs,
int _alt; int _alt;
<endif> <endif>
<code> <code>
_localctx.stop = _input.LT(-1);
<postamble; separator="\n"> <postamble; separator="\n">
<namedActions.after> <namedActions.after>
} }
catch (RecognitionException re) { catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re); _errHandler.reportError(this, re);
_errHandler.recover(this, re); _errHandler.recover(this, re);
} }