Create new parser (works for lists so far)

This commit is contained in:
Bartłomiej Pluta
2019-07-05 16:45:59 +02:00
parent 23e0f3f33e
commit ed73aa1ad1
32 changed files with 516 additions and 59 deletions

View File

@@ -8,7 +8,7 @@ from smnp.token.type import TokenType
# i potem sprawdzać przy wszystkich parent.pop(-1) czy pobrany z parenta element
# jest rzeczywiście wyrażeniem, bo teraz możliwe jest np. {}.fun()
def parseAccess(input, parent):
if input.current().type == TokenType.DOT:
if input.isCurrent(TokenType.DOT):
token = input.current()
input.ahead()

View File

@@ -1,2 +0,0 @@

View File

@@ -5,7 +5,7 @@ from smnp.token.type import TokenType
# asterisk -> expr '*' stmt
def parseAsterisk(expr, input, parent):
if input.hasMore() and input.current().type == TokenType.ASTERISK:
if input.hasMore() and input.isCurrent(TokenType.ASTERISK):
token = input.current()
input.ahead()

View File

@@ -12,7 +12,7 @@ def parseBlock(input, parent):
node = BlockNode(parent, token.pos)
# '}'
if input.hasCurrent() and input.current().type == TokenType.CLOSE_BRACKET:
if input.isCurrent(TokenType.CLOSE_BRACKET):
input.ahead()
return node
@@ -28,7 +28,7 @@ def parseBlock(input, parent):
# blockItem -> stmt | '}'
def parseBlockItem(input, parent):
# '}'
if input.hasCurrent() and input.current().type == TokenType.CLOSE_BRACKET:
if input.isCurrent(TokenType.CLOSE_BRACKET):
close = CloseBlockNode(parent, input.current().pos)
input.ahead()
return close

View File

@@ -6,7 +6,7 @@ from smnp.token.type import TokenType
# colon -> expr ':' expr
def parseColon(expr1, input, parent):
if input.hasCurrent() and input.current().type == TokenType.COLON:
if input.isCurrent(TokenType.COLON):
token = input.current()
input.ahead()
expr2 = parseExpression(input, parent)

View File

@@ -7,7 +7,7 @@ from smnp.token.type import TokenType
def parseFunctionDefinition(input, parent):
if input.current().type == TokenType.FUNCTION:
if input.isCurrent(TokenType.FUNCTION):
token = input.current()
input.ahead()

View File

@@ -3,12 +3,13 @@ from smnp.ast.node.function import FunctionCallNode
from smnp.ast.node.identifier import IdentifierNode
from smnp.ast.parsers.expression import parseExpression
from smnp.ast.parsers.list import parseList
from smnp.ast.tools import greedy
from smnp.token.type import TokenType
# id -> IDENTIFIER
def parseIdentifier(input, parent):
if input.current().type == TokenType.IDENTIFIER:
if input.isCurrent(TokenType.IDENTIFIER):
identifier = IdentifierNode(input.current().value, parent, input.current().pos)
input.ahead()
@@ -28,7 +29,7 @@ def parseIdentifierOrFunctionCallOrAssignment(input, parent):
token = input.current()
input.ahead()
expr = parseExpression(input, parent)
expr = greedy(parseExpression)(input, parent)
assignment = AssignmentNode(identifier, expr, parent, token.pos)
identifier.parent = assignment

View File

@@ -5,7 +5,7 @@ from smnp.token.type import TokenType
# int -> INTEGER
def parseInteger(input, parent):
if input.current().type == TokenType.INTEGER:
if input.isCurrent(TokenType.INTEGER):
integer = IntegerLiteralNode(int(input.current().value), parent, input.current().pos)
input.ahead()
@@ -17,7 +17,7 @@ def parseInteger(input, parent):
# int -> int
def parseIntegerAndPercent(input, parent):
integer = parseInteger(input, parent)
if integer is not None and input.hasCurrent() and input.current().type == TokenType.PERCENT:
if integer is not None and input.isCurrent(TokenType.PERCENT):
percent = PercentNode(integer, parent, input.current().pos)
integer.parent = percent
input.ahead()

