Skip to content

Fix describing concatenations in alternations #62646

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

Merged
merged 1 commit into from
Dec 10, 2021
Merged
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 @@ -3318,6 +3318,7 @@ private static string DescribeNode(RegexNode node) =>
RegexNode.Boundary => $"Match if at a word boundary.",
RegexNode.Capture when node.N != -1 => $"{DescribeNonNegative(node.M)} capturing group. Uncaptures the {DescribeNonNegative(node.N)} capturing group.",
RegexNode.Capture when node.N == -1 => $"{DescribeNonNegative(node.M)} capturing group.",
RegexNode.Concatenate => "Match a sequence of expressions.",
RegexNode.ECMABoundary => $"Match if at a word boundary (according to ECMAScript rules).",
RegexNode.Empty => $"Match an empty string.",
RegexNode.End => "Match if at the end of the string.",
Expand Down Expand Up @@ -3353,9 +3354,18 @@ private static string DescribeNode(RegexNode node) =>
/// <param name="depth">The depth of the current node.</param>
private static void DescribeExpression(TextWriter writer, RegexNode node, string prefix, int depth = 0)
{
bool skip =
node.Type is RegexNode.Concatenate ||
(node.Type == RegexNode.Atomic && node.Child(0).Type is RegexNode.Loop or RegexNode.Lazyloop or RegexNode.Alternate);
bool skip = node.Type switch
{
// For concatenations, flatten the contents into the parent, but only if the parent isn't a form of alternation,
// where each branch is considered to be independent rather than a concatenation.
RegexNode.Concatenate when node.Next is not { Type: RegexNode.Alternate or RegexNode.Testref or RegexNode.Testgroup } => true,

// For atomic, skip the node if we'll instead render the atomic label as part of rendering the child.
RegexNode.Atomic when node.Child(0).Type is RegexNode.Loop or RegexNode.Lazyloop or RegexNode.Alternate => true,

// Don't skip anything else.
_ => false,
};

if (!skip)
{
Expand Down