Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
matrixbot
@matrixbot
bspeice Currently I'm getting type errors with both let (_, (a, b)): (&[u8], (u8, u8)) = tuple((bits(take(4usize)), bits(take(4usize))))(buf).expect("Parse failed"); and let (_, (a, b)): (&[u8], (u8, u8)) = bits(tuple((take(4usize), take(4usize))))(buf).expect("Parse failed");.
bspeice (Noting that the take in question has been imported from the bits::complete)
Denis Lisov
@tanriol
bits(tuple(...)) should work. However, you may need to use some dark magic for type inference to work... for me it required bits::<_, _, (_, _), _, _>(tuple(...))
James Carl
@crazycarl

I like Nom. It's incredibly fast and very maintainable.
I think the documentation for the whitespace module hasn't been updated yet. I just know that in the source code, ws! is marked as deprecated.

Looking at the source... is sep the right function to use here? Is this function finished?

prbs23
@prbs23_gitlab
I am trying to figure out if an issue with unicode characters I'm running into is a bug, or intentional. Specifically I'm looking at the "character" parsers (nom::character::streaming::{char, anychar}). It looks like when using &[u8] as the input type these parsers don't correctly handle multi-byte characters. For example the following works:
assert_eq!(nom::character::streaming::anychar::<_, (_, nom::error::ErrorKind)>(&b"A"[..]), Ok((&b""[..], 'A')));
But this does not:
assert_eq!(nom::character::streaming::anychar::<_, (_, nom::error::ErrorKind)>("\u{2140}".as_bytes()), Ok((&b""[..], '\u{2140}')));
Instead it returns: Ok(([133, 128], 'รข'))
This issue doesn't appear to happen when the input is a String type
Is this expected or intended behavior? It seems a little incorrect given that rust characters are by definition utf8 encoded
peckpeck
@peckpeck
this seems normal to me, when you parse a byte slice, you intend to process bytes one by one, whereas when you parse a string you intend to process characters (in rust sense) one by one
Adrien FAURE
@adfaure
Hello, I am trying to do a simple parser that consumes alphanumeric char and "-". It is not clear how to do that for me.
Is it possible to write a function like alphanumeric1 dedicated to my use case ? Thanks!
Adrien FAURE
@adfaure

I did something like:

fn uuid_parser(s: &str) -> IResult<&str, Uuid, VerboseError<&str>> {
     map_res(recognize(uuidchar), Uuid::parse_str)(s)
}

fn uuidchar<T, E: ParseError<T>>(s: T) -> IResult<T, T, E>
where
  T: InputTakeAtPosition,
  <T as InputTakeAtPosition>::Item: AsChar,
{
  s.split_at_position1_complete(|item| {
      let ch = item.as_char();
      !(ch == '-' || is_alphanumeric(ch as u8))
  }, ErrorKind::AlphaNumeric)
}

#[test]
fn test() {
    println!("{:?}", uuid_parser("c15a23cd-22d8-4351-b738-396b274599f8"));
}

However, it looks like a bit hacky. Is there a simpler way to do that?
I basically copied the function : https://docs.rs/nom/5.0.1/src/nom/character/complete.rs.html#571

Denis Lisov
@tanriol
take_while1 will probably be more readable.
Adrien FAURE
@adfaure
Yes, but take_while1 needs &[u8], I could not understand how to work with this function.
Denis Lisov
@tanriol
That's really not obvious from the module path, but it does work for strings too.
Adrien FAURE
@adfaure

By doing:

println!("{:?}", take_while1(is_alphanumeric)("c15a23cd-22d8-4351-b738-396b274599f8"));

I get error:

error[E0271]: type mismatch resolving `<&str as nom::InputTakeAtPosition>::Item == u8`
   --> src/main.rs:207:22
    |
