Skip to content

Commit

Permalink
fix preprocessRegex function (fixes #51)
Browse files Browse the repository at this point in the history
  • Loading branch information
InfectedBytes authored and h0tk3y committed Apr 18, 2022
1 parent 3927674 commit 1204f8a
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/jsMain/kotlin/com/github/h0tk3y/betterParse/lexer/RegexToken.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.github.h0tk3y.betterParse.lexer

import kotlin.js.RegExp

public actual class RegexToken : Token {
private val pattern: String
private val regex: Regex

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

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

0 comments on commit 1204f8a

Please sign in to comment.