Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -125,16 +125,16 @@ internal sealed partial class Amd64InstructionSample
@"(?<operands>(\S[^#]*?)?)\s*" +
@"(?<comment>#.*)?$",
RegexOptions.ExplicitCapture)]
private static partial Regex EncDisassemblySplit();
private static partial Regex EncDisassemblySplit { get; }

[GeneratedRegex(@"^\s*,?\s*(?<op>[^\(,]*(\([^\)]*\))?)?(?<rest>.+$)?", RegexOptions.ExplicitCapture)]
private static partial Regex EncOperandSplit();
private static partial Regex EncOperandSplit { get; }

[GeneratedRegex(@"\[.*\]({1to[0-9]+})?$")]
private static partial Regex EncOperandIsMemOp();
private static partial Regex EncOperandIsMemOp { get; }

[GeneratedRegex(@"\[rip.*\]({1to[0-9]+})?$")]
private static partial Regex EncOperandIsMOp();
private static partial Regex EncOperandIsMOp { get; }

private static readonly HashSet<string> allOperands = new HashSet<string>();

Expand Down Expand Up @@ -255,7 +255,7 @@ public Amd64InstructionSample(string disassembly_)

if (Debug.debug) Console.WriteLine($"new sample: {disassembly}");

var match = EncDisassemblySplit().Match(disassembly);
var match = EncDisassemblySplit.Match(disassembly);

