The Parser API provides functions for creating parser instances, configuring parsing behavior, and parsing source code into syntax trees.Documentation 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.
Creating and deleting parsers
ts_parser_new
Create a new parser.NULL if allocation failed.
Example:
ts_parser_delete
Delete the parser, freeing all of the memory that it used.The parser to delete
Language configuration
ts_parser_language
Get the parser’s current language.The parser instance
NULL if no language is set.
ts_parser_set_language
Set the language that the parser should use for parsing.The parser instance
The language to use
true if the language was successfully assigned, false if there was a version mismatch.
A false return value means the language was generated with an incompatible version of the Tree-sitter CLI. Check the language’s ABI version using ts_language_abi_version and compare it to TREE_SITTER_LANGUAGE_VERSION and TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION.
Example:
Parsing ranges
ts_parser_set_included_ranges
Set the ranges of text that the parser should include when parsing.The parser instance
Array of ranges to include
Number of ranges in the array
true on success, false if the ranges are invalid.
By default, the parser will always include entire documents. This function allows you to parse only a portion of a document but still return a syntax tree whose ranges match up with the document as a whole. You can also pass multiple disjoint ranges.
The parser copies the range data, so you can free the ranges array after calling this function.
If count is zero, the entire document will be parsed. Otherwise, the ranges must be:
- Ordered from earliest to latest in the document
- Non-overlapping:
ranges[i].end_byte <= ranges[i + 1].start_bytefor alli < count - 1
ts_parser_included_ranges
Get the ranges of text that the parser will include when parsing.The parser instance
Output parameter for the number of ranges
Parsing functions
ts_parser_parse
Parse source code and create a syntax tree.The parser instance
Previous syntax tree for incremental parsing, or
NULL for the first parseInput structure describing how to read the source code
NULL on failure.
If you’re parsing this document for the first time, pass NULL for old_tree. If you have already parsed an earlier version of this document and it has since been edited, pass the previous syntax tree so that unchanged parts can be reused. This saves time and memory.
For incremental parsing to work correctly, you must have already edited the old syntax tree using ts_tree_edit in a way that exactly matches the source code changes.
The TSInput parameter has these fields:
read: Function to retrieve text at a given byte offset and positionpayload: Arbitrary pointer passed to eachreadinvocationencoding: EitherTSInputEncodingUTF8orTSInputEncodingUTF16decode: Optional custom decode function for custom encodings
- The parser does not have a language assigned
- Parsing was cancelled due to a progress callback returning
true
ts_parser_parse_with_options
Parse source code with additional options.The parser instance
Previous syntax tree for incremental parsing, or
NULLInput structure describing how to read the source code
Options structure with progress callback
NULL on failure.
This extends ts_parser_parse with a TSParseOptions struct containing:
payload: Arbitrary pointer passed to the progress callbackprogress_callback: Function called periodically during parsing that can cancel parsing by returningtrue
ts_parser_parse_string
Parse source code stored in a contiguous buffer.The parser instance
Previous syntax tree for incremental parsing, or
NULLSource code string
Length of the string in bytes
NULL on failure.
Example:
ts_parser_parse_string_encoding
Parse source code stored in a contiguous buffer with a specified encoding.The parser instance
Previous syntax tree for incremental parsing, or
NULLSource code string
Length of the string in bytes
The text encoding:
TSInputEncodingUTF8, TSInputEncodingUTF16LE, or TSInputEncodingUTF16BENULL on failure.
ts_parser_reset
Instruct the parser to start the next parse from the beginning.The parser instance
Logging and debugging
ts_parser_set_logger
Set the logger that a parser should use during parsing.The parser instance
Logger structure with callback function and payload
TSLogger struct contains:
payload: Arbitrary pointer passed to the log callbacklog: Callback function that receives log messages
ts_parser_logger
Get the parser’s current logger.The parser instance
ts_parser_print_dot_graphs
Set the file descriptor to which the parser should write debugging graphs during parsing.The parser instance
File descriptor for output, or a negative number to disable
dot(1) process to generate SVG output.
WebAssembly support
ts_parser_set_wasm_store
Assign a Wasm store to the parser.The parser instance
The Wasm store to assign
ts_parser_take_wasm_store
Remove the parser’s current Wasm store and return it.The parser instance
NULL if the parser doesn’t have one.