• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 namespace Antlr.Runtime.Debug {
34 
35     public class DebugTokenStream : ITokenStream {
36         protected IDebugEventListener dbg;
37         public ITokenStream input;
38         protected bool initialStreamState = true;
39 
40         /** <summary>Track the last mark() call result value for use in rewind().</summary> */
41         protected int lastMarker;
42 
DebugTokenStream(ITokenStream input, IDebugEventListener dbg)43         public DebugTokenStream(ITokenStream input, IDebugEventListener dbg) {
44             this.input = input;
45             DebugListener = dbg;
46             // force TokenStream to get at least first valid token
47             // so we know if there are any hidden tokens first in the stream
48             input.LT(1);
49         }
50 
51         #region Properties
52 
53         public virtual int Index {
54             get {
55                 return input.Index;
56             }
57         }
58 
59         public virtual int Range {
60             get {
61                 return input.Range;
62             }
63         }
64 
65         public virtual IDebugEventListener DebugListener {
66             get {
67                 return dbg;
68             }
69             set {
70                 dbg = value;
71             }
72         }
73 
74         #endregion
75 
Consume()76         public virtual void Consume() {
77             if (initialStreamState) {
78                 ConsumeInitialHiddenTokens();
79             }
80             int a = input.Index;
81             IToken t = input.LT(1);
82             input.Consume();
83             int b = input.Index;
84             dbg.ConsumeToken(t);
85             if (b > a + 1) {
86                 // then we consumed more than one token; must be off channel tokens
87                 for (int i = a + 1; i < b; i++) {
88                     dbg.ConsumeHiddenToken(input.Get(i));
89                 }
90             }
91         }
92 
93         /** <summary>Consume all initial off-channel tokens</summary> */
ConsumeInitialHiddenTokens()94         protected virtual void ConsumeInitialHiddenTokens() {
95             int firstOnChannelTokenIndex = input.Index;
96             for (int i = 0; i < firstOnChannelTokenIndex; i++) {
97                 dbg.ConsumeHiddenToken(input.Get(i));
98             }
99             initialStreamState = false;
100         }
101 
LT(int i)102         public virtual IToken LT(int i) {
103             if (initialStreamState) {
104                 ConsumeInitialHiddenTokens();
105             }
106             dbg.LT(i, input.LT(i));
107             return input.LT(i);
108         }
109 
LA(int i)110         public virtual int LA(int i) {
111             if (initialStreamState) {
112                 ConsumeInitialHiddenTokens();
113             }
114             dbg.LT(i, input.LT(i));
115             return input.LA(i);
116         }
117 
Get(int i)118         public virtual IToken Get(int i) {
119             return input.Get(i);
120         }
121 
Mark()122         public virtual int Mark() {
123             lastMarker = input.Mark();
124             dbg.Mark(lastMarker);
125             return lastMarker;
126         }
127 
Rewind(int marker)128         public virtual void Rewind(int marker) {
129             dbg.Rewind(marker);
130             input.Rewind(marker);
131         }
132 
Rewind()133         public virtual void Rewind() {
134             dbg.Rewind();
135             input.Rewind(lastMarker);
136         }
137 
Release(int marker)138         public virtual void Release(int marker) {
139         }
140 
Seek(int index)141         public virtual void Seek(int index) {
142             // TODO: implement seek in dbg interface
143             // db.seek(index);
144             input.Seek(index);
145         }
146 
147         public virtual int Count {
148             get {
149                 return input.Count;
150             }
151         }
152 
153         public virtual ITokenSource TokenSource {
154             get {
155                 return input.TokenSource;
156             }
157         }
158 
159         public virtual string SourceName {
160             get {
161                 return TokenSource.SourceName;
162             }
163         }
164 
ToString()165         public override string ToString() {
166             return input.ToString();
167         }
168 
ToString(int start, int stop)169         public virtual string ToString(int start, int stop) {
170             return input.ToString(start, stop);
171         }
172 
ToString(IToken start, IToken stop)173         public virtual string ToString(IToken start, IToken stop) {
174             return input.ToString(start, stop);
175         }
176     }
177 }
178