-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStepTokenizer.cs
318 lines (272 loc) · 9.48 KB
/
StepTokenizer.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Ara3D.Buffers;
namespace Ara3D.StepParser
{
public static class StepTokenizer
{
public static readonly StepTokenType[] TokenLookup =
CreateTokenLookup();
public static readonly bool[] IsNumberLookup =
CreateNumberLookup();
public static readonly bool[] IsIdentLookup =
CreateIdentLookup();
public static StepTokenType[] CreateTokenLookup()
{
var r = new StepTokenType[256];
for (var i = 0; i < 256; i++)
r[i] = GetTokenType((byte)i);
return r;
}
public static bool[] CreateNumberLookup()
{
var r = new bool[256];
for (var i = 0; i < 256; i++)
r[i] = IsNumberChar((byte)i);
return r;
}
public static bool[] CreateIdentLookup()
{
var r = new bool[256];
for (var i = 0; i < 256; i++)
r[i] = IsIdentOrDigitChar((byte)i);
return r;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static StepTokenType LookupToken(byte b)
=> TokenLookup[b];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNumberChar(byte b)
{
switch (b)
{
case (byte)'0':
case (byte)'1':
case (byte)'2':
case (byte)'3':
case (byte)'4':
case (byte)'5':
case (byte)'6':
case (byte)'7':
case (byte)'8':
case (byte)'9':
case (byte)'E':
case (byte)'e':
case (byte)'+':
case (byte)'-':
case (byte)'.':
return true;
}
return false;
}
public static StepTokenType GetTokenType(byte b)
{
switch (b)
{
case (byte)'0':
case (byte)'1':
case (byte)'2':
case (byte)'3':
case (byte)'4':
case (byte)'5':
case (byte)'6':
case (byte)'7':
case (byte)'8':
case (byte)'9':
case (byte)'+':
case (byte)'-':
return StepTokenType.Number;
case (byte)' ':
case (byte)'\t':
return StepTokenType.Whitespace;
case (byte)'\n':
case (byte)'\r':
return StepTokenType.LineBreak;
case (byte)'\'':
case (byte)'"':
return StepTokenType.String;
case (byte)'.':
return StepTokenType.Symbol;
case (byte)'#':
return StepTokenType.Id;
case (byte)';':
return StepTokenType.EndOfLine;
case (byte)'(':
return StepTokenType.BeginGroup;
case (byte)'=':
return StepTokenType.Definition;
case (byte)')':
return StepTokenType.EndGroup;
case (byte)',':
return StepTokenType.Separator;
case (byte)'$':
return StepTokenType.Unassigned;
case (byte)'*':
return StepTokenType.Redeclared;
case (byte)'/':
return StepTokenType.Comment;
case (byte)'a':
case (byte)'b':
case (byte)'c':
case (byte)'d':
case (byte)'e':
case (byte)'f':
case (byte)'g':
case (byte)'h':
case (byte)'i':
case (byte)'j':
case (byte)'k':
case (byte)'l':
case (byte)'m':
case (byte)'n':
case (byte)'o':
case (byte)'p':
case (byte)'q':
case (byte)'r':
case (byte)'s':
case (byte)'t':
case (byte)'u':
case (byte)'v':
case (byte)'w':
case (byte)'x':
case (byte)'y':
case (byte)'z':
case (byte)'A':
case (byte)'B':
case (byte)'C':
case (byte)'D':
case (byte)'E':
case (byte)'F':
case (byte)'G':
case (byte)'H':
case (byte)'I':
case (byte)'J':
case (byte)'K':
case (byte)'L':
case (byte)'M':
case (byte)'N':
case (byte)'O':
case (byte)'P':
case (byte)'Q':
case (byte)'R':
case (byte)'S':
case (byte)'T':
case (byte)'U':
case (byte)'V':
case (byte)'W':
case (byte)'X':
case (byte)'Y':
case (byte)'Z':
case (byte)'_':
return StepTokenType.Ident;
default:
return StepTokenType.Unknown;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsWhiteSpace(byte b)
=> b == ' ' || b == '\t';
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLineBreak(byte b)
=> b == '\n' || b == '\r';
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsIdent(byte b)
=> b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' || b == '_';
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDigit(byte b)
=> b >= '0' && b <= '9';
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsIdentOrDigitChar(byte b)
=> IsIdent(b) || IsDigit(b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe byte* AdvancePast(byte* begin, byte* end, string s)
{
if (end - begin < s.Length)
return null;
foreach (var c in s)
if (*begin++ != (byte)c)
return null;
return begin;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe StepToken ParseToken(byte* begin, byte* end)
{
var cur = begin;
var tt = InternalParseToken(ref cur, end);
Debug.Assert(cur < end);
var span = new ByteSpan(begin, cur);
return new StepToken(span, tt);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool EatWSpace(ref StepToken cur, byte* end)
{
while (cur.Type == StepTokenType.Comment
|| cur.Type == StepTokenType.Whitespace
|| cur.Type == StepTokenType.LineBreak)
{
if (!ParseNextToken(ref cur, end))
return false;
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool ParseNextToken(ref StepToken prev, byte* end)
{
var cur = prev.Span.End();
if (cur >= end) return false;
prev = ParseToken(cur, end);
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe StepTokenType InternalParseToken(ref byte* cur, byte* end)
{
var type = TokenLookup[*cur++];
switch (type)
{
case StepTokenType.Ident:
while (IsIdentLookup[*cur])
cur++;
break;
case StepTokenType.String:
// usually it is as single quote,
// but in rare cases it could be a double quote
var quoteChar = *(cur - 1);
while (cur < end)
{
if (*cur++ == quoteChar)
{
if (*cur != quoteChar)
break;
else
cur++;
}
}
break;
case StepTokenType.LineBreak:
while (IsLineBreak(*cur))
cur++;
break;
case StepTokenType.Number:
while (IsNumberLookup[*cur])
cur++;
break;
case StepTokenType.Symbol:
while (*cur++ != '.')
{
}
break;
case StepTokenType.Id:
while (IsNumberLookup[*cur])
cur++;
break;
case StepTokenType.Comment:
var prev = *cur++;
while (cur < end && (prev != '*' || *cur != '/'))
prev = *cur++;
cur++;
break;
}
return type;
}
}
}