Skip to main content

harn_vm/vm/
format.rs

1use crate::value::VmError;
2
3impl super::Vm {
4    pub fn format_runtime_error(&self, error: &VmError) -> String {
5        let entry_file = self.source_file.as_deref().unwrap_or("<unknown>");
6        let entry_source = self.source_text.as_deref();
7
8        let error_msg = format!("{error}");
9        let mut out = String::new();
10
11        out.push_str(&format!("error: {error_msg}\n"));
12
13        // Prefer captured stack trace (taken before unwinding); fall back to live frames.
14        let frames: Vec<(String, usize, usize, Option<String>)> =
15            if !self.error_stack_trace.is_empty() {
16                self.error_stack_trace
17                    .iter()
18                    .map(|(name, line, col, src)| (name.clone(), *line, *col, src.clone()))
19                    .collect()
20            } else {
21                self.frames
22                    .iter()
23                    .map(|f| {
24                        let idx = if f.ip > 0 { f.ip - 1 } else { 0 };
25                        let line = f.chunk.lines.get(idx).copied().unwrap_or(0) as usize;
26                        let col = f.chunk.columns.get(idx).copied().unwrap_or(0) as usize;
27                        (f.fn_name.clone(), line, col, f.chunk.source_file.clone())
28                    })
29                    .collect()
30            };
31
32        if let Some((_name, line, col, frame_file)) = frames.last() {
33            let line = *line;
34            let col = *col;
35            let filename = frame_file.as_deref().unwrap_or(entry_file);
36            let display_filename = harn_parser::diagnostic::normalize_diagnostic_path(filename);
37            // Read the frame's own source so the caret line is meaningful;
38            // fall back to entry-point source (e.g. for stdlib modules).
39            let owned_source: Option<String> = frame_file
40                .as_deref()
41                .and_then(|p| std::fs::read_to_string(p).ok());
42            let source_for_line: Option<&str> =
43                owned_source.as_deref().or(if frame_file.is_none() {
44                    entry_source
45                } else {
46                    None
47                });
48            if line > 0 {
49                let display_col = if col > 0 { col } else { 1 };
50                let gutter_width = line.to_string().len();
51                out.push_str(&format!(
52                    "{:>width$}--> {display_filename}:{line}:{display_col}\n",
53                    " ",
54                    width = gutter_width + 1,
55                ));
56                if let Some(source_line) =
57                    source_for_line.and_then(|s| s.lines().nth(line.saturating_sub(1)))
58                {
59                    out.push_str(&format!("{:>width$} |\n", " ", width = gutter_width + 1));
60                    out.push_str(&format!(
61                        "{:>width$} | {source_line}\n",
62                        line,
63                        width = gutter_width + 1,
64                    ));
65                    let caret_col = if col > 0 { col } else { 1 };
66                    let trimmed = source_line.trim();
67                    let leading = source_line
68                        .len()
69                        .saturating_sub(source_line.trim_start().len());
70                    let caret_len = if col > 0 {
71                        Self::token_len_at(source_line, col)
72                    } else {
73                        trimmed.len().max(1)
74                    };
75                    let padding = if col > 0 {
76                        " ".repeat(caret_col.saturating_sub(1))
77                    } else {
78                        " ".repeat(leading)
79                    };
80                    let carets = "^".repeat(caret_len);
81                    out.push_str(&format!(
82                        "{:>width$} | {padding}{carets}\n",
83                        " ",
84                        width = gutter_width + 1,
85                    ));
86                }
87            }
88        }
89
90        // Call stack, bottom-up, skipping the top frame (already shown).
91        if frames.len() > 1 {
92            for (name, line, _col, frame_file) in frames.iter().rev().skip(1) {
93                let display_name = if name.is_empty() { "pipeline" } else { name };
94                if *line > 0 {
95                    let filename = frame_file.as_deref().unwrap_or(entry_file);
96                    let display_filename =
97                        harn_parser::diagnostic::normalize_diagnostic_path(filename);
98                    out.push_str(&format!(
99                        "  = note: called from {display_name} at {display_filename}:{line}\n"
100                    ));
101                }
102            }
103        }
104
105        out
106    }
107
108    /// Estimate the length of the token at the given 1-based column position
109    /// in a source line. Scans forward from that position to find a word/operator
110    /// boundary.
111    fn token_len_at(source_line: &str, col: usize) -> usize {
112        let chars: Vec<char> = source_line.chars().collect();
113        let start = col.saturating_sub(1);
114        if start >= chars.len() {
115            return 1;
116        }
117        let first = chars[start];
118        if first.is_alphanumeric() || first == '_' {
119            let mut end = start + 1;
120            while end < chars.len() && (chars[end].is_alphanumeric() || chars[end] == '_') {
121                end += 1;
122            }
123            end - start
124        } else {
125            1
126        }
127    }
128}