6 |
// author: Dan Lewis (dlewis@gmx.co.uk) |
// author: Dan Lewis (dlewis@gmx.co.uk) |
7 |
// (c) 2002 |
// (c) 2002 |
8 |
|
|
9 |
|
// |
10 |
|
// Permission is hereby granted, free of charge, to any person obtaining |
11 |
|
// a copy of this software and associated documentation files (the |
12 |
|
// "Software"), to deal in the Software without restriction, including |
13 |
|
// without limitation the rights to use, copy, modify, merge, publish, |
14 |
|
// distribute, sublicense, and/or sell copies of the Software, and to |
15 |
|
// permit persons to whom the Software is furnished to do so, subject to |
16 |
|
// the following conditions: |
17 |
|
// |
18 |
|
// The above copyright notice and this permission notice shall be |
19 |
|
// included in all copies or substantial portions of the Software. |
20 |
|
// |
21 |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
22 |
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
23 |
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
24 |
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
25 |
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
26 |
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
27 |
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
28 |
|
// |
29 |
|
|
30 |
using System; |
using System; |
31 |
using System.Collections; |
using System.Collections; |
32 |
using System.Diagnostics; |
using System.Diagnostics; |
41 |
|
|
42 |
// process info block |
// process info block |
43 |
|
|
44 |
Debug.Assert ((OpCode)program[0] == OpCode.Info, "Regex", "Cant' find info block"); |
//Debug.Assert ((OpCode)program[0] == OpCode.Info, "Regex", "Cant' find info block"); |
45 |
|
|
46 |
this.group_count = program[1] + 1; |
this.group_count = program[1] + 1; |
47 |
this.match_min = program[2]; |
this.match_min = program[2]; |
48 |
this.match_max = program[3]; |
//this.match_max = program[3]; |
49 |
|
|
50 |
// setup |
// setup |
51 |
|
|
625 |
} |
} |
626 |
|
|
627 |
case OpCode.Info: { |
case OpCode.Info: { |
628 |
Debug.Assert (false, "Regex", "Info block found in pattern"); |
//Debug.Assert (false, "Regex", "Info block found in pattern"); |
629 |
goto Fail; |
goto Fail; |
630 |
} |
} |
631 |
} |
} |
858 |
return false; |
return false; |
859 |
} |
} |
860 |
|
|
861 |
Debug.Assert (marks [b].IsDefined, "Regex", "Balancng group not closed"); |
//Debug.Assert (marks [b].IsDefined, "Regex", "Balancng group not closed"); |
862 |
|
|
863 |
if (gid > 0 && capture){ |
if (gid > 0 && capture){ |
864 |
Open (gid, marks [b].Index + marks [b].Length); |
Open (gid, marks [b].Index + marks [b].Length); |
876 |
} |
} |
877 |
|
|
878 |
private void Backtrack (int cp) { |
private void Backtrack (int cp) { |
879 |
Debug.Assert (cp >= mark_start, "Regex", "Attempt to backtrack forwards"); |
//Debug.Assert (cp > mark_start, "Regex", "Attempt to backtrack forwards"); |
880 |
|
|
881 |
for (int i = 0; i < groups.Length; ++ i) { |
for (int i = 0; i < groups.Length; ++ i) { |
882 |
int m = groups [i]; |
int m = groups [i]; |
926 |
return m; |
return m; |
927 |
} |
} |
928 |
|
|
929 |
private Match GenerateMatch (Regex regex) { |
private void GetGroupInfo (int gid, out int first_mark_index, out int n_caps) |
930 |
int[][] grps = new int[groups.Length][]; |
{ |
931 |
ArrayList caps = new ArrayList (); |
first_mark_index = -1; |
932 |
|
bool first = true; |
933 |
for (int gid = 0; gid < groups.Length; ++ gid) { |
n_caps = 0; |
934 |
caps.Clear (); |
for (int m = groups [gid]; m >= 0; m = marks [m].Previous) { |
935 |
for (int m = groups[gid]; m >= 0; m = marks[m].Previous) { |
if (!marks [m].IsDefined) |
936 |
if (!marks[m].IsDefined) |
continue; |
937 |
continue; |
++n_caps; |
938 |
|
if (first) { |
939 |
caps.Add (marks[m].Index); |
first = false; |
940 |
caps.Add (marks[m].Length); |
first_mark_index = m; |
941 |
} |
} |
942 |
|
} |
943 |
|
} |
944 |
|
|
945 |
grps[gid] = (int[])caps.ToArray (typeof (int)); |
private void PopulateGroup (Group g, int first_mark_index, int n_caps) |
946 |
|
{ |
947 |
|
int i = 1; |
948 |
|
for (int m = marks [first_mark_index].Previous; m >= 0; m = marks [m].Previous) { |
949 |
|
if (!marks [m].IsDefined) |
950 |
|
continue; |
951 |
|
Capture cap = new Capture (text, marks [m].Index, marks [m].Length); |
952 |
|
g.Captures.SetValue (cap, n_caps - 1 - i); |
953 |
|
++i; |
954 |
} |
} |
955 |
|
} |
956 |
|
|
957 |
return new Match (regex, this, text, text_end, grps); |
private Match GenerateMatch (Regex regex) |
958 |
|
{ |
959 |
|
int n_caps, first_mark_index; |
960 |
|
Group g; |
961 |
|
GetGroupInfo (0, out first_mark_index, out n_caps); |
962 |
|
Match retval = new Match (regex, this, text, text_end, groups.Length, |
963 |
|
marks [first_mark_index].Index, marks [first_mark_index].Length, n_caps); |
964 |
|
PopulateGroup (retval, first_mark_index, n_caps); |
965 |
|
|
966 |
|
for (int gid = 1; gid < groups.Length; ++ gid) { |
967 |
|
GetGroupInfo (gid, out first_mark_index, out n_caps); |
968 |
|
if (first_mark_index < 0) { |
969 |
|
g = Group.Fail; |
970 |
|
} else { |
971 |
|
g = new Group (text, marks [first_mark_index].Index, marks [first_mark_index].Length, n_caps); |
972 |
|
PopulateGroup (g, first_mark_index, n_caps); |
973 |
|
} |
974 |
|
retval.Groups.SetValue (g, gid); |
975 |
|
} |
976 |
|
return retval; |
977 |
} |
} |
978 |
|
|
979 |
// interpreter attributes |
// interpreter attributes |
983 |
private string text; // input text |
private string text; // input text |
984 |
private int text_end; // end of input text (last character + 1) |
private int text_end; // end of input text (last character + 1) |
985 |
private int group_count; // number of capturing groups |
private int group_count; // number of capturing groups |
986 |
private int match_min, match_max; // match width information |
private int match_min;//, match_max; // match width information |
987 |
private QuickSearch qs; // fast substring matcher |
private QuickSearch qs; // fast substring matcher |
988 |
|
|
989 |
// match state |
// match state |