Add BoundedSeparated with parser and printer

This type should be able to replace Separated (with a bit of effort).

The hope is that this type is a better representation for table-like
syntax with optional trailing delimiter that will let me remove_map
elements without too much difficulty. This is necessary for desugaring
table constructors.
This commit is contained in:
Joscha 2022-11-21 22:49:40 +01:00
parent 9591b23082
commit a1867fdc4e
3 changed files with 86 additions and 2 deletions

View file

@ -1,6 +1,8 @@
use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::{Ident, Separated};
use crate::ast::{BoundedSeparated, Ident, Separated};
use super::NEST_DEPTH;
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Ident {
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
@ -45,3 +47,32 @@ impl<E, S1, S2> Separated<E, S1, S2> {
}
}
}
impl<E> BoundedSeparated<E> {
pub fn pretty<'a, D, FE>(
self,
allocator: &'a D,
start: DocBuilder<'a, D>,
end: DocBuilder<'a, D>,
separator: DocBuilder<'a, D>,
elem_pretty: FE,
) -> DocBuilder<'a, D>
where
D: DocAllocator<'a>,
D::Doc: Clone,
FE: Fn(E) -> DocBuilder<'a, D>,
{
allocator
.intersperse(
self.elems
.into_iter()
.map(|(s0, elem, s1)| allocator.line().append(elem_pretty(elem))),
separator.clone(),
)
.append(self.trailing.map(|s| separator))
.nest(NEST_DEPTH)
.append(allocator.line())
.enclose(start, end)
.group()
}
}