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.CommonToken;
313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.Token;
323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** A TreeAdaptor that works with any Tree implementation.  It provides
343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  really just factory methods; all the work is done by BaseTreeAdaptor.
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  If you would like to have different tokens created than ClassicToken
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  objects, you need to override this and then set the parser tree adaptor to
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  use your subclass.
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  To get your parser to build nodes of a different type, override
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  create(Token), errorNode(), and to be safe, YourTreeClass.dupNode().
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  dupNode is called to duplicate nodes during rewrite operations.
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class CommonTreeAdaptor extends BaseTreeAdaptor {
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Duplicate a node.  This is part of the factory;
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *	override if you want another kind of node to be built.
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  I could use reflection to prevent having to override this
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  but reflection is slow.
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object dupNode(Object t) {
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return null;
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return ((Tree)t).dupNode();
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object create(Token payload) {
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return new CommonTree(payload);
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Tell me how to create a token for use with imaginary token nodes.
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  For example, there is probably no input symbol associated with imaginary
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  token DECL, but you need to create it as a payload or whatever for
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  the DECL node as in ^(DECL type ID).
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If you care what the token payload objects' type is, you should
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  override this method and any other createToken variant.
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Token createToken(int tokenType, String text) {
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return new CommonToken(tokenType, text);
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Tell me how to create a token for use with imaginary token nodes.
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  For example, there is probably no input symbol associated with imaginary
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  token DECL, but you need to create it as a payload or whatever for
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  the DECL node as in ^(DECL type ID).
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  This is a variant of createToken where the new token is derived from
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  an actual real input token.  Typically this is for converting '{'
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  tokens to BLOCK etc...  You'll see
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *    r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  If you care what the token payload objects' type is, you should
833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  override this method and any other createToken variant.
843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Token createToken(Token fromToken) {
863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return new CommonToken(fromToken);
873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Track start/stop token for subtree root created for a rule.
903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Only works with Tree nodes.  For rules that match nothing,
913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  seems like this will yield start=i and stop=i-1 in a nil node.
923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  Might be useful info so I'll not force to be i..i.
933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setTokenBoundaries(Object t, Token startToken, Token stopToken) {
953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return;
963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		int start = 0;
973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		int stop = 0;
983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( startToken!=null ) start = startToken.getTokenIndex();
993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( stopToken!=null ) stop = stopToken.getTokenIndex();
1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		((Tree)t).setTokenStartIndex(start);
1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		((Tree)t).setTokenStopIndex(stop);
1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getTokenStartIndex(Object t) {
1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return -1;
1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return ((Tree)t).getTokenStartIndex();
1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getTokenStopIndex(Object t) {
1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return -1;
1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return ((Tree)t).getTokenStopIndex();
1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String getText(Object t) {
1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return null;
1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return ((Tree)t).getText();
1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public int getType(Object t) {
1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return Token.INVALID_TOKEN_TYPE;
1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return ((Tree)t).getType();
1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** What is the Token associated with this node?  If
1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  you are not using CommonTree, then you must
1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  override this in your own adaptor.
1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Token getToken(Object t) {
1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t instanceof CommonTree ) {
1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			return ((CommonTree)t).getToken();
1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return null; // no idea what to do
1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object getChild(Object t, int i) {
1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return null;
1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        return ((Tree)t).getChild(i);
1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public int getChildCount(Object t) {
1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return 0;
1423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        return ((Tree)t).getChildCount();
1433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
1443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Object getParent(Object t) {
1463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( t==null ) return null;
1473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        return ((Tree)t).getParent();
1483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setParent(Object t, Object parent) {
1513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        if ( t!=null ) ((Tree)t).setParent((Tree)parent);
1523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getChildIndex(Object t) {
1553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        if ( t==null ) return 0;
1563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return ((Tree)t).getChildIndex();
1573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setChildIndex(Object t, int index) {
1603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        if ( t!=null ) ((Tree)t).setChildIndex(index);
1613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) {
1643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( parent!=null ) {
1653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			((Tree)parent).replaceChildren(startChildIndex, stopChildIndex, t);
1663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
1673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
169