Create assertions on import and function definition parsers

This commit is contained in:
Bartłomiej Pluta
2019-07-12 17:00:44 +02:00
parent b711b6a582
commit 0aad7e52dd
3 changed files with 10 additions and 8 deletions

View File

@@ -103,7 +103,7 @@ def ArgumentParser(input):
return Parser.allOf( return Parser.allOf(
Parser.optional(TypeParser), Parser.optional(TypeParser),
IdentifierLiteralParser, Parser.doAssert(IdentifierLiteralParser, "argument name"),
Parser.optional(Parser.terminal(TokenType.DOTS, lambda val, pos: True)), Parser.optional(Parser.terminal(TokenType.DOTS, lambda val, pos: True)),
createNode=createNode, createNode=createNode,
name="function argument" name="function argument"
@@ -115,24 +115,26 @@ def ArgumentsDeclarationParser(input):
ArgumentsDeclaration, ArgumentsDeclaration,
TokenType.OPEN_PAREN, TokenType.OPEN_PAREN,
TokenType.CLOSE_PAREN, TokenType.CLOSE_PAREN,
ArgumentParser Parser.doAssert(ArgumentParser, "function/method argument")
)(input) )(input)
def FunctionDefinitionParser(input): def FunctionDefinitionParser(input):
return Parser.allOf( return Parser.allOf(
Parser.terminal(TokenType.FUNCTION), Parser.terminal(TokenType.FUNCTION),
IdentifierLiteralParser, Parser.doAssert(IdentifierLiteralParser, "function/method name"),
ArgumentsDeclarationParser, Parser.doAssert(ArgumentsDeclarationParser, "function/method arguments"),
MethodBodyParser, Parser.doAssert(MethodBodyParser, "function/method body"),
createNode=lambda _, name, args, body: FunctionDefinition.withValues(name, args, body), createNode=lambda _, name, args, body: FunctionDefinition.withValues(name, args, body),
name="function definition" name="function definition"
)(input) )(input)
def MethodBodyParser(input): def MethodBodyParser(input):
bodyItem = Parser.oneOf( bodyItem = Parser.oneOf(
StatementParser,
ReturnParser, ReturnParser,
StatementParser,
assertExpected="statement",
name="function body item" name="function body item"
) )

View File

@@ -27,7 +27,7 @@ class Import(Node):
def ImportParser(input): def ImportParser(input):
return Parser.allOf( return Parser.allOf(
Parser.terminal(TokenType.IMPORT), Parser.terminal(TokenType.IMPORT),
StringParser, Parser.doAssert(StringParser, "import source as string"),
createNode=lambda imp, source: Import.withValue(source), createNode=lambda imp, source: Import.withValue(source),
name="import" name="import"
)(input) )(input)

View File

@@ -14,7 +14,7 @@ def noteTokenizer(input, current, line):
rawValue = '' rawValue = ''
if input[current] == '@': if input[current] == '@':
rawValue += input[current+consumedChars] rawValue += input[current+consumedChars]
consumedChars += 1 consumedChars += 1 # TODO: Check if next item does even exist
if input[current+consumedChars] in ('C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'A', 'a', 'H', 'h', 'B', 'b'): if input[current+consumedChars] in ('C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'A', 'a', 'H', 'h', 'B', 'b'):
rawValue += input[current + consumedChars] rawValue += input[current + consumedChars]
notePitch = input[current+consumedChars] notePitch = input[current+consumedChars]