From 244cc7ad6855696891cd8c72bfedb68c91fd1aaa Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 7 Jun 2023 20:03:39 +0100 Subject: Add parser for minimal functions --- .../ast/UntypedFunctionDefinitionNodeMatcher.java | 50 ++++++++++++++++++++++ .../shedlang/compiler/ast/UntypedNodeMatchers.java | 14 ++++++ .../parser/ParserFunctionDefinitionTests.java | 29 +++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java new file mode 100644 index 00000000..775cf5f5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java @@ -0,0 +1,50 @@ +package org.shedlang.compiler.ast; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.zwobble.precisely.Matcher; + +import java.util.Optional; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedFunctionDefinitionNodeMatcher extends MatcherBuilder { + public UntypedFunctionDefinitionNodeMatcher() { + super(); + } + + public UntypedFunctionDefinitionNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedFunctionDefinitionNode.class; + } + + @Override + protected UntypedFunctionDefinitionNodeMatcher create(PVector> submatchers) { + return new UntypedFunctionDefinitionNodeMatcher(); + } + + public UntypedFunctionDefinitionNodeMatcher withBody(Matcher> body) { + return with(has("body", x -> x.body(), body)); + } + + public UntypedFunctionDefinitionNodeMatcher withEffect(Matcher> effect) { + return with(has("effect", x -> x.effect(), effect)); + } + + public UntypedFunctionDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedFunctionDefinitionNodeMatcher withParams(Matcher params) { + return with(has("params", x -> x.params(), params)); + } + + public UntypedFunctionDefinitionNodeMatcher withReturnType(Matcher returnType) { + return with(has("returnType", x -> x.returnType(), returnType)); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java index a6f0bf82..ca538a97 100644 --- a/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java @@ -25,6 +25,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedFunctionDefinitionNodeMatcher isUntypedFunctionDefinitionNode() { + return new UntypedFunctionDefinitionNodeMatcher(); + } + public static Matcher isUntypedNamedArgNode( String name, Matcher expression @@ -35,6 +39,16 @@ public class UntypedNodeMatchers { ); } + public static Matcher isUntypedParamsNode( + Matcher> positional, + Matcher> named + ) { + return allOf( + has("positional", x -> x.positional(), positional), + has("named", x -> x.named(), named) + ); + } + public static Matcher isUntypedReferenceNode(String name) { return instanceOf( UntypedReferenceNode.class, diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java new file mode 100644 index 00000000..34c50065 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java @@ -0,0 +1,29 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; + +import static org.shedlang.compiler.ast.UntypedNodeMatchers.*; +import static org.shedlang.compiler.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ParserFunctionDefinitionTests { + @Test + public void canParseMinimalFunction() { + var source = """ + fun f() -> Unit { + }"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedFunctionDefinitionNode() + .withName("f") + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + .withBody(isSequence()) + ); + } +} -- cgit v1.2.3