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§
Sourcetype Parser
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
.
Sourcetype Error: Error + ToDiagnostic
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.
Sourcetype Config
type Config
The concrete type representing the parser configuration.
For many use cases, no configuration is needed, in which case you should use ()
.
Sourcetype Token
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§
Sourcefn root_file_error(err: Error, path: PathBuf) -> Self::Error
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.
Sourcefn parse<S>(
parser: &Parser<Self::Config>,
diagnostics: &DiagnosticsHandler,
source: S,
) -> Result<T, Self::Error>where
S: 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.
Sourcefn parse_tokens<S>(
diagnostics: &DiagnosticsHandler,
codemap: Arc<CodeMap>,
tokens: S,
) -> Result<T, Self::Error>where
S: IntoIterator<Item = Self::Token>,
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.