diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-29 22:19:10 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-29 22:19:10 +0100 |
| commit | bd664e8cffcaf997631c9a8f5a5770d5a8bbe4d7 (patch) | |
| tree | 1f3e22a4690c9f636d7766222c7fa2e621893c13 | |
| parent | 73b58b585b0e12b9baf263fdc55da2f2b501651f (diff) | |
Add class declarations to TypeScript AST
7 files changed, 155 insertions, 0 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob index 80b7eeb..1af6aca 100644 --- a/hobgoblin/src/output/lang/typescript/ast.hob +++ b/hobgoblin/src/output/lang/typescript/ast.hob @@ -6,6 +6,16 @@ struct TypescriptModule { field name: List[TypescriptIdentifier]; } +// == Statements == + +sum TypescriptStatement { + variant TypescriptClassDeclaration; +} + +struct TypescriptClassDeclaration { + field name: TypescriptIdentifier; +} + // == Identifiers == struct TypescriptIdentifier { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java index a031a82..c230705 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java @@ -1,7 +1,10 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript; import org.zwobble.hobgoblin.compiler.output.CodeWriter; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier; import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement; import java.io.IOException; import java.nio.file.Path; @@ -45,4 +48,28 @@ public class TypescriptWriter implements AutoCloseable { public void writeModule(TypescriptModule module) throws IOException { this.writer.writeHeader(); } + + // == Statements == + + void writeStatement(TypescriptStatement statement) throws IOException { + switch (statement) { + case TypescriptClassDeclaration classDeclaration -> { + this.writeClassDeclaration(classDeclaration); + } + } + } + + private void writeClassDeclaration(TypescriptClassDeclaration classDeclaration) throws IOException { + this.writer.write("class "); + this.writeIdentifier(classDeclaration.name()); + this.writer.write(" {"); + this.writer.newLine(); + this.writer.write("}"); + } + + // == Identifiers == + + private void writeIdentifier(TypescriptIdentifier name) throws IOException { + this.writer.write(name.value()); + } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java new file mode 100644 index 0000000..a2e992c --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java @@ -0,0 +1,36 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration imports + +public record TypescriptClassDeclaration( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptIdentifier.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptIdentifier.java index a87c792..f9b85e9 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptIdentifier.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptIdentifier.java @@ -28,5 +28,8 @@ public record TypescriptIdentifier( } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier body + public static TypescriptIdentifier of(String value) { + return new TypescriptIdentifier(value); + } // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier body } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java new file mode 100644 index 0000000..8bb978b --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement imports + +public sealed interface TypescriptStatement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration { + public interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java index 71978f0..a5cf539 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java @@ -2,6 +2,8 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.output.CodeWriter; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier; import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule; import java.io.IOException; @@ -27,6 +29,24 @@ public class TypescriptWriterTests { )); } + // == Statements == + + // === Class declarations === + + @Test + public void emptyClassDeclaration() throws IOException { + var typescript = TypescriptClassDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("X")) + .build(); + + var string = write(writer -> writer.writeStatement(typescript)); + + assertThat(string, equalTo(""" + class X { + }""" + )); + } + private String write(TypescriptWriter.Write write) throws IOException { return write(write, Optional.empty()); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclarationMatcher.java new file mode 100644 index 0000000..8bde6e1 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclarationMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher imports + +public final class TypescriptClassDeclarationMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher isTypescriptClassDeclaration() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration>> submatchers; + + private TypescriptClassDeclarationMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration>> 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.typescript.ast.TypescriptClassDeclaration.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration::name, name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclarationMatcher body +} |
