* RegexRunnerFactory.cs: removed comment, no longer throw exception
[mono.git] / mcs / class / System / System.Text.RegularExpressions / arch.cs
1 //
2 // assembly:    System
3 // namespace:   System.Text.RegularExpressions
4 // file:        arch.cs
5 //
6 // author:      Dan Lewis (dlewis@gmx.co.uk)
7 //              (c) 2002
8
9 using System;
10 using System.Collections;
11
12 namespace System.Text.RegularExpressions {
13
14         enum OpCode : ushort {
15                 False           = 0,    // always fails
16                 True,                   // always succeeds
17
18                 // matching
19
20                 Position,               // zero-width position assertion
21                 String,                 // match string literal
22                 Reference,              // back reference
23
24                 // character matching
25
26                 Character,              // match character exactly
27                 Category,               // match character from category
28                 Range,                  // match character from range
29                 Set,                    // match character from set
30                 In,                     // match character from group of tests
31
32                 // capturing
33
34                 Open,                   // open group
35                 Close,                  // close group
36                 Balance,                // balance groups
37                 BalanceStart,           //track balance group length
38
39                 // control flow
40
41                 IfDefined,              // conditional on capture
42                 Sub,                    // non-backtracking subexpression
43                 Test,                   // non-backtracking lookahead/behind
44                 Branch,                 // alternative expression
45                 Jump,                   // unconditional goto
46                 Repeat,                 // new repeat context
47                 Until,                  // repeat subexpression within context
48                 FastRepeat,             // repeat simple subexpression
49                 Anchor,                 // anchoring expression
50
51                 // miscellaneous
52                 
53                 Info                    // pattern information
54         }
55
56         [Flags]
57         enum OpFlags : ushort {
58                 None            = 0x000,
59                 Negate          = 0x100,        // succeed on mismatch
60                 IgnoreCase      = 0x200,        // case insensitive matching
61                 RightToLeft     = 0x400,        // right-to-left matching
62                 Lazy            = 0x800         // minimizing repeat
63         }
64
65         enum Position : ushort {
66                 Any,                    // anywhere
67                 Start,                  // start of string                      \A
68                 StartOfString,          // start of string                      \A
69                 StartOfLine,            // start of line                        ^
70                 StartOfScan,            // start of scan                        \G
71                 End,                    // end or before newline at end         \Z
72                 EndOfString,            // end of string                        \z
73                 EndOfLine,              // end of line                          $
74                 Boundary,               // word boundary                        \b
75                 NonBoundary             // not word boundary                    \B
76         };
77         
78         // see category.cs for Category enum
79
80         interface IMachine {
81                 Match Scan (Regex regex, string text, int start, int end);
82         }
83
84         interface IMachineFactory {
85                 IMachine NewInstance ();
86                 IDictionary Mapping { get; set; }
87                 int GroupCount { get; }
88         }
89
90         // Anchor SKIP OFFSET
91         //
92         // Flags:       [RightToLeft] ??
93         // SKIP:        relative address of tail expression
94         // OFFSET:      offset of anchor from start of pattern
95         //
96         // Usage:
97         //
98         //      Anchor :1 OFFSET
99         //              <expr>
100         //              True
101         // 1:   <tail>
102         //
103         // Notes:
104         //
105         // In practice, the anchoring expression is only going to be
106         // Position (StartOfString, StartOfLine, StartOfScan) or String.
107         // This is because the optimizer looks for position anchors at the
108         // start of the expression, and if that fails it looks for the
109         // longest substring. If an expression has neither a position
110         // anchor or a longest substring anchor, then the anchoring expression
111         // is left empty. Since an empty expression will anchor at any
112         // position in any string, the entire input string will be scanned.
113
114         // String LEN STR...
115         //
116         // Flags:       [RightToLeft, IgnoreCase]
117         // LEN:         length of string
118         // STR:         string characters
119
120         // Branch SKIP
121         //
122         // SKIP:        relative address of next branch
123         //
124         //      Branch :1
125         //              <alt expr 1>
126         //              Jump :4
127         // 1:   Branch :2
128         //              <alt expr 2>
129         //              Jump :4
130         // 2:   Branch :3
131         //              <alt expr 3>
132         //              Jump :4
133         // 3:   False
134         // 4:   <tail>
135
136         // Repeat SKIP MIN MAX
137         //
138         // Flags:       [Lazy]
139         // SKIP:        relative address of Until instruction
140         // MIN:         minimum iterations
141         // MAX:         maximum iterations (0xffff is infinity)
142         //
143         //      Repeat :1 MIN MAX
144         //              <expr>
145         //              Until
146         // 1:   <tail>
147
148         // FastRepeat SKIP MIN MAX
149         //
150         // Flags:       [Lazy]
151         // SKIP:        relative address of tail expression
152         // MIN:         minimum iterations
153         // MAX:         maximum iterations (0xffff is infinity)
154         //
155         //      FastRepeat :1 MIN MAX
156         //              <expr>
157         //              True
158         // 1:   <tail>
159         //
160         // Notes:
161         //
162         // The subexpression of a FastRepeat construct must not contain any
163         // complex operators. These include: Open, Close, Balance, Repeat,
164         // FastRepeat, Sub, Test. In addition, the subexpression must have
165         // been determined to have a fixed width.
166         
167         // Sub SKIP
168         //
169         // SKIP:        relative address of tail expression
170         //
171         //      Sub :1
172         //              <expr>
173         // 1:   <tail>
174         //
175         // Notes:
176         //
177         // The Sub operator invokes an independent subexpression. This means
178         // that the subexpression will match only once and so will not
179         // participate in any backtracking.
180
181         // Test TSKIP FSKIP
182         //
183         // TSKIP:       relative address of true expression
184         // FSKIP:       relative address of false expression
185         //
186         // Usage:       (?(?=test)true|false)
187         //
188         //      Test :1 :2
189         //              <test expr>
190         // 1:           <true expr>
191         //              Jump
192         // 2:           <false epxr>
193         //      <tail>
194         //
195         // Usage:       (?(?=test)true)
196         //
197         //      Test :1 :2
198         //              <test expr>
199         // 1:           <true expr>
200         // 2:   <tail>
201         //
202         // Usage:       (?=test)
203         //
204         //      Test :1 :2
205         //              <test expr>
206         // 1:           <true expr>
207         //              Jump 3:
208         // 2:           False
209         // 3:           <tail>
210         //
211         // Notes:
212         //
213         // For negative lookaheads, just swap the values of TSKIP and
214         // FSKIP. For lookbehinds, the test expression must be compiled
215         // in reverse. The test expression is always executed as an
216         // independent subexpression, so its behaviour is non-backtracking
217         // (like a Sub clause.)
218
219         // IfDefined SKIP GID
220         //
221         // SKIP:        relative address of else expression
222         // GID:         number of group to check
223         //
224         // Usage:       (?(gid)true)
225         //
226         //      IfDefined :1
227         //              <true expr>
228         // 1:   <tail>
229         //
230         // Usage:       (?(gid)true|false)
231         //
232         //      IfDefined :1
233         //              <true expr>
234         //              Jump :2
235         // 1:           <false expr>
236         // 2:   <tail>
237
238         // Jump SKIP
239         //
240         // SKIP:        relative address of target expression
241         //
242         //      Jump :1
243         //      ...
244         // :1   <target expr>
245
246         // Character CHAR
247         //
248         // Flags:       [Negate, IgnoreCase, RightToLeft]
249         // CHAR:        exact character to match
250
251         // Category CAT
252         //
253         // Flags:       [Negate, RightToLeft]
254         // CAT:         category to match (see Category enum)
255
256         // Range LO HI
257         //
258         // Flags:       [Negate, IgnoreCase, RightToLeft]
259         // LO:          lowest character in range
260         // HI:          higest character in range
261
262         // Set LO LEN SET...
263         //
264         // Flags:       [Negate, IgnoreCase, RightToLeft]
265         // LO:          lowest character in set
266         // LEN:         number of words in set
267         // SET:         bit array representing characters in set
268         //
269         // Notes:
270         //
271         // Each word in the set represents 16 characters, so the first word
272         // defines membership for characters LO to LO + 15, the second for
273         // LO + 16 to LO + 31, and so on up to LO + (LEN * 16 - 1). It is
274         // up to the compiler to provide a compact representation for sparse
275         // unicode sets. The simple way is to use Set 0 4096. Other methods
276         // involve paritioning the set and placing the components into an
277         // In block.
278
279         // In SKIP
280         //
281         // SKIP:        relative address of tail expression
282         //
283         // Usage:       [expr]
284         //
285         //      In :1
286         //              <expr>
287         //              True
288         // :1   <tail>
289         //
290         // Usage:       [^expr]
291         //
292         //      In :1
293         //              <expr>
294         //              False
295         // :1   <tail>
296         //
297         // Notes:
298         //
299         // The In instruction consumes a single character, using the flags
300         // of the first instruction in the subexpression to determine its
301         // IgnoreCase and RightToLeft properties. The subexpression is then
302         // applied to the single character as a disjunction. If any instruction
303         // in the subexpression succeeds, the entire In construct succeeds
304         // and matching continues with the tail.
305
306         // Position POS
307         //
308         // POS:         position to match (see Position enum)
309
310         // Open GID
311         //
312         // GID:         number of group to open
313
314         // Close GID
315         //
316         // GID:         number of group to close
317         
318         // Balance GID BAL
319         //
320         // GID:         number of capturing group (0 if none)
321         // BAL:         number of group to undefine
322
323         // Info GROUPS MIN MAX
324         //
325         // GROUPS:      number of capturing groups
326         // MIN:         minimum width of pattern
327         // MAX:         maximum width of pattern (0xffff means undefined)
328
329         // False
330
331         // True
332
333         // Reference GID
334         //
335         // Flags:       [IgnoreCase, RightToLeft]
336         // GID:         number of group to reference
337 }