/******************************************************************************
 * Copyright 2021 TypeFox GmbH
 * This program and the accompanying materials are made available under the
 * terms of the MIT License, which is available in the project root.
 ******************************************************************************/ import { AbstractRule , isCrossReference , isRuleCall } from '../grammar/generated/ast' ; import { CstNode } from '../syntax-tree' ; export interface ValueConverter { convert ( input , cstNode ) : ValueType ; } export type ValueType = string | number | boolean | bigint | Date ; export class DefaultValueConverter implements ValueConverter { convert ( input , cstNode ) { let feature = cstNode . feature ; if ( isCrossReference ( feature ) ) { feature = feature . terminal ; } if ( isRuleCall ( feature ) ) { const rule = feature . rule . ref ; if ( ! rule ) { throw new Error ( 'This cst node was not parsed by a rule.' ) ; } return this . runConverter ( rule , input , cstNode ) ; } return input ; } protected runConverter ( rule , input , cstNode ) { switch ( rule . name . toUpperCase ( ) ) { case 'INT' : return convertInt ( input ) ; case 'STRING' : return convertString ( input ) ; case 'ID' : return convertID ( input ) ; case 'REGEXLITERAL' : return convertString ( input ) ; } switch ( rule . type . toLowerCase ( ) ) { case 'number' : return convertNumber ( input ) ; case 'boolean' : return convertBoolean ( input ) ; default : return input ; } } } export function convertString ( input ) { return input . substring ( 1 , input . length - 1 ) ; } export function convertID ( input ) { if ( input . charAt ( 0 ) === '^' ) { return input . substring ( 1 ) ; } else { return input ; } } export function convertInt ( input ) { return parseInt ( input ) ; } export function convertNumber ( input ) { return Number ( input ) ; } export function convertBoolean ( input ) { return input . toLowerCase ( ) === 'true' ; }