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
use chumsky::error::RichReason;
use codespan_reporting::diagnostic::LabelStyle::{
    self, Primary, Secondary,
};
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::Chars;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use comat::cformat as cmt;

use crate::exec::Error as ExecutionError;
use crate::parser::types::Error;

pub fn display_execution<T>(
    x: Result<T, ExecutionError>,
    code: &str,
) -> T {
    let e = match x {
        Ok(x) => return x,
        Err(e) => e,
    };
    let mut files = SimpleFiles::new();
    files.add("x.kale", code);
    let mut d = Diagnostic::<usize>::new(
        codespan_reporting::diagnostic::Severity::Error,
    )
    .with_message(e.name)
    .with_label(
        Label::new(LabelStyle::Primary, 0, e.message.span())
            .with_message(e.message.raw().0),
    );
    for label in e.labels {
        d = d.with_label(
            Label::new(LabelStyle::Secondary, 0, label.span())
                .with_message(label.raw().0),
        );
    }

    d = d.with_notes(e.notes);

    let writer = StandardStream::stderr(ColorChoice::Always);
    let mut config = codespan_reporting::term::Config::default();
    config.chars = Chars::box_drawing();
    codespan_reporting::term::emit(
        &mut writer.lock(),
        &config,
        &files,
        &d,
    )
    .unwrap();
    std::process::exit(2);
}

pub fn display<T>(
    result: Result<T, Vec<Error>>,
    code: &str,
) -> Result<T, ()> {
    let e = match result {
        Ok(x) => return Ok(x),
        Err(e) => e,
    };

    let mut files = SimpleFiles::new();
    files.add("x.kale", code);

    for e in e.into_iter().map(|e| e.map_token(|c| c.to_string())) {
        let mut d = Diagnostic::<usize>::new(
            codespan_reporting::diagnostic::Severity::Error,
        );
        // let mut o = lerr::Error::new(code);
        d = d.with_label(Label {
            style: Primary,
            file_id: 0,
            range: e.span().into_range(),
            message: "here".into(),
        });
        // o.label((e.span().into_range(), "here"));
        match e.reason() {
            RichReason::Custom(x) => {
                d = d.with_message(cmt!("{red}error{reset}: {x}"));
                // o.message(cmt!("{red}error{reset}: {x}"));
            }
            RichReason::ExpectedFound { .. } => {
                d = d.with_message(format!("{e}"));
                // o.message(cmt!("{red}error{reset}: {e}"));
            } // RichReason::Many(x) => {
              //     match &x[..] {
              //         [x, rest @ ..] => {
              //             o.message(cmt!("{red}error{reset}: {x}"));
              //             for elem in rest {
              //                 o.note(cmt!("{yellow}also{reset}: {elem}"));
              //             }
              //         }
              //         _ => unreachable!(),
              //     };
              // }
        }
        for (l, span) in e.contexts() {
            d = d.with_label(Label {
                style: Secondary,
                file_id: 0,
                range: span.into_range(),
                message: cmt!("{yellow}while parsing this{reset}: {l}"),
            })
            // o.label((
            //     span.into_range(),
            //     cmt!("{yellow}while parsing this{reset}: {l}"),
            // ));
        }
        // eprintln!("{o}");

        let writer = StandardStream::stderr(ColorChoice::Always);
        let mut config = codespan_reporting::term::Config::default();
        config.chars = Chars::box_drawing();
        codespan_reporting::term::emit(
            &mut writer.lock(),
            &config,
            &files,
            &d,
        )
        .unwrap();
    }
    Err(())
}