1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2005-2008 Terence Parr
4 * All rights reserved.
5 *
6 * Conversion to C#:
7 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33namespace Antlr.Runtime.Debug {
34    using Obsolete = System.ObsoleteAttribute;
35    using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor;
36    using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream;
37
38    /** <summary>
39     *  Debug any tree node stream.  The constructor accepts the stream
40     *  and a debug listener.  As node stream calls come in, debug events
41     *  are triggered.
42     *  </summary>
43     */
44    public class DebugTreeNodeStream : ITreeNodeStream {
45        protected IDebugEventListener dbg;
46        protected ITreeAdaptor adaptor;
47        protected ITreeNodeStream input;
48        protected bool initialStreamState = true;
49
50        /** <summary>Track the last mark() call result value for use in rewind().</summary> */
51        protected int lastMarker;
52
53        public DebugTreeNodeStream(ITreeNodeStream input,
54                                   IDebugEventListener dbg) {
55            this.input = input;
56            this.adaptor = input.TreeAdaptor;
57            this.input.UniqueNavigationNodes = true;
58            DebugListener = dbg;
59        }
60
61        #region Properties
62        public virtual IDebugEventListener DebugListener {
63            get {
64                return dbg;
65            }
66            set {
67                dbg = value;
68            }
69        }
70        public virtual int Index {
71            get {
72                return input.Index;
73            }
74        }
75        public virtual ITokenStream TokenStream {
76            get {
77                return input.TokenStream;
78            }
79        }
80        public virtual ITreeAdaptor TreeAdaptor {
81            get {
82                return adaptor;
83            }
84        }
85        public virtual object TreeSource {
86            get {
87                return input;
88            }
89        }
90        /** <summary>
91         *  It is normally this object that instructs the node stream to
92         *  create unique nav nodes, but to satisfy interface, we have to
93         *  define it.  It might be better to ignore the parameter but
94         *  there might be a use for it later, so I'll leave.
95         *  </summary>
96         */
97        public bool UniqueNavigationNodes {
98            get {
99                return input.UniqueNavigationNodes;
100            }
101            set {
102                input.UniqueNavigationNodes = value;
103            }
104        }
105
106        #endregion
107
108        public virtual void Consume() {
109            object node = input.LT(1);
110            input.Consume();
111            dbg.ConsumeNode(node);
112        }
113
114        public virtual object this[int i] {
115            get {
116                return input[i];
117            }
118        }
119
120        public virtual object LT(int i) {
121            object node = input.LT(i);
122            int ID = adaptor.GetUniqueID(node);
123            string text = adaptor.GetText(node);
124            int type = adaptor.GetType(node);
125            dbg.LT(i, node);
126            return node;
127        }
128
129        public virtual int LA(int i) {
130            object node = input.LT(i);
131            int ID = adaptor.GetUniqueID(node);
132            string text = adaptor.GetText(node);
133            int type = adaptor.GetType(node);
134            dbg.LT(i, node);
135            return type;
136        }
137
138        public virtual int Mark() {
139            lastMarker = input.Mark();
140            dbg.Mark(lastMarker);
141            return lastMarker;
142        }
143
144        public virtual void Rewind(int marker) {
145            dbg.Rewind(marker);
146            input.Rewind(marker);
147        }
148
149        public virtual void Rewind() {
150            dbg.Rewind();
151            input.Rewind(lastMarker);
152        }
153
154        public virtual void Release(int marker) {
155        }
156
157        public virtual void Seek(int index) {
158            // TODO: implement seek in dbg interface
159            // db.seek(index);
160            input.Seek(index);
161        }
162
163        public virtual int Count {
164            get {
165                return input.Count;
166            }
167        }
168
169        public virtual string SourceName {
170            get {
171                return TokenStream.SourceName;
172            }
173        }
174
175        public virtual void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) {
176            input.ReplaceChildren(parent, startChildIndex, stopChildIndex, t);
177        }
178
179        public virtual string ToString(object start, object stop) {
180            return input.ToString(start, stop);
181        }
182    }
183}
184