예제 #1
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));
     }
 }
        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);
                }
            }
        }
예제 #3
0
        public 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;
                    }
                    if (item is WordExpressionToken word && IsRegisteredHelperName(word.Value))
                    {
                        yield return(HandlebarsExpression.HelperExpression(word.Value));
                    }
                    else
                    {
                        yield return(item);
                    }
                }
예제 #4
0
 public override Expression GetAccumulatedBlock()
 {
     return(HandlebarsExpression.PartialExpression(
                _startingNode.PartialName,
                _startingNode.Argument,
                _body.Count > 1 ? Expression.Block(_body) : _body.First()));
 }
 protected override Expression VisitBlockHelperExpression(BlockHelperExpression bhex)
 {
     return(HandlebarsExpression.BlockHelperExpression(
                bhex.HelperName,
                bhex.Arguments.Select(Visit),
                Visit(bhex.Body),
                Visit(bhex.Inversion)));
 }
 private static object Convert(object item)
 {
     if (item is CommentToken commentToken)
     {
         return(HandlebarsExpression.CommentExpression(commentToken.Value));
     }
     if (item is LayoutToken)
     {
         return(HandlebarsExpression.CommentExpression(null));
     }
     return(item);
 }
예제 #7
0
        protected override System.Linq.Expressions.Expression VisitDeferredSectionExpression(DeferredSectionExpression dsex)
        {
#if netstandard
            var method = new Action <object, BindingContext, Action <TextWriter, object>, Action <TextWriter, object> >(RenderSection).GetMethodInfo();
#else
            var method = new Action <object, BindingContext, Action <TextWriter, object>, Action <TextWriter, object> >(RenderSection).Method;
#endif
            System.Linq.Expressions.Expression   path      = HandlebarsExpression.PathExpression(dsex.Path.Path.Substring(1));
            System.Linq.Expressions.Expression   context   = CompilationContext.BindingContext;
            System.Linq.Expressions.Expression[] templates = GetDeferredSectionTemplates(dsex);

            return(System.Linq.Expressions.Expression.Call(method, new[] { path, context }.Concat(templates)));
        }
 public override void HandleElement(Expression item)
 {
     if (IsElseBlock(item))
     {
         _accumulatedExpression = HandlebarsExpression.IteratorExpression(
             _startingNode.Arguments.Single(),
             Expression.Block(_body));
         _body = new List <Expression>();
     }
     else
     {
         _body.Add(item);
     }
 }
예제 #9
0
 public IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
 {
     foreach (var item in sequence)
     {
         if (item is WordExpressionToken wordExpressionToken)
         {
             yield return(HandlebarsExpression.PathExpression(wordExpressionToken.Value));
         }
         else
         {
             yield return(item);
         }
     }
 }
        public ConditionalBlockAccumulatorContext(Expression startingNode)
        {
            var item = (HelperExpression)UnwrapStatement(startingNode);

            BlockName = item.HelperName.Replace("#", "");
            if (new [] { "if", "unless" }.Contains(BlockName) == false)
            {
                throw new HandlebarsCompilerException(string.Format(
                                                          "Tried to convert {0} expression to conditional block", BlockName));
            }
            var testType = BlockName == "if";
            var argument = HandlebarsExpression.BoolishExpression(item.Arguments.Single());

            _currentCondition = testType ? (Expression)argument : Expression.Not(argument);
        }
예제 #11
0
        private static SubExpressionExpression BuildSubExpression(IEnumerator <object> enumerator)
        {
            object item = GetNext(enumerator);
            var    path = item as PathExpression;

            if (path == null)
            {
                throw new HandlebarsCompilerException("Found a sub-expression that does not contain a path expression");
            }
            var helperArguments = AccumulateSubExpression(enumerator);

            return(HandlebarsExpression.SubExpressionExpression(
                       HandlebarsExpression.HelperExpression(
                           path.Path,
                           helperArguments)));
        }
예제 #12
0
        private static object ParseValue(string value)
        {
            if (value.StartsWith("'") || value.StartsWith("\""))
            {
                return(value.Trim('\'', '"'));
            }
            if (bool.TryParse(value, out bool boolValue))
            {
                return(boolValue);
            }
            if (int.TryParse(value, out int intValue))
            {
                return(intValue);
            }

            return(HandlebarsExpression.PathExpression(value));
        }
예제 #13
0
        private static object Convert(object item)
        {
            var commentToken = item as CommentToken;

            if (commentToken != null)
            {
                return(HandlebarsExpression.Comment(commentToken.Value));
            }
            else if (item is LayoutToken)
            {
                return(HandlebarsExpression.Comment(null));
            }
            else
            {
                return(item);
            }
        }
예제 #14
0
 public IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
 {
     foreach (var item in sequence)
     {
         if (item is StaticToken staticToken)
         {
             if (staticToken.Value != string.Empty)
             {
                 yield return(HandlebarsExpression.StaticExpression(((StaticToken)item).Value));
             }
         }
         else
         {
             yield return(item);
         }
     }
 }
예제 #15
0
        public IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                if (item is PartialToken)
                {
                    var arguments = AccumulateArguments(enumerator);
                    if (arguments.Count == 0)
                    {
                        throw new HandlebarsCompilerException("A partial must have a name");
                    }

                    var partialName = arguments[0];

                    if (partialName is PathExpression pathExpression)
                    {
                        partialName = Expression.Constant(pathExpression.Path);
                    }

                    switch (arguments.Count)
                    {
                    case 1:
                        yield return(HandlebarsExpression.PartialExpression(partialName));

                        break;

                    case 2:
                        yield return(HandlebarsExpression.PartialExpression(partialName, arguments [1]));

                        break;

                    default:
                        throw new HandlebarsCompilerException("A partial can only accept 0 or 1 arguments");
                    }
                    yield return(enumerator.Current);
                }
                else
                {
                    yield return(item);
                }
            }
        }
예제 #16
0
        public override IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var item = enumerator.Current;
                if (item is HelperExpression)
                {
                    var helper          = item as HelperExpression;
                    var helperArguments = AccumulateArguments(enumerator);
                    yield return(HandlebarsExpression.Helper(
                                     helper.HelperName,
                                     helperArguments));

                    yield return(enumerator.Current);
                }
                else if (item is PathExpression)
                {
                    var helperArguments = AccumulateArguments(enumerator);
                    if (helperArguments.Count > 0)
                    {
                        var path = item as PathExpression;
                        yield return(HandlebarsExpression.Helper(
                                         path.Path,
                                         helperArguments));

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

                        yield return(enumerator.Current);
                    }
                }
                else
                {
                    yield return(item);
                }
            }
        }
예제 #17
0
        public override Expression GetAccumulatedBlock()
        {
            if (_accumulatedBody == null)
            {
                _accumulatedBody      = Expression.Block(_body);
                _accumulatedInversion = Expression.Block(Expression.Empty());
            }
            else if (_accumulatedInversion == null && _body.Any())
            {
                _accumulatedInversion = Expression.Block(_body);
            }
            else
            {
                _accumulatedInversion = Expression.Block(Expression.Empty());
            }

            return(HandlebarsExpression.DeferredSectionExpression(
                       _startingNode,
                       _accumulatedBody,
                       _accumulatedInversion));
        }
 public override Expression GetAccumulatedBlock()
 {
     if (_accumulatedBody == null)
     {
         _accumulatedBody      = Expression.Block(_body);
         _accumulatedInversion = Expression.Block(Expression.Empty());
     }
     else if (_accumulatedInversion == null && _body.Any())
     {
         _accumulatedInversion = Expression.Block(_body);
     }
     else
     {
         _accumulatedInversion = Expression.Block(Expression.Empty());
     }
     return(HandlebarsExpression.BlockHelperExpression(
                _startingNode.HelperName,
                _startingNode.Arguments,
                _accumulatedBody,
                _accumulatedInversion));
 }
