full-moon icon indicating copy to clipboard operation
full-moon copied to clipboard

Exponential time explosion with deeply nested binary expressions

Open lizreu opened this issue 1 year ago • 2 comments

Minimal reproducible example:

return (((((((((((((((("a" .. a) .. ",") .. b) .. ",") .. c) .. ",") .. d) .. ",") .. e) .. ",") .. f) .. ",") .. g) .. ",") .. h) .. ",") .. i

On my hardware, full_moon took >100s to parse this expression in release mode.

I had a very similar issue in my hand-rolled pest parser for Lua, which drove me to switch to full_moon. Admittedly, it was much worse with pest, because it really didn't handle that sort of recursion for a number of reasons.

While it's easy to rewrite this statement manually, this is still valid Lua, and the Lua parser itself has no issues parsing this in linear time. Incidentally, this is how TypeScriptToLua outputs interpolated string expressions.

lizreu avatar Jul 02 '23 15:07 lizreu

I have a feeling this might be fixed by the ongoing parser rewrite, where we try to remove the backtracking in the parser: https://github.com/Kampfkarren/full-moon/pull/274

I think the baseline Lua 5.1 syntax is complete, so if you can get it compiling enough for your case, I wonder if it indeed improves for this case

JohnnyMorganz avatar Jul 02 '23 15:07 JohnnyMorganz

Can confirm, this is fixed in the parser rewrite and is basically instant

JohnnyMorganz avatar Sep 30 '23 16:09 JohnnyMorganz