From b247d99c6950fec45199ff78f8bd16af48d5f96c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 2 Nov 2026 18:27:06 +0000 Subject: Add int literals to SQL AST --- .../compiler/output/lang/sql/SqlWriter.java | 8 +++++ .../output/lang/sql/ast/SqlExpression.java | 2 +- .../output/lang/sql/ast/SqlIntLiteral.java | 32 +++++++++++++++++ .../output/lang/sql/ast/SqlOrderingTerm.java | 2 +- .../output/lang/sql/ast/SqlResultColumn.java | 2 +- .../compiler/output/lang/sql/SqlWriterTests.java | 11 ++++++ .../output/lang/sql/ast/SqlIntLiteralMatcher.java | 41 ++++++++++++++++++++++ 7 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteral.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteralMatcher.java (limited to 'src') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java index 23b8e2a..7f0c17a 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriter.java @@ -169,12 +169,20 @@ public class SqlWriter { void writeExpression(SqlExpression expression) throws IOException { switch (expression) { + case SqlIntLiteral intLiteral -> { + this.writeIntLiteral(intLiteral); + } + case SqlRef ref -> { this.writeRef(ref); } } } + private void writeIntLiteral(SqlIntLiteral intLiteral) throws IOException { + this.writer.write(Long.toString(intLiteral.value())); + } + private void writeRef(SqlRef ref) throws IOException { this.writeIdentifier(ref.name()); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java index 37f5f93..8479160 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression imports -public sealed interface SqlExpression permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef { +public sealed interface SqlExpression permits org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral, org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteral.java new file mode 100644 index 0000000..8a6c3a4 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteral.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral imports + +public record SqlIntLiteral( + long value +) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.Builder(0); + } + + public record Builder( + long value + ) implements org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral build() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral(value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.Builder withValue(long value) { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.Builder(value); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java index 6b3b17a..80ca2e6 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlOrderingTerm.java @@ -9,7 +9,7 @@ public record SqlOrderingTerm( org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlExpression expression ) { public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTerm.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTerm.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTerm.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build()); } public record Builder( diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java index 1db29b1..26516b0 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlResultColumn.java @@ -10,7 +10,7 @@ public record SqlResultColumn( org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier alias ) { public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRef.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumn.Builder(org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier.arbitrary().build()); } public record Builder( diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java index be45935..20d6dac 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java @@ -201,6 +201,17 @@ public class SqlWriterTests { // === Expressions === + @Test + public void intLiteral() throws IOException { + var sql = SqlIntLiteral.arbitrary() + .withValue(42) + .build(); + + var string = write(writer -> writer.writeExpression(sql)); + + assertThat(string, equalTo("42")); + } + @Test public void ref() throws IOException { var sql = ref("a"); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteralMatcher.java new file mode 100644 index 0000000..36fe6be --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlIntLiteralMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.sql.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher imports + +public final class SqlIntLiteralMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher isSqlIntLiteral() { + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private SqlIntLiteralMatcher( + 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.sql.ast.SqlIntLiteral.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher withValue(org.zwobble.precisely.Matcher value) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteral::value, (org.zwobble.precisely.Matcher) value)); + return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIntLiteralMatcher body +} -- cgit v1.2.3