Skip to content

Commit 631bc49

Browse files
author
bors-servo
authored
Auto merge of #161 - jyc:parse-until-before-doc, r=Manishearth
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. Thanks for the library! <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/161) <!-- Reviewable:end -->
2 parents 1cf263a + ffb82d2 commit 631bc49

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

0 commit comments

Comments
 (0)