Skip to content

Clarify comment for parse_until_before; it also stops parsing at the end of the input. #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ impl<'i: 't, 't> Parser<'i, 't> {
parse_nested_block(self, parse)
}

/// Limit parsing to until a given delimiter. (E.g. a semicolon for a property value.)
/// Limit parsing to until a given delimiter or the end of the input. (E.g.
/// a semicolon for a property value.)
///
/// The given closure is called with a "delimited" parser
/// that stops before the first character at this block/function nesting level
/// that matches the given set of delimiters.
/// that matches the given set of delimiters, or at the end of the input.
///
/// The result is overridden to `Err(())` if the closure leaves some input before that point.
#[inline]
Expand Down
39 changes: 39 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,42 @@ fn procedural_masquerade_whitespace() {
_ => panic!("4"),
}
}

#[test]
fn parse_until_before_stops_at_delimiter_or_end_of_input() {
// For all j and k, inputs[i].1[j] should parse the same as inputs[i].1[k]
// when we use delimiters inputs[i].0.
let inputs = vec![
(Delimiter::Bang | Delimiter::Semicolon,
// Note that the ';extra' is fine, because the ';' acts the same as
// the end of input.
vec!["token stream;extra", "token stream!", "token stream"]),
(Delimiter::Bang | Delimiter::Semicolon,
vec![";", "!", ""]),
];
for equivalent in inputs {
for (j, x) in equivalent.1.iter().enumerate() {
for y in equivalent.1[j + 1..].iter() {
let mut ix = ParserInput::new(x);
let mut ix = Parser::new(&mut ix);

let mut iy = ParserInput::new(y);
let mut iy = Parser::new(&mut iy);

let _ = ix.parse_until_before::<_, _, ()>(equivalent.0, |ix| {
iy.parse_until_before::<_, _, ()>(equivalent.0, |iy| {
loop {
let ox = ix.next();
let oy = iy.next();
assert_eq!(ox, oy);
if let Err(_) = ox {
break
}
}
Ok(())
})
});
}
}
}
}