diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-02-25 18:48:53 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-02-25 18:48:53 +0000 |
| commit | e7d00f5a62e0f03161dbc12939840d332dc76506 (patch) | |
| tree | c0f76daa9549e18f5808b057d27d8e78c51293cb /src | |
| parent | 5396117397cda16a4ab23e6c098253af20f085ff (diff) | |
Parse type level params in function signature
Diffstat (limited to 'src')
4 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java index 641a9411..42795805 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java @@ -5,9 +5,11 @@ import org.pcollections.PVector; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; +import java.util.List; import java.util.Optional; public record UntypedFunctionSignatureNode( + Optional<List<UntypedTypeLevelParamNode>> typeLevelParams, UntypedParamsNode params, Optional<UntypedTypeLevelExpressionNode> effect, Optional<UntypedTypeLevelExpressionNode> returnType, @@ -15,6 +17,7 @@ public record UntypedFunctionSignatureNode( ) implements UntypedNode { public static Builder builder() { return new Builder( + Optional.empty(), Empty.vector(), Empty.vector(), Optional.empty(), @@ -24,6 +27,7 @@ public record UntypedFunctionSignatureNode( } public record Builder( + Optional<List<UntypedTypeLevelParamNode>> typeLevelParams, PVector<UntypedParamNode> positionalParams, PVector<UntypedParamNode> namedParams, Optional<UntypedTypeLevelExpressionNode> effect, @@ -32,6 +36,7 @@ public record UntypedFunctionSignatureNode( ) { public UntypedFunctionSignatureNode build() { return new UntypedFunctionSignatureNode( + typeLevelParams, new UntypedParamsNode(positionalParams, namedParams, Sources.NULL), effect, returnType, @@ -49,6 +54,7 @@ public record UntypedFunctionSignatureNode( ) { var paramNode = new UntypedParamNode(name, type, Sources.NULL); return new Builder( + this.typeLevelParams, this.positionalParams.plus(paramNode), this.namedParams, this.effect, @@ -64,6 +70,7 @@ public record UntypedFunctionSignatureNode( public Builder addNamedParam(String name, Optional<UntypedTypeLevelExpressionNode> type) { var paramNode = new UntypedParamNode(name, type, Sources.NULL); return new Builder( + this.typeLevelParams, this.positionalParams, this.namedParams.plus(paramNode), this.effect, @@ -74,6 +81,7 @@ public record UntypedFunctionSignatureNode( public Builder withEffect(UntypedTypeLevelExpressionNode effect) { return new Builder( + this.typeLevelParams, this.positionalParams, this.namedParams, Optional.of(effect), @@ -84,6 +92,7 @@ public record UntypedFunctionSignatureNode( public Builder withReturnType(UntypedTypeLevelExpressionNode returnType) { return new Builder( + this.typeLevelParams, this.positionalParams, this.namedParams, this.effect, @@ -94,6 +103,7 @@ public record UntypedFunctionSignatureNode( public Builder withoutReturnType() { return new Builder( + this.typeLevelParams, this.positionalParams, this.namedParams, this.effect, @@ -104,6 +114,7 @@ public record UntypedFunctionSignatureNode( public Builder addStatement(UntypedFunctionStatementNode statement) { return new Builder( + this.typeLevelParams, this.positionalParams, this.namedParams, this.effect, diff --git a/src/main/java/org/shedlang/compiler/parser/Parser.java b/src/main/java/org/shedlang/compiler/parser/Parser.java index aed9d79d..73f9cb23 100644 --- a/src/main/java/org/shedlang/compiler/parser/Parser.java +++ b/src/main/java/org/shedlang/compiler/parser/Parser.java @@ -501,6 +501,8 @@ public class Parser { ) { var start = tokens.position(); + var typeLevelParams = parseTypeLevelParamsOptional(tokens); + tokens.skip(TokenType.SYMBOL_PAREN_OPEN); var params = parseParams(tokens); tokens.skip(TokenType.SYMBOL_PAREN_CLOSE); @@ -513,6 +515,7 @@ public class Parser { var source = start.to(end); return new UntypedFunctionSignatureNode( + typeLevelParams, params, effect, returnType, diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java index 8cd1c638..2059f506 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java @@ -5,6 +5,7 @@ import org.pcollections.PVector; import org.shedlang.compiler.testing.MatcherBuilder; import org.zwobble.precisely.Matcher; +import java.util.List; import java.util.Optional; import static org.zwobble.precisely.Matchers.*; @@ -59,4 +60,10 @@ public class UntypedFunctionSignatureNodeMatcher extends MatcherBuilder< ) { return with(has("returnType", x -> x.returnType(), returnType)); } + + public UntypedFunctionSignatureNodeMatcher withTypeLevelParams( + Matcher<Optional<? extends List<UntypedTypeLevelParamNode>>> typeLevelParams + ) { + return with(has("typeLevelParams", x -> x.typeLevelParams(), typeLevelParams)); + } } diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java index f26efa64..1e639069 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java @@ -210,4 +210,46 @@ public class ParserFunctionSignatureTests { .withoutReturnType() ); } + + @Test + public void canParseAbsentTypeLevelParams() { + var source = "() -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withTypeLevelParams(isOptionalEmpty()) + ); + } + + @Test + public void canParseSingleTypeLevelParam() { + var source = "[A]() -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("A") + ))) + ); + } + + @Test + public void canParseManyTypeLevelParams() { + var source = "[A, B, C]() -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("A"), + isUntypedTypeLevelParamNode("B"), + isUntypedTypeLevelParamNode("C") + ))) + ); + } } |
