use getset::Getters;
use crate::{
base::{
self,
source_file::{SourceElement, Span},
Handler, VoidHandler,
},
lexical::{
token::{Punctuation, Token},
token_stream::Delimiter,
},
syntax::parser::Reading,
};
use super::{error::ParseResult, parser::Parser};
pub mod condition;
pub mod declaration;
pub mod expression;
pub mod program;
pub mod statement;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Getters)]
pub struct ConnectedList<Element, Separator> {
#[get = "pub"]
first: Element,
#[get = "pub"]
rest: Vec<(Separator, Element)>,
#[get = "pub"]
trailing_separator: Option<Separator>,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DelimitedList<T> {
pub open: Punctuation,
pub list: Option<ConnectedList<T, Punctuation>>,
pub close: Punctuation,
}
impl<'a> Parser<'a> {
pub fn parse_enclosed_list<T>(
&mut self,
delimiter: Delimiter,
separator: char,
mut f: impl FnMut(&mut Self) -> ParseResult<T>,
handler: &impl Handler<base::Error>,
) -> ParseResult<DelimitedList<T>> {
fn skip_to_next_separator(this: &mut Parser, separator: char) -> Option<Punctuation> {
if let Reading::Atomic(Token::Punctuation(punc)) = this.stop_at(|token| {
matches!(
token, Reading::Atomic(Token::Punctuation(punc))
if punc.punctuation == separator
)
}) {
this.forward();
Some(punc)
} else {
None
}
}
let delimited_tree = self.step_into(
delimiter,
|parser| {
let mut first = None;
let mut rest = Vec::new();
let mut trailing_separator: Option<Punctuation> = None;
while !parser.is_exhausted() {
let Ok(element) = f(parser) else {
skip_to_next_separator(parser, separator);
continue;
};
match (&first, &trailing_separator) {
(None, None) => {
first = Some(element);
}
(Some(_), Some(separator)) => {
rest.push((separator.clone(), element));
trailing_separator = None;
}
_ => {
unreachable!()
}
}
if !parser.is_exhausted() {
let Ok(separator) = parser.parse_punctuation(separator, true, handler)
else {
if let Some(punctuation) = skip_to_next_separator(parser, separator) {
trailing_separator = Some(punctuation);
}
continue;
};
trailing_separator = Some(separator);
}
}
Ok(first.map(|first| ConnectedList {
first,
rest,
trailing_separator,
}))
},
handler,
)?;
Ok(DelimitedList {
open: delimited_tree.open,
list: delimited_tree.tree.unwrap(),
close: delimited_tree.close,
})
}
pub fn parse_connected_list<T>(
&mut self,
seperator: char,
mut f: impl FnMut(&mut Self) -> ParseResult<T>,
_handler: &impl Handler<base::Error>,
) -> ParseResult<ConnectedList<T, Punctuation>> {
let first = f(self)?;
let mut rest = Vec::new();
while let Ok(sep) =
self.try_parse(|parser| parser.parse_punctuation(seperator, true, &VoidHandler))
{
if let Ok(element) = self.try_parse(&mut f) {
rest.push((sep, element));
} else {
return Ok(ConnectedList {
first,
rest,
trailing_separator: Some(sep),
});
}
}
Ok(ConnectedList {
first,
rest,
trailing_separator: None,
})
}
}
impl<Element: SourceElement, Separator: SourceElement> SourceElement
for ConnectedList<Element, Separator>
{
fn span(&self) -> Span {
let end = self.trailing_separator.as_ref().map_or_else(
|| {
self.rest
.last()
.map_or_else(|| self.first.span(), |(_, element)| element.span())
},
SourceElement::span,
);
self.first.span().join(&end).unwrap()
}
}
impl<Element, Separator> ConnectedList<Element, Separator> {
pub fn elements(&self) -> impl Iterator<Item = &Element> {
std::iter::once(&self.first).chain(self.rest.iter().map(|(_, element)| element))
}
pub fn into_elements(self) -> impl Iterator<Item = Element> {
std::iter::once(self.first).chain(self.rest.into_iter().map(|(_, element)| element))
}
pub fn len(&self) -> usize {
self.rest.len() + 1
}
pub fn is_empty(&self) -> bool {
false
}
}