207 |     println!("{:?}", take_while1(is_alphanumeric)("c15a23cd-22d8-4351-b738-396b274599f8"));
    |                      ^^^^^^^^^^^ expected char, found u8

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.
error: Could not compile `trust`.
Denis Lisov
@tanriol
The one not working with strings here is nom::character::is_alphanumeric, not take_while1
Adrien FAURE
@adfaure
Using alphanumeric1 is even worse
Denis Lisov
@tanriol
Something like take_while1(|c| c.is_alphanumeric() || c == '-') should work
Being more precise, take_while1(|c| c.is_ascii_hexdigit() || c == '-')
Adrien FAURE
@adfaure
\0/ Ok, it works. Thank you. It was simple as I expected...
I could not find it myself.
eignnx
@eignnx
Hi all, is there a way to sequence parsers so that they concatenate the &strs they match? Like tuple but where I get one &str back instead of an n-tuple of &strs?
matrixbot
@matrixbot
bspeice Given that the &strs could be non-contiguous, I don't think that's possible. You could use that tuple to build a new String though.
eignnx
@eignnx
@matrixbot ahh I see :/ that's too bad. Thanks
Denis Lisov
@tanriol
@eignnx Maybe check recognize?
Alberto
@0X1A
Hi folks, does anyone know if nom_methods supports nom 5's non-macro functions? AFAICT from looking at the macros it seems like it doesn't?
Denis Lisov
@tanriol
IIUC, one does not need nom_methods to have nom parsers implemented as methods :-)
Alberto
@0X1A
Oh wow, all this time I was still thinking parser functions had some magic and had to be fn x(input: i) -> IResult!
Good to know
prbs23
@prbs23_gitlab
Hey folks, is there an existing parser for matching a character that is inside a range of values? For example match any character between 'A' and 'Z'
character::streaming::one_of() is close, but it appears you need to enumerate every valid character
eignnx
@eignnx
@tanriol recognize is exactly what I was looking for, thank you!
Alberto
@0X1A
@tanriol So it turns out choice combinators don't quite work with parser methods :\
Denis Lisov
@tanriol
What do you mean - the fact changes are not undone if the branch fails?
Alberto
@0X1A
No, methods can't be passed to alt as it expects functions with only one argument
Denis Lisov
@tanriol
You can pass closures like |input| my_struct.method_parser(input)
Alberto
@0X1A
Gracias :pray:
Geoff Shannon
@RadicalZephyr
hi all, I'm trying to write a combinator that ensures that the first character is alphabetic, but the remainder of the string is alphabetic. Is there a better way than parsing with alphanumeric1 and then checking the first character is alphabetic?
Michael P. Jung
@bikeshedder
@RadicalZephyr You mean something like that?
named!(parse_identifier<CompleteStr, String>, do_parse!(
    first: take_while1!(char::is_alphabetic) >>
    rest: take_while!(char::is_alphanumeric) >>
    (first.0.into() + rest.0)
));
Michael P. Jung
@bikeshedder
I just realized I'm still using nom 4 and CompleteStr is no longer a thing. whops
Gotta try migrating to nom 5. Looks like I'm going to like the changes. Less macro magic.
Denis Lisov
@tanriol
@RadicalZephyr Something like recognize(tuple((alpha1, alphanumeric0)))?
Geoff Shannon
@RadicalZephyr
@tanriol heh, I got too focused on ensuring that only the first character was alphanumeric, but that do exactly what I want! thanks!
Michael P. Jung
@bikeshedder
Is there anywhere a migration guide for nom 4->5. When starting with nom 4 I got confused by the CompleteStr and with nom 5 it seams everything has changed.
Denis Lisov
@tanriol
Something like this?
jtenner
@jtenner
Hey guys. How can I assert the end of input?
    input.len() == 0;
Just want to be idiomatic :)
jtenner
@jtenner
I think I have a fundamental misunderstanding here.
jtenner
@jtenner
I come from a peg background, and I know what to expect in terms of parsing logic.
I'm just having a hard time with the rust api and rust itself.
fn parse_expression(expression: &str) -> IResult<&str, DiceNode, &str> {
    let (input, taken) = space0(expression)?;