-
Notifications
You must be signed in to change notification settings - Fork 8
/
regex-help.mjs
141 lines (124 loc) · 3.18 KB
/
regex-help.mjs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { promises as fs } from "fs";
const prettify = (regexJson) => {
const regex = JSON.parse(regexJson);
if (typeof regex !== "string") {
throw new Error("regex is not single JSON string");
}
let result = "";
let escaped = false;
let indent = 0;
for (let i = 0; i < regex.length; i++) {
const c = regex[i];
if (c === "\\" && !escaped) {
result += c;
escaped = true;
} else if (escaped) {
result += c;
escaped = false;
} else if (c === "(") {
indent++;
result += c;
result += "\n" + " ".repeat(indent * 4);
} else if (c === ")") {
indent--;
result += "\n" + " ".repeat(indent * 4);
result += c;
} else if (c === "|") {
result += c;
result += "\n" + " ".repeat(indent * 4);
} else {
result += c;
}
}
if (escaped || indent > 0) {
throw new Error("invalid regex: still escaped or indent>0 at EOF");
}
return result;
};
const minify = (regex) => {
const validateIndent = (at, indent) => {
const spaces = indent * 4;
for (let i = 0; i < spaces; i++) {
at++;
if (regex.length <= at || regex[at] !== " ") {
throw new Error("expected space");
}
}
return spaces;
};
const validateNewLineAndIndent = (at, indent) => {
at++;
if (regex.length <= at || regex[at] !== "\n") {
throw new Error("expected newline");
}
return 1 + validateIndent(at, indent);
};
const validateIndentAndClosingBacket = (at, indent) => {
const spaces = validateIndent(at, indent);
at += spaces + 1;
if (regex.length <= at || regex[at] !== ")") {
throw new Error(
`expected closing bracket at ${at} and found "${
regex[at]
}" in "${regex.substr(at - 10, 20)}"`
);
}
return spaces + 1;
};
regex = regex.trimEnd();
let result = "";
let escaped = false;
let indent = 0;
for (let i = 0; i < regex.length; i++) {
const c = regex[i];
if (c === "\\" && !escaped) {
result += c;
escaped = true;
} else if (escaped) {
result += c;
escaped = false;
} else if (c === "(") {
indent++;
result += c;
i += validateNewLineAndIndent(i, indent);
} else if (c === "\n") {
indent--;
i += validateIndentAndClosingBacket(i, indent);
result += ")";
} else if (c === "|") {
result += c;
i += validateNewLineAndIndent(i, indent);
} else {
result += c;
}
}
if (escaped || indent > 0) {
throw new Error("invalid regex: still escaped or indent>0 at EOF");
}
return JSON.stringify(result);
};
const main = async () => {
if (process.argv.length !== 4) {
console.log(`Usage: ${process.argv[1]} <prettify|minify> <regex-file>`);
process.exitCode = 1;
return;
}
const [cmd, regexFile] = process.argv.slice(2);
const regex = await fs.readFile(regexFile, "utf8");
let result;
switch (cmd) {
case "prettify":
result = prettify(regex);
break;
case "minify":
result = minify(regex);
break;
default:
throw new Error("invalid command");
}
console.log(result);
};
main().catch((err) => {
console.error(err);
process.exitCode = 1;
});