# String inside mixed language content

**URL:** <https://discuss.codemirror.net/t/string-inside-mixed-language-content/6942>\
**Category:** Lezer\
**Created:** [August 7, 2023, 11:43pm UTC](https://discuss.codemirror.net/t/string-inside-mixed-language-content/6942 "2023-08-07T23:43:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samueldmeyer](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/samueldmeyer/32/3532_2.png) [@samueldmeyer](https://discuss.codemirror.net/u/samueldmeyer)\
**Post date:** [August 7, 2023, 11:43pm UTC](https://discuss.codemirror.net/t/string-inside-mixed-language-content/6942/1 "2023-08-07T23:43:30Z")

</div>

I’m working on a language syntax that looks very much like JavaScript’s string interpolation, but with a much more limited language inside:

```auto
outside plain text ${functionName('testvalue}3')} endingtext

```

To parse this, I have written a basic grammar based on the Twig parser from the mixed language example:

```auto
@top Template { (directive | Text)* }

directive {
  Insert
}

@skip {space} {
  Insert { "${" DirectiveContent "}" }
}

@tokens {
  Text { ![$] Text? | "$" (@eof | ![{] Text?) }
  space { @whitespace+ }
  @precedence { space DirectiveContent }
  "${" "}"
}

```

This works great except that the closing brace inside the inner string closes the directive. How should I approach making quotes inside the DirectiveContent hide the closing brace?

---

<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:** [August 8, 2023, 6:57am UTC](https://discuss.codemirror.net/t/string-inside-mixed-language-content/6942/2 "2023-08-08T06:57:02Z")

</div>

This isn’t something that Lezer can do, really. You could include a limited JavaScript tokenizer in your outer language, but there’s no way to defer to another parser and then pick up the parsing again after that has finished an expression (the outer parser runs to completion before the inner parser gets involved).

---

<div class="post-metadata">

**Author:** ![samueldmeyer](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/samueldmeyer/32/3532_2.png) [@samueldmeyer](https://discuss.codemirror.net/u/samueldmeyer)\
**Post date:** [August 8, 2023, 3:50pm UTC](https://discuss.codemirror.net/t/string-inside-mixed-language-content/6942/3 "2023-08-08T15:50:59Z")

</div>

Thanks for the clarification. It explains why I haven’t seen any non-trivial examples of mixed language parsing without an external tokenizer. I’ll look closer at what [lezer-parser/html](https://github.com/lezer-parser/html/blob/main/src/html.grammar) does for [scriptTokens](https://github.com/lezer-parser/html/blob/main/src/tokens.js) and work from there.

---

<div class="post-metadata">

**Author:** ![samueldmeyer](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/samueldmeyer/32/3532_2.png) [@samueldmeyer](https://discuss.codemirror.net/u/samueldmeyer)\
**Post date:** [August 16, 2023, 9:22pm UTC](https://discuss.codemirror.net/t/string-inside-mixed-language-content/6942/4 "2023-08-16T21:22:33Z")

</div>

Although the original idea of using an external tokenizer is probably still the right approach for most people, I found that my language is simple enough I can include a minimal tokenizer in the outer language by expanding on the example in [SrivishnuR’s question](https://discuss.codemirror.net/t/injectgaps-in-lang-markdown-crashing-when-used-with-mixed-language-parsing/6729). It won’t fully support something like JavaScript (backticks don’t work right, for example), but I hope it will help others who are learning to use Lezer.

```auto
@top TemplateString { (Expression | StringContent)* }

@local tokens {
    ExpressionStart[closedBy=ExpressionEnd]{ "${" }
    @else StringContent
}

@precedence {
    Expression
    StringContent
}

@skip {} {
    Expression {
        ExpressionStart Interpolation ExpressionEnd
    }
    Interpolation {
        (InnerString | ScriptText)*
    }
}

@local tokens {
  // Get strings, but don't let them be terminated by escaped quotes
  InnerString { '"' (("\\" ![}] ) | ( !["] ))* '"' | "'" (("\\" ![}] ) | ( !['] ))* "'" }
  ExpressionEnd[openedBy=ExpressionStart] { "}" }
  @else ScriptText
}

```
