-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (71 loc) · 2.5 KB
/
index.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
const fs = require('fs');
const path = require('path');
const vsctm = require('vscode-textmate');
const oniguruma = require('vscode-oniguruma');
/**
* Utility to read a file as a promise
*/
function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (error, data) => error ? reject(error) : resolve(data));
})
}
const wasmBin = fs.readFileSync(path.join(__dirname, './node_modules/vscode-oniguruma/release/onig.wasm')).buffer;
const vscodeOnigurumaLib = oniguruma.loadWASM(wasmBin).then(() => {
return {
createOnigScanner(patterns) {
return new oniguruma.OnigScanner(patterns);
},
createOnigString(s) {
return new oniguruma.OnigString(s);
}
};
});
// Create a registry that can create a grammar from a scope name.
const registry = new vsctm.Registry({
onigLib: vscodeOnigurumaLib,
loadGrammar: (scopeName) => {
if (scopeName === 'source.c') {
// https://github.com/textmate/javascript.tmbundle/blob/master/Syntaxes/JavaScript.plist
return readFile('./extensions/c.tmLanguage.json').then(data => vsctm.parseRawGrammar(data.toString(), "c.json"))
}
console.log(`Unknown scope name: ${scopeName}`);
return null;
}
});
// Load the JavaScript grammar and any other grammars included by it async.
registry.loadGrammar('source.c').then(grammar => {
const text = `#include <stdio.h>
int main(){
printf("Hello, World!");
return 0;
}
`.split("\n");
let ruleStack = vsctm.INITIAL;
let results = {
path: '',
name: 'helloworld.c',
elements: []
};
for (let i = 0; i < text.length; i++) {
const line = text[i];
const lineTokens = grammar.tokenizeLine(line, ruleStack);
for (let j = 0; j < lineTokens.tokens.length; j++) {
const token = lineTokens.tokens[j];
let value = line.substring(token.startIndex, token.endIndex);
let lineNum = i + 1;
let pos = `${lineNum}:${token.startIndex}-${token.endIndex}`;
results.elements.push({
line_num: lineNum,
start_index: token.startIndex,
pos: pos,
end_index: token.endIndex,
value: value,
scopes: token.scopes
})
console.log(`| ${pos} | ${value} | ${token.scopes.join(",")} |`)
}
ruleStack = lineTokens.ruleStack;
}
fs.writeFileSync("scie.json", JSON.stringify(results));
});