export namespace Message { function isMessage ( obj ) { return obj !== null && typeof obj !== "undefined" && ( obj as Partial < LuaDebug . Message > ) . tag === "$luaDebug" ; } const startToken = "@lldbg|" ; const endToken = "|lldbg@" ; interface ParsedInfo { leadingText : string ; newPosition : number ; message ? : LuaDebug . Message ; } function parseMessage ( text , position ) { const firstStart = text . indexOf ( startToken , position ) ; if ( firstStart === - 1 ) { return { leadingText : text . substring ( position ) , newPosition : text . length } ; } let start = firstStart ; while ( true ) { const messageStart = start + ( startToken as string ) . length ; let messageEnd = text . indexOf ( endToken , messageStart ) ; while ( messageEnd >= 0 ) { const possibleMessage = text . substring ( messageStart , messageEnd ) ; let message ; try { message = JSON . parse ( possibleMessage ) ; } catch { } const end = messageEnd + ( endToken as string ) . length ; if ( isMessage ( message ) ) { return { leadingText : text . substring ( position , start ) , newPosition : end , message } ; } messageEnd = text . indexOf ( endToken , end ) ; } start = text . indexOf ( startToken , messageStart ) ; if ( start < 0 ) { break ; } } return { leadingText : text . substring ( position , firstStart ) , newPosition : firstStart } ; } export function parse ( text ) { const messages = [ ] ; const processed = [ ] ; let position = 0 ; while ( position < text . length ) { const result = parseMessage ( text , position ) ; processed . push ( result . leadingText ) ; position = result . newPosition ; if ( ! result . message ) { break ; } messages . push ( result . message ) ; } return [ messages , processed . join ( "" ) , text . substring ( position ) ] ; } }