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
;
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>
52 private $peeking = false;
59 public function push(Token
$token): self
61 $this->tokens
[] = $token;
71 public function freeze(): self
79 * @throws InternalErrorException If there is no more token
81 public function getNext(): Token
84 $this->peeking
= false;
85 $this->used
[] = $this->peeked
;
90 if (!isset($this->tokens
[$this->cursor
])) {
91 throw new InternalErrorException('Unexpected token stream end.');
94 return $this->tokens
[$this->cursor++
];
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.
115 public function getUsed(): array
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(['*'])) {
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()) {