UNPKG

regexpstructor

Version:

Build regular expressions in a safe and readable way.

2,214 lines (2,203 loc) 49.2 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); }; var __privateGet = (obj, member, getter) => { __accessCheck(obj, member, "read from private field"); return getter ? getter.call(obj) : member.get(obj); }; var __privateAdd = (obj, member, value) => { if (member.has(obj)) throw TypeError("Cannot add the same private member more than once"); member instanceof WeakSet ? member.add(obj) : member.set(obj, value); }; var __privateSet = (obj, member, value, setter) => { __accessCheck(obj, member, "write to private field"); setter ? setter.call(obj, value) : member.set(obj, value); return value; }; // node_modules/@rescript/std/lib/es6/caml_option.js function valFromOption(x) { if (!(x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== void 0)) { return x; } var depth = x.BS_PRIVATE_NESTED_SOME_NONE; if (depth === 0) { return; } else { return { BS_PRIVATE_NESTED_SOME_NONE: depth - 1 | 0 }; } } // node_modules/@rescript/std/lib/es6/belt_List.js function fromArray(a) { var _i = a.length - 1 | 0; var _res = ( /* [] */ 0 ); while (true) { var res = _res; var i = _i; if (i < 0) { return res; } _res = { hd: a[i], tl: res }; _i = i - 1 | 0; continue; } ; } // node_modules/@rescript/std/lib/es6/belt_internalAVLset.js function create(l, v, r) { var hl = l !== void 0 ? l.h : 0; var hr = r !== void 0 ? r.h : 0; return { v, h: (hl >= hr ? hl : hr) + 1 | 0, l, r }; } function singleton(x) { return { v: x, h: 1, l: void 0, r: void 0 }; } function heightGe(l, r) { if (r !== void 0) { if (l !== void 0) { return l.h >= r.h; } else { return false; } } else { return true; } } function bal(l, v, r) { var hl = l !== void 0 ? l.h : 0; var hr = r !== void 0 ? r.h : 0; if (hl > (hr + 2 | 0)) { var ll = l.l; var lr = l.r; if (heightGe(ll, lr)) { return create(ll, l.v, create(lr, v, r)); } else { return create(create(ll, l.v, lr.l), lr.v, create(lr.r, v, r)); } } if (hr <= (hl + 2 | 0)) { return { v, h: (hl >= hr ? hl : hr) + 1 | 0, l, r }; } var rl = r.l; var rr = r.r; if (heightGe(rr, rl)) { return create(create(l, v, rl), r.v, rr); } else { return create(create(l, v, rl.l), rl.v, create(rl.r, r.v, rr)); } } function removeMinAuxWithRef(n, v) { var ln = n.l; if (ln !== void 0) { return bal(removeMinAuxWithRef(ln, v), n.v, n.r); } else { v.contents = n.v; return n.r; } } function addMinElement(n, v) { if (n !== void 0) { return bal(addMinElement(n.l, v), n.v, n.r); } else { return singleton(v); } } function addMaxElement(n, v) { if (n !== void 0) { return bal(n.l, n.v, addMaxElement(n.r, v)); } else { return singleton(v); } } function joinShared(ln, v, rn) { if (ln === void 0) { return addMinElement(rn, v); } if (rn === void 0) { return addMaxElement(ln, v); } var lh = ln.h; var rh = rn.h; if (lh > (rh + 2 | 0)) { return bal(ln.l, ln.v, joinShared(ln.r, v, rn)); } else if (rh > (lh + 2 | 0)) { return bal(joinShared(ln, v, rn.l), rn.v, rn.r); } else { return create(ln, v, rn); } } function concatShared(t1, t2) { if (t1 === void 0) { return t2; } if (t2 === void 0) { return t1; } var v = { contents: t2.v }; var t2r = removeMinAuxWithRef(t2, v); return joinShared(t1, v.contents, t2r); } function lengthNode(n) { var l = n.l; var r = n.r; var sizeL = l !== void 0 ? lengthNode(l) : 0; var sizeR = r !== void 0 ? lengthNode(r) : 0; return (1 + sizeL | 0) + sizeR | 0; } function fillArray(_n, _i, arr) { while (true) { var i = _i; var n = _n; var v = n.v; var l = n.l; var r = n.r; var next = l !== void 0 ? fillArray(l, i, arr) : i; arr[next] = v; var rnext = next + 1 | 0; if (r === void 0) { return rnext; } _i = rnext; _n = r; continue; } ; } function toArray(n) { if (n === void 0) { return []; } var size2 = lengthNode(n); var v = new Array(size2); fillArray(n, 0, v); return v; } function fromSortedArrayRevAux(arr, off, len) { switch (len) { case 0: return; case 1: return singleton(arr[off]); case 2: var x0 = arr[off]; var x1 = arr[off - 1 | 0]; return { v: x1, h: 2, l: singleton(x0), r: void 0 }; case 3: var x0$1 = arr[off]; var x1$1 = arr[off - 1 | 0]; var x2 = arr[off - 2 | 0]; return { v: x1$1, h: 2, l: singleton(x0$1), r: singleton(x2) }; default: var nl = len / 2 | 0; var left = fromSortedArrayRevAux(arr, off, nl); var mid = arr[off - nl | 0]; var right = fromSortedArrayRevAux(arr, (off - nl | 0) - 1 | 0, (len - nl | 0) - 1 | 0); return create(left, mid, right); } } function fromSortedArrayAux(arr, off, len) { switch (len) { case 0: return; case 1: return singleton(arr[off]); case 2: var x0 = arr[off]; var x1 = arr[off + 1 | 0]; return { v: x1, h: 2, l: singleton(x0), r: void 0 }; case 3: var x0$1 = arr[off]; var x1$1 = arr[off + 1 | 0]; var x2 = arr[off + 2 | 0]; return { v: x1$1, h: 2, l: singleton(x0$1), r: singleton(x2) }; default: var nl = len / 2 | 0; var left = fromSortedArrayAux(arr, off, nl); var mid = arr[off + nl | 0]; var right = fromSortedArrayAux(arr, (off + nl | 0) + 1 | 0, (len - nl | 0) - 1 | 0); return create(left, mid, right); } } function rotateWithLeftChild(k2) { var k1 = k2.l; k2.l = k1.r; k1.r = k2; var n = k2.l; var hlk2 = n !== void 0 ? n.h : 0; var n$1 = k2.r; var hrk2 = n$1 !== void 0 ? n$1.h : 0; k2.h = (hlk2 > hrk2 ? hlk2 : hrk2) + 1 | 0; var n$2 = k1.l; var hlk1 = n$2 !== void 0 ? n$2.h : 0; var hk2 = k2.h; k1.h = (hlk1 > hk2 ? hlk1 : hk2) + 1 | 0; return k1; } function rotateWithRightChild(k1) { var k2 = k1.r; k1.r = k2.l; k2.l = k1; var n = k1.l; var hlk1 = n !== void 0 ? n.h : 0; var n$1 = k1.r; var hrk1 = n$1 !== void 0 ? n$1.h : 0; k1.h = (hlk1 > hrk1 ? hlk1 : hrk1) + 1 | 0; var n$2 = k2.r; var hrk2 = n$2 !== void 0 ? n$2.h : 0; var hk1 = k1.h; k2.h = (hrk2 > hk1 ? hrk2 : hk1) + 1 | 0; return k2; } function doubleWithLeftChild(k3) { var k3l = k3.l; var v = rotateWithRightChild(k3l); k3.l = v; return rotateWithLeftChild(k3); } function doubleWithRightChild(k2) { var k2r = k2.r; var v = rotateWithLeftChild(k2r); k2.r = v; return rotateWithRightChild(k2); } function heightUpdateMutate(t) { var n = t.l; var hlt = n !== void 0 ? n.h : 0; var n$1 = t.r; var hrt = n$1 !== void 0 ? n$1.h : 0; t.h = (hlt > hrt ? hlt : hrt) + 1 | 0; return t; } function balMutate(nt) { var l = nt.l; var r = nt.r; var hl = l !== void 0 ? l.h : 0; var hr = r !== void 0 ? r.h : 0; if (hl > (2 + hr | 0)) { var ll = l.l; var lr = l.r; if (heightGe(ll, lr)) { return heightUpdateMutate(rotateWithLeftChild(nt)); } else { return heightUpdateMutate(doubleWithLeftChild(nt)); } } if (hr > (2 + hl | 0)) { var rl = r.l; var rr = r.r; if (heightGe(rr, rl)) { return heightUpdateMutate(rotateWithRightChild(nt)); } else { return heightUpdateMutate(doubleWithRightChild(nt)); } } nt.h = (hl > hr ? hl : hr) + 1 | 0; return nt; } // node_modules/@rescript/std/lib/es6/belt_SortArrayString.js function sortedLengthAuxMore(xs, _prec, _acc, len) { while (true) { var acc = _acc; var prec = _prec; if (acc >= len) { return acc; } var v = xs[acc]; if (prec <= v) { return acc; } _acc = acc + 1 | 0; _prec = v; continue; } ; } function strictlySortedLength(xs) { var len = xs.length; if (len === 0 || len === 1) { return len; } var x0 = xs[0]; var x1 = xs[1]; if (x0 < x1) { var _prec = x1; var _acc = 2; while (true) { var acc = _acc; var prec = _prec; if (acc >= len) { return acc; } var v = xs[acc]; if (prec >= v) { return acc; } _acc = acc + 1 | 0; _prec = v; continue; } ; } else if (x0 > x1) { return -sortedLengthAuxMore(xs, x1, 2, len) | 0; } else { return 1; } } // node_modules/@rescript/std/lib/es6/belt_internalSetString.js function has(_t, x) { while (true) { var t = _t; if (t === void 0) { return false; } var v = t.v; if (x === v) { return true; } _t = x < v ? t.l : t.r; continue; } ; } function addMutate(t, x) { if (t === void 0) { return singleton(x); } var k = t.v; if (x === k) { return t; } var l = t.l; var r = t.r; if (x < k) { t.l = addMutate(l, x); } else { t.r = addMutate(r, x); } return balMutate(t); } function fromArray2(xs) { var len = xs.length; if (len === 0) { return; } var next = strictlySortedLength(xs); var result; if (next >= 0) { result = fromSortedArrayAux(xs, 0, next); } else { next = -next | 0; result = fromSortedArrayRevAux(xs, next - 1 | 0, next); } for (var i = next; i < len; ++i) { result = addMutate(result, xs[i]); } return result; } // node_modules/@rescript/std/lib/es6/belt_SetString.js function add(t, x) { if (t === void 0) { return singleton(x); } var v = t.v; if (x === v) { return t; } var l = t.l; var r = t.r; if (x < v) { var ll = add(l, x); if (ll === l) { return t; } else { return bal(ll, v, r); } } var rr = add(r, x); if (rr === r) { return t; } else { return bal(l, v, rr); } } function splitAuxNoPivot(n, x) { var v = n.v; var l = n.l; var r = n.r; if (x === v) { return [ l, r ]; } if (x < v) { if (l === void 0) { return [ void 0, n ]; } var match = splitAuxNoPivot(l, x); return [ match[0], joinShared(match[1], v, r) ]; } if (r === void 0) { return [ n, void 0 ]; } var match$1 = splitAuxNoPivot(r, x); return [ joinShared(l, v, match$1[0]), match$1[1] ]; } function splitAuxPivot(n, x, pres) { var v = n.v; var l = n.l; var r = n.r; if (x === v) { pres.contents = true; return [ l, r ]; } if (x < v) { if (l === void 0) { return [ void 0, n ]; } var match = splitAuxPivot(l, x, pres); return [ match[0], joinShared(match[1], v, r) ]; } if (r === void 0) { return [ n, void 0 ]; } var match$1 = splitAuxPivot(r, x, pres); return [ joinShared(l, v, match$1[0]), match$1[1] ]; } function union(s1, s2) { if (s1 === void 0) { return s2; } if (s2 === void 0) { return s1; } var h1 = s1.h; var h2 = s2.h; if (h1 >= h2) { if (h2 === 1) { return add(s1, s2.v); } var v1 = s1.v; var l1 = s1.l; var r1 = s1.r; var match = splitAuxNoPivot(s2, v1); return joinShared(union(l1, match[0]), v1, union(r1, match[1])); } if (h1 === 1) { return add(s2, s1.v); } var v2 = s2.v; var l2 = s2.l; var r2 = s2.r; var match$1 = splitAuxNoPivot(s1, v2); return joinShared(union(match$1[0], l2), v2, union(match$1[1], r2)); } function diff(s1, s2) { if (s1 === void 0) { return s1; } if (s2 === void 0) { return s1; } var v1 = s1.v; var l1 = s1.l; var r1 = s1.r; var pres = { contents: false }; var match = splitAuxPivot(s2, v1, pres); var ll = diff(l1, match[0]); var rr = diff(r1, match[1]); if (pres.contents) { return concatShared(ll, rr); } else { return joinShared(ll, v1, rr); } } var fromArray3 = fromArray2; var has2 = has; var toArray2 = toArray; // src/RegexAst.bs.js var toEscape = /([.\][|*?+(){}^$\\:=])/g; var escapeRegEx = /(?:\\)+/g; var toEscapeInLiteral = /[\]\-\\]/g; function removeDoubleEscapes(value) { return value.split(escapeRegEx).join("\\"); } function sanitize(value) { return removeDoubleEscapes(value).replace(toEscape, function(match_, param, param$1) { return "\\" + match_; }); } function escapeCharsForLiterals(value) { return removeDoubleEscapes(value).split("").map(function($$char) { return $$char.replace(toEscapeInLiteral, function(match_, param, param$1) { return "\\" + match_; }); }).join(""); } function getNode(root) { return root.node; } function setNode(root, node) { return ( /* RootNode */ { prefix: root.prefix, node, suffix: root.suffix, flags: root.flags } ); } function setPrefix(root, prefix) { return ( /* RootNode */ { prefix, node: root.node, suffix: root.suffix, flags: root.flags } ); } function setSuffix(root, suffix) { return ( /* RootNode */ { prefix: root.prefix, node: root.node, suffix, flags: root.flags } ); } function addFlags(root, flags) { return ( /* RootNode */ { prefix: root.prefix, node: root.node, suffix: root.suffix, flags: union(root.flags, fromArray3(flags.split(""))) } ); } function removeFlags(root, flags) { return ( /* RootNode */ { prefix: root.prefix, node: root.node, suffix: root.suffix, flags: diff(root.flags, fromArray3(flags.split(""))) } ); } function hasFlag(root, flag) { return has2(root.flags, flag); } function makeNode(source, node, sanitize$1) { if (source !== void 0) { if (node !== void 0) { return node; } else if (sanitize$1) { return { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: sanitize(source) } }; } else { return { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: source } }; } } else if (node !== void 0) { return node; } else { return ( /* Empty */ 0 ); } } function defaultTo(defaultValue, value) { if (value !== void 0) { return valFromOption(value); } else { return defaultValue; } } function createRoot_(prefix, suffix, flags, source, node, sanitize2, param) { return ( /* RootNode */ { prefix, node: makeNode(source, node, sanitize2), suffix, flags } ); } function createRoot(param) { return createRoot_(defaultTo("", param.prefix), defaultTo("", param.suffix), fromArray3(defaultTo("gm", param.flags).split("")), param.source, param.node, defaultTo(true, param.sanitize), void 0); } function flags_to_opt_string(flags) { var str = toArray2(flags).join(""); if (str.length === 0) { return; } else { return str; } } function and_(a, b) { if (typeof a === "number") { if (typeof b === "number") { return ( /* Empty */ 0 ); } } else if (typeof b === "number") { return a; } if (typeof a === "number") { return b; } else { return { TAG: ( /* ConcatExpr */ 1 ), left: a, right: b, kind: "Conjunction" }; } } function seq(nodes, kindOpt, param) { var kind = kindOpt !== void 0 ? kindOpt : "Conjunction"; var nodeList = fromArray(nodes); if (nodeList) { var _node = nodeList.hd; var _nodeList = nodeList.tl; while (true) { var nodeList$1 = _nodeList; var node = _node; if (!nodeList$1) { return node; } var next = nodeList$1.hd; if (typeof next === "number") { _nodeList = nodeList$1.tl; continue; } _nodeList = nodeList$1.tl; _node = { TAG: ( /* ConcatExpr */ 1 ), left: node, right: next, kind }; continue; } ; } else { return ( /* Empty */ 0 ); } } function alt(nodes) { return seq(nodes, "Disjunction", void 0); } var then_ = and_; function maybe(nodeA, nodeB) { return and_(nodeA, { TAG: ( /* QuantifierExpr */ 2 ), node: nodeB, kind: "ZeroOrOne" }); } function or_(nodeA, nodeB) { if (typeof nodeA === "number" && typeof nodeB === "number") { return ( /* Empty */ 0 ); } else { return { TAG: ( /* ConcatExpr */ 1 ), left: nodeA, right: nodeB, kind: "Disjunction" }; } } function anything(node, asLazy) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* QuantifierExpr */ 2 ), node: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "." } }, kind: { NAME: "ZeroOrMore", VAL: asLazy ? "Lazy" : "Greedy" } }, kind: "Conjunction" }; } function something(node) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* QuantifierExpr */ 2 ), node: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "." } }, kind: "OneOrMore" }, kind: "Conjunction" }; } function anythingBut(node, characters, asLazy) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* QuantifierExpr */ 2 ), node: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Chars */ 1 ), chars: [{ NAME: "Chars", VAL: escapeCharsForLiterals(characters) }], negative: true } }, kind: { NAME: "ZeroOrMore", VAL: asLazy ? "Lazy" : "Greedy" } }, kind: "Conjunction" }; } function somethingBut(node, characters) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* QuantifierExpr */ 2 ), node: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Chars */ 1 ), chars: [{ NAME: "Chars", VAL: escapeCharsForLiterals(characters) }], negative: true } }, kind: "OneOrMore" }, kind: "Conjunction" }; } function anyOf(node, characters) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* QuantifierExpr */ 2 ), node: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Chars */ 1 ), chars: [{ NAME: "Chars", VAL: escapeCharsForLiterals(characters) }], negative: false } }, kind: "ZeroOrOne" }, kind: "Conjunction" }; } function someOf(node, characters) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* QuantifierExpr */ 2 ), node: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Chars */ 1 ), chars: [{ NAME: "Chars", VAL: escapeCharsForLiterals(characters) }], negative: false } }, kind: "OneOrMore" }, kind: "Conjunction" }; } function oneOf(node, characters) { return { TAG: ( /* ConcatExpr */ 1 ), left: node, right: { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Chars */ 1 ), chars: [{ NAME: "Chars", VAL: escapeCharsForLiterals(characters) }], negative: false } }, kind: "Conjunction" }; } function zeroOrMore(astNode, isLazy) { return { TAG: ( /* QuantifierExpr */ 2 ), node: astNode, kind: { NAME: "ZeroOrMore", VAL: isLazy !== void 0 && isLazy ? "Lazy" : "Greedy" } }; } function oneOrMore(astNode) { return { TAG: ( /* QuantifierExpr */ 2 ), node: astNode, kind: "OneOrMore" }; } function followedBy(node, lookahead) { return { TAG: ( /* LookaheadExpr */ 3 ), node, lookahead, kind: "Positive" }; } function notFollowedBy(node, lookahead) { return { TAG: ( /* LookaheadExpr */ 3 ), node, lookahead, kind: "Negative" }; } var tab = { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\t" } }; var digit = { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\d" } }; var whitespace = { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\s" } }; var word_0 = { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\w" } }; var word = { TAG: ( /* QuantifierExpr */ 2 ), node: word_0, kind: "OneOrMore" }; var linebreak = seq([ and_({ TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\r" } }, { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\n" } }), { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\r" } }, { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Exact */ 0 ), _0: "\\n" } } ], "Disjunction", void 0); function ranges(node, rangeArray, negateOpt, param) { var negate = negateOpt !== void 0 ? negateOpt : false; return and_(node, { TAG: ( /* Match */ 0 ), _0: { TAG: ( /* Chars */ 1 ), chars: rangeArray.map(function(param2) { return { NAME: "Range", VAL: [ escapeCharsForLiterals(param2[0]), escapeCharsForLiterals(param2[1]) ] }; }), negative: negate } }); } function repeat(node, low, high) { return { TAG: ( /* QuantifierExpr */ 2 ), node, kind: low !== void 0 ? high !== void 0 ? { NAME: "MinMax", VAL: [ Math.max(Math.min(low, high), 0), Math.max(0, low, high) ] } : { NAME: "MinMax", VAL: [ Math.max(low, 0), void 0 ] } : high !== void 0 ? { NAME: "MinMax", VAL: [ Math.max(high, 0), void 0 ] } : { NAME: "MinMax", VAL: [ low, high ] } }; } function repeatExact(node, n) { return { TAG: ( /* QuantifierExpr */ 2 ), node, kind: { NAME: "Exact", VAL: n } }; } function group(astNode, name) { return { TAG: ( /* CaptureGroup */ 5 ), node: astNode, name }; } var empty = ( /* Empty */ 0 ); // src/StringifyAst.bs.js function stringifyQuanifier(kind) { if (typeof kind === "string") { if (kind === "OneOrMore") { return "+"; } else { return "?"; } } var variant = kind.NAME; if (variant !== "MinMax") { if (variant === "Exact") { return "{" + (String(kind.VAL) + "}"); } else { return "*" + (kind.VAL === "Lazy" ? "?" : ""); } } var match = kind.VAL; var min2 = match[0]; if (min2 !== void 0) { var max2 = match[1]; if (max2 !== void 0) { return "{" + (String(min2) + ("," + (String(max2) + "}"))); } else { return "{" + (String(min2) + ",}"); } } var max$1 = match[1]; if (max$1 !== void 0) { return "{," + (String(max$1) + "}"); } else { return "{,}"; } } function stringifyNode(node, parents) { var id = function(x) { return x; }; var stringify2 = function(_node, _parents, _k) { while (true) { var k = _k; var parents2 = _parents; var node2 = _node; if (typeof node2 === "number") { return k(""); } switch (node2.TAG | 0) { case /* Match */ 0: var str = node2._0; if (str.TAG === /* Exact */ 0) { return k(str._0); } else { return k("[" + ((str.negative ? "^" : "") + (str.chars.map(function(kind2) { if (kind2.NAME !== "Range") { return kind2.VAL; } var match2 = kind2.VAL; return match2[0] + ("-" + match2[1]); }).join("") + "]"))); } case /* ConcatExpr */ 1: var right = node2.right; var left = node2.left; if (node2.kind === "Conjunction") { _k = function(node3, parents3, k2, right2) { return function(leftStr) { return stringify2(right2, { hd: node3, tl: parents3 }, function(rightStr) { return k2(leftStr + rightStr); }); }; }(node2, parents2, k, right); _parents = { hd: node2, tl: parents2 }; _node = left; continue; } if (parents2) { var match = parents2.hd; var exit = 0; if (typeof match !== "number") { switch (match.TAG | 0) { case /* ConcatExpr */ 1: if (match.kind === "Disjunction") { exit = 2; } break; case /* NonCaptureGroup */ 4: case /* CaptureGroup */ 5: exit = 2; break; default: } } if (exit === 2) { _k = function(node3, parents3, k2, right2) { return function(leftStr) { return stringify2(right2, { hd: node3, tl: parents3 }, function(rightStr) { return k2(leftStr + ("|" + rightStr)); }); }; }(node2, parents2, k, right); _parents = { hd: node2, tl: parents2 }; _node = left; continue; } } _k = function(node3, parents3, k2, right2) { return function(leftStr) { return stringify2(right2, { hd: node3, tl: parents3 }, function(rightStr) { return k2("(?:" + (leftStr + ("|" + (rightStr + ")")))); }); }; }(node2, parents2, k, right); _parents = { hd: node2, tl: parents2 }; _node = left; continue; break; case /* QuantifierExpr */ 2: var node$1 = node2.node; var exit$1 = 0; if (typeof node$1 === "number") { _parents = { hd: node2, tl: parents2 }; _node = node$1; continue; } switch (node$1.TAG | 0) { case /* Match */ 0: exit$1 = node$1._0.TAG === /* Exact */ 0 ? 1 : 2; break; case /* NonCaptureGroup */ 4: case /* CaptureGroup */ 5: exit$1 = 2; break; default: exit$1 = 1; } switch (exit$1) { case 1: var kind = node2.kind; _k = function(k2, kind2) { return function(nodeStr) { return k2(nodeStr + stringifyQuanifier(kind2)); }; }(k, kind); _parents = { hd: node2, tl: parents2 }; _node = { TAG: ( /* NonCaptureGroup */ 4 ), node: node$1 }; continue; case 2: var kind$1 = node2.kind; _k = function(k2, kind$12) { return function(nodeStr) { return k2(nodeStr + stringifyQuanifier(kind$12)); }; }(k, kind$1); _node = node$1; continue; } break; case /* LookaheadExpr */ 3: var kind$2 = node2.kind; var lookahead = node2.lookahead; _k = function(parents3, k2, lookahead2, kind$22) { return function(nodeStr_a) { return stringify2(lookahead2, { hd: { TAG: ( /* NonCaptureGroup */ 4 ), node: lookahead2 }, tl: parents3 }, function(lookaheadStr) { return k2(nodeStr_a + ("(?" + ((kind$22 === "Negative" && kind$22 !== "Positive" ? "!" : "=") + (lookaheadStr + ")")))); }); }; }(parents2, k, lookahead, kind$2); _parents = { hd: node2, tl: parents2 }; _node = node2.node; continue; case /* NonCaptureGroup */ 4: var node$2 = node2.node; if (parents2) { var tmp = parents2.hd; if (typeof tmp !== "number") { switch (tmp.TAG | 0) { case /* NonCaptureGroup */ 4: case /* CaptureGroup */ 5: _node = node$2; continue; default: } } } _k = function(k2) { return function(nodeStr) { return k2("(?:" + (nodeStr + ")")); }; }(k); _parents = { hd: node2, tl: parents2 }; _node = node$2; continue; case /* CaptureGroup */ 5: var name = node2.name; var node$3 = node2.node; if (name !== void 0) { _k = function(k2, name2) { return function(nodeStr) { return k2("(?<" + (name2 + (">" + (nodeStr + ")")))); }; }(k, name); _parents = { hd: node2, tl: parents2 }; _node = node$3; continue; } _k = function(k2) { return function(nodeStr) { return k2("(" + (nodeStr + ")")); }; }(k); _parents = { hd: node2, tl: parents2 }; _node = node$3; continue; } } ; }; return stringify2(node, parents, id); } function stringify(param) { return { source: param.prefix + (stringifyNode( param.node, /* [] */ 0 ) + param.suffix), flags: toArray2(param.flags).join("") }; } // src/RegExpstructor.ts var EXISTING_INSTANCES_KEY = "$7a3afcebc18ce55fe6f8445324d27b6e$"; var existingClasses = ( // @ts-ignore RegExp.prototype[Symbol.for(EXISTING_INSTANCES_KEY)] || [] ); Object.defineProperty(RegExp.prototype, Symbol.for(EXISTING_INSTANCES_KEY), { enumerable: false, configurable: true, writable: false, value: existingClasses }); var isInstanceOfExisting = (obj) => existingClasses.find((Class) => obj instanceof Class) != null; var isInstance = (obj) => { if (obj == null) return false; return obj instanceof RegExpstructor || isInstanceOfExisting(obj); }; var _rootNode, _setPrefix, _setSuffix; var _RegExpstructor = class { constructor(arg, init = true) { __privateAdd(this, _rootNode, void 0); __privateAdd(this, _setPrefix, (prefix) => { const newInstance = new _RegExpstructor(void 0, false); __privateSet(newInstance, _rootNode, setPrefix(__privateGet(this, _rootNode), prefix)); return newInstance; }); __privateAdd(this, _setSuffix, (suffix) => { const newInstance = new _RegExpstructor(void 0, false); __privateSet(newInstance, _rootNode, setSuffix(__privateGet(this, _rootNode), suffix)); return newInstance; }); if (init) { const { source, node, flags, prefix, suffix, sanitize: sanitize2 } = arg != null ? arg : {}; __privateSet(this, _rootNode, createRoot({ prefix, suffix, flags: flags_to_opt_string(flags), source, node, sanitize: sanitize2 })); } } /** * @description creates a ReStructor from a rootNode type * @param node */ static from(node) { return new this(node); } /** * @description creates ReStructors from a variety of source formats * @param x */ static of(x) { if (x instanceof this) { return x; } if (isInstance(x)) { return new this(this._preSanitize(x.compile())); } const params = this._preSanitize(x); return new this(params); } /** * @description disjunction (one must match) between the argument expressions * @param xs */ static or(...xs) { const verbals = xs.map((x) => { if (isInstance(x)) return x; return _RegExpstructor.of(x); }); const nodes = verbals.map((x) => this._getRootNode(x).node); return this.from({ node: alt(nodes) }); } /** * @description conjunction (all must match) of the argument expressions * @param xs */ static seq(...xs) { const verbals = xs.map((x) => _RegExpstructor.of(x)); const nodes = verbals.map((x) => this._getRootNode(x).node); return this.from({ node: seq(nodes) }); } // Rules // /** * @description Control start-of-line matching * @param [enable=true] whether to enable this behaviour */ assertStartOfLine(enable = true) { return __privateGet(this, _setPrefix).call(this, enable ? "^" : ""); } /** * @description Control end-of-line matching * @param [enable=true] whether to enable this behaviour */ assertEndOfLine(enable = true) { return __privateGet(this, _setSuffix).call(this, enable ? "$" : ""); } /** * @description Look for the value passed * @param value value to find */ then(value) { return _RegExpstructor.from( setNode( setSuffix(__privateGet(this, _rootNode), ""), then_(getNode(__privateGet(this, _rootNode)), __privateGet(_RegExpstructor.of(value), _rootNode).node) ) ); } /** * @description Add an optional branch for matching * @param value value to find */ maybe(value) { return _RegExpstructor.from( setNode( setSuffix(__privateGet(this, _rootNode), ""), maybe(getNode(__privateGet(this, _rootNode)), __privateGet(_RegExpstructor.of(value), _rootNode).node) ) ); } /** * @description Add alternative expressions * @param value value to find */ or(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), or_(getNode(__privateGet(this, _rootNode)), __privateGet(_RegExpstructor.of(value), _rootNode).node) ) ); } /** * @description Any character any number of times * @param lazy match least number of characters */ anything(lazy = false) { return _RegExpstructor.from( setNode(__privateGet(this, _rootNode), anything(getNode(__privateGet(this, _rootNode)), lazy)) ); } /** * @description Anything but these characters * @param value characters to not match * @param lazy match least number of characters */ anythingBut(value, lazy = false) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), anythingBut(getNode(__privateGet(this, _rootNode)), [value].flat().join(""), lazy) ) ); } /** * @description Any character(s) at least once */ something() { return _RegExpstructor.from( setNode(__privateGet(this, _rootNode), something(getNode(__privateGet(this, _rootNode)))) ); } /** * @description Any character at least one time except for these characters * @param value characters to not match */ somethingBut(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), somethingBut(getNode(__privateGet(this, _rootNode)), [value].flat().join("")) ) ); } /** * @description Match any of the given characters * @param value characters to match */ anyOf(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), anyOf(getNode(__privateGet(this, _rootNode)), [value].flat().join("")) ) ); } /** * @description Match some of the given characters * @param {(string|number|string[]|number[])} value characters to match */ someOf(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), someOf(getNode(__privateGet(this, _rootNode)), [value].flat().join("")) ) ); } /** * @description Match one chartacter of the given characters * @param {(string|number|string[]|number[])} value characters to match */ oneOf(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), oneOf(getNode(__privateGet(this, _rootNode)), [value].flat().join("")) ) ); } /** * @description Shorthand for anyOf(value) * @param {string|number} value value to find */ any(value) { return this.anyOf(value); } /** * @description Ensure that the parameter does not follow (negative lookahead) * @param {string|number|RegExp|RegExpstructor} value */ assertNotFollowedBy(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), notFollowedBy( getNode(__privateGet(this, _rootNode)), __privateGet(_RegExpstructor.of(value), _rootNode).node ) ) ); } /** * @description Ensure that the parameter does not follow (negative lookahead) * @param {string|number|RegExp|RegExpstructor} value */ notFollowedBy(value) { return this.assertNotFollowedBy(value); } /** * @description Ensure that the parameter does follow (positive lookahead) * @param {string|number|RegExp|RegExpstructor} value */ assertFollowedBy(value) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), followedBy( getNode(__privateGet(this, _rootNode)), __privateGet(_RegExpstructor.of(value), _rootNode).node ) ) ); } /** * @description Ensure that the parameter does follow (positive lookahead) * @param {string|number|RegExp|RegExpstructor} value */ followedBy(value) { return this.assertFollowedBy(value); } /** * @description Match any character in these ranges * @example RegExpstructor.empty.charOfRanges(["a","z"], ["0", "9"]) // [a-z0-9] * @param {...([string, string])} characterRanges total number of elements must be even * * */ charOfRanges(...characterRanges) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), ranges(getNode(__privateGet(this, _rootNode)), characterRanges, false) ) ); } /** * @description Match any character that is not in these ranges * @example RegExpstructor.empty.charNotOfRanges(["a","z"], ["0", "9"]) // [^a-z0-9] * @param {...([string, string])} characterRanges total number of elements must be even * * */ charNotOfRanges(...characterRanges) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), ranges(getNode(__privateGet(this, _rootNode)), characterRanges, true) ) ); } // Special characters // /** * @description Match a Line break */ lineBreak() { return this.then(_RegExpstructor.linebreak); } /** * @description A shorthand for lineBreak() for html-minded users */ br() { return this.lineBreak(); } /** * @description Match a tab character */ tab() { return this.then(_RegExpstructor.tab); } /** * @description Match any alphanumeric sequence */ word() { return this.then(_RegExpstructor.word); } /** * @description Match a single digit */ digit() { return this.then(_RegExpstructor.digit); } /** * @description Match a single whitespace */ whitespace() { return this.then(_RegExpstructor.whitespace); } // Modifiers // /** * @description Add a regex flag - default flags are: "gi" * @param {string} flag */ addFlag(flag = "") { return _RegExpstructor.from(addFlags(__privateGet(this, _rootNode), flag)); } /** * @description Remove a regex flag - default flags are: "gi" * @param {string} flag */ removeFlag(flag) { return _RegExpstructor.from(removeFlags(__privateGet(this, _rootNode), flag)); } /** * * @param {string} flag * @returns {boolean} */ hasFlag(flag) { return hasFlag(__privateGet(this, _rootNode), flag); } /** * @description Adds an "i" regex flag - default flags are: "gm" * @param enable */ withAnyCase(enable = true) { return enable ? this.addFlag("i") : this.removeFlag("i"); } /** * @description Removes a "g" regex flag - default flags are: "gm" * @param enable `true` means no "g" flag */ stopAtFirst(enable = true) { return !enable ? this.addFlag("g") : this.removeFlag("g"); } /** * * @param enable * */ global(enable = true) { return enable ? this.addFlag("g") : this.removeFlag("g"); } /** * * @param flag * */ toggleFlag(flag) { return !this.hasFlag(flag) ? this.addFlag(flag) : this.removeFlag(flag); } /** * @description Removes any set "m" regex flag - default flags are: "gm" * @param enable `true` means "m" flag will be removed */ searchOneLine(enable = true) { return enable ? this.removeFlag("m") : this.addFlag("m"); } // Loops // /** * @description match the expression <min> to <max> times * @example ```js * Sx("abc").whitespace().repeat(2, 4).compile().toString() === /(?:abc\w){2,4}/gm.toString() * ``` * @param min * @param max */ repeat(min2, max2) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), repeat( getNode(__privateGet(this, _rootNode)), min2 != null ? min2 | 0 : void 0, max2 != null ? max2 | 0 : void 0 ) ) ); } /** * @description match the expression exactly <n> times * @example ```js * Sx("abc").whitespace().repeatExactly(5).compile().toString() === /(?:abc\w){5}/gm.toString() * ``` * @param n must be > 0 */ repeatExactly(n) { return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), repeatExact(getNode(__privateGet(this, _rootNode)), n != null ? n | 0 : void 0) ) ); } /** * @description the expression should match at least once * */ oneOrMore() { return _RegExpstructor.from( setNode(__privateGet(this, _rootNode), oneOrMore(getNode(__privateGet(this, _rootNode)))) ); } /** * @description the expression should match zero or more times * @param lazy enable lazy (non greedy) matching */ zeroOrMore(lazy = false) { return _RegExpstructor.from( setNode(__privateGet(this, _rootNode), zeroOrMore(getNode(__privateGet(this, _rootNode)), lazy)) ); } // Capture groups // /** * * @param name optionally name your capturing group */ capture(name) { var _a, _b; return _RegExpstructor.from( setNode( __privateGet(this, _rootNode), group(getNode(__privateGet(this, _rootNode)), (_b = (_a = name == null ? void 0 : name.toString) == null ? void 0 : _a.call(name)) != null ? _b : void 0) ) ); } /** * * @param name optionally name your capturing group */ group(name) { return this.capture(name); } // Miscellaneous // /** * @description compile the ReStructor to a RegExp * */ compile() { const { source, flags } = stringify(__privateGet(this, _rootNode)); return new RegExp(source, flags); } }; var RegExpstructor = _RegExpstructor; _rootNode = new WeakMap(); _setPrefix = new WeakMap(); _setSuffix = new WeakMap(); __publicField(RegExpstructor, "_getRootNode", (x) => { if (!isInstance(x)) throw new Error(`${x} is not an instance of RegExpstructor`); function _getNode() { return __privateGet(this, _rootNode); } return _getNode.call(x); }); /** * @description a ReStructor that matches a whitespace */ __publicField(RegExpstructor, "whitespace", new _RegExpstructor({ node: whitespace })); /** * @description an empty ReStructor */ __publicField(RegExpstructor, "empty", new _RegExpstructor({ node: empty })); /** * @description match one digit */ __publicField(RegExpstructor, "digit", new _RegExpstructor({ node: digit })); /** * @description match a tab-character */ __publicField(RegExpstructor, "tab", new _RegExpstructor({ node: tab })); /** * @description matches a whole word */ __publicField(RegExpstructor, "word", new _RegExpstructor({ node: word })); /** * @description match any kind of line break or new-lines */ __publicField(RegExpstructor, "linebreak", new _RegExpstructor({ node: linebreak })); __publicField(RegExpstructor, "any", new _RegExpstructor({ node: something(empty) })); // Utility // __publicField(RegExpstructor, "_preSanitize", (value) => { if (value instanceof RegExp) { const source = value.source === "(?:)" ? "" : value.source; const prefix = source.startsWith("^") ? "^" : ""; const suffix = source.endsWith("$") ? "$" : ""; return { source: source.replace(/^\^/, "").replace(/\$$/, ""), flags: value.flags, sanitize: false, prefix, suffix }; } if (typeof value === "number") { return { source: value.toString(), sanitize: false }; } if (typeof value !== "string") { return { source: "", sanitize: false }; } return { source: value, sanitize: true }; }); existingClasses.push(RegExpstructor); RegExpstructor.prototype.notFollowedBy = RegExpstructor.prototype.assertNotFollowedBy; var Warning = class extends Error { constructor(msg) { super(msg); this.name = "Warning"; } }; var writeWarning = (text, logType = "error") => { return warn; function warn() { if (!warn.warned) { warn.warned = true; (typeof console[logType] === "function" ? ( // @ts-ignore console[logType] ) : console.error)(new Warning(text)); } } }; var warnArgType = writeWarning( "Argument type is not supported. Returning empty ReStructor." ); function ReStructor(value) { const valueType = typeof value; if (value != null && (valueType === "string" || valueType === "number" || value instanceof RegExp || isInstance(value))) { return RegExpstructor.of(value); } if (value != null && false) { warnArgType(); } return RegExpstructor.empty; } ReStructor.or = RegExpstructor.or.bind(RegExpstructor); ReStructor.seq = RegExpstructor.seq.bind(RegExpstructor); ReStructor.of = RegExpstructor.of.bind(RegExpstructor); ReStructor.digit = RegExpstructor.digit; ReStructor.word = RegExpstructor.word; ReStructor.empty = RegExpstructor.empty; ReStructor.linebreak = RegExpstructor.linebreak; ReStructor.tab = RegExpstructor.tab; ReStructor.whitespace = RegExpstructor.whitespace; ReStructor.any = RegExpstructor.any; // src/index.mjs var src_default = ReStructor; export { src_default as default };