use crate::{PrettyPrintSExpr, Spanned}; use pretty::DocAllocator; use trilogy_scanner::Token; /// A punctuated list of nodes, separated by a punctuation token. /// The list may have an optional trailing punctuator. #[derive(Clone, Debug)] pub struct Punctuated { pub elements: Vec<(T, Token)>, pub last: Option, } impl<'a, T: PrettyPrintSExpr<'a>> PrettyPrintSExpr<'a> for Punctuated { fn pretty_print_sexpr(&self, printer: &'a crate::PrettyPrinter) -> crate::PrettyPrinted<'a> { printer .intersperse( self.iter().map(|node| node.pretty_print_sexpr(printer)), printer.line(), ) .nest(2) .group() .brackets() } } impl Default for Punctuated { fn default() -> Self { Self::new() } } impl Spanned for Punctuated { fn span(&self) -> source_span::Span { self.elements .first() .map(|(item, _)| item.span()) .map(|first| { self.last .as_ref() .map_or_else(|| self.elements.last().unwrap().1.span(), Spanned::span) .union(first) }) .unwrap_or_default() } } impl Punctuated { pub(crate) const fn new() -> Self { Self { elements: vec![], last: None, } } pub(crate) fn init(item: T) -> Self { Self { elements: vec![], last: Some(item), } } pub(crate) fn follow(&mut self, punctuation: Token, item: T) { self.elements .push((self.last.replace(item).unwrap(), punctuation)); } pub(crate) fn push(&mut self, item: T, punctuation: Token) { self.elements.push((item, punctuation)); } pub(crate) fn push_last(&mut self, last: T) { self.last = Some(last); } pub(crate) fn finish(&mut self, last: Token) { self.elements.push((self.last.take().unwrap(), last)); } /// An iterator over the useful elements in this punctuated list. pub fn iter(&self) -> impl Iterator { self.into_iter() } } pub struct PunctuatedIterator { elements: as IntoIterator>::IntoIter, last: Option, } impl IntoIterator for Punctuated { type Item = T; type IntoIter = PunctuatedIterator; fn into_iter(self) -> PunctuatedIterator { PunctuatedIterator { elements: self.elements.into_iter(), last: self.last, } } } impl Iterator for PunctuatedIterator { type Item = T; fn next(&mut self) -> Option { self.elements .next() .map(|(t, _)| t) .or_else(|| self.last.take()) } } pub struct PunctuatedIter<'a, T> { elements: <&'a Vec<(T, Token)> as IntoIterator>::IntoIter, last: Option<&'a T>, } impl<'a, T> IntoIterator for &'a Punctuated { type Item = &'a T; type IntoIter = PunctuatedIter<'a, T>; fn into_iter(self) -> PunctuatedIter<'a, T> { PunctuatedIter { elements: self.elements.iter(), last: self.last.as_ref(), } } } impl<'a, T> Iterator for PunctuatedIter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option { self.elements .next() .map(|(t, _)| t) .or_else(|| self.last.take()) } }