spec: regular expressions (make-regexp/regexp-match/regexp-replace + split)
Adds 9 regexp primitives to stdlib.regexp. OCaml: SxRegexp(src,flags,Re.re)
using Re.Pcre; $&/$1 capture expansion in replace. JS: native RegExp
with SxRegexp wrapper; regexp-match returns {:match :start :end :groups}.
32 tests in test-regexp.sx, all pass on both hosts.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1702,6 +1702,67 @@ PRIMITIVES_JS_MODULES: dict[str, str] = {
|
||||
src.data.forEach(function(v, k) { dst.data.set(k, v); });
|
||||
return null;
|
||||
};
|
||||
''',
|
||||
"stdlib.regexp": '''
|
||||
// stdlib.regexp — native JS RegExp wrappers
|
||||
function SxRegexp(source, flags) {
|
||||
this._regexp = true;
|
||||
this.source = source;
|
||||
this.flags = flags || "";
|
||||
}
|
||||
function sxRxCompile(rx) {
|
||||
if (!rx._compiled) {
|
||||
var jsFlags = "";
|
||||
if (rx.flags.indexOf("i") >= 0) jsFlags += "i";
|
||||
if (rx.flags.indexOf("m") >= 0) jsFlags += "m";
|
||||
if (rx.flags.indexOf("s") >= 0) jsFlags += "s";
|
||||
rx._compiled = new RegExp(rx.source, jsFlags);
|
||||
}
|
||||
return rx._compiled;
|
||||
}
|
||||
function sxRxMatchDict(m, input) {
|
||||
if (!m) return NIL;
|
||||
var groups = [];
|
||||
for (var i = 1; i < m.length; i++) groups.push(m[i] !== undefined ? m[i] : "");
|
||||
return {"match": m[0], "start": m.index, "end": m.index + m[0].length,
|
||||
"input": input, "groups": groups};
|
||||
}
|
||||
PRIMITIVES["make-regexp"] = function(src, flags) {
|
||||
return new SxRegexp(src, flags || "");
|
||||
};
|
||||
PRIMITIVES["regexp?"] = function(v) { return v instanceof SxRegexp; };
|
||||
PRIMITIVES["regexp-source"] = function(rx) { return rx.source; };
|
||||
PRIMITIVES["regexp-flags"] = function(rx) { return rx.flags; };
|
||||
PRIMITIVES["regexp-match"] = function(rx, s) {
|
||||
var re = new RegExp(sxRxCompile(rx).source,
|
||||
sxRxCompile(rx).flags.replace("g",""));
|
||||
var m = s.match(re);
|
||||
return sxRxMatchDict(m, s);
|
||||
};
|
||||
PRIMITIVES["regexp-match-all"] = function(rx, s) {
|
||||
var compiled = sxRxCompile(rx);
|
||||
var re = new RegExp(compiled.source, "g" + compiled.flags.replace("g",""));
|
||||
var results = [], m;
|
||||
while ((m = re.exec(s)) !== null) {
|
||||
results.push(sxRxMatchDict(m, s));
|
||||
if (m[0].length === 0) re.lastIndex++;
|
||||
}
|
||||
return results;
|
||||
};
|
||||
PRIMITIVES["regexp-replace"] = function(rx, s, replacement) {
|
||||
var compiled = sxRxCompile(rx);
|
||||
var re = new RegExp(compiled.source, compiled.flags.replace("g",""));
|
||||
return s.replace(re, replacement);
|
||||
};
|
||||
PRIMITIVES["regexp-replace-all"] = function(rx, s, replacement) {
|
||||
var compiled = sxRxCompile(rx);
|
||||
var re = new RegExp(compiled.source, "g" + compiled.flags.replace("g",""));
|
||||
return s.replace(re, replacement);
|
||||
};
|
||||
PRIMITIVES["regexp-split"] = function(rx, s) {
|
||||
var re = sxRxCompile(rx);
|
||||
return s.split(re);
|
||||
};
|
||||
''',
|
||||
"stdlib.sets": '''
|
||||
// stdlib.sets — structural sets keyed by write-to-string serialization
|
||||
@@ -1802,6 +1863,7 @@ PLATFORM_JS_PRE = '''
|
||||
if (x._string_buffer) return "string-buffer";
|
||||
if (x._hash_table) return "hash-table";
|
||||
if (x._sxset) return "set";
|
||||
if (x._regexp) return "regexp";
|
||||
if (x._rational) return "rational";
|
||||
if (typeof Node !== "undefined" && x instanceof Node) return "dom-node";
|
||||
if (Array.isArray(x)) return "list";
|
||||
|
||||
Reference in New Issue
Block a user