import { Token } from "./lexer" ; export function compressNewlines ( tokens ) { const out = tokens . filter ( ( item , pos , array ) => { if ( item . token !== "newline" ) { return true ; } if ( pos === 0 ) { return false ; } if ( array [ pos - 1 ] . token === "newline" ) { return false ; } return true ; } ) ; if ( out . length !== 0 && out [ out . length - 1 ] . token === "newline" ) { out . pop ( ) ; } return out ; } export function indentify ( tokens ) { let indents = 0 ; let currentIndentStreak = 0 ; const out = [ ] ; for ( let i = 0 ; i < tokens . length ; i ++ ) { const currentToken = tokens [ i ] ; if ( currentToken . token === "indent" ) { currentIndentStreak ++ ; continue ; } else if ( i > 0 && tokens [ i - currentIndentStreak - 1 ] . token === "newline" ) { const deltaIndents = currentIndentStreak - indents ; if ( deltaIndents > 0 ) { for ( let j = 0 ; j < deltaIndents ; j ++ ) { out . push ( { token : "shiftIn" } ) ; } } else if ( deltaIndents < 0 ) { for ( let j = 0 ; j < - deltaIndents ; j ++ ) { const prev = out . pop ( ) ; out . push ( { token : "shiftOut" } ) ; prev && out . push ( prev ) ; } } indents = currentIndentStreak ; currentIndentStreak = 0 ; } out . push ( currentToken ) ; } if ( out . length && out [ out . length - 1 ] . token === "newline" ) { out . pop ( ) ; } for ( let j = 0 ; j < indents ; j ++ ) { out . push ( { token : "shiftOut" } ) ; } return out ; } export const postLex = ( tokens ) : Token [ ] => { return indentify ( compressNewlines ( tokens ) ) ; } ;