From 76259b1c4cb97d59573673fa7e645a394b27198e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 Jul 2026 14:33:09 +0100 Subject: Add Rust path expressions --- .../compiler/output/lang/rust/RustWriter.java | 27 ++++++++++ .../output/lang/rust/ast/RustExpression.java | 2 +- .../output/lang/rust/ast/RustPathExprSegment.java | 49 ++++++++++++++++++ .../output/lang/rust/ast/RustPathInExpression.java | 60 ++++++++++++++++++++++ .../compiler/output/lang/rust/RustWriterTests.java | 41 +++++++++++---- .../lang/rust/ast/RustPathExprSegmentMatcher.java | 45 ++++++++++++++++ .../lang/rust/ast/RustPathInExpressionMatcher.java | 39 ++++++++++++++ 7 files changed, 251 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegment.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpression.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegmentMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpressionMatcher.java (limited to 'src') 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 04c4f60..9f14153 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 @@ -231,6 +231,10 @@ public class RustWriter implements AutoCloseable { case RustBoolLiteral boolLiteral -> { this.writeBoolLiteral(boolLiteral); } + + case RustPathInExpression pathInExpression -> { + this.writePathInExpression(pathInExpression); + } } } @@ -269,6 +273,29 @@ public class RustWriter implements AutoCloseable { // == Paths == + private void writePathInExpression(RustPathInExpression pathInExpression) throws IOException { + writeWithSeparator( + pathInExpression.segments(), + this::writePathExprSegment, + () -> this.writer.write("::") + ); + } + + private void writePathExprSegment(RustPathExprSegment segment) throws IOException { + writePathIdentSegment(segment.pathIdentSegment()); + + if (segment.args().isPresent()) { + this.writer.write("::"); + this.writer.write("<"); + writeWithSeparator( + segment.args().get(), + this::writeType, + () -> this.writer.write(", ") + ); + this.writer.write(">"); + } + } + private void writeTypePath(RustTypePath typePath) throws IOException { writeWithSeparator( typePath.segments(), 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 13c50b0..ec2a3c0 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.RustBlockExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral { +public sealed interface RustExpression permits 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.RustPathInExpression { 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/RustPathExprSegment.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegment.java new file mode 100644 index 0000000..9e55ecf --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegment.java @@ -0,0 +1,49 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment imports +import java.util.List; +import java.util.Optional; +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment imports + +public record RustPathExprSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment, java.util.Optional> args) { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate.arbitrary().build(), java.util.Optional.empty()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment, java.util.Optional> args) { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment(pathIdentSegment, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder withPathIdentSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder(pathIdentSegment, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder withPathIdentSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment.Builder pathIdentSegment) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder(pathIdentSegment.build(), args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder withArgs(java.util.Optional> args) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder(pathIdentSegment, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder withArgs(java.util.List args) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder(pathIdentSegment, java.util.Optional.of(args)); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment body + public RustPathExprSegment withArgs(List args) { + if (this.args.isPresent()) { + throw new IllegalArgumentException("Expression path segment already has args"); + } + + return new RustPathExprSegment(this.pathIdentSegment, Optional.of(args)); + } + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpression.java new file mode 100644 index 0000000..be28b7c --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpression.java @@ -0,0 +1,60 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression imports +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression imports + +public record RustPathInExpression(java.util.List segments) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder(java.util.List.of()); + } + + public record Builder(java.util.List segments) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression(segments); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder withSegments(java.util.List segments) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder(segments); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder addSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment segment) { + var segments = new java.util.ArrayList(this.segments); + segments.add(segment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder(segments); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder addSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment.Builder segment) { + var segments = new java.util.ArrayList(this.segments); + segments.add(segment.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder(segments); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression body + public static RustPathInExpression of(String... names) { + var segments = Arrays.stream(names) + .map(name -> new RustPathExprSegment( + new RustPathIdentSegmentIdentifier(new RustIdentifier(name)), + Optional.empty() + )) + .toList(); + return new RustPathInExpression(segments); + } + + public RustPathInExpression withArgs(List args) { + var segments = new ArrayList<>(this.segments); + var lastSegment = segments.removeLast().withArgs(args); + segments.add(lastSegment); + return new RustPathInExpression(segments); + } + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression 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 476a876..2090c1e 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 @@ -360,34 +360,35 @@ public class RustWriterTests { // == Paths == @Test - public void typeSegmentsAreSeparatedByDoubleColons() throws IOException { - var rust = RustTypePath.of("std", "string", "String"); + public void pathExprSegmentsAreSeparatedByDoubleColons() throws IOException { + var rust = RustPathInExpression.of("std", "string", "String"); - var string = write(writer -> writer.writeType(rust)); + var string = write(writer -> writer.writeExpression(rust)); assertThat(string, equalTo("std::string::String")); } @Test - public void crateTypeSegmentIsWritten() throws IOException { - var rust = RustTypePath.crate("a", "b"); + public void pathExprSegmentGenericArgsAreWritten() throws IOException { + var rust = RustPathInExpression.of("std", "collections", "HashMap") + .withArgs(List.of(RustTypePath.of("i32"), RustTypePath.of("f64"))); - var string = write(writer -> writer.writeType(rust)); + var string = write(writer -> writer.writeExpression(rust)); - assertThat(string, equalTo("crate::a::b")); + assertThat(string, equalTo("std::collections::HashMap::")); } @Test - public void globalTypeSegmentIsWritten() throws IOException { - var rust = RustTypePath.global("a", "b"); + public void typePathSegmentsAreSeparatedByDoubleColons() throws IOException { + var rust = RustTypePath.of("std", "string", "String"); var string = write(writer -> writer.writeType(rust)); - assertThat(string, equalTo("::a::b")); + assertThat(string, equalTo("std::string::String")); } @Test - public void typeSegmentGenericArgsAreWritten() throws IOException { + public void typePathSegmentGenericArgsAreWritten() throws IOException { var rust = RustTypePath.of("std", "collections", "HashMap") .withArgs(List.of(RustTypePath.of("i32"), RustTypePath.of("f64"))); @@ -396,6 +397,24 @@ public class RustWriterTests { assertThat(string, equalTo("std::collections::HashMap")); } + @Test + public void crateSegmentIsWritten() throws IOException { + var rust = RustTypePath.crate("a", "b"); + + var string = write(writer -> writer.writeType(rust)); + + assertThat(string, equalTo("crate::a::b")); + } + + @Test + public void globalSegmentIsWritten() throws IOException { + var rust = RustTypePath.global("a", "b"); + + var string = write(writer -> writer.writeType(rust)); + + assertThat(string, equalTo("::a::b")); + } + // == Identifiers == @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegmentMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegmentMatcher.java new file mode 100644 index 0000000..04fbe5b --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathExprSegmentMatcher.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.RustPathExprSegmentMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher imports + +public class RustPathExprSegmentMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher isRustPathExprSegment() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustPathExprSegmentMatcher(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.RustPathExprSegment.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher withPathIdentSegment(org.zwobble.precisely.Matcher pathIdentSegment) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("pathIdentSegment", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment::pathIdentSegment, pathIdentSegment)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher withArgs(org.zwobble.precisely.Matcher>> args) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("args", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegment::args, args)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathExprSegmentMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpressionMatcher.java new file mode 100644 index 0000000..4fad401 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathInExpressionMatcher.java @@ -0,0 +1,39 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher imports + +public class RustPathInExpressionMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher isRustPathInExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustPathInExpressionMatcher(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.RustPathInExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher withSegments(org.zwobble.precisely.Matcher> segments) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("segments", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression::segments, segments)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpressionMatcher body +} -- cgit v1.2.3