/// <summary> /// Invokes all mappers of the given type and returns the result /// and the BNF match for each item /// </summary> /// <param name="source">The list of tokens to examine</param> /// <param name="instance">The instance to match, or <c>null</c> to match any</param> /// <typeparam name="T">The mapper type to match</typeparam> /// <returns>A sequence of the invoked mappers and the match tokens</returns> private IEnumerable <Tuple <T, Match> > GetMappers <T>(IEnumerable <Match> source, BNF.Mapper <T> instance = null) { return(source .Where(n => n.Matched) .Where(n => n.Token is BNF.Mapper <T>) .Where(n => instance == null || n.Token == instance) .Select(n => new Tuple <T, Match>( ((BNF.Mapper <T>)n.Token).Matcher(n), n ) )); }
/// <summary> /// Invokes all mappers of the given type and returns the result /// and the BNF match for each item, but does not recurse into submatches /// </summary> /// <param name="instance">The instance to match</param> /// <typeparam name="T">The mapper type to match</typeparam> /// <returns>A sequence of the invoked mappers and the match tokens</returns> public IEnumerable <Tuple <T, Match> > GetFirstLevelMappers <T>(BNF.Mapper <T> instance = null) { return(GetMappers(this.SubMatches, instance)); }
/// <summary> /// Invokes all mappers of the given type and returns the result /// and the BNF match for each item /// </summary> /// <param name="instance">The instance to match, or <c>null</c> to match any</param> /// <typeparam name="T">The mapper type to match</typeparam> /// <returns>A sequence of the invoked mappers and the match tokens</returns> public IEnumerable <Tuple <T, Match> > GetMappers <T>(BNF.Mapper <T> instance = null) { return(GetMappers(Flat, instance)); }
/// <summary> /// Invokes all mappers of the given type and returns the result, but does not recurse into submatches /// </summary> /// <param name="instance">The instance to match, or <c>null</c> to match any</param> /// <typeparam name="T">The mapper type to match</typeparam> /// <returns>A sequence of the invoked mappers</returns> public IEnumerable <T> InvokeFirstLevelMappers <T>(BNF.Mapper <T> instance = null) { return(GetFirstLevelMappers(instance).Select(x => x.Item1)); }
/// <summary> /// Gets the first mapper that has the given instance /// </summary> /// <param name="instance">The instance to find</param> /// <typeparam name="T">The mapped type</typeparam> /// <returns>The item returned by the mapper</returns> public T FirstOrDefaultMapper <T>(BNF.Mapper <T> instance) { return(InvokeMappers(instance).FirstOrDefault()); }
/// <summary> /// Gets the last mapper that has the given instance /// </summary> /// <param name="instance">The instance to find</param> /// <typeparam name="T">The mapped type</typeparam> /// <returns>The item returned by the mapper</returns> public T LastMapper <T>(BNF.Mapper <T> instance) { return(InvokeMappers(instance).Last()); }