From cdb420fddeb7d1a9ac3b89edb7b233c4a6d7c64d Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Tue, 14 Feb 2012 15:01:03 -0800 Subject: [PATCH] got implementation of getters in! Fixes #8 --- .../antlr/v4/runtime/ParserRuleContext.java | 56 +++++++++++++++---- .../src/org/antlr/v4/runtime/RuleContext.java | 17 ------ .../v4/tool/templates/codegen/Java/Java.stg | 30 ++++++++-- 3 files changed, 69 insertions(+), 34 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java index c51775dd2..304ab24f0 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java @@ -131,17 +131,6 @@ public class ParserRuleContext 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 listener) { } @@ -190,6 +179,51 @@ public class ParserRuleContext 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 getTokens(int ttype) { + if ( children==null ) throw new UnsupportedOperationException("there are no children"); + List tokens = null; + for (Object o : children) { + if ( o instanceof Token ) { + if ( tokens==null ) tokens = new ArrayList(); + tokens.add((Token)o); + } + } + return tokens; + } + + public ParserRuleContext getRuleContext(Class ctxType, int i) { + return (ParserRuleContext)getChild(ctxType, i); + } + + public List getRuleContexts(Class ctxType) { + if ( children==null ) throw new UnsupportedOperationException("there are no children"); + List contexts = null; + for (Object o : children) { + if ( o.getClass().isInstance(ctxType) ) { + if ( contexts==null ) contexts = new ArrayList(); + contexts.add((ParserRuleContext)o); + } + } + return contexts; + } + @Override public int getChildCount() { return children!=null ? children.size() : 0; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java index 2d0113178..b762eb516 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java +++ b/runtime/Java/src/org/antlr/v4/runtime/RuleContext.java @@ -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. } diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index e315672a1..af8037353 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -493,12 +493,30 @@ TokenListDecl(t) ::= "public List\ = new ArrayList\();" RuleContextDecl(r) ::= "public ;" RuleContextListDecl(rdecl) ::= "public List\<> = new ArrayList\<>();" -ContextTokenGetterDecl(t) ::= "public Token () { return null; }" -ContextTokenListGetterDecl(t) ::= "public List\ () { return null; }" -ContextTokenListIndexedGetterDecl(t) ::= "public Token (int i) { return null; }" -ContextRuleGetterDecl(r) ::= "public () { return null; }" -ContextRuleListGetterDecl(r) ::= "public List\<> () { return null; }" -ContextRuleListIndexedGetterDecl(r) ::= "public (int i) { return null; }" +ContextTokenGetterDecl(t) ::= + "public Token () { return getToken(., 0); }" +ContextTokenListGetterDecl(t) ::= + "public List\ () { return getTokens(.); }" +ContextTokenListIndexedGetterDecl(t) ::= << +public Token (int i) { + return getToken(., i); +} +>> +ContextRuleGetterDecl(r) ::= << +public () { + return ()getRuleContext(.class,0); +} +>> +ContextRuleListGetterDecl(r) ::= << +public List\<\> () { + return (List\<\>)getRuleContexts(.class); +} +>> +ContextRuleListIndexedGetterDecl(r) ::= << +public (int i) { + return ()getRuleContext(.class,i); +} +>> LexerRuleContext() ::= "RuleContext"