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>
32 private $tokens = array();
37 private $used = array();
52 private $peeking = false;
59 public function push(Token $token)
61 $this->tokens[] = $token;
71 public function freeze()
81 * @throws InternalErrorException If there is no more token
83 public function getNext()
86 $this->peeking = false;
87 $this->used[] = $this->peeked;
92 if (!isset($this->tokens[$this->cursor])) {
93 throw new InternalErrorException('Unexpected token stream end.');
96 return $this->tokens[$this->cursor++];
100 * Returns peeked token.
104 public function getPeek()
106 if (!$this->peeking) {
107 $this->peeked = $this->getNext();
108 $this->peeking = true;
111 return $this->peeked;
115 * Returns used tokens.
119 public function getUsed()
125 * Returns nex identifier token.
127 * @return string The identifier token value
129 * @throws SyntaxErrorException If next token is not an identifier
131 public function getNextIdentifier()
133 $next = $this->getNext();
135 if (!$next->isIdentifier()) {
136 throw SyntaxErrorException::unexpectedToken('identifier', $next);
139 return $next->getValue();
143 * Returns nex identifier or star delimiter token.
145 * @return string|null The identifier token value or null if star found
147 * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
149 public function getNextIdentifierOrStar()
151 $next = $this->getNext();
153 if ($next->isIdentifier()) {
154 return $next->getValue();
157 if ($next->isDelimiter(array('*'))) {
161 throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
165 * Skips next whitespace if any.
167 public function skipWhitespace()
169 $peek = $this->getPeek();
171 if ($peek->isWhitespace()) {