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.debug;
29
30import org.antlr.runtime.tree.TreeAdaptor;
31import org.antlr.runtime.tree.TreeNodeStream;
32import org.antlr.runtime.TokenStream;
33
34/** Debug any tree node stream.  The constructor accepts the stream
35 *  and a debug listener.  As node stream calls come in, debug events
36 *  are triggered.
37 */
38public class DebugTreeNodeStream implements TreeNodeStream {
39	protected DebugEventListener dbg;
40	protected TreeAdaptor adaptor;
41	protected TreeNodeStream input;
42	protected boolean initialStreamState = true;
43
44	/** Track the last mark() call result value for use in rewind(). */
45	protected int lastMarker;
46
47	public DebugTreeNodeStream(TreeNodeStream input,
48							   DebugEventListener dbg)
49	{
50		this.input = input;
51		this.adaptor = input.getTreeAdaptor();
52		this.input.setUniqueNavigationNodes(true);
53		setDebugListener(dbg);
54	}
55
56	public void setDebugListener(DebugEventListener dbg) {
57		this.dbg = dbg;
58	}
59
60	public TreeAdaptor getTreeAdaptor() {
61		return adaptor;
62	}
63
64	public void consume() {
65		Object node = input.LT(1);
66		input.consume();
67		dbg.consumeNode(node);
68	}
69
70	public Object get(int i) {
71		return input.get(i);
72	}
73
74	public Object LT(int i) {
75		Object node = input.LT(i);
76		int ID = adaptor.getUniqueID(node);
77		String text = adaptor.getText(node);
78		int type = adaptor.getType(node);
79		dbg.LT(i, node);
80		return node;
81	}
82
83	public int LA(int i) {
84		Object node = input.LT(i);
85		int ID = adaptor.getUniqueID(node);
86		String text = adaptor.getText(node);
87		int type = adaptor.getType(node);
88		dbg.LT(i, node);
89		return type;
90	}
91
92	public int mark() {
93		lastMarker = input.mark();
94		dbg.mark(lastMarker);
95		return lastMarker;
96	}
97
98	public int index() {
99		return input.index();
100	}
101
102	public void rewind(int marker) {
103		dbg.rewind(marker);
104		input.rewind(marker);
105	}
106
107	public void rewind() {
108		dbg.rewind();
109		input.rewind(lastMarker);
110	}
111
112	public void release(int marker) {
113	}
114
115	public void seek(int index) {
116		// TODO: implement seek in dbg interface
117		// db.seek(index);
118		input.seek(index);
119	}
120
121	public int size() {
122		return input.size();
123	}
124
125    public void reset() { ; }
126
127    public Object getTreeSource() {
128		return input;
129	}
130
131	public String getSourceName() {
132		return getTokenStream().getSourceName();
133	}
134
135	public TokenStream getTokenStream() {
136		return input.getTokenStream();
137	}
138
139	/** It is normally this object that instructs the node stream to
140	 *  create unique nav nodes, but to satisfy interface, we have to
141	 *  define it.  It might be better to ignore the parameter but
142	 *  there might be a use for it later, so I'll leave.
143	 */
144	public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) {
145		input.setUniqueNavigationNodes(uniqueNavigationNodes);
146	}
147
148	public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) {
149		input.replaceChildren(parent, startChildIndex, stopChildIndex, t);
150	}
151
152	public String toString(Object start, Object stop) {
153		return input.toString(start,stop);
154	}
155}
156