Fix generic usage

This commit is contained in:
Sam Harwell 2012-02-15 10:50:37 -06:00
parent 2d2134eb6b
commit 5ab082967f
1 changed files with 28 additions and 18 deletions

View File

@ -63,7 +63,7 @@ import java.util.List;
* satisfy the superclass interface.
*/
public class ParserRuleContext<Symbol> extends RuleContext {
public static final ParserRuleContext EMPTY = new ParserRuleContext();
public static final ParserRuleContext<?> EMPTY = new ParserRuleContext<Object>();
/** If we are debugging or building a parse tree for a visitor,
* we need to track all of the tokens and rule invocations associated
@ -179,13 +179,15 @@ public class ParserRuleContext<Symbol> extends RuleContext {
return children!=null ? children.get(i) : null;
}
public Object getChild(Class ctxType, int i) {
public <T> T getChild(Class<? extends T> ctxType, int i) {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
int j = -1; // what element have we found with ctxType?
for (Object o : children) {
if ( ctxType.isAssignableFrom(o.getClass()) ) {
if ( ctxType.isInstance(o) ) {
j = j+1;
if ( j == i ) return o;
if ( j == i ) {
return ctxType.cast(o);
}
}
}
return null;
@ -195,11 +197,16 @@ public class ParserRuleContext<Symbol> extends RuleContext {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
int j = -1; // what token with ttype have we found?
for (Object o : children) {
if ( o instanceof TerminalNode ) {
TerminalNode<Token> tnode = (TerminalNode<Token>)o;
if ( tnode.getSymbol().getType()==ttype ) {
if ( o instanceof TerminalNode<?> ) {
TerminalNode<?> tnode = (TerminalNode<?>)o;
if ( tnode.getSymbol() instanceof Token ) {
Token symbol = (Token)tnode.getSymbol();
if ( symbol.getType()==ttype ) {
j++;
if ( j == i ) return tnode.getSymbol();
if ( j == i ) {
return symbol;
}
}
}
}
}
@ -218,17 +225,20 @@ public class ParserRuleContext<Symbol> extends RuleContext {
return tokens;
}
public ParserRuleContext getRuleContext(Class ctxType, int i) {
return (ParserRuleContext)getChild(ctxType, i);
public <T extends ParserRuleContext<?>> T getRuleContext(Class<? extends T> ctxType, int i) {
return getChild(ctxType, i);
}
public List<? extends ParserRuleContext> getRuleContexts(Class ctxType) {
public <T extends ParserRuleContext<?>> List<? extends T> getRuleContexts(Class<? extends T> ctxType) {
if ( children==null ) throw new UnsupportedOperationException("there are no children");
List<ParserRuleContext> contexts = null;
List<T> contexts = null;
for (Object o : children) {
if ( o.getClass().isInstance(ctxType) ) {
if ( contexts==null ) contexts = new ArrayList<ParserRuleContext>();
contexts.add((ParserRuleContext)o);
if ( ctxType.isInstance(o) ) {
if ( contexts==null ) {
contexts = new ArrayList<T>();
}
contexts.add(ctxType.cast(o));
}
}
return contexts;
@ -247,7 +257,7 @@ public class ParserRuleContext<Symbol> extends RuleContext {
public String toString(@NotNull Recognizer<?,?> recog, RuleContext stop) {
if ( recog==null ) return super.toString(recog, stop);
StringBuilder buf = new StringBuilder();
ParserRuleContext p = this;
ParserRuleContext<?> p = this;
buf.append("[");
while ( p != null && p != stop ) {
ATN atn = recog.getATN();
@ -258,7 +268,7 @@ public class ParserRuleContext<Symbol> extends RuleContext {
// ATNState invoker = atn.states.get(ctx.invokingState);
// RuleTransition rt = (RuleTransition)invoker.transition(0);
// buf.append(recog.getRuleNames()[rt.target.ruleIndex]);
p = (ParserRuleContext)p.parent;
p = (ParserRuleContext<?>)p.parent;
}
buf.append("]");
return buf.toString();