forked from zaach/jison
-
Notifications
You must be signed in to change notification settings - Fork 20
/
__patch_parser_kernel_in_js.js
109 lines (87 loc) · 3.37 KB
/
__patch_parser_kernel_in_js.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
const globby = require('globby');
const fs = require('fs');
function encode(str) {
return str
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\$\{/g, '$\\{')
.trim();
}
var kernel = encode(fs.readFileSync('lib/jison-parser-kernel.js', 'utf8'));
var parseErrorCode = encode(fs.readFileSync('lib/jison-parser-parseError-function.js', 'utf8'));
var errorClassCode = encode(fs.readFileSync('lib/jison-parser-error-code.js', 'utf8'));
var debugTraceCode = encode(fs.readFileSync('lib/jison-parser-debugTrace-function.js', 'utf8'));
var commonJsMainCode = encode(fs.readFileSync('lib/jison-parser-commonJsMain-function.js', 'utf8'));
var parserAPIs1Code = encode(fs.readFileSync('lib/jison-parser-API-section1.js', 'utf8'))
.replace(/^[^{]*\{/, '')
.replace(/\}[^\}]*$/, '')
.trim();
// DIAGNOSTICS/DEBUGGING:
//
// set `test` to TRUE to help verify that all code chunks are properly detected and edited:
const test = false;
if (test) {
kernel = 'kernel::xxxxxx';
parseErrorCode = 'parseError::xxxxxx';
errorClassCode = 'errorClass::xxxxxx';
debugTraceCode = 'debugTrace::xxxxxx';
commonJsMainCode = 'commonJsMain::xxxxxx';
parserAPIs1Code = 'APIchunk::xxxxxx';
}
globby(['lib/jison.js']).then(paths => {
var count = 0;
var edit_cnt = 0;
//console.log(paths);
paths.forEach(path => {
var updated = false;
//console.log('path: ', path);
var src = fs.readFileSync(path, 'utf8');
var dst = src
.replace(/(\/\/ --- START parser kernel ---)[^]+?(\/\/ --- END( of)? parser kernel ---)/, function f(m, p1, p2) {
edit_cnt++;
return p1 + `
parser.parse = \`
${kernel}
\`;
` + p2;
})
.replace(/(\/\/ --- START parser error class ---)[^]+?(\/\/ --- END( of)? parser error class ---)/, function f(m, p1, p2) {
edit_cnt++;
return p1 + `
const prelude = \`
${errorClassCode}
\`;
` + p2;
})
.replace(/(const parseErrorSourceCode = `)[^]+?(\/\/ --- END( of)? parseErrorSourceCode chunk ---)/, function f(m, p1, p2) {
edit_cnt++;
return p1 + '\n' + parseErrorCode + '\n\`;\n' + p2;
})
.replace(/(const debugTraceSrc = `)[^]+?(\/\/ --- END( of)? debugTraceSrc chunk ---)/, function f(m, p1, p2) {
edit_cnt++;
return p1 + '\n' + debugTraceCode + '\n\`;\n' + p2;
})
.replace(/(const commonJsMain = `)[^]+?(\/\/ --- END( of)? commonJsMain chunk ---)/, function f(m, p1, p2) {
edit_cnt++;
return p1 + '\n' + commonJsMainCode + '\n\`;\n' + p2;
})
.replace(/(const define_parser_APIs_1 = `)[^]+?(\/\/ --- END( of)? define_parser_APIs_1 chunk ---)/, function f(m, p1, p2) {
edit_cnt++;
return p1 + '\n ' + parserAPIs1Code + '\n\`;\n' + p2;
});
updated = (dst !== src);
if (updated) {
count++;
console.log('updated: ', path);
fs.writeFileSync(path, dst, {
encoding: 'utf8',
flags: 'w'
});
}
});
if (edit_cnt !== 6) {
console.error('ERROR: unexpected number of edits: check jison.js and this patch tool\'s regexes, then fix them or this verification number:', edit_cnt);
process.exit(1);
}
console.log('\nUpdated', count, 'files\' parser kernel core code.');
});