Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
zserik
@zserik
is there some way to avoid a part of the code duplication for handling such a flag (e.g. at least at some levels it would include a switch from a streaming parser to a complete parser, I suppose)?
Denis Lisov
@tanriol
That's why I'm talking about a flag and not about duplicating every parser :-)
zserik
@zserik
is there some best practise on how to implement such a flag?
Denis Lisov
@tanriol
No idea, I haven't dealt with this yet.
I'm not sure whether it's possible to reproduce nom 4's approach of having an input type wrapper for that.
Elliot Stern
@PipocaQuemada

I'm trying to write a scheme parser.

flat_map(tag("("), |_| {
  flat_map(listContents, |l| {
    map(tag(")"), |_| l)})})(i)

fails with

error[E0507]: cannot move out of `l`, a captured variable in an `Fn` closure
  --> src/parser.rs:80:31
   |
79 |           flat_map(listContents, |l| {
   |                                   - captured outer variable
80 |             map(tag(")"), |_| l)})})(i)
   |                               ^ move occurs because `l` has type `ast::LispVal`, which does not implement the `Copy` trait

I've tried map(tag(")"), move |_| l) and flat_map(listContents, move |l| {, but I can't seem to figure out how to fix that error.

zserik
@zserik
wouldn't delimited be more appropriate there?
this error occurs because flat_map expects an Fn, which isn't allowed to move out of outer variables.
Elliot Stern
@PipocaQuemada
Hmm. Looks like I could use that, there, but that doesn't really help me figure out how to use flat_map to parse e.g. dotted lists (i.e. (foo bar . baz))
gamma-delta
@gamma-delta
hello!
i'm trying to use nom to parse meshes i write in a text file, like really really simple svg sort of
how do I parse an f32?
as in, is there a function that will consume characters until it finishes with the f32?
hm that was easy
zserik
@zserik
@PipocaQuemada |_| l.clone() maybe?
gamma-delta
@gamma-delta
OK I have a legitimate issue this time
I would like to parse stuff like this: 1, 3
so, f32, maybe whitespaces, comma, maybe whitespaces, and another f32
/// Parses a Point.
fn point(input: &str) -> IResult<&str, Point> {
    let (input, x) = recognize_float(input)?;
    // allow spaces between number & comma
    let (input, _) = take_while(is_space)(input)?;
    // the comma
    let (input, _) = char(',')(input)?;
    // more maybe whitespace
    let (input, _) = take_while(is_space)(input)?;
    // And the y
    let (input, y) = recognize_float(input)?;

    // Return OK!
    Ok((x, y))
}
but, it looks like take_while doesn't work on characters or something?
error[E0271]: type mismatch resolving `<&str as nom::traits::InputTakeAtPosition>::Item == u8`
  --> trident\src\parse.rs:17:22
   |
17 |     let (input, _) = take_while(is_space)(input)?;
   |                      ^^^^^^^^^^ expected `char`, found `u8`

error[E0271]: type mismatch resolving `<&str as nom::traits::InputTakeAtPosition>::Item == u8`
  --> trident\src\parse.rs:21:22
   |
21 |     let (input, _) = take_while(is_space)(input)?;
   |                      ^^^^^^^^^^ expected `char`, found `u8`

error[E0308]: mismatched types
  --> trident\src\parse.rs:26:12
   |
26 |     Ok((x, y))
   |            ^ expected tuple, found `&str`
   |
   = note:  expected tuple `(f32, f32)`
           found reference `&str`
i don't understand what is wrong
please help?
zserik
@zserik
maybe take_while(|x| x.is_ascii_whitespace()) or something like that
and you probably need to change Ok((x,y)) to Ok(Point(x,y)) or something like that (I don't know your type definition of Point...)...
gamma-delta
@gamma-delta
It's pub type Point = (f32, f32) dw :)
zserik
@zserik
hm
gamma-delta
@gamma-delta
is_a(" \t")(input)?; seems to work
But now, it thinks y is a (f32, f32)?
oh
i messed up the return
How do you convert some other kind of error into an IResult?
specifically the error from .parse()
zserik
@zserik
to parse floats maybe use nom::number::streaming::float
gamma-delta
@gamma-delta
i thought that took bytes though?
like the 4 bytes of a f32 in memory, not the textual representation
zserik
@zserik
s/streaming/complete/;
ah, ok.
gamma-delta
@gamma-delta
oh nope you're right
that would be big endian float parsing :-)
gamma-delta
@gamma-delta
OK
Is there a way to tell nom about comments?
like "Hey at any point if I see a # just ignore everything till eol"
zserik
@zserik
you could try to insert calls to opt or ss
so.
gamma-delta
@gamma-delta
after every statement?
owch
zserik
@zserik
maybe you should split the parser into multiple parts 1. parse comments and escapes, return byte slices, fold them, then run a second parser on that or so...