-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdumpScopes.mjs
More file actions
118 lines (111 loc) · 2.73 KB
/
dumpScopes.mjs
File metadata and controls
118 lines (111 loc) · 2.73 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
/// node dumpScopes.mjs >langScopes
import jai from './src/languages/jai.js';
/*
import hljs from 'highlight.js';
/*/
import hljs from './localHilightDebug.js';
/**/
hljs.registerLanguage('jai', jai);
const seen = new Set();
function eq(a, b, path = '$') {
if (path === '$') seen.clear();
if (seen.has(a)) {
//console.log('already seen', path);
return true;
}
seen.add(a);
console.log('eq', path, '\x1b[K\x1b[A');
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
console.log(`Different at ${path}`);
return false;
}
for (let i = 0; i < a.length; i++) {
if (!eq(a[i], b[i], `${path}[${i}]`)) return false;
}
return true;
} else if (a && b && typeof a === 'object' && typeof b === 'object') {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
for (let key of aKeys) {
if (!eq(a[key], b[key], `${path}.${key}`)) return false;
}
for (let key of bKeys.filter(k => !aKeys.includes(k))) {
console.log(`Different at ${path}.${key}`, { a: a[key], b: b[key] });
}
return true;
} else if (typeof a === 'function' && typeof b === 'function') {
if (a.toString() !== b.toString()) {
console.log(`Different at ${path}`);
}
return a.toString() === b.toString();
} else {
if (path.endsWith('_inheritID')) return true;
if (a !== b) {
console.log(`Different at ${path}`, { a, b });
}
return a === b;
}
}
function walk(obj, matches, path = '$') {
const result = [];
if (path === '$') {
seen.clear();
matches = matches.map(m => {
const r = new RegExp(
m.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
.replace(/\./g, '\\.')
.replace(/:/g, '')
.replace(/(.+)\?/g, '(?<=$1)[^\\[$]+')
.replace(/\*/g, '[^.$]+')
+ '$'
);
r.matchValue = m.endsWith(':');
return r;
});
}
matches.forEach(m => {
const match = m.exec(path);
if (match) {
result.push(m.matchValue ? obj : match[0]);
}
});
//console.log('walk', path, '\x1b[K\x1b[A');
if (Array.isArray(obj)) {
if (seen.has(obj)) {
//console.log('already seen []', path);
return result;
}
seen.add(obj);
for (let i = 0; i < obj.length; i++) {
const sub = walk(obj[i], matches, `${path}[${i}]`);
result.push(...sub);
}
} else if (obj && typeof obj === 'object') {
if (seen.has(obj)) {
//console.log('already seen {}', path);
return result;
}
seen.add(obj);
const keys = Object.keys(obj);
for (let key of keys) {
const sub = walk(obj[key], matches, `${path}.${key}`);
result.push(...sub);
}
}
return result;
}
const jaiDef = jai(hljs);
const langScopes = walk(jaiDef, ['keywords.?', 'scope:', 'beginScope.*:', `endScope.*:`]);
let prev = "";
langScopes
.sort()
.forEach(
s => {
if (s !== prev) {
console.log(s);
prev = s;
}
}
);