diff options
7 files changed, 42 insertions, 12 deletions
diff --git a/examples/18-database/src/data.hob b/examples/18-database/src/data.hob index 3c3ba47..cad23dd 100644 --- a/examples/18-database/src/data.hob +++ b/examples/18-database/src/data.hob @@ -5,7 +5,7 @@ struct StructWithInt32 { databaseSchema Data { table structs { -// row StructWithInt32; + row StructWithInt32; } // // query fetchAllStructs() { diff --git a/hobgoblin/src/ast/untyped.hob b/hobgoblin/src/ast/untyped.hob index 150f78c..d01e331 100644 --- a/hobgoblin/src/ast/untyped.hob +++ b/hobgoblin/src/ast/untyped.hob @@ -92,6 +92,7 @@ sum UntypedDatabaseSchemaStatementNode { struct UntypedTableDefinitionNode { field name: String; + field rowType: UntypedTypeLevelExpressionNode; field docComment: DocComment; field source: Source; } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNode.java index 4841ef8..faaef14 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNode.java @@ -7,32 +7,42 @@ package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedTableDefinitionNode( String name, + org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode rowType, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source ) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedDatabaseSchemaStatementNode { public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder("", org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource()); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder("", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource()); } public record Builder( String name, + org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode rowType, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source ) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedDatabaseSchemaStatementNode.Builder { public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode build() { - return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode(name, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode(name, rowType, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder withName(String name) { - return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, rowType, docComment, source); + } + + public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder withRowType(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode rowType) { + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, rowType, docComment, source); + } + + public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder withRowType(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode.Builder rowType) { + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, rowType.build(), docComment, source); } public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) { - return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, rowType, docComment, source); } public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) { - return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, docComment, source); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder(name, rowType, docComment, source); } // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode.Builder body diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java index 96448c6..23ba43b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java @@ -365,6 +365,11 @@ public class Parser { tokens.skip(TokenType.IDENTIFIER, "table"); var name = parseIdentifier(tokens); tokens.skip(TokenType.SYMBOL_BRACE_OPEN); + + tokens.skip(TokenType.IDENTIFIER, "row"); + var rowType = parseTypeLevelExpression(tokens); + tokens.skip(TokenType.SYMBOL_SEMICOLON); + tokens.skip(TokenType.SYMBOL_BRACE_CLOSE); var end = tokens.endPosition(); @@ -372,6 +377,7 @@ public class Parser { return new UntypedTableDefinitionNode( name, + rowType, docComment, source ); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNodeMatcher.java index 4c016a3..8e4226c 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNodeMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTableDefinitionNodeMatcher.java @@ -36,6 +36,12 @@ public final class UntypedTableDefinitionNodeMatcher implements org.zwobble.prec return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNodeMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNodeMatcher withRowType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode> rowType) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("rowType", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode::rowType, (org.zwobble.precisely.Matcher) rowType)); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNodeMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNodeMatcher withDocComment(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.DocComment> docComment) { var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode>>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("docComment", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNode::docComment, (org.zwobble.precisely.Matcher) docComment)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserDatabaseSchemaDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserDatabaseSchemaDefinitionTests.java index 8241eda..5c41fa1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserDatabaseSchemaDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserDatabaseSchemaDefinitionTests.java @@ -31,10 +31,12 @@ public class ParserDatabaseSchemaDefinitionTests { public void schemaWithStatements() { var source = """ databaseSchema BlogApp { - table Post { + table posts { + row Post; } - table Author { + table author { + row Author; } }"""; @@ -46,8 +48,8 @@ public class ParserDatabaseSchemaDefinitionTests { assertThat(node, isUntypedDatabaseSchemaDefinitionNode() .withName(equalTo("BlogApp")) .withBody(isSequence( - isUntypedTableDefinitionNode().withName(equalTo("Post")), - isUntypedTableDefinitionNode().withName(equalTo("Author")) + isUntypedTableDefinitionNode().withName(equalTo("posts")), + isUntypedTableDefinitionNode().withName(equalTo("author")) )) ); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserTableDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserTableDefinitionTests.java index b39d203..889b30c 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserTableDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserTableDefinitionTests.java @@ -3,6 +3,7 @@ package org.zwobble.hobgoblin.compiler.parser; import org.junit.jupiter.api.Test; import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTableDefinitionNodeMatcher.isUntypedTableDefinitionNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNodeMatcher.isUntypedTypeLevelReferenceNode; import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; @@ -11,7 +12,8 @@ public class ParserTableDefinitionTests { @Test public void minimalTable() { var source = """ - table Post { + table posts { + row Post; }"""; var node = parseString( @@ -19,6 +21,9 @@ public class ParserTableDefinitionTests { Parser::parseDatabaseSchemaStatement ); - assertThat(node, isUntypedTableDefinitionNode().withName(equalTo("Post"))); + assertThat(node, isUntypedTableDefinitionNode() + .withName(equalTo("posts")) + .withRowType(isUntypedTypeLevelReferenceNode().withName(equalTo("Post"))) + ); } } |
