use std::borrow::Cow;
use std::fmt::Write as _;
use crate::lines::LineIndex;
use crate::parse::{ParseError, ParseErrorKind, Recovery};
const TAB_COLUMNS: usize = 4;
const MAX_LINE_COLUMNS: usize = 120;
const CONTEXT_COLUMNS: usize = 40;
const CUT: &str = "...";
const CONTROL_PICTURES: u32 = 0x2400;
const DELETE_PICTURE: char = '\u{2421}';
#[derive(Clone, Debug)]
pub struct Source<'t> {
text: &'t str,
path: Option<&'t str>,
lines: Cow<'t, LineIndex>,
}
impl<'t> Source<'t> {
pub fn new(text: &'t str, path: Option<&'t str>) -> Source<'t> {
Source {
text,
path,
lines: Cow::Owned(LineIndex::new(text)),
}
}
pub fn indexed(text: &'t str, path: Option<&'t str>, lines: &'t LineIndex) -> Source<'t> {
Source {
text,
path,
lines: Cow::Borrowed(lines),
}
}
pub fn byte_offset(&self, chars: usize) -> usize {
self.lines.byte_offset(self.text, chars)
}
pub fn render(&self, name: Option<&str>, message: &str, start: usize, end: usize) -> String {
let start = self.text.floor_char_boundary(start);
let end = self.text.floor_char_boundary(end).max(start);
let line = self.lines.line(start);
let (from, to) = self.lines.bounds(self.text, line);
let text = self.text.get(from..to).unwrap_or_default();
let first = start.min(to).saturating_sub(from);
let shown = Shown::new(text, first, end.min(to).saturating_sub(from));
let column = self.lines.position(self.text, start).column;
let number = (line + 1).to_string();
let pad = " ".repeat(number.len());
let mut out = error_title(name, message);
let _ = write!(out, "\n{pad}--> ");
if let Some(path) = self.path {
push_shown(&mut out, path);
out.push(':');
}
let _ = write!(out, "{number}:{column}\n{pad} |\n{number} |");
if !shown.text.is_empty() {
out.push(' ');
out.push_str(&shown.text);
}
let carets = "^".repeat(shown.end.saturating_sub(shown.start).max(1));
let _ = write!(out, "\n{pad} | {}{carets}", " ".repeat(shown.start));
out
}
}
impl ParseError {
pub fn render(&self, source: &Source<'_>) -> String {
let mut shown: Vec<String> = (self.recoveries.iter())
.map(|recovery| recovery.render(source))
.collect();
let name = match &self.kind {
ParseErrorKind::UnknownStart(_) => {
shown.push(error_title(None, &self.message()));
return shown.join("\n\n");
}
ParseErrorKind::Label {
label, named: true, ..
} => Some(label.as_str()),
_ => None,
};
shown.push(source.render(name, &self.message(), self.position, self.end()));
shown.join("\n\n")
}
}
impl Recovery {
pub fn render(&self, source: &Source<'_>) -> String {
let end = self.end.max(self.position);
source.render(Some(&self.label), &self.message(), self.position, end)
}
}
pub fn error_title(name: Option<&str>, message: &str) -> String {
let mut out = String::from("error");
let rest = match name {
Some(name) => {
out.push('[');
push_shown(&mut out, name);
out.push(']');
match message.strip_prefix(name) {
Some(rest) if rest.is_empty() => rest,
Some(rest) => rest.strip_prefix(": ").unwrap_or(message),
None => message,
}
}
None => message,
};
if !rest.is_empty() {
out.push_str(": ");
push_shown(&mut out, rest);
}
out
}
pub fn visible(text: &str) -> String {
let mut out = String::with_capacity(text.len());
push_shown(&mut out, text);
out
}
struct Shown {
text: String,
start: usize,
end: usize,
}
impl Shown {
fn new(line: &str, first: usize, last: usize) -> Shown {
let before = line.get(..first).unwrap_or(line);
let mut width = 0;
let far = before.chars().any(|c| {
width += reach(c);
width > MAX_LINE_COLUMNS - CONTEXT_COLUMNS
});
let mut from = 0;
if far {
from = first;
let mut width = 0;
for (i, c) in before.char_indices().rev() {
width += reach(c);
if width > CONTEXT_COLUMNS {
break;
}
from = i;
}
}
let mut text = String::new();
let mut column = 0;
if from > 0 {
text.push_str(CUT);
column = CUT.len();
}
let (mut start, mut end) = (None, None);
let mut cut = false;
let mut width = column;
for (i, c) in line.get(from..).unwrap_or_default().char_indices() {
let i = from + i;
if i >= first && start.is_none() {
start = Some(column);
}
if i >= last && end.is_none() {
end = Some(column);
}
if width >= MAX_LINE_COLUMNS && i > first {
cut = true;
break;
}
let taken = push_char(&mut text, c, column);
column += taken;
width += taken.max(1);
}
let (start, end) = (start.unwrap_or(column), end.unwrap_or(column));
if cut {
text.push_str(CUT);
}
Shown { text, start, end }
}
}
fn push_shown(out: &mut String, text: &str) {
let mut column = 0;
for c in text.chars() {
column += push_char(out, c, column);
}
}
fn push_char(out: &mut String, c: char, column: usize) -> usize {
match c {
'\t' => {
let spaces = TAB_COLUMNS - column % TAB_COLUMNS;
out.extend(std::iter::repeat_n(' ', spaces));
spaces
}
'\0'..='\x1f' => {
let picture = char::from_u32(CONTROL_PICTURES + u32::from(c));
out.push(picture.unwrap_or(char::REPLACEMENT_CHARACTER));
1
}
'\x7f' => {
out.push(DELETE_PICTURE);
1
}
'\u{80}'..='\u{9f}'
| '\u{61c}'
| '\u{200e}'
| '\u{200f}'
| '\u{2028}'..='\u{202e}'
| '\u{2066}'..='\u{2069}' => {
out.push(char::REPLACEMENT_CHARACTER);
1
}
c => {
out.push(c);
columns(c)
}
}
}
fn reach(c: char) -> usize {
match c {
'\t' => TAB_COLUMNS,
c => columns(c).max(1),
}
}
fn columns(c: char) -> usize {
match u32::from(c) {
0x0300..=0x036F
| 0x0483..=0x0489
| 0x0591..=0x05BD
| 0x0610..=0x061A
| 0x064B..=0x065F
| 0x1AB0..=0x1AFF
| 0x1DC0..=0x1DFF
| 0x200B..=0x200D
| 0x2060..=0x2064
| 0x20D0..=0x20FF
| 0xFE00..=0xFE0F
| 0xFE20..=0xFE2F
| 0xFEFF
| 0xE0100..=0xE01EF => 0,
0x1100..=0x115F
| 0x2E80..=0x303E
| 0x3041..=0x33FF
| 0x3400..=0x4DBF
| 0x4E00..=0x9FFF
| 0xA000..=0xA4CF
| 0xA960..=0xA97F
| 0xAC00..=0xD7A3
| 0xF900..=0xFAFF
| 0xFE10..=0xFE19
| 0xFE30..=0xFE6F
| 0xFF00..=0xFF60
| 0xFFE0..=0xFFE6
| 0x1F300..=0x1F64F
| 0x1F680..=0x1F6FF
| 0x1F900..=0x1F9FF
| 0x1FA70..=0x1FAFF
| 0x20000..=0x2FFFD
| 0x30000..=0x3FFFD => 2,
_ => 1,
}
}