private ILeaf <t1, t2, t3> ParseDelimiters(string Formula) { foreach (Dictionary <string, Type> delims in this.DelimiterLeaves) { IEnumerable <DelimResult> matches = Formula.IndexesWhere(delims, (d) => Formula.Substring(d.Index).StartsWith(d.Delimiter.Key) && d.LHS.CharacterCount('\'') % 2 == 0 && d.RHS.CharacterCount('\'') % 2 == 0 && d.LHS.CharCountMatches('(', ')') && d.RHS.CharCountMatches('(', ')'), (d1, d2) => d1.Delimiter.Key.Length > d2.Delimiter.Key.Length ? d1 : d2).DefaultIfEmpty(null); DelimResult match = matches.DefaultIfEmpty(null).LastOrDefault(d => d != null && d.LHS.CharCountMatches('(', ')') && d.RHS.CharCountMatches('(', ')') && d.LHS.CharacterCount('\'') % 2 == 0 && d.RHS.CharacterCount('\'') % 2 == 0 && !this.DelimiterLeaves.SelectMany(d2 => d2.Select(d3 => d3.Key)).Any(d4 => d.LHS.EndsWith(d4))); if (match != null) { if (match.Delimiter.Key.Equals("-") && String.IsNullOrEmpty(match.LHS)) { return(null); } return((BranchingLeaf <t1, t2, t3>)Activator.CreateInstance(match.Delimiter.Value, Formula, Parse(match.LHS), Parse(match.RHS))); } } return(null); }
internal static IEnumerable <DelimResult> IndexesWhere(this string source, IEnumerable <KeyValuePair <string, Type> > search, Func <DelimResult, bool> predicate, Func <DelimResult, DelimResult, DelimResult> conflictResolution) { DelimResult result = null; IEnumerable <DelimResult> delimResults; for (int index = 0; index < source.Length; index++) { result = null; delimResults = from s in search where index + s.Key.Length < source.Length select new DelimResult(index, source, s); foreach (DelimResult d in delimResults) { if (predicate(d)) { result = result == null ? d : conflictResolution(result, d); } } if (result != null) { yield return(result); index += result.Delimiter.Key.Length - 1; } } }