diff --git a/src/ast/lit.rs b/src/ast/lit.rs index 61bb9e1..ee486ef 100644 --- a/src/ast/lit.rs +++ b/src/ast/lit.rs @@ -126,8 +126,6 @@ impl HasSpan for TableLitElem { } /// `'{ a, foo: b }` -/// -/// Structure: `'{ s0 elems s1 }` #[derive(Debug, Clone)] pub struct TableLit(pub BoundedSeparated); diff --git a/src/ast/table_constr.rs b/src/ast/table_constr.rs index d347ce1..0e16ae6 100644 --- a/src/ast/table_constr.rs +++ b/src/ast/table_constr.rs @@ -1,6 +1,6 @@ use crate::span::{HasSpan, Span}; -use super::{Expr, Separated, Space, TableLitElem}; +use super::{BoundedSeparated, Expr, Space, TableLitElem}; #[derive(Debug, Clone)] pub enum TableConstrElem { @@ -31,18 +31,11 @@ impl HasSpan for TableConstrElem { } /// `{ a, b, foo: c, [d]: e }` -/// -/// Structure: `{ s0 elems s1 }` #[derive(Debug, Clone)] -pub struct TableConstr { - pub s0: Space, - pub elems: Separated, - pub s1: Space, - pub span: Span, -} +pub struct TableConstr(pub BoundedSeparated); impl HasSpan for TableConstr { fn span(&self) -> Span { - self.span + self.0.span() } } diff --git a/src/desugar/field.rs b/src/desugar/field.rs index 7618326..496cd44 100644 --- a/src/desugar/field.rs +++ b/src/desugar/field.rs @@ -1,5 +1,5 @@ use crate::ast::{ - Call, Expr, Field, Line, Lit, Separated, Space, StringLit, StringLitElem, TableConstr, + BoundedSeparated, Call, Expr, Field, Line, Lit, Space, StringLit, StringLitElem, TableConstr, TableConstrElem, TableLitElem, }; use crate::builtin::Builtin; @@ -17,25 +17,26 @@ impl Field { } => { // ` expr s0 [ s1 index s2 ]` // -> `'get s0 { expr, s1 index s2 }` - let elems = Separated::NonEmpty { - first_elem: TableConstrElem::Lit(TableLitElem::Positional(expr)), - last_elems: vec![( - (Space::empty(span), s1), + let elems = vec![ + ( + Space::empty(span), + TableConstrElem::Lit(TableLitElem::Positional(expr)), + Space::empty(span), + ), + ( + s1, TableConstrElem::Lit(TableLitElem::Positional(index)), - )], - trailing: None, - span, - }; - let constr = TableConstr { - s0: Space::empty(span), - elems, - s1: s2, - span, - }; + s2, + ), + ]; let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Get, span))), s0, - constr, + constr: TableConstr(BoundedSeparated { + elems, + trailing: None, + span, + }), span, }); (new, true) @@ -54,31 +55,31 @@ impl Field { } => { // `expr s0 [ s1 index s2 ] s3 = s4 value` // -> `'set s0 { expr, s1 index s2, s3 s4 value }` - let elems = Separated::NonEmpty { - first_elem: TableConstrElem::Lit(TableLitElem::Positional(expr)), - last_elems: vec![ - ( - (Space::empty(span), s1), - TableConstrElem::Lit(TableLitElem::Positional(index)), - ), - ( - (s2, s3.then_line(Line::Empty).then(s4)), - TableConstrElem::Lit(TableLitElem::Positional(value)), - ), - ], - trailing: None, - span, - }; - let constr = TableConstr { - s0: Space::empty(span), - elems, - s1: Space::empty(span), - span, - }; + let elems = vec![ + ( + Space::empty(span), + TableConstrElem::Lit(TableLitElem::Positional(expr)), + Space::empty(span), + ), + ( + s1, + TableConstrElem::Lit(TableLitElem::Positional(index)), + s2, + ), + ( + s3.then_line(Line::Empty).then(s4), + TableConstrElem::Lit(TableLitElem::Positional(value)), + Space::empty(span), + ), + ]; let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::Set, span))), s0, - constr, + constr: TableConstr(BoundedSeparated { + elems, + trailing: None, + span, + }), span, }); (new, true) diff --git a/src/desugar/var.rs b/src/desugar/var.rs index 887d115..7cb5268 100644 --- a/src/desugar/var.rs +++ b/src/desugar/var.rs @@ -1,5 +1,5 @@ use crate::ast::{ - Call, Expr, Field, Line, Lit, Separated, Space, StringLit, StringLitElem, TableConstr, + BoundedSeparated, Call, Expr, Field, Line, Lit, Space, StringLit, StringLitElem, TableConstr, TableConstrElem, TableLitElem, Var, }; use crate::builtin::Builtin; @@ -83,31 +83,31 @@ impl Var { s1: Space::empty(span), span, }); - let elems = Separated::NonEmpty { - first_elem: TableConstrElem::Lit(TableLitElem::Positional(Box::new(scope))), - last_elems: vec![ - ( - (Space::empty(span), local.then_line(Line::Empty).then(s0)), - TableConstrElem::Lit(TableLitElem::Positional(index)), - ), - ( - (s1, s2.then_line(Line::Empty).then(s3)), - TableConstrElem::Lit(TableLitElem::Positional(value)), - ), - ], - trailing: None, - span, - }; - let constr = TableConstr { - s0: Space::empty(span), - elems, - s1: Space::empty(span), - span, - }; + let elems = vec![ + ( + Space::empty(span), + TableConstrElem::Lit(TableLitElem::Positional(Box::new(scope))), + Space::empty(span), + ), + ( + local.then_line(Line::Empty).then(s0), + TableConstrElem::Lit(TableLitElem::Positional(index)), + s1, + ), + ( + s2.then_line(Line::Empty).then(s3), + TableConstrElem::Lit(TableLitElem::Positional(value)), + Space::empty(span), + ), + ]; let new = Expr::Call(Call::Constr { expr: Box::new(Expr::Lit(Lit::Builtin(Builtin::SetRaw, span))), s0: Space::empty(span), - constr, + constr: TableConstr(BoundedSeparated { + elems, + trailing: None, + span, + }), span, }); (new, true) diff --git a/src/parser/table_constr.rs b/src/parser/table_constr.rs index fd6aefd..65abbcb 100644 --- a/src/parser/table_constr.rs +++ b/src/parser/table_constr.rs @@ -4,7 +4,7 @@ use chumsky::prelude::*; use crate::ast::{Expr, Space, TableConstr, TableConstrElem, TableLitElem}; -use super::basic::{separated_by, EParser, Error}; +use super::basic::{bounded_separated, EParser, Error}; fn table_constr_elem( space: EParser, @@ -43,19 +43,13 @@ pub fn table_constr( expr: EParser, ) -> EParser { let elem = table_constr_elem(space.clone(), table_lit_elem, expr); - let separator = space.clone().then_ignore(just(',')).then(space.clone()); - let trailing_separator = space.clone().then_ignore(just(',')); - - space - .clone() - .then(separated_by(elem, separator, trailing_separator)) - .then(space) - .delimited_by(just('{'), just('}')) - .map_with_span(|((s0, elems), s1), span| TableConstr { - s0, - elems, - s1, - span, - }) - .boxed() + bounded_separated( + space, + just('{').to(()), + just('}').to(()), + just(',').to(()), + elem, + ) + .map(TableConstr) + .boxed() } diff --git a/src/pretty/table_constr.rs b/src/pretty/table_constr.rs index 704ab3e..3b3fdb1 100644 --- a/src/pretty/table_constr.rs +++ b/src/pretty/table_constr.rs @@ -2,8 +2,6 @@ use pretty::{DocAllocator, DocBuilder, Pretty}; use crate::ast::{TableConstr, TableConstrElem}; -use super::NEST_DEPTH; - impl<'a, D> Pretty<'a, D> for TableConstrElem where D: DocAllocator<'a>, @@ -35,16 +33,12 @@ where D::Doc: Clone, { fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> { - self.elems - .pretty( - allocator, - |e| allocator.line().append(e.pretty(allocator)), - |(s0, s1)| allocator.text(","), - |s| allocator.text(","), - ) - .nest(NEST_DEPTH) - .append(allocator.line()) - .braces() - .group() + self.0.pretty( + allocator, + allocator.text("{"), + allocator.text("}"), + allocator.text(","), + |e| e.pretty(allocator), + ) } }