forked from ml4tp/gamepad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_script.py
138 lines (121 loc) · 3.82 KB
/
parse_script.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
import argparse
import json
import os.path as op
from lib.myfile import MyFile
def create_lemma(f_out, hdr, body):
# Unpack
s_hdr = "\n".join(hdr)
toks = s_hdr.split()
kind = toks[0].strip()
name = toks[1].strip()
stmt = " ".join(toks[2:])
proof = "\n".join(body)
# Emit
msg = json.dumps({"kind": kind,
"name": name,
"stmt": stmt,
"proof": proof})
f_out.write(msg)
f_out.write("\n")
return (kind, name, stmt, proof)
def parse_file(inpath, filename, outpath):
in_filename = op.join(inpath, filename)
print("PARSING: {}".format(in_filename))
f = MyFile(in_filename)
out_filename = op.join(outpath, filename)
out = open("{}.parse".format(out_filename), 'w')
hdr = []
body = []
acc = []
f_hdr = False
f_let = False
f_proof = False
while f.raw_peek_line() != "":
line = f.consume_line()
print("Parsing", line)
if line.startswith("Fact") or \
line.startswith("Remark") or \
line.startswith("Proposition") or \
line.startswith("Corollary") or \
line.startswith("Lemma") or \
line.startswith("Theorem"):
f_hdr = True
hdr += [line]
elif line.startswith("Let"):
if f_let:
hdr = []
f_let = True
f_hdr = True
hdr += [line]
elif line.startswith("Proof"):
f_proof = True
f_hdr = False
if line != "Proof.":
if line.endswith("Qed."):
end = len(line) - 6 - 4
body += [line[6:end]]
acc += [create_lemma(out, hdr, body)]
hdr = []
body = []
f_proof = False
else:
body += [line[6:]]
elif line.startswith("Qed"):
if not f_proof:
out.close()
raise NameError("Expected to be in proof mode parsing {}".
format(line))
acc += [create_lemma(out, hdr, body)]
hdr = []
body = []
f_proof = False
else:
if f_hdr:
hdr += [line]
elif f_proof:
body += [line]
out.close()
if __name__ == "__main__":
# Set up command line
argparser = argparse.ArgumentParser()
argparser.add_argument("-p", "--path", default="odd-order/mathcomp/odd_order",
type=str, help="Path to files")
argparser.add_argument("-op", "--outpath", default="data/odd-order",
type=str, help="Path to files")
args = argparser.parse_args()
files = ["BGsection1.v",
"BGsection2.v",
"BGsection3.v",
"BGsection4.v",
"BGsection5.v",
"BGsection6.v",
"BGsection7.v",
"BGsection8.v",
"BGsection9.v",
"BGsection10.v",
"BGsection11.v",
"BGsection12.v",
"BGsection13.v",
"BGsection14.v",
"BGsection15.v",
"BGsection16.v",
"BGappendixAB.v",
"BGappendixC.v",
"PFsection1.v",
"PFsection2.v",
"PFsection3.v",
"PFsection4.v",
"PFsection5.v",
"PFsection6.v",
"PFsection7.v",
"PFsection8.v",
"PFsection9.v",
"PFsection10.v",
"PFsection11.v",
"PFsection12.v",
"PFsection13.v",
"PFsection14.v",
"stripped_odd_order_theorem.v",
"wielandt_fixpoint.v"]
for file in files:
parse_file(args.path, file, args.outpath)