summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-26 00:24:04 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-26 00:24:04 +0100
commit27dd47f88f1bfea84752d7bf30ef2f7ef3c8cc8e (patch)
tree006fdca57c323dfcc706e3527066310cd267e1f5 /src
parent35e5a39b044f5916fb528ec1c80d05c05e4389c4 (diff)
Add index expressions to Rust AST
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java12
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpression.java40
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java14
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpressionMatcher.java45
5 files changed, 112 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 af6c1a7..fe12f9f 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
@@ -347,6 +347,10 @@ public class RustWriter implements AutoCloseable {
this.writeIfExpression(ifExpression);
}
+ case RustIndexExpression indexExpression -> {
+ this.writeIndexExpression(indexExpression);
+ }
+
case RustIntegerLiteral integerLiteral -> {
this.writeIntegerLiteral(integerLiteral);
}
@@ -425,6 +429,7 @@ public class RustWriter implements AutoCloseable {
case RustClosureExpression _ -> RustPrecedence.PRIMARY;
case RustFieldExpression _ -> RustPrecedence.FIELD_EXPRESSIONS;
case RustIfExpression _ -> RustPrecedence.PRIMARY;
+ case RustIndexExpression _ -> RustPrecedence.FUNCTION_CALLS;
case RustIntegerLiteral _ -> RustPrecedence.PRIMARY;
case RustIteratorLoopExpression _ -> RustPrecedence.PRIMARY;
case RustMatchExpression _ -> RustPrecedence.PRIMARY;
@@ -569,6 +574,13 @@ public class RustWriter implements AutoCloseable {
this.writeBlockExpression(ifExpression.ifFalse());
}
+ private void writeIndexExpression(RustIndexExpression indexExpression) throws IOException {
+ this.writeSubExpression(indexExpression.indexed(), precedence(indexExpression), true);
+ this.writer.write("[");
+ this.writeTopLevelExpression(indexExpression.index());
+ this.writer.write("]");
+ }
+
private void writeIntegerLiteral(RustIntegerLiteral integerLiteral) throws IOException {
this.writer.write(Long.toString(integerLiteral.value()));
if (integerLiteral.type().isPresent()) {
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 9f15f5b..c418608 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.RustClosureExpression, 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.RustTypeCastExpression, 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.RustClosureExpression, 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.RustIndexExpression, 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.RustTypeCastExpression, 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/RustIndexExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpression.java
new file mode 100644
index 0000000..a204997
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpression.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.RustIndexExpression imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression imports
+
+public record RustIndexExpression(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression indexed, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression index) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build());
+ }
+
+ public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression indexed, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression index) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression(indexed, index);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder withIndexed(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression indexed) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder(indexed, index);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder withIndexed(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder indexed) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder(indexed.build(), index);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder withIndex(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression index) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder(indexed, index);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder withIndex(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder index) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder(indexed, index.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression 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 b5dd515..8946855 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
@@ -650,6 +650,20 @@ public class RustWriterTests {
}"""));
}
+ // === Index expressions ==
+
+ @Test
+ public void indexExpression() throws IOException {
+ var rust = RustIndexExpression.arbitrary()
+ .withIndexed(RustPath.of("x"))
+ .withIndex(RustPath.of("y"))
+ .build();
+
+ var string = write(writer -> writer.writeTopLevelExpression(rust));
+
+ assertThat(string, equalTo("x[y]"));
+ }
+
// === Integer literals ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpressionMatcher.java
new file mode 100644
index 0000000..4261e0e
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIndexExpressionMatcher.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.RustIndexExpressionMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher imports
+
+public class RustIndexExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher isRustIndexExpression() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression>> submatchers;
+
+ private RustIndexExpressionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression>> 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.RustIndexExpression.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher withIndexed(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> indexed) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("indexed", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression::indexed, indexed));
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher withIndex(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> index) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("index", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpression::index, index));
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIndexExpressionMatcher body
+}