diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-05-17 10:05:28 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-05-17 10:05:28 +0100 |
| commit | 23985cff89b03270d429aedd1a7b5ccb0ec66073 (patch) | |
| tree | a12843a27b00d7f4e89730db9851b219e7c6b89c /src | |
| parent | fe9d820d35bd3759d390593fc08ec241fbb21680 (diff) | |
Introduce DocComment
Diffstat (limited to 'src')
16 files changed, 65 insertions, 31 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java new file mode 100644 index 0000000..f7a8ac2 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java @@ -0,0 +1,9 @@ +package org.zwobble.hobgoblin.compiler.ast; + +public record DocComment(String value) { + public static final DocComment EMPTY = new DocComment(""); + + public Iterable<String> lines() { + return () -> value.lines().iterator(); + } +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedStructDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedStructDefinitionNode.java index 0472496..dc9a8fe 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedStructDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedStructDefinitionNode.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.ast.typed; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.sources.Source; import org.zwobble.hobgoblin.compiler.types.StructType; @@ -8,7 +9,7 @@ import java.util.List; public record TypedStructDefinitionNode( StructType type, List<TypedStructFieldDefinitionNode> fields, - String docComment, + DocComment docComment, Source source ) implements TypedNamespaceStatementNode { public String name() { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedSumDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedSumDefinitionNode.java index d795430..4022bdd 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedSumDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedSumDefinitionNode.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.ast.typed; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.sources.Source; import org.zwobble.hobgoblin.compiler.types.SumType; @@ -8,7 +9,7 @@ import java.util.List; public record TypedSumDefinitionNode( SumType type, List<TypedSumVariantDefinitionNode> variants, - String docComment, + DocComment docComment, Source source ) implements TypedNamespaceStatementNode { public String name() { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java index 139db67..179aca2 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.ast.untyped; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.sources.Source; import java.util.List; @@ -7,7 +8,7 @@ import java.util.List; public record UntypedStructDefinitionNode( String name, List<UntypedStructFieldDefinitionNode> fields, - String docComment, + DocComment docComment, Source source ) implements UntypedNamespaceStatementNode { } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumDefinitionNode.java index 6ffcb6f..842dc0c 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumDefinitionNode.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.ast.untyped; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.sources.Source; import java.util.List; @@ -7,7 +8,7 @@ import java.util.List; public record UntypedSumDefinitionNode( String name, List<UntypedSumVariantDefinitionNode> variants, - String docComment, + DocComment docComment, Source source ) implements UntypedNamespaceStatementNode { } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java index fed544d..adf231d 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.output.lang.java; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.output.CodeWriter; import org.zwobble.hobgoblin.compiler.output.lang.java.ast.*; @@ -44,9 +45,8 @@ public class JavaWriter implements AutoCloseable { } } - private void writeDocComment(String docComment) throws IOException { - Iterable<String> lines = docComment.lines()::iterator; - for (var line : lines) { + private void writeDocComment(DocComment docComment) throws IOException { + for (var line : docComment.lines()) { this.writer.write("/// "); this.writer.write(line); this.writer.write("\n"); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java index 298e31f..948be8d 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java @@ -1,10 +1,12 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; +import org.zwobble.hobgoblin.compiler.ast.DocComment; + import java.util.List; public record JavaClassDeclaration( String name, List<JavaClassBodyDeclaration> body, - String docComment + DocComment docComment ) implements JavaTypeDeclaration { } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java index 2fe3a8d..2f5a5b4 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java @@ -1,10 +1,12 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; +import org.zwobble.hobgoblin.compiler.ast.DocComment; + import java.util.List; public record JavaInterfaceDeclaration( String name, List<JavaTypeRef> permitsTypes, - String docComment + DocComment docComment ) implements JavaTypeDeclaration { } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaRecordDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaRecordDeclaration.java index 10b80a5..4d7e033 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaRecordDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaRecordDeclaration.java @@ -1,11 +1,13 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; +import org.zwobble.hobgoblin.compiler.ast.DocComment; + import java.util.List; public record JavaRecordDeclaration( String name, List<JavaRecordComponent> components, List<JavaTypeRef> implementsTypes, - String docComment + DocComment docComment ) implements JavaTypeDeclaration { } 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 c50c4d6..d2594ee 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.parser; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.ast.untyped.*; import org.zwobble.hobgoblin.compiler.sources.FileFragmentSource; import org.zwobble.hobgoblin.compiler.sources.Source; @@ -60,7 +61,7 @@ public class Parser { } } - private static String parseDocComment(TokenIterator tokens) { + private static DocComment parseDocComment(TokenIterator tokens) { var docComment = new StringBuilder(); while (tokens.isNext(TokenType.DOC_COMMENT)) { @@ -76,12 +77,12 @@ public class Parser { docComment.append(docCommentContents); } - return docComment.toString(); + return new DocComment(docComment.toString()); } private static UntypedStructDefinitionNode parseStructDefinition( TokenIterator tokens, - String docComment + DocComment docComment ) { var start = tokens.startPosition(); @@ -131,7 +132,7 @@ public class Parser { private static UntypedSumDefinitionNode parseSumDefinition( TokenIterator tokens, - String docComment + DocComment docComment ) { var start = tokens.startPosition(); 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 36e7202..a6f7e45 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 @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.java; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.output.CodeWriter; import org.zwobble.hobgoblin.compiler.output.lang.java.ast.*; @@ -17,7 +18,7 @@ public class JavaWriterTests { var java = new JavaClassDeclaration( "Rectangle", List.of(), - "" + DocComment.EMPTY ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -42,7 +43,7 @@ public class JavaWriterTests { JavaTypeRef.VOID ) ), - "" + DocComment.EMPTY ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -63,7 +64,7 @@ public class JavaWriterTests { var java = new JavaClassDeclaration( "Point", List.of(), - "A 2D point." + new DocComment("A 2D point.") ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -80,7 +81,7 @@ public class JavaWriterTests { var java = new JavaInterfaceDeclaration( "Shape", List.of(), - "" + DocComment.EMPTY ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -99,7 +100,7 @@ public class JavaWriterTests { new JavaTypeRef(List.of("abc", "def"), "One", List.of()), new JavaTypeRef(List.of("abc", "def"), "Two", List.of()) ), - "" + DocComment.EMPTY ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -115,7 +116,7 @@ public class JavaWriterTests { var java = new JavaInterfaceDeclaration( "Shape", List.of(), - "A 2D shape." + new DocComment("A 2D shape.") ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -133,7 +134,7 @@ public class JavaWriterTests { "Point", List.of(), List.of(), - "" + DocComment.EMPTY ); var string = write(writer -> writer.writeTypeDeclaration(java)); @@ -150,7 +151,7 @@ public class JavaWriterTests { "Point", List.of(), List.of(), - "A 2D point." + new DocComment("A 2D point.") ); var string = write(writer -> writer.writeTypeDeclaration(java)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java index a0394ce..6c90356 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.parser; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode; @@ -87,7 +88,11 @@ public class ParserStructDefinitionTests { assertThat(node, instanceOf( UntypedStructDefinitionNode.class, - has("docComment", UntypedStructDefinitionNode::docComment, equalTo("A 2D point.\n\nPositive y is up.\n")) + has( + "docComment", + UntypedStructDefinitionNode::docComment, + equalTo(new DocComment("A 2D point.\n\nPositive y is up.\n")) + ) )); } } 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 c512c6f..34b94fc 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.parser; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode; @@ -94,7 +95,11 @@ public class ParserSumDefinitionTests { assertThat(node, instanceOf( UntypedSumDefinitionNode.class, - has("docComment", UntypedSumDefinitionNode::docComment, equalTo("A 2D shape.\n")) + has( + "docComment", + UntypedSumDefinitionNode::docComment, + equalTo(new DocComment("A 2D shape.\n")) + ) )); } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java index 1bb84d1..de0ffec 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceNode; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceNode; @@ -23,7 +24,7 @@ public class TypeCheckerNamespaceTests { new UntypedStructDefinitionNode( "X", List.of(), - "", + DocComment.EMPTY, NullSource.INSTANCE ) ), diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java index 117bce4..336c496 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceNode; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructFieldDefinitionNode; @@ -27,7 +28,7 @@ public class TypeCheckerStructDefinitionTests { var untyped = new UntypedStructDefinitionNode( "X", List.of(), - "", + DocComment.EMPTY, NullSource.INSTANCE ); var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); @@ -54,7 +55,7 @@ public class TypeCheckerStructDefinitionTests { new UntypedStructFieldDefinitionNode("a", UntypedArb.typeLevelReference("Int32"), NullSource.INSTANCE), new UntypedStructFieldDefinitionNode("b", UntypedArb.typeLevelReference("Int64"), NullSource.INSTANCE) ), - "", + DocComment.EMPTY, NullSource.INSTANCE ); var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); @@ -104,7 +105,7 @@ public class TypeCheckerStructDefinitionTests { List.of( new UntypedStructFieldDefinitionNode("value", UntypedArb.typeLevelReference("Y"), NullSource.INSTANCE) ), - "", + DocComment.EMPTY, NullSource.INSTANCE ), new UntypedStructDefinitionNode( @@ -112,7 +113,7 @@ public class TypeCheckerStructDefinitionTests { List.of( new UntypedStructFieldDefinitionNode("value", UntypedArb.typeLevelReference("Int32"), NullSource.INSTANCE) ), - "", + DocComment.EMPTY, NullSource.INSTANCE ) ), diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java index a037bdf..1d968d6 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.ast.typed.*; import org.zwobble.hobgoblin.compiler.ast.untyped.*; import org.zwobble.hobgoblin.compiler.sources.NullSource; @@ -18,7 +19,7 @@ public class TypeCheckerSumDefinitionTests { var untyped = new UntypedSumDefinitionNode( "X", List.of(), - "", + DocComment.EMPTY, NullSource.INSTANCE ); var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); @@ -45,7 +46,7 @@ public class TypeCheckerSumDefinitionTests { new UntypedSumVariantDefinitionNode(UntypedArb.typeLevelReference("Rectangle"), NullSource.INSTANCE), new UntypedSumVariantDefinitionNode(UntypedArb.typeLevelReference("Triangle"), NullSource.INSTANCE) ), - "", + DocComment.EMPTY, NullSource.INSTANCE ); var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); |
