From 1e6b65207e0fc3ead1edbd2bb56b1578cfc647ec Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 10 Dec 2024 23:29:42 +0000 Subject: Add basic parsing of positional struct fields --- .../org/shedlang/compiler/ast/untyped/Untyped.java | 2 +- .../UntypedStructFieldDefinitionNodeMatcher.java | 4 +++ .../parser/ParserStructDefinitionTests.java | 29 +++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java b/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java index 2eacfade..43e8154c 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java @@ -189,7 +189,7 @@ public class Untyped { String name, UntypedTypeLevelExpressionNode type ) { - return new UntypedStructFieldDefinitionNode(name, type, Sources.NULL); + return new UntypedStructFieldDefinitionNode(name, type, false, Sources.NULL); } public static UntypedBinaryOperationNode sub( diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java index 2e0f1d63..5ca0d994 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java @@ -34,4 +34,8 @@ public class UntypedStructFieldDefinitionNodeMatcher extends MatcherBuilder type) { return with(has("type", x -> x.type(), type)); } + + public UntypedStructFieldDefinitionNodeMatcher withIsPositional(boolean isPositional) { + return with(has("isPositional", x -> x.isPositional(), equalTo(isPositional))); + } } diff --git a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java index 903c4c52..4f0698ad 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java @@ -40,10 +40,37 @@ public class ParserStructDefinitionTests { .withFields(isSequence( isUntypedStructFieldDefinitionNode() .withName("name") - .withType(isUntypedTypeLevelReferenceNode("String")), + .withType(isUntypedTypeLevelReferenceNode("String")) + .withIsPositional(false), isUntypedStructFieldDefinitionNode() .withName("accessLevel") .withType(isUntypedTypeLevelReferenceNode("AccessLevel")) + .withIsPositional(false) + )) + ); + } + + @Test + public void canParseStructWithPositionalFields() { + 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")) + .withIsPositional(true), + isUntypedStructFieldDefinitionNode() + .withName("accessLevel") + .withType(isUntypedTypeLevelReferenceNode("AccessLevel")) + .withIsPositional(true) )) ); } -- cgit v1.2.3