Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(async function f() {
await Parser.init()
const parser = new Parser()
const Lang = await Parser.Language.load('tree-sitter-javascript.wasm')
parser.setLanguage(Lang)
// let's parse the code of this file itself:
const tree = parser.parse('export class C { get p() { return 1 } }')
console.log(dump(tree.rootNode).trim())
})()
visitor.options = options
tree.accept(visitor)
ast = visitor.getAst()
}
else if (info.treeSitterParser) {
options.basePath = options.basePath || ''
let parser: Parser = wasmLoaded[options.basePath] as any
if (!parser) {
await Parser.init()
options.debug && console.log('Parser.init()')
parser = new Parser()
const Lang = await Parser.Language.load(pathJoin(options.basePath, info.treeSitterParser))
options.debug && console.log('load')
parser.setLanguage(Lang)
options.debug && console.log('setLanguage')
wasmLoaded[options.basePath] = parser
}
// TODO; don't load again the .wasm if already loaded.
const tree = parser.parse(input)
parseTime = Date.now() - parseT0
postProcessingT0 = Date.now()
options.debug && console.log(tree.rootNode.toString())
const normalizer = new TreeSitterVisitor()
normalizer.options = { ...options, root: tree.rootNode }
ast = normalizer.getAst()
return null;
}
});
}
// block on parser initialization from WASM
await Parser.init(); // TODO: double-check that this doesn't crash, as the tree-sitter extension suggests
// load Rust language module
const absoluteRustPath = path.join(
context.extensionPath,
"parsers",
`${RUST_WASM_MODULE}.wasm`
);
const Rust = await Parser.Language.load(absoluteRustPath); // TODO: cache these parsers between files or init on workspace open
const parser = new Parser();
// set Rust as standard tree-sitter language module
parser.setLanguage(Rust);
// TODO: this should probably be constrained a bit
function getTree(document: any) {
// TODO: investigate getText vs Parser.input()
// parse tree of given document
return parser.parse(document.getText());
}
// register the hover action for Rust files
vscode.languages.registerHoverProvider(RUST_HOVER_SCHEME, {
provideHover(document, { line: row, character: column }, token) {
async init() {
// Parser
await parserPromise;
this.parser = new parser();
let langFile = path.join(__dirname, "../parsers", this.lang + ".wasm");
const langObj = await parser.Language.load(langFile);
this.parser.setLanguage(langObj);
}
}
async function open(editor: vscode.TextEditor) {
const language = languages[editor.document.languageId]
if (language == null) return
if (language.parser == null) {
const absolute = path.join(context.extensionPath, 'parsers', language.module + '.wasm')
const wasm = path.relative(process.cwd(), absolute)
const lang = await Parser.Language.load(wasm)
const parser = new Parser()
parser.setLanguage(lang)
language.parser = parser
}
const t = language.parser.parse(editor.document.getText()) // TODO don't use getText, use Parser.Input
trees[editor.document.uri.toString()] = t
colorUri(editor.document.uri)
}
// NOTE: if you make this an async function, it seems to cause edit anomalies
async (params: InitializeParams): Promise => {
await Parser.init();
const absolute = Path.join(__dirname, "tree-sitter-elm.wasm");
const pathToWasm = Path.relative(process.cwd(), absolute);
connection.console.info(
`Loading Elm tree-sitter syntax from ${pathToWasm}`,
);
const language = await Parser.Language.load(pathToWasm);
const parser = new Parser();
parser.setLanguage(language);
const { Server } = await import("./server");
server = new Server(connection, params, parser);
await server.init();
return server.capabilities;
},
);
export async function initializeParser(): Promise {
await Parser.init()
const parser = new Parser()
/**
* See https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_web#generate-wasm-language-files
*
* To compile:
* yarn add --dev tree-sitter-cli
* npx tree-sitter build-wasm node_modules/tree-sitter-bash
*
* The current files was compiled with:
* "tree-sitter-bash": "^0.16.0",
* "tree-sitter-cli": "^0.15.9"
*/
const lang = await Parser.Language.load(`${__dirname}/../tree-sitter-bash.wasm`)
parser.setLanguage(lang)
return parser
}