Ejemplo n.º 1
0
 public IAbstractBinding BuildBinding(BindingParserOptions bindingOptions, IDataContextStack dataContext, DothtmlBindingNode node, IPropertyDescriptor property)
 {
     return(new ResolvedBinding(bindingService, bindingOptions, (DataContextStack)dataContext, node.Value, property: property as DotvvmProperty)
     {
         DothtmlNode = node,
     });
 }
Ejemplo n.º 2
0
        public ResolvedBinding(BindingCompilationService bindingService, BindingParserOptions bindingOptions, DataContextStack dataContext, string code = null, Expression parsedExpression = null, DotvvmProperty property = null)
        {
            var bindingType = bindingOptions.BindingType;
            var properties  = new List <object> {
                dataContext,
                this,
                bindingOptions,
                new BindingErrorReporterProperty()
            };

            if (code != null)
            {
                properties.Add(new OriginalStringBindingProperty(code));
            }
            if (parsedExpression != null)
            {
                properties.Add(new ParsedExpressionBindingProperty(parsedExpression));
            }
            if (property != null)
            {
                properties.Add(new AssignedPropertyBindingProperty(property));
            }
            this.BindingService = bindingService;
            this.Binding        = bindingService.CreateBinding(bindingType, properties.ToArray());
        }
Ejemplo n.º 3
0
 protected override IAbstractBinding CompileBinding(DothtmlBindingNode node, BindingParserOptions bindingOptions, IDataContextStack context, IPropertyDescriptor property)
 {
     if (context == null)
     {
         node.AddError("The DataContext couldn't be evaluated because of the errors above.");
     }
     return(treeBuilder.BuildBinding(bindingOptions, context, node, property));
 }
Ejemplo n.º 4
0
 public IAbstractBinding BuildBinding(BindingParserOptions bindingOptions, IDataContextStack dataContext, DothtmlBindingNode node, ITypeDescriptor resultType = null, Exception parsingError = null, object customData = null)
 {
     return(new ResolvedBinding()
     {
         BindingType = bindingOptions.BindingType,
         Value = node.Value,
         Expression = (Expression)customData,
         DataContextTypeStack = (DataContextStack)dataContext,
         ParsingError = parsingError,
         DothtmlNode = node,
         ResultType = resultType
     });
 }
Ejemplo n.º 5
0
 public IAbstractBinding BuildBinding(BindingParserOptions bindingOptions, DothtmlBindingNode node, IDataContextStack dataContext, Exception parsingError, ITypeDescriptor resultType, object customData)
 {
     return new ResolvedBinding()
     {
         BindingType = bindingOptions.BindingType,
         Value = node.Value,
         Expression = (Expression)customData,
         DataContextTypeStack = (DataContextStack)dataContext,
         ParsingError = parsingError,
         BindingNode = node,
         ResultType = resultType
     };
 }
        public object ExecuteBinding(string expression, object[] contexts, DotvvmControl control)
        {
            var context = new DataContextStack(contexts.FirstOrDefault()?.GetType() ?? typeof(object));

            context.RootControlType = control?.GetType() ?? typeof(DotvvmControl);
            for (int i = 1; i < contexts.Length; i++)
            {
                context = new DataContextStack(contexts[i].GetType(), context);
            }
            var parser         = new CompileTimeBindingParser();
            var expressionTree = parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>());

            return(new BindingCompilationAttribute().CompileToDelegate(expressionTree, context, typeof(object)).Compile()(contexts, control));
        }
