Is there a way to escape from another language grammar?


#1

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

# random code from my language
script:
    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!"/
script:
    javascript ->
        alert("<-$variable->");
    >/
/

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

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

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?


#2

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!")?


#3

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.


#4

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

or


#5

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.


#6

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.


#7

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: https://github.com/Aarilight/language-khp