diff options
Diffstat (limited to 'src')
8 files changed, 129 insertions, 5 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 9c5361b..c3fa404 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 @@ -432,6 +432,18 @@ public class RustWriter implements AutoCloseable { }; } + private void writeLiteralExpression(RustLiteralExpression literal) throws IOException { + switch (literal) { + case RustBoolLiteral boolLiteral -> { + this.writeBoolLiteral(boolLiteral); + } + + case RustIntegerLiteral integerLiteral -> { + this.writeIntegerLiteral(integerLiteral); + } + } + } + private void writeArrayRepeatExpression(RustArrayRepeatExpression arrayRepeatExpression) throws IOException { this.writer.write("["); this.writeTopLevelExpression(arrayRepeatExpression.repeatOperand()); @@ -622,6 +634,10 @@ public class RustWriter implements AutoCloseable { this.writeIdentifierPattern(identifierPattern); } + case RustLiteralPattern literalPattern -> { + this.writeLiteralPattern(literalPattern); + } + case RustPathPattern pathPattern -> { this.writePathPattern(pathPattern); } @@ -638,6 +654,12 @@ public class RustWriter implements AutoCloseable { this.writeIdentifier(identifierPattern.variableName()); } + private void writeLiteralPattern( + RustLiteralPattern literalPattern + ) throws IOException { + this.writeLiteralExpression(literalPattern.literal()); + } + private void writePathPattern(RustPathPattern pathPattern) throws IOException { this.writePath(pathPattern.path()); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java index 5e6a90a..88377e2 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java @@ -5,12 +5,12 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral imports -public record RustBoolLiteral(boolean value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { +public record RustBoolLiteral(boolean value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder arbitrary() { return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder(false); } - public record Builder(boolean value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public record Builder(boolean value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression.Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral build() { return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral(value); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java index eb297b0..3764ecf 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java @@ -5,12 +5,12 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral imports -public record RustIntegerLiteral(long value, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { +public record RustIntegerLiteral(long value, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder arbitrary() { return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(0, java.util.Optional.empty()); } - public record Builder(long value, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public record Builder(long value, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression.Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral build() { return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral(value, type); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralExpression.java new file mode 100644 index 0000000..87335b7 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralExpression.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression imports + +public sealed interface RustLiteralExpression permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral { + public interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralPattern.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralPattern.java new file mode 100644 index 0000000..556ad59 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralPattern.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern imports + +public record RustLiteralPattern(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression literal) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression literal) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern(literal); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder withLiteral(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression literal) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder(literal); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder withLiteral(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression.Builder literal) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder(literal.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPattern.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPattern.java index b012a9c..c24a1ee 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPattern.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPattern.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.RustPattern imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern imports -public sealed interface RustPattern permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifierPattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathPattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustWildcardPattern { +public sealed interface RustPattern permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifierPattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathPattern, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustWildcardPattern { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern build(); 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 24a57a6..5cb711a 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 @@ -850,6 +850,19 @@ public class RustWriterTests { assertThat(string, equalTo("x")); } + // === Literal patterns === + + @Test + public void literalPattern() throws IOException { + var rust = RustLiteralPattern.arbitrary() + .withLiteral(RustBoolLiteral.arbitrary().withValue(false)) + .build(); + + var string = write(writer -> writer.writePattern(rust)); + + assertThat(string, equalTo("false")); + } + // === Path patterns === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralPatternMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralPatternMatcher.java new file mode 100644 index 0000000..5af5abc --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLiteralPatternMatcher.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.RustLiteralPatternMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher imports + +public class RustLiteralPatternMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher isRustLiteralPattern() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern>> submatchers; + + private RustLiteralPatternMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern>> 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.RustLiteralPattern.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher withLiteral(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralExpression> literal) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("literal", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPattern::literal, literal)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLiteralPatternMatcher body +} |