View File

@@ -1,17 +1,17 @@
from smnp.ast.node.list import ListNode, ListItemNode, CloseListNode
from smnp.ast.parsers.expression import parseExpression
from smnp.ast.tools import rollup, assertToken
from smnp.ast.tools import greedy, assertToken
from smnp.token.type import TokenType
# list -> CLOSE_PAREN | expr listTail
def parseList(input, parent):
if input.current().type == TokenType.OPEN_PAREN:
if input.isCurrent(TokenType.OPEN_PAREN):
node = ListNode(parent, input.current().pos)
input.ahead()
# list -> CLOSE_PAREN (end of list)
if input.hasCurrent() and input.current().type == TokenType.CLOSE_PAREN:
if input.isCurrent(TokenType.CLOSE_PAREN):
close = CloseListNode(node, input.current().pos)
node.append(close)
input.ahead()
@@ -20,7 +20,7 @@ def parseList(input, parent):
# list -> expr listTail
if input.hasCurrent():
token = input.current()
expr = parseExpression(input, node)
expr = greedy(parseExpression)(input, parent)
item = ListItemNode(expr, node, token.pos)
expr.parent = item
node.append(item)
@@ -33,7 +33,7 @@ def parseList(input, parent):
# listTail -> COMMA expr listTail | CLOSE_PAREN
def parseListTail(input, parent):
# listTail -> CLOSE_PAREN
if input.hasCurrent() and input.current().type == TokenType.CLOSE_PAREN:
if input.isCurrent(TokenType.CLOSE_PAREN):
close = CloseListNode(parent, input.current().pos)
input.ahead()
return close
@@ -42,7 +42,7 @@ def parseListTail(input, parent):
if input.hasCurrent() and input.hasMore():
assertToken(TokenType.COMMA, input)
input.ahead()
expr = rollup(parseExpression)(input, parent)
expr = greedy(parseExpression)(input, parent)
if expr is not None:
item = ListItemNode(expr, parent, expr.pos)
expr.parent = item

View File

@@ -5,10 +5,13 @@ from smnp.token.type import TokenType
# minus -> '-' int
def parseMinus(input, parent):
if input.current().type == TokenType.MINUS:
if input.isCurrent(TokenType.MINUS):
token = input.current()
input.ahead()
expr = parseInteger(input, parent)
if input.hasCurrent():
expr = parseInteger(input, parent)
return IntegerLiteralNode(-expr.value, parent, token.pos)
return IntegerLiteralNode(-expr.value, parent, token.pos)
return None

View File

@@ -7,7 +7,7 @@ from smnp.token.type import TokenType
# note -> NOTE
def parseNote(input, parent):
if input.current().type == TokenType.NOTE:
if input.isCurrent(TokenType.NOTE):
token = input.current()
value = token.value
consumedChars = 1

View File

@@ -4,7 +4,7 @@ from smnp.token.type import TokenType
def parseReturn(input, parent):
if input.current().type == TokenType.RETURN:
if input.isCurrent(TokenType.RETURN):
token = input.current()
input.ahead()

View File

@@ -4,7 +4,7 @@ from smnp.token.type import TokenType
# string -> STRING
def parseString(input, parent):
if input.current().type == TokenType.STRING:
if input.isCurrent(TokenType.STRING):
string = StringLiteralNode(input.current().value[1:len(input.current().value) - 1], parent, input.current().pos)
input.ahead()

View File

@@ -2,7 +2,7 @@ from smnp.ast.node.model import Node
from smnp.error.syntax import SyntaxException
def rollup(parser):
def greedy(parser):
def _rollup(input, parent):
node = Node(None, (-1, -1))
elem = parser(input, node)