diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-19 17:38:39 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-19 17:38:39 +0100 |
| commit | a11c9415b63f45c316d3b08d656fa93eb9602f13 (patch) | |
| tree | 8e6f8de2bf6e1921a35974b5ef38ed7bc84a8fb5 /src | |
| parent | 1f005669637f624ac5fdd42cb999558cde6aed33 (diff) | |
Add match expression to Rust AST
Diffstat (limited to 'src')
7 files changed, 230 insertions, 1 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index c3fa404..0630e07 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -351,6 +351,10 @@ public class RustWriter implements AutoCloseable { this.writeIteratorLoopExpression(iteratorLoopExpression); } + case RustMatchExpression matchExpression -> { + this.writeMatchExpression(matchExpression); + } + case RustPath path -> { this.writePath(path); } @@ -414,6 +418,7 @@ public class RustWriter implements AutoCloseable { case RustIfExpression _ -> RustPrecedence.PRIMARY; case RustIntegerLiteral _ -> RustPrecedence.PRIMARY; case RustIteratorLoopExpression _ -> RustPrecedence.PRIMARY; + case RustMatchExpression _ -> RustPrecedence.PRIMARY; case RustPath _ -> RustPrecedence.PATHS; case RustPrefixExpression _ -> RustPrecedence.PREFIX; case RustRangeExpr _ -> RustPrecedence.RANGE; @@ -550,6 +555,27 @@ public class RustWriter implements AutoCloseable { this.writeBlockExpression(iteratorLoopExpression.body()); } + private void writeMatchExpression( + RustMatchExpression matchExpression + ) throws IOException { + this.writer.write("match "); + this.writeTopLevelExpression(matchExpression.scrutinee()); + this.writer.write(" {"); + this.writer.indent(); + this.writer.newLine(); + + for (var arm : matchExpression.arms()) { + this.writePattern(arm.pattern()); + this.writer.write(" => "); + this.writeTopLevelExpression(arm.body()); + this.writer.write(","); + this.writer.newLine(); + } + + this.writer.dedent(); + this.writer.write("}"); + } + private void writePrefixExpression(RustPrefixExpression prefixExpression) throws IOException { this.writer.write(switch (prefixExpression.operator()) { case BORROW -> "&"; diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java index 6cfea75..8e55bc4 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression imports -public sealed interface RustExpression permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustArrayRepeatExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBinaryExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFieldExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPrefixExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustRangeExpr, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTryPropagationExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression { +public sealed interface RustExpression permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustArrayRepeatExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBinaryExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFieldExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPrefixExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustRangeExpr, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTryPropagationExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchArm.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchArm.java new file mode 100644 index 0000000..a946ce1 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchArm.java @@ -0,0 +1,40 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm imports + +public record RustMatchArm(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern pattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body) { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifierPattern.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern pattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body) { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm(pattern, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder withPattern(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern pattern) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder(pattern, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder withPattern(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern.Builder pattern) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder(pattern.build(), body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder(pattern, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder body) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder(pattern, body.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchExpression.java new file mode 100644 index 0000000..4770b86 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchExpression.java @@ -0,0 +1,48 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression imports + +public record RustMatchExpression(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression scrutinee, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm> arms) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build(), java.util.List.of()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression scrutinee, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm> arms) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression(scrutinee, arms); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder withScrutinee(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression scrutinee) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder(scrutinee, arms); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder withScrutinee(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder scrutinee) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder(scrutinee.build(), arms); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder withArms(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm> arms) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder(scrutinee, arms); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder addArm(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm arm) { + var arms = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>(this.arms); + arms.add(arm); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder(scrutinee, arms); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder addArm(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm.Builder arm) { + var arms = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>(this.arms); + arms.add(arm.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder(scrutinee, arms); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java index 5cb711a..c766157 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java @@ -644,6 +644,31 @@ public class RustWriterTests { }""")); } + // === Match expressions === + + @Test + public void matchExpression() throws IOException { + var rust = RustMatchExpression.arbitrary() + .withScrutinee(RustPath.of("x")) + .addArm(RustMatchArm.arbitrary() + .withPattern(RustPathPattern.arbitrary().withPath(RustPath.of("X", "Y"))) + .withBody(RustIntegerLiteral.arbitrary().withValue(1)) + ) + .addArm(RustMatchArm.arbitrary() + .withPattern(RustWildcardPattern.arbitrary()) + .withBody(RustIntegerLiteral.arbitrary().withValue(2)) + ) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo(""" + match x { + X::Y => 1, + _ => 2, + }""")); + } + // === Prefix expressions === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchArmMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchArmMatcher.java new file mode 100644 index 0000000..697f6c5 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchArmMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher imports + +public class RustMatchArmMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher isRustMatchArm() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>> submatchers; + + private RustMatchArmMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>> 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.rust.ast.RustMatchArm.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher withPattern(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern> pattern) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("pattern", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm::pattern, pattern)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher withBody(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> body) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArmMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchExpressionMatcher.java new file mode 100644 index 0000000..8dcee56 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustMatchExpressionMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher imports + +public class RustMatchExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher isRustMatchExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression>> submatchers; + + private RustMatchExpressionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression>> 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.rust.ast.RustMatchExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher withScrutinee(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> scrutinee) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("scrutinee", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression::scrutinee, scrutinee)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher withArms(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchArm>> arms) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("arms", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression::arms, arms)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpressionMatcher body +} |
