Comments (7)
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.
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.
@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.
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 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.
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.
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)
- separated_list1 by multipspace0 sep failed HOT 3
- Allow tag/is_not/similar to take u8 in addition to arrays and slices
- docs: Dead link in README
- alt short circuits with Incomplete return HOT 3
- unsigned integer underflow HOT 3
- using `.clone()` on a double reference HOT 1
- Nom's error is referencing data owned by the calling function
- Any other approach to handle left recursion
- How to parse until a range of tags HOT 3
- Mention difference between is_not and take_until in their respective docs.
- I was going to complain about an issue in the function take_while_m_n, but [update : and I will do] ...
- Add u48 number parser HOT 1
- `tag_no_case` panicks while matching certain unicode capitalized characters.
- Ergonomics of `alt()` taking tuple versus slice HOT 3
- A new release for 8.0?
- FromStr-esque parsing trait HOT 1
- Float parsing with trailing `e` broken
- A fix has to be implemented by those dependencies HOT 7
- FlatMap does not properly implement Parser HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nom.