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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::{
fmt::{Debug, Display},
rc::Rc,
sync::{atomic::AtomicU32, Arc},
};
use line_span::LineSpans;
#[cfg(feature = "parse")]
use crate::parsing::Source;
use crate::theme::ThemeGen;
pub mod themes;
#[derive(Clone, Copy, Debug)]
pub struct SourcePos(pub(crate) usize);
impl SourcePos {
pub fn pos(&self) -> usize {
self.0
}
}
#[derive(Clone, Debug)]
pub struct SourceRange {
in_file: Arc<Source>,
start: SourcePos,
end: SourcePos,
}
impl From<(SourcePos, SourcePos, &Arc<Source>)> for SourceRange {
fn from(value: (SourcePos, SourcePos, &Arc<Source>)) -> Self {
SourceRange {
in_file: Arc::clone(value.2),
start: value.0,
end: value.1,
}
}
}
impl SourceRange {
pub fn start(&self) -> SourcePos {
self.start
}
pub fn end(&self) -> SourcePos {
self.end
}
pub fn in_file(&self) -> &Arc<Source> {
&self.in_file
}
}
/// To `Display` this, use one of the `display` methods to get a struct with some configuration options.
/// The `Debug` impl of this is the same as `Display`ing `this.display_term()` or `this.display_notheme()`, depending on if the `ecolor-term` feature is enabled or not.
/// Since this may use ansi color codes, it should only be used when printing to a terminal, which is why `CheckError` itself has no `Display` implementation, only this one for `Debug`.
#[derive(Clone)]
pub struct CheckError(pub Vec<CheckErrorComponent>);
#[derive(Clone, Copy)]
pub enum EColor {
Indent(u32),
UnknownVariable,
WhitespaceAfterHashtag,
HashUnknown,
HashIncludeCantLoadFile,
HashIncludeNotAString,
HashIncludeErrorInIncludedFile,
BackslashEscapeUnknown,
BackslashEscapeEOF,
StringEOF,
TypeEOF,
/// `&[Int/String` (notice the missing `]`)
BracketedRefTypeNoClosingBracket,
IfConditionNotBool,
ChainWithNonFunction,
Function,
FunctionArgument,
ObjectField,
InitFrom,
InitTo,
AssignFrom,
AssignTo,
AssignTargetNonReference,
AsTypeStatementWithTooBroadType,
AsTypeTypeAnnotation,
BadCharInTupleType,
BadCharInFunctionType,
BadCharAtStartOfStatement,
BadTypeFromParsed,
ObjectDuplicateField,
TypeAnnotationNoClosingBracket,
TryBadSyntax,
TryNoFunctionFound,
TryNotAFunction,
TryUnusedFunction1,
TryUnusedFunction2,
CustomTypeTestFailed,
StacktraceDescend,
StacktraceDescendHashInclude,
MaximumRuntimeExceeded,
InCodePositionLine,
Warning,
}
pub trait ETheme: ThemeGen<C = EColor, T = String> {}
impl<T: ThemeGen<C = EColor, T = String>> ETheme for T {}
pub fn colorize_str(
message: &Vec<(String, Option<EColor>)>,
theme: &(impl ETheme + ?Sized),
) -> String {
let mut t = String::new();
for (text, color) in message {
if let Some(color) = *color {
theme.color(text, color, &mut t)
} else {
theme.nocolor(text, &mut t)
}
}
t
}
#[derive(Clone)]
pub enum CheckErrorComponent {
Message(Vec<(String, Option<EColor>)>),
Error(CheckError),
ErrorWithDifferentSource(CheckError),
Source(Vec<(SourceRange, Option<EColor>)>),
}
pub struct CheckErrorHRConfig {
color_index_ptr: Rc<AtomicU32>,
color_index: u32,
theme: Rc<dyn ETheme>,
is_inner: bool,
style: u8,
idt_start: String,
idt_default: String,
idt_end: String,
idt_single: String,
/// if true, shows "original" source code, if false, shows source with comments removed (this is what the parser uses internally)
show_comments: bool,
}
type BorderCharsSet = [[&'static str; 4]; 3];
pub struct IndentStr<'a>(&'a str, String);
impl Display for IndentStr<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.0, self.1)
}
}
impl CheckErrorHRConfig {
pub const BORDER_STYLE_THIN: u8 = 0;
pub const BORDER_STYLE_THICK: u8 = 1;
pub const BORDER_STYLE_DOUBLE: u8 = 2;
pub const STYLE_DEFAULT: u8 = Self::BORDER_STYLE_THIN;
pub const STYLE_DIFFSRC: u8 = Self::BORDER_STYLE_THICK;
const CHARS_HORIZONTAL: [&'static str; 3] = ["╶", "╴", "─"];
const CHARS: [BorderCharsSet; 3] = [
[
["╷", "┌", "┐", "┬"],
["│", "├", "┤", "┼"],
["╵", "└", "┘", "┴"],
],
[
["╻", "┎", "┒", "┰"],
["┃", "┠", "┨", "╂"],
["╹", "┖", "┚", "┸"],
],
[
["╦", "╓", "╖", "╥"],
["║", "╟", "╢", "╫"],
["╩", "╙", "╜", "╨"],
],
];
fn color(&self, s: &str) -> String {
let mut t = String::new();
self.theme
.color(s, EColor::Indent(self.color_index), &mut t);
return t;
}
pub fn indent_start(&'_ self, right: bool) -> IndentStr<'_> {
IndentStr(
&self.idt_start,
self.color(
Self::CHARS[self.style as usize][0][self.is_inner as usize * 2 + right as usize],
),
)
}
pub fn indent_default(&'_ self, right: bool) -> IndentStr<'_> {
IndentStr(
&self.idt_default,
self.color(Self::CHARS[self.style as usize][1][right as usize]),
)
}
pub fn indent_end(&'_ self, right: bool) -> IndentStr<'_> {
IndentStr(
&self.idt_end,
self.color(
Self::CHARS[self.style as usize][2][self.is_inner as usize * 2 + right as usize],
),
)
}
pub fn indent_single(&'_ self, right: bool) -> IndentStr<'_> {
IndentStr(
&self.idt_single,
self.color(if self.is_inner {
if right {
Self::CHARS_HORIZONTAL[2] // left+right
} else {
Self::CHARS_HORIZONTAL[1] // left only
}
} else {
if right {
Self::CHARS_HORIZONTAL[0] // right only
} else {
Self::CHARS_HORIZONTAL[1] // left only (so that there is something)
}
}),
)
}
pub fn for_inner(&self, is_first: bool, is_last: bool, style: u8) -> Self {
let color_index = self
.color_index_ptr
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Self {
color_index_ptr: self.color_index_ptr.clone(),
color_index,
theme: Rc::clone(&self.theme),
is_inner: true,
style,
idt_start: if is_first {
self.indent_start(true)
} else {
self.indent_default(true)
}
.to_string(),
idt_default: self.indent_default(false).to_string(),
idt_end: if is_last {
self.indent_end(true)
} else {
self.indent_default(true)
}
.to_string(),
idt_single: match (is_first, is_last) {
(false, false) => self.indent_default(true),
(false, true) => self.indent_end(true),
(true, false) => self.indent_start(true),
(true, true) => self.indent_single(true),
}
.to_string(),
show_comments: self.show_comments,
}
}
}
#[cfg(feature = "parse")]
pub struct CheckErrorDisplay<'a> {
e: &'a CheckError,
theme: Rc<dyn ETheme>,
pub show_comments: bool,
}
#[cfg(feature = "parse")]
impl<'a> CheckErrorDisplay<'a> {
pub fn show_comments(mut self, show_comments: bool) -> Self {
self.show_comments = show_comments;
self
}
}
#[cfg(feature = "parse")]
impl Display for CheckErrorDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.e.human_readable(
f,
&CheckErrorHRConfig {
color_index: 0,
color_index_ptr: Rc::new(AtomicU32::new(1)),
theme: Rc::clone(&self.theme),
is_inner: false,
style: CheckErrorHRConfig::STYLE_DEFAULT,
idt_start: String::new(),
idt_default: String::new(),
idt_end: String::new(),
idt_single: String::new(),
show_comments: self.show_comments,
},
)
}
}
#[allow(unused)]
impl CheckError {
pub fn new() -> Self {
CheckError(vec![])
}
fn add(mut self, v: CheckErrorComponent) -> Self {
self.0.push(v);
self
}
fn add_mut(&mut self, v: CheckErrorComponent) -> &mut Self {
self.0.push(v);
self
}
pub(crate) fn msg_str(self, s: String) -> Self {
self.add(CheckErrorComponent::Message(vec![(s, None)]))
}
pub(crate) fn msg_mut_str(&mut self, s: String) -> &mut Self {
self.add_mut(CheckErrorComponent::Message(vec![(s, None)]))
}
pub(crate) fn msg(self, s: Vec<(String, Option<EColor>)>) -> Self {
self.add(CheckErrorComponent::Message(s))
}
pub(crate) fn msg_mut(&mut self, s: Vec<(String, Option<EColor>)>) -> &mut Self {
self.add_mut(CheckErrorComponent::Message(s))
}
pub(crate) fn err(self, e: Self) -> Self {
self.add(CheckErrorComponent::Error(e))
}
pub(crate) fn err_mut(&mut self, e: Self) -> &mut Self {
self.add_mut(CheckErrorComponent::Error(e))
}
pub(crate) fn err_with_diff_src(self, e: CheckError) -> Self {
self.add(CheckErrorComponent::ErrorWithDifferentSource(e))
}
pub(crate) fn err_with_diff_src_mut(&mut self, e: CheckError) -> &mut Self {
self.add_mut(CheckErrorComponent::ErrorWithDifferentSource(e))
}
pub(crate) fn src(self, s: Vec<(SourceRange, Option<EColor>)>) -> Self {
self.add(CheckErrorComponent::Source(s))
}
pub(crate) fn src_mut(&mut self, s: Vec<(SourceRange, Option<EColor>)>) -> &mut Self {
self.add_mut(CheckErrorComponent::Source(s))
}
#[cfg(feature = "parse")]
pub fn display<'a>(&'a self, theme: impl ETheme + 'static) -> CheckErrorDisplay<'a> {
CheckErrorDisplay {
e: self,
theme: Rc::new(theme),
show_comments: true,
}
}
/// Like `display`, but doesn't use any theme (doesn't colorize its output)
#[cfg(feature = "parse")]
pub fn display_notheme<'a>(&'a self) -> CheckErrorDisplay<'a> {
self.display(themes::NoTheme)
}
/// Like `display`, but uses the default terminal theme
#[cfg(feature = "parse")]
#[cfg(feature = "ecolor-term")]
pub fn display_term<'a>(&'a self) -> CheckErrorDisplay<'a> {
self.display(themes::TermDefaultTheme)
}
/// will, unless empty, end in a newline
#[cfg(feature = "parse")]
fn human_readable(
&self,
f: &mut std::fmt::Formatter<'_>,
cfg: &CheckErrorHRConfig,
) -> std::fmt::Result {
const ADD_RIGHT_BITS: bool = false;
use crate::{parsing::SourceFrom, theme::ThemeTo};
let len = self.0.len();
for (i, component) in self.0.iter().enumerate() {
let is_first = i == 0;
let is_last = i + 1 == len;
let i = (); // to see if we use `i` anywhere else
macro_rules! indent {
($s:expr, $e:expr, $right:expr) => {
match ($s && is_first, $e && is_last) {
(false, false) => cfg.indent_default($right),
(false, true) => cfg.indent_end($right),
(true, false) => cfg.indent_start($right),
(true, true) => cfg.indent_single($right),
}
};
}
match component {
CheckErrorComponent::Message(msg) => {
let msg = colorize_str(msg, cfg.theme.as_ref());
let lines = msg.lines().collect::<Vec<_>>();
let lc = lines.len();
for (i, line) in lines.into_iter().enumerate() {
let s = i == 0;
let e = i + 1 == lc;
writeln!(f, "{}{line}", indent!(s, e, s && ADD_RIGHT_BITS))?
}
}
CheckErrorComponent::Error(err) => {
let cfg = cfg.for_inner(is_first, is_last, CheckErrorHRConfig::STYLE_DEFAULT);
err.human_readable(f, &cfg)?;
}
CheckErrorComponent::ErrorWithDifferentSource(err) => {
let cfg = cfg.for_inner(is_first, is_last, CheckErrorHRConfig::STYLE_DIFFSRC);
err.human_readable(f, &cfg)?;
}
CheckErrorComponent::Source(highlights) => {
if let Some(src) = highlights.first().map(|v| v.0.in_file.as_ref()) {
let start = highlights.iter().map(|v| v.0.start.pos()).min();
let end = highlights.iter().map(|v| v.0.end.pos()).max();
if let (Some(start_in_line), Some(end_in_line)) = (start, end) {
let start = src.get_line_start(start_in_line);
let end = src.get_line_end(end_in_line);
let (start_with_comments, end_with_comments) = (
src.pos_in_og(start_in_line, true),
src.pos_in_og(end_in_line, false),
);
let (mut start, mut end) = if cfg.show_comments {
(src.pos_in_og(start, true), src.pos_in_og(end, false))
} else {
(start, end)
};
let mut first_line_start = 0;
let first_line_nr = src
.src_og()
.line_spans()
.take_while(|l| {
if l.start() <= start_with_comments {
first_line_start = l.start();
true
} else {
false
}
})
.count();
if cfg.show_comments && first_line_start < start {
start = first_line_start;
}
let mut last_line_start = 0;
let last_line_nr = src
.src_og()
.line_spans()
.take_while(|l| {
if l.start() <= end_with_comments {
last_line_start = l.start();
if cfg.show_comments && l.end() > end {
end = l.end();
}
true
} else {
false
}
})
.count();
let src_from = match src.src_from() {
SourceFrom::File(path) => format!(" [{}]", path.to_string_lossy()),
SourceFrom::Unspecified => String::with_capacity(0),
};
if first_line_nr == last_line_nr {
writeln!(
f,
"{}{}",
indent!(true, false, ADD_RIGHT_BITS),
cfg.theme.color_to(
&format!(
"Line {first_line_nr} ({}..{}){}",
start_with_comments + 1 - first_line_start,
end_with_comments - last_line_start,
src_from,
),
EColor::InCodePositionLine
)
)?;
} else {
writeln!(
f,
"{}{}",
indent!(true, false, ADD_RIGHT_BITS),
cfg.theme.color_to(
&format!(
"Lines {first_line_nr}-{last_line_nr} ({}..{}){}",
start_with_comments + 1 - first_line_start,
end_with_comments - last_line_start,
src_from,
),
EColor::InCodePositionLine
)
)?;
}
let lines = if cfg.show_comments {
src.src_og()[start..end].line_spans().collect::<Vec<_>>()
} else {
src.src()[start..end].line_spans().collect::<Vec<_>>()
};
let lines_count = lines.len();
for (line_nr_rel, line) in lines.into_iter().enumerate() {
let last_line = line_nr_rel + 1 == lines_count;
let line_start = line.start();
let line_end = line.end();
let line = line.as_str();
let mut line_printed = false;
let mut right = 0;
for (highlight_index, (highlight_pos, color)) in
highlights.iter().enumerate()
{
if let Some(color) = *color {
let (highlight_start, highlight_end) = if cfg.show_comments
{
(
src.pos_in_og(highlight_pos.start.pos(), true),
src.pos_in_og(highlight_pos.end.pos(), false),
)
} else {
(highlight_pos.start.pos(), highlight_pos.end.pos())
};
let highlight_start = highlight_start - start;
let highlight_end = highlight_end - start;
if highlight_start < line_end && highlight_end > line_start
{
if !line_printed {
// this isn't the last line (important for indent)
writeln!(
f,
"{} {line}",
indent!(false, false, false)
)?;
line_printed = true;
}
// where the highlight starts in this line
let hl_start =
highlight_start.saturating_sub(line_start);
// highlight would be further left than cursor, so we need a new line
if hl_start < right {
right = 0;
writeln!(f)?;
}
// length of the highlight
let hl_len = highlight_end
.saturating_sub(line_start)
.saturating_sub(hl_start);
let hl_space = hl_start - right;
let print_indent = right == 0;
let hl_len = hl_len.min(line.len() - right);
right += hl_space + hl_len;
if print_indent && right != 0 {
write!(
f,
"{} ",
indent!(
false,
// is end if last_line and
// all following highlights can be put on this line
last_line
&& highlights
.iter()
.skip(highlight_index + 1)
.try_fold(
// accumulator = end position of previous highlight
highlight_pos.end().pos(),
// success if all highlights start only after the previous highlight ended: a < hl.start
|a, hl| if a < hl
.0
.start()
.pos()
{
Some(hl.0.end().pos())
} else {
None
}
)
.is_some(),
false
)
)?;
}
write!(
f,
"{}{}",
" ".repeat(hl_space),
&cfg.theme.color_to(&"~".repeat(hl_len), color)
)?;
}
}
}
if !line_printed {
// may be last line (important for indent)
writeln!(f, "{} {line}", indent!(false, last_line, false))?;
}
if right != 0 {
writeln!(f)?;
}
}
}
}
}
}
}
Ok(())
}
}
impl From<String> for CheckError {
fn from(value: String) -> Self {
Self::new().msg_str(value)
}
}
impl From<&str> for CheckError {
fn from(value: &str) -> Self {
value.to_owned().into()
}
}
impl Debug for CheckError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
#[cfg(feature = "ecolor-term")]
let e = self.display_term();
#[cfg(not(feature = "ecolor-term"))]
let e = self.display_notheme();
write!(f, "{e}")
}
}