Ejemplo n.º 7
0
        public object ExecuteBinding(string expression, object[] contexts, DotvvmControl control, NamespaceImport[] imports = null, Type expectedType = null)
        {
            var context = new DataContextStack(contexts.FirstOrDefault()?.GetType() ?? typeof(object), rootControlType: control?.GetType() ?? typeof(DotvvmControl));

            for (int i = 1; i < contexts.Length; i++)
            {
                context = new DataContextStack(contexts[i].GetType(), context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>(importNs: new[] { new NamespaceImport("DotVVM.Framework.Tests.Binding") }.Concat(imports ?? Enumerable.Empty <NamespaceImport>()).ToArray()));

            Array.Reverse(contexts);
            return(new BindingCompilationAttribute().CompileToDelegate(expressionTree, context, expectedType ?? typeof(object)).Compile()(contexts, control));
        }
Ejemplo n.º 8
0
        public Expression Parse(string expression, DataContextStack dataContexts, BindingParserOptions options, params KeyValuePair <string, Expression>[] additionalSymbols)
        {
            try
            {
                var tokenizer = new BindingTokenizer();
                tokenizer.Tokenize(expression);

                var parser = new BindingParser();
                parser.Tokens = tokenizer.Tokens;
                var node = parser.ReadExpression();
                if (!parser.OnEnd())
                {
                    throw new BindingCompilationException(
                              $"Unexpected token '{expression.Substring(0, parser.Peek().StartPosition)} ---->{parser.Peek().Text}<---- {expression.Substring(parser.Peek().EndPosition)}'",
                              null, new TokenBase[] { parser.Peek() });
                }
                foreach (var n in node.EnumerateNodes())
                {
                    if (n.HasNodeErrors)
                    {
                        throw new BindingCompilationException(string.Join(", ", n.NodeErrors), n);
                    }
                }

                var symbols = InitSymbols(dataContexts);
                symbols = options.AddImportedTypes(symbols, compiledAssemblyCache);
                symbols = symbols.AddSymbols(options.ExtensionParameters.Select(p => CreateParameter(dataContexts, p.Identifier, p)));
                symbols = symbols.AddSymbols(additionalSymbols);

                var visitor = new ExpressionBuildingVisitor(symbols);
                visitor.Scope = symbols.Resolve(options.ScopeParameter);
                return(visitor.Visit(node));
            }
            catch (Exception ex)
            {
                ex.ForInnerExceptions <BindingCompilationException>(bce =>
                {
                    if (bce.Expression == null)
                    {
                        bce.Expression = expression;
                    }
                });
                throw;
            }
        }
        public Expression Parse(string expression, DataContextStack dataContexts, BindingParserOptions options)
        {
            try
            {
                var tokenizer = new Parser.Binding.Tokenizer.BindingTokenizer();
                tokenizer.Tokenize(new StringReader(expression));

                var parser = new Parser.Binding.Parser.BindingParser();
                parser.Tokens = tokenizer.Tokens;
                var node = parser.ReadExpression();
                if (!parser.OnEnd())
                {
                    throw new BindingCompilationException(
                              $"unexpected token '{ expression.Substring(0, parser.Peek().StartPosition)} ---->{ parser.Peek().Text }<---- { expression.Substring(parser.Peek().StartPosition + parser.Peek().Length) }'",
                              null, new TokenBase[] { parser.Peek() });
                }
                foreach (var n in node.EnumerateNodes())
                {
                    if (n.HasNodeErrors)
                    {
                        throw new BindingCompilationException(string.Join(", ", n.NodeErrors), n);
                    }
                }

                var symbols = InitSymbols(dataContexts);
                symbols = options.AddTypes(symbols);
                var visitor = new ExpressionBuildingVisitor(symbols);
                visitor.Scope = symbols.Resolve(options.ScopeParameter);
                return(visitor.Visit(node));
            }
            catch (Exception ex)
            {
                ex.ForInnerExceptions <BindingCompilationException>(bce =>
                {
                    if (bce.Expression == null)
                    {
                        bce.Expression = expression;
                    }
                });
                throw;
            }
        }
Ejemplo n.º 10
0
        public Expression Parse(string expression, DataContextStack dataContexts, BindingParserOptions options)
        {
            try
            {
                var tokenizer = new BindingTokenizer();
                tokenizer.Tokenize(new StringReader(expression));

                var parser = new BindingParser();
                parser.Tokens = tokenizer.Tokens;
                var node = parser.ReadExpression();
                if (!parser.OnEnd())
                {
                    throw new BindingCompilationException(
                        $"Unexpected token '{expression.Substring(0, parser.Peek().StartPosition)} ---->{parser.Peek().Text}<---- {expression.Substring(parser.Peek().StartPosition + parser.Peek().Length)}'",
                        null, new TokenBase[] { parser.Peek() });
                }
                foreach (var n in node.EnumerateNodes())
                {
                    if (n.HasNodeErrors) throw new BindingCompilationException(string.Join(", ", n.NodeErrors), n);
                }

                var symbols = InitSymbols(dataContexts);
                symbols = options.AddTypes(symbols);

                var visitor = new ExpressionBuildingVisitor(symbols);
                visitor.Scope = symbols.Resolve(options.ScopeParameter);
                return visitor.Visit(node);
            }
            catch (Exception ex)
            {
                ex.ForInnerExceptions<BindingCompilationException>(bce =>
                {
                    if (bce.Expression == null) bce.Expression = expression;
                });
                throw;
            }
        }
Ejemplo n.º 11
0
        protected override IAbstractBinding CompileBinding(DothtmlBindingNode node, BindingParserOptions bindingOptions, IDataContextStack context)
        {
            Expression expression = null;
            Exception parsingError = null;
            ITypeDescriptor resultType = null;

            if (context == null)
            {
                parsingError = new DotvvmCompilationException("The DataContext couldn't be evaluated because of the errors above.", node.Tokens);
            }
            else
            {
                try
                {
                    expression = bindingExpressionBuilder.Parse(node.Value, (DataContextStack)context, bindingOptions);
                    resultType = new ResolvedTypeDescriptor(expression.Type);
                }
                catch (Exception exception)
                {
                    parsingError = exception;
                }
            }
            return treeBuilder.BuildBinding(bindingOptions, node, context, parsingError, resultType, expression);
        }
Ejemplo n.º 12
0
        protected override IAbstractBinding CompileBinding(DothtmlBindingNode node, BindingParserOptions bindingOptions, IDataContextStack context)
        {
            Expression      expression   = null;
            Exception       parsingError = null;
            ITypeDescriptor resultType   = null;

            if (context == null)
            {
                parsingError = new DotvvmCompilationException("The DataContext couldn't be evaluated because of the errors above.", node.Tokens);
            }
            else
            {
                try
                {
                    expression = bindingExpressionBuilder.Parse(node.Value, (DataContextStack)context, bindingOptions);
                    resultType = new ResolvedTypeDescriptor(expression.Type);
                }
                catch (Exception exception)
                {
                    parsingError = exception;
                }
            }
            return(treeBuilder.BuildBinding(bindingOptions, context, node, resultType, parsingError, expression));
        }
Ejemplo n.º 13
0
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var configuration = DotvvmTestHelper.CreateConfiguration();

            configuration.RegisterApiClient(typeof(TestApiClient), "http://server/api", "./apiscript.js", "_api");
            configuration.Markup.ImportedNamespaces.Add(new NamespaceImport("DotVVM.Framework.Tests.Binding"));

            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] {
                new BindingPageInfoExtensionParameter(),
            }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = parser.ParseWithLambdaConversion(expression, context, BindingParserOptions.Create <ValueBindingExpression>(), expectedType);
            var jsExpression   =
                configuration.ServiceProvider.GetRequiredService <StaticCommandBindingCompiler>().CompileToJavascript(context, expressionTree);

            return(KnockoutHelper.GenerateClientPostBackScript(
                       "",
                       new FakeCommandBinding(BindingPropertyResolvers.FormatJavascript(jsExpression, nullChecks: false), null),
                       new Literal(),
                       new PostbackScriptOptions(
                           allowPostbackHandlers: false,
                           returnValue: null,
                           commandArgs: CodeParameterAssignment.FromIdentifier("commandArguments")
                           )));
        }
