private string BlockCodeParser(string seltext, int start, ref int end)
{
string stmt = string.Empty;
string subs = seltext.Substring(start).Replace("}}", "} }").Replace(";{", "{").Replace("{;", "{");
int blockendindex = 0;
int curlyopen = 0;
int indeOfFirstCloseCurly = subs.IndexOf('}');
for (int st = 0; st < indeOfFirstCloseCurly; )//count opening curly brackets
{
int curindex = subs.IndexOf('{', st);
if (curindex >= 0 && curindex < indeOfFirstCloseCurly)//if found before first closing '}'
{ curlyopen++; st = curindex + 1; }
else break;
}
int curlyclose = 0;
for (int st = 0; st < subs.Length - 1; )//count closing curly brackets
{
int curindex = subs.IndexOf('}', st);
if (curindex >= 0)//if found
{ curlyclose++; st = curindex + 1; }
else break;
if (curlyclose == curlyopen)
{
blockendindex = curindex;//length to be extracted
break;
}
}
if (curlyopen != curlyclose)
{
MessageBox.Show(this, "Error in block declaration. Mismatch { or }");
return "";
}
string tmpstr = subs.Substring(0, blockendindex + 1).Replace('\n', ';').Replace('\r', ' ').Replace(" in ", "$#in#$").Replace(" ", string.Empty).Replace("$#in#$", " in ").Trim();
do
{
stmt = tmpstr.Replace(";;", ";");///.Replace("}", ";};")
} while (stmt.Contains(";;"));
end = blockendindex + 1;
stmt = stmt.Replace("}", ";} ");
return stmt;
}