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