diff --git a/genesis-core/src/main/kotlin/com/github/kennarddh/mindustry/genesis/core/commons/StringParser.kt b/genesis-core/src/main/kotlin/com/github/kennarddh/mindustry/genesis/core/commons/StringParser.kt index 4bb676d..436a5d0 100644 --- a/genesis-core/src/main/kotlin/com/github/kennarddh/mindustry/genesis/core/commons/StringParser.kt +++ b/genesis-core/src/main/kotlin/com/github/kennarddh/mindustry/genesis/core/commons/StringParser.kt @@ -9,73 +9,71 @@ open class StringParserToken data object SkipToken : StringParserToken() data class StringToken(val value: String) : StringParserToken() -class StringParser { - companion object { - private val escapedCharactersMap = mapOf('n' to "\n", '\"' to "\"", '\\' to "\\", '*' to "*") - - fun parse(input: String): Iterator = - iterator { - var isEscaping = false - var isInQuote = false - - val output = buildString { - for (char in input) { - if (isEscaping) { - if (escapedCharactersMap.contains(char)) { - append(escapedCharactersMap[char]) - - isEscaping = false - - continue - } else { - throw InvalidEscapedCharacterException("$char is not a valid character to be escaped") - } - } +object StringParser { + private val escapedCharactersMap = mapOf('n' to "\n", '\"' to "\"", '\\' to "\\", '*' to "*") + + fun parse(input: String): Iterator = + iterator { + var isEscaping = false + var isInQuote = false + + val output = buildString { + for (char in input) { + if (isEscaping) { + if (escapedCharactersMap.contains(char)) { + append(escapedCharactersMap[char]) + + isEscaping = false - when (char) { - '\\' -> isEscaping = true - '"' -> isInQuote = !isInQuote - '*' -> { - if (!isInQuote && isEmpty()) - yield(SkipToken) - else - append(char) - } - - ' ' -> { - if (!isInQuote) { - if (isNotEmpty()) { - yield(StringToken(toString())) - clear() - } - } else - append(char) - } - - else -> append(char) + continue + } else { + throw InvalidEscapedCharacterException("$char is not a valid character to be escaped") } } - } - if (isEscaping) { - throw InvalidEscapedCharacterException("No character provided after escape character") - } else if (isInQuote) { - throw UnterminatedStringException("Double quoted string $output is not terminated") - } else if (output.isNotBlank()) { - yield(StringToken(output.toString())) + when (char) { + '\\' -> isEscaping = true + '"' -> isInQuote = !isInQuote + '*' -> { + if (!isInQuote && isEmpty()) + yield(SkipToken) + else + append(char) + } + + ' ' -> { + if (!isInQuote) { + if (isNotEmpty()) { + yield(StringToken(toString())) + clear() + } + } else + append(char) + } + + else -> append(char) + } } } - fun parseToArray(input: String): Array { - val parsed = parse(input) + if (isEscaping) { + throw InvalidEscapedCharacterException("No character provided after escape character") + } else if (isInQuote) { + throw UnterminatedStringException("Double quoted string $output is not terminated") + } else if (output.isNotBlank()) { + yield(StringToken(output.toString())) + } + } - val output: MutableList = mutableListOf() + fun parseToArray(input: String): Array { + val parsed = parse(input) - parsed.forEach { - output.add(it) - } + val output: MutableList = mutableListOf() - return output.toTypedArray() + parsed.forEach { + output.add(it) } + + return output.toTypedArray() } } \ No newline at end of file