11function assert ( condition : any , message ?: string ) {
22 if ( ! condition ) {
3- throw message || 'Assertion failed'
3+ throw new Error ( message || 'Assertion failed' )
44 }
55}
6- function every ( v : number [ ] , arg1 : ( x : any ) => boolean ) : any {
6+ function every < T > ( v : T [ ] , arg1 : ( x : T ) => boolean ) : any {
77 assert ( Array . isArray ( v ) , 'expected an array' )
88 return ! v . find ( ( x ) => ! arg1 ( x ) )
99}
@@ -443,19 +443,33 @@ class StringReader {
443443 readString ( ) {
444444 const start = this . cursor
445445 if ( this . peek ( 1 ) === '"' ) this . skip ( 1 )
446- while ( this . peek ( 1 ) !== '"' && this . peekReversed ( 1 ) !== '\\' ) {
446+ while (
447+ this . peek ( 1 ) !== '"' &&
448+ this . peekReversed ( 1 ) !== '\\' &&
449+ ! this . isEnd ( )
450+ ) {
447451 this . skip ( 1 )
448452 }
453+ if ( this . isEnd ( ) ) {
454+ throw new Error ( 'Unexpected end of string' )
455+ }
449456 this . skip ( 1 )
450457 const end = this . cursor
451458 return this . str . substr ( start , end - start )
452459 }
453460 readSingleQuotedString ( ) {
454461 const start = this . cursor
455462 if ( this . peek ( 1 ) === "'" ) this . skip ( 1 )
456- while ( this . peek ( 1 ) !== "'" && this . peekReversed ( 1 ) !== '\\' ) {
463+ while (
464+ this . peek ( 1 ) !== "'" &&
465+ this . peekReversed ( 1 ) !== '\\' &&
466+ ! this . isEnd ( )
467+ ) {
457468 this . skip ( 1 )
458469 }
470+ if ( this . isEnd ( ) ) {
471+ throw new Error ( 'Unexpected end of string' )
472+ }
459473 this . skip ( 1 )
460474 const end = this . cursor
461475 return this . str . substr ( start , end - start )
@@ -466,6 +480,7 @@ class StringReader {
466480 this . skip ( count )
467481 let inString : '"' | "'" | null = null
468482 while ( count > 0 ) {
483+ if ( this . isEnd ( ) ) throw new Error ( 'Unmatched Brackets' )
469484 if ( inString !== null ) {
470485 if ( this . peek ( 1 ) === inString ) inString = null
471486 } else if ( this . peek ( 1 ) === '"' ) {
@@ -480,7 +495,7 @@ class StringReader {
480495 this . skip ( 1 )
481496 }
482497 const end = this . cursor
483- if ( ! this . isEnd ( ) ) this . skip ( 1 )
498+ // if (!this.isEnd()) this.skip(1)
484499 return this . str . substr ( start , end - start )
485500 }
486501 readUntilAnyOf ( chars : string [ ] ) {
@@ -515,6 +530,7 @@ class StringReader {
515530 }
516531 readNumber ( ) {
517532 const start = this . cursor
533+ if ( this . peek ( 1 ) === '-' ) this . skip ( 1 )
518534 while ( this . peek ( 1 ) >= '0' && this . peek ( 1 ) <= '9' ) {
519535 this . skip ( 1 )
520536 }
@@ -553,8 +569,14 @@ class SNBTParser {
553569 this . reader . peek ( 5 ) === 'false'
554570 ) {
555571 result = this . parseBoolean ( )
556- } else {
572+ } else if (
573+ this . reader . peek ( 1 ) === '-' ||
574+ this . reader . peek ( 1 ) === '.' ||
575+ ( this . reader . peek ( 1 ) >= '0' && this . reader . peek ( 1 ) <= '9' )
576+ ) {
557577 result = this . parseNumber ( )
578+ } else {
579+ throw new Error ( 'Unexpected character ' + this . reader . peek ( 1 ) )
558580 }
559581 return result
560582 }
@@ -734,7 +756,12 @@ export function removeSpacesAndNewlines(str: string): string {
734756}
735757export const SNBT = {
736758 parse ( str : string ) : SNBTTag {
737- return new SNBTParser ( removeSpacesAndNewlines ( str ) ) . parse ( )
759+ let parser = new SNBTParser ( removeSpacesAndNewlines ( str . trim ( ) ) )
760+ let result = parser . parse ( )
761+ if ( ! parser . reader . isEnd ( ) ) {
762+ throw new Error ( 'finished reading before end of string.' )
763+ }
764+ return result
738765 } ,
739766 // type creations
740767 Byte ( v : number = 0 ) {
0 commit comments