Inheritance: System.Linq.Expressions.Expression
Esempio n. 1
0
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var  result           = new List <object>();
            bool foundBlockParams = false;

            foreach (var item in sequence)
            {
                if (item is BlockParameterToken blockParameterToken)
                {
                    if (foundBlockParams)
                    {
                        throw new HandlebarsCompilerException("multiple blockParams expressions are not supported");
                    }

                    foundBlockParams = true;
                    if (!(result.Last() is PathExpression pathExpression))
                    {
                        throw new HandlebarsCompilerException("blockParams definition has incorrect syntax");
                    }
                    if (!string.Equals("as", pathExpression.Path, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new HandlebarsCompilerException("blockParams definition has incorrect syntax");
                    }

                    result[result.Count - 1] = HandlebarsExpression.BlockParams(pathExpression.Path, blockParameterToken.Value);
                }
                else
                {
                    result.Add(item);
                }
            }

            return(result);
        }
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                if (item is StartExpressionToken)
                {
                    yield return(item);

                    item = GetNext(enumerator);
                    if (item is Expression)
                    {
                        yield return(item);

                        continue;
                    }
                    var word = item as WordExpressionToken;
                    if (word != null && IsRegisteredHelperName(word.Value))
                    {
                        yield return(HandlebarsExpression.Helper(word.Value));
                    }
                    else
                    {
                        yield return(item);
                    }
                }
                else
                {
                    yield return(item);
                }
            }
        }
        public ConditionalBlockAccumulatorContext(Expression startingNode)
            : base(startingNode)
        {
            startingNode = UnwrapStatement(startingNode);
            var helperExpression = (HelperExpression)startingNode;
            var testType         = helperExpression.HelperName[0] == '#' ? TestType.Direct : TestType.Reverse;

            BlockName = helperExpression.HelperName.Substring(1, helperExpression.HelperName.Length - 1);

            if (!ValidHelperNames.Contains(BlockName))
            {
                throw new HandlebarsCompilerException($"Tried to convert {BlockName} expression to conditional block", helperExpression.Context);
            }

            var argument = HandlebarsExpression.Boolish(helperExpression.Arguments.Single());

            _currentCondition = BlockName switch
            {
                "if" when testType == TestType.Direct => argument,
                "if" when testType == TestType.Reverse => Expression.Not(argument),
                "unless" when testType == TestType.Direct => Expression.Not(argument),
                "unless" when testType == TestType.Reverse => argument,
                _ => throw new HandlebarsCompilerException($"Tried to convert {BlockName} expression to conditional block", helperExpression.Context)
            };
        }
