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

How get values from pusher! macro? #55

Closed
nnovikov opened this issue Jun 25, 2015 · 4 comments
Closed

How get values from pusher! macro? #55

nnovikov opened this issue Jun 25, 2015 · 4 comments

Comments

@nnovikov
Copy link

Hi!
I used pusher!() macro with FileProducer. But generated code doesn't return parser results.
For example, I have

pub struct Test {...}
...
named!(parse_test<&[u8],Test>, ...)

I want get [Test] or iterable object of Test sequence.
Does nom have appropriate macro or example?

@Geal
Copy link
Collaborator

Geal commented Jun 26, 2015

Hi,

it depends on what you want to do. If the data you want to parse can be entirely loaded into memory at once, you don't need pusher, you just call parse-test with the data slice.
The producers are consumers are here for streaming problems, where data can have no end. pusher calls the function repeatedly on the input, so if there was a result, it may not be what you want: first one parsed? Last one? a Vec? But then, wouldn't that Vec grow too big?
The consumers are here to be more flexible in producer usage. They allow the creation of a state machine, and the ability to seek within the input (if possible).

Will the producer be useful for your use case? By the way, you do not need to mess with producers and consumers for parser testing. Just load your test file and refer to an indexed slice of that file.

@nnovikov
Copy link
Author

I want parse a large file. And want do something with struct Test, for example (pseudocode):

let mut f = FileProducer("myfile.bin");
for i in f.into_iter(parse_test) {
   println!("{:?}", i)
   // do something
}

or

let mut f = FileProducer("myfile.bin");
loop {
   // parse as reading from file handler
   let t: Test = parse_test(f);
   println!("{:?}", t)
   // do something
}

With pusher!() I can parse file, but can't return structs from macros for do something.

@Geal
Copy link
Collaborator

Geal commented Jun 28, 2015

I see. I could provide another structure that will just apply the same parser over and over. Or provide an iterator

@Geal
Copy link
Collaborator

Geal commented Jul 3, 2015

Hi!

I think the commit 700c942 should do what you want.

Basically, this structure will wrap a producer, and has a step method that you call with a closure. It will buffer data as needed, but will not loop as the consumer does. Instead, it returns StepperState::Continue to let you decide what to do.

Let me know if that could work for you.

@Geal Geal closed this as completed Sep 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