-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkopik.cs
417 lines (338 loc) · 13.9 KB
/
Skopik.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Skopik
{
internal static class Skopik
{
private static bool m_lookupReady = false;
private static int[] m_lookup = new int[128];
internal static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
internal static readonly string[] Keywords = { "true", "false", "null" };
internal static readonly string CommentLineKey = "//";
internal static readonly string CommentBlockOpenKey = "/*";
internal static readonly string CommentBlockCloseKey = "*/";
internal static readonly char AssignmentKey = '=';
internal static readonly char ScopeBlockKey = ':'; // opening of a scope block
internal static readonly char[] BlockKeys = {
'{', '}',
'[', ']',
'(', ')',
};
internal static readonly char[] OperatorKeys = { '@', '"', '\'' };
internal static readonly char[] DelimiterKeys = { ',', ';' };
internal static readonly char[] SuffixKeys = { 'b', 'd', 'f', 'u', 'U', 'L' };
internal static readonly char[] ExponentialKeys = { 'e', 'E' };
internal static readonly char NegativePrefixKey = '-';
internal static readonly char DecimalPointKey = '.';
internal static readonly string HexadecimalPrefix = "0x";
internal static readonly SkopikDataType[] WordLookup = {
SkopikDataType.Boolean, // 'true'
SkopikDataType.Boolean, // 'false'
SkopikDataType.Null, // 'null'
};
internal static readonly SkopikDataType[] BlockLookup = {
SkopikDataType.Scope, // '{}'
SkopikDataType.Array, // '[]'
SkopikDataType.Tuple, // '()'
};
internal static readonly SkopikDataType[] OperatorLookup = {
SkopikDataType.Keyword, // '@'
SkopikDataType.String, // ' " '
SkopikDataType.String, // ' ' '
};
internal static readonly SkopikDataType[] DelimiterLookup = {
SkopikDataType.Array | SkopikDataType.Tuple,
// ','
SkopikDataType.Scope, // ';'
};
internal static readonly SkopikDataType[] SuffixLookup = {
SkopikDataType.Binary, // 'b'
SkopikDataType.Double, // 'd'
SkopikDataType.Float, // 'f'
SkopikDataType.Unsigned, // 'u'
SkopikDataType.Unsigned, // 'U'
SkopikDataType.Long, // 'L'
};
internal static void MapLookupTypes()
{
for (int i = 0; i < BlockKeys.Length; i += 2)
{
var opBlockType = BlockLookup[i >> 1];
// set open/close controls
m_lookup[BlockKeys[i + 0]] = (int)(opBlockType | SkopikDataType.OpBlockOpen);
m_lookup[BlockKeys[i + 1]] = (int)(opBlockType | SkopikDataType.OpBlockClose);
}
for (int i = 0; i < OperatorKeys.Length; i++)
m_lookup[OperatorKeys[i]] = (int)OperatorLookup[i];
for (int i = 0; i < DelimiterKeys.Length; i++)
m_lookup[DelimiterKeys[i]] = (int)(DelimiterLookup[i] | SkopikDataType.OpBlockDelim);
for (int i = 0; i < SuffixKeys.Length; i++)
m_lookup[SuffixKeys[i]] = (int)SuffixLookup[i];
m_lookup[AssignmentKey] = (int)SkopikDataType.OpStmtAssignmt;
m_lookup[ScopeBlockKey] = (int)SkopikDataType.OpStmtBlock;
}
// TODO: give this a datatype to work with
internal static string SanitizeNumber(string value, bool isHex)
{
var length = 0;
foreach (var c in value)
{
var flags = CharUtils.GetCharFlags(c);
// don't allow decimals/negative hexadecimal numbers
// (negative hex numbers need to be processed at another point)
if (!isHex)
{
if (c == DecimalPointKey || c == NegativePrefixKey)
{
++length;
continue;
}
}
if ((flags & CharacterTypeFlags.Digit) != 0)
{
++length;
continue;
}
if ((flags & CharacterTypeFlags.Letter) != 0)
{
// ABCDEF or abcdef?
// (adding 1 prevents 'g' and 'G' false positives)
if (isHex && (((c + 1) & ~0x67) == 0))
{
++length;
continue;
}
var type = GetDataType(c);
if (type != SkopikDataType.None)
{
// first suffix character, we can safely break
break;
}
}
// can't sanitize the string
// reset the length and stop
length = 0;
break;
}
return (length > 0 ) ? value.Substring(0, length) : String.Empty;
}
internal static SkopikDataType GetDataType(char value)
{
if (!m_lookupReady)
{
MapLookupTypes();
m_lookupReady = true;
}
return (SkopikDataType)m_lookup[value];
}
internal static SkopikDataType GetDataType(string value)
{
return GetDataType(value[0]);
}
internal static bool IsDataType(char value, SkopikDataType dataType)
{
var type = (int)GetDataType(value);
var checkType = (int)dataType;
return ((type & checkType) == checkType);
}
internal static bool IsDataType(string value, SkopikDataType dataType)
{
return IsDataType(value[0], dataType);
}
internal static SkopikDataType GetWordDataType(string value)
{
for (int i = 0; i < Keywords.Length; i++)
{
var k = Keywords[i];
if (value.Length != k.Length)
continue;
if (value.Equals(k, StringComparison.InvariantCultureIgnoreCase))
return WordLookup[i];
}
return SkopikDataType.None;
}
internal static SkopikDataType GetNumberDataType(string value)
{
if (value.Length < 1)
return SkopikDataType.None;
var strIndex = 0;
var isNegative = false;
var isHex = false;
var hasExponent = false;
var hasDigit = false;
var hasSeparator = false; // floats
var numberType = SkopikDataType.None;
var suffixType = SkopikDataType.None;
if (IsHexadecimalNumber(value))
{
strIndex += 2;
isHex = true;
if (strIndex == value.Length)
throw new InvalidOperationException($"Malformed hexadecimal number data: '{value}'");
}
for (int i = strIndex; i < value.Length; i++)
{
var c = value[i];
var flags = CharUtils.GetCharFlags(c);
if ((flags & CharacterTypeFlags.Digit) != 0)
{
hasDigit = true;
}
else if ((flags & CharacterTypeFlags.Letter) != 0)
{
suffixType |= GetDataType(c);
if (suffixType == SkopikDataType.None)
{
var eIdx = ((flags & CharacterTypeFlags.Lowercase) != 0) ? 0 : 1;
// check for exponential float
if (!isHex && (c == ExponentialKeys[eIdx]))
{
if (hasExponent || (!hasDigit || (!hasDigit && !hasSeparator)))
throw new InvalidOperationException($"Malformed number data: '{value}'");
hasExponent = true;
}
}
}
else
{
if (c == DecimalPointKey)
{
if (!hasDigit || hasSeparator)
throw new InvalidOperationException($"Malformed number data: '{value}'");
hasSeparator = true;
}
else if (c == NegativePrefixKey)
{
if (hasExponent)
{
if (!hasDigit || (!hasDigit && !hasSeparator))
throw new InvalidOperationException($"Malformed number data: '{value}'");
// exponential float
// just continue normally
}
else
{
if (isNegative || hasDigit || hasSeparator)
throw new InvalidOperationException($"Malformed number data: '{value}'");
// negative number
isNegative = true;
}
}
}
}
// figure out the number type
if (suffixType == SkopikDataType.None)
{
// setup the default value if we couldn't figure it out above
if (hasSeparator)
{
if (hasExponent)
Debug.WriteLine($"Successfully detected an exponential decimal: '{value}'");
numberType |= SkopikDataType.Double;
}
else
{
numberType |= SkopikDataType.Integer32;
}
}
else
{
if ((suffixType & (SkopikDataType.Float | SkopikDataType.Double)) != 0)
numberType |= (suffixType & (SkopikDataType.Float | SkopikDataType.Double));
if ((suffixType & SkopikDataType.NumberFlagMask) != 0)
{
numberType |= SkopikDataType.Integer;
// binary data has no sign
if ((suffixType & SkopikDataType.Binary) == 0)
{
// signed?
if ((suffixType & SkopikDataType.Unsigned) == 0)
numberType |= SkopikDataType.Signed;
}
numberType |= (suffixType & (SkopikDataType.NumberFlagMask));
}
}
// can still be invalid!
return numberType;
}
internal static SkopikDataType GetAnyDataType(string value)
{
var dataType = SkopikDataType.None;
if (value.Length != 1)
{
// word data?
dataType = GetWordDataType(value);
}
// control flow data?
if (dataType == SkopikDataType.None)
dataType = GetDataType(value[0]);
// number data?
if (dataType == SkopikDataType.None)
dataType = GetNumberDataType(value);
// may still be invalid
return dataType;
}
internal static bool IsAssignmentOperator(string value)
{
return IsDataType(value, SkopikDataType.OpStmtAssignmt);
}
internal static bool IsScopeBlockOperator(string value)
{
return IsDataType(value, SkopikDataType.OpStmtBlock);
}
internal static bool IsEndStatementOperator(string value)
{
return IsDataType(value, SkopikDataType.OpBlockDelim);
}
internal static bool IsArraySeparator(string value)
{
return IsDataType(value, SkopikDataType.OpArrayDelim);
}
internal static bool IsScopeSeparator(string value)
{
return IsDataType(value, SkopikDataType.OpScopeDelim);
}
internal static bool IsDelimiter(string value, SkopikDataType subType = SkopikDataType.None)
{
return IsDataType(value, subType | SkopikDataType.OpBlockDelim);
}
internal static bool IsOpeningBrace(string value, SkopikDataType subType = SkopikDataType.None)
{
return IsDataType(value, subType | SkopikDataType.OpBlockOpen);
}
internal static bool IsClosingBrace(string value, SkopikDataType subType = SkopikDataType.None)
{
return IsDataType(value, subType | SkopikDataType.OpBlockClose);
}
internal static bool IsNegativeNumber(string value)
{
return ((value.Length > 1) && (value[0] == NegativePrefixKey));
}
internal static bool IsHexadecimalNumber(string value)
{
if (value.Length < HexadecimalPrefix.Length)
return false;
var strIndex = 0;
if (IsNegativeNumber(value))
++strIndex;
for (int i = 0; i < HexadecimalPrefix.Length; i++)
{
if (value[strIndex + i] != HexadecimalPrefix[i])
return false;
}
return true;
}
internal static bool IsNumberValue(SkopikDataType dataType)
{
return ((dataType & SkopikDataType.Integer) != 0);
}
internal static bool IsDecimalNumberValue(SkopikDataType dataType)
{
return ((dataType & (SkopikDataType.Float | SkopikDataType.Double)) != 0);
}
}
}