You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
3.4 KiB

3 years ago
  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): self
  51. {
  52. $this->tokens[] = $token;
  53. return $this;
  54. }
  55. /**
  56. * Freezes stream.
  57. *
  58. * @return $this
  59. */
  60. public function freeze(): self
  61. {
  62. return $this;
  63. }
  64. /**
  65. * Returns next token.
  66. *
  67. * @throws InternalErrorException If there is no more token
  68. */
  69. public function getNext(): Token
  70. {
  71. if ($this->peeking) {
  72. $this->peeking = false;
  73. $this->used[] = $this->peeked;
  74. return $this->peeked;
  75. }
  76. if (!isset($this->tokens[$this->cursor])) {
  77. throw new InternalErrorException('Unexpected token stream end.');
  78. }
  79. return $this->tokens[$this->cursor++];
  80. }
  81. /**
  82. * Returns peeked token.
  83. */
  84. public function getPeek(): Token
  85. {
  86. if (!$this->peeking) {
  87. $this->peeked = $this->getNext();
  88. $this->peeking = true;
  89. }
  90. return $this->peeked;
  91. }
  92. /**
  93. * Returns used tokens.
  94. *
  95. * @return Token[]
  96. */
  97. public function getUsed(): array
  98. {
  99. return $this->used;
  100. }
  101. /**
  102. * Returns nex identifier token.
  103. *
  104. * @return string The identifier token value
  105. *
  106. * @throws SyntaxErrorException If next token is not an identifier
  107. */
  108. public function getNextIdentifier(): string
  109. {
  110. $next = $this->getNext();
  111. if (!$next->isIdentifier()) {
  112. throw SyntaxErrorException::unexpectedToken('identifier', $next);
  113. }
  114. return $next->getValue();
  115. }
  116. /**
  117. * Returns nex identifier or star delimiter token.
  118. *
  119. * @return string|null The identifier token value or null if star found
  120. *
  121. * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
  122. */
  123. public function getNextIdentifierOrStar(): ?string
  124. {
  125. $next = $this->getNext();
  126. if ($next->isIdentifier()) {
  127. return $next->getValue();
  128. }
  129. if ($next->isDelimiter(['*'])) {
  130. return null;
  131. }
  132. throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
  133. }
  134. /**
  135. * Skips next whitespace if any.
  136. */
  137. public function skipWhitespace()
  138. {
  139. $peek = $this->getPeek();
  140. if ($peek->isWhitespace()) {
  141. $this->getNext();
  142. }
  143. }
  144. }