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