mxmlextrema_as3parser/diagnostics/
diagnostics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::any::Any;

use hydroperfox_filepaths::FlexPath;
use maplit::hashmap;
use crate::ns::*;

#[path = "diagnostics_english_resources.rs"]
mod diagnostics_english_resources;

/// Represents a diagnostic originated from a compilation unit.
/// 
/// Arguments are formatted using integer keys counted from 1 (one).
#[derive(Clone)]
pub struct Diagnostic {
    pub(crate) location: Location,
    pub(crate) kind: DiagnosticKind,
    pub(crate) is_warning: bool,
    pub(crate) is_verify_error: bool,
    pub(crate) arguments: Vec<Rc<dyn DiagnosticArgument>>,
    pub(crate) custom_kind: RefCell<Option<Rc<dyn Any>>>,
}

impl Eq for Diagnostic {}

impl PartialEq for Diagnostic {
    fn eq(&self, other: &Self) -> bool {
        self.location == other.location &&
        self.kind == other.kind
    }
}

impl Ord for Diagnostic {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.location.cmp(&other.location)
    }
}

impl PartialOrd for Diagnostic {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.location.partial_cmp(&other.location)
    }
}

impl Diagnostic {
    pub fn new_syntax_error(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
        Self {
            location: location.clone(),
            kind,
            is_verify_error: false,
            is_warning: false,
            arguments,
            custom_kind: RefCell::new(None),
        }
    }

    pub fn new_verify_error(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
        Self {
            location: location.clone(),
            kind,
            is_verify_error: true,
            is_warning: false,
            arguments,
            custom_kind: RefCell::new(None),
        }
    }

    pub fn new_warning(location: &Location, kind: DiagnosticKind, arguments: Vec<Rc<dyn DiagnosticArgument>>) -> Self {
        Self {
            location: location.clone(),
            kind,
            is_verify_error: false,
            is_warning: true,
            arguments,
            custom_kind: RefCell::new(None),
        }
    }

    pub fn location(&self) -> Location {
        self.location.clone()
    }

    pub fn kind(&self) -> DiagnosticKind {
        self.kind.clone()
    }

    pub fn is_warning(&self) -> bool {
        self.is_warning
    }

    pub fn is_error(&self) -> bool {
        !self.is_warning
    }

    pub fn is_syntax_error(&self) -> bool {
        !self.is_verify_error && !self.is_warning
    }

    pub fn is_verify_error(&self) -> bool {
        self.is_verify_error
    }

    pub fn arguments(&self) -> Vec<Rc<dyn DiagnosticArgument>> {
        self.arguments.clone()
    }

    pub fn id(&self) -> i32 {
        self.kind.id()
    }

    pub fn custom_kind(&self) -> Option<Rc<dyn Any>> {
        self.custom_kind.borrow().clone()
    }

    pub fn set_custom_kind(&self, id: Option<Rc<dyn Any>>) {
        self.custom_kind.replace(id);
    }

    /// Formats the diagnostic by overriding the message text.
    pub fn format_with_message(&self, message: &str, id: Option<i32>) -> String {
        self.format_with_message_and_base_path(message, id, None)
    }

    /// Formats the diagnostic by overriding the message text and providing a base Whack package's path
    /// (to relativize the source path).
    pub fn format_with_message_and_base_path(&self, message: &str, id: Option<i32>, base_path: Option<&str>) -> String {
        let category = (if self.is_verify_error {
            "Verify error"
        } else if self.is_warning {
            "Warning"
        } else {
            "Syntax error"
        }).to_owned();

        let mut file_path = self.location.compilation_unit.file_path.clone().map_or("".to_owned(), |s| format!("{s}:"));
        if let Some(base_path) = base_path {
            file_path = FlexPath::new_native(base_path).relative(&file_path).to_owned();
        }
        if file_path.starts_with(r"\\?\") {
            file_path = file_path[4..].to_owned();
        }
        let line = self.location.first_line_number();
        let column = self.location.first_column() + 1;
        if let Some(id) = id {
            format!("{file_path}{line}:{column}: {category} #{}: {message}", id.to_string())
        } else {
            format!("{file_path}{line}:{column}: {category}: {message}")
        }
    }

    /// Formats the diagnostic in English.
    pub fn format_english(&self) -> String {
        self.format_with_message(&self.format_message_english(), Some(self.id()))
    }

    /// Formats the diagnostic in English.
    pub fn format_english_with_base_path(&self, base_path: &str) -> String {
        self.format_with_message_and_base_path(&self.format_message_english(), Some(self.id()), Some(base_path))
    }

    pub fn format_message_english(&self) -> String {
        self.format_message(&diagnostics_english_resources::DATA)
    }

    pub fn format_message(&self, messages: &HashMap<i32, String>) -> String {
        let mut string_arguments: HashMap<String, String> = hashmap!{};
        let mut i = 1;
        for argument in &self.arguments {
            string_arguments.insert(i.to_string(), argument.to_string());
            i += 1;
        }
        use hydroperfox_lateformat::LateFormat;
        let Some(msg) = messages.get(&self.id()) else {
            let id = self.id();
            panic!("Message resource is missing for ID {id}");
        };
        msg.hydroperfox_lateformat(string_arguments)
    }
}

/// The `diagarg![...]` literal is used for initializing
/// diagnostic arguments.
/// 
/// For example: `diagarg![token, "foo".to_owned()]`.
pub macro diagarg {
    ($($value:expr),*) => { vec![ $(Rc::new($value)),* ] },
}

pub trait DiagnosticArgument: Any + ToString + 'static {
}

impl DiagnosticArgument for String {}

impl DiagnosticArgument for Token {}