Generalize pattern that matches JS module declaration to accept unicode characters
(cherry picked from commit eb44819)
This commit is contained in:
@@ -42,7 +42,10 @@ import java.io.File
|
|||||||
* Matches string like Kotlin.defineModule("stdlib", _)
|
* Matches string like Kotlin.defineModule("stdlib", _)
|
||||||
* Kotlin, _ can be renamed by minifier, quotes type can be changed too (" to ')
|
* Kotlin, _ can be renamed by minifier, quotes type can be changed too (" to ')
|
||||||
*/
|
*/
|
||||||
private val DEFINE_MODULE_PATTERN = "(\\w+)\\.defineModule\\(\\s*(['\"])(\\w+)\\2\\s*,\\s*(\\w+)\\s*\\)".toRegex()
|
private val JS_IDENTIFIER_START = "\\p{Lu}\\p{Ll}\\p{Lt}\\p{Lm}\\p{Lo}\\p{Nl}\\\$_"
|
||||||
|
private val JS_IDENTIFIER_PART = "$JS_IDENTIFIER_START\\p{Pc}\\p{Mc}\\p{Mn}\\d"
|
||||||
|
private val JS_IDENTIFIER="[$JS_IDENTIFIER_START][$JS_IDENTIFIER_PART]*"
|
||||||
|
private val DEFINE_MODULE_PATTERN = ("($JS_IDENTIFIER)\\.defineModule\\(\\s*(['\"])(\\w+)\\2\\s*,\\s*(\\w+)\\s*\\)").toRegex().toPattern()
|
||||||
private val DEFINE_MODULE_FIND_PATTERN = ".defineModule("
|
private val DEFINE_MODULE_FIND_PATTERN = ".defineModule("
|
||||||
|
|
||||||
class FunctionReader(private val context: TranslationContext) {
|
class FunctionReader(private val context: TranslationContext) {
|
||||||
@@ -76,8 +79,8 @@ class FunctionReader(private val context: TranslationContext) {
|
|||||||
if (index < 0) break
|
if (index < 0) break
|
||||||
|
|
||||||
current = index + 1
|
current = index + 1
|
||||||
index = rewindToFirstAlpha(fileContent, index)
|
index = rewindToIdentifierStart(fileContent, index)
|
||||||
val preciseMatcher = DEFINE_MODULE_PATTERN.toPattern().matcher(offset(fileContent, index))
|
val preciseMatcher = DEFINE_MODULE_PATTERN.matcher(offset(fileContent, index))
|
||||||
if (!preciseMatcher.lookingAt()) continue
|
if (!preciseMatcher.lookingAt()) continue
|
||||||
|
|
||||||
val moduleName = preciseMatcher.group(3)
|
val moduleName = preciseMatcher.group(3)
|
||||||
@@ -91,9 +94,9 @@ class FunctionReader(private val context: TranslationContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun rewindToFirstAlpha(text: String, index: Int): Int {
|
private fun rewindToIdentifierStart(text: String, index: Int): Int {
|
||||||
var result = index
|
var result = index
|
||||||
while (result > 0 && isAlphaNum(text[result - 1])) {
|
while (result > 0 && Character.isJavaIdentifierPart(text[result - 1])) {
|
||||||
--result
|
--result
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -110,13 +113,6 @@ class FunctionReader(private val context: TranslationContext) {
|
|||||||
override fun toString() = text.substring(offset)
|
override fun toString() = text.substring(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isAlphaNum(c: Char) = when (c) {
|
|
||||||
in 'A'..'Z',
|
|
||||||
in 'a'..'z',
|
|
||||||
in '0'..'9' -> true
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
|
|
||||||
private val functionCache = object : SLRUCache<CallableDescriptor, JsFunction>(50, 50) {
|
private val functionCache = object : SLRUCache<CallableDescriptor, JsFunction>(50, 50) {
|
||||||
override fun createValue(descriptor: CallableDescriptor): JsFunction =
|
override fun createValue(descriptor: CallableDescriptor): JsFunction =
|
||||||
readFunction(descriptor).sure { "Could not read function: $descriptor" }
|
readFunction(descriptor).sure { "Could not read function: $descriptor" }
|
||||||
|
|||||||
Reference in New Issue
Block a user