Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augment generated regex XML comment with original pattern and options #88175

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ private static void EmitRegexPartialMethod(RegexMethod regexMethod, IndentedText
}

// Emit the partial method definition.
writer.WriteLine("/// <remarks>");
writer.WriteLine("/// Pattern explanation:<br/>");
writer.WriteLine("/// <code>");
writer.WriteLine($"/// <remarks>");
writer.WriteLine($"/// Pattern:<br/>");
writer.WriteLine($"/// <code>{EscapeXmlComment(Literal(regexMethod.Pattern, quote: false))}</code><br/>");
if (regexMethod.Options != RegexOptions.None)
{
writer.WriteLine($"/// Options:<br/>");
writer.WriteLine($"/// <code>{Literal(regexMethod.Options)}</code><br/>");
}
writer.WriteLine($"/// Explanation:<br/>");
writer.WriteLine($"/// <code>");
DescribeExpressionAsXmlComment(writer, regexMethod.Tree.Root.Child(0), regexMethod); // skip implicit root capture
writer.WriteLine("/// </code>");
writer.WriteLine("/// </remarks>");
writer.WriteLine($"/// </code>");
writer.WriteLine($"/// </remarks>");
writer.WriteLine($"[global::System.CodeDom.Compiler.{s_generatedCodeAttribute}]");
writer.WriteLine($"{regexMethod.Modifiers} global::System.Text.RegularExpressions.Regex {regexMethod.MethodName}() => global::{GeneratedNamespace}.{regexMethod.GeneratedName}.Instance;");

Expand Down Expand Up @@ -5084,7 +5091,7 @@ private static void InsertAdditionalDeclarations(IndentedTextWriter writer, Hash
private static string Literal(char c) => SymbolDisplay.FormatLiteral(c, quote: true);

/// <summary>Formats the string as valid C#.</summary>
private static string Literal(string s) => SymbolDisplay.FormatLiteral(s, quote: true);
private static string Literal(string s, bool quote = true) => SymbolDisplay.FormatLiteral(s, quote);

private static string Literal(RegexOptions options)
{
Expand Down Expand Up @@ -5183,11 +5190,9 @@ private static string DescribeSet(string charClass) =>
{
RegexCharClass.AnyClass => "any character",
RegexCharClass.DigitClass => "a Unicode digit",
RegexCharClass.ECMADigitClass => "'0' through '9'",
RegexCharClass.ECMASpaceClass => "a whitespace character (ECMA)",
RegexCharClass.ECMAWordClass => "a word character (ECMA)",
RegexCharClass.NotDigitClass => "any character other than a Unicode digit",
RegexCharClass.NotECMADigitClass => "any character other than '0' through '9'",
RegexCharClass.NotECMASpaceClass => "any character other than a whitespace character (ECMA)",
RegexCharClass.NotECMAWordClass => "any character other than a word character (ECMA)",
RegexCharClass.NotSpaceClass => "any character other than a whitespace character",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ partial class C
partial class C
{
/// <remarks>
/// Pattern explanation:<br/>
/// Pattern:<br/>
/// <code>^(?&lt;proto&gt;\\w+)://[^/]+?(?&lt;port&gt;:\\d+)?/</code><br/>
/// Explanation:<br/>
/// <code>
/// ○ Match if at the beginning of the string.<br/>
/// ○ "proto" capture group.<br/>
Expand Down Expand Up @@ -431,7 +433,9 @@ partial class C
partial class C
{
/// <remarks>
/// Pattern explanation:<br/>
/// Pattern:<br/>
/// <code>href\\s*=\\s*(?:["'](?&lt;1&gt;[^"']*)["']|(?&lt;1&gt;[^&gt;\\s]+))</code><br/>
/// Explanation:<br/>
/// <code>
/// ○ Match the string "href".<br/>
/// ○ Match a whitespace character atomically any number of times.<br/>
Expand Down Expand Up @@ -727,7 +731,9 @@ partial class C
partial class C
{
/// <remarks>
/// Pattern explanation:<br/>
/// Pattern:<br/>
/// <code>[A-Za-z]+</code><br/>
/// Explanation:<br/>
/// <code>
/// ○ Match a character in the set [A-Za-z] atomically at least once.<br/>
/// </code>
Expand Down