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 $frozen = false;
42 private $used = array();
52 private $peeked = null;
57 private $peeking = false;
66 public function push(Token $token)
68 $this->tokens[] = $token;
78 public function freeze()
90 * @throws InternalErrorException If there is no more token
92 public function getNext()
95 $this->peeking = false;
96 $this->used[] = $this->peeked;
101 if (!isset($this->tokens[$this->cursor])) {
102 throw new InternalErrorException('Unexpected token stream end.');
105 return $this->tokens[$this->cursor++];
109 * Returns peeked token.
113 public function getPeek()
115 if (!$this->peeking) {
116 $this->peeked = $this->getNext();
117 $this->peeking = true;
120 return $this->peeked;
124 * Returns used tokens.
128 public function getUsed()
134 * Returns nex identifier token.
136 * @return string The identifier token value
138 * @throws SyntaxErrorException If next token is not an identifier
140 public function getNextIdentifier()
142 $next = $this->getNext();
144 if (!$next->isIdentifier()) {
145 throw SyntaxErrorException::unexpectedToken('identifier', $next);
148 return $next->getValue();
152 * Returns nex identifier or star delimiter token.
154 * @return null|string The identifier token value or null if star found
156 * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
158 public function getNextIdentifierOrStar()
160 $next = $this->getNext();
162 if ($next->isIdentifier()) {
163 return $next->getValue();
166 if ($next->isDelimiter(array('*'))) {
170 throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
174 * Skips next whitespace if any.
176 public function skipWhitespace()
178 $peek = $this->getPeek();
180 if ($peek->isWhitespace()) {