Skip to content

Commit ffb82d2

Browse files
committed
Clarify comment for parse_until_before; it also stops parsing at the end of the input.
Also add a test for this behavior. I was wondering about this myself, and figure it might save others time in the future.
1 parent 06e204e commit ffb82d2

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,12 @@ impl<'i: 't, 't> Parser<'i, 't> {
403403
parse_nested_block(self, parse)
404404
}
405405

406-
/// Limit parsing to until a given delimiter. (E.g. a semicolon for a property value.)
406+
/// Limit parsing to until a given delimiter or the end of the input. (E.g.
407+
/// a semicolon for a property value.)
407408
///
408409
/// The given closure is called with a "delimited" parser
409410
/// that stops before the first character at this block/function nesting level
410-
/// that matches the given set of delimiters.
411+
/// that matches the given set of delimiters, or at the end of the input.
411412
///
412413
/// The result is overridden to `Err(())` if the closure leaves some input before that point.
413414
#[inline]

src/tests.rs

+39
Original file line numberDiff line numberDiff line change
@@ -882,3 +882,42 @@ fn procedural_masquerade_whitespace() {
882882
_ => panic!("4"),
883883
}
884884
}
885+
886+
#[test]
887+
fn parse_until_before_stops_at_delimiter_or_end_of_input() {
888+
// For all j and k, inputs[i].1[j] should parse the same as inputs[i].1[k]
889+
// when we use delimiters inputs[i].0.
890+
let inputs = vec![
891+
(Delimiter::Bang | Delimiter::Semicolon,
892+
// Note that the ';extra' is fine, because the ';' acts the same as
893+
// the end of input.
894+
vec!["token stream;extra", "token stream!", "token stream"]),
895+
(Delimiter::Bang | Delimiter::Semicolon,
896+
vec![";", "!", ""]),
897+
];
898+
for equivalent in inputs {
899+
for (j, x) in equivalent.1.iter().enumerate() {
900+
for y in equivalent.1[j + 1..].iter() {
901+
let mut ix = ParserInput::new(x);
902+
let mut ix = Parser::new(&mut ix);
903+
904+
let mut iy = ParserInput::new(y);
905+
let mut iy = Parser::new(&mut iy);
906+
907+
let _ = ix.parse_until_before::<_, _, ()>(equivalent.0, |ix| {
908+
iy.parse_until_before::<_, _, ()>(equivalent.0, |iy| {
909+
loop {
910+
let ox = ix.next();
911+
let oy = iy.next();
912+
assert_eq!(ox, oy);
913+
if let Err(_) = ox {
914+
break
915+
}
916+
}
917+
Ok(())
918+
})
919+
});
920+
}
921+
}
922+
}
923+
}

0 commit comments

Comments
 (0)