Skip to content

Commit

Permalink
Don't add VirtualUnaryTuple node for final inner capture
Browse files Browse the repository at this point in the history
  • Loading branch information
sblom committed Dec 20, 2023
1 parent c52923a commit c66401b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions RegExtract.Test/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ public void a010()
Assert.Equal([[['a', 's', 'd', 'f'], ['l', 'k', 'j']], [['w', 'e', 'r', 'o'], ['o', 'i', 'u']]], result);
}

[Fact]
public void a011()
{
var plan = CreateAndLogPlan<((char? type, string name) module, List<string> outputs)>(/* language=regex */@"^(([%&])?([a-z]+)) -> (([a-z]+),? ?)+$");

var result = plan.Extract("&kx -> zs, br, jd, bj, vg");

Assert.Equivalent((('&', "kx"), new List<string>() { "zs", "br", "jd", "bj", "vg"}), result);
}

[Fact]
public void slow()
{
Expand Down
4 changes: 3 additions & 1 deletion RegExtract/ExtractionPlan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ private ExtractionPlanNode AssignTypesToTree(RegexCaptureGroupNode tree, Type ty

if (IsDirectlyConstructable(type))
{
// We're at a leaf--if there's an inner capture group, use it instead of everything.
if (tree.children.Count() == 1)
{
return new VirtualUnaryTupleNode(tree.name, type, new[] { AssignTypesToTree(tree.children.Single(), type) }, new ExtractionPlanNode[0]);
tree = tree.children.Single();
}

return ExtractionPlanNode.BindLeaf(tree.name, type, groups.ToArray(), namedgroups.ToArray());
}
else if (IsTuple(unwrappedType))
Expand Down

0 comments on commit c66401b

Please sign in to comment.