mortar_compiler 0.5.2

Mortar language compiler core library
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! # diagnostics.rs
//!
//! # diagnostics.rs 文件
//!
//! ## Module Overview
//!
//! ## 模块概述
//!
//! Provides the diagnostic system for reporting errors and warnings during compilation.
//!
//! 提供用于在编译期间报告错误和警告的诊断系统。
//!
//! It handles error collection, formatting, and semantic analysis checks (e.g., type checking, unused variables).
//!
//! 它处理错误收集、格式化和语义分析检查(例如类型检查、未使用的变量)。
//!
//! ## Source File Overview
//!
//! ## 源文件概述
//!
//! Defines `Diagnostic`, `DiagnosticCollector`, and various analysis methods to validate the AST.
//!
//! 定义了 `Diagnostic`、`DiagnosticCollector` 以及用于验证 AST 的各种分析方法。

use crate::Language;
use crate::ast::{
    Arg, ChoiceDest, ChoiceItem, EventAction, FuncCall, FunctionDecl, NodeDef, NodeJump, NodeStmt,
    Program, TopLevel,
};
use std::collections::{HashMap, HashSet};

mod analysis_helpers;
mod presentation;

use presentation::{format_message, get_text};

#[derive(Debug, Clone)]
pub enum Severity {
    Error,
    Warning,
}

#[derive(Debug, Clone)]
pub enum DiagnosticKind {
    // Errors
    NodeNotFound {
        node_name: String,
    },
    FunctionNotFound {
        function_name: String,
    },
    SyntaxError {
        message: String,
    },
    TypeError {
        message: String,
    },
    ArgumentCountMismatch {
        function_name: String,
        expected: usize,
        actual: usize,
    },
    ArgumentTypeMismatch {
        function_name: String,
        parameter: String,
        expected: String,
        actual: String,
    },
    ConditionTypeMismatch {
        expected: String,
        actual: String,
    },

    // Warnings
    NonSnakeCaseFunction {
        function_name: String,
    },
    NonPascalCaseNode {
        node_name: String,
    },
    UnusedFunction {
        function_name: String,
    },
}

#[derive(Debug, Clone)]
pub struct Diagnostic {
    pub kind: DiagnosticKind,
    pub severity: Severity,
    pub span: Option<(usize, usize)>, // (start, end) byte positions
    pub message: String,
}

pub struct DiagnosticCollector {
    diagnostics: Vec<Diagnostic>,
    file_name: String,
    language: Language,
}

impl DiagnosticCollector {
    pub fn new(file_name: String) -> Self {
        Self {
            diagnostics: Vec::new(),
            file_name,
            language: Language::English,
        }
    }

    pub fn new_with_language(file_name: String, language: Language) -> Self {
        Self {
            diagnostics: Vec::new(),
            file_name,
            language,
        }
    }

