From d1d0984bc8a157a2f001d3c991dae5c9498cd790 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Oct 2023 22:23:01 +0100 Subject: Parse function types --- .../untyped/UntypedFunctionTypeNodeMatcher.java | 48 ++++++ .../compiler/ast/untyped/UntypedNodeMatchers.java | 14 ++ .../compiler/parser/ParserFunctionTypeTests.java | 168 +++++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionTypeNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionTypeNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionTypeNodeMatcher.java new file mode 100644 index 00000000..166dd1b0 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionTypeNodeMatcher.java @@ -0,0 +1,48 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.Empty; +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.has; + +public class UntypedFunctionTypeNodeMatcher extends MatcherBuilder< + UntypedTypeLevelExpressionNode, + UntypedFunctionTypeNode, + UntypedFunctionTypeNodeMatcher +> { + public static UntypedFunctionTypeNodeMatcher empty() { + return new UntypedFunctionTypeNodeMatcher(Empty.vector()); + } + + public UntypedFunctionTypeNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedFunctionTypeNode.class; + } + + @Override + protected UntypedFunctionTypeNodeMatcher create( + PVector> submatchers + ) { + return new UntypedFunctionTypeNodeMatcher(submatchers); + } + + public UntypedFunctionTypeNodeMatcher withEffect(Matcher> effect) { + return with(has("effect", x -> x.effect(), effect)); + } + + public UntypedFunctionTypeNodeMatcher withParams(Matcher params) { + return with(has("params", x -> x.params(), params)); + } + + public UntypedFunctionTypeNodeMatcher withReturnType(Matcher returnType) { + return with(has("returnType", x -> x.returnType(), returnType)); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java index 35e8247a..f45adba9 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -123,6 +123,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedFunctionTypeNodeMatcher isUntypedFunctionTypeNode() { + return UntypedFunctionTypeNodeMatcher.empty(); + } + public static Matcher isUntypedIfNode( Matcher> conditionalBranches, Matcher elseBody @@ -188,6 +192,16 @@ public class UntypedNodeMatchers { ); } + public static Matcher isUntypedParamTypesNode( + 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/ParserFunctionTypeTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java new file mode 100644 index 00000000..467930a3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java @@ -0,0 +1,168 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; +import static org.shedlang.compiler.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ParserFunctionTypeTests { + @Test + public void canParseFunctionTypeWithNoParamsAndNoEffect() { + var source = "fn () -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( + isSequence(), + isSequence() + )) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ); + } + + @Test + public void canParseFunctionWithSinglePositionalParam() { + var source = "fn (Int) -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( + isSequence( + isUntypedTypeLevelReferenceNode("Int") + ), + isSequence() + )) + ); + } + + @Test + public void canParseFunctionWithManyPositionalParams() { + var source = "fn (Int, String, Bool) -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( + isSequence( + isUntypedTypeLevelReferenceNode("Int"), + isUntypedTypeLevelReferenceNode("String"), + isUntypedTypeLevelReferenceNode("Bool") + ), + isSequence() + )) + ); + } + + @Test + public void canParseFunctionWithSingleNamedParam() { + var source = "fn (.a: Int) -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( + isSequence(), + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + ) + )) + ); + } + + @Test + public void canParseFunctionWithManyNamedParams() { + var source = "fn (.a: Int, .b: String, .c: Bool) -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( + isSequence(), + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), + isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), + isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) + ) + )) + ); + } + + @Test + public void canParseNamedParamAfterPositionalParam() { + var source = "fn (Int, .b: String) -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( + isSequence( + isUntypedTypeLevelReferenceNode("Int") + ), + isSequence( + isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")) + ) + )) + ); + } + + @Test + public void cannotParsePositionalParamAfterNamedParam() { + var source = "fn (.a: Int, String) -> Unit"; + + assertThrows( + PositionalParamAfterNamedParamError.class, + () -> parseString(source, Parser::parseTopLevelTypeLevelExpression) + ); + } + + @Test + public void paramsCanHaveTrailingComma() { + var source = """ + fun f(a: Int, ) -> Unit { + }"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedFunctionDefinitionNode() + .withParams(isUntypedParamsNode( + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + ), + isSequence() + )) + ); + } + + @Test + public void canParseFunctionTypeWithEffect() { + var source = "fn () ! Io -> Unit"; + + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); + + assertThat( + node, + isUntypedFunctionTypeNode() + .withEffect(isOptionalOf(isUntypedTypeLevelReferenceNode("Io"))) + ); + } +} -- cgit v1.2.3