if (Debug.debug)
{
Expand Down Expand Up @@ -299,7 +299,7 @@ private static List<string> parseOperands(string operandDisassembly)

while (rest?.Length != 0)
{
var opMatch = EncOperandSplit().Match(rest);
var opMatch = EncOperandSplit.Match(rest);

string op = opMatch.Groups["op"].ToString();
operands.Add(op);
Expand Down Expand Up @@ -593,7 +593,7 @@ public SuffixFlags parseSuffix()
for (int i = 0; i < operands.Count; i++)
{
string operand = operands[i];
bool memop = EncOperandIsMemOp().IsMatch(operand);
bool memop = EncOperandIsMemOp.IsMatch(operand);

if (memop)
{
Expand All @@ -603,7 +603,7 @@ public SuffixFlags parseSuffix()

accounted += hasSIB ? 6 : 5;

if (EncOperandIsMOp().IsMatch(operand))
if (EncOperandIsMOp.IsMatch(operand))
{
if (i == 0)
{
Expand Down Expand Up @@ -661,19 +661,19 @@ internal sealed partial class Amd64InstructionTableGenerator
private List<Amd64InstructionSample> samples = new List<Amd64InstructionSample>();

[GeneratedRegex(@"^\s+0x00000000")]
private static partial Regex AssemblyPrefix();
private static partial Regex AssemblyPrefix { get; }

// The '0x' prefix is not included in the regex match.
[GeneratedRegex(@"^\s*0x(?<address>[0-9a-fA-F]+)", RegexOptions.ExplicitCapture)]
private static partial Regex AssemblyAddress();
private static partial Regex AssemblyAddress { get; }

// NOTE: APX instructions push2/push2p/pop2/pop2p are not causing gdb to report an illegal instruction,
// which is causing problems. So manually disallow them.
// NOTE: we don't disqualify disassembly with `{evex}` in the text: there are some cases where an instruction
// can be encoded with either an EVEX or VEX encoding, and the disassembler will annotate the instruction with
// `{evex}` to indicate it is not the canonical encoding.
[GeneratedRegex(@"((push2)|(pop2)|(\{vex\})|(\{bad\})|(\(bad\))|(\srex(\.[WRXB]*)?\s*(#.*)?$))")]
private static partial Regex BadDisassembly();
private static partial Regex BadDisassembly { get; }

private List<(Map, int)> regExpandOpcodes;

Expand Down Expand Up @@ -737,7 +737,7 @@ private void ParseSamples()
{
//if (Debug.debug) Console.WriteLine($"line: {line}");

var match = AssemblyAddress().Match(line);
var match = AssemblyAddress.Match(line);
if (!match.Success)
{
continue;
Expand All @@ -759,7 +759,7 @@ private void ParseSamples()
continue;
}

if (!BadDisassembly().IsMatch(sample))
if (!BadDisassembly.IsMatch(sample))
{
try
{
Expand Down
24 changes: 12 additions & 12 deletions src/coreclr/ilasm/GrammarExtractor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

string fileContent = File.ReadAllText(filePath);

var match = GetRegexExtractMarkers().Match(fileContent);
var match = GetRegexExtractMarkers.Match(fileContent);
if (!match.Success)
{
Console.Error.WriteLine("Could not find %% markers");
Expand All @@ -27,7 +27,7 @@
string grammar = match.Groups[2].Value;

// Remove any text in {}
var regexRemoveTextInBraces = GetRegexRemoveTextInBraces();
var regexRemoveTextInBraces = GetRegexRemoveTextInBraces;
string previousGrammar;

do
Expand All @@ -37,14 +37,14 @@
} while (grammar != previousGrammar);

// Change keyword identifiers into the string they represent (lowercase)
grammar = GetRegexKeywordIdentifiers().Replace(grammar, m => $"'{m.Groups[1].Value.ToLowerInvariant()}'");
grammar = GetRegexKeywordIdentifiers.Replace(grammar, m => $"'{m.Groups[1].Value.ToLowerInvariant()}'");

// Change assembler directives into their string (lowercase with a period)
grammar = GetRegexAssemblerDirectives().Replace(grammar, m => $"'.{m.Groups[1].Value.ToLowerInvariant()}'");
grammar = GetRegexAssemblerDirectives.Replace(grammar, m => $"'.{m.Groups[1].Value.ToLowerInvariant()}'");

// Handle special punctuation
grammar = GetRegexEllipsis().Replace(grammar, "'...'");
grammar = GetRegexDcolon().Replace(grammar, "'::'");
grammar = GetRegexEllipsis.Replace(grammar, "'...'");
grammar = GetRegexDcolon.Replace(grammar, "'::'");

// Print the output header
Console.Write(@"// Licensed to the .NET Foundation under one or more agreements.
Expand Down Expand Up @@ -79,20 +79,20 @@ Auxiliary lexical tokens
internal static partial class Patterns
{
[GeneratedRegex(@"^(.*)%%(.*)%%", RegexOptions.Singleline)]
internal static partial Regex GetRegexExtractMarkers();
internal static partial Regex GetRegexExtractMarkers { get; }

[GeneratedRegex(@"\s*([^'])\{[^{}]*\}", RegexOptions.Singleline)]
internal static partial Regex GetRegexRemoveTextInBraces();
internal static partial Regex GetRegexRemoveTextInBraces { get; }

[GeneratedRegex(@"\b([A-Z0-9_]+)_\b", RegexOptions.Singleline)]
internal static partial Regex GetRegexKeywordIdentifiers();
internal static partial Regex GetRegexKeywordIdentifiers { get; }

[GeneratedRegex(@"\b_([A-Z0-9]+)\b", RegexOptions.Singleline)]
internal static partial Regex GetRegexAssemblerDirectives();
internal static partial Regex GetRegexAssemblerDirectives { get; }

[GeneratedRegex(@"\bELLIPSIS\b", RegexOptions.Singleline)]
internal static partial Regex GetRegexEllipsis();
internal static partial Regex GetRegexEllipsis { get; }

[GeneratedRegex(@"\bDCOLON\b", RegexOptions.Singleline)]
internal static partial Regex GetRegexDcolon();
internal static partial Regex GetRegexDcolon { get; }
}
8 changes: 4 additions & 4 deletions src/coreclr/inc/CrstTypeTool/CrstTypeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,10 @@ internal sealed partial class TypeFileParser

// Compile regular expressions for detecting comments and tokens in the parser input.
[GeneratedRegex(@"//.*")]
private static partial Regex CommentRegex();
private static partial Regex CommentRegex { get; }

[GeneratedRegex(@"^(\s*(\S+)\s*)*")]
private static partial Regex TokenRegex();
private static partial Regex TokenRegex { get; }

// Input is lexed into an array of tokens. We record the index of the token being currently parsed.
private Token[] m_tokens;
Expand Down Expand Up @@ -630,10 +630,10 @@ private void InitTokenStream()
while ((line = file.ReadLine()) != null)
{
// Remove comments from the current line.
line = CommentRegex().Replace(line, "");
line = CommentRegex.Replace(line, "");

// Match all contiguous non-whitespace characters as individual tokens.
Match match = TokenRegex().Match(line);
Match match = TokenRegex.Match(line);
if (match.Success)
{
// For each token captured build a token instance and record the token text and the file, line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ContractDescriptorSourceFileEmitter(string templateFilePath)
}

[GeneratedRegex("%%([a-zA-Z0-9_]+)%%", RegexOptions.CultureInvariant)]
private static partial Regex FindTemplatePlaceholderRegex();
private static partial Regex FindTemplatePlaceholderRegex { get; }

private string GetTemplateString()
{
Expand All @@ -44,20 +44,20 @@ public void SetPlatformFlags(uint platformFlags)
public void SetJsonDescriptor(string jsonDescriptor)
{
var count = jsonDescriptor.Length; // return the length before escaping
var escaped = CStringEscape().Replace(jsonDescriptor, "\\$1");
var escaped = CStringEscape.Replace(jsonDescriptor, "\\$1");
Elements[JsonDescriptorKey] = escaped;
Elements[JsonDescriptorSizeKey] = count.ToString();
}

[GeneratedRegex("(\")", RegexOptions.CultureInvariant)]
private static partial Regex CStringEscape();
private static partial Regex CStringEscape { get; }

public Dictionary<string, string> Elements { get; } = new();

public void Emit(TextWriter dest)
{
var template = GetTemplateString();
var matches = FindTemplatePlaceholderRegex().Matches(template);
var matches = FindTemplatePlaceholderRegex.Matches(template);
var prevPos = 0;
foreach (Match match in matches)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,30 @@ internal partial class DbConnectionOptions
+ "[\\s;]*[\u0000\\s]*" // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
;

private static readonly Regex s_connectionStringRegex = CreateConnectionStringRegex();
private static readonly Regex s_connectionStringRegexOdbc = CreateConnectionStringRegexOdbc();

#if NET
[GeneratedRegex(ConnectionStringPattern, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegex();
private static partial Regex s_connectionStringRegex { get; }

[GeneratedRegex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegexOdbc();
private static partial Regex s_connectionStringRegexOdbc { get; }
#else
private static Regex CreateConnectionStringRegex() => new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex CreateConnectionStringRegexOdbc() => new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex s_connectionStringRegex { get; } = new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex s_connectionStringRegexOdbc { get; } = new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
#endif
internal const string DataDirectory = "|datadirectory|";

private static readonly Regex s_connectionStringValidKeyRegex = CreateConnectionStringValidKeyRegex(); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static readonly Regex s_connectionStringQuoteValueRegex = CreateConnectionStringQuoteValueRegex(); // generally do not quote the value if it matches the pattern
private static readonly Regex s_connectionStringQuoteOdbcValueRegex = CreateConnectionStringQuoteOdbcValueRegex(); // do not quote odbc value if it matches this pattern

#if NET
[GeneratedRegex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$")]
private static partial Regex CreateConnectionStringValidKeyRegex();
private static partial Regex s_connectionStringValidKeyRegex { get; } // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
[GeneratedRegex("^[^\"'=;\\s\\p{Cc}]*$")]
private static partial Regex CreateConnectionStringQuoteValueRegex();
private static partial Regex s_connectionStringQuoteValueRegex { get; } // generally do not quote the value if it matches the pattern
[GeneratedRegex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringQuoteOdbcValueRegex();
private static partial Regex s_connectionStringQuoteOdbcValueRegex { get; } // do not quote odbc value if it matches this pattern
#else
private static Regex CreateConnectionStringValidKeyRegex() => new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteValueRegex() => new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteOdbcValueRegex() => new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex s_connectionStringValidKeyRegex { get; } = new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static Regex s_connectionStringQuoteValueRegex { get; } = new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled); // generally do not quote the value if it matches the pattern
private static Regex s_connectionStringQuoteOdbcValueRegex { get; } = new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled); // do not quote odbc value if it matches this pattern
#endif

// connection string common keywords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private PropsFileEntry(uint firstCodePoint, uint lastCodePoint, string propName)

public static bool TryParseLine(string line, [NotNullWhen(true)] out PropsFileEntry? value)
{
Match match = GetRegex().Match(line);
Match match = GetRegex.Match(line);

if (!match.Success)
{
Expand All @@ -53,6 +53,6 @@ public static bool TryParseLine(string line, [NotNullWhen(true)] out PropsFileEn
}

[GeneratedRegex(@"^\s*(?<firstCodePoint>[0-9a-f]{4,})(\.\.(?<lastCodePoint>[0-9a-f]{4,}))?\s*;\s*(?<propName>.+?)\s*(#\s*(?<comment>.*))?$", RegexOptions.IgnoreCase)]
private static partial Regex GetRegex();
private static partial Regex GetRegex { get; }
}
}
27 changes: 10 additions & 17 deletions src/libraries/System.Data.OleDb/src/DbConnectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,30 @@ internal partial class DbConnectionOptions
;


private static readonly Regex ConnectionStringRegex = CreateConnectionStringRegex();
private static readonly Regex ConnectionStringRegexOdbc = CreateConnectionStringRegexOdbc();

#if NET
[GeneratedRegex(ConnectionStringPattern, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegex();
private static partial Regex ConnectionStringRegex { get; }

[GeneratedRegex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegexOdbc();
private static partial Regex ConnectionStringRegexOdbc { get; }
#else
private static Regex CreateConnectionStringRegex() => new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex CreateConnectionStringRegexOdbc() => new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex ConnectionStringRegex { get; } = new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex ConnectionStringRegexOdbc { get; } = new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
#endif
internal const string DataDirectory = "|datadirectory|";

private static readonly Regex ConnectionStringValidKeyRegex = CreateConnectionStringValidKeyRegex(); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static readonly Regex ConnectionStringQuoteValueRegex = CreateConnectionStringQuoteValueRegex(); // generally do not quote the value if it matches the pattern
private static readonly Regex ConnectionStringQuoteOdbcValueRegex = CreateConnectionStringQuoteOdbcValueRegex(); // do not quote odbc value if it matches this pattern

#if NET
[GeneratedRegex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$")]
private static partial Regex CreateConnectionStringValidKeyRegex();
private static partial Regex ConnectionStringValidKeyRegex { get; } // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
[GeneratedRegex("^[^\"'=;\\s\\p{Cc}]*$")]
private static partial Regex CreateConnectionStringQuoteValueRegex();
private static partial Regex ConnectionStringQuoteValueRegex { get; } // generally do not quote the value if it matches the pattern
[GeneratedRegex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringQuoteOdbcValueRegex();
private static partial Regex ConnectionStringQuoteOdbcValueRegex { get; } // do not quote odbc value if it matches this pattern
#else
private static Regex CreateConnectionStringValidKeyRegex() => new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteValueRegex() => new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteOdbcValueRegex() => new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex ConnectionStringValidKeyRegex { get; } = new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static Regex ConnectionStringQuoteValueRegex { get; } = new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled); // generally do not quote the value if it matches the pattern
private static Regex ConnectionStringQuoteOdbcValueRegex { get; } = new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled); // do not quote odbc value if it matches this pattern
#endif

// connection string common keywords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,14 @@ public static bool HasAllSet(this BitArray bitArray)
/// <summary>
/// Gets a Regex instance for recognizing integer representations of enums.
/// </summary>
public static readonly Regex IntegerRegex = CreateIntegerRegex();
private const string IntegerRegexPattern = @"^\s*(?:\+|\-)?[0-9]+\s*$";
private const int IntegerRegexTimeoutMs = 200;

#if NET
[GeneratedRegex(IntegerRegexPattern, RegexOptions.None, matchTimeoutMilliseconds: IntegerRegexTimeoutMs)]
private static partial Regex CreateIntegerRegex();
public static partial Regex IntegerRegex { get; }
#else
private static Regex CreateIntegerRegex() => new(IntegerRegexPattern, RegexOptions.Compiled, TimeSpan.FromMilliseconds(IntegerRegexTimeoutMs));
public static Regex IntegerRegex { get; } = new(IntegerRegexPattern, RegexOptions.Compiled, TimeSpan.FromMilliseconds(IntegerRegexTimeoutMs));
#endif

/// <summary>
Expand Down
Loading
Loading