Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TokenStream.php

class TokenStream

Same name in this branch
  1. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Internal/TokenStream.php \PhpParser\Internal\TokenStream
  2. 11.1.x vendor/symfony/css-selector/Parser/TokenStream.php \Symfony\Component\CssSelector\Parser\TokenStream

Represents a token stream.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Twig\TokenStream

Expanded class hierarchy of TokenStream

1 file declares its use of TokenStream
TypesTokenParser.php in vendor/twig/twig/src/TokenParser/TypesTokenParser.php

File

vendor/twig/twig/src/TokenStream.php, line 22

Namespace

Twig
View source
final class TokenStream {
    private $current = 0;
    public function __construct(array $tokens, ?Source $source = null) {
        if (null === $this->source) {
            trigger_deprecation('twig/twig', '3.16', \sprintf('Not passing a "%s" object to "%s" constructor is deprecated.', Source::class, __CLASS__));
            $this->source = new Source('', '');
        }
    }
    public function __toString() {
        return implode("\n", $this->tokens);
    }
    public function injectTokens(array $tokens) {
        $this->tokens = array_merge(\array_slice($this->tokens, 0, $this->current), $tokens, \array_slice($this->tokens, $this->current));
    }
    
    /**
     * Sets the pointer to the next token and returns the old one.
     */
    public function next() : Token {
        if (!isset($this->tokens[++$this->current])) {
            throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current - 1]
                ->getLine(), $this->source);
        }
        return $this->tokens[$this->current - 1];
    }
    
    /**
     * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
     *
     * @return Token|null The next token if the condition is true, null otherwise
     */
    public function nextIf($primary, $secondary = null) {
        return $this->tokens[$this->current]
            ->test($primary, $secondary) ? $this->next() : null;
    }
    
    /**
     * Tests a token and returns it or throws a syntax error.
     */
    public function expect($type, $value = null, ?string $message = null) : Token {
        $token = $this->tokens[$this->current];
        if (!$token->test($type, $value)) {
            $line = $token->getLine();
            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);
        }
        $this->next();
        return $token;
    }
    
    /**
     * Looks at the next token.
     */
    public function look(int $number = 1) : Token {
        if (!isset($this->tokens[$this->current + $number])) {
            throw new SyntaxError('Unexpected end of template.', $this->tokens[$this->current + $number - 1]
                ->getLine(), $this->source);
        }
        return $this->tokens[$this->current + $number];
    }
    
    /**
     * Tests the current token.
     */
    public function test($primary, $secondary = null) : bool {
        return $this->tokens[$this->current]
            ->test($primary, $secondary);
    }
    
    /**
     * Checks if end of stream was reached.
     */
    public function isEOF() : bool {
        return Token::EOF_TYPE === $this->tokens[$this->current]
            ->getType();
    }
    public function getCurrent() : Token {
        return $this->tokens[$this->current];
    }
    public function getSourceContext() : Source {
        return $this->source;
    }

}

Members

Title Sort descending Modifiers Object type Summary
TokenStream::$current private property
TokenStream::expect public function Tests a token and returns it or throws a syntax error.
TokenStream::getCurrent public function
TokenStream::getSourceContext public function
TokenStream::injectTokens public function
TokenStream::isEOF public function Checks if end of stream was reached.
TokenStream::look public function Looks at the next token.
TokenStream::next public function Sets the pointer to the next token and returns the old one.
TokenStream::nextIf public function Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
TokenStream::test public function Tests the current token.
TokenStream::__construct public function
TokenStream::__toString public function
RSS feed
Powered by Drupal