diff --git a/packages/adl-vscode/adl.tmlanguage.ts b/packages/adl-vscode/adl.tmlanguage.ts index 34138a0d7..12b8ad50f 100644 --- a/packages/adl-vscode/adl.tmlanguage.ts +++ b/packages/adl-vscode/adl.tmlanguage.ts @@ -1,8 +1,12 @@ // TextMate-based syntax highlighting is implemented in this file. // adl.tmLanguage.json is generated by running this script. -import { writeFileSync } from "fs"; -import { OnigRegExp } from "oniguruma"; +import fs from "fs"; +import { promisify } from "util"; +import { loadWASM, OnigRegExp } from "onigasm"; + +const readFile = promisify(fs.readFile); +const writeFile = promisify(fs.writeFile); const schema = "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json"; @@ -317,10 +321,12 @@ const grammar: Grammar = { }; /** Entry point, write the grammar to disk. */ -function main() { +async function main() { + const onigasm = await readFile("node_modules/onigasm/lib/onigasm.wasm"); + await loadWASM(onigasm.buffer); const filePath = "./dist/adl.tmlanguage.json"; const json = emit(grammar); - writeFileSync(filePath, json); + await writeFile(filePath, json); } /** Emit the grammar to a JSON string matching tmlanguage.json schema. */ @@ -405,10 +411,17 @@ function processGrammar(grammar: Grammar): any { function validateRegexp(regexp: string, node: any, prop: string) { try { - new OnigRegExp(regexp); + new OnigRegExp(regexp).testSync(""); } catch (err) { + let message: string = err.message; + if (/^[0-9,]+/.test(message)) { + // Work around for https://github.com/NeekSandhu/onigasm/issues/26 + const array = new Uint8Array(message.split(",").map((s: string) => Number(s))); + const buffer = Buffer.from(array); + message = buffer.toString("utf-8"); + } console.error(`Error: Bad regex: ${JSON.stringify({ [prop]: regexp })}`); - console.error(`Error: ${err.message}`); + console.error(`Error: ${message}`); console.error(); console.error("Context:"); console.dir(node); @@ -418,4 +431,8 @@ function processGrammar(grammar: Grammar): any { } } -main(); +main() + .then() + .catch((err) => { + console.log(err.stack); + }); diff --git a/packages/adl-vscode/package.json b/packages/adl-vscode/package.json index 363e47b4c..ff853735e 100644 --- a/packages/adl-vscode/package.json +++ b/packages/adl-vscode/package.json @@ -24,8 +24,7 @@ }, "devDependencies": { "@types/node": "14.0.27", - "@types/oniguruma": "~7.0.1", - "oniguruma": "~7.2.1", + "onigasm": "~2.2.5", "typescript": "~4.1.5", "vsce": "^1.85.1" },