From a7c33eef7d2598d5636ff5bcde07da4f9c057184 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 23 Dec 2023 11:50:33 +0000 Subject: Parse type level params for sum types --- .../ast/untyped/UntypedSumTypeDefinitionNode.java | 13 ++++- .../java/org/shedlang/compiler/parser/Parser.java | 10 +++- .../UntypedSumTypeDefinitionNodeMatcher.java | 8 +++ .../parser/ParserSumTypeDefinitionTests.java | 64 +++++++++++++++++++++- 4 files changed, 91 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java index a478e588..d727b98e 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java @@ -7,26 +7,35 @@ import org.shedlang.compiler.types.Visibility; import org.shedlang.compiler.util.P; import java.util.List; +import java.util.Optional; public record UntypedSumTypeDefinitionNode( String name, Visibility visibility, + Optional> typeLevelParams, List variants, Source source ) implements UntypedNamespaceStatementNode, UntypedVariableDeclarationNode { public static Builder builder() { - return new Builder("DefaultSumTypeName", Visibility.PRIVATE, P.vector()); + return new Builder( + "DefaultSumTypeName", + Visibility.PRIVATE, + Optional.empty(), + P.vector() + ); } public record Builder( String name, Visibility visibility, + Optional> typeLevelParams, PVector variants ) { public UntypedSumTypeDefinitionNode build() { return new UntypedSumTypeDefinitionNode( this.name, this.visibility, + this.typeLevelParams, this.variants, Sources.NULL ); @@ -36,6 +45,7 @@ public record UntypedSumTypeDefinitionNode( return new Builder( name, this.visibility, + this.typeLevelParams, this.variants ); } @@ -44,6 +54,7 @@ public record UntypedSumTypeDefinitionNode( return new Builder( this.name, this.visibility, + this.typeLevelParams, this.variants.plus(variant) ); } diff --git a/src/main/java/org/shedlang/compiler/parser/Parser.java b/src/main/java/org/shedlang/compiler/parser/Parser.java index 9fb8a93b..753a6d75 100644 --- a/src/main/java/org/shedlang/compiler/parser/Parser.java +++ b/src/main/java/org/shedlang/compiler/parser/Parser.java @@ -968,6 +968,8 @@ public class Parser { var name = tokens.nextValue(TokenType.IDENTIFIER); + var typeLevelParams = parseTypeLevelParamsOptional(tokens); + tokens.skip(TokenType.SYMBOL_EQUALS); var variants = parseOneOrMoreNoTrailingSeparator( @@ -980,7 +982,13 @@ public class Parser { var end = tokens.position(); var source = start.to(end); - return new UntypedSumTypeDefinitionNode(name, visibility, variants, source); + return new UntypedSumTypeDefinitionNode( + name, + visibility, + typeLevelParams, + variants, + source + ); } static UntypedExpressionNode parseTopLevelExpression(TokenIterator tokens) { diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java index aa1ba8d0..42d30cff 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java @@ -6,6 +6,8 @@ import org.shedlang.compiler.types.Visibility; import org.shedlang.compiler.util.P; import org.zwobble.precisely.Matcher; +import java.util.Optional; + import static org.zwobble.precisely.Matchers.equalTo; import static org.zwobble.precisely.Matchers.has; @@ -40,6 +42,12 @@ public class UntypedSumTypeDefinitionNodeMatcher extends MatcherBuilder< return with(has("name", x -> x.name(), equalTo(name))); } + public UntypedSumTypeDefinitionNodeMatcher withTypeLevelParams( + Matcher>> typeLevelParams + ) { + return with(has("typeLevelParams", x -> x.typeLevelParams(), typeLevelParams)); + } + public UntypedSumTypeDefinitionNodeMatcher withVariants( Matcher> variants ) { diff --git a/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java index 32392389..eb8285aa 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java @@ -3,8 +3,8 @@ 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.isUntypedSumTypeDefinitionNode; -import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedTypeLevelReferenceNode; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedTypeLevelParamNode; import static org.shedlang.compiler.parser.ParserTesting.parseString; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -68,4 +68,64 @@ public class ParserSumTypeDefinitionTests { .withVisibility(Visibility.PUBLIC) ); } + + @Test + public void canParseSumTypeWithNoTypeLevelParams() { + var source = "sumtype Option = None;"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedSumTypeDefinitionNode() + .withTypeLevelParams(isOptionalEmpty()) + ); + } + + @Test + public void canParseSumTypeWithSingleTypeLevelParam() { + var source = "sumtype Option[T1] = None;"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedSumTypeDefinitionNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("T1") + ))) + ); + } + + @Test + public void canParseSumTypeWithMultipleTypeLevelParams() { + var source = "sumtype Option[T1, T2, T3] = None;"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedSumTypeDefinitionNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("T1"), + isUntypedTypeLevelParamNode("T2"), + isUntypedTypeLevelParamNode("T3") + ))) + ); + } + + @Test + public void typeLevelParamsCanHaveTrailingComma() { + var source = "sumtype Option[T1,] = None;"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedSumTypeDefinitionNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("T1") + ))) + ); + } } -- cgit v1.2.3