Skip to content

Commit d7b9fce

Browse files
authored
A few tweaks to regex source generator comments / naming for lookbehinds (#69773)
* Improve loop comments to highlight when iterating in reverse * Use more descriptive local for lookaround naming * Address PR feedback
1 parent 554aa54 commit d7b9fce

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,7 @@ void EmitPositiveLookaroundAssertion(RegexNode node)
21742174
}
21752175

21762176
// Save off pos. We'll need to reset this upon successful completion of the lookaround.
2177-
string startingPos = ReserveName("positivelookaround_starting_pos");
2177+
string startingPos = ReserveName((node.Options & RegexOptions.RightToLeft) != 0 ? "positivelookbehind_starting_pos" : "positivelookahead_starting_pos");
21782178
writer.WriteLine($"int {startingPos} = pos;");
21792179
writer.WriteLine();
21802180
int startingSliceStaticPos = sliceStaticPos;
@@ -2224,7 +2224,7 @@ void EmitNegativeLookaroundAssertion(RegexNode node)
22242224
string originalDoneLabel = doneLabel;
22252225

22262226
// Save off pos. We'll need to reset this upon successful completion of the lookaround.
2227-
string startingPos = ReserveName("negativelookaround_starting_pos");
2227+
string startingPos = ReserveName((node.Options & RegexOptions.RightToLeft) != 0 ? "negativelookbehind_starting_pos" : "negativelookahead_starting_pos");
22282228
writer.WriteLine($"int {startingPos} = pos;");
22292229
int startingSliceStaticPos = sliceStaticPos;
22302230

@@ -4633,6 +4633,7 @@ private static string Literal(RegexOptions options)
46334633
private static string DescribeNode(RegexNode node, RegexMethod rm)
46344634
{
46354635
bool rtl = (node.Options & RegexOptions.RightToLeft) != 0;
4636+
string direction = rtl ? " right-to-left" : "";
46364637
return node.Kind switch
46374638
{
46384639
RegexNodeKind.Alternate => $"Match with {node.ChildCount()} alternative expressions{(rm.Analysis.IsAtomicByAncestor(node) ? ", atomically" : "")}.",
@@ -4649,20 +4650,20 @@ private static string DescribeNode(RegexNode node, RegexMethod rm)
46494650
RegexNodeKind.End => "Match if at the end of the string.",
46504651
RegexNodeKind.EndZ => "Match if at the end of the string or if before an ending newline.",
46514652
RegexNodeKind.Eol => "Match if at the end of a line.",
4652-
RegexNodeKind.Loop or RegexNodeKind.Lazyloop => node.M == 0 && node.N == 1 ? $"Optional ({(node.Kind is RegexNodeKind.Loop ? "greedy" : "lazy")})." : $"Loop {DescribeLoop(node, rm)}.",
4653-
RegexNodeKind.Multi => $"Match the string {Literal(node.Str!)}{(rtl ? " backwards" : "")}.",
4653+
RegexNodeKind.Loop or RegexNodeKind.Lazyloop => node.M == 0 && node.N == 1 ? $"Optional ({(node.Kind is RegexNodeKind.Loop ? "greedy" : "lazy")})." : $"Loop {DescribeLoop(node, rm)}{direction}.",
4654+
RegexNodeKind.Multi => $"Match the string {Literal(node.Str!)}{direction}.",
46544655
RegexNodeKind.NonBoundary => $"Match if at anything other than a word boundary.",
46554656
RegexNodeKind.NonECMABoundary => $"Match if at anything other than a word boundary (according to ECMAScript rules).",
46564657
RegexNodeKind.Nothing => $"Fail to match.",
4657-
RegexNodeKind.Notone => $"Match any character other than {Literal(node.Ch)}{(rtl ? " backwards" : "")}.",
4658-
RegexNodeKind.Notoneloop or RegexNodeKind.Notoneloopatomic or RegexNodeKind.Notonelazy => $"Match a character other than {Literal(node.Ch)} {DescribeLoop(node, rm)}.",
4659-
RegexNodeKind.One => $"Match {Literal(node.Ch)}{(rtl ? " backwards" : "")}.",
4660-
RegexNodeKind.Oneloop or RegexNodeKind.Oneloopatomic or RegexNodeKind.Onelazy => $"Match {Literal(node.Ch)} {DescribeLoop(node, rm)}.",
4658+
RegexNodeKind.Notone => $"Match any character other than {Literal(node.Ch)}{direction}.",
4659+
RegexNodeKind.Notoneloop or RegexNodeKind.Notoneloopatomic or RegexNodeKind.Notonelazy => $"Match a character other than {Literal(node.Ch)} {DescribeLoop(node, rm)}{direction}.",
4660+
RegexNodeKind.One => $"Match {Literal(node.Ch)}{direction}.",
4661+
RegexNodeKind.Oneloop or RegexNodeKind.Oneloopatomic or RegexNodeKind.Onelazy => $"Match {Literal(node.Ch)} {DescribeLoop(node, rm)}{direction}.",
46614662
RegexNodeKind.NegativeLookaround => $"Zero-width negative {(rtl ? "lookbehind" : "lookahead")}.",
4662-
RegexNodeKind.Backreference => $"Match the same text as matched by the {DescribeCapture(node.M, rm)}.",
4663+
RegexNodeKind.Backreference => $"Match the same text as matched by the {DescribeCapture(node.M, rm)}{direction}.",
46634664
RegexNodeKind.PositiveLookaround => $"Zero-width positive {(rtl ? "lookbehind" : "lookahead")}.",
4664-
RegexNodeKind.Set => $"Match {DescribeSet(node.Str!)}{(rtl ? " backwards" : "")}.",
4665-
RegexNodeKind.Setloop or RegexNodeKind.Setloopatomic or RegexNodeKind.Setlazy => $"Match {DescribeSet(node.Str!)} {DescribeLoop(node, rm)}.",
4665+
RegexNodeKind.Set => $"Match {DescribeSet(node.Str!)}{direction}.",
4666+
RegexNodeKind.Setloop or RegexNodeKind.Setloopatomic or RegexNodeKind.Setlazy => $"Match {DescribeSet(node.Str!)} {DescribeLoop(node, rm)}{direction}.",
46664667
RegexNodeKind.Start => "Match if at the start position.",
46674668
RegexNodeKind.ExpressionConditional => $"Conditionally match one of two expressions depending on whether an initial expression matches.",
46684669
RegexNodeKind.BackreferenceConditional => $"Conditionally match one of two expressions depending on whether the {DescribeCapture(node.M, rm)} matched.",

0 commit comments

Comments
 (0)