-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathLexer.php
525 lines (430 loc) · 14.3 KB
/
Lexer.php
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
declare(strict_types=1);
namespace Antlr\Antlr4\Runtime;
use Antlr\Antlr4\Runtime\Atn\LexerATNSimulator;
use Antlr\Antlr4\Runtime\Error\Exceptions\LexerNoViableAltException;
use Antlr\Antlr4\Runtime\Error\Exceptions\RecognitionException;
use Antlr\Antlr4\Runtime\Utils\Pair;
/**
* A lexer is recognizer that draws input symbols from a character stream.
* lexer grammars result in a subclass of this object. A Lexer object
* uses simplified match() and error recovery mechanisms in the interest
* of speed.
*/
abstract class Lexer extends Recognizer implements TokenSource
{
public const DEFAULT_MODE = 0;
public const MORE = -2;
public const SKIP = -3;
public const DEFAULT_TOKEN_CHANNEL = Token::DEFAULT_CHANNEL;
public const HIDDEN = Token::HIDDEN_CHANNEL;
public const MIN_CHAR_VALUE = 0x0000;
public const MAX_CHAR_VALUE = 0x10FFFF;
public ?CharStream $input = null;
/** @var Pair Pair<TokenSource, CharStream> */
protected Pair $tokenFactorySourcePair;
protected TokenFactory $factory;
/**
* The goal of all lexer rules/methods is to create a token object.
* This is an instance variable as multiple rules may collaborate to
* create a single token. `nextToken` will return this object after
* matching lexer rule(s).
*
* If you subclass to allow multiple token emissions, then set this
* to the last token to be matched or something nonnull so that
* the auto token emit mechanism will not emit another token.
*/
public ?Token $token = null;
/**
* What character index in the stream did the current token start at?
* Needed, for example, to get the text for current token. Set at
* the start of nextToken.
*/
public int $tokenStartCharIndex = -1;
/**
* The line on which the first character of the token resides.
*/
public int $tokenStartLine = -1;
/**
* The character position of first character within the line
*/
public int $tokenStartCharPositionInLine = -1;
/**
* Once we see EOF on char stream, next token will be EOF.
* If you have DONE : EOF ; then you see DONE EOF.
*/
public bool $hitEOF = false;
/**
* The channel number for the current token.
*/
public int $channel = Token::DEFAULT_CHANNEL;
/**
* The token type for the current token.
*/
public int $type = Token::INVALID_TYPE;
/** @var array<int> */
public array $modeStack = [];
public int $mode = self::DEFAULT_MODE;
/**
* You can set the text for the current token to override what is in the
* input char buffer. Use {@see Lexer::setText()} or can set this instance var.
*/
public ?string $text = null;
public function __construct(?CharStream $input = null)
{
parent::__construct();
$this->input = $input;
$this->factory = CommonTokenFactory::default();
$this->tokenFactorySourcePair = new Pair($this, $input);
}
public function reset(): void
{
// wack Lexer state variables
if ($this->input !== null) {
$this->input->seek(0);// rewind the input
}
$this->token = null;
$this->type = Token::INVALID_TYPE;
$this->channel = Token::DEFAULT_CHANNEL;
$this->tokenStartCharIndex = -1;
$this->tokenStartCharPositionInLine = -1;
$this->tokenStartLine = -1;
$this->text = null;
$this->hitEOF = false;
$this->mode = self::DEFAULT_MODE;
$this->modeStack = [];
if ($this->interp !== null) {
$this->interp->reset();
}
}
/**
* Return a token from this source; i.e., match a token on the char stream.
*/
public function nextToken(): ?Token
{
$input = $this->input;
if ($input === null) {
throw new \LogicException('NextToken requires a non-null input stream.');
}
$interpreter = $this->interp;
if (!$interpreter instanceof LexerATNSimulator) {
throw new \LogicException('Unexpected interpreter type.');
}
// Mark start location in char stream so unbuffered streams are
// guaranteed at least have text of current token
$tokenStartMarker = $input->mark();
try {
while (true) {
if ($this->hitEOF) {
$this->emitEOF();
return $this->token;
}
$this->token = null;
$this->channel = Token::DEFAULT_CHANNEL;
$this->tokenStartCharIndex = $input->getIndex();
$this->tokenStartCharPositionInLine = $interpreter->getCharPositionInLine();
$this->tokenStartLine = $interpreter->getLine();
$this->text = null;
$continueOuter = false;
while (true) {
$this->type = Token::INVALID_TYPE;
$ttype = self::SKIP;
try {
$ttype = $interpreter->match($input, $this->mode);
} catch (LexerNoViableAltException $e) {
$this->notifyListeners($e); // report error
$this->recover($e);
}
if ($input->LA(1) === Token::EOF) {
$this->hitEOF = true;
}
if ($this->type === Token::INVALID_TYPE) {
$this->type = $ttype;
}
if ($this->type === self::SKIP) {
$continueOuter = true;
break;
}
if ($this->type !== self::MORE) {
break;
}
}
if ($continueOuter) {
continue;
}
if ($this->token === null) {
$this->emit();
}
return $this->token;
}
} finally {
// make sure we release marker after match or
// unbuffered char stream will keep buffering
$input->release($tokenStartMarker);
}
}
/**
* Instruct the lexer to skip creating a token for current lexer rule
* and look for another token. `nextToken` knows to keep looking when
* a lexer rule finishes with token set to SKIP_TOKEN. Recall that
* if `token === null` at end of any token rule, it creates one for you
* and emits it.
*/
public function skip(): void
{
$this->type = self::SKIP;
}
public function more(): void
{
$this->type = self::MORE;
}
public function mode(int $m): void
{
$this->mode = $m;
}
public function pushMode(int $m): void
{
$this->modeStack[] = $this->mode;
$this->mode($m);
}
public function popMode(): int
{
if (\count($this->modeStack) === 0) {
throw new \LogicException('Empty Stack');
}
$this->mode(\array_pop($this->modeStack));
return $this->mode;
}
public function getSourceName(): string
{
return $this->input === null ? '' : $this->input->getSourceName();
}
public function getInputStream(): ?IntStream
{
return $this->input;
}
public function getTokenFactory(): TokenFactory
{
return $this->factory;
}
public function setTokenFactory(TokenFactory $factory): void
{
$this->factory = $factory;
}
public function setInputStream(IntStream $input): void
{
$this->input = null;
$this->tokenFactorySourcePair = new Pair($this, $this->input);
$this->reset();
if (!$input instanceof CharStream) {
throw new \LogicException('Input must be CharStream.');
}
$this->input = $input;
$this->tokenFactorySourcePair = new Pair($this, $this->input);
}
/**
* By default does not support multiple emits per nextToken invocation
* for efficiency reasons. Subclass and override this method, nextToken,
* and getToken (to push tokens into a list and pull from that list
* rather than a single variable as this implementation does).
*/
public function emitToken(Token $token): void
{
$this->token = $token;
}
/**
* The standard method called to automatically emit a token at the
* outermost lexical rule. The token object should point into the
* char buffer start..stop. If there is a text override in 'text',
* use that to set the token's text. Override this method to emit
* custom Token objects or provide a new factory.
*/
public function emit(): Token
{
$token = $this->factory->createEx(
$this->tokenFactorySourcePair,
$this->type,
$this->text,
$this->channel,
$this->tokenStartCharIndex,
$this->getCharIndex() - 1,
$this->tokenStartLine,
$this->tokenStartCharPositionInLine,
);
$this->emitToken($token);
return $token;
}
public function emitEOF(): Token
{
if ($this->input === null) {
throw new \LogicException('Cannot emit EOF for null stream.');
}
$cpos = $this->getCharPositionInLine();
$lpos = $this->getLine();
$eof = $this->factory->createEx(
$this->tokenFactorySourcePair,
Token::EOF,
null,
Token::DEFAULT_CHANNEL,
$this->input->getIndex(),
$this->input->getIndex() - 1,
$lpos,
$cpos,
);
$this->emitToken($eof);
return $eof;
}
public function getLine(): int
{
if (!$this->interp instanceof LexerATNSimulator) {
throw new \LogicException('Unexpected interpreter type.');
}
return $this->interp->getLine();
}
public function setLine(int $line): void
{
if (!$this->interp instanceof LexerATNSimulator) {
throw new \LogicException('Unexpected interpreter type.');
}
$this->interp->setLine($line);
}
public function getCharPositionInLine(): int
{
if (!$this->interp instanceof LexerATNSimulator) {
throw new \LogicException('Unexpected interpreter type.');
}
return $this->interp->getCharPositionInLine();
}
public function setCharPositionInLine(int $charPositionInLine): void
{
if (!$this->interp instanceof LexerATNSimulator) {
throw new \LogicException('Unexpected interpreter type.');
}
$this->interp->setCharPositionInLine($charPositionInLine);
}
/**
* What is the index of the current character of lookahead?
*/
public function getCharIndex(): int
{
if ($this->input === null) {
throw new \LogicException('Cannot know char index for null stream.');
}
return $this->input->getIndex();
}
/**
* Return the text matched so far for the current token or any text override.
*/
public function getText(): string
{
if ($this->text !== null) {
return $this->text;
}
if (!$this->interp instanceof LexerATNSimulator) {
throw new \LogicException('Unexpected interpreter type.');
}
return $this->input === null ? '' : $this->interp->getText($this->input);
}
/**
* Set the complete text of this token; it wipes any previous changes to the text.
*/
public function setText(string $text): void
{
$this->text = $text;
}
public function getToken(): ?Token
{
return $this->token;
}
/**
* Override if emitting multiple tokens.
*/
public function setToken(Token $token): void
{
$this->token = $token;
}
public function getType(): int
{
return $this->type;
}
public function setType(int $type): void
{
$this->type = $type;
}
public function getChannel(): int
{
return $this->channel;
}
public function setChannel(int $channel): void
{
$this->channel = $channel;
}
/**
* @return array<string>|null
*/
public function getChannelNames(): ?array
{
return null;
}
/**
* @return array<string>|null
*/
public function getModeNames(): ?array
{
return null;
}
/**
* Return a list of all Token objects in input char stream.
* Forces load of all tokens. Does not include EOF token.
*
* @return array<Token>
*/
public function getAllTokens(): array
{
$tokens = [];
$token = $this->nextToken();
while ($token !== null && $token->getType() !== Token::EOF) {
$tokens[] = $token;
$token = $this->nextToken();
}
return $tokens;
}
/**
* Lexers can normally match any char in it's vocabulary after matching
* a token, so do the easy thing and just kill a character and hope
* it all works out. You can instead use the rule invocation stack
* to do sophisticated error recovery if you are in a fragment rule.
*/
public function recover(RecognitionException $re): void
{
if ($this->input !== null && $this->input->LA(1) !== Token::EOF) {
if ($re instanceof LexerNoViableAltException
&& $this->interp instanceof LexerATNSimulator) {
// skip a char and try again
$this->interp->consume($this->input);
} else {
// TODO: Do we lose character or line position information?
$this->input->consume();
}
}
}
public function notifyListeners(LexerNoViableAltException $e): void
{
$start = $this->tokenStartCharIndex;
if ($this->input === null) {
$text = '';
} else {
$stop = $this->input->getIndex();
$text = $this->input->getText($start, $stop);
}
$listener = $this->getErrorListenerDispatch();
$listener->syntaxError(
$this,
null,
$this->tokenStartLine,
$this->tokenStartCharPositionInLine,
\sprintf('token recognition error at: \'%s\'', $text),
$e,
);
}
}