-
Notifications
You must be signed in to change notification settings - Fork 2
/
rewrite-dbscheme.js
107 lines (92 loc) · 3.04 KB
/
rewrite-dbscheme.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
const fs = require('fs');
// helpers
function read(filename) { return fs.readFileSync(filename); }
function readLines(filename) { return read(filename).toString().split('\n'); }
function write(filename, data) { fs.writeFileSync(filename, data); }
function writeLines(filename, lines) { write(filename, lines.join('\n')); }
function walk(dir, ext) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file, ext));
} else if (file.endsWith(ext)) {
/* Is a file */
results.push(file);
}
});
return results;
}
function rewriteDBSchemes(dir, lang1, lang2, rewritter1, rewritter2) {
console.log(`making new dbscheme in lib-${dir} by merging lib/${lang1} and lib/${lang2}`);
// variables
const STAT1 = walk(`lib/${lang1}/ql`, 'stats')[0];
const STAT2 = walk(`lib/${lang2}/ql`, 'stats')[0];
const DBSCHEME1 = STAT1.slice(0, -6);
const DBSCHEME2 = STAT2.slice(0, -6);
const DBSCHEME = `lib-${dir}/merged.dbscheme`;
const STAT = DBSCHEME + ".stats";
// dbscheme
const dbscheme1 = readLines(DBSCHEME1);
const dbscheme2 = readLines(DBSCHEME2);
/*
var lastOpen = -1;
var startIdx, endIdx;
for (let i = 0; i < cpp_dbscheme.length; ++i) {
let l = cpp_dbscheme[i];
if (l.startsWith("/**")) {
lastOpen = i;
}
if (l.includes("sourceLocationPrefix")) {
startIdx = lastOpen;
endIdx = i;
break;
}
}
cpp_dbscheme.splice(startIdx, endIdx - startIdx + 1);
*/
const new_dbscheme1 = dbscheme1.map(rewritter1);
const new_dbscheme2 = dbscheme2.map(rewritter2);
writeLines(DBSCHEME, new_dbscheme1.concat(new_dbscheme2));
// dbscheme.stat
const stat1 = readLines(STAT1);
const stat2 = readLines(STAT2);
/* TODO */
const stat = stat2.map(l => {
l = l.replaceAll("@", "@cpp_");
if(l.includes("name") && !l.includes("sourceLocationPrefix"))
return l.replace("<name>", "<name>cpp_");
return l;
});
/* End Of TODO */
writeLines(STAT, stat);
}
function javaRewritter(l) {
l = l.replaceAll("@", "@java_");
if(l.startsWith(" ") || l.startsWith("//") || l.startsWith("#") || !l.includes("("))
return l;
//if(l.includes("sourceLocationPrefix"))
//return l;
return 'java_' + l;
}
function cppRewritter(l) {
l = l.replaceAll("@", "@cpp_");
if(l.includes("sourceLocationPrefix"))
return l;
if(l.includes("(") && !l.split("(")[0].includes(" "))
return 'cpp_' + l;
return l;
}
function pythonRewritter(l) {
l = l.replaceAll("@", "@python_");
if(l.startsWith('xml')) // db whose name starts with xml is formatted this way
return 'python_' + l;
if(l.includes("(") && !l.split("(")[0].includes(" "))
return 'python_' + l;
return l;
}
rewriteDBSchemes("jni", "java", "cpp", javaRewritter, cppRewritter);
rewriteDBSchemes("cpython", "python", "cpp", pythonRewritter, cppRewritter);