pub struct Parser(/* private fields */);
Expand description
A stateful object that this is used to produce a Tree
based on some
source code.
Implementations§
Source§impl Parser
impl Parser
pub fn set_wasm_store(&mut self, store: WasmStore) -> Result<(), LanguageError>
pub fn take_wasm_store(&mut self) -> Option<WasmStore>
Source§impl Parser
impl Parser
Sourcepub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError>
pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError>
Set the language that the parser should use for parsing.
Returns a Result indicating whether or not the language was successfully
assigned. True means assignment succeeded. False means there was a
version mismatch: the language was generated with an incompatible
version of the Tree-sitter CLI. Check the language’s version using
Language::version
and compare it to this library’s
LANGUAGE_VERSION
and MIN_COMPATIBLE_LANGUAGE_VERSION
constants.
Sourcepub fn language(&self) -> Option<LanguageRef<'_>>
pub fn language(&self) -> Option<LanguageRef<'_>>
Get the parser’s current language.
Sourcepub fn logger(&self) -> Option<&Box<dyn FnMut(LogType, &str) + '_>>
pub fn logger(&self) -> Option<&Box<dyn FnMut(LogType, &str) + '_>>
Get the parser’s current logger.
Sourcepub fn set_logger(&mut self, logger: Option<Box<dyn FnMut(LogType, &str) + '_>>)
pub fn set_logger(&mut self, logger: Option<Box<dyn FnMut(LogType, &str) + '_>>)
Set the logging callback that the parser should use during parsing.
Sourcepub fn print_dot_graphs(&mut self, file: &impl AsRawFd)
Available on crate feature std
only.
pub fn print_dot_graphs(&mut self, file: &impl AsRawFd)
std
only.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.
Sourcepub fn stop_printing_dot_graphs(&mut self)
Available on crate feature std
only.
pub fn stop_printing_dot_graphs(&mut self)
std
only.Stop the parser from printing debugging graphs while parsing.
Sourcepub fn parse(
&mut self,
text: impl AsRef<[u8]>,
old_tree: Option<&Tree>,
) -> Option<Tree>
pub fn parse( &mut self, text: impl AsRef<[u8]>, old_tree: Option<&Tree>, ) -> Option<Tree>
Parse a slice of UTF8 text.
§Arguments:
text
The UTF8-encoded text to parse.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.
Returns a Tree
if parsing succeeded, or None
if:
- The parser has not yet had a language assigned with
Parser::set_language
- The timeout set with
Parser::set_timeout_micros
expired (deprecated) - The cancellation flag set with
Parser::set_cancellation_flag
was flipped (deprecated)
Sourcepub fn parse_utf16(
&mut self,
input: impl AsRef<[u16]>,
old_tree: Option<&Tree>,
) -> Option<Tree>
👎Deprecated since 0.25.0: Prefer parse_utf16_le instead
pub fn parse_utf16( &mut self, input: impl AsRef<[u16]>, old_tree: Option<&Tree>, ) -> Option<Tree>
Parse a slice of UTF16 text.
§Arguments:
text
The UTF16-encoded text to parse.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.
Sourcepub fn parse_with<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
&mut self,
callback: &mut F,
old_tree: Option<&Tree>,
) -> Option<Tree>
👎Deprecated since 0.25.0: Prefer parse_with_options
instead
pub fn parse_with<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, old_tree: Option<&Tree>, ) -> Option<Tree>
parse_with_options
insteadParse UTF8 text provided in chunks by a callback.
§Arguments:
callback
A function that takes a byte offset and position and returns a slice of UTF8-encoded text starting at that byte 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.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.
Sourcepub fn parse_with_options<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
&mut self,
callback: &mut F,
old_tree: Option<&Tree>,
options: Option<ParseOptions<'_>>,
) -> Option<Tree>
pub fn parse_with_options<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, old_tree: Option<&Tree>, options: Option<ParseOptions<'_>>, ) -> Option<Tree>
Parse text provided in chunks by a callback.
§Arguments:
callback
A function that takes a byte offset and position and returns a slice of UTF8-encoded text starting at that byte 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.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.options
Options for parsing the text. This can be used to set a progress callback.
Sourcepub fn parse_utf16_with<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
&mut self,
callback: &mut F,
old_tree: Option<&Tree>,
) -> Option<Tree>
👎Deprecated since 0.25.0: Prefer parse_utf16_le_with_options
instead
pub fn parse_utf16_with<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, old_tree: Option<&Tree>, ) -> Option<Tree>
parse_utf16_le_with_options
insteadParse UTF16 text provided in chunks by a callback.
§Arguments:
callback
A function that takes a code point offset and position and returns a slice of UTF16-encoded text starting at that byte 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.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.
Sourcepub fn parse_utf16_le(
&mut self,
input: impl AsRef<[u16]>,
old_tree: Option<&Tree>,
) -> Option<Tree>
pub fn parse_utf16_le( &mut self, input: impl AsRef<[u16]>, old_tree: Option<&Tree>, ) -> Option<Tree>
Parse a slice of UTF16 little-endian text.
§Arguments:
text
The UTF16-encoded text to parse.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.
Sourcepub fn parse_utf16_le_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
&mut self,
callback: &mut F,
old_tree: Option<&Tree>,
options: Option<ParseOptions<'_>>,
) -> Option<Tree>
pub fn parse_utf16_le_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, old_tree: Option<&Tree>, options: Option<ParseOptions<'_>>, ) -> Option<Tree>
Parse UTF16 little-endian text provided in chunks by a callback.
§Arguments:
callback
A function that takes a code point offset and position and returns a slice of UTF16-encoded text starting at that byte 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.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.options
Options for parsing the text. This can be used to set a progress callback.
Sourcepub fn parse_utf16_be(
&mut self,
input: impl AsRef<[u16]>,
old_tree: Option<&Tree>,
) -> Option<Tree>
pub fn parse_utf16_be( &mut self, input: impl AsRef<[u16]>, old_tree: Option<&Tree>, ) -> Option<Tree>
Parse a slice of UTF16 big-endian text.
§Arguments:
text
The UTF16-encoded text to parse.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.
Sourcepub fn parse_utf16_be_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
&mut self,
callback: &mut F,
old_tree: Option<&Tree>,
options: Option<ParseOptions<'_>>,
) -> Option<Tree>
pub fn parse_utf16_be_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, old_tree: Option<&Tree>, options: Option<ParseOptions<'_>>, ) -> Option<Tree>
Parse UTF16 big-endian text provided in chunks by a callback.
§Arguments:
callback
A function that takes a code point offset and position and returns a slice of UTF16-encoded text starting at that byte 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.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.options
Options for parsing the text. This can be used to set a progress callback.
Sourcepub fn parse_custom_encoding<D: Decode, T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
&mut self,
callback: &mut F,
old_tree: Option<&Tree>,
options: Option<ParseOptions<'_>>,
) -> Option<Tree>
pub fn parse_custom_encoding<D: Decode, T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, old_tree: Option<&Tree>, options: Option<ParseOptions<'_>>, ) -> Option<Tree>
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.
§Arguments:
callback
A function that takes a byte offset and position and returns a slice of text starting at that byte 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.old_tree
A previous syntax tree parsed from the same document. If the text of the document has changed sinceold_tree
was created, then you must editold_tree
to match the new text usingTree::edit
.options
Options for parsing the text. This can be used to set a progress callback.
Additionally, you must set the generic parameter [D
] to a type that implements the
Decode
trait. This trait has a single method, decode
, which takes a
slice of bytes and returns a tuple of the code point and the number of bytes consumed.
The decode
method should return -1
for the code point if decoding fails.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Instruct the parser to start the next parse from the beginning.
If the parser previously failed because of a timeout, cancellation,
or callback, then by default, it will resume where it left off on the
next call to parse
or other parsing functions.
If you don’t want to resume, and instead intend to use this parser to
parse some other document, you must call reset
first.
Sourcepub fn timeout_micros(&self) -> u64
👎Deprecated since 0.25.0: Prefer using parse_with_options
and using a callback
pub fn timeout_micros(&self) -> u64
parse_with_options
and using a callbackGet the duration in microseconds that parsing is allowed to take.
This is set via set_timeout_micros
.
Sourcepub fn set_timeout_micros(&mut self, timeout_micros: u64)
👎Deprecated since 0.25.0: Prefer using parse_with_options
and using a callback
pub fn set_timeout_micros(&mut self, timeout_micros: u64)
parse_with_options
and using a callbackSet the maximum duration in microseconds that parsing should be allowed to take before halting.
If parsing takes longer than this, it will halt early, returning None
.
See parse
for more information.
Sourcepub fn set_included_ranges(
&mut self,
ranges: &[Range],
) -> Result<(), IncludedRangesError>
pub fn set_included_ranges( &mut self, ranges: &[Range], ) -> Result<(), IncludedRangesError>
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. You can also pass multiple disjoint ranges.
If ranges
is empty, then the entire document will be parsed.
Otherwise, the given ranges must be ordered from earliest to latest
in the document, and they must not overlap. That is, the following
must hold for all i
< length - 1
:
ranges[i].end_byte <= ranges[i + 1].start_byte
If this requirement is not satisfied, method will return
IncludedRangesError
error with an offset in the passed ranges
slice pointing to a first incorrect range.
Sourcepub fn included_ranges(&self) -> Vec<Range>
pub fn included_ranges(&self) -> Vec<Range>
Get the ranges of text that the parser will include when parsing.
Sourcepub unsafe fn cancellation_flag(&self) -> Option<&AtomicUsize>
👎Deprecated since 0.25.0: Prefer using parse_with_options
and using a callback
pub unsafe fn cancellation_flag(&self) -> Option<&AtomicUsize>
parse_with_options
and using a callbackSourcepub unsafe fn set_cancellation_flag(&mut self, flag: Option<&AtomicUsize>)
👎Deprecated since 0.25.0: Prefer using parse_with_options
and using a callback
pub unsafe fn set_cancellation_flag(&mut self, flag: Option<&AtomicUsize>)
parse_with_options
and using a callbackTrait Implementations§
impl Send for Parser
impl Sync for Parser
Auto Trait Implementations§
impl Freeze for Parser
impl RefUnwindSafe for Parser
impl Unpin for Parser
impl UnwindSafe for Parser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more