Esempio n. 4
0
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;

                if (item is HashParameterToken)
                {
                    var parameters = AccumulateParameters(enumerator);

                    if (parameters.Any())
                    {
                        yield return(HandlebarsExpression.HashParametersExpression(parameters));
                    }

                    yield return(enumerator.Current);
                }
                else
                {
                    yield return(item);
                }
            }
        }
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;

                if (item is HashParameterAssignmentExpression parameterAssignment)
                {
                    bool moveNext;
                    var  parameters = AccumulateParameters(enumerator, out moveNext);

                    if (parameters.Any())
                    {
                        yield return(HandlebarsExpression.HashParametersExpression(parameters));
                    }

                    if (!moveNext)
                    {
                        yield break;
                    }

                    item = enumerator.Current;
                }

                yield return(item is Expression expression?Visit(expression) : item);
            }
        }
 public override bool IsClosingElement(Expression item)
 {
     if (IsClosingNode(item))
     {
         // If the template has no content within the block, e.g. `{{#each ...}}{{/each}`, then the block body is a no-op.
         var bodyStatements = _body.Count != 0 ? _body : new List <Expression> {
             Expression.Empty()
         };
         if (_accumulatedExpression == null)
         {
             _accumulatedExpression = HandlebarsExpression.Iterator(
                 _startingNode.Arguments.Single(),
                 Expression.Block(bodyStatements));
         }
         else
         {
             _accumulatedExpression = HandlebarsExpression.Iterator(
                 ((IteratorExpression)_accumulatedExpression).Sequence,
                 ((IteratorExpression)_accumulatedExpression).Template,
                 Expression.Block(bodyStatements));
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 7
0
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;

                while (item is WordExpressionToken word)
                {
                    item = GetNext(enumerator);
                    if (item is AssignmentToken)
                    {
                        yield return(HandlebarsExpression.HashParameterAssignmentExpression(word.Value));

                        item = GetNext(enumerator);
                    }
                    else
                    {
                        yield return(word);
                    }
                }

                yield return(item);
            }
        }
 Expression Visit(Expression expression)
 {
     if (expression is HelperExpression helperExpression)
     {
         var originalArguments = helperExpression.Arguments.ToArray();
         var arguments         = ConvertTokens(originalArguments)
                                 .Cast <Expression>()
                                 .ToArray();
         if (!arguments.SequenceEqual(originalArguments))
         {
             return(HandlebarsExpression.Helper(
                        helperExpression.HelperName,
                        arguments,
                        helperExpression.IsRaw));
         }
     }
     if (expression is SubExpressionExpression subExpression)
     {
         Expression childExpression = Visit(subExpression.Expression);
         if (childExpression != subExpression.Expression)
         {
             return(HandlebarsExpression.SubExpression(childExpression));
         }
     }
     return(expression);
 }
 public override Expression GetAccumulatedBlock()
 {
     return(HandlebarsExpression.Partial(
                _startingNode.PartialName,
                _startingNode.Argument,
                _body.Count > 1 ? Expression.Block(_body) : _body.First()));
 }
Esempio n. 10
0
 private PartialExpression ConvertToPartialExpression(Expression expression)
 {
     if (expression is PathExpression)
     {
         var pathExpression = (PathExpression)expression;
         _blockName = pathExpression.Path.Replace("#>", "");
         return(HandlebarsExpression.Partial(Expression.Constant(_blockName)));
     }
     else if (expression is HelperExpression)
     {
         var helperExpression = (HelperExpression)expression;
         _blockName = helperExpression.HelperName.Replace("#>", "");
         if (helperExpression.Arguments.Count() == 0)
         {
             return(HandlebarsExpression.Partial(Expression.Constant(_blockName)));
         }
         else if (helperExpression.Arguments.Count() == 1)
         {
             return(HandlebarsExpression.Partial(
                        Expression.Constant(_blockName),
                        helperExpression.Arguments.First()));
         }
         else
         {
             throw new InvalidOperationException("Cannot convert a multi-argument helper expression to a partial expression");
         }
     }
     else
     {
         throw new InvalidOperationException(string.Format("Cannot convert '{0}' to a partial expression", expression));
     }
 }
Esempio n. 11
0
        public override Expression GetAccumulatedBlock()
        {
            if (_accumulatedBody == null)
            {
                _accumulatedBody      = GetBlockBody();
                _accumulatedInversion = Expression.Block(Expression.Empty());
            }
            else if (_accumulatedInversion == null)
            {
                _accumulatedInversion = GetBlockBody();
            }

            var resultExpr = HandlebarsExpression.BlockHelper(
                _startingNode.HelperName,
                _startingNode.Arguments.Where(o => o.NodeType != (ExpressionType)HandlebarsExpressionType.BlockParamsExpression),
                _startingNode.Arguments.OfType <BlockParamsExpression>().SingleOrDefault() ?? BlockParamsExpression.Empty(),
                _accumulatedBody,
                _accumulatedInversion,
                _startingNode.IsRaw);

            if (_startingNode.IsRaw)
            {
                return(HandlebarsExpression.Statement(
                           resultExpr,
                           false,
                           _trimBefore,
                           _trimAfter));
            }

            return(resultExpr);
        }
Esempio n. 12
0
        public override Expression GetAccumulatedBlock()
        {
            if (_accumulatedBody == null)
            {
                _accumulatedBody      = GetBlockBody();
                _accumulatedInversion = Expression.Block(Expression.Empty());
            }
            else if (_accumulatedInversion == null)
            {
                _accumulatedInversion = GetBlockBody();
            }

            var resultExpr = HandlebarsExpression.BlockHelper(
                _startingNode.HelperName,
                _startingNode.Arguments,
                _accumulatedBody,
                _accumulatedInversion,
                _startingNode.IsRaw);

            if (_startingNode.IsRaw)
            {
                return(HandlebarsExpression.Statement(
                           resultExpr,
                           false,
                           _trimBefore,
                           _trimAfter));
            }

            return(resultExpr);
        }
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item         = enumerator.Current;
                var partialToken = item as PartialToken;
                if (partialToken != null)
                {
                    var partialName = partialToken.Value.Substring(1).Trim('"');
                    var arguments   = AccumulateArguments(enumerator);
                    if (arguments.Count == 0)
                    {
                        yield return(HandlebarsExpression.Partial(partialName));
                    }
                    else if (arguments.Count == 1)
                    {
                        yield return(HandlebarsExpression.Partial(partialName, arguments[0]));
                    }
                    else
                    {
                        throw new HandlebarsCompilerException(string.Format("Partial {0} can only accept 0 or 1 arguments", partialName));
                    }
                    yield return(enumerator.Current);
                }
                else
                {
                    yield return(item);
                }
            }
        }
 protected override Expression VisitBlockHelperExpression(BlockHelperExpression bhex)
 {
     return(HandlebarsExpression.BlockHelper(
                bhex.HelperName,
                bhex.Arguments.Select(arg => Visit(arg)),
                Visit(bhex.Body),
                Visit(bhex.Inversion)));
 }
        protected virtual Expression VisitStatementExpression(StatementExpression sex)
        {
            Expression body = Visit(sex.Body);

            if (body != sex.Body)
            {
                return(HandlebarsExpression.Statement(body, sex.IsEscaped, sex.TrimBefore, sex.TrimAfter));
            }
            return(sex);
        }
        protected virtual Expression VisitHelperExpression(HelperExpression hex)
        {
            var arguments = VisitExpressionList(hex.Arguments);

            if (arguments != hex.Arguments)
            {
                return(HandlebarsExpression.Helper(hex.HelperName, hex.IsBlock, arguments, hex.IsRaw));
            }
            return(hex);
        }
        protected virtual Expression VisitBoolishExpression(BoolishExpression bex)
        {
            Expression condition = Visit(bex.Condition);

            if (condition != bex.Condition)
            {
                return(HandlebarsExpression.Boolish(condition));
            }
            return(bex);
        }
        protected virtual Expression VisitSubExpression(SubExpressionExpression subex)
        {
            Expression expression = Visit(subex.Expression);

            if (expression != subex.Expression)
            {
                return(HandlebarsExpression.SubExpression(expression));
            }
            return(subex);
        }
        protected override Expression VisitDeferredSectionExpression(DeferredSectionExpression dsex)
        {
            var method = new Action <object, BindingContext, Action <TextWriter, object>, Action <TextWriter, object> >(RenderSection).Method;

            Expression path    = HandlebarsExpression.Path(dsex.Path.Path.Substring(1));
            Expression context = CompilationContext.BindingContext;

            Expression[] templates = GetDeferredSectionTemplates(dsex);

            return(Expression.Call(method, new[] { path, context }.Concat(templates)));
        }
        protected virtual Expression VisitDeferredSectionExpression(DeferredSectionExpression dsex)
        {
            PathExpression path = (PathExpression)Visit(dsex.Path);

            // Don't visit Body/Inversion - they will be compiled separately

            if (path != dsex.Path)
            {
                return(HandlebarsExpression.DeferredSection(path, dsex.Body, dsex.Inversion));
            }
            return(dsex);
        }
