-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathConditionalBlocks.cs
208 lines (166 loc) · 6.42 KB
/
ConditionalBlocks.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
internal sealed class CBSyntaxException : Exception
{
public readonly int ErrorPosition;
public CBSyntaxException(string message, int spos) : base(message)
{
ErrorPosition = spos;
}
}
internal sealed class ConditionalBlocks
{
private readonly HashSet<string> CompileTimeTokens = new HashSet<string>();
private int SourcePosition;
public void LoadConditionalTokens(List<string> tokens)
{
CompileTimeTokens.Clear();
foreach (var token in tokens)
CompileTimeTokens.Add(token.ToLower().Trim());
}
private enum RBCBlockState
{
NoCondition,
FailsCondition,
MeetsCondition
}
private enum RBCParseState
{
Ready,
String,
BlockComment,
LineComment
}
private enum RCBBlockType
{
IFDEF,
IFNDEF,
ELSE,
ENDIF
}
private enum ConditionState
{
Matches,
DoesntMatch,
Else
}
private RBCParseState ParseState;
private RBCBlockState BlockState;
private Stack<RBCBlockState> PrevConditions = new Stack<RBCBlockState>();
private char[] InPlace;
public string ParseSource(string input)
{
InPlace = input.ToCharArray();
Regex regex = new Regex(@"^(#ifdef|#ifndef|#else|#endif)\b");
ReadOnlySpan<char> CurrString;
for (SourcePosition = 0; SourcePosition < input.Length; SourcePosition++)
{
switch (ParseState)
{
case RBCParseState.Ready:
if (input[SourcePosition] == '"')
ParseState = RBCParseState.String;
else if (input[SourcePosition] == '/')
{
if (input.Length < SourcePosition + 1)
break;
if (input[SourcePosition + 1] == '*')
ParseState = RBCParseState.BlockComment;
else if (input[SourcePosition + 1] == '/')
ParseState = RBCParseState.LineComment;
}
else if (input[SourcePosition] == '#')
{
CurrString = input.AsSpan().Slice(SourcePosition, Math.Min(10, input.Length - SourcePosition));
var resultantstring = CurrString.ToString();
if (!regex.IsMatch(resultantstring)) //there is no clean way around this sadly
break;
var match = regex.Match(resultantstring);
InterpretMatch(match);
SourcePosition--;
continue;
}
break;
case RBCParseState.String:
if (input[SourcePosition] == '"' && input[SourcePosition - 1] != '\\')
ParseState = RBCParseState.Ready;
break;
case RBCParseState.BlockComment:
if (input[SourcePosition] == '*' && input.Length > SourcePosition + 1 && input[SourcePosition + 1] == '/')
ParseState = RBCParseState.Ready;
break;
case RBCParseState.LineComment:
if (input[SourcePosition] == '\r' || input[SourcePosition] == '\n')
ParseState = RBCParseState.Ready;
break;
}
if (BlockState == RBCBlockState.FailsCondition && InPlace[SourcePosition] != '\n')
InPlace[SourcePosition] = ' ';
}
if (PrevConditions.Count > 0)
throw new CBSyntaxException("Expected #endif", SourcePosition);
return new string(InPlace);
}
private void InterpretMatch(Match match)
{
string mv = match.Value.Substring(1);
Enum.TryParse<RCBBlockType>(mv, true, out var result);
bool isTerminator = (result == RCBBlockType.ELSE || result == RCBBlockType.ENDIF);
bool isIf = result == RCBBlockType.IFDEF;
if (isTerminator && (PrevConditions.Count < 1 || BlockState == RBCBlockState.NoCondition))
throw new CBSyntaxException($"Extraneous {result.ToString().ToLower()}", SourcePosition);
ReplaceRange(match.Value.Length);
if (isTerminator)
{
if (result == RCBBlockType.ELSE)
{
if (IsChainNegated())
PrevConditions.Push(RBCBlockState.FailsCondition);
else
PrevConditions.Push(BlockState == RBCBlockState.MeetsCondition ? RBCBlockState.FailsCondition : RBCBlockState.MeetsCondition);
}
BlockState = PrevConditions.Pop();
return;
}
if (!TryFindToken(out string Token))
throw new CBSyntaxException($"Expected identifier", SourcePosition);
SourcePosition -= Token.Length;
ReplaceRange(Token.Length);
PrevConditions.Push(BlockState);
if (BlockState == RBCBlockState.FailsCondition)
return;
BlockState = (CompileTimeTokens.Contains(Token) == isIf) ? RBCBlockState.MeetsCondition : RBCBlockState.FailsCondition;
}
private bool IsChainNegated()
{
foreach (var condition in PrevConditions)
if (condition == RBCBlockState.FailsCondition)
return true;
return false;
}
private bool TryFindToken(out string Token)
{
Token = null;
StringBuilder tb = new StringBuilder();
while (SourcePosition < InPlace.Length && char.IsWhiteSpace(InPlace[SourcePosition]))
SourcePosition++; //safely ignore whitespace
if (SourcePosition >= InPlace.Length)
return false;
if (!char.IsLetterOrDigit(InPlace[SourcePosition]) && InPlace[SourcePosition] != '_')
return false;
tb.Append(InPlace[SourcePosition++]);
while (SourcePosition < InPlace.Length && char.IsLetterOrDigit(InPlace[SourcePosition]))
tb.Append(InPlace[SourcePosition++]);
Token = tb.ToString().ToLower();
return true;
}
private void ReplaceRange(int length)
{
for (int i = 0; i < length; i++)
if (InPlace[i + SourcePosition] != '\n')
InPlace[i + SourcePosition] = ' ';
SourcePosition += length;
}
}