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 Sapperstein/** How to create and navigate trees.  Rather than have a separate factory
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  and adaptor, I've merged them.  Makes sense to encapsulate.
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  This takes the place of the tree construction code generated in the
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  generated code in 2.x and the ASTFactory.
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  I do not need to know the type of a tree at all so they are all
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  generic Objects.  This may increase the amount of typecasting needed. :(
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic interface TreeAdaptor {
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	// C o n s t r u c t i o n
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Create a tree node from Token object; for CommonTree type trees,
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  then the token just becomes the payload.  This is the most
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  common create call.
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Override if you want another kind of node to be built.
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object create(Token payload);
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Duplicate a single tree node.
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Override if you want another kind of node to be built.
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object dupNode(Object treeNode);
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Duplicate tree recursively, using dupNode() for each node */
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object dupTree(Object tree);
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Return a nil node (an empty but non-null node) that can hold
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  a list of element as the children.  If you want a flat tree (a list)
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  use "t=adaptor.nil(); t.addChild(x); t.addChild(y);"
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object nil();
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Return a tree node representing an error.  This node records the
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  tokens consumed during error recovery.  The start token indicates the
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  input symbol at which the error was detected.  The stop token indicates
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  the last symbol consumed during recovery.
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  You must specify the input stream so that the erroneous text can
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  be packaged up in the error node.  The exception could be useful
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  to some applications; default implementation stores ptr to it in
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  the CommonErrorNode.
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This only makes sense during token parsing, not tree parsing.
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Tree parsing should happen only when parsing and tree construction
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  succeed.
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object errorNode(TokenStream input, Token start, Token stop, RecognitionException e);
833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Is tree considered a nil node used to make lists of child nodes? */
853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public boolean isNil(Object tree);
863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Add a child to the tree t.  If child is a flat tree (a list), make all
883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  in list children of t.  Warning: if t has no children, but child does
893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  and child isNil then you can decide it is ok to move children to t via
903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  t.children = child.children; i.e., without copying the array.  Just
913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  make sure that this is consistent with have the user will build
923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  ASTs.  Do nothing if t or child is null.
933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void addChild(Object t, Object child);
953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** If oldRoot is a nil root, just copy or move the children to newRoot.
973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If not a nil root, make oldRoot a child of newRoot.
983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    old=^(nil a b c), new=r yields ^(r a b c)
1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    old=^(a b c), new=r yields ^(r ^(a b c))
1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If newRoot is a nil-rooted single child tree, use the single
1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  child as the new root node.
1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    old=^(nil a b c), new=^(nil r) yields ^(r a b c)
1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    old=^(a b c), new=^(nil r) yields ^(r ^(a b c))
1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If oldRoot was null, it's ok, just return newRoot (even if isNil).
1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    old=null, new=r yields r
1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    old=null, new=^(nil r) yields ^(nil r)
1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Return newRoot.  Throw an exception if newRoot is not a
1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  simple node or nil root with a single child node--it must be a root
1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  node.  If newRoot is ^(nil x) return x as newRoot.
1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Be advised that it's ok for newRoot to point at oldRoot's
1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  children; i.e., you don't have to copy the list.  We are
1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  constructing these nodes so we should have this control for
1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  efficiency.
1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object becomeRoot(Object newRoot, Object oldRoot);
1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Given the root of the subtree created for this rule, post process
1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  it to do any simplifications or whatever you want.  A required
1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  behavior is to convert ^(nil singleSubtree) to singleSubtree
1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  as the setting of start/stop indexes relies on a single non-nil root
1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  for non-flat trees.
1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Flat trees such as for lists like "idlist : ID+ ;" are left alone
1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  unless there is only one ID.  For a list, the start/stop indexes
1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  are set in the nil node.
1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This method is executed after all rule tree construction and right
1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  before setTokenBoundaries().
1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object rulePostProcessing(Object root);
1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** For identifying trees.
1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  How to identify nodes so we can say "add node to a prior node"?
1423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Even becomeRoot is an issue.  Use System.identityHashCode(node)
1433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  usually.
1443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getUniqueID(Object node);
1463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	// R e w r i t e  R u l e s
1493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Create a node for newRoot make it the root of oldRoot.
1513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If oldRoot is a nil root, just copy or move the children to newRoot.
1523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If not a nil root, make oldRoot a child of newRoot.
1533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Return node created for newRoot.
1553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Be advised: when debugging ASTs, the DebugTreeAdaptor manually
1573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  calls create(Token child) and then plain becomeRoot(node, node)
1583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  because it needs to trap calls to create, but it can't since it delegates
1593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  to not inherits from the TreeAdaptor.
1603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object becomeRoot(Token newRoot, Object oldRoot);
1623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Create a new node derived from a token, with a new token type.
1643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This is invoked from an imaginary node ref on right side of a
1653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  rewrite rule as IMAG[$tokenLabel].
1663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This should invoke createToken(Token).
1683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object create(int tokenType, Token fromToken);
1703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Same as create(tokenType,fromToken) except set the text too.
1723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This is invoked from an imaginary node ref on right side of a
1733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  rewrite rule as IMAG[$tokenLabel, "IMAG"].
1743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This should invoke createToken(Token).
1763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object create(int tokenType, Token fromToken, String text);
1783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Create a new node derived from a token, with a new token type.
1803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This is invoked from an imaginary node ref on right side of a
1813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  rewrite rule as IMAG["IMAG"].
1823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
1833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This should invoke createToken(int,String).
1843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object create(int tokenType, String text);
1863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	// C o n t e n t
1893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** For tree parsing, I need to know the token type of a node */
1913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getType(Object t);
1923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Node constructors can set the type of a node */
1943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setType(Object t, int type);
1953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String getText(Object t);
1973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Node constructors can set the text of a node */
1993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setText(Object t, String text);
2003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Return the token object from which this node was created.
2023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Currently used only for printing an error message.
2033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  The error display routine in BaseRecognizer needs to
2043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  display where the input the error occurred. If your
2053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  tree of limitation does not store information that can
2063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  lead you to the token, you can create a token filled with
2073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  the appropriate information and pass that back.  See
2083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  BaseRecognizer.getErrorMessage().
2093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
2103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Token getToken(Object t);
2113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Where are the bounds in the input token stream for this node and
2133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  all children?  Each rule that creates AST nodes will call this
2143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  method right before returning.  Flat trees (i.e., lists) will
2153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  still usually have a nil root node just to hold the children list.
2163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  That node would contain the start/stop indexes then.
2173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
2183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setTokenBoundaries(Object t, Token startToken, Token stopToken);
2193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Get the token start index for this subtree; return -1 if no such index */
2213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getTokenStartIndex(Object t);
2223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Get the token stop index for this subtree; return -1 if no such index */
2243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getTokenStopIndex(Object t);
2253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	// N a v i g a t i o n  /  T r e e  P a r s i n g
2283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Get a child 0..n-1 node */
2303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object getChild(Object t, int i);
2313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Set ith child (0..n-1) to t; t must be non-null and non-nil node */
2333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setChild(Object t, int i, Object child);
2343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Remove ith child and shift children down from right. */
2363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object deleteChild(Object t, int i);
2373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** How many children?  If 0, then this is a leaf node */
2393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getChildCount(Object t);
2403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Who is the parent node of this node; if null, implies node is root.
2423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If your node type doesn't handle this, it's ok but the tree rewrites
2433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  in tree parsers need this functionality.
2443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
2453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object getParent(Object t);
2463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setParent(Object t, Object parent);
2473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** What index is this node in the child list? Range: 0..n-1
2493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If your node type doesn't handle this, it's ok but the tree rewrites
2503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  in tree parsers need this functionality.
2513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
2523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getChildIndex(Object t);
2533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setChildIndex(Object t, int index);
2543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
2553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Replace from start to stop child index of parent with t, which might
2563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  be a list.  Number of children may be different
2573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  after this call.
2583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
2593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If parent is null, don't do anything; must be at root of overall tree.
2603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Can't replace whatever points to the parent externally.  Do nothing.
2613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
2623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t);
2633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
264