summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-06-19 18:27:41 +0100
committerMichael Williamson <mike@zwobble.org>2026-06-19 18:27:41 +0100
commit64344428233e3ed4250477b52f5eb560e168c29b (patch)
tree7d2f4e049a798bd91de7da679112a915ca0d1acc /src
parent4a50855f064f0744a0e4b03cb505271880589936 (diff)
Add Java enum declarations
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java30
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstant.java25
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclaration.java41
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeDeclaration.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java62
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java34
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java46
7 files changed, 239 insertions, 1 deletions
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 0299a31..0e5fd92 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
@@ -61,6 +61,10 @@ public class JavaWriter implements AutoCloseable {
writeClassDeclaration(classDeclaration);
}
+ case JavaEnumDeclaration enumDeclaration -> {
+ writeEnumDeclaration(enumDeclaration);
+ }
+
case JavaInterfaceDeclaration interfaceDeclaration -> {
writeInterfaceDeclaration(interfaceDeclaration);
}
@@ -181,6 +185,32 @@ public class JavaWriter implements AutoCloseable {
this.writer.writeCustomArea(customAreaName.toString());
}
+ private void writeEnumDeclaration(JavaEnumDeclaration enumDeclaration) throws IOException {
+ this.writeDocComment(enumDeclaration.docComment());
+ this.writer.write("public enum ");
+ this.writer.write(enumDeclaration.name());
+ this.writer.write(" {");
+ this.writer.indent();
+ this.writer.newLine();
+
+ if (!enumDeclaration.constants().isEmpty()) {
+ writeWithSeparator(
+ enumDeclaration.constants(),
+ constant -> this.writer.write(constant.name()),
+ () -> {
+ this.writer.write(",");
+ this.writer.newLine();
+ }
+ );
+ this.writer.newLine();
+ this.writer.newLine();
+ }
+
+ this.writeCustomArea(enumDeclaration.customArea());
+ this.writer.dedent();
+ this.writer.write("}");
+ }
+
void writeExpression(JavaExpression expression) throws IOException {
switch (expression) {
case JavaAssignment assignment -> {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstant.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstant.java
new file mode 100644
index 0000000..6e7e775
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstant.java
@@ -0,0 +1,25 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+public record JavaEnumConstant(String name) {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant.Builder("");
+ }
+
+ public record Builder(String name) {
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant(name);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant.Builder withName(String name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant.Builder(name);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclaration.java
new file mode 100644
index 0000000..dffd204
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclaration.java
@@ -0,0 +1,41 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+public record JavaEnumDeclaration(String name, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant> constants, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCustomArea customArea, org.zwobble.hobgoblin.compiler.ast.DocComment docComment) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeDeclaration {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder("", java.util.List.of(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCustomArea.arbitrary().build(), org.zwobble.hobgoblin.compiler.ast.Native.arbitraryDocComment());
+ }
+
+ public record Builder(String name, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant> constants, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCustomArea customArea, org.zwobble.hobgoblin.compiler.ast.DocComment docComment) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeDeclaration.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration(name, constants, customArea, docComment);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder withName(String name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder(name, constants, customArea, docComment);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder withConstants(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant> constants) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder(name, constants, customArea, docComment);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder withCustomArea(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCustomArea customArea) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder(name, constants, customArea, docComment);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder withCustomArea(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCustomArea.Builder customArea) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder(name, constants, customArea.build(), docComment);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder(name, constants, customArea, docComment);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeDeclaration.java
index b4c5d49..93e3474 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeDeclaration.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeDeclaration.java
@@ -2,7 +2,7 @@
package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
-public sealed interface JavaTypeDeclaration permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaInterfaceDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaRecordDeclaration {
+public sealed interface JavaTypeDeclaration permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaInterfaceDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaRecordDeclaration {
public interface Builder {
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeDeclaration build();
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 1de864d..78b975d 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
@@ -174,6 +174,68 @@ public class JavaWriterTests {
}
@Test
+ public void emptyEnumDeclaration() throws IOException {
+ var java = JavaEnumDeclaration.arbitrary()
+ .withName("SmallNumber")
+ .withCustomArea(JavaCustomArea.forType(JavaTypeRef.topLevel(List.of(), "SmallNumber"), "body"))
+ .build();
+
+ var string = write(writer -> writer.writeTypeDeclaration(java));
+
+ assertThat(string, equalTo("""
+ public enum SmallNumber {
+ // Custom area start: SmallNumber body
+ // Custom area end: SmallNumber body
+ }"""
+ ));
+ }
+
+ @Test
+ public void enumDeclarationWithConstants() throws IOException {
+ var java = JavaEnumDeclaration.arbitrary()
+ .withName("SmallNumber")
+ .withCustomArea(JavaCustomArea.forType(JavaTypeRef.topLevel(List.of(), "SmallNumber"), "body"))
+ .withConstants(List.of(
+ JavaEnumConstant.arbitrary().withName("ZERO").build(),
+ JavaEnumConstant.arbitrary().withName("ONE").build(),
+ JavaEnumConstant.arbitrary().withName("TWO").build()
+ ))
+ .build();
+
+ var string = write(writer -> writer.writeTypeDeclaration(java));
+
+ assertThat(string, equalTo("""
+ public enum SmallNumber {
+ ZERO,
+ ONE,
+ TWO
+
+ // Custom area start: SmallNumber body
+ // Custom area end: SmallNumber body
+ }"""
+ ));
+ }
+
+ @Test
+ public void enumDeclarationWithDocComment() throws IOException {
+ var java = JavaEnumDeclaration.arbitrary()
+ .withName("SmallNumber")
+ .withDocComment(new DocComment("A small number."))
+ .withCustomArea(JavaCustomArea.forType(JavaTypeRef.topLevel(List.of(), "SmallNumber"), "body"))
+ .build();
+
+ var string = write(writer -> writer.writeTypeDeclaration(java));
+
+ assertThat(string, equalTo("""
+ /// A small number.
+ public enum SmallNumber {
+ // Custom area start: SmallNumber body
+ // Custom area end: SmallNumber body
+ }"""
+ ));
+ }
+
+ @Test
public void openInterfaceDeclaration() throws IOException {
var java = new JavaInterfaceDeclaration(
"Shape",
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java
new file mode 100644
index 0000000..58d6f54
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java
@@ -0,0 +1,34 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+public class JavaEnumConstantMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher isJavaEnumConstant() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant>> submatchers;
+
+ private JavaEnumConstantMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant>> 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.JavaEnumConstant.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher withName(org.zwobble.precisely.Matcher<? super String> name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant::name, name))).toList());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java
new file mode 100644
index 0000000..0c7be66
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java
@@ -0,0 +1,46 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+public class JavaEnumDeclarationMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher isJavaEnumDeclaration() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration>> submatchers;
+
+ private JavaEnumDeclarationMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration>> 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.JavaEnumDeclaration.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withName(org.zwobble.precisely.Matcher<? super String> name) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::name, name))).toList());
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withConstants(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant>> constants) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("constants", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::constants, constants))).toList());
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withCustomArea(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCustomArea> customArea) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("customArea", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::customArea, customArea))).toList());
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withDocComment(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.DocComment> docComment) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("docComment", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::docComment, docComment))).toList());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher body
+}