use std::fmt;
use crate::cst::{TerminalKind, TextRange};
#[cfg(feature = "__private_ariadne_errors")]
use crate::diagnostic::{self, Diagnostic};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ParseError {
text_range: TextRange,
terminals_that_would_have_allowed_more_progress: Vec<TerminalKind>,
}
impl ParseError {
pub fn text_range(&self) -> &TextRange {
&self.text_range
}
pub fn message(&self) -> String {
self.to_string()
}
}
impl ParseError {
pub(crate) fn create(
text_range: TextRange,
mut terminals_that_would_have_allowed_more_progress: Vec<TerminalKind>,
) -> Self {
terminals_that_would_have_allowed_more_progress.sort_unstable();
terminals_that_would_have_allowed_more_progress.dedup();
Self {
text_range,
terminals_that_would_have_allowed_more_progress,
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self
.terminals_that_would_have_allowed_more_progress
.is_empty()
{
return write!(f, "Expected end of file.");
}
let mut expected = self.terminals_that_would_have_allowed_more_progress.iter();
let first = expected
.next()
.expect("we just checked that it's not empty");
write!(f, "Expected {first}")?;
for kind in expected {
write!(f, " or {kind}")?;
}
write!(f, ".")
}
}
#[cfg(feature = "__private_ariadne_errors")]
impl Diagnostic for ParseError {
fn text_range(&self) -> TextRange {
self.text_range.clone()
}
fn severity(&self) -> diagnostic::Severity {
diagnostic::Severity::Error
}
fn message(&self) -> String {
self.to_string()
}
}