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

@ -1,6 +1,6 @@
use crate::span::{HasSpan, Span};
use super::{Expr, Space, TableLitElem};
use super::{Expr, Separated, Space, TableLitElem};
#[derive(Debug, Clone)]
pub enum TableConstrElem {
@ -31,11 +31,13 @@ impl HasSpan for TableConstrElem {
}
/// `{ a, b, foo: c, [d]: e }`
///
/// Structure: `{ s0 elems s1 }`
#[derive(Debug, Clone)]
pub struct TableConstr {
pub elems: Vec<(Space, TableConstrElem, Space)>,
/// `Some` if there is a trailing comma, `None` otherwise.
pub trailing_comma: Option<Space>,
pub s0: Space,
pub elems: Separated<TableConstrElem, (Space, Space), Space>,
pub s1: Space,
pub span: Span,
}