forked from isaacg1/pyth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_parse.py
74 lines (60 loc) · 2.18 KB
/
extra_parse.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
# To be included in pyth.py
class PythParseError(Exception):
def __init__(self, active_char, rest_code):
self.active_char = active_char
self.rest_code = rest_code
def __str__(self):
return "%s is not implemented, %d from the end." % \
(self.active_char, len(self.rest_code) + 1)
class UnsafeInputError(Exception):
def __init__(self, active_char, rest_code):
self.active_char = active_char
self.rest_code = rest_code
def __str__(self):
return "%s is unsafe, %d from the end." % \
(self.active_char, len(self.rest_code) + 1)
def num_parse(active_char, rest_code):
output = active_char
while len(rest_code) > 0 \
and rest_code[0] in ".0123456789" \
and (output + rest_code[0]).count(".") <= 1:
output += rest_code[0]
rest_code = rest_code[1:]
if output[-1] == "." and len(rest_code) > 0 and rest_code[0] not in ' \n':
output = output[:-1]
rest_code = "." + rest_code
return output, rest_code
def str_parse(active_char, rest_code):
output = active_char
found_end = False
while len(rest_code) > 0 and not found_end:
if rest_code[0] == '\\' and len(rest_code) == 1:
output += rest_code + '\\'
rest_code = ''
break
if rest_code[0] == '\\' and rest_code[1] in ('"', '\\'):
output += rest_code[:2]
rest_code = rest_code[2:]
elif rest_code[0] == '\\' and rest_code[1] == '\n':
rest_code = rest_code[2:]
elif rest_code[0] == '\n':
output += '\\n'
rest_code = rest_code[1:]
elif rest_code[0] == '\0':
output += '\\000'
rest_code = rest_code[1:]
else:
output += rest_code[0]
rest_code = rest_code[1:]
if output[-1] == '"':
found_end = True
if not found_end:
output += '"'
return output, rest_code
def python_parse(active_char, rest_code):
output = ''
while (len(rest_code) > 0
and rest_code[0] != '$'):
output += rest_code[0]
rest_code = rest_code[1:]
return output, rest_code[1:]