summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-26 11:47:38 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-26 11:47:38 +0100
commitb86689ba0ed73567993f304f963479633c8d0352 (patch)
treebf74806ef4ceb982153b4e12072f1cb3ff1b2c57 /src
parentf47074c2955d97c581255886b6597bbc95a4ff10 (diff)
Add string literals 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/RustStringLiteral.java28
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java13
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStringLiteralMatcher.java39
5 files changed, 93 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 6d92c5c..2d7e9ed 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
@@ -375,6 +375,10 @@ public class RustWriter implements AutoCloseable {
this.writeRangeExpr(rangeExpr);
}
+ case RustStringLiteral stringLiteral -> {
+ this.writeStringLiteral(stringLiteral);
+ }
+
case RustStructExpression structExpression -> {
this.writeStructExpression(structExpression);
}
@@ -436,6 +440,7 @@ public class RustWriter implements AutoCloseable {
case RustPath _ -> RustPrecedence.PATHS;
case RustPrefixExpression _ -> RustPrecedence.PREFIX;
case RustRangeExpr _ -> RustPrecedence.RANGE;
+ case RustStringLiteral _ -> RustPrecedence.PRIMARY;
case RustStructExpression _ -> RustPrecedence.PRIMARY;
case RustTryPropagationExpression _ -> RustPrecedence.TRY_PROPAGATION;
case RustTupleExpression _ -> RustPrecedence.PRIMARY;
@@ -635,6 +640,13 @@ public class RustWriter implements AutoCloseable {
this.writeSubExpression(rangeExpr.end(), precedence(rangeExpr), false);
}
+ private void writeStringLiteral(RustStringLiteral stringLiteral) throws IOException {
+ // TODO: handle escaping
+ this.writer.write("\"");
+ this.writer.write(stringLiteral.value());
+ this.writer.write("\"");
+ }
+
private void writeStructExpression(RustStructExpression structExpression) throws IOException {
this.writePath(structExpression.structPath());
this.writer.write(" {");
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 c418608..f1096f6 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.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 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.RustStringLiteral, 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/RustStringLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStringLiteral.java
new file mode 100644
index 0000000..d39fca5
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStringLiteral.java
@@ -0,0 +1,28 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.rust.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral imports
+
+public record RustStringLiteral(String value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral.Builder("");
+ }
+
+ public record Builder(String value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral(value);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral.Builder withValue(String value) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral.Builder(value);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral 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 1b3fdac..e9edb72 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
@@ -787,6 +787,19 @@ public class RustWriterTests {
assertThat(string, equalTo("x..y"));
}
+ // === String literals ===
+
+ @Test
+ public void stringLiteral() throws IOException {
+ var rust = RustStringLiteral.arbitrary()
+ .withValue("hello")
+ .build();
+
+ var string = write(writer -> writer.writeTopLevelExpression(rust));
+
+ assertThat(string, equalTo("\"hello\""));
+ }
+
// === Struct expressions ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStringLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStringLiteralMatcher.java
new file mode 100644
index 0000000..33965cc
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStringLiteralMatcher.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.RustStringLiteralMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher imports
+
+public class RustStringLiteralMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher isRustStringLiteral() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral>> submatchers;
+
+ private RustStringLiteralMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral>> 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.RustStringLiteral.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher withValue(org.zwobble.precisely.Matcher<? super String> value) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteral::value, value));
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStringLiteralMatcher body
+}