Skip to main content
The Parser class is the main interface for parsing source code. It is a stateful object that produces a Tree based on source code input.

Initialization

Parser.init(moduleOptions?)

Initialize the Tree-sitter library. This must be called before creating any Parser instances.
Parameters:
  • moduleOptions (optional) - Options to configure the WebAssembly module
    • locateFile?: (scriptName: string, scriptDirectory: string) => string - Function to locate the .wasm file
Example:

Constructor

new Parser()

Create a new parser instance.
You must call Parser.init() before constructing a parser, or a runtime error will be thrown.

Properties

language

The parser’s current language.
Example:

Methods

setLanguage(language)

Set the language that the parser should use for parsing.
Parameters:
  • language - A Language object loaded from a .wasm file, or null to unset
Returns: The parser instance (for method chaining) Throws: If the language version is incompatible with the current library version Example:

parse(input, oldTree?, options?)

Parse source code and create a syntax tree.
Parameters:
  • input - Either a string of source code, or a callback function that returns text chunks
  • oldTree (optional) - A previous syntax tree for incremental parsing. If provided, it must have been edited with tree.edit() to match the new text
  • options (optional) - Parsing options
    • includedRanges?: Range[] - Array of ranges to parse (default: entire document)
    • progressCallback?: (state: ParseState) => void - Called periodically during parsing
Returns: A Tree object, or null if:
  • The parser has no language assigned
  • The progress callback cancelled parsing by returning true
Example with string input:
Example with callback:
Example with incremental parsing:
Example with included ranges:
Example with timeout:

reset()

Instruct the parser to start the next parse from the beginning.
If the parser previously failed because of a callback, by default it will resume where it left off. Call reset() if you want to parse a different document instead. Example:

getIncludedRanges()

Get the ranges of text that the parser will include when parsing.
Returns: Array of ranges that were set via parse() options, or a default range covering the entire document Example:

setLogger(callback)

Set a logging callback for debugging the parser.
Parameters:
  • callback - A function to receive log messages, or false/null to disable logging
    • LogCallback: (message: string, isLex: boolean) => void
Returns: The parser instance (for method chaining) Example:

getLogger()

Get the parser’s current logger callback.
Returns: The current logger function, or null if no logger is set Example:

delete()

Delete the parser and free its resources.
Example:
While the library uses FinalizationRegistry for automatic cleanup when available, explicitly calling delete() is recommended for deterministic resource management.

Types

ParseOptions

Options for configuring the parsing process.

ParseState

Represents the current state of the parser, passed to the progress callback.

Constants

LANGUAGE_VERSION

The latest ABI version supported by the current library version.

MIN_COMPATIBLE_VERSION

The earliest ABI version supported by the current library version.
Example:

See Also