-
Notifications
You must be signed in to change notification settings - Fork 17
/
extract-keywords.js
64 lines (56 loc) · 1.65 KB
/
extract-keywords.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
var fs = require('fs');
const yaml = require('js-yaml');
function yamlValue(array) {
return '\b(' + array.join('|') + ')\b';
}
function yaml2Array(value) {
return value.replace('\\b(',"").replace(')\\b','').split("|")
}
const chunk = (arr, size) =>
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
arr.slice(i * size, i * size + size)
);
let ballerinaYAML = yaml.safeLoad(fs.readFileSync("syntaxes/ballerina.YAML-tmLanguage").toString());
const control = yaml2Array(ballerinaYAML.repository.keywords.patterns[0].match);
let types = [];
ballerinaYAML.repository.types.patterns.forEach((typeList)=>{
types = types.concat(yaml2Array(typeList.match));
});
console.log(types);
fs.readFile(process.argv[2], 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
const regex = /=.*".*"/gm;
let config = {
"keywords": {
"patterns": [
{
"name": "keyword.control.ballerina",
"match": yamlValue(control)
}
]
}
}
let m;
let keys = [];
while ((m = regex.exec(data)) !== null) {
keys.push(m[0]);
}
keys = keys.map((item) => {
return item.replace("= \"", "").replace("\"", "");
})
keys = keys.filter((item) => {
return !control.includes(item);
})
keys = keys.filter((item) => {
return !types.includes(item);
})
chunk(keys, 15).forEach((group)=>{
config.keywords.patterns.push({
"name": "keyword.other.ballerina",
"match": yamlValue(group)
});
});
console.log(yaml.safeDump(config));
});