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 */
28package org.antlr.runtime.tree;
29
30import org.antlr.runtime.Token;
31
32import java.util.List;
33
34/** A record of the rules used to match a token sequence.  The tokens
35 *  end up as the leaves of this tree and rule nodes are the interior nodes.
36 *  This really adds no functionality, it is just an alias for CommonTree
37 *  that is more meaningful (specific) and holds a String to display for a node.
38 */
39public class ParseTree extends BaseTree {
40	public Object payload;
41	public List hiddenTokens;
42
43	public ParseTree(Object label) {
44		this.payload = label;
45	}
46
47	public Tree dupNode() {
48		return null;
49	}
50
51	public int getType() {
52		return 0;
53	}
54
55	public String getText() {
56		return toString();
57	}
58
59	public int getTokenStartIndex() {
60		return 0;
61	}
62
63	public void setTokenStartIndex(int index) {
64	}
65
66	public int getTokenStopIndex() {
67		return 0;
68	}
69
70	public void setTokenStopIndex(int index) {
71	}
72
73	public String toString() {
74		if ( payload instanceof Token ) {
75			Token t = (Token)payload;
76			if ( t.getType() == Token.EOF ) {
77				return "<EOF>";
78			}
79			return t.getText();
80		}
81		return payload.toString();
82	}
83
84	/** Emit a token and all hidden nodes before.  EOF node holds all
85	 *  hidden tokens after last real token.
86	 */
87	public String toStringWithHiddenTokens() {
88		StringBuffer buf = new StringBuffer();
89		if ( hiddenTokens!=null ) {
90			for (int i = 0; i < hiddenTokens.size(); i++) {
91				Token hidden = (Token) hiddenTokens.get(i);
92				buf.append(hidden.getText());
93			}
94		}
95		String nodeText = this.toString();
96		if ( !nodeText.equals("<EOF>") ) buf.append(nodeText);
97		return buf.toString();
98	}
99
100	/** Print out the leaves of this tree, which means printing original
101	 *  input back out.
102	 */
103	public String toInputString() {
104		StringBuffer buf = new StringBuffer();
105		_toStringLeaves(buf);
106		return buf.toString();
107	}
108
109	public void _toStringLeaves(StringBuffer buf) {
110		if ( payload instanceof Token ) { // leaf node token?
111			buf.append(this.toStringWithHiddenTokens());
112			return;
113		}
114		for (int i = 0; children!=null && i < children.size(); i++) {
115			ParseTree t = (ParseTree)children.get(i);
116			t._toStringLeaves(buf);
117		}
118	}
119}
120