private void ResolveReferences() { int gid = 1; Hashtable dict = new Hashtable(); ArrayList explicit_numeric_groups = null; // number unnamed groups foreach (CapturingGroup group in caps) { if (group.Name != null) { continue; } dict.Add(gid.ToString(), group); group.Index = gid++; ++num_groups; } // number named groups foreach (CapturingGroup group in caps) { if (group.Name == null) { continue; } if (dict.Contains(group.Name)) { CapturingGroup prev = (CapturingGroup)dict [group.Name]; group.Index = prev.Index; if (group.Index == gid) { gid++; } else if (group.Index > gid) { explicit_numeric_groups.Add(group); } continue; } if (Char.IsDigit(group.Name [0])) { int ptr = 0; int group_gid = ParseDecimal(group.Name, ref ptr); if (ptr == group.Name.Length) { group.Index = group_gid; dict.Add(group.Name, group); ++num_groups; if (group_gid == gid) { gid++; } else { // all numbers before 'gid' are already in the dictionary. So, we know group_gid > gid if (explicit_numeric_groups == null) { explicit_numeric_groups = new ArrayList(4); } explicit_numeric_groups.Add(group); } continue; } } string gid_s = gid.ToString(); while (dict.Contains(gid_s)) { gid_s = (++gid).ToString(); } dict.Add(gid_s, group); dict.Add(group.Name, group); group.Index = gid++; ++num_groups; } gap = gid; // == 1 + num_groups, if explicit_numeric_groups == null if (explicit_numeric_groups != null) { HandleExplicitNumericGroups(explicit_numeric_groups); } // resolve references foreach (Expression expr in refs.Keys) { string name = (string)refs [expr]; if (!dict.Contains(name)) { if (expr is CaptureAssertion && !Char.IsDigit(name [0])) { continue; } BackslashNumber bn = expr as BackslashNumber; if (bn != null && bn.ResolveReference(name, dict)) { continue; } throw NewParseException("Reference to undefined group " + (Char.IsDigit(name[0]) ? "number " : "name ") + name); } CapturingGroup group = (CapturingGroup)dict[name]; if (expr is Reference) { ((Reference)expr).CapturingGroup = group; } else if (expr is CaptureAssertion) { ((CaptureAssertion)expr).CapturingGroup = group; } else if (expr is BalancingGroup) { ((BalancingGroup)expr).Balance = group; } } }
private void ResolveReferences() { int num = 1; Hashtable hashtable = new Hashtable(); ArrayList arrayList = null; foreach (CapturingGroup cap in caps) { if (cap.Name == null) { hashtable.Add(num.ToString(), cap); cap.Index = num++; num_groups++; } } foreach (CapturingGroup cap2 in caps) { if (cap2.Name != null) { if (hashtable.Contains(cap2.Name)) { CapturingGroup capturingGroup3 = (CapturingGroup)hashtable[cap2.Name]; cap2.Index = capturingGroup3.Index; if (cap2.Index == num) { num++; } else if (cap2.Index > num) { arrayList.Add(cap2); } } else { if (char.IsDigit(cap2.Name[0])) { int num3 = 0; int num4 = ParseDecimal(cap2.Name, ref num3); if (num3 == cap2.Name.Length) { cap2.Index = num4; hashtable.Add(cap2.Name, cap2); num_groups++; if (num4 == num) { num++; } else { if (arrayList == null) { arrayList = new ArrayList(4); } arrayList.Add(cap2); } continue; } } string key = num.ToString(); while (hashtable.Contains(key)) { int num5 = ++num; key = num5.ToString(); } hashtable.Add(key, cap2); hashtable.Add(cap2.Name, cap2); cap2.Index = num++; num_groups++; } } } gap = num; if (arrayList != null) { HandleExplicitNumericGroups(arrayList); } foreach (Expression key2 in refs.Keys) { string text = (string)refs[key2]; if (!hashtable.Contains(text)) { if (!(key2 is CaptureAssertion) || char.IsDigit(text[0])) { BackslashNumber backslashNumber = key2 as BackslashNumber; if (backslashNumber == null || !backslashNumber.ResolveReference(text, hashtable)) { throw NewParseException("Reference to undefined group " + ((!char.IsDigit(text[0])) ? "name " : "number ") + text); } } } else { CapturingGroup capturingGroup4 = (CapturingGroup)hashtable[text]; if (key2 is Reference) { ((Reference)key2).CapturingGroup = capturingGroup4; } else if (key2 is CaptureAssertion) { ((CaptureAssertion)key2).CapturingGroup = capturingGroup4; } else if (key2 is BalancingGroup) { ((BalancingGroup)key2).Balance = capturingGroup4; } } } }