Clean code
This commit is contained in:
@@ -9,11 +9,9 @@ class Block(Node):
|
||||
|
||||
|
||||
def BlockParser(input):
|
||||
parser = Parser.loop(
|
||||
return Parser.loop(
|
||||
Parser.terminalParser(TokenType.OPEN_CURLY),
|
||||
Parser.doAssert(StatementParser, f"statement or '{TokenType.CLOSE_CURLY.key}'"),
|
||||
Parser.terminalParser(TokenType.CLOSE_CURLY),
|
||||
createNode=lambda open, statements, close: Block.withChildren(statements, open.pos)
|
||||
)
|
||||
|
||||
return Parser(parser, "block", [parser])(input)
|
||||
)(input)
|
||||
|
||||
@@ -10,12 +10,18 @@ from smnp.token.type import TokenType
|
||||
class Chain(Valuable):
|
||||
pass
|
||||
|
||||
itemParser = Parser.oneOf(
|
||||
ListParser,
|
||||
MapParser,
|
||||
AtomParser,
|
||||
)
|
||||
|
||||
ChainParser = Parser.leftAssociativeOperatorParser(itemParser, [TokenType.DOT], itemParser,
|
||||
lambda left, op, right: Chain.withValue(BinaryOperator.withValues(left, op, right)))
|
||||
def ChainParser(input):
|
||||
itemParser = Parser.oneOf(
|
||||
ListParser,
|
||||
MapParser,
|
||||
AtomParser,
|
||||
)
|
||||
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
itemParser,
|
||||
[TokenType.DOT],
|
||||
itemParser,
|
||||
lambda left, op, right: Chain.withValue(BinaryOperator.withValues(left, op, right))
|
||||
)(input)
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ class Expression(Valuable):
|
||||
|
||||
|
||||
def ExpressionParser(input):
|
||||
from smnp.ast.node.condition import IfElse
|
||||
|
||||
expr1 = Parser.leftAssociativeOperatorParser(
|
||||
TermParser,
|
||||
[TokenType.PLUS, TokenType.MINUS],
|
||||
|
||||
@@ -9,5 +9,10 @@ class List(Node):
|
||||
|
||||
def ListParser(input):
|
||||
from smnp.ast.node.expression import ExpressionParser
|
||||
return abstractIterableParser(List, TokenType.OPEN_SQUARE, TokenType.CLOSE_SQUARE,
|
||||
ExpressionParser)(input)
|
||||
|
||||
return abstractIterableParser(
|
||||
List,
|
||||
TokenType.OPEN_SQUARE,
|
||||
TokenType.CLOSE_SQUARE,
|
||||
ExpressionParser
|
||||
)(input)
|
||||
|
||||
@@ -41,6 +41,10 @@ def MapParser(input):
|
||||
createNode=MapEntry.withValues
|
||||
)
|
||||
|
||||
mapParser = abstractIterableParser(Map, TokenType.OPEN_CURLY, TokenType.CLOSE_CURLY, mapEntryParser)
|
||||
return abstractIterableParser(
|
||||
Map,
|
||||
TokenType.OPEN_CURLY,
|
||||
TokenType.CLOSE_CURLY,
|
||||
mapEntryParser
|
||||
)(input)
|
||||
|
||||
return Parser(mapParser, "map", [mapParser])(input)
|
||||
|
||||
@@ -68,68 +68,7 @@ class BinaryOperator(Node):
|
||||
node.right = right
|
||||
return node
|
||||
|
||||
#
|
||||
# class LeftAssociativeOperatorNode(ExpressionNode):
|
||||
# def __init__(self, pos):
|
||||
# super().__init__(pos)
|
||||
# self.children = [NoneNode(), NoneNode(), NoneNode()]
|
||||
#
|
||||
# @property
|
||||
# def left(self):
|
||||
# return self[0]
|
||||
#
|
||||
# @left.setter
|
||||
# def left(self, value):
|
||||
# self[0] = value
|
||||
#
|
||||
# @property
|
||||
# def operator(self):
|
||||
# return self[1]
|
||||
#
|
||||
# @operator.setter
|
||||
# def operator(self, value):
|
||||
# self[1] = value
|
||||
#
|
||||
# @property
|
||||
# def right(self):
|
||||
# return self[2]
|
||||
#
|
||||
# @right.setter
|
||||
# def right(self, value):
|
||||
# self[2] = value
|
||||
#
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def createNode(left, operator, right):
|
||||
# node = LeftAssociativeOperatorNode(right.pos)
|
||||
# node.left = left
|
||||
# node.operator = operator
|
||||
# node.right = right
|
||||
# return node
|
||||
#
|
||||
# return Parser.leftAssociativeOperatorParser(
|
||||
# cls._lhsParser(),
|
||||
# TokenType.DOT,
|
||||
# cls._rhsParser(),
|
||||
# createNode=createNode
|
||||
# )(input)
|
||||
#
|
||||
# @classmethod
|
||||
# def _lhsParser(cls):
|
||||
# raise RuntimeError(f"LHS parser is not implemented in {cls.__name__}")
|
||||
#
|
||||
# @staticmethod
|
||||
# def _rhsParser():
|
||||
# from smnp.ast.node.identifier import IdentifierNode
|
||||
#
|
||||
# return Parser.oneOf(
|
||||
# # TODO!!!
|
||||
# IdentifierNode._lhsParser(),
|
||||
# IdentifierNode._functionCallParser(),
|
||||
# exception=lambda input: SyntaxException(f"Expected property name or method call, found '{input.current().rawValue}'", input.currentPos())
|
||||
# )
|
||||
#
|
||||
#
|
||||
|
||||
class Operator(Node):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
|
||||
@@ -8,22 +8,23 @@ class Program(Node):
|
||||
def __init__(self):
|
||||
super().__init__((-1, -1))
|
||||
|
||||
def parse(input):
|
||||
root = Program()
|
||||
while input.hasCurrent():
|
||||
result = Parser.oneOf(
|
||||
# Start Symbol
|
||||
ImportParser,
|
||||
StatementParser,
|
||||
exception=RuntimeError("Nie znam tego wyrazenia")
|
||||
)(input)
|
||||
def ProgramParser(input):
|
||||
def parse(input):
|
||||
root = Program()
|
||||
while input.hasCurrent():
|
||||
result = Parser.oneOf(
|
||||
# Start Symbol
|
||||
ImportParser,
|
||||
StatementParser,
|
||||
exception=RuntimeError("Nie znam tego wyrazenia")
|
||||
)(input)
|
||||
|
||||
if result.result:
|
||||
root.append(result.node)
|
||||
if result.result:
|
||||
root.append(result.node)
|
||||
|
||||
return ParseResult.OK(root)
|
||||
return ParseResult.OK(root)
|
||||
|
||||
ProgramParser = Parser(parse, name="program")
|
||||
return Parser(parse, name="program")(input)
|
||||
# @classmethod
|
||||
# def _parse(cls, input):
|
||||
# def parseToken(input):
|
||||
|
||||
@@ -8,5 +8,11 @@ from smnp.token.type import TokenType
|
||||
class Term(Valuable):
|
||||
pass
|
||||
|
||||
TermParser = Parser.leftAssociativeOperatorParser(FactorParser, [TokenType.ASTERISK, TokenType.SLASH], FactorParser,
|
||||
lambda left, op, right: Term.withValue(BinaryOperator.withValues(left, op, right)))
|
||||
|
||||
def TermParser(input):
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
FactorParser,
|
||||
[TokenType.ASTERISK, TokenType.SLASH],
|
||||
FactorParser,
|
||||
lambda left, op, right: Term.withValue(BinaryOperator.withValues(left, op, right))
|
||||
)(input)
|
||||
Reference in New Issue
Block a user