From b94fc2c22021dc50381856f170fe9dca4b879468 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 18 Jul 2026 19:10:38 +0100 Subject: Add iterator loop expressions to Rust AST --- .../compiler/output/lang/rust/RustWriterTests.java | 23 ++++++++++ .../ast/RustIteratorLoopExpressionMatcher.java | 51 ++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIteratorLoopExpressionMatcher.java (limited to 'src/test/java/org/zwobble') 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 bf93c1d..e947db9 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 @@ -621,6 +621,29 @@ public class RustWriterTests { assertThat(string, equalTo("123i32")); } + // === Iterator expressions === + + @Test + public void iteratorExpression() throws IOException { + var rust = RustIteratorLoopExpression.arbitrary() + .withElementName(RustIdentifier.of("x")) + .withElements(RustPath.of("xs")) + .withBody(RustBlockExpression.arbitrary() + .withFinalOperand(RustCallExpression.arbitrary() + .withFunction(RustPath.of("print")) + .addArg(RustPath.of("x")) + ) + ) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo(""" + for x in xs { + print(x) + }""")); + } + // === Prefix expressions === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIteratorLoopExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIteratorLoopExpressionMatcher.java new file mode 100644 index 0000000..6bd153a --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIteratorLoopExpressionMatcher.java @@ -0,0 +1,51 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher imports + +public class RustIteratorLoopExpressionMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher isRustIteratorLoopExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustIteratorLoopExpressionMatcher(java.util.List> 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 toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher withElementName(org.zwobble.precisely.Matcher elementName) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("elementName", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression::elementName, elementName)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher withElements(org.zwobble.precisely.Matcher elements) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("elements", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression::elements, elements)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher withBody(org.zwobble.precisely.Matcher body) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpressionMatcher body +} -- cgit v1.2.3