miden-debug 0.7.0

An interactive debugger for Miden VM programs
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
487
488
489
490
491
use std::{borrow::Cow, ops::Range, path::Path, rc::Rc};

mod syntax {
    pub(super) use syntect::{
        highlighting::{
            Color, FontStyle, HighlightIterator, HighlightState, Highlighter, Style, StyleModifier,
            Theme, ThemeSet,
        },
        parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet},
    };
}

use miden_assembly_syntax::diagnostics::miette::SpanContents;
use ratatui::{
    style::{Color, Modifier, Style},
    text::Span,
};

pub trait Highlighter {
    ///  Creates a new [HighlighterState] to begin parsing and highlighting
    /// a [SpanContents].
    ///
    /// The [GraphicalReportHandler](crate::GraphicalReportHandler) will call
    /// this method at the start of rendering a [SpanContents].
    ///
    /// The [SpanContents] is provided as input only so that the [Highlighter]
    /// can detect language syntax and make other initialization decisions prior
    /// to highlighting, but it is not intended that the Highlighter begin
    /// highlighting at this point. The returned [HighlighterState] is
    /// responsible for the actual rendering.
    fn start_highlighter_state(&self, source: &dyn SpanContents<'_>) -> Box<dyn HighlighterState>;
}

/// A stateful highlighter that incrementally highlights lines of a particular
/// source code.
///
/// The [GraphicalReportHandler](crate::GraphicalReportHandler)
/// will create a highlighter state by calling
/// [start_highlighter_state](Highlighter::start_highlighter_state) at the
/// start of rendering, then it will iteratively call
/// [highlight_line](HighlighterState::highlight_line) to render individual
/// highlighted lines. This allows [Highlighter] implementations to maintain
/// mutable parsing and highlighting state.
pub trait HighlighterState {
    /// Highlight an individual line from the source code by returning a vector of [Styled]
    /// regions.
    fn highlight_line<'a>(&mut self, line: Cow<'a, str>) -> Vec<Span<'a>>;
    fn highlight_line_with_selection<'a>(
        &mut self,
        line: Cow<'a, str>,
        selected: Range<usize>,
        style: Style,
    ) -> Vec<Span<'a>>;
}

/// The fallback syntax highlighter.
///
/// This simply returns a line without any styling at all
#[derive(Debug, Clone)]
pub struct NoopHighlighter;

impl Highlighter for NoopHighlighter {
    fn start_highlighter_state(&self, _source: &dyn SpanContents<'_>) -> Box<dyn HighlighterState> {
        Box::new(NoopHighlighterState)
    }
}

impl Default for NoopHighlighter {
    fn default() -> Self {
        NoopHighlighter
    }
}

/// The fallback highlighter state.
#[derive(Debug, Clone)]
pub struct NoopHighlighterState;

impl HighlighterState for NoopHighlighterState {
    fn highlight_line<'a>(&mut self, line: Cow<'a, str>) -> Vec<Span<'a>> {
        vec![Span::raw(line)]
    }

    fn highlight_line_with_selection<'a>(
        &mut self,
        line: Cow<'a, str>,
        selected: Range<usize>,
        style: Style,
    ) -> Vec<Span<'a>> {
        default_line_with_selection(line, selected, style)
    }
}

fn default_line_with_selection(
    line: Cow<'_, str>,
    selected: Range<usize>,
    style: Style,
) -> Vec<Span<'_>> {
    let selected = clamp_byte_selection_to_str(&line, selected);
    let prefix_content =
        core::str::from_utf8(&line.as_bytes()[..selected.start]).expect("invalid selection");
    let selected_content =
        core::str::from_utf8(&line.as_bytes()[selected.clone()]).expect("invalid selection");
    let suffix_content =
        core::str::from_utf8(&line.as_bytes()[selected.end..]).expect("invalid selection");
    let (selected_content, suffix_content) = if suffix_content.is_empty() {
        (selected_content.strip_suffix('\n').unwrap_or(selected_content), suffix_content)
    } else {
        (selected_content, suffix_content.strip_suffix('\n').unwrap_or(suffix_content))
    };
    vec![
        Span::raw(prefix_content.to_string()),
        Span::styled(selected_content.to_string(), style),
        Span::raw(suffix_content.to_string()),
    ]
}

pub fn clamp_byte_selection_to_str(line: &str, selected: Range<usize>) -> Range<usize> {
    fn floor_char_boundary(line: &str, idx: usize) -> usize {
        let mut idx = idx.min(line.len());
        while idx > 0 && !line.is_char_boundary(idx) {
            idx -= 1;
        }
        idx
    }

    let start = floor_char_boundary(line, selected.start);
    let end = floor_char_boundary(line, selected.end).max(start);
    start..end
}

/// Syntax highlighting provided by [syntect](https://docs.rs/syntect/latest/syntect/).
///
/// Currently only 24-bit truecolor output is supported due to syntect themes
/// representing color as RGBA.
#[derive(Debug, Clone)]
pub struct SyntectHighlighter {
    theme: &'static syntax::Theme,
    syntax_set: Rc<syntax::SyntaxSet>,
    use_bg_color: bool,
}

impl Default for SyntectHighlighter {
    fn default() -> Self {
        let theme_set = syntax::ThemeSet::load_defaults();
        let theme = theme_set.themes["base16-ocean.dark"].clone();
        Self::new_themed(theme, false)
    }
}

impl Highlighter for SyntectHighlighter {
    fn start_highlighter_state(&self, source: &dyn SpanContents<'_>) -> Box<dyn HighlighterState> {
        if let Some(syntax) = self.detect_syntax(source) {
            let highlighter = syntax::Highlighter::new(self.theme);
            let parse_state = syntax::ParseState::new(syntax);
            let highlight_state =
                syntax::HighlightState::new(&highlighter, syntax::ScopeStack::new());
            Box::new(SyntectHighlighterState {
                syntax_set: Rc::clone(&self.syntax_set),
                highlighter,
                parse_state,
                highlight_state,
                use_bg_color: self.use_bg_color,
            })
        } else {
            Box::new(NoopHighlighterState)
        }
    }
}

impl SyntectHighlighter {
    /// Create a syntect highlighter with the given theme and syntax set.
    pub fn new(syntax_set: syntax::SyntaxSet, theme: syntax::Theme, use_bg_color: bool) -> Self {
        // This simplifies a lot of things API-wise, we only ever really have one of these anyway
        let theme = Box::leak(Box::new(theme));
        Self {
            theme,
            syntax_set: Rc::new(syntax_set),
            use_bg_color,
        }
    }

    /// Create a syntect highlighter with the given theme and the default syntax set.
    pub fn new_themed(theme: syntax::Theme, use_bg_color: bool) -> Self {
        Self::new(syntax::SyntaxSet::load_defaults_nonewlines(), theme, use_bg_color)
    }

    /// Determine syntect SyntaxReference to use for given SourceCode
    fn detect_syntax(&self, contents: &dyn SpanContents<'_>) -> Option<&syntax::SyntaxReference> {
        // use language if given
        if let Some(language) = contents.language() {
            return self.syntax_set.find_syntax_by_name(language);
        }
        // otherwise try to use any file extension provided in the name
        if let Some(name) = contents.name()
            && let Some(ext) = Path::new(name).extension()
        {
            return self.syntax_set.find_syntax_by_extension(ext.to_string_lossy().as_ref());
        }
        // finally, attempt to guess syntax based on first line
        self.syntax_set.find_syntax_by_first_line(
            core::str::from_utf8(contents.data()).ok()?.split('\n').next()?,
        )
    }
}

/// Stateful highlighting iterator for [SyntectHighlighter]
#[derive(Debug)]
pub(crate) struct SyntectHighlighterState<'h> {
    syntax_set: Rc<syntax::SyntaxSet>,
    highlighter: syntax::Highlighter<'h>,
    parse_state: syntax::ParseState,
    highlight_state: syntax::HighlightState,
    use_bg_color: bool,
}

impl HighlighterState for SyntectHighlighterState<'_> {
    fn highlight_line<'a>(&mut self, line: Cow<'a, str>) -> Vec<Span<'a>> {
        if let Ok(ops) = self.parse_state.parse_line(&line, &self.syntax_set) {
            let use_bg_color = self.use_bg_color;
            syntax::HighlightIterator::new(
                &mut self.highlight_state,
                &ops,
                &line,
                &self.highlighter,
            )
            .map(|(style, str)| Span::styled(str.to_string(), convert_style(style, use_bg_color)))
            .collect()
        } else {
            vec![Span::raw(line)]
        }
    }

    fn highlight_line_with_selection<'a>(
        &mut self,
        line: Cow<'a, str>,
        selected: Range<usize>,
        style: Style,
    ) -> Vec<Span<'a>> {
        let selected = clamp_byte_selection_to_str(&line, selected);
        if let Ok(ops) = self.parse_state.parse_line(&line, &self.syntax_set) {
            let use_bg_color = self.use_bg_color;
            let parts = syntax::HighlightIterator::new(
                &mut self.highlight_state,
                &ops,
                &line,
                &self.highlighter,
            )
            .collect::<Vec<_>>();
            let syntect_style = syntax::StyleModifier {
                foreground: style.fg.map(convert_to_syntect_color),
                background: style.bg.map(convert_to_syntect_color),
                font_style: if style.add_modifier.is_empty() {
                    None
                } else {
                    Some(convert_to_font_style(style.add_modifier))
                },
            };
            syntect::util::modify_range(&parts, selected, syntect_style)
                .into_iter()
                .map(|(style, str)| {
                    Span::styled(str.to_string(), convert_style(style, use_bg_color))
                })
                .collect()
        } else {
            default_line_with_selection(line, selected, style)
        }
    }
}

/// Convert syntect [syntax::Style] into ratatui [Style] */
#[inline]
pub fn convert_style(syntect_style: syntax::Style, use_bg_color: bool) -> Style {
    let fg = syntect_style.foreground;
    let bg = syntect_style.background;
    let mut style = Style::new();
    // Skip transparent colors (alpha=0) to use the terminal's native colors
    if fg.a > 0 {
        let fg_color = if use_bg_color {
            blend_fg_color(syntect_style)
        } else {
            convert_color(fg)
        };
        style = style.fg(fg_color);
    }
    if use_bg_color && bg.a > 0 {
        style = style.bg(convert_color(bg));
    }
    let mods = convert_font_style(syntect_style.font_style);
    style.add_modifier(mods)
}

pub fn convert_to_syntect_style(style: Style, _use_bg_color: bool) -> syntax::Style {
    let fg = style.fg.map(convert_to_syntect_color);
    let bg = style.bg.map(convert_to_syntect_color);
    let fs = convert_to_font_style(style.add_modifier);
    // Use transparent (alpha=0) fallbacks so that convert_style will skip
    // setting explicit colors, letting the terminal's native colors show through.
    // This avoids hardcoded White/Black that break on light/dark terminals.
    let transparent = syntax::Color {
        r: 0,
        g: 0,
        b: 0,
        a: 0,
    };
    syntax::Style {
        foreground: fg.unwrap_or(transparent),
        background: bg.unwrap_or(transparent),
        font_style: fs,
    }
}

/// Blend foreground RGB into background RGB according to alpha channel
#[inline]
fn blend_fg_color(syntect_style: syntax::Style) -> Color {
    let fg = syntect_style.foreground;
    if fg.a == 0xff {
        return convert_color(fg);
    }
    let bg = syntect_style.background;
    let ratio = fg.a as u32;
    let r = (fg.r as u32 * ratio + bg.r as u32 * (255 - ratio)) / 255;
    let g = (fg.g as u32 * ratio + bg.g as u32 * (255 - ratio)) / 255;
    let b = (fg.b as u32 * ratio + bg.b as u32 * (255 - ratio)) / 255;
    Color::from_u32(u32::from_be_bytes([0, r as u8, g as u8, b as u8]))
}

/// Convert syntect color into ratatui color
///
/// Note: ignores alpha channel. use [`blend_fg_color`] if you need that
#[inline]
pub fn convert_color(color: syntax::Color) -> Color {
    Color::from_u32(u32::from_be_bytes([color.a, color.r, color.g, color.b]))
}

