irongit

A better version of regex 101

just need to update 101 correctly

huncholanehuncholaneauthored
parent d2f45c1commit 447ece99af7295d1507ef753741590532a95abc8Browse files

2 files changed, +13 -75

+2-68src/App.jsx
@@ -1,4 +1,5 @@
11 import { useState } from "react";
2+import parseBuilder from "./builder";
23
34 const placeholder = `Enter your variables here like below.
45 group1Delim:\\n
@@ -8,74 +9,6 @@ group1Delim:\\n
89 \t\tvar3:\\w
910 \t\tvar4:\\d`;
1011
11-const buildToRegex = (build) => {
12- const re = buildToRegexRecursion(build, "");
13- console.log(re);
14- return re;
15-};
16-
17-const buildToRegexRecursion = (build, re) => {
18- if (!build?.length) return re;
19- const val = build.shift();
20- if (val.name) re += `(?P<${val.name}>${val.regex}`;
21- else re += `(?:${val.regex}`;
22- if (val.children) re += buildToRegexRecursion(val.children, "");
23- re += `${val.optional})`;
24-
25- return buildToRegexRecursion(build, re, val);
26-};
27-
28-const parseBuilder = (str) => {
29- const r = parseBuilderRegex(0);
30- const m = r.exec(str);
31- const vars = parseBuilderRecursion([], m, 0);
32- return buildToRegex(vars);
33-};
34-
35-const parseBuilderRecursion = (vars, pm, lvl) => {
36- if (!pm) return;
37- vars.push(pm.groups);
38- pm.input = pm.input.split(pm[0])[1];
39- if (!pm.input.trim()) {
40- return vars;
41- }
42-
43- // get next on this lvl
44- const r = parseBuilderRegex(lvl);
45- const m = r.exec(pm.input);
46-
47- // get between if there is a next on this lvl
48- if (m) {
49- const children = pm.input.split(m[0])[0];
50- const childR = parseBuilderRegex(lvl + 1);
51- const childM = childR.exec(children);
52- if (childM) {
53- pm.groups.children = [];
54- parseBuilderRecursion(pm.groups.children, childM, lvl + 1);
55- }
56- return parseBuilderRecursion(vars, m, lvl);
57- }
58- // gap between if there is not a next on this lvl
59- else {
60- const childR = parseBuilderRegex(lvl + 1);
61- const childM = childR.exec(pm.input);
62- if (childM) {
63- pm.groups.children = [];
64- parseBuilderRecursion(pm.groups.children, childM, lvl + 1);
65- }
66- return vars;
67- }
68-
69- // return this level with recursion
70-};
71-
72-const parseBuilderRegex = (numTabs) => {
73- return new RegExp(
74- `^(\\t{${numTabs}})(?<name>\\w*)(?<optional>\\??):(?<regex>.*)$`,
75- "gm"
76- );
77-};
78-
7912 function App() {
8013 const [state, setState] = useState({
8114 valid: true,
@@ -118,6 +51,7 @@ function App() {
11851 state.build = [];
11952 setState({ ...state });
12053 const parseBuild = parseBuilder(e.target.value);
54+ console.log(parseBuild);
12155 if (!parseBuild) setState({ valid: false });
12256 else setState({ valid: true });
12357 //regexInput.focus();
+11-7src/builder.js
@@ -1,11 +1,13 @@
1-export const parseBuilder = (str) => {
1+const parseBuilder = (str) => {
22 let lines = str.split("\n");
33 const ref = {};
44 for (let line of lines) {
5- const lineData =
6- /^(?<tabs>\s*)(?<name>\w*)(?<optional>\??):(?<regex>.*)/.exec(
7- line
8- ).groups;
5+ if (!line.trim()) continue;
6+ const r = /^(?<tabs>\s*)(?<name>\w*)(?<optional>\??):(?<regex>.*)/.exec(
7+ line
8+ );
9+ if (!r) return "";
10+ const lineData = r.groups;
911 const tabs = lineData.tabs.length;
1012 if (!ref[tabs]) ref[tabs] = [lineData];
1113 else ref[tabs].push(lineData);
@@ -16,10 +18,10 @@ export const parseBuilder = (str) => {
1618 else parent.children.push(lineData);
1719 }
1820 }
19- return ref[0];
21+ return buildToRegex(ref[0]);
2022 };
2123
22-export const buildToRegex = (build) => {
24+const buildToRegex = (build) => {
2325 const re = buildToRegexRecursion(build, "");
2426 return re;
2527 };
@@ -34,3 +36,5 @@ const buildToRegexRecursion = (build, re) => {
3436
3537 return buildToRegexRecursion(build, re);
3638 };
39+
40+export default parseBuilder;