Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
nulldg committed Aug 14, 2024
2 parents d95bae2 + 522789e commit ff97b24
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 219 deletions.
23 changes: 13 additions & 10 deletions DiscordChatExporter.Core/Markdown/Parsing/AggregateMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@

namespace DiscordChatExporter.Core.Markdown.Parsing;

internal class AggregateMatcher<T>(IReadOnlyList<IMatcher<T>> matchers) : IMatcher<T>
internal class AggregateMatcher<TContext, TValue>(
IReadOnlyList<IMatcher<TContext, TValue>> matchers
) : IMatcher<TContext, TValue>
{
public AggregateMatcher(params IMatcher<T>[] matchers)
: this((IReadOnlyList<IMatcher<T>>) matchers)
{
}
public AggregateMatcher(params IMatcher<TContext, TValue>[] matchers)
: this((IReadOnlyList<IMatcher<TContext, TValue>>)matchers) { }

public ParsedMatch<T>? TryMatch(StringSegment segment)
public ParsedMatch<TValue>? TryMatch(TContext context, StringSegment segment)
{
ParsedMatch<T>? earliestMatch = null;
ParsedMatch<TValue>? earliestMatch = null;

// Try to match the input with each matcher and get the match with the lowest start index
foreach (var matcher in matchers)
{
// Try to match
var match = matcher.TryMatch(segment);
var match = matcher.TryMatch(context, segment);

// If there's no match - continue
if (match is null)
continue;

// If this match is earlier than previous earliest - replace
if (earliestMatch is null || match.Segment.StartIndex < earliestMatch.Segment.StartIndex)
if (
earliestMatch is null
|| match.Segment.StartIndex < earliestMatch.Segment.StartIndex
)
earliestMatch = match;

// If the earliest match starts at the very beginning - break,
Expand All @@ -35,4 +38,4 @@ public AggregateMatcher(params IMatcher<T>[] matchers)

return earliestMatch;
}
}
}
35 changes: 19 additions & 16 deletions DiscordChatExporter.Core/Markdown/Parsing/IMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@

namespace DiscordChatExporter.Core.Markdown.Parsing;

internal interface IMatcher<T>
internal interface IMatcher<in TContext, TValue>
{
ParsedMatch<T>? TryMatch(StringSegment segment);
ParsedMatch<TValue>? TryMatch(TContext context, StringSegment segment);
}

internal static class MatcherExtensions
{
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(
this IMatcher<T> matcher,
public static IEnumerable<ParsedMatch<TValue>> MatchAll<TContext, TValue>(
this IMatcher<TContext, TValue> matcher,
TContext context,
StringSegment segment,
Func<StringSegment, T> transformFallback)
Func<TContext, StringSegment, TValue> transformFallback
)
{
// Loop through segments divided by individual matches
var currentIndex = segment.StartIndex;
while (currentIndex < segment.EndIndex)
{
// Find a match within this segment
var match = matcher.TryMatch(
segment.Relocate(
currentIndex,
segment.EndIndex - currentIndex
)
context,
segment.Relocate(currentIndex, segment.EndIndex - currentIndex)
);

if (match is null)
Expand All @@ -38,7 +38,10 @@ public static IEnumerable<ParsedMatch<T>> MatchAll<T>(
match.Segment.StartIndex - currentIndex
);

yield return new ParsedMatch<T>(fallbackSegment, transformFallback(fallbackSegment));
yield return new ParsedMatch<TValue>(
fallbackSegment,
transformFallback(context, fallbackSegment)
);
}

yield return match;
Expand All @@ -50,12 +53,12 @@ public static IEnumerable<ParsedMatch<T>> MatchAll<T>(
// If EOL hasn't been reached - transform and yield remaining part as fallback
if (currentIndex < segment.EndIndex)
{
var fallbackSegment = segment.Relocate(
currentIndex,
segment.EndIndex - currentIndex
);
var fallbackSegment = segment.Relocate(currentIndex, segment.EndIndex - currentIndex);

yield return new ParsedMatch<T>(fallbackSegment, transformFallback(fallbackSegment));
yield return new ParsedMatch<TValue>(
fallbackSegment,
transformFallback(context, fallbackSegment)
);
}
}
}
}
3 changes: 3 additions & 0 deletions DiscordChatExporter.Core/Markdown/Parsing/MarkdownContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DiscordChatExporter.Core.Markdown.Parsing;

internal readonly record struct MarkdownContext(int Depth = 0);
Loading

0 comments on commit ff97b24

Please sign in to comment.