123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Symfony\Component\CssSelector\Parser;
- use Symfony\Component\CssSelector\Exception\InternalErrorException;
- use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
- class TokenStream
- {
-
- private $tokens = [];
-
- private $used = [];
-
- private $cursor = 0;
-
- private $peeked;
-
- private $peeking = false;
-
- public function push(Token $token)
- {
- $this->tokens[] = $token;
- return $this;
- }
-
- public function freeze()
- {
- return $this;
- }
-
- public function getNext()
- {
- if ($this->peeking) {
- $this->peeking = false;
- $this->used[] = $this->peeked;
- return $this->peeked;
- }
- if (!isset($this->tokens[$this->cursor])) {
- throw new InternalErrorException('Unexpected token stream end.');
- }
- return $this->tokens[$this->cursor++];
- }
-
- public function getPeek()
- {
- if (!$this->peeking) {
- $this->peeked = $this->getNext();
- $this->peeking = true;
- }
- return $this->peeked;
- }
-
- public function getUsed()
- {
- return $this->used;
- }
-
- public function getNextIdentifier()
- {
- $next = $this->getNext();
- if (!$next->isIdentifier()) {
- throw SyntaxErrorException::unexpectedToken('identifier', $next);
- }
- return $next->getValue();
- }
-
- public function getNextIdentifierOrStar()
- {
- $next = $this->getNext();
- if ($next->isIdentifier()) {
- return $next->getValue();
- }
- if ($next->isDelimiter(['*'])) {
- return;
- }
- throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
- }
-
- public function skipWhitespace()
- {
- $peek = $this->getPeek();
- if ($peek->isWhitespace()) {
- $this->getNext();
- }
- }
- }
|