From edaafcc1cce385902bb8e5e70c4c7dae111ed482 Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Tue, 17 Sep 2024 20:27:15 +0200 Subject: [PATCH] Make HorizontalRule, Comment, Newline and BlockText data objects --- .../iamgio/quarkdown/ast/base/block/BlockText.kt | 4 ++-- .../quarkdown/ast/base/block/HorizontalRule.kt | 4 +--- .../eu/iamgio/quarkdown/ast/base/block/Newline.kt | 4 +--- .../eu/iamgio/quarkdown/ast/base/inline/Comment.kt | 4 +--- .../value/output/node/NodeOutputValueVisitor.kt | 2 +- .../eu/iamgio/quarkdown/parser/BlockTokenParser.kt | 6 +++--- .../iamgio/quarkdown/parser/InlineTokenParser.kt | 2 +- .../eu/iamgio/quarkdown/HtmlNodeRendererTest.kt | 14 +++++++------- .../main/kotlin/eu/iamgio/quarkdown/stdlib/Flow.kt | 2 +- .../eu/iamgio/quarkdown/test/FullPipelineTest.kt | 2 +- 10 files changed, 19 insertions(+), 25 deletions(-) diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/BlockText.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/BlockText.kt index 229f710c..fbea3e61 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/BlockText.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/BlockText.kt @@ -4,8 +4,8 @@ import eu.iamgio.quarkdown.ast.Node import eu.iamgio.quarkdown.visitor.node.NodeVisitor /** - * Anything else (should not happen). + * Any unknown node type (should not happen). */ -class BlockText : Node { +data object BlockText : Node { override fun accept(visitor: NodeVisitor) = visitor.visit(this) } diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/HorizontalRule.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/HorizontalRule.kt index 2ca8e351..986af3cc 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/HorizontalRule.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/HorizontalRule.kt @@ -6,8 +6,6 @@ import eu.iamgio.quarkdown.visitor.node.NodeVisitor /** * A horizontal line (thematic break). */ -class HorizontalRule : Node { - override fun toString() = "HorizontalRule" - +data object HorizontalRule : Node { override fun accept(visitor: NodeVisitor) = visitor.visit(this) } diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/Newline.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/Newline.kt index 999cbdac..13341df6 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/Newline.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/block/Newline.kt @@ -6,8 +6,6 @@ import eu.iamgio.quarkdown.visitor.node.NodeVisitor /** * A blank line. */ -class Newline : Node { - override fun toString() = "Newline" - +data object Newline : Node { override fun accept(visitor: NodeVisitor) = visitor.visit(this) } diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/inline/Comment.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/inline/Comment.kt index 40840cb8..fe25c780 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/inline/Comment.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/ast/base/inline/Comment.kt @@ -6,8 +6,6 @@ import eu.iamgio.quarkdown.visitor.node.NodeVisitor /** * A comment whose content is ignored. */ -class Comment : Node { - override fun toString() = "Comment" - +data object Comment : Node { override fun accept(visitor: NodeVisitor) = visitor.visit(this) } diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/function/value/output/node/NodeOutputValueVisitor.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/function/value/output/node/NodeOutputValueVisitor.kt index 5bd76787..23943af1 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/function/value/output/node/NodeOutputValueVisitor.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/function/value/output/node/NodeOutputValueVisitor.kt @@ -53,7 +53,7 @@ abstract class NodeOutputValueVisitor : OutputValueVisitor { override fun visit(value: NodeValue) = value.unwrappedValue - override fun visit(value: VoidValue) = BlockText() + override fun visit(value: VoidValue) = BlockText // Dynamic output (e.g. produced by the stdlib function `.function`) is treated: // - If it is a suitable output value: its content is visited again with this visitor. diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/parser/BlockTokenParser.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/parser/BlockTokenParser.kt index 6a522c6d..9180a6aa 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/parser/BlockTokenParser.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/parser/BlockTokenParser.kt @@ -80,7 +80,7 @@ class BlockTokenParser(private val context: MutableContext) : BlockTokenVisitor< .tokenizeAndParse() override fun visit(token: NewlineToken): Node { - return Newline() + return Newline } override fun visit(token: BlockCodeToken): Node { @@ -110,7 +110,7 @@ class BlockTokenParser(private val context: MutableContext) : BlockTokenVisitor< } override fun visit(token: HorizontalRuleToken): Node { - return HorizontalRule() + return HorizontalRule } /** @@ -382,7 +382,7 @@ class BlockTokenParser(private val context: MutableContext) : BlockTokenVisitor< } override fun visit(token: BlockTextToken): Node { - return BlockText() + return BlockText } override fun visit(token: PageBreakToken): Node { diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/parser/InlineTokenParser.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/parser/InlineTokenParser.kt index 40661bde..425961d9 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/parser/InlineTokenParser.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/parser/InlineTokenParser.kt @@ -138,7 +138,7 @@ class InlineTokenParser(private val context: MutableContext) : InlineTokenVisito override fun visit(token: CommentToken): Node { // Content is ignored. - return Comment() + return Comment } override fun visit(token: LineBreakToken): Node { diff --git a/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlNodeRendererTest.kt b/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlNodeRendererTest.kt index 4f6a8c94..450ea93d 100644 --- a/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlNodeRendererTest.kt +++ b/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlNodeRendererTest.kt @@ -89,7 +89,7 @@ class HtmlNodeRendererTest { // Inline @Test fun comment() { - assertEquals("", Comment().render()) + assertEquals("", Comment.render()) } @Test @@ -391,7 +391,7 @@ class HtmlNodeRendererTest { @Test fun horizontalRule() { - assertEquals("
", HorizontalRule().render()) + assertEquals("
", HorizontalRule.render()) } @Test @@ -443,7 +443,7 @@ class HtmlNodeRendererTest { children = listOf( Paragraph(listOf(Text("A1"))), - HorizontalRule(), + HorizontalRule, Paragraph(listOf(Text("A2"))), ), ), @@ -451,7 +451,7 @@ class HtmlNodeRendererTest { children = listOf( Paragraph(listOf(Text("B1"))), - HorizontalRule(), + HorizontalRule, Paragraph(listOf(Text("B2"))), ), ), @@ -459,7 +459,7 @@ class HtmlNodeRendererTest { children = listOf( Paragraph(listOf(Text("C1"))), - HorizontalRule(), + HorizontalRule, Paragraph(listOf(Text("C2"))), ), ), @@ -468,7 +468,7 @@ class HtmlNodeRendererTest { children = listOf( Paragraph(listOf(Text("D1"))), - HorizontalRule(), + HorizontalRule, Paragraph(listOf(Text("D2"))), ), ), @@ -476,7 +476,7 @@ class HtmlNodeRendererTest { isChecked = true, listOf( Paragraph(listOf(Text("E1"))), - HorizontalRule(), + HorizontalRule, Paragraph(listOf(Text("E2"))), ), ), diff --git a/stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Flow.kt b/stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Flow.kt index 010d1809..112f8b2b 100644 --- a/stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Flow.kt +++ b/stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Flow.kt @@ -199,4 +199,4 @@ fun let( * in case the visible output does not match the expected one. * @return an invisible mock node */ -fun node(): NodeValue = BlockText().wrappedAsValue() +fun node(): NodeValue = BlockText.wrappedAsValue() diff --git a/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt b/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt index 9dcfd932..a85280f5 100644 --- a/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt +++ b/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt @@ -1157,7 +1157,7 @@ class FullPipelineTest { "

Error: sum

" + "

" + "Cannot call function sum" + - "" + "(Number a, Number b)" + "" +