-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathtransformGrammar.py
More file actions
56 lines (48 loc) · 1.5 KB
/
transformGrammar.py
File metadata and controls
56 lines (48 loc) · 1.5 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
import sys, os, re, shutil
from glob import glob
from pathlib import Path
def main(argv):
for file in glob("./parser/*Lexer.g4"):
fix_lexer(file)
for file in glob("./parser/*Parser.g4"):
fix_parser(file)
def fix_lexer(file_path):
print("Altering " + file_path)
if not os.path.exists(file_path):
print(f"Could not find file: {file_path}")
sys.exit(1)
parts = os.path.split(file_path)
file_name = parts[-1]
shutil.move(file_path, file_path + ".bak")
input_file = open(file_path + ".bak",'r')
output_file = open(file_path, 'w')
for x in input_file:
if 'this.' in x and '}?' in x:
x = x.replace('this.', 'p.')
elif 'this.' in x:
x = x.replace('this.', 'l.')
output_file.write(x)
output_file.flush()
print("Writing ...")
input_file.close()
output_file.close()
def fix_parser(file_path):
print("Altering " + file_path)
if not os.path.exists(file_path):
print(f"Could not find file: {file_path}")
sys.exit(1)
parts = os.path.split(file_path)
file_name = parts[-1]
shutil.move(file_path, file_path + ".bak")
input_file = open(file_path + ".bak",'r')
output_file = open(file_path, 'w')
for x in input_file:
if 'this.' in x:
x = x.replace('this.', 'p.')
output_file.write(x)
output_file.flush()
print("Writing ...")
input_file.close()
output_file.close()
if __name__ == '__main__':
main(sys.argv)