-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug-example-regex.ts
51 lines (48 loc) · 1.42 KB
/
aug-example-regex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//import { createRequire } from "module";
import { languageFor } from "./core/languages.js";
import { useRef } from "./external/preact-hooks.mjs";
import { h } from "./external/preact.mjs";
import { log, metaexec } from "./sandblocks/query-builder/functionQueries.js";
export const augRegex = {
matcherDepth: Infinity,
model: languageFor("typescript"),
match: (it) =>
metaexec(it, (capture) => [
(it) => it.type == "regex_pattern",
capture("regex"),
]),
view: ({ regex }) => {
//var require = createRequire(import.meta.url);
var re = new RegExp(regex.sourceString);
const regexVisual = useRef(null);
try {
visualize(parse(re.source), getRegexFlags(re), regexVisual);
} catch (e) {
if (e instanceof parse.RegexSyntaxError) {
logError(re, e);
} else {
throw e;
}
}
return h("div", {}, regex.sourceString, h("div", { ref: regexVisual }));
},
rerender: () => true,
examples: [
["const test = /rgb( ?(d+), ?(d+), ?(d+), ?([01].d+))/gm", [0, 0]],
],
};
function logError(re, err) {
var msg = ["Error:" + err.message, ""];
if (typeof err.lastIndex === "number") {
msg.push(re);
msg.push(new Array(err.lastIndex).join("-") + "^");
}
console.log(msg.join("\n"));
}
function getRegexFlags(re) {
var flags = "";
flags += re.ignoreCase ? "i" : "";
flags += re.global ? "g" : "";
flags += re.multiline ? "m" : "";
return flags;
}