From 01020ee1bbaa50bb0feed3b6da4a1dd03c2e254a Mon Sep 17 00:00:00 2001 From: parrt Date: Sun, 4 Sep 2011 15:55:45 -0800 Subject: [PATCH] more cleanup [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9042] --- .../src/org/antlr/v4/runtime/tree/AST.java | 21 +- .../org/antlr/v4/runtime/tree/ASTAdaptor.java | 5 +- .../org/antlr/v4/runtime/tree/BaseAST.java | 132 +------------ .../org/antlr/v4/runtime/tree/CommonAST.java | 53 ----- .../v4/runtime/tree/CommonASTAdaptor.java | 2 +- ...deStream.java => CommonASTNodeStream.java} | 6 +- .../org/antlr/v4/runtime/tree/TreeFilter.java | 4 +- .../src/org/antlr/v4/runtime/tree/Trees.java | 184 ++++++++++++++++++ tool/test/org/antlr/v4/test/BaseTest.java | 4 +- .../org/antlr/v4/test/TestRewriteAST.java | 2 +- 10 files changed, 208 insertions(+), 205 deletions(-) rename runtime/Java/src/org/antlr/v4/runtime/tree/{CommonTreeNodeStream.java => CommonASTNodeStream.java} (96%) diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java index 6801d89d4..a44d1ba7a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/AST.java @@ -31,27 +31,8 @@ package org.antlr.v4.runtime.tree; import org.antlr.v4.runtime.Token; -import java.util.List; - -/** An abstract syntax tree built by ANTLR during a parcel or tree parse. */ +/** An abstract syntax tree built by ANTLR during a parse or tree parse. */ public interface AST extends SyntaxTree { - /** Is there is a node above with token type ttype? */ - public boolean hasAncestor(int ttype); - - /** Walk upwards and get first ancestor with this token type. */ - public AST getAncestor(int ttype); - - /** Return a list of all ancestors of this node. The first node of - * list is the root and the last is the parent of this node. - */ - public List getAncestors(); - - /** This node is what child index? 0..n-1 */ - public int getChildIndex(); - - /** Set the parent and child index values for all children */ - public void freshenParentAndChildIndexes(); - /** Indicates the node is a nil node but may still have children, meaning * the tree is a flat list. */ diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java index a0d0233fb..23ad673fb 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ASTAdaptor.java @@ -47,7 +47,10 @@ public interface ASTAdaptor { //public List getChildren(Object root); - /** Used to track elements to left of -> for use in rewrite */ + /** Used to track elements to left of -> for use in rewrite. These + * are some kind of trees, but we generically use Object + * for tree types in ANTLR. + */ public List createElementList(); // END new v4 stuff diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java index 80ead9e5a..de9dc6f9a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/BaseAST.java @@ -66,24 +66,14 @@ public abstract class BaseAST implements AST { /** Get the children internal List; note that if you directly mess with * the list, do so at your own risk. */ - public List getChildren() { - return children; - } + public List getChildren() { return children; } public AST getFirstChildWithType(int type) { - for (int i = 0; children!=null && i < children.size(); i++) { - AST t = (AST) children.get(i); - if ( t.getType()==type ) { - return t; - } - } - return null; + return Trees.getFirstChildWithType(this, type); } public int getChildCount() { - if ( children==null ) { - return 0; - } + if ( children==null ) return 0; return children.size(); } @@ -136,7 +126,7 @@ public abstract class BaseAST implements AST { } /** Add all elements of kids list as children of this node */ - public void addChildren(List kids) { + public void addChildren(List kids) { if ( kids==null ) return; for (int i = 0; i < kids.size(); i++) { BaseAST t = kids.get(i); @@ -148,7 +138,7 @@ public abstract class BaseAST implements AST { if ( t==null ) { return; } - if ( ((AST)t).isNil() ) { + if ( t.isNil() ) { throw new IllegalArgumentException("Can't set single child to a list"); } if ( children==null ) { @@ -185,7 +175,7 @@ public abstract class BaseAST implements AST { return killed; } - public boolean deleteChild(AST t) { + public boolean deleteChild(BaseAST t) { for (int i=0; i newChildren = null; - // normalize to a list of children to add: newChildren - if ( newTree.isNil() ) { - newChildren = newTree.children; - } - else { - newChildren = new ArrayList(1); - newChildren.add(newTree); - } - replacingWithHowMany = newChildren.size(); - int numNewChildren = newChildren.size(); - int delta = replacingHowMany - replacingWithHowMany; - // if same number of nodes, do direct replace - if ( delta == 0 ) { - int j = 0; // index into new children - for (int i=startChildIndex; i<=stopChildIndex; i++) { - BaseAST child = (BaseAST)newChildren.get(j); - children.set(i, child); - child.setParent(this); - child.setChildIndex(i); - j++; - } - } - else if ( delta > 0 ) { // fewer new nodes than there were - // set children and then delete extra - for (int j=0; j ancestors = new ArrayList(); - AST t = this; - t = t.getParent(); - while ( t!=null ) { - ancestors.add(0, t); // insert at start - t = t.getParent(); - } - return ancestors; - } + public List getAncestors() { return Trees.getAncestors(this); } /** Don't use standard tree printing mechanism since ASTs can have nil * root nodes. diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java index 014329fe4..07886bd49 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonAST.java @@ -33,8 +33,6 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.tree.gui.ASTViewer; -import java.util.Set; - /** A tree node that is wrapper for a Token object. */ public class CommonAST extends BaseAST { /** A single token is the payload */ @@ -130,63 +128,12 @@ public class CommonAST extends BaseAST { stopIndex = index; } - /** For every node in this subtree, make sure it's start/stop token's - * are set. Walk depth first, visit bottom up. Only updates nodes - * with at least one token index < 0. - */ - public void setUnknownTokenBoundaries() { - if ( children==null ) { - if ( startIndex<0 || stopIndex<0 ) { - startIndex = stopIndex = token.getTokenIndex(); - } - return; - } - for (int i=0; i=0 && stopIndex>=0 ) return; // already set - if ( children.size() > 0 ) { - CommonAST firstChild = (CommonAST)children.get(0); - CommonAST lastChild = (CommonAST)children.get(children.size()-1); - startIndex = firstChild.getTokenStartIndex(); - stopIndex = lastChild.getTokenStopIndex(); - } - } - // TODO: move to basetree when i settle on how runtime works public void inspect() { ASTViewer viewer = new ASTViewer(this); viewer.open(); } - // TODO: move to basetree when i settle on how runtime works - // TODO: don't include this node!! - // TODO: reuse other method - public CommonAST getFirstDescendantWithType(int type) { - if ( getType()==type ) return this; - if ( children==null ) return null; - for (Object c : children) { - CommonAST t = (CommonAST)c; - if ( t.getType()==type ) return t; - CommonAST d = t.getFirstDescendantWithType(type); - if ( d!=null ) return d; - } - return null; - } - - // TODO: don't include this node!! - public CommonAST getFirstDescendantWithType(Set types) { - if ( types.contains(getType()) ) return this; - if ( children==null ) return null; - for (Object c : children) { - CommonAST t = (CommonAST)c; - if ( types.contains(t.getType()) ) return t; - CommonAST d = t.getFirstDescendantWithType(types); - if ( d!=null ) return d; - } - return null; - } - public String toString() { if ( isNil() ) { return "nil"; diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java index 79565f7d3..1e9fb8c7d 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTAdaptor.java @@ -165,7 +165,7 @@ public class CommonASTAdaptor extends BaseASTAdaptor { public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) { if ( parent!=null ) { - ((CommonAST)parent).replaceChildren(startChildIndex, stopChildIndex, t); + Trees.replaceChildren((CommonAST)parent, startChildIndex, stopChildIndex, t); } } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonTreeNodeStream.java b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java similarity index 96% rename from runtime/Java/src/org/antlr/v4/runtime/tree/CommonTreeNodeStream.java rename to runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java index 88e21a9cf..37c577f58 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/CommonTreeNodeStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/CommonASTNodeStream.java @@ -34,7 +34,7 @@ import org.antlr.v4.runtime.misc.LookaheadStream; import java.util.Stack; -public class CommonTreeNodeStream extends LookaheadStream implements TreeNodeStream { +public class CommonASTNodeStream extends LookaheadStream implements TreeNodeStream { public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100; public static final int INITIAL_CALL_STACK_SIZE = 10; @@ -59,11 +59,11 @@ public class CommonTreeNodeStream extends LookaheadStream implements Tre /** Tracks tree depth. Level=0 means we're at root node level. */ protected int level = 0; - public CommonTreeNodeStream(Object tree) { + public CommonASTNodeStream(Object tree) { this(new CommonASTAdaptor(), tree); } - public CommonTreeNodeStream(ASTAdaptor adaptor, Object tree) { + public CommonASTNodeStream(ASTAdaptor adaptor, Object tree) { this.root = tree; this.adaptor = adaptor; it = new TreeIterator(adaptor,root); diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java index 548869595..eaed002ab 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/TreeFilter.java @@ -93,8 +93,8 @@ public class TreeFilter extends TreeParser { if ( t==null ) return; try { // share TreeParser object but not parsing-related state - input = new CommonTreeNodeStream(originalAdaptor, t); - ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream); + input = new CommonASTNodeStream(originalAdaptor, t); + ((CommonASTNodeStream)input).setTokenStream(originalTokenStream); whichRule.rule(); } catch (RecognitionException e) { ; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java index 89e431f71..398d05778 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java @@ -29,6 +29,8 @@ package org.antlr.v4.runtime.tree; +import java.util.*; + /** A set of utility routines useful for all kinds of ANTLR trees */ public class Trees { /** Print out a whole tree in LISP form. toString is used on the @@ -48,4 +50,186 @@ public class Trees { return buf.toString(); } + /** Walk upwards and get first ancestor with this token type. */ + public static AST getAncestor(AST t, int ttype) { + t = t.getParent(); + while ( t!=null ) { + if ( t.getType()==ttype ) return t; + t = t.getParent(); + } + return null; + } + + /** Return a list of all ancestors of this node. The first node of + * list is the root and the last is the parent of this node. + */ + public static List getAncestors(Tree t) { + if ( t.getParent()==null ) return null; + List ancestors = new ArrayList(); + t = t.getParent(); + while ( t!=null ) { + ancestors.add(0, t); // insert at start + t = t.getParent(); + } + return ancestors; + } + + public static AST getFirstChildWithType(AST t, int type) { + for (int i = 0; i newChildren = null; + // normalize to a list of children to add: newChildren + if ( newTree.isNil() ) { + newChildren = newTree.children; + } + else { + newChildren = new ArrayList(1); + newChildren.add(newTree); + } + replacingWithHowMany = newChildren.size(); + int numNewChildren = newChildren.size(); + int delta = replacingHowMany - replacingWithHowMany; + // if same number of nodes, do direct replace + if ( delta == 0 ) { + int j = 0; // index into new children + for (int i=startChildIndex; i<=stopChildIndex; i++) { + BaseAST child = (BaseAST)newChildren.get(j); + tree.setChild(i, child); + child.setParent(tree); + child.setChildIndex(i); + j++; + } + } + else if ( delta > 0 ) { // fewer new nodes than there were + // set children and then delete extra + for (int j=0; j\n" + " if ( r.tree!=null ) {\n" + " System.out.println(((Tree)r.tree).toStringTree());\n" + - " ((CommonAST)r.tree).sanityCheckParentAndChildIndexes();\n" + + " Trees.sanityCheckParentAndChildIndexes((CommonAST)r.tree);\n" + " }\n" + " \n" + " CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);\n" + @@ -1002,7 +1002,7 @@ public abstract class BaseTest { " TokenRewriteStream tokens = new TokenRewriteStream(lex);\n" + " \n"+ " ParserRuleContext r = parser.();\n" + - " ((CommonAST)r.tree).sanityCheckParentAndChildIndexes();\n" + + " Trees.sanityCheckParentAndChildIndexes((CommonAST)r.tree);\n" + " CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);\n" + " nodes.setTokenStream(tokens);\n" + " walker = new (nodes);\n" + diff --git a/tool/test/org/antlr/v4/test/TestRewriteAST.java b/tool/test/org/antlr/v4/test/TestRewriteAST.java index 8b5bfbd73..0045a6a5e 100644 --- a/tool/test/org/antlr/v4/test/TestRewriteAST.java +++ b/tool/test/org/antlr/v4/test/TestRewriteAST.java @@ -1006,7 +1006,7 @@ public class TestRewriteAST extends BaseTest { "options { output = AST; }\n" + "tokens { FLOAT; }\n" + "a\n" + - " : INT -> {new CommonTree(new CommonAST(FLOAT,$INT.text+\".0\"))} \n" + + " : INT -> {new CommonAST(new CommonAST(FLOAT,$INT.text+\".0\"))} \n" + " ; \n" + "INT : '0'..'9'+; \n" + "WS: (' ' | '\\n' | '\\t')+ {$channel = HIDDEN;}; \n";