TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
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.
moduleOptions(optional) - Options to configure the WebAssembly modulelocateFile?: (scriptName: string, scriptDirectory: string) => string- Function to locate the.wasmfile
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.
Methods
setLanguage(language)
Set the language that the parser should use for parsing.
language- ALanguageobject loaded from a.wasmfile, ornullto unset
parse(input, oldTree?, options?)
Parse source code and create a syntax tree.
input- Either a string of source code, or a callback function that returns text chunksoldTree(optional) - A previous syntax tree for incremental parsing. If provided, it must have been edited withtree.edit()to match the new textoptions(optional) - Parsing optionsincludedRanges?: Range[]- Array of ranges to parse (default: entire document)progressCallback?: (state: ParseState) => void- Called periodically during parsing
Tree object, or null if:
- The parser has no language assigned
- The progress callback cancelled parsing by returning
true
reset()
Instruct the parser to start the next parse from the beginning.
reset() if you want to parse a different document instead.
Example:
getIncludedRanges()
Get the ranges of text that the parser will include when parsing.
parse() options, or a default range covering the entire document
Example:
setLogger(callback)
Set a logging callback for debugging the parser.
callback- A function to receive log messages, orfalse/nullto disable loggingLogCallback: (message: string, isLex: boolean) => void
getLogger()
Get the parser’s current logger callback.
null if no logger is set
Example:
delete()
Delete the parser and free its resources.
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.
See Also
- Tree class - Working with syntax trees
- Node class - Traversing and inspecting nodes
- Query API - Pattern matching on syntax trees