import { AllTokens } from '../tokenizer' ; export interface TokenScanner { tokens : AllTokens [ ] ; start : number ; pos : number ; size : number ; } type TestFn = ( token ? : AllTokens ) => boolean ; export default function tokenScanner ( tokens ) { return { tokens , start : 0 , pos : 0 , size : tokens . length } ; } export function peek ( scanner ) { return scanner . tokens [ scanner . pos ] ; } export function next ( scanner ) { return scanner . tokens [ scanner . pos ++ ] ; } export function slice ( scanner , from = scanner . start , to = scanner . pos ) { return scanner . tokens . slice ( from , to ) ; } export function readable ( scanner ) { return scanner . pos < scanner . size ; } export function consume ( scanner , test ) { const token = peek ( scanner ) ; if ( token && test ( token ) ) { scanner . pos ++ ; return true ; } return false ; } export function error ( scanner , message , token = peek ( scanner ) ) { if ( token && token . start != null ) { message += ` at ${ token . start }` ; } const err = new Error ( message ) ; err [ 'pos' ] = token && token . start ; return err ; } export function consumeWhile ( scanner , test ) { const start = scanner . pos ; while ( consume ( scanner , test ) ) { } return scanner . pos !== start ; }