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.

Added in version 0.22.0: constructor

Methods#

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

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.

Changed in version 0.22.0: Now accepts an encoding parameter.

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.

set_included_ranges(ranges, /)#

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

Deprecated since version 0.22.0: Use the included_ranges setter instead.

set_language(language, /)#

Set the language that will be used for parsing.

Deprecated since version 0.22.0: Use the language setter instead.

set_timeout_micros(timeout, /)#

Set the duration in microseconds that parsing is allowed to take.

Deprecated since version 0.22.0: Use the timeout_micros setter instead.

Attributes#

included_ranges#

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

Added in version 0.22.0.

language#

The language that will be used for parsing.

Added in version 0.22.0.

timeout_micros#

The duration in microseconds that parsing is allowed to take.

Added in version 0.22.0.