vscode-omega
aien-dev/vscode-omega
Brought by drakestapleton
vscode-omega / test / agree.js
| 1 | #!/usr/bin/env node |
| 2 | 'use strict'; |
| 3 | // Token-class agreement between this extension's TextMate grammar and aien-dev/tree-sitter-omega's |
| 4 | // highlight query, over a directory of .om files (CI: every *.om in aien-dev/omega-mv at omega.ref). |
| 5 | // |
| 6 | // Usage: node test/agree.js --ts <tree-sitter-omega dir> --src <dir of .om> [--files <list>] |
| 7 | // [--grammar <tmLanguage.json>] [--min <percent>] |
| 8 | // --files: a file of paths relative to --src, one per line; only those files are compared |
| 9 | // (CI passes the files omc accepts). Default: every *.om under --src. |
| 10 | // stdout: one line per disagreeing token <path>\t<row>:<col>\t<text>\tts=<class>\ttm=<class> |
| 11 | // then AGREE <agreeing>/<compared> <percent>% and FILES <n> |
| 12 | // Exit: 0 when the percentage is at least --min (default 100), 1 below it, 2 on a usage or tool error. |
| 13 | // |
| 14 | // Every node tree-sitter captures is compared, with tree-sitter's winning capture (the later |
| 15 | // pattern wins) and the TextMate scope at the node's first character both mapped to one class. |
| 16 | const fs = require('fs'); |
| 17 | const path = require('path'); |
| 18 | const { execFileSync } = require('child_process'); |
| 19 | const vsctm = require('vscode-textmate'); |
| 20 | const oniguruma = require('vscode-oniguruma'); |
| 21 | |
| 22 | function arg(name, dflt) { |
| 23 | const i = process.argv.indexOf(name); |
| 24 | return i > 0 && i + 1 < process.argv.length ? process.argv[i + 1] : dflt; |
| 25 | } |
| 26 | const tsDir = arg('--ts'); |
| 27 | const srcDir = arg('--src'); |
| 28 | const filesList = arg('--files'); |
| 29 | const grammarPath = arg('--grammar', path.join(__dirname, '..', 'syntaxes', 'omega.tmLanguage.json')); |
| 30 | const min = Number(arg('--min', '100')); |
| 31 | if (!tsDir || !srcDir) { |
| 32 | process.stderr.write('usage: agree.js --ts <dir> --src <dir> [--files list] [--grammar g.json] [--min pct]\n'); |
| 33 | process.exit(2); |
| 34 | } |
| 35 | |
| 36 | const TS_CLASS = { |
| 37 | comment: 'comment', |
| 38 | string: 'string', |
| 39 | 'string.escape': 'escape', |
| 40 | number: 'number', |
| 41 | 'number.float': 'number', |
| 42 | keyword: 'keyword', |
| 43 | 'keyword.conditional': 'keyword', |
| 44 | 'keyword.repeat': 'keyword', |
| 45 | 'keyword.return': 'keyword', |
| 46 | 'keyword.directive': 'keyword', |
| 47 | 'keyword.modifier': 'modifier', |
| 48 | 'keyword.operator': 'operator-word', |
| 49 | operator: 'operator', |
| 50 | 'punctuation.bracket': 'punct', |
| 51 | 'type.builtin': 'type', |
| 52 | boolean: 'constant', |
| 53 | 'variable.builtin': 'variable.builtin', |
| 54 | 'function.builtin': 'builtin', |
| 55 | function: 'function', |
| 56 | type: 'name', |
| 57 | 'type.definition': 'name', |
| 58 | constant: 'name', |
| 59 | variable: 'name', |
| 60 | 'variable.parameter': 'name', |
| 61 | 'variable.member': 'name', |
| 62 | 'function.call': 'name', |
| 63 | }; |
| 64 | // The unsized `*` is @type in tree-sitter and storage.modifier.unsized in TextMate. |
| 65 | function tmClass(scopes) { |
| 66 | const s = scopes[scopes.length - 1] || ''; |
| 67 | if (s.startsWith('comment.')) return 'comment'; |
| 68 | if (s.startsWith('constant.character.escape')) return 'escape'; |
| 69 | if (s.startsWith('string.')) return 'string'; |
| 70 | if (s.startsWith('constant.numeric')) return 'number'; |
| 71 | if (s.startsWith('storage.modifier.unsized')) return 'name'; |
| 72 | if (s.startsWith('keyword.operator.word')) return 'operator-word'; |
| 73 | if (s.startsWith('keyword.operator.assignment')) return 'operator'; |
| 74 | if (s.startsWith('keyword.')) return 'keyword'; |
| 75 | if (s.startsWith('storage.modifier')) return 'modifier'; |
| 76 | if (s.startsWith('punctuation.')) return 'punct'; |
| 77 | if (s.startsWith('support.type')) return 'type'; |
| 78 | if (s.startsWith('constant.language')) return 'constant'; |
| 79 | if (s.startsWith('variable.language')) return 'variable.builtin'; |
| 80 | if (s.startsWith('support.function')) return 'builtin'; |
| 81 | if (s.startsWith('entity.name.function')) return 'function'; |
| 82 | if (s.startsWith('entity.name') || s.startsWith('variable.other')) return 'name'; |
| 83 | return 'plain'; |
| 84 | } |
| 85 | |
| 86 | function listFiles(dir) { |
| 87 | const out = []; |
| 88 | const walk = (d) => { |
| 89 | for (const e of fs.readdirSync(d, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) { |
| 90 | if (e.name === '.git') continue; |
| 91 | const p = path.join(d, e.name); |
| 92 | if (e.isDirectory()) walk(p); |
| 93 | else if (e.isFile() && e.name.endsWith('.om')) out.push(p); |
| 94 | } |
| 95 | }; |
| 96 | walk(dir); |
| 97 | return out; |
| 98 | } |
| 99 | |
| 100 | // tree-sitter query -c prints, per file, `pattern: P, capture: C - NAME, start: (R, C), end: (R, C), text: ...`. |
| 101 | function treeSitterCaptures(files) { |
| 102 | const list = path.join(fs.mkdtempSync(path.join(require('os').tmpdir(), 'agree-')), 'paths'); |
| 103 | fs.writeFileSync(list, files.join('\n') + '\n'); |
| 104 | const cli = path.join(tsDir, 'node_modules', '.bin', 'tree-sitter'); |
| 105 | const out = execFileSync( |
| 106 | cli, |
| 107 | ['query', '-p', tsDir, '-c', path.join(tsDir, 'queries', 'highlights.scm'), '--paths', list], |
| 108 | { maxBuffer: 1 << 30, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }, |
| 109 | ); |
| 110 | const byFile = new Map(); |
| 111 | let cur = null; |
| 112 | const re = /^\s+pattern:\s*(\d+), capture: \d+ - ([\w.]+), start: \((\d+), (\d+)\), end: \((\d+), (\d+)\)/; |
| 113 | for (const line of out.split('\n')) { |
| 114 | if (!line) continue; |
| 115 | if (!/^\s/.test(line)) { |
| 116 | cur = new Map(); |
| 117 | byFile.set(line.trim(), cur); |
| 118 | continue; |
| 119 | } |
| 120 | const m = re.exec(line); |
| 121 | if (!m || !cur) continue; |
| 122 | const key = `${m[3]}:${m[4]}:${m[5]}:${m[6]}`; |
| 123 | const prev = cur.get(key); |
| 124 | if (!prev || +m[1] >= prev.pattern) cur.set(key, { pattern: +m[1], name: m[2], row: +m[3], col: +m[4] }); |
| 125 | } |
| 126 | return byFile; |
| 127 | } |
| 128 | |
| 129 | async function main() { |
| 130 | const wasm = fs.readFileSync(require.resolve('vscode-oniguruma/release/onig.wasm')).buffer; |
| 131 | await oniguruma.loadWASM(wasm); |
| 132 | const registry = new vsctm.Registry({ |
| 133 | onigLib: Promise.resolve({ |
| 134 | createOnigScanner: (p) => new oniguruma.OnigScanner(p), |
| 135 | createOnigString: (s) => new oniguruma.OnigString(s), |
| 136 | }), |
| 137 | loadGrammar: async () => vsctm.parseRawGrammar(fs.readFileSync(grammarPath, 'utf8'), grammarPath), |
| 138 | }); |
| 139 | const grammar = await registry.loadGrammar('source.omega'); |
| 140 | const files = filesList |
| 141 | ? fs.readFileSync(filesList, 'utf8').split('\n').filter(Boolean).map((p) => path.join(srcDir, p)) |
| 142 | : listFiles(srcDir); |
| 143 | if (files.length === 0) { |
| 144 | process.stderr.write(`agree.js: no .om files under ${srcDir}\n`); |
| 145 | process.exit(2); |
| 146 | } |
| 147 | const caps = treeSitterCaptures(files); |
| 148 | let compared = 0; |
| 149 | let agree = 0; |
| 150 | for (const f of files) { |
| 151 | const lines = fs.readFileSync(f, 'utf8').split('\n'); |
| 152 | const tm = []; |
| 153 | let state = vsctm.INITIAL; |
| 154 | for (const l of lines) { |
| 155 | const r = grammar.tokenizeLine(l, state); |
| 156 | tm.push(r.tokens); |
| 157 | state = r.ruleStack; |
| 158 | } |
| 159 | for (const c of (caps.get(f) || new Map()).values()) { |
| 160 | const tsc = TS_CLASS[c.name]; |
| 161 | if (!tsc) continue; |
| 162 | // tree-sitter columns count UTF-8 bytes; TextMate token indices count UTF-16 code units. |
| 163 | const line = lines[c.row] || ''; |
| 164 | const col = Buffer.from(line, 'utf8').subarray(0, c.col).toString('utf8').length; |
| 165 | const toks = tm[c.row] || []; |
| 166 | const t = toks.find((k) => k.startIndex <= col && col < k.endIndex); |
| 167 | const tmc = t ? tmClass(t.scopes) : 'plain'; |
| 168 | compared++; |
| 169 | if (tmc === tsc) agree++; |
| 170 | else { |
| 171 | const text = line.slice(col).split(/[ \t]/)[0].slice(0, 24); |
| 172 | process.stdout.write(`${path.relative(srcDir, f)}\t${c.row + 1}:${col + 1}\t${text}\tts=${tsc}\ttm=${tmc}\n`); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | const pct = compared ? (100 * agree) / compared : 0; |
| 177 | process.stdout.write(`AGREE ${agree}/${compared} ${pct.toFixed(3)}%\nFILES ${files.length}\n`); |
| 178 | process.exit(pct >= min ? 0 : 1); |
| 179 | } |
| 180 | |
| 181 | main().catch((e) => { |
| 182 | process.stderr.write(`agree.js: ${e.stack || e}\n`); |
| 183 | process.exit(2); |
| 184 | }); |