peruse icon indicating copy to clipboard operation
peruse copied to clipboard

new skip_keep function

Open White-Oak opened this issue 9 years ago • 0 comments

Adds a 'sugar' parser that tries to skip as much as possible and to keep essential element(s).

    use peruse::parsers::*;
    use peruse::slice_parsers::lit;
    let keep = one_of(vec![lit(3), lit(4)]);
    let skip = one_of(vec![lit(1), lit(2)]);
    let parser = keep_skip(keep, skip).repeat();
    let input = [1, 4, 2, 1, 4, 4, 3, 1, 5];
    assert_eq!(parser.parse(&input), Ok((vec![4, 4, 4, 3], &input[8..])));
    //Ok(([4, 4, 4, 3], [5])) 
    let parser = keep_skip(lit(4), one_of(vec![lit(1), lit(2)])).repeat();
    let input = [1, 4, 2, 1, 4, 4, 3];
    assert_eq!(parser.parse(&input), Ok((vec![4, 4, 4], &input[6..])));
    //Ok(([4, 4, 4], [3]))

I found it useful to skip useless elements such as comments, new lines etc.

Documented the function and added a test of it.

White-Oak avatar Dec 31 '15 01:12 White-Oak