feat: whenの書き方を変更しアスタリスクに対応

This commit is contained in:
usbharu 2024-11-13 12:02:21 +09:00
parent 21087e0244
commit 0a972cb8eb
Signed by: usbharu
GPG Key ID: 8CB1087135660B8D
3 changed files with 103 additions and 53 deletions

View File

@ -6,6 +6,9 @@ class Lexer {
fun lex(input: String): List<Token> {
val tokens = mutableListOf<Token>()
val lines = PeekableStringIterator(input.lines())
var inQuote = false
line@ while (lines.hasNext()) {
if (lines.peekOrNull() == "") {
@ -15,10 +18,15 @@ class Lexer {
val iterator = PeekableCharIterator(line.toCharArray())
char@ while (iterator.hasNext()) {
when (val next = iterator.next()) {
'#', '' -> header(iterator, tokens)
'>', '' -> quote(iterator, tokens)
'-', '=', 'ー', '' -> {
val next = iterator.next()
when {
next == '#' || next == '' -> header(iterator, tokens)
(next == '>' || next == '') && !inQuote -> {
inQuote = true
quote(iterator, tokens)
}
next == '-' || next == '=' || next == 'ー' || next == '' -> {
if (iterator.peekOrNull()?.isWhitespace() == true) { //-の直後がスペースならリストの可能性
list(iterator, tokens)
} else {//それ以外ならセパレーターの可能性
@ -26,56 +34,35 @@ class Lexer {
}
}
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '', '', '', '', '', '', '', '', '', '' ->
next in '0'..'9' || next in ''..'' ->
decimalList(iterator, tokens, next)
'[', '「' -> {
tokens.add(SquareBracketStart)
}
next == '[' || next == '「' -> tokens.add(SquareBracketStart)
']', '」' -> {
tokens.add(SquareBracketEnd)
}
next == ']' || next == '」' -> tokens.add(SquareBracketEnd)
'', '(' -> {
tokens.add(ParenthesesStart)
}
next == '' || next == '(' -> tokens.add(ParenthesesStart)
')', '' -> {
tokens.add(ParenthesesEnd)
}
next == ')' || next == '' -> tokens.add(ParenthesesEnd)
' ', ' ' -> {
tokens.add(Whitespace(skipWhitespace(iterator) + 1, next)) //nextの分1足す
}
next.isWhitespace() -> tokens.add(
Whitespace(
skipWhitespace(iterator) + 1,
next
)
) //nextの分1足す
'h' -> {
//todo httpにも対応
val charIterator = "ttps://".iterator()
val urlBuilder = StringBuilder()
urlBuilder.append(next)
while (charIterator.hasNext() && iterator.hasNext()) {
val nextC = charIterator.next()
val nextC2 = iterator.next()
urlBuilder.append(nextC2)
if (nextC != nextC2) {
tokens.add(Text(urlBuilder.toString()))
continue@char
}
next == 'h' -> url(next, iterator, tokens)
next == '*' -> {
var count = 1
while (iterator.peekOrNull() == '*') {
count++
iterator.next()
}
if (urlBuilder.length == 1) {
tokens.add(Text(urlBuilder.toString())) //hだけのときはURLじゃないのでテキストとして追加
} else {
while (iterator.hasNext() && iterator.peekOrNull()?.isWhitespace() != true) {
urlBuilder.append(iterator.next())
}
tokens.add(Url(urlBuilder.toString()))
}
tokens.add(Asterisk(count))
}
else -> {
val lastToken = tokens.lastOrNull()
if (lastToken is Text) {
@ -89,7 +76,7 @@ class Lexer {
tokens.add(Break(1))
}
inQuote = false
}
println(tokens)
@ -104,10 +91,38 @@ class Lexer {
return tokens
}
private fun url(
next: Char,
iterator: PeekableCharIterator,
tokens: MutableList<Token>,
) {
//todo httpにも対応
val charIterator = "ttps://".iterator()
val urlBuilder = StringBuilder()
urlBuilder.append(next)
while (charIterator.hasNext() && iterator.hasNext()) {
val nextC = charIterator.next()
val nextC2 = iterator.next()
urlBuilder.append(nextC2)
if (nextC != nextC2) {
tokens.add(Text(urlBuilder.toString()))
return
}
}
if (urlBuilder.length == 1) {
tokens.add(Text(urlBuilder.toString())) //hだけのときはURLじゃないのでテキストとして追加
} else {
while (iterator.hasNext() && iterator.peekOrNull()?.isWhitespace() != true) {
urlBuilder.append(iterator.next())
}
tokens.add(Url(urlBuilder.toString()))
}
}
private fun decimalList(
iterator: PeekableCharIterator,
tokens: MutableList<Token>,
next: Char
next: Char,
) {
val comma = iterator.peekOrNull()
if (comma == null) {
@ -127,7 +142,7 @@ class Lexer {
private fun list(
iterator: PeekableCharIterator,
tokens: MutableList<Token>
tokens: MutableList<Token>,
) {
if (iterator.peekOrNull()?.isWhitespace() == true) {
@ -159,7 +174,7 @@ class Lexer {
private fun separator(
next: Char,
iterator: PeekableCharIterator,
tokens: MutableList<Token>
tokens: MutableList<Token>,
) {
val builder = StringBuilder()
builder.append(next)
@ -181,7 +196,7 @@ class Lexer {
private fun quote(
iterator: PeekableCharIterator,
tokens: MutableList<Token>
tokens: MutableList<Token>,
) {
var count = 1
while (iterator.peekOrNull()?.isWhitespace() == false) {
@ -190,12 +205,11 @@ class Lexer {
}
tokens.add(Quote(count))
skipWhitespace(iterator)
tokens.add(Text(collect(iterator)))
}
private fun header(
iterator: PeekableCharIterator,
tokens: MutableList<Token>
tokens: MutableList<Token>,
) {
var count = 1
while (iterator.peekOrNull()?.isWhitespace() == false) {
@ -229,7 +243,7 @@ class Lexer {
*/
private fun blankLine(
lines: PeekableStringIterator,
tokens: MutableList<Token>
tokens: MutableList<Token>,
) {
var count = 0
while (lines.peekOrNull() == "") {

View File

@ -23,3 +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()

View File

@ -483,4 +483,39 @@ class LexerTest {
), actual
)
}
@Test
fun アスタリスク() {
val lexer = Lexer()
val actual = lexer.lex("*a*")
println(actual)
assertContentEquals(
listOf(
Asterisk(1),
Text("a"),
Asterisk(1)
), actual
)
}
@Test
fun アスタリスク2() {
val lexer = Lexer()
val actual = lexer.lex("> *a*")
println(actual)
assertContentEquals(
listOf(
Quote(1),
Asterisk(1),
Text("a"),
Asterisk(1)
), actual
)
}
}