parser.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: parser.cs
  5. //
  6. // author: Dan Lewis ([email protected])
  7. // (c) 2002
  8. using System;
  9. using System.Collections;
  10. using System.Globalization;
  11. namespace System.Text.RegularExpressions.Syntax {
  12. class Parser {
  13. public static int ParseDecimal (string str, ref int ptr) {
  14. return ParseNumber (str, ref ptr, 10, 1, Int32.MaxValue);
  15. }
  16. public static int ParseOctal (string str, ref int ptr) {
  17. return ParseNumber (str, ref ptr, 8, 1, 3);
  18. }
  19. public static int ParseHex (string str, ref int ptr, int digits) {
  20. return ParseNumber (str, ref ptr, 16, digits, digits);
  21. }
  22. public static int ParseNumber (string str, ref int ptr, int b, int min, int max) {
  23. int p = ptr, n = 0, digits = 0, d;
  24. if (max < min)
  25. max = Int32.MaxValue;
  26. while (digits < max && p < str.Length) {
  27. d = ParseDigit (str[p ++], b, digits);
  28. if (d < 0) {
  29. -- p;
  30. break;
  31. }
  32. n = n * b + d;
  33. ++ digits;
  34. }
  35. if (digits < min)
  36. return -1;
  37. ptr = p;
  38. return n;
  39. }
  40. public static string ParseName (string str, ref int ptr) {
  41. if (Char.IsDigit (str[ptr])) {
  42. int gid = ParseNumber (str, ref ptr, 10, 1, 0);
  43. if (gid > 0)
  44. return gid.ToString ();
  45. return null;
  46. }
  47. int start = ptr;
  48. for (;;) {
  49. if (!IsNameChar (str[ptr]))
  50. break;
  51. ++ ptr;
  52. }
  53. if (ptr - start > 0)
  54. return str.Substring (start, ptr - start);
  55. return null;
  56. }
  57. public static string Escape (string str) {
  58. string result = "";
  59. for (int i = 0; i < str.Length; ++ i) {
  60. char c = str[i];
  61. switch (c) {
  62. case '\\': case '*': case '+': case '?': case '|':
  63. case '{': case '[': case '(': case ')': case '^':
  64. case '$': case '.': case '#': case ' ':
  65. result += "\\" + c;
  66. break;
  67. case '\t': result += "\\t"; break;
  68. case '\n': result += "\\n"; break;
  69. case '\r': result += "\\r"; break;
  70. case '\f': result += "\\f"; break;
  71. default: result += c; break;
  72. }
  73. }
  74. return result;
  75. }
  76. public static string Unescape (string str) {
  77. return new Parser ().ParseString (str);
  78. }
  79. // public instance
  80. public Parser () {
  81. this.caps = new ArrayList ();
  82. this.refs = new Hashtable ();
  83. }
  84. public RegularExpression ParseRegularExpression (string pattern, RegexOptions options) {
  85. this.pattern = pattern;
  86. this.ptr = 0;
  87. caps.Clear ();
  88. refs.Clear ();
  89. this.num_groups = 0;
  90. try {
  91. RegularExpression re = new RegularExpression ();
  92. ParseGroup (re, options, null);
  93. ResolveReferences ();
  94. re.GroupCount = num_groups;
  95. return re;
  96. }
  97. catch (IndexOutOfRangeException) {
  98. throw NewParseException ("Unexpected end of pattern.");
  99. }
  100. }
  101. public IDictionary GetMapping () {
  102. Hashtable mapping = new Hashtable ();
  103. Hashtable numbers = new Hashtable ();
  104. int end = caps.Count;
  105. mapping.Add ("0", 0);
  106. for (int i = 0; i < end; i++) {
  107. CapturingGroup group = (CapturingGroup) caps [i];
  108. if (group.Name != null && !mapping.Contains (group.Name)) {
  109. mapping.Add (group.Name, group.Number);
  110. numbers.Add (group.Number, group.Number);
  111. }
  112. }
  113. for (int i = 1; i < end; i++) {
  114. if (numbers [i] == null)
  115. mapping.Add (i.ToString (), i);
  116. }
  117. return mapping;
  118. }
  119. // private methods
  120. private void ParseGroup (Group group, RegexOptions options, Assertion assertion) {
  121. bool is_top_level = group is RegularExpression;
  122. Alternation alternation = null;
  123. string literal = null;
  124. Group current = new Group ();
  125. Expression expr = null;
  126. bool closed = false;
  127. while (true) {
  128. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  129. if (ptr >= pattern.Length)
  130. break;
  131. // (1) Parse for Expressions
  132. char ch = pattern[ptr ++];
  133. switch (ch) {
  134. case '^': {
  135. Position pos =
  136. IsMultiline (options) ? Position.StartOfLine : Position.Start;
  137. expr = new PositionAssertion (pos);
  138. break;
  139. }
  140. case '$': {
  141. Position pos =
  142. IsMultiline (options) ? Position.EndOfLine : Position.End;
  143. expr = new PositionAssertion (pos);
  144. break;
  145. }
  146. case '.': {
  147. Category cat =
  148. IsSingleline (options) ? Category.AnySingleline : Category.Any;
  149. expr = new CharacterClass (cat, false);
  150. break;
  151. }
  152. case '\\': {
  153. int c = ParseEscape ();
  154. if (c >= 0)
  155. ch = (char)c;
  156. else {
  157. expr = ParseSpecial (options);
  158. if (expr == null)
  159. ch = pattern[ptr ++]; // default escape
  160. }
  161. break;
  162. }
  163. case '[': {
  164. expr = ParseCharacterClass (options);
  165. break;
  166. }
  167. case '(': {
  168. bool ignore = IsIgnoreCase (options);
  169. expr = ParseGroupingConstruct (ref options);
  170. if (expr == null) {
  171. if (literal != null && IsIgnoreCase (options) != ignore) {
  172. current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
  173. literal = null;
  174. }
  175. continue;
  176. }
  177. break;
  178. }
  179. case ')': {
  180. closed = true;
  181. goto EndOfGroup;
  182. }
  183. case '|': {
  184. if (literal != null) {
  185. current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
  186. literal = null;
  187. }
  188. if (assertion != null) {
  189. if (assertion.TrueExpression == null)
  190. assertion.TrueExpression = current;
  191. else if (assertion.FalseExpression == null)
  192. assertion.FalseExpression = current;
  193. else
  194. throw NewParseException ("Too many | in (?()|).");
  195. }
  196. else {
  197. if (alternation == null)
  198. alternation = new Alternation ();
  199. alternation.AddAlternative (current);
  200. }
  201. current = new Group ();
  202. continue;
  203. }
  204. case '*': case '+': case '?': {
  205. throw NewParseException ("Bad quantifier.");
  206. }
  207. default:
  208. break; // literal character
  209. }
  210. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  211. // (2) Check for Repetitions
  212. if (ptr < pattern.Length) {
  213. char k = pattern[ptr];
  214. if (k == '?' || k == '*' || k == '+' || k == '{') {
  215. ++ ptr;
  216. int min = 0, max = 0;
  217. bool lazy = false;
  218. switch (k) {
  219. case '?': min = 0; max = 1; break;
  220. case '*': min = 0; max = 0xffff; break;
  221. case '+': min = 1; max = 0xffff; break;
  222. case '{': ParseRepetitionBounds (out min, out max, options); break;
  223. }
  224. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  225. if (ptr < pattern.Length && pattern[ptr] == '?') {
  226. ++ ptr;
  227. lazy = true;
  228. }
  229. Repetition repetition = new Repetition (min, max, lazy);
  230. if (expr == null)
  231. repetition.Expression = new Literal (ch.ToString (), IsIgnoreCase (options));
  232. else
  233. repetition.Expression = expr;
  234. expr = repetition;
  235. }
  236. }
  237. // (3) Append Expression and/or Literal
  238. if (expr == null) {
  239. if (literal == null)
  240. literal = "";
  241. literal += ch;
  242. }
  243. else {
  244. if (literal != null) {
  245. current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
  246. literal = null;
  247. }
  248. current.AppendExpression (expr);
  249. expr = null;
  250. }
  251. if (is_top_level && ptr >= pattern.Length)
  252. goto EndOfGroup;
  253. }
  254. EndOfGroup:
  255. if (is_top_level && closed)
  256. throw NewParseException ("Too many )'s.");
  257. if (!is_top_level && !closed)
  258. throw NewParseException ("Not enough )'s.");
  259. // clean up literals and alternations
  260. if (literal != null)
  261. current.AppendExpression (new Literal (literal, IsIgnoreCase (options)));
  262. if (assertion != null) {
  263. if (assertion.TrueExpression == null)
  264. assertion.TrueExpression = current;
  265. else
  266. assertion.FalseExpression = current;
  267. group.AppendExpression (assertion);
  268. }
  269. else if (alternation != null) {
  270. alternation.AddAlternative (current);
  271. group.AppendExpression (alternation);
  272. }
  273. else
  274. group.AppendExpression (current);
  275. }
  276. private Expression ParseGroupingConstruct (ref RegexOptions options) {
  277. if (pattern[ptr] != '?') {
  278. Group group;
  279. if (IsExplicitCapture (options))
  280. group = new Group ();
  281. else {
  282. group = new CapturingGroup ();
  283. caps.Add (group);
  284. }
  285. ParseGroup (group, options, null);
  286. return group;
  287. }
  288. else
  289. ++ ptr;
  290. switch (pattern[ptr]) {
  291. case ':': { // non-capturing group
  292. ++ ptr;
  293. Group group = new Group ();
  294. ParseGroup (group, options, null);
  295. return group;
  296. }
  297. case '>': { // non-backtracking group
  298. ++ ptr;
  299. Group group = new NonBacktrackingGroup ();
  300. ParseGroup (group, options, null);
  301. return group;
  302. }
  303. case 'i': case 'm': case 'n':
  304. case 's': case 'x': case '-': { // options
  305. RegexOptions o = options;
  306. ParseOptions (ref o, false);
  307. if (pattern[ptr] == '-') {
  308. ++ ptr;
  309. ParseOptions (ref o, true);
  310. }
  311. if (pattern[ptr] == ':') { // pass options to child group
  312. ++ ptr;
  313. Group group = new Group ();
  314. ParseGroup (group, o, null);
  315. return group;
  316. }
  317. else if (pattern[ptr] == ')') { // change options of enclosing group
  318. ++ ptr;
  319. options = o;
  320. return null;
  321. }
  322. else
  323. throw NewParseException ("Bad options");
  324. }
  325. case '<': case '=': case '!': { // lookahead/lookbehind
  326. ExpressionAssertion asn = new ExpressionAssertion ();
  327. if (!ParseAssertionType (asn))
  328. goto case '\''; // it's a (?<name> ) construct
  329. Group test = new Group ();
  330. ParseGroup (test, options, null);
  331. asn.TestExpression = test;
  332. return asn;
  333. }
  334. case '\'': { // named/balancing group
  335. char delim;
  336. if (pattern[ptr] == '<')
  337. delim = '>';
  338. else
  339. delim = '\'';
  340. ++ ptr;
  341. string name = ParseName ();
  342. if (pattern[ptr] == delim) {
  343. // capturing group
  344. if (name == null)
  345. throw NewParseException ("Bad group name.");
  346. ++ ptr;
  347. CapturingGroup cap = new CapturingGroup ();
  348. cap.Name = name;
  349. caps.Add (cap);
  350. ParseGroup (cap, options, null);
  351. return cap;
  352. }
  353. else if (pattern[ptr] == '-') {
  354. // balancing group
  355. ++ ptr;
  356. string balance_name = ParseName ();
  357. if (balance_name == null || pattern[ptr] != delim)
  358. throw NewParseException ("Bad balancing group name.");
  359. ++ ptr;
  360. BalancingGroup bal = new BalancingGroup ();
  361. bal.Name = name;
  362. if(bal.IsNamed) {
  363. caps.Add (bal);
  364. }
  365. refs.Add (bal, balance_name);
  366. ParseGroup (bal, options, null);
  367. return bal;
  368. }
  369. else
  370. throw NewParseException ("Bad group name.");
  371. }
  372. case '(': { // expression/capture test
  373. Assertion asn;
  374. ++ ptr;
  375. int p = ptr;
  376. string name = ParseName ();
  377. if (name == null || pattern[ptr] != ')') { // expression test
  378. // FIXME MS implementation doesn't seem to
  379. // implement this version of (?(x) ...)
  380. ptr = p;
  381. ExpressionAssertion expr_asn = new ExpressionAssertion ();
  382. if (pattern[ptr] == '?') {
  383. ++ ptr;
  384. if (!ParseAssertionType (expr_asn))
  385. throw NewParseException ("Bad conditional.");
  386. }
  387. else {
  388. expr_asn.Negate = false;
  389. expr_asn.Reverse = false;
  390. }
  391. Group test = new Group ();
  392. ParseGroup (test, options, null);
  393. expr_asn.TestExpression = test;
  394. asn = expr_asn;
  395. }
  396. else { // capture test
  397. ++ ptr;
  398. asn = new CaptureAssertion ();
  399. refs.Add (asn, name);
  400. }
  401. Group group = new Group ();
  402. ParseGroup (group, options, asn);
  403. return group;
  404. }
  405. case '#': { // comment
  406. ++ ptr;
  407. while (pattern[ptr ++] != ')') {
  408. if (ptr >= pattern.Length)
  409. throw NewParseException ("Unterminated (?#...) comment.");
  410. }
  411. return null;
  412. }
  413. default: // error
  414. throw NewParseException ("Bad grouping construct.");
  415. }
  416. }
  417. private bool ParseAssertionType (ExpressionAssertion assertion) {
  418. if (pattern[ptr] == '<') {
  419. switch (pattern[ptr + 1]) {
  420. case '=':
  421. assertion.Negate = false;
  422. break;
  423. case '!':
  424. assertion.Negate = true;
  425. break;
  426. default:
  427. return false;
  428. }
  429. assertion.Reverse = true;
  430. ptr += 2;
  431. }
  432. else {
  433. switch (pattern[ptr]) {
  434. case '=':
  435. assertion.Negate = false;
  436. break;
  437. case '!':
  438. assertion.Negate = true;
  439. break;
  440. default:
  441. return false;
  442. }
  443. assertion.Reverse = false;
  444. ptr += 1;
  445. }
  446. return true;
  447. }
  448. private void ParseOptions (ref RegexOptions options, bool negate) {
  449. for (;;) {
  450. switch (pattern[ptr]) {
  451. case 'i':
  452. if (negate)
  453. options &= ~RegexOptions.IgnoreCase;
  454. else
  455. options |= RegexOptions.IgnoreCase;
  456. break;
  457. case 'm':
  458. if (negate)
  459. options &= ~RegexOptions.Multiline;
  460. else
  461. options |= RegexOptions.Multiline;
  462. break;
  463. case 'n':
  464. if (negate)
  465. options &= ~RegexOptions.ExplicitCapture;
  466. else
  467. options |= RegexOptions.ExplicitCapture;
  468. break;
  469. case 's':
  470. if (negate)
  471. options &= ~RegexOptions.Singleline;
  472. else
  473. options |= RegexOptions.Singleline;
  474. break;
  475. case 'x':
  476. if (negate)
  477. options &= ~RegexOptions.IgnorePatternWhitespace;
  478. else
  479. options |= RegexOptions.IgnorePatternWhitespace;
  480. break;
  481. default:
  482. return;
  483. }
  484. ++ ptr;
  485. }
  486. }
  487. private Expression ParseCharacterClass (RegexOptions options) {
  488. bool negate, ecma;
  489. if (pattern[ptr] == '^') {
  490. negate = true;
  491. ++ ptr;
  492. }
  493. else
  494. negate = false;
  495. ecma = IsECMAScript (options);
  496. CharacterClass cls = new CharacterClass (negate, IsIgnoreCase (options));
  497. if (pattern[ptr] == ']') {
  498. cls.AddCharacter (']');
  499. ++ ptr;
  500. }
  501. int c = -1;
  502. int last = -1;
  503. bool range = false;
  504. bool closed = false;
  505. while (ptr < pattern.Length) {
  506. c = pattern[ptr ++];
  507. if (c == ']') {
  508. closed = true;
  509. break;
  510. }
  511. if (c == '-') {
  512. range = true;
  513. continue;
  514. }
  515. if (c == '\\') {
  516. c = ParseEscape ();
  517. if (c < 0) {
  518. // didn't recognize escape
  519. c = pattern[ptr ++];
  520. switch (c) {
  521. case 'b': c = '\b'; break;
  522. case 'd':
  523. cls.AddCategory (ecma ? Category.EcmaDigit : Category.Digit, false);
  524. last = -1;
  525. continue;
  526. case 'w':
  527. cls.AddCategory (ecma ? Category.EcmaWord : Category.Word, false);
  528. last = -1;
  529. continue;
  530. case 's':
  531. cls.AddCategory (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, false);
  532. last = -1;
  533. continue;
  534. case 'p':
  535. cls.AddCategory (ParseUnicodeCategory (), false); // ignore ecma
  536. last = -1;
  537. continue;
  538. case 'D':
  539. cls.AddCategory (ecma ? Category.EcmaDigit : Category.Digit, true);
  540. last = -1;
  541. continue;
  542. case 'W':
  543. cls.AddCategory (ecma ? Category.EcmaWord : Category.Word, true);
  544. last = -1;
  545. continue;
  546. case 'S':
  547. cls.AddCategory (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, true);
  548. last = -1;
  549. continue;
  550. case 'P':
  551. cls.AddCategory (ParseUnicodeCategory (), true);
  552. last = -1;
  553. continue;
  554. default: break; // add escaped character
  555. }
  556. }
  557. }
  558. if (range) {
  559. if (c < last)
  560. throw NewParseException ("[x-y] range in reverse order.");
  561. if (last >=0 )
  562. cls.AddRange ((char)last, (char)c);
  563. else {
  564. cls.AddCharacter ((char)c);
  565. cls.AddCharacter ('-');
  566. }
  567. range = false;
  568. last = -1;
  569. }
  570. else {
  571. cls.AddCharacter ((char)c);
  572. last = c;
  573. }
  574. }
  575. if (!closed)
  576. throw NewParseException ("Unterminated [] set.");
  577. if (range)
  578. cls.AddCharacter ('-');
  579. return cls;
  580. }
  581. private void ParseRepetitionBounds (out int min, out int max, RegexOptions options) {
  582. int n, m;
  583. /* check syntax */
  584. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  585. if (pattern[ptr] == ',') {
  586. n = -1;
  587. } else {
  588. n = ParseNumber (10, 1, 0);
  589. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  590. }
  591. switch (pattern[ptr ++]) {
  592. case '}':
  593. m = n;
  594. break;
  595. case ',':
  596. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  597. m = ParseNumber (10, 1, 0);
  598. ConsumeWhitespace (IsIgnorePatternWhitespace (options));
  599. if (pattern[ptr ++] != '}')
  600. throw NewParseException ("Illegal {x,y} - bad value of y.");
  601. break;
  602. default:
  603. throw NewParseException ("Illegal {x,y}");
  604. }
  605. /* check bounds and ordering */
  606. if (n >= 0xffff || m >= 0xffff)
  607. throw NewParseException ("Illegal {x, y} - maximum of 65535.");
  608. if (m >= 0 && m < n)
  609. throw NewParseException ("Illegal {x, y} with x > y.");
  610. /* assign min and max */
  611. min = n;
  612. if (m > 0)
  613. max = m;
  614. else
  615. max = 0xffff;
  616. }
  617. private Category ParseUnicodeCategory () {
  618. if (pattern[ptr ++] != '{')
  619. throw NewParseException ("Incomplete \\p{X} character escape.");
  620. string name = ParseName (pattern, ref ptr);
  621. if (name == null)
  622. throw NewParseException ("Incomplete \\p{X} character escape.");
  623. Category cat = CategoryUtils.CategoryFromName (name);
  624. if (cat == Category.None)
  625. throw NewParseException ("Unknown property '" + name + "'.");
  626. if (pattern[ptr ++] != '}')
  627. throw NewParseException ("Incomplete \\p{X} character escape.");
  628. return cat;
  629. }
  630. private Expression ParseSpecial (RegexOptions options) {
  631. int p = ptr;
  632. bool ecma = IsECMAScript (options);
  633. Expression expr = null;
  634. switch (pattern[ptr ++]) {
  635. // categories
  636. case 'd':
  637. expr = new CharacterClass (ecma ? Category.EcmaDigit : Category.Digit, false);
  638. break;
  639. case 'w':
  640. expr = new CharacterClass (ecma ? Category.EcmaWord : Category.Word, false);
  641. break;
  642. case 's':
  643. expr = new CharacterClass (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, false);
  644. break;
  645. case 'p':
  646. // this is odd - ECMAScript isn't supposed to support Unicode,
  647. // yet \p{..} compiles and runs under the MS implementation
  648. // identically to canonical mode. That's why I'm ignoring the
  649. // value of ecma here.
  650. expr = new CharacterClass (ParseUnicodeCategory (), false);
  651. break;
  652. case 'D':
  653. expr = new CharacterClass (ecma ? Category.EcmaDigit : Category.Digit, true);
  654. break;
  655. case 'W':
  656. expr = new CharacterClass (ecma ? Category.EcmaWord : Category.Word, true);
  657. break;
  658. case 'S':
  659. expr = new CharacterClass (ecma ? Category.EcmaWhiteSpace : Category.WhiteSpace, true);
  660. break;
  661. case 'P':
  662. expr = new CharacterClass (ParseUnicodeCategory (), true);
  663. break;
  664. // positions
  665. case 'A': expr = new PositionAssertion (Position.StartOfString); break;
  666. case 'Z': expr = new PositionAssertion (Position.End); break;
  667. case 'z': expr = new PositionAssertion (Position.EndOfString); break;
  668. case 'G': expr = new PositionAssertion (Position.StartOfScan); break;
  669. case 'b': expr = new PositionAssertion (Position.Boundary); break;
  670. case 'B': expr = new PositionAssertion (Position.NonBoundary); break;
  671. // references
  672. case '1': case '2': case '3': case '4': case '5':
  673. case '6': case '7': case '8': case '9': {
  674. ptr --;
  675. int n = ParseNumber (10, 1, 0);
  676. if (n < 0) {
  677. ptr = p;
  678. return null;
  679. }
  680. // FIXME test if number is within number of assigned groups
  681. // this may present a problem for right-to-left matching
  682. Reference reference = new Reference (IsIgnoreCase (options));
  683. refs.Add (reference, n.ToString ());
  684. expr = reference;
  685. break;
  686. }
  687. case 'k': {
  688. char delim = pattern[ptr ++];
  689. if (delim == '<')
  690. delim = '>';
  691. else if (delim != '\'')
  692. throw NewParseException ("Malformed \\k<...> named backreference.");
  693. string name = ParseName ();
  694. if (name == null || pattern[ptr] != delim)
  695. throw NewParseException ("Malformed \\k<...> named backreference.");
  696. ++ ptr;
  697. Reference reference = new Reference (IsIgnoreCase (options));
  698. refs.Add (reference, name);
  699. expr = reference;
  700. break;
  701. }
  702. default:
  703. expr = null;
  704. break;
  705. }
  706. if (expr == null)
  707. ptr = p;
  708. return expr;
  709. }
  710. private int ParseEscape () {
  711. int p = ptr;
  712. int c;
  713. if (p >= pattern.Length)
  714. throw new ArgumentException (
  715. String.Format ("Parsing \"{0}\" - Illegal \\ at end of " +
  716. "pattern.", pattern), pattern);
  717. switch (pattern[ptr ++]) {
  718. // standard escapes (except \b)
  719. case 'a': return '\u0007';
  720. case 't': return '\u0009';
  721. case 'r': return '\u000d';
  722. case 'v': return '\u000b';
  723. case 'f': return '\u000c';
  724. case 'n': return '\u000a';
  725. case 'e': return '\u001b';
  726. case '\\': return '\\';
  727. // character codes
  728. case '0':
  729. int prevptr = ptr;
  730. int result = ParseOctal (pattern, ref ptr);
  731. if (result == -1 && prevptr == ptr)
  732. return 0;
  733. return result;
  734. case 'x':
  735. c = ParseHex (pattern, ref ptr, 2);
  736. if (c < 0)
  737. throw NewParseException ("Insufficient hex digits");
  738. return c;
  739. case 'u':
  740. c = ParseHex (pattern, ref ptr, 4);
  741. if (c < 0)
  742. throw NewParseException ("Insufficient hex digits");
  743. return c;
  744. // control characters
  745. case 'c':
  746. c = pattern[p ++];
  747. if (c >= 'A' && c <= 'Z')
  748. return c - 'A';
  749. else if (c >= '@' && c <= '_')
  750. return c - '@';
  751. else
  752. throw NewParseException ("Unrecognized control character.");
  753. // unknown escape
  754. default:
  755. ptr = p;
  756. return -1;
  757. }
  758. }
  759. private string ParseName () {
  760. return Parser.ParseName (pattern, ref ptr);
  761. }
  762. private static bool IsNameChar (char c) {
  763. UnicodeCategory cat = Char.GetUnicodeCategory (c);
  764. if (cat == UnicodeCategory.ModifierLetter)
  765. return false;
  766. if (cat == UnicodeCategory.ConnectorPunctuation)
  767. return true;
  768. return Char.IsLetterOrDigit (c);
  769. }
  770. private int ParseNumber (int b, int min, int max) {
  771. return Parser.ParseNumber (pattern, ref ptr, b, min, max);
  772. }
  773. private int ParseDecimal () {
  774. return Parser.ParseDecimal (pattern, ref ptr);
  775. }
  776. private static int ParseDigit (char c, int b, int n) {
  777. switch (b) {
  778. case 8:
  779. if (c >= '0' && c <= '7')
  780. return c - '0';
  781. else
  782. return -1;
  783. case 10:
  784. if (c >= '0' && c <= '9')
  785. return c - '0';
  786. else
  787. return -1;
  788. case 16:
  789. if (c >= '0' && c <= '9')
  790. return c - '0';
  791. else if (c >= 'a' && c <= 'f')
  792. return 10 + c - 'a';
  793. else if (c >= 'A' && c <= 'F')
  794. return 10 + c - 'A';
  795. else
  796. return -1;
  797. default:
  798. return -1;
  799. }
  800. }
  801. private void ConsumeWhitespace (bool ignore) {
  802. while (true) {
  803. if (ptr >= pattern.Length)
  804. break;
  805. if (pattern[ptr] == '(') {
  806. if (ptr + 3 >= pattern.Length)
  807. return;
  808. if (pattern[ptr + 1] != '?' || pattern[ptr + 2] != '#')
  809. return;
  810. ptr += 3;
  811. while (pattern[ptr ++] != ')')
  812. /* ignore */ ;
  813. }
  814. else if (ignore && pattern[ptr] == '#') {
  815. while (ptr < pattern.Length && pattern[ptr ++] != '\n')
  816. /* ignore */ ;
  817. }
  818. else if (ignore && Char.IsWhiteSpace (pattern[ptr])) {
  819. while (ptr < pattern.Length && Char.IsWhiteSpace (pattern[ptr]))
  820. ++ ptr;
  821. }
  822. else
  823. return;
  824. }
  825. }
  826. private string ParseString (string pattern) {
  827. this.pattern = pattern;
  828. this.ptr = 0;
  829. string result = "";
  830. while (ptr < pattern.Length) {
  831. int c = pattern[ptr];
  832. if (c == '\\')
  833. c = ParseEscape ();
  834. ptr ++;
  835. result += (char)c;
  836. }
  837. return result;
  838. }
  839. private void ResolveReferences () {
  840. int gid = 1;
  841. Hashtable dict = new Hashtable ();
  842. // number unnamed groups
  843. foreach (CapturingGroup group in caps) {
  844. if (group.Name == null) {
  845. dict.Add (gid.ToString (), group);
  846. group.Number = gid ++;
  847. ++ num_groups;
  848. }
  849. }
  850. // number named groups
  851. foreach (CapturingGroup group in caps) {
  852. if (group.Name != null) {
  853. if (!dict.Contains (group.Name)) {
  854. dict.Add (group.Name, group);
  855. group.Number = gid ++;
  856. ++ num_groups;
  857. }
  858. else {
  859. CapturingGroup prev = (CapturingGroup)dict[group.Name];
  860. group.Number = prev.Number;
  861. }
  862. }
  863. }
  864. // resolve references
  865. foreach (Expression expr in refs.Keys) {
  866. string name = (string)refs[expr];
  867. if (!dict.Contains (name)) {
  868. throw NewParseException ("Reference to undefined group " +
  869. (Char.IsDigit (name[0]) ? "number " : "name ") +
  870. name);
  871. }
  872. CapturingGroup group = (CapturingGroup)dict[name];
  873. if (expr is Reference)
  874. ((Reference)expr).CapturingGroup = group;
  875. else if (expr is CaptureAssertion)
  876. ((CaptureAssertion)expr).CapturingGroup = group;
  877. else if (expr is BalancingGroup)
  878. ((BalancingGroup)expr).Balance = group;
  879. }
  880. }
  881. // flag helper functions
  882. private static bool IsIgnoreCase (RegexOptions options) {
  883. return (options & RegexOptions.IgnoreCase) != 0;
  884. }
  885. private static bool IsMultiline (RegexOptions options) {
  886. return (options & RegexOptions.Multiline) != 0;
  887. }
  888. private static bool IsExplicitCapture (RegexOptions options) {
  889. return (options & RegexOptions.ExplicitCapture) != 0;
  890. }
  891. private static bool IsSingleline (RegexOptions options) {
  892. return (options & RegexOptions.Singleline) != 0;
  893. }
  894. private static bool IsIgnorePatternWhitespace (RegexOptions options) {
  895. return (options & RegexOptions.IgnorePatternWhitespace) != 0;
  896. }
  897. private static bool IsRightToLeft (RegexOptions options) {
  898. return (options & RegexOptions.RightToLeft) != 0;
  899. }
  900. private static bool IsECMAScript (RegexOptions options) {
  901. return (options & RegexOptions.ECMAScript) != 0;
  902. }
  903. // exception creation
  904. private ArgumentException NewParseException (string msg) {
  905. msg = "parsing \"" + pattern + "\" - " + msg;
  906. return new ArgumentException (msg, pattern);
  907. }
  908. private string pattern;
  909. private int ptr;
  910. private ArrayList caps;
  911. private Hashtable refs;
  912. private int num_groups;
  913. }
  914. }