001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.runtime.debug;
029:
030: import org.antlr.runtime.tree.TreeAdaptor;
031: import org.antlr.runtime.tree.TreeNodeStream;
032: import org.antlr.runtime.TokenStream;
033:
034: /** Debug any tree node stream. The constructor accepts the stream
035: * and a debug listener. As node stream calls come in, debug events
036: * are triggered.
037: */
038: public class DebugTreeNodeStream implements TreeNodeStream {
039: protected DebugEventListener dbg;
040: protected TreeAdaptor adaptor;
041: protected TreeNodeStream input;
042: protected boolean initialStreamState = true;
043:
044: /** Track the last mark() call result value for use in rewind(). */
045: protected int lastMarker;
046:
047: public DebugTreeNodeStream(TreeNodeStream input,
048: DebugEventListener dbg) {
049: this .input = input;
050: this .adaptor = input.getTreeAdaptor();
051: this .input.setUniqueNavigationNodes(true);
052: setDebugListener(dbg);
053: }
054:
055: public void setDebugListener(DebugEventListener dbg) {
056: this .dbg = dbg;
057: }
058:
059: public TreeAdaptor getTreeAdaptor() {
060: return adaptor;
061: }
062:
063: public void consume() {
064: Object node = input.LT(1);
065: input.consume();
066: dbg.consumeNode(node);
067: }
068:
069: public Object get(int i) {
070: return input.get(i);
071: }
072:
073: public Object LT(int i) {
074: Object node = input.LT(i);
075: int ID = adaptor.getUniqueID(node);
076: String text = adaptor.getText(node);
077: int type = adaptor.getType(node);
078: dbg.LT(i, node);
079: return node;
080: }
081:
082: public int LA(int i) {
083: Object node = input.LT(i);
084: int ID = adaptor.getUniqueID(node);
085: String text = adaptor.getText(node);
086: int type = adaptor.getType(node);
087: dbg.LT(i, node);
088: return type;
089: }
090:
091: public int mark() {
092: lastMarker = input.mark();
093: dbg.mark(lastMarker);
094: return lastMarker;
095: }
096:
097: public int index() {
098: return input.index();
099: }
100:
101: public void rewind(int marker) {
102: dbg.rewind(marker);
103: input.rewind(marker);
104: }
105:
106: public void rewind() {
107: dbg.rewind();
108: input.rewind(lastMarker);
109: }
110:
111: public void release(int marker) {
112: }
113:
114: public void seek(int index) {
115: // TODO: implement seek in dbg interface
116: // db.seek(index);
117: input.seek(index);
118: }
119:
120: public int size() {
121: return input.size();
122: }
123:
124: public Object getTreeSource() {
125: return input;
126: }
127:
128: public TokenStream getTokenStream() {
129: return input.getTokenStream();
130: }
131:
132: /** It is normally this object that instructs the node stream to
133: * create unique nav nodes, but to satisfy interface, we have to
134: * define it. It might be better to ignore the parameter but
135: * there might be a use for it later, so I'll leave.
136: */
137: public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) {
138: input.setUniqueNavigationNodes(uniqueNavigationNodes);
139: }
140:
141: public String toString(Object start, Object stop) {
142: return input.toString(start, stop);
143: }
144: }
|