Skip to content

Conversation

@White-Oak
Copy link

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.

Implemented according traits for a new struct SkippingParser returned by a keep_skip function.
Moved according test from a separate module to the slice_parser_tests one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant