Replies: 1 comment 2 replies
-
Thanks for the feedback! The Instead of requiring The custom use peg::{Parse, ParseElem, RuleResult};
pub struct SliceByRef<'a, T>(pub &'a [T]);
impl<'a , T> Parse for SliceByRef<'a, T> {
type PositionRepr = usize;
fn start(&self) -> usize {
0
}
fn is_eof(&self, pos: usize) -> bool {
pos >= self.0.len()
}
fn position_repr(&self, pos: usize) -> usize {
pos
}
}
impl<'a, T: 'a> ParseElem<'a> for SliceByRef<'a, T> {
type Element = &'a T;
fn parse_elem(&'a self, pos: usize) -> RuleResult<&'a T> {
match self.0[pos..].first() {
Some(c) => RuleResult::Matched(pos + 1, c),
None => RuleResult::Failed,
}
}
} Given that this happens in a trait impl, using a wrapper type is really the only way to choose an alternative implementation. Perhaps something like this wrapper should be included in the |
Beta Was this translation helpful? Give feedback.
-
Hello,
First, thanks for publishing this library. I’ve experimented with a few different libs in Rust for parsing text, and rust-peg is by far the best one I’ve tried.
The breaking change in 0.8 to require elements to be copyable is creating some extra complexity in a set of parsers I’ve written and I just wanted to describe my situation in case anything useful can come from it, either in showing me a better way, or else to have the constraint relaxed again.
I’m reimplementing a language parser that originally consisted of a hand-written lexer and Pratt (I think? I am very unknowledgeable here) parser. The lexer was used for a couple of totally different DSLs in the application, and to maximise compatibility and improve the clarity of implementation I am taking the same basic approach.
The problematic thing that the lexer does is transform multi-line string literals into single strings, which means that the
Token
type contains aCow<'input, str>
and cannot beCopy
.Right now as a stopgap solution I’m just creating a second
Vec<&Token>
and then have the parsers operate on that instead, which is not great.At some point I was already planning to try making the lexer implement the
Parse
trait for some currently missing features, and I think that this may also solve the problem since backtracking should be limited enough that I think I can just store a small buffer of tokens and return references to them instead, but I haven’t tried doing this yet to know how difficult it will be, and I don’t know if this is a generalisable solution for everyone in a similar situation (though I also don’t know how frequently this ‘similar’ situation will be for anyone, since I don’t expect most parsers to have a separate lexer like this).Thank you again for the library, and eager to hear feedback from your greater knowledge and experience.
Best,
Beta Was this translation helpful? Give feedback.
All reactions