From 198f56226eac52c1e7610831360add869f627921 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 21 Nov 2022 23:57:58 +0100 Subject: [PATCH] Switch TablePattern to BoundedSeparated --- src/ast/table_destr.rs | 11 +++-------- src/parser/table_destr.rs | 26 ++++++++++---------------- src/pretty/table_destr.rs | 26 ++++++++++++-------------- 3 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/ast/table_destr.rs b/src/ast/table_destr.rs index eb88dcb..6b24d8f 100644 --- a/src/ast/table_destr.rs +++ b/src/ast/table_destr.rs @@ -1,6 +1,6 @@ use crate::span::{HasSpan, Span}; -use super::{Expr, Ident, Separated, Space}; +use super::{BoundedSeparated, Expr, Ident, Space}; // TODO Make table patterns recursive @@ -34,16 +34,11 @@ impl HasSpan for TablePatternElem { /// /// Structure: `{ s0 elems s1 }` #[derive(Debug, Clone)] -pub struct TablePattern { - pub s0: Space, - pub elems: Separated, - pub s1: Space, - pub span: Span, -} +pub struct TablePattern(pub BoundedSeparated); impl HasSpan for TablePattern { fn span(&self) -> Span { - self.span + self.0.span() } } diff --git a/src/parser/table_destr.rs b/src/parser/table_destr.rs index 4fb5eda..0504513 100644 --- a/src/parser/table_destr.rs +++ b/src/parser/table_destr.rs @@ -4,7 +4,7 @@ use chumsky::prelude::*; use crate::ast::{Expr, Ident, Space, TableDestr, TablePattern, TablePatternElem}; -use super::basic::{separated_by, EParser, Error}; +use super::basic::{bounded_separated, EParser, Error}; fn table_pattern_elem( space: EParser, @@ -31,21 +31,15 @@ fn table_pattern_elem( pub fn table_pattern(space: EParser, ident: EParser) -> EParser { let elem = table_pattern_elem(space.clone(), ident); - 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| TablePattern { - s0, - elems, - s1, - span, - }) - .boxed() + bounded_separated( + space, + just('{').to(()), + just('}').to(()), + just(',').to(()), + elem, + ) + .map(TablePattern) + .boxed() } pub fn table_destr( diff --git a/src/pretty/table_destr.rs b/src/pretty/table_destr.rs index 795bfa2..777d0ae 100644 --- a/src/pretty/table_destr.rs +++ b/src/pretty/table_destr.rs @@ -2,8 +2,6 @@ use pretty::{DocAllocator, DocBuilder, Pretty}; use crate::ast::{TableDestr, TablePattern, TablePatternElem}; -use super::NEST_DEPTH; - impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePatternElem { fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> { match self { @@ -22,19 +20,19 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePatternElem { } } -impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePattern { +impl<'a, D> Pretty<'a, D> for TablePattern +where + D: DocAllocator<'a>, + 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), + ) } }