Handle things separated by things differently

I noticed that programs like '{} would parse correctly while '{ } would
expect an inner element. This was because the leading space was actually
part of the element parser, which is a violation of the (as of yet
unspoken) rule that parsers should not parse surrounding whitespace.

Because whitespace whas treated differently from everywhere else and
because this implementation was wrong, I decided to reimplement it,
abstracting the concept of things separated by other things with
optional trailing things. I did this in such a way that surrounding
whitespace is not touched.
This commit is contained in:
Joscha 2022-11-20 20:25:39 +01:00
parent 407786b98c
commit 6533c9dcf7
10 changed files with 116 additions and 77 deletions

View file

@ -47,3 +47,23 @@ impl HasSpan for Ident {
self.span
}
}
#[derive(Debug, Clone)]
pub enum Separated<E, S1, S2> {
Empty(Span),
NonEmpty {
first_elem: E,
last_elems: Vec<(S1, E)>,
trailing: Option<S2>,
span: Span,
},
}
impl<E, S1, S2> HasSpan for Separated<E, S1, S2> {
fn span(&self) -> Span {
match self {
Separated::Empty(span) => *span,
Separated::NonEmpty { span, .. } => *span,
}
}
}