Skip to content

Commit 1204f8a

Browse files
InfectedBytesh0tk3y
authored andcommitted
fix preprocessRegex function (fixes #51)
1 parent 3927674 commit 1204f8a

File tree

1 file changed

+12
-12
lines changed
  • src/jsMain/kotlin/com/github/h0tk3y/betterParse/lexer

1 file changed

+12
-12
lines changed

src/jsMain/kotlin/com/github/h0tk3y/betterParse/lexer/RegexToken.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package com.github.h0tk3y.betterParse.lexer
22

3+
import kotlin.js.RegExp
4+
35
public actual class RegexToken : Token {
46
private val pattern: String
57
private val regex: Regex
68

79
/** To ensure that the [regex] will only match its pattern from the index where it is called on with
810
* Regex.find(input, startIndex), set the JS RegExp flag 'y', which makes the RegExp 'sticky'.
911
* See: https://javascript.info/regexp-sticky */
10-
private fun preprocessRegex(@Suppress("UNUSED_PARAMETER") regex: Regex) {
11-
js(
12-
"""
13-
var r = regex.nativePattern_0;
14-
15-
if (typeof r === 'undefined' || r === null) {
16-
r = regex._nativePattern;
17-
regex._nativePattern = new RegExp(r.source, r.flags + (r.sticky ? "" : "y"));
18-
} else {
19-
regex.nativePattern_0 = new RegExp(r.source, r.flags + (r.sticky ? "" : "y"));
12+
private fun preprocessRegex(regex: Regex) {
13+
val possibleNames = listOf("nativePattern_1", "nativePattern_0", "_nativePattern")
14+
for(name in possibleNames) {
15+
val r = regex.asDynamic()[name]
16+
if(jsTypeOf(r) !== "undefined" && r !== null) {
17+
val src = r.source as String
18+
val flags = r.flags as String + if(r.sticky as Boolean) "" else "y"
19+
regex.asDynamic()[name] = RegExp(src, flags)
20+
break
2021
}
21-
"""
22-
)
22+
}
2323
}
2424

2525
public actual constructor(name: String?, patternString: String, ignored: Boolean)

0 commit comments

Comments
 (0)