This commit is contained in:
usbharu 2024-11-13 12:22:07 +09:00
parent 0a972cb8eb
commit 6d49a4ad5e
Signed by: usbharu
GPG Key ID: 8CB1087135660B8D
3 changed files with 69 additions and 19 deletions

View File

@ -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<Token>,
) {
var count = 1
while (iterator.peekOrNull() == next) {
count++
iterator.next()
}
tokens.add(Asterisk(count, next))
}
private fun url(
next: Char,
iterator: PeekableCharIterator,
tokens: MutableList<Token>,
) {
//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)

View File

@ -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()
data class Asterisk(var count: Int, var char: Char) : Token()

View File

@ -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
)
}