How to create grammar rules for this?

I’m trying to create the following colors through grammar rules:

image

I have assembly:

  assembly: {
    match: '^\\s*!.+'
    name: 'assembly.purebasic'
  }

comment:

  comment: {
    match: ';+.*$'
    name: 'comment.line.purebasic'
  }

and the ‘!’ operator:

  operator: {
    match: '!'
    name: 'keyword.operator.purebasic'
  }

The problem I’m having is that I can’t figure out how to get the assembly rule to work. When I have the operator rule then it overrides the assembly rule completely:

How can I fix this?

The order of what you defined is very important. Matches are attempted top to bottom.

I’m not sure what the point of the operator rule is here though; from your example, both would match the assembly rule. Is the operator meant for some other kind of syntax? Edit: It won’t pick up the second line because it contains whitespace before the !.

If you want to assign a scope to the ! inside the assembly rule, you can do so with the captures property

Yes. The ! operator is treated in the same was as + or - unless it is the first thing on a line (leading whitespace is OK), at which point the line is instead colored red to indicate that it is to be treated as inline assembly.

Oh, if that’s your literal source code, then you need to escape the backslashes. There’s one level of escape reading from the file into a JS object, and another level of escape reading from the JS string to a regex. So to match \, you would need \\\\ as your pattern. In this case, to match any word character, the pattern is \\w.

That was it, thank you. I keep forgetting the double-backslash when working with patterns in CSON.