-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathConditionalBlockAccumulatorContext.cs
More file actions
159 lines (137 loc) · 6.07 KB
/
Copy pathConditionalBlockAccumulatorContext.cs
File metadata and controls
159 lines (137 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Linq;
namespace HandlebarsDotNet.Compiler
{
internal class ConditionalBlockAccumulatorContext : BlockAccumulatorContext
{
private enum TestType { Direct, Reverse }
private static readonly HashSet<string> ValidHelperNames = new HashSet<string> { "if", "unless" };
private readonly List<ConditionalExpression> _conditionalBlock = new List<ConditionalExpression>();
private Expression _currentCondition;
private List<Expression> _bodyBuffer = new List<Expression>();
public sealed override string BlockName { get; protected set; }
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 (value, hashParameters) = UnwrapBoolishHelperArguments(helperExpression.Arguments, helperExpression.Context);
var argument = HandlebarsExpression.Boolish(value, hashParameters);
_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)
};
}
public override void HandleElement(Expression item)
{
if (IsElseBlock(item))
{
_conditionalBlock.Add(Expression.IfThen(_currentCondition, SinglifyExpressions(_bodyBuffer)));
if (IsElseIfBlock(item))
{
_currentCondition = GetElseIfTestExpression(item);
}
else
{
_currentCondition = null;
}
_bodyBuffer = new List<Expression>();
}
else
{
_bodyBuffer.Add((Expression)item);
}
}
public override bool IsClosingElement(Expression item)
{
if (IsClosingNode(item))
{
if (_currentCondition != null)
{
_conditionalBlock.Add(Expression.IfThen(_currentCondition, SinglifyExpressions(_bodyBuffer)));
}
else
{
var lastCondition = _conditionalBlock.Last();
_conditionalBlock[_conditionalBlock.Count - 1] = Expression.IfThenElse(
lastCondition.Test,
lastCondition.IfTrue,
SinglifyExpressions(_bodyBuffer));
}
return true;
}
else
{
return false;
}
}
public override Expression GetAccumulatedBlock()
{
ConditionalExpression singleConditional = null;
foreach (var condition in _conditionalBlock.AsEnumerable().Reverse())
{
singleConditional = Expression.IfThenElse(
condition.Test,
condition.IfTrue,
(Expression)singleConditional ?? condition.IfFalse);
}
return singleConditional;
}
private bool IsElseBlock(Expression item)
{
item = UnwrapStatement(item);
return item is HelperExpression && ((HelperExpression)item).HelperName == "else";
}
private bool IsElseIfBlock(Expression item)
{
item = UnwrapStatement(item);
return IsElseBlock(item) && ((HelperExpression)item).Arguments.Count() == 2;
}
private Expression GetElseIfTestExpression(Expression item)
{
item = UnwrapStatement(item);
var helperExpression = (HelperExpression)item;
var (value, hashParameters) = UnwrapBoolishHelperArguments(helperExpression.Arguments.Skip(1), helperExpression.Context);
return HandlebarsExpression.Boolish(value, hashParameters);
}
private bool IsClosingNode(Expression item)
{
item = UnwrapStatement(item);
return item is PathExpression expression && expression.Path == "/" + BlockName;
}
private static Expression SinglifyExpressions(IEnumerable<Expression> expressions)
{
if (expressions.IsMultiple())
{
return Expression.Block(expressions);
}
return expressions.SingleOrDefault() ?? Expression.Empty();
}
private static (Expression Value, HashParametersExpression HashParameters) UnwrapBoolishHelperArguments(IEnumerable<Expression> arguments, IReaderContext context)
{
var value = arguments.First();
if (arguments.Count() == 1)
{
var blankHashParameters = HandlebarsExpression.HashParametersExpression(new Dictionary<string, Expression>());
return (value, blankHashParameters);
}
if (arguments.Count() == 2 && arguments.Last() is HashParametersExpression hashParameters)
{
return (value, hashParameters);
}
throw new HandlebarsCompilerException("Invalid argument list for conditional block.", context);
}
}
}