Override getRuleIndex() in generated context objects so ParserRuleContext.ruleIndex field can be removed

This commit is contained in:
Sam Harwell 2012-03-25 07:53:59 -05:00
parent 542e700644
commit df2e24a228
3 changed files with 4 additions and 12 deletions

View File

@ -45,18 +45,18 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
public class TraceListener implements ParseListener<Token> {
@Override
public void enterNonLRRule(ParserRuleContext<Token> ctx) {
System.out.println("enter " + getRuleNames()[ctx.ruleIndex] + ", LT(1)=" + _input.LT(1).getText());
System.out.println("enter " + getRuleNames()[ctx.getRuleIndex()] + ", LT(1)=" + _input.LT(1).getText());
}
@Override
public void exitEveryRule(ParserRuleContext<Token> ctx) {
System.out.println("exit "+getRuleNames()[ctx.ruleIndex]+", LT(1)="+_input.LT(1).getText());
System.out.println("exit "+getRuleNames()[ctx.getRuleIndex()]+", LT(1)="+_input.LT(1).getText());
}
@Override
public void visitTerminal(ParserRuleContext<Token> parent, Token token) {
System.out.println("consume "+token+" rule "+
getRuleNames()[parent.ruleIndex]+
getRuleNames()[parent.getRuleIndex()]+
" alt="+parent.altNum);
}
}
@ -380,7 +380,6 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
public void enterRule(ParserRuleContext<Token> localctx, int ruleIndex) {
_ctx = localctx;
_ctx.start = _input.LT(1);
_ctx.ruleIndex = ruleIndex;
if (_buildParseTrees) addContextToParseTree();
if ( _parseListeners != null) triggerEnterRuleEvent();
}
@ -407,7 +406,6 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
public void pushNewRecursionContext(ParserRuleContext<Token> localctx, int ruleIndex) {
_ctx = localctx;
_ctx.start = _input.LT(1);
_ctx.ruleIndex = ruleIndex;
}
public void unrollRecursionContexts(ParserRuleContext<Token> _parentctx) {

View File

@ -103,9 +103,6 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
public Symbol start, stop;
/** Set during parsing to identify which rule parser is in. */
public int ruleIndex;
/** Set during parsing to identify which alt of rule parser is in. */
public int altNum;
@ -120,7 +117,6 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
this.start = ctx.start;
this.stop = ctx.stop;
this.ruleIndex = ctx.ruleIndex;
}
public ParserRuleContext(@Nullable ParserRuleContext<Symbol> parent, int invokingStateNumber, int stateNumber) {
@ -289,9 +285,6 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
@Override
public int getChildCount() { return children!=null ? children.size() : 0; }
@Override
public int getRuleIndex() { return ruleIndex; }
@Override
public Interval getSourceInterval() {
if ( start==null || stop==null ) return Interval.EMPTY;

View File

@ -608,6 +608,7 @@ public static class <struct.name> extends <superClass><if(interfaces)> implement
super(parent, state);
<struct.ctorAttrs:{a | this.<a.name> = <a.name>;}; separator="\n">
}
@Override public int getRuleIndex() { return RULE_<struct.derivedFromName>; }
<if(struct.provideCopyFrom)> <! don't need copy unless we have subclasses !>
public <struct.name>() { }
public void copyFrom(<struct.name> ctx) {