Debian Codegraph @

mcs/class/referencesource/System/regex/system/text/regularexpressions/RegexCode.cs

raw
1 //------------------------------------------------------------------------------
2 // <copyright file="RegexCode.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 // This RegexCode class is internal to the regular expression package.
8 // It provides operator constants for use by the Builder and the Machine.
9
10 // Implementation notes:
11 //
12 // Regexps are built into RegexCodes, which contain an operation array,
13 // a string table, and some constants.
14 //
15 // Each operation is one of the codes below, followed by the integer
16 // operands specified for each op.
17 //
18 // Strings and sets are indices into a string table.
19
20
21 namespace System.Text.RegularExpressions {
22
23 using System.Collections;
24 using System.Collections.Generic;
25 using System.Diagnostics;
26 using System.Globalization;
27
28 internal sealed class RegexCode {
29 // the following primitive operations come directly from the parser
30
31 // lef/back operands description
32
33 internal const int Onerep = 0; // lef,back char,min,max a {n}
34 internal const int Notonerep = 1; // lef,back char,min,max .{n}
35 internal const int Setrep = 2; // lef,back set,min,max [\d]{n}
36
37 internal const int Oneloop = 3; // lef,back char,min,max a {,n}
38 internal const int Notoneloop = 4; // lef,back char,min,max .{,n}
39 internal const int Setloop = 5; // lef,back set,min,max [\d]{,n}
40
41 internal const int Onelazy = 6; // lef,back char,min,max a {,n}?
42 internal const int Notonelazy = 7; // lef,back char,min,max .{,n}?
43 internal const int Setlazy = 8; // lef,back set,min,max [\d]{,n}?
44
45 internal const int One = 9; // lef char a
46 internal const int Notone = 10; // lef char [^a]
47 internal const int Set = 11; // lef set [a-z\s] \w \s \d
48
49 internal const int Multi = 12; // lef string abcd
50 internal const int Ref = 13; // lef group \#
51
52 internal const int Bol = 14; // ^
53 internal const int Eol = 15; // $
54 internal const int Boundary = 16; // \b
55 internal const int Nonboundary = 17; // \B
56 internal const int Beginning = 18; // \A
57 internal const int Start = 19; // \G
58 internal const int EndZ = 20; // \Z
59 internal const int End = 21; // \Z
60
61 internal const int Nothing = 22; // Reject!
62
63 // primitive control structures
64
65 internal const int Lazybranch = 23; // back jump straight first
66 internal const int Branchmark = 24; // back jump branch first for loop
67 internal const int Lazybranchmark = 25; // back jump straight first for loop
68 internal const int Nullcount = 26; // back val set counter, null mark
69 internal const int Setcount = 27; // back val set counter, make mark
70 internal const int Branchcount = 28; // back jump,limit branch++ if zero<=c<limit
71 internal const int Lazybranchcount= 29; // back jump,limit same, but straight first
72 internal const int Nullmark = 30; // back save position
73 internal const int Setmark = 31; // back save position
74 internal const int Capturemark = 32; // back group define group
75 internal const int Getmark = 33; // back recall position
76 internal const int Setjump = 34; // back save backtrack state
77 internal const int Backjump = 35; // zap back to saved state
78 internal const int Forejump = 36; // zap backtracking state
79 internal const int Testref = 37; // backtrack if ref undefined
80 internal const int Goto = 38; // jump just go
81
82 internal const int Prune = 39; // prune it baby
83 internal const int Stop = 40; // done!
84
85 internal const int ECMABoundary = 41; // \b
86 internal const int NonECMABoundary= 42; // \B
87
88 // modifiers for alternate modes
89
90 internal const int Mask = 63; // Mask to get unmodified ordinary operator
91 internal const int Rtl = 64; // bit to indicate that we're reverse scanning.
92 internal const int Back = 128; // bit to indicate that we're backtracking.
93 internal const int Back2 = 256; // bit to indicate that we're backtracking on a second branch.
94 internal const int Ci = 512; // bit to indicate that we're case-insensitive.
95
96 // the code
97
98 internal int[] _codes; // the code
99 internal String[] _strings; // the string/set table
100 // not used! internal int[] _sparseIndex; // a list of the groups that are used
101 internal int _trackcount; // how many instructions use backtracking
102 #if SILVERLIGHT
103 internal Dictionary<Int32, Int32> _caps; // mapping of user group numbers -> impl group slots
104 #else
105 internal Hashtable _caps; // mapping of user group numbers -> impl group slots
106 #endif
107 internal int _capsize; // number of impl group slots
108 internal RegexPrefix _fcPrefix; // the set of candidate first characters (may be null)
109 internal RegexBoyerMoore _bmPrefix; // the fixed prefix string as a Boyer-Moore machine (may be null)
110 internal int _anchors; // the set of zero-length start anchors (RegexFCD.Bol, etc)
111 internal bool _rightToLeft; // true if right to left
112
113 // optimizations
114
115 // constructor
116
117 internal RegexCode(int [] codes, List<String> stringlist, int trackcount,
118 #if SILVERLIGHT
119 Dictionary<Int32, Int32> caps, int capsize,
120 #else
121 Hashtable caps, int capsize,
122 #endif
123 RegexBoyerMoore bmPrefix, RegexPrefix fcPrefix,
124 int anchors, bool rightToLeft) {
125 _codes = codes;
126 _strings = new String[stringlist.Count];
127 _trackcount = trackcount;
128 _caps = caps;
129 _capsize = capsize;
130 _bmPrefix = bmPrefix;
131 _fcPrefix = fcPrefix;
132 _anchors = anchors;
133 _rightToLeft = rightToLeft;
134 stringlist.CopyTo(0, _strings, 0, stringlist.Count);
135 }
136
137 internal static bool OpcodeBacktracks(int Op) {
138 Op &= Mask;
139
140 switch (Op) {
141 case Oneloop:
142 case Notoneloop:
143 case Setloop:
144 case Onelazy:
145 case Notonelazy:
146 case Setlazy:
147 case Lazybranch:
148 case Branchmark:
149 case Lazybranchmark:
150 case Nullcount:
151 case Setcount:
152 case Branchcount:
153 case Lazybranchcount:
154 case Setmark:
155 case Capturemark:
156 case Getmark:
157 case Setjump:
158 case Backjump:
159 case Forejump:
160 case Goto:
161 return true;
162
163 default:
164 return false;
165 }
166 }
167
168 internal static int OpcodeSize(int Opcode) {
169 Opcode &= Mask;
170
171 switch (Opcode) {
172 case Nothing:
173 case Bol:
174 case Eol:
175 case Boundary:
176 case Nonboundary:
177 case ECMABoundary:
178 case NonECMABoundary:
179 case Beginning:
180 case Start:
181 case EndZ:
182 case End:
183
184 case Nullmark:
185 case Setmark:
186 case Getmark:
187 case Setjump:
188 case Backjump:
189 case Forejump:
190 case Stop:
191
192 return 1;
193
194 case One:
195 case Notone:
196 case Multi:
197 case Ref:
198 case Testref:
199
200
201 case Goto:
202 case Nullcount:
203 case Setcount:
204 case Lazybranch:
205 case Branchmark:
206 case Lazybranchmark:
207 case Prune:
208 case Set:
209
210 return 2;
211
212 case Capturemark:
213 case Branchcount:
214 case Lazybranchcount:
215
216 case Onerep:
217 case Notonerep:
218 case Oneloop:
219 case Notoneloop:
220 case Onelazy:
221 case Notonelazy:
222 case Setlazy:
223 case Setrep:
224 case Setloop:
225
226 return 3;
227
228 default:
229
230 throw MakeException(SR.GetString(SR.UnexpectedOpcode, Opcode.ToString(CultureInfo.CurrentCulture)));
231 }
232 }
233
234 internal static ArgumentException MakeException(String message) {
235 return new ArgumentException(message);
236 }
237
238 // Debug only code below
239
240 #if DBG
241 internal static String[] CodeStr = new String[]
242 {
243 "Onerep", "Notonerep", "Setrep",
244 "Oneloop", "Notoneloop", "Setloop",
245 "Onelazy", "Notonelazy", "Setlazy",
246 "One", "Notone", "Set",
247 "Multi", "Ref",
248 "Bol", "Eol", "Boundary", "Nonboundary", "Beginning", "Start", "EndZ", "End",
249 "Nothing",
250 "Lazybranch", "Branchmark", "Lazybranchmark",
251 "Nullcount", "Setcount", "Branchcount", "Lazybranchcount",
252 "Nullmark", "Setmark", "Capturemark", "Getmark",
253 "Setjump", "Backjump", "Forejump", "Testref", "Goto",
254 "Prune", "Stop",
255 #if ECMA
256 "ECMABoundary", "NonECMABoundary",
257 #endif
258 };
259
260 internal static String OperatorDescription(int Opcode) {
261 bool isCi = ((Opcode & Ci) != 0);
262 bool isRtl = ((Opcode & Rtl) != 0);
263 bool isBack = ((Opcode & Back) != 0);
264 bool isBack2 = ((Opcode & Back2) != 0);
265
266 return CodeStr[Opcode & Mask] +
267 (isCi ? "-Ci" : "") + (isRtl ? "-Rtl" : "") + (isBack ? "-Back" : "") + (isBack2 ? "-Back2" : "");
268 }
269
270 internal String OpcodeDescription(int offset) {
271 StringBuilder sb = new StringBuilder();
272 int opcode = _codes[offset];
273
274 sb.AppendFormat("{0:D6} ", offset);
275 sb.Append(OpcodeBacktracks(opcode & Mask) ? '*' : ' ');
276 sb.Append(OperatorDescription(opcode));
277 sb.Append('(');
278
279 opcode &= Mask;
280
281 switch (opcode) {
282 case One:
283 case Notone:
284 case Onerep:
285 case Notonerep:
286 case Oneloop:
287 case Notoneloop:
288 case Onelazy:
289 case Notonelazy:
290 sb.Append("Ch = ");
291 sb.Append(RegexCharClass.CharDescription((char)_codes[offset+1]));
292 break;
293
294 case Set:
295 case Setrep:
296 case Setloop:
297 case Setlazy:
298 sb.Append("Set = ");
299 sb.Append(RegexCharClass.SetDescription(_strings[_codes[offset+1]]));
300 break;
301
302 case Multi:
303 sb.Append("String = ");
304 sb.Append(_strings[_codes[offset+1]]);
305 break;
306
307 case Ref:
308 case Testref:
309 sb.Append("Index = ");
310 sb.Append(_codes[offset+1]);
311 break;
312
313 case Capturemark:
314 sb.Append("Index = ");
315 sb.Append(_codes[offset+1]);
316 if (_codes[offset+2] != -1) {
317 sb.Append(", Unindex = ");
318 sb.Append(_codes[offset+2]);
319 }
320 break;
321
322 case Nullcount:
323 case Setcount:
324 sb.Append("Value = ");
325 sb.Append(_codes[offset+1]);
326 break;
327
328 case Goto:
329 case Lazybranch:
330 case Branchmark:
331 case Lazybranchmark:
332 case Branchcount:
333 case Lazybranchcount:
334 sb.Append("Addr = ");
335 sb.Append(_codes[offset+1]);
336 break;
337 }
338
339 switch (opcode) {
340 case Onerep:
341 case Notonerep:
342 case Oneloop:
343 case Notoneloop:
344 case Onelazy:
345 case Notonelazy:
346 case Setrep:
347 case Setloop:
348 case Setlazy:
349 sb.Append(", Rep = ");
350 if (_codes[offset + 2] == Int32.MaxValue)
351 sb.Append("inf");
352 else
353 sb.Append(_codes[offset + 2]);
354 break;
355
356 case Branchcount:
357 case Lazybranchcount:
358 sb.Append(", Limit = ");
359 if (_codes[offset + 2] == Int32.MaxValue)
360 sb.Append("inf");
361 else
362 sb.Append(_codes[offset + 2]);
363 break;
364 }
365
366 sb.Append(")");
367
368 return sb.ToString();
369 }
370
371 internal void Dump() {
372 int i;
373
374 Debug.WriteLine("Direction: " + (_rightToLeft ? "right-to-left" : "left-to-right"));
375 Debug.WriteLine("Firstchars: " + (_fcPrefix == null ? "n/a" : RegexCharClass.SetDescription(_fcPrefix.Prefix)));
376 Debug.WriteLine("Prefix: " + (_bmPrefix == null ? "n/a" : Regex.Escape(_bmPrefix.ToString())));
377 Debug.WriteLine("Anchors: " + RegexFCD.AnchorDescription(_anchors));
378 Debug.WriteLine("");
379 if (_bmPrefix != null) {
380 Debug.WriteLine("BoyerMoore:");
381 Debug.WriteLine(_bmPrefix.Dump(" "));
382 }
383 for (i = 0; i < _codes.Length;) {
384 Debug.WriteLine(OpcodeDescription(i));
385 i += OpcodeSize(_codes[i]);
386 }
387
388 Debug.WriteLine("");
389 }
390 #endif
391
392 }
393 }