Parser#

class tree_sitter.Parser(language, *, included_ranges=None, timeout_micros=None)#

A class that is used to produce a Tree based on some source code.

Methods#

parse(source, /, old_tree=None, encoding='utf8')#

Parse a slice of a bytestring or bytes provided in chunks by a callback.

The callback function takes a byte offset and position and returns a bytestring starting at that offset and position. The slices can be of any length. If the given position is at the end of the text, the callback should return an empty slice.

Returns:

A Tree if parsing succeeded or None if the parser does not have an assigned language or the timeout expired.

print_dot_graphs(file)#

Set the file descriptor to which the parser should write debugging graphs during parsing. The graphs are formatted in the DOT language. You can turn off this logging by passing None.

Added in version 0.24.0.

reset()#

Instruct the parser to start the next parse from the beginning.

Note

If the parser previously failed because of a timeout, then by default, it will resume where it left off on the next call to parse(). If you don’t want to resume, and instead intend to use this parser to parse some other document, you must call reset() first.

Attributes#

included_ranges#

The ranges of text that the parser will include when parsing.

language#

The language that will be used for parsing.

logger#

The logger that the parser should use during parsing.

Added in version 0.24.0.

timeout_micros#

The duration in microseconds that parsing is allowed to take.