diff options
Diffstat (limited to 'src/main/java/org/zwobble')
12 files changed, 73 insertions, 11 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java index fd332d7..34c305d 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedConstructedTypeNode(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode receiver, java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode> args, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceNode.java index afa5b0e..b5771c2 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedNamespaceNode(org.zwobble.hobgoblin.compiler.types.NamespaceName namespaceName, java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode> body, org.zwobble.hobgoblin.compiler.sources.Source source) { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java index 0627a6b..a7be931 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public sealed interface UntypedNamespaceStatementNode permits org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNode { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java index 9b99c32..ee745b6 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedNativeTypeDefinitionNode(String name, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode { 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 8611651..242a8e1 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,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedStructDefinitionNode(String name, java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode> fields, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java index f221bec..795ea6f 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedStructFieldDefinitionNode(String name, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode type, org.zwobble.hobgoblin.compiler.sources.Source source) { 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 8f5520b..c7a9f53 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,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedSumDefinitionNode(String name, java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode> variants, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java index f4edd89..9bb2411 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedSumVariantDefinitionNode(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode type, org.zwobble.hobgoblin.compiler.sources.Source source) { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelExpressionNode.java index 67f56e6..b5dbd5d 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelExpressionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelExpressionNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public sealed interface UntypedTypeLevelExpressionNode permits org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelReferenceNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelReferenceNode.java index bcde610..98dcce6 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelReferenceNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedTypeLevelReferenceNode.java @@ -1,3 +1,5 @@ +// Generated by hobgoblin. + package org.zwobble.hobgoblin.compiler.ast.untyped; public record UntypedTypeLevelReferenceNode(String name, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java index b8b0709..ac83084 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java @@ -9,6 +9,11 @@ import java.nio.file.Path; import java.util.*; public class CodeWriter implements AutoCloseable { + public interface CodeWriterFile { + String readString() throws IOException; + Writer openWriter() throws IOException; + } + public record CustomAreas( Map<String, List<String>> existing, String customAreaStart, @@ -59,23 +64,44 @@ public class CodeWriter implements AutoCloseable { public static CodeWriter file( Path path, - String customAreaStart, - String customAreaEnd + String lineCommentStart + ) throws IOException { + return file( + new CodeWriterFile() { + @Override + public String readString() throws IOException { + return Files.readString(path); + } + + @Override + public Writer openWriter() throws IOException { + Files.createDirectories(path.getParent()); + return new FileWriter(path.toFile()); + } + }, + lineCommentStart + ); + } + + public static CodeWriter file( + CodeWriterFile file, + String lineCommentStart ) throws IOException { - var customAreas = readCustomAreas(path, customAreaStart, customAreaEnd); - Files.createDirectories(path.getParent()); - var writer = new FileWriter(path.toFile()); + var customAreaStart = lineCommentStart + " Custom area start: "; + var customAreaEnd = lineCommentStart + " Custom area end: "; + var customAreas = readCustomAreas(file, customAreaStart, customAreaEnd); + var writer = file.openWriter(); // TODO: warn if custom areas are unused? - return new CodeWriter(writer, customAreas); + return new CodeWriter(writer, lineCommentStart, customAreas); } private static CustomAreas readCustomAreas( - Path path, + CodeWriterFile file, String customAreaStart, String customAreaEnd ) throws IOException { try { - var contents = Files.readString(path); + var contents = file.readString(); return CustomAreas.read(contents, customAreaStart, customAreaEnd); } catch (NoSuchFileException exception) { return CustomAreas.empty(customAreaStart, customAreaEnd); @@ -83,17 +109,24 @@ public class CodeWriter implements AutoCloseable { } private final Writer writer; + private final String lineCommentStart; private final CustomAreas customAreas; private int indent; private boolean pendingIndent; - public CodeWriter(Writer writer, CustomAreas customAreas) { + public CodeWriter(Writer writer, String lineCommentStart, CustomAreas customAreas) { this.writer = writer; + this.lineCommentStart = lineCommentStart; this.customAreas = customAreas; this.indent = 0; this.pendingIndent = false; } + public void writeHeader() throws IOException { + writer.write(lineCommentStart); + writer.write(" Generated by hobgoblin.\n\n"); + } + public void write(String value) throws IOException { if (this.pendingIndent) { for (var i = 0; i < this.indent; i++) { 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 7dbcdfc..3b7987b 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 @@ -9,11 +9,19 @@ import java.io.StringWriter; import java.nio.file.Path; public class JavaWriter implements AutoCloseable { + private static final String LINE_COMMENT_START = "//"; + + public static JavaWriter file(CodeWriter.CodeWriterFile file) throws IOException { + return new JavaWriter(CodeWriter.file( + file, + LINE_COMMENT_START + )); + } + public static JavaWriter file(Path path) throws IOException { return new JavaWriter(CodeWriter.file( path, - "// Custom area start: ", - "// Custom area end: " + LINE_COMMENT_START )); } @@ -29,6 +37,7 @@ public class JavaWriter implements AutoCloseable { } public void writeCompilationUnit(JavaCompilationUnit compilationUnit) throws IOException { + this.writer.writeHeader(); this.writer.write("package "); this.writer.write(String.join(".", compilationUnit.packageName())); this.writer.write(";"); |
