diff options
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java | 10 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java | 45 |
2 files changed, 53 insertions, 2 deletions
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 31ead2a..1bafe96 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java @@ -166,11 +166,17 @@ public class Parser { tokens.skip(TokenType.SYMBOL_BRACE_OPEN); var variants = parseMany( - () -> tokens.isNext(TokenType.SYMBOL_BRACE_CLOSE), + () -> !tokens.isNext(TokenType.KEYWORD_VARIANT), () -> parseSumVariantDefinition(tokens), () -> true ); + var fields = parseMany( + () -> !tokens.isNext(TokenType.KEYWORD_FIELD), + () -> parseStructFieldDefinition(tokens), + () -> true + ); + tokens.skip(TokenType.SYMBOL_BRACE_CLOSE); var end = tokens.endPosition(); @@ -179,7 +185,7 @@ public class Parser { return new UntypedSumDefinitionNode( name.charSequence().toString(), variants, - List.of(), + fields, docComment, source ); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java index 34b94fc..20de7ba 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java @@ -6,6 +6,9 @@ import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNodeMatcher.isUntypedStructFieldDefinitionNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNodeMatcher.isUntypedSumDefinitionNode; +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.*; @@ -82,6 +85,48 @@ public class ParserSumDefinitionTests { } @Test + public void emptySumHasNoFields() { + var source = """ + sum Shape { + }"""; + + var node = parseString( + source, + Parser::parseNamespaceStatement + ); + + assertThat(node, instanceOf( + UntypedSumDefinitionNode.class, + has("fields", UntypedSumDefinitionNode::fields, isSequence()) + )); + } + + @Test + public void sumWithFields() { + var source = """ + sum Shape { + field height: Int32; + field width: Int64; + }"""; + + var node = parseString( + source, + Parser::parseNamespaceStatement + ); + + assertThat(node, isUntypedSumDefinitionNode() + .withFields(isSequence( + isUntypedStructFieldDefinitionNode() + .withName(equalTo("height")) + .withType(isUntypedTypeLevelReferenceNode().withName(equalTo("Int32"))), + isUntypedStructFieldDefinitionNode() + .withName(equalTo("width")) + .withType(isUntypedTypeLevelReferenceNode().withName(equalTo("Int64"))) + )) + ); + } + + @Test public void sumWithDocComment() { var source = """ /// A 2D shape. |
