MPC/tools/genANTLR4.py

40 lines
746 B
Python
Raw Normal View History

2018-12-04 02:05:06 +07:00
import os
import subprocess
ANTLR_JAR = os.environ.get('ANTLR_LIB')
if ANTLR_JAR is None:
# fall back, not recommended
ANTLR_JAR = '../external/antrl4.jar'
files_from_antlr4 = [
'parser/MP.interp',
'parser/MPLexer.interp',
'parser/MPLexer.py',
'parser/MPLexer.tokens',
'parser/MPParser.py',
'parser/MP.tokens',
'parser/MPVisitor.py'
]
2018-12-04 02:05:06 +07:00
def generate():
if all(list(map(os.path.isfile, files_from_antlr4))):
return
gen_antlr_class_cmd = [
"java",
"-jar",
ANTLR_JAR,
"-no-listener",
"-visitor",
"parser/MP.g4"
]
subprocess.run(gen_antlr_class_cmd)
def regenerate():
for f in files_from_antlr4:
os.remove(f)
2018-12-04 02:05:06 +07:00
generate()