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

changing expander regex #11210

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
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
53 changes: 43 additions & 10 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ internal enum ExpanderOptions
/// </remarks>
/// <typeparam name="P">Type of the properties used.</typeparam>
/// <typeparam name="I">Type of the items used.</typeparam>
internal class Expander<P, I>
internal partial class Expander<P, I>
where P : class, IProperty
where I : class, IItem
{
Expand Down Expand Up @@ -952,7 +952,7 @@ internal static string ExpandMetadataLeaveEscaped(string expression, IMetadataTa
// if there are no item vectors in the string
// run a simpler Regex to find item metadata references
MetadataMatchEvaluator matchEvaluator = new MetadataMatchEvaluator(metadata, options, elementLocation, loggingContext);
result = RegularExpressions.ItemMetadataPattern.Value.Replace(expression, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
result = RegularExpressions.ItemMetadataPatternWrapper().Replace(expression, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
}
else
{
Expand Down Expand Up @@ -983,15 +983,15 @@ internal static string ExpandMetadataLeaveEscaped(string expression, IMetadataTa
// Extract the part of the expression that appears before the item vector expression
// e.g. the ABC in ABC@(foo->'%(FullPath)')
string subExpressionToReplaceIn = expression.Substring(start, itemVectorExpressions[n].Index - start);
string replacementResult = RegularExpressions.NonTransformItemMetadataPattern.Value.Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
string replacementResult = RegularExpressions.NonTransformItemMetadataPatternWrapper().Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));

// Append the metadata replacement
finalResultBuilder.Append(replacementResult);

// Expand any metadata that appears in the item vector expression's separator
if (itemVectorExpressions[n].Separator != null)
{
vectorExpression = RegularExpressions.NonTransformItemMetadataPattern.Value.Replace(itemVectorExpressions[n].Value, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata), -1, itemVectorExpressions[n].SeparatorStart);
vectorExpression = RegularExpressions.NonTransformItemMetadataPatternWrapper().Replace(itemVectorExpressions[n].Value, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata), -1, itemVectorExpressions[n].SeparatorStart);
}

// Append the item vector expression as is
Expand All @@ -1008,7 +1008,7 @@ internal static string ExpandMetadataLeaveEscaped(string expression, IMetadataTa
if (start < expression.Length)
{
string subExpressionToReplaceIn = expression.Substring(start);
string replacementResult = RegularExpressions.NonTransformItemMetadataPattern.Value.Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
string replacementResult = RegularExpressions.NonTransformItemMetadataPatternWrapper().Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));

finalResultBuilder.Append(replacementResult);
}
Expand Down Expand Up @@ -2719,7 +2719,7 @@ internal static IEnumerable<KeyValuePair<string, S>> ExpandQuotedExpressionFunct
{
matchEvaluator = new MetadataMatchEvaluator(item.Key, item.Value, elementLocation);

include = RegularExpressions.ItemMetadataPattern.Value.Replace(arguments[0], matchEvaluator.GetMetadataValueFromMatch);
include = RegularExpressions.ItemMetadataPatternWrapper().Replace(arguments[0], matchEvaluator.GetMetadataValueFromMatch);
}

// Include may be empty. Historically we have created items with empty include
Expand Down Expand Up @@ -3086,24 +3086,40 @@ internal string GetMetadataValueFromMatch(Match match)
/// Regular expressions used by the expander.
/// The expander currently uses regular expressions rather than a parser to do its work.
/// </summary>
private static class RegularExpressions
private static partial class RegularExpressions
{
/**************************************************************************************************************************
* WARNING: The regular expressions below MUST be kept in sync with the expressions in the ProjectWriter class -- if the
* description of an item vector changes, the expressions must be updated in both places.
*************************************************************************************************************************/



#if NET7_0_OR_GREATER
[GeneratedRegex(ItemMetadataSpecification, RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)]
internal static partial Regex ItemMetadataPattern();
#else
/// <summary>
/// Regular expression used to match item metadata references embedded in strings.
/// For example, %(Compile.DependsOn) or %(DependsOn).
/// </summary>
internal static readonly Lazy<Regex> ItemMetadataPattern = new Lazy<Regex>(
() => new Regex(ItemMetadataSpecification,
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.Compiled));
#endif

/// <summary>
/// Name of the group matching the "name" of a metadatum.
/// </summary>
internal static Regex ItemMetadataPatternWrapper()
SimaTian marked this conversation as resolved.
Show resolved Hide resolved
{
#if NET7_0_OR_GREATER
return ItemMetadataPattern();
#else
return ItemMetadataPattern.Value;
#endif
}

/// <summary>
/// Name of the group matching the "name" of a metadatum.
/// </summary>
internal const string NameGroup = "NAME";

/// <summary>
Expand All @@ -3116,6 +3132,14 @@ private static class RegularExpressions
/// </summary>
internal const string ItemTypeGroup = "ITEM_TYPE";

#if NET7_0_OR_GREATER
[GeneratedRegex(@"((?<=" + ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
SimaTian marked this conversation as resolved.
Show resolved Hide resolved
ItemVectorWithTransformRHS + @")) | ((?<!" + ItemVectorWithTransformLHS + @")" +
ItemMetadataSpecification + @"(?=" + ItemVectorWithTransformRHS + @")) | ((?<!" +
ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
ItemVectorWithTransformRHS + @"))", RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)]
internal static partial Regex NonTransformItemMetadataPattern();
#else
/// <summary>
/// regular expression used to match item metadata references outside of item vector transforms.
/// </summary>
Expand All @@ -3128,6 +3152,15 @@ private static class RegularExpressions
ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
ItemVectorWithTransformRHS + @"))",
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.Compiled));
#endif
internal static Regex NonTransformItemMetadataPatternWrapper()
SimaTian marked this conversation as resolved.
Show resolved Hide resolved
{
#if NET7_0_OR_GREATER
return NonTransformItemMetadataPattern();
#else
return NonTransformItemMetadataPattern.Value;
#endif
}

/// <summary>
/// Complete description of an item metadata reference, including the optional qualifying item type.
Expand Down