# Greedy nested parsers

**URL:** <https://discuss.codemirror.net/t/greedy-nested-parsers/4622>\
**Category:** Lezer\
**Created:** [June 28, 2022, 4:27pm UTC](https://discuss.codemirror.net/t/greedy-nested-parsers/4622 "2022-06-28T16:27:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mif](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/mif/32/2212_2.png) [@mif](https://discuss.codemirror.net/u/mif)\
**Post date:** [June 28, 2022, 4:27pm UTC](https://discuss.codemirror.net/t/greedy-nested-parsers/4622/1 "2022-06-28T16:27:34Z")

</div>

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:

> [@Making a context-sensitive grammar](https://discuss.codemirror.net/t/making-a-context-sensitive-grammar/3760):
>
> Hi, just wanted to share something I’ve been working on ([github.com/energeticsproject/uqbar](http://github.com/energeticsproject/uqbar)). OK, so here’s a simple grammar: 
> 
> > **Simple Grammar**
> >
> > @top Reaction { reaction } @skip { space } reaction { Reagents { Substance ("+" Substance)\* } "-\>" Products { Substance ("+" Substance)\* } "~" Rate { Embed } } @external tokens embedTokenizer from "./tokens" { Embed } @tokens { Substance { $[a-zA-Z\_] $[a-zA-Z\_0-9]\* } space { $[\t\n\r]+ } }…

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?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [June 28, 2022, 4:54pm UTC](https://discuss.codemirror.net/t/greedy-nested-parsers/4622/2 "2022-06-28T16:54:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ashtonsix](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/ashtonsix/32/1639_2.png) [@ashtonsix](https://discuss.codemirror.net/u/ashtonsix)\
**Post date:** [June 28, 2022, 6:56pm UTC](https://discuss.codemirror.net/t/greedy-nested-parsers/4622/3 "2022-06-28T18:56:16Z")

</div>

@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](https://github.com/lezer-parser/lezer/issues/17)
