Remove Separated
This commit is contained in:
parent
198f56226e
commit
94ea196933
4 changed files with 3 additions and 117 deletions
|
|
@ -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)]
|
||||
pub struct BoundedSeparated<E> {
|
||||
pub elems: Vec<(Space, E, Space)>,
|
||||
|
|
|
|||
|
|
@ -1,39 +1,4 @@
|
|||
use crate::ast::{BoundedSeparated, Separated};
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::ast::BoundedSeparated;
|
||||
|
||||
impl<E> BoundedSeparated<E> {
|
||||
pub fn desugar(self, desugar_elem: impl Fn(E) -> (E, bool)) -> (Self, bool) {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
use chumsky::prelude::*;
|
||||
use chumsky::text::Character;
|
||||
|
||||
use crate::ast::{BoundedSeparated, Ident, Line, Separated, Space};
|
||||
use crate::ast::{BoundedSeparated, Ident, Line, Space};
|
||||
use crate::span::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
|
||||
// code nicer, I have decided that the rules specified in the `parser` module
|
||||
// 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>(
|
||||
space: impl Parser<char, Space, Error = Error> + Clone + 'static,
|
||||
start: impl Parser<char, (), Error = Error> + 'static,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use pretty::{DocAllocator, DocBuilder, Pretty};
|
||||
|
||||
use crate::ast::{BoundedSeparated, Ident, Separated};
|
||||
use crate::ast::{BoundedSeparated, Ident};
|
||||
|
||||
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> {
|
||||
pub fn pretty<'a, D, FE>(
|
||||
self,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue