All Projects → tree-sitter → Node Tree Sitter

tree-sitter / Node Tree Sitter

Licence: mit
Node.js bindings for tree-sitter

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Node Tree Sitter

Proteus
Proteus : A JSON based LayoutInflater for Android
Stars: ✭ 1,179 (+585.47%)
Mutual labels:  binding
Nukleardotnet
.NET binding for the Nuklear immediate mode GUI
Stars: ✭ 126 (-26.74%)
Mutual labels:  binding
Ginrpc
gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具
Stars: ✭ 157 (-8.72%)
Mutual labels:  binding
Raylib Lua
A simple and easy-to-use Lua library to enjoy videogames programming
Stars: ✭ 80 (-53.49%)
Mutual labels:  binding
Vkbind
Single file Vulkan API loader.
Stars: ✭ 110 (-36.05%)
Mutual labels:  binding
Dukglue
A C++ binding/wrapper library for the Duktape JavaScript interpreter.
Stars: ✭ 132 (-23.26%)
Mutual labels:  binding
Docker Bind
Bind caching DNS server on Debian with wild-card domain support
Stars: ✭ 50 (-70.93%)
Mutual labels:  binding
Mgs Machinery
Unity plugin for binding machinery joint in scene.
Stars: ✭ 164 (-4.65%)
Mutual labels:  binding
Go Sdl2
SDL2 binding for Go
Stars: ✭ 1,667 (+869.19%)
Mutual labels:  binding
Rxrealmdatasources
An easy way to bind an RxRealm observable to a table or collection view
Stars: ✭ 154 (-10.47%)
Mutual labels:  binding
Query State
Application state in query string
Stars: ✭ 88 (-48.84%)
Mutual labels:  binding
Samples
Community driven repository for Dapr samples
Stars: ✭ 104 (-39.53%)
Mutual labels:  binding
Conari
🧬 Platform for unmanaged memory, pe-modules, related PInvoke features, and more for: Libraries, Executable Modules, enjoy using of the unmanaged native C/C++ in .NET world, and other raw binary data …
Stars: ✭ 138 (-19.77%)
Mutual labels:  binding
Expostal
Elixir binding for Libpostal - a library for parsing/normalizing street addresses around the world. Powered by statistical NLP and open geo data.
Stars: ✭ 80 (-53.49%)
Mutual labels:  binding
Pangolin
Python binding of 3D visualization library Pangolin
Stars: ✭ 157 (-8.72%)
Mutual labels:  binding
Slacko
A neat interface for Slack
Stars: ✭ 64 (-62.79%)
Mutual labels:  binding
Tensorflow
Go binding for Tensorflow
Stars: ✭ 131 (-23.84%)
Mutual labels:  binding
Node Postal
NodeJS bindings to libpostal for fast international address parsing/normalization
Stars: ✭ 165 (-4.07%)
Mutual labels:  binding
Rsbind
Tools for building mobile applications in Rust.
Stars: ✭ 160 (-6.98%)
Mutual labels:  binding
Go Sdk
Dapr SDK for go
Stars: ✭ 149 (-13.37%)
Mutual labels:  binding

node tree-sitter

Build Status Build status

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

Asynchronous Parsing

If you have source code stored in a superstring TextBuffer, you can parse that source code on a background thread with a Promise-based interface:

const {TextBuffer} = require('superstring');

async function test() {
  const buffer = new TextBuffer('const x= 1; console.log(x);');
  const newTree = await parser.parseTextBuffer(buffer, oldTree);
}

Using a background thread can introduce a slight delay, so you may want to allow some work to be done on the main thread, in the hopes that parsing will complete so quickly that you won't even need a background thread:

async function test2() {
  const buffer = new TextBuffer('const x= 1; console.log(x);');
  const newTree = await parser.parseTextBuffer(buffer, oldTree, {
    syncOperationCount: 1000
  });
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].