菜谱项目

TokenStream.php 3.5KB

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