-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.py
More file actions
executable file
·361 lines (309 loc) · 11.9 KB
/
tokenizer.py
File metadata and controls
executable file
·361 lines (309 loc) · 11.9 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
#!/usr/bin/env python3
""" Tokenizer module. """
from enum import Enum, auto
import os
import sys
class TokenType(Enum):
""" TokenType defines all valid token types. See tokens.txt for the token
definition. """
NUMBER = "number"
IDENTIFIER = "identifier"
STRING = "string"
LEFT_PARENTHESIS = "("
RIGHT_PARENTHESIS = ")"
LEFT_BRACKET = "["
RIGHT_BRACKET = "]"
LEFT_BRACE = "{"
RIGHT_BRACE = "}"
MULTIPLY = "*"
DIVIDE = "/"
MOD = "%"
PLUS = "+"
MINUS = "-"
LT = "<"
GT = ">"
LE = "<="
GE = ">="
EQEQ = "=="
NOTEQ = "!="
EQ = "="
COMMA = ","
SEMICOLON = ";"
AND = "and"
OR = "or"
IF = "if"
ELSE = "else"
WHILE = "while"
BREAK = "break"
CONTINUE = "continue"
RETURN = "return"
DEF = "def"
NONE = "None"
class Token:
""" A Token object represents one token.
Parameters:
----------
self.type : TokenType
The type of the token.
self.value :
If self.type is TokenType.NUMBER, self.value stores the number (int or
float).
If self.type is TokenType.STRING, self.value stores the string.
If self.type is TokenType.IDENTIFIER, self.value stores a string of the
identifier.
Otherwise, self.value is None.
"""
def __init__(self, token_type, value = None):
self.type = token_type
self.value = value
def print(self):
""" Print the token. """
if self.type == TokenType.NUMBER:
print(self.value, end="")
elif self.type == TokenType.STRING:
print('"' + self.value + '"', end="")
elif self.type == TokenType.IDENTIFIER:
print(self.value, end="")
else:
print(self.type.value, end="")
class DebugInfo:
""" DebugInfo is used to raise an exception with useful debug information.
How to use: You create DebugInfo by passing the tokenizer:
debug_info = DebugInfo(tokenizer)
The DebugInfo remembers the current line number and other useful debug
information. Later you can call:
debug_info.raise_exception("message")
This raises an exception with the message, the line number and the source
code of the line.
Parameters:
----------
self.tokenizer : Tokenizer
The tokenizer.
self.line_start_index : int
The source code index that starts the current line.
self.tokenizer.source[self.line_start_index] gives the first character
of the current line.
self.line_number : int
The current line number.
"""
def __init__(self, tokenizer):
self.tokenizer = tokenizer
self.line_start_index = tokenizer.line_start_index
self.line_number = tokenizer.line_number
def raise_exception(self, message):
""" Raise an exception with useful debug information. """
line_end = self.tokenizer.source.find('\n', self.line_start_index)
raise Exception(
"%s\nline %d: %s" %
(message, self.line_number,
self.tokenizer.source[
self.line_start_index :
(self.tokenizer.__source_length
if line_end == -1 else line_end)]))
class Tokenizer:
""" Tokenizer.
How to use: You can create a Tokenizer by giving a program filename:
tokenizer = Tokenizer("program.step")
tokenizer.read_token() reads one Token and returns the Token. When the
tokenizer reaches the end of the file, it returns None.
while token := tokenizer.read_token():
token.print() # Print the Token.
Parameters:
----------
self.source : string
The source code.
self.__source_length : int
The length of the source code.
self.__index : int
The current index the tokenizer is reading.
self.line_start_index : int
The index that starts the current line.
self.line_number : int
The current line number.
"""
def __init__(self, filename):
with open(filename) as file:
self.source = "".join(file.readlines())
self.__source_length = len(self.source)
self.__index = 0
self.line_start_index = 0
self.line_number = 1
def read_token(self):
""" Read one Token and return the Token. When the tokenizer reaches
the end of the file, return None.
"""
# Skip whitespaces and comments.
self.__read_whitespace_and_comment()
if self.__index >= self.__source_length:
return None
# Tokenize two character operators first. "<=" needs to be parsed as
# TokenType.LE, not as TokenType.LT and TokenType.EQ.
if self.__read_operator("<="):
return Token(TokenType.LE)
if self.__read_operator(">="):
return Token(TokenType.GE)
if self.__read_operator("=="):
return Token(TokenType.EQEQ)
if self.__read_operator("!="):
return Token(TokenType.NOTEQ)
# Tokenize one character operators second.
if self.__read_operator("("):
return Token(TokenType.LEFT_PARENTHESIS)
if self.__read_operator(")"):
return Token(TokenType.RIGHT_PARENTHESIS)
if self.__read_operator("["):
return Token(TokenType.LEFT_BRACKET)
if self.__read_operator("]"):
return Token(TokenType.RIGHT_BRACKET)
if self.__read_operator("{"):
return Token(TokenType.LEFT_BRACE)
if self.__read_operator("}"):
return Token(TokenType.RIGHT_BRACE)
if self.__read_operator("*"):
return Token(TokenType.MULTIPLY)
if self.__read_operator("/"):
return Token(TokenType.DIVIDE)
if self.__read_operator("%"):
return Token(TokenType.MOD)
if self.__read_operator("+"):
return Token(TokenType.PLUS)
if self.__read_operator("-"):
return Token(TokenType.MINUS)
if self.__read_operator("<"):
return Token(TokenType.LT)
if self.__read_operator(">"):
return Token(TokenType.GT)
if self.__read_operator("="):
return Token(TokenType.EQ)
if self.__read_operator(","):
return Token(TokenType.COMMA)
if self.__read_operator(";"):
return Token(TokenType.SEMICOLON)
if (string := self.__read_string()) != None:
return Token(TokenType.STRING, string)
if (number := self.__read_number()) != None:
return Token(TokenType.NUMBER, number)
if (identifier := self.__read_identifier()) != None:
# If the identifier is a registered keyword, return the
# corresponding Token.
if identifier == "and":
return Token(TokenType.AND)
if identifier == "or":
return Token(TokenType.OR)
if identifier == "if":
return Token(TokenType.IF)
if identifier == "else":
return Token(TokenType.ELSE)
if identifier == "while":
return Token(TokenType.WHILE)
if identifier == "break":
return Token(TokenType.BREAK)
if identifier == "continue":
return Token(TokenType.CONTINUE)
if identifier == "return":
return Token(TokenType.RETURN)
if identifier == "def":
return Token(TokenType.DEF)
if identifier == "None":
return Token(TokenType.NONE)
# Otherwise, return the identifier.
return Token(TokenType.IDENTIFIER, identifier)
DebugInfo(self).raise_exception("Undefined token found.")
def __read_whitespace_and_comment(self):
""" Read whitespaces (' ', '\t' and '\n') and comments. They are
ignored by the syntax. """
while True:
previous_index = self.__index
if (self.__index < self.__source_length and
self.source[self.__index] == '#'):
while (self.__index < self.__source_length and
self.source[self.__index] != '\n'):
self.__index += 1
while (self.__index < self.__source_length and
(self.source[self.__index] == ' ' or
self.source[self.__index] == '\t' or
self.source[self.__index] == '\n')):
if self.source[self.__index] == '\n':
self.line_number += 1
self.line_start_index = self.__index + 1
self.__index += 1
if previous_index == self.__index:
return
def __read_operator(self, operator):
""" Read an operator specified by `operator`. Return True if the
`operator` is found. Return False otherwise. """
operator_length = len(operator)
if self.__index + operator_length > self.__source_length:
return False
if self.source[self.__index :
self.__index + operator_length] == operator:
self.__index += operator_length
return True
return False
def __read_string(self):
""" Read a string. Return the string if it's found. Return None
otherwise. """
assert(self.__index < self.__source_length)
if self.source[self.__index] == '"':
self.__index += 1
current_index = self.__index
while (current_index < self.__source_length and
self.source[current_index] != '"'):
if self.source[current_index] == '\n':
self.line_number += 1
self.line_start_index = self.__index + 1
current_index += 1
if current_index >= self.__source_length:
DebugInfo(self).raise_exception("Invalid string found.")
string = self.source[self.__index : current_index]
self.__index = current_index + 1
return string
return None
def __read_identifier(self):
""" Read an identifier. Return the identifier if it's found. Return
None otherwise. """
assert(self.__index < self.__source_length)
current_index = self.__index
if (self.source[current_index] == '_' or
self.source[current_index].isalpha()):
current_index += 1
while (current_index < self.__source_length and
(self.source[current_index] == '_' or
self.source[current_index].isalpha() or
self.source[current_index].isdigit())):
current_index += 1
identifier = self.source[self.__index : current_index]
self.__index = current_index
return identifier
return None
def __read_number(self):
""" Read a number. Return the number if it's found. Return None
otherwise. """
assert(self.__index < self.__source_length)
if (self.source[self.__index].isdigit()):
number = 0
while (self.__index < self.__source_length and
self.source[self.__index].isdigit()):
number = number * 10 + int(self.source[self.__index])
self.__index += 1
if (self.__index < self.__source_length and
self.source[self.__index] == '.'):
self.__index += 1
decimal = 0.1
while (self.__index < self.__source_length and
self.source[self.__index].isdigit()):
number += int(self.source[self.__index]) * decimal
decimal /= 10
self.__index += 1
return number
return None
def main(filename):
tokenizer = Tokenizer(filename)
while token := tokenizer.read_token():
token.print()
print()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("usage: %s filename" % sys.argv[0])
exit(1)
main(sys.argv[1])