Switch TablePattern to BoundedSeparated

This commit is contained in:
Joscha 2022-11-21 23:57:58 +01:00
parent 0e9cfd67c2
commit 198f56226e
3 changed files with 25 additions and 38 deletions

View file

@ -1,6 +1,6 @@
use crate::span::{HasSpan, Span}; use crate::span::{HasSpan, Span};
use super::{Expr, Ident, Separated, Space}; use super::{BoundedSeparated, Expr, Ident, Space};
// TODO Make table patterns recursive // TODO Make table patterns recursive
@ -34,16 +34,11 @@ impl HasSpan for TablePatternElem {
/// ///
/// Structure: `{ s0 elems s1 }` /// Structure: `{ s0 elems s1 }`
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TablePattern { pub struct TablePattern(pub BoundedSeparated<TablePatternElem>);
pub s0: Space,
pub elems: Separated<TablePatternElem, (Space, Space), Space>,
pub s1: Space,
pub span: Span,
}
impl HasSpan for TablePattern { impl HasSpan for TablePattern {
fn span(&self) -> Span { fn span(&self) -> Span {
self.span self.0.span()
} }
} }

View file

@ -4,7 +4,7 @@ use chumsky::prelude::*;
use crate::ast::{Expr, Ident, Space, TableDestr, TablePattern, TablePatternElem}; 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( fn table_pattern_elem(
space: EParser<Space>, space: EParser<Space>,
@ -31,21 +31,15 @@ fn table_pattern_elem(
pub fn table_pattern(space: EParser<Space>, ident: EParser<Ident>) -> EParser<TablePattern> { pub fn table_pattern(space: EParser<Space>, ident: EParser<Ident>) -> EParser<TablePattern> {
let elem = table_pattern_elem(space.clone(), ident); let elem = table_pattern_elem(space.clone(), ident);
let separator = space.clone().then_ignore(just(',')).then(space.clone()); bounded_separated(
let trailing_separator = space.clone().then_ignore(just(',')); space,
just('{').to(()),
space just('}').to(()),
.clone() just(',').to(()),
.then(separated_by(elem, separator, trailing_separator)) elem,
.then(space) )
.delimited_by(just('{'), just('}')) .map(TablePattern)
.map_with_span(|((s0, elems), s1), span| TablePattern { .boxed()
s0,
elems,
s1,
span,
})
.boxed()
} }
pub fn table_destr( pub fn table_destr(

View file

@ -2,8 +2,6 @@ use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::{TableDestr, TablePattern, TablePatternElem}; use crate::ast::{TableDestr, TablePattern, TablePatternElem};
use super::NEST_DEPTH;
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePatternElem { impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for TablePatternElem {
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> { fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
match self { 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> { fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
self.elems self.0.pretty(
.pretty( allocator,
allocator, allocator.text("{"),
|e| allocator.line().append(e.pretty(allocator)), allocator.text("}"),
|(s0, s1)| allocator.text(","), allocator.text(","),
|s| allocator.text(","), |e| e.pretty(allocator),
) )
.nest(NEST_DEPTH)
.append(allocator.line())
.braces()
.group()
} }
} }