Switch TablePattern to BoundedSeparated
This commit is contained in:
parent
0e9cfd67c2
commit
198f56226e
3 changed files with 25 additions and 38 deletions
|
|
@ -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<TablePatternElem, (Space, Space), Space>,
|
||||
pub s1: Space,
|
||||
pub span: Span,
|
||||
}
|
||||
pub struct TablePattern(pub BoundedSeparated<TablePatternElem>);
|
||||
|
||||
impl HasSpan for TablePattern {
|
||||
fn span(&self) -> Span {
|
||||
self.span
|
||||
self.0.span()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Space>,
|
||||
|
|
@ -31,20 +31,14 @@ fn table_pattern_elem(
|
|||
|
||||
pub fn table_pattern(space: EParser<Space>, ident: EParser<Ident>) -> EParser<TablePattern> {
|
||||
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,
|
||||
})
|
||||
bounded_separated(
|
||||
space,
|
||||
just('{').to(()),
|
||||
just('}').to(()),
|
||||
just(',').to(()),
|
||||
elem,
|
||||
)
|
||||
.map(TablePattern)
|
||||
.boxed()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
self.0.pretty(
|
||||
allocator,
|
||||
|e| allocator.line().append(e.pretty(allocator)),
|
||||
|(s0, s1)| allocator.text(","),
|
||||
|s| allocator.text(","),
|
||||
allocator.text("{"),
|
||||
allocator.text("}"),
|
||||
allocator.text(","),
|
||||
|e| e.pretty(allocator),
|
||||
)
|
||||
.nest(NEST_DEPTH)
|
||||
.append(allocator.line())
|
||||
.braces()
|
||||
.group()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue