From 6f554d8e265ad2664c97d9d8d6efef2f53e9eb5f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 12 Jan 2025 21:25:32 +0000 Subject: Parse native function declarations --- ...ntypedNativeFunctionDeclarationNodeMatcher.java | 52 +++++++++++++++++++ .../compiler/ast/untyped/UntypedNodeMatchers.java | 4 ++ .../ParserNativeFunctionDeclarationTests.java | 60 ++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java new file mode 100644 index 00000000..9eb338fd --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java @@ -0,0 +1,52 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.Empty; +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.shedlang.compiler.types.Visibility; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedNativeFunctionDeclarationNodeMatcher extends MatcherBuilder< + UntypedNamespaceStatementNode, + UntypedNativeFunctionDeclarationNode, + UntypedNativeFunctionDeclarationNodeMatcher +> { + public static UntypedNativeFunctionDeclarationNodeMatcher empty() { + return new UntypedNativeFunctionDeclarationNodeMatcher(Empty.vector()); + } + + public UntypedNativeFunctionDeclarationNodeMatcher( + PVector> submatchers + ) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedNativeFunctionDeclarationNode.class; + } + + @Override + protected UntypedNativeFunctionDeclarationNodeMatcher create( + PVector> submatchers + ) { + return new UntypedNativeFunctionDeclarationNodeMatcher(submatchers); + } + + public UntypedNativeFunctionDeclarationNodeMatcher withSignature( + Matcher signature + ) { + return with(has("signature", x -> x.signature(), signature)); + } + + public UntypedNativeFunctionDeclarationNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedNativeFunctionDeclarationNodeMatcher withVisibility(Visibility visibility) { + return with(has("visibility", x -> x.visibility(), equalTo(visibility))); + } +} 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 0e11231b..94fc68e1 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -235,6 +235,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedNativeFunctionDeclarationNodeMatcher isUntypedNativeFunctionDeclarationNode() { + return UntypedNativeFunctionDeclarationNodeMatcher.empty(); + } + public static Matcher isUntypedNotEqualNode( Matcher left, Matcher right diff --git a/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java b/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java new file mode 100644 index 00000000..f11a673c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java @@ -0,0 +1,60 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.types.Visibility; + +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.isOptionalEmpty; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ParserNativeFunctionDeclarationTests { + @Test + public void canParseMinimalFunction() { + var source = """ + native fun f() -> Unit;"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeFunctionDeclarationNode() + .withName("f") + .withSignature( + isUntypedFunctionSignatureNode() + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ) + ); + } + + @Test + public void defaultVisibilityIsPrivate() { + var source = """ + native fun f() -> Unit;"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeFunctionDeclarationNode() + .withVisibility(Visibility.PRIVATE) + ); + } + + @Test + public void whenFunIsPrefixedWithPubThenVisibilityIsPublic() { + var source = """ + pub native fun f() -> Unit;"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeFunctionDeclarationNode() + .withVisibility(Visibility.PUBLIC) + ); + } +} -- cgit v1.2.3