-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.php
More file actions
336 lines (275 loc) · 7.11 KB
/
parser.php
File metadata and controls
336 lines (275 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* Class parser
* Parser for simple PHP arrays
* Modified by battye, originally from: http://jsteemann.github.io/blog/2015/06/16/parsing-php-arrays-with-php/
*/
namespace battye\array_parser;
use battye\array_parser\exception\parser_exception;
class parser
{
const NOT_STRING_LITERAL = '-- Not a supported string literal (unprocessed value). --';
private static $CONSTANTS = array(
'null' => null,
'true' => true,
'false' => false
);
private $tokens;
/**
* parser constructor.
* @param tokens $tokens
*/
public function __construct(tokens $tokens)
{
$this->tokens = $tokens;
}
/**
* Simple parser to convert a simple string to an array
* @param $value
* @return array
* @throws parser_exception
*/
public static function parse_simple($value)
{
$tokens = new tokens($value);
$parser = new parser($tokens);
$result = $parser->parse_array($tokens->does_match('['));
return $result;
}
/**
* Simple and generic regex parser, made with phpBB in mind.
* @param $regex
* @param $path
* @param int $group capture group
* @return array|null
* @throws parser_exception
*/
public static function parse_regex($regex, $path, $group = 1)
{
if (!file_exists($path))
{
// The file does not exist, so exit cleanly
throw new parser_exception('The specified file does not exist: ' . $path);
}
// Find a regex match
$result = null;
$file = file_get_contents($path);
preg_match_all($regex, $file, $matches);
$multiple_matches = count($matches[$group]);
$parse = function($match) {
$tokens = new tokens($match, true);
$parser = new parser($tokens);
$result = $parser->parse_array();
if (!$tokens->done())
{
throw new parser_exception('Still tokens left after parsing.');
}
return $result;
};
// Return the results in an array
$result = [];
if ($multiple_matches > 0)
{
foreach ($matches[$group] as $match)
{
$result[] = $parse($match);
}
}
return $result;
}
/**
* Parse each token
* @return float|int|string
* @throws parser_exception
*/
public function parse_value()
{
// Ignore values that rely on another variable
if (!$this->tokens->done())
{
if (is_array($this->tokens->peek()) && count($this->tokens->peek()) > 1 && substr($this->tokens->peek()[1], 0, 1) === "$")
{
// If a variable is being used as a value then don't bother with this any more
$literalValue = self::NOT_STRING_LITERAL;
while (!$this->tokens->does_match(","))
{
if ($this->tokens->does_match(")"))
{
return $literalValue;
}
$this->tokens->pop(); // Ignore comments
}
return $literalValue;
}
}
if ($this->tokens->does_match(T_CONSTANT_ENCAPSED_STRING))
{
// Strings
$token = $this->tokens->pop();
return stripslashes(substr($token[1], 1, -1));
}
if ($this->tokens->does_match(T_STRING))
{
// Built-in string literals: null, false, true
$token = $this->tokens->pop();
$value = strtolower($token[1]);
if (array_key_exists($value, self::$CONSTANTS))
{
return self::$CONSTANTS[$value];
}
throw new parser_exception('Unexpected string literal: ' . $token[1]);
}
else if ($this->tokens->does_match(T_ARRAY) || $this->tokens->does_match('['))
{
$square_bracket_array = $this->tokens->does_match('[');
return $this->parse_array($square_bracket_array);
}
// The rest...
// We expect a number here
$uminus = 1;
if ($this->tokens->does_match("-"))
{
// Unary minus
$this->tokens->force_match("-");
$uminus = -1;
}
if ($this->tokens->does_match(T_LNUMBER))
{
// Long number
$value = $this->tokens->pop();
return $uminus * (int) $value[1];
}
if ($this->tokens->does_match(T_DNUMBER))
{
// Double number
$value = $this->tokens->pop();
return $uminus * (double) $value[1];
}
throw new parser_exception('Unexpected token encountered, potentially due to a malformed array.');
}
/**
* Disregard comments when parsing
* @throws parser_exception
*/
public function ignore_comments()
{
if (!$this->tokens->done())
{
// Check for both short and long form comment blocks
while (is_array($this->tokens->peek()) && count($this->tokens->peek()) > 1 && $this->is_comment_block($this->tokens->peek()[1]))
{
$this->tokens->pop(); // Ignore comments
}
}
}
/**
* Determine whether something is a comment
* @param $code
* @return bool
*/
public function is_comment_block($code)
{
// Short form
$short = (substr($code, 0, 2) === "//");
// Long form block
$long = (substr($code, 0, 2) == '/*' && substr($code, -2) == '*/');
// Hash form
$hash = (substr($code, 0, 1) === '#');
return ($short || $long || $hash);
}
/**
* @param bool $square
* @return array
* @throws parser_exception
*/
public function parse_array($square = false)
{
$found = 0;
$result = array();
if ($square)
{
$this->tokens->force_match("[");
}
else
{
$this->tokens->force_match(T_ARRAY);
$this->tokens->force_match("(");
}
while (true)
{
// Immediately ignore any comments
$this->ignore_comments();
if ($this->tokens->does_match(")") || $this->tokens->does_match("]"))
{
// Reached the end of the array
if ($square)
{
$this->tokens->force_match("]");
}
else
{
$this->tokens->force_match(")");
}
// If we are done, just break immediately
if ($this->tokens->done())
{
break;
}
if ($this->tokens->does_match(";"))
{
$this->tokens->force_match(";");
}
break;
}
if ($found > 0)
{
// We must see a comma following the first element
$this->tokens->force_match(",");
$this->ignore_comments();
if ($square && $this->tokens->does_match("]"))
{
// end of the square bracket array
$this->tokens->force_match("]");
break;
}
if ($this->tokens->does_match(")"))
{
// reached the end of the array
$this->tokens->force_match(")");
break;
}
}
$this->ignore_comments();
if ($this->tokens->does_match(T_ARRAY) || $this->tokens->does_match('['))
{
$square_bracket_array = $this->tokens->does_match('[');
// Nested array
$result[] = $this->parse_array($square_bracket_array);
}
else if ($this->tokens->does_match(T_CONSTANT_ENCAPSED_STRING) || $this->tokens->does_match(T_LNUMBER))
{
// String
$string = $this->parse_value();
if ($this->tokens->does_match(T_DOUBLE_ARROW))
{
// Array key (key => value)
$this->tokens->pop();
// Ignore comments in case someone put them on a new line within the array
$this->ignore_comments();
$result[$string] = $this->parse_value();
}
else
{
// simple string
$result[] = $string;
}
}
else
{
$result[] = $this->parse_value();
}
++$found;
}
return $result;
}
}