-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.py
370 lines (312 loc) · 10.5 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
360
361
362
363
364
365
366
367
368
369
370
# Lexical Analisys
# Authors: Joel David Hernandez Cruz
# Juan Jose Roque Cires
# Julio Cesar Rodriguez Sanchez
# ------------------------------------------------------------------------------------------------------
# Reference ply (Python Lex-Yacc) Documentation
#
# Reference links
# https://www.dabeaz.com/ply/ply.html#ply_nn13
# http://www.dalkescientific.com/writings/NBN/parsing_with_ply.html
# https://www.dabeaz.com/ply/PLYTalk.pdf
#
#
# ------------------------------------------------------------------------------------------------------
import ply.lex as lex
from ply.lex import TOKEN
class CoolLexer(object):
"""
CoolLexer class.
"""
def __init__(self,
build_lexer=True,
debug=False,
lextab="lextab",
optimize=True,
outputdir="",
debuglog=None,
errorlog=None):
self.lexer = None # ply lexer instance
self.tokens = () # ply tokens collection
self.reserved = {} # ply reserved keywords map
self.last_token = None # last returned token
# Save Flags - PRIVATE PROPERTIES
self._debug = debug
self._lextab = lextab
self._optimize = optimize
self._outputdir = outputdir
self._debuglog = debuglog
self._errorlog = errorlog
# Build lexer if build_lexer flag is set to True
if build_lexer is True:
self.build(debug=debug, lextab=lextab, optimize=optimize, outputdir=outputdir, debuglog=debuglog,
errorlog=errorlog)
# ################################# READONLY #####################################
@property
def tokens_collection(self):
"""
Collection of COOL Syntax Tokens.
:return: Tuple.
"""
return (
# Identifiers
"ID", "TYPE",
# Primitive Types
"INTEGER", "STRING", "BOOLEAN",
# Literals
"LPAREN", "RPAREN", "LBRACE", "RBRACE", "COLON", "COMMA", "DOT", "SEMICOLON", "AT",
# Operators
"PLUS", "MINUS", "MULTIPLY", "DIVIDE", "EQ", "LT", "LTEQ", "ASSIGN", "INT_COMP",
# Special Operators
"ARROW"
)
@property
def basic_reserved(self):
"""
Map of Basic-COOL reserved keywords.
:return: dict.
"""
return {
"case": "CASE",
"class": "CLASS",
"else": "ELSE",
"esac": "ESAC",
"fi": "FI",
"if": "IF",
"in": "IN",
"inherits": "INHERITS",
"isvoid": "ISVOID",
"let": "LET",
"loop": "LOOP",
"new": "NEW",
"of": "OF",
"not": "NOT",
"pool": "POOL",
"self": "SELF",
"then": "THEN",
"while": "WHILE"
}
@property
def builtin_types(self):
"""
A map of the built-in types.
:return dict
"""
return {
"Bool": "BOOL_TYPE",
"Int": "INT_TYPE",
"IO": "IO_TYPE",
"Main": "MAIN_TYPE",
"Object": "OBJECT_TYPE",
"String": "STRING_TYPE",
"SELF_TYPE": "SELF_TYPE"
}
# ################################ PRIVATE #######################################
# ################# START OF LEXICAL ANALYSIS RULES DECLARATION ####################
# Ignore rule for single line comments
t_ignore_SINGLE_LINE_COMMENT = r"\-\-[^\n]*"
###
# SIMPLE TOKENS
t_LPAREN = r'\(' # (
t_RPAREN = r'\)' # )
t_LBRACE = r'\{' # {
t_RBRACE = r'\}' # }
t_COLON = r'\:' # :
t_COMMA = r'\,' # ,
t_DOT = r'\.' # .
t_SEMICOLON = r'\;' # ;
t_AT = r'\@' # @
t_MULTIPLY = r'\*' # *
t_DIVIDE = r'\/' # /
t_PLUS = r'\+' # +
t_MINUS = r'\-' # -
t_INT_COMP = r'~' # ~
t_LT = r'\<' # <
t_EQ = r'\=' # =
t_LTEQ = r'\<\=' # <=
t_ASSIGN = r'\<\-' # <-
t_ARROW = r'\=\>' # =>
@TOKEN(r"(true|false)")
def t_BOOLEAN(self, t):
t.value = True if t.value == "true" else False
return t
@TOKEN(r"\d+")
def t_INTEGER(self, t):
t.value = int(t.value)
return t
@TOKEN(r"[A-Z][a-zA-Z_0-9]*")
def t_TYPE(self, t):
t.type = self.basic_reserved.get(t.value, 'TYPE')
return t
@TOKEN(r"[a-z_][a-zA-Z_0-9]*")
def t_ID(self, t):
# Check for reserved words
t.type = self.basic_reserved.get(t.value, 'ID')
return t
@TOKEN(r"\n+")
def t_newline(self, t):
t.lexer.lineno += len(t.value)
def t_error(self, t):
"""
Error Handling and Reporting Rule.
"""
# print("Illegal character! Line: {0}, character: {1}".format(t.lineno, t.value[0]))
print("Illegal character! Line: {0}, character: {1}".format(t.lineno, t.value[0]))
t.lexer.skip(1)
# Ignore Whitespace Character Rule
t_ignore = ' \t\r\f'
# ################# LEXER STATES ######################################
@property
def states(self):
return (
("STRING", "exclusive"),
("COMMENT", "exclusive")
)
###
# THE STRING STATE
@TOKEN(r"\"")
def t_start_string(self, t):
t.lexer.begin("STRING")
t.lexer.string_backslashed = False
t.lexer.stringbuf = ""
@TOKEN(r"\n")
def t_STRING_newline(self, t):
t.lexer.lineno += 1
if not t.lexer.string_backslashed:
print("String newline not escaped")
t.lexer.skip(1)
else:
t.lexer.string_backslashed = False
@TOKEN(r"\"")
def t_STRING_end(self, t):
if not t.lexer.string_backslashed:
t.lexer.begin("INITIAL")
t.value = t.lexer.stringbuf
t.type = "STRING"
return t
else:
t.lexer.stringbuf += '"'
t.lexer.string_backslashed = False
@TOKEN(r"[^\n]")
def t_STRING_anything(self, t):
if t.lexer.string_backslashed:
if t.value == 'b':
t.lexer.stringbuf += '\b'
elif t.value == 't':
t.lexer.stringbuf += '\t'
elif t.value == 'n':
t.lexer.stringbuf += '\n'
elif t.value == 'f':
t.lexer.stringbuf += '\f'
elif t.value == '\\':
t.lexer.stringbuf += '\\'
else:
t.lexer.stringbuf += t.value
t.lexer.string_backslashed = False
else:
if t.value != '\\':
t.lexer.stringbuf += t.value
else:
t.lexer.string_backslashed = True
# STRING ignored characters
t_STRING_ignore = ''
# STRING error handler
def t_STRING_error(self, t):
print("Illegal character! Line: {0}, character: {1}".format(t.lineno, t.value[0]))
t.lexer.skip(1)
###
# THE COMMENT STATE
@TOKEN(r"\(\*")
def t_start_comment(self, t):
t.lexer.begin("COMMENT")
t.lexer.comment_count = 0
@TOKEN(r"\(\*")
def t_COMMENT_startanother(self, t):
t.lexer.comment_count += 1
@TOKEN(r"\*\)")
def t_COMMENT_end(self, t):
if t.lexer.comment_count == 0:
t.lexer.begin("INITIAL")
else:
t.lexer.comment_count -= 1
# COMMENT ignored characters
t_COMMENT_ignore = ''
# COMMENT error handler
def t_COMMENT_error(self, t):
t.lexer.skip(1)
# ################# END OF LEXICAL ANALYSIS RULES DECLARATION ######################
def build(self, **kwargs):
"""
Builds the CoolLexer instance
:param:
:return: None
"""
# Parse the parameters
if kwargs is None or len(kwargs) == 0:
debug, lextab, optimize, outputdir, debuglog, errorlog = \
self._debug, self._lextab, self._optimize, self._outputdir, self._debuglog, self._errorlog
else:
debug = kwargs.get("debug", self._debug)
lextab = kwargs.get("lextab", self._lextab)
optimize = kwargs.get("optimize", self._optimize)
outputdir = kwargs.get("outputdir", self._outputdir)
debuglog = kwargs.get("debuglog", self._debuglog)
errorlog = kwargs.get("errorlog", self._errorlog)
self.reserved = self.basic_reserved.keys()
self.tokens = self.tokens_collection + tuple(self.basic_reserved.values())
# Build internal ply.lex instance
self.lexer = lex.lex(module=self, lextab=lextab, debug=debug, optimize=optimize, outputdir=outputdir,
debuglog=debuglog, errorlog=errorlog)
def input(self, cool_program_source_code: str):
if self.lexer is None:
raise Exception("Lexer was not built. Try calling the build() method first, and then tokenize().")
self.lexer.input(cool_program_source_code)
def token(self):
if self.lexer is None:
raise Exception("Lexer was not built. Try building the lexer with the build() method.")
self.last_token = self.lexer.token()
return self.last_token
def clone_ply_lexer(self):
a_clone = self.lexer.clone()
return a_clone
@staticmethod
def test(program_source_code: str):
temp_lexer = CoolLexer()
temp_lexer.input(program_source_code)
iter_token_stream = iter([some_token for some_token in temp_lexer])
del temp_lexer
return iter_token_stream
# ################### ITERATOR PROTOCOL ############################################
def __iter__(self):
return self
def __next__(self):
t = self.token()
if t is None:
raise StopIteration
return t
def next(self):
return self.__next__()
def make_lexer(**kwargs) -> CoolLexer:
"""
Utility function.
:return: CoolLexer object.
"""
a_lexer = CoolLexer(**kwargs)
a_lexer.build()
return a_lexer
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: ./lexer.py program.cl")
exit()
elif not str(sys.argv[1]).endswith(".cl"):
print("Cool program source code files must end with .cl extension.")
print("Usage: ./lexer.py program.cl")
exit()
input_file = sys.argv[1]
with open(input_file, encoding="utf-8") as file:
cool_program_code = file.read()
lexer = make_lexer()
lexer.input(cool_program_code)
for token in lexer:
print(token)