Trait Parse

Source
pub trait Parse<T = Self> {
    type Parser;
    type Error: Error + ToDiagnostic;
    type Config;
    type Token;

    // Required methods
    fn root_file_error(err: Error, path: PathBuf) -> Self::Error;
    fn parse<S>(
        parser: &Parser<Self::Config>,
        diagnostics: &DiagnosticsHandler,
        source: S,
    ) -> Result<T, Self::Error>
       where S: Source;
    fn parse_tokens<S>(
        diagnostics: &DiagnosticsHandler,
        codemap: Arc<CodeMap>,
        tokens: S,
    ) -> Result<T, Self::Error>
       where S: IntoIterator<Item = Self::Token>;
}
Expand description

The Parse trait abstracts over the common machinery used to parse some type [T].

Required Associated Types§

Source

type Parser

The concrete type of the parser implementation

For example, if using LALRPOP, this would correspond to the specific generated parser type, e.g. grammar::FooParser.

Source

type Error: Error + ToDiagnostic

The concrete type of errors which are produced by the parser

To better interact with our diagnostics infrastructure, it is required that this type implement ToDiagnostic.

Source

type Config

The concrete type representing the parser configuration.

For many use cases, no configuration is needed, in which case you should use ().

Source

type Token

The concrete type of the lexical token consumed by the parser

For example, if using LALRPOP, this would correspond go the token type used in the LALRPOP grammar.

This crate is built under the assumption that you are using a custom lexer for greater control in the parser, and to associate a [miden_diagnostic::SourceSpan] to each token produced by the lexer. If you are building a parser without a lexer, you are better off using the underlying primitives directly as you see fit.

Required Methods§

Source

fn root_file_error(err: Error, path: PathBuf) -> Self::Error

Constructs an instance of Self::Error when a [std:::io::Error] is raised

This allows us to handle the machinery of reading files from disk without having to know anything about the specific error type produced by a parser.

Source

fn parse<S>( parser: &Parser<Self::Config>, diagnostics: &DiagnosticsHandler, source: S, ) -> Result<T, Self::Error>
where S: Source,

Parses a [T] from the given Source.

Internally, this is expected to construct a token stream from source, typically by constructing a lexer which implements Iterator for the expected token type, and then invoke parse_tokens to handle the actual parsing.

Source

fn parse_tokens<S>( diagnostics: &DiagnosticsHandler, codemap: Arc<CodeMap>, tokens: S, ) -> Result<T, Self::Error>
where S: IntoIterator<Item = Self::Token>,

Parses a [T] from the given token stream.

If using LALRPOP, this is where you would invoke the generated parser, passing it the token iterator.

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§