chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

how properly use recursive

Open sineptic opened this issue 1 year ago • 4 comments

When i run my parser(application). There is this error

thread 'main' panicked at /home/sineptic/.cargo/registry/src/index.crates.io-6f17d22bba15001f/stacker-0.1.15/src/lib.rs:197:21:
setting stack permissions failed with: Cannot allocate memory (os error 12)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

and it freezes.

When I try to RUST_BACKTRACE=1 it run smth but, and print only memory allocation of {different count} bytes failed and then run, but don't print.

I can build without problem.

sineptic avatar Jul 27 '24 12:07 sineptic

This may mean that your parser is trying to infinitely recurse (stacker allocates more memory for the stack for deep recursion), can you provide the code for it?

wackbyte avatar Jul 27 '24 13:07 wackbyte

Yes, I agree. This looks like left recursion.

zesterer avatar Jul 29 '24 10:07 zesterer

I implement #653 and write this:

// ...
recursive(|x| {
    x.then_with(move |ctx: Context| {
        take_until(just::<_, _, Simple<char>>('`').repeated().at_least(1)).map(
            move |(chars, ticks)| match ticks.len() {
                n if n == backticks => Context::Ok(
                    [ctx.clone().into_intermediate_state().unwrap(), chars]
                        .concat(),
                    ticks,
                ),
                _ => Context::IntermediateState(
                    [
                        ctx.clone().into_intermediate_state().unwrap(),
                        chars,
                        ticks,
                    ]
                    .concat(),
                ),
            },
        )
    })
})
.map(|x| match x {
    Context::Ok(a, b) => (a, b),
    Context::IntermediateState(_) => unreachable!(),
})
// ...

And have Infinite recursion. How can I avoid this?

sineptic avatar Jul 30 '24 09:07 sineptic

I presume that you're using 0.9.

I don't see anything here that would result in infinite recursion (to be clear, do you mean infinite recursion or infinite iteration?) by itself, the problem must be elsewhere.

zesterer avatar Jul 30 '24 10:07 zesterer

@sineptic Have you been able to fix this, or are you able to share more of the code that is producing the error? Otherwise this issue can't move forward.

Zij-IT avatar Oct 22 '24 16:10 Zij-IT

Ohhh, I don't remember what I do to make it works, but this issue is irrelevant.

sineptic avatar Oct 23 '24 19:10 sineptic