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
use std::{collections::HashSet, fmt::Write, iter};
use crate::{chars_to_string, Ast, Delimiter, PText, Text};
/// Wrap `s` in ANSI delimiters for terminal colors.
/// {90..=97} => {grey, red, green, yellow, blue, purple, cyan, white}
pub fn color_text(s: &str, c: u8) -> String {
format!("\x1b[{c}m{s}\x1b[0m")
}
#[derive(Debug, Default, Clone)]
pub struct CCMacroError {
pub red_text: Vec<PText>,
pub error: String,
pub help: Option<String>,
}
impl CCMacroError {
pub fn new(error: String, red_text: PText) -> Self {
Self {
red_text: vec![red_text],
error,
help: None,
}
}
/// Creates a formatted block of the cc optionally with red colored pointers
/// to a specific concatenation or component, then `error` is appended
/// inline.
pub fn ast_error(&self, ast: &Ast) -> String {
// enhance cc level punctuation
let colon = &color_text(":", 97);
let comma = &color_text(", ", 97);
let semicolon = &color_text("; ", 97);
let lbracket = &color_text("[", 97);
let rbracket = &color_text("]", 97);
let mut s = String::new();
let mut color_line = String::new();
// efficiency is not going to matter on terminal errors
let red_text: HashSet<PText> = self.red_text.iter().copied().collect();
if let Some(txt_init) = ast.txt_init {
let mut init = vec![];
ast.chars_assign_subtree(&mut init, txt_init);
writeln!(s, "{}{}", chars_to_string(&init), colon).unwrap();
}
// if some, color carets are active
let mut color_lvl = None;
let mut use_color_line = false;
let extend = |color_line: &mut String, color_lvl: &Option<usize>, len: usize| {
color_line.extend(iter::repeat(if color_lvl.is_some() { '^' } else { ' ' }).take(len))
};
let mut stack: Vec<(PText, usize)> = vec![(ast.txt_root, 0)];
loop {
let last = stack.len() - 1;
if let Some(txt) = ast.txt[stack[last].0].get(stack[last].1) {
match txt {
Text::Group(d, p) => {
if color_lvl.is_none() && red_text.contains(p) {
color_lvl = Some(last);
}
extend(&mut color_line, &color_lvl, d.lhs_chars().len());
if *d == Delimiter::RangeBracket {
s += lbracket;
} else {
s += d.lhs_str();
}
stack.push((*p, 0));
continue
}
Text::Chars(chars) => {
extend(&mut color_line, &color_lvl, chars.len());
for c in chars {
s.push(*c);
}
stack[last].1 += 1;
}
}
} else {
if last == 0 {
break
}
stack.pop();
let last = stack.len() - 1;
let mut unset_color = false;
if let Some(prev_last) = color_lvl {
if last == prev_last {
use_color_line = true;
unset_color = true;
}
}
match ast.txt[stack[last].0][stack[last].1] {
Text::Group(d, _) => match d {
Delimiter::Component => {
let len = ast.txt[stack[last].0].len();
if (stack[last].1 + 1) != len {
s += comma;
extend(&mut color_line, &color_lvl, 1);
}
if unset_color {
if red_text.len() == 1 {
write!(
color_line,
" component {}: {}",
len - 1 - stack[last].1,
self.error
)
.unwrap();
}
color_lvl = None;
} else if (stack[last].1 + 1) != ast.txt[stack[last].0].len() {
extend(&mut color_line, &color_lvl, 1);
}
}
Delimiter::Concatenation => {
s += semicolon;
extend(&mut color_line, &color_lvl, 1);
if use_color_line {
s += &color_text(
&format!("concatenation {}\n{}", stack[last].1, color_line),
91,
);
use_color_line = false;
color_lvl = None;
} else {
extend(&mut color_line, &color_lvl, 1);
}
color_line.clear();
s.push('\n');
}
Delimiter::RangeBracket => {
s += rbracket;
extend(&mut color_line, &color_lvl, 1);
}
_ => {
s += d.rhs_str();
extend(&mut color_line, &color_lvl, d.rhs_str().len());
}
},
_ => unreachable!(),
}
if unset_color {
color_lvl = None;
}
stack[last].1 += 1;
}
}
if let Some(ref help) = self.help {
format!("{}\n{}{} {}", self.error, s, color_text("help:", 93), help)
} else {
format!("{}\n{}", self.error, s)
}
}
}
pub fn error_and_help(error: &str, help: &str) -> String {
format!("{}\n{} {}", error, color_text("help:", 93), help)
}