-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompiler.py
More file actions
34 lines (26 loc) · 789 Bytes
/
compiler.py
File metadata and controls
34 lines (26 loc) · 789 Bytes
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
from io import StringIO
import parser.parser as p
from enum import Enum
from lexer.utils import clean
# from .grammar import Grammar
class ParseTypes(Enum):
LINK = p.LinkP
TEMPLATE = p.TemplateP
class Compiler:
def __init__(self):
self.writer = StringIO()
# self.grammar = Grammar()
self.parser = p.Parser()
def render(self, node):
""" Render function """
self.writer = StringIO()
node.compile(self.writer, self.parser)
result = self.writer.getvalue()
self.writer.close()
return result
def compile(self, text):
ast = self.parser.parse(text)
# print(ast)
return clean(self.render(ast))
def on(self, fn, parse_type):
return self.parser.on(fn, parse_type)