public static IParsingProduct TryAlternativeParse( this IEnumerable <IExpressionParser> parsers, ISourceCode src, ParsingAlternationType parsingAlternationType = ParsingAlternationType.TakeFirst, ParsingOptions options = DefaultParsingOptions) { if (parsingAlternationType == ParsingAlternationType.TakeFirst) { return(parsers.Select(p => p.TryParse(src, options)).FirstNotNullOrDefault()); } if (parsingAlternationType != ParsingAlternationType.TakeLongest && parsingAlternationType != ParsingAlternationType.TakeShortest) { throw new NotSupportedException($"{parsingAlternationType} is not supported."); } var shortest = parsingAlternationType == ParsingAlternationType.TakeShortest; var replaceCmp = shortest ? -1 : 1; IParsingContextStream selectedCtx = null; IParsingProduct result = null; foreach (var parser in parsers) { var ctx = src.GetFurtherContext(); var res = parser.TryParse(src, options); if (res != null && (result == null || replaceCmp == ctx.CompareTo(selectedCtx))) { selectedCtx?.Dispose(); selectedCtx = ctx; result = res; } } selectedCtx?.Accept(); return(result); }