TokenStream.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 array $tokens = [];
  29. /**
  30. * @var Token[]
  31. */
  32. private array $used = [];
  33. private int $cursor = 0;
  34. private ?Token $peeked;
  35. private bool $peeking = false;
  36. /**
  37. * Pushes a token.
  38. *
  39. * @return $this
  40. */
  41. public function push(Token $token): static
  42. {
  43. $this->tokens[] = $token;
  44. return $this;
  45. }
  46. /**
  47. * Freezes stream.
  48. *
  49. * @return $this
  50. */
  51. public function freeze(): static
  52. {
  53. return $this;
  54. }
  55. /**
  56. * Returns next token.
  57. *
  58. * @throws InternalErrorException If there is no more token
  59. */
  60. public function getNext(): Token
  61. {
  62. if ($this->peeking) {
  63. $this->peeking = false;
  64. $this->used[] = $this->peeked;
  65. return $this->peeked;
  66. }
  67. if (!isset($this->tokens[$this->cursor])) {
  68. throw new InternalErrorException('Unexpected token stream end.');
  69. }
  70. return $this->tokens[$this->cursor++];
  71. }
  72. /**
  73. * Returns peeked token.
  74. */
  75. public function getPeek(): Token
  76. {
  77. if (!$this->peeking) {
  78. $this->peeked = $this->getNext();
  79. $this->peeking = true;
  80. }
  81. return $this->peeked;
  82. }
  83. /**
  84. * Returns used tokens.
  85. *
  86. * @return Token[]
  87. */
  88. public function getUsed(): array
  89. {
  90. return $this->used;
  91. }
  92. /**
  93. * Returns next identifier token.
  94. *
  95. * @throws SyntaxErrorException If next token is not an identifier
  96. */
  97. public function getNextIdentifier(): string
  98. {
  99. $next = $this->getNext();
  100. if (!$next->isIdentifier()) {
  101. throw SyntaxErrorException::unexpectedToken('identifier', $next);
  102. }
  103. return $next->getValue();
  104. }
  105. /**
  106. * Returns next identifier or null if star delimiter token is found.
  107. *
  108. * @throws SyntaxErrorException If next token is not an identifier or a star delimiter
  109. */
  110. public function getNextIdentifierOrStar(): ?string
  111. {
  112. $next = $this->getNext();
  113. if ($next->isIdentifier()) {
  114. return $next->getValue();
  115. }
  116. if ($next->isDelimiter(['*'])) {
  117. return null;
  118. }
  119. throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);
  120. }
  121. /**
  122. * Skips next whitespace if any.
  123. */
  124. public function skipWhitespace(): void
  125. {
  126. $peek = $this->getPeek();
  127. if ($peek->isWhitespace()) {
  128. $this->getNext();
  129. }
  130. }
  131. }