Skip to content

Commit

Permalink
Improved parser errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Aug 2, 2024
1 parent 67bc348 commit 06a5cf8
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 120 deletions.
88 changes: 61 additions & 27 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Test {
fn main() -> int {
let x: Test = Test { a: 3 };

+ a +
+;

return 8;
}
2 changes: 1 addition & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::path::PathBuf;
use std::time::Instant;

#[cfg(debug_assertions)]
pub const DEBUG_ON_ERROR: bool = true;
pub const DEBUG_ON_ERROR: bool = false;

// #[cfg(target_os = "windows")]
// use crate::root::runner::run;
Expand Down
21 changes: 19 additions & 2 deletions src/root/errors/parser_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,25 @@ pub enum ParseError {
}

pub fn create_custom_error(e: String, l: Span) -> nom::Err<ErrorTree> {
nom::Err::Error(ErrorTree::Base {
nom::Err::Error(create_custom_error_tree(e, l))
}

pub fn create_custom_error_tree(e: String, l: Span) -> ErrorTree {
ErrorTree::Base {
location: l,
kind: BaseErrorKind::External(e),
})
}
}

pub fn to_error_tree<'a>(e: nom::Err<ErrorTree<'a>>, s: Span<'a>) -> ErrorTree<'a> {
match e {
nom::Err::Incomplete(i) => {
create_custom_error_tree(
"Expected more characters".to_string(),
s
)
}
nom::Err::Error(e) => e,
nom::Err::Failure(f) => f,
}
}
6 changes: 3 additions & 3 deletions src/root/parser/handle_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn handle_error<'a>(
Ok(v) => Ok(v),
Err(e) => match &e {
nom::Err::Incomplete(x) => WErr::ne(
ParseError::ParserIncompleteErrorsNotImplemented,
ParseError::ParserIncompleteErrorsNotImplemented, // TODO
Location::builtin(),
),
nom::Err::Error(y) | nom::Err::Failure(y) => Err(handle_error_tree(y)),
Expand Down Expand Up @@ -59,10 +59,10 @@ fn handle_error_tree(e: &ErrorTree) -> WErr {
WErr::locationless(sb)
}
ErrorTree::Alt(z) => {
let mut sb = "Failed multiple parsers -".to_string();
let mut sb = "Failed multiple parsers -\n".to_string();

for (i, e) in z.iter().enumerate() {
sb += &format!("\n{}:\n", i + 1);
sb += &format!("{}:\n", i + 1);

let werr = handle_error_tree(e).to_string();
for line in werr.lines() {
Expand Down
Loading

0 comments on commit 06a5cf8

Please sign in to comment.