Beispiel #1
0
 public virtual object Validate(ParseInfo parseInfo, IExpression value, DocRange valueRange, object additionalData)
 {
     // If the type of the parameter is a lambda, then resolve the expression.
     if (_type is PortableLambdaType lambdaType && lambdaType.LambdaKind == LambdaKind.Constant)
     {
         ConstantExpressionResolver.Resolve(value, expr =>
         {
             // If the expression is a lambda...
             if (expr is Lambda.LambdaAction lambda)
             {
                 // ...then if this parameter is invoked, apply the restricted calls and recursion info.
                 Invoked.OnInvoke(() =>
                 {
                     LambdaInvoke.LambdaInvokeApply(parseInfo, lambda, valueRange);
                 });
             }
             // Otherwise, if the expression resolves to an IBridgeInvocable...
             else if (LambdaInvoke.ParameterInvocableBridge(value, out IBridgeInvocable invocable))
             {
                 // ...then this lambda parameter is invoked, invoke the resolved invocable.
                 Invoked.OnInvoke(() => invocable.WasInvoked());
             }
         });
     }
     return(null);
 }
        public override object Validate(ParseInfo parseInfo, IExpression value, DocRange valueRange)
        {
            // ConstantExpressionResolver.Resolve's callback will be called after this function runs,
            // so we store the value in an object reference whose value will be set later.
            var promise = new ConstHeroValueResolver();

            // Resolve the expression.
            ConstantExpressionResolver.Resolve(value, expr => {
                // If the resulting expression is an EnumValuePair and the EnumValuePair's enum is Hero,
                if (expr is CallVariableAction call && call.Calling is EnumValuePair pair && pair.Member.Enum.Type == typeof(Hero))
                {
                    // Resolve the value.
                    promise.Resolve((Hero)pair.Member.Value);
                }
 public override object Validate(ParseInfo parseInfo, IExpression value, DocRange valueRange, object additionalData)
 {
     if (additionalData is StringFormatSourceResolver stringData)
     {
         // Resolve the parameter expression.
         ConstantExpressionResolver.Resolve(value, value => {
             // Make sure the resolved expression is an array and the arg count matches.
             if (value is CreateArrayAction array && array.Values.Length == stringData.ArgCount)
             {
                 // Resolve the array values.
                 stringData.ResolveFormats(array.Values);
             }
             else // If the resolved expression is not an array or the array length is not equal to the format arg count, add an error.
             {
                 parseInfo.Script.Diagnostics.Error("Expected an array with " + stringData.ArgCount + " elements", valueRange);
             }
         });
        // String Format function
        FuncMethod FormatFunction(ITypeSupplier supplier) => new FuncMethodBuilder()
        {
            Name          = "Format",
            Documentation = "Inserts an array of objects into a string.",
            ReturnType    = supplier.String(),
            Parameters    = new CodeParameter[] {
                new StringFormatArrayParameter(supplier)
            },
            OnCall = (parseInfo, range) => {
                // Resolve the source usage with StringFormat.
                // This will tell the source string to not add an error for incorrect formats.
                parseInfo.SourceUsageResolver?.Resolve(UsageType.StringFormat);

                // The source string will be resolved after this function returns.
                var sourceResolver = new StringFormatSourceResolver();

                parseInfo.SourceExpression.OnResolve(expr => ConstantExpressionResolver.Resolve(expr, expr => {
                    // Make sure the resolved source expression is a string.
                    if (expr is StringAction stringAction)
                    {
                        // Resolve the sourceResolver with the obtained string.
                        sourceResolver.ResolveString(stringAction);
                    }
                    else // Expression is not a constant string, add an error.
                    {
                        parseInfo.Script.Diagnostics.Error("Source expression must be a string constant", range);
                    }
                }));

                return(sourceResolver);
            },
            Action = (actionSet, methodCall) => {
                // This will get the source resolver instance that was initialized above.
                var sourceResolver = (StringFormatSourceResolver)methodCall.AdditionalData;

                // Create and return the string.
                return(sourceResolver.ParseString(actionSet));
            }
        };
    }