Is there a way to escape from another language grammar?


Here’s some pseudo-code (that is valid in my language):

# random code from my language
    javascript ->
        alert("Hello, world!");
# other random code

If you know html, you can think of the javascript part as this:
<script type="text/javascript"></script>

However, in my language it’s completely valid to escape out of embedded code to form an expression and insert it into that embedded code:

$variable = "Hello, world!"/
    javascript ->

You can think of the way that works as similar to php:

<?php $variable = "Hello, world!"; ?>
<script type="text/html">
    alert("<?php echo $variable; ?>");

The problem is not implementing this, the problem is figuring out how to write something that works for it in my atom language grammar. =P
Currently, the grammar reaches an embedding section and uses the identifier (javascript, coffeescript, etc) to include that grammar. include: "source.js"

But now I have no control over the grammar until the JS grammar finishes successfully, and I’m pretty sure when it reaches my <-$variable-> it’s just going to stop working.

Is there a way I can make an escape back into my grammar temporarily?


I’m not quite sure I understand what you’re trying to do. Using your example are you trying to have your grammar apply the javascript grammar just to the section alert("Hello, world!")?


No, I already have that working. I’m trying to escape out of the middle of the javascript grammar to do my own stuff again, basically.


Depending on how you have your scopes set up you should be able to inject your language grammar back into the embedded javascript. There are a couple threads that cover grammar injections, such as



I got it working, thanks! It was really confusing because I didn’t understand for a long time that I had to write it to match EVERY class that an element had.


Glad to hear it. Would you mind posting the grammar rule you ended up using? Or maybe at least an outline of it? I’m always curious to see what tricks other people use in their grammars.


Yeah, sure. I was going to put it up on Github when it was finished, cause it’s not actually useable right now (I haven’t written the compiler yet, and there’s no documentation).

A lot of the stuff I’ve been doing has largely been experimental, so it’s very messy.

Here’s the repository: