dampen_core/parser/
error.rs

1use crate::ir::span::Span;
2use proc_macro2::TokenStream;
3use quote::quote;
4
5/// Error during parsing
6#[derive(Debug, Clone, PartialEq)]
7pub struct ParseError {
8    pub kind: ParseErrorKind,
9    pub message: String,
10    pub span: Span,
11    pub suggestion: Option<String>,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum ParseErrorKind {
16    XmlSyntax,
17    UnknownWidget,
18    UnknownAttribute,
19    InvalidValue,
20    InvalidExpression,
21    UnclosedBinding,
22    MissingAttribute,
23    /// Schema version is newer than the framework supports
24    UnsupportedVersion,
25    /// Attribute is deprecated in favor of a standardized alternative
26    DeprecatedAttribute,
27}
28
29impl std::fmt::Display for ParseError {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(
32            f,
33            "error[{}]: {} at line {}, column {}",
34            self.kind as u8, self.message, self.span.line, self.span.column
35        )?;
36        if let Some(suggestion) = &self.suggestion {
37            write!(f, "\n  help: {}", suggestion)?;
38        }
39        Ok(())
40    }
41}
42
43impl ParseError {
44    /// Convert this error into a compile_error! macro invocation.
45    ///
46    /// This is used by procedural macros to emit compile-time errors
47    /// with proper location information and helpful suggestions.
48    ///
49    /// # Returns
50    ///
51    /// A `TokenStream` containing a `compile_error!` macro invocation.
52    pub fn to_compile_error(&self) -> TokenStream {
53        let message = format!(
54            "Dampen parsing error: {}\n  at line {}, column {}",
55            self.message, self.span.line, self.span.column
56        );
57
58        let mut tokens = quote! {
59            compile_error!(#message);
60        };
61
62        if let Some(ref suggestion) = self.suggestion {
63            let help = format!("help: {}", suggestion);
64            tokens.extend(quote! {
65                compile_error!(#help);
66            });
67        }
68
69        tokens
70    }
71}