Esempio n. 21
0
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                if (!(item is StartExpressionToken token))
                {
                    yield return(item);

                    continue;
                }

                var isRaw = token.IsRaw;
                yield return(token);

                item = GetNext(enumerator);
                switch (item)
                {
                case Expression _:
                    yield return(item);

                    continue;

                case WordExpressionToken word when IsRegisteredHelperName(word.Value):
                    yield return(HandlebarsExpression.Helper(word.Value, false, isRaw, word.Context));

                    break;

                case WordExpressionToken word when IsRegisteredBlockHelperName(word.Value, isRaw):
                {
                    yield return(HandlebarsExpression.Helper(word.Value, true, isRaw, word.Context));

                    break;
                }

                case WordExpressionToken word when IsUnregisteredBlockHelperName(word.Value, isRaw, sequence):
                {
                    var expression = HandlebarsExpression.Helper(word.Value, true, isRaw, word.Context);
                    expression.IsBlock = true;
                    yield return(expression);

                    break;
                }

                default:
                    yield return(item);

                    break;
                }
            }
        }
        protected virtual Expression VisitIteratorExpression(IteratorExpression iex)
        {
            Expression sequence = Visit(iex.Sequence);

            // Don't visit Template/IfEmpty - they will be compiled separately

            if (sequence != iex.Sequence)
            {
                return(HandlebarsExpression.Iterator(sequence, iex.BlockParams, iex.Template, iex.IfEmpty));
            }
            return(iex);
        }
        protected virtual Expression VisitBlockHelperExpression(BlockHelperExpression bhex)
        {
            var arguments = VisitExpressionList(bhex.Arguments);

            // Don't visit Body/Inversion - they will be compiled separately

            if (arguments != bhex.Arguments)
            {
                return(HandlebarsExpression.BlockHelper(bhex.HelperName, arguments, bhex.BlockParams, bhex.Body, bhex.Inversion, bhex.IsRaw));
            }
            return(bhex);
        }
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                switch (item)
                {
                case HelperExpression helper:
                {
                    var helperArguments = AccumulateArguments(enumerator);
                    yield return(HandlebarsExpression.Helper(
                                     helper.HelperName,
                                     helper.IsBlock,
                                     helperArguments,
                                     helper.IsRaw));

                    yield return(enumerator.Current);

                    break;
                }

                case PathExpression path:
                {
                    var helperArguments = AccumulateArguments(enumerator);
                    if (helperArguments.Count > 0)
                    {
                        yield return(HandlebarsExpression.Helper(
                                         path.Path,
                                         false,
                                         helperArguments,
                                         ((EndExpressionToken)enumerator.Current)?.IsRaw ?? false));

                        yield return(enumerator.Current);
                    }
                    else
                    {
                        yield return(path);

                        yield return(enumerator.Current);
                    }

                    break;
                }

                default:
                    yield return(item);

                    break;
                }
            }
        }
