Sublime Forum

Matching subgroups for syntax highlight, and overriding keywords at the same time (tmLanguage)

#1

Apologies for the somewhat vague topic. I am developing a loosely lisp language, and have added basic highlighting for it in a modified tmLanguage file. What I’m trying to do is highlight these separately:

1 - keywords: define, lambda etc
2 - function calls: hash etc
3 - parentheses

(define moo (lambda ...)
(hash "somestring")
(moo ...)

In the above example, I would like the parens to be one colour, the keywords to have their own colour, and the calls to hash/moo to have their own colouring as well (donsn’t need to recognise that moo is a defined function, just that it’s the first thing to come up after the paren

Now I tried a simple

\(\b([A-Za-z_.]*)

and that happily finds the function call examples, as long as it’s above the check for parens. However, it matches the paren too. It also clobbers the keywords regardless of where I put them in order in the file.

I’m hoping that if I can do the match but return only the matched text (excluding the paren), then that should allow me to keep the paren / keyword matching intact - or should I be looking elsewhere?

0 Likes

#2

FWIW, I’m quite happy to change to sublime syntax. It’s a fairly small rule set and if that’s cleaner / easier to deal with then feel free to suggest :slight_smile:

0 Likes

#3

First, maybe you are insterested in https://github.com/sublimehq/Packages/pull/2387 if you think the current built-in Lisp syntax highlighting is bad.


That’s not how the syntax engine works. The engine always parse the text forward. That’s said, once something has been consumed, basically you never go back (lookbehind is not recommended due to performance and it’s a bad practice). ST 4 has “go back” feature (branch & fail) but I don’t think you have to use it, maybe.

Hmm… but I can’t find “how the syntax engine works” information on the docs page. I guess it just assumes you know how a typical parser works.

Unless you have to support editors other than ST, .sublime-syntax is definitely recommended. It’s much more powerful and much easier to read/write.

To convert .tmLanguage to .sublime-syntax, just

  1. Open the .tmLanguage file
  2. Open the Command Palette
  3. Type convert syntax and there should be a command for it
    image
  4. PackageDev can help you a lot if you are going to write .sublime-syntax
0 Likes

#4

Thanks much, will have a look at this after work this evening!

0 Likes