From a14906e7299a230bab5a8cfb6b73fcbd05d8b688 Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 12 Nov 2024 21:44:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=AA=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/markdown/Lexer.kt | 35 ++++++++- .../kotlin/dev/usbharu/markdown/Token.kt | 6 +- .../kotlin/dev/usbharu/markdown/LexerTest.kt | 77 +++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt index a322f7f..0127ff4 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt @@ -20,12 +20,23 @@ class Lexer { '>', '>' -> quote(iterator, tokens) '-', '=', 'ー', '=' -> { if (iterator.peekOrNull()?.isWhitespace() == true) { //-の直後がスペースならリストの可能性 - list(iterator, tokens, next) + list(iterator, tokens) } else {//それ以外ならセパレーターの可能性 separator(next, iterator, tokens) } } + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> + decimalList(iterator, tokens, next) + + '[', '「' -> { + tokens.add(SquareBracketStart) + } + + ']', '」' -> { + tokens.add(SquareBracketEnd) + } + ' ', ' ' -> { tokens.add(Whitespace(skipWhitespace(iterator) + 1, next)) //nextの分1足す } @@ -53,11 +64,31 @@ class Lexer { return tokens } - private fun list( + private fun decimalList( iterator: PeekableCharIterator, tokens: MutableList, next: Char ) { + val comma = iterator.peekOrNull() + if (comma == null) { + tokens.add(Text(next.toString())) + return + } + if (comma == '.' || comma == '。' || comma == '、') { + iterator.next() + if (iterator.peekOrNull()?.isWhitespace() == true) { + iterator.next() + tokens.add(DecimalList(next)) + return + } + } + tokens.add(Text(next + "" + comma)) + } + + private fun list( + iterator: PeekableCharIterator, + tokens: MutableList + ) { if (iterator.peekOrNull()?.isWhitespace() == true) { tokens.add(DiscList) diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt index 3f290a9..3552e0e 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt @@ -16,5 +16,7 @@ abstract class List(val type: ListType) : Token() { } data object DiscList : List(ListType.DISC) -data class DecimalList(val number: Int) : List(ListType.DECIMAL) -data class CheckBox(val checked:Boolean): Token() \ No newline at end of file +data class DecimalList(val number: Char) : List(ListType.DECIMAL) +data class CheckBox(val checked: Boolean) : Token() +data object SquareBracketStart : Token() +data object SquareBracketEnd : Token() \ No newline at end of file diff --git a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt index afb1b14..58785dd 100644 --- a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt +++ b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt @@ -331,4 +331,81 @@ class LexerTest { assertContentEquals(listOf(DiscList, Text("aiueo"), Break(1), DiscList, Text("abcd")), actual) } + + @Test + fun ディスクリストネスト() { + val lexer = Lexer() + + val actual = lexer.lex("- aiueo\n - abcd") + + println(actual) + + assertContentEquals( + listOf(DiscList, Text("aiueo"), Break(1), Whitespace(4, ' '), DiscList, Text("abcd")), + actual + ) + } + + @Test + fun 数字リスト() { + val lexer = Lexer() + + val actual = lexer.lex("1. aiueo\n 2. abcd") + + println(actual) + + assertContentEquals( + listOf( + DecimalList('1'), + Text("aiueo"), + Break(1), + Whitespace(4, ' '), + DecimalList('2'), + Text("abcd") + ), + actual + ) + } + + @Test + fun 全角数字リスト() { + val lexer = Lexer() + + val actual = lexer.lex("1. aiueo\n 2. abcd") + + println(actual) + + assertContentEquals( + listOf( + DecimalList('1'), + Text("aiueo"), + Break(1), + Whitespace(4, ' '), + DecimalList('2'), + Text("abcd") + ), + actual + ) + } + + @Test + fun 全角コンマリスト() { + val lexer = Lexer() + + val actual = lexer.lex("1。 aiueo\n 2、 abcd") + + println(actual) + + assertContentEquals( + listOf( + DecimalList('1'), + Text("aiueo"), + Break(1), + Whitespace(4, ' '), + DecimalList('2'), + Text("abcd") + ), + actual + ) + } } \ No newline at end of file