Code Monkey home page Code Monkey logo

Comments (7)

epage avatar epage commented on May 29, 2024

For myself, I find there are a lot of unpredicatble ways I want to skip fields in a tuple. Having something like https://docs.rs/combine/latest/combine/macro.struct_parser.html that also applies to tuples could help and be a more general form of this proposed combinator.

from nom.

coalooball avatar coalooball commented on May 29, 2024

Hello, @BGR360!

I don't think there's much need to come up with a separated_tuple function specifically, we can use separated_list instead of tuple.

fn separated_tuple(s: &str) -> IResult<&str, Vec<&str>> {
    map(
        separated_list1(
            multispace1,
            alt((tag("thing1"), tag("thing2"), tag("thing3"))),
        ),
        |x| x,
    )(s)
}

It's easy to def a function with same functions while no extra _.

FULL CODES BELOW:

use nom::{
    branch::alt, bytes::complete::tag, character::complete::multispace1, combinator::map,
    multi::separated_list1, IResult,
};

fn separated_tuple(s: &str) -> IResult<&str, Vec<&str>> {
    map(
        separated_list1(
            multispace1,
            alt((tag("thing1"), tag("thing2"), tag("thing3"))),
        ),
        |x| x,
    )(s)
}

fn main() {
    assert_eq!(
        separated_tuple("thing1 thing2   thing3").unwrap().1,
        vec!["thing1", "thing2", "thing3"]
    );
}

from nom.

BGR360 avatar BGR360 commented on May 29, 2024

@coalooball That will not work when the tuple elements are not of the same type. Plus, even if they were the same type, this doesn't provide any type-safe guarantee that I'm parsing exactly N things.

from nom.

epage avatar epage commented on May 29, 2024

What are your thoughts on the more general idea of

let (i, (value1, value2, value3)) = seq!(
    thing1,
    _: space1,
    thing2,
    _: space1,
    thing3,
).parse(i)?;

from nom.

coalooball avatar coalooball commented on May 29, 2024

@coalooball That will not work when the tuple elements are not of the same type. Plus, even if they were the same type, this doesn't provide any type-safe guarantee that I'm parsing exactly N things.

Regarding the first point, the method I provided allows heterogeneous things:

fn separated_tuple(s: &str) -> IResult<&str, Vec<&str>> {
    map(
        separated_list1(
            multispace1,
            alt((
                tag("thing1"),
                delimited(tag("\""), alphanumeric1, tag("\"")),
                tag_no_case("thing3"),
            )),
        ),
        |x| x,
    )(s)
}

assert_eq!(
    separated_tuple("THING3 thing2   \"thing1\"").unwrap().1,
    vec!["THING3", "thing2", "thing1"]
);

Concerning the second point, I don t understand type-safe guarantee refers to.

from nom.

coalooball avatar coalooball commented on May 29, 2024

What are your thoughts on the more general idea of

let (i, (value1, value2, value3)) = seq!(
    thing1,
    _: space1,
    thing2,
    _: space1,
    thing3,
).parse(i)?;

I prefer this:

let (i, parsers:iter) = seq!(seperated_parser, permutational_parsers:iter).parse(i)?;

from nom.

BGR360 avatar BGR360 commented on May 29, 2024

Regarding the first point, the method I provided allows heterogeneous things

@coalooball all of those parsers share the same return type, &str. IMO, the much more common use case is parsers that have different return types:

fn thing1(input: &str) -> IResult<&str, Thing1> {...}
fn thing2(input: &str) -> IResult<&str, Thing2> {...}
fn thing3(input: &str) -> IResult<&str, Thing3> {...}

It's not possible to use separated_list to parse a sequence of parsers with heterogeneous outputs.

Concerning the second point, I don t understand type-safe guarantee refers to.

The output of tuple((thing1, thing2, thing3)) is a tuple, (Thing1, Thing2, Thing3). Rust's type system guarantees that that output has exactly three elements, which is what I want. And nom will automatically produce parse errors if the input has more or fewer than 3 elements.

The output of separated_list(alt((a, b, c))) is Vec<T>, which could be any length. I would have to perform additional error checking on that vector to make sure it's exactly three elements long.

What are your thoughts on the more general idea of

@epage A macro that supports discarding arbitrary elements in a sequence is definitely more flexible than my idea of separated_tuple, which assumes parsing exactly one instance of the same parser between each element in the sequence. I generally prefer a non-macro solution though.

But IMO my idea aligns with the nom ideals. separated_list also assumes exactly one instance of the same parser between each sequence element.

from nom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.