forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_parser.py
90 lines (80 loc) · 2.25 KB
/
cmd_parser.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
import re
import sys
NUMBER_REGEX = r"(?:-?(?:(?:[1-9][0-9]*)|[0-9])(?:\.[0-9]+)?(?:e-?[0-9]+)?)|nan|inf|-inf"
CHAR_REGEX = r"[^'\\]|(?:\\(?:['\\ntr]|x[0-9A-Fa-f]{2}))"
CHAR2_REGEX = r"[^\"\\]|(?:\\(?:[\"\\ntr]|x[0-9A-Fa-f]{2}))"
STRING_REGEX = r"(?:'(?:%s)*')|(?:\"(?:%s)*\")" % (CHAR_REGEX, CHAR2_REGEX)
BOOLEAN_REGEX = r"True|False"
REF_REGEX = r"\*[0-9]+"
NONE_REGEX = r"None"
ARGUMENT_REGEX = re.compile("(%s|%s|%s|%s|%s)[,)]" % (
NUMBER_REGEX,
STRING_REGEX,
BOOLEAN_REGEX,
REF_REGEX,
NONE_REGEX
))
LINE_START_REGEX = re.compile(r"([A-Z_]+)\(")
class HeapRef(object):
def __init__(self, id):
self.id = id
def serialize_member(self):
return '^' + str(self.id)
def __repr__(self):
return f"HeapRef({self.id})"
def __eq__(self, other):
return self.id == other.id
def __hash__(self):
return self.id
def parse_value(value):
if value[0] == "*":
return HeapRef(int(value[1:]))
elif value == "nan":
return float("nan")
elif value == "inf":
return float('inf')
elif value == "-inf":
return float('-inf')
else:
return eval(value)
def parse_line(line):
args = []
m = LINE_START_REGEX.match(line)
if m is None:
raise Exception("Parse error on line '%s'" % line)
fun_name = m.group(1)
start_idx = m.span()[1]
while True:
m = ARGUMENT_REGEX.match(line, start_idx)
if m is None:
break
value = parse_value(m.group(1))
args.append(value)
if m.span()[1] == len(line):
break
if line[m.span()[1]] == "\n":
break
assert line[m.span()[1]] == " "
start_idx = m.span()[1] + 1
return (fun_name, args)
def main():
if len(sys.argv) < 2:
print("Please provide a .rewind file.")
exit(1)
filename = sys.argv[1]
file = open(filename, "r")
count = 0
file_iterator = iter(file)
try:
while True:
line = next(file_iterator)
if line.startswith("--"):
continue
command = parse_line(line)
count += 1
# print("\r%d" % count, end="")
except StopIteration:
pass
file.close()
if __name__ == '__main__':
main()