-
Notifications
You must be signed in to change notification settings - Fork 1
/
go_lexer.py
452 lines (351 loc) · 8.38 KB
/
go_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
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
import sys
import colorama
from colorama import Fore, Style
from ply import lex
from symbol_table import SymbolTable, TypeTable
import utils
from utils import print_line, print_marker, print_lexer_error
colorama.init()
with open(sys.argv[1], "r") as f:
input_code = f.read()
if input_code[len(input_code) - 1] != "\n":
input_code += "\n"
# Find column number of token
def find_column(token):
line_start = input_code.rfind("\n", 0, token.lexpos) + 1
return (token.lexpos - line_start) + 1
# Lexing states
states = (("InsertSemi", "exclusive"),)
# List of literal tokens
literals = ";,.=+-*/%()[]!{}"
# List of token names. This is always required
tokens = (
"CARET",
"RIGHT_SHIFT",
"LEFT_SHIFT",
"CARET_EQ",
"BAR_EQ",
"AMP_EQ",
"AMP_CARET_EQ",
"RIGHT_SHIFT_EQ",
"LEFT_SHIFT_EQ",
"AMPERSAND",
"BAR",
"AMPER_AMPER",
"BAR_BAR",
# "EXCLAMATION",
"COLON",
# assignment operators
"WALRUS",
"ADD_EQ",
"SUB_EQ",
"MUL_EQ",
"DIV_EQ",
"MOD_EQ",
# arithmetic operators
"INCREMENT",
"DECREMENT",
# relational operators
"EQ_EQ",
"NOT_EQ",
"LT",
"LT_EQ",
"GT",
"GT_EQ",
# literals
"INT_LIT",
"FLOAT_LIT",
"STRING_LIT",
"BOOL_LIT",
"IDENTIFIER",
"ELLIPSIS",
)
# List of keywords
keywords = {
"break": "KW_BREAK",
"default": "KW_DEFAULT",
"func": "KW_FUNC",
"interface": "KW_INTERFACE",
"select": "KW_SELECT",
"case": "KW_CASE",
"defer": "KW_DEFER",
"go": "KW_GO",
"map": "KW_MAP",
"struct": "KW_STRUCT",
"chan": "KW_CHAN",
"else": "KW_ELSE",
"goto": "KW_GOTO",
"package": "KW_PACKAGE",
"switch": "KW_SWITCH",
"const": "KW_CONST",
"fallthrough": "KW_FALLTHROUGH",
"if": "KW_IF",
"range": "KW_RANGE",
"type": "KW_TYPE",
"continue": "KW_CONTINUE",
"for": "KW_FOR",
"import": "KW_IMPORT",
"return": "KW_RETURN",
"var": "KW_VAR",
}
# List of types
# types -> (symbol, storage in bytes)
types = {
#For INT
"int": ("INT", 8),
"int8": ("INT8", 1),
"int16": ("INT16", 2),
"int32": ("INT32", 4),
"int64": ("INT64", 8),
#For Float
"float32": ("FLOAT32", 4),
"float64": ("FLOAT64", 8),
#For UINT
"uint": ("UINT", 8),
"uint8": ("UINT8", 1),
"uint16": ("UINT16", 2),
"uint32": ("UINT32", 4),
"uint64": ("UINT64", 8),
#For Complex
"complex64": ("COMPLEX64", 8),
"complex128": ("COMPLEX128", 16),
#For Misc
"string": ("STRING", None),
"byte": ("BYTE", 1),
"bool": ("BOOL", 1),
"rune": ("RUNE", 4)
}
# updating list of tokens with keywords and types
tokens = tokens + tuple(keywords.values()) + tuple(i[0] for i in types.values())
unused_tokens = {
"AMPERSAND",
"AMP_CARET_EQ",
"AMP_EQ",
"BAR",
"BAR_EQ",
"CARET",
"CARET_EQ",
"COLON",
"KW_CASE",
"KW_CHAN",
"KW_DEFAULT",
"KW_DEFER",
"KW_FALLTHROUGH",
"KW_GO",
"KW_GOTO",
"KW_INTERFACE",
"KW_MAP",
"KW_SELECT",
"KW_STRUCT",
"KW_SWITCH",
"LEFT_SHIFT",
"LEFT_SHIFT_EQ",
"RIGHT_SHIFT",
"RIGHT_SHIFT_EQ",
}
required_tokens_for_parser = list(set(tokens) - unused_tokens)
# print(required_tokens_for_parser)
# tokens to ignore in ANY state
def t_ANY_ignore_SPACES(t):
r"\ +"
def t_ANY_ignore_TABS(t):
r"\t+"
def t_ANY_ignore_SINGLE_COMMENT(t):
r"//.*"
def t_ANY_ignore_MULTI_COMMENT(t):
r"/\*(.|\n)*?\*/"
t.lexer.lineno += t.value.count("\n")
# tokens with no actions
t_CARET = r"\^"
t_RIGHT_SHIFT = r">>"
t_LEFT_SHIFT = r"<<"
t_CARET_EQ = r"\^="
t_BAR_EQ = r"\|="
t_AMP_EQ = r"&="
t_AMP_CARET_EQ = r"&\^="
t_RIGHT_SHIFT_EQ = r">>="
t_LEFT_SHIFT_EQ = r"<<="
t_AMPERSAND = r"&"
t_BAR = r"\|"
t_AMPER_AMPER = r"&&"
t_BAR_BAR = r"\|\|"
# t_EXCLAMATION = r"!"
t_COLON = r":"
t_WALRUS = r":="
# TODO: Move the assignment operators above, below
# assignment operators
t_ADD_EQ = r'\+='
t_SUB_EQ = r'-='
t_MUL_EQ = r'\*='
t_DIV_EQ = r'/='
t_MOD_EQ = r'%='
# relational operators
t_EQ_EQ = r"=="
t_NOT_EQ = r"!="
t_LT = r"<"
t_LT_EQ = r"<="
t_GT = r">"
t_GT_EQ = r">="
# INT type
t_INT = r"int"
t_INT8 = r"int8"
t_INT16 = r"int16"
t_INT32 = r"int32"
t_INT64 = r"int64"
#FLOAT Type
t_FLOAT32 = r"float32"
t_FLOAT64 = r"float64"
#UNIT Type
t_UINT = r"uint"
t_UINT8 = r"uint8"
t_UINT16 = r"uint16"
t_UINT32 = r"uint32"
t_UINT64 = r"uint64"
#Complex Type
t_COMPLEX64 = r"complex64"
t_COMPLEX128 = r"complex128"
#MISC Type
t_BOOL = r"bool"
t_RUNE = r"rune"
t_BYTE = r"byte"
t_ELLIPSIS = r"\.\.\."
# tokens with actions
# tokens in ANY state
def t_ANY_UNCLOSED_MULTI_COMMENT(t):
r"/\*(.|\n)*"
print_lexer_error("Unclosed Multiline comment")
col = find_column(t)
print(f"at line {t.lineno}, column {col}")
print(
f"{Fore.GREEN}{t.lineno:>10}:\t{Style.RESET_ALL}",
lines[t.lineno - 1],
sep="",
)
print_marker(col - 1, 1)
# token in InsertSemi state
def t_InsertSemi_NEWLINE(t):
r"\n"
t.lexer.lineno += 1 # track line numbers
t.lexer.begin("INITIAL")
semi_tok = lex.LexToken()
semi_tok.type = ";"
semi_tok.value = ";"
semi_tok.lineno = t.lexer.lineno
semi_tok.lexpos = t.lexer.lexpos
return semi_tok
def t_InsertSemi_others(t):
r"."
t.lexer.lexpos -= 1
t.lexer.begin("INITIAL")
# tokens in INITIAL state
# newline characters
def t_NEWLINE(t):
r"\n+"
t.lexer.lineno += len(t.value) # track line numbers
# closing parantheses
def t_round_end(t):
r"\)"
t.lexer.begin("InsertSemi")
t.type = ")"
return t
def t_sq_end(t):
r"\]"
t.lexer.begin("InsertSemi")
t.type = "]"
return t
def t_curl_end(t):
r"\}"
t.lexer.begin("InsertSemi")
t.type = "}"
return t
# increment/decrement operators
def t_INCREMENT(t):
r"\+\+"
t.lexer.begin("InsertSemi")
return t
def t_DECREMENT(t):
r"--"
t.lexer.begin("InsertSemi")
return t
# literals
def t_STRING_LIT(t):
r"\"[^\"]*\""
# if r"\s*\*/":
# print_error("ERROR: Wrong Multiline Comment")
# return
if "\n" in t.value:
print_lexer_error("string cannot contain line breaks")
lineno = t.lexer.lineno
pos = find_column(t)
splits = list(t.value.split("\n"))
for i, line_ in enumerate(splits):
print_line(lineno)
line_actual = lines[lineno - 1]
if i == 0:
print_marker(pos - 1, len(line_actual) - pos + 1)
elif i == len(splits) - 1:
print_marker(0, line_actual.find('"') + 1)
else:
print_marker(0, len(line_actual))
lineno += 1
t.lexer.lineno += t.value.count("\n")
return
t.value = ("string", t.value)
t.lexer.begin("InsertSemi")
return t
def t_FLOAT_LIT(t):
r"[+-]?(\d+[.]\d*[eE][+-]?\d+)|[+-]?(\d+([.]\d*)|[+-]?\d+([eE][+-]?\d+)|[.]\d+([eE][+-]?\d+)?)"
t.value = ("float64", float(t.value))
t.lexer.begin("InsertSemi")
return t
def t_INT_LIT(t):
r"\d+"
t.value = ("int", int(t.value))
t.lexer.begin("InsertSemi")
return t
def t_BOOL_LIT(t):
r"(true|false)"
t.value = ("bool", t.value)
t.lexer.begin("InsertSemi")
return t
# identifier
def t_IDENTIFIER(t):
r"([a-zA-Z]([a-zA-Z0-9_])*)|_"
# There is no limit on length of identifier in go
# if len(t.value) > 31:
# # TODO print error in a better way
# print_lexer_error("Identifiers must be shorter than 32 characters")
# print_line(t.lexer.lineno)
if t.value in keywords:
t.type = keywords[t.value]
elif t.value in types:
t.type = types[t.value][0]
else:
t.type = "IDENTIFIER"
t.value = ("identifier", t.value, find_column(t))
t.lexer.begin('InsertSemi')
return t
# Error handling rule for ANY state
def t_ANY_error(t):
print_lexer_error(f"Illegal character {t.value[0]}")
col = find_column(t)
print(f"at line {t.lineno}, column {col}")
print(
f"{Fore.GREEN}{t.lineno:>10}:\t{Style.RESET_ALL}",
lines[t.lineno - 1],
sep="",
)
print_marker(col - 1, 1)
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# Give input to the lexer
lexer.input(input_code)
lines = input_code.split("\n")
utils.lines = lines
type_table = TypeTable()
symtab = SymbolTable(type_table)
if __name__ == "__main__":
# Tokenize
for tok in lexer:
print(tok)