-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
359 lines (326 loc) · 12.6 KB
/
lexer.py
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
from enum import Enum
import re
from typing import Pattern, AnyStr, List, Optional, Tuple, Union
class TokenType(Enum):
IDENTIFIER = "IDENTIFIER"
FUN = "FUN"
RETURN = "RETURN"
STRUCT = "STRUCT"
CLASS = "CLASS"
# types
INT = "INT"
DOUBLE = "DOUBLE"
STRING = "STRING"
BOOLEAN = "BOOLEAN"
LIST = "LIST"
# literals
TRUE = "TRUE"
FALSE = "FALSE"
ASSIGNMENT = "ASSIGNMENT"
# keywords (loops, conditions)
FOR = "FOR"
IN = "IN"
WHILE = "WHILE"
IF = "IF"
ELIF = "ELIF"
ELSE = "ELSE"
MODULO = "MODULO"
# I/O
WRITE = "WRITE"
READ = "READ"
# arithmetic operators
PLUS = "PLUS"
MINUS = "MINUS"
MUL = "MUL"
DIV = "DIV"
# separators
LEFT_BRACKET = "LEFT_BRACKET"
RIGHT_BRACKET = "RIGHT_BRACKET"
LEFT_CURLY_BRACKET = "LEFT_CURLY_BRACKET"
RIGHT_CURLY_BRACKET = "RIGHT_CURLY_BRACKET"
LEFT_CORNERED_BRACKET = "LEFT_CORNERED_BRACKET"
RIGHT_CORNERED_BRACKET = "RIGHT_CORNERED_BRACKET"
SEMICOLON = "SEMICOLON"
COMMA = "COMMA"
LINE_BREAK = "LINE_BREAK"
# arithmetic comparators (only possible on number types)
GREATER_EQUALS = "GREATER_EQUALS"
LESSER_EQUALS = "LESSER_EQUALS"
GREATER = "GREATER"
LESSER = "LESSER"
# comparators possible on other objects
EQUALS = "EQUALS"
NOT_EQUALS = "NOT_EQUALS"
# logical operators
AND = "AND"
OR = "OR"
NOT = "NOT"
# error token
ERROR = "ERROR"
EOF = "EOF"
class Token:
def __init__(self, _type: TokenType, value: Optional[Union[str, int, float]] = None):
self.type = _type
self.value = value
def __repr__(self):
if self.value is None:
return f"Token({self.type})"
elif isinstance(self.value, str):
return f"Token({self.type}, \"{self.value}\")"
return f"Token({self.type}, {self.value})"
def __eq__(self, other):
return self.type == other.type and self.value == other.value
class LocationInformation:
def __init__(self, start_line: int, end_line: int, start_col: int, end_col: int):
self.start_line = start_line
self.end_line = end_line
self.start_col = start_col
self.end_col = end_col
def __repr__(self):
return f"LocationInformation(start_line={self.start_line}, end_line={self.end_line}, start_col={self.start_col}, end_col={self.end_col})"
def __eq__(self, other):
return self.start_line == other.start_line and self.end_line == other.end_line and \
self.start_col == other.start_col and self.end_col == other.end_col
class TokenObject:
def __init__(self, token: Token, location_info: LocationInformation):
self.token = token
self.location_info = location_info
def __repr__(self):
return f"TokenObject({self.token}, {self.location_info})"
def __eq__(self, other):
return self.token == other.token and self.location_info == other.location_info
def get_token(self):
return self.token
# max variable name length is 51
ALLOWED_VARIABLE_CHARS_REGEX: Pattern[AnyStr] = re.compile("^([A-Z]|[a-z])([A-Z]|[a-z]|[0-9]){0,50}$")
LITERAL_REGEX = re.compile("^([0-9][0-9]*)$|^([1-9][0-9]*).([0-9]*)$|^(\"[\w]\")$|false|true")
INT_REGEX = re.compile("^([0-9][0-9]*)$")
DOUBLE_REGEX = re.compile("^([1-9][0-9]*)(.)([0-9]*)$")
def is_allowed_identifier(identifier: str) -> bool:
return bool(re.match(ALLOWED_VARIABLE_CHARS_REGEX, identifier))
def next_word_is(text: str, word: str):
if text[0:len(word)] == word:
return True
return False
class Lexer:
def __init__(self, code: str):
self.code = code
self.tokens: List[TokenObject] = []
self.index = 0
self.line = 1
self.column = 1
def run_lexer(self):
while self.index < len(self.code):
self.go_forward()
self.tokens.append(TokenObject(Token(TokenType.EOF), LocationInformation(0, 0, 0, 0)))
def get_token_objects(self) -> List[TokenObject]:
return self.tokens
def get_tokens(self) -> List[Token]:
return [token_obj.get_token() for token_obj in self.tokens]
def advance(self, increment_value: int = 1, column_length: Optional[int] = None):
self.index += increment_value
if column_length is None:
self.column += increment_value
else:
self.column = column_length
def get_current_location(self) -> Tuple[int, int]:
line_number = len(self.code[0:self.index].split("\n"))
col_number = len(self.code[0:self.index].split("\n")[-1])
return line_number, col_number
def go_forward(self):
if self.index == len(self.code):
# should be a } at the end
return
current_char = self.code[self.index]
next_char = self.code[self.index + 1] if self.index + 1 < len(self.code) - 1 else ""
start_line, start_col = self.get_current_location()
skip_length = 1
token = None
if current_char in [" ", "\t"]:
self.column += 1
elif current_char == "\n":
self.line += 1
self.column = 1
elif current_char == ">":
if next_char == "=":
token = Token(TokenType.GREATER_EQUALS)
skip_length = 2
else:
token = Token(TokenType.GREATER)
elif current_char == "<":
if next_char == "=":
token = Token(TokenType.LESSER_EQUALS)
skip_length = 2
else:
token = Token(TokenType.LESSER)
elif current_char == "=":
if next_char == "=":
token = Token(TokenType.EQUALS)
skip_length = 2
else:
token = Token(TokenType.ASSIGNMENT)
elif current_char == "&":
if next_char == "&":
token = Token(TokenType.AND)
skip_length = 2
else:
token = Token(TokenType.ERROR, "Expected a '&' after a '&'")
elif current_char == "|":
if next_char == "|":
token = Token(TokenType.OR)
skip_length = 2
else:
token = Token(TokenType.ERROR, "Expected a '|' after a '|'")
elif current_char == "!":
if next_char == "=":
token = Token(TokenType.NOT_EQUALS)
skip_length = 2
else:
token = Token(TokenType.NOT)
elif current_char == ";":
token=Token(TokenType.SEMICOLON)
elif current_char == "-":
token = Token(TokenType.MINUS)
elif current_char == "+":
token = Token(TokenType.PLUS)
elif current_char == "*":
token = Token(TokenType.MUL)
elif current_char == "/":
token = Token(TokenType.DIV)
elif current_char == "(":
token = Token(TokenType.LEFT_BRACKET)
elif current_char == ")":
token = Token(TokenType.RIGHT_BRACKET)
elif current_char == "{":
token = Token(TokenType.LEFT_CURLY_BRACKET)
elif current_char == "}":
token = Token(TokenType.RIGHT_CURLY_BRACKET)
elif current_char == "[":
token = Token(TokenType.LEFT_CORNERED_BRACKET)
elif current_char == "]":
token = Token(TokenType.RIGHT_CORNERED_BRACKET)
elif current_char == ";":
token = Token(TokenType.SEMICOLON)
elif current_char == ",":
token = Token(TokenType.COMMA)
elif current_char == "\"":
string, error = self.get_string_from_text(text=self.code[self.index:])
column_length = self.column
if error is None:
skip_length = len(string) + 2
token = Token(TokenType.STRING, string)
else:
token = Token(TokenType.ERROR, error)
skip_length = len(string) + 1
elif (identifier_param := self.get_full_identifier(text=self.code[self.index:]))[0] != "":
full_word, length, error = identifier_param
skip_length = length
column_length = self.column
if error is not None:
token = Token(TokenType.ERROR, error)
elif full_word == "int":
token = Token(TokenType.INT)
elif full_word == "double":
token = Token(TokenType.DOUBLE)
elif full_word == "str":
token = Token(TokenType.STRING)
elif full_word == "List":
token = Token(TokenType.LIST)
elif full_word == "bool":
token = Token(TokenType.BOOLEAN)
elif full_word == "for":
token = Token(TokenType.FOR)
elif full_word == "in":
token = Token(TokenType.IN)
elif full_word == "while":
token = Token(TokenType.WHILE)
elif full_word == "if":
token = Token(TokenType.IF)
elif full_word == "elif":
token = Token(TokenType.ELIF)
elif full_word == "else":
token = Token(TokenType.ELSE)
elif full_word == "write":
token = Token(TokenType.WRITE)
elif full_word == "fun":
token = Token(TokenType.FUN)
elif full_word == "struct":
token = Token(TokenType.STRUCT)
elif full_word == "class":
token = Token(TokenType.CLASS)
elif full_word == "return":
token = Token(TokenType.RETURN)
elif re.match(LITERAL_REGEX, full_word):
if full_word == "true":
token = Token(TokenType.TRUE)
elif full_word == "false":
token = Token(TokenType.FALSE)
elif re.match(INT_REGEX, full_word):
token = Token(TokenType.INT, int(full_word))
elif re.match(DOUBLE_REGEX, full_word):
token = Token(TokenType.DOUBLE, float(full_word))
elif re.match(ALLOWED_VARIABLE_CHARS_REGEX, full_word):
token = Token(TokenType.IDENTIFIER, full_word)
self.advance(skip_length)
end_line, end_col = self.get_current_location()
location_info = LocationInformation(
start_line=start_line,
end_line=end_line,
start_col=start_col,
end_col=end_col
)
if token is not None:
self.tokens.append(TokenObject(token=token, location_info=location_info))
def get_string_from_text(self, text: str) -> Tuple[str, Optional[str]]:
"""
This function gets called when a " character gets spotted
:param text:
:return:
"""
if text[0] != "\"":
return "", "Something went horribly wrong when parsing a string."
string = ""
for i in range(1, len(text)):
if text[i] == "\"":
return string, None
else:
string += text[i]
if text[i] == "\n":
self.line += 1
self.column = 1
else:
self.column += 1
return string, "String never ended."
def get_full_identifier(self, text: str) -> Tuple[str, int, Optional[str]]:
"""
This function gets the next full viable word in the text
:param text: The trimmed down text
:return: the identifier, length of the identifier, optional error message
"""
identifier = ""
word_regex = re.compile("([A-Z]|[a-z]|[0-9])")
digit_regex = re.compile("([0-9])")
# this means that we look for a number
if re.match(digit_regex, text[0]):
after_point = False
for i in range(len(text)):
if re.match(digit_regex, text[i]):
identifier += text[i]
elif text[i] == "." and after_point:
return identifier, len(identifier) + 1, "Expected only one . for a number."
elif text[i] == ".":
after_point = True
identifier += "."
else:
if text[i - 1] == ".":
return identifier, len(
identifier) + 1, f"No lonely point allowed for double. Write {identifier}0 instead"
return identifier, len(identifier), None
return identifier, len(identifier), None
else:
for i in range(0, len(text)):
if re.match(word_regex, text[i]):
identifier += text[i]
else:
break
return identifier, len(identifier), None