import { FunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter' ; import { RegexParser , IPrimitiveExpression } from '../Parser/Regex/RegexParser' ; import { ExpressionRegexBuilder } from '../Parser/Regex/ExpressionRegexBuilder' ; export class WithParser extends RegexParser { protected readonly regex = new ExpressionRegexBuilder ( ) . expectExpressionStart ( ) . expectCharacters ( 'with' ) . expectOneOrMoreWhitespaces ( ) . expectCharacters ( '$' ) . matchUntilFirstWhitespace ( ) . expectExpressionEnd ( ) . matchAnythingExceptSurroundingWhitespaces ( ) . expectExpressionStart ( ) . expectCharacters ( 'end' ) . expectExpressionEnd ( ) . buildRegExp ( ) ; protected buildExpression ( match ) { const parameterName = match [ 1 ] ; const scopeText = match [ 2 ] ; return { parameters : [ new FunctionParameter ( parameterName , true ) ] , evaluator : ( context ) => { const argumentValue = context . args . hasArgument ( parameterName ) ? context . args . getArgument ( parameterName ) . argumentValue : undefined ; if ( ! argumentValue ) { return '' ; } return replaceEachScopeSubstitution ( scopeText , ( pipeline ) => { if ( ! pipeline ) { return argumentValue ; } return context . pipelineCompiler . compile ( argumentValue , pipeline ) ; } ) ; } , } ; } } const ScopeSubstitutionRegEx = new ExpressionRegexBuilder ( ) . expectExpressionStart ( ) . expectCharacters ( '.' ) . matchPipeline ( ) . expectExpressionEnd ( ) . buildRegExp ( ) ; function replaceEachScopeSubstitution ( scopeText , replacer ) { return scopeText . replaceAll ( ScopeSubstitutionRegEx , ( _$ , match1 ) => { return replacer ( match1 ) ; } ) ; }