tree-sitter/lib/binding_web
2019-05-27 15:10:43 +01:00
..
test Implement Wasm binding for Tree.getChangedRanges() 2019-05-09 23:52:57 -07:00
.gitignore Set up code to publish web bindings to npm 2019-05-07 13:11:04 -07:00
.npmignore Set up code to publish web bindings to npm 2019-05-07 13:11:04 -07:00
binding.c Implement Wasm binding for Tree.getChangedRanges() 2019-05-09 23:52:57 -07:00
binding.js Ensure Point has column typeof number field 2019-05-24 14:34:04 +02:00
check-artifacts-fresh.js Set up code to publish web bindings to npm 2019-05-07 13:11:04 -07:00
exports.json Implement Wasm binding for Tree.getChangedRanges() 2019-05-09 23:52:57 -07:00
imports.js Reorganize language bindings 2019-05-07 10:41:49 -07:00
package.json Reference tree-sitter-web.d.ts in package.json 2019-05-27 15:10:43 +01:00
prefix.js Reorganize language bindings 2019-05-07 10:41:49 -07:00
README.md Set up code to publish web bindings to npm 2019-05-07 13:11:04 -07:00
tree-sitter-web.d.ts Add type definitions 2019-05-27 15:08:29 +01:00

Web Tree-sitter

Build Status

WebAssembly bindings to the Tree-sitter parsing library.

Setup

You can download the the tree-sitter.js and tree-sitter.wasm files from the latest GitHub release and load them using a standalone script:

<script src="/the/path/to/tree-sitter.js"/>

<script>
  const Parser = window.TreeSitter;
  Parser.init().then(() => { /* the library is ready */ });
</script>

You can also install the web-tree-sitter module from NPM and load it using a system like Webpack:

const Parser = require('web-tree-sitter');
Parser.init().then(() => { /* the library is ready */ });

Basic Usage

First, create a parser:

const parser = new Parser;

Then assign a language to the parser. Tree-sitter languages are packaged as individual .wasm files:

const JavaScript = await Parser.Language.load('/path/to/tree-sitter-javascript.wasm');
parser.setLanguage(JavaScript);

Now you can parse source code:

const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);

and inspect the syntax tree.

console.log(tree.rootNode.toString());

// (program
//   (lexical_declaration
//     (variable_declarator (identifier) (number)))
//   (expression_statement
//     (call_expression
//       (member_expression (identifier) (property_identifier))
//       (arguments (identifier)))))

const callExpression = tree.rootNode.child(1).firstChild;
console.log(callExpression);

// { type: 'call_expression',
//   startPosition: {row: 0, column: 16},
//   endPosition: {row: 0, column: 30},
//   startIndex: 0,
//   endIndex: 30 }

Editing

If your source code changes, you can update the syntax tree. This will take less time than the first parse.

// Replace 'let' with 'const'
const newSourceCode = 'const x = 1; console.log(x);';

tree.edit({
  startIndex: 0,
  oldEndIndex: 3,
  newEndIndex: 5,
  startPosition: {row: 0, column: 0},
  oldEndPosition: {row: 0, column: 3},
  newEndPosition: {row: 0, column: 5},
});

const newTree = parser.parse(newSourceCode, tree);

Parsing Text From a Custom Data Structure

If your text is stored in a data structure other than a single string, you can parse it by supplying a callback to parse instead of a string:

const sourceLines = [
  'let x = 1;',
  'console.log(x);'
];

const tree = parser.parse((index, position) => {
  let line = sourceLines[position.row];
  if (line) return line.slice(position.column);
});