• Stars
    star
    464
  • Rank 94,450 (Top 2 %)
  • Language
    C++
  • License
    MIT License
  • Created over 10 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js bindings for tree-sitter

node tree-sitter

Incremental parsers for node

Installation

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 such as tree-sitter-javascript and tree-sitter-go. You can also develop a new grammar using the Tree-sitter CLI.

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

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,

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 }

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);
  }
});

More Repositories

1

tree-sitter

An incremental parsing system for programming tools
Rust
18,352
star
2

py-tree-sitter

Python bindings to the Tree-sitter parsing library
C
825
star
3

tree-sitter-rust

Rust grammar for tree-sitter
JavaScript
335
star
4

tree-sitter-javascript

Javascript grammar for tree-sitter
JavaScript
272
star
5

tree-sitter-typescript

TypeScript grammar for tree-sitter
JavaScript
272
star
6

tree-sitter-python

Python grammar for tree-sitter
JavaScript
261
star
7

tree-sitter-go

Go grammar for tree-sitter
JavaScript
224
star
8

tree-sitter-cpp

C++ grammar for tree-sitter
JavaScript
198
star
9

tree-sitter-graph

Construct graphs from parsed source code
Rust
196
star
10

tree-sitter-c-sharp

C# Grammar for tree-sitter
JavaScript
190
star
11

tree-sitter-c

C grammar for tree-sitter
JavaScript
175
star
12

tree-sitter-haskell

Haskell grammar for tree-sitter.
C
155
star
13

tree-sitter-scala

Scala grammar for tree-sitter
JavaScript
153
star
14

haskell-tree-sitter

Haskell bindings for tree-sitter
Haskell
152
star
15

tree-sitter-bash

Bash grammar for tree-sitter
JavaScript
152
star
16

tree-sitter-ruby

Ruby grammar for tree-sitter
JavaScript
137
star
17

tree-sitter-verilog

SystemVerilog grammar for tree-sitter
JavaScript
91
star
18

tree-sitter-java

Java grammar for tree-sitter
JavaScript
86
star
19

tree-sitter-json

JSON grammar for tree-sitter
Makefile
86
star
20

tree-sitter-php

PHP grammar for tree-sitter
JavaScript
86
star
21

tree-sitter-html

HTML grammar for Tree-sitter
C++
82
star
22

tree-sitter-julia

Julia grammar for Tree-sitter
JavaScript
78
star
23

tree-sitter-ocaml

OCaml grammar for tree-sitter
JavaScript
74
star
24

tree-sitter-css

CSS grammar for Tree-sitter
JavaScript
66
star
25

ruby-tree-sitter.old

Ruby bindings to tree-sitter
C
60
star
26

tree-sitter-swift

Swift grammar for tree-sitter
JavaScript
56
star
27

tree-sitter-regex

Tree-sitter parser for regular expressions
JavaScript
47
star
28

tree-sitter-cli

CLI tool for creating and testing tree-sitter parsers
JavaScript
43
star
29

tree-sitter-embedded-template

Tree-sitter grammar for embedded template languages like ERB, EJS
C
34
star
30

rust-tree-sitter

Rust bindings to Tree-sitter
Rust
30
star
31

tree-sitter-agda

Agda grammar for tree-sitter
Yacc
29
star
32

tree-sitter-jsdoc

JSDoc grammar for Tree-sitter
Rust
20
star
33

tree-sitter-ql

tree-sitter grammar for the CodeQL language
JavaScript
17
star
34

tree-sitter.github.io

Source HTML for the Tree-sitter organization site
JavaScript
10
star
35

highlight-schema

Schema for syntax highlighting property sheets
JavaScript
7
star
36

csharp-tree-sitter

C# bindings to the Tree-sitter parsing library
C#
6
star
37

tree-sitter-tsq

tree-sitter grammar for the tree-sitter query language
JavaScript
6
star
38

afl-tree-sitter

AFL test harness for tree-sitter runtime and parsers
C
5
star
39

tree-sitter-fluent

JavaScript
4
star
40

tree-sitter-razor

(WIP) C# Razor grammar for tree-sitter
C
4
star
41

tree-sitter-ql-dbscheme

tree-sitter support for `.dbscheme` files (as used in CodeQL).
JavaScript
2
star
42

.github

Tree-sitter organization info
2
star