public static int _SplitArgumentList(string actionText,
int start,
int targetChar,
int separatorChar,
IList<System.Tuple<string, int>> args)
{
if (actionText == null)
{
return -1;
}
actionText = Regex.Replace(actionText, "//[^\\n]*", "");
int n = actionText.Length;
//System.Console.WriteLine("actionText@" + start + "->" + (char)targetChar + "=" + actionText.Substring(start, n - start));
int p = start;
int last = p;
while (p < n && actionText[p] != targetChar)
{
int c = actionText[p];
switch (c)
{
case '\'':
p++;
while (p < n && actionText[p] != '\'')
{
if (actionText[p] == '\\' && (p + 1) < n &&
actionText[p + 1] == '\'')
{
p++; // skip escaped quote
}
p++;
}
p++;
break;
case '"':
p++;
while (p < n && actionText[p] != '\"')
{
if (actionText[p] == '\\' && (p + 1) < n &&
actionText[p + 1] == '\"')
{
p++; // skip escaped quote
}
p++;
}
p++;
break;
case '(':
p = _SplitArgumentList(actionText, p + 1, ')', separatorChar, args);
break;
case '{':
p = _SplitArgumentList(actionText, p + 1, '}', separatorChar, args);
break;
case '<':
if (actionText.IndexOf('>', p + 1) >= p)
{
// do we see a matching '>' ahead? if so, hope it's a generic
// and not less followed by expr with greater than
p = _SplitArgumentList(actionText, p + 1, '>', separatorChar, args);
}
else
{
p++; // treat as normal char
}
break;
case '[':
p = _SplitArgumentList(actionText, p + 1, ']', separatorChar, args);
break;
default:
if (c == separatorChar && targetChar == -1)
{
string arg = actionText.Substring(last, p - last);
int index = last;
while (index < p && char.IsWhiteSpace(actionText[index]))
{
index++;
}
//System.out.println("arg="+arg);
args.Add(Tuple.Create(arg.Trim(), index));
last = p + 1;
}
p++;
break;
}
}
if (targetChar == -1 && p <= n)
{
string arg = actionText.Substring(last, p - last).Trim();
int index = last;
while (index < p && char.IsWhiteSpace(actionText[index]))
{
index++;
}
//System.out.println("arg="+arg);
if (arg.Length > 0)
{
args.Add(Tuple.Create(arg.Trim(), index));
}
}
p++;
return p;
}