I'm trying to write a parser.
I want to create a convenience method that will parse a sequence of AST nodes separated by some arbitrary token.
I have this method:
pub fn parse_sequence<T, F> (&mut self, separator: TokenKind, func: F) -> PResult<Vec<T>>
where
F: Fn(&mut Parser) -> PResult<T>
{
let items = Vec::new();
items.push(func(self)?);
while self.tokens.peek().kind == separator {
self.eat(separator)?;
items.push(func(self)?);
}
Ok(items)
}
I am calling the function from another method like this:
self.parse_sequence(TokenKind::Comma, Parser::parse_simple_factor)?
With this code I get the following error:
mismatched types
expected type `std::ops::FnOnce<(&mut parse::Parser<'_>,)>`
found type `for<'r> std::ops::FnOnce<(&'r mut parse::Parser<'_>,)>`rustc(E0308)
This is a tough one. I've tried putting explicit lifetimes all over the parse_sequence
function signature, but I always get the same error.
What does this error mean?
Is it possible to pass a method to another method in the same impl?