Parsing

The minor mode tree-sitter-mode provides the high-level interface for working with an up-to-date buffer-local syntax tree. Writing a Dependent Minor Mode is recommended over directly using the low level parsing APIs below.

Parsing is done through stateful parser objects.

tsc-make-parser
Create a new parser without setting a language.
tsc-set-language parser language
Set a parser’s active language.
tsc-parse-string parser string
Parse a single string of source code. This is useful for quick, one-off parsing needs.
(let ((parser (tsc-make-parser)))
  (tsc-set-language parser (tree-sitter-require 'rust))
  (tsc-parse-string parser "fn foo() {}"))
tsc-parse-chunks parser input-function old-tree
Parse chunks of source code generated by an input-function. The function should take 3 parameters: (bytepos line-number byte-column), and return a fragment of the source code, starting from the position identified by either bytepos or (line-number . byte-column). It should return an empty string to signal the end of the source code.

Incremental parsing: If you have already parsed an earlier version of this document, and it has since been edited, pass the previously parsed old-tree so that its unchanged parts can be reused. This will save time and memory. For this to work correctly, you must have already edited it using tsc-edit-tree function in a way that exactly matches the source code changes.

tsc-edit-tree tree ...
Prepare a tree for incremental parsing, by editing it to keep it in sync with source code that has been edited. You must describe the edit both in terms of byte positions and in terms of (line-number . byte-column) coordinates.

For more details, see Tree-sitter’s documentation: