pub enum ParsedSyntax {
Absent,
Present(CompletedMarker),
}Expand description
Syntax that is either present in the source tree or absent.
This type is commonly used as the return type of parse functions with the following types
§Parse Rule conventions
- A parse rule must return ParsedSyntax::Present if it is able to parse a node or at least parts of it. For example,
the
parse_for_statementshould return ParsedSyntax::Present forfor (even tough many of the required children are missing because it is still able to parse parts of the for statement. - A parse rule must return ParsedSyntax::Absent if the expected node isn’t present in the source code.
In most cases, this means if the first expected token isn’t present, for example,
if the
forkeyword isn’t present when parsing a for statement.
However, it can be possible for rules to recover even if the first token doesn’t match. One example
is when parsing an assignment target that has an optional default. The rule can recover even
if the assignment target is missing as long as the cursor is then positioned at an = token.
The rule must then return ParsedSyntax::Present with the partial parsed node.
- A parse rule must not eat any tokens when it returns ParsedSyntax::Absent
- A parse rule must not add any errors when it returns ParsedSyntax::Absent
This is a custom enum over using Option because ParsedSyntax::Absent values must be handled by the caller.
Variants§
Absent
A syntax that isn’t present in the source code. Used when a parse rule can’t match the current token of the parser.
Present(CompletedMarker)
A completed syntax node with all or some of its children.
Implementations§
Source§impl ParsedSyntax
impl ParsedSyntax
Sourcepub fn ok(self) -> Option<CompletedMarker>
pub fn ok(self) -> Option<CompletedMarker>
Converts from ParsedSyntax to Option<CompletedMarker>.
Converts self into an Option<CompletedMarker>, consuming self
Sourcepub fn and_then<F>(self, op: F) -> ParsedSyntax
pub fn and_then<F>(self, op: F) -> ParsedSyntax
Calls op if the syntax is present and otherwise returns ParsedSyntax::Absent
Sourcepub fn or_else<F>(self, op: F) -> ParsedSyntaxwhere
F: FnOnce() -> ParsedSyntax,
pub fn or_else<F>(self, op: F) -> ParsedSyntaxwhere
F: FnOnce() -> ParsedSyntax,
Calls op if the syntax is absent and otherwise returns ParsedSyntax::Present
Sourcepub fn is_present(&self) -> bool
pub fn is_present(&self) -> bool
Returns true if the parsed syntax is ParsedSyntax::Present
Sourcepub fn is_absent(&self) -> bool
pub fn is_absent(&self) -> bool
Returns true if the parsed syntax is ParsedSyntax::Absent
Sourcepub fn unwrap(self) -> CompletedMarker
pub fn unwrap(self) -> CompletedMarker
It returns the contained ParsedSyntax::Present value, consuming the self value
§Panics
Panics if the current syntax is ParsedSyntax::Absent
Sourcepub fn unwrap_or(self, default: CompletedMarker) -> CompletedMarker
pub fn unwrap_or(self, default: CompletedMarker) -> CompletedMarker
Returns the contained ParsedSyntax::Present value or passed default
Sourcepub fn unwrap_or_else<F>(self, default: F) -> CompletedMarkerwhere
F: FnOnce() -> CompletedMarker,
pub fn unwrap_or_else<F>(self, default: F) -> CompletedMarkerwhere
F: FnOnce() -> CompletedMarker,
Returns the contained ParsedSyntax::Present value or computes it from a clojure.
Sourcepub fn expect(self, msg: &str) -> CompletedMarker
pub fn expect(self, msg: &str) -> CompletedMarker
Returns the contained ParsedSyntax::Present value, consuming the self value.
§Panics
Panics if the value is an ParsedSyntax::Absent with a custom panic message provided by msg.
Sourcepub fn map<F>(self, mapper: F) -> ParsedSyntax
pub fn map<F>(self, mapper: F) -> ParsedSyntax
Maps a ParsedSyntax::Present ParsedSyntax by applying a function to a contained ParsedSyntax::Present value,
leaving an ParsedSyntax::Absent value untouched.
This function can be used to compose the results of two functions.
Sourcepub fn kind<P>(&self, p: &P) -> Option<<P as Parser>::Kind>where
P: Parser,
pub fn kind<P>(&self, p: &P) -> Option<<P as Parser>::Kind>where
P: Parser,
Returns the kind of the syntax if it is present or None otherwise
Sourcepub fn add_diagnostic_if_present<P, E, D>(
self,
p: &mut P,
error_builder: E,
) -> Option<CompletedMarker>
pub fn add_diagnostic_if_present<P, E, D>( self, p: &mut P, error_builder: E, ) -> Option<CompletedMarker>
Adds a diagnostic at the current parser position if the syntax is present and return its marker.
Sourcepub fn or_add_diagnostic<P, E, D>(
self,
p: &mut P,
error_builder: E,
) -> Option<CompletedMarker>
pub fn or_add_diagnostic<P, E, D>( self, p: &mut P, error_builder: E, ) -> Option<CompletedMarker>
It returns the syntax if present or adds a diagnostic at the current parser position.
Sourcepub fn precede_or_add_diagnostic<P, E, D>(
self,
p: &mut P,
error_builder: E,
) -> Marker
pub fn precede_or_add_diagnostic<P, E, D>( self, p: &mut P, error_builder: E, ) -> Marker
It creates and returns a marker preceding this parsed syntax if it is present or starts a new marker and adds an error to the current parser position. See [CompletedMarker.precede]
Sourcepub fn precede<P>(self, p: &mut P) -> Markerwhere
P: Parser,
pub fn precede<P>(self, p: &mut P) -> Markerwhere
P: Parser,
Creates a new marker that precedes this syntax or starts a new marker
Sourcepub fn or_recover_with_token_set<P, E>(
self,
p: &mut P,
recovery: &ParseRecoveryTokenSet<<P as Parser>::Kind>,
error_builder: E,
) -> Result<CompletedMarker, RecoveryError>
pub fn or_recover_with_token_set<P, E>( self, p: &mut P, recovery: &ParseRecoveryTokenSet<<P as Parser>::Kind>, error_builder: E, ) -> Result<CompletedMarker, RecoveryError>
Returns this Syntax if it is present in the source text or tries to recover the parser if the syntax is absent. The recovery…
- eats all unexpected tokens into a
Bogus*node until the parser reaches one of the “safe tokens” configured in the ParseRecoveryTokenSet. - creates an error using the passed in error builder and adds it to the parsing diagnostics.
The error recovery can fail if the parser is located at the EOF token or if the parser is already at a valid position according to the ParseRecoveryTokenSet.
Sourcepub fn or_recover<'source, P, E, R>(
self,
p: &mut P,
recovery: &R,
error_builder: E,
) -> Result<CompletedMarker, RecoveryError>where
P: Parser,
R: ParseRecovery<Parser<'source> = P>,
E: FnOnce(&P, TextRange) -> ParseDiagnostic,
pub fn or_recover<'source, P, E, R>(
self,
p: &mut P,
recovery: &R,
error_builder: E,
) -> Result<CompletedMarker, RecoveryError>where
P: Parser,
R: ParseRecovery<Parser<'source> = P>,
E: FnOnce(&P, TextRange) -> ParseDiagnostic,
Returns this Syntax if it is present in the source text or tries to recover the parser if the syntax is absent. The recovery…
- eats all unexpected tokens into a
Bogus*node until the parser reaches one of the “safe tokens” configured in the ParseRecovery. - creates an error using the passed in error builder and adds it to the parsing diagnostics.
The error recovery can fail if the parser is located at the EOF token or if the parser is already at a valid position according to the ParseRecovery.
Trait Implementations§
Source§impl Debug for ParsedSyntax
impl Debug for ParsedSyntax
Source§impl From<CompletedMarker> for ParsedSyntax
impl From<CompletedMarker> for ParsedSyntax
Source§fn from(marker: CompletedMarker) -> ParsedSyntax
fn from(marker: CompletedMarker) -> ParsedSyntax
Source§impl From<Option<CompletedMarker>> for ParsedSyntax
impl From<Option<CompletedMarker>> for ParsedSyntax
Source§fn from(option: Option<CompletedMarker>) -> ParsedSyntax
fn from(option: Option<CompletedMarker>) -> ParsedSyntax
Source§impl PartialEq for ParsedSyntax
impl PartialEq for ParsedSyntax
impl Eq for ParsedSyntax
impl StructuralPartialEq for ParsedSyntax
Auto Trait Implementations§
impl Freeze for ParsedSyntax
impl RefUnwindSafe for ParsedSyntax
impl Send for ParsedSyntax
impl Sync for ParsedSyntax
impl Unpin for ParsedSyntax
impl UnwindSafe for ParsedSyntax
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.