Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alt! + map! + call! strange behavior #58

Closed
kgv opened this issue Jul 2, 2015 · 5 comments
Closed

alt! + map! + call! strange behavior #58

kgv opened this issue Jul 2, 2015 · 5 comments

Comments

@kgv
Copy link

kgv commented Jul 2, 2015

alt! + map! + call! have strange behavior.

This code work. See to closure in map!:

named!(range<&[u8], Range>,
    alt!(
        chain!(
            start: take_char ~
            tag!("-") ~
            end: take_char,
            || {
                debug!("range: (start, end): ({:?}, {:?})", start, end);
                Range {
                    start: start,
                    end: end,
                }
            }
        ) |
        map!(
            take_char,
            |c| {
                debug!("range: c: {:?}", c);
                Range {
                    start: c,
                    end: c,
                }
            }
        )
    )
);

If we try to wrap closure in map! by call!, it will not work:

...
        map!(
            take_char,
            call!(|c| {
                debug!("range: c: {:?}", c);
                Range {
                    start: c,
                    end: c,
                }
            })
        )
...

Error:

src/parser.rs:212:13: 212:14 error: unexpected end of macro invocation
src/parser.rs:212             })
                              ^

But if we use map! without alt! it have different behavior. Next code work:

named!(literal<&[u8], Expr>,
    map!(
        many1!(take_char),
        call!(|cs| {
            debug!("literal: cs: {:?}", cs);
            Expr::Literal {
                chars: cs,
            }
        })
    )
);

Without call! it not work:

named!(literal<&[u8], Expr>,
    map!(
        many1!(take_char),
        |cs| {
            debug!("literal: cs: {:?}", cs);
            Expr::Literal {
                chars: cs,
            }
        }
    )
);

Error:

src/parser.rs:220:9: 220:10 error: expected ident, found |
src/parser.rs:220         |cs| {
                          ^
@Geal
Copy link
Collaborator

Geal commented Jul 3, 2015

Hi! Thanks for your report, I'm on it

@Geal Geal closed this as completed in 240f3b2 Jul 3, 2015
@Geal
Copy link
Collaborator

Geal commented Jul 3, 2015

It should work with that commit. You do not have to use call! in map!, flat_map! and others like these, they use it already.

@kgv
Copy link
Author

kgv commented Jul 3, 2015

Now map! does not require call! wrapper for expression. But map_opt! and map_res! still require it.

For example:

Ok:

named!(parse_u32<&[u8], u32>, map_res!(map_res!(digit, str::from_utf8), call!(|s: &str| s.parse::<u32>())));

Err:

named!(parse_u32<&[u8], u32>, map_res!(map_res!(digit, str::from_utf8), |s: &str| s.parse::<u32>()));

@Geal
Copy link
Collaborator

Geal commented Jul 3, 2015

ok, I'll fix them as well

@Geal Geal reopened this Jul 8, 2015
@Geal
Copy link
Collaborator

Geal commented Jul 8, 2015

Hi!

I think 94fbe95 will fix it for map_opt!and map_res!

@Geal Geal closed this as completed Aug 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants