Do some refactor with multiple left associative operators
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from smnp.ast.node.expression import ExpressionNode
|
||||
from smnp.ast.node.ignore import IgnoredNode
|
||||
from smnp.ast.node.none import NoneNode
|
||||
from smnp.ast.parser import Parser
|
||||
from smnp.error.syntax import SyntaxException
|
||||
from smnp.token.type import TokenType
|
||||
@@ -8,7 +8,7 @@ from smnp.token.type import TokenType
|
||||
class AccessNode(ExpressionNode):
|
||||
def __init__(self, pos):
|
||||
super().__init__(pos)
|
||||
self.children.append(IgnoredNode(pos))
|
||||
self.children = [ NoneNode(), NoneNode() ]
|
||||
|
||||
@property
|
||||
def left(self):
|
||||
@@ -28,29 +28,29 @@ class AccessNode(ExpressionNode):
|
||||
|
||||
@classmethod
|
||||
def accessParser(cls):
|
||||
def createNode(left, right):
|
||||
def createNode(left, operator, right):
|
||||
node = AccessNode(right.pos)
|
||||
node.left = left
|
||||
node.right = right
|
||||
return node
|
||||
|
||||
return Parser.leftAssociativeOperatorParser(
|
||||
cls._accessLiteralParser(),
|
||||
cls._accessLhs(),
|
||||
TokenType.DOT,
|
||||
cls._parseAccessingProperty(),
|
||||
cls._accessRhs(),
|
||||
createNode=createNode
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _accessLiteralParser(cls):
|
||||
raise RuntimeError(f"_accessLiteralParser() is not implemented in {cls.__name__} class")
|
||||
def _accessLhs(cls):
|
||||
raise RuntimeError(f"_accessLhs() is not implemented in {cls.__name__} class")
|
||||
|
||||
@staticmethod
|
||||
def _parseAccessingProperty():
|
||||
def _accessRhs():
|
||||
from smnp.ast.node.identifier import IdentifierNode
|
||||
|
||||
return Parser.oneOf(
|
||||
IdentifierNode.functionCallParser(),
|
||||
IdentifierNode.identifierParser(),
|
||||
IdentifierNode._functionCallParser(),
|
||||
exception=lambda input: SyntaxException(f"Expected property name or method call, found '{input.current().rawValue}'", input.currentPos())
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user