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
/* 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.Collections.Generic;
using Antlr4.Runtime;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime
{
/// <summary>
/// Provides an implementation of
/// <see cref="ITokenSource"/>
/// as a wrapper around a list
/// of
/// <see cref="IToken"/>
/// objects.
/// <p>If the final token in the list is an
/// <see cref="TokenConstants.EOF"/>
/// token, it will be used
/// as the EOF token for every call to
/// <see cref="NextToken()"/>
/// after the end of the
/// list is reached. Otherwise, an EOF token will be created.</p>
/// </summary>
public class ListTokenSource : ITokenSource
{
/// <summary>
/// The wrapped collection of
/// <see cref="IToken"/>
/// objects to return.
/// </summary>
protected internal readonly IList<IToken> tokens;
/// <summary>The name of the input source.</summary>
/// <remarks>
/// The name of the input source. If this value is
/// <see langword="null"/>
/// , a call to
/// <see cref="SourceName()"/>
/// should return the source name used to create the
/// the next token in
/// <see cref="tokens"/>
/// (or the previous token if the end of
/// the input has been reached).
/// </remarks>
private readonly string sourceName;
/// <summary>
/// The index into
/// <see cref="tokens"/>
/// of token to return by the next call to
/// <see cref="NextToken()"/>
/// . The end of the input is indicated by this value
/// being greater than or equal to the number of items in
/// <see cref="tokens"/>
/// .
/// </summary>
protected internal int i;
/// <summary>This field caches the EOF token for the token source.</summary>
/// <remarks>This field caches the EOF token for the token source.</remarks>
protected internal IToken eofToken;
/// <summary>
/// This is the backing field for the <see cref="TokenFactory"/> property.
/// </summary>
private ITokenFactory _factory = CommonTokenFactory.Default;
/// <summary>
/// Constructs a new
/// <see cref="ListTokenSource"/>
/// instance from the specified
/// collection of
/// <see cref="IToken"/>
/// objects.
/// </summary>
/// <param name="tokens">
/// The collection of
/// <see cref="IToken"/>
/// objects to provide as a
/// <see cref="ITokenSource"/>
/// .
/// </param>
/// <exception>
/// NullPointerException
/// if
/// <paramref name="tokens"/>
/// is
/// <see langword="null"/>
/// </exception>
public ListTokenSource(IList<IToken> tokens)
: this(tokens, null)
{
}
/// <summary>
/// Constructs a new
/// <see cref="ListTokenSource"/>
/// instance from the specified
/// collection of
/// <see cref="IToken"/>
/// objects and source name.
/// </summary>
/// <param name="tokens">
/// The collection of
/// <see cref="IToken"/>
/// objects to provide as a
/// <see cref="ITokenSource"/>
/// .
/// </param>
/// <param name="sourceName">
/// The name of the
/// <see cref="ITokenSource"/>
/// . If this value is
/// <see langword="null"/>
/// ,
/// <see cref="SourceName()"/>
/// will attempt to infer the name from
/// the next
/// <see cref="IToken"/>
/// (or the previous token if the end of the input has
/// been reached).
/// </param>
/// <exception>
/// NullPointerException
/// if
/// <paramref name="tokens"/>
/// is
/// <see langword="null"/>
/// </exception>
public ListTokenSource(IList<IToken> tokens, string sourceName)
{
if (tokens == null)
{
throw new ArgumentNullException("tokens cannot be null");
}
this.tokens = tokens;
this.sourceName = sourceName;
}
/// <summary><inheritDoc/></summary>
public virtual int Column
{
get
{
if (i < tokens.Count)
{
return tokens[i].Column;
}
else
{
if (eofToken != null)
{
return eofToken.Column;
}
else
{
if (tokens.Count > 0)
{
// have to calculate the result from the line/column of the previous
// token, along with the text of the token.
IToken lastToken = tokens[tokens.Count - 1];
string tokenText = lastToken.Text;
if (tokenText != null)
{
int lastNewLine = tokenText.LastIndexOf('\n');
if (lastNewLine >= 0)
{
return tokenText.Length - lastNewLine - 1;
}
}
return lastToken.Column + lastToken.StopIndex - lastToken.StartIndex + 1;
}
}
}
// only reach this if tokens is empty, meaning EOF occurs at the first
// position in the input
return 0;
}
}
/// <summary><inheritDoc/></summary>
public virtual IToken NextToken()
{
if (i >= tokens.Count)
{
if (eofToken == null)
{
int start = -1;
if (tokens.Count > 0)
{
int previousStop = tokens[tokens.Count - 1].StopIndex;
if (previousStop != -1)
{
start = previousStop + 1;
}
}
int stop = Math.Max(-1, start - 1);
eofToken = _factory.Create(Tuple.Create((ITokenSource)this, InputStream), TokenConstants.EOF, "EOF", TokenConstants.DefaultChannel, start, stop, Line, Column);
}
return eofToken;
}
IToken t = tokens[i];
if (i == tokens.Count - 1 && t.Type == TokenConstants.EOF)
{
eofToken = t;
}
i++;
return t;
}
/// <summary><inheritDoc/></summary>
public virtual int Line
{
get
{
if (i < tokens.Count)
{
return tokens[i].Line;
}
else
{
if (eofToken != null)
{
return eofToken.Line;
}
else
{
if (tokens.Count > 0)
{
// have to calculate the result from the line/column of the previous
// token, along with the text of the token.
IToken lastToken = tokens[tokens.Count - 1];
int line = lastToken.Line;
string tokenText = lastToken.Text;
if (tokenText != null)
{
for (int j = 0; j < tokenText.Length; j++)
{
if (tokenText[j] == '\n')
{
line++;
}
}
}
// if no text is available, assume the token did not contain any newline characters.
return line;
}
}
}
// only reach this if tokens is empty, meaning EOF occurs at the first
// position in the input
return 1;
}
}
/// <summary><inheritDoc/></summary>
public virtual ICharStream InputStream
{
get
{
if (i < tokens.Count)
{
return tokens[i].InputStream;
}
else
{
if (eofToken != null)
{
return eofToken.InputStream;
}
else
{
if (tokens.Count > 0)
{
return tokens[tokens.Count - 1].InputStream;
}
}
}
// no input stream information is available
return null;
}
}
/// <summary><inheritDoc/></summary>
public virtual string SourceName
{
get
{
if (sourceName != null)
{
return sourceName;
}
ICharStream inputStream = InputStream;
if (inputStream != null)
{
return inputStream.SourceName;
}
return "List";
}
}
/// <summary><inheritDoc/></summary>
/// <summary><inheritDoc/></summary>
public virtual ITokenFactory TokenFactory
{
get
{
return _factory;
}
set
{
ITokenFactory factory = value;
this._factory = factory;
}
}
}
}