vendor/symfony/expression-language/TokenStream.php line 59

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\ExpressionLanguage;
  11. /**
  12.  * Represents a token stream.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class TokenStream
  17. {
  18.     public $current;
  19.     private array $tokens;
  20.     private int $position = 0;
  21.     private string $expression;
  22.     public function __construct(array $tokens, string $expression = '')
  23.     {
  24.         $this->tokens = $tokens;
  25.         $this->current = $tokens[0];
  26.         $this->expression = $expression;
  27.     }
  28.     /**
  29.      * Returns a string representation of the token stream.
  30.      */
  31.     public function __toString(): string
  32.     {
  33.         return implode("\n", $this->tokens);
  34.     }
  35.     /**
  36.      * Sets the pointer to the next token and returns the old one.
  37.      */
  38.     public function next()
  39.     {
  40.         ++$this->position;
  41.         if (!isset($this->tokens[$this->position])) {
  42.             throw new SyntaxError('Unexpected end of expression.', $this->current->cursor, $this->expression);
  43.         }
  44.         $this->current = $this->tokens[$this->position];
  45.     }
  46.     /**
  47.      * @param string|null $message The syntax error message
  48.      */
  49.     public function expect(string $type, string $value = null, string $message = null)
  50.     {
  51.         $token = $this->current;
  52.         if (!$token->test($type, $value)) {
  53.             throw new SyntaxError(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s).', $message ? $message.'. ' : '', $token->type, $token->value, $type, $value ? sprintf(' with value "%s"', $value) : ''), $token->cursor, $this->expression);
  54.         }
  55.         $this->next();
  56.     }
  57.     /**
  58.      * Checks if end of stream was reached.
  59.      */
  60.     public function isEOF(): bool
  61.     {
  62.         return Token::EOF_TYPE === $this->current->type;
  63.     }
  64.     /**
  65.      * @internal
  66.      */
  67.     public function getExpression(): string
  68.     {
  69.         return $this->expression;
  70.     }
  71. }