Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tree-sitter/node-tree-sitter
Node.js bindings for tree-sitter
https://github.com/tree-sitter/node-tree-sitter
binding javascript nodejs tree-sitter
Last synced: about 1 month ago
JSON representation
Node.js bindings for tree-sitter
- Host: GitHub
- URL: https://github.com/tree-sitter/node-tree-sitter
- Owner: tree-sitter
- License: mit
- Created: 2014-03-06T17:04:37.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-03-28T21:29:20.000Z (8 months ago)
- Last Synced: 2024-04-14T14:58:30.736Z (7 months ago)
- Topics: binding, javascript, nodejs, tree-sitter
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/tree-sitter
- Size: 563 KB
- Stars: 542
- Watchers: 18
- Forks: 101
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-tree-sitter - JavaScript / Node
README
node tree-sitter
================Incremental parsers for node
### Installation
```sh
npm install tree-sitter
```### Usage
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).
Once you've got your grammar, create a parser with that grammar.
```javascript
const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');const parser = new Parser();
parser.setLanguage(JavaScript);
```Then you can parse some source code,
```javascript
const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);
```and inspect the syntax tree.
```javascript
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 }
```If your source code *changes*, you can update the syntax tree. This will take less time than the first parse.
```javascript
// 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:
```javascript
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);
}
});
```