-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformGrammar.py
More file actions
44 lines (40 loc) · 1.81 KB
/
transformGrammar.py
File metadata and controls
44 lines (40 loc) · 1.81 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
import sys, os, re, shutil
if __name__ == "__main__":
print("Python utility to transform the JavaScript Parser and Lexer grammars to work with a python output")
if len(sys.argv) == 1:
print(f"""Please run this script as {sys.argv[0]} <grammar>.g4""")
sys.exit(0)
file_path = sys.argv[1]
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]
if file_name.lower() not in ["javascriptparser.g4", "javascriptlexer.g4"]:
print("Could not determine which grammar you have specified, please specify either the Paarser or the Lexer")
sys.exit(1)
shutil.move(file_path, file_path + ".bak")
input_file = open(file_path + ".bak",'r')
output_file = open(file_path, 'w')
if file_name.lower() == "javascriptparser.g4":
for x in input_file:
if 'this.' in x:
x = x.replace('this.', 'self.')
output_file.write(x)
output_file.flush()
elif file_name.lower() == "javascriptlexer.g4":
for x in input_file:
if '!this.IsStrictMode' in x:
x = x.replace('!this.Is', 'not self.is')
if 'this.Is' in x:
x = x.replace('this.Is', 'self.is')
if 'this.ProcessOpenBrace();' in x:
x = x.replace('this.ProcessOpenBrace();', 'self.processOpenBrace()')
if 'this.ProcessCloseBrace();' in x:
x = x.replace('this.ProcessCloseBrace();', 'self.processCloseBrace()')
if 'this.ProcessStringLiteral();' in x:
x = x.replace('this.ProcessStringLiteral();', 'self.processStringLiteral()')
output_file.write(x)
output_file.flush()
input_file.close()
output_file.close()