private string CurlyBracketParser(string comm, int start, ref int end)
{
string str = string.Empty;
int curlyopen = 0, curlyclose = 0;
for (int i = comm.IndexOf('{', start); i < comm.Length; i++)
{
if (comm.ElementAt(i).Equals('{')) curlyopen++;
else if (comm.ElementAt(i).Equals('}')) curlyclose++;
if (curlyopen == curlyclose)
{
end = i + 1 - start;
//if(start < comm.Length)
str = comm.Substring(start, end).Replace("}}", "} }").Replace(";{", "{").Replace("{;", "{").Replace("}", ";} ");
start = i + 1;
break;
}
}
if (curlyopen != curlyclose)
{
CommandRequest cmdprn = new CommandRequest();
cmdprn.CommandSyntax = "write(\"Error in block declaration. Mismatch { or }\",fp)";
analytics.ExecuteR(cmdprn, false, false); /// for printing command in file
return "ERROR";
}
str = Regex.Replace(str, @";+", ";");//multi semicolon to one ( no space between them)
//str = Regex.Replace(str, @"}\s+;", "} ");//semicolon after close }
str = Regex.Replace(str, @";\s*;", ";");//multi semicolon to one(space between them)
str = Regex.Replace(str, @"}\s*;\s*}", "} }");//semicolon between two closing } }
str = Regex.Replace(str, @"{\s*;", "{ ");//semicolon immediatly after opening {
str = Regex.Replace(str, @";\s*{", "{ ");//semicolon immediatly after opening {
if (str.Contains("else"))
{
str = Regex.Replace(str, @"}\s*;*\s*else", "} else");//semicolon before for is needed. Fix for weird bug.
//str = Regex.Replace(str, @"\s*;*\s*else", "else");//semicolon before for is needed. Fix for weird bug.
}
///if .. else if logic ///
if ((str.Trim().StartsWith("if") || str.Trim().StartsWith("else")) && comm.Length > end + 1)
{
string elsestr = string.Empty;
if (start + 1 < comm.Length)
elsestr = comm.Substring(start + 1);
int originalLen = elsestr.Length;
elsestr = Regex.Replace(elsestr, @";*\s*else", " else").Trim();
int newLen = elsestr.Length;
if (elsestr.StartsWith("else"))
{
int end2 = 0;
str = str + CurlyBracketParser(elsestr, 0, ref end2);
end = end + end2 + (originalLen - newLen + 1);
}
}
return str;
}