I was recently looking into the feasibility of working on a Razor templating syntax for C# when I eventually ran into a problem I don’t think I can work around without modifying the embedded syntaxes. Take this Razor snippet:
<p>This is a test</p>
@{
string[] teamMembers = {"Matt", "Joanne", "Robert", "Nancy"};
foreach (var name in teamMembers)
{
<p>@name</p>
}
Array.Reverse(teamMembers);
foreach (var reversedItem in teamMembers)
{
<p>@reversedItem</p>
}
}
The @{ identifier starts a C# code block. The last } terminates it. How am I supposed to differentiate the last brace with any of the ones in the middle?
When you include another syntax, the with_prototype tag lets you inject matches into EVERY context. Because of this, there is no way I can properly check that last brace. If there was a way to only inject it into the main context (or whatever context was the target to be injected), then I could easily detect it.
The only workaround I can think of would be convincing the author of the language syntax to include a special embedding context that would be as simple as:
embed:
- match: '(?=\})'
pop: true
- include: main
It would be nice not to have to do that, though.