예제 #19
0
        public void Variables()
        {
            var expr   = "v";
            var parsed = HandlebarsExpression.Parse(expr);

            Assert.IsType <HandlebarsVariable>(parsed);
            Assert.Equal("v", (parsed as HandlebarsVariable).Name);

            expr   = "var1";
            parsed = HandlebarsExpression.Parse(expr);
            Assert.IsType <HandlebarsVariable>(parsed);
            Assert.Equal("var1", (parsed as HandlebarsVariable).Name);

            expr   = "var_1";
            parsed = HandlebarsExpression.Parse(expr);
            Assert.IsType <HandlebarsVariable>(parsed);
            Assert.Equal("var_1", (parsed as HandlebarsVariable).Name);

            expr = "*"; // illegal character
            Assert.Throws <Sprache.ParseException>(() => HandlebarsExpression.Parse(expr));
        }
예제 #20
0
        public IEnumerable <object> ConvertTokens(IEnumerable <object> sequence)
        {
            var enumerator = sequence.GetEnumerator();

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

                if (startExpression == null)
                {
                    yield return(item);

                    continue;
                }

                var possibleBody = GetNext(enumerator);
                if (!(possibleBody is Expression))
                {
                    throw new HandlebarsCompilerException(string.Format("Token '{0}' could not be converted to an expression", possibleBody));
                }

                var endExpression = GetNext(enumerator) as EndExpressionToken;
                if (endExpression == null)
                {
                    throw new HandlebarsCompilerException("Handlebars statement was not reduced to a single expression");
                }

                if (endExpression.IsEscaped != startExpression.IsEscaped)
                {
                    throw new HandlebarsCompilerException("Starting and ending handlebars do not match");
                }

                yield return(HandlebarsExpression.StatementExpression(
                                 (Expression)possibleBody,
                                 startExpression.IsEscaped,
                                 startExpression.TrimPreceedingWhitespace,
                                 endExpression.TrimTrailingWhitespace));
            }
        }
        public override bool IsClosingElement(Expression item)
        {
            if (!IsClosingNode(item))
            {
                return(false);
            }

            if (_accumulatedExpression == null)
            {
                _accumulatedExpression = HandlebarsExpression.IteratorExpression(
                    _startingNode.Arguments.Single(),
                    Expression.Block(_body));
            }
            else
            {
                _accumulatedExpression = HandlebarsExpression.IteratorExpression(
                    ((IteratorExpression)_accumulatedExpression).Sequence,
                    ((IteratorExpression)_accumulatedExpression).Template,
                    Expression.Block(_body));
            }
            return(true);
        }
예제 #22
0
 protected override System.Linq.Expressions.Expression VisitHelperExpression(HelperExpression hex)
 {
     return(HandlebarsExpression.Helper(
                hex.HelperName,
                hex.Arguments.Select(Visit)));
 }
예제 #23
0
 protected override System.Linq.Expressions.Expression VisitSubExpression(SubExpressionExpression subex)
 {
     return(HandlebarsExpression.SubExpression(
                Visit(subex.Expression)));
 }
예제 #24
0
 protected override Expression VisitBoolishExpression(BoolishExpression bex)
 {
     return(HandlebarsExpression.BoolishExpression(Visit(bex.ConditionExpression)));
 }
 private static Expression GetElseIfTestExpression(Expression item)
 {
     item = UnwrapStatement(item);
     return(HandlebarsExpression.BoolishExpression(((HelperExpression)item).Arguments.Skip(1).Single()));
 }
 protected override System.Linq.Expressions.Expression VisitBoolishExpression(BoolishExpression bex)
 {
     return(HandlebarsExpression.Boolish(Visit(bex.Condition)));
 }