Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
whentze
@whentze
ascii decimal integers
like "14"
of course std can do this, but then i'd have to wrap that in something to make it work with nom's combinators (right?)
ah, the s_expression example does this as well
Simon Thornington
@sthornington
quick question, just trying to get up to speed on nom. a lot of the docs still refer to macros, like https://github.com/Geal/nom/blob/master/doc/choosing_a_combinator.md -- in nom 5, is it safe to assume that for all of these, I should instead be looking for like-named functions ?
dangrabcad
@dangrabcad
@sthornington yes, that's right
whentze
@whentze
somehow i'm still seeing different behavior when swapping the order of arguments to alt(), even though all the lowest-level parsers are "complete" variants
is this expected?
i.e. I have alt(parser1, parser2), which errors because parser1 fails, even though parser2 would match
alt(parser2, parser1) accepts the same string
Denis Lisov
@tanriol
Does parser1 fail with an Err::Error or with an Err::Failure?
whentze
@whentze
Err::Error
Denis Lisov
@tanriol
Sounds like a bug to me.
whentze
@whentze
Err::Error(Tag) to be precise
Simon Thornington
@sthornington
okay cool thanks @dangrabcad . I'm actually pretty happy it's switched to functions, all the macros was my #1 reason for being wary of nom :)
whentze
@whentze
the parser is already quite complicated, let's see if i can make a more simple example that has the same behavior
Simon Thornington
@sthornington
do nom functional parsers have to return an impl trait, or can they also return a boxed trait ?
Denis Lisov
@tanriol
They can return anything that implements the trait.
Simon Thornington
@sthornington
what is the non-macro version of switch! ? I'm trying to parse a 'type'-prefixed streaming protocol
Denis Lisov
@tanriol
Parse the first value and then match on the result?
Simon Thornington
@sthornington
Yeah at the moment I’m doing let (input, discriminator) = one_of(“ABC”)(input)?
Then doing a match to a parser I also run on I put. Was just wondering if there was anything else I could do.
Jordan Mack
@jordanmack
Any good sources of additional Nom 5 examples?
I'm brand new to it, and I'm struggling a bit with syntax. More examples beyond the color examples would help.
Jordan Mack
@jordanmack
How would I take all a-z until ":", and error if there is something not a-z before the ":"?
Simon Thornington
@sthornington
Jordan Mack
@jordanmack
Thanks! 👍
Wait a sec, it disappeared.
Simon Thornington
@sthornington
because I am not good at gitter
would be curious to hear better ways of doing this
(I've been using nom for like 3 days so this was practice)
working through https://bodil.lol/parser-combinators/ was helpful for me to understand the whole paradigm in Rust
I think you could make a more efficient one_of for ascii A-Z by implementing your own FindToken on 'A' <= c <= 'Z' for instance
or maybe something like that already exists
Jordan Mack
@jordanmack
I actually need a-zA-Z0-9_, which is equiv to \w in regex.
I'm trying to learn Nom as well, so I want to do it the Nom way.
Simon Thornington
@sthornington
maybe implement a FindToken using is_alphanumeric ?
Jordan Mack
@jordanmack
Yes, I think I can tie that with alt or something to get the _ in there.
that simplifies it to:
fn paz(input: &str) -> IResult<&str, &str>
{
    terminated(alphanumeric0,
               nom::character::complete::char(':'))(input)
}

#[test]
fn test_az() {
    assert_eq!(paz("ABC:"), Ok(("", "ABC")));
}
Alberto
@0X1A
Hi folks. I want to use the error propagation operator in a parser function. The function using the propagator returns a custom error I've defined (we'll call it MyError). The parser function returns the usual IResult. I've tried looking at other examples for nom 5 but I can't quite figure out how this can be done. I can obviously define the From trait for MyError -> nom::Err<_> but then I would have to return an ErrorKind, where the ErrorKind would be totally unrelated since errors returned by MyError have essentially nothing to do with parsing.
peckpeck
@peckpeck
the recommended way is to define a global error type and implement ParseError on it
then all my parsers return an IResult<I,O,MyError>
this is done automatically since I use nom5 functions and use the results directly as a MyError
Alberto
@0X1A
@peckpeck See, that's what I thought as well but then I seem to get :
`?` couldn't convert the error to `nom::Err<MyError>`
When ever I use the prop error with a nom parser function. And I can't exactly impl From for Err<_> for Err<MyError> since I can't impl for a type outside my crate
peckpeck
@peckpeck
did you implement ParseError ?
Alberto
@0X1A
Yep, have impl<'a> ParseError<MyInputType<&'a str>> for MyError in my crate
? does an implicit conversion using the From trait, so I wonder if this simply isn't possible without defining From, which of course I can't do since Err is nom's
peckpeck
@peckpeck
i didn't have to define it in my code