-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerate.js
428 lines (344 loc) · 11.7 KB
/
generate.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
const fs = require('fs')
const isEqual = require('lodash.isequal')
const SCRIPTS = {}
const include = function (src) {
// Some black magic of eval. Load the script from src to global scope. Source: https://stackoverflow.com/a/23699187/17140794
(1, eval)(src.toString())
}
async function loadScript(src) {
const script = await fetch(src)
const text = await script.text()
include(text)
}
async function loadLanguages(lngs) {
if (lngs) {
lngs = Array.isArray(lngs) ? lngs : [lngs];
for (const lng of lngs) {
await loadLanguage(lng)
}
}
}
let langNumber = 0
async function loadLanguage(lng) {
if (!components.languages[lng].title) {
return
}
await loadLanguages(components.languages[lng].optional)
await loadLanguages(components.languages[lng].require)
await loadLanguages(components.languages[lng].modify)
if (!SCRIPTS[lng]) {
SCRIPTS[lng] = true
langNumber += 1
console.log(`${langNumber} | Loading ${lng}`);
// TODO: version should probably not be hardcoded
await loadScript(`https://cdn.jsdelivr.net/npm/[email protected]/components/prism-${lng}.min.js`)
}
}
function loadLocalLanguage(path, code, title, alias) {
include(fs.readFileSync(path))
components.languages[code] = {
title: title,
alias: alias
}
}
function unique(a, fn) {
if (a.length === 0 || a.length === 1) {
return a;
}
if (!fn) {
return a;
}
for (let i = 0; i < a.length; i++) {
for (let j = i + 1; j < a.length; j++) {
if (fn(a[i], a[j])) {
a.splice(i, 1);
}
}
}
return a;
}
function uniqlo(a, fn) {
var size = a.length;
do {
size = a.length
a = unique(a, fn)
}
while (size > a.length)
return a
}
async function generate() {
var tempPatterns = []
var tempLanguages = {}
var tempTokens = []
var tempGrammars = []
var weak = new WeakMap
function flatten(grammar) {
var keys = {}
var cache = weak.get(grammar)
if (cache !== undefined) {
return cache
}
weak.set(grammar, keys)
var copy = grammar;
var rest = copy.rest;
if (rest) {
copy = {}
Object.keys(grammar).forEach(name => {
copy[name] = grammar[name]
})
for (var token in rest) {
copy[token] = rest[token];
}
delete copy.rest;
}
function sanitize(pattern) {
// Unsupported:
// UTF-16 ranges
// [^] => [\s\S] <- matches any character, including new line
// [] => ?? <- matches _empty_ string
// All the whitelisted languages have 0xFFFF as maximum range
// This is not the case for all the grammars supported by Prisma.
pattern = pattern.replaceAll("\\uFFFF", "\\xFF");
pattern = pattern.replaceAll("[^]", "[\\s\\S]");
// TODO: This just bruteforces the regex to work, but of course
// result may vary from the original one.
//static const boost::regex hex(R"(\\u([0-9a-fA-F]{4}))");
//pattern = boost::regex_replace(pattern, hex, R"(\\xFF)");
// TODO: Again, none of the whitelisted languages use [], but others do.
// Howhever, it is unclear to me how [] is supposed to work.
pattern = pattern.replaceAll("|[])", ")");
pattern = pattern.replaceAll(":[]", ":");
return pattern
}
for (var token in copy) {
if (!copy.hasOwnProperty(token) || !copy[token]) {
continue;
}
var patterns = copy[token];
patterns = Array.isArray(patterns) ? patterns : [patterns];
var indexes = []
for (var j = 0; j < patterns.length; ++j) {
var patternObj = patterns[j];
var inside = patternObj.inside;
var lookbehind = !!patternObj.lookbehind;
var greedy = !!patternObj.greedy;
var alias = patternObj.alias;
//alias = Array.isArray(alias) ? alias : [alias];
//alias = alias.join('/')
alias = Array.isArray(alias) ? alias[0] : alias;
var pattern = patternObj.pattern || patternObj;
var patternStr = sanitize(pattern.toString())
if (lookbehind) {
patternStr += "l"
}
if (greedy) {
patternStr += "y"
}
var np
if (alias || inside) {
np = {
pattern: patternStr
}
if (alias) {
np.alias = alias
}
if (inside) {
np.inside = flatten(inside)
}
} else if (pattern instanceof RegExp) {
np = patternStr
} else {
debugger
}
tempPatterns.push(np)
indexes.push(np)
}
keys[token] = indexes
tempTokens.push(indexes)
}
tempGrammars.push(keys)
return keys
}
var unsupported = [
"bsl",
"coq",
"gherkin",
"jexl",
"kumir",
"pure",
"purescript",
"turtle",
"sparql" // requires turtle
]
await loadScript("https://cdn.jsdelivr.net/npm/[email protected]/components/prism-core.min.js")
await loadScript("https://prismjs.com/components.js")
await loadLanguages(Object.keys(components.languages))
console.log(`\nLoaded all ${langNumber} languages`)
console.log("Processing...")
// Manually add local definitions
loadLocalLanguage('./components/prism-tl.js', 'typelanguage', 'TypeLanguage', 'tl')
loadLocalLanguage('./components/prism-tlb.js', 'tlb', 'TypeLanguage-Binary', 'tlb')
loadLocalLanguage('./components/prism-fift.js', 'fift', 'Fift', 'fift')
loadLocalLanguage('./components/prism-func.js', 'func', 'FunC', 'func')
loadLocalLanguage('./components/prism-tact.js', 'tact', 'Tact', 'tact')
Object.keys(Prism.languages).forEach(lng => {
if (unsupported.includes(lng) || !components.languages[lng]) {
return
}
tempLanguages[lng] = flatten(Prism.languages[lng])
})
var allTokens = uniqlo(tempTokens, isEqual)
var allGrammars = uniqlo(tempGrammars, isEqual)
var allPatterns = uniqlo(tempPatterns, isEqual)
Object.keys(tempLanguages).forEach(name => {
var find = allGrammars.find(x => isEqual(x, tempLanguages[name]))
if (find === undefined) {
debugger
}
tempLanguages[name] = find
})
for (var i = 0; i < allPatterns.length; i++) {
if (allPatterns[i].inside) {
var find = allGrammars.find(x => isEqual(x, allPatterns[i].inside))
if (find === undefined) {
debugger
}
allPatterns[i].inside = find
}
}
for (var i = 0; i < allTokens.length; i++) {
var token = allTokens[i]
for (var j = 0; j < token.length; j++) {
var find = allPatterns.find(x => isEqual(x, token[j]))
if (find === undefined) {
debugger
}
token[j] = find
}
}
for (var i = 0; i < allGrammars.length; i++) {
Object.keys(allGrammars[i]).forEach(name => {
var find = allTokens.find(x => isEqual(x, allGrammars[i][name]))
if (find === undefined) {
debugger
}
allGrammars[i][name] = find
})
}
for (var i = 0; i < allPatterns.length; i++) {
if (allPatterns[i].inside) {
allPatterns[i].inside = allGrammars.indexOf(allPatterns[i].inside)
}
}
for (var i = 0; i < allTokens.length; i++) {
var token = allTokens[i]
for (var j = 0; j < token.length; j++) {
token[j] = allPatterns.indexOf(token[j])
}
}
/*for (var i = 0; i < allGrammars.length; i++) {
Object.keys(allGrammars[i]).forEach(name => {
if (allGrammars[i][name].length == 1) {
allGrammars[i][name] = allGrammars[i][name][0]
}
})
}*/
for (var i = 0; i < allPatterns.length; i++) {
if (allPatterns[i].pattern) {
var patternStr = allPatterns[i].pattern + ",";
if (allPatterns[i].alias) {
patternStr += allPatterns[i].alias
}
patternStr += ","
if (allPatterns[i].inside) {
patternStr += allPatterns[i].inside
}
allPatterns[i] = patternStr
} else {
allPatterns[i] += ",,"
}
}
var allLanguages = {}
var languageNames = {}
Object.keys(tempLanguages).forEach(name => {
var find = allGrammars.find(x => isEqual(x, tempLanguages[name]))
if (find === undefined) {
debugger
}
allLanguages[name] = allGrammars.indexOf(find)
languageNames[name] = components.languages[name].title
var alias = components.languages[name].alias
if (alias) {
alias = Array.isArray(alias) ? alias : [alias];
for (const lng of alias) {
allLanguages[lng] = allGrammars.indexOf(find)
if (components.languages[name].aliasTitles) {
languageNames[lng] = components.languages[name].aliasTitles[lng]
} else {
languageNames[lng] = components.languages[name].title
}
}
}
})
var final = {
patterns: allPatterns,
grammars: allGrammars,
languages: allLanguages
}
const chunks = [];
const writeUint16 = i => chunks.push(new Uint16Array([i]))
const writeUint8 = i => chunks.push(new Uint8Array([i]))
const writeString = str => {
if (str.length < 253) {
writeUint8(str.length)
} else {
writeUint8(254 & 0xFF)
writeUint8(str.length & 0xFF)
writeUint8((str.length >> 8) & 0xFF)
writeUint8((str.length >> 16) & 0xFF)
}
chunks.push(new Uint8Array(str.split('').map(char => char.charCodeAt(0))))
}
// Patterns
writeUint16(allPatterns.length)
allPatterns.forEach(pattern => {
writeString(pattern)
})
// Grammars
writeUint16(allGrammars.length)
for (var i = 0; i < allGrammars.length; i++) {
writeUint8(Object.keys(allGrammars[i]).length)
Object.keys(allGrammars[i]).forEach(name => {
writeString(name)
writeUint8(allGrammars[i][name].length)
allGrammars[i][name].forEach(id => {
writeUint16(id)
})
})
}
// Languages
writeUint16(Object.keys(allLanguages).length)
Object.keys(allLanguages).forEach(name => {
writeString(name)
if (languageNames[name]) {
writeString(languageNames[name])
} else {
writeString("")
}
writeUint16(allLanguages[name])
})
const blob = new Blob(chunks, {type: 'application/octet-binary'});
console.log(blob)
return blob;
}
async function saveBlob(blob, filename) {
console.log(`Saving ${blob} to ${filename}`)
const buffer = Buffer.from(await blob.arrayBuffer())
fs.writeFileSync(filename, buffer)
}
const filepath = "libprisma/grammars.dat";
generate().then(blob => {
saveBlob(blob, filepath).then(() => {
console.log("Done! Saved to " + filepath)
})
})