Update Scintilla to version 3.4.4
[TortoiseGit.git] / ext / scintilla / src / RESearch.cxx
blob4c2957bdfe0ee5ffd2e7b1e2dda2dcfdf1e4b460
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 #ifdef SCI_NAMESPACE
211 using namespace Scintilla;
212 #endif
214 #define OKP 1
215 #define NOP 0
217 #define CHR 1
218 #define ANY 2
219 #define CCL 3
220 #define BOL 4
221 #define EOL 5
222 #define BOT 6
223 #define EOT 7
224 #define BOW 8
225 #define EOW 9
226 #define REF 10
227 #define CLO 11
228 #define CLQ 12 /* 0 to 1 closure */
229 #define LCLO 13 /* lazy closure */
231 #define END 0
234 * The following defines are not meant to be changeable.
235 * They are for readability only.
237 #define BLKIND 0370
238 #define BITIND 07
240 const char bitarr[] = { 1, 2, 4, 8, 16, 32, 64, '\200' };
242 #define badpat(x) (*nfa = END, x)
245 * Character classification table for word boundary operators BOW
246 * and EOW is passed in by the creator of this object (Scintilla
247 * Document). The Document default state is that word chars are:
248 * 0-9, a-z, A-Z and _
251 RESearch::RESearch(CharClassify *charClassTable) {
252 failure = 0;
253 charClass = charClassTable;
254 Init();
257 RESearch::~RESearch() {
258 Clear();
261 void RESearch::Init() {
262 sta = NOP; /* status of lastpat */
263 bol = 0;
264 for (int i = 0; i < MAXTAG; i++)
265 pat[i].clear();
266 for (int j = 0; j < BITBLK; j++)
267 bittab[j] = 0;
270 void RESearch::Clear() {
271 for (int i = 0; i < MAXTAG; i++) {
272 pat[i].clear();
273 bopat[i] = NOTFOUND;
274 eopat[i] = NOTFOUND;
278 void RESearch::GrabMatches(CharacterIndexer &ci) {
279 for (unsigned int i = 0; i < MAXTAG; i++) {
280 if ((bopat[i] != NOTFOUND) && (eopat[i] != NOTFOUND)) {
281 unsigned int len = eopat[i] - bopat[i];
282 pat[i] = std::string(len+1, '\0');
283 for (unsigned int j = 0; j < len; j++)
284 pat[i][j] = ci.CharAt(bopat[i] + j);
285 pat[i][len] = '\0';
290 void RESearch::ChSet(unsigned char c) {
291 bittab[((c) & BLKIND) >> 3] |= bitarr[(c) & BITIND];
294 void RESearch::ChSetWithCase(unsigned char c, bool caseSensitive) {
295 if (caseSensitive) {
296 ChSet(c);
297 } else {
298 if ((c >= 'a') && (c <= 'z')) {
299 ChSet(c);
300 ChSet(static_cast<unsigned char>(c - 'a' + 'A'));
301 } else if ((c >= 'A') && (c <= 'Z')) {
302 ChSet(c);
303 ChSet(static_cast<unsigned char>(c - 'A' + 'a'));
304 } else {
305 ChSet(c);
310 unsigned char escapeValue(unsigned char ch) {
311 switch (ch) {
312 case 'a': return '\a';
313 case 'b': return '\b';
314 case 'f': return '\f';
315 case 'n': return '\n';
316 case 'r': return '\r';
317 case 't': return '\t';
318 case 'v': return '\v';
320 return 0;
323 static int GetHexaChar(unsigned char hd1, unsigned char hd2) {
324 int hexValue = 0;
325 if (hd1 >= '0' && hd1 <= '9') {
326 hexValue += 16 * (hd1 - '0');
327 } else if (hd1 >= 'A' && hd1 <= 'F') {
328 hexValue += 16 * (hd1 - 'A' + 10);
329 } else if (hd1 >= 'a' && hd1 <= 'f') {
330 hexValue += 16 * (hd1 - 'a' + 10);
331 } else {
332 return -1;
334 if (hd2 >= '0' && hd2 <= '9') {
335 hexValue += hd2 - '0';
336 } else if (hd2 >= 'A' && hd2 <= 'F') {
337 hexValue += hd2 - 'A' + 10;
338 } else if (hd2 >= 'a' && hd2 <= 'f') {
339 hexValue += hd2 - 'a' + 10;
340 } else {
341 return -1;
343 return hexValue;
347 * Called when the parser finds a backslash not followed
348 * by a valid expression (like \( in non-Posix mode).
349 * @param pattern: pointer on the char after the backslash.
350 * @param incr: (out) number of chars to skip after expression evaluation.
351 * @return the char if it resolves to a simple char,
352 * or -1 for a char class. In this case, bittab is changed.
354 int RESearch::GetBackslashExpression(
355 const char *pattern,
356 int &incr) {
357 // Since error reporting is primitive and messages are not used anyway,
358 // I choose to interpret unexpected syntax in a logical way instead
359 // of reporting errors. Otherwise, we can stick on, eg., PCRE behavior.
360 incr = 0; // Most of the time, will skip the char "naturally".
361 int c;
362 int result = -1;
363 unsigned char bsc = *pattern;
364 if (!bsc) {
365 // Avoid overrun
366 result = '\\'; // \ at end of pattern, take it literally
367 return result;
370 switch (bsc) {
371 case 'a':
372 case 'b':
373 case 'n':
374 case 'f':
375 case 'r':
376 case 't':
377 case 'v':
378 result = escapeValue(bsc);
379 break;
380 case 'x': {
381 unsigned char hd1 = *(pattern + 1);
382 unsigned char hd2 = *(pattern + 2);
383 int hexValue = GetHexaChar(hd1, hd2);
384 if (hexValue >= 0) {
385 result = hexValue;
386 incr = 2; // Must skip the digits
387 } else {
388 result = 'x'; // \x without 2 digits: see it as 'x'
391 break;
392 case 'd':
393 for (c = '0'; c <= '9'; c++) {
394 ChSet(static_cast<unsigned char>(c));
396 break;
397 case 'D':
398 for (c = 0; c < MAXCHR; c++) {
399 if (c < '0' || c > '9') {
400 ChSet(static_cast<unsigned char>(c));
403 break;
404 case 's':
405 ChSet(' ');
406 ChSet('\t');
407 ChSet('\n');
408 ChSet('\r');
409 ChSet('\f');
410 ChSet('\v');
411 break;
412 case 'S':
413 for (c = 0; c < MAXCHR; c++) {
414 if (c != ' ' && !(c >= 0x09 && c <= 0x0D)) {
415 ChSet(static_cast<unsigned char>(c));
418 break;
419 case 'w':
420 for (c = 0; c < MAXCHR; c++) {
421 if (iswordc(static_cast<unsigned char>(c))) {
422 ChSet(static_cast<unsigned char>(c));
425 break;
426 case 'W':
427 for (c = 0; c < MAXCHR; c++) {
428 if (!iswordc(static_cast<unsigned char>(c))) {
429 ChSet(static_cast<unsigned char>(c));
432 break;
433 default:
434 result = bsc;
436 return result;
439 const char *RESearch::Compile(const char *pattern, int length, bool caseSensitive, bool posix) {
440 char *mp=nfa; /* nfa pointer */
441 char *lp; /* saved pointer */
442 char *sp=nfa; /* another one */
443 char *mpMax = mp + MAXNFA - BITBLK - 10;
445 int tagi = 0; /* tag stack index */
446 int tagc = 1; /* actual tag count */
448 int n;
449 char mask; /* xor mask -CCL/NCL */
450 int c1, c2, prevChar;
452 if (!pattern || !length) {
453 if (sta)
454 return 0;
455 else
456 return badpat("No previous regular expression");
458 sta = NOP;
460 const char *p=pattern; /* pattern pointer */
461 for (int i=0; i<length; i++, p++) {
462 if (mp > mpMax)
463 return badpat("Pattern too long");
464 lp = mp;
465 switch (*p) {
467 case '.': /* match any char */
468 *mp++ = ANY;
469 break;
471 case '^': /* match beginning */
472 if (p == pattern) {
473 *mp++ = BOL;
474 } else {
475 *mp++ = CHR;
476 *mp++ = *p;
478 break;
480 case '$': /* match endofline */
481 if (!*(p+1)) {
482 *mp++ = EOL;
483 } else {
484 *mp++ = CHR;
485 *mp++ = *p;
487 break;
489 case '[': /* match char class */
490 *mp++ = CCL;
491 prevChar = 0;
493 i++;
494 if (*++p == '^') {
495 mask = '\377';
496 i++;
497 p++;
498 } else {
499 mask = 0;
502 if (*p == '-') { /* real dash */
503 i++;
504 prevChar = *p;
505 ChSet(*p++);
507 if (*p == ']') { /* real brace */
508 i++;
509 prevChar = *p;
510 ChSet(*p++);
512 while (*p && *p != ']') {
513 if (*p == '-') {
514 if (prevChar < 0) {
515 // Previous def. was a char class like \d, take dash literally
516 prevChar = *p;
517 ChSet(*p);
518 } else if (*(p+1)) {
519 if (*(p+1) != ']') {
520 c1 = prevChar + 1;
521 i++;
522 c2 = static_cast<unsigned char>(*++p);
523 if (c2 == '\\') {
524 if (!*(p+1)) { // End of RE
525 return badpat("Missing ]");
526 } else {
527 i++;
528 p++;
529 int incr;
530 c2 = GetBackslashExpression(p, incr);
531 i += incr;
532 p += incr;
533 if (c2 >= 0) {
534 // Convention: \c (c is any char) is case sensitive, whatever the option
535 ChSet(static_cast<unsigned char>(c2));
536 prevChar = c2;
537 } else {
538 // bittab is already changed
539 prevChar = -1;
543 if (prevChar < 0) {
544 // Char after dash is char class like \d, take dash literally
545 prevChar = '-';
546 ChSet('-');
547 } else {
548 // Put all chars between c1 and c2 included in the char set
549 while (c1 <= c2) {
550 ChSetWithCase(static_cast<unsigned char>(c1++), caseSensitive);
553 } else {
554 // Dash before the ], take it literally
555 prevChar = *p;
556 ChSet(*p);
558 } else {
559 return badpat("Missing ]");
561 } else if (*p == '\\' && *(p+1)) {
562 i++;
563 p++;
564 int incr;
565 int c = GetBackslashExpression(p, incr);
566 i += incr;
567 p += incr;
568 if (c >= 0) {
569 // Convention: \c (c is any char) is case sensitive, whatever the option
570 ChSet(static_cast<unsigned char>(c));
571 prevChar = c;
572 } else {
573 // bittab is already changed
574 prevChar = -1;
576 } else {
577 prevChar = static_cast<unsigned char>(*p);
578 ChSetWithCase(*p, caseSensitive);
580 i++;
581 p++;
583 if (!*p)
584 return badpat("Missing ]");
586 for (n = 0; n < BITBLK; bittab[n++] = 0)
587 *mp++ = static_cast<char>(mask ^ bittab[n]);
589 break;
591 case '*': /* match 0 or more... */
592 case '+': /* match 1 or more... */
593 case '?':
594 if (p == pattern)
595 return badpat("Empty closure");
596 lp = sp; /* previous opcode */
597 if (*lp == CLO || *lp == LCLO) /* equivalence... */
598 break;
599 switch (*lp) {
601 case BOL:
602 case BOT:
603 case EOT:
604 case BOW:
605 case EOW:
606 case REF:
607 return badpat("Illegal closure");
608 default:
609 break;
612 if (*p == '+')
613 for (sp = mp; lp < sp; lp++)
614 *mp++ = *lp;
616 *mp++ = END;
617 *mp++ = END;
618 sp = mp;
620 while (--mp > lp)
621 *mp = mp[-1];
622 if (*p == '?') *mp = CLQ;
623 else if (*(p+1) == '?') *mp = LCLO;
624 else *mp = CLO;
626 mp = sp;
627 break;
629 case '\\': /* tags, backrefs... */
630 i++;
631 switch (*++p) {
632 case '<':
633 *mp++ = BOW;
634 break;
635 case '>':
636 if (*sp == BOW)
637 return badpat("Null pattern inside \\<\\>");
638 *mp++ = EOW;
639 break;
640 case '1':
641 case '2':
642 case '3':
643 case '4':
644 case '5':
645 case '6':
646 case '7':
647 case '8':
648 case '9':
649 n = *p-'0';
650 if (tagi > 0 && tagstk[tagi] == n)
651 return badpat("Cyclical reference");
652 if (tagc > n) {
653 *mp++ = static_cast<char>(REF);
654 *mp++ = static_cast<char>(n);
655 } else {
656 return badpat("Undetermined reference");
658 break;
659 default:
660 if (!posix && *p == '(') {
661 if (tagc < MAXTAG) {
662 tagstk[++tagi] = tagc;
663 *mp++ = BOT;
664 *mp++ = static_cast<char>(tagc++);
665 } else {
666 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 \\)");
677 } else {
678 int incr;
679 int c = GetBackslashExpression(p, incr);
680 i += incr;
681 p += incr;
682 if (c >= 0) {
683 *mp++ = CHR;
684 *mp++ = static_cast<unsigned char>(c);
685 } else {
686 *mp++ = CCL;
687 mask = 0;
688 for (n = 0; n < BITBLK; bittab[n++] = 0)
689 *mp++ = static_cast<char>(mask ^ bittab[n]);
693 break;
695 default : /* an ordinary char */
696 if (posix && *p == '(') {
697 if (tagc < MAXTAG) {
698 tagstk[++tagi] = tagc;
699 *mp++ = BOT;
700 *mp++ = static_cast<char>(tagc++);
701 } else {
702 return badpat("Too many () pairs");
704 } else if (posix && *p == ')') {
705 if (*sp == BOT)
706 return badpat("Null pattern inside ()");
707 if (tagi > 0) {
708 *mp++ = static_cast<char>(EOT);
709 *mp++ = static_cast<char>(tagstk[tagi--]);
710 } else {
711 return badpat("Unmatched )");
713 } else {
714 unsigned char c = *p;
715 if (!c) // End of RE
716 c = '\\'; // We take it as raw backslash
717 if (caseSensitive || !iswordc(c)) {
718 *mp++ = CHR;
719 *mp++ = c;
720 } else {
721 *mp++ = CCL;
722 mask = 0;
723 ChSetWithCase(c, false);
724 for (n = 0; n < BITBLK; bittab[n++] = 0)
725 *mp++ = static_cast<char>(mask ^ bittab[n]);
728 break;
730 sp = lp;
732 if (tagi > 0)
733 return badpat((posix ? "Unmatched (" : "Unmatched \\("));
734 *mp = END;
735 sta = OKP;
736 return 0;
740 * RESearch::Execute:
741 * execute nfa to find a match.
743 * special cases: (nfa[0])
744 * BOL
745 * Match only once, starting from the
746 * beginning.
747 * CHR
748 * First locate the character without
749 * calling PMatch, and if found, call
750 * PMatch for the remaining string.
751 * END
752 * RESearch::Compile failed, poor luser did not
753 * check for it. Fail fast.
755 * If a match is found, bopat[0] and eopat[0] are set
756 * to the beginning and the end of the matched fragment,
757 * respectively.
760 int RESearch::Execute(CharacterIndexer &ci, int lp, int endp) {
761 unsigned char c;
762 int ep = NOTFOUND;
763 char *ap = nfa;
765 bol = lp;
766 failure = 0;
768 Clear();
770 switch (*ap) {
772 case BOL: /* anchored: match from BOL only */
773 ep = PMatch(ci, lp, endp, ap);
774 break;
775 case EOL: /* just searching for end of line normal path doesn't work */
776 if (*(ap+1) == END) {
777 lp = endp;
778 ep = lp;
779 break;
780 } else {
781 return 0;
783 case CHR: /* ordinary char: locate it fast */
784 c = *(ap+1);
785 while ((lp < endp) && (static_cast<unsigned char>(ci.CharAt(lp)) != c))
786 lp++;
787 if (lp >= endp) /* if EOS, fail, else fall through. */
788 return 0;
789 default: /* regular matching all the way. */
790 while (lp < endp) {
791 ep = PMatch(ci, lp, endp, ap);
792 if (ep != NOTFOUND)
793 break;
794 lp++;
796 break;
797 case END: /* munged automaton. fail always */
798 return 0;
800 if (ep == NOTFOUND)
801 return 0;
803 bopat[0] = lp;
804 eopat[0] = ep;
805 return 1;
809 * PMatch: internal routine for the hard part
811 * This code is partly snarfed from an early grep written by
812 * David Conroy. The backref and tag stuff, and various other
813 * innovations are by oz.
815 * special case optimizations: (nfa[n], nfa[n+1])
816 * CLO ANY
817 * We KNOW .* will match everything up to the
818 * end of line. Thus, directly go to the end of
819 * line, without recursive PMatch calls. As in
820 * the other closure cases, the remaining pattern
821 * must be matched by moving backwards on the
822 * string recursively, to find a match for xy
823 * (x is ".*" and y is the remaining pattern)
824 * where the match satisfies the LONGEST match for
825 * x followed by a match for y.
826 * CLO CHR
827 * We can again scan the string forward for the
828 * single char and at the point of failure, we
829 * execute the remaining nfa recursively, same as
830 * above.
832 * At the end of a successful match, bopat[n] and eopat[n]
833 * are set to the beginning and end of subpatterns matched
834 * by tagged expressions (n = 1 to 9).
837 extern void re_fail(char *,char);
839 #define isinset(x,y) ((x)[((y)&BLKIND)>>3] & bitarr[(y)&BITIND])
842 * skip values for CLO XXX to skip past the closure
845 #define ANYSKIP 2 /* [CLO] ANY END */
846 #define CHRSKIP 3 /* [CLO] CHR chr END */
847 #define CCLSKIP 34 /* [CLO] CCL 32 bytes END */
849 int RESearch::PMatch(CharacterIndexer &ci, int lp, int endp, char *ap) {
850 int op, c, n;
851 int e; /* extra pointer for CLO */
852 int bp; /* beginning of subpat... */
853 int ep; /* ending of subpat... */
854 int are; /* to save the line ptr. */
855 int llp; /* lazy lp for LCLO */
857 while ((op = *ap++) != END)
858 switch (op) {
860 case CHR:
861 if (ci.CharAt(lp++) != *ap++)
862 return NOTFOUND;
863 break;
864 case ANY:
865 if (lp++ >= endp)
866 return NOTFOUND;
867 break;
868 case CCL:
869 if (lp >= endp)
870 return NOTFOUND;
871 c = ci.CharAt(lp++);
872 if (!isinset(ap,c))
873 return NOTFOUND;
874 ap += BITBLK;
875 break;
876 case BOL:
877 if (lp != bol)
878 return NOTFOUND;
879 break;
880 case EOL:
881 if (lp < endp)
882 return NOTFOUND;
883 break;
884 case BOT:
885 bopat[static_cast<int>(*ap++)] = lp;
886 break;
887 case EOT:
888 eopat[static_cast<int>(*ap++)] = lp;
889 break;
890 case BOW:
891 if ((lp!=bol && iswordc(ci.CharAt(lp-1))) || !iswordc(ci.CharAt(lp)))
892 return NOTFOUND;
893 break;
894 case EOW:
895 if (lp==bol || !iswordc(ci.CharAt(lp-1)) || iswordc(ci.CharAt(lp)))
896 return NOTFOUND;
897 break;
898 case REF:
899 n = *ap++;
900 bp = bopat[n];
901 ep = eopat[n];
902 while (bp < ep)
903 if (ci.CharAt(bp++) != ci.CharAt(lp++))
904 return NOTFOUND;
905 break;
906 case LCLO:
907 case CLQ:
908 case CLO:
909 are = lp;
910 switch (*ap) {
912 case ANY:
913 if (op == CLO || op == LCLO)
914 while (lp < endp)
915 lp++;
916 else if (lp < endp)
917 lp++;
919 n = ANYSKIP;
920 break;
921 case CHR:
922 c = *(ap+1);
923 if (op == CLO || op == LCLO)
924 while ((lp < endp) && (c == ci.CharAt(lp)))
925 lp++;
926 else if ((lp < endp) && (c == ci.CharAt(lp)))
927 lp++;
928 n = CHRSKIP;
929 break;
930 case CCL:
931 while ((lp < endp) && isinset(ap+1,ci.CharAt(lp)))
932 lp++;
933 n = CCLSKIP;
934 break;
935 default:
936 failure = true;
937 //re_fail("closure: bad nfa.", *ap);
938 return NOTFOUND;
940 ap += n;
942 llp = lp;
943 e = NOTFOUND;
944 while (llp >= are) {
945 int q;
946 if ((q = PMatch(ci, llp, endp, ap)) != NOTFOUND) {
947 e = q;
948 lp = llp;
949 if (op != LCLO) return e;
951 if (*ap == END) return e;
952 --llp;
954 if (*ap == EOT)
955 PMatch(ci, lp, endp, ap);
956 return e;
957 default:
958 //re_fail("RESearch::Execute: bad nfa.", static_cast<char>(op));
959 return NOTFOUND;
961 return lp;