lezer-parser/javascript

Support for nested grammars in tagged template literals

Closed this issue · 2 comments

Tagged template literals are often used to embed another language in JavaScript. It'd be nice if this grammar had built-in support for delegating to another grammar based on the template tag, ie you can register grammars against template tag identifiers, like html, css, svg, json, etc. Some editors allow registering against regexes that match the tag.

One complication is the expressions in template literals. A nested grammar should be able to parse the joined template literal strings as one character stream.

A stack of nested grammars and documents will be necessary for complex nesting. It's not uncommon to see structures like this:

html`
  <style>
    p { color: red; }
  </style>
  <div>${x ? html`<p>X</p>` : html`<p>Y</p>`</div>
  <script>...</script>
`

This should be one of the use cases unblocked by Lezer 0.15. See parseMixed.

This is now possible via a custom mixed parser wrapper.