• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 import 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  */
39 public class ParseTree extends BaseTree {
40 	public Object payload;
41 	public List<Token> hiddenTokens;
42 
ParseTree(Object label)43 	public ParseTree(Object label) {
44 		this.payload = label;
45 	}
46 
47 	@Override
dupNode()48 	public Tree dupNode() {
49 		return null;
50 	}
51 
52 	@Override
getType()53 	public int getType() {
54 		return 0;
55 	}
56 
57 	@Override
getText()58 	public String getText() {
59 		return toString();
60 	}
61 
62 	@Override
getTokenStartIndex()63 	public int getTokenStartIndex() {
64 		return 0;
65 	}
66 
67 	@Override
setTokenStartIndex(int index)68 	public void setTokenStartIndex(int index) {
69 	}
70 
71 	@Override
getTokenStopIndex()72 	public int getTokenStopIndex() {
73 		return 0;
74 	}
75 
76 	@Override
setTokenStopIndex(int index)77 	public void setTokenStopIndex(int index) {
78 	}
79 
80 	@Override
toString()81 	public String toString() {
82 		if ( payload instanceof Token ) {
83 			Token t = (Token)payload;
84 			if ( t.getType() == Token.EOF ) {
85 				return "<EOF>";
86 			}
87 			return t.getText();
88 		}
89 		return payload.toString();
90 	}
91 
92 	/** Emit a token and all hidden nodes before.  EOF node holds all
93 	 *  hidden tokens after last real token.
94 	 */
toStringWithHiddenTokens()95 	public String toStringWithHiddenTokens() {
96 		StringBuilder buf = new StringBuilder();
97 		if ( hiddenTokens!=null ) {
98 			for (int i = 0; i < hiddenTokens.size(); i++) {
99 				Token hidden = hiddenTokens.get(i);
100 				buf.append(hidden.getText());
101 			}
102 		}
103 		String nodeText = this.toString();
104 		if ( !nodeText.equals("<EOF>") ) buf.append(nodeText);
105 		return buf.toString();
106 	}
107 
108 	/** Print out the leaves of this tree, which means printing original
109 	 *  input back out.
110 	 */
toInputString()111 	public String toInputString() {
112 		StringBuffer buf = new StringBuffer();
113 		_toStringLeaves(buf);
114 		return buf.toString();
115 	}
116 
_toStringLeaves(StringBuffer buf)117 	public void _toStringLeaves(StringBuffer buf) {
118 		if ( payload instanceof Token ) { // leaf node token?
119 			buf.append(this.toStringWithHiddenTokens());
120 			return;
121 		}
122 		for (int i = 0; children!=null && i < children.size(); i++) {
123 			ParseTree t = (ParseTree)children.get(i);
124 			t._toStringLeaves(buf);
125 		}
126 	}
127 }
128