1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: compiler.cs
//
// author: Dan Lewis (dlewis@gmx.co.uk)
// (c) 2002
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Diagnostics;
using System.Collections;
namespace System.Text.RegularExpressions {
abstract class LinkRef {
#if TRACE_REGEX
static int next_label = 1;
int label = next_label++;
public override string ToString ()
{
return "L" + label.ToString ();
}
#endif
}
interface ICompiler {
void Reset ();
IMachineFactory GetMachineFactory ();
// instruction emission
void EmitFalse ();
void EmitTrue ();
// character matching
void EmitCharacter (char c, bool negate, bool ignore, bool reverse);
void EmitCategory (Category cat, bool negate, bool reverse);
void EmitNotCategory (Category cat, bool negate, bool reverse);
void EmitRange (char lo, char hi, bool negate, bool ignore, bool reverse);
void EmitSet (char lo, BitArray set, bool negate, bool ignore, bool reverse);
// other operators
void EmitString (string str, bool ignore, bool reverse);
void EmitPosition (Position pos);
void EmitOpen (int gid);
void EmitClose (int gid);
void EmitBalanceStart(int gid, int balance, bool capture, LinkRef tail);
void EmitBalance ();
void EmitReference (int gid, bool ignore, bool reverse);
// constructs
void EmitIfDefined (int gid, LinkRef tail);
void EmitSub (LinkRef tail);
void EmitTest (LinkRef yes, LinkRef tail);
void EmitBranch (LinkRef next);
void EmitJump (LinkRef target);
void EmitRepeat (int min, int max, bool lazy, LinkRef until);
void EmitUntil (LinkRef repeat);
void EmitIn (LinkRef tail);
void EmitInfo (int count, int min, int max);
void EmitFastRepeat (int min, int max, bool lazy, LinkRef tail);
void EmitAnchor (bool reverse, int offset, LinkRef tail);
// event for the CILCompiler
void EmitBranchEnd();
void EmitAlternationEnd();
LinkRef NewLink ();
void ResolveLink (LinkRef link);
}
class InterpreterFactory : IMachineFactory {
public InterpreterFactory (ushort[] pattern) {
this.pattern = pattern;
}
public IMachine NewInstance () {
return new Interpreter (pattern);
}
public int GroupCount {
get { return pattern[1]; }
}
public int Gap {
get { return gap; }
set { gap = value; }
}
public IDictionary Mapping {
get { return mapping; }
set { mapping = value; }
}
public string [] NamesMapping {
get { return namesMapping; }
set { namesMapping = value; }
}
private IDictionary mapping;
private ushort[] pattern;
private string [] namesMapping;
private int gap;
}
class PatternCompiler : ICompiler {
public static ushort EncodeOp (OpCode op, OpFlags flags) {
return (ushort)((int)op | ((int)flags & 0xff00));
}
[Conditional ("TRACE_REGEX")]
static void TraceRegexp (string fmt, params object[] args)
{
Console.Write ("\t");
Console.WriteLine (fmt, args);
}
[Conditional ("TRACE_REGEX")]
static void TraceRegexpLabel (LinkRef lref)
{
Console.Write ("{0}:", lref);
}
public static void DecodeOp (ushort word, out OpCode op, out OpFlags flags) {
op = (OpCode)(word & 0x00ff);
flags = (OpFlags)(word & 0xff00);
}
public PatternCompiler () {
pgm = new ArrayList ();
}
// ICompiler implementation
public void Reset () {
pgm.Clear ();
}
public IMachineFactory GetMachineFactory () {
ushort[] image = new ushort[pgm.Count];
pgm.CopyTo (image);
return new InterpreterFactory (image);
}
public void EmitFalse () {
Emit (OpCode.False);
TraceRegexp ("false");
}
public void EmitTrue () {
Emit (OpCode.True);
TraceRegexp ("true");
}
void EmitCount (int count)
{
uint ucount = (uint) count;
Emit ((ushort) (ucount & 0xFFFF)); // lo 16bits
Emit ((ushort) (ucount >> 16)); // hi
}
public void EmitCharacter (char c, bool negate, bool ignore, bool reverse) {
Emit (OpCode.Character, MakeFlags (negate, ignore, reverse, false));
if (ignore)
c = Char.ToLower (c);
Emit ((ushort)c);
TraceRegexp ("character {0} negate {1} ignore {2} reverse {3}", c, negate, ignore, reverse);
}
public void EmitCategory (Category cat, bool negate, bool reverse) {
Emit (OpCode.Category, MakeFlags (negate, false, reverse, false));
Emit ((ushort)cat);
TraceRegexp ("category {0} negate {1} reverse {2}", cat, negate, reverse);
}
public void EmitNotCategory (Category cat, bool negate, bool reverse) {
Emit (OpCode.NotCategory, MakeFlags (negate, false, reverse, false));
Emit ((ushort)cat);
TraceRegexp ("not category {0} negate {1} reverse {2}", cat, negate, reverse);
}
public void EmitRange (char lo, char hi, bool negate, bool ignore, bool reverse) {
Emit (OpCode.Range, MakeFlags (negate, ignore, reverse, false));
Emit ((ushort)lo);
Emit ((ushort)hi);
TraceRegexp ("char range '{0}' - '{1}' negate {2} ignore {3} reverse {4}", lo, hi, negate, ignore, reverse);
}
public void EmitSet (char lo, BitArray set, bool negate, bool ignore, bool reverse) {
Emit (OpCode.Set, MakeFlags (negate, ignore, reverse, false));
Emit ((ushort)lo);
int len = (set.Length + 0xf) >> 4;
Emit ((ushort)len);
int b = 0;
while (len -- != 0) {
ushort word = 0;
for (int i = 0; i < 16; ++ i) {
if (b >= set.Length)
break;
if (set[b ++])
word |= (ushort)(1 << i);
}
Emit (word);
}
TraceRegexp ("set lo '{0}' - '{1}' negate {2} ignore {3} reverse {4}", lo, set, negate, ignore, reverse);
}
public void EmitString (string str, bool ignore, bool reverse) {
Emit (OpCode.String, MakeFlags (false, ignore, reverse, false));
int len = str.Length;
Emit ((ushort)len);
if (ignore)
str = str.ToLower ();
for (int i = 0; i < len; ++ i)
Emit ((ushort)str[i]);
TraceRegexp ("string '{0}' ignore {1} reverse {2}", str, ignore, reverse);
}
public void EmitPosition (Position pos) {
Emit (OpCode.Position, 0);
Emit ((ushort)pos);
TraceRegexp ("position {0}", pos);
}
public void EmitOpen (int gid) {
Emit (OpCode.Open);
Emit ((ushort)gid);
TraceRegexp ("open {0}", gid);
}
public void EmitClose (int gid) {
Emit (OpCode.Close);
Emit ((ushort)gid);
TraceRegexp ("close {0}", gid);
}
public void EmitBalanceStart (int gid, int balance, bool capture, LinkRef tail) {
BeginLink (tail);
Emit (OpCode.BalanceStart);
Emit ((ushort)gid);
Emit ((ushort)balance);
Emit ((ushort)(capture ? 1 : 0));
EmitLink (tail);
TraceRegexp ("balance start gid {0} balance {1} capture {2} tail {3}", gid, balance, capture, tail);
}
public void EmitBalance () {
Emit (OpCode.Balance);
TraceRegexp ("balance");
}
public void EmitReference (int gid, bool ignore, bool reverse) {
Emit (OpCode.Reference, MakeFlags (false, ignore, reverse, false));
Emit ((ushort)gid);
TraceRegexp ("reference gid {0} ignore {1} reverse {2}", gid, ignore, reverse);
}
public void EmitIfDefined (int gid, LinkRef tail) {
BeginLink (tail);
Emit (OpCode.IfDefined);
EmitLink (tail);
Emit ((ushort)gid);
TraceRegexp ("if defined gid {1} tail {2}", gid, tail);
}
public void EmitSub (LinkRef tail) {
BeginLink (tail);
Emit (OpCode.Sub);
EmitLink (tail);
TraceRegexp ("sub {0}", tail);
}
public void EmitTest (LinkRef yes, LinkRef tail) {
BeginLink (yes);
BeginLink (tail);
Emit (OpCode.Test);
EmitLink (yes);
EmitLink (tail);
TraceRegexp ("test yes {0} tail {1}", yes, tail);
}
public void EmitBranch (LinkRef next) {
BeginLink (next);
Emit (OpCode.Branch, 0);
EmitLink (next);
TraceRegexp ("branch next {0}", next);
}
public void EmitJump (LinkRef target) {
BeginLink (target);
Emit (OpCode.Jump, 0);
EmitLink (target);
TraceRegexp ("jmp target {0}", target);
}
public void EmitRepeat (int min, int max, bool lazy, LinkRef until) {
BeginLink (until);
Emit (OpCode.Repeat, MakeFlags (false, false, false, lazy));
EmitLink (until);
EmitCount (min);
EmitCount (max);
TraceRegexp ("repeat min {0} max {1} lazy {2} until {3}", min, max, lazy, until);
}
public void EmitUntil (LinkRef repeat) {
ResolveLink (repeat);
Emit (OpCode.Until);
TraceRegexp ("end until {0}", repeat);
}
public void EmitFastRepeat (int min, int max, bool lazy, LinkRef tail) {
BeginLink (tail);
Emit (OpCode.FastRepeat, MakeFlags (false, false, false, lazy));
EmitLink (tail);
EmitCount (min);
EmitCount (max);
TraceRegexp ("repeat-fast min {0} max {1} lazy {2} tail {3}", min, max, lazy, tail);
}
public void EmitIn (LinkRef tail) {
BeginLink (tail);
Emit (OpCode.In);
EmitLink (tail);
TraceRegexp ("in tail {0}", tail);
}
public void EmitAnchor (bool reverse, int offset, LinkRef tail) {
BeginLink (tail);
Emit (OpCode.Anchor, MakeFlags(false, false, reverse, false));
EmitLink (tail);
Emit ((ushort)offset);
TraceRegexp ("anchor reverse {0} offset {1} tail {2}", reverse, offset, tail);
}
public void EmitInfo (int count, int min, int max) {
Emit (OpCode.Info);
EmitCount (count);
EmitCount (min);
EmitCount (max);
TraceRegexp ("info group count {0} match_min {1} match_max {2}", count, min, max);
}
public LinkRef NewLink () {
return new PatternLinkStack ();
}
public void ResolveLink (LinkRef lref) {
PatternLinkStack stack = (PatternLinkStack)lref;
while (stack.Pop ())
pgm[stack.OffsetAddress] = (ushort)stack.GetOffset (CurrentAddress);
TraceRegexpLabel (lref);
}
public void EmitBranchEnd(){}
public void EmitAlternationEnd(){}
// private members
private static OpFlags MakeFlags (bool negate, bool ignore, bool reverse, bool lazy) {
OpFlags flags = 0;
if (negate) flags |= OpFlags.Negate;
if (ignore) flags |= OpFlags.IgnoreCase;
if (reverse) flags |= OpFlags.RightToLeft;
if (lazy) flags |= OpFlags.Lazy;
return flags;
}
private void Emit (OpCode op) {
Emit (op, (OpFlags)0);
}
private void Emit (OpCode op, OpFlags flags) {
Emit (EncodeOp (op, flags));
}
private void Emit (ushort word) {
pgm.Add (word);
}
private int CurrentAddress {
get { return pgm.Count; }
}
private void BeginLink (LinkRef lref) {
PatternLinkStack stack = (PatternLinkStack)lref;
stack.BaseAddress = CurrentAddress;
}
private void EmitLink (LinkRef lref) {
PatternLinkStack stack = (PatternLinkStack)lref;
stack.OffsetAddress = CurrentAddress;
Emit ((ushort)0); // placeholder
stack.Push ();
}
private class PatternLinkStack : LinkStack {
public PatternLinkStack () {
}
public int BaseAddress {
set { link.base_addr = value; }
}
public int OffsetAddress {
get { return link.offset_addr; }
set { link.offset_addr = value; }
}
public int GetOffset (int target_addr) {
return target_addr - link.base_addr;
}
// LinkStack implementation
protected override object GetCurrent () { return link; }
protected override void SetCurrent (object l) { link = (Link)l; }
private struct Link {
public int base_addr;
public int offset_addr;
}
Link link;
}
private ArrayList pgm;
}
abstract class LinkStack : LinkRef {
public LinkStack () {
stack = new Stack ();
}
public void Push () {
stack.Push (GetCurrent ());
}
public bool Pop () {
if (stack.Count > 0) {
SetCurrent (stack.Pop ());
return true;
}
return false;
}
protected abstract object GetCurrent ();
protected abstract void SetCurrent (object l);
private Stack stack;
}
//Used by CILCompiler and Interpreter
internal struct Mark {
public int Start, End;
public int Previous;
public bool IsDefined {
get { return Start >= 0 && End >= 0; }
}
public int Index {
get { return Start < End ? Start : End; }
}
public int Length {
get { return Start < End ? End - Start : Start - End; }
}
}
}
|