private ParseEntity GetParsedTokens(Match m) { // Handle each match group var head = new ParseEntity(".", m, null); var list = head.children = new List <ParseEntity>(); foreach (var gn in MyRegex.GetGroupNames()) { if (gn != "OPT" && gn != "0") { var g = m.Groups[gn]; if (g.Success) { foreach (Capture c in g.Captures) { list.Add(new ParseEntity(gn, c, head)); } } } } list.Sort((p1, p2) => p1.start - p2.start); // Put in groupings foreach (Capture c in m.Groups["OPT"].Captures) { var f = list.FindIndex(p1 => p1.start > c.Index); var l = list.FindLastIndex(p1 => p1.start + p1.length < c.Index + c.Length); var p = new ParseEntity(null, c, head) { children = list.GetRange(f, l - f + 1) }; list.RemoveRange(f, l - f + 1); p.children.ForEach(delegate(ParseEntity pe) { pe.parent = p; }); list.Insert(f, p); } #if DEBUG for (var i = 0; i < list.Count; i++) { Debug.WriteLine(string.Format("Match {3} = \"{1}\" ({0}:{2})", list[i].start, list[i].value, list[i].length, list[i].name)); } #endif return(head); }