Skip to content

Commit

Permalink
Improve expected note if only one item would have been appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivorforce committed Apr 26, 2024
1 parent c15adca commit 030535a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub fn parse_program(content: &str) -> RResult<(ast::Block, Vec<ErrorRecovery<us
},
ParseError::UnrecognizedEof { location, expected } => {
RuntimeError::error("File ended unexpectedly.").in_range(location..location)
.with_note(RuntimeError::note(format!("Expected one of: {}", expected.iter().map(|s| rem_first_and_last(s)).join(" ")).as_str()))
.with_note(make_expected_note(expected))
}
ParseError::UnrecognizedToken { token: (start, token, end), expected } => {
RuntimeError::error("Unrecognized token.").in_range(start..end)
.with_note(RuntimeError::note(format!("Expected one of: {}", expected.iter().map(|s| rem_first_and_last(s)).join(" ")).as_str()))
.with_note(make_expected_note(expected))
}
ParseError::ExtraToken { token: (start, token, end) } => {
RuntimeError::error("Extra token.").in_range(start..end)
Expand All @@ -48,3 +48,10 @@ pub fn parse_program(content: &str) -> RResult<(ast::Block, Vec<ErrorRecovery<us

Ok((ast, errors))
}

fn make_expected_note(expected: Vec<String>) -> RuntimeError {
match &expected[..] {
[one] => RuntimeError::note(format!("Expected: {}", rem_first_and_last(one)).as_str()),
expected => RuntimeError::note(format!("Expected one of: {}", expected.iter().map(|s| rem_first_and_last(s)).join(" ")).as_str()),
}
}

0 comments on commit 030535a

Please sign in to comment.