-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.cs
53 lines (45 loc) · 1.5 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
namespace ClassLibrary;
using System.Text.RegularExpressions;
public class Lexer
{
public static List<Token> Tokenize(string source, List<Error> compilingErrors)
{
var tokens = new List<Token>();
int pos = 0;
int line = 1, column = 1;
char CurrentChar() => source[pos];
while (pos < source.Length)
{
if (char.IsWhiteSpace(CurrentChar()))
{
pos++; column++;
continue;
}
if (CurrentChar() == '\n')
{
pos++; line++; column = 1;
continue;
}
foreach (var pattern in Token.Patterns)
{
var match = Regex.Match(source.Substring(pos), pattern.Value);
if (match.Success)
{
tokens.Add(new Token(pattern.Key, match.Value, line, column));
pos += match.Length;
column += match.Length;
break;
}
}
}
tokens.Add(new Token(TokenType.EOF, null, line, column));
// Lexical Check
foreach (var invalidToken in tokens.Where(token => token.Type == TokenType.InvalidToken))
{
var error = new Error(invalidToken.Line,invalidToken.Col,ErrorType.LexicalError,
$"Unknown Token '{invalidToken.Lexeme}'");
compilingErrors.Add(error);
}
return tokens;
}
}