Ejemplo n.º 14
0
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] {
                new CurrentCollectionIndexExtensionParameter(),
                new BindingCollectionInfoExtensionParameter("_collection"),
                new BindingPageInfoExtensionParameter(),
                new BindingApiExtensionParameter(),
            }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }
            var parser           = new BindingExpressionBuilder();
            var parsedExpression = parser.ParseWithLambdaConversion(expression, context, BindingParserOptions.Create <ValueBindingExpression>(), expectedType);
            var expressionTree   =
                TypeConversion.MagicLambdaConversion(parsedExpression, expectedType) ??
                TypeConversion.ImplicitConversion(parsedExpression, expectedType, true, true);
            var jsExpression = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(expressionTree, context));

            jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
            JsTemporaryVariableResolver.ResolveVariables(jsExpression);
            return(JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression));
        }
Ejemplo n.º 15
0
        public ParsedExpressionBindingProperty GetExpression(OriginalStringBindingProperty originalString, DataContextStack dataContext, BindingParserOptions options, ExpectedTypeBindingProperty expectedType = null)
        {
            var expr = bindingParser.ParseWithLambdaConversion(originalString.Code, dataContext, options, expectedType?.Type ?? typeof(object));

            if (expr is StaticClassIdentifierExpression)
            {
                throw new Exception($"'{originalString.Code}' is a static class reference, not a valid expression.");
            }
            else if (expr is UnknownStaticClassIdentifierExpression)
            {
                expr = expr.Reduce();
            }
            return(new ParsedExpressionBindingProperty(expr));
        }
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extenstionParameters: new BindingExtensionParameter[] {
                new BindingPageInfoExtensionParameter()
            });

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = TypeConversion.ImplicitConversion(parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>()), expectedType, true, true);
            var jsExpression   = new JsParenthesizedExpression(JavascriptTranslator.CompileToJavascript(expressionTree, context,
                                                                                                        DotvvmConfiguration.CreateDefault().ServiceLocator.GetService <IViewModelSerializationMapper>()));

            jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
            JsTemporaryVariableResolver.ResolveVariables(jsExpression);
            return(JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression));
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Compiles the binding.
 /// </summary>
 protected abstract IAbstractBinding CompileBinding(DothtmlBindingNode node, BindingParserOptions bindingOptions, IDataContextStack context, IPropertyDescriptor property);
