Function tree_sitter::ffi::ts_parser_parse
source · pub unsafe extern "C" fn ts_parser_parse(
self_: *mut TSParser,
old_tree: *const TSTree,
input: TSInput
) -> *mut TSTreeExpand description
Use the parser to parse some source code and create a syntax tree.
If you are parsing this document for the first time, pass NULL for the
old_tree parameter. Otherwise, if you have already parsed an earlier
version of this document and the document has since been edited, pass the
previous syntax tree so that the unchanged parts of it can be reused.
This will save time and memory. For this to work correctly, you must have
already edited the old syntax tree using the ts_tree_edit function in a
way that exactly matches the source code changes.
The TSInput parameter lets you specify how to read the text. It has the
following three fields:
read: A function to retrieve a chunk of text at a given byte offset and (row, column) position. The function should return a pointer to the text and write its length to thebytes_readpointer. The parser does not take ownership of this buffer; it just borrows it until it has finished reading it. The function should write a zero value to thebytes_readpointer to indicate the end of the document.payload: An arbitrary pointer that will be passed to each invocation of thereadfunction.encoding: An indication of how the text is encoded. EitherTSInputEncodingUTF8orTSInputEncodingUTF16.
This function returns a syntax tree on success, and NULL on failure. There
are four possible reasons for failure:
- The parser does not have a language assigned. Check for this using the
ts_parser_languagefunction. - Parsing was cancelled due to a timeout that was set by an earlier call to
the
ts_parser_set_timeout_microsfunction. You can resume parsing from where the parser left out by callingts_parser_parseagain with the same arguments. Or you can start parsing from scratch by first callingts_parser_reset. - Parsing was cancelled using a cancellation flag that was set by an
earlier call to
ts_parser_set_cancellation_flag. You can resume parsing from where the parser left out by callingts_parser_parseagain with the same arguments. - Parsing was cancelled due to the progress callback returning true. This callback
is passed in
ts_parser_parse_with_optionsinside theTSParseOptionsstruct.