From 059d6276a4d2ef23672adb009e633a06f3603a8c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 27 Sep 2023 20:28:09 +0100 Subject: Parse struct definition fields --- .../compiler/ast/untyped/UntypedNodeMatchers.java | 4 +++ .../UntypedStructFieldDefinitionNodeMatcher.java | 37 ++++++++++++++++++++++ .../parser/ParserStructDefinitionTests.java | 25 ++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java (limited to 'src/test/java') 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 95d666cd..d08e784d 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -198,6 +198,10 @@ public class UntypedNodeMatchers { return UntypedStructDefinitionNodeMatcher.empty(); } + public static UntypedStructFieldDefinitionNodeMatcher isUntypedStructFieldDefinitionNode() { + return UntypedStructFieldDefinitionNodeMatcher.empty(); + } + public static Matcher isUntypedSubNode( Matcher left, Matcher right diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java new file mode 100644 index 00000000..2e0f1d63 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java @@ -0,0 +1,37 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.shedlang.compiler.util.P; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedStructFieldDefinitionNodeMatcher extends MatcherBuilder { + public static UntypedStructFieldDefinitionNodeMatcher empty() { + return new UntypedStructFieldDefinitionNodeMatcher(P.vector()); + } + + public UntypedStructFieldDefinitionNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedStructFieldDefinitionNode.class; + } + + @Override + protected UntypedStructFieldDefinitionNodeMatcher create(PVector> submatchers) { + return new UntypedStructFieldDefinitionNodeMatcher(submatchers); + } + + public UntypedStructFieldDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedStructFieldDefinitionNodeMatcher withType(Matcher type) { + return with(has("type", x -> x.type(), type)); + } +} diff --git a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java index 8a0fe6b6..3fd640b1 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java @@ -2,7 +2,7 @@ package org.shedlang.compiler.parser; import org.junit.jupiter.api.Test; -import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedStructDefinitionNode; +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.isSequence; @@ -23,4 +23,27 @@ public class ParserStructDefinitionTests { .withFields(isSequence()) ); } + + @Test + public void canParseStructWithFields() { + var source = """ + struct X(.name: String, .accessLevel: AccessLevel) {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withName("X") + .withFields(isSequence( + isUntypedStructFieldDefinitionNode() + .withName("name") + .withType(isUntypedTypeLevelReferenceNode("String")), + isUntypedStructFieldDefinitionNode() + .withName("accessLevel") + .withType(isUntypedTypeLevelReferenceNode("AccessLevel")) + )) + ); + } } -- cgit v1.2.3