Skip to content

Repository files navigation

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.

constParser=require('tree-sitter');constJavaScript=require('tree-sitter-javascript');constparser=newParser();parser.setLanguage(JavaScript);

Then you can parse some source code,

constsourceCode='let x = 1; console.log(x);';consttree=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)))))constcallExpression=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'constnewSourceCode='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},});constnewTree=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:

constsourceLines=['let x = 1;','console.log(x);'];consttree=parser.parse((index,position)=>{letline=sourceLines[position.row];if(line){returnline.slice(position.column);}});

About

Node.js bindings for tree-sitter

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages