summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java28
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNode.java38
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java29
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java6
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNodeMatcher.java47
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java55
6 files changed, 198 insertions, 5 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java
index 92501e4..43d642e 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java
@@ -7,32 +7,50 @@ package org.zwobble.hobgoblin.compiler.ast.untyped;
public record UntypedNativeTypeDefinitionNode(
String name,
+ java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams,
org.zwobble.hobgoblin.compiler.ast.DocComment docComment,
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder("", org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder("", java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
String name,
+ java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams,
org.zwobble.hobgoblin.compiler.ast.DocComment docComment,
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode.Builder {
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode build() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode(name, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode(name, typeParams, docComment, source);
}
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withName(String name) {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withTypeParams(java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder addTypeParam(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode typeParam) {
+ var typeParams = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>(this.typeParams);
+ typeParams.add(typeParam);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder addTypeParam(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder typeParam) {
+ var typeParams = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>(this.typeParams);
+ typeParams.add(typeParam.build());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
}
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
}
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, docComment, source);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
}
// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder body
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNode.java
new file mode 100644
index 0000000..fe27b0a
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNode.java
@@ -0,0 +1,38 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode imports
+
+public record UntypedTypeParamNode(
+ String name,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+) {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder("", org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ }
+
+ public record Builder(
+ String name,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+ ) {
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode(name, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder withName(String name) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder(name, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder(name, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
index ab88af0..1e03672 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
@@ -167,6 +167,9 @@ public class Parser {
tokens.skip(TokenType.KEYWORD_NATIVE);
var name = tokens.next(TokenType.IDENTIFIER);
+
+ var typeParams = parseTypeParams(tokens);
+
tokens.skip(TokenType.SYMBOL_BRACE_OPEN);
tokens.skip(TokenType.SYMBOL_BRACE_CLOSE);
@@ -176,6 +179,7 @@ public class Parser {
return new UntypedNativeTypeDefinitionNode(
name.charSequence().toString(),
+ typeParams,
docComment,
source
);
@@ -315,6 +319,31 @@ public class Parser {
);
}
+ private static List<UntypedTypeParamNode> parseTypeParams(TokenIterator tokens) {
+ if (!tokens.trySkip(TokenType.SYMBOL_SQUARE_OPEN)) {
+ return List.of();
+ }
+
+ var typeParams = parseMany(
+ () -> tokens.isNext(TokenType.SYMBOL_SQUARE_CLOSE),
+ () -> {
+ var start = tokens.startPosition();
+
+ var name = parseIdentifier(tokens);
+
+ var end = tokens.endPosition();
+ var source = createSource(start, end);
+
+ return new UntypedTypeParamNode(name, source);
+ },
+ () -> tokens.trySkip(TokenType.SYMBOL_COMMA)
+ );
+
+ tokens.skip(TokenType.SYMBOL_SQUARE_CLOSE);
+
+ return typeParams;
+ }
+
static UntypedTypeLevelExpressionNode parseTypeLevelExpression(
TokenIterator tokens
) {
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java
index f07b8a3..e1f02a0 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java
@@ -36,6 +36,12 @@ public final class UntypedNativeTypeDefinitionNodeMatcher implements org.zwobble
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher(submatchers);
}
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher withTypeParams(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> typeParams) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("typeParams", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode::typeParams, typeParams));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher(submatchers);
+ }
+
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher withDocComment(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.DocComment> docComment) {
var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode>>(this.submatchers);
submatchers.add(org.zwobble.precisely.Matchers.has("docComment", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode::docComment, docComment));
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNodeMatcher.java
new file mode 100644
index 0000000..4086b0f
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeParamNodeMatcher.java
@@ -0,0 +1,47 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher imports
+
+public final class UntypedTypeParamNodeMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher isUntypedTypeParamNode() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> submatchers;
+
+ private UntypedTypeParamNodeMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher withName(org.zwobble.precisely.Matcher<? super String> name) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode::name, name));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher withSource(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.sources.Source> source) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode::source, source));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java
index b8e014a..ca0a71d 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java
@@ -3,6 +3,8 @@ package org.zwobble.hobgoblin.compiler.parser;
import org.junit.jupiter.api.Test;
import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode;
+import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher.isUntypedNativeTypeDefinitionNode;
+import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher.isUntypedTypeParamNode;
import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -24,4 +26,57 @@ public class ParserNativeTypeDefinitionTests {
has("name", UntypedNativeTypeDefinitionNode::name, equalTo("Point"))
));
}
+
+ @Test
+ public void noTypeParams() {
+ var source = """
+ native Point {
+ }""";
+
+ var node = parseString(
+ source,
+ Parser::parseNamespaceStatement
+ );
+
+ assertThat(node, isUntypedNativeTypeDefinitionNode()
+ .withTypeParams(isSequence())
+ );
+ }
+
+ @Test
+ public void oneTypeParam() {
+ var source = """
+ native Line[TPoint] {
+ }""";
+
+ var node = parseString(
+ source,
+ Parser::parseNamespaceStatement
+ );
+
+ assertThat(node, isUntypedNativeTypeDefinitionNode()
+ .withTypeParams(isSequence(
+ isUntypedTypeParamNode().withName(equalTo("TPoint"))
+ ))
+ );
+ }
+
+ @Test
+ public void manyTypeParams() {
+ var source = """
+ native Map[K, V] {
+ }""";
+
+ var node = parseString(
+ source,
+ Parser::parseNamespaceStatement
+ );
+
+ assertThat(node, isUntypedNativeTypeDefinitionNode()
+ .withTypeParams(isSequence(
+ isUntypedTypeParamNode().withName(equalTo("K")),
+ isUntypedTypeParamNode().withName(equalTo("V"))
+ ))
+ );
+ }
}