Question about "lifetime mismatch" #295
-
Here is my simplified code. A lexer produces use peg::{Parse, ParseElem, RuleResult};
enum Token<'lex> {
Keyword,
Name(Vec<&'lex str>),
}
struct LexerResult<'lex>(Vec<Token<'lex>>);
impl<'lex> Parse for LexerResult<'lex> {
type PositionRepr = usize;
fn start(&self) -> usize { 0 }
fn is_eof(&self, p: usize) -> bool { p >= self.0.len() }
fn position_repr(&self, p: usize) -> Self::PositionRepr { p }
}
impl<'parse: 'lex, 'lex> ParseElem<'parse> for LexerResult<'lex>
{
type Element = &'lex Token<'lex>;
fn parse_elem(&'parse self, pos: usize) -> RuleResult<Self::Element> {
match self.0.get(pos) {
Some(t) => RuleResult::Matched(pos + 1, t),
None => RuleResult::Failed,
}
}
}
peg::parser! {
grammar parser<'lex>() for LexerResult<'lex> {
rule keyword() = [&Token::Keyword]
rule name() -> Vec<&'lex str> = [&Token::Name(s)] { s }
rule syntax() = keyword() name()
}
} But Rust complains about lifetime mismatch:
Is there a workaround for this? Or is this the same problem as #291 (comment) described? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is pretty similar to #291. You can make it compile if you get the lifetimes just right: --- a/src/main.rs
+++ b/src/main.rs
@@ -17,9 +17,9 @@
fn position_repr(&self, p: usize) -> Self::PositionRepr { p }
}
-impl<'parse: 'lex, 'lex> ParseElem<'parse> for LexerResult<'lex>
+impl<'parse, 'lex: 'parse> ParseElem<'parse> for LexerResult<'lex>
{
- type Element = &'lex Token<'lex>;
+ type Element = &'parse Token<'lex>;
fn parse_elem(&'parse self, pos: usize) -> RuleResult<Self::Element> {
match self.0.get(pos) {
@@ -33,7 +33,7 @@
grammar parser<'lex>() for LexerResult<'lex> {
rule keyword() = [&Token::Keyword]
- rule name() -> Vec<&'lex str> = [&Token::Name(s)] { s }
+ rule name() -> &'input Vec<&'lex str> = [&Token::Name(ref s)] { s }
rule syntax() = keyword() name()
}
This is harder than it needs to be -- realistically, you probably don't need the flexibility offered by using two lifetimes here to be able to discard the I'd like to make that input reference and associated lifetime explicit instead of implicit in an upcoming release to let you write |
Beta Was this translation helpful? Give feedback.
This is pretty similar to #291.
You can make it compile if you get the lifetimes just right: