-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from cucumber/restore-wasm
Restore WASM support
- Loading branch information
Showing
12 changed files
with
182 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env node | ||
|
||
import { exec } from 'child_process' | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
const languages = { | ||
typescript: ['typescript', 'typescript'], | ||
java: ['java'], | ||
} | ||
|
||
// Build wasm parsers for supported languages | ||
const parsersDir = 'dist' | ||
if (!fs.existsSync(parsersDir)) { | ||
fs.mkdirSync(parsersDir) | ||
} | ||
for (const [lang, names] of Object.entries(languages)) { | ||
const [moduleName, ...variant] = names | ||
const module = path.join('node_modules/tree-sitter-' + moduleName, ...variant) | ||
const output = 'tree-sitter-' + names[names.length - 1] + '.wasm' | ||
|
||
let command | ||
if (process.env.CI) { | ||
console.log(`Compiling ${lang} parser`) | ||
command = `node_modules/.bin/tree-sitter build-wasm ${module}` | ||
} else { | ||
console.log(`Compiling ${lang} parser inside docker`) | ||
// https://github.com/tree-sitter/tree-sitter/issues/1560 | ||
command = `node_modules/.bin/tree-sitter build-wasm ${module} --docker` | ||
} | ||
exec(command, (err) => { | ||
if (err) { | ||
console.error('Failed to build wasm for ' + lang + ': ' + err.message) | ||
process.exit(1) | ||
} else { | ||
const newPath = `${parsersDir}/${lang}.wasm` | ||
fs.rename(output, newPath, (err) => { | ||
if (err) { | ||
console.error('Failed to copy built parser: ' + err.message) | ||
process.exit(1) | ||
} else console.log(`Successfully compiled ${newPath}`) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import NodeParser from 'tree-sitter' | ||
import Parser from 'web-tree-sitter' | ||
|
||
import { LanguageName, LanguageNames, ParserAdapter } from '../tree-sitter/types.js' | ||
|
||
export class WasmParserAdapter implements ParserAdapter { | ||
public parser: NodeParser | ||
private languages: Record<LanguageName, Parser.Language> | ||
|
||
async init(wasmBaseUrl: string) { | ||
await Parser.init() | ||
// @ts-ignore | ||
this.parser = new Parser() | ||
|
||
const languages = await Promise.all( | ||
LanguageNames.map((languageName) => { | ||
const wasmUrl = `${wasmBaseUrl}/${languageName}.wasm` | ||
return Parser.Language.load(wasmUrl) | ||
}) | ||
) | ||
// @ts-ignore | ||
this.languages = Object.fromEntries( | ||
LanguageNames.map((languageName, i) => [languageName as LanguageName, languages[i]]) | ||
) | ||
} | ||
|
||
query(source: string): NodeParser.Query { | ||
return this.parser.getLanguage().query(source) | ||
} | ||
|
||
setLanguage(language: LanguageName): void { | ||
this.parser.setLanguage(this.languages[language]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './ExpressionBuilder.js' | ||
export * from './NodeParserAdapter.js' | ||
export * from './types.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters