13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/* 23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"] 33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr 43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved. 53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without 73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions 83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met: 93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright 103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer. 113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright 123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer in the 133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein documentation and/or other materials provided with the distribution. 143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products 153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein derived from this software without specific prior written permission. 163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime.tree; 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.Token; 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.TokenStream; 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.RecognitionException; 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.HashMap; 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.Map; 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** A TreeAdaptor that works with any Tree implementation. */ 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic abstract class BaseTreeAdaptor implements TreeAdaptor { 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** System.identityHashCode() is not always unique; we have to 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * track ourselves. That's ok, it's only for debugging, though it's 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * expensive: we have to create a hashtable with all tree nodes in it. 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected Map treeToUniqueIDMap; 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected int uniqueNodeID = 1; 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object nil() { 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return create(null); 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** create tree node that holds the start and stop tokens associated 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * with an error. 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If you specify your own kind of tree nodes, you will likely have to 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * override this method. CommonTree returns Token.INVALID_TOKEN_TYPE 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * if no token payload but you might have to set token type for diff 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * node type. 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * You don't have to subclass CommonErrorNode; you will likely need to 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * subclass your own tree node class to avoid class cast exception. 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object errorNode(TokenStream input, Token start, Token stop, 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein RecognitionException e) 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein { 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein CommonErrorNode t = new CommonErrorNode(input, start, stop, e); 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein //System.out.println("returning error node '"+t+"' @index="+input.index()); 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return t; 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public boolean isNil(Object tree) { 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ((Tree)tree).isNil(); 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object dupTree(Object tree) { 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return dupTree(tree, null); 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** This is generic in the sense that it will work with any kind of 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * tree (not just Tree interface). It invokes the adaptor routines 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * not the tree node routines to do the construction. 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object dupTree(Object t, Object parent) { 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( t==null ) { 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return null; 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Object newTree = dupNode(t); 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // ensure new subtree root has parent/child index set 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein setChildIndex(newTree, getChildIndex(t)); // same index in new tree 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein setParent(newTree, parent); 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int n = getChildCount(t); 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein for (int i = 0; i < n; i++) { 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Object child = getChild(t, i); 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Object newSubTree = dupTree(child, t); 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein addChild(newTree, newSubTree); 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return newTree; 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Add a child to the tree t. If child is a flat tree (a list), make all 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * in list children of t. Warning: if t has no children, but child does 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * and child isNil then you can decide it is ok to move children to t via 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * t.children = child.children; i.e., without copying the array. Just 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * make sure that this is consistent with have the user will build 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * ASTs. 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void addChild(Object t, Object child) { 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( t!=null && child!=null ) { 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ((Tree)t).addChild((Tree)child); 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** If oldRoot is a nil root, just copy or move the children to newRoot. 1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If not a nil root, make oldRoot a child of newRoot. 1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * old=^(nil a b c), new=r yields ^(r a b c) 1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * old=^(a b c), new=r yields ^(r ^(a b c)) 1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If newRoot is a nil-rooted single child tree, use the single 1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * child as the new root node. 1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * old=^(nil a b c), new=^(nil r) yields ^(r a b c) 1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) 1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If oldRoot was null, it's ok, just return newRoot (even if isNil). 1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * old=null, new=r yields r 1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * old=null, new=^(nil r) yields ^(nil r) 1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Return newRoot. Throw an exception if newRoot is not a 1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * simple node or nil root with a single child node--it must be a root 1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * node. If newRoot is ^(nil x) return x as newRoot. 1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Be advised that it's ok for newRoot to point at oldRoot's 1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * children; i.e., you don't have to copy the list. We are 1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * constructing these nodes so we should have this control for 1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * efficiency. 1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object becomeRoot(Object newRoot, Object oldRoot) { 1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein //System.out.println("becomeroot new "+newRoot.toString()+" old "+oldRoot); 1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Tree newRootTree = (Tree)newRoot; 1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Tree oldRootTree = (Tree)oldRoot; 1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( oldRoot==null ) { 1423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return newRoot; 1433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // handle ^(nil real-node) 1453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( newRootTree.isNil() ) { 1463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int nc = newRootTree.getChildCount(); 1473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( nc==1 ) newRootTree = (Tree)newRootTree.getChild(0); 1483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( nc >1 ) { 1493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // TODO: make tree run time exceptions hierarchy 1503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein throw new RuntimeException("more than one node as root (TODO: make exception hierarchy)"); 1513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // add oldRoot to newRoot; addChild takes care of case where oldRoot 1543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // is a flat list (i.e., nil-rooted tree). All children of oldRoot 1553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // are added to newRoot. 1563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein newRootTree.addChild(oldRootTree); 1573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return newRootTree; 1583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Transform ^(nil x) to x and nil to null */ 1613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object rulePostProcessing(Object root) { 1623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein //System.out.println("rulePostProcessing: "+((Tree)root).toStringTree()); 1633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Tree r = (Tree)root; 1643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( r!=null && r.isNil() ) { 1653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( r.getChildCount()==0 ) { 1663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein r = null; 1673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( r.getChildCount()==1 ) { 1693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein r = (Tree)r.getChild(0); 1703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // whoever invokes rule will set parent and child index 1713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein r.setParent(null); 1723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein r.setChildIndex(-1); 1733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return r; 1763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object becomeRoot(Token newRoot, Object oldRoot) { 1793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return becomeRoot(create(newRoot), oldRoot); 1803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object create(int tokenType, Token fromToken) { 1833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein fromToken = createToken(fromToken); 1843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein //((ClassicToken)fromToken).setType(tokenType); 1853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein fromToken.setType(tokenType); 1863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Tree t = (Tree)create(fromToken); 1873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return t; 1883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object create(int tokenType, Token fromToken, String text) { 1913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if (fromToken == null) return create(tokenType, text); 1923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein fromToken = createToken(fromToken); 1933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein fromToken.setType(tokenType); 1943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein fromToken.setText(text); 1953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Tree t = (Tree)create(fromToken); 1963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return t; 1973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object create(int tokenType, String text) { 2003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Token fromToken = createToken(tokenType, text); 2013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Tree t = (Tree)create(fromToken); 2023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return t; 2033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getType(Object t) { 2063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ((Tree)t).getType(); 2073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setType(Object t, int type) { 2103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein throw new NoSuchMethodError("don't know enough about Tree node"); 2113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String getText(Object t) { 2143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ((Tree)t).getText(); 2153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setText(Object t, String text) { 2183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein throw new NoSuchMethodError("don't know enough about Tree node"); 2193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object getChild(Object t, int i) { 2223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ((Tree)t).getChild(i); 2233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setChild(Object t, int i, Object child) { 2263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ((Tree)t).setChild(i, (Tree)child); 2273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object deleteChild(Object t, int i) { 2303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ((Tree)t).deleteChild(i); 2313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getChildCount(Object t) { 2343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ((Tree)t).getChildCount(); 2353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getUniqueID(Object node) { 2383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( treeToUniqueIDMap==null ) { 2393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein treeToUniqueIDMap = new HashMap(); 2403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Integer prevID = (Integer)treeToUniqueIDMap.get(node); 2423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( prevID!=null ) { 2433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return prevID.intValue(); 2443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int ID = uniqueNodeID; 2463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein treeToUniqueIDMap.put(node, new Integer(ID)); 2473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein uniqueNodeID++; 2483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return ID; 2493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // GC makes these nonunique: 2503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // return System.identityHashCode(node); 2513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 2523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Tell me how to create a token for use with imaginary token nodes. 2543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * For example, there is probably no input symbol associated with imaginary 2553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * token DECL, but you need to create it as a payload or whatever for 2563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the DECL node as in ^(DECL type ID). 2573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 2583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If you care what the token payload objects' type is, you should 2593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * override this method and any other createToken variant. 2603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 2613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public abstract Token createToken(int tokenType, String text); 2623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Tell me how to create a token for use with imaginary token nodes. 2643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * For example, there is probably no input symbol associated with imaginary 2653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * token DECL, but you need to create it as a payload or whatever for 2663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * the DECL node as in ^(DECL type ID). 2673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 2683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * This is a variant of createToken where the new token is derived from 2693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * an actual real input token. Typically this is for converting '{' 2703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * tokens to BLOCK etc... You'll see 2713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 2723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; 2733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 2743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If you care what the token payload objects' type is, you should 2753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * override this method and any other createToken variant. 2763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 2773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public abstract Token createToken(Token fromToken); 2783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 2793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 280