regexvisual-studio-codesyntax-highlightingvscode-extensionstmlanguage

Custom language vscode extension syntax highlighting for variables and function call


I'm working on vscode extension for custom language. I've developed tmlangauge.json for syntax highlighting keywords and operators.

I find it a hard time to add syntax highlighting for variables and function calls.

The variable declaration looks like this:

let var1(integer)
let var2(string)

The tmlanguage.json for the variable is below:

"identifier": {
        "name": "variable.other.cl",
        "match": "(?i:(let)\\s*(?:(\\b[a-zA-Z][a-zA-Z0-9]*?\\b)\\s*(,?)))"
    }    

The function-call looks like this:

var1 = className.Func1()
var2 = className.Func2(arg1,arg2,...)
var3 = className->Func3()
var4 = className->Func4(arg1)

Class functions can be accessed by the dot or arrow operator. I want to highlight the Funcx in this.

The tmlanguage.json for the function call is below:

"function-call":{
        "name": "entity.name.function.ekl",
        "match": "([A-Za-z@!?][A-Za-z0-9@!?]*)\\s*\\(",
        "captures": {
            "1": {
                "name": "support.function.cl"
            }
        }
    }

Function-call grammar matches the variable declaration as well. It's unable to differentiate variables and function-call.

I'm making a mistake in the match pattern. Your help is highly appreciated.


Solution

  • Below regex for the function call solved the issue. It's able to differentiate variable vs function call.

    (?i:(?<=(\.|\>|=))\s*(\b[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*?\b)(?=\(\)?))