namespace StateMachineWithSumType { class TextLineProcessor { result : string [ ] ; constructor ( result ) { this . result = result ; } process ( line ) { this . result . push ( line ) ; if ( line . startsWith ( "<!--" ) ) { return new MarkerLineProcessor ( this . result , this . loadCodeSample ( line ) ) ; } else { return this ; } } private loadCodeSample ( line ) { return [ ] ; } } class MarkerLineProcessor { result : string [ ] ; codeSample : string [ ] ; constructor ( result , codeSample ) { this . result = result ; this . codeSample = codeSample ; } process ( line ) { this . result . push ( line ) ; if ( line . startsWith ( "```ts" ) ) { this . result = this . result . concat ( this . codeSample ) ; return new CodeLineProcessor ( this . result ) ; } else { return this ; } } } class CodeLineProcessor { result : string [ ] ; constructor ( result ) { this . result = result ; } process ( line ) { if ( line . startsWith ( "```" ) ) { this . result . push ( line ) ; return new TextLineProcessor ( this . result ) ; } else { return this ; } } } function processText ( lines ) { let processor = new TextLineProcessor ( [ ] ) ; for ( let line of lines ) { processor = processor . process ( line ) ; } return processor . result ; } }