diff --git a/RegExtract.Test/Usage.cs b/RegExtract.Test/Usage.cs index 208aa78..b0f5d6b 100644 --- a/RegExtract.Test/Usage.cs +++ b/RegExtract.Test/Usage.cs @@ -94,6 +94,16 @@ public void a008() var result = plan.Extract(@"rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"); } + [Fact] + public void a009() + { + var plan = ExtractionPlan>>>.CreatePlan(new Regex(@"(((\w)+ ?)+,? ?)+")); + var str = plan.ToString("x"); + output.WriteLine(str); + + var result = plan.Extract(@"asdf lkj, wero oiu"); + } + [Fact] public void slow() { @@ -335,7 +345,11 @@ public void nested_extraction() [Fact] public void nested_extraction_of_list() { - var result = "The quick brown fox jumps over the lazy dog.".Extract>>(@"(?:((\w)+) ?)+"); + var plan = ExtractionPlan>>.CreatePlan(new Regex(@"(((\w)+) ?)+")); + var str = plan.ToString("x"); + output.WriteLine(str); + + var result = plan.Extract("The quick brown fox jumps over the lazy dog."); } [Fact] @@ -418,7 +432,7 @@ public void CreateTreePlan() var plan = ExtractionPlan<((int?, int?)?, char, string)?>.CreatePlan(regex); object? result = plan.Extract(regex.Match("2-12 c: abcdefgji")); - regex = new Regex(@"(?:((\w)+) ?)+"); + regex = new Regex(@"(((\w)+) ?)+"); var plan2 = ExtractionPlan>>.CreatePlan(regex); result = plan2.Extract(regex.Match("The quick brown fox jumps over the lazy dog")); diff --git a/RegExtract/ExtractionPlan.cs b/RegExtract/ExtractionPlan.cs index fd21b2a..e3bc0ef 100644 --- a/RegExtract/ExtractionPlan.cs +++ b/RegExtract/ExtractionPlan.cs @@ -53,8 +53,10 @@ internal void InitializePlan(Regex regex) // We use C#'s definition of an initializable collection, which is any type that implements IEnumerable and has a public Add() method. // In our case, we also require that the Add() method has parameters of the same type as the collection's generic parameters. - protected bool IsInitializableCollection(Type type) + protected bool IsInitializableCollection(Type? type) { + if (type == null) + return false; var genericParameters = type.GetGenericArguments(); var addMethod = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, genericParameters, null); @@ -239,7 +241,7 @@ private ExtractionPlanNode AssignTypesToTree(RegexCaptureGroupNode tree, Type ty return new VirtualUnaryTupleNode(tree.name, type, new[] { AssignTypesToTree(tree.children.Single(), type) }, new ExtractionPlanNode[0]); } - if (typeParams.Length < 2) + if (typeParams.Length < 2 && !IsInitializableCollection(typeParams.FirstOrDefault())) { return ExtractionPlanNode.Bind(tree.name, type, new[] { BindConstructorPlan(tree, type, 0, 1, stack) }, new ExtractionPlanNode[0]); }