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())
})()
(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())
})()
(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())
})()
}
return vscode.window.createTextEditorDecorationType(options)
}
// Load styles from the current active theme
async function loadStyles() {
await scopes.load()
// Clear old styles
for (const style of decorationCache.values()) {
style.dispose()
}
decorationCache.clear()
}
// For some reason this crashes if we put it inside activate
const initParser = Parser.init() // TODO this isn't a field, suppress package member coloring like Go
// Called when the extension is first activated by user opening a file with the appropriate language
export async function activate(context: vscode.ExtensionContext) {
console.log("Activating tree-sitter...")
// Parse of all visible documents
const trees: {[uri: string]: Parser.Tree} = {}
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
parseTime = Date.now() - parseT0
postProcessingT0 = Date.now()
const visitor = new Visitor()
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()
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()
const visitor = new Visitor()
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 }
import * as vscode from 'vscode';
import * as parser from 'web-tree-sitter';
import * as jsonc from 'jsonc-parser';
import * as fs from 'fs';
import * as path from 'path';
import { clearTimeout } from 'timers';
// Grammar class
const parserPromise = parser.init();
class Grammar {
// Parser
readonly lang: string;
parser: parser;
// Grammar
readonly simpleTerms: { [sym: string]: string } = {};
readonly complexTerms: string[] = [];
readonly complexScopes: { [sym: string]: string } = {};
readonly complexDepth: number;
readonly complexOrder: boolean;
constructor(lang: string) {
// Grammar
this.lang = lang;
const grammarFile = __dirname + "/../grammars/" + lang + ".json";
const grammarJson = jsonc.parse(fs.readFileSync(grammarFile).toString());
lspClient.code2ProtocolConverter.asTextDocumentPositionParams(
document,
position.translate(0, -1)
),
token
);
lspClient.protocol2CodeConverter.asHover(lspResponse);
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()
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) {