summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-26 16:22:07 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-26 16:22:07 +0100
commit93cefbd3626e516acdec12016d97eff0dc1670e5 (patch)
treee1d7a14da292ce732d19b56d85f41793da892f0b /src
parent3ffddab4deb7e1c115936792d45f4ede5b904901 (diff)
Extract JavaPattern from JavaCasePattern
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java12
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java22
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPattern.java42
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java8
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java10
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPatternMatcher.java47
7 files changed, 114 insertions, 31 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
index 7a27e52..05ce8f1 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
@@ -504,10 +504,10 @@ public class JavaTransient0Generator implements Generator {
));
return new JavaSwitchRule(
- new JavaCasePattern(
+ new JavaCasePattern(new JavaPattern(
this.javaGenerator.generateTypeRef(variant.type().value()),
variantVariable
- ),
+ )),
new JavaBlock(statements)
);
})
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
index 1ca3abd..e13290f 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
@@ -571,9 +571,7 @@ public class JavaWriter implements AutoCloseable {
case JavaCasePattern casePattern -> {
this.writer.write("case ");
- this.writeTypeRef(casePattern.type());
- this.writer.write(" ");
- this.writeIdentifier(casePattern.variableName());
+ this.writePattern(casePattern.pattern());
}
}
this.writer.write(" -> ");
@@ -590,6 +588,14 @@ public class JavaWriter implements AutoCloseable {
this.writer.write(";");
}
+ // == Patterns ==
+
+ private void writePattern(JavaPattern pattern) throws IOException {
+ this.writeTypeRef(pattern.type());
+ this.writer.write(" ");
+ this.writeIdentifier(pattern.variableName());
+ }
+
// == Expressions ==
void writeTopLevelExpression(JavaExpression expression) throws IOException {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java
index 4b93726..aa50c86 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java
@@ -6,31 +6,25 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern imports
public record JavaCasePattern(
- org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type,
- org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName
+ org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern pattern
) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel {
public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.HobgoblinNativeAst.arbitraryJavaTypeRef(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.arbitrary().build());
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.arbitrary().build());
}
public record Builder(
- org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type,
- org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName
+ org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern pattern
) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel.Builder {
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern build() {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern(type, variableName);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern(pattern);
}
- public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(type, variableName);
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withPattern(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern pattern) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(pattern);
}
- public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(type, variableName);
- }
-
- public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.Builder variableName) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(type, variableName.build());
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withPattern(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder pattern) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(pattern.build());
}
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder body
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPattern.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPattern.java
new file mode 100644
index 0000000..dff6691
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPattern.java
@@ -0,0 +1,42 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern imports
+
+public record JavaPattern(
+ org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type,
+ org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName
+) {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.HobgoblinNativeAst.arbitraryJavaTypeRef(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.arbitrary().build());
+ }
+
+ public record Builder(
+ org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type,
+ org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName
+ ) {
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern(type, variableName);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder(type, variableName);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder(type, variableName);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.Builder variableName) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder(type, variableName.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
index 178ca0a..4ce2ea1 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
@@ -1219,19 +1219,19 @@ public class JavaWriterTests {
new JavaRef(JavaIdentifier.of("x")),
List.of(
new JavaSwitchRule(
- new JavaCasePattern(
+ new JavaCasePattern(new JavaPattern(
JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("A")),
JavaIdentifier.of("a")
- ),
+ )),
new JavaBlock(List.of(
new JavaReturn(new JavaIntegerLiteral(0))
))
),
new JavaSwitchRule(
- new JavaCasePattern(
+ new JavaCasePattern(new JavaPattern(
JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("B")),
JavaIdentifier.of("b")
- ),
+ )),
new JavaBlock(List.of(
new JavaReturn(new JavaIntegerLiteral(1))
))
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java
index ebf029c..d4bf59a 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java
@@ -30,15 +30,9 @@ public class JavaCasePatternMatcher implements org.zwobble.precisely.Matcher<jav
return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.class, this.submatchers);
}
- public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef> type) {
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher withPattern(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern> pattern) {
var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern>>(this.submatchers);
- submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern::type, type));
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher(submatchers);
- }
-
- public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher withVariableName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier> variableName) {
- var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern>>(this.submatchers);
- submatchers.add(org.zwobble.precisely.Matchers.has("variableName", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern::variableName, variableName));
+ submatchers.add(org.zwobble.precisely.Matchers.has("pattern", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern::pattern, pattern));
return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher(submatchers);
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPatternMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPatternMatcher.java
new file mode 100644
index 0000000..789da61
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaPatternMatcher.java
@@ -0,0 +1,47 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher imports
+
+public class JavaPatternMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher isJavaPattern() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern>> submatchers;
+
+ private JavaPatternMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern>> 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.output.lang.java.ast.JavaPattern.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef> type) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern::type, type));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher withVariableName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier> variableName) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("variableName", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPattern::variableName, variableName));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaPatternMatcher body
+}