2 hours ago · Writing · 0 comments

I've been writing a LOT of Gleam recently. So it felt like it was probably time for me to get my parser combinator library (aka. parz) into a fully usable state Since I wrote it back in 2024 (wow that long ago huh?) I hadn't really had a need for it. Recently I wanted to write some small parsers to help me with my site but the library was still missing one thing that would make it actually sorta useful - recursive parsing The actual implementation isn't really difficult, it's basically just defining a combinator that delays actually calling a parser which then lets a parser reference itself, for example: /// Takes a thunk that will be lazily evaluated to a parser. This makes it /// possible to define recursive parsers pub fn lazy( thunk: fn() -> Parser(a), ) -> fn(String) -> Result(ParserState(a), String) { fn(state) { thunk()(state) } } // using it then looks something like this fn group() { lazy(fn() { between(str("("), choice([constant(), group()]), str(")")) // ^ we can reference…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.