-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex_integer_2.py
531 lines (516 loc) · 19.7 KB
/
lex_integer_2.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 1 18:11:05 2022
Floats added Wed Sep 9 8:34 2022
@author: github.com/TARDIInsanity
"""
__version__ = "2.4"
# all bases must begin with numeric sequences, and not begin with other base headers
BASES = {"0a":36, "0b":2, "0d":10, "0o":8, "0p":5, "0q":4, "0s":6, "0t":3, "0u":1, "0v":20, "0x":16}
ALNUM = "".join(chr(i+48) for i in range(10))+"".join(chr(i+97) for i in range(26))
ALDICTS = dict((base, dict((c, i) for i, c in enumerate(ALNUM[:base]))) for base in BASES.values())
def pull_ide(code:str, allow_underscore:bool=True) -> (bool, str, str):
if not any(i.isalpha() or i == "_" for i in code[:1]):
return (False, "", code)
for index, char in enumerate(code):
if not (char.isalnum() or (allow_underscore and char == "_")):
break
else:
index += 1
return (index > 0, code[:index], code[index:])
def get_int(code:str) -> int:
if not code:
return 0
if (key := code[:2]) in BASES:
base = BASES[key]
code = code[2:]
else:
key = ""
base = 10
index = 0
clen = len(code)
if base == 1:
if set(code) - {"1"}: # if the set of chars in the integer that aren't '1' is non-empty...
raise SyntaxError("Unexpected chars when processing integer in base 1: "+key+code)
return clen
'''
quick = ALDICTS[base]
result = 0
while index < clen:
try:
result = result*base + quick[code[index]]
index += 1
except KeyError:
raise SyntaxError(f"Unexpected chars when processing integer in base {base}"+
(f" (valid portion: {key}{code[:index]})" if index else "")+
f": {code[index:]}")
'''
try:
return int(code, base=base)
except ValueError as e:
raise SyntaxError(e)
class IntPuller:
valid_char = staticmethod(str.isalnum)
convert = staticmethod(get_int)
def __init__(self,
single_seps:set=(), multi_seps:set=(),
single_sep_suffix:bool=False, multi_sep_suffix:bool=False):
self.single_seps = set(single_seps)
self.multi_seps = set(multi_seps)
self.seps = self.single_seps.union(self.multi_seps)
self.single_sep_suffix = single_sep_suffix
self.multi_sep_suffix = multi_sep_suffix
@staticmethod
def singlet_error(char:str, segment:str) -> SyntaxError:
return SyntaxError(f"multiple singlet separators {repr(char)} appeared "
"in a row while parsing an int: {repr(segment)}")
@staticmethod
def suffix_error(char:str, segment:str, name:str) -> SyntaxError:
return SyntaxError(f"{name} separator {repr(char)} appeared at the end "
"while parsing an int: {repr(segment)}")
def pull_int(self, code:str) -> (bool, object, str):
if self.single_seps:
if self.multi_seps:
return self.sep_pull_int(code)
return self.nomul_pull_int(code)
if self.multi_seps:
return self.nosing_pull_int(code)
return self.nosep_pull_int(code)
def nosep_pull_int(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
for index, char in enumerate(code):
if not char.isalnum():
break
else:
index += 1
try:
result = self.convert(code[:index])
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def nomul_pull_int(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
single = False
for index, char in enumerate(code):
if char in self.single_seps:
if single:
return (False, self.singlet_error(char, code[:index]), code)
single = True
elif char.isalnum():
single = False
else:
break
else:
index += 1
attempt = code[:index]
if single and not self.single_sep_suffix:
return (False, self.suffix_error(char, attempt, "singlet"), code)
try:
result = self.convert("".join(i for i in attempt if i not in self.seps))
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def nosing_pull_int(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
digit = False
for index, char in enumerate(code):
if char.isalnum():
digit = True
elif char in self.multi_seps:
digit = False
else:
break
else:
index += 1
attempt = code[:index]
if not (digit or self.multi_sep_suffix):
return (False, self.suffix_error(char, attempt, "multi"), code)
try:
result = self.convert("".join(i for i in attempt if i not in self.seps))
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def sep_pull_int(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
single = False
digit = False
for index, char in enumerate(code):
if char in self.single_seps:
if single:
return (False, self.singlet_error(char, code[:index]), code)
single = True
digit = False
elif char.isalnum():
single = False
digit = True
elif char in self.multi_seps:
single = False
digit = False
else:
break
else:
index += 1
attempt = code[:index]
if single and not self.single_sep_suffix:
return (False, self.suffix_error(char, attempt, "singlet"), code)
if not (single or digit) and not self.multi_sep_suffix:
return (False, self.suffix_error(char, attempt, "multi"), code)
try:
result = self.convert("".join(i for i in attempt if i not in self.seps))
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def pull_string(code:str, opener:str, closer:str, escapes:(dict, sorted)) -> (bool, str, str):
# expects (opener) to be the detected opening sequence
# expects (closer) to be the appropriate closing sequence
# expects (escapes) to be a dict of first_char:sorted_dict pairs
# sorted_dicts should contain sequence:interpretation pairs
old = code
code = code[len(opener):]
clen = len(code)
result = []
index = 0
success = False
while index < clen:
char = code[index]
if char in escapes:
target = escapes[char]
segment = code[index:index+target[None][0]]
for length in target[None]:
segment = segment[index:index+length]
if segment in target[length]:
result.append(code[:index]+target[length][segment])
code = code[index+length:]
clen -= index+length
index = -1
break
if closer.startswith(char) and code.count(closer, index, index+len(closer)):
result.append(code[:index])
code = code[index+len(closer):]
success = True
break
index += 1
else:
return (False, "", old)
if success:
return (success, "".join(result), code)
return (False, "", old)
def convert_to_float(code:str) -> int:
'''assumes a string containing exactly one ., without which it would be a valid integer.'''
if not code:
return 0
original = code
base = BASES.get(code[:2], 10)
left, code = code.split(".")
integral = get_int(left)
if base == 10:
return integral+float("."+code)
clen = len(code)
if base == 1:
if set(code) - {"1"}: # if the set of chars in the float that aren't '1' is non-empty...
raise SyntaxError("Unexpected chars when processing float in base 1: "+original)
return float(integral+clen)
result = 0
d = ALDICTS[base]
for i in reversed(code):
result = (result+d[i])/base
return integral + result
class FloatPuller:
@staticmethod
def valid_char(char:str):
return char.isalnum() or char == "."
convert = staticmethod(convert_to_float)
def __init__(self,
single_seps:set=(), multi_seps:set=(),
single_sep_suffix:bool=False, multi_sep_suffix:bool=False):
self.single_seps = set(single_seps)
self.multi_seps = set(multi_seps)
self.seps = self.single_seps.union(self.multi_seps)
self.single_sep_suffix = single_sep_suffix
self.multi_sep_suffix = multi_sep_suffix
@staticmethod
def singlet_error(char:str, segment:str) -> SyntaxError:
return SyntaxError(f"multiple singlet separators {repr(char)} appeared "
"in a row while parsing a float: {repr(segment)}")
@staticmethod
def suffix_error(char:str, segment:str, name:str) -> SyntaxError:
return SyntaxError(f"{name} separator {repr(char)} appeared at the end "
"while parsing a float: {repr(segment)}")
def pull_float(self, code:str) -> (bool, object, str):
if self.single_seps:
if self.multi_seps:
return self.sep_pull_float(code)
return self.nomul_pull_float(code)
if self.multi_seps:
return self.nosing_pull_float(code)
return self.nosep_pull_float(code)
def nosep_pull_float(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
dot = False
for index, char in enumerate(code):
if char == ".":
if dot:
break
dot = True
continue
if not char.isalnum():
break
else:
index += 1
try:
result = self.convert(code[:index])
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def nomul_pull_float(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
single = False
dot = False
for index, char in enumerate(code):
if char == ".":
if dot:
break
dot = True
single = False
continue
if char in self.single_seps:
if single:
return (False, self.singlet_error(char, code[:index]), code)
single = True
elif char.isalnum():
single = False
else:
break
else:
index += 1
attempt = code[:index]
if single and not self.single_sep_suffix:
return (False, self.suffix_error(char, attempt, "singlet"), code)
try:
result = self.convert("".join(i for i in attempt if i not in self.seps))
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def nosing_pull_float(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
digit = False
dot = False
for index, char in enumerate(code):
if char == ".":
if dot:
break
dot = True
digit = True
continue
if char.isalnum():
digit = True
elif char in self.multi_seps:
digit = False
else:
break
else:
index += 1
attempt = code[:index]
if not (digit or self.multi_sep_suffix):
return (False, self.suffix_error(char, attempt, "multi"), code)
try:
result = self.convert("".join(i for i in attempt if i not in self.seps))
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
def sep_pull_float(self, code:str) -> (bool, object, str):
if not self.valid_char(code[:1]):
return (False, None, code)
single = False
digit = False
dot = False
for index, char in enumerate(code):
if char == ".":
if dot:
break
dot = True
digit = True
single = False
if char in self.single_seps:
if single:
return (False, self.singlet_error(char, code[:index]), code)
single = True
digit = False
elif char.isalnum():
single = False
digit = True
elif char in self.multi_seps:
single = False
digit = False
else:
break
else:
index += 1
attempt = code[:index]
if single and not self.single_sep_suffix:
return (False, self.suffix_error(char, attempt, "singlet"), code)
if not (single or digit) and not self.multi_sep_suffix:
return (False, self.suffix_error(char, attempt, "multi"), code)
try:
result = self.convert("".join(i for i in attempt if i not in self.seps))
return (True, result, code[index:])
except SyntaxError as e:
return (False, e, code)
#########################
# unrelated handy utils #
#########################
def sorted_set(vals:set) -> dict:
'''assumes an iterable input.
returns a dict of the form {None:[...], *(key:int, val:set)},
where [...] is a reverse-sorted list of the integer keys.
all items in the input are assumed to have a len() property.
all items are slotted into exactly one set.'''
result = {}
for i in vals:
li = len(i)
if li not in result:
result[li] = set()
result[li].add(i)
result[None] = sorted(result, reverse=True)
return result
def sorted_dict(vals:dict) -> dict:
'''assumes an iterable input that defines vals[x] for x in vals.
returns a dict of the form {None:[...], *(key:int, val:dict)},
where [...] is a reverse-sorted list of the integer keys.
all keys in the input are assumed to have a len() property.
all key:val pairs are slotted into exactly one dict.'''
result = {}
for i in vals:
li = len(i)
if li not in result:
result[li] = {}
result[li][i] = vals[i]
result[None] = sorted(result, reverse=True)
return result
pyth_openclose = {
chr(39):chr(39),
chr(34):chr(34),
chr(39)*3:chr(39)*3,
chr(34)*3:chr(34)*3,
}
pyth_escape = {
"\\":sorted_dict({"\\\'":"\'", "\\\"":"\"", "\\\\":"\\",
"\\n":"\n", "\\r":"\r", "\\t":"\t",
"\\b":"\b", "\\f":"\f", "\\\n":"",
})
}
def gen_tab_tree_first_pass(code:str, feed:callable=None) -> "list[(str[str.isspace], str)]":
'''splitting code by \n, turn " ..." into (" ", "...")
feed can be substituted with any function that returns an
iterable over the lines of the code.
'''
if feed is None:
feed = lambda s: s.split("\n")
temp = []
for line in feed(code):
index = 0
llen = len(line)
while index < llen and line[index].isspace():
index += 1
temp.append((line[:index], line[index:]))
return temp
def gen_tab_tree_second_pass(code:"list[(str, str)]") -> "list[(int[abs], str)]":
'''taking output from first_pass, convert the spaces into >=0 integer depth measures.
(" ", "a") -> (1, "a").
does not error for any particular space string in the (space, str) input pairs.'''
temp = code
index = 0
tlen = len(temp)-1
buffer = [(0, temp[0][1])]
while index < tlen:
index += 1
a, b = temp[index-1][0], temp[index][0]
if b.startswith(a):
depth = buffer[-1][0] + (b != a)
buffer.append((depth, temp[index][1]))
continue
j = index-1
while j >= 0:
if temp[j][0] == b:
depth = buffer[j][0]
buffer.append((depth, temp[index][1]))
break
j -= 1
else:
raise IndentationError(f"line {index} in code sample ~ {temp[index]}")
return buffer
def gen_tab_tree_third_pass(code:"list[(int[abs], str)]") -> dict:
'''expecting (int>=0, str) pairs, nest items.'''
buffer = code
nest = [{"depth":-1, "name":None, "block":[]}]
#nest = [{None:0}]
def pop_level():
siblings = nest.pop(-1)
nest[-1]["block"].append(siblings)
# nest[-1][nest[-1][True]] = siblings
while buffer:
depth, name = buffer.pop(0)
if depth > nest[-1]["depth"]:
nest.append({"depth":depth, "name":name, "block":[]})
# if depth > nest[-1][None]:
# nest.append({None:depth, True:name, name:{}})
continue
while nest[-1]["depth"] > depth:
pop_level()
# while nest[-1][None] > depth:
# pop_level()
if depth == nest[-1]["depth"]:
pop_level()
nest.append({"depth":depth, "name":name, "block":[]})
continue
# if depth == nest[-1][None]:
# nest[-1][True] = name
# nest[-1][name] = {}
# continue
raise IndentationError("unindent does not match any outer indentation level")
while len(nest) > 1:
pop_level()
return nest[0]
def generate_tab_tree(code:str, feed=None) -> dict:
# generate a tree according to python's indentation rules
temp = gen_tab_tree_first_pass(code, feed)
buffer = gen_tab_tree_second_pass(temp)
nesting = gen_tab_tree_third_pass(buffer)
return nesting
def gen_exception_util(nesting:dict, parent:type=BaseException):
classes = {}
for tree in nesting["block"]:
if not str.isidentifier(tree['name']):
raise ValueError(("requires an identifier", tree['name']))
exec("\n".join((
f"class {tree['name']}(parent):",
" pass",
f"classes[tree['name']] = {tree['name']}"
)))
classes.update(gen_exception_util(tree, classes[tree['name']]))
return classes
def generate_classes(base_class:type, code:str, feed=None) -> dict:
'''expects input of the following form:
"\n".join((
"BaseException",
" KeyboardInterrupt",
" Exception",
" ArithmeticError",
" ZeroDivisionError",
" AssertionError"))'''
nesting = generate_tab_tree(code, feed)
exceptions = gen_exception_util(nesting, base_class)
return exceptions
def ignore_iterator(iterator:iter):
try:
while True:
next(iterator)
except StopIteration as e:
return e.value