diff options
Diffstat (limited to 'src')
5 files changed, 95 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 e85230a..d1b0ccc 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 @@ -423,6 +423,10 @@ public class JavaWriter implements AutoCloseable { case JavaSwitchStatement switchStatement -> { writeSwitchStatement(switchStatement); } + + case JavaThrowStatement throwStatement -> { + writeThrowStatement(throwStatement); + } } } @@ -571,6 +575,12 @@ public class JavaWriter implements AutoCloseable { this.writer.write("}"); } + private void writeThrowStatement(JavaThrowStatement throwStatement) throws IOException { + this.writer.write("throw "); + this.writeTopLevelExpression(throwStatement.expression()); + this.writer.write(";"); + } + // == Expressions == void writeTopLevelExpression(JavaExpression expression) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java index 6f6096d..50d6970 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement imports -public sealed interface JavaBlockStatement permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaReturn, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement { +public sealed interface JavaBlockStatement permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaReturn, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaThrowStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaThrowStatement.java new file mode 100644 index 0000000..e88ad67 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaThrowStatement.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement imports + +public record JavaThrowStatement(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression expression) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBooleanLiteral.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression expression) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement build() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement(expression); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression expression) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder(expression); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression.Builder expression) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder(expression.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement 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 58fd7a4..9f6214d 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 @@ -1177,6 +1177,19 @@ public class JavaWriterTests { }""")); } + // === Throw statements === + + @Test + public void throwStatement() throws IOException { + var java = JavaThrowStatement.arbitrary() + .withExpression(JavaRef.arbitrary().withName(JavaIdentifier.of("exception"))) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo("throw exception;")); + } + // == Expressions == // === Binary operations === diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaThrowStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaThrowStatementMatcher.java new file mode 100644 index 0000000..b08e1b0 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaThrowStatementMatcher.java @@ -0,0 +1,39 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher imports + +public class JavaThrowStatementMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher isJavaThrowStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement>> submatchers; + + private JavaThrowStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement>> 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.JavaThrowStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher withExpression(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> expression) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("expression", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement::expression, expression)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatementMatcher body +} |
