-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_tokenizer.py
More file actions
executable file
·356 lines (259 loc) · 8.25 KB
/
python_tokenizer.py
File metadata and controls
executable file
·356 lines (259 loc) · 8.25 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
import sys
import token_defs
import buffer_wrapper
import tiny_parser
file_contents = buffer_wrapper.read_ahead(sys.stdin.read())
token_list = buffer_wrapper.token_list()
# ########################################
def get_read_ahead():
return file_contents
def get_next_char():
c = file_contents.get_char()
if len(c) == 0:
return chr(3)
return c
def bookmark():
file_contents.bookmark()
def reset_to_bookmark():
file_contents.reset_to_bookmark()
def set_bookmark(pos):
file_contents.set_bookmark(pos)
def get_bookmark():
return file_contents.bookmark_pos
def get_position():
return file_contents.pos
# ########################################
def get_token_list():
return token_list
def add_token_to_list(token):
token_list.add_token(token)
def get_next_token():
return token_list.get_next_token()
# #######################################
def ignore(char):
return ord(char) in [ord(x) for x in " \n\t\r"]
def is_digit(char):
return ord("0") <= ord(char) <= ord("9")
def is_alpha(char):
return (ord("a") <= ord(char) <= ord("z")) or \
(ord("A") <= ord(char) <= ord("Z")) or \
(ord(char) in [ord("_")])
def scan_str():
bookmark()
str_beginning = get_bookmark()
token_str = get_next_char()
next_char = get_next_char()
#
# check for excluding characters and shit
#
while next_char != "\"":
token_str += next_char
next_char = get_next_char()
token_str += next_char
return token_defs.create_str(token_str)
#
# <float> -> <digit-list> <decimal-part> <exp> |
# <digit-list> <decimal-part> |
# <decimal-part> <exp> |
# <decimal-list> <exp> |
# <decimal-list> '.' <exp>
#
# <decimal-part> -> '.' <decimal-list>
#
#
def scan_float():
bookmark()
float_beginning = get_bookmark()
token_str = get_next_char()
next_digit = get_next_char()
# <digit-list>
while is_digit(next_digit):
token_str += next_digit
next_digit = get_next_char()
if next_digit == token_defs.decimal_prefix["DECIMAL"]:
token_str += next_digit
next_digit = get_next_char()
# <digit-list>
while is_digit(next_digit):
token_str += next_digit
next_digit = get_next_char()
if next_digit == token_defs.decimal_prefix["EXPONENT"]:
token_str += next_digit
next_digit = get_next_char()
if next_digit in "+-":
token_str += next_digit
next_digit = get_next_char()
# <digit-list>
while is_digit(next_digit):
token_str += next_digit
next_digit = get_next_char()
set_bookmark(get_position() - 1)
reset_to_bookmark()
return token_defs.create_float(token_str)
elif token_str[0] == token_defs.decimal_prefix["DECIMAL"] and next_digit == token_defs.decimal_prefix["EXPONENT"]:
token_str += next_digit
next_digit = get_next_char()
if next_digit in "+-":
token_str += next_digit
next_digit = get_next_char()
# <digit-list>
while is_digit(next_digit):
token_str += next_digit
next_digit = get_next_char()
set_bookmark(get_position() - 1)
reset_to_bookmark()
return token_defs.create_float(token_str)
elif token_str[0] == token_defs.decimal_prefix["DECIMAL"] and next_digit != token_defs.decimal_prefix["EXPONENT"]:
set_bookmark(get_position() - 1)
reset_to_bookmark()
return token_defs.create_float(token_str)
return None
#
# <integer> -> <digit> <integer> |
# <digit>
#
# <digit> -> '0' | '1' | ... | '9'
#
#
def scan_int():
bookmark()
int_beginning = get_bookmark()
token_str = get_next_char()
# <float>
# <decimal-part> <exp>
if token_str == token_defs.decimal_prefix["DECIMAL"]:
set_bookmark(int_beginning)
reset_to_bookmark()
return scan_float()
# <integer>
# <integer> -> <digit> <integer> |
# <digit>
next_digit = get_next_char()
while is_digit(next_digit):
token_str += next_digit
next_digit = get_next_char()
#
# <float> -> <digit-list> <dec-part> <exp> |
# <digit-list> <dec-part> |
# <digit-list> <exp> |
# <digit-list> '.' <exp>
#
if next_digit == token_defs.decimal_prefix["DECIMAL"] or \
next_digit == token_defs.decimal_prefix["EXPONENT"]:
set_bookmark(int_beginning)
reset_to_bookmark()
return scan_float()
#
# MADE IT HERE BECAUSE IT DOESNT MATCH ANYTHING
#
set_bookmark(get_position() - 1)
reset_to_bookmark()
return token_defs.create_int_token(token_str)
def next_token():
bookmark()
token_str = get_next_char()
if is_digit(token_str):
reset_to_bookmark()
return scan_int()
elif token_str == token_defs.decimal_prefix["DECIMAL"]:
reset_to_bookmark()
return scan_float()
elif is_alpha(token_str):
bookmark()
next_alpha = get_next_char()
while is_alpha(next_alpha):
bookmark()
token_str += next_alpha
next_alpha = get_next_char()
reset_to_bookmark()
return token_defs.create_id(token_str)
elif ignore(token_str):
return next_token()
elif token_str == token_defs.token_str_def["STR_QUOTE"]:
reset_to_bookmark()
return scan_str()
elif token_str == token_defs.token_defs_operations["ASSIGNMENT"]:
bookmark()
next_relation = get_next_char()
if next_relation == token_defs.token_defs_operations["ASSIGNMENT"]:
token_str += next_relation
else:
reset_to_bookmark()
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["NOT"]:
bookmark()
next_relation = get_next_char()
if next_relation == token_defs.token_defs_operations["ASSIGNMENT"]:
token_str += next_relation
else:
reset_to_bookmark()
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["LESS_THAN"]:
bookmark()
next_relation = get_next_char()
if next_relation == token_defs.token_defs_operations["ASSIGNMENT"]:
token_str += next_relation
else:
reset_to_bookmark()
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["GRT_THAN"]:
bookmark()
next_relation = get_next_char()
if next_relation == token_defs.token_defs_operations["ASSIGNMENT"]:
token_str += next_relation
else:
reset_to_bookmark()
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["BIT_AND"]:
bookmark()
next_relation = get_next_char()
if next_relation == token_defs.token_defs_operations["BIT_AND"]:
token_str += next_relation
else:
reset_to_bookmark()
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["BIT_OR"]:
bookmark()
next_relation = get_next_char()
if next_relation == token_defs.token_defs_operations["BIT_OR"]:
token_str += next_relation
else:
reset_to_bookmark()
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["LESS_THAN"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["GRT_THAN"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["OPAR"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["CPAR"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["OSQ"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["CSQ"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["OCB"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["CCB"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["ADD"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["SUB"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["MULT"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["DIV"]:
return token_defs.create_operation(token_str)
elif token_str == token_defs.token_defs_operations["MOD"]:
return token_defs.create_operation(token_str)
return None
if __name__ == "__main__":
c = 0
token = next_token()
while token is not None:
add_token_to_list(token)
token = next_token()
add_token_to_list(token_defs.create_EOF())
tiny_parser.parse_tokens(get_token_list().token_list)
# for token in get_token_list().token_list:
# print(token)