Fixed issue #2059: "Go to line" CLI argument support for diff command
[TortoiseGit.git] / ext / scintilla / src / RESearch.cxx
blobbe8166bcae88ee97c287a189da5a56cdb81bf64b
1 // Scintilla source code edit control
2 /** @file RESearch.cxx
3 ** Regular expression search library.
4 **/
6 /*
7 * regex - Regular expression pattern matching and replacement
9 * By: Ozan S. Yigit (oz)
10 * Dept. of Computer Science
11 * York University
13 * Original code available from http://www.cs.yorku.ca/~oz/
14 * Translation to C++ by Neil Hodgson neilh@scintilla.org
15 * Removed all use of register.
16 * Converted to modern function prototypes.
17 * Put all global/static variables into an object so this code can be
18 * used from multiple threads, etc.
19 * Some extensions by Philippe Lhoste PhiLho(a)GMX.net
20 * '?' extensions by Michael Mullin masmullin@gmail.com
22 * These routines are the PUBLIC DOMAIN equivalents of regex
23 * routines as found in 4.nBSD UN*X, with minor extensions.
25 * These routines are derived from various implementations found
26 * in software tools books, and Conroy's grep. They are NOT derived
27 * from licensed/restricted software.
28 * For more interesting/academic/complicated implementations,
29 * see Henry Spencer's regexp routines, or GNU Emacs pattern
30 * matching module.
32 * Modification history removed.
34 * Interfaces:
35 * RESearch::Compile: compile a regular expression into a NFA.
37 * const char *RESearch::Compile(const char *pattern, int length,
38 * bool caseSensitive, bool posix)
40 * Returns a short error string if they fail.
42 * RESearch::Execute: execute the NFA to match a pattern.
44 * int RESearch::Execute(characterIndexer &ci, int lp, int endp)
46 * re_fail: failure routine for RESearch::Execute. (no longer used)
48 * void re_fail(char *msg, char op)
50 * Regular Expressions:
52 * [1] char matches itself, unless it is a special
53 * character (metachar): . \ [ ] * + ? ^ $
54 * and ( ) if posix option.
56 * [2] . matches any character.
58 * [3] \ matches the character following it, except:
59 * - \a, \b, \f, \n, \r, \t, \v match the corresponding C
60 * escape char, respectively BEL, BS, FF, LF, CR, TAB and VT;
61 * Note that \r and \n are never matched because Scintilla
62 * regex searches are made line per line
63 * (stripped of end-of-line chars).
64 * - if not in posix mode, when followed by a
65 * left or right round bracket (see [8]);
66 * - when followed by a digit 1 to 9 (see [9]);
67 * - when followed by a left or right angle bracket
68 * (see [10]);
69 * - when followed by d, D, s, S, w or W (see [11]);
70 * - when followed by x and two hexa digits (see [12].
71 * Backslash is used as an escape character for all
72 * other meta-characters, and itself.
74 * [4] [set] matches one of the characters in the set.
75 * If the first character in the set is "^",
76 * it matches the characters NOT in the set, i.e.
77 * complements the set. A shorthand S-E (start dash end)
78 * is used to specify a set of characters S up to
79 * E, inclusive. S and E must be characters, otherwise
80 * the dash is taken literally (eg. in expression [\d-a]).
81 * The special characters "]" and "-" have no special
82 * meaning if they appear as the first chars in the set.
83 * To include both, put - first: [-]A-Z]
84 * (or just backslash them).
85 * examples: match:
87 * [-]|] matches these 3 chars,
89 * []-|] matches from ] to | chars
91 * [a-z] any lowercase alpha
93 * [^-]] any char except - and ]
95 * [^A-Z] any char except uppercase
96 * alpha
98 * [a-zA-Z] any alpha
100 * [5] * any regular expression form [1] to [4]
101 * (except [8], [9] and [10] forms of [3]),
102 * followed by closure char (*)
103 * matches zero or more matches of that form.
105 * [6] + same as [5], except it matches one or more.
107 * [5-6] Both [5] and [6] are greedy (they match as much as possible).
108 * Unless they are followed by the 'lazy' quantifier (?)
109 * In which case both [5] and [6] try to match as little as possible
111 * [7] ? same as [5] except it matches zero or one.
113 * [8] a regular expression in the form [1] to [13], enclosed
114 * as \(form\) (or (form) with posix flag) matches what
115 * form matches. The enclosure creates a set of tags,
116 * used for [9] and for pattern substitution.
117 * The tagged forms are numbered starting from 1.
119 * [9] a \ followed by a digit 1 to 9 matches whatever a
120 * previously tagged regular expression ([8]) matched.
122 * [10] \< a regular expression starting with a \< construct
123 * \> and/or ending with a \> construct, restricts the
124 * pattern matching to the beginning of a word, and/or
125 * the end of a word. A word is defined to be a character
126 * string beginning and/or ending with the characters
127 * A-Z a-z 0-9 and _. Scintilla extends this definition
128 * by user setting. The word must also be preceded and/or
129 * followed by any character outside those mentioned.
131 * [11] \l a backslash followed by d, D, s, S, w or W,
132 * becomes a character class (both inside and
133 * outside sets []).
134 * d: decimal digits
135 * D: any char except decimal digits
136 * s: whitespace (space, \t \n \r \f \v)
137 * S: any char except whitespace (see above)
138 * w: alphanumeric & underscore (changed by user setting)
139 * W: any char except alphanumeric & underscore (see above)
141 * [12] \xHH a backslash followed by x and two hexa digits,
142 * becomes the character whose Ascii code is equal
143 * to these digits. If not followed by two digits,
144 * it is 'x' char itself.
146 * [13] a composite regular expression xy where x and y
147 * are in the form [1] to [12] matches the longest
148 * match of x followed by a match for y.
150 * [14] ^ a regular expression starting with a ^ character
151 * $ and/or ending with a $ character, restricts the
152 * pattern matching to the beginning of the line,
153 * or the end of line. [anchors] Elsewhere in the
154 * pattern, ^ and $ are treated as ordinary characters.
157 * Acknowledgements:
159 * HCR's Hugh Redelmeier has been most helpful in various
160 * stages of development. He convinced me to include BOW
161 * and EOW constructs, originally invented by Rob Pike at
162 * the University of Toronto.
164 * References:
165 * Software tools Kernighan & Plauger
166 * Software tools in Pascal Kernighan & Plauger
167 * Grep [rsx-11 C dist] David Conroy
168 * ed - text editor Un*x Programmer's Manual
169 * Advanced editing on Un*x B. W. Kernighan
170 * RegExp routines Henry Spencer
172 * Notes:
174 * This implementation uses a bit-set representation for character
175 * classes for speed and compactness. Each character is represented
176 * by one bit in a 256-bit block. Thus, CCL always takes a
177 * constant 32 bytes in the internal nfa, and RESearch::Execute does a single
178 * bit comparison to locate the character in the set.
180 * Examples:
182 * pattern: foo*.*
183 * compile: CHR f CHR o CLO CHR o END CLO ANY END END
184 * matches: fo foo fooo foobar fobar foxx ...
186 * pattern: fo[ob]a[rz]
187 * compile: CHR f CHR o CCL bitset CHR a CCL bitset END
188 * matches: fobar fooar fobaz fooaz
190 * pattern: foo\\+
191 * compile: CHR f CHR o CHR o CHR \ CLO CHR \ END END
192 * matches: foo\ foo\\ foo\\\ ...
194 * pattern: \(foo\)[1-3]\1 (same as foo[1-3]foo)
195 * compile: BOT 1 CHR f CHR o CHR o EOT 1 CCL bitset REF 1 END
196 * matches: foo1foo foo2foo foo3foo
198 * pattern: \(fo.*\)-\1
199 * compile: BOT 1 CHR f CHR o CLO ANY END EOT 1 CHR - REF 1 END
200 * matches: foo-foo fo-fo fob-fob foobar-foobar ...
203 #include <stdlib.h>
205 #include <string>
207 #include "CharClassify.h"
208 #include "RESearch.h"
210 // Shut up annoying Visual C++ warnings:
211 #ifdef _MSC_VER
212 #pragma warning(disable: 4514)
213 #endif
215 #ifdef SCI_NAMESPACE
216 using namespace Scintilla;
217 #endif
219 #define OKP 1
220 #define NOP 0
222 #define CHR 1
223 #define ANY 2
224 #define CCL 3
225 #define BOL 4
226 #define EOL 5
227 #define BOT 6
228 #define EOT 7
229 #define BOW 8
230 #define EOW 9
231 #define REF 10
232 #define CLO 11
233 #define CLQ 12 /* 0 to 1 closure */
234 #define LCLO 13 /* lazy closure */
236 #define END 0
239 * The following defines are not meant to be changeable.
240 * They are for readability only.
242 #define BLKIND 0370
243 #define BITIND 07
245 const char bitarr[] = { 1, 2, 4, 8, 16, 32, 64, '\200' };
247 #define badpat(x) (*nfa = END, x)
250 * Character classification table for word boundary operators BOW
251 * and EOW is passed in by the creator of this object (Scintilla
252 * Document). The Document default state is that word chars are:
253 * 0-9, a-z, A-Z and _
256 RESearch::RESearch(CharClassify *charClassTable) {
257 failure = 0;
258 charClass = charClassTable;
259 Init();
262 RESearch::~RESearch() {
263 Clear();
266 void RESearch::Init() {
267 sta = NOP; /* status of lastpat */
268 bol = 0;
269 for (int i = 0; i < MAXTAG; i++)
270 pat[i].clear();
271 for (int j = 0; j < BITBLK; j++)
272 bittab[j] = 0;
275 void RESearch::Clear() {
276 for (int i = 0; i < MAXTAG; i++) {
277 pat[i].clear();
278 bopat[i] = NOTFOUND;
279 eopat[i] = NOTFOUND;
283 void RESearch::GrabMatches(CharacterIndexer &ci) {
284 for (unsigned int i = 0; i < MAXTAG; i++) {
285 if ((bopat[i] != NOTFOUND) && (eopat[i] != NOTFOUND)) {
286 unsigned int len = eopat[i] - bopat[i];
287 pat[i] = std::string(len+1, '\0');
288 for (unsigned int j = 0; j < len; j++)
289 pat[i][j] = ci.CharAt(bopat[i] + j);
290 pat[i][len] = '\0';
295 void RESearch::ChSet(unsigned char c) {
296 bittab[((c) & BLKIND) >> 3] |= bitarr[(c) & BITIND];
299 void RESearch::ChSetWithCase(unsigned char c, bool caseSensitive) {
300 if (caseSensitive) {
301 ChSet(c);
302 } else {
303 if ((c >= 'a') && (c <= 'z')) {
304 ChSet(c);
305 ChSet(static_cast<unsigned char>(c - 'a' + 'A'));
306 } else if ((c >= 'A') && (c <= 'Z')) {
307 ChSet(c);
308 ChSet(static_cast<unsigned char>(c - 'A' + 'a'));
309 } else {
310 ChSet(c);
315 unsigned char escapeValue(unsigned char ch) {
316 switch (ch) {
317 case 'a': return '\a';
318 case 'b': return '\b';
319 case 'f': return '\f';
320 case 'n': return '\n';
321 case 'r': return '\r';
322 case 't': return '\t';
323 case 'v': return '\v';
325 return 0;
328 static int GetHexaChar(unsigned char hd1, unsigned char hd2) {
329 int hexValue = 0;
330 if (hd1 >= '0' && hd1 <= '9') {
331 hexValue += 16 * (hd1 - '0');
332 } else if (hd1 >= 'A' && hd1 <= 'F') {
333 hexValue += 16 * (hd1 - 'A' + 10);
334 } else if (hd1 >= 'a' && hd1 <= 'f') {
335 hexValue += 16 * (hd1 - 'a' + 10);
336 } else
337 return -1;
338 if (hd2 >= '0' && hd2 <= '9') {
339 hexValue += hd2 - '0';
340 } else if (hd2 >= 'A' && hd2 <= 'F') {
341 hexValue += hd2 - 'A' + 10;
342 } else if (hd2 >= 'a' && hd2 <= 'f') {
343 hexValue += hd2 - 'a' + 10;
344 } else
345 return -1;
346 return hexValue;
350 * Called when the parser finds a backslash not followed
351 * by a valid expression (like \( in non-Posix mode).
352 * @param pattern: pointer on the char after the backslash.
353 * @param incr: (out) number of chars to skip after expression evaluation.
354 * @return the char if it resolves to a simple char,
355 * or -1 for a char class. In this case, bittab is changed.
357 int RESearch::GetBackslashExpression(
358 const char *pattern,
359 int &incr) {
360 // Since error reporting is primitive and messages are not used anyway,
361 // I choose to interpret unexpected syntax in a logical way instead
362 // of reporting errors. Otherwise, we can stick on, eg., PCRE behavior.
363 incr = 0; // Most of the time, will skip the char "naturally".
364 int c;
365 int result = -1;
366 unsigned char bsc = *pattern;
367 if (!bsc) {
368 // Avoid overrun
369 result = '\\'; // \ at end of pattern, take it literally
370 return result;
373 switch (bsc) {
374 case 'a':
375 case 'b':
376 case 'n':
377 case 'f':
378 case 'r':
379 case 't':
380 case 'v':
381 result = escapeValue(bsc);
382 break;
383 case 'x': {
384 unsigned char hd1 = *(pattern + 1);
385 unsigned char hd2 = *(pattern + 2);
386 int hexValue = GetHexaChar(hd1, hd2);
387 if (hexValue >= 0) {
388 result = hexValue;
389 incr = 2; // Must skip the digits
390 } else {
391 result = 'x'; // \x without 2 digits: see it as 'x'
394 break;
395 case 'd':
396 for (c = '0'; c <= '9'; c++) {
397 ChSet(static_cast<unsigned char>(c));
399 break;
400 case 'D':
401 for (c = 0; c < MAXCHR; c++) {
402 if (c < '0' || c > '9') {
403 ChSet(static_cast<unsigned char>(c));
406 break;
407 case 's':
408 ChSet(' ');
409 ChSet('\t');
410 ChSet('\n');
411 ChSet('\r');
412 ChSet('\f');
413 ChSet('\v');
414 break;
415 case 'S':
416 for (c = 0; c < MAXCHR; c++) {
417 if (c != ' ' && !(c >= 0x09 && c <= 0x0D)) {
418 ChSet(static_cast<unsigned char>(c));
421 break;
422 case 'w':
423 for (c = 0; c < MAXCHR; c++) {
424 if (iswordc(static_cast<unsigned char>(c))) {
425 ChSet(static_cast<unsigned char>(c));
428 break;
429 case 'W':
430 for (c = 0; c < MAXCHR; c++) {
431 if (!iswordc(static_cast<unsigned char>(c))) {
432 ChSet(static_cast<unsigned char>(c));
435 break;
436 default:
437 result = bsc;
439 return result;
442 const char *RESearch::Compile(const char *pattern, int length, bool caseSensitive, bool posix) {
443 char *mp=nfa; /* nfa pointer */
444 char *lp; /* saved pointer */
445 char *sp=nfa; /* another one */
446 char *mpMax = mp + MAXNFA - BITBLK - 10;
448 int tagi = 0; /* tag stack index */
449 int tagc = 1; /* actual tag count */
451 int n;
452 char mask; /* xor mask -CCL/NCL */
453 int c1, c2, prevChar;
455 if (!pattern || !length) {
456 if (sta)
457 return 0;
458 else
459 return badpat("No previous regular expression");
461 sta = NOP;
463 const char *p=pattern; /* pattern pointer */
464 for (int i=0; i<length; i++, p++) {
465 if (mp > mpMax)
466 return badpat("Pattern too long");
467 lp = mp;
468 switch (*p) {
470 case '.': /* match any char */
471 *mp++ = ANY;
472 break;
474 case '^': /* match beginning */
475 if (p == pattern)
476 *mp++ = BOL;
477 else {
478 *mp++ = CHR;
479 *mp++ = *p;
481 break;
483 case '$': /* match endofline */
484 if (!*(p+1))
485 *mp++ = EOL;
486 else {
487 *mp++ = CHR;
488 *mp++ = *p;
490 break;
492 case '[': /* match char class */
493 *mp++ = CCL;
494 prevChar = 0;
496 i++;
497 if (*++p == '^') {
498 mask = '\377';
499 i++;
500 p++;
501 } else
502 mask = 0;
504 if (*p == '-') { /* real dash */
505 i++;
506 prevChar = *p;
507 ChSet(*p++);
509 if (*p == ']') { /* real brace */
510 i++;
511 prevChar = *p;
512 ChSet(*p++);
514 while (*p && *p != ']') {
515 if (*p == '-') {
516 if (prevChar < 0) {
517 // Previous def. was a char class like \d, take dash literally
518 prevChar = *p;
519 ChSet(*p);
520 } else if (*(p+1)) {
521 if (*(p+1) != ']') {
522 c1 = prevChar + 1;
523 i++;
524 c2 = static_cast<unsigned char>(*++p);
525 if (c2 == '\\') {
526 if (!*(p+1)) // End of RE
527 return badpat("Missing ]");
528 else {
529 i++;
530 p++;
531 int incr;
532 c2 = GetBackslashExpression(p, incr);
533 i += incr;
534 p += incr;
535 if (c2 >= 0) {
536 // Convention: \c (c is any char) is case sensitive, whatever the option
537 ChSet(static_cast<unsigned char>(c2));
538 prevChar = c2;
539 } else {
540 // bittab is already changed
541 prevChar = -1;
545 if (prevChar < 0) {
546 // Char after dash is char class like \d, take dash literally
547 prevChar = '-';
548 ChSet('-');
549 } else {
550 // Put all chars between c1 and c2 included in the char set
551 while (c1 <= c2) {
552 ChSetWithCase(static_cast<unsigned char>(c1++), caseSensitive);
555 } else {
556 // Dash before the ], take it literally
557 prevChar = *p;
558 ChSet(*p);
560 } else {
561 return badpat("Missing ]");
563 } else if (*p == '\\' && *(p+1)) {
564 i++;
565 p++;
566 int incr;
567 int c = GetBackslashExpression(p, incr);
568 i += incr;
569 p += incr;
570 if (c >= 0) {
571 // Convention: \c (c is any char) is case sensitive, whatever the option
572 ChSet(static_cast<unsigned char>(c));
573 prevChar = c;
574 } else {
575 // bittab is already changed
576 prevChar = -1;
578 } else {
579 prevChar = static_cast<unsigned char>(*p);
580 ChSetWithCase(*p, caseSensitive);
582 i++;
583 p++;
585 if (!*p)
586 return badpat("Missing ]");
588 for (n = 0; n < BITBLK; bittab[n++] = 0)
589 *mp++ = static_cast<char>(mask ^ bittab[n]);
591 break;
593 case '*': /* match 0 or more... */
594 case '+': /* match 1 or more... */
595 case '?':
596 if (p == pattern)
597 return badpat("Empty closure");
598 lp = sp; /* previous opcode */
599 if (*lp == CLO || *lp == LCLO) /* equivalence... */
600 break;
601 switch (*lp) {
603 case BOL:
604 case BOT:
605 case EOT:
606 case BOW:
607 case EOW:
608 case REF:
609 return badpat("Illegal closure");
610 default:
611 break;
614 if (*p == '+')
615 for (sp = mp; lp < sp; lp++)
616 *mp++ = *lp;
618 *mp++ = END;
619 *mp++ = END;
620 sp = mp;
622 while (--mp > lp)
623 *mp = mp[-1];
624 if (*p == '?') *mp = CLQ;
625 else if (*(p+1) == '?') *mp = LCLO;
626 else *mp = CLO;
628 mp = sp;
629 break;
631 case '\\': /* tags, backrefs... */
632 i++;
633 switch (*++p) {
634 case '<':
635 *mp++ = BOW;
636 break;
637 case '>':
638 if (*sp == BOW)
639 return badpat("Null pattern inside \\<\\>");
640 *mp++ = EOW;
641 break;
642 case '1':
643 case '2':
644 case '3':
645 case '4':
646 case '5':
647 case '6':
648 case '7':
649 case '8':
650 case '9':
651 n = *p-'0';
652 if (tagi > 0 && tagstk[tagi] == n)
653 return badpat("Cyclical reference");
654 if (tagc > n) {
655 *mp++ = static_cast<char>(REF);
656 *mp++ = static_cast<char>(n);
657 } else
658 return badpat("Undetermined reference");
659 break;
660 default:
661 if (!posix && *p == '(') {
662 if (tagc < MAXTAG) {
663 tagstk[++tagi] = tagc;
664 *mp++ = BOT;
665 *mp++ = static_cast<char>(tagc++);
666 } else
667 return badpat("Too many \\(\\) pairs");
668 } else if (!posix && *p == ')') {
669 if (*sp == BOT)
670 return badpat("Null pattern inside \\(\\)");
671 if (tagi > 0) {
672 *mp++ = static_cast<char>(EOT);
673 *mp++ = static_cast<char>(tagstk[tagi--]);
674 } else
675 return badpat("Unmatched \\)");
676 } else {
677 int incr;
678 int c = GetBackslashExpression(p, incr);
679 i += incr;
680 p += incr;
681 if (c >= 0) {
682 *mp++ = CHR;
683 *mp++ = static_cast<unsigned char>(c);
684 } else {
685 *mp++ = CCL;
686 mask = 0;
687 for (n = 0; n < BITBLK; bittab[n++] = 0)
688 *mp++ = static_cast<char>(mask ^ bittab[n]);
692 break;
694 default : /* an ordinary char */
695 if (posix && *p == '(') {
696 if (tagc < MAXTAG) {
697 tagstk[++tagi] = tagc;
698 *mp++ = BOT;
699 *mp++ = static_cast<char>(tagc++);
700 } else
701 return badpat("Too many () pairs");
702 } else if (posix && *p == ')') {
703 if (*sp == BOT)
704 return badpat("Null pattern inside ()");
705 if (tagi > 0) {
706 *mp++ = static_cast<char>(EOT);
707 *mp++ = static_cast<char>(tagstk[tagi--]);
708 } else
709 return badpat("Unmatched )");
710 } else {
711 unsigned char c = *p;
712 if (!c) // End of RE
713 c = '\\'; // We take it as raw backslash
714 if (caseSensitive || !iswordc(c)) {
715 *mp++ = CHR;
716 *mp++ = c;
717 } else {
718 *mp++ = CCL;
719 mask = 0;
720 ChSetWithCase(c, false);
721 for (n = 0; n < BITBLK; bittab[n++] = 0)
722 *mp++ = static_cast<char>(mask ^ bittab[n]);
725 break;
727 sp = lp;
729 if (tagi > 0)
730 return badpat((posix ? "Unmatched (" : "Unmatched \\("));
731 *mp = END;
732 sta = OKP;
733 return 0;
737 * RESearch::Execute:
738 * execute nfa to find a match.
740 * special cases: (nfa[0])
741 * BOL
742 * Match only once, starting from the
743 * beginning.
744 * CHR
745 * First locate the character without
746 * calling PMatch, and if found, call
747 * PMatch for the remaining string.
748 * END
749 * RESearch::Compile failed, poor luser did not
750 * check for it. Fail fast.
752 * If a match is found, bopat[0] and eopat[0] are set
753 * to the beginning and the end of the matched fragment,
754 * respectively.
757 int RESearch::Execute(CharacterIndexer &ci, int lp, int endp) {
758 unsigned char c;
759 int ep = NOTFOUND;
760 char *ap = nfa;
762 bol = lp;
763 failure = 0;
765 Clear();
767 switch (*ap) {
769 case BOL: /* anchored: match from BOL only */
770 ep = PMatch(ci, lp, endp, ap);
771 break;
772 case EOL: /* just searching for end of line normal path doesn't work */
773 if (*(ap+1) == END) {
774 lp = endp;
775 ep = lp;
776 break;
777 } else {
778 return 0;
780 case CHR: /* ordinary char: locate it fast */
781 c = *(ap+1);
782 while ((lp < endp) && (static_cast<unsigned char>(ci.CharAt(lp)) != c))
783 lp++;
784 if (lp >= endp) /* if EOS, fail, else fall thru. */
785 return 0;
786 default: /* regular matching all the way. */
787 while (lp < endp) {
788 ep = PMatch(ci, lp, endp, ap);
789 if (ep != NOTFOUND)
790 break;
791 lp++;
793 break;
794 case END: /* munged automaton. fail always */
795 return 0;
797 if (ep == NOTFOUND)
798 return 0;
800 bopat[0] = lp;
801 eopat[0] = ep;
802 return 1;
806 * PMatch: internal routine for the hard part
808 * This code is partly snarfed from an early grep written by
809 * David Conroy. The backref and tag stuff, and various other
810 * innovations are by oz.
812 * special case optimizations: (nfa[n], nfa[n+1])
813 * CLO ANY
814 * We KNOW .* will match everything upto the
815 * end of line. Thus, directly go to the end of
816 * line, without recursive PMatch calls. As in
817 * the other closure cases, the remaining pattern
818 * must be matched by moving backwards on the
819 * string recursively, to find a match for xy
820 * (x is ".*" and y is the remaining pattern)
821 * where the match satisfies the LONGEST match for
822 * x followed by a match for y.
823 * CLO CHR
824 * We can again scan the string forward for the
825 * single char and at the point of failure, we
826 * execute the remaining nfa recursively, same as
827 * above.
829 * At the end of a successful match, bopat[n] and eopat[n]
830 * are set to the beginning and end of subpatterns matched
831 * by tagged expressions (n = 1 to 9).
834 extern void re_fail(char *,char);
836 #define isinset(x,y) ((x)[((y)&BLKIND)>>3] & bitarr[(y)&BITIND])
839 * skip values for CLO XXX to skip past the closure
842 #define ANYSKIP 2 /* [CLO] ANY END */
843 #define CHRSKIP 3 /* [CLO] CHR chr END */
844 #define CCLSKIP 34 /* [CLO] CCL 32 bytes END */
846 int RESearch::PMatch(CharacterIndexer &ci, int lp, int endp, char *ap) {
847 int op, c, n;
848 int e; /* extra pointer for CLO */
849 int bp; /* beginning of subpat... */
850 int ep; /* ending of subpat... */
851 int are; /* to save the line ptr. */
852 int llp; /* lazy lp for LCLO */
854 while ((op = *ap++) != END)
855 switch (op) {
857 case CHR:
858 if (ci.CharAt(lp++) != *ap++)
859 return NOTFOUND;
860 break;
861 case ANY:
862 if (lp++ >= endp)
863 return NOTFOUND;
864 break;
865 case CCL:
866 if (lp >= endp)
867 return NOTFOUND;
868 c = ci.CharAt(lp++);
869 if (!isinset(ap,c))
870 return NOTFOUND;
871 ap += BITBLK;
872 break;
873 case BOL:
874 if (lp != bol)
875 return NOTFOUND;
876 break;
877 case EOL:
878 if (lp < endp)
879 return NOTFOUND;
880 break;
881 case BOT:
882 bopat[static_cast<int>(*ap++)] = lp;
883 break;
884 case EOT:
885 eopat[static_cast<int>(*ap++)] = lp;
886 break;
887 case BOW:
888 if ((lp!=bol && iswordc(ci.CharAt(lp-1))) || !iswordc(ci.CharAt(lp)))
889 return NOTFOUND;
890 break;
891 case EOW:
892 if (lp==bol || !iswordc(ci.CharAt(lp-1)) || iswordc(ci.CharAt(lp)))
893 return NOTFOUND;
894 break;
895 case REF:
896 n = *ap++;
897 bp = bopat[n];
898 ep = eopat[n];
899 while (bp < ep)
900 if (ci.CharAt(bp++) != ci.CharAt(lp++))
901 return NOTFOUND;
902 break;
903 case LCLO:
904 case CLQ:
905 case CLO:
906 are = lp;
907 switch (*ap) {
909 case ANY:
910 if (op == CLO || op == LCLO)
911 while (lp < endp)
912 lp++;
913 else if (lp < endp)
914 lp++;
916 n = ANYSKIP;
917 break;
918 case CHR:
919 c = *(ap+1);
920 if (op == CLO || op == LCLO)
921 while ((lp < endp) && (c == ci.CharAt(lp)))
922 lp++;
923 else if ((lp < endp) && (c == ci.CharAt(lp)))
924 lp++;
925 n = CHRSKIP;
926 break;
927 case CCL:
928 while ((lp < endp) && isinset(ap+1,ci.CharAt(lp)))
929 lp++;
930 n = CCLSKIP;
931 break;
932 default:
933 failure = true;
934 //re_fail("closure: bad nfa.", *ap);
935 return NOTFOUND;
937 ap += n;
939 llp = lp;
940 e = NOTFOUND;
941 while (llp >= are) {
942 int q;
943 if ((q = PMatch(ci, llp, endp, ap)) != NOTFOUND) {
944 e = q;
945 lp = llp;
946 if (op != LCLO) return e;
948 if (*ap == END) return e;
949 --llp;
951 if (*ap == EOT)
952 PMatch(ci, lp, endp, ap);
953 return e;
954 default:
955 //re_fail("RESearch::Execute: bad nfa.", static_cast<char>(op));
956 return NOTFOUND;
958 return lp;