How would go about building parsers at runtime? #221
-
I'm aware of the boxed method, but I would like to get a simple example on using it to create a parser at runtime. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Normally, parsers (like iterators) have a unique type that depends on how you compose them. For example, the type of If you're trying to build up parsers at runtime, this is obviously not particularly useful. 'Simple' code like the following will fail to compile due to the parsers having mismatched types: let twitter_alias = just('@').ignore_then(text::ident());
let mastodon_alias = just('@')
.ignore_then(text::ident())
.chain(just('@').ignore_then(url));
// Error: type mismatch between match arms
let alias_parser = match get_alias_mode_at_runtime() {
AliasMode::Twitter => twitter_alias,
AliasMode::Mastodon => mastodon_alias,
}; You can use ...
// No compilation error: both parsers now have type `Boxed<...>`!
let alias_parser = match get_alias_mode_at_runtime() {
AliasMode::Twitter => twitter_alias.boxed(),
AliasMode::Mastodon => mastodon_alias.boxed(),
}; This is a technique I've seen some people use for building up operator precedence parsers, for example. |
Beta Was this translation helpful? Give feedback.
Normally, parsers (like iterators) have a unique type that depends on how you compose them.
For example, the type of
just("a").or(just("b"))
will look something likeOr<Just<...>, Just<...>>
.If you're trying to build up parsers at runtime, this is obviously not particularly useful. 'Simple' code like the following will fail to compile due to the parsers having mismatched types: