How to use the web-tree-sitter.Language function in web-tree-sitter

To help you get started, we’ve selected a few web-tree-sitter examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cancerberoSgx / univac / test / probes / treeSitter.ts View on Github external
(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())

})()
github cancerberoSgx / univac / univac / src / parseAst.ts View on Github external
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()
github boilerplatter / learn / src / extension.ts View on Github external
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) {
github EvgeniyPeshkov / syntax-highlighter / src / extension.ts View on Github external
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);
    }
}
github georgewfraser / vscode-tree-sitter / src / extension.ts View on Github external
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
github elm-tooling / elm-language-server / src / index.ts View on Github external
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;
  },
);
github mads-hartmann / bash-language-server / server / src / parser.ts View on Github external
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
}

web-tree-sitter

Tree-sitter bindings for the web

MIT
Latest version published 11 days ago

Package Health Score

95 / 100
Full package analysis