-
Notifications
You must be signed in to change notification settings - Fork 0
/
PML2PythonTranslator.py
55 lines (48 loc) · 1.79 KB
/
PML2PythonTranslator.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
import sys
import os
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker
from pml_grammar.pmlLexer import pmlLexer
from pml_grammar.pmlParser import pmlParser
from PythonListener import PythonListener
from ExceptionManagement import ParserExceptionListener
from PML_Preprocessor import PML_Preprocessor
from ExceptionManagement import ParserException
def main(argv):
# argv[2] is the path to the folder where the asset packages
# are stored. Do not add '/' at end.
preprocessor = PML_Preprocessor(argv[2])
hadErrors = False
errorText = ''
try:
preprocessor.process(argv[1])
except Exception as e:
errorText = str(e)
hadErrors = True
if not hadErrors:
input_stream = InputStream(preprocessor.getStream())
lexer = pmlLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = pmlParser(stream)
parser.removeErrorListeners()
exceptionListener = ParserExceptionListener()
parser.addErrorListener( exceptionListener )
try:
tree = parser.styles()
except ParserException as e:
line,col,msg = e.errParams()
localFile,localLine = preprocessor.trackDownLineNr(line)
errorText = 'Error in file {file} on line {line}, col {col}: {msg}'.format(
file = localFile, line = localLine, col = col, msg = msg)
hadErrors = True
except Exception as e:
errorText = str(e)
hadErrors = True
if not hadErrors:
translator = PythonListener()
walker = ParseTreeWalker()
walker.walk(translator, tree)
sys.stdout.write( translator.getCode() )
if hadErrors:
sys.stdout.write(errorText)
if __name__ == '__main__':
main(sys.argv)