Esempio n. 25
0
        private static object Convert(object item)
        {
            if (item is CommentToken commentToken)
            {
                return(HandlebarsExpression.Comment(commentToken.Value));
            }

            if (item is LayoutToken)
            {
                return(HandlebarsExpression.Comment(null));
            }

            return(item);
        }
        protected virtual Expression VisitPartialExpression(PartialExpression pex)
        {
            Expression partialName = Visit(pex.PartialName);
            Expression argument    = Visit(pex.Argument);

            // Don't visit Fallback - it will be compiled separately

            if (partialName != pex.PartialName ||
                argument != pex.Argument)
            {
                return(HandlebarsExpression.Partial(partialName, argument, pex.Fallback));
            }
            return(pex);
        }
 public override void HandleElement(Expression item)
 {
     if (IsElseBlock(item))
     {
         _accumulatedExpression = HandlebarsExpression.Iterator(
             _startingNode.Arguments.Single(),
             Expression.Block(_body));
         _body = new List <Expression>();
     }
     else
     {
         _body.Add((Expression)item);
     }
 }
Esempio n. 28
0
 public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
 {
     foreach (var item in sequence)
     {
         if (item is WordExpressionToken wordExpressionToken)
         {
             yield return(HandlebarsExpression.Path(wordExpressionToken.Value));
         }
         else
         {
             yield return(item);
         }
     }
 }
Esempio n. 29
0
 public override void HandleElement(Expression item)
 {
     if (IsElseBlock(item))
     {
         _accumulatedExpression = HandlebarsExpression.Iterator(
             _startingNode.Arguments.Single(o => o.NodeType != (ExpressionType)HandlebarsExpressionType.BlockParamsExpression),
             _startingNode.Arguments.OfType <BlockParamsExpression>().SingleOrDefault() ?? BlockParamsExpression.Empty(),
             Expression.Block(_body));
         _body = new List <Expression>();
     }
     else
     {
         _body.Add((Expression)item);
     }
 }
Esempio n. 30
0
        public ConditionalBlockAccumulatorContext(Expression startingNode)
            : base(startingNode)
        {
            startingNode = UnwrapStatement(startingNode);
            _blockName   = ((HelperExpression)startingNode).HelperName.Replace("#", "");
            if (new [] { "if", "unless" }.Contains(_blockName) == false)
            {
                throw new HandlebarsCompilerException(string.Format(
                                                          "Tried to convert {0} expression to conditional block", _blockName));
            }
            _testType = _blockName == "if";
            var argument = HandlebarsExpression.Boolish(((HelperExpression)startingNode).Arguments.Single());

            _currentCondition = _testType ? (Expression)argument : Expression.Not(argument);
        }