-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
50 lines (41 loc) · 1.72 KB
/
__init__.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
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker
from .pml_grammar.pmlLexer import pmlLexer
from .pml_grammar.pmlParser import pmlParser
from .PythonListener import PythonListener
from .PML_Preprocessor import PML_Preprocessor
from .ExceptionManagement import ParserExceptionListener
from .ExceptionManagement import ParserException
class PML:
def __init__(self, pmlFilePath, rootDir):
"""
Args:
pmlFilePath (str): Path to a PML file
rootDir (str): Root directory to use for includes <@include>
if the path in <@include> starts with '/'
"""
self.pmlFilePath = pmlFilePath
self.rootDir = rootDir
def getPythonCode(self):
preprocessor = PML_Preprocessor(self.rootDir)
preprocessor.process(self.pmlFilePath)
inputStream = InputStream(preprocessor.getStream())
lexer = pmlLexer(inputStream)
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)
raise Exception(
"Error in file {file} on line {line}, col {col}: {msg}".format(
file = localFile, line = localLine, col = col, msg = msg
)
)
translator = PythonListener()
walker = ParseTreeWalker()
walker.walk(translator, tree)
return translator.getCode()