dampen_core/parser/
error.rs1use crate::ir::span::Span;
2use proc_macro2::TokenStream;
3use quote::quote;
4
5#[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}
24
25impl std::fmt::Display for ParseError {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 write!(
28 f,
29 "error[{}]: {} at line {}, column {}",
30 self.kind as u8, self.message, self.span.line, self.span.column
31 )?;
32 if let Some(suggestion) = &self.suggestion {
33 write!(f, "\n help: {}", suggestion)?;
34 }
35 Ok(())
36 }
37}
38
39impl ParseError {
40 pub fn to_compile_error(&self) -> TokenStream {
49 let message = format!(
50 "Dampen parsing error: {}\n at line {}, column {}",
51 self.message, self.span.line, self.span.column
52 );
53
54 let mut tokens = quote! {
55 compile_error!(#message);
56 };
57
58 if let Some(ref suggestion) = self.suggestion {
59 let help = format!("help: {}", suggestion);
60 tokens.extend(quote! {
61 compile_error!(#help);
62 });
63 }
64
65 tokens
66 }
67}