1 /* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package org.antlr.runtime.tree; 29 30 import org.antlr.runtime.Token; 31 import org.antlr.runtime.TokenStream; 32 import org.antlr.runtime.RecognitionException; 33 34 /** How to create and navigate trees. Rather than have a separate factory 35 * and adaptor, I've merged them. Makes sense to encapsulate. 36 * 37 * This takes the place of the tree construction code generated in the 38 * generated code in 2.x and the ASTFactory. 39 * 40 * I do not need to know the type of a tree at all so they are all 41 * generic Objects. This may increase the amount of typecasting needed. :( 42 */ 43 public interface TreeAdaptor { 44 // C o n s t r u c t i o n 45 46 /** Create a tree node from Token object; for CommonTree type trees, 47 * then the token just becomes the payload. This is the most 48 * common create call. 49 * 50 * Override if you want another kind of node to be built. 51 */ 52 public Object create(Token payload); 53 54 /** Duplicate a single tree node. 55 * Override if you want another kind of node to be built. 56 */ 57 public Object dupNode(Object treeNode); 58 59 /** Duplicate tree recursively, using dupNode() for each node */ 60 public Object dupTree(Object tree); 61 62 /** Return a nil node (an empty but non-null node) that can hold 63 * a list of element as the children. If you want a flat tree (a list) 64 * use "t=adaptor.nil(); t.addChild(x); t.addChild(y);" 65 */ 66 public Object nil(); 67 68 /** Return a tree node representing an error. This node records the 69 * tokens consumed during error recovery. The start token indicates the 70 * input symbol at which the error was detected. The stop token indicates 71 * the last symbol consumed during recovery. 72 * 73 * You must specify the input stream so that the erroneous text can 74 * be packaged up in the error node. The exception could be useful 75 * to some applications; default implementation stores ptr to it in 76 * the CommonErrorNode. 77 * 78 * This only makes sense during token parsing, not tree parsing. 79 * Tree parsing should happen only when parsing and tree construction 80 * succeed. 81 */ 82 public Object errorNode(TokenStream input, Token start, Token stop, RecognitionException e); 83 84 /** Is tree considered a nil node used to make lists of child nodes? */ 85 public boolean isNil(Object tree); 86 87 /** Add a child to the tree t. If child is a flat tree (a list), make all 88 * in list children of t. Warning: if t has no children, but child does 89 * and child isNil then you can decide it is ok to move children to t via 90 * t.children = child.children; i.e., without copying the array. Just 91 * make sure that this is consistent with have the user will build 92 * ASTs. Do nothing if t or child is null. 93 */ 94 public void addChild(Object t, Object child); 95 96 /** If oldRoot is a nil root, just copy or move the children to newRoot. 97 * If not a nil root, make oldRoot a child of newRoot. 98 * 99 * old=^(nil a b c), new=r yields ^(r a b c) 100 * old=^(a b c), new=r yields ^(r ^(a b c)) 101 * 102 * If newRoot is a nil-rooted single child tree, use the single 103 * child as the new root node. 104 * 105 * old=^(nil a b c), new=^(nil r) yields ^(r a b c) 106 * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) 107 * 108 * If oldRoot was null, it's ok, just return newRoot (even if isNil). 109 * 110 * old=null, new=r yields r 111 * old=null, new=^(nil r) yields ^(nil r) 112 * 113 * Return newRoot. Throw an exception if newRoot is not a 114 * simple node or nil root with a single child node--it must be a root 115 * node. If newRoot is ^(nil x) return x as newRoot. 116 * 117 * Be advised that it's ok for newRoot to point at oldRoot's 118 * children; i.e., you don't have to copy the list. We are 119 * constructing these nodes so we should have this control for 120 * efficiency. 121 */ 122 public Object becomeRoot(Object newRoot, Object oldRoot); 123 124 /** Given the root of the subtree created for this rule, post process 125 * it to do any simplifications or whatever you want. A required 126 * behavior is to convert ^(nil singleSubtree) to singleSubtree 127 * as the setting of start/stop indexes relies on a single non-nil root 128 * for non-flat trees. 129 * 130 * Flat trees such as for lists like "idlist : ID+ ;" are left alone 131 * unless there is only one ID. For a list, the start/stop indexes 132 * are set in the nil node. 133 * 134 * This method is executed after all rule tree construction and right 135 * before setTokenBoundaries(). 136 */ 137 public Object rulePostProcessing(Object root); 138 139 /** For identifying trees. 140 * 141 * How to identify nodes so we can say "add node to a prior node"? 142 * Even becomeRoot is an issue. Use System.identityHashCode(node) 143 * usually. 144 */ 145 public int getUniqueID(Object node); 146 147 148 // R e w r i t e R u l e s 149 150 /** Create a node for newRoot make it the root of oldRoot. 151 * If oldRoot is a nil root, just copy or move the children to newRoot. 152 * If not a nil root, make oldRoot a child of newRoot. 153 * 154 * Return node created for newRoot. 155 * 156 * Be advised: when debugging ASTs, the DebugTreeAdaptor manually 157 * calls create(Token child) and then plain becomeRoot(node, node) 158 * because it needs to trap calls to create, but it can't since it delegates 159 * to not inherits from the TreeAdaptor. 160 */ 161 public Object becomeRoot(Token newRoot, Object oldRoot); 162 163 /** Create a new node derived from a token, with a new token type. 164 * This is invoked from an imaginary node ref on right side of a 165 * rewrite rule as IMAG[$tokenLabel]. 166 * 167 * This should invoke createToken(Token). 168 */ 169 public Object create(int tokenType, Token fromToken); 170 171 /** Same as create(tokenType,fromToken) except set the text too. 172 * This is invoked from an imaginary node ref on right side of a 173 * rewrite rule as IMAG[$tokenLabel, "IMAG"]. 174 * 175 * This should invoke createToken(Token). 176 */ 177 public Object create(int tokenType, Token fromToken, String text); 178 179 /** Create a new node derived from a token, with a new token type. 180 * This is invoked from an imaginary node ref on right side of a 181 * rewrite rule as IMAG["IMAG"]. 182 * 183 * This should invoke createToken(int,String). 184 */ 185 public Object create(int tokenType, String text); 186 187 188 // C o n t e n t 189 190 /** For tree parsing, I need to know the token type of a node */ 191 public int getType(Object t); 192 193 /** Node constructors can set the type of a node */ 194 public void setType(Object t, int type); 195 196 public String getText(Object t); 197 198 /** Node constructors can set the text of a node */ 199 public void setText(Object t, String text); 200 201 /** Return the token object from which this node was created. 202 * Currently used only for printing an error message. 203 * The error display routine in BaseRecognizer needs to 204 * display where the input the error occurred. If your 205 * tree of limitation does not store information that can 206 * lead you to the token, you can create a token filled with 207 * the appropriate information and pass that back. See 208 * BaseRecognizer.getErrorMessage(). 209 */ 210 public Token getToken(Object t); 211 212 /** Where are the bounds in the input token stream for this node and 213 * all children? Each rule that creates AST nodes will call this 214 * method right before returning. Flat trees (i.e., lists) will 215 * still usually have a nil root node just to hold the children list. 216 * That node would contain the start/stop indexes then. 217 */ 218 public void setTokenBoundaries(Object t, Token startToken, Token stopToken); 219 220 /** Get the token start index for this subtree; return -1 if no such index */ 221 public int getTokenStartIndex(Object t); 222 223 /** Get the token stop index for this subtree; return -1 if no such index */ 224 public int getTokenStopIndex(Object t); 225 226 227 // N a v i g a t i o n / T r e e P a r s i n g 228 229 /** Get a child 0..n-1 node */ 230 public Object getChild(Object t, int i); 231 232 /** Set ith child (0..n-1) to t; t must be non-null and non-nil node */ 233 public void setChild(Object t, int i, Object child); 234 235 /** Remove ith child and shift children down from right. */ 236 public Object deleteChild(Object t, int i); 237 238 /** How many children? If 0, then this is a leaf node */ 239 public int getChildCount(Object t); 240 241 /** Who is the parent node of this node; if null, implies node is root. 242 * If your node type doesn't handle this, it's ok but the tree rewrites 243 * in tree parsers need this functionality. 244 */ 245 public Object getParent(Object t); 246 public void setParent(Object t, Object parent); 247 248 /** What index is this node in the child list? Range: 0..n-1 249 * If your node type doesn't handle this, it's ok but the tree rewrites 250 * in tree parsers need this functionality. 251 */ 252 public int getChildIndex(Object t); 253 public void setChildIndex(Object t, int index); 254 255 /** Replace from start to stop child index of parent with t, which might 256 * be a list. Number of children may be different 257 * after this call. 258 * 259 * If parent is null, don't do anything; must be at root of overall tree. 260 * Can't replace whatever points to the parent externally. Do nothing. 261 */ 262 public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t); 263 } 264