-
Notifications
You must be signed in to change notification settings - Fork 32
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
Support back-references with ">>" #49
Comments
A workaround is:
You may else define a new owned version of |
I prefer your workaround as I don't need to use fn seq_owned<'a, I>(tag: Vec<I>) -> Parser<'a, I, Vec<I>>
where
I: PartialEq + Debug + Clone,
{
Parser::new(move |input: &[I], start: usize| {
let mut index = 0;
loop {
let pos = start + index;
if index == tag.len() {
return Ok((tag.to_owned(), pos));
}
if let Some(s) = input.get(pos) {
if tag[index] != *s {
return Err(Error::Mismatch {
message: format!("seq {:?} expect: {:?}, found: {:?}", tag, tag[index], s),
position: pos,
});
}
} else {
return Err(Error::Incomplete);
}
index += 1;
}
})
} |
...And here is a much shorter owned version of fn seq_owned(tag: &[u8]) -> Parser<u8, ()> {
let tag = tag.to_owned();
take(tag.len()).convert(move |t| if t == tag { Ok(()) } else { Err(()) })
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because
seq
produces aParser
that continues to borrow itstag
, it's not possible to use the overloaded right shift operator (>>) withseq
to create a back-reference to a previously parsed fragment.As a basic example, the following will not compile because
tag
does not live long enough:One solution is to modify
seq
so that it makes an internal copy oftag
to be moved into the closure it generates. I tried this but I wasn't quite successful as I also changed the return type toParser<'a, I, Vec<I>>
and introduced a copy every time the sequence matched (only for the result to be immediately discarded).Perhaps there is a way for
seq
to support both borrowing and owning its tag, or perhaps there is a good case for a new parser factory that matches against an owned tag?Suggestions for alternatives are welcome.
The text was updated successfully, but these errors were encountered: