How do I make this parser recursive?

I want to make a parser that can handle a recursive data structure. E.g.

["a", ["b"]]

["a", ["b", ["c", "d"]]]

Here is my attempt https://ellie-app.com/7MdkFhPYmTDa1

The first example above should parse into AstList [AstVal "a", AstList [AstVal "b" ] ]

In listParser I tried setting item = parser, but the compiler complains with:

The `listParser` value depends on itself through the following chain of
definitions:

    ┌─────┐
    │    listParser
    │     ↓
    │    parser
    └─────┘

How can I make this parser recursive? Thanks

You are looking for Parser.lazy ?

1 Like

That was it, so simple, thanks
https://ellie-app.com/7MF2kDtLF83a1

1 Like