From bab3189ae3e5050afd8bcbc81e936a6ee56f0377 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Jul 2026 16:33:56 +0100 Subject: Add array access to Java AST --- .../compiler/output/lang/java/JavaWriterTests.java | 12 ++++++ .../lang/java/ast/JavaArrayAccessMatcher.java | 45 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaArrayAccessMatcher.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java index f768827..b26b8f5 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java @@ -1074,6 +1074,18 @@ public class JavaWriterTests { // === Binary operations === + @Test + public void arrayAccess() throws IOException { + var java = JavaArrayAccess.arbitrary() + .withArrayReference(JavaRef.arbitrary().withName(JavaIdentifier.of("x"))) + .withIndex(JavaIntegerLiteral.arbitrary().withValue(42)) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(java)); + + assertThat(string, equalTo("x[42]")); + } + @Test public void binaryOperationAdd() throws IOException { var java = new JavaBinaryOperation( diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaArrayAccessMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaArrayAccessMatcher.java new file mode 100644 index 0000000..1d6887e --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaArrayAccessMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher imports + +public class JavaArrayAccessMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher isJavaArrayAccess() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaArrayAccessMatcher(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.java.ast.JavaArrayAccess.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher withArrayReference(org.zwobble.precisely.Matcher arrayReference) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("arrayReference", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccess::arrayReference, arrayReference)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher withIndex(org.zwobble.precisely.Matcher index) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("index", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccess::index, index)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaArrayAccessMatcher body +} -- cgit v1.2.3