From 2336aeb4c32864f53a4d4e0f72e54a174df47a60 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 16 Dec 2020 16:24:06 +0100 Subject: =?UTF-8?q?Tweak=20parser=20error=20messages=20=F0=9F=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parse/parser.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/parse/parser.rs') 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) { - 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. -- cgit v1.2.3