| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* (c) Armin Ronacher |
| 8 |
* |
| 9 |
* For the full copyright and license information, please view the LICENSE |
| 10 |
* file that was distributed with this source code. |
| 11 |
*/ |
| 12 |
namespace ElementorDeps\Twig; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 15 |
/** |
| 16 |
* Represents a token stream. |
| 17 |
* |
| 18 |
* @author Fabien Potencier <fabien@symfony.com> |
| 19 |
*/ |
| 20 |
final class TokenStream |
| 21 |
{ |
| 22 |
private $tokens; |
| 23 |
private $current = 0; |
| 24 |
private $source; |
| 25 |
public function __construct(array $tokens, ?Source $source = null) |
| 26 |
{ |
| 27 |
$this->tokens = $tokens; |
| 28 |
$this->source = $source ?: new Source('', ''); |
| 29 |
} |
| 30 |
public function __toString() |
| 31 |
{ |
| 32 |
return \implode("\n", $this->tokens); |
| 33 |
} |
| 34 |
public function injectTokens(array $tokens) |
| 35 |
{ |
| 36 |
$this->tokens = \array_merge(\array_slice($this->tokens, 0, $this->current), $tokens, \array_slice($this->tokens, $this->current)); |
| 37 |
} |
| 38 |
/** |
| 39 |
* Sets the pointer to the next token and returns the old one. |
| 40 |
*/ |
| 41 |
public function next() : Token |
| 42 |
{ |
| 43 |
if (!isset($this->tokens[++$this->current])) { |
| 44 |
throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source); |
| 45 |
} |
| 46 |
return $this->tokens[$this->current - 1]; |
| 47 |
} |
| 48 |
/** |
| 49 |
* Tests a token, sets the pointer to the next one and returns it or throws a syntax error. |
| 50 |
* |
| 51 |
* @return Token|null The next token if the condition is true, null otherwise |
| 52 |
*/ |
| 53 |
public function nextIf($primary, $secondary = null) |
| 54 |
{ |
| 55 |
return $this->tokens[$this->current]->test($primary, $secondary) ? $this->next() : null; |
| 56 |
} |
| 57 |
/** |
| 58 |
* Tests a token and returns it or throws a syntax error. |
| 59 |
*/ |
| 60 |
public function expect($type, $value = null, ?string $message = null) : Token |
| 61 |
{ |
| 62 |
$token = $this->tokens[$this->current]; |
| 63 |
if (!$token->test($type, $value)) { |
| 64 |
$line = $token->getLine(); |
| 65 |
throw new SyntaxError(\sprintf('%sUnexpected token "%s"%s ("%s" expected%s).', $message ? $message . '. ' : '', Token::typeToEnglish($token->getType()), $token->getValue() ? \sprintf(' of value "%s"', $token->getValue()) : '', Token::typeToEnglish($type), $value ? \sprintf(' with value "%s"', $value) : ''), $line, $this->source); |
| 66 |
} |
| 67 |
$this->next(); |
| 68 |
return $token; |
| 69 |
} |
| 70 |
/** |
| 71 |
* Looks at the next token. |
| 72 |
*/ |
| 73 |
public function look(int $number = 1) : Token |
| 74 |
{ |
| 75 |
if (!isset($this->tokens[$this->current + $number])) { |
| 76 |
throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source); |
| 77 |
} |
| 78 |
return $this->tokens[$this->current + $number]; |
| 79 |
} |
| 80 |
/** |
| 81 |
* Tests the current token. |
| 82 |
*/ |
| 83 |
public function test($primary, $secondary = null) : bool |
| 84 |
{ |
| 85 |
return $this->tokens[$this->current]->test($primary, $secondary); |
| 86 |
} |
| 87 |
/** |
| 88 |
* Checks if end of stream was reached. |
| 89 |
*/ |
| 90 |
public function isEOF() : bool |
| 91 |
{ |
| 92 |
return -1 === $this->tokens[$this->current]->getType(); |
| 93 |
} |
| 94 |
public function getCurrent() : Token |
| 95 |
{ |
| 96 |
return $this->tokens[$this->current]; |
| 97 |
} |
| 98 |
/** |
| 99 |
* Gets the source associated with this stream. |
| 100 |
* |
| 101 |
* @internal |
| 102 |
*/ |
| 103 |
public function getSourceContext() : Source |
| 104 |
{ |
| 105 |
return $this->source; |
| 106 |
} |
| 107 |
} |
| 108 |
|