-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
142 lines (109 loc) · 3.69 KB
/
main.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
139
140
141
142
from tkinter import filedialog as fd, messagebox
import os
import platform
import sys
import tkinter as tk
def SelectFile():
return fd.askopenfilename()
def GetGrammarFile():
# Get the grammar file for the user
grammar_file = SelectFile()
while not grammar_file:
retry_box = messagebox.askretrycancel(
title='Gramática',
message='Por favor, seleccione el archivo de gramática'
)
if not retry_box:
exit()
grammar_file = SelectFile()
g_file = grammar_file
return os.path.normpath(g_file)
def GetTestFile():
# Get the test file from the user
test_file = SelectFile()
while not test_file:
retry_box = messagebox.askretrycancel(
title='Archivo de Entrada',
message='Por favor, seleccione el archivo de entrada'
)
if not retry_box:
exit()
test_file = SelectFile()
t_file = test_file
return os.path.normpath(t_file)
def CompileAndExecute(grammar_path, grammar_name, start_rule, file_path):
g_command = f'antlr4 -o . -lib . {grammar_path}'
j_command = f'javac *.java'
t_command = f'grun {grammar_name} {start_rule} -gui -tokens {file_path}'
platform_name = platform.system()
if platform_name == 'Linux' or platform_name == 'Darwin':
g_command = f'java -jar /usr/local/lib/antlr-4.9.2-complete.jar -o . -lib . {grammar_path}'
j_command = f'javac *.java'
t_command = f'java org.antlr.v4.gui.TestRig {grammar_name} {start_rule} -gui -tokens {file_path}'
os.system(g_command)
os.system(j_command)
os.system(t_command)
def GetTextInput(label):
master = tk.Tk()
def GetInput():
master.withdraw()
master.quit()
tk.Label(master, text=label).pack()
# Ask for the user to input the grammar name
text_input = tk.Entry(
master,
justify=tk.LEFT,
width=50
)
text_input.pack()
button = tk.Button(
master,
text='Aceptar',
command=GetInput
)
button.pack()
master.mainloop()
return text_input.get()
if __name__ == "__main__":
grammar_name = None
if len(sys.argv) > 1:
first_argument = sys.argv[1]
if first_argument.lower() == 'decaf':
grammar_name = 'Decaf'
start_rule = 'program'
grammar_file = './Decaf/Decaf.g4'
root = tk.Tk()
root.withdraw()
# Ask for required preparations
requirements = messagebox.askyesno(
title='Requerimientos',
message='¿Está configurado java y ANTLR, junto con los comandos antlr4 y grun?')
# If he doesn't have them, then exit
if not requirements:
messagebox.showerror(
title='Error de Requerimientos',
message='Por favor, instale los requerimientos para utilizar el programa'
)
exit()
if not grammar_name:
grammar_name = GetTextInput('Nombre de la gramática: ')
# Ask for the user to select the grammar file
grammar_box = messagebox.askokcancel(
title='Gramática',
message='Por favor, seleccione el archivo de gramática'
)
# exit out if he cancels
if not grammar_box:
exit()
grammar_file = GetGrammarFile()
start_rule = GetTextInput('Nombre de la regla de inicio:')
# Ask for the user to select the input file
input_file = messagebox.askokcancel(
title='Archivo de entrada',
message='Por favor, seleccione el archivo de entrada'
)
# exit out if he cancels
if not input_file:
exit()
test_file = GetTestFile()
CompileAndExecute(grammar_file, grammar_name, start_rule, test_file)