Create ast package

This commit is contained in:
Bartłomiej Pluta
2019-07-03 11:27:51 +02:00
parent 2823fd1896
commit c8ff5ce38f
38 changed files with 622 additions and 11 deletions

30
smnp/ast/tools.py Normal file
View File

@@ -0,0 +1,30 @@
from smnp.ast.node.model import Node
from smnp.error.syntax import SyntaxException
def rollup(parser):
def _rollup(input, parent):
node = Node(None, (-1, -1))
elem = parser(input, node)
while elem is not None:
node.append(elem)
elem = parser(input, node)
return node.children[0] if len(node.children) > 0 else None
return _rollup
def assertToken(expected, input):
if not input.hasCurrent():
raise SyntaxException(None, f"Expected '{expected}'")
if expected != input.current().type:
raise SyntaxException(input.current().pos, f"Expected '{expected}', found '{input.current().value}'")
def combineParsers(parsers):
def combinedParser(input, parent):
for parser in parsers:
value = parser(input, parent)
if value is not None:
return value
return None
return combinedParser