Fix typos
[TortoiseGit.git] / ext / scintilla / src / RESearch.cxx
blobf4974c59bb8c3f19a5cc8018c3670576f788f35d
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 <cstddef>
204 #include <cstdlib>
206 #include <stdexcept>
207 #include <string>
208 #include <array>
209 #include <algorithm>
210 #include <iterator>
212 #include "Position.h"
213 #include "CharClassify.h"
214 #include "RESearch.h"
216 using namespace Scintilla::Internal;
218 #define OKP 1
219 #define NOP 0
221 #define CHR 1
222 #define ANY 2
223 #define CCL 3
224 #define BOL 4
225 #define EOL 5
226 #define BOT 6
227 #define EOT 7
228 #define BOW 8
229 #define EOW 9
230 #define REF 10
231 #define CLO 11
232 #define CLQ 12 /* 0 to 1 closure */
233 #define LCLO 13 /* lazy closure */
235 #define END 0
238 * The following defines are not meant to be changeable.
239 * They are for readability only.
241 #define BITIND 07
243 #define badpat(x) (*nfa = END, x)
246 * Character classification table for word boundary operators BOW
247 * and EOW is passed in by the creator of this object (Scintilla
248 * Document). The Document default state is that word chars are:
249 * 0-9, a-z, A-Z and _
252 RESearch::RESearch(CharClassify *charClassTable) {
253 failure = 0;
254 charClass = charClassTable;
255 sta = NOP; /* status of lastpat */
256 lineStartPos = 0;
257 lineEndPos = 0;
258 nfa[0] = END;
259 Clear();
262 void RESearch::Clear() {
263 bopat.fill(NOTFOUND);
264 eopat.fill(NOTFOUND);
267 void RESearch::ChSet(unsigned char c) noexcept {
268 bittab[c >> 3] |= 1 << (c & BITIND);
271 void RESearch::ChSetWithCase(unsigned char c, bool caseSensitive) noexcept {
272 ChSet(c);
273 if (!caseSensitive) {
274 if ((c >= 'a') && (c <= 'z')) {
275 ChSet(c - 'a' + 'A');
276 } else if ((c >= 'A') && (c <= 'Z')) {
277 ChSet(c - 'A' + 'a');
282 namespace {
284 constexpr unsigned char escapeValue(unsigned char ch) noexcept {
285 switch (ch) {
286 case 'a': return '\a';
287 case 'b': return '\b';
288 case 'f': return '\f';
289 case 'n': return '\n';
290 case 'r': return '\r';
291 case 't': return '\t';
292 case 'v': return '\v';
293 default: break;
295 return 0;
298 constexpr int GetHexaChar(unsigned char hd1, unsigned char hd2) noexcept {
299 int hexValue = 0;
300 if (hd1 >= '0' && hd1 <= '9') {
301 hexValue += 16 * (hd1 - '0');
302 } else if (hd1 >= 'A' && hd1 <= 'F') {
303 hexValue += 16 * (hd1 - 'A' + 10);
304 } else if (hd1 >= 'a' && hd1 <= 'f') {
305 hexValue += 16 * (hd1 - 'a' + 10);
306 } else {
307 return -1;
309 if (hd2 >= '0' && hd2 <= '9') {
310 hexValue += hd2 - '0';
311 } else if (hd2 >= 'A' && hd2 <= 'F') {
312 hexValue += hd2 - 'A' + 10;
313 } else if (hd2 >= 'a' && hd2 <= 'f') {
314 hexValue += hd2 - 'a' + 10;
315 } else {
316 return -1;
318 return hexValue;
321 constexpr int isinset(const char *ap, unsigned char c) noexcept {
322 return ap[c >> 3] & (1 << (c & BITIND));
328 * Called when the parser finds a backslash not followed
329 * by a valid expression (like \( in non-Posix mode).
330 * @param pattern : pointer on the char after the backslash.
331 * @param incr : (out) number of chars to skip after expression evaluation.
332 * @return the char if it resolves to a simple char,
333 * or -1 for a char class. In this case, bittab is changed.
335 int RESearch::GetBackslashExpression(const char *pattern, int &incr) noexcept {
336 // Since error reporting is primitive and messages are not used anyway,
337 // I choose to interpret unexpected syntax in a logical way instead
338 // of reporting errors. Otherwise, we can stick on, eg., PCRE behaviour.
339 incr = 0; // Most of the time, will skip the char "naturally".
340 int result = -1;
341 const unsigned char bsc = *pattern;
342 if (!bsc) {
343 // Avoid overrun
344 result = '\\'; // \ at end of pattern, take it literally
345 return result;
348 switch (bsc) {
349 case 'a':
350 case 'b':
351 case 'n':
352 case 'f':
353 case 'r':
354 case 't':
355 case 'v':
356 result = escapeValue(bsc);
357 break;
358 case 'x': {
359 const unsigned char hd1 = *(pattern + 1);
360 const unsigned char hd2 = *(pattern + 2);
361 const int hexValue = GetHexaChar(hd1, hd2);
362 if (hexValue >= 0) {
363 result = hexValue;
364 incr = 2; // Must skip the digits
365 } else {
366 result = 'x'; // \x without 2 digits: see it as 'x'
369 break;
370 case 'd':
371 for (int c = '0'; c <= '9'; c++) {
372 ChSet(static_cast<unsigned char>(c));
374 break;
375 case 'D':
376 for (int c = 0; c < MAXCHR; c++) {
377 if (c < '0' || c > '9') {
378 ChSet(static_cast<unsigned char>(c));
381 break;
382 case 's':
383 ChSet(' ');
384 ChSet('\t');
385 ChSet('\n');
386 ChSet('\r');
387 ChSet('\f');
388 ChSet('\v');
389 break;
390 case 'S':
391 for (int c = 0; c < MAXCHR; c++) {
392 if (c != ' ' && !(c >= 0x09 && c <= 0x0D)) {
393 ChSet(static_cast<unsigned char>(c));
396 break;
397 case 'w':
398 for (int c = 0; c < MAXCHR; c++) {
399 if (iswordc(static_cast<unsigned char>(c))) {
400 ChSet(static_cast<unsigned char>(c));
403 break;
404 case 'W':
405 for (int c = 0; c < MAXCHR; c++) {
406 if (!iswordc(static_cast<unsigned char>(c))) {
407 ChSet(static_cast<unsigned char>(c));
410 break;
411 default:
412 result = bsc;
414 return result;
417 const char *RESearch::Compile(const char *pattern, Sci::Position length, bool caseSensitive, bool posix) {
418 if (!pattern || !length) {
419 if (sta)
420 return nullptr;
421 else
422 return badpat("No previous regular expression");
425 bittab.fill(0);
426 nfa[0] = END;
428 char *mp=nfa; /* nfa pointer */
429 char *sp=nfa; /* another saved pointer */
430 const char * const mpMax = mp + MAXNFA - BITBLK - 10;
432 int tagstk[MAXTAG]{}; /* subpat tag stack */
433 int tagi = 0; /* tag stack index */
434 int tagc = 1; /* actual tag count */
436 sta = NOP;
438 const char *p=pattern; /* pattern pointer */
439 for (int i=0; i<length; i++, p++) {
440 if (mp > mpMax)
441 return badpat("Pattern too long");
442 char *lp = mp; /* saved pointer */
443 switch (*p) {
445 case '.': /* match any char */
446 *mp++ = ANY;
447 break;
449 case '^': /* match beginning */
450 if (p == pattern) {
451 *mp++ = BOL;
452 } else {
453 *mp++ = CHR;
454 *mp++ = *p;
456 break;
458 case '$': /* match endofline */
459 if (!p[1]) {
460 *mp++ = EOL;
461 } else {
462 *mp++ = CHR;
463 *mp++ = *p;
465 break;
467 case '[': { /* match char class */
468 int prevChar = 0;
469 char mask = 0; /* xor mask -CCL/NCL */
471 i++;
472 if (*++p == '^') {
473 mask = '\377';
474 i++;
475 p++;
478 if (*p == '-') { /* real dash */
479 i++;
480 prevChar = *p;
481 ChSet(*p++);
483 if (*p == ']') { /* real brace */
484 i++;
485 prevChar = *p;
486 ChSet(*p++);
488 while (*p && *p != ']') {
489 if (*p == '-') {
490 if (prevChar < 0) {
491 // Previous def. was a char class like \d, take dash literally
492 prevChar = *p;
493 ChSet(*p);
494 } else if (p[1]) {
495 if (p[1] != ']') {
496 int c1 = prevChar + 1;
497 i++;
498 int c2 = static_cast<unsigned char>(*++p);
499 if (c2 == '\\') {
500 if (!p[1]) { // End of RE
501 return badpat("Missing ]");
502 } else {
503 i++;
504 p++;
505 int incr = 0;
506 c2 = GetBackslashExpression(p, incr);
507 i += incr;
508 p += incr;
509 if (c2 >= 0) {
510 // Convention: \c (c is any char) is case sensitive, whatever the option
511 ChSet(static_cast<unsigned char>(c2));
512 prevChar = c2;
513 } else {
514 // bittab is already changed
515 prevChar = -1;
519 if (prevChar < 0) {
520 // Char after dash is char class like \d, take dash literally
521 prevChar = '-';
522 ChSet('-');
523 } else {
524 // Put all chars between c1 and c2 included in the char set
525 while (c1 <= c2) {
526 ChSetWithCase(static_cast<unsigned char>(c1++), caseSensitive);
529 } else {
530 // Dash before the ], take it literally
531 prevChar = *p;
532 ChSet(*p);
534 } else {
535 return badpat("Missing ]");
537 } else if (*p == '\\' && p[1]) {
538 i++;
539 p++;
540 int incr = 0;
541 const int c = GetBackslashExpression(p, incr);
542 i += incr;
543 p += incr;
544 if (c >= 0) {
545 // Convention: \c (c is any char) is case sensitive, whatever the option
546 ChSet(static_cast<unsigned char>(c));
547 prevChar = c;
548 } else {
549 // bittab is already changed
550 prevChar = -1;
552 } else {
553 prevChar = static_cast<unsigned char>(*p);
554 ChSetWithCase(*p, caseSensitive);
556 i++;
557 p++;
559 if (!*p)
560 return badpat("Missing ]");
562 *mp++ = CCL;
563 for (const unsigned char byte : bittab) {
564 *mp++ = mask ^ byte;
566 bittab.fill(0);
567 } break;
569 case '*': /* match 0 or more... */
570 case '+': /* match 1 or more... */
571 case '?':
572 if (p == pattern)
573 return badpat("Empty closure");
574 lp = sp; /* previous opcode */
575 if (*lp == CLO || *lp == LCLO) /* equivalence... */
576 break;
577 switch (*lp) {
579 case BOL:
580 case BOT:
581 case EOT:
582 case BOW:
583 case EOW:
584 case REF:
585 return badpat("Illegal closure");
586 default:
587 break;
590 if (*p == '+')
591 for (sp = mp; lp < sp; lp++)
592 *mp++ = *lp;
594 *mp++ = END;
595 *mp++ = END;
596 sp = mp;
598 while (--mp > lp)
599 *mp = mp[-1];
600 if (*p == '?') *mp = CLQ;
601 else if (p[1] == '?') *mp = LCLO;
602 else *mp = CLO;
604 mp = sp;
605 break;
607 case '\\': /* tags, backrefs... */
608 i++;
609 switch (*++p) {
610 case '<':
611 *mp++ = BOW;
612 break;
613 case '>':
614 if (*sp == BOW)
615 return badpat("Null pattern inside \\<\\>");
616 *mp++ = EOW;
617 break;
618 case '1':
619 case '2':
620 case '3':
621 case '4':
622 case '5':
623 case '6':
624 case '7':
625 case '8':
626 case '9': {
627 const int n = *p-'0';
628 if (tagi > 0 && tagstk[tagi] == n)
629 return badpat("Cyclical reference");
630 if (tagc > n) {
631 *mp++ = REF;
632 *mp++ = static_cast<char>(n);
633 } else {
634 return badpat("Undetermined reference");
636 } break;
637 default:
638 if (!posix && *p == '(') {
639 if (tagc < MAXTAG) {
640 tagstk[++tagi] = tagc;
641 *mp++ = BOT;
642 *mp++ = static_cast<char>(tagc++);
643 } else {
644 return badpat("Too many \\(\\) pairs");
646 } else if (!posix && *p == ')') {
647 if (*sp == BOT)
648 return badpat("Null pattern inside \\(\\)");
649 if (tagi > 0) {
650 *mp++ = EOT;
651 *mp++ = static_cast<char>(tagstk[tagi--]);
652 } else {
653 return badpat("Unmatched \\)");
655 } else {
656 int incr = 0;
657 const int c = GetBackslashExpression(p, incr);
658 i += incr;
659 p += incr;
660 if (c >= 0) {
661 *mp++ = CHR;
662 *mp++ = static_cast<unsigned char>(c);
663 } else {
664 *mp++ = CCL;
665 for (const unsigned char byte : bittab) {
666 *mp++ = byte;
668 bittab.fill(0);
672 break;
674 default : /* an ordinary char */
675 if (posix && *p == '(') {
676 if (tagc < MAXTAG) {
677 tagstk[++tagi] = tagc;
678 *mp++ = BOT;
679 *mp++ = static_cast<char>(tagc++);
680 } else {
681 return badpat("Too many () pairs");
683 } else if (posix && *p == ')') {
684 if (*sp == BOT)
685 return badpat("Null pattern inside ()");
686 if (tagi > 0) {
687 *mp++ = EOT;
688 *mp++ = static_cast<char>(tagstk[tagi--]);
689 } else {
690 return badpat("Unmatched )");
692 } else {
693 unsigned char c = *p;
694 if (!c) // End of RE
695 c = '\\'; // We take it as raw backslash
696 if (caseSensitive || !iswordc(c)) {
697 *mp++ = CHR;
698 *mp++ = c;
699 } else {
700 ChSetWithCase(c, false);
701 *mp++ = CCL;
702 for (const unsigned char byte : bittab) {
703 *mp++ = byte;
705 bittab.fill(0);
708 break;
710 sp = lp;
712 if (tagi > 0)
713 return badpat((posix ? "Unmatched (" : "Unmatched \\("));
714 *mp = END;
715 sta = OKP;
716 return nullptr;
720 * RESearch::Execute:
721 * execute nfa to find a match.
723 * special cases: (nfa[0])
724 * BOL
725 * Match only once, starting from the
726 * beginning.
727 * CHR
728 * First locate the character without
729 * calling PMatch, and if found, call
730 * PMatch for the remaining string.
731 * END
732 * RESearch::Compile failed, poor luser did not
733 * check for it. Fail fast.
735 * If a match is found, bopat[0] and eopat[0] are set
736 * to the beginning and the end of the matched fragment,
737 * respectively.
740 int RESearch::Execute(const CharacterIndexer &ci, Sci::Position lp, Sci::Position endp) {
741 Sci::Position ep = NOTFOUND;
742 const char * const ap = nfa;
744 failure = 0;
746 Clear();
748 switch (*ap) {
750 case BOL: /* anchored: match from BOL only */
751 ep = PMatch(ci, lp, endp, ap);
752 break;
753 case EOL: /* just searching for end of line normal path doesn't work */
754 if (endp == lineEndPos && ap[1] == END) {
755 lp = endp;
756 ep = lp;
757 break;
758 } else {
759 return 0;
761 case CHR: { /* ordinary char: locate it fast */
762 const unsigned char c = ap[1];
763 while ((lp < endp) && (static_cast<unsigned char>(ci.CharAt(lp)) != c))
764 lp++;
765 if (lp >= endp) /* if EOS, fail, else fall through. */
766 return 0;
768 [[fallthrough]];
769 default: /* regular matching all the way. */
770 while (lp < endp) {
771 ep = PMatch(ci, lp, endp, ap);
772 if (ep != NOTFOUND) {
773 // fix match started from middle of character like DBCS trailing ASCII byte
774 const Sci::Position pos = ci.MovePositionOutsideChar(lp, -1);
775 if (pos != lp) {
776 ep = NOTFOUND;
777 } else {
778 break;
781 lp++;
783 break;
784 case END: /* munged automaton. fail always */
785 return 0;
787 if (ep == NOTFOUND) {
788 /* similar to EOL, match EOW at line end */
789 if (endp == lineEndPos && *ap == EOW) {
790 if ((ap[1] == END || ((ap[1] == EOL && ap[2] == END))) && iswordc(ci.CharAt(lp - 1))) {
791 lp = endp;
792 ep = lp;
793 } else {
794 return 0;
796 } else {
797 return 0;
801 ep = ci.MovePositionOutsideChar(ep, 1);
802 bopat[0] = lp;
803 eopat[0] = ep;
804 return 1;
808 * PMatch: internal routine for the hard part
810 * This code is partly snarfed from an early grep written by
811 * David Conroy. The backref and tag stuff, and various other
812 * innovations are by oz.
814 * special case optimizations: (nfa[n], nfa[n+1])
815 * CLO ANY
816 * We KNOW .* will match everything up to the
817 * end of line. Thus, directly go to the end of
818 * line, without recursive PMatch calls. As in
819 * the other closure cases, the remaining pattern
820 * must be matched by moving backwards on the
821 * string recursively, to find a match for xy
822 * (x is ".*" and y is the remaining pattern)
823 * where the match satisfies the LONGEST match for
824 * x followed by a match for y.
825 * CLO CHR
826 * We can again scan the string forward for the
827 * single char and at the point of failure, we
828 * execute the remaining nfa recursively, same as
829 * above.
831 * At the end of a successful match, bopat[n] and eopat[n]
832 * are set to the beginning and end of subpatterns matched
833 * by tagged expressions (n = 1 to 9).
836 //extern void re_fail(char *,char);
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 Sci::Position RESearch::PMatch(const CharacterIndexer &ci, Sci::Position lp, Sci::Position endp, const char *ap) {
847 unsigned char op = 0;
849 while ((op = *ap++) != END)
850 switch (op) {
852 case CHR:
853 if (ci.CharAt(lp++) != *ap++)
854 return NOTFOUND;
855 break;
856 case ANY:
857 if (lp++ >= endp)
858 return NOTFOUND;
859 break;
860 case CCL:
861 if (lp >= endp)
862 return NOTFOUND;
863 if (!isinset(ap, ci.CharAt(lp++)))
864 return NOTFOUND;
865 ap += BITBLK;
866 break;
867 case BOL:
868 if (lp != lineStartPos)
869 return NOTFOUND;
870 break;
871 case EOL:
872 if (lp < lineEndPos)
873 return NOTFOUND;
874 break;
875 case BOT:
876 if (lp != ci.MovePositionOutsideChar(lp, -1)) {
877 return NOTFOUND;
879 bopat[static_cast<unsigned char>(*ap++)] = lp;
880 break;
881 case EOT:
882 lp = ci.MovePositionOutsideChar(lp, 1);
883 eopat[static_cast<unsigned char>(*ap++)] = lp;
884 break;
885 case BOW:
886 if ((lp!=lineStartPos && iswordc(ci.CharAt(lp-1))) || !iswordc(ci.CharAt(lp)))
887 return NOTFOUND;
888 break;
889 case EOW:
890 if (lp==lineStartPos || !iswordc(ci.CharAt(lp-1)) || iswordc(ci.CharAt(lp)))
891 return NOTFOUND;
892 break;
893 case REF: {
894 const int n = static_cast<unsigned char>(*ap++);
895 Sci::Position bp = bopat[n]; /* beginning of subpat... */
896 const Sci::Position ep = eopat[n]; /* ending of subpat... */
897 while (bp < ep)
898 if (ci.CharAt(bp++) != ci.CharAt(lp++))
899 return NOTFOUND;
900 } break;
901 case LCLO:
902 case CLQ:
903 case CLO: {
904 int n = 0;
905 const Sci::Position are = lp; /* to save the line ptr. */
906 switch (*ap) {
908 case ANY:
909 if (op == CLO || op == LCLO)
910 while (lp < endp)
911 lp++;
912 else if (lp < endp)
913 lp++;
915 n = ANYSKIP;
916 break;
917 case CHR: {
918 const char c = ap[1];
919 if (op == CLO || op == LCLO)
920 while ((lp < endp) && (c == ci.CharAt(lp)))
921 lp++;
922 else if ((lp < endp) && (c == ci.CharAt(lp)))
923 lp++;
924 n = CHRSKIP;
925 } break;
926 case CCL:
927 while ((lp < endp) && isinset(ap+1, ci.CharAt(lp)))
928 lp++;
929 n = CCLSKIP;
930 break;
931 default:
932 failure = true;
933 //re_fail("closure: bad nfa.", *ap);
934 return NOTFOUND;
936 ap += n;
938 Sci::Position llp = lp; /* lazy lp for LCLO */
939 Sci::Position e = NOTFOUND; /* extra pointer for CLO */
940 while (llp >= are) {
941 const Sci::Position q = PMatch(ci, llp, endp, ap);
942 if (q != NOTFOUND) {
943 e = q;
944 lp = llp;
945 if (op != LCLO) return e;
947 if (*ap == END) return e;
948 --llp;
950 if (*ap == EOT)
951 PMatch(ci, lp, endp, ap);
952 return e;
954 default:
955 //re_fail("RESearch::Execute: bad nfa.", static_cast<char>(op));
956 return NOTFOUND;
958 return lp;