#![macro_use]
use std::cmp::min;
use std::fmt::Display;
use std::io::Error;
use std::path::Path;
use peg::error::ParseError;
use peg::str::LineCol;
use crate::preprocess::PreprocessInfo;
#[macro_export]
macro_rules! error {
($($arg:tt)*) => (
std::io::Error::new(std::io::ErrorKind::Other, format!($($arg)*))
)
}
pub trait ErrorExt<T> {
fn prepend_error<M: AsRef<[u8]> + Display>(self, msg: M) -> Result<T, Error>;
}
impl<T> ErrorExt<T> for Result<T, Error> {
fn prepend_error<M: AsRef<[u8]> + Display>(self, msg: M) -> Result<T, Error> {
match self {
Ok(t) => Ok(t),
Err(e) => Err(error!("{}\n{}", msg, e)),
}
}
}
pub(crate) trait PreprocessParseErrorExt<T> {
fn format_error(self, origin: Option<&Path>, input: &str) -> Result<T, Error>;
}
impl<T> PreprocessParseErrorExt<T> for Result<T, ParseError<LineCol>> {
fn format_error(self, origin: Option<&Path>, input: &str) -> Result<T, Error> {
match self {
Ok(t) => Ok(t),
Err(pe) => {
let line_index = pe
.location
.line
.checked_sub(1)
.ok_or_else(|| error!("invalid line number {}", pe.location.line))?;
let line_origin = u32::try_from(pe.location.line)
.map_err(|_| error!("invalid line number {}", pe.location.line))?;
let file_origin = match origin {
Some(path) => format!("{}:", printable(&path.display().to_string())),
None => String::new(),
};
let line = input.lines().nth(line_index).unwrap_or("");
Err(format_parse_error(
line,
&file_origin,
line_origin,
pe.location.column,
&pe.expected,
))
}
}
}
}
pub(crate) trait ConfigParseErrorExt<T> {
fn format_error(self, info: &PreprocessInfo, input: &str) -> Result<T, Error>;
}
impl<T> ConfigParseErrorExt<T> for Result<T, ParseError<LineCol>> {
fn format_error(self, info: &PreprocessInfo, input: &str) -> Result<T, Error> {
match self {
Ok(t) => Ok(t),
Err(pe) => {
let idx = min(pe.location.line, info.line_origins.len()).saturating_sub(1);
let Some((line_origin, file)) = info.line_origins.get(idx) else {
return Err(error!(
"Parse error location out of range: line {}",
pe.location.line
));
};
let line_origin = *line_origin;
let file_origin = match file {
Some(path) => format!("{}:", printable(&path.display().to_string())),
None => String::new(),
};
let line = input
.lines()
.nth(pe.location.line.saturating_sub(1))
.unwrap_or("");
Err(format_parse_error(
line,
&file_origin,
line_origin,
pe.location.column,
&pe.expected,
))
}
}
}
}
fn format_parse_error(
line: &str,
file: &str,
line_number: u32,
column_number: usize,
expected: &impl Display,
) -> Error {
let trimmed = line.trim_start();
let stripped = line.chars().count() - trimmed.chars().count();
let column = column_number.saturating_sub(1);
let offending = line.chars().nth(column).map_or_else(
|| "\\n".to_string(),
|c| {
if c.is_whitespace() && !c.is_ascii() {
c.escape_unicode().to_string()
} else {
printable(&c.to_string())
}
},
);
let padding = printable_line(
&trimmed
.chars()
.take(column.saturating_sub(stripped))
.collect::<String>(),
)
.chars()
.count();
error!(
"In line {}{}:\n\n {}\n {}^\n\nUnexpected token \"{}\", expected: {}",
file,
line_number,
printable_line(trimmed),
" ".repeat(padding),
offending,
expected
)
}
fn printable_line(text: &str) -> String {
text.split('\t')
.map(printable)
.collect::<Vec<_>>()
.join("\t")
}
#[must_use]
pub fn printable(text: &str) -> String {
text.chars()
.flat_map(|c| {
if c.is_control() {
c.escape_default().collect::<Vec<char>>()
} else {
vec![c]
}
})
.collect()
}