| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// <copyright file="RegexWriter.cs" company="Microsoft"> |
| 3 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 |
// </copyright> |
| 5 |
//------------------------------------------------------------------------------ |
| 6 |
|
| 7 |
// This RegexWriter class is internal to the Regex package. |
| 8 |
// It builds a block of regular expression codes (RegexCode) |
| 9 |
// from a RegexTree parse tree. |
| 10 |
|
| 11 |
// Implementation notes: |
| 12 |
// |
| 13 |
// This step is as simple as walking the tree and emitting |
| 14 |
// sequences of codes. |
| 15 |
// |
| 16 |
|
| 17 |
|
| 18 |
namespace System.Text.RegularExpressions { |
| 19 |
|
| 20 |
using System.Collections.Generic; |
| 21 |
using System.Collections; |
| 22 |
using System.Globalization; |
| 23 |
|
| 24 |
internal sealed class RegexWriter { |
| 25 |
internal int[] _intStack; |
| 26 |
internal int _depth; |
| 27 |
internal int[] _emitted; |
| 28 |
internal int _curpos; |
| 29 |
internal Dictionary<string, int> _stringhash; |
| 30 |
internal List<String> _stringtable; |
| 31 |
// not used! internal int _stringcount; |
| 32 |
internal bool _counting; |
| 33 |
internal int _count; |
| 34 |
internal int _trackcount; |
| 35 |
#if SILVERLIGHT |
| 36 |
internal Dictionary<Int32, Int32> _caps; |
| 37 |
#else |
| 38 |
internal Hashtable _caps; |
| 39 |
#endif |
| 40 |
|
| 41 |
internal const int BeforeChild = 64; |
| 42 |
internal const int AfterChild = 128; |
| 43 |
|
| 44 |
/* |
| 45 |
* This is the only function that should be called from outside. |
| 46 |
* It takes a RegexTree and creates a corresponding RegexCode. |
| 47 |
*/ |
| 48 |
internal static RegexCode Write(RegexTree t) { |
| 49 |
RegexWriter w = new RegexWriter(); |
| 50 |
RegexCode retval = w.RegexCodeFromRegexTree(t); |
| 51 |
#if DBG |
| 52 |
if (t.Debug) { |
| 53 |
t.Dump(); |
| 54 |
retval.Dump(); |
| 55 |
} |
| 56 |
#endif |
| 57 |
return retval; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* private constructor; can't be created outside |
| 62 |
*/ |
| 63 |
private RegexWriter() { |
| 64 |
_intStack = new int[32]; |
| 65 |
_emitted = new int[32]; |
| 66 |
_stringhash = new Dictionary<string, int>(); |
| 67 |
_stringtable = new List<String>(); |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
* To avoid recursion, we use a simple integer stack. |
| 72 |
* This is the push. |
| 73 |
*/ |
| 74 |
internal void PushInt(int I) { |
| 75 |
if (_depth >= _intStack.Length) { |
| 76 |
int [] expanded = new int[_depth * 2]; |
| 77 |
|
| 78 |
System.Array.Copy(_intStack, 0, expanded, 0, _depth); |
| 79 |
|
| 80 |
_intStack = expanded; |
| 81 |
} |
| 82 |
|
| 83 |
_intStack[_depth++] = I; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* True if the stack is empty. |
| 88 |
*/ |
| 89 |
internal bool EmptyStack() { |
| 90 |
return _depth == 0; |
| 91 |
} |
| 92 |
|
| 93 |
/* |
| 94 |
* This is the pop. |
| 95 |
*/ |
| 96 |
internal int PopInt() { |
| 97 |
return _intStack[--_depth]; |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* Returns the current position in the emitted code. |
| 102 |
*/ |
| 103 |
internal int CurPos() { |
| 104 |
return _curpos; |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
* Fixes up a jump instruction at the specified offset |
| 109 |
* so that it jumps to the specified jumpDest. |
| 110 |
*/ |
| 111 |
internal void PatchJump(int Offset, int jumpDest) { |
| 112 |
_emitted[Offset + 1] = jumpDest; |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Emits a zero-argument operation. Note that the emit |
| 117 |
* functions all run in two modes: they can emit code, or |
| 118 |
* they can just count the size of the code. |
| 119 |
*/ |
| 120 |
internal void Emit(int op) { |
| 121 |
if (_counting) { |
| 122 |
_count += 1; |
| 123 |
if (RegexCode.OpcodeBacktracks(op)) |
| 124 |
_trackcount += 1; |
| 125 |
return; |
| 126 |
} |
| 127 |
_emitted[_curpos++] = op; |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* Emits a one-argument operation. |
| 132 |
*/ |
| 133 |
internal void Emit(int op, int opd1) { |
| 134 |
if (_counting) { |
| 135 |
_count += 2; |
| 136 |
if (RegexCode.OpcodeBacktracks(op)) |
| 137 |
_trackcount += 1; |
| 138 |
return; |
| 139 |
} |
| 140 |
_emitted[_curpos++] = op; |
| 141 |
_emitted[_curpos++] = opd1; |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* Emits a two-argument operation. |
| 146 |
*/ |
| 147 |
internal void Emit(int op, int opd1, int opd2) { |
| 148 |
if (_counting) { |
| 149 |
_count += 3; |
| 150 |
if (RegexCode.OpcodeBacktracks(op)) |
| 151 |
_trackcount += 1; |
| 152 |
return; |
| 153 |
} |
| 154 |
_emitted[_curpos++] = op; |
| 155 |
_emitted[_curpos++] = opd1; |
| 156 |
_emitted[_curpos++] = opd2; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Returns an index in the string table for a string; |
| 161 |
* uses a hashtable to eliminate duplicates. |
| 162 |
*/ |
| 163 |
internal int StringCode(String str) { |
| 164 |
Int32 i; |
| 165 |
|
| 166 |
if (_counting) |
| 167 |
return 0; |
| 168 |
|
| 169 |
if (str == null) |
| 170 |
str = String.Empty; |
| 171 |
|
| 172 |
if (_stringhash.ContainsKey(str)) { |
| 173 |
i = (Int32)_stringhash[str]; |
| 174 |
} |
| 175 |
else { |
| 176 |
i = _stringtable.Count; |
| 177 |
_stringhash[str] = i; |
| 178 |
_stringtable.Add(str); |
| 179 |
} |
| 180 |
|
| 181 |
return i; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* Just returns an exception; should be dead code |
| 186 |
*/ |
| 187 |
internal ArgumentException MakeException(String message) { |
| 188 |
return new ArgumentException(message); |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* When generating code on a regex that uses a sparse set |
| 193 |
* of capture slots, we hash them to a dense set of indices |
| 194 |
* for an array of capture slots. Instead of doing the hash |
| 195 |
* at match time, it's done at compile time, here. |
| 196 |
*/ |
| 197 |
internal int MapCapnum(int capnum) { |
| 198 |
if (capnum == -1) |
| 199 |
return -1; |
| 200 |
|
| 201 |
if (_caps != null) |
| 202 |
#if SILVERLIGHT |
| 203 |
return _caps[capnum]; |
| 204 |
#else |
| 205 |
return(Int32)_caps[capnum]; |
| 206 |
#endif |
| 207 |
else |
| 208 |
return capnum; |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
* The top level RegexCode generator. It does a depth-first walk |
| 213 |
* through the tree and calls EmitFragment to emits code before |
| 214 |
* and after each child of an interior node, and at each leaf. |
| 215 |
* |
| 216 |
* It runs two passes, first to count the size of the generated |
| 217 |
* code, and second to generate the code. |
| 218 |
* |
| 219 |
* < |
| 220 |
|
| 221 |
*/ |
| 222 |
internal RegexCode RegexCodeFromRegexTree(RegexTree tree) { |
| 223 |
RegexNode curNode; |
| 224 |
int curChild; |
| 225 |
int capsize; |
| 226 |
RegexPrefix fcPrefix; |
| 227 |
RegexPrefix prefix; |
| 228 |
int anchors; |
| 229 |
RegexBoyerMoore bmPrefix; |
| 230 |
bool rtl; |
| 231 |
|
| 232 |
// construct sparse capnum mapping if some numbers are unused |
| 233 |
|
| 234 |
if (tree._capnumlist == null || tree._captop == tree._capnumlist.Length) { |
| 235 |
capsize = tree._captop; |
| 236 |
_caps = null; |
| 237 |
} |
| 238 |
else { |
| 239 |
capsize = tree._capnumlist.Length; |
| 240 |
_caps = tree._caps; |
| 241 |
for (int i = 0; i < tree._capnumlist.Length; i++) |
| 242 |
_caps[tree._capnumlist[i]] = i; |
| 243 |
} |
| 244 |
|
| 245 |
_counting = true; |
| 246 |
|
| 247 |
for (;;) { |
| 248 |
if (!_counting) |
| 249 |
_emitted = new int[_count]; |
| 250 |
|
| 251 |
curNode = tree._root; |
| 252 |
curChild = 0; |
| 253 |
|
| 254 |
Emit(RegexCode.Lazybranch, 0); |
| 255 |
|
| 256 |
for (;;) { |
| 257 |
if (curNode._children == null) { |
| 258 |
EmitFragment(curNode._type, curNode, 0); |
| 259 |
} |
| 260 |
else if (curChild < curNode._children.Count) { |
| 261 |
EmitFragment(curNode._type | BeforeChild, curNode, curChild); |
| 262 |
|
| 263 |
curNode = (RegexNode)curNode._children[curChild]; |
| 264 |
PushInt(curChild); |
| 265 |
curChild = 0; |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
if (EmptyStack()) |
| 270 |
break; |
| 271 |
|
| 272 |
curChild = PopInt(); |
| 273 |
curNode = curNode._next; |
| 274 |
|
| 275 |
EmitFragment(curNode._type | AfterChild, curNode, curChild); |
| 276 |
curChild++; |
| 277 |
} |
| 278 |
|
| 279 |
PatchJump(0, CurPos()); |
| 280 |
Emit(RegexCode.Stop); |
| 281 |
|
| 282 |
if (!_counting) |
| 283 |
break; |
| 284 |
|
| 285 |
_counting = false; |
| 286 |
} |
| 287 |
|
| 288 |
fcPrefix = RegexFCD.FirstChars(tree); |
| 289 |
|
| 290 |
prefix = RegexFCD.Prefix(tree); |
| 291 |
rtl = ((tree._options & RegexOptions.RightToLeft) != 0); |
| 292 |
|
| 293 |
CultureInfo culture = (tree._options & RegexOptions.CultureInvariant) != 0 ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture; |
| 294 |
if (prefix != null && prefix.Prefix.Length > 0) |
| 295 |
bmPrefix = new RegexBoyerMoore(prefix.Prefix, prefix.CaseInsensitive, rtl, culture); |
| 296 |
else |
| 297 |
bmPrefix = null; |
| 298 |
|
| 299 |
anchors = RegexFCD.Anchors(tree); |
| 300 |
|
| 301 |
return new RegexCode(_emitted, _stringtable, _trackcount, _caps, capsize, bmPrefix, fcPrefix, anchors, rtl); |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* The main RegexCode generator. It does a depth-first walk |
| 306 |
* through the tree and calls EmitFragment to emits code before |
| 307 |
* and after each child of an interior node, and at each leaf. |
| 308 |
*/ |
| 309 |
internal void EmitFragment(int nodetype, RegexNode node, int CurIndex) { |
| 310 |
int bits = 0; |
| 311 |
|
| 312 |
if (nodetype <= RegexNode.Ref) { |
| 313 |
if (node.UseOptionR()) |
| 314 |
bits |= RegexCode.Rtl; |
| 315 |
if ((node._options & RegexOptions.IgnoreCase) != 0) |
| 316 |
bits |= RegexCode.Ci; |
| 317 |
} |
| 318 |
|
| 319 |
switch (nodetype) { |
| 320 |
case RegexNode.Concatenate | BeforeChild: |
| 321 |
case RegexNode.Concatenate | AfterChild: |
| 322 |
case RegexNode.Empty: |
| 323 |
break; |
| 324 |
|
| 325 |
case RegexNode.Alternate | BeforeChild: |
| 326 |
if (CurIndex < node._children.Count - 1) { |
| 327 |
PushInt(CurPos()); |
| 328 |
Emit(RegexCode.Lazybranch, 0); |
| 329 |
} |
| 330 |
break; |
| 331 |
|
| 332 |
case RegexNode.Alternate | AfterChild: { |
| 333 |
|
| 334 |
if (CurIndex < node._children.Count - 1) { |
| 335 |
int LBPos = PopInt(); |
| 336 |
PushInt(CurPos()); |
| 337 |
Emit(RegexCode.Goto, 0); |
| 338 |
PatchJump(LBPos, CurPos()); |
| 339 |
} |
| 340 |
else { |
| 341 |
int I; |
| 342 |
for (I = 0; I < CurIndex; I++) { |
| 343 |
PatchJump(PopInt(), CurPos()); |
| 344 |
} |
| 345 |
} |
| 346 |
break; |
| 347 |
} |
| 348 |
|
| 349 |
case RegexNode.Testref | BeforeChild: |
| 350 |
switch (CurIndex) { |
| 351 |
case 0: |
| 352 |
Emit(RegexCode.Setjump); |
| 353 |
PushInt(CurPos()); |
| 354 |
Emit(RegexCode.Lazybranch, 0); |
| 355 |
Emit(RegexCode.Testref, MapCapnum(node._m)); |
| 356 |
Emit(RegexCode.Forejump); |
| 357 |
break; |
| 358 |
} |
| 359 |
break; |
| 360 |
|
| 361 |
case RegexNode.Testref | AfterChild: |
| 362 |
switch (CurIndex) { |
| 363 |
case 0: { |
| 364 |
int Branchpos = PopInt(); |
| 365 |
PushInt(CurPos()); |
| 366 |
Emit(RegexCode.Goto, 0); |
| 367 |
PatchJump(Branchpos, CurPos()); |
| 368 |
Emit(RegexCode.Forejump); |
| 369 |
if (node._children.Count > 1) |
| 370 |
break; |
| 371 |
// else fallthrough |
| 372 |
goto case 1; |
| 373 |
} |
| 374 |
case 1: |
| 375 |
PatchJump(PopInt(), CurPos()); |
| 376 |
break; |
| 377 |
} |
| 378 |
break; |
| 379 |
|
| 380 |
case RegexNode.Testgroup | BeforeChild: |
| 381 |
switch (CurIndex) { |
| 382 |
case 0: |
| 383 |
Emit(RegexCode.Setjump); |
| 384 |
Emit(RegexCode.Setmark); |
| 385 |
PushInt(CurPos()); |
| 386 |
Emit(RegexCode.Lazybranch, 0); |
| 387 |
break; |
| 388 |
} |
| 389 |
break; |
| 390 |
|
| 391 |
case RegexNode.Testgroup | AfterChild: |
| 392 |
switch (CurIndex) { |
| 393 |
case 0: |
| 394 |
Emit(RegexCode.Getmark); |
| 395 |
Emit(RegexCode.Forejump); |
| 396 |
break; |
| 397 |
case 1: |
| 398 |
int Branchpos = PopInt(); |
| 399 |
PushInt(CurPos()); |
| 400 |
Emit(RegexCode.Goto, 0); |
| 401 |
PatchJump(Branchpos, CurPos()); |
| 402 |
Emit(RegexCode.Getmark); |
| 403 |
Emit(RegexCode.Forejump); |
| 404 |
|
| 405 |
if (node._children.Count > 2) |
| 406 |
break; |
| 407 |
// else fallthrough |
| 408 |
goto case 2; |
| 409 |
case 2: |
| 410 |
PatchJump(PopInt(), CurPos()); |
| 411 |
break; |
| 412 |
} |
| 413 |
break; |
| 414 |
|
| 415 |
case RegexNode.Loop | BeforeChild: |
| 416 |
case RegexNode.Lazyloop | BeforeChild: |
| 417 |
|
| 418 |
if (node._n < Int32.MaxValue || node._m > 1) |
| 419 |
Emit(node._m == 0 ? RegexCode.Nullcount : RegexCode.Setcount, node._m == 0 ? 0 : 1 - node._m); |
| 420 |
else |
| 421 |
Emit(node._m == 0 ? RegexCode.Nullmark : RegexCode.Setmark); |
| 422 |
|
| 423 |
if (node._m == 0) { |
| 424 |
PushInt(CurPos()); |
| 425 |
Emit(RegexCode.Goto, 0); |
| 426 |
} |
| 427 |
PushInt(CurPos()); |
| 428 |
break; |
| 429 |
|
| 430 |
case RegexNode.Loop | AfterChild: |
| 431 |
case RegexNode.Lazyloop | AfterChild: { |
| 432 |
int StartJumpPos = CurPos(); |
| 433 |
int Lazy = (nodetype - (RegexNode.Loop | AfterChild)); |
| 434 |
|
| 435 |
if (node._n < Int32.MaxValue || node._m > 1) |
| 436 |
Emit(RegexCode.Branchcount + Lazy, PopInt(), node._n == Int32.MaxValue ? Int32.MaxValue : node._n - node._m); |
| 437 |
else |
| 438 |
Emit(RegexCode.Branchmark + Lazy, PopInt()); |
| 439 |
|
| 440 |
if (node._m == 0) |
| 441 |
PatchJump(PopInt(), StartJumpPos); |
| 442 |
} |
| 443 |
break; |
| 444 |
|
| 445 |
case RegexNode.Group | BeforeChild: |
| 446 |
case RegexNode.Group | AfterChild: |
| 447 |
break; |
| 448 |
|
| 449 |
case RegexNode.Capture | BeforeChild: |
| 450 |
Emit(RegexCode.Setmark); |
| 451 |
break; |
| 452 |
|
| 453 |
case RegexNode.Capture | AfterChild: |
| 454 |
Emit(RegexCode.Capturemark, MapCapnum(node._m), MapCapnum(node._n)); |
| 455 |
break; |
| 456 |
|
| 457 |
case RegexNode.Require | BeforeChild: |
| 458 |
// NOTE: the following line causes lookahead/lookbehind to be |
| 459 |
// NON-BACKTRACKING. It can be commented out with (*) |
| 460 |
Emit(RegexCode.Setjump); |
| 461 |
|
| 462 |
|
| 463 |
Emit(RegexCode.Setmark); |
| 464 |
break; |
| 465 |
|
| 466 |
case RegexNode.Require | AfterChild: |
| 467 |
Emit(RegexCode.Getmark); |
| 468 |
|
| 469 |
// NOTE: the following line causes lookahead/lookbehind to be |
| 470 |
// NON-BACKTRACKING. It can be commented out with (*) |
| 471 |
Emit(RegexCode.Forejump); |
| 472 |
|
| 473 |
break; |
| 474 |
|
| 475 |
case RegexNode.Prevent | BeforeChild: |
| 476 |
Emit(RegexCode.Setjump); |
| 477 |
PushInt(CurPos()); |
| 478 |
Emit(RegexCode.Lazybranch, 0); |
| 479 |
break; |
| 480 |
|
| 481 |
case RegexNode.Prevent | AfterChild: |
| 482 |
Emit(RegexCode.Backjump); |
| 483 |
PatchJump(PopInt(), CurPos()); |
| 484 |
Emit(RegexCode.Forejump); |
| 485 |
break; |
| 486 |
|
| 487 |
case RegexNode.Greedy | BeforeChild: |
| 488 |
Emit(RegexCode.Setjump); |
| 489 |
break; |
| 490 |
|
| 491 |
case RegexNode.Greedy | AfterChild: |
| 492 |
Emit(RegexCode.Forejump); |
| 493 |
break; |
| 494 |
|
| 495 |
case RegexNode.One: |
| 496 |
case RegexNode.Notone: |
| 497 |
Emit(node._type | bits, (int)node._ch); |
| 498 |
break; |
| 499 |
|
| 500 |
case RegexNode.Notoneloop: |
| 501 |
case RegexNode.Notonelazy: |
| 502 |
case RegexNode.Oneloop: |
| 503 |
case RegexNode.Onelazy: |
| 504 |
if (node._m > 0) |
| 505 |
Emit(((node._type == RegexNode.Oneloop || node._type == RegexNode.Onelazy) ? |
| 506 |
RegexCode.Onerep : RegexCode.Notonerep) | bits, (int)node._ch, node._m); |
| 507 |
if (node._n > node._m) |
| 508 |
Emit(node._type | bits, (int)node._ch, node._n == Int32.MaxValue ? |
| 509 |
Int32.MaxValue : node._n - node._m); |
| 510 |
break; |
| 511 |
|
| 512 |
case RegexNode.Setloop: |
| 513 |
case RegexNode.Setlazy: |
| 514 |
if (node._m > 0) |
| 515 |
Emit(RegexCode.Setrep | bits, StringCode(node._str), node._m); |
| 516 |
if (node._n > node._m) |
| 517 |
Emit(node._type | bits, StringCode(node._str), |
| 518 |
(node._n == Int32.MaxValue) ? Int32.MaxValue : node._n - node._m); |
| 519 |
break; |
| 520 |
|
| 521 |
case RegexNode.Multi: |
| 522 |
Emit(node._type | bits, StringCode(node._str)); |
| 523 |
break; |
| 524 |
|
| 525 |
case RegexNode.Set: |
| 526 |
Emit(node._type | bits, StringCode(node._str)); |
| 527 |
break; |
| 528 |
|
| 529 |
case RegexNode.Ref: |
| 530 |
Emit(node._type | bits, MapCapnum(node._m)); |
| 531 |
break; |
| 532 |
|
| 533 |
case RegexNode.Nothing: |
| 534 |
case RegexNode.Bol: |
| 535 |
case RegexNode.Eol: |
| 536 |
case RegexNode.Boundary: |
| 537 |
case RegexNode.Nonboundary: |
| 538 |
case RegexNode.ECMABoundary: |
| 539 |
case RegexNode.NonECMABoundary: |
| 540 |
case RegexNode.Beginning: |
| 541 |
case RegexNode.Start: |
| 542 |
case RegexNode.EndZ: |
| 543 |
case RegexNode.End: |
| 544 |
Emit(node._type); |
| 545 |
break; |
| 546 |
|
| 547 |
default: |
| 548 |
throw MakeException(SR.GetString(SR.UnexpectedOpcode, nodetype.ToString(CultureInfo.CurrentCulture))); |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |