Trait arithmetic_parser::grammars::Parse

source ·
pub trait Parse {
    type Base: Grammar;

    const FEATURES: Features;

    // Provided methods
    fn parse_statements<'a, I>(input: I) -> Result<Block<'a, Self::Base>, Error>
       where I: IntoInputSpan<'a>,
             Self: Sized { ... }
    fn parse_streaming_statements<'a, I>(
        input: I,
    ) -> Result<Block<'a, Self::Base>, Error>
       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 Types§

source

type Base: Grammar

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

Required Associated Constants§

source

const FEATURES: Features

Features supported by this grammar.

Provided Methods§

source

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

Parses a list of statements.

source

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

Parses a potentially incomplete list of statements.

Object Safety§

This trait is not object safe.

Implementors§