Multiple grammar matches but only one gets parsed


Trying to crate a custom syntax highlighter, and it’s working fine for the limited use cases I’m interested in, however one thing I noticed is that once a match is made and the string is classed as whatever, no further matches will be considered. Therefore this type of stuff is probably done elsewhere? All the “tutorials” I can find online seem to handle it all in grammar.

What I’m after is changing the color of strings enclosed in *stars* even though that’s part of test that was already matched by another rule which classes it as string.quoted.single.

Any help is appreciated.


You handle matching within a match through nested patterns, and you can loop back to other parts of the same grammar with include statements. Here’s a good example: the grammar checks for the string html, followed by a backtick, and everything between that and the next backtick it encounters is given the string.quoted.template.html.js scope and then matched against the three patterns following it (which include two rules in the repository and the grammar for HTML). If you want to loop back to the top level of the current grammar, use include: '$self'.

I’ve also built a grammar template with annotations for reference. It might be helpful.


Thank you, I was looking at language-yaml just now. Good stuff, I appreciate it.


I found that the patterns tag doesn’t work when you use match and not begin and end. Is there a way to get around that?


Not to my knowledge. patterns is for matching as-yet-unmatched content inside a zone dictated by the two matches.

Ignore the above. I was wrong.

'match': 'stuff'
  '0': # or whatever capture you need
    'patterns': [
        # patterns go here


I stand corrected.