Skip to main content

rustia/
source_error.rs

1//! Source failures without parser or compiler implementation types.
2
3use crate::SourceSpan;
4use std::fmt;
5
6/// Source text that could not be tokenized or parsed.
7#[derive(Clone, Debug, Eq, PartialEq)]
8#[non_exhaustive]
9pub enum SourceError {
10    /// A token, string, or number could not be read.
11    Lex(LexError),
12    /// The tokens do not form a valid script.
13    Parse(ParseError),
14}
15
16/// A failure while reading a source token.
17#[derive(Clone, Debug, Eq, PartialEq)]
18#[non_exhaustive]
19pub struct LexError {
20    /// Byte range containing the failure.
21    pub span: SourceSpan,
22    /// Original message, without a source prefix.
23    pub message: String,
24}
25
26/// A failure while parsing a script.
27#[derive(Clone, Debug, Eq, PartialEq)]
28#[non_exhaustive]
29pub struct ParseError {
30    /// The construct that still needs its closing keyword, when known.
31    pub unclosed_block: Option<Box<UnclosedBlock>>,
32    /// Byte range containing the failure.
33    pub span: SourceSpan,
34    /// Original message, without a source prefix.
35    pub message: String,
36}
37
38/// A source construct missing its closing keyword.
39#[derive(Clone, Debug, Eq, PartialEq)]
40#[non_exhaustive]
41pub struct UnclosedBlock {
42    /// Byte offset of the opening keyword.
43    pub start: usize,
44    /// The closing keyword expected by the parser.
45    pub expected: &'static str,
46}
47
48/// Parsed source that cannot be compiled.
49#[derive(Clone, Debug, Eq, PartialEq)]
50#[non_exhaustive]
51pub struct CompileError {
52    /// Byte range containing the failure, when known.
53    pub span: Option<SourceSpan>,
54    /// Original message, without a source prefix.
55    pub message: String,
56}
57
58impl SourceError {
59    pub(crate) fn from_core(error: rustia_parser::SourceError) -> Self {
60        match error {
61            rustia_parser::SourceError::Lex(error) => Self::Lex(LexError {
62                span: SourceSpan::from_lexer(error.span),
63                message: error.message,
64            }),
65            rustia_parser::SourceError::Parse(error) => Self::Parse(ParseError {
66                unclosed_block: error.unclosed_block.map(|block| {
67                    Box::new(UnclosedBlock {
68                        start: block.start,
69                        expected: block.expected,
70                    })
71                }),
72                span: SourceSpan::from_lexer(error.span),
73                message: error.message,
74            }),
75        }
76    }
77}
78
79impl CompileError {
80    pub(crate) fn from_core(error: rustia_compiler::CompileError) -> Self {
81        Self {
82            span: error.span.map(SourceSpan::from_lexer),
83            message: error.message,
84        }
85    }
86}
87
88impl fmt::Display for SourceError {
89    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
90        match self {
91            Self::Lex(error) => error.fmt(formatter),
92            Self::Parse(error) => error.fmt(formatter),
93        }
94    }
95}
96
97impl fmt::Display for LexError {
98    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
99        write!(
100            formatter,
101            "lex error at {}..{}: {}",
102            self.span.start, self.span.end, self.message
103        )
104    }
105}
106
107impl fmt::Display for ParseError {
108    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
109        write!(
110            formatter,
111            "parse error at {}..{}: {}",
112            self.span.start, self.span.end, self.message
113        )
114    }
115}
116
117impl fmt::Display for CompileError {
118    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
119        if let Some(span) = self.span {
120            write!(
121                formatter,
122                "compile error at {}..{}: {}",
123                span.start, span.end, self.message
124            )
125        } else {
126            write!(formatter, "compile error: {}", self.message)
127        }
128    }
129}
130
131impl std::error::Error for SourceError {}
132impl std::error::Error for LexError {}
133impl std::error::Error for ParseError {}
134impl std::error::Error for CompileError {}