Format a [`ParseError`] as a human-readable diagnostic message.
(error: &ParseError)
| 19 | |
| 20 | /// Format a [`ParseError`] as a human-readable diagnostic message. |
| 21 | pub(crate) fn format_parse_error(error: &ParseError) -> String { |
| 22 | match error { |
| 23 | ParseError::SyntaxError(e) => format_syntax_error(e), |
| 24 | ParseError::UnexpectedEndOfFile(expected, _, _) => { |
| 25 | let expected = friendly_token_list(expected); |
| 26 | if expected.is_empty() { |
| 27 | "Syntax error: unexpected end of file".to_string() |
| 28 | } else { |
| 29 | format!("Syntax error: unexpected end of file, expected {expected}") |
| 30 | } |
| 31 | } |
| 32 | ParseError::UnexpectedToken(expected, found, _) => { |
| 33 | let found = friendly_token_name(found); |
| 34 | let expected = friendly_token_list(expected); |
| 35 | if expected.is_empty() { |
| 36 | format!("Syntax error: unexpected token {found}") |
| 37 | } else { |
| 38 | format!("Syntax error: unexpected token {found}, expected {expected}") |
| 39 | } |
| 40 | } |
| 41 | ParseError::UnclosedLiteralString(kind, _) => { |
| 42 | use mago_syntax::ast::LiteralStringKind; |
| 43 | match kind { |
| 44 | LiteralStringKind::SingleQuoted => { |
| 45 | "Syntax error: unclosed single-quoted string".to_string() |
| 46 | } |
| 47 | LiteralStringKind::DoubleQuoted => { |
| 48 | "Syntax error: unclosed double-quoted string".to_string() |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | ParseError::RecursionLimitExceeded(_) => { |
| 53 | "Syntax error: maximum nesting depth exceeded".to_string() |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn format_syntax_error(error: &SyntaxError) -> String { |
| 59 | match error { |