You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to make the parser result generic and reuse the same parser for different tasks. The following variant is working:
pub trait Factory {
type Node;
fn build_number(&mut self, value: u32) -> Self::Node;
}
peg::parser! {
grammar working() for str {
pub rule number<T: Factory>(factory: &mut T) -> T::Node
= n:$(['0'..='9']+) {?
Ok(factory.build_number(n.parse().or(Err("u32"))?))
}
// more rules all having factory argument...
}
}
But there is much boiler plate, always mentioning the Factory argument. Especially calling other rules always needs mentioning the factory argument there, too.
I would have expected that the following variant works, but on a grammar only life time generics may be specified:
peg::parser! {
grammar not_working<T: Factory>(factory: &mut T) for str {
pub rule number() -> T::Node
= n:$(['0'..='9']+) {?
Ok(factory.build_number(n.parse().or(Err("u32"))?))
}
// more rules all having factory argument...
}
}
I tried also the following variant, but I was unable to specify the return type of the rules:
peg::parser! {
grammar almost_working(factory: &mut impl Factory) for str {
pub rule number() -> ???::Node
= n:$(['0'..='9']+) {?
Ok(factory.build_number(n.parse().or(Err("u32"))?))
}
// more rules all having factory argument...
}
}
Is there a way to realize this?
Would it be possible to allow type generics on the grammar itself?
The text was updated successfully, but these errors were encountered:
I believe this would be possible to add, but would make the signatures of the rule functions more confusing. Generics can't be attached to a mod, so they're prepended to the generics list of the functions corresponding to rules generated within it:
I am trying to make the parser result generic and reuse the same parser for different tasks. The following variant is working:
But there is much boiler plate, always mentioning the Factory argument. Especially calling other rules always needs mentioning the factory argument there, too.
I would have expected that the following variant works, but on a grammar only life time generics may be specified:
I tried also the following variant, but I was unable to specify the return type of the rules:
Is there a way to realize this?
Would it be possible to allow type generics on the grammar itself?
The text was updated successfully, but these errors were encountered: