-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cs
478 lines (424 loc) · 19.1 KB
/
Lexer.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
using CowSpeak.Exceptions;
using System.Reflection;
namespace CowSpeak
{
public static class Lexer
{
static Lexer()
{
terms.Sort((x, y) => y.Length.CompareTo(x.Length));
}
public static Token ParseToken(string token, bool @throw = true, int index = -1)
{
while (token.Length > 0 && token[0] == ' ')
token = token.Remove(0);
if (token.Length > 0)
{
if (Interpreter.Definitions.ContainsKey(token))
token = Interpreter.Definitions[token].To;
if (Utils.IsHexadecimal(token))
token = long.Parse(token.Substring(2), NumberStyles.HexNumber).ToString();
Type type = Type.GetType(token, false);
if (type != null)
return new Token(TokenType.TypeIdentifier, token, index);
switch (token)
{
case "true":
case "false":
return new Token(TokenType.BooleanLiteral, token, index);
case Syntax.Statements.Return:
return new Token(TokenType.ReturnStatement, token, index);
case Syntax.Operators.Or:
return new Token(TokenType.OrOperator, token, index);
case Syntax.Operators.BitwiseOR:
return new Token(TokenType.BitwiseOROperator, token, index);
case Syntax.Operators.BitwiseAND:
return new Token(TokenType.BitwiseANDOperator, token, index);
case Syntax.Comparators.IsEqual:
return new Token(TokenType.IsEqualOperator, token, index);
case Syntax.Comparators.IsNotEqual:
return new Token(TokenType.IsNotEqualOperator, token, index);
case Syntax.Comparators.IsGreaterThan:
return new Token(TokenType.IsGreaterThanOperator, token, index);
case Syntax.Comparators.IsLessThan:
return new Token(TokenType.IsLessThanOperator, token, index);
case Syntax.Comparators.IsGreaterThanOrEqual:
return new Token(TokenType.IsGreaterThanOrEqualOperator, token, index);
case Syntax.Comparators.IsLessThanOrEqual:
return new Token(TokenType.IsLessThanOrEqualOperator, token, index);
case Syntax.Operators.Add:
return new Token(TokenType.AddOperator, token, index);
case Syntax.Operators.And:
return new Token(TokenType.AndOperator, token, index);
case Syntax.Operators.Subtract:
return new Token(TokenType.SubtractOperator, token, index);
case Syntax.Operators.Multiply:
return new Token(TokenType.MultiplyOperator, token, index);
case Syntax.Operators.Divide:
return new Token(TokenType.DivideOperator, token, index);
case Syntax.Operators.Modulo:
return new Token(TokenType.ModuloOperator, token, index);
case Syntax.Operators.Equal:
return new Token(TokenType.EqualOperator, token, index);
case "''":
return new Token(TokenType.CharacterLiteral, "", index); // support empty Characters ('')
case "{":
return new Token(TokenType.StartBracket, token, index);
case "}":
return new Token(TokenType.EndBracket, token, index);
case "(":
case ")":
return new Token(TokenType.Parenthesis, token, index);
case Syntax.Conditionals.Else:
return new Token(TokenType.ElseConditional, token, index);
}
if (token[0] == '\"' && token[token.Length - 1] == '\"' && token.OccurrencesOf("\"") == 2)
return new Token(TokenType.StringLiteral, token.Substring(1, token.Length - 2).FromBase64().Replace("\\\"", "\""), index);
else if (token[0] == '\'' && token[token.Length - 1] == '\'') // we can't ensure the length here because the contents are encoded
return new Token(TokenType.CharacterLiteral, token.Substring(1, token.Length - 2).FromBase64(), index);
else if (token.IndexOf(Syntax.Conditionals.If + "(") == 0 && token[token.Length - 1] == ')')
return new Token(TokenType.IfConditional, token, index);
else if (token.IndexOf(Syntax.Conditionals.While + "(") == 0 && token[token.Length - 1] == ')')
return new Token(TokenType.WhileConditional, token, index);
else if (token.IndexOf(Syntax.Conditionals.Loop + "(") == 0 && token[token.Length - 1] == ')')
return new Token(TokenType.LoopConditional, token, index);
else if (Utils.IsNumber(token))
{
try
{
if (token.OccurrencesOf(".") == 1)
{
Convert.ToDecimal(token);
return new Token(TokenType.DecimalLiteral, token, index);
}
else
{
long number = Convert.ToInt64(token);
if (number <= int.MaxValue && number >= int.MinValue)
{
return new Token(TokenType.IntegerLiteral, token, index);
}
else
{
return new Token(TokenType.Integer64Literal, token, index);
}
}
}
catch (FormatException)
{
throw new BaseException("Number literal is in a invalid format");
}
catch (OverflowException)
{
throw new BaseException("Number literal is either too large or small to be parsed");
}
}
else if (FunctionChain.IsChain(token))
return new Token(TokenType.FunctionChain, token, index);
else if (BaseFunction.IsFunctionCall(token))
return new Token(TokenType.FunctionCall, token, index);
else if (FastStruct.IsFastStruct(token))
return new Token(TokenType.FastStruct, token, index);
else if (Utils.IsValidObjectName(token) || FastStruct.IsValidMemberAccessor(token))
return new Token(TokenType.VariableIdentifier, token, index);
}
if (@throw)
throw new BaseException("Unknown token: " + token);
return null;
}
private readonly static string[] statements = typeof(Syntax.Statements).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray();
private readonly static string[] identifiers = typeof(Syntax.Identifiers).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray();
private readonly static string[] operators = typeof(Syntax.Operators).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray();
private readonly static string[] comparators = typeof(Syntax.Comparators).GetFields(BindingFlags.Public | BindingFlags.Static).Where(x => x.IsLiteral && !x.IsInitOnly).Select(x => (string)x.GetValue(null)).ToArray();
private readonly static List<string> terms = Utils.ConcatArrays(statements, identifiers, operators, comparators).ToList();
public static List<Token> ParseLine(string line, bool @throw = true)
{
if (string.IsNullOrWhiteSpace(line))
return new List<Token>(); // don't parse empty line
List<Token> tokens = new List<Token>();
string currentIdentifier = "";
Token currentToken = new Token();
for (int i = 0; i < line.Length; i++)
{
if ((Utils.IsValidFunctionName(currentIdentifier) || (currentIdentifier.EndsWith(".") && Utils.IsValidObjectName(currentIdentifier.Substring(0, currentIdentifier.Length - 1)))) && line[i] == '(')
{
// this is the start of a function, we must handle this differently or it will be parsed into several different tokens
int closingParenthesis = Utils.GetClosingParenthesis(line.Substring(i));
if (closingParenthesis == -1)
throw new BaseException("Function is missing a closing parenthesis");
// skip to the closing parenthesis, it will be handled below
int closingParenthesisIndex = i + closingParenthesis;
currentIdentifier += line.Substring(i, closingParenthesisIndex - i);
i = closingParenthesisIndex;
}
if (Utils.IsValidObjectName(currentIdentifier) && line[i] == '<')
{
// this is the start of a FastStruct, we must handle this differently or it will be parsed into several different tokens
int closingBracket = Utils.GetClosingBracket(line.Substring(i));
if (closingBracket == -1)
throw new BaseException("FastStruct definition is missing a closing bracket");
// skip to the closing bracket, it will be handled below
int closingBracketIndex = i + closingBracket;
currentIdentifier += line.Substring(i, closingBracketIndex - i);
i = closingBracketIndex;
}
// this prevents from periods splitting up identifier when calling static type methods (EX: IntegerArray.Create(0))
if (Type.GetType(currentIdentifier, false) != null && line[i] == '.')
{
currentIdentifier += line[i];
continue;
}
if (currentIdentifier == "")
{
foreach (var term in terms)
{
if (line.Length > term.Length + i && line.Substring(i, term.Length) == term)
{
tokens.Add(ParseToken(term));
currentIdentifier = "";
currentToken = new Token();
i += term.Length;
}
}
}
if (line[i] == '\"' || line[i] == '\'')
{
// this is the start of a string/character literal, we must handle this differently or it will be parsed into several different tokens
int closing = line.Substring(i + 1).IndexOf(line[i]);
if (closing == -1)
{
if (line[i] == '\"')
throw new BaseException("String literal is missing a closing quote");
else
throw new BaseException("Character literal is missing a closing apostrophe");
}
else
{
// skip to the closing quote or apostrophe, it will be handled below
int closeIndex = i + closing + 1;
currentIdentifier += line.Substring(i, closeIndex - i);
i = closeIndex;
}
}
if (line[i] == ' ')
{
if (currentToken.type != TokenType.None)
{
tokens.Add(currentToken);
currentIdentifier = "";
currentToken = new Token();
}
}
else
{
currentIdentifier += line[i];
var newToken = ParseToken(currentIdentifier, false);
if (newToken == null)
{
// this letter invalidated the token, add the previous token. Reset the current token, and set the current identifier to the current character
tokens.Add(currentToken);
currentIdentifier = line[i].ToString();
currentToken = ParseToken(currentIdentifier, false);
if (currentToken == null)
{
// current character is not at all valid so we will ignore it
currentIdentifier = "";
currentToken = new Token();
}
}
else
{
currentToken = newToken;
}
}
}
if (currentToken.type != TokenType.None)
{
tokens.Add(currentToken);
}
for (int i = 0; i < tokens.Count; i++)
{
if (i + 1 < tokens.Count && tokens[i].type == TokenType.VariableIdentifier && tokens[i + 1].type == TokenType.VariableIdentifier)
{
// this token is a concated version of this token and the one after
var concated = new Token(TokenType.VariableIdentifier, tokens[i].identifier + "." + tokens[i + 1].identifier);
tokens.RemoveRange(i, 2);
tokens.Insert(i, concated);
}
}
// now we must link consecutive FunctionCall tokens, and VariableIdentifier and consecutive FunctionCall tokens together to form FunctionChains
int chainStart = -1;
for (int i = 0; i < tokens.Count; i++)
{
if (chainStart == -1)
{
if (tokens[i].type == TokenType.TypeIdentifier || tokens[i].type == TokenType.VariableIdentifier || tokens[i].type == TokenType.FunctionCall)
chainStart = i;
}
else if (tokens[i].type != TokenType.FunctionCall)
{
if (i - chainStart > 1)
{
// chain is longer than 1 token long
var chainTokens = tokens.GetRange(chainStart, i - chainStart);
// this prevents user defined functions from being put into a singular FunctionCall token (for example, this prevents two tokens: integer foo() from becoming integer.foo())
if (chainTokens.Count >= 2 && chainTokens[0].type == TokenType.TypeIdentifier && chainTokens[1].type == TokenType.FunctionCall && !Interpreter.Functions.FunctionExists(chainTokens[0].identifier + chainTokens[1].identifier))
break;
tokens.RemoveRange(chainStart, i - chainStart);
string identifier = "";
foreach (var chainToken in chainTokens)
{
identifier += chainToken.identifier + ".";
}
identifier = identifier.Remove(identifier.Length - 1);
if (chainTokens.Count == 2 && (chainTokens[0].type == TokenType.VariableIdentifier || chainTokens[0].type == TokenType.TypeIdentifier))
{
// it's just a method (EX: myVar.ToString())
tokens.Insert(chainStart, new Token(TokenType.FunctionCall, identifier, chainTokens[0].Index));
}
else
{
tokens.Insert(chainStart, new Token(TokenType.FunctionChain, identifier, chainTokens[0].Index));
}
i = chainStart;
}
chainStart = -1;
}
else if (i == tokens.Count - 1)
{
var chainTokens = tokens.GetRange(chainStart, i - chainStart + 1);
// this prevents user defined functions from being put into a singular FunctionCall token (for example, this prevents two tokens: integer foo() from becoming integer.foo())
if (chainTokens.Count >= 2 && chainTokens[0].type == TokenType.TypeIdentifier && chainTokens[1].type == TokenType.FunctionCall && !Interpreter.Functions.FunctionExists(chainTokens[0].identifier + chainTokens[1].identifier))
break;
tokens.RemoveRange(chainStart, i - chainStart + 1);
string identifier = "";
foreach (var chainToken in chainTokens)
{
identifier += chainToken.identifier + ".";
}
identifier = identifier.Remove(identifier.Length - 1);
if (chainTokens.Count == 2 && (chainTokens[0].type == TokenType.VariableIdentifier || chainTokens[0].type == TokenType.TypeIdentifier))
{
// it's just a method (EX: myVar.ToString() or myType.ToString())
tokens.Insert(chainStart, new Token(TokenType.FunctionCall, identifier, chainTokens[0].Index));
}
else
{
tokens.Insert(chainStart, new Token(TokenType.FunctionChain, identifier, chainTokens[0].Index));
}
break;
}
}
if (@throw && tokens.Count > 1)
{
// NO? We can't check if a type is valid or not because types can be created at runtime
if (tokens[0].type == TokenType.VariableIdentifier && tokens[1].type == TokenType.VariableIdentifier)
throw new BaseException("Unknown type: " + tokens[0].identifier);
if (tokens[0].type == TokenType.TypeIdentifier && tokens[1].type == TokenType.TypeIdentifier)
throw new BaseException("Cannot define a variable with the same name as an existing type");
}
if (@throw)
{
for (int i = 0; i < tokens.Count; i++)
{
if (tokens[i].type == TokenType.TypeIdentifier)
{
if (i + 1 < tokens.Count)
{
var nextToken = tokens[i + 1];
if (nextToken.type != TokenType.FunctionCall && nextToken.type != TokenType.FunctionCall && nextToken.type != TokenType.VariableIdentifier)
{
throw new BaseException("Unexpected token preceding a TypeIdentifier: '" + nextToken.type.ToString() + "'");
}
}
else
{
throw new BaseException("Expected a token preceding a TypeIdentifier");
}
}
}
}
return tokens;
}
public static string EncodeLiterals(string str)
{
// Encode the contents in between "s or 's to base64 so they don't interfere with anything
MatchCollection literalMatches = Regex.Matches(str, "([\"\'])(?:(?:\\\\\\1|.)*?)\\1"); // matches for text surrounded in "s or 's (non-empty) (keep in mind the matches include the "s or 's)
int indexOffset = 0;
foreach (Match match in literalMatches)
{
// text inside of the "s or 's
var inside = match.Value.Substring(1, match.Length - 2);
if (match.Value[0] == '\'' && match.Length > 3 && inside != "\\\"" && inside != "\\\'")
throw new BaseException("Character literal must have no more than one character");
if (match.Length > 2) // not just "s or 's
{
string base64 = inside.Replace("\\\"", "\"").Replace("\\'", "\'").ToBase64();
str = str.Remove(indexOffset + match.Index + 1, match.Length - 2); // remove contents of string
str = str.Insert(indexOffset + match.Index + 1, base64); // fill contents of string to base64 of old contents
indexOffset += base64.Length - (match.Length - 2);
}
}
return str;
}
public static List<Line> Parse(List<string> codeLines, int currentLineOffset = 0, bool isNestedInFunction = false, bool isNestedInConditional = false)
{
Stopwatch sw = new Stopwatch();
sw.Start();
List<Line> lines = new List<Line>();
for (int i = 0; i < codeLines.Count; i++)
{
Interpreter.CurrentLine = i + 1 + currentLineOffset;
while (codeLines[i].IndexOf(" ") == 0 || codeLines[i].IndexOf(" ") == 0)
codeLines[i] = codeLines[i].Remove(0, 1);
string safeLine = codeLines[i];
if (!isNestedInConditional && !isNestedInFunction) // literals are already encoded
safeLine = EncodeLiterals(safeLine);
while (safeLine.IndexOf(Syntax.Identifiers.Comment) != -1)
{
int pos = safeLine.IndexOf(Syntax.Identifiers.Comment);
safeLine = safeLine.Remove(pos, safeLine.Length - pos);
} // get rid of all comments and anything after it (but it cannot be enclosed in quotes or apostrophes)
if (string.IsNullOrWhiteSpace(safeLine))
{
lines.Add(new Line(new List<Token>()));
continue;
} // no need to parse or evaluate empty line
lines.Add(new Line(ParseLine(safeLine)));
Line currentLine = lines[lines.Count - 1];
if (!isNestedInFunction && Interpreter.Debug && currentLine.Count > 0)
{
Console.WriteLine("\n(" + Interpreter.CurrentFile + ") Line " + (i + 1) + ": ");
foreach (var token in currentLine)
Console.WriteLine(token.type.ToString() + " - " + token.identifier.Replace("\n", @"\n"));
}
// We must handle Define function usages and FastStruct definitions before execution to avoid bad lexing errors
if (Interpreter.Functions.FunctionExists("Define(") && currentLine.Count > 0 && currentLine[0].type == TokenType.FunctionCall && currentLine[0].identifier.IndexOf("Define(") == 0)
{
Interpreter.Functions["Define("].Invoke(currentLine[0].identifier);
lines[lines.Count - 1] = new Line(new List<Token>()); // line was already handled, clear line
}
else if (currentLine.Count == 1 && currentLine[0].type == TokenType.FastStruct)
{
var @struct = lines[i][0].identifier;
string name = @struct.Substring(0, @struct.IndexOf("<"));
@struct = @struct.Substring(@struct.IndexOf("<"));
Interpreter.Structs.Create(new FastStruct(name, FastStruct.ParseDefinitionParams(@struct)));
lines[lines.Count - 1] = new Line(new List<Token>()); // line was already handled, clear line
}
}
sw.Stop();
if (Interpreter.Debug)
Console.WriteLine("Parsing " + (Interpreter.CurrentFile != "" ? "'" + Interpreter.CurrentFile + "'" : codeLines.Count + " lines") + " took " + sw.ElapsedMilliseconds + " ms");
return lines;
}
}
}