tree-sitter
Incremental parsers for node
Last updated a day ago by maxbrunsfeld .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install tree-sitter 
SYNC missed versions from official npm registry.

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

Current Tags

  • 0.17.0                                ...           latest (a day ago)

180 Versions

  • 0.17.0                                ...           a day ago
  • 0.16.2                                ...           a month ago
  • 0.16.1                                ...           6 months ago
  • 0.16.0                                ...           9 months ago
  • 0.15.15                                ...           10 months ago
  • 0.15.14                                ...           a year ago
  • 0.15.13                                ...           a year ago
  • 0.15.12                                ...           a year ago
  • 0.15.11                                ...           a year ago
  • 0.15.10                                ...           a year ago
  • 0.15.9                                ...           a year ago
  • 0.15.8                                ...           a year ago
  • 0.15.7                                ...           a year ago
  • 0.15.6                                ...           a year ago
  • 0.15.5                                ...           a year ago
  • 0.15.4                                ...           a year ago
  • 0.15.3                                ...           a year ago
  • 0.15.2                                ...           a year ago
  • 0.15.1                                ...           a year ago
  • 0.15.0                                ...           a year ago
  • 0.15.0-1                                ...           a year ago
  • 0.15.0-0                                ...           a year ago
  • 0.14.0                                ...           a year ago
  • 0.13.23                                ...           2 years ago
  • 0.13.22                                ...           2 years ago
  • 0.13.21                                ...           2 years ago
  • 0.13.20                                ...           2 years ago
  • 0.13.19                                ...           2 years ago
  • 0.13.18                                ...           2 years ago
  • 0.13.17                                ...           2 years ago
  • 0.13.16                                ...           2 years ago
  • 0.13.15                                ...           2 years ago
  • 0.13.14                                ...           2 years ago
  • 0.13.13                                ...           2 years ago
  • 0.13.12                                ...           2 years ago
  • 0.13.11                                ...           2 years ago
  • 0.13.10                                ...           2 years ago
  • 0.13.9                                ...           2 years ago
  • 0.13.8                                ...           2 years ago
  • 0.13.7                                ...           2 years ago
  • 0.13.6                                ...           2 years ago
  • 0.13.5                                ...           2 years ago
  • 0.13.4                                ...           2 years ago
  • 0.13.3                                ...           2 years ago
  • 0.13.2                                ...           2 years ago
  • 0.13.2-1                                ...           2 years ago
  • 0.13.2-0                                ...           2 years ago
  • 0.13.1                                ...           2 years ago
  • 0.13.1-1                                ...           2 years ago
  • 0.13.0                                ...           2 years ago
  • 0.12.20                                ...           2 years ago
  • 0.12.20-1                                ...           2 years ago
  • 0.12.20-0                                ...           2 years ago
  • 0.12.19                                ...           2 years ago
  • 0.12.18                                ...           2 years ago
  • 0.12.17                                ...           2 years ago
  • 0.12.16                                ...           2 years ago
  • 0.12.15                                ...           2 years ago
  • 0.12.14                                ...           2 years ago
  • 0.12.14-0                                ...           2 years ago
  • 0.12.13                                ...           2 years ago
  • 0.12.12                                ...           2 years ago
  • 0.12.11                                ...           2 years ago
  • 0.12.10                                ...           2 years ago
  • 0.12.9                                ...           2 years ago
  • 0.12.8                                ...           2 years ago
  • 0.12.8-1                                ...           2 years ago
  • 0.12.8-0                                ...           2 years ago
  • 0.12.7                                ...           2 years ago
  • 0.12.6                                ...           2 years ago
  • 0.12.5                                ...           2 years ago
  • 0.12.4                                ...           2 years ago
  • 0.12.3                                ...           2 years ago
  • 0.12.2                                ...           2 years ago
  • 0.12.1                                ...           2 years ago
  • 0.12.1-2                                ...           2 years ago
  • 0.12.1-1                                ...           2 years ago
  • 0.12.1-0                                ...           2 years ago
  • 0.12.0                                ...           2 years ago
  • 0.11.2                                ...           2 years ago
  • 0.11.1                                ...           2 years ago
  • 0.11.0                                ...           2 years ago
  • 0.10.0                                ...           3 years ago
  • 0.9.2                                ...           3 years ago
  • 0.9.1                                ...           3 years ago
  • 0.9.0                                ...           3 years ago
  • 0.8.8                                ...           3 years ago
  • 0.8.7                                ...           3 years ago
  • 0.8.6                                ...           3 years ago
  • 0.8.5                                ...           3 years ago
  • 0.8.4                                ...           3 years ago
  • 0.8.3                                ...           3 years ago
  • 0.8.2                                ...           3 years ago
  • 0.8.1                                ...           3 years ago
  • 0.8.0                                ...           3 years ago
  • 0.7.5                                ...           3 years ago
  • 0.7.4                                ...           3 years ago
  • 0.7.3                                ...           3 years ago
  • 0.7.2                                ...           3 years ago
  • 0.7.1                                ...           3 years ago
  • 0.7.0                                ...           3 years ago
  • 0.6.7                                ...           3 years ago
  • 0.6.6                                ...           3 years ago
  • 0.6.6-0                                ...           3 years ago
  • 0.6.4                                ...           3 years ago
  • 0.6.3                                ...           3 years ago
  • 0.6.2                                ...           3 years ago
  • 0.6.1                                ...           3 years ago
  • 0.6.0                                ...           3 years ago
  • 0.5.4-1                                ...           3 years ago
  • 0.5.4-0                                ...           3 years ago
  • 0.5.3                                ...           3 years ago
  • 0.5.2                                ...           3 years ago
  • 0.5.1                                ...           3 years ago
  • 0.5.1-0                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.4.6                                ...           4 years ago
  • 0.4.5                                ...           4 years ago
  • 0.4.4                                ...           4 years ago
  • 0.4.3                                ...           4 years ago
  • 0.4.2                                ...           4 years ago
  • 0.4.1                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.2-5                                ...           4 years ago
  • 0.3.2-3                                ...           4 years ago
  • 0.3.2-2                                ...           4 years ago
  • 0.3.2-1                                ...           4 years ago
  • 0.3.2-0                                ...           4 years ago
  • 0.3.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.4                                ...           4 years ago
  • 0.1.3                                ...           4 years ago
  • 0.1.2                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
  • 0.0.44                                ...           4 years ago
  • 0.0.43                                ...           5 years ago
  • 0.0.42                                ...           5 years ago
  • 0.0.41                                ...           5 years ago
  • 0.0.40                                ...           5 years ago
  • 0.0.39                                ...           5 years ago
  • 0.0.38                                ...           5 years ago
  • 0.0.37                                ...           5 years ago
  • 0.0.36                                ...           5 years ago
  • 0.0.35                                ...           5 years ago
  • 0.0.34                                ...           5 years ago
  • 0.0.33                                ...           5 years ago
  • 0.0.32                                ...           5 years ago
  • 0.0.31                                ...           5 years ago
  • 0.0.29                                ...           5 years ago
  • 0.0.28                                ...           5 years ago
  • 0.0.27                                ...           5 years ago
  • 0.0.26                                ...           5 years ago
  • 0.0.25                                ...           5 years ago
  • 0.0.24                                ...           5 years ago
  • 0.0.23                                ...           6 years ago
  • 0.0.22                                ...           6 years ago
  • 0.0.21                                ...           6 years ago
  • 0.0.20                                ...           6 years ago
  • 0.0.19                                ...           6 years ago
  • 0.0.18                                ...           6 years ago
  • 0.0.17                                ...           6 years ago
  • 0.0.16                                ...           6 years ago
  • 0.0.15                                ...           6 years ago
  • 0.0.14                                ...           6 years ago
  • 0.0.13                                ...           6 years ago
  • 0.0.12                                ...           6 years ago
  • 0.0.11                                ...           6 years ago
  • 0.0.10                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
  • 0.0.0                                ...           6 years ago

Copyright 2014 - 2016 © taobao.org |