From fb646d9cb6809d027c3ab89b54b6c5be661f6faa Mon Sep 17 00:00:00 2001 From: Scott Blomquist Date: Wed, 20 Dec 2023 14:08:26 -0800 Subject: [PATCH] Eliminate need for VirtualUnaryTuple extraction node type --- RegExtract/ExtractionPlan.cs | 7 ++++--- RegExtract/ExtractionPlanNode.cs | 4 ++-- RegExtract/ExtractionPlanNodeTypes.cs | 14 -------------- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/RegExtract/ExtractionPlan.cs b/RegExtract/ExtractionPlan.cs index c4153d7..670f8d7 100644 --- a/RegExtract/ExtractionPlan.cs +++ b/RegExtract/ExtractionPlan.cs @@ -12,7 +12,7 @@ namespace RegExtract { public class ExtractionPlan: IFormattable { - public ExtractionPlanNode Plan { get; protected set; } + ExtractionPlanNode Plan { get; set; } RegexCaptureGroupTree? _tree; protected ExtractionPlan() @@ -226,7 +226,8 @@ 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. + // We're at a leaf in the type hierarchy, and all we need is a string. + // If there's an inner capture group, use it to narrow the match. if (tree.children.Count() == 1) { tree = tree.children.Single(); @@ -244,7 +245,7 @@ private ExtractionPlanNode AssignTypesToTree(RegexCaptureGroupNode tree, Type ty if (tree.name == "0") { - return new VirtualUnaryTupleNode(tree.name, type, new[] { AssignTypesToTree(tree.children.Single(), type) }, new ExtractionPlanNode[0]); + return AssignTypesToTree(tree.children.Single(), type); } if (typeParams.Length < 2 && !IsInitializableCollection(typeParams.FirstOrDefault())) diff --git a/RegExtract/ExtractionPlanNode.cs b/RegExtract/ExtractionPlanNode.cs index 45fd5bd..cd9b32c 100644 --- a/RegExtract/ExtractionPlanNode.cs +++ b/RegExtract/ExtractionPlanNode.cs @@ -192,7 +192,7 @@ internal virtual void Validate() return result; } - public object? Execute(Match match) + internal object? Execute(Match match) { if (!match.Success) { @@ -201,7 +201,7 @@ internal virtual void Validate() Dictionary cache = new(); - return Execute(match, match.Groups[groupName].Index, match.Groups[groupName].Length, cache); + return Execute(match, match.Groups[0].Index, match.Groups[0].Length, cache); } protected const string VALUETUPLE_TYPENAME = "System.ValueTuple`"; diff --git a/RegExtract/ExtractionPlanNodeTypes.cs b/RegExtract/ExtractionPlanNodeTypes.cs index 7f6ad83..9036560 100644 --- a/RegExtract/ExtractionPlanNodeTypes.cs +++ b/RegExtract/ExtractionPlanNodeTypes.cs @@ -16,20 +16,6 @@ internal record UninitializedNode() : } } - internal record VirtualUnaryTupleNode(string groupName, Type type, ExtractionPlanNode[] constructorParams, ExtractionPlanNode[] propertySetters) : - ExtractionPlanNode(groupName, type, constructorParams, propertySetters) - { - internal override object? Execute(Match match, int captureStart, int captureLength, Dictionary cache) - { - return constructorParams.Single().Execute(match, captureStart, captureLength, cache); - } - - internal override void Validate() - { - base.Validate(); - } - } - internal record CollectionInitializerNode(string groupName, Type type, ExtractionPlanNode[] constructorParams, ExtractionPlanNode[] propertySetters) : ExtractionPlanNode(groupName, type, constructorParams, propertySetters) {