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