private static int ParseTokenString(TokenStringRegistry registry, int offset, string eval, TokenString returnString) { TokenString currentString = new TokenString(); // Eat the TOKEN_START offset += TOKEN_START.Length; while (offset < eval.Length) { // Find the next TOKEN_START, comma, or TOKEN_END int tokenStartOffset = eval.IndexOf(TOKEN_START, offset); int commaOffset = eval.IndexOf(',', offset); int tokenEndOffset = eval.IndexOf(TOKEN_END, offset); if (tokenStartOffset > -1) { commaOffset = (commaOffset == -1) ? eval.Length : commaOffset; tokenEndOffset = (tokenEndOffset == -1) ? eval.Length : tokenEndOffset; } else if (commaOffset > -1) { tokenEndOffset = (tokenEndOffset == -1) ? eval.Length : tokenEndOffset; } if ((tokenStartOffset > -1) && ((tokenStartOffset < commaOffset) && (tokenStartOffset < tokenEndOffset))) { // If it's TOKEN_START, add this run to the current string, and call recursively TokenString newString = new TokenString(); currentString.Add(eval.Substring(offset, tokenStartOffset - offset)); offset = ParseTokenString(registry, tokenStartOffset, eval, newString); currentString.Add(newString); } else if ((commaOffset > -1) && (commaOffset < tokenEndOffset)) { // If it's a comma, add this run to the current string and start a new string currentString.Add(eval.Substring(offset, commaOffset - offset)); returnString.AddArgument(currentString); currentString = new TokenString(); offset = commaOffset + 1; } else { // If it's a TOKEN_END, add this run to the current string and return currentString.Add(eval.Substring(offset, tokenEndOffset - offset)); returnString.AddArgument(currentString); returnString.Token = registry.NewObject(returnString.Name); return tokenEndOffset + 1; } } return offset; }
public static TokenString Parse(TokenStringRegistry registry, string eval) { TokenString newString = new TokenString(); int offset = 0; while (offset < eval.Length) { int tokenStartOffset = eval.IndexOf(TOKEN_START, offset); if (tokenStartOffset == -1) { newString.Add(eval.Substring(offset, eval.Length - offset)); offset = eval.Length; } else { TokenString innerNewString = new TokenString(); newString.Add(eval.Substring(offset, tokenStartOffset - offset)); offset = ParseTokenString(registry, tokenStartOffset, eval, innerNewString); newString.Add(innerNewString); } } return newString; }