jsexportを追加

This commit is contained in:
usbharu 2024-11-15 14:56:40 +09:00
parent 8beae5e426
commit ecff061924
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
7 changed files with 114 additions and 98 deletions

View File

@ -27,12 +27,11 @@ kotlin {
linuxX64()
mingwX64()
@OptIn(ExperimentalWasmDsl::class)
wasmWasi {
nodejs()
}
js {
browser()
nodejs()
binaries.library()
generateTypeScriptDefinitions()
}
sourceSets {

View File

@ -1,71 +1,74 @@
package dev.usbharu.markdown
import kotlin.collections.List
import kotlin.js.JsExport
@JsExport
sealed class AstNode {
open fun print(): String {
return toString()
}
}
data class RootNode(val node: AstNode) : AstNode() {
override fun print(): String {
return node.print()
data class RootNode(val node: AstNode) : AstNode() {
override fun print(): String {
return node.print()
}
}
data class BodyNode(val body: List<AstNode>) : AstNode() {
override fun print(): String {
return body.joinToString("\n") { it.print() }
}
}
sealed class BlockNode : AstNode()
data class HeaderNode(val header: Int, val headerTextNode: HeaderTextNode?) : BlockNode() {
override fun print(): String {
return "#".repeat(header) + " " + headerTextNode?.print().orEmpty()
}
}
data class HeaderTextNode(val text: String) : BlockNode() {
override fun print(): String {
return text
}
}
sealed interface QuotableNode
data class QuoteNode(val nodes: List<QuotableNode>) : AstNode(), QuotableNode
data object SeparatorNode : BlockNode() {
override fun print(): String {
return "---"
}
}
sealed class ListNode : BlockNode()
data class DiscListNode(val node: InlineNode, val childList: List<ListNode>) : ListNode()
data class DecimalListNode(val node: InlineNode, val childList: List<ListNode>) : ListNode()
data class ParagraphNode(val nodes: List<InlineNode>) : ListNode() {
override fun print(): String {
return nodes.joinToString("\n") { it.print() }
}
}
sealed class InlineNode : AstNode(), QuotableNode
data class ItalicNode(val nodes: MutableList<InlineNode>) : InlineNode() {
override fun print(): String {
return nodes.joinToString("", prefix = "*", postfix = "*") { it.print() }
}
}
data class BoldNode(val nodes: MutableList<InlineNode>) : InlineNode() {
override fun print(): String {
return nodes.joinToString("", prefix = "**", postfix = "**") { it.print() }
}
}
data class StrikeThroughNode(val nodes: List<InlineNode>) : InlineNode()
data class PlainText(val text: String) : InlineNode() {
override fun print(): String {
return text
}
}
}
data class BodyNode(val body: List<AstNode>) : AstNode() {
override fun print(): String {
return body.joinToString("\n") { it.print() }
}
}
sealed class BlockNode : AstNode()
data class HeaderNode(val header: Int, val headerTextNode: HeaderTextNode?) : BlockNode() {
override fun print(): String {
return "#".repeat(header) + " " + headerTextNode?.print().orEmpty()
}
}
data class HeaderTextNode(val text: String) : BlockNode() {
override fun print(): String {
return text
}
}
sealed interface QuotableNode
data class QuoteNode(val nodes: List<QuotableNode>) : AstNode(), QuotableNode
data object SeparatorNode : BlockNode() {
override fun print(): String {
return "---"
}
}
sealed class ListNode : BlockNode()
data class DiscListNode(val node: InlineNode, val childList: List<ListNode>) : ListNode()
data class DecimalListNode(val node: InlineNode, val childList: List<ListNode>) : ListNode()
data class ParagraphNode(val nodes: List<InlineNode>) : ListNode() {
override fun print(): String {
return nodes.joinToString("\n") { it.print() }
}
}
sealed class InlineNode : AstNode(), QuotableNode
data class ItalicNode(val nodes: MutableList<InlineNode>) : InlineNode() {
override fun print(): String {
return nodes.joinToString("", prefix = "*", postfix = "*") { it.print() }
}
}
data class BoldNode(val nodes: MutableList<InlineNode>) : InlineNode() {
override fun print(): String {
return nodes.joinToString("", prefix = "**", postfix = "**") { it.print() }
}
}
data class StrikeThroughNode(val nodes: List<InlineNode>) : InlineNode()
data class PlainText(val text: String) : InlineNode() {
override fun print(): String {
return text
}
}

View File

@ -1,7 +1,10 @@
package dev.usbharu.markdown
import dev.usbharu.markdown.Token.*
import kotlin.collections.List
import kotlin.js.JsExport
@JsExport
class Lexer {
fun lex(input: String): List<Token> {
val tokens = mutableListOf<Token>()

View File

@ -1,7 +1,11 @@
package dev.usbharu.markdown
import dev.usbharu.markdown.AstNode.*
import dev.usbharu.markdown.Token.*
import kotlin.collections.List
import kotlin.js.JsExport
@JsExport
class Parser {
fun parse(tokens: List<Token>): AstNode {
val iterator = PeekableTokenIterator(tokens)
@ -18,7 +22,7 @@ class Parser {
Exclamation -> TODO()
is Header -> header(next, iterator)
is Html -> TODO()
is dev.usbharu.markdown.List -> TODO()
is Token.List -> TODO()
ParenthesesEnd -> TODO()
ParenthesesStart -> TODO()
is Quote -> TODO()

View File

@ -1,40 +1,44 @@
package dev.usbharu.markdown
sealed class Token()
import kotlin.js.JsExport
data class Text(var text: String) : Token()
data class Break(var count: Int) : Token()
data class Header(var count: Int) : Token()
data class Quote(var count: Int) : Token()
data class Separator(var count: Int, val char: Char) : Token()
data class Whitespace(var count: Int, val whitespace: Char) : Token()
abstract class List(val type: ListType) : Token() {
enum class ListType {
DISC,
DECIMAL
@JsExport
sealed class Token {
data class Text(var text: String) : Token()
data class Break(var count: Int) : Token()
data class Header(var count: Int) : Token()
data class Quote(var count: Int) : Token()
data class Separator(var count: Int, val char: Char) : Token()
data class Whitespace(var count: Int, val whitespace: Char) : Token()
abstract class List(val type: ListType) : Token() {
enum class ListType {
DISC,
DECIMAL
}
}
data object DiscList : List(ListType.DISC)
data class DecimalList(val number: Char) : List(ListType.DECIMAL)
data class CheckBox(val checked: Boolean) : Token()
data object SquareBracketStart : Token()
data object SquareBracketEnd : Token()
data object ParenthesesStart : Token()
data object ParenthesesEnd : Token()
data class Url(var url: String) : Token()
data class Asterisk(var count: Int, var char: Char) : Token()
data object Exclamation : Token()
data class UrlTitle(val title: String) : Token()
data class InlineCodeBlock(val text: String) : Token()
data class CodeBlock(val text: String) : Token()
data class CodeBlockLanguage(val language: String, val filename: String) : Token()
data class Strike(val strike: String) : Token()
abstract class Html() : Token()
data class StartTagStart(var tag: String, val void: Boolean) : Html()
data class EndTagStart(var tag: String) : Html()
data class TagEnd(var tag: String) : Html()
data class AttributeName(val name: String) : Html()
data class AttributeValue(val value: String) : Html()
data class HtmlValue(val value: String) : Html()
}
data object DiscList : List(ListType.DISC)
data class DecimalList(val number: Char) : List(ListType.DECIMAL)
data class CheckBox(val checked: Boolean) : Token()
data object SquareBracketStart : Token()
data object SquareBracketEnd : Token()
data object ParenthesesStart : Token()
data object ParenthesesEnd : Token()
data class Url(var url: String) : Token()
data class Asterisk(var count: Int, var char: Char) : Token()
data object Exclamation : Token()
data class UrlTitle(val title: String) : Token()
data class InlineCodeBlock(val text: String) : Token()
data class CodeBlock(val text: String) : Token()
data class CodeBlockLanguage(val language: String, val filename: String) : Token()
data class Strike(val strike: String) : Token()
abstract class Html() : Token()
data class StartTagStart(var tag: String, val void: Boolean) : Html()
data class EndTagStart(var tag: String) : Html()
data class TagEnd(var tag: String) : Html()
data class AttributeName(val name: String) : Html()
data class AttributeValue(val value: String) : Html()
data class HtmlValue(val value: String) : Html()

View File

@ -1,5 +1,6 @@
package dev.usbharu.markdown
import dev.usbharu.markdown.Token.*
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

View File

@ -1,5 +1,7 @@
package dev.usbharu.markdown
import dev.usbharu.markdown.AstNode.*
import dev.usbharu.markdown.Token.*
import kotlin.test.Test
import kotlin.test.assertEquals