I am writing a custom parser using regular expressions, but I can't work out how to match functions.
An example of a function in my custom language is:
function int add(int num1, int num2){
return num1 + num2;
}
My tokenizer uses RegEx to get the next token and remove it from the source code string supplied earlier. This means that when it comes to parsing a function, I can be sure that the code will start with the function statement. I currently have the following expression:
^([\s]*function[\s]+[a-zA-Z][a-zA-Z0-9]*[\s]+[a-zA-Z][a-zA-Z0-9]*[\s]*\(([\s]*[a-zA-Z][a-zA-Z0-9]*[\s]+[a-zA-Z][a-zA-Z0-9]*[\s]*)*\)[\s]*\{.*\}.*)$
It is very long, but it successfully matches these two functions:
function void log(string msg){
Console.log(msg);
}
and
function int add(int num1 int num2){
return num1 + num2;
}
I want to be able to split the arguments by a comma.
I could make the comma required after a parameter, but then the last parameter would end with a comma.
I could make the comma optional after a parameter, but then the user would be able to not put a comma in.
I need to be able to only require the comma between parameters, otherwise it will mess up my parser later. How can I edit my expression to look for a comma between arguments?
Thank you very much for your time.
(int num1, int num2). This is just\(([\s]*[a-zA-Z][a-zA-Z0-9]*[\s]+[a-zA-Z][a-zA-Z0-9]*[\s]*)*\). I need to check that the parameters are split by a comma.