Skip to content

Commit

Permalink
Add multiline comment support
Browse files Browse the repository at this point in the history
  • Loading branch information
i2van committed Jul 17, 2024
1 parent 9ce8fbb commit 9822c36
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/Parsable.linq
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

#nullable enable

static class Parsable
{
private const string MultilineCommentPattern = @"/\*[^*]*\*+(?:[^/*][^*]*\*+)*/";

internal static readonly TimeSpan RegexTimeout = FromMilliseconds(100);

public static string RemoveMultilineComments(this string values) =>
Regex.Replace(values, MultilineCommentPattern, Empty, RegexOptions.None, RegexTimeout);
}

abstract class Parsable<T> : IEnumerable<T>
{
private const string Comment = "//";
Expand All @@ -32,7 +42,7 @@ abstract class Parsable<T> : IEnumerable<T>

private T ParseAndCreate(string str)
{
var regex = new Regex(Regex, ExplicitCapture, FromMilliseconds(100));
var regex = new Regex(Regex, ExplicitCapture, Parsable.RegexTimeout);

var match = regex.Match(str);
if(match.Success)
Expand All @@ -46,6 +56,7 @@ abstract class Parsable<T> : IEnumerable<T>

private T[] Parse(string values) =>
values
.RemoveMultilineComments()
.Replace(Comment, NewLine + Comment)
.Split(NewLine.ToCharArray())
.Select(static v => v.Trim())
Expand Down

0 comments on commit 9822c36

Please sign in to comment.