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

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

Current Tags

  • 0.22.1                                ...           latest (5 days ago)

195 Versions

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

© 2010 - cnpmjs.org x YWFE | Home | YWFE