/
Json5Decoder.php
633 lines (550 loc) · 18.5 KB
/
Json5Decoder.php
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?php
/*
* This file is part of the colinodell/json5 package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Based on the official JSON5 implementation for JavaScript (https://github.com/json5/json5)
* - (c) 2012-2016 Aseem Kishore and others (https://github.com/json5/json5/contributors)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ColinODell\Json5;
final class Json5Decoder
{
private $json;
private $length;
private $at = 0;
private $currentByte;
private $lineNumber = 1;
private $associative = false;
private $maxDepth = 512;
private $castBigIntToString = false;
private $depth = 1;
private $currentLineStartsAt = 0;
/**
* Private constructor.
*
* @param string $json
* @param bool $associative
* @param int $depth
* @param bool $castBigIntToString
*/
private function __construct($json, $associative = false, $depth = 512, $castBigIntToString = false)
{
$this->json = $json;
$this->associative = $associative;
$this->maxDepth = $depth;
$this->castBigIntToString = $castBigIntToString;
$this->length = \strlen($json);
$this->currentByte = $this->getByte(0);
}
/**
* Takes a JSON encoded string and converts it into a PHP variable.
*
* The parameters exactly match PHP's json_decode() function - see
* http://php.net/manual/en/function.json-decode.php for more information.
*
* @param string $source The JSON string being decoded.
* @param bool $associative When TRUE, returned objects will be converted into associative arrays.
* @param int $depth User specified recursion depth.
* @param int $options Bitmask of JSON decode options.
*
* @return mixed
*/
public static function decode($source, $associative = false, $depth = 512, $options = 0)
{
// Try parsing with json_decode first, since that's much faster
// We only attempt this on PHP 7+ because 5.x doesn't parse some edge cases correctly
if (PHP_VERSION_ID >= 70000) {
try {
$result = \json_decode($source, $associative, $depth, $options);
if (\json_last_error() === \JSON_ERROR_NONE) {
return $result;
}
} catch (\Throwable $e) {
// ignore exception, continue parsing as JSON5
}
}
// Fall back to JSON5 if that fails
$associative = $associative === true || ($associative === null && $options & \JSON_OBJECT_AS_ARRAY);
$castBigIntToString = $options & \JSON_BIGINT_AS_STRING;
$decoder = new self((string)$source, $associative, $depth, $castBigIntToString);
$result = $decoder->value();
$decoder->white();
if ($decoder->currentByte) {
$decoder->throwSyntaxError('Syntax error');
}
return $result;
}
/**
* @param int $at
*
* @return null
*/
private function getByte($at)
{
if ($at >= $this->length) {
return null;
}
return $this->json[$at];
}
/**
* @return string|null
*/
private function currentChar()
{
if ($this->at >= $this->length) {
return null;
}
return \mb_substr(\substr($this->json, $this->at, 4), 0, 1);
}
/**
* Parse the next character.
*
* @return null|string
*/
private function next()
{
// Get the next character. When there are no more characters,
// return the empty string.
if ($this->currentByte === "\n" || ($this->currentByte === "\r" && $this->peek() !== "\n")) {
$this->lineNumber++;
$this->currentLineStartsAt = $this->at + 1;
}
$this->at++;
return $this->currentByte = $this->getByte($this->at);
}
/**
* Parse the next character if it matches $c or fail.
*
* @param string $c
*
* @return string|null
*/
private function nextOrFail($c)
{
if ($c !== $this->currentByte) {
$this->throwSyntaxError(\sprintf(
'Expected %s instead of %s',
self::renderChar($c),
self::renderChar($this->currentChar())
));
}
return $this->next();
}
/**
* Get the next character without consuming it or
* assigning it to the ch variable.
*
* @return mixed
*/
private function peek()
{
return $this->getByte($this->at + 1);
}
/**
* Attempt to match a regular expression at the current position on the current line.
*
* This function will not match across multiple lines.
*
* @param string $regex
*
* @return string|null
*/
private function match($regex)
{
$subject = \substr($this->json, $this->at);
// Only match on the current line
if ($pos = \strpos($subject, "\n")) {
$subject = \substr($subject, 0, $pos);
}
if (!\preg_match($regex, $subject, $matches, PREG_OFFSET_CAPTURE)) {
return null;
}
$this->at += $matches[0][1] + \strlen($matches[0][0]);
$this->currentByte = $this->getByte($this->at);
return $matches[0][0];
}
/**
* Parse an identifier.
*
* Normally, reserved words are disallowed here, but we
* only use this for unquoted object keys, where reserved words are allowed,
* so we don't check for those here. References:
* - http://es5.github.com/#x7.6
* - https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables
* - http://docstore.mik.ua/orelly/webprog/jscript/ch02_07.htm
*/
private function identifier()
{
// @codingStandardsIgnoreStart
// Be careful when editing this regex, there are a couple Unicode characters in between here -------------vv
$match = $this->match('/^(?:[\$_\p{L}\p{Nl}]|\\\\u[0-9A-Fa-f]{4})(?:[\$_\p{L}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]|\\\\u[0-9A-Fa-f]{4})*/u');
// @codingStandardsIgnoreEnd
if ($match === null) {
$this->throwSyntaxError('Bad identifier as unquoted key');
}
// Un-escape escaped Unicode chars
$unescaped = \preg_replace_callback('/(?:\\\\u[0-9A-Fa-f]{4})+/', function ($m) {
return \json_decode('"'.$m[0].'"');
}, $match);
return $unescaped;
}
private function number()
{
$number = null;
$sign = '';
$string = '';
$base = 10;
if ($this->currentByte === '-' || $this->currentByte === '+') {
$sign = $this->currentByte;
$this->next();
}
// support for Infinity
if ($this->currentByte === 'I') {
$this->word();
return ($sign === '-') ? -INF : INF;
}
// support for NaN
if ($this->currentByte === 'N') {
$number = $this->word();
// ignore sign as -NaN also is NaN
return $number;
}
if ($this->currentByte === '0') {
$string .= $this->currentByte;
$this->next();
if ($this->currentByte === 'x' || $this->currentByte === 'X') {
$string .= $this->currentByte;
$this->next();
$base = 16;
} elseif (\is_numeric($this->currentByte)) {
$this->throwSyntaxError('Octal literal');
}
}
switch ($base) {
case 10:
// @codingStandardsIgnoreStart
if ((\is_numeric($this->currentByte) || $this->currentByte === '.') && ($match = $this->match('/^\d*\.?\d*/')) !== null) {
$string .= $match;
}
if (($this->currentByte === 'E' || $this->currentByte === 'e') && ($match = $this->match('/^[Ee][-+]?\d*/')) !== null) {
$string .= $match;
}
// @codingStandardsIgnoreEnd
$number = $string;
break;
case 16:
if (($match = $this->match('/^[A-Fa-f0-9]+/')) !== null) {
$string .= $match;
$number = \hexdec($string);
break;
}
$this->throwSyntaxError('Bad hex number');
}
if ($sign === '-') {
$number = -1 * $number;
}
if (!\is_numeric($number) || !\is_finite($number)) {
$this->throwSyntaxError('Bad number');
}
if ($this->castBigIntToString) {
return $number;
}
// Adding 0 will automatically cast this to an int or float
return $number + 0;
}
private function string()
{
$string = '';
$delim = $this->currentByte;
$this->next();
while ($this->currentByte !== null) {
if ($this->currentByte === $delim) {
$this->next();
return $string;
}
if ($this->currentByte === '\\') {
if ($this->peek() === 'u' && $unicodeEscaped = $this->match('/^(?:\\\\u[A-Fa-f0-9]{4})+/')) {
$string .= \json_decode('"'.$unicodeEscaped.'"');
continue;
}
$this->next();
if ($this->currentByte === "\r") {
if ($this->peek() === "\n") {
$this->next();
}
} elseif (($escapee = self::getEscapee($this->currentByte)) !== null) {
$string .= $escapee;
} else {
break;
}
} elseif ($this->currentByte === "\n") {
// unescaped newlines are invalid; see:
// https://github.com/json5/json5/issues/24
// @todo this feels special-cased; are there other invalid unescaped chars?
break;
} else {
$string .= $this->currentByte;
}
$this->next();
}
$this->throwSyntaxError('Bad string');
}
/**
* Skip an inline comment, assuming this is one.
*
* The current character should be the second / character in the // pair that begins this inline comment.
* To finish the inline comment, we look for a newline or the end of the text.
*/
private function inlineComment()
{
do {
$this->next();
if ($this->currentByte === "\n" || $this->currentByte === "\r") {
$this->next();
return;
}
} while ($this->currentByte !== null);
}
/**
* Skip a block comment, assuming this is one.
*
* The current character should be the * character in the /* pair that begins this block comment.
* To finish the block comment, we look for an ending */ pair of characters,
* but we also watch for the end of text before the comment is terminated.
*/
private function blockComment()
{
do {
$this->next();
while ($this->currentByte === '*') {
$this->nextOrFail('*');
if ($this->currentByte === '/') {
$this->nextOrFail('/');
return;
}
}
} while ($this->currentByte !== null);
$this->throwSyntaxError('Unterminated block comment');
}
/**
* Skip a comment, whether inline or block-level, assuming this is one.
*/
private function comment()
{
// Comments always begin with a / character.
$this->nextOrFail('/');
if ($this->currentByte === '/') {
$this->inlineComment();
} elseif ($this->currentByte === '*') {
$this->blockComment();
} else {
$this->throwSyntaxError('Unrecognized comment');
}
}
/**
* Skip whitespace and comments.
*
* Note that we're detecting comments by only a single / character.
* This works since regular expressions are not valid JSON(5), but this will
* break if there are other valid values that begin with a / character!
*/
private function white()
{
while ($this->currentByte !== null) {
if ($this->currentByte === '/') {
$this->comment();
} elseif (\preg_match('/^[ \t\r\n\v\f\xA0]/', $this->currentByte) === 1) {
$this->next();
} elseif (\ord($this->currentByte) === 0xC2 && \ord($this->peek()) === 0xA0) {
// Non-breaking space in UTF-8
$this->next();
$this->next();
} else {
return;
}
}
}
/**
* Matches true, false, null, etc
*/
private function word()
{
switch ($this->currentByte) {
case 't':
$this->nextOrFail('t');
$this->nextOrFail('r');
$this->nextOrFail('u');
$this->nextOrFail('e');
return true;
case 'f':
$this->nextOrFail('f');
$this->nextOrFail('a');
$this->nextOrFail('l');
$this->nextOrFail('s');
$this->nextOrFail('e');
return false;
case 'n':
$this->nextOrFail('n');
$this->nextOrFail('u');
$this->nextOrFail('l');
$this->nextOrFail('l');
return null;
case 'I':
$this->nextOrFail('I');
$this->nextOrFail('n');
$this->nextOrFail('f');
$this->nextOrFail('i');
$this->nextOrFail('n');
$this->nextOrFail('i');
$this->nextOrFail('t');
$this->nextOrFail('y');
return INF;
case 'N':
$this->nextOrFail('N');
$this->nextOrFail('a');
$this->nextOrFail('N');
return NAN;
}
$this->throwSyntaxError('Unexpected ' . self::renderChar($this->currentChar()));
}
private function arr()
{
$arr = [];
if (++$this->depth > $this->maxDepth) {
$this->throwSyntaxError('Maximum stack depth exceeded');
}
$this->nextOrFail('[');
$this->white();
while ($this->currentByte !== null) {
if ($this->currentByte === ']') {
$this->nextOrFail(']');
$this->depth--;
return $arr; // Potentially empty array
}
// ES5 allows omitting elements in arrays, e.g. [,] and
// [,null]. We don't allow this in JSON5.
if ($this->currentByte === ',') {
$this->throwSyntaxError('Missing array element');
}
$arr[] = $this->value();
$this->white();
// If there's no comma after this value, this needs to
// be the end of the array.
if ($this->currentByte !== ',') {
$this->nextOrFail(']');
$this->depth--;
return $arr;
}
$this->nextOrFail(',');
$this->white();
}
$this->throwSyntaxError('Invalid array');
}
/**
* Parse an object value
*/
private function obj()
{
$object = $this->associative ? [] : new \stdClass;
if (++$this->depth > $this->maxDepth) {
$this->throwSyntaxError('Maximum stack depth exceeded');
}
$this->nextOrFail('{');
$this->white();
while ($this->currentByte !== null) {
if ($this->currentByte === '}') {
$this->nextOrFail('}');
$this->depth--;
return $object; // Potentially empty object
}
// Keys can be unquoted. If they are, they need to be
// valid JS identifiers.
if ($this->currentByte === '"' || $this->currentByte === "'") {
$key = $this->string();
} else {
$key = $this->identifier();
}
$this->white();
$this->nextOrFail(':');
if ($this->associative) {
$object[$key] = $this->value();
} else {
$object->{$key} = $this->value();
}
$this->white();
// If there's no comma after this pair, this needs to be
// the end of the object.
if ($this->currentByte !== ',') {
$this->nextOrFail('}');
$this->depth--;
return $object;
}
$this->nextOrFail(',');
$this->white();
}
$this->throwSyntaxError('Invalid object');
}
/**
* Parse a JSON value.
*
* It could be an object, an array, a string, a number,
* or a word.
*/
private function value()
{
$this->white();
switch ($this->currentByte) {
case '{':
return $this->obj();
case '[':
return $this->arr();
case '"':
case "'":
return $this->string();
case '-':
case '+':
case '.':
return $this->number();
default:
return \is_numeric($this->currentByte) ? $this->number() : $this->word();
}
}
private function throwSyntaxError($message)
{
// Calculate the column number
$str = \substr($this->json, $this->currentLineStartsAt, $this->at - $this->currentLineStartsAt);
$column = \mb_strlen($str) + 1;
throw new SyntaxError($message, $this->lineNumber, $column);
}
private static function renderChar($chr)
{
return $chr === null ? 'EOF' : "'" . $chr . "'";
}
/**
* @param string $ch
*
* @return string|null
*/
private static function getEscapee($ch)
{
switch ($ch) {
// @codingStandardsIgnoreStart
case "'": return "'";
case '"': return '"';
case '\\': return '\\';
case '/': return '/';
case "\n": return '';
case 'b': return \chr(8);
case 'f': return "\f";
case 'n': return "\n";
case 'r': return "\r";
case 't': return "\t";
default: return null;
// @codingStandardsIgnoreEnd
}
}
}