-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenStream.php
More file actions
143 lines (124 loc) · 3.37 KB
/
TokenStream.php
File metadata and controls
143 lines (124 loc) · 3.37 KB
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
<?php
declare(strict_types=1);
namespace Qubus\View;
use function count;
use function implode;
use function is_array;
use function is_int;
use function sprintf;
use function str_replace;
final class TokenStream
{
public array $tokens {
get => $this->tokens;
}
public Token $currentToken {
get => $this->currentToken;
}
protected array $queue;
protected int $cursor;
protected bool $eos;
public function __construct(array $tokens)
{
$this->tokens = $tokens;
$this->currentToken = new Token(Token::EOF, null, 1, 1);
$this->queue = [];
$this->cursor = 0;
$this->eos = false;
$this->next();
}
public function next(): Token
{
if ($this->eos) {
return $this->currentToken;
}
$token = $this->tokens[$this->cursor++];
$old = $this->currentToken;
$this->currentToken = $token;
$this->eos = $token->type === Token::EOF;
return $old;
}
public function look(int $t = 1): Token
{
$t--;
$length = count($this->tokens);
if ($this->cursor + $t > $length) {
$t = 0;
}
if ($this->cursor + $t < 0) {
$t = -$this->cursor;
}
return $this->tokens[$this->cursor + $t];
}
public function skip(int $times = 1): TokenStream
{
for ($i = 0; $i < $times; $i++) {
$this->next();
}
return $this;
}
/**
* @throws SyntaxErrorException
*/
public function expect($primary, $secondary = null): Token
{
$token = $this->currentToken;
if (null === $secondary && ! is_int($primary)) {
$secondary = $primary;
$primary = Token::NAME;
}
if (! $token->test($primary, $secondary)) {
if (null === $secondary) {
$expecting = Token::getTypeError($primary);
} elseif (is_array($secondary)) {
$expecting = '"' . implode('" or "', $secondary) . '"';
} else {
$expecting = '"' . $secondary . '"';
}
if ($token->type === Token::EOF) {
throw new SyntaxErrorException('unexpected end of file', $token);
} else {
throw new SyntaxErrorException(
sprintf(
'unexpected "%s", expecting %s',
str_replace("\n", '\n', $token->value),
$expecting
),
$token
);
}
}
$this->next();
return $token;
}
/**
* @throws SyntaxErrorException
*/
public function expectTokens($tokens): TokenStream
{
foreach ($tokens as $token) {
$this->expect($token->type, $token->value);
}
return $this;
}
public function test($primary, $secondary = null): bool
{
return $this->currentToken->test($primary, $secondary);
}
/**
* @throws SyntaxErrorException
*/
public function consume($primary, $secondary = null): bool
{
if ($this->test($primary, $secondary)) {
$this->expect($primary, $secondary);
return true;
} else {
return false;
}
}
public function isEOS(): bool
{
return $this->eos;
}
}