From 6d49a4ad5eeb576a67dc679e3ac60fa0d1779050 Mon Sep 17 00:00:00 2001 From: usbharu Date: Wed, 13 Nov 2024 12:22:07 +0900 Subject: [PATCH] wip --- .../kotlin/dev/usbharu/markdown/Lexer.kt | 30 +++++----- .../kotlin/dev/usbharu/markdown/Token.kt | 2 +- .../kotlin/dev/usbharu/markdown/LexerTest.kt | 56 +++++++++++++++++-- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt index dedd93c..a7c7ff9 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt @@ -38,30 +38,17 @@ class Lexer { decimalList(iterator, tokens, next) next == '[' || next == '「' -> tokens.add(SquareBracketStart) - next == ']' || next == '」' -> tokens.add(SquareBracketEnd) - next == '(' || next == '(' -> tokens.add(ParenthesesStart) - next == ')' || next == ')' -> tokens.add(ParenthesesEnd) - next.isWhitespace() -> tokens.add( Whitespace( skipWhitespace(iterator) + 1, next ) ) //nextの分1足す - next == 'h' -> url(next, iterator, tokens) - - next == '*' -> { - var count = 1 - while (iterator.peekOrNull() == '*') { - count++ - iterator.next() - } - tokens.add(Asterisk(count)) - } + next == '*' || next == '_' -> asterisk(iterator, next, tokens) else -> { val lastToken = tokens.lastOrNull() @@ -91,16 +78,31 @@ class Lexer { return tokens } + private fun asterisk( + iterator: PeekableCharIterator, + next: Char, + tokens: MutableList, + ) { + var count = 1 + while (iterator.peekOrNull() == next) { + count++ + iterator.next() + } + tokens.add(Asterisk(count, next)) + } + private fun url( next: Char, iterator: PeekableCharIterator, tokens: MutableList, ) { //todo httpにも対応 + //todo nextでみずにpeekでみて確認してからnextする hのあとにアスタリスク等が来たときに対応できない val charIterator = "ttps://".iterator() val urlBuilder = StringBuilder() urlBuilder.append(next) while (charIterator.hasNext() && iterator.hasNext()) { +// charIterator val nextC = charIterator.next() val nextC2 = iterator.next() urlBuilder.append(nextC2) diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt index 36ff4a2..eed086b 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt @@ -23,4 +23,4 @@ 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) : Token() \ No newline at end of file +data class Asterisk(var count: Int, var char: Char) : 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 21114e4..6f38bc3 100644 --- a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt +++ b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt @@ -494,9 +494,9 @@ class LexerTest { assertContentEquals( listOf( - Asterisk(1), + Asterisk(1, '*'), Text("a"), - Asterisk(1) + Asterisk(1, '*') ), actual ) } @@ -512,9 +512,57 @@ class LexerTest { assertContentEquals( listOf( Quote(1), - Asterisk(1), + Asterisk(1, '*'), Text("a"), - Asterisk(1) + Asterisk(1, '*') + ), actual + ) + } + + @Test + fun アスタリスク複数() { + val lexer = Lexer() + + val actual = lexer.lex("**a**") + + println(actual) + + assertContentEquals( + listOf( + Asterisk(2, '*'), + Text("a"), + Asterisk(2, '*') + ), actual + ) + } + + @Test + fun アンダーバー() { + val lexer = Lexer() + + val actual = lexer.lex("__a__") + + println(actual) + + assertContentEquals( + listOf( + Asterisk(2, '_'), + Text("a"), + Asterisk(2, '_') + ), actual + ) + } + + @Test + fun urlかと思ったらアスタリスク() { + val lexer = Lexer() + + val actual = lexer.lex("h*a*") + + println(actual) + + assertContentEquals( + listOf( ), actual ) }