@@ -245,20 +245,14 @@ private function number()
245245
246246 // support for Infinity
247247 if ($ this ->ch === 'I ' ) {
248- $ number = $ this ->word ();
249- if ($ number === null ) {
250- $ this ->throwSyntaxError ('Unexpected word for number ' );
251- }
248+ $ this ->word ();
252249
253250 return ($ sign === '- ' ) ? -INF : INF ;
254251 }
255252
256253 // support for NaN
257254 if ($ this ->ch === 'N ' ) {
258255 $ number = $ this ->word ();
259- if ($ number !== NAN ) {
260- $ this ->throwSyntaxError ('expected word to be NaN ' );
261- }
262256
263257 // ignore sign as -NaN also is NaN
264258 return $ number ;
@@ -483,41 +477,37 @@ private function arr()
483477 {
484478 $ arr = [];
485479
486- if ($ this ->ch === '[ ' ) {
487- if (++$ this ->depth > $ this ->maxDepth ) {
488- $ this ->throwSyntaxError ('Maximum stack depth exceeded ' );
489- }
480+ if (++$ this ->depth > $ this ->maxDepth ) {
481+ $ this ->throwSyntaxError ('Maximum stack depth exceeded ' );
482+ }
490483
491- $ this ->nextOrFail ('[ ' );
492- $ this ->white ();
493- while ($ this ->ch !== null ) {
494- if ($ this ->ch === '] ' ) {
495- $ this ->nextOrFail ('] ' );
496- $ this ->depth --;
497- return $ arr ; // Potentially empty array
498- }
499- // ES5 allows omitting elements in arrays, e.g. [,] and
500- // [,null]. We don't allow this in JSON5.
501- if ($ this ->ch === ', ' ) {
502- $ this ->throwSyntaxError ('Missing array element ' );
503- }
484+ $ this ->nextOrFail ('[ ' );
485+ $ this ->white ();
486+ while ($ this ->ch !== null ) {
487+ if ($ this ->ch === '] ' ) {
488+ $ this ->nextOrFail ('] ' );
489+ $ this ->depth --;
490+ return $ arr ; // Potentially empty array
491+ }
492+ // ES5 allows omitting elements in arrays, e.g. [,] and
493+ // [,null]. We don't allow this in JSON5.
494+ if ($ this ->ch === ', ' ) {
495+ $ this ->throwSyntaxError ('Missing array element ' );
496+ }
504497
505- $ arr [] = $ this ->value ();
498+ $ arr [] = $ this ->value ();
506499
507- $ this ->white ();
508- // If there's no comma after this value, this needs to
509- // be the end of the array.
510- if ($ this ->ch !== ', ' ) {
511- $ this ->nextOrFail ('] ' );
512- $ this ->depth --;
513- return $ arr ;
514- }
515- $ this ->nextOrFail (', ' );
516- $ this ->white ();
500+ $ this ->white ();
501+ // If there's no comma after this value, this needs to
502+ // be the end of the array.
503+ if ($ this ->ch !== ', ' ) {
504+ $ this ->nextOrFail ('] ' );
505+ $ this ->depth --;
506+ return $ arr ;
517507 }
508+ $ this ->nextOrFail (', ' );
509+ $ this ->white ();
518510 }
519-
520- $ this ->throwSyntaxError ('Bad array ' );
521511 }
522512
523513 /**
@@ -527,49 +517,45 @@ private function obj()
527517 {
528518 $ object = $ this ->associative ? [] : new \stdClass ;
529519
530- if ($ this ->ch === '{ ' ) {
531- if (++$ this ->depth > $ this ->maxDepth ) {
532- $ this ->throwSyntaxError ('Maximum stack depth exceeded ' );
533- }
520+ if (++$ this ->depth > $ this ->maxDepth ) {
521+ $ this ->throwSyntaxError ('Maximum stack depth exceeded ' );
522+ }
534523
535- $ this ->nextOrFail ('{ ' );
536- $ this ->white ();
537- while ($ this ->ch ) {
538- if ($ this ->ch === '} ' ) {
539- $ this ->nextOrFail ('} ' );
540- $ this ->depth --;
541- return $ object ; // Potentially empty object
542- }
524+ $ this ->nextOrFail ('{ ' );
525+ $ this ->white ();
526+ while ($ this ->ch !== null ) {
527+ if ($ this ->ch === '} ' ) {
528+ $ this ->nextOrFail ('} ' );
529+ $ this ->depth --;
530+ return $ object ; // Potentially empty object
531+ }
543532
544- // Keys can be unquoted. If they are, they need to be
545- // valid JS identifiers.
546- if ($ this ->ch === '" ' || $ this ->ch === "' " ) {
547- $ key = $ this ->string ();
548- } else {
549- $ key = $ this ->identifier ();
550- }
533+ // Keys can be unquoted. If they are, they need to be
534+ // valid JS identifiers.
535+ if ($ this ->ch === '" ' || $ this ->ch === "' " ) {
536+ $ key = $ this ->string ();
537+ } else {
538+ $ key = $ this ->identifier ();
539+ }
551540
552- $ this ->white ();
553- $ this ->nextOrFail (': ' );
554- if ($ this ->associative ) {
555- $ object [$ key ] = $ this ->value ();
556- } else {
557- $ object ->{$ key } = $ this ->value ();
558- }
559- $ this ->white ();
560- // If there's no comma after this pair, this needs to be
561- // the end of the object.
562- if ($ this ->ch !== ', ' ) {
563- $ this ->nextOrFail ('} ' );
564- $ this ->depth --;
565- return $ object ;
566- }
567- $ this ->nextOrFail (', ' );
568- $ this ->white ();
541+ $ this ->white ();
542+ $ this ->nextOrFail (': ' );
543+ if ($ this ->associative ) {
544+ $ object [$ key ] = $ this ->value ();
545+ } else {
546+ $ object ->{$ key } = $ this ->value ();
569547 }
548+ $ this ->white ();
549+ // If there's no comma after this pair, this needs to be
550+ // the end of the object.
551+ if ($ this ->ch !== ', ' ) {
552+ $ this ->nextOrFail ('} ' );
553+ $ this ->depth --;
554+ return $ object ;
555+ }
556+ $ this ->nextOrFail (', ' );
557+ $ this ->white ();
570558 }
571-
572- $ this ->throwSyntaxError ('Bad object ' );
573559 }
574560
575561 /**
0 commit comments