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/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/parse/mod.rs') diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 33de5c24..61c90f6c 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -85,8 +85,9 @@ fn node(p: &mut Parser, at_start: bool) -> Option> { } // Bad tokens. - token => { - p.diag_unexpected(token.span_with(start .. p.pos())); + _ => { + p.jump(start); + p.diag_unexpected(); return None; } }; @@ -233,6 +234,7 @@ fn paren_call(p: &mut Parser, name: Spanned) -> ExprCall { fn dict_contents(p: &mut Parser) -> (LitDict, bool) { let mut dict = LitDict::new(); let mut comma_and_keyless = true; + let mut expected_comma = None; loop { p.skip_white(); @@ -243,10 +245,15 @@ fn dict_contents(p: &mut Parser) -> (LitDict, bool) { let entry = if let Some(entry) = dict_entry(p) { entry } else { - p.diag_expected("value"); + expected_comma = None; + p.diag_unexpected(); continue; }; + if let Some(pos) = expected_comma.take() { + p.diag_expected_at("comma", pos); + } + if let Some(key) = &entry.key { comma_and_keyless = false; p.deco(Deco::DictKey.span_with(key.span)); @@ -261,7 +268,7 @@ fn dict_contents(p: &mut Parser) -> (LitDict, bool) { } if !p.eat_if(Token::Comma) { - p.diag_expected_at("comma", behind); + expected_comma = Some(behind); } comma_and_keyless = false; @@ -286,6 +293,7 @@ fn dict_entry(p: &mut Parser) -> Option { expr, }) } else { + p.diag_expected("value"); None } } -- cgit v1.2.3