1324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver/*
2324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * [The "BSD licence"]
3324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Copyright (c) 2005-2008 Terence Parr
4324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * All rights reserved.
5324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *
6324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Conversion to C#:
7324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * All rights reserved.
9324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *
10324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * Redistribution and use in source and binary forms, with or without
11324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * modification, are permitted provided that the following conditions
12324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * are met:
13324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * 1. Redistributions of source code must retain the above copyright
14324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    notice, this list of conditions and the following disclaimer.
15324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * 2. Redistributions in binary form must reproduce the above copyright
16324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    notice, this list of conditions and the following disclaimer in the
17324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    documentation and/or other materials provided with the distribution.
18324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * 3. The name of the author may not be used to endorse or promote products
19324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *    derived from this software without specific prior written permission.
20324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver *
21324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver */
32324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
33324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruvernamespace Antlr.Runtime.Debug {
34324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    using Obsolete = System.ObsoleteAttribute;
35324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor;
36324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream;
37324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
38324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    /** <summary>
39324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver     *  Debug any tree node stream.  The constructor accepts the stream
40324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver     *  and a debug listener.  As node stream calls come in, debug events
41324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver     *  are triggered.
42324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver     *  </summary>
43324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver     */
44324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    public class DebugTreeNodeStream : ITreeNodeStream {
45324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        protected IDebugEventListener dbg;
46324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        protected ITreeAdaptor adaptor;
47324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        protected ITreeNodeStream input;
48324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        protected bool initialStreamState = true;
49324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
50324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        /** <summary>Track the last mark() call result value for use in rewind().</summary> */
51324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        protected int lastMarker;
52324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
53324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public DebugTreeNodeStream(ITreeNodeStream input,
54324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                                   IDebugEventListener dbg) {
55324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            this.input = input;
56324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            this.adaptor = input.TreeAdaptor;
57324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            this.input.UniqueNavigationNodes = true;
58324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            DebugListener = dbg;
59324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
60324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
61324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        #region Properties
62324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual IDebugEventListener DebugListener {
63324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
64324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return dbg;
65324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
66324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            set {
67324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                dbg = value;
68324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
69324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
70324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual int Index {
71324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
72324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return input.Index;
73324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
74324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
75324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual ITokenStream TokenStream {
76324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
77324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return input.TokenStream;
78324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
79324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
80324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual ITreeAdaptor TreeAdaptor {
81324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
82324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return adaptor;
83324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
84324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
85324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual object TreeSource {
86324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
87324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return input;
88324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
89324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
90324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        /** <summary>
91324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver         *  It is normally this object that instructs the node stream to
92324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver         *  create unique nav nodes, but to satisfy interface, we have to
93324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver         *  define it.  It might be better to ignore the parameter but
94324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver         *  there might be a use for it later, so I'll leave.
95324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver         *  </summary>
96324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver         */
97324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public bool UniqueNavigationNodes {
98324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
99324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return input.UniqueNavigationNodes;
100324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
101324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            set {
102324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                input.UniqueNavigationNodes = value;
103324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
104324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
105324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
106324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        #endregion
107324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
108324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual void Consume() {
109324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            object node = input.LT(1);
110324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            input.Consume();
111324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            dbg.ConsumeNode(node);
112324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
113324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
114324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual object this[int i] {
115324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
116324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return input[i];
117324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
118324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
119324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
120324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual object LT(int i) {
121324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            object node = input.LT(i);
122324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            int ID = adaptor.GetUniqueID(node);
123324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            string text = adaptor.GetText(node);
124324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            int type = adaptor.GetType(node);
125324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            dbg.LT(i, node);
126324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            return node;
127324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
128324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
129324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual int LA(int i) {
130324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            object node = input.LT(i);
131324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            int ID = adaptor.GetUniqueID(node);
132324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            string text = adaptor.GetText(node);
133324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            int type = adaptor.GetType(node);
134324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            dbg.LT(i, node);
135324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            return type;
136324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
137324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
138324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual int Mark() {
139324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            lastMarker = input.Mark();
140324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            dbg.Mark(lastMarker);
141324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            return lastMarker;
142324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
143324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
144324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual void Rewind(int marker) {
145324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            dbg.Rewind(marker);
146324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            input.Rewind(marker);
147324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
148324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
149324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual void Rewind() {
150324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            dbg.Rewind();
151324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            input.Rewind(lastMarker);
152324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
153324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
154324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual void Release(int marker) {
155324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
156324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
157324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual void Seek(int index) {
158324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            // TODO: implement seek in dbg interface
159324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            // db.seek(index);
160324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            input.Seek(index);
161324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
162324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
163324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual int Count {
164324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
165324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return input.Count;
166324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
167324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
168324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
169324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual string SourceName {
170324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            get {
171324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                return TokenStream.SourceName;
172324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            }
173324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
174324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
175324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) {
176324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            input.ReplaceChildren(parent, startChildIndex, stopChildIndex, t);
177324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
178324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
179324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        public virtual string ToString(object start, object stop) {
180324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            return input.ToString(start, stop);
181324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        }
182324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    }
183324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver}
184