Trait Parse

Source
pub trait Parse<'a> {
    type Base: Grammar<'a>;

    const FEATURES: Features;

    // Provided methods
    fn parse_statements<I>(input: I) -> Result<Block<'a, Self::Base>, Error<'a>>
       where I: IntoInputSpan<'a>,
             Self: Sized { ... }
    fn parse_streaming_statements<I>(
        input: I,
    ) -> Result<Block<'a, Self::Base>, Error<'a>>
       where I: IntoInputSpan<'a>,
             Self: Sized { ... }
}
Expand description

Unites all necessary parsers to form a complete grammar definition.

The two extension points for a Grammar are literals and type annotations. They are defined via Base type. A Grammar also defines a set of supported Features. This allows to customize which constructs are parsed.

Most common sets of Features are covered by Typed and Untyped wrappers; these types allow to not declare Parse impl explicitly. It is still possible to define custom Parse implementations, as shown in the example below.

§Examples

#[derive(Debug)]
struct IntegerGrammar;

impl ParseLiteral for IntegerGrammar {
    // Implementation skipped...
}

impl Parse<'_> for IntegerGrammar {
    type Base = Untyped<Self>;
    const FEATURES: Features = Features::empty();
}

// Here's how a grammar can be used.
let program = "x = 1 + 2 * 3 + sin(a^3 / b^2);";
let parsed = IntegerGrammar::parse_statements(program)?;
println!("{:#?}", parsed);

Required Associated Constants§

Source

const FEATURES: Features

Features supported by this grammar.

Required Associated Types§

Source

type Base: Grammar<'a>

Base for the grammar providing the literal and type annotation parsers.

Provided Methods§

Source

fn parse_statements<I>(input: I) -> Result<Block<'a, Self::Base>, Error<'a>>
where I: IntoInputSpan<'a>, Self: Sized,

Parses a list of statements.

Source

fn parse_streaming_statements<I>( input: I, ) -> Result<Block<'a, Self::Base>, Error<'a>>
where I: IntoInputSpan<'a>, Self: Sized,

Parses a potentially incomplete list of statements.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§