-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.mjs
More file actions
298 lines (262 loc) · 7.75 KB
/
debug.mjs
File metadata and controls
298 lines (262 loc) · 7.75 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
function emitterStack() {
return $emitter.stack.map(
(s, i) => typeof(s) === 'string'
? `${i}"${s}"`
: `${i}!${s._id}:${s.scope ?? s.$name ?? '???'}[${s.children?.length ?? '-'}]`
//{${s.children.map((c, j) => typeof (c) === 'string' ? `${j}"${c}"` : `${j}/${c._id}:${c.scope ?? c.$name ?? '??'}[${c.children?.length ?? '-'}]`)}}`
).toReversed();
}
function matchMode($m, matcher) {
const extractName = re => /^\(\?!\n'([^']+).+$/.exec(re)?.[1];
return $m
.map((e, i) => ({ e, i }))
.filter(e => e.e !== undefined)
.map(e => `${e.i}:[${e.e}]`)
.concat(
[
$m.type,
(typeof($r = $m.rule) !== 'undefined'
? $r.scope
?? $r.$name
?? (($b = $r.beginScope) && JSON.stringify($b))
?? $r.beginRe
: `${
extractName(matcher?.rules?.[$m.position][0])
?? `${($s = $emitter.stack.slice(-1)[0])._id}<${$s.scope}>`
}`//${($s = $emitter.stack.slice(-1)[0])._id}<${$s.scope}>`
)
]
);
}
function textLeft(index, s) {
return `${index}[${s.substr(index)}]`;
}
function matchNext(m) {
switch (m.constructor.name) {
case 'ResumableMultiRegex':
return `[${m.regexIndex}/${m.rules.length}]${m.lastIndex}${m.rules[m.regexIndex % m.rules.length][0]}`;
case 'MultiRegex':
return m.regexes.map((r, i) => `${i}:${($r = r[0]).type} ${$r.type === 'begin' ? $r.rule.$name ?? $r.rule.scope : ''}`);
default:
return m.constructor.name;
};
}
const seen = new Set();
function walk(obj, matches, proc, path = '$', key = "", parent = null, grandParent = null, parentName = null, grandParentName = null, namePath = []) {
if (path.length === 1) {
seen.clear();
matches = matches.map(m => {
const r = new RegExp(
m.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
.replace(/\./g, '\\.')
.replace(/\$/g, '\\$')
.replace(/:/g, '')
.replace(/(.+)\?/g, '(?<=$1)[^\\[$]+')
.replace(/^\^/, '(?<!:)')
.replace(/\*/g, '[^.$]+')
.replace(/\//, '\\[\\d+\\]')
+ '$'
);
r.matchValue = m.endsWith(':');
return r;
});
}
const parentKey = grandParent && parent && Object.keys(grandParent).find(k => grandParent[k] === parent);
let name = (
obj.$name
?? obj.scope
?? obj.name
?? (
`${key}`.length && isFinite(Number(key))
&& grandParent?.[parentKey.includes("Scope") ? parentKey.replace("Scope", "") : parentKey + "Scope"]?.[key + 1]
)
) || '';
if (name) {
if (/variants\[\d+\].\w+$/.test(path)) {
name = `\t${name}`;
}
} else if (parentKey === 'begin' || parentKey === 'end') {
name = `${grandParentName ?? grandParent?.[parentKey + 'Scope']?.[key + 1] ?? '...'}`;
}
namePath = [
...namePath,
`${`${key}`.length ? isFinite(Number(key)) ? `[${key}]` : `.${key}` : ''}${name}`
];//console.debug(namePath.join(''));
let reName = namePath[namePath.length - 1].replace(/^\[\d+\]/, '');
if (reName == '.begin' || reName == '.end') {
reName = namePath[namePath.length - 2].replace(/^\[\d+\]/, '');
}
matches.forEach(m => {
const match = m.exec(path);
if (match) {
const result = proc(reName, obj, path, namePath);
if (result !== obj) {
obj = result;
}
}
});
//console.log('walk', path, '\x1b[K\x1b[A');
if (Array.isArray(obj)) {
if (seen.has(obj)) {
return obj;
}
seen.add(obj);
for (let i = 0; i < obj.length; i++) {
const result = walk(obj[i], matches, proc, `${path}[${i}]`, i, obj, parent, name, parentName, namePath);
if (result !== obj[i]) {
obj[i] = result;
}
}
} else if (obj && typeof obj === 'object') {
if (seen.has(obj)) {
return obj;
}
seen.add(obj);
const keys = Object.keys(obj);
for (let key of keys) {
const result = walk(obj[key], matches, proc, `${path}.${key}`, key, obj, parent, name, parentName, namePath);
if (result !== obj[key]) {
obj[key] = result;
}
}
}
return obj;
}
function reError(re, i, path, message) {
const min = Math.max(0, i - 20);
const max = min + 40;
const error = `${message} at position ${i} for ${path}:\n${min ? '...' : ''}${re.slice(min, max)}${max <= re.length ? '...' : ''}\n${' '.repeat(max <= re.length ? i - min + 3 : i - min)}^\n${re}`;
console.error(error);
throw new Error(error);
}
function validateAtomics(re, path, validateParenthes = true) {
if (!/\(\?=\(.+?\\\d/.test(re) || !validateParenthes || re.indexOf(('(')) === -1) {
return;
}
const depthStack = [];
const groups = [-1];
const atomics = [];
for (let i = 0, len = re.length, end = len - 1; i < len; i++) {
if (/^\\[()]/.test(re.slice(i, i + 2))) {
i += 1;
continue; // <- i++
} else if (re[i] === '(') {
depthStack.push(i);
if (!(re[i + 1] === '?' && ':=!<'.includes(re[i + 2]))) {
groups.push(i);
} else if (re.slice(i, i + 4) === '(?=(') {
atomics.push(i + 3);
i += 2;
continue; // <- i++
}
} else if (re[i] === ')') {
if (!depthStack.length) {
reError(re, i, path, 'Unmatched )');
}
depthStack.pop();
} else if (match = /^\\(\d+)\b/.exec(re.slice(i))) {
groupNum = +match[1];
if (groupNum > groups.length) {
reError(re, i, path, `Invalid backreference \\${groupNum} (only ${groups.length} groups)`);
}
if (atomics[atomics.length - 1] === groups[groups.length - 1]) {
if (groupNum !== groups.length - 1) {
reError(re, i, path, `Mismatched Atomic backref \\${groupNum} (expected ${groups.length})`);
}
}
} else if (i === end && depthStack.length) {
reError(re, i, path, 'Unterminated (');
}
}
}
function regexDebugPre(lang) {
const defn = walk(lang(hljs), ['^begin', 'begin/', '^end', 'end/', '$pattern'], (name, value, _, namePath) => {
if (Array.isArray(value)) {
return value;
}
let re = value;
if (typeof(re) !== 'string') {
re = re.source;
}
if (re === undefined) {
console.error('undefined regex at', namePath, value);
debugger;
}
let i = 0;
validateAtomics(re, namePath.reduce((s, p) => `${s}\n${' '.repeat(i++)}${p}`, ''));
re = `(?!\n'${name}')` + re;
return re;
});
return () => defn;
}
function clearHilight() {
[...document.getElementsByClassName('hover')].forEach(el => {
el.setAttribute('class', el.getAttribute('class').replace(/ hover d\d/, ''));
});
}
function hilightPath(clear) {
return (e) => {
if (clear) {
clearHilight();
}
let el = e.target;
for (let i = 1; el.tagName === 'SPAN'; i++) {
el.classList.add('hover');
el.classList.add(`d${i}`);
el = el.parentElement;
}
};
}
function getClassPath(el) {
if (!el.classList.length) {
return '';
}
const classes = [];
let e = el;
while (e && e.tagName === 'SPAN') {
classes.unshift('.' + e.getAttribute('class').replace(/ /g, '.'));
e = e.parentElement;
}
return classes.reverse().join('\n <- ');
}
function applyDebugInfo() {
setTimeout(() => [...document.getElementsByTagName('span')].forEach(span => {
span.setAttribute('title', getClassPath(span));
span.onmouseenter = hilightPath(true);
span.onmousemove = hilightPath(false);
span.onmouseleave = clearHilight;
}), 1000);
}
function debugInit(langName = 'jai', lang = jai) {
hljs.debugMode();
hljs.unregisterLanguage(langName);
hljs.registerLanguage(langName, regexDebugPre(lang));
console.log('starting...');
console.time('hilight');
hljs.highlightElement(document.getElementById('it').firstChild);
console.timeEnd('hilight');
applyDebugInfo();
}
//Breakpoints: processLexeme(bef; [const match = this.matcherRe.exec(s);]+1.
//Live expressions:
//emitterStack()
//
//matchMode(typeof match !== 'undefined'?match:result)
//
//textLeft(typeof index!=='undefined'?index:this.lastIndex, typeof codeToHighlight!='undefined'?codeToHighlight:s)
//
//matchNext(top !== window.top && typeof top.matcher !== undefined ? top.matcher : this)
window.jaiDebug = {
emitterStack,
matchMode,
textLeft,
matchNext,
walk,
regexDebugPre,
clearHilight,
hilightPath,
getClassPath,
applyDebugInfo,
debugInit
};