use crate::sources::span::Span;
use itertools::Itertools;
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone)]
pub struct ParsePairSort<'src> {
pub sort: &'src str,
pub constructor_name: &'src str,
pub constructor_value: ParsePairExpression<'src>,
}
impl ParsePairSort<'_> {
pub fn span(&self) -> Span {
self.constructor_value.span()
}
}
impl<'src> Display for ParsePairSort<'src> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({})", self.constructor_name, self.constructor_value)
}
}
#[derive(Debug, Clone)]
pub enum ParsePairExpression<'src> {
Sort(Span, Box<ParsePairSort<'src>>),
List(Span, Vec<ParsePairExpression<'src>>),
Choice(Span, usize, Box<ParsePairExpression<'src>>),
Empty(Span),
Error(Span),
}
impl<'src> ParsePairExpression<'src> {
pub fn span(&self) -> Span {
match self {
ParsePairExpression::Sort(span, _) => span,
ParsePairExpression::List(span, _) => span,
ParsePairExpression::Choice(span, _, _) => span,
ParsePairExpression::Empty(span) => span,
ParsePairExpression::Error(span) => span,
}
.clone()
}
}
impl<'src> Display for ParsePairExpression<'src> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ParsePairExpression::Sort(_, sort) => {
write!(f, "{}", sort)
}
ParsePairExpression::List(_, exprs) => {
write!(f, "[{}]", exprs.iter().map(|e| e.to_string()).join(", "))
}
ParsePairExpression::Choice(_, num, child) => {
write!(f, "{}:{}", num, child)
}
ParsePairExpression::Empty(_) => {
write!(f, "_")
}
ParsePairExpression::Error(_) => {
write!(f, "#")
}
}
}
}