4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
7 namespace Wikimedia\CSS\Parser;
9 use Wikimedia\CSS\Util;
10 use Wikimedia\CSS\Objects\Token;
11 use Wikimedia\CSS\Objects\TokenList;
14 * Tokenizer that just returns a predefined list of tokens
16 class TokenListTokenizer implements Tokenizer {
25 * @param Token[]|TokenList $tokens Tokens to return
26 * @param Token|null $eof Token to copy as EOF
28 public function __construct( $tokens, Token $eof = null ) {
29 if ( $tokens instanceof TokenList ) {
30 $this->tokens = $tokens->toTokenArray();
31 } elseif ( is_array( $tokens ) ) {
32 Util::assertAllInstanceOf( $tokens, Token::class, '$tokens' );
33 $this->tokens = $tokens;
35 throw new \InvalidArgumentException( '$tokens must be a TokenList or an array of tokens' );
38 if ( $eof && $eof->type() === Token::T_EOF ) {
43 $data['position'] = $eof->getPosition();
45 $this->eof = new Token( Token::T_EOF, $data );
49 public function getParseErrors() {
53 public function clearParseErrors() {
56 public function consumeToken() {
57 return array_shift( $this->tokens ) ?: $this->eof;