Sublime Forum

Syntax highlighting regex finding repeat occurences

#1

Hello!

I’m trying to create syntax highlighting for the language Inform III (used to program Yaskawa Motoman industrial robots). So far I’m happy with what I’ve come up with but now I want to do a thing I can’t figure out how to do.

Here’s an example of a simple Inform III program:

/JOB //NAME Test //POS ///NPOS 0,0,0,0,0,0 //ALIAS ///LVARS 1,0,0,0,0,0,0,0 LB000 byteVariableName //INST ///DATE 2015/11/25 13:37 ///ATTR SC,RO ///GROUP1 RB1 ///LVARS 1,0,0,0,0,0,0,0 NOP '-------------------------------- SET byteVariableName 0 INC byteVariableName '-------------------------------- END

At the moment I’m succesfully matching the first occurence of byteVariableName with this:

(?<=([0-9]{4} ))\w+|(?<=([LBIDRSPCE][0-9]{3} ))\w+

(the name can be preceeded with either a 4-digit number or at least one of those letters followed by a 3-digit number)

What I want to do is have a separate regex that will find every other occurence of byteVariabelName, and I can’t figure out how to do that, what I’ve tried so far haven’t even come close.

//Stefan

0 Likes

#2

Since this isn’t going anywhere, would it be possible instead to have the regex I have at the moment to match every occurence in the entire file?

To be clear:
If I write LB000 varName in the top of the file I want all occurences of varName to be matched no matter where in the file they are

0 Likes

#3

Short answer: No.

Long answer:

  1. Regular expressions support back-references, but Sublime Text only ever applies an expression to one line. Ever. So you can’t do it in one expression.
  2. There are begin-end-patterns that can match the same thing over multiple lines, but an end pattern can only ever match once. And matches are not exposed anywhere else.

You’ll need to use a plugin for that functionality, utilizing the view.add_region API.

Edit: I also just tested this with a .sublime-syntax file, where you can have multiple “end” patterns since it has a concept of contexts, but the backreference is only valid in a pattern that specifies “pop: true”, thus exiting the context where \1 would be defined immediately. Yes, “set” doesn’t work either.

0 Likes

#4

Well, that explains why nothing I tried worked.
Thanks for the answer, I guess I’ll have work around that some other way.

0 Likes

#5

Is it possbile to get the file to import syntax? :grin:

0 Likes