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:
parent
407786b98c
commit
6533c9dcf7
10 changed files with 116 additions and 77 deletions
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use std::fmt;
|
|||
use crate::builtin::Builtin;
|
||||
use crate::span::{HasSpan, Span};
|
||||
|
||||
use super::{Expr, Ident, Space};
|
||||
use super::{Expr, Ident, Separated, Space};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum NumLitStr {
|
||||
|
|
@ -126,11 +126,13 @@ impl HasSpan for TableLitElem {
|
|||
}
|
||||
|
||||
/// `'{ a, foo: b }`
|
||||
///
|
||||
/// Structure: `'{ s0 elems s1 }`
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TableLit {
|
||||
pub elems: Vec<(Space, TableLitElem, Space)>,
|
||||
/// `Some` if there is a trailing comma, `None` otherwise.
|
||||
pub trailing_comma: Option<Space>,
|
||||
pub s0: Space,
|
||||
pub elems: Separated<TableLitElem, (Space, Space), Space>,
|
||||
pub s1: Space,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 Program {
|
||||
|
|
@ -12,12 +12,12 @@ pub enum Program {
|
|||
span: Span,
|
||||
},
|
||||
|
||||
/// Structure: `s0 module elems trailing_comma`
|
||||
/// Structure: `s0 module s1 elems s2`
|
||||
Module {
|
||||
s0: Space,
|
||||
elems: Vec<(Space, TableLitElem, Space)>,
|
||||
/// `Some` if there is a trailing comma, `None` otherwise.
|
||||
trailing_comma: Option<Space>,
|
||||
s1: Space,
|
||||
elems: Separated<TableLitElem, (Space, Space), Space>,
|
||||
s2: Space,
|
||||
span: Span,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::span::{HasSpan, Span};
|
||||
|
||||
use super::{Expr, Ident, Space};
|
||||
use super::{Expr, Ident, Separated, Space};
|
||||
|
||||
// TODO Make table patterns recursive
|
||||
|
||||
|
|
@ -30,12 +30,14 @@ impl HasSpan for TablePatternElem {
|
|||
}
|
||||
}
|
||||
|
||||
/// `'{ foo, bar: baz }`
|
||||
/// `{ foo, bar: baz }`
|
||||
///
|
||||
/// Structure: `{ s0 elems s1 }`
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TablePattern {
|
||||
pub elems: Vec<(Space, TablePatternElem, Space)>,
|
||||
/// `Some` if there is a trailing comma, `None` otherwise.
|
||||
pub trailing_comma: Option<Space>,
|
||||
pub s0: Space,
|
||||
pub elems: Separated<TablePatternElem, (Space, Space), Space>,
|
||||
pub s1: Space,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue