Merge pull request #4036 from dokuwiki/issue4033
[dokuwiki.git] / _test / vendor / symfony / css-selector / Parser / TokenStream.php
blobf4c2585aa03b2535aafd1e51704a0f545c46cf9c
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\Parser;
14 use Symfony\Component\CssSelector\Exception\InternalErrorException;
15 use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
17 /**
18 * CSS selector token stream.
20 * This component is a port of the Python cssselect library,
21 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
23 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
25 * @internal
27 class TokenStream
29 /**
30 * @var Token[]
32 private $tokens = [];
34 /**
35 * @var Token[]
37 private $used = [];
39 /**
40 * @var int
42 private $cursor = 0;
44 /**
45 * @var Token|null
47 private $peeked;
49 /**
50 * @var bool
52 private $peeking = false;
54 /**
55 * Pushes a token.
57 * @return $this
59 public function push(Token $token): self
61 $this->tokens[] = $token;
63 return $this;
66 /**
67 * Freezes stream.
69 * @return $this
71 public function freeze(): self
73 return $this;
76 /**
77 * Returns next token.
79 * @throws InternalErrorException If there is no more token
81 public function getNext(): Token
83 if ($this->peeking) {
84 $this->peeking = false;
85 $this->used[] = $this->peeked;
87 return $this->peeked;
90 if (!isset($this->tokens[$this->cursor])) {
91 throw new InternalErrorException('Unexpected token stream end.');
94 return $this->tokens[$this->cursor++];
97 /**
98 * Returns peeked token.
100 public function getPeek(): Token
102 if (!$this->peeking) {
103 $this->peeked = $this->getNext();
104 $this->peeking = true;
107 return $this->peeked;
111 * Returns used tokens.
113 * @return Token[]
115 public function getUsed(): array
117 return $this->used;
121 * Returns nex identifier token.
123 * @return string The identifier token value
125 * @throws SyntaxErrorException If next token is not an identifier
127 public function getNextIdentifier(): string
129 $next = $this->getNext();
131 if (!$next->isIdentifier()) {
132 throw SyntaxErrorException::unexpectedToken('identifier', $next);
135 return $next->getValue();
139 * Returns nex identifier or star delimiter token.
141 * @return string|null The identifier token value or null if star found
143 * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
145 public function getNextIdentifierOrStar(): ?string
147 $next = $this->getNext();
149 if ($next->isIdentifier()) {
150 return $next->getValue();
153 if ($next->isDelimiter(['*'])) {
154 return null;
157 throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
161 * Skips next whitespace if any.
163 public function skipWhitespace()
165 $peek = $this->getPeek();
167 if ($peek->isWhitespace()) {
168 $this->getNext();