Create parser for chain (dot operator)
This commit is contained in:
@@ -1,2 +1,11 @@
|
|||||||
|
from smnp.ast.node.atom import AtomParser
|
||||||
|
from smnp.ast.node.operator import BinaryOperator
|
||||||
|
from smnp.ast.parser import Parser
|
||||||
|
from smnp.token.type import TokenType
|
||||||
|
|
||||||
|
|
||||||
|
class Chain(BinaryOperator):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
ChainParser = Parser.leftAssociativeOperatorParser(AtomParser, [TokenType.DOT], AtomParser, lambda left, op, right: Chain.withValues(left, op, right))
|
||||||
@@ -6,6 +6,44 @@ from smnp.error.syntax import SyntaxException
|
|||||||
from smnp.token.type import TokenType
|
from smnp.token.type import TokenType
|
||||||
|
|
||||||
|
|
||||||
|
class BinaryOperator(Node):
|
||||||
|
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 withValues(cls, left, operator, right):
|
||||||
|
node = cls(operator.pos)
|
||||||
|
node.left = left
|
||||||
|
node.operator = operator
|
||||||
|
node.right = right
|
||||||
|
return node
|
||||||
|
|
||||||
|
|
||||||
class LeftAssociativeOperatorNode(ExpressionNode):
|
class LeftAssociativeOperatorNode(ExpressionNode):
|
||||||
def __init__(self, pos):
|
def __init__(self, pos):
|
||||||
super().__init__(pos)
|
super().__init__(pos)
|
||||||
@@ -67,7 +105,7 @@ class LeftAssociativeOperatorNode(ExpressionNode):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class OperatorNode(Node):
|
class Operator(Node):
|
||||||
def __init__(self, pos):
|
def __init__(self, pos):
|
||||||
super().__init__(pos)
|
super().__init__(pos)
|
||||||
self.children = [None]
|
self.children = [None]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from smnp.ast.node.atom import AtomParser
|
from smnp.ast.node.chain import ChainParser
|
||||||
from smnp.ast.node.model import Node, ParseResult
|
from smnp.ast.node.model import Node, ParseResult
|
||||||
from smnp.ast.parser import Parser
|
from smnp.ast.parser import Parser
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ def parse(input):
|
|||||||
|
|
||||||
|
|
||||||
#TODO -> temporary (to remove):
|
#TODO -> temporary (to remove):
|
||||||
AtomParser
|
ChainParser
|
||||||
)(input)
|
)(input)
|
||||||
|
|
||||||
if result.result:
|
if result.result:
|
||||||
|
|||||||
@@ -122,10 +122,10 @@ class Parser:
|
|||||||
# leftAssociative -> left | left OP right
|
# leftAssociative -> left | left OP right
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def leftAssociativeOperatorParser(leftParser, operatorTokenTypes, rightParser, createNode, name="leftAssoc"):
|
def leftAssociativeOperatorParser(leftParser, operatorTokenTypes, rightParser, createNode, name="leftAssoc"):
|
||||||
from smnp.ast.node.operator import OperatorNode
|
from smnp.ast.node.operator import Operator
|
||||||
|
|
||||||
def parse(input):
|
def parse(input):
|
||||||
operatorParser = Parser.oneOfTerminals(*operatorTokenTypes, createNode=lambda val, pos: OperatorNode.withChildren([val], pos))
|
operatorParser = Parser.oneOfTerminals(*operatorTokenTypes, createNode=lambda val, pos: Operator.withChildren([val], pos))
|
||||||
left = leftParser(input)
|
left = leftParser(input)
|
||||||
if left.result:
|
if left.result:
|
||||||
operator = operatorParser(input)
|
operator = operatorParser(input)
|
||||||
|
|||||||
Reference in New Issue
Block a user