Skip to content

Commit c7b9cd4

Browse files
committed
docs(readme): improve intro, add badges
1 parent 2d75271 commit c7b9cd4

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

README.md

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1-
node tree-sitter
2-
================
1+
# Node Tree-sitter
32

4-
Incremental parsers for node
3+
[![CI][ci]](https://github.com/tree-sitter/node-tree-sitter/actions/workflows/ci.yml)
4+
[![npm][npm]](https://npmjs.com/package/tree-sitter)
5+
[![docs][docs]](https://tree-sitter.github.io/node-tree-sitter)
56

6-
### Installation
7+
This module provides Node.js bindings to the [tree-sitter] parsing library.
8+
9+
## Installation
710

811
```sh
912
npm install tree-sitter
1013
```
1114

12-
### Usage
15+
## Basic Usage
16+
17+
### Prerequisites
18+
19+
First, you'll need a Tree-sitter grammar for the language you want to parse. There are many [existing grammars][grammars],
20+
such as [tree-sitter-javascript][javascript]. These grammars can typically be installed with a package manager like NPM,
21+
so long as the author has published them.
22+
23+
```sh
24+
npm install tree-sitter-javascript
25+
```
26+
27+
You can also develop a new grammar by using the [Tree-sitter CLI][cli] and following the [docs][docs].
1328

14-
First, you'll need a Tree-sitter grammar for the language you want to parse. There are many [existing grammars](https://github.com/tree-sitter) such as [tree-sitter-javascript](http://github.com/tree-sitter/tree-sitter-javascript) and [tree-sitter-go](http://github.com/tree-sitter/tree-sitter-go). You can also develop a new grammar using the [Tree-sitter CLI](https://github.com/tree-sitter/tree-sitter/tree/master/cli).
29+
### Parsing Source Code
1530

1631
Once you've got your grammar, create a parser with that grammar.
1732

@@ -46,18 +61,25 @@ console.log(tree.rootNode.toString());
4661
const callExpression = tree.rootNode.child(1).firstChild;
4762
console.log(callExpression);
4863

49-
// { type: 'call_expression',
64+
// {
65+
// type: 'call_expression',
5066
// startPosition: {row: 0, column: 16},
5167
// endPosition: {row: 0, column: 30},
5268
// startIndex: 0,
53-
// endIndex: 30 }
69+
// endIndex: 30
70+
// }
5471
```
5572

56-
If your source code *changes*, you can update the syntax tree. This will take less time than the first parse.
73+
If your source code *changes*, you can update the syntax tree. This is much faster than the first parse.
5774

5875
```javascript
59-
// Replace 'let' with 'const'
76+
// In the code, we replaced 'let' with 'const'.
77+
// So, we set our old end index to 3, and our new end index to 5.
78+
// Note that the end index is exclusive.
6079
const newSourceCode = 'const x = 1; console.log(x);';
80+
// ^ ^
81+
// indices: 3 5
82+
// points: (0,3) (0,5)
6183

6284
tree.edit({
6385
startIndex: 0,
@@ -73,7 +95,8 @@ const newTree = parser.parse(newSourceCode, tree);
7395

7496
### Parsing Text From a Custom Data Structure
7597

76-
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:
98+
If your text is stored in a data structure other than a single string, such as a rope or array, you can parse it by supplying
99+
a callback to `parse` instead of a string:
77100

78101
```javascript
79102
const sourceLines = [
@@ -88,3 +111,17 @@ const tree = parser.parse((index, position) => {
88111
}
89112
});
90113
```
114+
115+
### Further Reading
116+
117+
It's recommended that you read the [Tree-sitter documentation][usage docs] on using parsers to get a higher-level overview
118+
of the API. Once you're comfortable with the basics, you can explore the [full API documentation](https://tree-sitter.github.io/node-tree-sitter),
119+
which should map closely to the C API, though there are some differences.
120+
121+
[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/node-tree-sitter/ci.yml?logo=github&label=CI
122+
[cli]: https://github.com/tree-sitter/tree-sitter/tree/master/cli
123+
[docs]: https://tree-sitter.github.io/tree-sitter/creating-parsers
124+
[npm]: https://img.shields.io/npm/v/tree-sitter?logo=npm
125+
[grammars]: https://github.com/tree-sitter/tree-sitter/wiki/List-of-parsers
126+
[javascript]: http://github.com/tree-sitter/tree-sitter-javascript
127+
[usage docs]: https://tree-sitter.github.io/tree-sitter/using-parsers

0 commit comments

Comments
 (0)