0.21.1 • Published 1 month ago

tree-sitter v0.21.1

Weekly downloads
8,727
License
MIT
Repository
github
Last release
1 month ago

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);
  }
});
@infinitebrahmanuniverse/nolb-tree-@everything-registry/sub-chunk-2969func-static-checkergenezioformat-nixduh-systemrdleaciest-clifoam-language-clientfoam-language-serverinfactorlanguage-4dcataloglanguage-4dlinklua-compilenapi-functionsnode-curl-to-harnvimlspinlistpeg-javaprettier-plugin-awkpug_html_locator_jsorigo-codequik-langmetric-gardenerpts-langremark-tree-sitterrepomapscribble-generatortalon-tree-sitter-servicesvlintsmells-code-analyzerts-lsptree-gettexttree-sitter-4dcatalogtree-sitter-4dlinktree-sitter-4dpreferencestreesitter-to-unisttree-sitter-spltree-sitter-sql-bigquerytree-sitter-rstmltree-sitter-rtxtree-sitter-svelte-atomtree-sitter-turbowavetree-sitter-types-buildertree-sitter-typescripttree-sitter-textooljsomtony-langvue-o2csweepaisplit-elm-bundlesourcesailorsetemtree-sitter-fudomotree-sitter-ekscripttree-sitter-compilertree-sitter-ptltree-sitter-morphtree-sitter-pascalabcnettree-sitter-ktree-sitter-hasttree-sitter-hovnozeppelin-language-serverwho-wrote-thatwintrospectorcodesee@thomasjm/bash-language-server@smk17/treer@wp-blocks/make-pot@helgardf/cucumber-language-service@trungdq88/curlconverter@vpzk/plugin-treesitter@valtown/typescript-tokenizer@asgerf/dts-tree-sitterbigquery-porter@binhtran432k/cucumber-language-servicebash-language-server-v-dakitbeancount-langserver@swagger-api/apidom-parser-adapter-json@swagger-api/apidom-parser-adapter-yaml-1-2@any-style/cliash2doc@autorest/compare@lukalabs/esbuild-styled-components@fullstackio/docusaurus-preset-newline@kt-graph/corecurlconvertercurlconverter-js@deep-grasp/build@dodona/dolos@dodona/dolos-lib@drudge/curlconvertercodeowners-cli@cucumber/language-servicedgaf-js@codeinsights/js@ectomigo/corecq-treesitter-engine@e024code/repomap
0.21.1

1 month ago

0.21.0

2 months ago

0.20.6

7 months ago

0.20.5

10 months ago

0.20.4

11 months ago

0.20.3

11 months ago

0.20.2

11 months ago

0.20.1

2 years ago

0.20.0

3 years ago

0.19.0

3 years ago

0.17.2

3 years ago

0.18.1

3 years ago

0.18.0

3 years ago

0.17.1

4 years ago

0.17.0

4 years ago

0.16.2

4 years ago

0.16.1

4 years ago

0.16.0

4 years ago

0.15.15

4 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

5 years ago

0.15.0

5 years ago

0.15.0-1

5 years ago

0.15.0-0

5 years ago

0.14.0

5 years ago

0.13.23

5 years ago

0.13.22

5 years ago

0.13.21

5 years ago

0.13.20

5 years ago

0.13.19

5 years ago

0.13.18

5 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.6

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

6 years ago

0.11.2

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.0

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.8

6 years ago

0.8.7

6 years ago

0.8.6

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.5

6 years ago

0.7.4

6 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

7 years ago

0.5.1-0

7 years ago

0.5.0

7 years ago

0.4.6

7 years ago

0.4.5

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.2-5

7 years ago

0.3.2-3

7 years ago

0.3.2-2

7 years ago

0.3.2-1

7 years ago

0.3.2-0

7 years ago

0.3.1

7 years ago

0.3.0

7 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

8 years ago

0.0.42

8 years ago

0.0.41

8 years ago

0.0.40

8 years ago

0.0.39

8 years ago

0.0.38

8 years ago

0.0.37

8 years ago

0.0.36

8 years ago

0.0.35

8 years ago

0.0.34

8 years ago

0.0.33

8 years ago

0.0.32

8 years ago

0.0.31

9 years ago

0.0.30

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

9 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