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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
|
//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: parser.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.Collections;
using System.Globalization;
namespace System.Text.RegularExpressions.Syntax {
class Parser {
public static int ParseDecimal (string str, ref int ptr) {
return ParseNumber (str, ref ptr, 10, 1, Int32.MaxValue);
}
public static int ParseOctal (string str, ref int ptr) {
return ParseNumber (str, ref ptr, 8, 1, 3);
}
public static int ParseHex (string str, ref int ptr, int digits) {
return ParseNumber (str, ref ptr, 16, digits, digits);
}
public static int ParseNumber (string str, ref int ptr, int b, int min, int max) {
int p = ptr, n = 0, digits = 0, d;
if (max < min)
max = Int32.MaxValue;
while (digits < max && p < str.Length) {
d = ParseDigit (str[p ++], b, digits);
if (d < 0) {
-- p;
break;
}
n = n * b + d;
++ digits;
}
if (digits < min)
return -1;
ptr = p;
return n;
}
public static string ParseName (string str, ref int ptr) {
if (Char.IsDigit (str[ptr])) {
int gid = ParseNumber (str, ref ptr, 10, 1, 0);
if (gid > 0)
return gid.ToString ();
return null;
}
int start = ptr;
for (;;) {
if (!IsNameChar (str[ptr]))
break;
++ ptr;
}
if (ptr - start > 0)
return str.Substring (start, ptr - start);
return null;
}
public static string Escape (string str) {
string result = "";
for (int i = 0; i < str.Length; ++ i) {
char c = str[i];
switch (c) {
case '\\': case '*': case '+': case '?': case '|':
case '{': case '[': case '(': case ')': case '^':
case '$': case '.': case '#': case ' ':
result += "\\" + c;
break;
case '\t': result += "\\t"; break;
case '\n': result += "\\n"; break;
case '\r': result += "\\r"; break;
case '\f': result += "\\f"; break;
default: result += c; break;
}
}
return result;
}
public static string Unescape (string str) {
if (str.IndexOf ('\\') == -1)
return str;
return new Parser ().ParseString (str);
}
// public instance
public Parser () {
this.caps = new ArrayList ();
this.refs = new Hashtable ();
}
public RegularExpression ParseRegularExpression (string pattern, RegexOptions options) {
this.pattern = pattern;
this.ptr = 0;
caps.Clear ();
refs.Clear ();
this.num_groups = 0;
try {
RegularExpression re = new RegularExpression ();
ParseGroup (re, options, null);
ResolveReferences ();
re.GroupCount = num_groups;
return re;
}
catch (IndexOutOfRangeException) {
throw NewParseException ("Unexpected end of pattern.");
}
}
public int GetMapping (Hashtable mapping)
{
int end = caps.Count;
mapping.Add ("0", 0);
for (int i = 0; i < end; i++) {
CapturingGroup group = (CapturingGroup) caps [i];
string name = group.Name != null ? group.Name : group.Index.ToString ();
if (mapping.Contains (name)) {
if ((int) mapping [name] != group.Index)
throw new SystemException ("invalid state");
continue;
}
mapping.Add (name, group.Index);
}
return gap;
}
// private methods
private void ParseGroup (Group group, RegexOptions options, Assertion assertion) {
bool is_top_level = group is RegularExpression;
Alternation alternation = null;
string literal = null;
Group current = new Group ();
Expression expr = null;
bool closed = false;
while (true) {
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
if (ptr >= pattern.Length)
break;
// (1) Parse for Expressions
char ch = pattern[ptr ++];
switch (ch) {
case '^': {
Position pos =
IsMultiline (options) ? Position.StartOfLine : Position.Start;
expr = new PositionAssertion (pos);
break;
}
case '$': {
Position pos =
IsMultiline (options) ? Position.EndOfLine : Position.End;
expr = new PositionAssertion (pos);
break;
}
case '.': {
Category cat =
IsSingleline (options) ? Category.AnySingleline : Category.Any;
expr = new CharacterClass (cat, false);
break;
}
case '\\': {
int c = ParseEscape (false);
if (c >= 0)
ch = (char)c;
else {
expr = ParseSpecial (options);
if (expr == null)
ch = pattern[ptr ++]; // default escape
}
break;
}
case '[': {
expr = ParseCharacterClass (options);
break;
}
case '(': {
bool ignore = IsIgnoreCase (options);
expr = ParseGroupingConstruct (ref options);
if (expr == null) {
if (literal != null && IsIgnoreCase (options) != ignore) {
current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
literal = null;
}
continue;
}
break;
}
case ')': {
closed = true;
goto EndOfGroup;
}
case '|': {
if (literal != null) {
current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
literal = null;
}
if (assertion != null) {
if (assertion.TrueExpression == null)
assertion.TrueExpression = current;
else if (assertion.FalseExpression == null)
assertion.FalseExpression = current;
else
throw NewParseException ("Too many | in (?()|).");
}
else {
if (alternation == null)
alternation = new Alternation ();
alternation.AddAlternative (current);
}
current = new Group ();
continue;
}
case '*': case '+': case '?': {
throw NewParseException ("Bad quantifier.");
}
default:
break; // literal character
}
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
// (2) Check for Repetitions
if (ptr < pattern.Length) {
char k = pattern[ptr];
int min = 0, max = 0;
bool lazy = false;
bool haveRep = false;
if (k == '?' || k == '*' || k == '+') {
++ ptr;
haveRep = true;
switch (k) {
case '?': min = 0; max = 1; break;
case '*': min = 0; max = 0x7fffffff; break;
case '+': min = 1; max = 0x7fffffff; break;
}
} else if (k == '{' && ptr + 1 < pattern.Length) {
int saved_ptr = ptr;
++ptr;
haveRep = ParseRepetitionBounds (out min, out max, options);
if (!haveRep)
ptr = saved_ptr;
}
if (haveRep) {
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
if (ptr < pattern.Length && pattern[ptr] == '?') {
++ ptr;
lazy = true;
}
//It doesn't make sense to assert a given position more than once.
bool ignore_repetition = false;
if (expr is PositionAssertion) {
ignore_repetition = min > 0 && !lazy;
max = 1;
}
if (!ignore_repetition) {
Repetition repetition = new Repetition (min, max, lazy);
if (expr == null)
repetition.Expression = new Literal (ch.ToString (), IsIgnoreCase (options));
else
repetition.Expression = expr;
expr = repetition;
}
}
}
// (3) Append Expression and/or Literal
if (expr == null) {
if (literal == null)
literal = "";
literal += ch;
}
else {
if (literal != null) {
current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
literal = null;
}
current.AppendExpression (expr);
expr = null;
}
if (is_top_level && ptr >= pattern.Length)
goto EndOfGroup;
}
EndOfGroup:
if (is_top_level && closed)
throw NewParseException ("Too many )'s.");
if (!is_top_level && !closed)
throw NewParseException ("Not enough )'s.");
// clean up literals and alternations
if (literal != null)
current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
if (assertion != null) {
if (assertion.TrueExpression == null)
assertion.TrueExpression = current;
else
assertion.FalseExpression = current;
group.AppendExpression (assertion);
}
else if (alternation != null) {
alternation.AddAlternative (current);
group.AppendExpression (alternation);
}
else
group.AppendExpression (current);
}
private Expression ParseGroupingConstruct (ref RegexOptions options) {
if (pattern[ptr] != '?') {
Group group;
if (IsExplicitCapture (options))
group = new Group ();
else {
group = new CapturingGroup ();
caps.Add (group);
}
ParseGroup (group, options, null);
return group;
}
else
++ ptr;
switch (pattern[ptr]) {
case ':': { // non-capturing group
++ ptr;
Group group = new Group ();
ParseGroup (group, options, null);
return group;
}
case '>': { // non-backtracking group
++ ptr;
Group group = new NonBacktrackingGroup ();
ParseGroup (group, options, null);
return group;
}
case 'i': case 'm': case 'n':
case 's': case 'x': case '-': { // options
RegexOptions o = options;
ParseOptions (ref o, false);
if (pattern[ptr] == '-') {
++ ptr;
ParseOptions (ref o, true);
}
if (pattern[ptr] == ':') { // pass options to child group
++ ptr;
Group group = new Group ();
ParseGroup (group, o, null);
return group;
}
else if (pattern[ptr] == ')') { // change options of enclosing group
++ ptr;
options = o;
return null;
}
else
throw NewParseException ("Bad options");
}
case '<': case '=': case '!': { // lookahead/lookbehind
ExpressionAssertion asn = new ExpressionAssertion ();
if (!ParseAssertionType (asn))
goto case '\''; // it's a (?<name> ) construct
Group test = new Group ();
ParseGroup (test, options, null);
asn.TestExpression = test;
return asn;
}
case '\'': { // named/balancing group
char delim;
if (pattern[ptr] == '<')
delim = '>';
else
delim = '\'';
++ ptr;
string name = ParseName ();
if (pattern[ptr] == delim) {
// capturing group
if (name == null)
throw NewParseException ("Bad group name.");
++ ptr;
CapturingGroup cap = new CapturingGroup ();
cap.Name = name;
caps.Add (cap);
ParseGroup (cap, options, null);
return cap;
}
else if (pattern[ptr] == '-') {
// balancing group
++ ptr;
string balance_name = ParseName ();
if (balance_name == null || pattern[ptr] != delim)
throw NewParseException ("Bad balancing group name.");
++ ptr;
BalancingGroup bal = new BalancingGroup ();
bal.Name = name;
if(bal.IsNamed) {
caps.Add (bal);
}
refs.Add (bal, balance_name);
ParseGroup (bal, options, null);
return bal;
}
else
throw NewParseException ("Bad group name.");
}
case '(': { // expression/capture test
Assertion asn;
++ ptr;
int p = ptr;
string name = ParseName ();
if (name == null || pattern[ptr] != ')') { // expression test
// FIXME MS implementation doesn't seem to
// implement this version of (?(x) ...)
ptr = p;
ExpressionAssertion expr_asn = new ExpressionAssertion ();
if (pattern[ptr] == '?') {
++ ptr;
if (!ParseAssertionType (expr_asn))
throw NewParseException ("Bad conditional.");
}
else {
expr_asn.Negate = false;
expr_asn.Reverse = false;
}
Group test = new Group ();
ParseGroup (test, options, null);
expr_asn.TestExpression = test;
asn = expr_asn;
}
else { // capture test
++ ptr;
asn = new CaptureAssertion (new Literal (name, IsIgnoreCase (options)));
refs.Add (asn, name);
}
Group group = new Group ();
ParseGroup (group, options, asn);
return group;
}
case '#': { // comment
++ ptr;
while (pattern[ptr ++] != ')') {
if (ptr >= pattern.Length)
throw NewParseException ("Unterminated (?#...) comment.");
}
return null;
}
default: // error
throw NewParseException ("Bad grouping construct.");
}
}
private bool ParseAssertionType (ExpressionAssertion assertion) {
if (pattern[ptr] == '<') {
switch (pattern[ptr + 1]) {
case '=':
assertion.Negate = false;
break;
case '!':
assertion.Negate = true;
break;
default:
return false;
}
assertion.Reverse = true;
ptr += 2;
}
else {
switch (pattern[ptr]) {
case '=':
assertion.Negate = false;
break;
case '!':
assertion.Negate = true;
break;
default:
return false;
}
assertion.Reverse = false;
ptr += 1;
}
return true;
}
private void ParseOptions (ref RegexOptions options, bool negate) {
for (;;) {
switch (pattern[ptr]) {
case 'i':
if (negate)
options &= ~RegexOptions.IgnoreCase;
else
options |= RegexOptions.IgnoreCase;
break;
case 'm':
if (negate)
options &= ~RegexOptions.Multiline;
else
options |= RegexOptions.Multiline;
break;
case 'n':
if (negate)
options &= ~RegexOptions.ExplicitCapture;
else
options |= RegexOptions.ExplicitCapture;
break;
case 's':
if (negate)
options &= ~RegexOptions.Singleline;
else
options |= RegexOptions.Singleline;
break;
case 'x':
if (negate)
options &= ~RegexOptions.IgnorePatternWhitespace;
else
options |= RegexOptions.IgnorePatternWhitespace;
break;
default:
return;
}
++ ptr;
}
}
private Expression ParseCharacterClass (RegexOptions options) {
bool negate = false;
if (pattern[ptr] == '^') {
negate = true;
++ ptr;
}
bool ecma = IsECMAScript (options);
CharacterClass cls = new CharacterClass (negate, IsIgnoreCase (options));
if (pattern[ptr] == ']') {
cls.AddCharacter (']');
++ ptr;
}
int c = -1;
int last = -1;
bool range = false;
bool closed = false;
while (ptr < pattern.Length) {
c = pattern[ptr ++];
if (c == ']') {
closed = true;
break;
}
if (c == '-' && last >= 0 && !range) {
range = true;
continue;
}
if (c == '\\') {
c = ParseEscape (true);
if (c >= 0)
goto char_recognized;
// didn't recognize escape
c = pattern [ptr ++];
switch (c) {
case 'b':
c = '\b';
goto char_recognized;
case 'd': case 'D':
cls.AddCategory (ecma ? Category.EcmaDigit : Category.Digit, c == 'D');
break;
case 'w': case 'W':
cls.AddCategory (ecma ? Category.EcmaWord : Category.Word, c == 'W');
break;
case 's': case 'S':
cls.AddCategory (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, c == 'S');
break;
case 'p': case 'P':
cls.AddCategory (ParseUnicodeCategory (), c == 'P'); // ignore ecma
break;
default: // add escaped character
goto char_recognized;
}
// if the pattern looks like [a-\s] ...
if (range)
throw NewParseException ("character range cannot have category \\" + c);
last = -1;
continue;
}
char_recognized:
if (range) {
// if 'range' is true, we know that 'last >= 0'
if (c < last)
throw NewParseException ("[" + last + "-" + c + "] range in reverse order.");
cls.AddRange ((char)last, (char)c);
last = -1;
range = false;
continue;
}
cls.AddCharacter ((char)c);
last = c;
}
if (!closed)
throw NewParseException ("Unterminated [] set.");
if (range)
cls.AddCharacter ('-');
return cls;
}
private bool ParseRepetitionBounds (out int min, out int max, RegexOptions options) {
int n, m;
min = max = 0;
/* check syntax */
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
if (pattern[ptr] == ',') {
n = -1;
} else {
n = ParseNumber (10, 1, 0);
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
}
switch (pattern[ptr ++]) {
case '}':
m = n;
break;
case ',':
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
m = ParseNumber (10, 1, 0);
ConsumeWhitespace (IsIgnorePatternWhitespace (options));
if (pattern[ptr ++] != '}')
return false;
break;
default:
return false;
}
/* check bounds and ordering */
if (n > 0x7fffffff || m > 0x7fffffff)
throw NewParseException ("Illegal {x, y} - maximum of 2147483647.");
if (m >= 0 && m < n)
throw NewParseException ("Illegal {x, y} with x > y.");
/* assign min and max */
min = n;
if (m > 0)
max = m;
else
max = 0x7fffffff;
return true;
}
private Category ParseUnicodeCategory () {
if (pattern[ptr ++] != '{')
throw NewParseException ("Incomplete \\p{X} character escape.");
string name = ParseName (pattern, ref ptr);
if (name == null)
throw NewParseException ("Incomplete \\p{X} character escape.");
Category cat = CategoryUtils.CategoryFromName (name);
if (cat == Category.None)
throw NewParseException ("Unknown property '" + name + "'.");
if (pattern[ptr ++] != '}')
throw NewParseException ("Incomplete \\p{X} character escape.");
return cat;
}
private Expression ParseSpecial (RegexOptions options) {
int p = ptr;
bool ecma = IsECMAScript (options);
Expression expr = null;
switch (pattern[ptr ++]) {
// categories
case 'd':
expr = new CharacterClass (ecma ? Category.EcmaDigit : Category.Digit, false);
break;
case 'w':
expr = new CharacterClass (ecma ? Category.EcmaWord : Category.Word, false);
break;
case 's':
expr = new CharacterClass (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, false);
break;
case 'p':
// this is odd - ECMAScript isn't supposed to support Unicode,
// yet \p{..} compiles and runs under the MS implementation
// identically to canonical mode. That's why I'm ignoring the
// value of ecma here.
expr = new CharacterClass (ParseUnicodeCategory (), false);
break;
case 'D':
expr = new CharacterClass (ecma ? Category.EcmaDigit : Category.Digit, true);
break;
case 'W':
expr = new CharacterClass (ecma ? Category.EcmaWord : Category.Word, true);
break;
case 'S':
expr = new CharacterClass (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, true);
break;
case 'P':
expr = new CharacterClass (ParseUnicodeCategory (), true);
break;
// positions
case 'A': expr = new PositionAssertion (Position.StartOfString); break;
case 'Z': expr = new PositionAssertion (Position.End); break;
case 'z': expr = new PositionAssertion (Position.EndOfString); break;
case 'G': expr = new PositionAssertion (Position.StartOfScan); break;
case 'b': expr = new PositionAssertion (Position.Boundary); break;
case 'B': expr = new PositionAssertion (Position.NonBoundary); break;
// references
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': {
ptr --;
int n = ParseNumber (10, 1, 0);
if (n < 0) {
ptr = p;
return null;
}
// FIXME test if number is within number of assigned groups
// this may present a problem for right-to-left matching
Reference reference = new BackslashNumber (IsIgnoreCase (options), ecma);
refs.Add (reference, n.ToString ());
expr = reference;
break;
}
case 'k': {
char delim = pattern[ptr ++];
if (delim == '<')
delim = '>';
else if (delim != '\'')
throw NewParseException ("Malformed \\k<...> named backreference.");
string name = ParseName ();
if (name == null || pattern[ptr] != delim)
throw NewParseException ("Malformed \\k<...> named backreference.");
++ ptr;
Reference reference = new Reference (IsIgnoreCase (options));
refs.Add (reference, name);
expr = reference;
break;
}
default:
expr = null;
break;
}
if (expr == null)
ptr = p;
return expr;
}
private int ParseEscape (bool inCharacterClass) {
int p = ptr;
int c;
if (p >= pattern.Length)
throw new ArgumentException (
String.Format ("Parsing \"{0}\" - Illegal \\ at end of " +
"pattern.", pattern), pattern);
switch (pattern[ptr ++]) {
// standard escapes (except \b)
case 'a': return '\u0007';
case 't': return '\u0009';
case 'r': return '\u000d';
case 'v': return '\u000b';
case 'f': return '\u000c';
case 'n': return '\u000a';
case 'e': return '\u001b';
case '\\': return '\\';
// character codes
case '0':
//
// Turns out that octal values can be specified
// without a leading zero. But also the limit
// of three character should include this first
// one.
//
ptr--;
int prevptr = ptr;
int result = ParseOctal (pattern, ref ptr);
if (result == -1 && prevptr == ptr)
return 0;
return result;
case '1': case '2': case '3': case '4': case '5':
case '6': case '7':
if (inCharacterClass){
ptr--;
return ParseOctal (pattern, ref ptr);
}
break;
case 'x':
c = ParseHex (pattern, ref ptr, 2);
if (c < 0)
throw NewParseException ("Insufficient hex digits");
return c;
case 'u':
c = ParseHex (pattern, ref ptr, 4);
if (c < 0)
throw NewParseException ("Insufficient hex digits");
return c;
// control characters
case 'c':
c = pattern[ptr ++];
if (c >= '@' && c <= '_')
return c - '@';
else
throw NewParseException ("Unrecognized control character.");
}
// unknown escape
ptr = p;
return -1;
}
private string ParseName () {
return Parser.ParseName (pattern, ref ptr);
}
private static bool IsNameChar (char c) {
UnicodeCategory cat = Char.GetUnicodeCategory (c);
if (cat == UnicodeCategory.ModifierLetter)
return false;
if (cat == UnicodeCategory.ConnectorPunctuation)
return true;
return Char.IsLetterOrDigit (c);
}
private int ParseNumber (int b, int min, int max) {
return Parser.ParseNumber (pattern, ref ptr, b, min, max);
}
private static int ParseDigit (char c, int b, int n) {
switch (b) {
case 8:
if (c >= '0' && c <= '7')
return c - '0';
else
return -1;
case 10:
if (c >= '0' && c <= '9')
return c - '0';
else
return -1;
case 16:
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
else if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
else
return -1;
default:
return -1;
}
}
private void ConsumeWhitespace (bool ignore) {
while (ptr < pattern.Length) {
if (pattern[ptr] == '(') {
if (ptr + 3 >= pattern.Length)
return;
if (pattern[ptr + 1] != '?' || pattern[ptr + 2] != '#')
return;
ptr += 3;
while (ptr < pattern.Length && pattern[ptr ++] != ')')
/* ignore */ ;
}
else if (ignore && pattern[ptr] == '#') {
while (ptr < pattern.Length && pattern[ptr ++] != '\n')
/* ignore */ ;
}
else if (ignore && Char.IsWhiteSpace (pattern[ptr])) {
while (ptr < pattern.Length && Char.IsWhiteSpace (pattern[ptr]))
++ ptr;
}
else
return;
}
}
private string ParseString (string pattern) {
this.pattern = pattern;
this.ptr = 0;
StringBuilder result = new StringBuilder (pattern.Length);
while (ptr < pattern.Length) {
int c = pattern[ptr ++];
if (c == '\\') {
c = ParseEscape (false);
if(c < 0) {
c = pattern[ptr ++];
if(c == 'b')
c = '\b';
}
}
result.Append ((char) c);
}
return result.ToString ();
}
private void ResolveReferences ()
{
int gid = 1;
Hashtable dict = new Hashtable ();
ArrayList explicit_numeric_groups = null;
// number unnamed groups
foreach (CapturingGroup group in caps) {
if (group.Name != null)
continue;
dict.Add (gid.ToString (), group);
group.Index = gid ++;
++ num_groups;
}
// number named groups
foreach (CapturingGroup group in caps) {
if (group.Name == null)
continue;
if (dict.Contains (group.Name)) {
CapturingGroup prev = (CapturingGroup) dict [group.Name];
group.Index = prev.Index;
if (group.Index == gid)
gid ++;
else if (group.Index > gid)
explicit_numeric_groups.Add (group);
continue;
}
if (Char.IsDigit (group.Name [0])) {
int ptr = 0;
int group_gid = ParseDecimal (group.Name, ref ptr);
if (ptr == group.Name.Length) {
group.Index = group_gid;
dict.Add (group.Name, group);
++ num_groups;
if (group_gid == gid) {
gid ++;
} else {
// all numbers before 'gid' are already in the dictionary. So, we know group_gid > gid
if (explicit_numeric_groups == null)
explicit_numeric_groups = new ArrayList (4);
explicit_numeric_groups.Add (group);
}
continue;
}
}
string gid_s = gid.ToString ();
while (dict.Contains (gid_s))
gid_s = (++gid).ToString ();
dict.Add (gid_s, group);
dict.Add (group.Name, group);
group.Index = gid ++;
++ num_groups;
}
gap = gid; // == 1 + num_groups, if explicit_numeric_groups == null
if (explicit_numeric_groups != null)
HandleExplicitNumericGroups (explicit_numeric_groups);
// resolve references
foreach (Expression expr in refs.Keys) {
string name = (string) refs [expr];
if (!dict.Contains (name)) {
if (expr is CaptureAssertion && !Char.IsDigit (name [0]))
continue;
BackslashNumber bn = expr as BackslashNumber;
if (bn != null && bn.ResolveReference (name, dict))
continue;
throw NewParseException ("Reference to undefined group " +
(Char.IsDigit (name[0]) ? "number " : "name ") +
name);
}
CapturingGroup group = (CapturingGroup)dict[name];
if (expr is Reference)
((Reference)expr).CapturingGroup = group;
else if (expr is CaptureAssertion)
((CaptureAssertion)expr).CapturingGroup = group;
else if (expr is BalancingGroup)
((BalancingGroup)expr).Balance = group;
}
}
private void HandleExplicitNumericGroups (ArrayList explicit_numeric_groups)
{
int gid = gap;
int i = 0;
int n_explicit = explicit_numeric_groups.Count;
explicit_numeric_groups.Sort ();
// move 'gap' forward to skip over all explicit groups that
// turn out to match their index
for (; i < n_explicit; ++i) {
CapturingGroup g = (CapturingGroup) explicit_numeric_groups [i];
if (g.Index > gid)
break;
if (g.Index == gid)
gid ++;
}
gap = gid;
// re-index all further groups so that the indexes are contiguous
int prev = gid;
for (; i < n_explicit; ++i) {
CapturingGroup g = (CapturingGroup) explicit_numeric_groups [i];
if (g.Index == prev) {
g.Index = gid - 1;
} else {
prev = g.Index;
g.Index = gid ++;
}
}
}
// flag helper functions
private static bool IsIgnoreCase (RegexOptions options) {
return (options & RegexOptions.IgnoreCase) != 0;
}
private static bool IsMultiline (RegexOptions options) {
return (options & RegexOptions.Multiline) != 0;
}
private static bool IsExplicitCapture (RegexOptions options) {
return (options & RegexOptions.ExplicitCapture) != 0;
}
private static bool IsSingleline (RegexOptions options) {
return (options & RegexOptions.Singleline) != 0;
}
private static bool IsIgnorePatternWhitespace (RegexOptions options) {
return (options & RegexOptions.IgnorePatternWhitespace) != 0;
}
private static bool IsECMAScript (RegexOptions options) {
return (options & RegexOptions.ECMAScript) != 0;
}
// exception creation
private ArgumentException NewParseException (string msg) {
msg = "parsing \"" + pattern + "\" - " + msg;
return new ArgumentException (msg, pattern);
}
private string pattern;
private int ptr;
private ArrayList caps;
private Hashtable refs;
private int num_groups;
private int gap;
}
}
|