import { Token } from './token' ; import { TokenHelper } from './token.helper' ; import { ParserError } from '../error' ; import { TokenError } from './token.error' ; export enum TokenValidateLevel { Pass , Escape , Fatal } export class TokenValidator { public static validateToken ( token ) { const level = TokenValidator . extractTokenLevel ( token ) ; if ( level === TokenValidateLevel . Fatal ) return new ParserError ( TokenError . invalidToken , token ) ; } public static validateValueToken ( token , prevToken ) { if ( ! prevToken ) return undefined ; if ( TokenHelper . isValue ( prevToken ) ) return new ParserError ( TokenError . missingOperator , prevToken ) ; if ( ! TokenHelper . isBracketOpen ( prevToken ) && ! TokenHelper . isOperator ( prevToken ) ) return new ParserError ( TokenError . missingOperator , prevToken ) ; } private static extractTokenLevel ( token ) { const levelExtractors = [ { predicate : TokenHelper . isUnkown , level : TokenValidateLevel . Fatal } , { predicate : TokenHelper . isToken , level : TokenValidateLevel . Pass } ] ; const extractedLevel = levelExtractors . find ( extractor => extractor . predicate ( token ) ) ; return extractedLevel ? extractedLevel . level : TokenValidateLevel . Fatal ; } }