1
|
|
|
<?php |
2
|
|
|
namespace Masterminds\Html5\Parser; |
3
|
|
|
use Psr\Log\LoggerInterface; |
4
|
|
|
use Psr\Log\NullLogger; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The scanner. |
8
|
|
|
* |
9
|
|
|
* This scans over an input stream. |
10
|
|
|
*/ |
11
|
|
|
class Scanner |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const CHARS_HEX = 'abcdefABCDEF01234567890'; |
15
|
|
|
|
16
|
|
|
const CHARS_ALNUM = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890'; |
17
|
|
|
|
18
|
|
|
const CHARS_ALPHA = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
19
|
|
|
|
20
|
|
|
protected $is; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var LoggerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $logger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new Scanner. |
29
|
|
|
* |
30
|
|
|
* @param InputStream $input An InputStream to be scanned. |
31
|
|
|
* @param LoggerInterface $logger |
32
|
|
|
*/ |
33
|
126 |
|
public function __construct(InputStream $input, LoggerInterface $logger = null) |
34
|
|
|
{ |
35
|
126 |
|
$this->is = $input; |
36
|
126 |
|
$this->logger = $logger ?: new NullLogger(); |
37
|
126 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the current position. |
41
|
|
|
* |
42
|
|
|
* @return int The current intiger byte position. |
43
|
|
|
*/ |
44
|
115 |
|
public function position() |
45
|
|
|
{ |
46
|
115 |
|
return $this->is->key(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Take a peek at the next character in the data. |
51
|
|
|
* |
52
|
|
|
* @return string The next character. |
53
|
|
|
*/ |
54
|
12 |
|
public function peek() |
55
|
|
|
{ |
56
|
12 |
|
return $this->is->peek(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the next character. |
61
|
|
|
* |
62
|
|
|
* Note: This advances the pointer. |
63
|
|
|
*/ |
64
|
122 |
|
public function next() |
65
|
|
|
{ |
66
|
122 |
|
$this->is->next(); |
67
|
122 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the current character. |
71
|
|
|
* |
72
|
|
|
* Note, this does not advance the pointer. |
73
|
|
|
* |
74
|
|
|
* @return mixed Any value can be returned. Typically the current character. |
75
|
|
|
*/ |
76
|
115 |
|
public function current() |
77
|
|
|
{ |
78
|
115 |
|
if ($this->is->valid()) { |
79
|
113 |
|
$this->logger->debug(sprintf("> %s\n", $this->is->current())); |
80
|
113 |
|
return $this->is->current(); |
81
|
|
|
} |
82
|
|
|
|
83
|
113 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Silently consume N chars. |
88
|
|
|
*/ |
89
|
36 |
|
public function consume($count = 1) |
90
|
|
|
{ |
91
|
36 |
|
for ($i = 0; $i < $count; ++ $i) { |
92
|
36 |
|
$this->next(); |
93
|
36 |
|
} |
94
|
36 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Unconsume some of the data. |
98
|
|
|
* This moves the data pointer backwards. |
99
|
|
|
* |
100
|
|
|
* @param int $howMany |
101
|
|
|
* The number of characters to move the pointer back. |
102
|
|
|
*/ |
103
|
52 |
|
public function unconsume($howMany = 1) |
104
|
|
|
{ |
105
|
52 |
|
$this->is->unconsume($howMany); |
106
|
52 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the next group of that contains hex characters. |
110
|
|
|
* |
111
|
|
|
* Note, along with getting the characters the pointer in the data will be |
112
|
|
|
* moved as well. |
113
|
|
|
* |
114
|
|
|
* @return string The next group that is hex characters. |
115
|
|
|
*/ |
116
|
4 |
|
public function getHex() |
117
|
|
|
{ |
118
|
4 |
|
return $this->is->charsWhile(static::CHARS_HEX); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the next group of characters that are ASCII Alpha characters. |
123
|
|
|
* |
124
|
|
|
* Note, along with getting the characters the pointer in the data will be |
125
|
|
|
* moved as well. |
126
|
|
|
* |
127
|
|
|
* @return string The next group of ASCII alpha characters. |
128
|
|
|
*/ |
129
|
16 |
|
public function getAsciiAlpha() |
130
|
|
|
{ |
131
|
16 |
|
return $this->is->charsWhile(static::CHARS_ALPHA); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the next group of characters that are ASCII Alpha characters and numbers. |
136
|
|
|
* |
137
|
|
|
* Note, along with getting the characters the pointer in the data will be |
138
|
|
|
* moved as well. |
139
|
|
|
* |
140
|
|
|
* @return string The next group of ASCII alpha characters and numbers. |
141
|
|
|
*/ |
142
|
4 |
|
public function getAsciiAlphaNum() |
143
|
|
|
{ |
144
|
4 |
|
return $this->is->charsWhile(static::CHARS_ALNUM); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the next group of numbers. |
149
|
|
|
* |
150
|
|
|
* Note, along with getting the characters the pointer in the data will be |
151
|
|
|
* moved as well. |
152
|
|
|
* |
153
|
|
|
* @return string The next group of numbers. |
154
|
|
|
*/ |
155
|
2 |
|
public function getNumeric() |
156
|
|
|
{ |
157
|
2 |
|
return $this->is->charsWhile('0123456789'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Consume whitespace. |
162
|
|
|
* |
163
|
|
|
* Whitespace in HTML5 is: formfeed, tab, newline, space. |
164
|
|
|
*/ |
165
|
107 |
|
public function whitespace() |
166
|
|
|
{ |
167
|
107 |
|
return $this->is->charsWhile("\n\t\f "); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the current line that is being consumed. |
172
|
|
|
* |
173
|
|
|
* @return int The current line number. |
174
|
|
|
*/ |
175
|
14 |
|
public function currentLine() |
176
|
|
|
{ |
177
|
14 |
|
return $this->is->currentLine(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Read chars until something in the mask is encountered. |
182
|
|
|
*/ |
183
|
102 |
|
public function charsUntil($mask) |
184
|
|
|
{ |
185
|
102 |
|
return $this->is->charsUntil($mask); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Read chars as long as the mask matches. |
190
|
|
|
*/ |
191
|
104 |
|
public function charsWhile($mask) |
192
|
|
|
{ |
193
|
104 |
|
return $this->is->charsWhile($mask); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the current column of the current line that the tokenizer is at. |
198
|
|
|
* |
199
|
|
|
* Newlines are column 0. The first char after a newline is column 1. |
200
|
|
|
* |
201
|
|
|
* @return int The column number. |
202
|
|
|
*/ |
203
|
14 |
|
public function columnOffset() |
204
|
|
|
{ |
205
|
14 |
|
return $this->is->columnOffset(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get all characters until EOF. |
210
|
|
|
* |
211
|
|
|
* This consumes characters until the EOF. |
212
|
|
|
* |
213
|
|
|
* @return int The number of characters remaining. |
214
|
|
|
*/ |
215
|
1 |
|
public function remainingChars() |
216
|
|
|
{ |
217
|
1 |
|
return $this->is->remainingChars(); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|