1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
using System;
using System.Text;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime
{
public class UnbufferedTokenStream : ITokenStream
{
private ITokenSource _tokenSource;
/// <summary>A moving window buffer of the data being scanned.</summary>
/// <remarks>
/// A moving window buffer of the data being scanned. While there's a marker,
/// we keep adding to buffer. Otherwise,
/// <see cref="Consume()">consume()</see>
/// resets so
/// we start filling at index 0 again.
/// </remarks>
protected internal IToken[] tokens;
/// <summary>
/// The number of tokens currently in
/// <see cref="tokens">tokens</see>
/// .
/// <p>This is not the buffer capacity, that's
/// <c>tokens.length</c>
/// .</p>
/// </summary>
protected internal int n;
/// <summary>
/// 0..n-1 index into
/// <see cref="tokens">tokens</see>
/// of next token.
/// <p>The
/// <c>LT(1)</c>
/// token is
/// <c>tokens[p]</c>
/// . If
/// <c>p == n</c>
/// , we are
/// out of buffered tokens.</p>
/// </summary>
protected internal int p = 0;
/// <summary>
/// Count up with
/// <see cref="Mark()">mark()</see>
/// and down with
/// <see cref="Release(int)">release()</see>
/// . When we
/// <c>release()</c>
/// the last mark,
/// <c>numMarkers</c>
/// reaches 0 and we reset the buffer. Copy
/// <c>tokens[p]..tokens[n-1]</c>
/// to
/// <c>tokens[0]..tokens[(n-1)-p]</c>
/// .
/// </summary>
protected internal int numMarkers = 0;
/// <summary>
/// This is the
/// <c>LT(-1)</c>
/// token for the current position.
/// </summary>
protected internal IToken lastToken;
/// <summary>
/// When
/// <c>numMarkers > 0</c>
/// , this is the
/// <c>LT(-1)</c>
/// token for the
/// first token in
/// <see cref="tokens"/>
/// . Otherwise, this is
/// <see langword="null"/>
/// .
/// </summary>
protected internal IToken lastTokenBufferStart;
/// <summary>Absolute token index.</summary>
/// <remarks>
/// Absolute token index. It's the index of the token about to be read via
/// <c>LT(1)</c>
/// . Goes from 0 to the number of tokens in the entire stream,
/// although the stream size is unknown before the end is reached.
/// <p>This value is used to set the token indexes if the stream provides tokens
/// that implement
/// <see cref="IWritableToken"/>
/// .</p>
/// </remarks>
protected internal int currentTokenIndex = 0;
public UnbufferedTokenStream(ITokenSource tokenSource)
: this(tokenSource, 256)
{
}
public UnbufferedTokenStream(ITokenSource tokenSource, int bufferSize)
{
this.TokenSource = tokenSource;
this.tokens = new IToken[bufferSize];
n = 0;
Fill(1);
}
// prime the pump
public virtual IToken Get(int i)
{
int bufferStartIndex = GetBufferStartIndex();
if (i < bufferStartIndex || i >= bufferStartIndex + n)
{
throw new ArgumentOutOfRangeException("get(" + i + ") outside buffer: " + bufferStartIndex + ".." + (bufferStartIndex + n));
}
return tokens[i - bufferStartIndex];
}
public virtual IToken LT(int i)
{
if (i == -1)
{
return lastToken;
}
Sync(i);
int index = p + i - 1;
if (index < 0)
{
throw new ArgumentOutOfRangeException("LT(" + i + ") gives negative index");
}
if (index >= n)
{
System.Diagnostics.Debug.Assert(n > 0 && tokens[n - 1].Type == TokenConstants.EOF);
return tokens[n - 1];
}
return tokens[index];
}
public virtual int LA(int i)
{
return LT(i).Type;
}
public virtual ITokenSource TokenSource
{
get
{
return _tokenSource;
}
set
{
_tokenSource = value;
}
}
[return: NotNull]
public virtual string GetText()
{
return string.Empty;
}
[return: NotNull]
public virtual string GetText(RuleContext ctx)
{
return GetText(ctx.SourceInterval);
}
[return: NotNull]
public virtual string GetText(IToken start, IToken stop)
{
if (start != null && stop != null)
{
return GetText(Interval.Of(start.TokenIndex, stop.TokenIndex));
}
throw new NotSupportedException("The specified start and stop symbols are not supported.");
}
public virtual void Consume()
{
if (LA(1) == TokenConstants.EOF)
{
throw new InvalidOperationException("cannot consume EOF");
}
// buf always has at least tokens[p==0] in this method due to ctor
lastToken = tokens[p];
// track last token for LT(-1)
// if we're at last token and no markers, opportunity to flush buffer
if (p == n - 1 && numMarkers == 0)
{
n = 0;
p = -1;
// p++ will leave this at 0
lastTokenBufferStart = lastToken;
}
p++;
currentTokenIndex++;
Sync(1);
}
/// <summary>
/// Make sure we have 'need' elements from current position
/// <see cref="p">p</see>
/// . Last valid
/// <c>p</c>
/// index is
/// <c>tokens.length-1</c>
/// .
/// <c>p+need-1</c>
/// is the tokens index 'need' elements
/// ahead. If we need 1 element,
/// <c>(p+1-1)==p</c>
/// must be less than
/// <c>tokens.length</c>
/// .
/// </summary>
protected internal virtual void Sync(int want)
{
int need = (p + want - 1) - n + 1;
// how many more elements we need?
if (need > 0)
{
Fill(need);
}
}
/// <summary>
/// Add
/// <paramref name="n"/>
/// elements to the buffer. Returns the number of tokens
/// actually added to the buffer. If the return value is less than
/// <paramref name="n"/>
/// ,
/// then EOF was reached before
/// <paramref name="n"/>
/// tokens could be added.
/// </summary>
protected internal virtual int Fill(int n)
{
for (int i = 0; i < n; i++)
{
if (this.n > 0 && tokens[this.n - 1].Type == TokenConstants.EOF)
{
return i;
}
IToken t = TokenSource.NextToken();
Add(t);
}
return n;
}
protected internal virtual void Add(IToken t)
{
if (n >= tokens.Length)
{
tokens = Arrays.CopyOf(tokens, tokens.Length * 2);
}
if (t is IWritableToken)
{
((IWritableToken)t).TokenIndex = GetBufferStartIndex() + n;
}
tokens[n++] = t;
}
/// <summary>Return a marker that we can release later.</summary>
/// <remarks>
/// Return a marker that we can release later.
/// <p>The specific marker value used for this class allows for some level of
/// protection against misuse where
/// <c>seek()</c>
/// is called on a mark or
/// <c>release()</c>
/// is called in the wrong order.</p>
/// </remarks>
public virtual int Mark()
{
if (numMarkers == 0)
{
lastTokenBufferStart = lastToken;
}
int mark = -numMarkers - 1;
numMarkers++;
return mark;
}
public virtual void Release(int marker)
{
int expectedMark = -numMarkers;
if (marker != expectedMark)
{
throw new InvalidOperationException("release() called with an invalid marker.");
}
numMarkers--;
if (numMarkers == 0)
{
// can we release buffer?
if (p > 0)
{
// Copy tokens[p]..tokens[n-1] to tokens[0]..tokens[(n-1)-p], reset ptrs
// p is last valid token; move nothing if p==n as we have no valid char
System.Array.Copy(tokens, p, tokens, 0, n - p);
// shift n-p tokens from p to 0
n = n - p;
p = 0;
}
lastTokenBufferStart = lastToken;
}
}
public virtual int Index
{
get
{
return currentTokenIndex;
}
}
public virtual void Seek(int index)
{
// seek to absolute index
if (index == currentTokenIndex)
{
return;
}
if (index > currentTokenIndex)
{
Sync(index - currentTokenIndex);
index = Math.Min(index, GetBufferStartIndex() + n - 1);
}
int bufferStartIndex = GetBufferStartIndex();
int i = index - bufferStartIndex;
if (i < 0)
{
throw new ArgumentException("cannot seek to negative index " + index);
}
else
{
if (i >= n)
{
throw new NotSupportedException("seek to index outside buffer: " + index + " not in " + bufferStartIndex + ".." + (bufferStartIndex + n));
}
}
p = i;
currentTokenIndex = index;
if (p == 0)
{
lastToken = lastTokenBufferStart;
}
else
{
lastToken = tokens[p - 1];
}
}
public virtual int Size
{
get
{
throw new NotSupportedException("Unbuffered stream cannot know its size");
}
}
public virtual string SourceName
{
get
{
return TokenSource.SourceName;
}
}
[return: NotNull]
public virtual string GetText(Interval interval)
{
int bufferStartIndex = GetBufferStartIndex();
int bufferStopIndex = bufferStartIndex + tokens.Length - 1;
int start = interval.a;
int stop = interval.b;
if (start < bufferStartIndex || stop > bufferStopIndex)
{
throw new NotSupportedException("interval " + interval + " not in token buffer window: " + bufferStartIndex + ".." + bufferStopIndex);
}
int a = start - bufferStartIndex;
int b = stop - bufferStartIndex;
StringBuilder buf = new StringBuilder();
for (int i = a; i <= b; i++)
{
IToken t = tokens[i];
buf.Append(t.Text);
}
return buf.ToString();
}
protected internal int GetBufferStartIndex()
{
return currentTokenIndex - p;
}
}
}