-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlexer.ts
More file actions
183 lines (154 loc) · 4.36 KB
/
lexer.ts
File metadata and controls
183 lines (154 loc) · 4.36 KB
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
import { Token, TOKEN_TYPE, TokenType, lookupIdentifier } from "./token";
export class Lexer {
private char = "\0";
private pos = 0;
private readPos = 0;
constructor(private readonly source: string) {
this.readChar();
}
nextToken() {
let token: Token;
this.skipWhitespace();
this.skipComment();
switch (this.char) {
case "-":
if (this.peekChar() === "-") {
const literal = this.char + this.peekChar();
this.readChar();
token = this.newToken(TOKEN_TYPE.Edge, literal);
} else if (this.peekChar() === ">") {
const literal = this.char + this.peekChar();
this.readChar();
token = this.newToken(TOKEN_TYPE.DirectedEdge, literal);
} else {
token = this.newToken(TOKEN_TYPE.Illegal, this.char + this.peekChar());
}
break;
case '"':
const string = this.readString();
if (string === null) {
token = this.newToken(TOKEN_TYPE.Illegal, this.char);
} else {
token = this.newToken(TOKEN_TYPE.String, string);
}
break;
case "=":
token = this.newToken(TOKEN_TYPE.Eq, this.char);
break;
case "{":
token = this.newToken(TOKEN_TYPE.LBrace, this.char);
break;
case "}":
token = this.newToken(TOKEN_TYPE.RBrace, this.char);
break;
case "[":
token = this.newToken(TOKEN_TYPE.LBracket, this.char);
break;
case "]":
token = this.newToken(TOKEN_TYPE.RBracket, this.char);
break;
case ";":
token = this.newToken(TOKEN_TYPE.Semicolon, this.char);
break;
case "\0":
token = this.newToken(TOKEN_TYPE.EOF, "<eof>");
break;
default:
if (this.isLetter(this.char)) {
const literal = this.readIdentifier();
return new Token(lookupIdentifier(literal), literal);
}
if (this.isNumber(this.char)) {
return new Token(TOKEN_TYPE.Number, this.readNumber());
}
token = this.newToken(TOKEN_TYPE.Illegal, this.char);
}
this.readChar();
return token;
}
private readString(): string | null {
let output = "";
this.readChar(); // Skip the initial quote
let escape = false;
while (this.char != '"' || escape) {
if (this.char == "\n") return null; // Early EoL
if (escape) {
output += this.char;
escape = false;
} else {
if (this.char == "\\") {
escape = true;
} else {
output += this.char;
}
}
this.readChar();
}
return output;
}
private readNumber(): string {
const position = this.pos;
while (this.isNumber(this.char)) {
this.readChar();
}
if (this.char === ".") {
this.readChar();
while (this.isNumber(this.char)) {
this.readChar();
}
}
return this.source.slice(position, this.pos);
}
private readIdentifier(): string {
const position = this.pos;
while (this.isAlphaNumeric(this.char)) {
this.readChar();
}
return this.source.slice(position, this.pos);
}
private skipComment() {
while (this.char === "#") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
while (this.char !== "\n" && this.char !== "\0") {
this.readChar();
}
this.skipWhitespace();
}
}
private skipWhitespace() {
while ([" ", "\t", "\n"].includes(this.char)) {
this.readChar();
}
}
private isNumber(char: string): boolean {
return "0" <= char && char <= "9";
}
private isLetter(char: string): boolean {
return ("a" <= char && char <= "z") || ("A" <= char && char <= "Z") || char === "_";
}
private isAlphaNumeric(char: string): boolean {
return this.isNumber(char) || this.isLetter(char);
}
private peekChar() {
if (this.readPos < this.source.length) {
return this.source[this.readPos];
}
return "\0";
}
private readChar() {
if (this.readPos >= this.source.length) {
this.char = "\0";
} else {
this.char = this.source[this.readPos];
}
this.pos = this.readPos;
this.readPos += 1;
}
private newToken(tokenType: TokenType, char: string): Token {
return new Token(tokenType, char);
}
private isAtEnd(): boolean {
return this.readPos >= this.source.length;
}
}