diff --git a/libraries/common/regex.effekt b/libraries/common/regex.effekt index 077f9409d..cafa1f49f 100644 --- a/libraries/common/regex.effekt +++ b/libraries/common/regex.effekt @@ -14,47 +14,51 @@ extern pure def regex(str: String): Regex = vm "regex::regex(String)" def exec(reg: Regex, str: String): Option[Match] = { - val v = reg.unsafeExec(str) + val v = internal::exec(reg, str) if (v.isUndefined) None() else - Some(Match(v.matched, v.index)) + Some(Match(internal::matched(v), internal::index(v))) } -extern type RegexMatch - // js: { matched: String, index: Int } | undefined - // vm: scala.util.matching.Regex.Match | null - -extern pure def matched(r: RegexMatch): String = - js "${r}.matched" - chez "(vector-ref ${r} 0)" - vm "regex::matched(RegexMatch)" - -extern pure def index(r: RegexMatch): Int = - js "${r}.index" - chez "(vector-ref ${r} 1)" - vm "regex::index(RegexMatch)" - -extern js """ - function regex$exec(reg, str) { - var res = reg.exec(str); - if (res === null) { return undefined } - else { return { matched: res[0], index: res.index } } - } -""" - -extern chez """ -(define regex-exec - (lambda (regex str) - (let* ([positions (pregexp-match-positions regex str)] - [match (pregexp-match regex str)]) - (if (and positions match) - (vector (car match) (caar positions)) - #f)))) -""" - -// internals -extern io def unsafeExec(reg: Regex, str: String): RegexMatch = - js "regex$exec(${reg}, ${str})" - chez "(regex-exec ${reg} ${str})" - vm "regex::exec(Regex, String)" +namespace internal { + + /// The type RegexMatch is used internally to represent platform + /// dependent results of matching a regex. + extern type RegexMatch + // js: { matched: String, index: Int } | undefined + // vm: scala.util.matching.Regex.Match | null + + extern pure def matched(r: RegexMatch): String = + js "${r}.matched" + chez "(vector-ref ${r} 0)" + vm "regex::matched(RegexMatch)" + + extern pure def index(r: RegexMatch): Int = + js "${r}.index" + chez "(vector-ref ${r} 1)" + vm "regex::index(RegexMatch)" + + extern js """ + function regex$exec(reg, str) { + var res = reg.exec(str); + if (res === null) { return undefined } + else { return { matched: res[0], index: res.index } } + } + """ + + extern chez """ + (define regex-exec + (lambda (regex str) + (let* ([positions (pregexp-match-positions regex str)] + [match (pregexp-match regex str)]) + (if (and positions match) + (vector (car match) (caar positions)) + #f)))) + """ + + extern io def exec(reg: Regex, str: String): RegexMatch = + js "regex$exec(${reg}, ${str})" + chez "(regex-exec ${reg} ${str})" + vm "regex::exec(Regex, String)" +} \ No newline at end of file