Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 'input lifetime parameter for token definitions. #270

Closed
sandeep-datta opened this issue Sep 4, 2021 · 4 comments
Closed

Allow 'input lifetime parameter for token definitions. #270

sandeep-datta opened this issue Sep 4, 2021 · 4 comments

Comments

@sandeep-datta
Copy link
Contributor

Note the following code does not compile because of 'input. Please consider allowing this use case.

#[derive(Clone)]
pub enum Token<'input> {
    Open,
    Number(i32),
    Comma,
    Close,
    Ident(&'input str)
}

peg::parser!{
    grammar tokenparser() for [Token<'input>] {
          ...
    }
}
@kevinmehall
Copy link
Owner

I think this was fixed by #254 -- does this work?

peg::parser!{
    grammar tokenparser<'a>() for [Token<'a>] {
          ...
    }
}

@sandeep-datta
Copy link
Contributor Author

sandeep-datta commented Sep 4, 2021

It just creates a separate lifetime ...
pub fn module_traced<'input, 'a>(__input: &'input Input<'a>, module_name: &str) -> ::std::result::Result<Ast<'input>, ::peg::error::ParseError<PositionRepr<'a>>>

Also the following does not work either ....

peg::parser!{
    grammar tokenparser<'input>() for [Token<'input>] {
          ...
    }
}

"lifetime name 'input declared twice in the same scope"

@kevinmehall
Copy link
Owner

Hmm, so the problem is that you want to produce a result containing &'input Token<'a> without requiring the result type to have both lifetime params?

Ideally I'd like to get rid of the magic 'input lifetime since hard-coding a name violates macro hygiene and reduces flexibility in places like this, but I'm not sure how to do it without fully exposing the &'input part of the input type and making everyone write grammar foo<'input> for &'input str for the common case.

@sandeep-datta
Copy link
Contributor Author

I might have had a brief mental lapse here. I conflated the lifetime of the token stream with the lifetime of the string used to generate it.

Regardless, I like your explicit lifetime syntax. Maybe you can elide the lifetime when the grammar does not reference it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants