zesterer / chumsky

Write expressive, high-performance parsers with ease.
https://crates.io/crates/chumsky
MIT License
3.53k stars 147 forks source link

how properly use recursive #654

Open sineptic opened 1 month ago

sineptic commented 1 month ago

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.

wackbyte commented 1 month ago

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?

zesterer commented 1 month ago

Yes, I agree. This looks like left recursion.

sineptic commented 1 month ago

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?

zesterer commented 1 month ago

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.