Add support for identifiers, functions, properties and methods calls

This commit is contained in:
Bartłomiej Pluta
2019-07-05 22:35:19 +02:00
parent ec3675ac43
commit 15dc909824
5 changed files with 82 additions and 3 deletions

View File

@@ -46,6 +46,9 @@ class AccessNode(ExpressionNode):
@staticmethod
def _parseAccessingProperty():
from smnp.newast.node.integer import IntegerLiteralNode
# TODO: Just for example. It is supposed to be functionCall (and identifier there)
return IntegerLiteralNode._literalParser()
from smnp.newast.node.identifier import IdentifierNode
return Parser.oneOf(
IdentifierNode._literalParser(),
IdentifierNode._functionCallParser()
)

11
smnp/newast/node/args.py Normal file
View File

@@ -0,0 +1,11 @@
from smnp.newast.node.expression import ExpressionNode
from smnp.newast.node.iterable import abstractIterableParser
from smnp.newast.node.model import Node
from smnp.token.type import TokenType
class ArgumentsListNode(Node):
@classmethod
def _parse(cls, input):
return abstractIterableParser(ArgumentsListNode, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, ExpressionNode.parse)(input)

View File

@@ -29,11 +29,13 @@ class ExpressionNode(Node):
from smnp.newast.node.integer import IntegerLiteralNode
from smnp.newast.node.string import StringLiteralNode
from smnp.newast.node.note import NoteLiteralNode
from smnp.newast.node.identifier import IdentifierNode
from smnp.newast.node.list import ListNode
return Parser.oneOf(
IntegerLiteralNode.parse,
StringLiteralNode.parse,
NoteLiteralNode.parse,
IdentifierNode.parse,
ListNode.parse
)(input)

View File

@@ -0,0 +1,36 @@
from smnp.newast.node.access import AccessNode
from smnp.newast.node.args import ArgumentsListNode
from smnp.newast.node.invocation import FunctionCall
from smnp.newast.parser import Parser
from smnp.token.type import TokenType
class IdentifierNode(AccessNode):
def __init__(self, pos):
super().__init__(pos)
del self.children[1]
@classmethod
def _literalParser(cls):
return Parser.oneOf(
IdentifierNode._functionCallParser(),
IdentifierNode._identifierParser()
)
@staticmethod
def _functionCallParser():
def createNode(name, arguments):
node = FunctionCall(name.pos)
node.name = name
node.arguments = arguments
return node
return Parser.allOf(
IdentifierNode._identifierParser(),
ArgumentsListNode.parse,
createNode=createNode
)
@staticmethod
def _identifierParser():
return Parser.terminalParser(TokenType.IDENTIFIER, lambda val, pos: IdentifierNode.withValue(val, pos))

View File

@@ -0,0 +1,27 @@
from smnp.newast.node.access import AccessNode
class FunctionCall(AccessNode):
def __init__(self, pos):
super().__init__(pos)
@property
def name(self):
return self[0]
@name.setter
def name(self, value):
self[0] = value
@property
def arguments(self):
return self[1]
@arguments.setter
def arguments(self, value):
self[1] = value
@classmethod
def _parse(cls, input):
raise RuntimeError("This class is not supposed to be automatically called")