shulkerscript 0.1.0

Shulkerscript language implementation with compiler
Documentation
use std::fmt::Display;

use getset::Getters;

use crate::base::{
    log::{Message, Severity, SourceCodeDisplay},
    source_file::Span,
};

use super::{token, token_stream::Delimiter};

/// Represents an error that occurred during the lexical analysis of the source code.
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    UnterminatedDelimitedComment(#[from] UnterminatedDelimitedComment),
    #[error(transparent)]
    UndelimitedDelimiter(#[from] UndelimitedDelimiter),
    #[error("Tokenize error: {0}")]
    TokenizeError(#[from] token::TokenizeError),
}

/// Source code contains an unclosed `/*` comment.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Getters, thiserror::Error)]
pub struct UnterminatedDelimitedComment {
    /// Span of the unclosed `/*` that starts the comment.
    pub span: Span,
}

impl Display for UnterminatedDelimitedComment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}\n{}",
            Message::new(Severity::Error, "found an unclosed `/*` comment"),
            SourceCodeDisplay::new(&self.span, Option::<i32>::None)
        )
    }
}

/// Delimiter is not closed by its corresponding closing pair.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Getters, thiserror::Error)]
pub struct UndelimitedDelimiter {
    /// Span of the opening delimiter.
    pub opening_span: Span,

    /// Kind of the delimiter.
    pub delimiter: Delimiter,
}

impl Display for UndelimitedDelimiter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}\n{}",
            Message::new(Severity::Error, "found an undelimited delimiter"),
            SourceCodeDisplay::new(
                &self.opening_span,
                Some("this delimiter is not closed by its corresponding closing pair")
            )
        )
    }
}