From 9e4052cc2ee12751ba94909ff479bd03df141ac4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 7 Jan 2018 14:56:08 +0300 Subject: Test utils --- src/parser/event_parser/grammar.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/parser') diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs index 79ef8b31c..e1f717714 100644 --- a/src/parser/event_parser/grammar.rs +++ b/src/parser/event_parser/grammar.rs @@ -8,7 +8,12 @@ pub fn file(p: &mut Parser) { node(p, FILE, |p| { shebang(p); inner_attributes(p); - many(p, |p| skip_to_first(p, item_first, item)); + many(p, |p| { + skip_to_first( + p, item_first, item, + "expected item", + ) + }); }) } @@ -84,19 +89,34 @@ fn comma_list bool>(p: &mut Parser, f: F) { } -fn skip_to_first(p: &mut Parser, cond: C, f: F) -> bool +fn skip_to_first(p: &mut Parser, cond: C, f: F, message: &str) -> bool where C: Fn(&Parser) -> bool, F: FnOnce(&mut Parser), { + let mut skipped = false; loop { if cond(p) { + if skipped { + p.finish(); + } f(p); return true; } - if p.bump().is_none() { + if p.is_eof() { + if skipped { + p.finish(); + } return false; } + if !skipped { + p.start(ERROR); + p.error() + .message(message) + .emit(); + } + p.bump().unwrap(); + skipped = true; } } -- cgit v1.2.3