Debian Codegraph @

vendor/wikimedia/css-sanitizer/src/Parser/TokenListTokenizer.php

raw
1 <?php
2 /**
3 * @file
4 * @license https://opensource.org/licenses/Apache-2.0 Apache-2.0
5 */
6
7 namespace Wikimedia\CSS\Parser;
8
9 use InvalidArgumentException;
10 use Wikimedia\CSS\Objects\Token;
11 use Wikimedia\CSS\Objects\TokenList;
12 use Wikimedia\CSS\Util;
13
14 /**
15 * Tokenizer that just returns a predefined list of tokens
16 */
17 class TokenListTokenizer implements Tokenizer {
18
19 /** @var Token[] */
20 protected $tokens;
21
22 /** @var Token */
23 protected $eof;
24
25 /**
26 * @param Token[]|TokenList $tokens Tokens to return
27 * @param Token|null $eof Token to copy as EOF
28 */
29 public function __construct( $tokens, ?Token $eof = null ) {
30 if ( $tokens instanceof TokenList ) {
31 $this->tokens = $tokens->toTokenArray();
32 } elseif ( is_array( $tokens ) ) {
33 Util::assertAllInstanceOf( $tokens, Token::class, '$tokens' );
34 $this->tokens = $tokens;
35 } else {
36 throw new InvalidArgumentException( '$tokens must be a TokenList or an array of tokens' );
37 }
38
39 if ( $eof && $eof->type() === Token::T_EOF ) {
40 $this->eof = $eof;
41 } else {
42 $data = [];
43 if ( $eof ) {
44 $data['position'] = $eof->getPosition();
45 }
46 $this->eof = new Token( Token::T_EOF, $data );
47 }
48 }
49
50 /** @inheritDoc */
51 public function getParseErrors() {
52 return [];
53 }
54
55 /** @inheritDoc */
56 public function clearParseErrors() {
57 }
58
59 /** @inheritDoc */
60 public function consumeToken() {
61 return array_shift( $this->tokens ) ?: $this->eof;
62 }
63
64 }