|
1 | 1 | package com.github.h0tk3y.betterParse.lexer
|
2 | 2 |
|
| 3 | +import kotlin.js.RegExp |
| 4 | + |
3 | 5 | public actual class RegexToken : Token {
|
4 | 6 | private val pattern: String
|
5 | 7 | private val regex: Regex
|
6 | 8 |
|
7 | 9 | /** To ensure that the [regex] will only match its pattern from the index where it is called on with
|
8 | 10 | * Regex.find(input, startIndex), set the JS RegExp flag 'y', which makes the RegExp 'sticky'.
|
9 | 11 | * 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 |
20 | 21 | }
|
21 |
| - """ |
22 |
| - ) |
| 22 | + } |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | public actual constructor(name: String?, patternString: String, ignored: Boolean)
|
|
0 commit comments