-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
TokenStream.php
94 lines (72 loc) 路 1.91 KB
/
TokenStream.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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Neon;
/** @internal */
final class TokenStream
{
private int $index = 0;
public function __construct(
/** @var Token[] */
public /*readonly*/ array $tokens,
) {
}
public function getIndex(): int
{
return $this->index;
}
public function seek(int $index): void
{
$this->index = $index;
}
/**
* Tells whether the token at current position is of given kind.
*/
public function is(int|string ...$kind): bool
{
while ($this->tokens[$this->index]->is(Token::Comment, Token::Whitespace)) {
$this->index++;
}
return $kind
? $this->tokens[$this->index]->is(...$kind)
: $this->tokens[$this->index]->type !== Token::End;
}
/**
* Consumes the current token of given kind or returns null.
*/
public function tryConsume(int|string ...$kind): ?Token
{
return $this->is(...$kind)
? $this->tokens[$this->index++]
: null;
}
public function getIndentation(): string
{
return in_array($this->tokens[$this->index - 2]->type ?? null, [Token::Newline, null], strict: true)
&& ($this->tokens[$this->index - 1]->type ?? null) === Token::Whitespace
? $this->tokens[$this->index - 1]->text
: '';
}
/** @return never */
public function error(?string $message = null, ?int $pos = null): void
{
$pos ??= $this->index;
$input = '';
foreach ($this->tokens as $i => $token) {
if ($i >= $pos) {
break;
}
$input .= $token->text;
}
$line = substr_count($input, "\n") + 1;
$col = strlen($input) - strrpos("\n" . $input, "\n") + 1;
$token = $this->tokens[$pos];
$message ??= 'Unexpected ' . ($token->type === Token::End
? 'end'
: "'" . str_replace("\n", '<new line>', substr($this->tokens[$pos]->text, 0, 40)) . "'");
throw new Exception("$message on line $line at column $col");
}
}