Ejemplo n.º 18
0
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var context = new DataContextStack(contexts.FirstOrDefault() ?? typeof(object), rootControlType: typeof(DotvvmControl));

            for (int i = 1; i < contexts.Length; i++)
            {
                context = new DataContextStack(contexts[i], context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = TypeConversion.ImplicitConversion(parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>()), expectedType, true, true);

            return(JavascriptTranslator.CompileToJavascript(expressionTree, context));
        }
Ejemplo n.º 19
0
        public ParsedExpressionBindingProperty GetExpression(OriginalStringBindingProperty originalString, DataContextStack dataContext, BindingParserOptions options)
        {
            var expr = bindingParser.Parse(originalString.Code, dataContext, options);

            return(new ParsedExpressionBindingProperty(expr.Reduce()));
        }
Ejemplo n.º 20
0
 public static Expression ParseWithLambdaConversion(this IBindingExpressionBuilder builder, string expression, DataContextStack dataContexts, BindingParserOptions options, Type expectedType, params KeyValuePair <string, Expression>[] additionalSymbols)
 {
     if (expectedType.IsDelegate())
     {
         var resultType      = expectedType.GetMethod("Invoke").ReturnType;
         var delegateSymbols = expectedType
                               .GetMethod("Invoke")
                               .GetParameters()
                               .Select((p, index) => new KeyValuePair <string, Expression>(
                                           p.Name,
                                           Expression.Parameter(p.ParameterType, p.Name)
                                           .AddParameterAnnotation(new BindingParameterAnnotation(
                                                                       extensionParameter: new TypeConversion.MagicLambdaConversionExtensionParameter(index, p.Name, p.ParameterType)))
                                           ))
                               .ToArray();
         return(builder.Parse(expression, dataContexts, options, additionalSymbols.Concat(delegateSymbols).ToArray()));
     }
     else
     {
         return(builder.Parse(expression, dataContexts, options, additionalSymbols));
     }
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Compiles the binding.
 /// </summary>
 protected abstract IAbstractBinding CompileBinding(DothtmlBindingNode node, BindingParserOptions bindingOptions, IDataContextStack context);