ParsedSyntax

Enum ParsedSyntax 

Source
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_statement should return ParsedSyntax::Present for for ( 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 for keyword 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.

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

Source

pub fn ok(self) -> Option<CompletedMarker>

Converts from ParsedSyntax to Option<CompletedMarker>.

Converts self into an Option<CompletedMarker>, consuming self

Source

pub fn and_then<F>(self, op: F) -> ParsedSyntax

Calls op if the syntax is present and otherwise returns ParsedSyntax::Absent

Source

pub fn or_else<F>(self, op: F) -> ParsedSyntax
where F: FnOnce() -> ParsedSyntax,

Calls op if the syntax is absent and otherwise returns ParsedSyntax::Present

Source

pub fn is_present(&self) -> bool

Returns true if the parsed syntax is ParsedSyntax::Present

Source

pub fn is_absent(&self) -> bool

Returns true if the parsed syntax is ParsedSyntax::Absent

Source

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

Source

pub fn unwrap_or(self, default: CompletedMarker) -> CompletedMarker

Returns the contained ParsedSyntax::Present value or passed default

Source

pub fn unwrap_or_else<F>(self, default: F) -> CompletedMarker
where F: FnOnce() -> CompletedMarker,

Returns the contained ParsedSyntax::Present value or computes it from a clojure.

Source

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.

Source

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.

Source

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

Source

pub fn add_diagnostic_if_present<P, E, D>( self, p: &mut P, error_builder: E, ) -> Option<CompletedMarker>
where P: Parser, E: FnOnce(&P, TextRange) -> D, D: ToDiagnostic<P>,

Adds a diagnostic at the current parser position if the syntax is present and return its marker.

Source

pub fn or_add_diagnostic<P, E, D>( self, p: &mut P, error_builder: E, ) -> Option<CompletedMarker>
where P: Parser, E: FnOnce(&P, TextRange) -> D, D: ToDiagnostic<P>,

It returns the syntax if present or adds a diagnostic at the current parser position.

Source

pub fn precede_or_add_diagnostic<P, E, D>( self, p: &mut P, error_builder: E, ) -> Marker
where P: Parser, E: FnOnce(&P, TextRange) -> D, D: ToDiagnostic<P>,

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]

Source

pub fn precede<P>(self, p: &mut P) -> Marker
where P: Parser,

Creates a new marker that precedes this syntax or starts a new marker

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<CompletedMarker> for ParsedSyntax

Source§

fn from(marker: CompletedMarker) -> ParsedSyntax

Converts to this type from the input type.
Source§

impl From<Option<CompletedMarker>> for ParsedSyntax

Source§

fn from(option: Option<CompletedMarker>) -> ParsedSyntax

Converts to this type from the input type.
Source§

impl PartialEq for ParsedSyntax

Source§

fn eq(&self, other: &ParsedSyntax) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ParsedSyntax

Source§

impl StructuralPartialEq for ParsedSyntax

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more