2
votes

As a self-exercise, I developed a simple assembler-like language. I'd like to add syntax highlighting for this in Atom. I have command, comments, and literals highlighting correctly, but for some reason my variables don't highlight.

Here is an example of the language:

# comment
cmd $v 1234    # comment
mov $ $v

Where $v and $ are variables, and cmd and mov are commands.

Here is my .cson for the highlighting:

'fileTypes': [
  'yasa'
]
'scopeName': 'source.yasa'
'name': 'yasa'
'patterns': [
  {
    'comment':'command'
    'match':'^[a-z]{3}'
    'name':'support.function.builtin.yasa'
  }
  {
    'comment':'variable'
    'match':'\$[a-z]?'
    'name':'variable.other.normal.yasa'
  }
  {
    'comment':'literal'
    'match':'[ 0-9]+'
    'name':'constant.numeric.yasa'
  }
  {
    'comment':'comment'
    'match': '#.*'
    'name':'comment.line.number-sign.yasa'
  }
]

Everything works fine except the \$[a-z]? in variable. As I understand it, this should match the character $ literally, followed by 0 or 1 of any letter from a to z, which is what I need. Unfortunately there is not highlighting on the variables.

1
Huh. That worked. Know why?Jackson

1 Answers

2
votes

You can use [$][a-z]? safely because inside a character class (those regex constructs defined within square brackets [...]) special regex metacharacters (or sometimes called magic characters) do not have to be escaped.

Normally, $ denotes the end of a string (or a line, it depends on the regex modifiers, and also its behavior differs among various regex flavors). Escaping it is done either with 1 slash or 2 slashes (depends on the environment). If you are unsure, the rule of thumb is to put it into a character class.