    pub fn add_diagnostic(&mut self, diagnostic: Diagnostic) {
        self.diagnostics.push(diagnostic);
    }

    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| matches!(d.severity, Severity::Error))
    }

    pub fn get_diagnostics(&self) -> &Vec<Diagnostic> {
        &self.diagnostics
    }

    pub fn analyze_program(&mut self, program: &Program) {
        // Collect all function declarations and nodes
        let mut declared_functions = HashMap::new();
        let mut declared_nodes = HashMap::new();
        let mut used_functions = HashSet::new();
        let mut used_nodes = HashSet::new();

        // First pass: collect declarations
        for item in &program.body {
            match item {
                TopLevel::FunctionDecl(func) => {
                    self.check_snake_case_naming(&func.name, func.name_span);
                    declared_functions.insert(func.name.clone(), func);
                }
                TopLevel::NodeDef(node) => {
                    self.check_pascal_case_naming(&node.name, node.name_span);
                    declared_nodes.insert(node.name.clone(), node);
                }
                TopLevel::VarDecl(_) | TopLevel::ConstDecl(_) | TopLevel::EnumDef(_) => {
                    // Variable, constant, and enum declarations don't need naming checks for now
                }
                TopLevel::EventDef(_) | TopLevel::TimelineDef(_) => {
                    // Event and timeline definitions don't need naming checks for now
                }
            }
        }

        // Second pass: check usages
        for item in &program.body {
            match item {
                TopLevel::NodeDef(node) => {
                    self.analyze_node_usage(
                        node,
                        &declared_functions,
                        &declared_nodes,
                        &mut used_functions,
                        &mut used_nodes,
                    );
                }
                TopLevel::EventDef(event_def) => {
                    self.analyze_event_action(
                        &event_def.action,
                        &declared_functions,
                        &mut used_functions,
                    );
                }
                TopLevel::TimelineDef(timeline_def) => {
                    Self::collect_timeline_usages(timeline_def, &mut used_functions)
                }
                TopLevel::VarDecl(var_decl) if var_decl.value.is_some() => {
                    let value = var_decl.value.as_ref().unwrap();
                    self.analyze_var_value(value, &declared_functions, &mut used_functions);
                }
                TopLevel::ConstDecl(const_decl) => {
                    self.analyze_var_value(
                        &const_decl.value,
                        &declared_functions,
                        &mut used_functions,
                    );
                }
                _ => {}
            }
        }

        // Check for unused functions
        for func_name in declared_functions.keys() {
            if !used_functions.contains(func_name) {
                self.add_diagnostic(Diagnostic {
                    kind: DiagnosticKind::UnusedFunction {
                        function_name: func_name.clone(),
                    },
                    severity: Severity::Warning,
                    span: declared_functions[func_name].name_span,
                    message: format_message(
                        get_text("function_declared_but_never_used", self.language),
                        &[func_name],
                    ),
                });
            }
        }
    }

    fn analyze_var_value(
        &mut self,
        value: &crate::ast::VarValue,
        declared_functions: &HashMap<String, &FunctionDecl>,
        used_functions: &mut HashSet<String>,
    ) {
        let crate::ast::VarValue::Branch(branch_val) = value else {
            return;
        };
        for case in &branch_val.cases {
            let Some(events) = &case.events else { continue };
            for event in events {
                self.analyze_event_action(&event.action, declared_functions, used_functions);
            }
        }
    }

    fn analyze_node_usage(
        &mut self,
        node: &NodeDef,
        declared_functions: &HashMap<String, &FunctionDecl>,
        declared_nodes: &HashMap<String, &NodeDef>,
        used_functions: &mut HashSet<String>,
        used_nodes: &mut HashSet<String>,
    ) {
        // Analyze node jump
        if let Some(jump) = &node.jump {
            self.analyze_node_jump(jump, declared_nodes, used_nodes);
        }

        // Analyze statements
        for stmt in &node.body {
            match stmt {
                NodeStmt::IfElse(_) => {
                    // If-else statements don't need special analysis for now
                }
                NodeStmt::Branch(_) => {
                    // Branch definitions don't need analysis here
                }

                NodeStmt::Choice(choices) => {
                    self.analyze_choices(
                        choices,
                        declared_functions,
                        declared_nodes,
                        used_functions,
                        used_nodes,
                    );
                }
                NodeStmt::Text(text) => {
                    // Check for function calls in text interpolation (old format)
                    self.analyze_text_interpolation(text, declared_functions, used_functions);
                }
                NodeStmt::InterpolatedText(interpolated) => {
                    // Check function calls in interpolated string
                    self.analyze_interpolated_string(
                        interpolated,
                        declared_functions,
                        used_functions,
                    );
                }
                NodeStmt::Line(text) => {
                    self.analyze_text_interpolation(text, declared_functions, used_functions);
                }
                NodeStmt::InterpolatedLine(interpolated) => {
                    self.analyze_interpolated_string(
                        interpolated,
                        declared_functions,
                        used_functions,
                    );
                }
                NodeStmt::Run(_) => {
                    // Run statements don't need analysis for now
                }
                NodeStmt::WithEvents(with_events) => {
                    self.analyze_with_events(with_events, declared_functions, used_functions);
                }
                NodeStmt::VarDecl(_) => {
                    // Variable declarations in node body are local scope
                }
                NodeStmt::Assignment(_) => {
                    // Assignment statements don't need special analysis for now
                }
            }
        }
    }

    fn analyze_with_events(
        &mut self,
        with_events: &crate::ast::WithEventsStmt,
        declared_functions: &HashMap<String, &FunctionDecl>,
        used_functions: &mut HashSet<String>,
    ) {
        for item in &with_events.events {
            self.analyze_with_event_item(item, declared_functions, used_functions);
        }
    }

    fn analyze_with_event_item(
        &mut self,
        item: &crate::ast::WithEventItem,
        declared_functions: &HashMap<String, &FunctionDecl>,
        used_functions: &mut HashSet<String>,
    ) {
        match item {
            crate::ast::WithEventItem::InlineEvent(event) => {
                self.analyze_event_action(&event.action, declared_functions, used_functions);
            }
            crate::ast::WithEventItem::EventList(list) => {
                for sub_item in list {
                    self.analyze_with_event_item(sub_item, declared_functions, used_functions);
                }
            }
            crate::ast::WithEventItem::EventRef(_, _)
            | crate::ast::WithEventItem::EventRefWithOverride(_, _, _) => {
                // References point to TopLevel::EventDef, which are analyzed separately.
            }
        }
    }

    fn analyze_node_jump(
        &mut self,
        jump: &NodeJump,
        declared_nodes: &HashMap<String, &NodeDef>,
        used_nodes: &mut HashSet<String>,
    ) {
        match jump {
            NodeJump::Identifier(node_name, span) => {
                used_nodes.insert(node_name.clone());
                if !declared_nodes.contains_key(node_name) {
                    self.add_diagnostic(Diagnostic {
                        kind: DiagnosticKind::NodeNotFound {
                            node_name: node_name.clone(),
                        },
                        severity: Severity::Error,
                        span: *span,
                        message: format_message(
                            get_text("node_not_defined", self.language),
                            &[node_name],
                        ),
                    });
                }
            }
            NodeJump::Return | NodeJump::Break => {
                // These are always valid
            }
        }
    }

    fn analyze_choices(
        &mut self,
        choices: &[ChoiceItem],
        declared_functions: &HashMap<String, &FunctionDecl>,
        declared_nodes: &HashMap<String, &NodeDef>,
        used_functions: &mut HashSet<String>,
        used_nodes: &mut HashSet<String>,
    ) {
        for choice in choices {
            // Analyze condition
            if let Some(condition) = &choice.condition {
                self.analyze_choice_condition(condition, declared_functions, used_functions);
            }

            // Analyze choice destination
            match &choice.target {
                ChoiceDest::Identifier(node_name, span) => {
                    self.check_node_exists(node_name, *span, declared_nodes, used_nodes);
                }
                ChoiceDest::NestedChoices(nested) => {
                    self.analyze_choices(
                        nested,
                        declared_functions,
                        declared_nodes,
                        used_functions,
                        used_nodes,
                    );
                }
                ChoiceDest::Return | ChoiceDest::Break => {
                    // These are always valid
                }
            }
        }
    }

    fn analyze_event_action(
        &mut self,
        action: &EventAction,
        declared_functions: &HashMap<String, &FunctionDecl>,
        used_functions: &mut HashSet<String>,
    ) {
        self.analyze_func_call(&action.call, declared_functions, used_functions);

        for chain in &action.chains {
            self.analyze_func_call(chain, declared_functions, used_functions);
        }
    }

    fn analyze_func_call(
        &mut self,
        func_call: &FuncCall,
        declared_functions: &HashMap<String, &FunctionDecl>,
        used_functions: &mut HashSet<String>,
    ) {
        used_functions.insert(func_call.name.clone());

        // Check if function is declared
        if !declared_functions.contains_key(&func_call.name) {
            self.add_diagnostic(Diagnostic {
                kind: DiagnosticKind::FunctionNotFound {
                    function_name: func_call.name.clone(),
                },
                severity: Severity::Error,
                span: func_call.name_span,
                message: format_message(
                    get_text("function_not_declared", self.language),
                    &[&func_call.name],
                ),
            });
            return;
        }

        let func_decl = declared_functions[&func_call.name];

        // Check argument count
        if func_call.args.len() != func_decl.params.len() {
            self.add_diagnostic(Diagnostic {
                kind: DiagnosticKind::ArgumentCountMismatch {
                    function_name: func_call.name.clone(),
                    expected: func_decl.params.len(),
                    actual: func_call.args.len(),
                },
                severity: Severity::Error,
                span: func_call.name_span,
                message: format_message(
                    get_text("function_expects_args", self.language),
                    &[
                        &func_call.name,
                        &func_decl.params.len().to_string(),
                        &func_call.args.len().to_string(),
                    ],
                ),
            });
        } else {
            self.check_argument_types(func_call, func_decl, declared_functions);
        }

        // Analyze nested function calls in arguments
        for arg in &func_call.args {
            if let Arg::FuncCall(nested_call) = arg {
                self.analyze_func_call(nested_call, declared_functions, used_functions);
            }
        }
    }

    fn check_node_exists(
        &mut self,
        node_name: &str,
        span: Option<(usize, usize)>,
        declared_nodes: &HashMap<String, &NodeDef>,
        used_nodes: &mut HashSet<String>,
    ) {
        used_nodes.insert(node_name.to_string());
        if !declared_nodes.contains_key(node_name) {
            self.add_diagnostic(Diagnostic {
                kind: DiagnosticKind::NodeNotFound {
                    node_name: node_name.to_string(),
                },
                severity: Severity::Error,
                span,
                message: format_message(get_text("node_not_defined", self.language), &[node_name]),
            });
        }
    }
}