diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-12-16 16:24:06 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-12-16 16:24:06 +0100 |
| commit | 2336aeb4c32864f53a4d4e0f72e54a174df47a60 (patch) | |
| tree | e45a039b82b62231a5bde9d58c28cb3df713a77e /src/parse/parser.rs | |
| parent | 6bbedeaa2c6e0068e2fb6602cbf0002fb6a6ce03 (diff) | |
Tweak parser error messages 🔮
Diffstat (limited to 'src/parse/parser.rs')
| -rw-r--r-- | src/parse/parser.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 047f6e4c..a8e5883a 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -38,7 +38,7 @@ impl<'s> Parser<'s> { self.f.diags.push(diag); } - /// Eat the next token and add a diagnostic that it was not the expected + /// Eat the next token and add a diagnostic that it is not the expected /// `thing`. pub fn diag_expected(&mut self, thing: &str) { let before = self.pos(); @@ -60,9 +60,16 @@ impl<'s> Parser<'s> { self.diag(error!(pos, "expected {}", thing)); } - /// Add a diagnostic that the given `token` was unexpected. - pub fn diag_unexpected(&mut self, token: Spanned<Token>) { - self.diag(error!(token.span, "unexpected {}", token.v.name())); + /// Eat the next token and add a diagnostic that it is unexpected. + pub fn diag_unexpected(&mut self) { + let before = self.pos(); + if let Some(found) = self.eat() { + let after = self.pos(); + self.diag(match found { + Token::Invalid(_) => error!(before .. after, "invalid token"), + _ => error!(before .. after, "unexpected {}", found.name()), + }); + } } /// Add a decoration to the feedback. |
