Greedy nested parsers

Hi!
I’m trying to implement a very simple grammar to parse strings with embedded JS snippets. Something like
Some text {{1+2}} more {{fn("haha")}} end.

The problem is that nesting the parsers using parseMixed is working as a post-processing step and thus it can’t take into account the nested grammar to decide where are the boundaries of the nested grammar.
For example, I’d expect this nested JS to be parsed as a fn("}}"), e.g. in a greedy manner, like “parse as much valid JS as you can”:
Before {{fn("}}")}} after
But, as I can see, usually nested grammars contain tokens like ![}] that just ejects as early as possible.

It looks like this topic is the closest thing I could find on the forum:

But at the same time the grammar there is much simpler and the implementation is quite complex (at least at the first glance).
Could you please point out where I could start?

This isn’t something that Lezer’s mixed parsing supports, and I don’t see an easy way to add it.

1 Like

@mif Not sure if relevant, but I wrote a bit more about nested parsers here: Proposal: a mixed-language interface accessible within ExternalTokenizer instances · Issue #17 · lezer-parser/lezer · GitHub

1 Like