Skip to main content
The Parser is a stateful object used to produce a Tree from source code.

Constructor

new

Create a new parser.

Configuration

set_language

Set the language that the parser should use for parsing.
&Language
required
The language to use for parsing
Returns: Ok(()) if the language was successfully assigned, or Err(LanguageError) if there was a version mismatch.

language

Get the parser’s current language. Returns: Some(LanguageRef) if a language is set, or None if no language has been assigned.

set_included_ranges

Set the ranges of text that the parser should include when parsing. 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.
&[Range]
required
The ranges to parse. Must be ordered from earliest to latest and non-overlapping.
Returns: Ok(()) if the ranges were valid, or Err(IncludedRangesError) with the index of the first invalid range.

included_ranges

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

Parsing Methods

parse

Parse a slice of UTF8 text.
impl AsRef<[u8]>
required
The UTF8-encoded text to parse
Option<&Tree>
A previous syntax tree parsed from the same document. If the text has changed since old_tree was created, you must edit old_tree to match the new text using Tree::edit.
Returns: A Tree if parsing succeeded, or None if the parser has not yet had a language assigned.

parse_with_options

Parse text provided in chunks by a callback.
&mut F
required
A function that takes a byte offset and position and returns a slice of UTF8-encoded text starting at that byte offset and position
Option<&Tree>
A previous syntax tree parsed from the same document
Option<ParseOptions>
Options for parsing, such as a progress callback

parse_utf16_le

Parse a slice of UTF16 little-endian text.
impl AsRef<[u16]>
required
The UTF16-encoded text to parse
Option<&Tree>
A previous syntax tree parsed from the same document

parse_utf16_be

Parse a slice of UTF16 big-endian text.
impl AsRef<[u16]>
required
The UTF16-encoded text to parse
Option<&Tree>
A previous syntax tree parsed from the same document

parse_custom_encoding

Parse text provided in chunks by a callback using a custom encoding. This is useful for parsing text in encodings that are not UTF-8 or UTF-16.
Decode
required
A type that implements the Decode trait to decode the custom encoding
&mut F
required
A function that takes a byte offset and position and returns a slice of text
Option<&Tree>
A previous syntax tree parsed from the same document
Option<ParseOptions>
Options for parsing

State Management

reset

Instruct the parser to start the next parse from the beginning. If the parser previously failed because of a callback, 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.

Logging and Debugging

set_logger

Set the logging callback that the parser should use during parsing.
Option<Logger>
A boxed closure that receives log messages. Set to None to disable logging.

logger

Get the parser’s current logger.
Set the destination to which the parser should write debugging graphs during parsing. The graphs are formatted in the DOT language. You may want to pipe these graphs directly to a dot(1) process in order to generate SVG output.
This method is only available on Unix platforms with the std feature enabled.

stop_printing_dot_graphs

Stop the parser from printing debugging graphs while parsing.

Parse Options

ParseOptions

Options for parsing text.

new

Create new parse options with default values.

progress_callback

Set a progress callback that will be called periodically during parsing.
&'a mut F
required
A callback that receives the current parse state and returns ControlFlow::Continue(()) to continue parsing or ControlFlow::Break(()) to abort

Examples

Basic Parsing

Incremental Parsing

Parsing with Callbacks