/// Convert syntect font style into ratatui modifiers
#[inline]
fn convert_font_style(font_style: syntax::FontStyle) -> Modifier {
    use syntax::FontStyle;

    let mut mods = Modifier::default();
    if font_style.contains(FontStyle::BOLD) {
        mods.insert(Modifier::BOLD);
    }
    if font_style.contains(FontStyle::ITALIC) {
        mods.insert(Modifier::ITALIC);
    }
    if font_style.contains(FontStyle::UNDERLINE) {
        mods.insert(Modifier::UNDERLINED);
    }
    mods
}

pub fn convert_to_font_style(mods: Modifier) -> syntax::FontStyle {
    use syntax::FontStyle;

    let mut style = FontStyle::default();
    if mods.contains(Modifier::BOLD) {
        style.insert(FontStyle::BOLD);
    }
    if mods.contains(Modifier::ITALIC) {
        style.insert(FontStyle::ITALIC);
    }
    if mods.contains(Modifier::UNDERLINED) {
        style.insert(FontStyle::UNDERLINE);
    }
    style
}

pub fn convert_to_syntect_color(color: Color) -> syntax::Color {
    match color {
        Color::Rgb(r, g, b) => syntax::Color { r, g, b, a: 0 },
        Color::Indexed(code) => convert_to_syntect_color(match code {
            0 => Color::Black,
            1 => Color::Red,
            2 => Color::Green,
            3 => Color::Yellow,
            4 => Color::Blue,
            5 => Color::Magenta,
            6 => Color::Cyan,
            7 => Color::Gray,
            8 => Color::DarkGray,
            9 => Color::LightRed,
            10 => Color::LightGreen,
            11 => Color::LightYellow,
            12 => Color::LightBlue,
            13 => Color::LightMagenta,
            14 => Color::LightCyan,
            15 => Color::White,
            code => panic!("unrecognized ansi color index: {code}"),
        }),
        Color::Black => syntax::Color {
            r: 0,
            g: 0,
            b: 0,
            a: u8::MAX,
        },
        Color::Green => syntax::Color {
            r: 0,
            g: 128,
            b: 0,
            a: u8::MAX,
        },
        Color::LightGreen => syntax::Color {
            r: 0,
            g: 255,
            b: 0,
            a: u8::MAX,
        },
        Color::Red => syntax::Color {
            r: 128,
            g: 0,
            b: 0,
            a: u8::MAX,
        },
        Color::LightRed => syntax::Color {
            r: 255,
            g: 0,
            b: 0,
            a: u8::MAX,
        },
        Color::Yellow => syntax::Color {
            r: 128,
            g: 128,
            b: 0,
            a: u8::MAX,
        },
        Color::LightYellow => syntax::Color {
            r: 255,
            g: 255,
            b: 0,
            a: u8::MAX,
        },
        Color::Blue => syntax::Color {
            r: 0,
            g: 0,
            b: 128,
            a: u8::MAX,
        },
        Color::LightBlue => syntax::Color {
            r: 0,
            g: 0,
            b: 255,
            a: u8::MAX,
        },
        Color::DarkGray => syntax::Color {
            r: 128,
            g: 128,
            b: 128,
            a: u8::MAX,
        },
        Color::Gray => syntax::Color {
            r: 192,
            g: 192,
            b: 192,
            a: u8::MAX,
        },
        Color::White => syntax::Color {
            r: 255,
            g: 255,
            b: 255,
            a: u8::MAX,
        },
        Color::Magenta => syntax::Color {
            r: 128,
            g: 0,
            b: 128,
            a: u8::MAX,
        },
        Color::LightMagenta => syntax::Color {
            r: 255,
            g: 0,
            b: 255,
            a: u8::MAX,
        },
        Color::Cyan => syntax::Color {
            r: 0,
            g: 128,
            b: 128,
            a: u8::MAX,
        },
        Color::LightCyan => syntax::Color {
            r: 0,
            g: 255,
            b: 255,
            a: u8::MAX,
        },
        Color::Reset => {
            panic!("invalid syntax color: reset cannot be used for syntax highlighting")
        }
    }
}