Sublime Forum

Regarding selector precedence in syntax definitions

#1

I’ve been reading up on the documentation regarding syntax definitions, and this part confuses me:

This is a small portion of the default Ruby.tmLanguage that ships with Sublime. It’s used to highlight @instance and @@class variables, and it does so perfectly, but I don’t quite understand how. Take this snippet of code, for instance:

@foo = 1
@@bar = 2

After the first rule runs, all that’s left is:

 = 1
@ = 2

So… how does Sublime correctly highlight both forms? Essentially, I’d appreciate it very much if somebody could give me the basic rundown of the precedence rules applied when handling these sorts of edge cases. What does the documentation mean by “a few exceptions”, and where would it be indicated that these exceptions should be accounted for? Thanks in advance for any help in this matter.

0 Likes

#2

Not quite. The syntax parses line-by-line, so the first dict matches @foo, but doesn’t match @@bar (since @ isn’t [a-zA-z_]) . Since the first dict doesn’t match @@bar, the second dict gives it a shot, and that’s where it matches.

The exceptions it’s talking about are probably when the dict has an include $self, or when using lookahead’s or lookbehind’s in the regexp.

0 Likes

#3

Ah, that’ll do it. I made the mistake of using scan, which I now know doesn’t work the same way as match. : ) Thanks, Nick.

0 Likes