forked from jasder/antlr
got implementation of getters in! Fixes #8
This commit is contained in:
parent
b8f50d46e7
commit
cdb420fdde
|
@ -131,17 +131,6 @@ public class ParserRuleContext<Symbol> extends RuleContext {
|
||||||
this(parent, parent!=null ? parent.s : -1 /* invoking state */, stateNumber);
|
this(parent, parent!=null ? parent.s : -1 /* invoking state */, stateNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
|
||||||
// public int hashCode() {
|
|
||||||
// return super.hashCode() + s;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public boolean equals(Object o) {
|
|
||||||
// if ( !super.equals(o) ) return false;
|
|
||||||
// return s != ((RuleContext)o).s; // must be parsing the same location in the ATN
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Double dispatch methods
|
// Double dispatch methods
|
||||||
|
|
||||||
public void enterRule(ParseTreeListener<Symbol> listener) { }
|
public void enterRule(ParseTreeListener<Symbol> listener) { }
|
||||||
|
@ -190,6 +179,51 @@ 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) {
|
||||||
|
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 ( o.getClass().isInstance(ctxType) ) {
|
||||||
|
j++;
|
||||||
|
if ( j == i ) return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Token getToken(int ttype, int i) {
|
||||||
|
if ( children==null ) throw new UnsupportedOperationException("there are no children");
|
||||||
|
return (Token)getChild(Token.class, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Token> getTokens(int ttype) {
|
||||||
|
if ( children==null ) throw new UnsupportedOperationException("there are no children");
|
||||||
|
List<Token> tokens = null;
|
||||||
|
for (Object o : children) {
|
||||||
|
if ( o instanceof Token ) {
|
||||||
|
if ( tokens==null ) tokens = new ArrayList<Token>();
|
||||||
|
tokens.add((Token)o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParserRuleContext getRuleContext(Class ctxType, int i) {
|
||||||
|
return (ParserRuleContext)getChild(ctxType, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<? extends ParserRuleContext> getRuleContexts(Class ctxType) {
|
||||||
|
if ( children==null ) throw new UnsupportedOperationException("there are no children");
|
||||||
|
List<ParserRuleContext> contexts = null;
|
||||||
|
for (Object o : children) {
|
||||||
|
if ( o.getClass().isInstance(ctxType) ) {
|
||||||
|
if ( contexts==null ) contexts = new ArrayList<ParserRuleContext>();
|
||||||
|
contexts.add((ParserRuleContext)o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return contexts;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getChildCount() { return children!=null ? children.size() : 0; }
|
public int getChildCount() { return children!=null ? children.size() : 0; }
|
||||||
|
|
||||||
|
|
|
@ -80,16 +80,6 @@ public class RuleContext implements ParseTree.RuleNode {
|
||||||
|
|
||||||
public RuleContext() {}
|
public RuleContext() {}
|
||||||
|
|
||||||
// public RuleContext(RuleContext parent) {
|
|
||||||
// this(parent, -1);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public RuleContext(RuleContext parent, int stateNumber) {
|
|
||||||
// // capture state that called us as we create this context; use later for
|
|
||||||
// // return state in closure
|
|
||||||
// this(parent, parent != null ? parent.s : -1, stateNumber);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public RuleContext(RuleContext parent, int invokingState) {
|
public RuleContext(RuleContext parent, int invokingState) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
//if ( parent!=null ) System.out.println("invoke "+stateNumber+" from "+parent);
|
//if ( parent!=null ) System.out.println("invoke "+stateNumber+" from "+parent);
|
||||||
|
@ -103,13 +93,6 @@ public class RuleContext implements ParseTree.RuleNode {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
// int h = 0;
|
|
||||||
// RuleContext p = this;
|
|
||||||
// while ( p!=null ) {
|
|
||||||
// h += p.stateNumber;
|
|
||||||
// p = p.parent;
|
|
||||||
// }
|
|
||||||
// return h;
|
|
||||||
return cachedHashCode; // works with tests; don't recompute.
|
return cachedHashCode; // works with tests; don't recompute.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -493,12 +493,30 @@ TokenListDecl(t) ::= "public List\<Token> <t.name> = new ArrayList\<Token>();"
|
||||||
RuleContextDecl(r) ::= "public <r.ctxName> <r.name>;"
|
RuleContextDecl(r) ::= "public <r.ctxName> <r.name>;"
|
||||||
RuleContextListDecl(rdecl) ::= "public List\<<rdecl.ctxName>> <rdecl.name> = new ArrayList\<<rdecl.ctxName>>();"
|
RuleContextListDecl(rdecl) ::= "public List\<<rdecl.ctxName>> <rdecl.name> = new ArrayList\<<rdecl.ctxName>>();"
|
||||||
|
|
||||||
ContextTokenGetterDecl(t) ::= "public Token <t.name>() { return null; }"
|
ContextTokenGetterDecl(t) ::=
|
||||||
ContextTokenListGetterDecl(t) ::= "public List\<Token> <t.name>() { return null; }"
|
"public Token <t.name>() { return getToken(<parser.name>.<t.name>, 0); }"
|
||||||
ContextTokenListIndexedGetterDecl(t) ::= "public Token <t.name>(int i) { return null; }"
|
ContextTokenListGetterDecl(t) ::=
|
||||||
ContextRuleGetterDecl(r) ::= "public <r.ctxName> <r.name>() { return null; }"
|
"public List\<Token> <t.name>() { return getTokens(<parser.name>.<t.name>); }"
|
||||||
ContextRuleListGetterDecl(r) ::= "public List\<<r.ctxName>> <r.name>() { return null; }"
|
ContextTokenListIndexedGetterDecl(t) ::= <<
|
||||||
ContextRuleListIndexedGetterDecl(r) ::= "public <r.ctxName> <r.name>(int i) { return null; }"
|
public Token <t.name>(int i) {
|
||||||
|
return getToken(<parser.name>.<t.name>, i);
|
||||||
|
}
|
||||||
|
>>
|
||||||
|
ContextRuleGetterDecl(r) ::= <<
|
||||||
|
public <r.ctxName> <r.name>() {
|
||||||
|
return (<r.ctxName>)getRuleContext(<r.ctxName>.class,0);
|
||||||
|
}
|
||||||
|
>>
|
||||||
|
ContextRuleListGetterDecl(r) ::= <<
|
||||||
|
public List\<<r.ctxName>\> <r.name>() {
|
||||||
|
return (List\<<r.ctxName>\>)getRuleContexts(<r.ctxName>.class);
|
||||||
|
}
|
||||||
|
>>
|
||||||
|
ContextRuleListIndexedGetterDecl(r) ::= <<
|
||||||
|
public <r.ctxName> <r.name>(int i) {
|
||||||
|
return (<r.ctxName>)getRuleContext(<r.ctxName>.class,i);
|
||||||
|
}
|
||||||
|
>>
|
||||||
|
|
||||||
LexerRuleContext() ::= "RuleContext"
|
LexerRuleContext() ::= "RuleContext"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue