public SyntaxPattern Add(IFunctionDeclaration declaration) { var syntaxPattern = new SyntaxPattern(declaration); var overlap = _sorted.GetViewBetween(syntaxPattern, syntaxPattern); switch (overlap.Count) { case 0: // no overlap => just add it _sorted.Add(syntaxPattern); break; case 1: // one overlap exists => merge with new declaration syntaxPattern = overlap.First(); MergeDeclaration(syntaxPattern, declaration); break; default: // multiple overlaps => join them to new pattern // overlap is a view - only valid when _sorted is not changed foreach (var pattern in overlap) { foreach (var member in pattern.Members) { MergeDeclaration(syntaxPattern, member); } } _sorted.RemoveWhere(x => x.Overlaps(syntaxPattern)); // remove existing overlaps _sorted.Add(syntaxPattern); // add the new bundle break; } return(syntaxPattern); }
static void MergeDeclaration(SyntaxPattern syntaxPattern, IFunctionDeclaration declaration) { if (syntaxPattern.Members.Contains(declaration)) { return; } syntaxPattern.MinArgumentCount = Math.Min(syntaxPattern.MinArgumentCount, declaration.MandatoryRightArgumentCount()); if (syntaxPattern.MaxArgumentCount.HasValue) { var max = declaration.MaxRightArgumentCount(); syntaxPattern.MaxArgumentCount = !max.HasValue ? null : (int?)Math.Max(syntaxPattern.MaxArgumentCount.Value, max.Value); } syntaxPattern.Members.Add(declaration); }