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
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
and that would imply defining it for nom::Err not for your Error type
on what did you use the ? ?
Alberto
@0X1A
preceded(_, _)(input)?
peckpeck
@peckpeck
are the _ things nom parsers or ones you wrote ?
Alberto
@0X1A
Oh fml, parser I was using there was still using generic IResult
Alberto
@0X1A
Okay, so that resolved that. I can now use IResult<I, O, MyError>, but now I'm back at not being able to use the ? propagator for my functions that return non-IResults. I'm guessing the only way to fix that would be to convert it manually because of IResult...
peckpeck
@peckpeck
for my part i strictly separated parser code from other code so i have few places where I had to manually translate from my parser errors into my application error
anything i used within parser either returned a MyError or I made it so using an equivalent of cut or i simply matched it for error handling
Evgeny Fomin
@fominok
Hello! I'm new to Nom and have a basic question, I've described it here: https://stackoverflow.com/questions/57608442/rust-nom-many-and-end-of-input. Thank you in advance!
peckpeck
@peckpeck
with nom 5 all macro are incomplete, if you want to use the complete version, your have to switch to functions
Evgeny Fomin
@fominok
@peckpeck is there any alternative to do_parse to deny some of the inputs?