[scripting] Fix script resolver options parsing
#KT-49400 Fixed
This commit is contained in:
committed by
TeamCityServer
parent
8a1362de03
commit
a22c8c8f2f
+17
-3
@@ -8,9 +8,23 @@ package kotlin.script.experimental.dependencies.impl
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.dependencies.ExternalDependenciesResolver
|
||||
|
||||
private val nameRegex = Regex("^[^\\S\\r\\n]*([a-zA-Z][a-zA-Z0-9-_]*)\\b")
|
||||
private val valueRegex = Regex("^[^\\S\\r\\n]*([a-zA-Z0-9-_,]+)\\b")
|
||||
private val nameRegex = Regex("^[^\\S\\r\\n]*([a-zA-Z][a-zA-Z0-9-_]*)[^\\S\\r\\n]?")
|
||||
private val valueRegex = Regex("^[^\\S\\r\\n]*((([a-zA-Z0-9-_,/$.:])|(\\\\[\\\\ nt]))+)[^\\S\\r\\n]?")
|
||||
private val equalsRegex = Regex("^[^\\S\\r\\n]*=")
|
||||
private val escapeRegex = Regex("\\\\(.)")
|
||||
|
||||
private fun String.unescape(): String {
|
||||
return replace(escapeRegex) { match ->
|
||||
when (val c = match.groups[1]!!.value.single()) {
|
||||
'\\' -> "\\"
|
||||
' ' -> " "
|
||||
'n' -> "\n"
|
||||
't' -> "\t"
|
||||
// Impossible situation: all possible values are mentioned in the regex
|
||||
else -> error("Unknown escaped symbol: $c")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple lightweight options parser for external dependency resolvers.
|
||||
@@ -42,7 +56,7 @@ object SimpleExternalDependenciesResolverOptionsParser {
|
||||
}
|
||||
|
||||
fun takeName(): Token.Name? = take(nameRegex)?.let { Token.Name(it.groups[1]!!.value) }
|
||||
fun takeValue(): Token.Value? = take(valueRegex)?.let { Token.Value(it.groups[1]!!.value) }
|
||||
fun takeValue(): Token.Value? = take(valueRegex)?.let { Token.Value(it.groups[1]!!.value.unescape()) }
|
||||
fun takeEquals(): Token.Equals? = take(equalsRegex)?.let { Token.Equals }
|
||||
|
||||
fun hasFinished(): Boolean = remaining.isBlank()
|
||||
|
||||
+16
@@ -65,6 +65,22 @@ class ResolverOptionsTest : TestCase() {
|
||||
assertEquals(options.flag("option3"), true)
|
||||
}
|
||||
|
||||
fun testParserAcceptsSpecialSymbols() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1=/User/path/file.kt option2=C:\\\\User\\\\file.pem option3=\$MY_ENV").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "/User/path/file.kt")
|
||||
assertEquals(options.value("option2"), "C:\\User\\file.pem")
|
||||
assertEquals(options.value("option3"), "\$MY_ENV")
|
||||
}
|
||||
|
||||
fun testParserAcceptsValuesWithSpaces() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1= spaced\\ \\ value\\ option2=\\ x option3=line1\\nline2").valueOrThrow()
|
||||
assertEquals(options.value("option1"), "spaced value ")
|
||||
assertEquals(options.value("option2"), " x")
|
||||
assertEquals(options.value("option3"), "line1\nline2")
|
||||
}
|
||||
|
||||
fun testParserReturnsMixOfValuesAndFlags() {
|
||||
val parser = SimpleExternalDependenciesResolverOptionsParser
|
||||
val options = parser("option1 = hello option2 option3=world option4 option5 = false").valueOrThrow()
|
||||
|
||||
Reference in New Issue
Block a user