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