public Func <object> Resolve(string lambdaExpression, ScopeChain scopeChain) { this.Split(lambdaExpression, out string name, out string paramList); if (!_lambdas.ContainsKey(name)) { throw new MergeException($"encountered function that does not exist in lambda repo: {name}"); } Delegate expr = _lambdas[name]; MethodInfo mi = expr.Method; ParameterInfo[] pInfos = mi.GetParameters(); string[] argLiterals = new string[pInfos.Length]; this.ParseLambdaArgs(paramList, ref argLiterals); if (pInfos.Length != argLiterals.Length) { string msg = "attempted function invocation with invalid number of parameters. " + $"lambda name: {name} expected count: {pInfos.Length} provided count: {argLiterals.Length}"; throw new MergeException(msg); } object[] args = new object[pInfos.Length]; for (int i = 0; i < pInfos.Length; i++) { args[i] = this.CaptureLambdaArgument(argLiterals[i], scopeChain, pInfos[i], name, i); } return(() => expr.DynamicInvoke(args)); }
public TemplateEngine(string template) { _index = 0; _template = template; _nextTag = new TagBuilder(); _result = new StringBuilder((int)(template.Length * 1.3)); _appendToResult = (c) => { _result.Append(c); }; _scopeChain = new ScopeChain(); }
private object EnsureCharArgument(string arg, ScopeChain scopeChain, string lambdaName, int index) { object target = null; if (BindHelper.IsDoubleQuoted(arg) || BindHelper.IsSingleQuoted(arg)) { target = arg.Substring(1, (arg.Length - 2)); } else { target = BindHelper.ResolveBindTarget(arg, this, scopeChain); this.EnsureArgumentType(arg, target, TypeCode.Char, lambdaName, index); } return(target); }
private object EnsureUInt64Argument(string arg, ScopeChain scopeChain, string lambdaName, int index) { object target = null; if (char.IsDigit(arg[0])) //must be numeric literal { if (!UInt64.TryParse(arg, out UInt64 u)) { throw new MergeException(this.FormatExceptionMessageBuilder(lambdaName, arg, index, TypeCode.UInt64)); } target = u; } else { target = BindHelper.ResolveBindTarget(arg, this, scopeChain); this.EnsureArgumentType(arg, target, TypeCode.UInt64, lambdaName, index); } return(target); }
private object EnsureSingleArgument(string arg, ScopeChain scopeChain, string lambdaName, int index) { object target = null; if (char.IsDigit(arg[0]) || arg[0] == '.' || arg[0] == '-' || arg[0] == '+') //must be numeric literal { if (!Single.TryParse(arg, out Single s)) { throw new MergeException(this.FormatExceptionMessageBuilder(lambdaName, arg, index, TypeCode.Single)); } target = s; } else { target = BindHelper.ResolveBindTarget(arg, this, scopeChain); this.EnsureArgumentType(arg, target, TypeCode.Single, lambdaName, index); } return(target); }
public object EnsureBooleanArgument(string arg, ScopeChain scopeChain, string lambdaName, int index) { object target = null; if (string.Compare(arg, "true", true) == 0) { target = true; } else if (string.Compare(arg, "false", true) == 0) { target = false; } else { target = BindHelper.ResolveBindTarget(arg, this, scopeChain); this.EnsureArgumentType(arg, target, TypeCode.Boolean, lambdaName, index); } return(target); }
private object EnsureDateTimeArgument(string arg, ScopeChain scopeChain, string lambdaName, int index) { object target = null; if (BindHelper.IsDoubleQuoted(arg) || BindHelper.IsSingleQuoted(arg)) { arg = arg.Substring(1, (arg.Length - 2)); if (!DateTime.TryParse(arg, out DateTime dt)) { throw new MergeException(this.FormatExceptionMessageBuilder(lambdaName, arg, index, TypeCode.DateTime)); } return(dt); } else { target = BindHelper.ResolveBindTarget(arg, this, scopeChain); this.EnsureArgumentType(arg, target, TypeCode.DateTime, lambdaName, index); } return(target); }
private static object ResolveLambdaExpressionBindTarget(string bindAs, LambdaRepository lambdaRepo, ScopeChain scopeChain) { Func <object> lambda = lambdaRepo.Resolve(bindAs, scopeChain); object target = lambda(); return(target); }
private static object ResolveScopeWalkBindTarget(string bindAs, LambdaRepository lambdaRepo, ScopeChain scopeChain) { int lastIdxOf; int depth = BindHelper.CountInstancesOfPattern(bindAs, @"..\", out lastIdxOf); object target = BindHelper.ResolveBindTarget(bindAs.Substring(lastIdxOf + 3, bindAs.Length - (depth * 3)), lambdaRepo, scopeChain, depth); return(target); }
private static object ResolveVariableReferenceBindTarget(string bindAs, LambdaRepository lambdaRepo, ScopeChain scopeChain) { object target = null; int dot = bindAs.IndexOf('.'); if (dot > -1) { target = scopeChain.AccessVariable(bindAs.Substring(0, dot)); scopeChain.Push(target); target = BindHelper.ResolveBindTarget(bindAs.Substring(++dot, bindAs.Length - dot), lambdaRepo, scopeChain); scopeChain.Pop(); } else { target = scopeChain.AccessVariable(bindAs); } return(target); }
public static object ResolveBindTarget(string bindAs, LambdaRepository lambdaRepo, ScopeChain scopeChain, int scopeLinkDepth = 0) { object target = null; object localScope = scopeChain.Peek(scopeLinkDepth); if (bindAs.Length == 1 && bindAs[0] == '$')//append bindto obj { target = localScope; } else if (bindAs[0] == '$' && bindAs[1] == '.')//reflect from bindto object { target = BindHelper.ResolveRootedBindTarget(bindAs, localScope); } else if (bindAs[0] == ':')//variable reference { target = BindHelper.ResolveVariableReferenceBindTarget(bindAs, lambdaRepo, scopeChain); } else if (bindAs[0] == '.' && bindAs[1] == '.' && bindAs[2] == '\\')//scope chain walk ..\ { target = BindHelper.ResolveScopeWalkBindTarget(bindAs, lambdaRepo, scopeChain); } else if (BindHelper.IsLambdaExpression(bindAs))//lambda expression { target = BindHelper.ResolveLambdaExpressionBindTarget(bindAs, lambdaRepo, scopeChain); } else//simple bind { target = ReflectionHelper.Expression.ReflectItem(localScope, bindAs); } return(target); }
private object CaptureLambdaArgument(string arg, ScopeChain scopeChain, ParameterInfo paramInfo, string lambda, int index) { object obj = null; TypeCode tCode = Type.GetTypeCode(paramInfo.ParameterType); switch (tCode) { case TypeCode.Object: obj = BindHelper.ResolveBindTarget(arg, this, scopeChain); break; case TypeCode.Boolean: obj = this.EnsureBooleanArgument(arg, scopeChain, lambda, index); break; case TypeCode.Byte: obj = this.EnsureByteArgument(arg, scopeChain, lambda, index); break; case TypeCode.Char: obj = this.EnsureCharArgument(arg, scopeChain, lambda, index); break; case TypeCode.String: obj = this.EnsureStringArgument(arg, scopeChain, lambda, index); break; case TypeCode.DateTime: obj = this.EnsureDateTimeArgument(arg, scopeChain, lambda, index); break; case TypeCode.Decimal: obj = this.EnsureDecimalArgument(arg, scopeChain, lambda, index); break; case TypeCode.Double: obj = this.EnsureDoubleArgument(arg, scopeChain, lambda, index); break; case TypeCode.Int16: obj = this.EnsureInt16Argument(arg, scopeChain, lambda, index); break; case TypeCode.Int32: obj = this.EnsureInt32Argument(arg, scopeChain, lambda, index); break; case TypeCode.Int64: obj = this.EnsureInt64Argument(arg, scopeChain, lambda, index); break; case TypeCode.UInt16: obj = this.EnsureUInt16Argument(arg, scopeChain, lambda, index); break; case TypeCode.UInt32: obj = this.EnsureUInt32Argument(arg, scopeChain, lambda, index); break; case TypeCode.UInt64: obj = this.EnsureUInt64Argument(arg, scopeChain, lambda, index); break; case TypeCode.SByte: obj = this.EnsureSByteArgument(arg, scopeChain, lambda, index); break; case TypeCode.Single: obj = this.EnsureSingleArgument(arg, scopeChain, lambda, index);; break; //case TypeCode.Empty: // break; //case TypeCode.DBNull: // break; default: throw new MergeException($"encountered un-expected Type.TypeCode: {tCode}"); } return(obj); }
private TemplateEngine WithScopeChain(ScopeChain scopeChain) { _scopeChain = scopeChain; return(this); }