blob: 0a6dded827068bbca50396fb665364be1b6be046 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
32/* Upper limit allowed for {m,n} repetitions handled by NFA */
33#define NFA_BRACES_MAXLIMIT 10
34/* For allocating space for the postfix representation */
35#define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020036
37enum
38{
39 NFA_SPLIT = -1024,
40 NFA_MATCH,
41 NFA_SKIP_CHAR, /* matches a 0-length char */
42 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
43
44 NFA_CONCAT,
45 NFA_OR,
46 NFA_STAR,
47 NFA_PLUS,
48 NFA_QUEST,
49 NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */
50 NFA_NOT, /* used for [^ab] negated char ranges */
51
52 NFA_BOL, /* ^ Begin line */
53 NFA_EOL, /* $ End line */
54 NFA_BOW, /* \< Begin word */
55 NFA_EOW, /* \> End word */
56 NFA_BOF, /* \%^ Begin file */
57 NFA_EOF, /* \%$ End file */
58 NFA_NEWL,
59 NFA_ZSTART, /* Used for \zs */
60 NFA_ZEND, /* Used for \ze */
61 NFA_NOPEN, /* Start of subexpression marked with \%( */
62 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
63 NFA_START_INVISIBLE,
64 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020065 NFA_COMPOSING, /* Next nodes in NFA are part of the
66 composing multibyte char */
67 NFA_END_COMPOSING, /* End of a composing char in the NFA */
68
69 /* The following are used only in the postfix form, not in the NFA */
70 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
71 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
72 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
73 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
74 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
75
76 NFA_MOPEN,
77 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
78
79 /* NFA_FIRST_NL */
80 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
81 NFA_ANYOF, /* Match any character in this string. */
82 NFA_ANYBUT, /* Match any character not in this string. */
83 NFA_IDENT, /* Match identifier char */
84 NFA_SIDENT, /* Match identifier char but no digit */
85 NFA_KWORD, /* Match keyword char */
86 NFA_SKWORD, /* Match word char but no digit */
87 NFA_FNAME, /* Match file name char */
88 NFA_SFNAME, /* Match file name char but no digit */
89 NFA_PRINT, /* Match printable char */
90 NFA_SPRINT, /* Match printable char but no digit */
91 NFA_WHITE, /* Match whitespace char */
92 NFA_NWHITE, /* Match non-whitespace char */
93 NFA_DIGIT, /* Match digit char */
94 NFA_NDIGIT, /* Match non-digit char */
95 NFA_HEX, /* Match hex char */
96 NFA_NHEX, /* Match non-hex char */
97 NFA_OCTAL, /* Match octal char */
98 NFA_NOCTAL, /* Match non-octal char */
99 NFA_WORD, /* Match word char */
100 NFA_NWORD, /* Match non-word char */
101 NFA_HEAD, /* Match head char */
102 NFA_NHEAD, /* Match non-head char */
103 NFA_ALPHA, /* Match alpha char */
104 NFA_NALPHA, /* Match non-alpha char */
105 NFA_LOWER, /* Match lowercase char */
106 NFA_NLOWER, /* Match non-lowercase char */
107 NFA_UPPER, /* Match uppercase char */
108 NFA_NUPPER, /* Match non-uppercase char */
109 NFA_FIRST_NL = NFA_ANY + ADD_NL,
110 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
111
112 /* Character classes [:alnum:] etc */
113 NFA_CLASS_ALNUM,
114 NFA_CLASS_ALPHA,
115 NFA_CLASS_BLANK,
116 NFA_CLASS_CNTRL,
117 NFA_CLASS_DIGIT,
118 NFA_CLASS_GRAPH,
119 NFA_CLASS_LOWER,
120 NFA_CLASS_PRINT,
121 NFA_CLASS_PUNCT,
122 NFA_CLASS_SPACE,
123 NFA_CLASS_UPPER,
124 NFA_CLASS_XDIGIT,
125 NFA_CLASS_TAB,
126 NFA_CLASS_RETURN,
127 NFA_CLASS_BACKSPACE,
128 NFA_CLASS_ESCAPE
129};
130
131/* Keep in sync with classchars. */
132static int nfa_classcodes[] = {
133 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
134 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
135 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
136 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
137 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
138 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
139 NFA_UPPER, NFA_NUPPER
140};
141
142static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
143
144/*
145 * NFA errors can be of 3 types:
146 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
147 * silently and revert the to backtracking engine.
148 * syntax_error = FALSE;
149 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
150 * The NFA engine displays an error message, and nothing else happens.
151 * syntax_error = TRUE
152 * *** Unsupported features, when the input regexp uses an operator that is not
153 * implemented in the NFA. The NFA engine fails silently, and reverts to the
154 * old backtracking engine.
155 * syntax_error = FALSE
156 * "The NFA fails" means that "compiling the regexp with the NFA fails":
157 * nfa_regcomp() returns FAIL.
158 */
159static int syntax_error = FALSE;
160
161/* NFA regexp \ze operator encountered. */
162static int nfa_has_zend = FALSE;
163
164static int *post_start; /* holds the postfix form of r.e. */
165static int *post_end;
166static int *post_ptr;
167
168static int nstate; /* Number of states in the NFA. */
169static int istate; /* Index in the state vector, used in new_state() */
170static int nstate_max; /* Upper bound of estimated number of states. */
171
172
173static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
174static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
175static int nfa_emit_equi_class __ARGS((int c, int neg));
176static void nfa_inc __ARGS((char_u **p));
177static void nfa_dec __ARGS((char_u **p));
178static int nfa_regatom __ARGS((void));
179static int nfa_regpiece __ARGS((void));
180static int nfa_regconcat __ARGS((void));
181static int nfa_regbranch __ARGS((void));
182static int nfa_reg __ARGS((int paren));
183#ifdef DEBUG
184static void nfa_set_code __ARGS((int c));
185static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200186static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
187static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200188static void nfa_dump __ARGS((nfa_regprog_T *prog));
189#endif
190static int *re2post __ARGS((void));
191static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
192static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
193static int check_char_class __ARGS((int class, int c));
194static void st_error __ARGS((int *postfix, int *end, int *p));
195static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
196static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
197static void nfa_set_null_listids __ARGS((nfa_state_T *start));
198static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
199static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
200static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
201static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
202static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
203static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
204
205/* helper functions used when doing re2post() ... regatom() parsing */
206#define EMIT(c) do { \
207 if (post_ptr >= post_end) \
208 return FAIL; \
209 *post_ptr++ = c; \
210 } while (0)
211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200212/*
213 * Initialize internal variables before NFA compilation.
214 * Return OK on success, FAIL otherwise.
215 */
216 static int
217nfa_regcomp_start(expr, re_flags)
218 char_u *expr;
219 int re_flags; /* see vim_regcomp() */
220{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200221 size_t postfix_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200222
223 nstate = 0;
224 istate = 0;
225 /* A reasonable estimation for size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200226 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200227
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200228 /* Some items blow up in size, such as [A-z]. Add more space for that.
229 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200230 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200231
232 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200234
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200235 post_start = (int *)lalloc(postfix_size, TRUE);
236 if (post_start == NULL)
237 return FAIL;
238 vim_memset(post_start, 0, postfix_size);
239 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200240 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200241 nfa_has_zend = FALSE;
242
243 regcomp_start(expr, re_flags);
244
245 return OK;
246}
247
248/*
249 * Search between "start" and "end" and try to recognize a
250 * character class in expanded form. For example [0-9].
251 * On success, return the id the character class to be emitted.
252 * On failure, return 0 (=FAIL)
253 * Start points to the first char of the range, while end should point
254 * to the closing brace.
255 */
256 static int
257nfa_recognize_char_class(start, end, extra_newl)
258 char_u *start;
259 char_u *end;
260 int extra_newl;
261{
262 int i;
263 /* Each of these variables takes up a char in "config[]",
264 * in the order they are here. */
265 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
266 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
267 char_u *p;
268#define NCONFIGS 16
269 int classid[NCONFIGS] = {
270 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
271 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
272 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
273 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
274 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200275 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276 char_u config[NCONFIGS][9] = {
277 "000000100", /* digit */
278 "100000100", /* non digit */
279 "011000100", /* hex-digit */
280 "111000100", /* non hex-digit */
281 "000001000", /* octal-digit */
282 "100001000", /* [^0-7] */
283 "000110110", /* [0-9A-Za-z_] */
284 "100110110", /* [^0-9A-Za-z_] */
285 "000110010", /* head of word */
286 "100110010", /* not head of word */
287 "000110000", /* alphabetic char a-z */
288 "100110000", /* non alphabetic char */
289 "000100000", /* lowercase letter */
290 "100100000", /* non lowercase */
291 "000010000", /* uppercase */
292 "100010000" /* non uppercase */
293 };
294
295 if (extra_newl == TRUE)
296 newl = TRUE;
297
298 if (*end != ']')
299 return FAIL;
300 p = start;
301 if (*p == '^')
302 {
303 not = TRUE;
304 p ++;
305 }
306
307 while (p < end)
308 {
309 if (p + 2 < end && *(p + 1) == '-')
310 {
311 switch (*p)
312 {
313 case '0':
314 if (*(p + 2) == '9')
315 {
316 o9 = TRUE;
317 break;
318 }
319 else
320 if (*(p + 2) == '7')
321 {
322 o7 = TRUE;
323 break;
324 }
325 case 'a':
326 if (*(p + 2) == 'z')
327 {
328 az = TRUE;
329 break;
330 }
331 else
332 if (*(p + 2) == 'f')
333 {
334 af = TRUE;
335 break;
336 }
337 case 'A':
338 if (*(p + 2) == 'Z')
339 {
340 AZ = TRUE;
341 break;
342 }
343 else
344 if (*(p + 2) == 'F')
345 {
346 AF = TRUE;
347 break;
348 }
349 /* FALLTHROUGH */
350 default:
351 return FAIL;
352 }
353 p += 3;
354 }
355 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
356 {
357 newl = TRUE;
358 p += 2;
359 }
360 else if (*p == '_')
361 {
362 underscore = TRUE;
363 p ++;
364 }
365 else if (*p == '\n')
366 {
367 newl = TRUE;
368 p ++;
369 }
370 else
371 return FAIL;
372 } /* while (p < end) */
373
374 if (p != end)
375 return FAIL;
376
377 /* build the config that represents the ranges we gathered */
378 STRCPY(myconfig, "000000000");
379 if (not == TRUE)
380 myconfig[0] = '1';
381 if (af == TRUE)
382 myconfig[1] = '1';
383 if (AF == TRUE)
384 myconfig[2] = '1';
385 if (az == TRUE)
386 myconfig[3] = '1';
387 if (AZ == TRUE)
388 myconfig[4] = '1';
389 if (o7 == TRUE)
390 myconfig[5] = '1';
391 if (o9 == TRUE)
392 myconfig[6] = '1';
393 if (underscore == TRUE)
394 myconfig[7] = '1';
395 if (newl == TRUE)
396 {
397 myconfig[8] = '1';
398 extra_newl = ADD_NL;
399 }
400 /* try to recognize character classes */
401 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200402 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200403 return classid[i] + extra_newl;
404
405 /* fallthrough => no success so far */
406 return FAIL;
407
408#undef NCONFIGS
409}
410
411/*
412 * Produce the bytes for equivalence class "c".
413 * Currently only handles latin1, latin9 and utf-8.
414 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
415 * equivalent to 'a OR b OR c'
416 *
417 * NOTE! When changing this function, also update reg_equi_class()
418 */
419 static int
420nfa_emit_equi_class(c, neg)
421 int c;
422 int neg;
423{
424 int first = TRUE;
425 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
426#define EMIT2(c) \
427 EMIT(c); \
428 if (neg == TRUE) { \
429 EMIT(NFA_NOT); \
430 } \
431 if (first == FALSE) \
432 EMIT(glue); \
433 else \
434 first = FALSE; \
435
436#ifdef FEAT_MBYTE
437 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
438 || STRCMP(p_enc, "iso-8859-15") == 0)
439#endif
440 {
441 switch (c)
442 {
443 case 'A': case '\300': case '\301': case '\302':
444 case '\303': case '\304': case '\305':
445 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
446 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
447 EMIT2('\305');
448 return OK;
449
450 case 'C': case '\307':
451 EMIT2('C'); EMIT2('\307');
452 return OK;
453
454 case 'E': case '\310': case '\311': case '\312': case '\313':
455 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
456 EMIT2('\312'); EMIT2('\313');
457 return OK;
458
459 case 'I': case '\314': case '\315': case '\316': case '\317':
460 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
461 EMIT2('\316'); EMIT2('\317');
462 return OK;
463
464 case 'N': case '\321':
465 EMIT2('N'); EMIT2('\321');
466 return OK;
467
468 case 'O': case '\322': case '\323': case '\324': case '\325':
469 case '\326':
470 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
471 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
472 return OK;
473
474 case 'U': case '\331': case '\332': case '\333': case '\334':
475 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
476 EMIT2('\333'); EMIT2('\334');
477 return OK;
478
479 case 'Y': case '\335':
480 EMIT2('Y'); EMIT2('\335');
481 return OK;
482
483 case 'a': case '\340': case '\341': case '\342':
484 case '\343': case '\344': case '\345':
485 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
486 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
487 EMIT2('\345');
488 return OK;
489
490 case 'c': case '\347':
491 EMIT2('c'); EMIT2('\347');
492 return OK;
493
494 case 'e': case '\350': case '\351': case '\352': case '\353':
495 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
496 EMIT2('\352'); EMIT2('\353');
497 return OK;
498
499 case 'i': case '\354': case '\355': case '\356': case '\357':
500 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
501 EMIT2('\356'); EMIT2('\357');
502 return OK;
503
504 case 'n': case '\361':
505 EMIT2('n'); EMIT2('\361');
506 return OK;
507
508 case 'o': case '\362': case '\363': case '\364': case '\365':
509 case '\366':
510 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
511 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
512 return OK;
513
514 case 'u': case '\371': case '\372': case '\373': case '\374':
515 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
516 EMIT2('\373'); EMIT2('\374');
517 return OK;
518
519 case 'y': case '\375': case '\377':
520 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
521 return OK;
522
523 default:
524 return FAIL;
525 }
526 }
527
528 EMIT(c);
529 return OK;
530#undef EMIT2
531}
532
533/*
534 * Code to parse regular expression.
535 *
536 * We try to reuse parsing functions in regexp.c to
537 * minimize surprise and keep the syntax consistent.
538 */
539
540/*
541 * Increments the pointer "p" by one (multi-byte) character.
542 */
543 static void
544nfa_inc(p)
545 char_u **p;
546{
547#ifdef FEAT_MBYTE
548 if (has_mbyte)
549 mb_ptr2char_adv(p);
550 else
551#endif
552 *p = *p + 1;
553}
554
555/*
556 * Decrements the pointer "p" by one (multi-byte) character.
557 */
558 static void
559nfa_dec(p)
560 char_u **p;
561{
562#ifdef FEAT_MBYTE
563 char_u *p2, *oldp;
564
565 if (has_mbyte)
566 {
567 oldp = *p;
568 /* Try to find the multibyte char that advances to the current
569 * position. */
570 do
571 {
572 *p = *p - 1;
573 p2 = *p;
574 mb_ptr2char_adv(&p2);
575 } while (p2 != oldp);
576 }
577#else
578 *p = *p - 1;
579#endif
580}
581
582/*
583 * Parse the lowest level.
584 *
585 * An atom can be one of a long list of items. Many atoms match one character
586 * in the text. It is often an ordinary character or a character class.
587 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
588 * is only for syntax highlighting.
589 *
590 * atom ::= ordinary-atom
591 * or \( pattern \)
592 * or \%( pattern \)
593 * or \z( pattern \)
594 */
595 static int
596nfa_regatom()
597{
598 int c;
599 int charclass;
600 int equiclass;
601 int collclass;
602 int got_coll_char;
603 char_u *p;
604 char_u *endp;
605#ifdef FEAT_MBYTE
606 char_u *old_regparse = regparse;
607 int clen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200608 int i;
609#endif
610 int extra = 0;
611 int first;
612 int emit_range;
613 int negated;
614 int result;
615 int startc = -1;
616 int endc = -1;
617 int oldstartc = -1;
618 int cpo_lit; /* 'cpoptions' contains 'l' flag */
619 int cpo_bsl; /* 'cpoptions' contains '\' flag */
620 int glue; /* ID that will "glue" nodes together */
621
622 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
623 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
624
625 c = getchr();
626
627#ifdef FEAT_MBYTE
628 /* clen has the length of the current char, without composing chars */
629 clen = (*mb_char2len)(c);
630 if (has_mbyte && clen > 1)
631 goto nfa_do_multibyte;
632#endif
633 switch (c)
634 {
635 case Magic('^'):
636 EMIT(NFA_BOL);
637 break;
638
639 case Magic('$'):
640 EMIT(NFA_EOL);
641#if defined(FEAT_SYN_HL) || defined(PROTO)
642 had_eol = TRUE;
643#endif
644 break;
645
646 case Magic('<'):
647 EMIT(NFA_BOW);
648 break;
649
650 case Magic('>'):
651 EMIT(NFA_EOW);
652 break;
653
654 case Magic('_'):
655 c = no_Magic(getchr());
656 if (c == '^') /* "\_^" is start-of-line */
657 {
658 EMIT(NFA_BOL);
659 break;
660 }
661 if (c == '$') /* "\_$" is end-of-line */
662 {
663 EMIT(NFA_EOL);
664#if defined(FEAT_SYN_HL) || defined(PROTO)
665 had_eol = TRUE;
666#endif
667 break;
668 }
669
670 extra = ADD_NL;
671
672 /* "\_[" is collection plus newline */
673 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200674 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675
676 /* "\_x" is character class plus newline */
677 /*FALLTHROUGH*/
678
679 /*
680 * Character classes.
681 */
682 case Magic('.'):
683 case Magic('i'):
684 case Magic('I'):
685 case Magic('k'):
686 case Magic('K'):
687 case Magic('f'):
688 case Magic('F'):
689 case Magic('p'):
690 case Magic('P'):
691 case Magic('s'):
692 case Magic('S'):
693 case Magic('d'):
694 case Magic('D'):
695 case Magic('x'):
696 case Magic('X'):
697 case Magic('o'):
698 case Magic('O'):
699 case Magic('w'):
700 case Magic('W'):
701 case Magic('h'):
702 case Magic('H'):
703 case Magic('a'):
704 case Magic('A'):
705 case Magic('l'):
706 case Magic('L'):
707 case Magic('u'):
708 case Magic('U'):
709 p = vim_strchr(classchars, no_Magic(c));
710 if (p == NULL)
711 {
712 return FAIL; /* runtime error */
713 }
714#ifdef FEAT_MBYTE
715 /* When '.' is followed by a composing char ignore the dot, so that
716 * the composing char is matched here. */
717 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
718 {
719 c = getchr();
720 goto nfa_do_multibyte;
721 }
722#endif
723 EMIT(nfa_classcodes[p - classchars]);
724 if (extra == ADD_NL)
725 {
726 EMIT(NFA_NEWL);
727 EMIT(NFA_OR);
728 regflags |= RF_HASNL;
729 }
730 break;
731
732 case Magic('n'):
733 if (reg_string)
734 /* In a string "\n" matches a newline character. */
735 EMIT(NL);
736 else
737 {
738 /* In buffer text "\n" matches the end of a line. */
739 EMIT(NFA_NEWL);
740 regflags |= RF_HASNL;
741 }
742 break;
743
744 case Magic('('):
745 if (nfa_reg(REG_PAREN) == FAIL)
746 return FAIL; /* cascaded error */
747 break;
748
749 case NUL:
750 syntax_error = TRUE;
751 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
752
753 case Magic('|'):
754 case Magic('&'):
755 case Magic(')'):
756 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200757 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200758 return FAIL;
759
760 case Magic('='):
761 case Magic('?'):
762 case Magic('+'):
763 case Magic('@'):
764 case Magic('*'):
765 case Magic('{'):
766 /* these should follow an atom, not form an atom */
767 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200768 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200769 return FAIL;
770
771 case Magic('~'): /* previous substitute pattern */
772 /* Not supported yet */
773 return FAIL;
774
775 case Magic('1'):
776 case Magic('2'):
777 case Magic('3'):
778 case Magic('4'):
779 case Magic('5'):
780 case Magic('6'):
781 case Magic('7'):
782 case Magic('8'):
783 case Magic('9'):
784 /* not supported yet */
785 return FAIL;
786
787 case Magic('z'):
788 c = no_Magic(getchr());
789 switch (c)
790 {
791 case 's':
792 EMIT(NFA_ZSTART);
793 break;
794 case 'e':
795 EMIT(NFA_ZEND);
796 nfa_has_zend = TRUE;
797 /* TODO: Currently \ze does not work properly. */
798 return FAIL;
799 /* break; */
800 case '1':
801 case '2':
802 case '3':
803 case '4':
804 case '5':
805 case '6':
806 case '7':
807 case '8':
808 case '9':
809 case '(':
810 /* \z1...\z9 and \z( not yet supported */
811 return FAIL;
812 default:
813 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200814 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200815 no_Magic(c));
816 return FAIL;
817 }
818 break;
819
820 case Magic('%'):
821 c = no_Magic(getchr());
822 switch (c)
823 {
824 /* () without a back reference */
825 case '(':
826 if (nfa_reg(REG_NPAREN) == FAIL)
827 return FAIL;
828 EMIT(NFA_NOPEN);
829 break;
830
831 case 'd': /* %d123 decimal */
832 case 'o': /* %o123 octal */
833 case 'x': /* %xab hex 2 */
834 case 'u': /* %uabcd hex 4 */
835 case 'U': /* %U1234abcd hex 8 */
836 /* Not yet supported */
837 return FAIL;
838
839 c = coll_get_char();
Bram Moolenaar3c577f22013-05-24 21:59:54 +0200840 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200841 break;
842
843 /* Catch \%^ and \%$ regardless of where they appear in the
844 * pattern -- regardless of whether or not it makes sense. */
845 case '^':
846 EMIT(NFA_BOF);
847 /* Not yet supported */
848 return FAIL;
849 break;
850
851 case '$':
852 EMIT(NFA_EOF);
853 /* Not yet supported */
854 return FAIL;
855 break;
856
857 case '#':
858 /* not supported yet */
859 return FAIL;
860 break;
861
862 case 'V':
863 /* not supported yet */
864 return FAIL;
865 break;
866
867 case '[':
868 /* \%[abc] not supported yet */
869 return FAIL;
870
871 default:
872 /* not supported yet */
873 return FAIL;
874 }
875 break;
876
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200877 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200878collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200879 /*
880 * Glue is emitted between several atoms from the [].
881 * It is either NFA_OR, or NFA_CONCAT.
882 *
883 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
884 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
885 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
886 * notation)
887 *
888 */
889
890
891/* Emit negation atoms, if needed.
892 * The CONCAT below merges the NOT with the previous node. */
893#define TRY_NEG() \
894 if (negated == TRUE) \
895 { \
896 EMIT(NFA_NOT); \
897 }
898
899/* Emit glue between important nodes : CONCAT or OR. */
900#define EMIT_GLUE() \
901 if (first == FALSE) \
902 EMIT(glue); \
903 else \
904 first = FALSE;
905
906 p = regparse;
907 endp = skip_anyof(p);
908 if (*endp == ']')
909 {
910 /*
911 * Try to reverse engineer character classes. For example,
912 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
913 * and perform the necessary substitutions in the NFA.
914 */
915 result = nfa_recognize_char_class(regparse, endp,
916 extra == ADD_NL);
917 if (result != FAIL)
918 {
919 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
920 EMIT(result);
921 else /* must be char class + newline */
922 {
923 EMIT(result - ADD_NL);
924 EMIT(NFA_NEWL);
925 EMIT(NFA_OR);
926 }
927 regparse = endp;
928 nfa_inc(&regparse);
929 return OK;
930 }
931 /*
932 * Failed to recognize a character class. Use the simple
933 * version that turns [abc] into 'a' OR 'b' OR 'c'
934 */
935 startc = endc = oldstartc = -1;
936 first = TRUE; /* Emitting first atom in this sequence? */
937 negated = FALSE;
938 glue = NFA_OR;
939 if (*regparse == '^') /* negated range */
940 {
941 negated = TRUE;
942 glue = NFA_CONCAT;
943 nfa_inc(&regparse);
944 }
945 if (*regparse == '-')
946 {
947 startc = '-';
948 EMIT(startc);
949 TRY_NEG();
950 EMIT_GLUE();
951 nfa_inc(&regparse);
952 }
953 /* Emit the OR branches for each character in the [] */
954 emit_range = FALSE;
955 while (regparse < endp)
956 {
957 oldstartc = startc;
958 startc = -1;
959 got_coll_char = FALSE;
960 if (*regparse == '[')
961 {
962 /* Check for [: :], [= =], [. .] */
963 equiclass = collclass = 0;
964 charclass = get_char_class(&regparse);
965 if (charclass == CLASS_NONE)
966 {
967 equiclass = get_equi_class(&regparse);
968 if (equiclass == 0)
969 collclass = get_coll_element(&regparse);
970 }
971
972 /* Character class like [:alpha:] */
973 if (charclass != CLASS_NONE)
974 {
975 switch (charclass)
976 {
977 case CLASS_ALNUM:
978 EMIT(NFA_CLASS_ALNUM);
979 break;
980 case CLASS_ALPHA:
981 EMIT(NFA_CLASS_ALPHA);
982 break;
983 case CLASS_BLANK:
984 EMIT(NFA_CLASS_BLANK);
985 break;
986 case CLASS_CNTRL:
987 EMIT(NFA_CLASS_CNTRL);
988 break;
989 case CLASS_DIGIT:
990 EMIT(NFA_CLASS_DIGIT);
991 break;
992 case CLASS_GRAPH:
993 EMIT(NFA_CLASS_GRAPH);
994 break;
995 case CLASS_LOWER:
996 EMIT(NFA_CLASS_LOWER);
997 break;
998 case CLASS_PRINT:
999 EMIT(NFA_CLASS_PRINT);
1000 break;
1001 case CLASS_PUNCT:
1002 EMIT(NFA_CLASS_PUNCT);
1003 break;
1004 case CLASS_SPACE:
1005 EMIT(NFA_CLASS_SPACE);
1006 break;
1007 case CLASS_UPPER:
1008 EMIT(NFA_CLASS_UPPER);
1009 break;
1010 case CLASS_XDIGIT:
1011 EMIT(NFA_CLASS_XDIGIT);
1012 break;
1013 case CLASS_TAB:
1014 EMIT(NFA_CLASS_TAB);
1015 break;
1016 case CLASS_RETURN:
1017 EMIT(NFA_CLASS_RETURN);
1018 break;
1019 case CLASS_BACKSPACE:
1020 EMIT(NFA_CLASS_BACKSPACE);
1021 break;
1022 case CLASS_ESCAPE:
1023 EMIT(NFA_CLASS_ESCAPE);
1024 break;
1025 }
1026 TRY_NEG();
1027 EMIT_GLUE();
1028 continue;
1029 }
1030 /* Try equivalence class [=a=] and the like */
1031 if (equiclass != 0)
1032 {
1033 result = nfa_emit_equi_class(equiclass, negated);
1034 if (result == FAIL)
1035 {
1036 /* should never happen */
1037 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1038 }
1039 EMIT_GLUE();
1040 continue;
1041 }
1042 /* Try collating class like [. .] */
1043 if (collclass != 0)
1044 {
1045 startc = collclass; /* allow [.a.]-x as a range */
1046 /* Will emit the proper atom at the end of the
1047 * while loop. */
1048 }
1049 }
1050 /* Try a range like 'a-x' or '\t-z' */
1051 if (*regparse == '-')
1052 {
1053 emit_range = TRUE;
1054 startc = oldstartc;
1055 nfa_inc(&regparse);
1056 continue; /* reading the end of the range */
1057 }
1058
1059 /* Now handle simple and escaped characters.
1060 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1061 * accepts "\t", "\e", etc., but only when the 'l' flag in
1062 * 'cpoptions' is not included.
1063 * Posix doesn't recognize backslash at all.
1064 */
1065 if (*regparse == '\\'
1066 && !cpo_bsl
1067 && regparse + 1 <= endp
1068 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1069 || (!cpo_lit
1070 && vim_strchr(REGEXP_ABBR, regparse[1])
1071 != NULL)
1072 )
1073 )
1074 {
1075 nfa_inc(&regparse);
1076
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001077 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001078 startc = reg_string ? NL : NFA_NEWL;
1079 else
1080 if (*regparse == 'd'
1081 || *regparse == 'o'
1082 || *regparse == 'x'
1083 || *regparse == 'u'
1084 || *regparse == 'U'
1085 )
1086 {
1087 /* TODO(RE) This needs more testing */
1088 startc = coll_get_char();
1089 got_coll_char = TRUE;
1090 nfa_dec(&regparse);
1091 }
1092 else
1093 {
1094 /* \r,\t,\e,\b */
1095 startc = backslash_trans(*regparse);
1096 }
1097 }
1098
1099 /* Normal printable char */
1100 if (startc == -1)
1101#ifdef FEAT_MBYTE
1102 startc = (*mb_ptr2char)(regparse);
1103#else
1104 startc = *regparse;
1105#endif
1106
1107 /* Previous char was '-', so this char is end of range. */
1108 if (emit_range)
1109 {
1110 endc = startc; startc = oldstartc;
1111 if (startc > endc)
1112 EMSG_RET_FAIL(_(e_invrange));
1113#ifdef FEAT_MBYTE
1114 if (has_mbyte && ((*mb_char2len)(startc) > 1
1115 || (*mb_char2len)(endc) > 1))
1116 {
1117 if (endc > startc + 256)
1118 EMSG_RET_FAIL(_(e_invrange));
1119 /* Emit the range. "startc" was already emitted, so
1120 * skip it. */
1121 for (c = startc + 1; c <= endc; c++)
1122 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001123 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001124 TRY_NEG();
1125 EMIT_GLUE();
1126 }
1127 emit_range = FALSE;
1128 }
1129 else
1130#endif
1131 {
1132#ifdef EBCDIC
1133 int alpha_only = FALSE;
1134
1135 /* for alphabetical range skip the gaps
1136 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1137 if (isalpha(startc) && isalpha(endc))
1138 alpha_only = TRUE;
1139#endif
1140 /* Emit the range. "startc" was already emitted, so
1141 * skip it. */
1142 for (c = startc + 1; c <= endc; c++)
1143#ifdef EBCDIC
1144 if (!alpha_only || isalpha(startc))
1145#endif
1146 {
1147 EMIT(c);
1148 TRY_NEG();
1149 EMIT_GLUE();
1150 }
1151 emit_range = FALSE;
1152 }
1153 }
1154 else
1155 {
1156 /*
1157 * This char (startc) is not part of a range. Just
1158 * emit it.
1159 *
1160 * Normally, simply emit startc. But if we get char
1161 * code=0 from a collating char, then replace it with
1162 * 0x0a.
1163 *
1164 * This is needed to completely mimic the behaviour of
1165 * the backtracking engine.
1166 */
1167 if (got_coll_char == TRUE && startc == 0)
1168 EMIT(0x0a);
1169 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001170 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001171 TRY_NEG();
1172 EMIT_GLUE();
1173 }
1174
1175 nfa_inc(&regparse);
1176 } /* while (p < endp) */
1177
1178 nfa_dec(&regparse);
1179 if (*regparse == '-') /* if last, '-' is just a char */
1180 {
1181 EMIT('-');
1182 TRY_NEG();
1183 EMIT_GLUE();
1184 }
1185 nfa_inc(&regparse);
1186
1187 if (extra == ADD_NL) /* \_[] also matches \n */
1188 {
1189 EMIT(reg_string ? NL : NFA_NEWL);
1190 TRY_NEG();
1191 EMIT_GLUE();
1192 }
1193
1194 /* skip the trailing ] */
1195 regparse = endp;
1196 nfa_inc(&regparse);
1197 if (negated == TRUE)
1198 {
1199 /* Mark end of negated char range */
1200 EMIT(NFA_END_NEG_RANGE);
1201 EMIT(NFA_CONCAT);
1202 }
1203 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001204 } /* if exists closing ] */
1205
1206 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001207 {
1208 syntax_error = TRUE;
1209 EMSG_RET_FAIL(_(e_missingbracket));
1210 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001211 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 default:
1214 {
1215#ifdef FEAT_MBYTE
1216 int plen;
1217
1218nfa_do_multibyte:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001219 /* Length of current char with composing chars. */
1220 if (enc_utf8 && clen != (plen = (*mb_ptr2len)(old_regparse)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001222 /* A base character plus composing characters.
1223 * This requires creating a separate atom as if enclosing
1224 * the characters in (), where NFA_COMPOSING is the ( and
1225 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226 * building the postfix form, not the NFA itself;
1227 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001228 * where 'b' and 'c' are chars with codes > 256. */
1229 i = 0;
1230 for (;;)
1231 {
1232 EMIT(c);
1233 if (i > 0)
1234 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001235 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001236 break;
1237 c = utf_ptr2char(old_regparse + i);
1238 }
1239 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001240 regparse = old_regparse + plen;
1241 }
1242 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243#endif
1244 {
1245 c = no_Magic(c);
1246 EMIT(c);
1247 }
1248 return OK;
1249 }
1250 }
1251
1252#undef TRY_NEG
1253#undef EMIT_GLUE
1254
1255 return OK;
1256}
1257
1258/*
1259 * Parse something followed by possible [*+=].
1260 *
1261 * A piece is an atom, possibly followed by a multi, an indication of how many
1262 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1263 * characters: "", "a", "aa", etc.
1264 *
1265 * piece ::= atom
1266 * or atom multi
1267 */
1268 static int
1269nfa_regpiece()
1270{
1271 int i;
1272 int op;
1273 int ret;
1274 long minval, maxval;
1275 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1276 char_u *old_regparse, *new_regparse;
1277 int c2;
1278 int *old_post_ptr, *my_post_start;
1279 int old_regnpar;
1280 int quest;
1281
1282 /* Save the current position in the regexp, so that we can use it if
1283 * <atom>{m,n} is next. */
1284 old_regparse = regparse;
1285 /* Save current number of open parenthesis, so we can use it if
1286 * <atom>{m,n} is next */
1287 old_regnpar = regnpar;
1288 /* store current pos in the postfix form, for \{m,n} involving 0s */
1289 my_post_start = post_ptr;
1290
1291 ret = nfa_regatom();
1292 if (ret == FAIL)
1293 return FAIL; /* cascaded error */
1294
1295 op = peekchr();
1296 if (re_multi_type(op) == NOT_MULTI)
1297 return OK;
1298
1299 skipchr();
1300 switch (op)
1301 {
1302 case Magic('*'):
1303 EMIT(NFA_STAR);
1304 break;
1305
1306 case Magic('+'):
1307 /*
1308 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1309 * first and only submatch would be "aaa". But the backtracking
1310 * engine interprets the plus as "try matching one more time", and
1311 * a* matches a second time at the end of the input, the empty
1312 * string.
1313 * The submatch will the empty string.
1314 *
1315 * In order to be consistent with the old engine, we disable
1316 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1317 */
1318 /* EMIT(NFA_PLUS); */
1319 regnpar = old_regnpar;
1320 regparse = old_regparse;
1321 curchr = -1;
1322 if (nfa_regatom() == FAIL)
1323 return FAIL;
1324 EMIT(NFA_STAR);
1325 EMIT(NFA_CONCAT);
1326 skipchr(); /* skip the \+ */
1327 break;
1328
1329 case Magic('@'):
1330 op = no_Magic(getchr());
1331 switch(op)
1332 {
1333 case '=':
1334 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1335 break;
1336 case '!':
1337 case '<':
1338 case '>':
1339 /* Not supported yet */
1340 return FAIL;
1341 default:
1342 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001343 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 return FAIL;
1345 }
1346 break;
1347
1348 case Magic('?'):
1349 case Magic('='):
1350 EMIT(NFA_QUEST);
1351 break;
1352
1353 case Magic('{'):
1354 /* a{2,5} will expand to 'aaa?a?a?'
1355 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1356 * version of '?'
1357 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1358 * parenthesis have the same id
1359 */
1360
1361 greedy = TRUE;
1362 c2 = peekchr();
1363 if (c2 == '-' || c2 == Magic('-'))
1364 {
1365 skipchr();
1366 greedy = FALSE;
1367 }
1368 if (!read_limits(&minval, &maxval))
1369 {
1370 syntax_error = TRUE;
1371 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1372 }
1373 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1374 * <atom>* */
1375 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1376 {
1377 EMIT(NFA_STAR);
1378 break;
1379 }
1380
1381 if (maxval > NFA_BRACES_MAXLIMIT)
1382 {
1383 /* This would yield a huge automaton and use too much memory.
1384 * Revert to old engine */
1385 return FAIL;
1386 }
1387
1388 /* Special case: x{0} or x{-0} */
1389 if (maxval == 0)
1390 {
1391 /* Ignore result of previous call to nfa_regatom() */
1392 post_ptr = my_post_start;
1393 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1394 EMIT(NFA_SKIP_CHAR);
1395 return OK;
1396 }
1397
1398 /* Ignore previous call to nfa_regatom() */
1399 post_ptr = my_post_start;
1400 /* Save pos after the repeated atom and the \{} */
1401 new_regparse = regparse;
1402
1403 new_regparse = regparse;
1404 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1405 for (i = 0; i < maxval; i++)
1406 {
1407 /* Goto beginning of the repeated atom */
1408 regparse = old_regparse;
1409 curchr = -1;
1410 /* Restore count of parenthesis */
1411 regnpar = old_regnpar;
1412 old_post_ptr = post_ptr;
1413 if (nfa_regatom() == FAIL)
1414 return FAIL;
1415 /* after "minval" times, atoms are optional */
1416 if (i + 1 > minval)
1417 EMIT(quest);
1418 if (old_post_ptr != my_post_start)
1419 EMIT(NFA_CONCAT);
1420 }
1421
1422 /* Go to just after the repeated atom and the \{} */
1423 regparse = new_regparse;
1424 curchr = -1;
1425
1426 break;
1427
1428
1429 default:
1430 break;
1431 } /* end switch */
1432
1433 if (re_multi_type(peekchr()) != NOT_MULTI)
1434 {
1435 /* Can't have a multi follow a multi. */
1436 syntax_error = TRUE;
1437 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1438 }
1439
1440 return OK;
1441}
1442
1443/*
1444 * Parse one or more pieces, concatenated. It matches a match for the
1445 * first piece, followed by a match for the second piece, etc. Example:
1446 * "f[0-9]b", first matches "f", then a digit and then "b".
1447 *
1448 * concat ::= piece
1449 * or piece piece
1450 * or piece piece piece
1451 * etc.
1452 */
1453 static int
1454nfa_regconcat()
1455{
1456 int cont = TRUE;
1457 int first = TRUE;
1458
1459 while (cont)
1460 {
1461 switch (peekchr())
1462 {
1463 case NUL:
1464 case Magic('|'):
1465 case Magic('&'):
1466 case Magic(')'):
1467 cont = FALSE;
1468 break;
1469
1470 case Magic('Z'):
1471#ifdef FEAT_MBYTE
1472 regflags |= RF_ICOMBINE;
1473#endif
1474 skipchr_keepstart();
1475 break;
1476 case Magic('c'):
1477 regflags |= RF_ICASE;
1478 skipchr_keepstart();
1479 break;
1480 case Magic('C'):
1481 regflags |= RF_NOICASE;
1482 skipchr_keepstart();
1483 break;
1484 case Magic('v'):
1485 reg_magic = MAGIC_ALL;
1486 skipchr_keepstart();
1487 curchr = -1;
1488 break;
1489 case Magic('m'):
1490 reg_magic = MAGIC_ON;
1491 skipchr_keepstart();
1492 curchr = -1;
1493 break;
1494 case Magic('M'):
1495 reg_magic = MAGIC_OFF;
1496 skipchr_keepstart();
1497 curchr = -1;
1498 break;
1499 case Magic('V'):
1500 reg_magic = MAGIC_NONE;
1501 skipchr_keepstart();
1502 curchr = -1;
1503 break;
1504
1505 default:
1506 if (nfa_regpiece() == FAIL)
1507 return FAIL;
1508 if (first == FALSE)
1509 EMIT(NFA_CONCAT);
1510 else
1511 first = FALSE;
1512 break;
1513 }
1514 }
1515
1516 return OK;
1517}
1518
1519/*
1520 * Parse a branch, one or more concats, separated by "\&". It matches the
1521 * last concat, but only if all the preceding concats also match at the same
1522 * position. Examples:
1523 * "foobeep\&..." matches "foo" in "foobeep".
1524 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1525 *
1526 * branch ::= concat
1527 * or concat \& concat
1528 * or concat \& concat \& concat
1529 * etc.
1530 */
1531 static int
1532nfa_regbranch()
1533{
1534 int ch;
1535 int *old_post_ptr;
1536
1537 old_post_ptr = post_ptr;
1538
1539 /* First branch, possibly the only one */
1540 if (nfa_regconcat() == FAIL)
1541 return FAIL;
1542
1543 ch = peekchr();
1544 /* Try next concats */
1545 while (ch == Magic('&'))
1546 {
1547 skipchr();
1548 EMIT(NFA_NOPEN);
1549 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1550 old_post_ptr = post_ptr;
1551 if (nfa_regconcat() == FAIL)
1552 return FAIL;
1553 /* if concat is empty, skip a input char. But do emit a node */
1554 if (old_post_ptr == post_ptr)
1555 EMIT(NFA_SKIP_CHAR);
1556 EMIT(NFA_CONCAT);
1557 ch = peekchr();
1558 }
1559
1560 /* Even if a branch is empty, emit one node for it */
1561 if (old_post_ptr == post_ptr)
1562 EMIT(NFA_SKIP_CHAR);
1563
1564 return OK;
1565}
1566
1567/*
1568 * Parse a pattern, one or more branches, separated by "\|". It matches
1569 * anything that matches one of the branches. Example: "foo\|beep" matches
1570 * "foo" and matches "beep". If more than one branch matches, the first one
1571 * is used.
1572 *
1573 * pattern ::= branch
1574 * or branch \| branch
1575 * or branch \| branch \| branch
1576 * etc.
1577 */
1578 static int
1579nfa_reg(paren)
1580 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1581{
1582 int parno = 0;
1583
1584#ifdef FEAT_SYN_HL
1585#endif
1586 if (paren == REG_PAREN)
1587 {
1588 if (regnpar >= NSUBEXP) /* Too many `(' */
1589 {
1590 syntax_error = TRUE;
1591 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1592 }
1593 parno = regnpar++;
1594 }
1595
1596 if (nfa_regbranch() == FAIL)
1597 return FAIL; /* cascaded error */
1598
1599 while (peekchr() == Magic('|'))
1600 {
1601 skipchr();
1602 if (nfa_regbranch() == FAIL)
1603 return FAIL; /* cascaded error */
1604 EMIT(NFA_OR);
1605 }
1606
1607 /* Check for proper termination. */
1608 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1609 {
1610 syntax_error = TRUE;
1611 if (paren == REG_NPAREN)
1612 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1613 else
1614 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1615 }
1616 else if (paren == REG_NOPAREN && peekchr() != NUL)
1617 {
1618 syntax_error = TRUE;
1619 if (peekchr() == Magic(')'))
1620 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1621 else
1622 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1623 }
1624 /*
1625 * Here we set the flag allowing back references to this set of
1626 * parentheses.
1627 */
1628 if (paren == REG_PAREN)
1629 {
1630 had_endbrace[parno] = TRUE; /* have seen the close paren */
1631 EMIT(NFA_MOPEN + parno);
1632 }
1633
1634 return OK;
1635}
1636
1637typedef struct
1638{
1639 char_u *start[NSUBEXP];
1640 char_u *end[NSUBEXP];
1641 lpos_T startpos[NSUBEXP];
1642 lpos_T endpos[NSUBEXP];
1643} regsub_T;
1644
1645static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
1646
1647#ifdef DEBUG
1648static char_u code[50];
1649
1650 static void
1651nfa_set_code(c)
1652 int c;
1653{
1654 int addnl = FALSE;
1655
1656 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1657 {
1658 addnl = TRUE;
1659 c -= ADD_NL;
1660 }
1661
1662 STRCPY(code, "");
1663 switch (c)
1664 {
1665 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1666 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1667 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1668 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1669 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1670 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1671
1672 case NFA_PREV_ATOM_NO_WIDTH:
1673 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1674 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1675 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1676 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1677 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1678
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001679 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1680 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1681
1682 case NFA_MOPEN + 0:
1683 case NFA_MOPEN + 1:
1684 case NFA_MOPEN + 2:
1685 case NFA_MOPEN + 3:
1686 case NFA_MOPEN + 4:
1687 case NFA_MOPEN + 5:
1688 case NFA_MOPEN + 6:
1689 case NFA_MOPEN + 7:
1690 case NFA_MOPEN + 8:
1691 case NFA_MOPEN + 9:
1692 STRCPY(code, "NFA_MOPEN(x)");
1693 code[10] = c - NFA_MOPEN + '0';
1694 break;
1695 case NFA_MCLOSE + 0:
1696 case NFA_MCLOSE + 1:
1697 case NFA_MCLOSE + 2:
1698 case NFA_MCLOSE + 3:
1699 case NFA_MCLOSE + 4:
1700 case NFA_MCLOSE + 5:
1701 case NFA_MCLOSE + 6:
1702 case NFA_MCLOSE + 7:
1703 case NFA_MCLOSE + 8:
1704 case NFA_MCLOSE + 9:
1705 STRCPY(code, "NFA_MCLOSE(x)");
1706 code[11] = c - NFA_MCLOSE + '0';
1707 break;
1708 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1709 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1710 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1711 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1712 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1713 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1714 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1715 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1716 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1717 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1718 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1719 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1720 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1721 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1722 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1723 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1724 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1725 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1726 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1727 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1728 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1729 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1730 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1731 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1732 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1733 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1734 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1735 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1736
1737 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1738 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1739 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1740 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1741 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1742 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1743 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1744 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1745 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1746 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1747 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1748 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1749 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1750 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1751 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1752 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1753 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1754 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1755 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1756 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1757 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1758 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1759 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1760 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1761 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1762 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1763 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1764
1765 default:
1766 STRCPY(code, "CHAR(x)");
1767 code[5] = c;
1768 }
1769
1770 if (addnl == TRUE)
1771 STRCAT(code, " + NEWLINE ");
1772
1773}
1774
1775#ifdef ENABLE_LOG
1776static FILE *log_fd;
1777
1778/*
1779 * Print the postfix notation of the current regexp.
1780 */
1781 static void
1782nfa_postfix_dump(expr, retval)
1783 char_u *expr;
1784 int retval;
1785{
1786 int *p;
1787 FILE *f;
1788
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001789 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001790 if (f != NULL)
1791 {
1792 fprintf(f, "\n-------------------------\n");
1793 if (retval == FAIL)
1794 fprintf(f, ">>> NFA engine failed ... \n");
1795 else if (retval == OK)
1796 fprintf(f, ">>> NFA engine succeeded !\n");
1797 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001798 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 {
1800 nfa_set_code(*p);
1801 fprintf(f, "%s, ", code);
1802 }
1803 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001804 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805 fprintf(f, "%d ", *p);
1806 fprintf(f, "\n\n");
1807 fclose(f);
1808 }
1809}
1810
1811/*
1812 * Print the NFA starting with a root node "state".
1813 */
1814 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001815nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 FILE *debugf;
1817 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001819 garray_T indent;
1820
1821 ga_init2(&indent, 1, 64);
1822 ga_append(&indent, '\0');
1823 nfa_print_state2(debugf, state, &indent);
1824 ga_clear(&indent);
1825}
1826
1827 static void
1828nfa_print_state2(debugf, state, indent)
1829 FILE *debugf;
1830 nfa_state_T *state;
1831 garray_T *indent;
1832{
1833 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834
1835 if (state == NULL)
1836 return;
1837
1838 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001839
1840 /* Output indent */
1841 p = (char_u *)indent->ga_data;
1842 if (indent->ga_len >= 3)
1843 {
1844 int last = indent->ga_len - 3;
1845 char_u save[2];
1846
1847 STRNCPY(save, &p[last], 2);
1848 STRNCPY(&p[last], "+-", 2);
1849 fprintf(debugf, " %s", p);
1850 STRNCPY(&p[last], save, 2);
1851 }
1852 else
1853 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001854
1855 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001856 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1857 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 if (state->id < 0)
1859 return;
1860
1861 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001862
1863 /* grow indent for state->out */
1864 indent->ga_len -= 1;
1865 if (state->out1)
1866 ga_concat(indent, "| ");
1867 else
1868 ga_concat(indent, " ");
1869 ga_append(indent, '\0');
1870
1871 nfa_print_state2(debugf, state->out, indent);
1872
1873 /* replace last part of indent for state->out1 */
1874 indent->ga_len -= 3;
1875 ga_concat(indent, " ");
1876 ga_append(indent, '\0');
1877
1878 nfa_print_state2(debugf, state->out1, indent);
1879
1880 /* shrink indent */
1881 indent->ga_len -= 3;
1882 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001883}
1884
1885/*
1886 * Print the NFA state machine.
1887 */
1888 static void
1889nfa_dump(prog)
1890 nfa_regprog_T *prog;
1891{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001892 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001893
1894 if (debugf != NULL)
1895 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001896 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 fclose(debugf);
1898 }
1899}
1900#endif /* ENABLE_LOG */
1901#endif /* DEBUG */
1902
1903/*
1904 * Parse r.e. @expr and convert it into postfix form.
1905 * Return the postfix string on success, NULL otherwise.
1906 */
1907 static int *
1908re2post()
1909{
1910 if (nfa_reg(REG_NOPAREN) == FAIL)
1911 return NULL;
1912 EMIT(NFA_MOPEN);
1913 return post_start;
1914}
1915
1916/* NB. Some of the code below is inspired by Russ's. */
1917
1918/*
1919 * Represents an NFA state plus zero or one or two arrows exiting.
1920 * if c == MATCH, no arrows out; matching state.
1921 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1922 * If c < 256, labeled arrow with character c to out.
1923 */
1924
1925static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1926
1927/*
1928 * Allocate and initialize nfa_state_T.
1929 */
1930 static nfa_state_T *
1931new_state(c, out, out1)
1932 int c;
1933 nfa_state_T *out;
1934 nfa_state_T *out1;
1935{
1936 nfa_state_T *s;
1937
1938 if (istate >= nstate)
1939 return NULL;
1940
1941 s = &state_ptr[istate++];
1942
1943 s->c = c;
1944 s->out = out;
1945 s->out1 = out1;
1946
1947 s->id = istate;
1948 s->lastlist = 0;
1949 s->lastthread = NULL;
1950 s->visits = 0;
1951 s->negated = FALSE;
1952
1953 return s;
1954}
1955
1956/*
1957 * A partially built NFA without the matching state filled in.
1958 * Frag_T.start points at the start state.
1959 * Frag_T.out is a list of places that need to be set to the
1960 * next state for this fragment.
1961 */
1962typedef union Ptrlist Ptrlist;
1963struct Frag
1964{
1965 nfa_state_T *start;
1966 Ptrlist *out;
1967};
1968typedef struct Frag Frag_T;
1969
1970static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1971static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1972static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1973static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1974static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1975static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1976
1977/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001978 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001979 */
1980 static Frag_T
1981frag(start, out)
1982 nfa_state_T *start;
1983 Ptrlist *out;
1984{
Bram Moolenaar053bb602013-05-20 13:55:21 +02001985 Frag_T n;
1986
1987 n.start = start;
1988 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989 return n;
1990}
1991
1992/*
1993 * Since the out pointers in the list are always
1994 * uninitialized, we use the pointers themselves
1995 * as storage for the Ptrlists.
1996 */
1997union Ptrlist
1998{
1999 Ptrlist *next;
2000 nfa_state_T *s;
2001};
2002
2003/*
2004 * Create singleton list containing just outp.
2005 */
2006 static Ptrlist *
2007list1(outp)
2008 nfa_state_T **outp;
2009{
2010 Ptrlist *l;
2011
2012 l = (Ptrlist *)outp;
2013 l->next = NULL;
2014 return l;
2015}
2016
2017/*
2018 * Patch the list of states at out to point to start.
2019 */
2020 static void
2021patch(l, s)
2022 Ptrlist *l;
2023 nfa_state_T *s;
2024{
2025 Ptrlist *next;
2026
2027 for (; l; l = next)
2028 {
2029 next = l->next;
2030 l->s = s;
2031 }
2032}
2033
2034
2035/*
2036 * Join the two lists l1 and l2, returning the combination.
2037 */
2038 static Ptrlist *
2039append(l1, l2)
2040 Ptrlist *l1;
2041 Ptrlist *l2;
2042{
2043 Ptrlist *oldl1;
2044
2045 oldl1 = l1;
2046 while (l1->next)
2047 l1 = l1->next;
2048 l1->next = l2;
2049 return oldl1;
2050}
2051
2052/*
2053 * Stack used for transforming postfix form into NFA.
2054 */
2055static Frag_T empty;
2056
2057 static void
2058st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002059 int *postfix UNUSED;
2060 int *end UNUSED;
2061 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002062{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002063#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002064 FILE *df;
2065 int *p2;
2066
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002067 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 if (df)
2069 {
2070 fprintf(df, "Error popping the stack!\n");
2071#ifdef DEBUG
2072 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2073#endif
2074 fprintf(df, "Postfix form is: ");
2075#ifdef DEBUG
2076 for (p2 = postfix; p2 < end; p2++)
2077 {
2078 nfa_set_code(*p2);
2079 fprintf(df, "%s, ", code);
2080 }
2081 nfa_set_code(*p);
2082 fprintf(df, "\nCurrent position is: ");
2083 for (p2 = postfix; p2 <= p; p2 ++)
2084 {
2085 nfa_set_code(*p2);
2086 fprintf(df, "%s, ", code);
2087 }
2088#else
2089 for (p2 = postfix; p2 < end; p2++)
2090 {
2091 fprintf(df, "%d, ", *p2);
2092 }
2093 fprintf(df, "\nCurrent position is: ");
2094 for (p2 = postfix; p2 <= p; p2 ++)
2095 {
2096 fprintf(df, "%d, ", *p2);
2097 }
2098#endif
2099 fprintf(df, "\n--------------------------\n");
2100 fclose(df);
2101 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002102#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002103 EMSG(_("E874: (NFA) Could not pop the stack !"));
2104}
2105
2106/*
2107 * Push an item onto the stack.
2108 */
2109 static void
2110st_push(s, p, stack_end)
2111 Frag_T s;
2112 Frag_T **p;
2113 Frag_T *stack_end;
2114{
2115 Frag_T *stackp = *p;
2116
2117 if (stackp >= stack_end)
2118 return;
2119 *stackp = s;
2120 *p = *p + 1;
2121}
2122
2123/*
2124 * Pop an item from the stack.
2125 */
2126 static Frag_T
2127st_pop(p, stack)
2128 Frag_T **p;
2129 Frag_T *stack;
2130{
2131 Frag_T *stackp;
2132
2133 *p = *p - 1;
2134 stackp = *p;
2135 if (stackp < stack)
2136 return empty;
2137 return **p;
2138}
2139
2140/*
2141 * Convert a postfix form into its equivalent NFA.
2142 * Return the NFA start state on success, NULL otherwise.
2143 */
2144 static nfa_state_T *
2145post2nfa(postfix, end, nfa_calc_size)
2146 int *postfix;
2147 int *end;
2148 int nfa_calc_size;
2149{
2150 int *p;
2151 int mopen;
2152 int mclose;
2153 Frag_T *stack = NULL;
2154 Frag_T *stackp = NULL;
2155 Frag_T *stack_end = NULL;
2156 Frag_T e1;
2157 Frag_T e2;
2158 Frag_T e;
2159 nfa_state_T *s;
2160 nfa_state_T *s1;
2161 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002162 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163
2164 if (postfix == NULL)
2165 return NULL;
2166
Bram Moolenaar053bb602013-05-20 13:55:21 +02002167#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002168#define POP() st_pop(&stackp, stack); \
2169 if (stackp < stack) \
2170 { \
2171 st_error(postfix, end, p); \
2172 return NULL; \
2173 }
2174
2175 if (nfa_calc_size == FALSE)
2176 {
2177 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002178 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002180 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181 }
2182
2183 for (p = postfix; p < end; ++p)
2184 {
2185 switch (*p)
2186 {
2187 case NFA_CONCAT:
2188 /* Catenation.
2189 * Pay attention: this operator does not exist
2190 * in the r.e. itself (it is implicit, really).
2191 * It is added when r.e. is translated to postfix
2192 * form in re2post().
2193 *
2194 * No new state added here. */
2195 if (nfa_calc_size == TRUE)
2196 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002197 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198 break;
2199 }
2200 e2 = POP();
2201 e1 = POP();
2202 patch(e1.out, e2.start);
2203 PUSH(frag(e1.start, e2.out));
2204 break;
2205
2206 case NFA_NOT:
2207 /* Negation of a character */
2208 if (nfa_calc_size == TRUE)
2209 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002210 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002211 break;
2212 }
2213 e1 = POP();
2214 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002215#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002216 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002218#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219 PUSH(e1);
2220 break;
2221
2222 case NFA_OR:
2223 /* Alternation */
2224 if (nfa_calc_size == TRUE)
2225 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002226 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002227 break;
2228 }
2229 e2 = POP();
2230 e1 = POP();
2231 s = new_state(NFA_SPLIT, e1.start, e2.start);
2232 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002233 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234 PUSH(frag(s, append(e1.out, e2.out)));
2235 break;
2236
2237 case NFA_STAR:
2238 /* Zero or more */
2239 if (nfa_calc_size == TRUE)
2240 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002241 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002242 break;
2243 }
2244 e = POP();
2245 s = new_state(NFA_SPLIT, e.start, NULL);
2246 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002247 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002248 patch(e.out, s);
2249 PUSH(frag(s, list1(&s->out1)));
2250 break;
2251
2252 case NFA_QUEST:
2253 /* one or zero atoms=> greedy match */
2254 if (nfa_calc_size == TRUE)
2255 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002256 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 break;
2258 }
2259 e = POP();
2260 s = new_state(NFA_SPLIT, e.start, NULL);
2261 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002262 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002263 PUSH(frag(s, append(e.out, list1(&s->out1))));
2264 break;
2265
2266 case NFA_QUEST_NONGREEDY:
2267 /* zero or one atoms => non-greedy match */
2268 if (nfa_calc_size == TRUE)
2269 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002270 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002271 break;
2272 }
2273 e = POP();
2274 s = new_state(NFA_SPLIT, NULL, e.start);
2275 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002276 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277 PUSH(frag(s, append(e.out, list1(&s->out))));
2278 break;
2279
2280 case NFA_PLUS:
2281 /* One or more */
2282 if (nfa_calc_size == TRUE)
2283 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002284 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 break;
2286 }
2287 e = POP();
2288 s = new_state(NFA_SPLIT, e.start, NULL);
2289 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002290 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002291 patch(e.out, s);
2292 PUSH(frag(e.start, list1(&s->out1)));
2293 break;
2294
2295 case NFA_SKIP_CHAR:
2296 /* Symbol of 0-length, Used in a repetition
2297 * with max/min count of 0 */
2298 if (nfa_calc_size == TRUE)
2299 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002300 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 break;
2302 }
2303 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2304 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002305 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002306 PUSH(frag(s, list1(&s->out)));
2307 break;
2308
2309 case NFA_PREV_ATOM_NO_WIDTH:
2310 /* The \@= operator: match the preceding atom with 0 width.
2311 * Surrounds the preceding atom with START_INVISIBLE and
2312 * END_INVISIBLE, similarly to MOPEN.
2313 */
2314 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002315 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002316
2317 if (nfa_calc_size == TRUE)
2318 {
2319 nstate += 2;
2320 break;
2321 }
2322 e = POP();
2323 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2324 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002325 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326 patch(e.out, s1);
2327
2328 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2329 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002330 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 PUSH(frag(s, list1(&s1->out)));
2332 break;
2333
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002334#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002335 case NFA_COMPOSING: /* char with composing char */
2336#if 0
2337 /* TODO */
2338 if (regflags & RF_ICOMBINE)
2339 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002340 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002341 }
2342#endif
2343 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002344#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002345
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346 case NFA_MOPEN + 0: /* Submatch */
2347 case NFA_MOPEN + 1:
2348 case NFA_MOPEN + 2:
2349 case NFA_MOPEN + 3:
2350 case NFA_MOPEN + 4:
2351 case NFA_MOPEN + 5:
2352 case NFA_MOPEN + 6:
2353 case NFA_MOPEN + 7:
2354 case NFA_MOPEN + 8:
2355 case NFA_MOPEN + 9:
2356 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 if (nfa_calc_size == TRUE)
2358 {
2359 nstate += 2;
2360 break;
2361 }
2362
2363 mopen = *p;
2364 switch (*p)
2365 {
2366 case NFA_NOPEN:
2367 mclose = NFA_NCLOSE;
2368 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002369#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 case NFA_COMPOSING:
2371 mclose = NFA_END_COMPOSING;
2372 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002373#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002374 default:
2375 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2376 mclose = *p + NSUBEXP;
2377 break;
2378 }
2379
2380 /* Allow "NFA_MOPEN" as a valid postfix representation for
2381 * the empty regexp "". In this case, the NFA will be
2382 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2383 * empty groups of parenthesis, and empty mbyte chars */
2384 if (stackp == stack)
2385 {
2386 s = new_state(mopen, NULL, NULL);
2387 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002388 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389 s1 = new_state(mclose, NULL, NULL);
2390 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002391 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002392 patch(list1(&s->out), s1);
2393 PUSH(frag(s, list1(&s1->out)));
2394 break;
2395 }
2396
2397 /* At least one node was emitted before NFA_MOPEN, so
2398 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2399 e = POP();
2400 s = new_state(mopen, e.start, NULL); /* `(' */
2401 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002402 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002403
2404 s1 = new_state(mclose, NULL, NULL); /* `)' */
2405 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002406 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407 patch(e.out, s1);
2408
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002409#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002410 if (mopen == NFA_COMPOSING)
2411 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002413#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414
2415 PUSH(frag(s, list1(&s1->out)));
2416 break;
2417
2418 case NFA_ZSTART:
2419 case NFA_ZEND:
2420 default:
2421 /* Operands */
2422 if (nfa_calc_size == TRUE)
2423 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002424 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 break;
2426 }
2427 s = new_state(*p, NULL, NULL);
2428 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002429 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 PUSH(frag(s, list1(&s->out)));
2431 break;
2432
2433 } /* switch(*p) */
2434
2435 } /* for(p = postfix; *p; ++p) */
2436
2437 if (nfa_calc_size == TRUE)
2438 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002439 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002440 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002441 }
2442
2443 e = POP();
2444 if (stackp != stack)
2445 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2446
2447 if (istate >= nstate)
2448 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2449
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 matchstate = &state_ptr[istate++]; /* the match state */
2451 matchstate->c = NFA_MATCH;
2452 matchstate->out = matchstate->out1 = NULL;
2453
2454 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002455 ret = e.start;
2456
2457theend:
2458 vim_free(stack);
2459 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460
2461#undef POP1
2462#undef PUSH1
2463#undef POP2
2464#undef PUSH2
2465#undef POP
2466#undef PUSH
2467}
2468
2469/****************************************************************
2470 * NFA execution code.
2471 ****************************************************************/
2472
2473/* thread_T contains runtime information of a NFA state */
2474struct thread
2475{
2476 nfa_state_T *state;
2477 regsub_T sub; /* submatch info */
2478};
2479
2480typedef struct
2481{
2482 thread_T *t;
2483 int n;
2484} List;
2485
2486static void addstate __ARGS((List *l, nfa_state_T *state, regsub_T *m, int off, int lid, int *match));
2487
2488 static void
2489addstate(l, state, m, off, lid, match)
2490 List *l; /* runtime state list */
2491 nfa_state_T *state; /* state to update */
2492 regsub_T *m; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002493 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 int lid;
2495 int *match; /* found match? */
2496{
2497 regsub_T save;
2498 int subidx = 0;
2499
2500 if (l == NULL || state == NULL)
2501 return;
2502
2503 switch (state->c)
2504 {
2505 case NFA_SPLIT:
2506 case NFA_NOT:
2507 case NFA_NOPEN:
2508 case NFA_NCLOSE:
2509 case NFA_MCLOSE:
2510 case NFA_MCLOSE + 1:
2511 case NFA_MCLOSE + 2:
2512 case NFA_MCLOSE + 3:
2513 case NFA_MCLOSE + 4:
2514 case NFA_MCLOSE + 5:
2515 case NFA_MCLOSE + 6:
2516 case NFA_MCLOSE + 7:
2517 case NFA_MCLOSE + 8:
2518 case NFA_MCLOSE + 9:
2519 /* Do not remember these nodes in list "thislist" or "nextlist" */
2520 break;
2521
2522 default:
2523 if (state->lastlist == lid)
2524 {
2525 if (++state->visits > 2)
2526 return;
2527 }
2528 else
2529 {
2530 /* add the state to the list */
2531 state->lastlist = lid;
2532 state->lastthread = &l->t[l->n++];
2533 state->lastthread->state = state;
2534 state->lastthread->sub = *m;
2535 }
2536 }
2537
2538#ifdef ENABLE_LOG
2539 nfa_set_code(state->c);
2540 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n",
2541 abs(state->id), code, state->c);
2542#endif
2543 switch (state->c)
2544 {
2545 case NFA_MATCH:
2546 *match = TRUE;
2547 break;
2548
2549 case NFA_SPLIT:
2550 addstate(l, state->out, m, off, lid, match);
2551 addstate(l, state->out1, m, off, lid, match);
2552 break;
2553
2554 case NFA_SKIP_CHAR:
2555 addstate(l, state->out, m, off, lid, match);
2556 break;
2557
2558#if 0
2559 case NFA_END_NEG_RANGE:
2560 /* Nothing to handle here. nfa_regmatch() will take care of it */
2561 break;
2562
2563 case NFA_NOT:
2564 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2565#ifdef ENABLE_LOG
2566 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2567#endif
2568 break;
2569
2570 case NFA_COMPOSING:
2571 /* nfa_regmatch() will match all the bytes of this composing char. */
2572 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573#endif
2574
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002575 case NFA_NOPEN:
2576 case NFA_NCLOSE:
2577 addstate(l, state->out, m, off, lid, match);
2578 break;
2579
2580 /* If this state is reached, then a recursive call of nfa_regmatch()
2581 * succeeded. the next call saves the found submatches in the
2582 * first state after the "invisible" branch. */
2583#if 0
2584 case NFA_END_INVISIBLE:
2585 break;
2586#endif
2587
2588 case NFA_MOPEN + 0:
2589 case NFA_MOPEN + 1:
2590 case NFA_MOPEN + 2:
2591 case NFA_MOPEN + 3:
2592 case NFA_MOPEN + 4:
2593 case NFA_MOPEN + 5:
2594 case NFA_MOPEN + 6:
2595 case NFA_MOPEN + 7:
2596 case NFA_MOPEN + 8:
2597 case NFA_MOPEN + 9:
2598 case NFA_ZSTART:
2599 subidx = state->c - NFA_MOPEN;
2600 if (state->c == NFA_ZSTART)
2601 subidx = 0;
2602
2603 if (REG_MULTI)
2604 {
2605 save.startpos[subidx] = m->startpos[subidx];
2606 save.endpos[subidx] = m->endpos[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02002607 if (off == -1)
2608 {
2609 m->startpos[subidx].lnum = reglnum + 1;
2610 m->startpos[subidx].col = 0;
2611 }
2612 else
2613 {
2614 m->startpos[subidx].lnum = reglnum;
2615 m->startpos[subidx].col =
2616 (colnr_T)(reginput - regline + off);
2617 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618 }
2619 else
2620 {
2621 save.start[subidx] = m->start[subidx];
2622 save.end[subidx] = m->end[subidx];
2623 m->start[subidx] = reginput + off;
2624 }
2625
2626 addstate(l, state->out, m, off, lid, match);
2627
2628 if (REG_MULTI)
2629 {
2630 m->startpos[subidx] = save.startpos[subidx];
2631 m->endpos[subidx] = save.endpos[subidx];
2632 }
2633 else
2634 {
2635 m->start[subidx] = save.start[subidx];
2636 m->end[subidx] = save.end[subidx];
2637 }
2638 break;
2639
2640 case NFA_MCLOSE + 0:
2641 if (nfa_has_zend == TRUE)
2642 {
2643 addstate(l, state->out, m, off, lid, match);
2644 break;
2645 }
2646 case NFA_MCLOSE + 1:
2647 case NFA_MCLOSE + 2:
2648 case NFA_MCLOSE + 3:
2649 case NFA_MCLOSE + 4:
2650 case NFA_MCLOSE + 5:
2651 case NFA_MCLOSE + 6:
2652 case NFA_MCLOSE + 7:
2653 case NFA_MCLOSE + 8:
2654 case NFA_MCLOSE + 9:
2655 case NFA_ZEND:
2656 subidx = state->c - NFA_MCLOSE;
2657 if (state->c == NFA_ZEND)
2658 subidx = 0;
2659
2660 if (REG_MULTI)
2661 {
2662 save.startpos[subidx] = m->startpos[subidx];
2663 save.endpos[subidx] = m->endpos[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02002664 if (off == -1)
2665 {
2666 m->endpos[subidx].lnum = reglnum + 1;
2667 m->endpos[subidx].col = 0;
2668 }
2669 else
2670 {
2671 m->endpos[subidx].lnum = reglnum;
2672 m->endpos[subidx].col = (colnr_T)(reginput - regline + off);
2673 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674 }
2675 else
2676 {
2677 save.start[subidx] = m->start[subidx];
2678 save.end[subidx] = m->end[subidx];
2679 m->end[subidx] = reginput + off;
2680 }
2681
2682 addstate(l, state->out, m, off, lid, match);
2683
2684 if (REG_MULTI)
2685 {
2686 m->startpos[subidx] = save.startpos[subidx];
2687 m->endpos[subidx] = save.endpos[subidx];
2688 }
2689 else
2690 {
2691 m->start[subidx] = save.start[subidx];
2692 m->end[subidx] = save.end[subidx];
2693 }
2694 break;
2695 }
2696}
2697
2698/*
2699 * Check character class "class" against current character c.
2700 */
2701 static int
2702check_char_class(class, c)
2703 int class;
2704 int c;
2705{
2706 switch (class)
2707 {
2708 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002709 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002710 return OK;
2711 break;
2712 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002713 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714 return OK;
2715 break;
2716 case NFA_CLASS_BLANK:
2717 if (c == ' ' || c == '\t')
2718 return OK;
2719 break;
2720 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002721 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002722 return OK;
2723 break;
2724 case NFA_CLASS_DIGIT:
2725 if (VIM_ISDIGIT(c))
2726 return OK;
2727 break;
2728 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002729 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730 return OK;
2731 break;
2732 case NFA_CLASS_LOWER:
2733 if (MB_ISLOWER(c))
2734 return OK;
2735 break;
2736 case NFA_CLASS_PRINT:
2737 if (vim_isprintc(c))
2738 return OK;
2739 break;
2740 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002741 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 return OK;
2743 break;
2744 case NFA_CLASS_SPACE:
2745 if ((c >=9 && c <= 13) || (c == ' '))
2746 return OK;
2747 break;
2748 case NFA_CLASS_UPPER:
2749 if (MB_ISUPPER(c))
2750 return OK;
2751 break;
2752 case NFA_CLASS_XDIGIT:
2753 if (vim_isxdigit(c))
2754 return OK;
2755 break;
2756 case NFA_CLASS_TAB:
2757 if (c == '\t')
2758 return OK;
2759 break;
2760 case NFA_CLASS_RETURN:
2761 if (c == '\r')
2762 return OK;
2763 break;
2764 case NFA_CLASS_BACKSPACE:
2765 if (c == '\b')
2766 return OK;
2767 break;
2768 case NFA_CLASS_ESCAPE:
2769 if (c == '\033')
2770 return OK;
2771 break;
2772
2773 default:
2774 /* should not be here :P */
2775 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2776 }
2777 return FAIL;
2778}
2779
2780/*
2781 * Set all NFA nodes' list ID equal to -1.
2782 */
2783 static void
2784nfa_set_neg_listids(start)
2785 nfa_state_T *start;
2786{
2787 if (start == NULL)
2788 return;
2789 if (start->lastlist >= 0)
2790 {
2791 start->lastlist = -1;
2792 nfa_set_neg_listids(start->out);
2793 nfa_set_neg_listids(start->out1);
2794 }
2795}
2796
2797/*
2798 * Set all NFA nodes' list ID equal to 0.
2799 */
2800 static void
2801nfa_set_null_listids(start)
2802 nfa_state_T *start;
2803{
2804 if (start == NULL)
2805 return;
2806 if (start->lastlist == -1)
2807 {
2808 start->lastlist = 0;
2809 nfa_set_null_listids(start->out);
2810 nfa_set_null_listids(start->out1);
2811 }
2812}
2813
2814/*
2815 * Save list IDs for all NFA states in "list".
2816 */
2817 static void
2818nfa_save_listids(start, list)
2819 nfa_state_T *start;
2820 int *list;
2821{
2822 if (start == NULL)
2823 return;
2824 if (start->lastlist != -1)
2825 {
2826 list[abs(start->id)] = start->lastlist;
2827 start->lastlist = -1;
2828 nfa_save_listids(start->out, list);
2829 nfa_save_listids(start->out1, list);
2830 }
2831}
2832
2833/*
2834 * Restore list IDs from "list" to all NFA states.
2835 */
2836 static void
2837nfa_restore_listids(start, list)
2838 nfa_state_T *start;
2839 int *list;
2840{
2841 if (start == NULL)
2842 return;
2843 if (start->lastlist == -1)
2844 {
2845 start->lastlist = list[abs(start->id)];
2846 nfa_restore_listids(start->out, list);
2847 nfa_restore_listids(start->out1, list);
2848 }
2849}
2850
2851/*
2852 * Main matching routine.
2853 *
2854 * Run NFA to determine whether it matches reginput.
2855 *
2856 * Return TRUE if there is a match, FALSE otherwise.
2857 * Note: Caller must ensure that: start != NULL.
2858 */
2859 static int
2860nfa_regmatch(start, submatch, m)
2861 nfa_state_T *start;
2862 regsub_T *submatch;
2863 regsub_T *m;
2864{
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002865 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002866 int n;
2867 int i = 0;
2868 int result;
2869 int size = 0;
2870 int match = FALSE;
2871 int flag = 0;
2872 int old_reglnum = -1;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002873 int go_to_nextline;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874 thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875 char_u *old_reginput = NULL;
2876 char_u *old_regline = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877 List list[3];
2878 List *listtbl[2][2];
2879 List *ll;
2880 int listid = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 List *thislist;
2882 List *nextlist;
2883 List *neglist;
2884 int *listids = NULL;
2885 int j = 0;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02002886#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002887 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888
2889 if (debug == NULL)
2890 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002891 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002892 return FALSE;
2893 }
2894#endif
2895
2896 /* Allocate memory for the lists of nodes */
2897 size = (nstate + 1) * sizeof(thread_T);
2898 list[0].t = (thread_T *)lalloc(size, TRUE);
2899 list[1].t = (thread_T *)lalloc(size, TRUE);
2900 list[2].t = (thread_T *)lalloc(size, TRUE);
2901 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
2902 goto theend;
2903 vim_memset(list[0].t, 0, size);
2904 vim_memset(list[1].t, 0, size);
2905 vim_memset(list[2].t, 0, size);
2906
2907#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002908 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002909 if (log_fd != NULL)
2910 {
2911 fprintf(log_fd, "**********************************\n");
2912 nfa_set_code(start->c);
2913 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
2914 abs(start->id), code);
2915 fprintf(log_fd, "**********************************\n");
2916 }
2917 else
2918 {
2919 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
2920 log_fd = stderr;
2921 }
2922#endif
2923
2924 thislist = &list[0];
2925 thislist->n = 0;
2926 nextlist = &list[1];
2927 nextlist->n = 0;
2928 neglist = &list[2];
2929 neglist->n = 0;
2930#ifdef ENABLE_LOG
2931 fprintf(log_fd, "(---) STARTSTATE\n");
2932#endif
2933 addstate(thislist, start, m, 0, listid, &match);
2934
2935 /* There are two cases when the NFA advances: 1. input char matches the
2936 * NFA node and 2. input char does not match the NFA node, but the next
2937 * node is NFA_NOT. The following macro calls addstate() according to
2938 * these rules. It is used A LOT, so use the "listtbl" table for speed */
2939 listtbl[0][0] = NULL;
2940 listtbl[0][1] = neglist;
2941 listtbl[1][0] = nextlist;
2942 listtbl[1][1] = NULL;
2943#define ADD_POS_NEG_STATE(node) \
2944 ll = listtbl[result ? 1 : 0][node->negated]; \
2945 if (ll != NULL) \
2946 addstate(ll, node->out , &t->sub, n, listid + 1, &match);
2947
2948
2949 /*
2950 * Run for each character.
2951 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002952 for (;;)
2953 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002954#ifdef FEAT_MBYTE
2955 if (has_mbyte)
2956 {
2957 c = (*mb_ptr2char)(reginput);
2958 n = (*mb_ptr2len)(reginput);
2959 }
2960 else
2961#endif
2962 {
2963 c = *reginput;
2964 n = 1;
2965 }
2966 if (c == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02002967 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002968 n = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002969 go_to_nextline = FALSE;
2970 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002971
2972 /* swap lists */
2973 thislist = &list[flag];
2974 nextlist = &list[flag ^= 1];
2975 nextlist->n = 0; /* `clear' nextlist */
2976 listtbl[1][0] = nextlist;
2977 ++listid;
2978
2979#ifdef ENABLE_LOG
2980 fprintf(log_fd, "------------------------------------------\n");
2981 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
2982 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", c, (int)c);
2983 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
2984 for (i = 0; i< thislist->n; i++)
2985 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
2986 fprintf(log_fd, "\n");
2987#endif
2988
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02002989#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002990 fprintf(debug, "\n-------------------\n");
2991#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02002992 /*
2993 * If the state lists are empty we can stop.
2994 */
2995 if (thislist->n == 0 && neglist->n == 0)
2996 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002997
2998 /* compute nextlist */
2999 for (i = 0; i < thislist->n || neglist->n > 0; ++i)
3000 {
3001 if (neglist->n > 0)
3002 {
3003 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003004 neglist->n--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003005 i--;
3006 }
3007 else
3008 t = &thislist->t[i];
3009
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003010#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003011 nfa_set_code(t->state->c);
3012 fprintf(debug, "%s, ", code);
3013#endif
3014#ifdef ENABLE_LOG
3015 nfa_set_code(t->state->c);
3016 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3017 code, (int)t->state->c);
3018#endif
3019
3020 /*
3021 * Handle the possible codes of the current state.
3022 * The most important is NFA_MATCH.
3023 */
3024 switch (t->state->c)
3025 {
3026 case NFA_MATCH:
3027 match = TRUE;
3028 *submatch = t->sub;
3029#ifdef ENABLE_LOG
3030 for (j = 0; j < 4; j++)
3031 if (REG_MULTI)
3032 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
3033 j,
3034 t->sub.startpos[j].col,
3035 (int)t->sub.startpos[j].lnum,
3036 t->sub.endpos[j].col,
3037 (int)t->sub.endpos[j].lnum);
3038 else
3039 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
3040 j,
3041 (char *)t->sub.start[j],
3042 (char *)t->sub.end[j]);
3043 fprintf(log_fd, "\n");
3044#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003045 /* Found the left-most longest match, do not look at any other
3046 * states at this position. */
3047 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003048
3049 case NFA_END_INVISIBLE:
3050 /* This is only encountered after a NFA_START_INVISIBLE node.
3051 * They surround a zero-width group, used with "\@=" and "\&".
3052 * If we got here, it means that the current "invisible" group
3053 * finished successfully, so return control to the parent
3054 * nfa_regmatch(). Submatches are stored in *m, and used in
3055 * the parent call. */
3056 if (start->c == NFA_MOPEN + 0)
3057 addstate(thislist, t->state->out, &t->sub, 0, listid,
3058 &match);
3059 else
3060 {
3061 *m = t->sub;
3062 match = TRUE;
3063 }
3064 break;
3065
3066 case NFA_START_INVISIBLE:
3067 /* Save global variables, and call nfa_regmatch() to check if
3068 * the current concat matches at this position. The concat
3069 * ends with the node NFA_END_INVISIBLE */
3070 old_reginput = reginput;
3071 old_regline = regline;
3072 old_reglnum = reglnum;
3073 if (listids == NULL)
3074 {
3075 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3076 if (listids == NULL)
3077 {
3078 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3079 return 0;
3080 }
3081 }
3082#ifdef ENABLE_LOG
3083 if (log_fd != stderr)
3084 fclose(log_fd);
3085 log_fd = NULL;
3086#endif
3087 /* Have to clear the listid field of the NFA nodes, so that
3088 * nfa_regmatch() and addstate() can run properly after
3089 * recursion. */
3090 nfa_save_listids(start, listids);
3091 nfa_set_null_listids(start);
3092 result = nfa_regmatch(t->state->out, submatch, m);
3093 nfa_set_neg_listids(start);
3094 nfa_restore_listids(start, listids);
3095
3096#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003097 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003098 if (log_fd != NULL)
3099 {
3100 fprintf(log_fd, "****************************\n");
3101 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3102 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3103 fprintf(log_fd, "****************************\n");
3104 }
3105 else
3106 {
3107 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3108 log_fd = stderr;
3109 }
3110#endif
3111 if (result == TRUE)
3112 {
3113 /* Restore position in input text */
3114 reginput = old_reginput;
3115 regline = old_regline;
3116 reglnum = old_reglnum;
3117 /* Copy submatch info from the recursive call */
3118 if (REG_MULTI)
3119 for (j = 1; j < NSUBEXP; j++)
3120 {
3121 t->sub.startpos[j] = m->startpos[j];
3122 t->sub.endpos[j] = m->endpos[j];
3123 }
3124 else
3125 for (j = 1; j < NSUBEXP; j++)
3126 {
3127 t->sub.start[j] = m->start[j];
3128 t->sub.end[j] = m->end[j];
3129 }
3130 /* t->state->out1 is the corresponding END_INVISIBLE node */
3131 addstate(thislist, t->state->out1->out, &t->sub, 0, listid,
3132 &match);
3133 }
3134 else
3135 {
3136 /* continue with next input char */
3137 reginput = old_reginput;
3138 }
3139 break;
3140
3141 case NFA_BOL:
3142 if (reginput == regline)
3143 addstate(thislist, t->state->out, &t->sub, 0, listid,
3144 &match);
3145 break;
3146
3147 case NFA_EOL:
3148 if (c == NUL)
3149 addstate(thislist, t->state->out, &t->sub, 0, listid,
3150 &match);
3151 break;
3152
3153 case NFA_BOW:
3154 {
3155 int bow = TRUE;
3156
3157 if (c == NUL)
3158 bow = FALSE;
3159#ifdef FEAT_MBYTE
3160 else if (has_mbyte)
3161 {
3162 int this_class;
3163
3164 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003165 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003166 if (this_class <= 1)
3167 bow = FALSE;
3168 else if (reg_prev_class() == this_class)
3169 bow = FALSE;
3170 }
3171#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003172 else if (!vim_iswordc_buf(c, reg_buf)
3173 || (reginput > regline
3174 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175 bow = FALSE;
3176 if (bow)
3177 addstate(thislist, t->state->out, &t->sub, 0, listid,
3178 &match);
3179 break;
3180 }
3181
3182 case NFA_EOW:
3183 {
3184 int eow = TRUE;
3185
3186 if (reginput == regline)
3187 eow = FALSE;
3188#ifdef FEAT_MBYTE
3189 else if (has_mbyte)
3190 {
3191 int this_class, prev_class;
3192
3193 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003194 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 prev_class = reg_prev_class();
3196 if (this_class == prev_class
3197 || prev_class == 0 || prev_class == 1)
3198 eow = FALSE;
3199 }
3200#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003201 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
3202 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 eow = FALSE;
3204 if (eow)
3205 addstate(thislist, t->state->out, &t->sub, 0, listid,
3206 &match);
3207 break;
3208 }
3209
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003210#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003212 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003213 int mc = c;
3214 int len = 0;
3215 nfa_state_T *end;
3216 nfa_state_T *sta;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003217
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 result = OK;
3219 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003220 len = 0;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003221 if (ireg_icombine)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003222 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003223 /* If \Z was present, then ignore composing characters. */
3224 /* TODO: How about negated? */
3225 if (sta->c != c)
3226 result = FAIL;
3227 len = n;
3228 while (sta->c != NFA_END_COMPOSING)
3229 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003231 else
3232 while (sta->c != NFA_END_COMPOSING && len < n)
3233 {
3234 if (len > 0)
3235 mc = mb_ptr2char(reginput + len);
3236 if (mc != sta->c)
3237 break;
3238 len += mb_char2len(mc);
3239 sta = sta->out;
3240 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241
3242 /* if input char length doesn't match regexp char length */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003243 if (len < n || sta->c != NFA_END_COMPOSING)
Bram Moolenaar1d814752013-05-24 20:25:33 +02003244 result = FAIL;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003245 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246 ADD_POS_NEG_STATE(end);
3247 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003248 }
3249#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003250
3251 case NFA_NEWL:
3252 if (!reg_line_lbr && REG_MULTI
Bram Moolenaar35b23862013-05-22 23:00:40 +02003253 && c == NUL && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003255 go_to_nextline = TRUE;
3256 /* Pass -1 for the offset, which means taking the position
3257 * at the start of the next line. */
3258 addstate(nextlist, t->state->out, &t->sub, -1,
3259 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003260 }
3261 break;
3262
3263 case NFA_CLASS_ALNUM:
3264 case NFA_CLASS_ALPHA:
3265 case NFA_CLASS_BLANK:
3266 case NFA_CLASS_CNTRL:
3267 case NFA_CLASS_DIGIT:
3268 case NFA_CLASS_GRAPH:
3269 case NFA_CLASS_LOWER:
3270 case NFA_CLASS_PRINT:
3271 case NFA_CLASS_PUNCT:
3272 case NFA_CLASS_SPACE:
3273 case NFA_CLASS_UPPER:
3274 case NFA_CLASS_XDIGIT:
3275 case NFA_CLASS_TAB:
3276 case NFA_CLASS_RETURN:
3277 case NFA_CLASS_BACKSPACE:
3278 case NFA_CLASS_ESCAPE:
3279 result = check_char_class(t->state->c, c);
3280 ADD_POS_NEG_STATE(t->state);
3281 break;
3282
3283 case NFA_END_NEG_RANGE:
3284 /* This follows a series of negated nodes, like:
3285 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
3286 if (c > 0)
3287 addstate(nextlist, t->state->out, &t->sub, n, listid + 1,
3288 &match);
3289 break;
3290
3291 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003292 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003293 if (c > 0)
3294 addstate(nextlist, t->state->out, &t->sub, n, listid + 1,
3295 &match);
3296 break;
3297
3298 /*
3299 * Character classes like \a for alpha, \d for digit etc.
3300 */
3301 case NFA_IDENT: /* \i */
3302 result = vim_isIDc(c);
3303 ADD_POS_NEG_STATE(t->state);
3304 break;
3305
3306 case NFA_SIDENT: /* \I */
3307 result = !VIM_ISDIGIT(c) && vim_isIDc(c);
3308 ADD_POS_NEG_STATE(t->state);
3309 break;
3310
3311 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003312 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 ADD_POS_NEG_STATE(t->state);
3314 break;
3315
3316 case NFA_SKWORD: /* \K */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003317 result = !VIM_ISDIGIT(c) && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 ADD_POS_NEG_STATE(t->state);
3319 break;
3320
3321 case NFA_FNAME: /* \f */
3322 result = vim_isfilec(c);
3323 ADD_POS_NEG_STATE(t->state);
3324 break;
3325
3326 case NFA_SFNAME: /* \F */
3327 result = !VIM_ISDIGIT(c) && vim_isfilec(c);
3328 ADD_POS_NEG_STATE(t->state);
3329 break;
3330
3331 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003332 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 ADD_POS_NEG_STATE(t->state);
3334 break;
3335
3336 case NFA_SPRINT: /* \P */
Bram Moolenaar08050492013-05-21 12:43:56 +02003337 result = !VIM_ISDIGIT(c) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003338 ADD_POS_NEG_STATE(t->state);
3339 break;
3340
3341 case NFA_WHITE: /* \s */
3342 result = vim_iswhite(c);
3343 ADD_POS_NEG_STATE(t->state);
3344 break;
3345
3346 case NFA_NWHITE: /* \S */
3347 result = c != NUL && !vim_iswhite(c);
3348 ADD_POS_NEG_STATE(t->state);
3349 break;
3350
3351 case NFA_DIGIT: /* \d */
3352 result = ri_digit(c);
3353 ADD_POS_NEG_STATE(t->state);
3354 break;
3355
3356 case NFA_NDIGIT: /* \D */
3357 result = c != NUL && !ri_digit(c);
3358 ADD_POS_NEG_STATE(t->state);
3359 break;
3360
3361 case NFA_HEX: /* \x */
3362 result = ri_hex(c);
3363 ADD_POS_NEG_STATE(t->state);
3364 break;
3365
3366 case NFA_NHEX: /* \X */
3367 result = c != NUL && !ri_hex(c);
3368 ADD_POS_NEG_STATE(t->state);
3369 break;
3370
3371 case NFA_OCTAL: /* \o */
3372 result = ri_octal(c);
3373 ADD_POS_NEG_STATE(t->state);
3374 break;
3375
3376 case NFA_NOCTAL: /* \O */
3377 result = c != NUL && !ri_octal(c);
3378 ADD_POS_NEG_STATE(t->state);
3379 break;
3380
3381 case NFA_WORD: /* \w */
3382 result = ri_word(c);
3383 ADD_POS_NEG_STATE(t->state);
3384 break;
3385
3386 case NFA_NWORD: /* \W */
3387 result = c != NUL && !ri_word(c);
3388 ADD_POS_NEG_STATE(t->state);
3389 break;
3390
3391 case NFA_HEAD: /* \h */
3392 result = ri_head(c);
3393 ADD_POS_NEG_STATE(t->state);
3394 break;
3395
3396 case NFA_NHEAD: /* \H */
3397 result = c != NUL && !ri_head(c);
3398 ADD_POS_NEG_STATE(t->state);
3399 break;
3400
3401 case NFA_ALPHA: /* \a */
3402 result = ri_alpha(c);
3403 ADD_POS_NEG_STATE(t->state);
3404 break;
3405
3406 case NFA_NALPHA: /* \A */
3407 result = c != NUL && !ri_alpha(c);
3408 ADD_POS_NEG_STATE(t->state);
3409 break;
3410
3411 case NFA_LOWER: /* \l */
3412 result = ri_lower(c);
3413 ADD_POS_NEG_STATE(t->state);
3414 break;
3415
3416 case NFA_NLOWER: /* \L */
3417 result = c != NUL && !ri_lower(c);
3418 ADD_POS_NEG_STATE(t->state);
3419 break;
3420
3421 case NFA_UPPER: /* \u */
3422 result = ri_upper(c);
3423 ADD_POS_NEG_STATE(t->state);
3424 break;
3425
3426 case NFA_NUPPER: /* \U */
3427 result = c != NUL && !ri_upper(c);
3428 ADD_POS_NEG_STATE(t->state);
3429 break;
3430
Bram Moolenaar12e40142013-05-21 15:33:41 +02003431 case NFA_MOPEN + 0:
3432 case NFA_MOPEN + 1:
3433 case NFA_MOPEN + 2:
3434 case NFA_MOPEN + 3:
3435 case NFA_MOPEN + 4:
3436 case NFA_MOPEN + 5:
3437 case NFA_MOPEN + 6:
3438 case NFA_MOPEN + 7:
3439 case NFA_MOPEN + 8:
3440 case NFA_MOPEN + 9:
3441 /* handled below */
3442 break;
3443
3444 case NFA_SKIP_CHAR:
3445 case NFA_ZSTART:
3446 /* TODO: should not happen? */
3447 break;
3448
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449 default: /* regular character */
Bram Moolenaar12e40142013-05-21 15:33:41 +02003450 /* TODO: put this in #ifdef later */
3451 if (t->state->c < -256)
3452 EMSGN("INTERNAL: Negative state char: %ld", t->state->c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453 result = (no_Magic(t->state->c) == c);
Bram Moolenaar12e40142013-05-21 15:33:41 +02003454
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 if (!result)
3456 result = ireg_ic == TRUE
3457 && MB_TOLOWER(t->state->c) == MB_TOLOWER(c);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003458#ifdef FEAT_MBYTE
3459 /* If there is a composing character which is not being
3460 * ignored there can be no match. Match with composing
3461 * character uses NFA_COMPOSING above. */
3462 if (result && enc_utf8 && !ireg_icombine
3463 && n != utf_char2len(c))
3464 result = FALSE;
3465#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 ADD_POS_NEG_STATE(t->state);
3467 break;
3468 }
3469
3470 } /* for (thislist = thislist; thislist->state; thislist++) */
3471
3472 /* The first found match is the leftmost one, but there may be a
3473 * longer one. Keep running the NFA, but don't start from the
3474 * beginning. Also, do not add the start state in recursive calls of
3475 * nfa_regmatch(), because recursive calls should only start in the
3476 * first position. */
3477 if (match == FALSE && start->c == NFA_MOPEN + 0)
3478 {
3479#ifdef ENABLE_LOG
3480 fprintf(log_fd, "(---) STARTSTATE\n");
3481#endif
3482 addstate(nextlist, start, m, n, listid + 1, &match);
3483 }
3484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485#ifdef ENABLE_LOG
3486 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
3487 for (i = 0; i< thislist->n; i++)
3488 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3489 fprintf(log_fd, "\n");
3490#endif
3491
3492nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003493 /* Advance to the next character, or advance to the next line, or
3494 * finish. */
3495 if (n != 0)
3496 reginput += n;
3497 else if (go_to_nextline)
3498 reg_nextline();
3499 else
3500 break;
3501 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502
3503#ifdef ENABLE_LOG
3504 if (log_fd != stderr)
3505 fclose(log_fd);
3506 log_fd = NULL;
3507#endif
3508
3509theend:
3510 /* Free memory */
3511 vim_free(list[0].t);
3512 vim_free(list[1].t);
3513 vim_free(list[2].t);
3514 list[0].t = list[1].t = list[2].t = NULL;
3515 if (listids != NULL)
3516 vim_free(listids);
3517#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003518#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519 fclose(debug);
3520#endif
3521
3522 return match;
3523}
3524
3525/*
3526 * Try match of "prog" with at regline["col"].
3527 * Returns 0 for failure, number of lines contained in the match otherwise.
3528 */
3529 static long
3530nfa_regtry(start, col)
3531 nfa_state_T *start;
3532 colnr_T col;
3533{
3534 int i;
3535 regsub_T sub, m;
3536#ifdef ENABLE_LOG
3537 FILE *f;
3538#endif
3539
3540 reginput = regline + col;
3541 need_clear_subexpr = TRUE;
3542
3543#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003544 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003545 if (f != NULL)
3546 {
3547 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3548 fprintf(f, " =======================================================\n");
3549#ifdef DEBUG
3550 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3551#endif
3552 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3553 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003554 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 fprintf(f, "\n\n");
3556 fclose(f);
3557 }
3558 else
3559 EMSG(_("Could not open temporary log file for writing "));
3560#endif
3561
3562 if (REG_MULTI)
3563 {
3564 /* Use 0xff to set lnum to -1 */
3565 vim_memset(sub.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3566 vim_memset(sub.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3567 vim_memset(m.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3568 vim_memset(m.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3569 }
3570 else
3571 {
3572 vim_memset(sub.start, 0, sizeof(char_u *) * NSUBEXP);
3573 vim_memset(sub.end, 0, sizeof(char_u *) * NSUBEXP);
3574 vim_memset(m.start, 0, sizeof(char_u *) * NSUBEXP);
3575 vim_memset(m.end, 0, sizeof(char_u *) * NSUBEXP);
3576 }
3577
3578 if (nfa_regmatch(start, &sub, &m) == FALSE)
3579 return 0;
3580
3581 cleanup_subexpr();
3582 if (REG_MULTI)
3583 {
3584 for (i = 0; i < NSUBEXP; i++)
3585 {
3586 reg_startpos[i] = sub.startpos[i];
3587 reg_endpos[i] = sub.endpos[i];
3588 }
3589
3590 if (reg_startpos[0].lnum < 0)
3591 {
3592 reg_startpos[0].lnum = 0;
3593 reg_startpos[0].col = col;
3594 }
3595 if (reg_endpos[0].lnum < 0)
3596 {
3597 reg_endpos[0].lnum = reglnum;
3598 reg_endpos[0].col = (int)(reginput - regline);
3599 }
3600 else
3601 /* Use line number of "\ze". */
3602 reglnum = reg_endpos[0].lnum;
3603 }
3604 else
3605 {
3606 for (i = 0; i < NSUBEXP; i++)
3607 {
3608 reg_startp[i] = sub.start[i];
3609 reg_endp[i] = sub.end[i];
3610 }
3611
3612 if (reg_startp[0] == NULL)
3613 reg_startp[0] = regline + col;
3614 if (reg_endp[0] == NULL)
3615 reg_endp[0] = reginput;
3616 }
3617
3618 return 1 + reglnum;
3619}
3620
3621/*
3622 * Match a regexp against a string ("line" points to the string) or multiple
3623 * lines ("line" is NULL, use reg_getline()).
3624 *
3625 * Returns 0 for failure, number of lines contained in the match otherwise.
3626 */
3627 static long
3628nfa_regexec_both(line, col)
3629 char_u *line;
3630 colnr_T col; /* column to start looking for match */
3631{
3632 nfa_regprog_T *prog;
3633 long retval = 0L;
3634 int i;
3635
3636 if (REG_MULTI)
3637 {
3638 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3639 line = reg_getline((linenr_T)0); /* relative to the cursor */
3640 reg_startpos = reg_mmatch->startpos;
3641 reg_endpos = reg_mmatch->endpos;
3642 }
3643 else
3644 {
3645 prog = (nfa_regprog_T *)reg_match->regprog;
3646 reg_startp = reg_match->startp;
3647 reg_endp = reg_match->endp;
3648 }
3649
3650 /* Be paranoid... */
3651 if (prog == NULL || line == NULL)
3652 {
3653 EMSG(_(e_null));
3654 goto theend;
3655 }
3656
3657 /* If the start column is past the maximum column: no need to try. */
3658 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3659 goto theend;
3660
3661 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3662 if (prog->regflags & RF_ICASE)
3663 ireg_ic = TRUE;
3664 else if (prog->regflags & RF_NOICASE)
3665 ireg_ic = FALSE;
3666
3667#ifdef FEAT_MBYTE
3668 /* If pattern contains "\Z" overrule value of ireg_icombine */
3669 if (prog->regflags & RF_ICOMBINE)
3670 ireg_icombine = TRUE;
3671#endif
3672
3673 regline = line;
3674 reglnum = 0; /* relative to line */
3675
3676 nstate = prog->nstate;
3677
3678 for (i = 0; i < nstate; ++i)
3679 {
3680 prog->state[i].id = i;
3681 prog->state[i].lastlist = 0;
3682 prog->state[i].visits = 0;
3683 prog->state[i].lastthread = NULL;
3684 }
3685
3686 retval = nfa_regtry(prog->start, col);
3687
3688theend:
3689 return retval;
3690}
3691
3692/*
3693 * Compile a regular expression into internal code for the NFA matcher.
3694 * Returns the program in allocated space. Returns NULL for an error.
3695 */
3696 static regprog_T *
3697nfa_regcomp(expr, re_flags)
3698 char_u *expr;
3699 int re_flags;
3700{
3701 nfa_regprog_T *prog;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003702 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 int *postfix;
3704
3705 if (expr == NULL)
3706 return NULL;
3707
3708#ifdef DEBUG
3709 nfa_regengine.expr = expr;
3710#endif
3711
3712 init_class_tab();
3713
3714 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3715 return NULL;
3716
3717 /* Space for compiled regexp */
3718 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate_max;
3719 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3720 if (prog == NULL)
3721 goto fail;
3722 vim_memset(prog, 0, prog_size);
3723
3724 /* Build postfix form of the regexp. Needed to build the NFA
3725 * (and count its size) */
3726 postfix = re2post();
3727 if (postfix == NULL)
3728 goto fail; /* Cascaded (syntax?) error */
3729
3730 /*
3731 * In order to build the NFA, we parse the input regexp twice:
3732 * 1. first pass to count size (so we can allocate space)
3733 * 2. second to emit code
3734 */
3735#ifdef ENABLE_LOG
3736 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003737 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003738
3739 if (f != NULL)
3740 {
3741 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3742 fclose(f);
3743 }
3744 }
3745#endif
3746
3747 /*
3748 * PASS 1
3749 * Count number of NFA states in "nstate". Do not build the NFA.
3750 */
3751 post2nfa(postfix, post_ptr, TRUE);
3752 state_ptr = prog->state;
3753
3754 /*
3755 * PASS 2
3756 * Build the NFA
3757 */
3758 prog->start = post2nfa(postfix, post_ptr, FALSE);
3759 if (prog->start == NULL)
3760 goto fail;
3761
3762 prog->regflags = regflags;
3763 prog->engine = &nfa_regengine;
3764 prog->nstate = nstate;
3765#ifdef ENABLE_LOG
3766 nfa_postfix_dump(expr, OK);
3767 nfa_dump(prog);
3768#endif
3769
3770out:
3771 vim_free(post_start);
3772 post_start = post_ptr = post_end = NULL;
3773 state_ptr = NULL;
3774 return (regprog_T *)prog;
3775
3776fail:
3777 vim_free(prog);
3778 prog = NULL;
3779#ifdef ENABLE_LOG
3780 nfa_postfix_dump(expr, FAIL);
3781#endif
3782#ifdef DEBUG
3783 nfa_regengine.expr = NULL;
3784#endif
3785 goto out;
3786}
3787
3788
3789/*
3790 * Match a regexp against a string.
3791 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
3792 * Uses curbuf for line count and 'iskeyword'.
3793 *
3794 * Return TRUE if there is a match, FALSE if not.
3795 */
3796 static int
3797nfa_regexec(rmp, line, col)
3798 regmatch_T *rmp;
3799 char_u *line; /* string to match against */
3800 colnr_T col; /* column to start looking for match */
3801{
3802 reg_match = rmp;
3803 reg_mmatch = NULL;
3804 reg_maxline = 0;
3805 reg_line_lbr = FALSE;
3806 reg_buf = curbuf;
3807 reg_win = NULL;
3808 ireg_ic = rmp->rm_ic;
3809#ifdef FEAT_MBYTE
3810 ireg_icombine = FALSE;
3811#endif
3812 ireg_maxcol = 0;
3813 return (nfa_regexec_both(line, col) != 0);
3814}
3815
3816#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3817 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
3818
3819static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3820
3821/*
3822 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
3823 */
3824 static int
3825nfa_regexec_nl(rmp, line, col)
3826 regmatch_T *rmp;
3827 char_u *line; /* string to match against */
3828 colnr_T col; /* column to start looking for match */
3829{
3830 reg_match = rmp;
3831 reg_mmatch = NULL;
3832 reg_maxline = 0;
3833 reg_line_lbr = TRUE;
3834 reg_buf = curbuf;
3835 reg_win = NULL;
3836 ireg_ic = rmp->rm_ic;
3837#ifdef FEAT_MBYTE
3838 ireg_icombine = FALSE;
3839#endif
3840 ireg_maxcol = 0;
3841 return (nfa_regexec_both(line, col) != 0);
3842}
3843#endif
3844
3845
3846/*
3847 * Match a regexp against multiple lines.
3848 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3849 * Uses curbuf for line count and 'iskeyword'.
3850 *
3851 * Return zero if there is no match. Return number of lines contained in the
3852 * match otherwise.
3853 *
3854 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
3855 *
3856 * ! Also NOTE : match may actually be in another line. e.g.:
3857 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
3858 *
3859 * +-------------------------+
3860 * |a |
3861 * |b |
3862 * |c |
3863 * | |
3864 * +-------------------------+
3865 *
3866 * then nfa_regexec_multi() returns 3. while the original
3867 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
3868 *
3869 * FIXME if this behavior is not compatible.
3870 */
3871 static long
3872nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
3873 regmmatch_T *rmp;
3874 win_T *win; /* window in which to search or NULL */
3875 buf_T *buf; /* buffer in which to search */
3876 linenr_T lnum; /* nr of line to start looking for match */
3877 colnr_T col; /* column to start looking for match */
3878 proftime_T *tm UNUSED; /* timeout limit or NULL */
3879{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003880 reg_match = NULL;
3881 reg_mmatch = rmp;
3882 reg_buf = buf;
3883 reg_win = win;
3884 reg_firstlnum = lnum;
3885 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3886 reg_line_lbr = FALSE;
3887 ireg_ic = rmp->rmm_ic;
3888#ifdef FEAT_MBYTE
3889 ireg_icombine = FALSE;
3890#endif
3891 ireg_maxcol = rmp->rmm_maxcol;
3892
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003893 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003894}
3895
3896#ifdef DEBUG
3897# undef ENABLE_LOG
3898#endif