-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConditionParser.cs
172 lines (146 loc) · 5.32 KB
/
ConditionParser.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using jamconverter.AST;
using BinaryOperatorExpression = jamconverter.AST.BinaryOperatorExpression;
using Expression = jamconverter.AST.Expression;
namespace jamconverter
{
partial class Parser
{
interface IEvaluationStackNode
{
}
class ValueNode : IEvaluationStackNode
{
public readonly NodeList<Expression> Expressions;
public ValueNode(NodeList<Expression> expressions)
{
Expressions = expressions;
}
}
class ParenthesisOpenNode : IEvaluationStackNode
{
}
class OperatorNode : IEvaluationStackNode
{
public readonly Operator Operator;
public OperatorNode(Operator @operator)
{
Operator = @operator;
}
}
public Expression ParseCondition()
{
var stack = new Stack<IEvaluationStackNode>();
Operator? mostRecentOperator = null;
while (true)
{
var nextToken = _scanResult.Peek();
if (nextToken.literal == "(")
{
stack.Push(new ParenthesisOpenNode());
_scanResult.Next();
continue;
}
if (nextToken.tokenType == TokenType.Not)
{
stack.Push(new OperatorNode(Operator.Not));
_scanResult.Next();
mostRecentOperator = Operator.Not;
continue;
}
stack.Push(ParseValue(mostRecentOperator));
nextToken = _scanResult.Peek();
var nextTokenType = nextToken.tokenType;
if (nextToken.tokenType == TokenType.ParenthesisClose)
{
CollapseStack(stack);
_scanResult.Next();
nextTokenType = _scanResult.Peek().tokenType; ;
}
if (!IsBinaryOperator(nextTokenType))
{
CollapseStack(stack);
return ((ValueNode)stack.Single()).Expressions.Single();
}
_scanResult.Next();
var nextOperator = OperatorFor(nextTokenType);
if (mostRecentOperator.HasValue)
{
var nextPrecedence = PrecendenceFor(nextOperator);
var mostRecentPrecedence = PrecendenceFor(mostRecentOperator.Value);
if (nextPrecedence < mostRecentPrecedence)
CollapseStack(stack);
}
stack.Push(new OperatorNode(nextOperator));
mostRecentOperator = nextOperator;
}
}
private void CollapseStack(Stack<IEvaluationStackNode> stack)
{
while (stack.Count() > 1)
{
if (stack.Peek() is ParenthesisOpenNode)
{
stack.Pop();
return;
}
var rightNode = (ValueNode)stack.Pop();
var evaluationStackNode = stack.Pop();
if (evaluationStackNode is ParenthesisOpenNode)
{
stack.Push(rightNode);
return;
}
var @operator = (OperatorNode)evaluationStackNode;
if (IsUnaryOperator(@operator.Operator))
{
stack.Push(new ValueNode(new NodeList<Expression>() { new NotOperatorExpression()
{
Expression = rightNode.Expressions.Single()
}}));
continue;
}
var left = ((ValueNode)stack.Pop()).Expressions;
var boe = new BinaryOperatorExpression() { Left = left.Single(), Operator = @operator.Operator, Right = rightNode.Expressions };
stack.Push(new ValueNode(new NodeList<Expression>() { boe }));
}
}
private bool IsUnaryOperator(Operator @operator)
{
return @operator == Operator.Not;
}
ValueNode ParseValue(Operator? @operator)
{
if (@operator == Operator.In)
return new ValueNode(new NodeList<Expression>(ParseExpressionList()));
var expression = ParseExpression();
if (expression == null)
throw new Exception();
return new ValueNode(new NodeList<Expression> { expression });
}
private static int PrecendenceFor(Operator o)
{
var order = new List<Operator>()
{
Operator.Or,
Operator.And,
Operator.Subtract,
Operator.AssignmentIfEmpty,
Operator.Append,
Operator.Not,
Operator.In,
Operator.Assignment,
Operator.NotEqual,
Operator.GreaterThan,
Operator.LessThan
};
int precendenceFor = order.IndexOf(o);
if (precendenceFor == -1)
throw new NotSupportedException();
return precendenceFor;
}
}
}