Search code examples
visual-studio-codesyntax-highlightingregex-lookaroundstextmate2oniguruma

Text Mate lookbehind after nonfixed character length


I'm working on a syntax highlighting for a programming language not supported by Visual Studio Code. The highlighting is working well,but I'm facing a problem to highlight the following pieces of code:

pool[] Test1 t1;
pool[1] Test2 t2;
pool[10] Test3 t3;

I'm highlighting the word "pool" using:

"storages": {
    "patterns": [{
        "name": "storage.type.ceu",
        "match": "\\bpool\\b"
    }]
},

and it's working, but I want to highlight the words Test1, Test2 and Test3 as well.

My only idea is to use a negative look behind, like this:

(?<=pool\[\d*\]\s+)([A-Z])\w+

I created an online link with this idea: https://regexr.com/4793u

But oniguruma (the regex used by TextMate - and also Ruby) do not allow the use of lookaround. From de doc:

(?=subexp)         look-ahead
  (?!subexp)         negative look-ahead
  (?<=subexp)        look-behind
  (?<!subexp)        negative look-behind

                     Subexp of look-behind must be fixed-width.
                     But top-level alternatives can be of various lengths.
                     ex. (?<=a|bc) is OK. (?<=aaa(?:b|cd)) is not allowed.

                     In negative look-behind, capturing group isn't allowed,
                     but non-capturing group (?:) is allowed.

Does anyone know any alternatives to highlight this syntax?


Solution

  • You could use capture groups. Here I capture 3 groups but only assign a scope to the 2nd one.

    "storages": {
        "patterns": [{
            "match": "^(\w+\[\d*\])\s+(\w+)\s+(\w+);$",
            "captures": {
                "2": {
                    "name": "storage.type.ceu"
                }
            }
        }]
    },