This question is related to this: Using regex to pass syntax-valid c++ declaration/initialization thanks to the answer of Jerry.
But now the program flow has been redesigned. It will now check the syntax of the declaration per data type.
//found int
if( /+sint$/.test(kword_search)){
//syntax check
if( //*modified regex for int declaration checking*//.test(kword_syntax_search) ){
//no error in int;
}
}
//found char
if( /+schar$/.test(kword_search)){
//syntax check
if( //*modified regex for char declaration checking*//.test(kword_syntax_search) ){
//no error in char;
}
}
//found float
if( /+sfloat$/.test(kword_search)){
//syntax check
if( //*modified regex for float declaration checking*//.test(kword_syntax_search) ){
//no error in float;
}
}
//found bool
if( /+sbool$/.test(kword_search)){
//syntax check
if( //*modified regex for bool declaration checking*//.test(kword_syntax_search) ){
//no error in bool;
}
}
So, basically the previous answer is used as this:
//found int|char|bool|float
if( /+s(?:int|float|char|bool)$/.test(kword_search)){
//syntax check
if( /^(?:\s*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[0-9]+(?:\.[0-9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*,)*\s*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[0-9]+(?:\.[0-9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*;/.test(kword_syntax_search) ){
//no error in declaration/initialization by mixing them;
}
}
I need some help to chop this regex to be in 4 sets of regex dedicated to evaluate an int,char,float and bool in the first block of code (Jerry's answer):
^(?:\s*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[0-9]+(?:\.[0-9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*,)*\s*[A-Za-z_][A-Za-z0-9]*\s*(?:=\s*(?:[A-Za-z0-9]+(?:[+\/*-][A-Za-z0-9]+)?|[0-9]+(?:\.[0-9]+)?(?:[+\/*-][0-9]+(?:\.[0-9]+)?)?|"[^"]*"|'[^']*'))?\s*;
http://regex101.com/r/aD2hA6
My goal is to tell this string inputs in my code editor if they are syntactically valid or not
int a,b,_ab;//valid
int a=0,b=333; //valid
int a, b=; //not valid
int a,,b,,; //not valid
int 1var; //not valid
int a='sts', b=false, c=23.0 // not valid
float a,b;//valid
float a=2, a=3.33//valid
float a='sfsdf';//not valid
float a==3;//not valid
float a,, b=3.0//not valid
char a, b;//valid
char a='a', b='!', c='3';//valid
char a='ab';//not valid
char a,b=3;//not valid
char a,b="a";//not valid
bool a=true, b=false;
bool a="true"; //not valid
(get the idea?)
Again... I know regex is not the right way of doing this. Thanks