3 // namespace: System.Text.RegularExpressions
6 // author: Dan Lewis (dlewis@gmx.co.uk)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 namespace System.Text.RegularExpressions {
34 abstract class LinkRef {
40 IMachineFactory GetMachineFactory ();
42 // instruction emission
49 void EmitCharacter (char c, bool negate, bool ignore, bool reverse);
50 void EmitCategory (Category cat, bool negate, bool reverse);
51 void EmitNotCategory (Category cat, bool negate, bool reverse);
52 void EmitRange (char lo, char hi, bool negate, bool ignore, bool reverse);
53 void EmitSet (char lo, BitArray set, bool negate, bool ignore, bool reverse);
57 void EmitString (string str, bool ignore, bool reverse);
58 void EmitPosition (Position pos);
59 void EmitOpen (int gid);
60 void EmitClose (int gid);
61 void EmitBalanceStart(int gid, int balance, bool capture, LinkRef tail);
63 void EmitReference (int gid, bool ignore, bool reverse);
67 void EmitIfDefined (int gid, LinkRef tail);
68 void EmitSub (LinkRef tail);
69 void EmitTest (LinkRef yes, LinkRef tail);
70 void EmitBranch (LinkRef next);
71 void EmitJump (LinkRef target);
72 void EmitRepeat (int min, int max, bool lazy, LinkRef until);
73 void EmitUntil (LinkRef repeat);
74 void EmitIn (LinkRef tail);
75 void EmitInfo (int count, int min, int max);
76 void EmitFastRepeat (int min, int max, bool lazy, LinkRef tail);
77 void EmitAnchor (bool reverse, int offset, LinkRef tail);
79 // event for the CILCompiler
81 void EmitAlternationEnd();
84 void ResolveLink (LinkRef link);
87 class InterpreterFactory : IMachineFactory {
88 public InterpreterFactory (ushort[] pattern) {
89 this.pattern = pattern;
92 public IMachine NewInstance () {
93 return new Interpreter (pattern);
96 public int GroupCount {
97 get { return pattern[1]; }
100 public IDictionary Mapping {
101 get { return mapping; }
102 set { mapping = value; }
105 private IDictionary mapping;
106 private ushort[] pattern;
109 class PatternCompiler : ICompiler {
110 public static ushort EncodeOp (OpCode op, OpFlags flags) {
111 return (ushort)((int)op | ((int)flags & 0xff00));
114 public static void DecodeOp (ushort word, out OpCode op, out OpFlags flags) {
115 op = (OpCode)(word & 0x00ff);
116 flags = (OpFlags)(word & 0xff00);
119 public PatternCompiler () {
120 pgm = new ArrayList ();
123 // ICompiler implementation
125 public void Reset () {
129 public IMachineFactory GetMachineFactory () {
130 ushort[] image = new ushort[pgm.Count];
133 return new InterpreterFactory (image);
136 public void EmitFalse () {
140 public void EmitTrue () {
144 void EmitCount (int count)
146 uint ucount = (uint) count;
147 Emit ((ushort) (ucount & 0xFFFF)); // lo 16bits
148 Emit ((ushort) (ucount >> 16)); // hi
151 public void EmitCharacter (char c, bool negate, bool ignore, bool reverse) {
152 Emit (OpCode.Character, MakeFlags (negate, ignore, reverse, false));
155 c = Char.ToLower (c);
160 public void EmitCategory (Category cat, bool negate, bool reverse) {
161 Emit (OpCode.Category, MakeFlags (negate, false, reverse, false));
165 public void EmitNotCategory (Category cat, bool negate, bool reverse) {
166 Emit (OpCode.NotCategory, MakeFlags (negate, false, reverse, false));
170 public void EmitRange (char lo, char hi, bool negate, bool ignore, bool reverse) {
171 Emit (OpCode.Range, MakeFlags (negate, ignore, reverse, false));
176 public void EmitSet (char lo, BitArray set, bool negate, bool ignore, bool reverse) {
177 Emit (OpCode.Set, MakeFlags (negate, ignore, reverse, false));
180 int len = (set.Length + 0xf) >> 4;
184 while (len -- != 0) {
186 for (int i = 0; i < 16; ++ i) {
191 word |= (ushort)(1 << i);
198 public void EmitString (string str, bool ignore, bool reverse) {
199 Emit (OpCode.String, MakeFlags (false, ignore, reverse, false));
200 int len = str.Length;
204 str = str.ToLower ();
206 for (int i = 0; i < len; ++ i)
207 Emit ((ushort)str[i]);
210 public void EmitPosition (Position pos) {
211 Emit (OpCode.Position, 0);
215 public void EmitOpen (int gid) {
220 public void EmitClose (int gid) {
227 public void EmitBalanceStart (int gid, int balance, bool capture, LinkRef tail) {
229 Emit (OpCode.BalanceStart);
231 Emit ((ushort)balance);
232 Emit ((ushort)(capture ? 1 : 0));
236 public void EmitBalance () {
237 Emit (OpCode.Balance);
240 public void EmitReference (int gid, bool ignore, bool reverse) {
241 Emit (OpCode.Reference, MakeFlags (false, ignore, reverse, false));
245 public void EmitIfDefined (int gid, LinkRef tail) {
247 Emit (OpCode.IfDefined);
252 public void EmitSub (LinkRef tail) {
258 public void EmitTest (LinkRef yes, LinkRef tail) {
266 public void EmitBranch (LinkRef next) {
268 Emit (OpCode.Branch, 0);
272 public void EmitJump (LinkRef target) {
274 Emit (OpCode.Jump, 0);
278 public void EmitRepeat (int min, int max, bool lazy, LinkRef until) {
280 Emit (OpCode.Repeat, MakeFlags (false, false, false, lazy));
286 public void EmitUntil (LinkRef repeat) {
287 ResolveLink (repeat);
291 public void EmitFastRepeat (int min, int max, bool lazy, LinkRef tail) {
293 Emit (OpCode.FastRepeat, MakeFlags (false, false, false, lazy));
299 public void EmitIn (LinkRef tail) {
305 public void EmitAnchor (bool reverse, int offset, LinkRef tail) {
307 Emit (OpCode.Anchor, MakeFlags(false, false, reverse, false));
309 Emit ((ushort)offset);
312 public void EmitInfo (int count, int min, int max) {
319 public LinkRef NewLink () {
320 return new PatternLinkStack ();
323 public void ResolveLink (LinkRef lref) {
324 PatternLinkStack stack = (PatternLinkStack)lref;
327 pgm[stack.OffsetAddress] = (ushort)stack.GetOffset (CurrentAddress);
330 public void EmitBranchEnd(){}
331 public void EmitAlternationEnd(){}
335 private static OpFlags MakeFlags (bool negate, bool ignore, bool reverse, bool lazy) {
337 if (negate) flags |= OpFlags.Negate;
338 if (ignore) flags |= OpFlags.IgnoreCase;
339 if (reverse) flags |= OpFlags.RightToLeft;
340 if (lazy) flags |= OpFlags.Lazy;
345 private void Emit (OpCode op) {
346 Emit (op, (OpFlags)0);
349 private void Emit (OpCode op, OpFlags flags) {
350 Emit (EncodeOp (op, flags));
353 private void Emit (ushort word) {
357 private int CurrentAddress {
358 get { return pgm.Count; }
361 private void BeginLink (LinkRef lref) {
362 PatternLinkStack stack = (PatternLinkStack)lref;
363 stack.BaseAddress = CurrentAddress;
366 private void EmitLink (LinkRef lref) {
367 PatternLinkStack stack = (PatternLinkStack)lref;
368 stack.OffsetAddress = CurrentAddress;
369 Emit ((ushort)0); // placeholder
374 private class PatternLinkStack : LinkStack {
375 public PatternLinkStack () {
378 public int BaseAddress {
379 set { link.base_addr = value; }
382 public int OffsetAddress {
383 get { return link.offset_addr; }
384 set { link.offset_addr = value; }
387 public int GetOffset (int target_addr) {
388 return target_addr - link.base_addr;
391 // LinkStack implementation
393 protected override object GetCurrent () { return link; }
394 protected override void SetCurrent (object l) { link = (Link)l; }
396 private struct Link {
397 public int base_addr;
398 public int offset_addr;
404 private ArrayList pgm;
407 abstract class LinkStack : LinkRef {
408 public LinkStack () {
409 stack = new Stack ();
412 public void Push () {
413 stack.Push (GetCurrent ());
417 if (stack.Count > 0) {
418 SetCurrent (stack.Pop ());
425 protected abstract object GetCurrent ();
426 protected abstract void SetCurrent (object l);
431 //Used by CILCompiler and Interpreter
432 internal struct Mark {
433 public int Start, End;
436 public bool IsDefined {
437 get { return Start >= 0 && End >= 0; }
441 get { return Start < End ? Start : End; }
445 get { return Start < End ? End - Start : Start - End; }