• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..21-Nov-2021-

test/H03-May-2022-

.gitignoreH A D21-Nov-202179 76

.npmignoreH A D21-Nov-202169 65

README.mdH A D21-Nov-20214 KiB145103

binding.cH A D21-Nov-202121.6 KiB691566

exports.jsonH A D21-Nov-20213.4 KiB107104

package.jsonH A D21-Nov-2021820 3534

tree-sitter-web.d.tsH A D21-Nov-20215.2 KiB182154

README.md

1Web Tree-sitter
2===============
3
4[![Build Status](https://travis-ci.org/tree-sitter/tree-sitter.svg?branch=master)](https://travis-ci.org/tree-sitter/tree-sitter)
5
6WebAssembly bindings to the [Tree-sitter](https://github.com/tree-sitter/tree-sitter) parsing library.
7
8### Setup
9
10You can download the the `tree-sitter.js` and `tree-sitter.wasm` files from [the latest GitHub release](https://github.com/tree-sitter/tree-sitter/releases/latest) and load them using a standalone script:
11
12```html
13<script src="/the/path/to/tree-sitter.js"/>
14
15<script>
16  const Parser = window.TreeSitter;
17  Parser.init().then(() => { /* the library is ready */ });
18</script>
19```
20
21You can also install [the `web-tree-sitter` module](https://www.npmjs.com/package/web-tree-sitter) from NPM and load it using a system like Webpack:
22
23```js
24const Parser = require('web-tree-sitter');
25Parser.init().then(() => { /* the library is ready */ });
26```
27
28### Basic Usage
29
30First, create a parser:
31
32```js
33const parser = new Parser;
34```
35
36Then assign a language to the parser. Tree-sitter languages are packaged as individual `.wasm` files (more on this below):
37
38```js
39const JavaScript = await Parser.Language.load('/path/to/tree-sitter-javascript.wasm');
40parser.setLanguage(JavaScript);
41```
42
43Now you can parse source code:
44
45```js
46const sourceCode = 'let x = 1; console.log(x);';
47const tree = parser.parse(sourceCode);
48```
49
50and inspect the syntax tree.
51
52```javascript
53console.log(tree.rootNode.toString());
54
55// (program
56//   (lexical_declaration
57//     (variable_declarator (identifier) (number)))
58//   (expression_statement
59//     (call_expression
60//       (member_expression (identifier) (property_identifier))
61//       (arguments (identifier)))))
62
63const callExpression = tree.rootNode.child(1).firstChild;
64console.log(callExpression);
65
66// { type: 'call_expression',
67//   startPosition: {row: 0, column: 16},
68//   endPosition: {row: 0, column: 30},
69//   startIndex: 0,
70//   endIndex: 30 }
71```
72
73### Editing
74
75If your source code *changes*, you can update the syntax tree. This will take less time than the first parse.
76
77```javascript
78// Replace 'let' with 'const'
79const newSourceCode = 'const x = 1; console.log(x);';
80
81tree.edit({
82  startIndex: 0,
83  oldEndIndex: 3,
84  newEndIndex: 5,
85  startPosition: {row: 0, column: 0},
86  oldEndPosition: {row: 0, column: 3},
87  newEndPosition: {row: 0, column: 5},
88});
89
90const newTree = parser.parse(newSourceCode, tree);
91```
92
93### Parsing Text From a Custom Data Structure
94
95If 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:
96
97```javascript
98const sourceLines = [
99  'let x = 1;',
100  'console.log(x);'
101];
102
103const tree = parser.parse((index, position) => {
104  let line = sourceLines[position.row];
105  if (line) return line.slice(position.column);
106});
107```
108
109### Generate .wasm language files
110
111The following example shows how to generate `.wasm` file for tree-sitter JavaScript grammar.
112
113**IMPORTANT**: [emscripten](https://emscripten.org/docs/getting_started/downloads.html) or [docker](https://www.docker.com/) need to be installed.
114
115First install `tree-sitter-cli` and the tree-sitter language for which to generate `.wasm` (`tree-sitter-javascript` in this example):
116
117```sh
118npm install --save-dev tree-sitter-cli tree-sitter-javascript
119```
120
121Then just use tree-sitter cli tool to generate the `.wasm`.
122
123```sh
124npx tree-sitter build-wasm node_modules/tree-sitter-javascript
125```
126
127If everything is fine, file `tree-sitter-javascript.wasm` should be generated in current directory.
128
129#### Running .wasm in Node.js
130
131Notice that executing `.wasm` files in node.js is considerably slower than running [node.js bindings](https://github.com/tree-sitter/node-tree-sitter). However could be useful for testing purposes:
132
133```javascript
134const Parser = require('web-tree-sitter');
135
136(async () => {
137  await Parser.init();
138  const parser = new Parser();
139  const Lang = await Parser.Language.load('tree-sitter-javascript.wasm');
140  parser.setLanguage(Lang);
141  const tree = parser.parse('let x = 1;');
142  console.log(tree.rootNode.toString());
143})();
144```
145