got implementation of getters in! Fixes #8

This commit is contained in:
Terence Parr 2012-02-14 15:01:03 -08:00
parent b8f50d46e7
commit cdb420fdde
3 changed files with 69 additions and 34 deletions

View File

@ -131,17 +131,6 @@ public class ParserRuleContext<Symbol> extends RuleContext {
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
public void enterRule(ParseTreeListener<Symbol> listener) { }
@ -190,6 +179,51 @@ public class ParserRuleContext<Symbol> extends RuleContext {
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
public int getChildCount() { return children!=null ? children.size() : 0; }

View File

@ -80,16 +80,6 @@ public class RuleContext implements ParseTree.RuleNode {
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) {
this.parent = parent;
//if ( parent!=null ) System.out.println("invoke "+stateNumber+" from "+parent);
@ -103,13 +93,6 @@ public class RuleContext implements ParseTree.RuleNode {
@Override
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.
}

View File

@ -493,12 +493,30 @@ TokenListDecl(t) ::= "public List\<Token> <t.name> = new ArrayList\<Token>();"
RuleContextDecl(r) ::= "public <r.ctxName> <r.name>;"
RuleContextListDecl(rdecl) ::= "public List\<<rdecl.ctxName>> <rdecl.name> = new ArrayList\<<rdecl.ctxName>>();"
ContextTokenGetterDecl(t) ::= "public Token <t.name>() { return null; }"
ContextTokenListGetterDecl(t) ::= "public List\<Token> <t.name>() { return null; }"
ContextTokenListIndexedGetterDecl(t) ::= "public Token <t.name>(int i) { return null; }"
ContextRuleGetterDecl(r) ::= "public <r.ctxName> <r.name>() { return null; }"
ContextRuleListGetterDecl(r) ::= "public List\<<r.ctxName>> <r.name>() { return null; }"
ContextRuleListIndexedGetterDecl(r) ::= "public <r.ctxName> <r.name>(int i) { return null; }"
ContextTokenGetterDecl(t) ::=
"public Token <t.name>() { return getToken(<parser.name>.<t.name>, 0); }"
ContextTokenListGetterDecl(t) ::=
"public List\<Token> <t.name>() { return getTokens(<parser.name>.<t.name>); }"
ContextTokenListIndexedGetterDecl(t) ::= <<
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"