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) decimalList(iterator, tokens, next)
next == '[' || next == '「' -> tokens.add(SquareBracketStart) next == '[' || next == '「' -> tokens.add(SquareBracketStart)
next == ']' || next == '」' -> tokens.add(SquareBracketEnd) next == ']' || next == '」' -> tokens.add(SquareBracketEnd)
next == '' || next == '(' -> tokens.add(ParenthesesStart) next == '' || next == '(' -> tokens.add(ParenthesesStart)
next == ')' || next == '' -> tokens.add(ParenthesesEnd) next == ')' || next == '' -> tokens.add(ParenthesesEnd)
next.isWhitespace() -> tokens.add( next.isWhitespace() -> tokens.add(
Whitespace( Whitespace(
skipWhitespace(iterator) + 1, skipWhitespace(iterator) + 1,
next next
) )
) //nextの分1足す ) //nextの分1足す
next == 'h' -> url(next, iterator, tokens) next == 'h' -> url(next, iterator, tokens)
next == '*' || next == '_' -> asterisk(iterator, next, tokens)
next == '*' -> {
var count = 1
while (iterator.peekOrNull() == '*') {
count++
iterator.next()
}
tokens.add(Asterisk(count))
}
else -> { else -> {
val lastToken = tokens.lastOrNull() val lastToken = tokens.lastOrNull()
@ -91,16 +78,31 @@ class Lexer {
return tokens 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( private fun url(
next: Char, next: Char,
iterator: PeekableCharIterator, iterator: PeekableCharIterator,
tokens: MutableList<Token>, tokens: MutableList<Token>,
) { ) {
//todo httpにも対応 //todo httpにも対応
//todo nextでみずにpeekでみて確認してからnextする hのあとにアスタリスク等が来たときに対応できない
val charIterator = "ttps://".iterator() val charIterator = "ttps://".iterator()
val urlBuilder = StringBuilder() val urlBuilder = StringBuilder()
urlBuilder.append(next) urlBuilder.append(next)
while (charIterator.hasNext() && iterator.hasNext()) { while (charIterator.hasNext() && iterator.hasNext()) {
// charIterator
val nextC = charIterator.next() val nextC = charIterator.next()
val nextC2 = iterator.next() val nextC2 = iterator.next()
urlBuilder.append(nextC2) urlBuilder.append(nextC2)

View File

@ -23,4 +23,4 @@ data object SquareBracketEnd : Token()
data object ParenthesesStart : Token() data object ParenthesesStart : Token()
data object ParenthesesEnd : Token() data object ParenthesesEnd : Token()
data class Url(var url: String) : 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( assertContentEquals(
listOf( listOf(
Asterisk(1), Asterisk(1, '*'),
Text("a"), Text("a"),
Asterisk(1) Asterisk(1, '*')
), actual ), actual
) )
} }
@ -512,9 +512,57 @@ class LexerTest {
assertContentEquals( assertContentEquals(
listOf( listOf(
Quote(1), Quote(1),
Asterisk(1), Asterisk(1, '*'),
Text("a"), 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 ), actual
) )
} }