Skip to content

Commit 734aa46

Browse files
committed
TASK: Fix Tokenizer not tokenizing all tokens
for example ExpressionNode::fromString('(true) ? 42 : "foo"') would previously only tokenize `(true)` due to the brace this is not an issue, when used in the return statement
1 parent e678017 commit 734aa46

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

src/Parser/Ast/ExpressionNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static function fromTokens(\Iterator &$tokens, Precedence $precedence = P
126126
);
127127
}
128128

129-
while ($tokens->valid()) {
129+
while (!Scanner::isEnd($tokens)) {
130130
Scanner::skipSpaceAndComments($tokens);
131131

132132
switch (Scanner::type($tokens)) {

src/Parser/Tokenizer/Scanner.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,14 @@ public static function isEnd(\Iterator $tokens): bool
169169
/**
170170
* @param \Iterator<mixed,Token> $tokens
171171
*/
172-
public static function debugPrint(\Iterator $tokens): string
172+
public static function debugPrint(\Iterator &$tokens): string
173173
{
174+
$tokens = (function(): \Generator {
175+
throw new \Exception('Once debugged, $tokens is empty.');
176+
// @phpstan-ignore-next-line
177+
yield;
178+
})();
179+
174180
$tokensAsArray = [];
175181
while ($tokens->valid()) {
176182
$tokensAsArray[] = [

src/Parser/Tokenizer/Tokenizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public static function fromSource(Source $source): Tokenizer
4444
*/
4545
public function getIterator(): \Iterator
4646
{
47-
yield from self::block($this->source->getIterator());
47+
$fragments = $this->source->getIterator();
48+
while ($fragments->valid()) {
49+
yield from self::block($fragments);
50+
}
4851
}
4952

5053
/**

test/Unit/Target/Php/Transpiler/TernaryOperation/TernaryOperationTranspilerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function ternaryOperationExamples(): array
3737
{
3838
return [
3939
'true ? 42 : "foo"' => ['true ? 42 : "foo"', '(true ? 42 : \'foo\')'],
40+
'(true) ? 42 : "foo"' => ['(true) ? 42 : "foo"', '(true ? 42 : \'foo\')'],
4041
'a ? 42 : "foo"' => ['a ? 42 : "foo"', '($this->a ? 42 : \'foo\')'],
4142
'true ? b : "foo"' => ['true ? b : "foo"', '(true ? $this->b : \'foo\')'],
4243
'true ? 42 : c' => ['true ? 42 : c', '(true ? 42 : $this->c)'],
@@ -70,4 +71,4 @@ public function transpilesTernaryOperationNodes(string $ternaryOperationAsString
7071
$actualTranspilationResult
7172
);
7273
}
73-
}
74+
}

0 commit comments

Comments
 (0)