Remove Separated

This commit is contained in:
Joscha 2022-11-22 00:00:43 +01:00
parent 198f56226e
commit 94ea196933
4 changed files with 3 additions and 117 deletions

View file

@ -77,26 +77,6 @@ impl HasSpan for Ident {
} }
} }
#[derive(Debug, Clone)]
pub enum Separated<E, S1, S2> {
Empty(Span),
NonEmpty {
first_elem: E,
last_elems: Vec<(S1, E)>,
trailing: Option<S2>,
span: Span,
},
}
impl<E, S1, S2> HasSpan for Separated<E, S1, S2> {
fn span(&self) -> Span {
match self {
Self::Empty(span) => *span,
Self::NonEmpty { span, .. } => *span,
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BoundedSeparated<E> { pub struct BoundedSeparated<E> {
pub elems: Vec<(Space, E, Space)>, pub elems: Vec<(Space, E, Space)>,

View file

@ -1,39 +1,4 @@
use crate::ast::{BoundedSeparated, Separated}; use crate::ast::BoundedSeparated;
impl<E, S1, S2> Separated<E, S1, S2> {
pub fn desugar_elem(self, desugar_elem: impl Fn(E) -> (E, bool)) -> (Self, bool) {
match self {
Self::Empty(span) => (Self::Empty(span), false),
Self::NonEmpty {
first_elem,
last_elems,
trailing,
span,
} => {
let (new_first_elem, mut desugared) = desugar_elem(first_elem);
let mut new_last_elems = vec![];
for (separator, elem) in last_elems {
if desugared {
new_last_elems.push((separator, elem));
} else {
let (elem, elem_desugared) = desugar_elem(elem);
desugared = desugared || elem_desugared;
new_last_elems.push((separator, elem));
}
}
let new = Self::NonEmpty {
first_elem: new_first_elem,
last_elems: new_last_elems,
trailing,
span,
};
(new, desugared)
}
}
}
}
impl<E> BoundedSeparated<E> { impl<E> BoundedSeparated<E> {
pub fn desugar(self, desugar_elem: impl Fn(E) -> (E, bool)) -> (Self, bool) { pub fn desugar(self, desugar_elem: impl Fn(E) -> (E, bool)) -> (Self, bool) {

View file

@ -3,7 +3,7 @@
use chumsky::prelude::*; use chumsky::prelude::*;
use chumsky::text::Character; use chumsky::text::Character;
use crate::ast::{BoundedSeparated, Ident, Line, Separated, Space}; use crate::ast::{BoundedSeparated, Ident, Line, Space};
use crate::span::Span; use crate::span::Span;
pub type Error = Simple<char, Span>; pub type Error = Simple<char, Span>;
@ -62,27 +62,6 @@ pub fn local(space: EParser<Space>) -> EParser<Option<Space>> {
// This function is more of a utility function. Because of this and to keep the // This function is more of a utility function. Because of this and to keep the
// code nicer, I have decided that the rules specified in the `parser` module // code nicer, I have decided that the rules specified in the `parser` module
// don't apply to it. // don't apply to it.
pub fn separated_by<E: 'static, S1: 'static, S2: 'static>(
elem: impl Parser<char, E, Error = Error> + Clone + 'static,
separator: impl Parser<char, S1, Error = Error> + 'static,
trailing_separator: impl Parser<char, S2, Error = Error> + 'static,
) -> EParser<Separated<E, S1, S2>> {
elem.clone()
.then(separator.then(elem).repeated())
.then(trailing_separator.or_not())
.or_not()
.map_with_span(|s, span| match s {
Some(((first_elem, last_elems), trailing)) => Separated::NonEmpty {
first_elem,
last_elems,
trailing,
span,
},
None => Separated::Empty(span),
})
.boxed()
}
pub fn bounded_separated<E: 'static>( pub fn bounded_separated<E: 'static>(
space: impl Parser<char, Space, Error = Error> + Clone + 'static, space: impl Parser<char, Space, Error = Error> + Clone + 'static,
start: impl Parser<char, (), Error = Error> + 'static, start: impl Parser<char, (), Error = Error> + 'static,

View file

@ -1,6 +1,6 @@
use pretty::{DocAllocator, DocBuilder, Pretty}; use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::{BoundedSeparated, Ident, Separated}; use crate::ast::{BoundedSeparated, Ident};
use super::NEST_DEPTH; use super::NEST_DEPTH;
@ -10,44 +10,6 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Ident {
} }
} }
impl<E, S1, S2> Separated<E, S1, S2> {
pub fn pretty<'a, D, FE, FS1, FS2>(
self,
allocator: &'a D,
elem_to_doc: FE,
separator_to_doc: FS1,
trailing_separator_to_doc: FS2,
) -> DocBuilder<'a, D>
where
D: DocAllocator<'a>,
FE: Fn(E) -> DocBuilder<'a, D>,
FS1: Fn(S1) -> DocBuilder<'a, D>,
FS2: Fn(S2) -> DocBuilder<'a, D>,
{
match self {
Self::Empty(_) => allocator.nil(),
Self::NonEmpty {
first_elem,
last_elems,
trailing,
span: _span,
} => elem_to_doc(first_elem)
.append(
allocator.concat(
last_elems
.into_iter()
.map(|(s, e)| separator_to_doc(s).append(elem_to_doc(e))),
),
)
.append(
trailing
.map(trailing_separator_to_doc)
.unwrap_or_else(|| allocator.nil()),
),
}
}
}
impl<E> BoundedSeparated<E> { impl<E> BoundedSeparated<E> {
pub fn pretty<'a, D, FE>( pub fn pretty<'a, D, FE>(
self, self,