-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
383 lines (346 loc) · 13.7 KB
/
lexer.py
File metadata and controls
383 lines (346 loc) · 13.7 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
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
from enum import Enum
from typing import NamedTuple
# The token types the Lexer recognizes.
class Tokentype(Enum):
EOI = 0 # end of input
Unknown = 1 # unknown
# Keywords
KwNone = 2 # None
KwPass = 3 # pass
KwBreak = 4 # break
KwContinue = 5 # continue
KwImport = 6 # import
KwFrom = 7 # from
KwAs = 8 # as
KwClass = 9 # class
KwIf = 10 # if
KwElif = 11 # elif
KwElse = 12 # else
KwFor = 13 # while
KwWhile = 14 # while
KwWith = 15 # while
KwDef = 16 # def
KwReturn = 17 # return
KwDel = 18 # del
KwAssert = 19 # assert
KwGlobal = 20 # global
KwNonLocal = 21 # nonlocal
KwTry = 22 # try
KwExcept = 23 # except
KwRaise = 24 # raise
KwFinally = 25 # finally
KwAsync = 26 # async
KwAwait = 27 # away
KwYield = 28 # yield
KwLambda = 29 # lambda
# Operators
OpOr = 30 # or
OpAnd = 31 # and
OpNot = 32 # not
OpIs = 33 # is
OpIn = 34 # in
OpPlus = 35 # +
OpMinus = 36 # -
OpMultiply = 37 # *
OpIntDivide = 38 # //
OpModulus = 39 # %
OpLt = 40 # <
OpGt = 41 # >
OpLtEq = 42 # <=
OpGtEq = 43 # >=
OpEq = 44 # ==
OpNotEq = 45 # !=
OpAssign = 46 # =
# Punctuation marks
ParenthesisL = 47 # (
ParenthesisR = 48 # )
BracketL = 49 # [
BracketR = 50 # ]
Comma = 51 # ,
Colon = 52 # :
Period = 53 # .
Arrow = 54 # ->
# Other
BoolTrueLiteral = 55 # True
BoolFalseLiteral = 56 # False
IntegerLiteral = 57 # digits (see project description)
StringLiteral = 58 # string literal (see project description)
Identifier = 59 # name (see project description)
Indent = 60 # indentation
Dedent = 61 # dedentation
Newline = 62 # newline
class Location(NamedTuple):
line: int
col: int
class Token(NamedTuple):
type: Tokentype
lexeme: str
location: Location
class SyntaxErrorException(Exception):
def __init__(self, message, loc):
self.message = message
self.location = loc
class Lexer:
# Private map of reserved words.
__reserved_words = {
"None": Tokentype.KwNone,
"pass": Tokentype.KwPass,
"break": Tokentype.KwBreak,
"continue": Tokentype.KwContinue,
"import": Tokentype.KwImport,
"from": Tokentype.KwFrom,
"as": Tokentype.KwAs,
"class": Tokentype.KwClass,
"if": Tokentype.KwIf,
"elif": Tokentype.KwElif,
"else": Tokentype.KwElse,
"for": Tokentype.KwFor,
"while": Tokentype.KwWhile,
"with": Tokentype.KwWith,
"def": Tokentype.KwDef,
"return": Tokentype.KwReturn,
"del": Tokentype.KwDel,
"assert": Tokentype.KwAssert,
"global": Tokentype.KwGlobal,
"nonlocal": Tokentype.KwNonLocal,
"try": Tokentype.KwTry,
"except": Tokentype.KwExcept,
"raise": Tokentype.KwRaise,
"finally": Tokentype.KwFinally,
"async": Tokentype.KwAsync,
"await": Tokentype.KwAwait,
"yield": Tokentype.KwYield,
"lambda": Tokentype.KwLambda,
"or": Tokentype.OpOr,
"and": Tokentype.OpAnd,
"not": Tokentype.OpNot,
"is": Tokentype.OpIs,
"in": Tokentype.OpIn,
"True": Tokentype.BoolTrueLiteral,
"False": Tokentype.BoolFalseLiteral
}
def __read_next_char(self):
"""
Private helper routine. Reads the next input character, while keeping
track of its location within the input file.
"""
if self.eof:
self.ch = ''
return
if self.ch == '\n':
self.line += 1
self.col = 1
else:
self.col += 1
self.ch = self.f.read(1)
if not self.ch: # eof
self.eof = True
def __init__(self, f):
"""
Constructor for the lexer.
:param: f handle to the input file (from open('filename')).
"""
self.f, self.ch, self.line, self.col = f, '', 1, 0
self.legal_indent_levels = [1]
self.beginning_of_logical_line = True
self.eof = False
self.__read_next_char() # Read in the first input character (self.ch).
def next(self):
"""
Match the next token in input.
:return: Token with information about the matched Tokentype.
"""
# Remove spaces, tabs, comments, and "empty" lines, if any, before matching the next Tokentype.
# skip character if space or tab
if self.beginning_of_logical_line:
while self.ch == ' ' or self.ch == '\t' or self.ch == '\n':
self.__read_next_char()
else:
while self.ch == ' ' or self.ch == '\t':
self.__read_next_char()
# if we see a comment start, skip that line: read characters until we see a newline character
if self.ch == '#':
self.beginning_of_logical_line = False
while self.ch != '\n' and self.ch != '':
self.__read_next_char()
# Record the start location of the lexeme we're matching.
loc = Location(self.line, self.col)
# Ensure indentation is correct, emitting (returning) an INDENT/DEDENT token if called for.
if self.beginning_of_logical_line:
if loc.col == self.legal_indent_levels[-1]:
pass
elif loc.col > self.legal_indent_levels[-1]:
self.legal_indent_levels.append(loc.col)
token = Token(Tokentype.Indent, "<INDENT>", loc)
return token
else:
self.legal_indent_levels.pop()
if loc.col > self.legal_indent_levels[-1]:
raise SyntaxErrorException("Non matching indentation", loc)
else:
token = Token(Tokentype.Dedent, "<DEDENT>", loc)
return token
# Now, try to match a lexeme.
if self.ch == '':
# at the end of the file, we first generate
# all remaining dedents as specified in manual
if self.legal_indent_levels[-1] > 1:
token = Token(Tokentype.Dedent, "DEDENT", loc)
self.legal_indent_levels.pop()
else:
# '' signifies EOI
token = Token(Tokentype.EOI, '', loc)
elif self.ch == '+':
token = Token(Tokentype.OpPlus, self.ch, loc)
self.__read_next_char()
elif self.ch == '-':
self.__read_next_char()
if self.ch == '>':
token = Token(Tokentype.Arrow, self.ch, loc)
self.__read_next_char()
else:
token = Token(Tokentype.OpMinus, '-', loc)
elif self.ch == '*':
token = Token(Tokentype.OpMultiply, self.ch, loc)
self.__read_next_char()
elif self.ch == '%':
token = Token(Tokentype.OpModulus, self.ch, loc)
self.__read_next_char()
elif self.ch == '/':
self.__read_next_char()
if self.ch == '/':
token = Token(Tokentype.OpIntDivide, "//", loc)
self.__read_next_char()
else:
token = Token(Tokentype.Unknown, "/", loc)
elif self.ch == '=':
self.__read_next_char()
if self.ch == '=':
token = Token(Tokentype.OpEq, "==", loc)
self.__read_next_char()
else:
token = Token(Tokentype.OpAssign, "=", loc)
elif self.ch == '!':
self.__read_next_char()
if self.ch == '=':
token = Token(Tokentype.OpNotEq, "!=", loc)
self.__read_next_char()
else:
token = Token(Tokentype.Unknown, "!", loc)
self.__read_next_char()
elif self.ch == '<':
self.__read_next_char()
if self.ch == '=':
token = Token(Tokentype.OpLtEq, '<=', loc)
self.__read_next_char()
else:
token = Token(Tokentype.OpLt, '<', loc)
elif self.ch == '>':
self.__read_next_char()
if self.ch == '=':
token = Token(Tokentype.OpGtEq, ">=", loc)
self.__read_next_char()
else:
token = Token(Tokentype.OpGt, '>', loc)
elif self.ch == ')':
token = Token(Tokentype.ParenthesisR, self.ch, loc)
self.__read_next_char()
elif self.ch == '(':
token = Token(Tokentype.ParenthesisL, self.ch, loc)
self.__read_next_char()
elif self.ch == ']':
token = Token(Tokentype.BracketR, self.ch, loc)
self.__read_next_char()
elif self.ch == '[':
token = Token(Tokentype.BracketL, self.ch, loc)
self.__read_next_char()
elif self.ch == '.':
token = Token(Tokentype.Period, self.ch, loc)
self.__read_next_char()
elif self.ch == ':':
token = Token(Tokentype.Colon, self.ch, loc)
self.__read_next_char()
elif self.ch == ',':
token = Token(Tokentype.Comma, self.ch, loc)
self.__read_next_char()
elif self.ch == '\n':
token = Token(Tokentype.Newline, '\n', loc)
self.__read_next_char()
elif self.ch == '"':
# Check for a string literal. Raise "Unterminated string"
# syntax error exception if the string doesn't close on the line.
self.__read_next_char()
chars = ""
while self.ch != '"':
chars += self.ch
# newline or comment means string hasn't been terminated
if self.ch == '\n' or self.ch == '#':
raise SyntaxErrorException("Unterminated string literal", loc)
# Only ASCII characters between 32 and 126 are supported
if not 32 <= ord(self.ch) <= 126:
raise SyntaxErrorException("Ill-formed string literal", loc)
# escape character
if self.ch == '\\':
chars = chars[:-1]
self.__read_next_char()
# only n, t, " and / can be escaped
if self.ch != 'n' and self.ch != '\\' and self.ch != 't' and self.ch != '\"':
raise SyntaxErrorException("Ill-formed string literal", loc)
else:
if self.ch == 'n':
next_char = '\n'
elif self.ch == '\\':
next_char = '\\'
elif self.ch == 't':
next_char = '\t'
else:
next_char = '\"'
chars += next_char
pass
self.__read_next_char()
token = Token(Tokentype.StringLiteral, chars, loc)
self.__read_next_char()
else:
# Check for identifiers/reserved words.
if ('a' <= self.ch <= 'z') or ('A' <= self.ch <= 'Z') or (self.ch == '_'):
# Match an identifier.
chars = [self.ch]
self.__read_next_char()
# recall we can also have digits in identifiers, just not as a starting character
while ('a' <= self.ch <= 'z') or ('A' <= self.ch <= 'Z') or (self.ch == '_') or self.ch.isdigit():
chars.append(self.ch)
self.__read_next_char()
joined_str = ''.join(chars)
if joined_str in self.__reserved_words.keys():
token = Token(self.__reserved_words.get(joined_str), joined_str, loc)
else:
token = Token(Tokentype.Identifier, joined_str, loc)
elif self.ch.isdigit():
# Match a number literal.
# if first character is a zero, there can be no more digits after
if self.ch == '0':
self.__read_next_char()
if self.ch.isdigit():
raise SyntaxErrorException("Ill-formed integer literal", loc)
else:
token = Token(Tokentype.IntegerLiteral, "0", loc)
else:
chars = [self.ch]
self.__read_next_char()
# read until no more digits
while self.ch.isdigit():
chars.append(self.ch)
self.__read_next_char()
# create an integer literal out of the character list
chars_to_int = int("".join([str(c) for c in chars]))
# if integer literal larger than max, throw error
if chars_to_int > 2147483647:
raise SyntaxErrorException("Ill-formed integer literal", loc)
else:
token = Token(Tokentype.IntegerLiteral, ''.join(chars), loc)
else:
# Return Unknown if no other known token is matched.
token = Token(Tokentype.Unknown, self.ch, loc)
self.__read_next_char()
self.beginning_of_logical_line = token.type == Tokentype.Newline
return token