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:
2026-05-01 18:57:54 +00:00
parent a40a970080
commit d8d5588e42
6 changed files with 496 additions and 1 deletions

View File

@@ -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";

View File

@@ -2224,6 +2224,127 @@ let () =
String flags
| _ -> raise (Eval_error "regex-flags: (regex)"));
(* make-regexp / regexp? / regexp-match / regexp-match-all / regexp-replace / regexp-replace-all / regexp-split *)
let parse_re_flags flags =
let opts = ref [] in
String.iter (function
| 'i' -> opts := `CASELESS :: !opts
| 'm' -> opts := `MULTILINE :: !opts
| 's' -> opts := `DOTALL :: !opts
| _ -> ()) flags;
!opts
in
let make_regexp_value source flags =
let opts = parse_re_flags flags in
try
let compiled = Re.compile (Re.Pcre.re ~flags:opts source) in
SxRegexp (source, flags, compiled)
with _ -> raise (Eval_error ("make-regexp: invalid pattern: " ^ source))
in
let match_dict g input =
let d = Hashtbl.create 4 in
Hashtbl.replace d "match" (String (Re.Group.get g 0));
Hashtbl.replace d "start" (Integer (Re.Group.start g 0));
Hashtbl.replace d "end" (Integer (Re.Group.stop g 0));
Hashtbl.replace d "input" (String input);
let count = Re.Group.nb_groups g in
let groups = ref [] in
for i = count - 1 downto 1 do
let s = try Re.Group.get g i with Not_found -> "" in
groups := String s :: !groups
done;
Hashtbl.replace d "groups" (List !groups);
Dict d
in
register "make-regexp" (fun args ->
match args with
| [String src] -> make_regexp_value src ""
| [String src; String flags] -> make_regexp_value src flags
| _ -> raise (Eval_error "make-regexp: (pattern [flags])"));
register "regexp?" (fun args ->
match args with
| [SxRegexp _] -> Bool true
| [_] -> Bool false
| _ -> raise (Eval_error "regexp?: 1 arg"));
register "regexp-source" (fun args ->
match args with
| [SxRegexp (src, _, _)] -> String src
| _ -> raise (Eval_error "regexp-source: expected regexp"));
register "regexp-flags" (fun args ->
match args with
| [SxRegexp (_, flags, _)] -> String flags
| _ -> raise (Eval_error "regexp-flags: expected regexp"));
register "regexp-match" (fun args ->
match args with
| [SxRegexp (_, _, re); String s] ->
(match Re.exec_opt re s with
| None -> Nil
| Some g -> match_dict g s)
| _ -> raise (Eval_error "regexp-match: (regexp string)"));
register "regexp-match-all" (fun args ->
match args with
| [SxRegexp (_, _, re); String s] ->
List (List.map (fun g -> match_dict g s) (Re.all re s))
| _ -> raise (Eval_error "regexp-match-all: (regexp string)"));
register "regexp-replace" (fun args ->
match args with
| [SxRegexp (_, _, re); String s; String replacement] ->
(match Re.exec_opt re s with
| None -> String s
| Some g ->
let buf = Buffer.create (String.length s) in
let i = ref 0 in
let n = String.length replacement in
let expand () =
while !i < n do
let c = replacement.[!i] in
if c = '$' && !i + 1 < n then
(match replacement.[!i + 1] with
| '&' -> Buffer.add_string buf (Re.Group.get g 0); i := !i + 2
| '$' -> Buffer.add_char buf '$'; i := !i + 2
| c when c >= '0' && c <= '9' ->
let idx = Char.code c - Char.code '0' in
(try Buffer.add_string buf (Re.Group.get g idx) with Not_found -> ());
i := !i + 2
| _ -> Buffer.add_char buf c; incr i)
else (Buffer.add_char buf c; incr i)
done
in
Buffer.add_string buf (String.sub s 0 (Re.Group.start g 0));
expand ();
Buffer.add_string buf (String.sub s (Re.Group.stop g 0)
(String.length s - Re.Group.stop g 0));
String (Buffer.contents buf))
| _ -> raise (Eval_error "regexp-replace: (regexp string replacement)"));
register "regexp-replace-all" (fun args ->
match args with
| [SxRegexp (_, _, re); String s; String replacement] ->
let expand g =
let buf = Buffer.create (String.length replacement) in
let i = ref 0 in
let n = String.length replacement in
while !i < n do
let c = replacement.[!i] in
if c = '$' && !i + 1 < n then
(match replacement.[!i + 1] with
| '&' -> Buffer.add_string buf (Re.Group.get g 0); i := !i + 2
| '$' -> Buffer.add_char buf '$'; i := !i + 2
| c when c >= '0' && c <= '9' ->
let idx = Char.code c - Char.code '0' in
(try Buffer.add_string buf (Re.Group.get g idx) with Not_found -> ());
i := !i + 2
| _ -> Buffer.add_char buf c; incr i)
else (Buffer.add_char buf c; incr i)
done;
Buffer.contents buf
in
String (Re.replace re ~f:expand s)
| _ -> raise (Eval_error "regexp-replace-all: (regexp string replacement)"));
register "regexp-split" (fun args ->
match args with
| [SxRegexp (_, _, re); String s] ->
List (List.map (fun x -> String x) (Re.split re s))
| _ -> raise (Eval_error "regexp-split: (regexp string)"));
(* Bitwise operations *)
register "bitwise-and" (fun args ->
match args with

View File

@@ -80,6 +80,7 @@ and value =
| Port of sx_port (** String port — input (string cursor) or output (buffer). *)
| Rational of int * int (** Exact rational: numerator, denominator (reduced, denom>0). *)
| SxSet of (string, value) Hashtbl.t (** Mutable set keyed by inspect(value). *)
| SxRegexp of string * string * Re.re (** Regexp: source, flags, compiled. *)
(** String input port: source string + mutable cursor position. *)
and sx_port_kind =
@@ -516,6 +517,7 @@ let type_of = function
| Port { sp_kind = PortOutput _; _ } -> "output-port"
| Rational _ -> "rational"
| SxSet _ -> "set"
| SxRegexp _ -> "regexp"
let is_nil = function Nil -> true | _ -> false
let is_lambda = function Lambda _ -> true | _ -> false
@@ -879,3 +881,4 @@ let rec inspect = function
Printf.sprintf "<output-port:len=%d%s>" (Buffer.length buf) (if sp_closed then ":closed" else "")
| Rational (n, d) -> Printf.sprintf "%d/%d" n d
| SxSet ht -> Printf.sprintf "<set:%d>" (Hashtbl.length ht)
| SxRegexp (src, flags, _) -> Printf.sprintf "#/%s/%s" src flags