Better error hanlding when parsing, added medical system

fixed grammar
This commit is contained in:
elvis
2025-08-20 19:51:03 +02:00
parent d4ade0d921
commit c58597389d
6 changed files with 432 additions and 45 deletions

View File

@ -187,48 +187,98 @@ where
}
fn reformat_error<T, S>(
e: ParseError<usize, T, &'static str>
e: ParseError<usize, T, &'static str>,
input_str: &str,
) -> Result<S, String>
where
T: Display,
{
match e {
ParseError::ExtraToken { token: (l, t, r) } => Err(format!(
"Unexpected token \"{t}\" \
ParseError::ExtraToken { token: (l, t, r) } => {
Err(format!(
"Unexpected token \"{t}\" \
between positions {l} and {r}."
)),
))
},
ParseError::UnrecognizedEof {
location: _,
expected: _,
} => Err("End of file encountered while parsing.".into()),
} => {
Err("End of file encountered while parsing.".into())
},
ParseError::InvalidToken { location } => {
Err(format!("Invalid token at position {location}."))
}
ParseError::UnrecognizedToken {
token: (l, t, r),
expected,
expected: _,
} => {
use colored::Colorize;
let mut err = format!(
"Unrecognized token \"{t}\" \
between positions {l} and {r}."
"Unrecognized token {}{}{} \
between positions {l} and {r}. ",
"\"".red(),
t.to_string().red(),
"\"".red(),
);
// Temporary debug.
err.push_str("Expected: ");
let mut it = expected.iter().peekable();
while let Some(s) = it.next() {
err.push('(');
err.push_str(s);
err.push(')');
if it.peek().is_some() {
err.push(',');
err.push(' ');
}
// // Temporary debug.
// err.push_str("Expected: ");
// let mut it = expected.iter().peekable();
// while let Some(s) = it.next() {
// err.push('(');
// err.push_str(&format!("{}", s.green()));
// err.push(')');
// if it.peek().is_some() {
// err.push(',');
// err.push(' ');
// }
// }
let right_new_line =
input_str[l..]
.find("\n")
.map(|pos| pos + l)
.unwrap_or(input_str.len());
let left_new_line =
input_str[..r].rfind("\n")
.map(|pos| pos + 1)
.unwrap_or_default();
let line_number =
input_str[..l].match_indices('\n').count() + 1;
let pre_no_color = format!("{line_number} |");
let pre = format!("{}", pre_no_color.blue());
let line_pos_l = l - left_new_line;
let line_pos_r = r - left_new_line;
err.push_str(
&format!(".\nLine {} position {} to {}:\n{}{}{}{}",
line_number,
line_pos_l,
line_pos_r,
&pre,
&input_str[left_new_line..l].green(),
&input_str[l..r].red(),
&input_str[r..right_new_line],
)
);
err.push('\n');
err.push_str(&" ".repeat(pre_no_color.len()-1));
err.push_str(&format!("{}", "|".blue()));
err.push_str(&" ".repeat(l - left_new_line));
err.push_str(&format!("{}", &"".red()));
if r - l > 2 {
err.push_str(&" ".repeat(r - l - 2));
err.push_str(&format!("{}", &"".red()));
}
Err(err)
},
ParseError::User { error } => Err(error.to_string()),
ParseError::User { error } => {
Err(error.to_string())
},
}
}
@ -238,7 +288,7 @@ fn parser_experiment(
) -> Result<(Vec<u32>, Vec<RSset>), String> {
match grammar::ExperimentParser::new().parse(translator, &contents) {
Ok(sys) => Ok(sys),
Err(e) => reformat_error(e),
Err(e) => reformat_error(e, &contents),
}
}
@ -248,7 +298,7 @@ fn parser_instructions(
) -> Result<Instructions, String> {
match grammar::RunParser::new().parse(translator, &contents) {
Ok(sys) => Ok(sys),
Err(e) => reformat_error(e),
Err(e) => reformat_error(e, &contents),
}
}