TokenStream.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Parser;
  11. use Symfony\Component\CssSelector\Exception\InternalErrorException;
  12. use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
  13. /**
  14. * CSS selector token stream.
  15. *
  16. * This component is a port of the Python cssselect library,
  17. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  18. *
  19. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  20. *
  21. * @internal
  22. */
  23. class TokenStream
  24. {
  25. /**
  26. * @var Token[]
  27. */
  28. private $tokens = [];
  29. /**
  30. * @var Token[]
  31. */
  32. private $used = [];
  33. /**
  34. * @var int
  35. */
  36. private $cursor = 0;
  37. /**
  38. * @var Token|null
  39. */
  40. private $peeked;
  41. /**
  42. * @var bool
  43. */
  44. private $peeking = false;
  45. /**
  46. * Pushes a token.
  47. *
  48. * @return $this
  49. */
  50. public function push(Token $token)
  51. {
  52. $this->tokens[] = $token;
  53. return $this;
  54. }
  55. /**
  56. * Freezes stream.
  57. *
  58. * @return $this
  59. */
  60. public function freeze()
  61. {
  62. return $this;
  63. }
  64. /**
  65. * Returns next token.
  66. *
  67. * @return Token
  68. *
  69. * @throws InternalErrorException If there is no more token
  70. */
  71. public function getNext()
  72. {
  73. if ($this->peeking) {
  74. $this->peeking = false;
  75. $this->used[] = $this->peeked;
  76. return $this->peeked;
  77. }
  78. if (!isset($this->tokens[$this->cursor])) {
  79. throw new InternalErrorException('Unexpected token stream end.');
  80. }
  81. return $this->tokens[$this->cursor++];
  82. }
  83. /**
  84. * Returns peeked token.
  85. *
  86. * @return Token
  87. */
  88. public function getPeek()
  89. {
  90. if (!$this->peeking) {
  91. $this->peeked = $this->getNext();
  92. $this->peeking = true;
  93. }
  94. return $this->peeked;
  95. }
  96. /**
  97. * Returns used tokens.
  98. *
  99. * @return Token[]
  100. */
  101. public function getUsed()
  102. {
  103. return $this->used;
  104. }
  105. /**
  106. * Returns nex identifier token.
  107. *
  108. * @return string The identifier token value
  109. *
  110. * @throws SyntaxErrorException If next token is not an identifier
  111. */
  112. public function getNextIdentifier()
  113. {
  114. $next = $this->getNext();
  115. if (!$next->isIdentifier()) {
  116. throw SyntaxErrorException::unexpectedToken('identifier', $next);
  117. }
  118. return $next->getValue();
  119. }
  120. /**
  121. * Returns nex identifier or star delimiter token.
  122. *
  123. * @return string|null The identifier token value or null if star found
  124. *
  125. * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
  126. */
  127. public function getNextIdentifierOrStar()
  128. {
  129. $next = $this->getNext();
  130. if ($next->isIdentifier()) {
  131. return $next->getValue();
  132. }
  133. if ($next->isDelimiter(['*'])) {
  134. return;
  135. }
  136. throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
  137. }
  138. /**
  139. * Skips next whitespace if any.
  140. */
  141. public function skipWhitespace()
  142. {
  143. $peek = $this->getPeek();
  144. if ($peek->isWhitespace()) {
  145. $this->getNext();
  146. }
  147. }
  148. }