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]; }
105 public IDictionary Mapping {
106 get { return mapping; }
107 set { mapping = value; }
110 public string [] NamesMapping {
111 get { return namesMapping; }
112 set { namesMapping = value; }
115 private IDictionary mapping;
116 private ushort[] pattern;
117 private string [] namesMapping;
121 class PatternCompiler : ICompiler {
122 public static ushort EncodeOp (OpCode op, OpFlags flags) {
123 return (ushort)((int)op | ((int)flags & 0xff00));
126 internal static readonly bool trace_regex =
128 Environment.GetEnvironmentVariable ("MONO_TRACE_REGEX") != null;
132 public static void DecodeOp (ushort word, out OpCode op, out OpFlags flags) {
133 op = (OpCode)(word & 0x00ff);
134 flags = (OpFlags)(word & 0xff00);
137 public PatternCompiler () {
138 pgm = new ArrayList ();
141 // ICompiler implementation
143 public void Reset () {
147 public IMachineFactory GetMachineFactory () {
148 ushort[] image = new ushort[pgm.Count];
151 return new InterpreterFactory (image);
154 public void EmitFalse () {
157 Console.WriteLine ("false");
160 public void EmitTrue () {
163 Console.WriteLine ("true");
166 void EmitCount (int count)
168 uint ucount = (uint) count;
169 Emit ((ushort) (ucount & 0xFFFF)); // lo 16bits
170 Emit ((ushort) (ucount >> 16)); // hi
172 Console.WriteLine ("count {0}", count);
175 public void EmitCharacter (char c, bool negate, bool ignore, bool reverse) {
176 Emit (OpCode.Character, MakeFlags (negate, ignore, reverse, false));
179 c = Char.ToLower (c);
184 Console.WriteLine ("character {0} negate {1} ignore {2} reverse {3}", c, negate, ignore, reverse);
187 public void EmitCategory (Category cat, bool negate, bool reverse) {
188 Emit (OpCode.Category, MakeFlags (negate, false, reverse, false));
192 Console.WriteLine ("category {0} negate {1} reverse {2}", cat, negate, reverse);
195 public void EmitNotCategory (Category cat, bool negate, bool reverse) {
196 Emit (OpCode.NotCategory, MakeFlags (negate, false, reverse, false));
200 Console.WriteLine ("not category {0} negate {1} reverse {2}", cat, negate, reverse);
203 public void EmitRange (char lo, char hi, bool negate, bool ignore, bool reverse) {
204 Emit (OpCode.Range, MakeFlags (negate, ignore, reverse, false));
209 Console.WriteLine ("char range '{0}' - '{1}' negate {2} ignore {3} reverse {4}", lo, hi, negate, ignore, reverse);
212 public void EmitSet (char lo, BitArray set, bool negate, bool ignore, bool reverse) {
213 Emit (OpCode.Set, MakeFlags (negate, ignore, reverse, false));
216 int len = (set.Length + 0xf) >> 4;
220 while (len -- != 0) {
222 for (int i = 0; i < 16; ++ i) {
227 word |= (ushort)(1 << i);
234 Console.WriteLine ("set lo '{0}' - '{1}' negate {2} ignore {3} reverse {4}", lo, set, negate, ignore, reverse);
237 public void EmitString (string str, bool ignore, bool reverse) {
238 Emit (OpCode.String, MakeFlags (false, ignore, reverse, false));
239 int len = str.Length;
243 str = str.ToLower ();
245 for (int i = 0; i < len; ++ i)
246 Emit ((ushort)str[i]);
248 Console.WriteLine ("string '{0}' ignore {1} reverse {2}", str, ignore, reverse);
251 public void EmitPosition (Position pos) {
252 Emit (OpCode.Position, 0);
256 Console.WriteLine ("position {0}", pos);
259 public void EmitOpen (int gid) {
264 Console.WriteLine ("open {0}", gid);
267 public void EmitClose (int gid) {
272 Console.WriteLine ("close {0}", gid);
277 public void EmitBalanceStart (int gid, int balance, bool capture, LinkRef tail) {
279 Emit (OpCode.BalanceStart);
281 Emit ((ushort)balance);
282 Emit ((ushort)(capture ? 1 : 0));
286 Console.WriteLine ("balance start gid {0} balance {1} capture {2} tail {3}", gid, balance, capture, tail);
289 public void EmitBalance () {
290 Emit (OpCode.Balance);
293 Console.WriteLine ("balance");
296 public void EmitReference (int gid, bool ignore, bool reverse) {
297 Emit (OpCode.Reference, MakeFlags (false, ignore, reverse, false));
301 Console.WriteLine ("reference gid {0} ignore {1} reverse {2}", gid, ignore, reverse);
304 public void EmitIfDefined (int gid, LinkRef tail) {
306 Emit (OpCode.IfDefined);
311 Console.WriteLine ("if defined gid {1} tail {2}", gid, tail);
314 public void EmitSub (LinkRef tail) {
320 Console.WriteLine ("sub {0}", tail);
323 public void EmitTest (LinkRef yes, LinkRef tail) {
331 Console.WriteLine ("test yes {0} tail {1}", yes, tail);
334 public void EmitBranch (LinkRef next) {
336 Emit (OpCode.Branch, 0);
340 Console.WriteLine ("branch next {0}", next);
343 public void EmitJump (LinkRef target) {
345 Emit (OpCode.Jump, 0);
349 Console.WriteLine ("jmp target {0}", target);
352 public void EmitRepeat (int min, int max, bool lazy, LinkRef until) {
354 Emit (OpCode.Repeat, MakeFlags (false, false, false, lazy));
360 Console.WriteLine ("repeat min {0} max {1} lazy {2} until {3}", min, max, lazy, until);
363 public void EmitUntil (LinkRef repeat) {
364 ResolveLink (repeat);
368 Console.WriteLine ("end until {0}", repeat);
371 public void EmitFastRepeat (int min, int max, bool lazy, LinkRef tail) {
373 Emit (OpCode.FastRepeat, MakeFlags (false, false, false, lazy));
379 Console.WriteLine ("repeat-fast min {0} max {1} lazy {2} tail {3}", min, max, lazy, tail);
382 public void EmitIn (LinkRef tail) {
388 Console.WriteLine ("in tail {0}", tail);
391 public void EmitAnchor (bool reverse, int offset, LinkRef tail) {
393 Emit (OpCode.Anchor, MakeFlags(false, false, reverse, false));
395 Emit ((ushort)offset);
398 Console.WriteLine ("anchor reverse {0} offset {1} tail {2}", reverse, offset, tail);
401 public void EmitInfo (int count, int min, int max) {
408 Console.WriteLine ("info group count {0} match_min {1} match_max {2}", count, min, max);
411 public LinkRef NewLink () {
412 return new PatternLinkStack ();
415 public void ResolveLink (LinkRef lref) {
416 PatternLinkStack stack = (PatternLinkStack)lref;
419 pgm[stack.OffsetAddress] = (ushort)stack.GetOffset (CurrentAddress);
422 public void EmitBranchEnd(){}
423 public void EmitAlternationEnd(){}
427 private static OpFlags MakeFlags (bool negate, bool ignore, bool reverse, bool lazy) {
429 if (negate) flags |= OpFlags.Negate;
430 if (ignore) flags |= OpFlags.IgnoreCase;
431 if (reverse) flags |= OpFlags.RightToLeft;
432 if (lazy) flags |= OpFlags.Lazy;
437 private void Emit (OpCode op) {
438 Emit (op, (OpFlags)0);
441 private void Emit (OpCode op, OpFlags flags) {
442 Emit (EncodeOp (op, flags));
445 private void Emit (ushort word) {
449 private int CurrentAddress {
450 get { return pgm.Count; }
453 private void BeginLink (LinkRef lref) {
454 PatternLinkStack stack = (PatternLinkStack)lref;
455 stack.BaseAddress = CurrentAddress;
458 private void EmitLink (LinkRef lref) {
459 PatternLinkStack stack = (PatternLinkStack)lref;
460 stack.OffsetAddress = CurrentAddress;
461 Emit ((ushort)0); // placeholder
466 private class PatternLinkStack : LinkStack {
467 public PatternLinkStack () {
470 public int BaseAddress {
471 set { link.base_addr = value; }
474 public int OffsetAddress {
475 get { return link.offset_addr; }
476 set { link.offset_addr = value; }
479 public int GetOffset (int target_addr) {
480 return target_addr - link.base_addr;
483 // LinkStack implementation
485 protected override object GetCurrent () { return link; }
486 protected override void SetCurrent (object l) { link = (Link)l; }
488 private struct Link {
489 public int base_addr;
490 public int offset_addr;
496 private ArrayList pgm;
499 abstract class LinkStack : LinkRef {
500 public LinkStack () {
501 stack = new Stack ();
504 public void Push () {
505 stack.Push (GetCurrent ());
509 if (stack.Count > 0) {
510 SetCurrent (stack.Pop ());
517 protected abstract object GetCurrent ();
518 protected abstract void SetCurrent (object l);
523 //Used by CILCompiler and Interpreter
524 internal struct Mark {
525 public int Start, End;
528 public bool IsDefined {
529 get { return Start >= 0 && End >= 0; }
533 get { return Start < End ? Start : End; }
537 get { return Start < End ? End - Start : Start - End; }