blob: 0f212554e644c65eaedcc426b1d5bd929a082312 [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 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200719 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200720 c = getchr();
721 goto nfa_do_multibyte;
722 }
723#endif
724 EMIT(nfa_classcodes[p - classchars]);
725 if (extra == ADD_NL)
726 {
727 EMIT(NFA_NEWL);
728 EMIT(NFA_OR);
729 regflags |= RF_HASNL;
730 }
731 break;
732
733 case Magic('n'):
734 if (reg_string)
735 /* In a string "\n" matches a newline character. */
736 EMIT(NL);
737 else
738 {
739 /* In buffer text "\n" matches the end of a line. */
740 EMIT(NFA_NEWL);
741 regflags |= RF_HASNL;
742 }
743 break;
744
745 case Magic('('):
746 if (nfa_reg(REG_PAREN) == FAIL)
747 return FAIL; /* cascaded error */
748 break;
749
750 case NUL:
751 syntax_error = TRUE;
752 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
753
754 case Magic('|'):
755 case Magic('&'):
756 case Magic(')'):
757 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200758 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 return FAIL;
760
761 case Magic('='):
762 case Magic('?'):
763 case Magic('+'):
764 case Magic('@'):
765 case Magic('*'):
766 case Magic('{'):
767 /* these should follow an atom, not form an atom */
768 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200769 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200770 return FAIL;
771
772 case Magic('~'): /* previous substitute pattern */
773 /* Not supported yet */
774 return FAIL;
775
776 case Magic('1'):
777 case Magic('2'):
778 case Magic('3'):
779 case Magic('4'):
780 case Magic('5'):
781 case Magic('6'):
782 case Magic('7'):
783 case Magic('8'):
784 case Magic('9'):
785 /* not supported yet */
786 return FAIL;
787
788 case Magic('z'):
789 c = no_Magic(getchr());
790 switch (c)
791 {
792 case 's':
793 EMIT(NFA_ZSTART);
794 break;
795 case 'e':
796 EMIT(NFA_ZEND);
797 nfa_has_zend = TRUE;
798 /* TODO: Currently \ze does not work properly. */
799 return FAIL;
800 /* break; */
801 case '1':
802 case '2':
803 case '3':
804 case '4':
805 case '5':
806 case '6':
807 case '7':
808 case '8':
809 case '9':
810 case '(':
811 /* \z1...\z9 and \z( not yet supported */
812 return FAIL;
813 default:
814 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200815 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 no_Magic(c));
817 return FAIL;
818 }
819 break;
820
821 case Magic('%'):
822 c = no_Magic(getchr());
823 switch (c)
824 {
825 /* () without a back reference */
826 case '(':
827 if (nfa_reg(REG_NPAREN) == FAIL)
828 return FAIL;
829 EMIT(NFA_NOPEN);
830 break;
831
832 case 'd': /* %d123 decimal */
833 case 'o': /* %o123 octal */
834 case 'x': /* %xab hex 2 */
835 case 'u': /* %uabcd hex 4 */
836 case 'U': /* %U1234abcd hex 8 */
837 /* Not yet supported */
838 return FAIL;
839
840 c = coll_get_char();
Bram Moolenaar3c577f22013-05-24 21:59:54 +0200841 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200842 break;
843
844 /* Catch \%^ and \%$ regardless of where they appear in the
845 * pattern -- regardless of whether or not it makes sense. */
846 case '^':
847 EMIT(NFA_BOF);
848 /* Not yet supported */
849 return FAIL;
850 break;
851
852 case '$':
853 EMIT(NFA_EOF);
854 /* Not yet supported */
855 return FAIL;
856 break;
857
858 case '#':
859 /* not supported yet */
860 return FAIL;
861 break;
862
863 case 'V':
864 /* not supported yet */
865 return FAIL;
866 break;
867
868 case '[':
869 /* \%[abc] not supported yet */
870 return FAIL;
871
872 default:
873 /* not supported yet */
874 return FAIL;
875 }
876 break;
877
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200878 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200879collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 /*
881 * Glue is emitted between several atoms from the [].
882 * It is either NFA_OR, or NFA_CONCAT.
883 *
884 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
885 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
886 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
887 * notation)
888 *
889 */
890
891
892/* Emit negation atoms, if needed.
893 * The CONCAT below merges the NOT with the previous node. */
894#define TRY_NEG() \
895 if (negated == TRUE) \
896 { \
897 EMIT(NFA_NOT); \
898 }
899
900/* Emit glue between important nodes : CONCAT or OR. */
901#define EMIT_GLUE() \
902 if (first == FALSE) \
903 EMIT(glue); \
904 else \
905 first = FALSE;
906
907 p = regparse;
908 endp = skip_anyof(p);
909 if (*endp == ']')
910 {
911 /*
912 * Try to reverse engineer character classes. For example,
913 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
914 * and perform the necessary substitutions in the NFA.
915 */
916 result = nfa_recognize_char_class(regparse, endp,
917 extra == ADD_NL);
918 if (result != FAIL)
919 {
920 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
921 EMIT(result);
922 else /* must be char class + newline */
923 {
924 EMIT(result - ADD_NL);
925 EMIT(NFA_NEWL);
926 EMIT(NFA_OR);
927 }
928 regparse = endp;
929 nfa_inc(&regparse);
930 return OK;
931 }
932 /*
933 * Failed to recognize a character class. Use the simple
934 * version that turns [abc] into 'a' OR 'b' OR 'c'
935 */
936 startc = endc = oldstartc = -1;
937 first = TRUE; /* Emitting first atom in this sequence? */
938 negated = FALSE;
939 glue = NFA_OR;
940 if (*regparse == '^') /* negated range */
941 {
942 negated = TRUE;
943 glue = NFA_CONCAT;
944 nfa_inc(&regparse);
945 }
946 if (*regparse == '-')
947 {
948 startc = '-';
949 EMIT(startc);
950 TRY_NEG();
951 EMIT_GLUE();
952 nfa_inc(&regparse);
953 }
954 /* Emit the OR branches for each character in the [] */
955 emit_range = FALSE;
956 while (regparse < endp)
957 {
958 oldstartc = startc;
959 startc = -1;
960 got_coll_char = FALSE;
961 if (*regparse == '[')
962 {
963 /* Check for [: :], [= =], [. .] */
964 equiclass = collclass = 0;
965 charclass = get_char_class(&regparse);
966 if (charclass == CLASS_NONE)
967 {
968 equiclass = get_equi_class(&regparse);
969 if (equiclass == 0)
970 collclass = get_coll_element(&regparse);
971 }
972
973 /* Character class like [:alpha:] */
974 if (charclass != CLASS_NONE)
975 {
976 switch (charclass)
977 {
978 case CLASS_ALNUM:
979 EMIT(NFA_CLASS_ALNUM);
980 break;
981 case CLASS_ALPHA:
982 EMIT(NFA_CLASS_ALPHA);
983 break;
984 case CLASS_BLANK:
985 EMIT(NFA_CLASS_BLANK);
986 break;
987 case CLASS_CNTRL:
988 EMIT(NFA_CLASS_CNTRL);
989 break;
990 case CLASS_DIGIT:
991 EMIT(NFA_CLASS_DIGIT);
992 break;
993 case CLASS_GRAPH:
994 EMIT(NFA_CLASS_GRAPH);
995 break;
996 case CLASS_LOWER:
997 EMIT(NFA_CLASS_LOWER);
998 break;
999 case CLASS_PRINT:
1000 EMIT(NFA_CLASS_PRINT);
1001 break;
1002 case CLASS_PUNCT:
1003 EMIT(NFA_CLASS_PUNCT);
1004 break;
1005 case CLASS_SPACE:
1006 EMIT(NFA_CLASS_SPACE);
1007 break;
1008 case CLASS_UPPER:
1009 EMIT(NFA_CLASS_UPPER);
1010 break;
1011 case CLASS_XDIGIT:
1012 EMIT(NFA_CLASS_XDIGIT);
1013 break;
1014 case CLASS_TAB:
1015 EMIT(NFA_CLASS_TAB);
1016 break;
1017 case CLASS_RETURN:
1018 EMIT(NFA_CLASS_RETURN);
1019 break;
1020 case CLASS_BACKSPACE:
1021 EMIT(NFA_CLASS_BACKSPACE);
1022 break;
1023 case CLASS_ESCAPE:
1024 EMIT(NFA_CLASS_ESCAPE);
1025 break;
1026 }
1027 TRY_NEG();
1028 EMIT_GLUE();
1029 continue;
1030 }
1031 /* Try equivalence class [=a=] and the like */
1032 if (equiclass != 0)
1033 {
1034 result = nfa_emit_equi_class(equiclass, negated);
1035 if (result == FAIL)
1036 {
1037 /* should never happen */
1038 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1039 }
1040 EMIT_GLUE();
1041 continue;
1042 }
1043 /* Try collating class like [. .] */
1044 if (collclass != 0)
1045 {
1046 startc = collclass; /* allow [.a.]-x as a range */
1047 /* Will emit the proper atom at the end of the
1048 * while loop. */
1049 }
1050 }
1051 /* Try a range like 'a-x' or '\t-z' */
1052 if (*regparse == '-')
1053 {
1054 emit_range = TRUE;
1055 startc = oldstartc;
1056 nfa_inc(&regparse);
1057 continue; /* reading the end of the range */
1058 }
1059
1060 /* Now handle simple and escaped characters.
1061 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1062 * accepts "\t", "\e", etc., but only when the 'l' flag in
1063 * 'cpoptions' is not included.
1064 * Posix doesn't recognize backslash at all.
1065 */
1066 if (*regparse == '\\'
1067 && !cpo_bsl
1068 && regparse + 1 <= endp
1069 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1070 || (!cpo_lit
1071 && vim_strchr(REGEXP_ABBR, regparse[1])
1072 != NULL)
1073 )
1074 )
1075 {
1076 nfa_inc(&regparse);
1077
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001078 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001079 startc = reg_string ? NL : NFA_NEWL;
1080 else
1081 if (*regparse == 'd'
1082 || *regparse == 'o'
1083 || *regparse == 'x'
1084 || *regparse == 'u'
1085 || *regparse == 'U'
1086 )
1087 {
1088 /* TODO(RE) This needs more testing */
1089 startc = coll_get_char();
1090 got_coll_char = TRUE;
1091 nfa_dec(&regparse);
1092 }
1093 else
1094 {
1095 /* \r,\t,\e,\b */
1096 startc = backslash_trans(*regparse);
1097 }
1098 }
1099
1100 /* Normal printable char */
1101 if (startc == -1)
1102#ifdef FEAT_MBYTE
1103 startc = (*mb_ptr2char)(regparse);
1104#else
1105 startc = *regparse;
1106#endif
1107
1108 /* Previous char was '-', so this char is end of range. */
1109 if (emit_range)
1110 {
1111 endc = startc; startc = oldstartc;
1112 if (startc > endc)
1113 EMSG_RET_FAIL(_(e_invrange));
1114#ifdef FEAT_MBYTE
1115 if (has_mbyte && ((*mb_char2len)(startc) > 1
1116 || (*mb_char2len)(endc) > 1))
1117 {
1118 if (endc > startc + 256)
1119 EMSG_RET_FAIL(_(e_invrange));
1120 /* Emit the range. "startc" was already emitted, so
1121 * skip it. */
1122 for (c = startc + 1; c <= endc; c++)
1123 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001124 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001125 TRY_NEG();
1126 EMIT_GLUE();
1127 }
1128 emit_range = FALSE;
1129 }
1130 else
1131#endif
1132 {
1133#ifdef EBCDIC
1134 int alpha_only = FALSE;
1135
1136 /* for alphabetical range skip the gaps
1137 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1138 if (isalpha(startc) && isalpha(endc))
1139 alpha_only = TRUE;
1140#endif
1141 /* Emit the range. "startc" was already emitted, so
1142 * skip it. */
1143 for (c = startc + 1; c <= endc; c++)
1144#ifdef EBCDIC
1145 if (!alpha_only || isalpha(startc))
1146#endif
1147 {
1148 EMIT(c);
1149 TRY_NEG();
1150 EMIT_GLUE();
1151 }
1152 emit_range = FALSE;
1153 }
1154 }
1155 else
1156 {
1157 /*
1158 * This char (startc) is not part of a range. Just
1159 * emit it.
1160 *
1161 * Normally, simply emit startc. But if we get char
1162 * code=0 from a collating char, then replace it with
1163 * 0x0a.
1164 *
1165 * This is needed to completely mimic the behaviour of
1166 * the backtracking engine.
1167 */
1168 if (got_coll_char == TRUE && startc == 0)
1169 EMIT(0x0a);
1170 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001171 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001172 TRY_NEG();
1173 EMIT_GLUE();
1174 }
1175
1176 nfa_inc(&regparse);
1177 } /* while (p < endp) */
1178
1179 nfa_dec(&regparse);
1180 if (*regparse == '-') /* if last, '-' is just a char */
1181 {
1182 EMIT('-');
1183 TRY_NEG();
1184 EMIT_GLUE();
1185 }
1186 nfa_inc(&regparse);
1187
1188 if (extra == ADD_NL) /* \_[] also matches \n */
1189 {
1190 EMIT(reg_string ? NL : NFA_NEWL);
1191 TRY_NEG();
1192 EMIT_GLUE();
1193 }
1194
1195 /* skip the trailing ] */
1196 regparse = endp;
1197 nfa_inc(&regparse);
1198 if (negated == TRUE)
1199 {
1200 /* Mark end of negated char range */
1201 EMIT(NFA_END_NEG_RANGE);
1202 EMIT(NFA_CONCAT);
1203 }
1204 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001205 } /* if exists closing ] */
1206
1207 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 {
1209 syntax_error = TRUE;
1210 EMSG_RET_FAIL(_(e_missingbracket));
1211 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001212 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 default:
1215 {
1216#ifdef FEAT_MBYTE
1217 int plen;
1218
1219nfa_do_multibyte:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001220 /* Length of current char with composing chars. */
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001221 if (enc_utf8 && (clen != (plen = (*mb_ptr2len)(old_regparse))
1222 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001224 /* A base character plus composing characters, or just one
1225 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001226 * This requires creating a separate atom as if enclosing
1227 * the characters in (), where NFA_COMPOSING is the ( and
1228 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001229 * building the postfix form, not the NFA itself;
1230 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001231 * where 'b' and 'c' are chars with codes > 256. */
1232 i = 0;
1233 for (;;)
1234 {
1235 EMIT(c);
1236 if (i > 0)
1237 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001238 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001239 break;
1240 c = utf_ptr2char(old_regparse + i);
1241 }
1242 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 regparse = old_regparse + plen;
1244 }
1245 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246#endif
1247 {
1248 c = no_Magic(c);
1249 EMIT(c);
1250 }
1251 return OK;
1252 }
1253 }
1254
1255#undef TRY_NEG
1256#undef EMIT_GLUE
1257
1258 return OK;
1259}
1260
1261/*
1262 * Parse something followed by possible [*+=].
1263 *
1264 * A piece is an atom, possibly followed by a multi, an indication of how many
1265 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1266 * characters: "", "a", "aa", etc.
1267 *
1268 * piece ::= atom
1269 * or atom multi
1270 */
1271 static int
1272nfa_regpiece()
1273{
1274 int i;
1275 int op;
1276 int ret;
1277 long minval, maxval;
1278 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1279 char_u *old_regparse, *new_regparse;
1280 int c2;
1281 int *old_post_ptr, *my_post_start;
1282 int old_regnpar;
1283 int quest;
1284
1285 /* Save the current position in the regexp, so that we can use it if
1286 * <atom>{m,n} is next. */
1287 old_regparse = regparse;
1288 /* Save current number of open parenthesis, so we can use it if
1289 * <atom>{m,n} is next */
1290 old_regnpar = regnpar;
1291 /* store current pos in the postfix form, for \{m,n} involving 0s */
1292 my_post_start = post_ptr;
1293
1294 ret = nfa_regatom();
1295 if (ret == FAIL)
1296 return FAIL; /* cascaded error */
1297
1298 op = peekchr();
1299 if (re_multi_type(op) == NOT_MULTI)
1300 return OK;
1301
1302 skipchr();
1303 switch (op)
1304 {
1305 case Magic('*'):
1306 EMIT(NFA_STAR);
1307 break;
1308
1309 case Magic('+'):
1310 /*
1311 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1312 * first and only submatch would be "aaa". But the backtracking
1313 * engine interprets the plus as "try matching one more time", and
1314 * a* matches a second time at the end of the input, the empty
1315 * string.
1316 * The submatch will the empty string.
1317 *
1318 * In order to be consistent with the old engine, we disable
1319 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1320 */
1321 /* EMIT(NFA_PLUS); */
1322 regnpar = old_regnpar;
1323 regparse = old_regparse;
1324 curchr = -1;
1325 if (nfa_regatom() == FAIL)
1326 return FAIL;
1327 EMIT(NFA_STAR);
1328 EMIT(NFA_CONCAT);
1329 skipchr(); /* skip the \+ */
1330 break;
1331
1332 case Magic('@'):
1333 op = no_Magic(getchr());
1334 switch(op)
1335 {
1336 case '=':
1337 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1338 break;
1339 case '!':
1340 case '<':
1341 case '>':
1342 /* Not supported yet */
1343 return FAIL;
1344 default:
1345 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001346 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001347 return FAIL;
1348 }
1349 break;
1350
1351 case Magic('?'):
1352 case Magic('='):
1353 EMIT(NFA_QUEST);
1354 break;
1355
1356 case Magic('{'):
1357 /* a{2,5} will expand to 'aaa?a?a?'
1358 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1359 * version of '?'
1360 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1361 * parenthesis have the same id
1362 */
1363
1364 greedy = TRUE;
1365 c2 = peekchr();
1366 if (c2 == '-' || c2 == Magic('-'))
1367 {
1368 skipchr();
1369 greedy = FALSE;
1370 }
1371 if (!read_limits(&minval, &maxval))
1372 {
1373 syntax_error = TRUE;
1374 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1375 }
1376 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1377 * <atom>* */
1378 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1379 {
1380 EMIT(NFA_STAR);
1381 break;
1382 }
1383
1384 if (maxval > NFA_BRACES_MAXLIMIT)
1385 {
1386 /* This would yield a huge automaton and use too much memory.
1387 * Revert to old engine */
1388 return FAIL;
1389 }
1390
1391 /* Special case: x{0} or x{-0} */
1392 if (maxval == 0)
1393 {
1394 /* Ignore result of previous call to nfa_regatom() */
1395 post_ptr = my_post_start;
1396 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1397 EMIT(NFA_SKIP_CHAR);
1398 return OK;
1399 }
1400
1401 /* Ignore previous call to nfa_regatom() */
1402 post_ptr = my_post_start;
1403 /* Save pos after the repeated atom and the \{} */
1404 new_regparse = regparse;
1405
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1407 for (i = 0; i < maxval; i++)
1408 {
1409 /* Goto beginning of the repeated atom */
1410 regparse = old_regparse;
1411 curchr = -1;
1412 /* Restore count of parenthesis */
1413 regnpar = old_regnpar;
1414 old_post_ptr = post_ptr;
1415 if (nfa_regatom() == FAIL)
1416 return FAIL;
1417 /* after "minval" times, atoms are optional */
1418 if (i + 1 > minval)
1419 EMIT(quest);
1420 if (old_post_ptr != my_post_start)
1421 EMIT(NFA_CONCAT);
1422 }
1423
1424 /* Go to just after the repeated atom and the \{} */
1425 regparse = new_regparse;
1426 curchr = -1;
1427
1428 break;
1429
1430
1431 default:
1432 break;
1433 } /* end switch */
1434
1435 if (re_multi_type(peekchr()) != NOT_MULTI)
1436 {
1437 /* Can't have a multi follow a multi. */
1438 syntax_error = TRUE;
1439 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1440 }
1441
1442 return OK;
1443}
1444
1445/*
1446 * Parse one or more pieces, concatenated. It matches a match for the
1447 * first piece, followed by a match for the second piece, etc. Example:
1448 * "f[0-9]b", first matches "f", then a digit and then "b".
1449 *
1450 * concat ::= piece
1451 * or piece piece
1452 * or piece piece piece
1453 * etc.
1454 */
1455 static int
1456nfa_regconcat()
1457{
1458 int cont = TRUE;
1459 int first = TRUE;
1460
1461 while (cont)
1462 {
1463 switch (peekchr())
1464 {
1465 case NUL:
1466 case Magic('|'):
1467 case Magic('&'):
1468 case Magic(')'):
1469 cont = FALSE;
1470 break;
1471
1472 case Magic('Z'):
1473#ifdef FEAT_MBYTE
1474 regflags |= RF_ICOMBINE;
1475#endif
1476 skipchr_keepstart();
1477 break;
1478 case Magic('c'):
1479 regflags |= RF_ICASE;
1480 skipchr_keepstart();
1481 break;
1482 case Magic('C'):
1483 regflags |= RF_NOICASE;
1484 skipchr_keepstart();
1485 break;
1486 case Magic('v'):
1487 reg_magic = MAGIC_ALL;
1488 skipchr_keepstart();
1489 curchr = -1;
1490 break;
1491 case Magic('m'):
1492 reg_magic = MAGIC_ON;
1493 skipchr_keepstart();
1494 curchr = -1;
1495 break;
1496 case Magic('M'):
1497 reg_magic = MAGIC_OFF;
1498 skipchr_keepstart();
1499 curchr = -1;
1500 break;
1501 case Magic('V'):
1502 reg_magic = MAGIC_NONE;
1503 skipchr_keepstart();
1504 curchr = -1;
1505 break;
1506
1507 default:
1508 if (nfa_regpiece() == FAIL)
1509 return FAIL;
1510 if (first == FALSE)
1511 EMIT(NFA_CONCAT);
1512 else
1513 first = FALSE;
1514 break;
1515 }
1516 }
1517
1518 return OK;
1519}
1520
1521/*
1522 * Parse a branch, one or more concats, separated by "\&". It matches the
1523 * last concat, but only if all the preceding concats also match at the same
1524 * position. Examples:
1525 * "foobeep\&..." matches "foo" in "foobeep".
1526 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1527 *
1528 * branch ::= concat
1529 * or concat \& concat
1530 * or concat \& concat \& concat
1531 * etc.
1532 */
1533 static int
1534nfa_regbranch()
1535{
1536 int ch;
1537 int *old_post_ptr;
1538
1539 old_post_ptr = post_ptr;
1540
1541 /* First branch, possibly the only one */
1542 if (nfa_regconcat() == FAIL)
1543 return FAIL;
1544
1545 ch = peekchr();
1546 /* Try next concats */
1547 while (ch == Magic('&'))
1548 {
1549 skipchr();
1550 EMIT(NFA_NOPEN);
1551 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1552 old_post_ptr = post_ptr;
1553 if (nfa_regconcat() == FAIL)
1554 return FAIL;
1555 /* if concat is empty, skip a input char. But do emit a node */
1556 if (old_post_ptr == post_ptr)
1557 EMIT(NFA_SKIP_CHAR);
1558 EMIT(NFA_CONCAT);
1559 ch = peekchr();
1560 }
1561
1562 /* Even if a branch is empty, emit one node for it */
1563 if (old_post_ptr == post_ptr)
1564 EMIT(NFA_SKIP_CHAR);
1565
1566 return OK;
1567}
1568
1569/*
1570 * Parse a pattern, one or more branches, separated by "\|". It matches
1571 * anything that matches one of the branches. Example: "foo\|beep" matches
1572 * "foo" and matches "beep". If more than one branch matches, the first one
1573 * is used.
1574 *
1575 * pattern ::= branch
1576 * or branch \| branch
1577 * or branch \| branch \| branch
1578 * etc.
1579 */
1580 static int
1581nfa_reg(paren)
1582 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1583{
1584 int parno = 0;
1585
1586#ifdef FEAT_SYN_HL
1587#endif
1588 if (paren == REG_PAREN)
1589 {
1590 if (regnpar >= NSUBEXP) /* Too many `(' */
1591 {
1592 syntax_error = TRUE;
1593 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1594 }
1595 parno = regnpar++;
1596 }
1597
1598 if (nfa_regbranch() == FAIL)
1599 return FAIL; /* cascaded error */
1600
1601 while (peekchr() == Magic('|'))
1602 {
1603 skipchr();
1604 if (nfa_regbranch() == FAIL)
1605 return FAIL; /* cascaded error */
1606 EMIT(NFA_OR);
1607 }
1608
1609 /* Check for proper termination. */
1610 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1611 {
1612 syntax_error = TRUE;
1613 if (paren == REG_NPAREN)
1614 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1615 else
1616 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1617 }
1618 else if (paren == REG_NOPAREN && peekchr() != NUL)
1619 {
1620 syntax_error = TRUE;
1621 if (peekchr() == Magic(')'))
1622 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1623 else
1624 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1625 }
1626 /*
1627 * Here we set the flag allowing back references to this set of
1628 * parentheses.
1629 */
1630 if (paren == REG_PAREN)
1631 {
1632 had_endbrace[parno] = TRUE; /* have seen the close paren */
1633 EMIT(NFA_MOPEN + parno);
1634 }
1635
1636 return OK;
1637}
1638
1639typedef struct
1640{
1641 char_u *start[NSUBEXP];
1642 char_u *end[NSUBEXP];
1643 lpos_T startpos[NSUBEXP];
1644 lpos_T endpos[NSUBEXP];
1645} regsub_T;
1646
1647static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
1648
1649#ifdef DEBUG
1650static char_u code[50];
1651
1652 static void
1653nfa_set_code(c)
1654 int c;
1655{
1656 int addnl = FALSE;
1657
1658 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1659 {
1660 addnl = TRUE;
1661 c -= ADD_NL;
1662 }
1663
1664 STRCPY(code, "");
1665 switch (c)
1666 {
1667 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1668 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1669 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1670 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1671 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1672 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1673
1674 case NFA_PREV_ATOM_NO_WIDTH:
1675 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1676 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1677 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1678 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1679 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1680
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1682 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1683
1684 case NFA_MOPEN + 0:
1685 case NFA_MOPEN + 1:
1686 case NFA_MOPEN + 2:
1687 case NFA_MOPEN + 3:
1688 case NFA_MOPEN + 4:
1689 case NFA_MOPEN + 5:
1690 case NFA_MOPEN + 6:
1691 case NFA_MOPEN + 7:
1692 case NFA_MOPEN + 8:
1693 case NFA_MOPEN + 9:
1694 STRCPY(code, "NFA_MOPEN(x)");
1695 code[10] = c - NFA_MOPEN + '0';
1696 break;
1697 case NFA_MCLOSE + 0:
1698 case NFA_MCLOSE + 1:
1699 case NFA_MCLOSE + 2:
1700 case NFA_MCLOSE + 3:
1701 case NFA_MCLOSE + 4:
1702 case NFA_MCLOSE + 5:
1703 case NFA_MCLOSE + 6:
1704 case NFA_MCLOSE + 7:
1705 case NFA_MCLOSE + 8:
1706 case NFA_MCLOSE + 9:
1707 STRCPY(code, "NFA_MCLOSE(x)");
1708 code[11] = c - NFA_MCLOSE + '0';
1709 break;
1710 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1711 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1712 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1713 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1714 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1715 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1716 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1717 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1718 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1719 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1720 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1721 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1722 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1723 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1724 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1725 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1726 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1727 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1728 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1729 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1730 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1731 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1732 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1733 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1734 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1735 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1736 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1737 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1738
1739 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1740 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1741 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1742 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1743 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1744 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1745 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1746 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1747 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1748 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1749 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1750 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1751 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1752 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1753 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1754 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1755 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1756 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1757 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1758 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1759 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1760 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1761 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1762 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1763 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1764 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1765 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1766
1767 default:
1768 STRCPY(code, "CHAR(x)");
1769 code[5] = c;
1770 }
1771
1772 if (addnl == TRUE)
1773 STRCAT(code, " + NEWLINE ");
1774
1775}
1776
1777#ifdef ENABLE_LOG
1778static FILE *log_fd;
1779
1780/*
1781 * Print the postfix notation of the current regexp.
1782 */
1783 static void
1784nfa_postfix_dump(expr, retval)
1785 char_u *expr;
1786 int retval;
1787{
1788 int *p;
1789 FILE *f;
1790
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001791 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001792 if (f != NULL)
1793 {
1794 fprintf(f, "\n-------------------------\n");
1795 if (retval == FAIL)
1796 fprintf(f, ">>> NFA engine failed ... \n");
1797 else if (retval == OK)
1798 fprintf(f, ">>> NFA engine succeeded !\n");
1799 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001800 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 {
1802 nfa_set_code(*p);
1803 fprintf(f, "%s, ", code);
1804 }
1805 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001806 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001807 fprintf(f, "%d ", *p);
1808 fprintf(f, "\n\n");
1809 fclose(f);
1810 }
1811}
1812
1813/*
1814 * Print the NFA starting with a root node "state".
1815 */
1816 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001817nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818 FILE *debugf;
1819 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001821 garray_T indent;
1822
1823 ga_init2(&indent, 1, 64);
1824 ga_append(&indent, '\0');
1825 nfa_print_state2(debugf, state, &indent);
1826 ga_clear(&indent);
1827}
1828
1829 static void
1830nfa_print_state2(debugf, state, indent)
1831 FILE *debugf;
1832 nfa_state_T *state;
1833 garray_T *indent;
1834{
1835 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836
1837 if (state == NULL)
1838 return;
1839
1840 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001841
1842 /* Output indent */
1843 p = (char_u *)indent->ga_data;
1844 if (indent->ga_len >= 3)
1845 {
1846 int last = indent->ga_len - 3;
1847 char_u save[2];
1848
1849 STRNCPY(save, &p[last], 2);
1850 STRNCPY(&p[last], "+-", 2);
1851 fprintf(debugf, " %s", p);
1852 STRNCPY(&p[last], save, 2);
1853 }
1854 else
1855 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856
1857 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001858 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1859 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001860 if (state->id < 0)
1861 return;
1862
1863 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001864
1865 /* grow indent for state->out */
1866 indent->ga_len -= 1;
1867 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001868 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001869 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001870 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001871 ga_append(indent, '\0');
1872
1873 nfa_print_state2(debugf, state->out, indent);
1874
1875 /* replace last part of indent for state->out1 */
1876 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001877 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001878 ga_append(indent, '\0');
1879
1880 nfa_print_state2(debugf, state->out1, indent);
1881
1882 /* shrink indent */
1883 indent->ga_len -= 3;
1884 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885}
1886
1887/*
1888 * Print the NFA state machine.
1889 */
1890 static void
1891nfa_dump(prog)
1892 nfa_regprog_T *prog;
1893{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001894 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895
1896 if (debugf != NULL)
1897 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001898 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 fclose(debugf);
1900 }
1901}
1902#endif /* ENABLE_LOG */
1903#endif /* DEBUG */
1904
1905/*
1906 * Parse r.e. @expr and convert it into postfix form.
1907 * Return the postfix string on success, NULL otherwise.
1908 */
1909 static int *
1910re2post()
1911{
1912 if (nfa_reg(REG_NOPAREN) == FAIL)
1913 return NULL;
1914 EMIT(NFA_MOPEN);
1915 return post_start;
1916}
1917
1918/* NB. Some of the code below is inspired by Russ's. */
1919
1920/*
1921 * Represents an NFA state plus zero or one or two arrows exiting.
1922 * if c == MATCH, no arrows out; matching state.
1923 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1924 * If c < 256, labeled arrow with character c to out.
1925 */
1926
1927static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1928
1929/*
1930 * Allocate and initialize nfa_state_T.
1931 */
1932 static nfa_state_T *
1933new_state(c, out, out1)
1934 int c;
1935 nfa_state_T *out;
1936 nfa_state_T *out1;
1937{
1938 nfa_state_T *s;
1939
1940 if (istate >= nstate)
1941 return NULL;
1942
1943 s = &state_ptr[istate++];
1944
1945 s->c = c;
1946 s->out = out;
1947 s->out1 = out1;
1948
1949 s->id = istate;
1950 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 s->visits = 0;
1952 s->negated = FALSE;
1953
1954 return s;
1955}
1956
1957/*
1958 * A partially built NFA without the matching state filled in.
1959 * Frag_T.start points at the start state.
1960 * Frag_T.out is a list of places that need to be set to the
1961 * next state for this fragment.
1962 */
1963typedef union Ptrlist Ptrlist;
1964struct Frag
1965{
1966 nfa_state_T *start;
1967 Ptrlist *out;
1968};
1969typedef struct Frag Frag_T;
1970
1971static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1972static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1973static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1974static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1975static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1976static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1977
1978/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001979 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001980 */
1981 static Frag_T
1982frag(start, out)
1983 nfa_state_T *start;
1984 Ptrlist *out;
1985{
Bram Moolenaar053bb602013-05-20 13:55:21 +02001986 Frag_T n;
1987
1988 n.start = start;
1989 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001990 return n;
1991}
1992
1993/*
1994 * Since the out pointers in the list are always
1995 * uninitialized, we use the pointers themselves
1996 * as storage for the Ptrlists.
1997 */
1998union Ptrlist
1999{
2000 Ptrlist *next;
2001 nfa_state_T *s;
2002};
2003
2004/*
2005 * Create singleton list containing just outp.
2006 */
2007 static Ptrlist *
2008list1(outp)
2009 nfa_state_T **outp;
2010{
2011 Ptrlist *l;
2012
2013 l = (Ptrlist *)outp;
2014 l->next = NULL;
2015 return l;
2016}
2017
2018/*
2019 * Patch the list of states at out to point to start.
2020 */
2021 static void
2022patch(l, s)
2023 Ptrlist *l;
2024 nfa_state_T *s;
2025{
2026 Ptrlist *next;
2027
2028 for (; l; l = next)
2029 {
2030 next = l->next;
2031 l->s = s;
2032 }
2033}
2034
2035
2036/*
2037 * Join the two lists l1 and l2, returning the combination.
2038 */
2039 static Ptrlist *
2040append(l1, l2)
2041 Ptrlist *l1;
2042 Ptrlist *l2;
2043{
2044 Ptrlist *oldl1;
2045
2046 oldl1 = l1;
2047 while (l1->next)
2048 l1 = l1->next;
2049 l1->next = l2;
2050 return oldl1;
2051}
2052
2053/*
2054 * Stack used for transforming postfix form into NFA.
2055 */
2056static Frag_T empty;
2057
2058 static void
2059st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002060 int *postfix UNUSED;
2061 int *end UNUSED;
2062 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002064#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 FILE *df;
2066 int *p2;
2067
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002068 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 if (df)
2070 {
2071 fprintf(df, "Error popping the stack!\n");
2072#ifdef DEBUG
2073 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2074#endif
2075 fprintf(df, "Postfix form is: ");
2076#ifdef DEBUG
2077 for (p2 = postfix; p2 < end; p2++)
2078 {
2079 nfa_set_code(*p2);
2080 fprintf(df, "%s, ", code);
2081 }
2082 nfa_set_code(*p);
2083 fprintf(df, "\nCurrent position is: ");
2084 for (p2 = postfix; p2 <= p; p2 ++)
2085 {
2086 nfa_set_code(*p2);
2087 fprintf(df, "%s, ", code);
2088 }
2089#else
2090 for (p2 = postfix; p2 < end; p2++)
2091 {
2092 fprintf(df, "%d, ", *p2);
2093 }
2094 fprintf(df, "\nCurrent position is: ");
2095 for (p2 = postfix; p2 <= p; p2 ++)
2096 {
2097 fprintf(df, "%d, ", *p2);
2098 }
2099#endif
2100 fprintf(df, "\n--------------------------\n");
2101 fclose(df);
2102 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002103#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 EMSG(_("E874: (NFA) Could not pop the stack !"));
2105}
2106
2107/*
2108 * Push an item onto the stack.
2109 */
2110 static void
2111st_push(s, p, stack_end)
2112 Frag_T s;
2113 Frag_T **p;
2114 Frag_T *stack_end;
2115{
2116 Frag_T *stackp = *p;
2117
2118 if (stackp >= stack_end)
2119 return;
2120 *stackp = s;
2121 *p = *p + 1;
2122}
2123
2124/*
2125 * Pop an item from the stack.
2126 */
2127 static Frag_T
2128st_pop(p, stack)
2129 Frag_T **p;
2130 Frag_T *stack;
2131{
2132 Frag_T *stackp;
2133
2134 *p = *p - 1;
2135 stackp = *p;
2136 if (stackp < stack)
2137 return empty;
2138 return **p;
2139}
2140
2141/*
2142 * Convert a postfix form into its equivalent NFA.
2143 * Return the NFA start state on success, NULL otherwise.
2144 */
2145 static nfa_state_T *
2146post2nfa(postfix, end, nfa_calc_size)
2147 int *postfix;
2148 int *end;
2149 int nfa_calc_size;
2150{
2151 int *p;
2152 int mopen;
2153 int mclose;
2154 Frag_T *stack = NULL;
2155 Frag_T *stackp = NULL;
2156 Frag_T *stack_end = NULL;
2157 Frag_T e1;
2158 Frag_T e2;
2159 Frag_T e;
2160 nfa_state_T *s;
2161 nfa_state_T *s1;
2162 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002163 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164
2165 if (postfix == NULL)
2166 return NULL;
2167
Bram Moolenaar053bb602013-05-20 13:55:21 +02002168#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169#define POP() st_pop(&stackp, stack); \
2170 if (stackp < stack) \
2171 { \
2172 st_error(postfix, end, p); \
2173 return NULL; \
2174 }
2175
2176 if (nfa_calc_size == FALSE)
2177 {
2178 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002179 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002181 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 }
2183
2184 for (p = postfix; p < end; ++p)
2185 {
2186 switch (*p)
2187 {
2188 case NFA_CONCAT:
2189 /* Catenation.
2190 * Pay attention: this operator does not exist
2191 * in the r.e. itself (it is implicit, really).
2192 * It is added when r.e. is translated to postfix
2193 * form in re2post().
2194 *
2195 * No new state added here. */
2196 if (nfa_calc_size == TRUE)
2197 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002198 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002199 break;
2200 }
2201 e2 = POP();
2202 e1 = POP();
2203 patch(e1.out, e2.start);
2204 PUSH(frag(e1.start, e2.out));
2205 break;
2206
2207 case NFA_NOT:
2208 /* Negation of a character */
2209 if (nfa_calc_size == TRUE)
2210 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002211 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212 break;
2213 }
2214 e1 = POP();
2215 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002216#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002217 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002219#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 PUSH(e1);
2221 break;
2222
2223 case NFA_OR:
2224 /* Alternation */
2225 if (nfa_calc_size == TRUE)
2226 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002227 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 break;
2229 }
2230 e2 = POP();
2231 e1 = POP();
2232 s = new_state(NFA_SPLIT, e1.start, e2.start);
2233 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002234 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235 PUSH(frag(s, append(e1.out, e2.out)));
2236 break;
2237
2238 case NFA_STAR:
2239 /* Zero or more */
2240 if (nfa_calc_size == TRUE)
2241 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002242 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 break;
2244 }
2245 e = POP();
2246 s = new_state(NFA_SPLIT, e.start, NULL);
2247 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002248 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002249 patch(e.out, s);
2250 PUSH(frag(s, list1(&s->out1)));
2251 break;
2252
2253 case NFA_QUEST:
2254 /* one or zero atoms=> greedy match */
2255 if (nfa_calc_size == TRUE)
2256 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002257 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002258 break;
2259 }
2260 e = POP();
2261 s = new_state(NFA_SPLIT, e.start, NULL);
2262 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002263 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264 PUSH(frag(s, append(e.out, list1(&s->out1))));
2265 break;
2266
2267 case NFA_QUEST_NONGREEDY:
2268 /* zero or one atoms => non-greedy match */
2269 if (nfa_calc_size == TRUE)
2270 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002271 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272 break;
2273 }
2274 e = POP();
2275 s = new_state(NFA_SPLIT, NULL, e.start);
2276 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002277 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278 PUSH(frag(s, append(e.out, list1(&s->out))));
2279 break;
2280
2281 case NFA_PLUS:
2282 /* One or more */
2283 if (nfa_calc_size == TRUE)
2284 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002285 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 break;
2287 }
2288 e = POP();
2289 s = new_state(NFA_SPLIT, e.start, NULL);
2290 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002291 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002292 patch(e.out, s);
2293 PUSH(frag(e.start, list1(&s->out1)));
2294 break;
2295
2296 case NFA_SKIP_CHAR:
2297 /* Symbol of 0-length, Used in a repetition
2298 * with max/min count of 0 */
2299 if (nfa_calc_size == TRUE)
2300 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002301 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302 break;
2303 }
2304 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2305 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002306 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002307 PUSH(frag(s, list1(&s->out)));
2308 break;
2309
2310 case NFA_PREV_ATOM_NO_WIDTH:
2311 /* The \@= operator: match the preceding atom with 0 width.
2312 * Surrounds the preceding atom with START_INVISIBLE and
2313 * END_INVISIBLE, similarly to MOPEN.
2314 */
2315 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002316 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317
2318 if (nfa_calc_size == TRUE)
2319 {
2320 nstate += 2;
2321 break;
2322 }
2323 e = POP();
2324 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2325 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002326 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002327 patch(e.out, s1);
2328
2329 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2330 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002331 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002332 PUSH(frag(s, list1(&s1->out)));
2333 break;
2334
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002335#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002336 case NFA_COMPOSING: /* char with composing char */
2337#if 0
2338 /* TODO */
2339 if (regflags & RF_ICOMBINE)
2340 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002341 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002342 }
2343#endif
2344 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002345#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002346
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347 case NFA_MOPEN + 0: /* Submatch */
2348 case NFA_MOPEN + 1:
2349 case NFA_MOPEN + 2:
2350 case NFA_MOPEN + 3:
2351 case NFA_MOPEN + 4:
2352 case NFA_MOPEN + 5:
2353 case NFA_MOPEN + 6:
2354 case NFA_MOPEN + 7:
2355 case NFA_MOPEN + 8:
2356 case NFA_MOPEN + 9:
2357 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 if (nfa_calc_size == TRUE)
2359 {
2360 nstate += 2;
2361 break;
2362 }
2363
2364 mopen = *p;
2365 switch (*p)
2366 {
2367 case NFA_NOPEN:
2368 mclose = NFA_NCLOSE;
2369 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002370#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 case NFA_COMPOSING:
2372 mclose = NFA_END_COMPOSING;
2373 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002374#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002375 default:
2376 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2377 mclose = *p + NSUBEXP;
2378 break;
2379 }
2380
2381 /* Allow "NFA_MOPEN" as a valid postfix representation for
2382 * the empty regexp "". In this case, the NFA will be
2383 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2384 * empty groups of parenthesis, and empty mbyte chars */
2385 if (stackp == stack)
2386 {
2387 s = new_state(mopen, NULL, NULL);
2388 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002389 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002390 s1 = new_state(mclose, NULL, NULL);
2391 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002392 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393 patch(list1(&s->out), s1);
2394 PUSH(frag(s, list1(&s1->out)));
2395 break;
2396 }
2397
2398 /* At least one node was emitted before NFA_MOPEN, so
2399 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2400 e = POP();
2401 s = new_state(mopen, e.start, NULL); /* `(' */
2402 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002403 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002404
2405 s1 = new_state(mclose, NULL, NULL); /* `)' */
2406 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002407 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002408 patch(e.out, s1);
2409
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002410#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002411 if (mopen == NFA_COMPOSING)
2412 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002413 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002414#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415
2416 PUSH(frag(s, list1(&s1->out)));
2417 break;
2418
2419 case NFA_ZSTART:
2420 case NFA_ZEND:
2421 default:
2422 /* Operands */
2423 if (nfa_calc_size == TRUE)
2424 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002425 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002426 break;
2427 }
2428 s = new_state(*p, NULL, NULL);
2429 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002430 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 PUSH(frag(s, list1(&s->out)));
2432 break;
2433
2434 } /* switch(*p) */
2435
2436 } /* for(p = postfix; *p; ++p) */
2437
2438 if (nfa_calc_size == TRUE)
2439 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002440 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002441 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 }
2443
2444 e = POP();
2445 if (stackp != stack)
2446 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2447
2448 if (istate >= nstate)
2449 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2450
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002451 matchstate = &state_ptr[istate++]; /* the match state */
2452 matchstate->c = NFA_MATCH;
2453 matchstate->out = matchstate->out1 = NULL;
2454
2455 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002456 ret = e.start;
2457
2458theend:
2459 vim_free(stack);
2460 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461
2462#undef POP1
2463#undef PUSH1
2464#undef POP2
2465#undef PUSH2
2466#undef POP
2467#undef PUSH
2468}
2469
2470/****************************************************************
2471 * NFA execution code.
2472 ****************************************************************/
2473
2474/* thread_T contains runtime information of a NFA state */
2475struct thread
2476{
2477 nfa_state_T *state;
2478 regsub_T sub; /* submatch info */
2479};
2480
2481typedef struct
2482{
2483 thread_T *t;
2484 int n;
2485} List;
2486
2487static void addstate __ARGS((List *l, nfa_state_T *state, regsub_T *m, int off, int lid, int *match));
2488
2489 static void
2490addstate(l, state, m, off, lid, match)
2491 List *l; /* runtime state list */
2492 nfa_state_T *state; /* state to update */
2493 regsub_T *m; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002494 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002495 int lid;
2496 int *match; /* found match? */
2497{
2498 regsub_T save;
2499 int subidx = 0;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002500 thread_T *lastthread;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002501
2502 if (l == NULL || state == NULL)
2503 return;
2504
2505 switch (state->c)
2506 {
2507 case NFA_SPLIT:
2508 case NFA_NOT:
2509 case NFA_NOPEN:
2510 case NFA_NCLOSE:
2511 case NFA_MCLOSE:
2512 case NFA_MCLOSE + 1:
2513 case NFA_MCLOSE + 2:
2514 case NFA_MCLOSE + 3:
2515 case NFA_MCLOSE + 4:
2516 case NFA_MCLOSE + 5:
2517 case NFA_MCLOSE + 6:
2518 case NFA_MCLOSE + 7:
2519 case NFA_MCLOSE + 8:
2520 case NFA_MCLOSE + 9:
2521 /* Do not remember these nodes in list "thislist" or "nextlist" */
2522 break;
2523
2524 default:
2525 if (state->lastlist == lid)
2526 {
2527 if (++state->visits > 2)
2528 return;
2529 }
2530 else
2531 {
2532 /* add the state to the list */
2533 state->lastlist = lid;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002534 lastthread = &l->t[l->n++];
2535 lastthread->state = state;
2536 lastthread->sub = *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 }
2538 }
2539
2540#ifdef ENABLE_LOG
2541 nfa_set_code(state->c);
2542 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n",
2543 abs(state->id), code, state->c);
2544#endif
2545 switch (state->c)
2546 {
2547 case NFA_MATCH:
2548 *match = TRUE;
2549 break;
2550
2551 case NFA_SPLIT:
2552 addstate(l, state->out, m, off, lid, match);
2553 addstate(l, state->out1, m, off, lid, match);
2554 break;
2555
2556 case NFA_SKIP_CHAR:
2557 addstate(l, state->out, m, off, lid, match);
2558 break;
2559
2560#if 0
2561 case NFA_END_NEG_RANGE:
2562 /* Nothing to handle here. nfa_regmatch() will take care of it */
2563 break;
2564
2565 case NFA_NOT:
2566 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2567#ifdef ENABLE_LOG
2568 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2569#endif
2570 break;
2571
2572 case NFA_COMPOSING:
2573 /* nfa_regmatch() will match all the bytes of this composing char. */
2574 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002575#endif
2576
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002577 case NFA_NOPEN:
2578 case NFA_NCLOSE:
2579 addstate(l, state->out, m, off, lid, match);
2580 break;
2581
2582 /* If this state is reached, then a recursive call of nfa_regmatch()
2583 * succeeded. the next call saves the found submatches in the
2584 * first state after the "invisible" branch. */
2585#if 0
2586 case NFA_END_INVISIBLE:
2587 break;
2588#endif
2589
2590 case NFA_MOPEN + 0:
2591 case NFA_MOPEN + 1:
2592 case NFA_MOPEN + 2:
2593 case NFA_MOPEN + 3:
2594 case NFA_MOPEN + 4:
2595 case NFA_MOPEN + 5:
2596 case NFA_MOPEN + 6:
2597 case NFA_MOPEN + 7:
2598 case NFA_MOPEN + 8:
2599 case NFA_MOPEN + 9:
2600 case NFA_ZSTART:
2601 subidx = state->c - NFA_MOPEN;
2602 if (state->c == NFA_ZSTART)
2603 subidx = 0;
2604
2605 if (REG_MULTI)
2606 {
2607 save.startpos[subidx] = m->startpos[subidx];
2608 save.endpos[subidx] = m->endpos[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02002609 if (off == -1)
2610 {
2611 m->startpos[subidx].lnum = reglnum + 1;
2612 m->startpos[subidx].col = 0;
2613 }
2614 else
2615 {
2616 m->startpos[subidx].lnum = reglnum;
2617 m->startpos[subidx].col =
2618 (colnr_T)(reginput - regline + off);
2619 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002620 }
2621 else
2622 {
2623 save.start[subidx] = m->start[subidx];
2624 save.end[subidx] = m->end[subidx];
2625 m->start[subidx] = reginput + off;
2626 }
2627
2628 addstate(l, state->out, m, off, lid, match);
2629
2630 if (REG_MULTI)
2631 {
2632 m->startpos[subidx] = save.startpos[subidx];
2633 m->endpos[subidx] = save.endpos[subidx];
2634 }
2635 else
2636 {
2637 m->start[subidx] = save.start[subidx];
2638 m->end[subidx] = save.end[subidx];
2639 }
2640 break;
2641
2642 case NFA_MCLOSE + 0:
2643 if (nfa_has_zend == TRUE)
2644 {
2645 addstate(l, state->out, m, off, lid, match);
2646 break;
2647 }
2648 case NFA_MCLOSE + 1:
2649 case NFA_MCLOSE + 2:
2650 case NFA_MCLOSE + 3:
2651 case NFA_MCLOSE + 4:
2652 case NFA_MCLOSE + 5:
2653 case NFA_MCLOSE + 6:
2654 case NFA_MCLOSE + 7:
2655 case NFA_MCLOSE + 8:
2656 case NFA_MCLOSE + 9:
2657 case NFA_ZEND:
2658 subidx = state->c - NFA_MCLOSE;
2659 if (state->c == NFA_ZEND)
2660 subidx = 0;
2661
2662 if (REG_MULTI)
2663 {
2664 save.startpos[subidx] = m->startpos[subidx];
2665 save.endpos[subidx] = m->endpos[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02002666 if (off == -1)
2667 {
2668 m->endpos[subidx].lnum = reglnum + 1;
2669 m->endpos[subidx].col = 0;
2670 }
2671 else
2672 {
2673 m->endpos[subidx].lnum = reglnum;
2674 m->endpos[subidx].col = (colnr_T)(reginput - regline + off);
2675 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676 }
2677 else
2678 {
2679 save.start[subidx] = m->start[subidx];
2680 save.end[subidx] = m->end[subidx];
2681 m->end[subidx] = reginput + off;
2682 }
2683
2684 addstate(l, state->out, m, off, lid, match);
2685
2686 if (REG_MULTI)
2687 {
2688 m->startpos[subidx] = save.startpos[subidx];
2689 m->endpos[subidx] = save.endpos[subidx];
2690 }
2691 else
2692 {
2693 m->start[subidx] = save.start[subidx];
2694 m->end[subidx] = save.end[subidx];
2695 }
2696 break;
2697 }
2698}
2699
2700/*
2701 * Check character class "class" against current character c.
2702 */
2703 static int
2704check_char_class(class, c)
2705 int class;
2706 int c;
2707{
2708 switch (class)
2709 {
2710 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002711 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712 return OK;
2713 break;
2714 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002715 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002716 return OK;
2717 break;
2718 case NFA_CLASS_BLANK:
2719 if (c == ' ' || c == '\t')
2720 return OK;
2721 break;
2722 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002723 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002724 return OK;
2725 break;
2726 case NFA_CLASS_DIGIT:
2727 if (VIM_ISDIGIT(c))
2728 return OK;
2729 break;
2730 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002731 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732 return OK;
2733 break;
2734 case NFA_CLASS_LOWER:
2735 if (MB_ISLOWER(c))
2736 return OK;
2737 break;
2738 case NFA_CLASS_PRINT:
2739 if (vim_isprintc(c))
2740 return OK;
2741 break;
2742 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002743 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 return OK;
2745 break;
2746 case NFA_CLASS_SPACE:
2747 if ((c >=9 && c <= 13) || (c == ' '))
2748 return OK;
2749 break;
2750 case NFA_CLASS_UPPER:
2751 if (MB_ISUPPER(c))
2752 return OK;
2753 break;
2754 case NFA_CLASS_XDIGIT:
2755 if (vim_isxdigit(c))
2756 return OK;
2757 break;
2758 case NFA_CLASS_TAB:
2759 if (c == '\t')
2760 return OK;
2761 break;
2762 case NFA_CLASS_RETURN:
2763 if (c == '\r')
2764 return OK;
2765 break;
2766 case NFA_CLASS_BACKSPACE:
2767 if (c == '\b')
2768 return OK;
2769 break;
2770 case NFA_CLASS_ESCAPE:
2771 if (c == '\033')
2772 return OK;
2773 break;
2774
2775 default:
2776 /* should not be here :P */
2777 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2778 }
2779 return FAIL;
2780}
2781
2782/*
2783 * Set all NFA nodes' list ID equal to -1.
2784 */
2785 static void
2786nfa_set_neg_listids(start)
2787 nfa_state_T *start;
2788{
2789 if (start == NULL)
2790 return;
2791 if (start->lastlist >= 0)
2792 {
2793 start->lastlist = -1;
2794 nfa_set_neg_listids(start->out);
2795 nfa_set_neg_listids(start->out1);
2796 }
2797}
2798
2799/*
2800 * Set all NFA nodes' list ID equal to 0.
2801 */
2802 static void
2803nfa_set_null_listids(start)
2804 nfa_state_T *start;
2805{
2806 if (start == NULL)
2807 return;
2808 if (start->lastlist == -1)
2809 {
2810 start->lastlist = 0;
2811 nfa_set_null_listids(start->out);
2812 nfa_set_null_listids(start->out1);
2813 }
2814}
2815
2816/*
2817 * Save list IDs for all NFA states in "list".
2818 */
2819 static void
2820nfa_save_listids(start, list)
2821 nfa_state_T *start;
2822 int *list;
2823{
2824 if (start == NULL)
2825 return;
2826 if (start->lastlist != -1)
2827 {
2828 list[abs(start->id)] = start->lastlist;
2829 start->lastlist = -1;
2830 nfa_save_listids(start->out, list);
2831 nfa_save_listids(start->out1, list);
2832 }
2833}
2834
2835/*
2836 * Restore list IDs from "list" to all NFA states.
2837 */
2838 static void
2839nfa_restore_listids(start, list)
2840 nfa_state_T *start;
2841 int *list;
2842{
2843 if (start == NULL)
2844 return;
2845 if (start->lastlist == -1)
2846 {
2847 start->lastlist = list[abs(start->id)];
2848 nfa_restore_listids(start->out, list);
2849 nfa_restore_listids(start->out1, list);
2850 }
2851}
2852
2853/*
2854 * Main matching routine.
2855 *
2856 * Run NFA to determine whether it matches reginput.
2857 *
2858 * Return TRUE if there is a match, FALSE otherwise.
2859 * Note: Caller must ensure that: start != NULL.
2860 */
2861 static int
2862nfa_regmatch(start, submatch, m)
2863 nfa_state_T *start;
2864 regsub_T *submatch;
2865 regsub_T *m;
2866{
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002867 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868 int n;
2869 int i = 0;
2870 int result;
2871 int size = 0;
2872 int match = FALSE;
2873 int flag = 0;
2874 int old_reglnum = -1;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002875 int go_to_nextline;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876 thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877 char_u *old_reginput = NULL;
2878 char_u *old_regline = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002879 List list[3];
2880 List *listtbl[2][2];
2881 List *ll;
2882 int listid = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002883 List *thislist;
2884 List *nextlist;
2885 List *neglist;
2886 int *listids = NULL;
2887 int j = 0;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02002888#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002889 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002890
2891 if (debug == NULL)
2892 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002893 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002894 return FALSE;
2895 }
2896#endif
2897
2898 /* Allocate memory for the lists of nodes */
2899 size = (nstate + 1) * sizeof(thread_T);
2900 list[0].t = (thread_T *)lalloc(size, TRUE);
2901 list[1].t = (thread_T *)lalloc(size, TRUE);
2902 list[2].t = (thread_T *)lalloc(size, TRUE);
2903 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
2904 goto theend;
2905 vim_memset(list[0].t, 0, size);
2906 vim_memset(list[1].t, 0, size);
2907 vim_memset(list[2].t, 0, size);
2908
2909#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002910 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002911 if (log_fd != NULL)
2912 {
2913 fprintf(log_fd, "**********************************\n");
2914 nfa_set_code(start->c);
2915 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
2916 abs(start->id), code);
2917 fprintf(log_fd, "**********************************\n");
2918 }
2919 else
2920 {
2921 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
2922 log_fd = stderr;
2923 }
2924#endif
2925
2926 thislist = &list[0];
2927 thislist->n = 0;
2928 nextlist = &list[1];
2929 nextlist->n = 0;
2930 neglist = &list[2];
2931 neglist->n = 0;
2932#ifdef ENABLE_LOG
2933 fprintf(log_fd, "(---) STARTSTATE\n");
2934#endif
2935 addstate(thislist, start, m, 0, listid, &match);
2936
2937 /* There are two cases when the NFA advances: 1. input char matches the
2938 * NFA node and 2. input char does not match the NFA node, but the next
2939 * node is NFA_NOT. The following macro calls addstate() according to
2940 * these rules. It is used A LOT, so use the "listtbl" table for speed */
2941 listtbl[0][0] = NULL;
2942 listtbl[0][1] = neglist;
2943 listtbl[1][0] = nextlist;
2944 listtbl[1][1] = NULL;
2945#define ADD_POS_NEG_STATE(node) \
2946 ll = listtbl[result ? 1 : 0][node->negated]; \
2947 if (ll != NULL) \
2948 addstate(ll, node->out , &t->sub, n, listid + 1, &match);
2949
2950
2951 /*
2952 * Run for each character.
2953 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002954 for (;;)
2955 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956#ifdef FEAT_MBYTE
2957 if (has_mbyte)
2958 {
2959 c = (*mb_ptr2char)(reginput);
2960 n = (*mb_ptr2len)(reginput);
2961 }
2962 else
2963#endif
2964 {
2965 c = *reginput;
2966 n = 1;
2967 }
2968 if (c == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02002969 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002970 n = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002971 go_to_nextline = FALSE;
2972 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002973
2974 /* swap lists */
2975 thislist = &list[flag];
2976 nextlist = &list[flag ^= 1];
2977 nextlist->n = 0; /* `clear' nextlist */
2978 listtbl[1][0] = nextlist;
2979 ++listid;
2980
2981#ifdef ENABLE_LOG
2982 fprintf(log_fd, "------------------------------------------\n");
2983 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
2984 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", c, (int)c);
2985 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002986 for (i = 0; i < thislist->n; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
2988 fprintf(log_fd, "\n");
2989#endif
2990
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02002991#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992 fprintf(debug, "\n-------------------\n");
2993#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02002994 /*
2995 * If the state lists are empty we can stop.
2996 */
2997 if (thislist->n == 0 && neglist->n == 0)
2998 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002999
3000 /* compute nextlist */
3001 for (i = 0; i < thislist->n || neglist->n > 0; ++i)
3002 {
3003 if (neglist->n > 0)
3004 {
3005 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003006 neglist->n--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003007 i--;
3008 }
3009 else
3010 t = &thislist->t[i];
3011
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003012#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003013 nfa_set_code(t->state->c);
3014 fprintf(debug, "%s, ", code);
3015#endif
3016#ifdef ENABLE_LOG
3017 nfa_set_code(t->state->c);
3018 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3019 code, (int)t->state->c);
3020#endif
3021
3022 /*
3023 * Handle the possible codes of the current state.
3024 * The most important is NFA_MATCH.
3025 */
3026 switch (t->state->c)
3027 {
3028 case NFA_MATCH:
3029 match = TRUE;
3030 *submatch = t->sub;
3031#ifdef ENABLE_LOG
3032 for (j = 0; j < 4; j++)
3033 if (REG_MULTI)
3034 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
3035 j,
3036 t->sub.startpos[j].col,
3037 (int)t->sub.startpos[j].lnum,
3038 t->sub.endpos[j].col,
3039 (int)t->sub.endpos[j].lnum);
3040 else
3041 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
3042 j,
3043 (char *)t->sub.start[j],
3044 (char *)t->sub.end[j]);
3045 fprintf(log_fd, "\n");
3046#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003047 /* Found the left-most longest match, do not look at any other
3048 * states at this position. */
3049 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003050
3051 case NFA_END_INVISIBLE:
3052 /* This is only encountered after a NFA_START_INVISIBLE node.
3053 * They surround a zero-width group, used with "\@=" and "\&".
3054 * If we got here, it means that the current "invisible" group
3055 * finished successfully, so return control to the parent
3056 * nfa_regmatch(). Submatches are stored in *m, and used in
3057 * the parent call. */
3058 if (start->c == NFA_MOPEN + 0)
3059 addstate(thislist, t->state->out, &t->sub, 0, listid,
3060 &match);
3061 else
3062 {
3063 *m = t->sub;
3064 match = TRUE;
3065 }
3066 break;
3067
3068 case NFA_START_INVISIBLE:
3069 /* Save global variables, and call nfa_regmatch() to check if
3070 * the current concat matches at this position. The concat
3071 * ends with the node NFA_END_INVISIBLE */
3072 old_reginput = reginput;
3073 old_regline = regline;
3074 old_reglnum = reglnum;
3075 if (listids == NULL)
3076 {
3077 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3078 if (listids == NULL)
3079 {
3080 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3081 return 0;
3082 }
3083 }
3084#ifdef ENABLE_LOG
3085 if (log_fd != stderr)
3086 fclose(log_fd);
3087 log_fd = NULL;
3088#endif
3089 /* Have to clear the listid field of the NFA nodes, so that
3090 * nfa_regmatch() and addstate() can run properly after
3091 * recursion. */
3092 nfa_save_listids(start, listids);
3093 nfa_set_null_listids(start);
3094 result = nfa_regmatch(t->state->out, submatch, m);
3095 nfa_set_neg_listids(start);
3096 nfa_restore_listids(start, listids);
3097
3098#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003099 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003100 if (log_fd != NULL)
3101 {
3102 fprintf(log_fd, "****************************\n");
3103 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3104 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3105 fprintf(log_fd, "****************************\n");
3106 }
3107 else
3108 {
3109 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3110 log_fd = stderr;
3111 }
3112#endif
3113 if (result == TRUE)
3114 {
3115 /* Restore position in input text */
3116 reginput = old_reginput;
3117 regline = old_regline;
3118 reglnum = old_reglnum;
3119 /* Copy submatch info from the recursive call */
3120 if (REG_MULTI)
3121 for (j = 1; j < NSUBEXP; j++)
3122 {
3123 t->sub.startpos[j] = m->startpos[j];
3124 t->sub.endpos[j] = m->endpos[j];
3125 }
3126 else
3127 for (j = 1; j < NSUBEXP; j++)
3128 {
3129 t->sub.start[j] = m->start[j];
3130 t->sub.end[j] = m->end[j];
3131 }
3132 /* t->state->out1 is the corresponding END_INVISIBLE node */
3133 addstate(thislist, t->state->out1->out, &t->sub, 0, listid,
3134 &match);
3135 }
3136 else
3137 {
3138 /* continue with next input char */
3139 reginput = old_reginput;
3140 }
3141 break;
3142
3143 case NFA_BOL:
3144 if (reginput == regline)
3145 addstate(thislist, t->state->out, &t->sub, 0, listid,
3146 &match);
3147 break;
3148
3149 case NFA_EOL:
3150 if (c == NUL)
3151 addstate(thislist, t->state->out, &t->sub, 0, listid,
3152 &match);
3153 break;
3154
3155 case NFA_BOW:
3156 {
3157 int bow = TRUE;
3158
3159 if (c == NUL)
3160 bow = FALSE;
3161#ifdef FEAT_MBYTE
3162 else if (has_mbyte)
3163 {
3164 int this_class;
3165
3166 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003167 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003168 if (this_class <= 1)
3169 bow = FALSE;
3170 else if (reg_prev_class() == this_class)
3171 bow = FALSE;
3172 }
3173#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003174 else if (!vim_iswordc_buf(c, reg_buf)
3175 || (reginput > regline
3176 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 bow = FALSE;
3178 if (bow)
3179 addstate(thislist, t->state->out, &t->sub, 0, listid,
3180 &match);
3181 break;
3182 }
3183
3184 case NFA_EOW:
3185 {
3186 int eow = TRUE;
3187
3188 if (reginput == regline)
3189 eow = FALSE;
3190#ifdef FEAT_MBYTE
3191 else if (has_mbyte)
3192 {
3193 int this_class, prev_class;
3194
3195 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003196 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003197 prev_class = reg_prev_class();
3198 if (this_class == prev_class
3199 || prev_class == 0 || prev_class == 1)
3200 eow = FALSE;
3201 }
3202#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003203 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
3204 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 eow = FALSE;
3206 if (eow)
3207 addstate(thislist, t->state->out, &t->sub, 0, listid,
3208 &match);
3209 break;
3210 }
3211
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003212#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003214 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003215 int mc = c;
3216 int len = 0;
3217 nfa_state_T *end;
3218 nfa_state_T *sta;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 result = OK;
3221 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003222 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003223 if (utf_iscomposing(sta->c))
3224 {
3225 /* Only match composing character(s), ignore base
3226 * character. Used for ".{composing}" and "{composing}"
3227 * (no preceding character). */
3228 len += mb_char2len(c);
3229 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003230 if (ireg_icombine)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003231 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003232 /* If \Z was present, then ignore composing characters.
3233 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003234 /* TODO: How about negated? */
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003235 if (len == 0 && sta->c != c)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003236 result = FAIL;
3237 len = n;
3238 while (sta->c != NFA_END_COMPOSING)
3239 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003241 else
3242 while (sta->c != NFA_END_COMPOSING && len < n)
3243 {
3244 if (len > 0)
3245 mc = mb_ptr2char(reginput + len);
3246 if (mc != sta->c)
3247 break;
3248 len += mb_char2len(mc);
3249 sta = sta->out;
3250 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251
3252 /* if input char length doesn't match regexp char length */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003253 if (len < n || sta->c != NFA_END_COMPOSING)
Bram Moolenaar1d814752013-05-24 20:25:33 +02003254 result = FAIL;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003255 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 ADD_POS_NEG_STATE(end);
3257 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003258 }
3259#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003260
3261 case NFA_NEWL:
3262 if (!reg_line_lbr && REG_MULTI
Bram Moolenaar35b23862013-05-22 23:00:40 +02003263 && c == NUL && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003264 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003265 go_to_nextline = TRUE;
3266 /* Pass -1 for the offset, which means taking the position
3267 * at the start of the next line. */
3268 addstate(nextlist, t->state->out, &t->sub, -1,
3269 listid + 1, &match);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 }
3271 break;
3272
3273 case NFA_CLASS_ALNUM:
3274 case NFA_CLASS_ALPHA:
3275 case NFA_CLASS_BLANK:
3276 case NFA_CLASS_CNTRL:
3277 case NFA_CLASS_DIGIT:
3278 case NFA_CLASS_GRAPH:
3279 case NFA_CLASS_LOWER:
3280 case NFA_CLASS_PRINT:
3281 case NFA_CLASS_PUNCT:
3282 case NFA_CLASS_SPACE:
3283 case NFA_CLASS_UPPER:
3284 case NFA_CLASS_XDIGIT:
3285 case NFA_CLASS_TAB:
3286 case NFA_CLASS_RETURN:
3287 case NFA_CLASS_BACKSPACE:
3288 case NFA_CLASS_ESCAPE:
3289 result = check_char_class(t->state->c, c);
3290 ADD_POS_NEG_STATE(t->state);
3291 break;
3292
3293 case NFA_END_NEG_RANGE:
3294 /* This follows a series of negated nodes, like:
3295 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
3296 if (c > 0)
3297 addstate(nextlist, t->state->out, &t->sub, n, listid + 1,
3298 &match);
3299 break;
3300
3301 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003302 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003303 if (c > 0)
3304 addstate(nextlist, t->state->out, &t->sub, n, listid + 1,
3305 &match);
3306 break;
3307
3308 /*
3309 * Character classes like \a for alpha, \d for digit etc.
3310 */
3311 case NFA_IDENT: /* \i */
3312 result = vim_isIDc(c);
3313 ADD_POS_NEG_STATE(t->state);
3314 break;
3315
3316 case NFA_SIDENT: /* \I */
3317 result = !VIM_ISDIGIT(c) && vim_isIDc(c);
3318 ADD_POS_NEG_STATE(t->state);
3319 break;
3320
3321 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003322 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003323 ADD_POS_NEG_STATE(t->state);
3324 break;
3325
3326 case NFA_SKWORD: /* \K */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003327 result = !VIM_ISDIGIT(c) && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328 ADD_POS_NEG_STATE(t->state);
3329 break;
3330
3331 case NFA_FNAME: /* \f */
3332 result = vim_isfilec(c);
3333 ADD_POS_NEG_STATE(t->state);
3334 break;
3335
3336 case NFA_SFNAME: /* \F */
3337 result = !VIM_ISDIGIT(c) && vim_isfilec(c);
3338 ADD_POS_NEG_STATE(t->state);
3339 break;
3340
3341 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003342 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 ADD_POS_NEG_STATE(t->state);
3344 break;
3345
3346 case NFA_SPRINT: /* \P */
Bram Moolenaar08050492013-05-21 12:43:56 +02003347 result = !VIM_ISDIGIT(c) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 ADD_POS_NEG_STATE(t->state);
3349 break;
3350
3351 case NFA_WHITE: /* \s */
3352 result = vim_iswhite(c);
3353 ADD_POS_NEG_STATE(t->state);
3354 break;
3355
3356 case NFA_NWHITE: /* \S */
3357 result = c != NUL && !vim_iswhite(c);
3358 ADD_POS_NEG_STATE(t->state);
3359 break;
3360
3361 case NFA_DIGIT: /* \d */
3362 result = ri_digit(c);
3363 ADD_POS_NEG_STATE(t->state);
3364 break;
3365
3366 case NFA_NDIGIT: /* \D */
3367 result = c != NUL && !ri_digit(c);
3368 ADD_POS_NEG_STATE(t->state);
3369 break;
3370
3371 case NFA_HEX: /* \x */
3372 result = ri_hex(c);
3373 ADD_POS_NEG_STATE(t->state);
3374 break;
3375
3376 case NFA_NHEX: /* \X */
3377 result = c != NUL && !ri_hex(c);
3378 ADD_POS_NEG_STATE(t->state);
3379 break;
3380
3381 case NFA_OCTAL: /* \o */
3382 result = ri_octal(c);
3383 ADD_POS_NEG_STATE(t->state);
3384 break;
3385
3386 case NFA_NOCTAL: /* \O */
3387 result = c != NUL && !ri_octal(c);
3388 ADD_POS_NEG_STATE(t->state);
3389 break;
3390
3391 case NFA_WORD: /* \w */
3392 result = ri_word(c);
3393 ADD_POS_NEG_STATE(t->state);
3394 break;
3395
3396 case NFA_NWORD: /* \W */
3397 result = c != NUL && !ri_word(c);
3398 ADD_POS_NEG_STATE(t->state);
3399 break;
3400
3401 case NFA_HEAD: /* \h */
3402 result = ri_head(c);
3403 ADD_POS_NEG_STATE(t->state);
3404 break;
3405
3406 case NFA_NHEAD: /* \H */
3407 result = c != NUL && !ri_head(c);
3408 ADD_POS_NEG_STATE(t->state);
3409 break;
3410
3411 case NFA_ALPHA: /* \a */
3412 result = ri_alpha(c);
3413 ADD_POS_NEG_STATE(t->state);
3414 break;
3415
3416 case NFA_NALPHA: /* \A */
3417 result = c != NUL && !ri_alpha(c);
3418 ADD_POS_NEG_STATE(t->state);
3419 break;
3420
3421 case NFA_LOWER: /* \l */
3422 result = ri_lower(c);
3423 ADD_POS_NEG_STATE(t->state);
3424 break;
3425
3426 case NFA_NLOWER: /* \L */
3427 result = c != NUL && !ri_lower(c);
3428 ADD_POS_NEG_STATE(t->state);
3429 break;
3430
3431 case NFA_UPPER: /* \u */
3432 result = ri_upper(c);
3433 ADD_POS_NEG_STATE(t->state);
3434 break;
3435
3436 case NFA_NUPPER: /* \U */
3437 result = c != NUL && !ri_upper(c);
3438 ADD_POS_NEG_STATE(t->state);
3439 break;
3440
Bram Moolenaar12e40142013-05-21 15:33:41 +02003441 case NFA_MOPEN + 0:
3442 case NFA_MOPEN + 1:
3443 case NFA_MOPEN + 2:
3444 case NFA_MOPEN + 3:
3445 case NFA_MOPEN + 4:
3446 case NFA_MOPEN + 5:
3447 case NFA_MOPEN + 6:
3448 case NFA_MOPEN + 7:
3449 case NFA_MOPEN + 8:
3450 case NFA_MOPEN + 9:
3451 /* handled below */
3452 break;
3453
3454 case NFA_SKIP_CHAR:
3455 case NFA_ZSTART:
3456 /* TODO: should not happen? */
3457 break;
3458
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 default: /* regular character */
Bram Moolenaar12e40142013-05-21 15:33:41 +02003460 /* TODO: put this in #ifdef later */
3461 if (t->state->c < -256)
3462 EMSGN("INTERNAL: Negative state char: %ld", t->state->c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003463 result = (no_Magic(t->state->c) == c);
Bram Moolenaar12e40142013-05-21 15:33:41 +02003464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 if (!result)
3466 result = ireg_ic == TRUE
3467 && MB_TOLOWER(t->state->c) == MB_TOLOWER(c);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003468#ifdef FEAT_MBYTE
3469 /* If there is a composing character which is not being
3470 * ignored there can be no match. Match with composing
3471 * character uses NFA_COMPOSING above. */
3472 if (result && enc_utf8 && !ireg_icombine
3473 && n != utf_char2len(c))
3474 result = FALSE;
3475#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003476 ADD_POS_NEG_STATE(t->state);
3477 break;
3478 }
3479
3480 } /* for (thislist = thislist; thislist->state; thislist++) */
3481
3482 /* The first found match is the leftmost one, but there may be a
3483 * longer one. Keep running the NFA, but don't start from the
3484 * beginning. Also, do not add the start state in recursive calls of
3485 * nfa_regmatch(), because recursive calls should only start in the
3486 * first position. */
3487 if (match == FALSE && start->c == NFA_MOPEN + 0)
3488 {
3489#ifdef ENABLE_LOG
3490 fprintf(log_fd, "(---) STARTSTATE\n");
3491#endif
3492 addstate(nextlist, start, m, n, listid + 1, &match);
3493 }
3494
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003495#ifdef ENABLE_LOG
3496 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
3497 for (i = 0; i< thislist->n; i++)
3498 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3499 fprintf(log_fd, "\n");
3500#endif
3501
3502nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003503 /* Advance to the next character, or advance to the next line, or
3504 * finish. */
3505 if (n != 0)
3506 reginput += n;
3507 else if (go_to_nextline)
3508 reg_nextline();
3509 else
3510 break;
3511 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003512
3513#ifdef ENABLE_LOG
3514 if (log_fd != stderr)
3515 fclose(log_fd);
3516 log_fd = NULL;
3517#endif
3518
3519theend:
3520 /* Free memory */
3521 vim_free(list[0].t);
3522 vim_free(list[1].t);
3523 vim_free(list[2].t);
3524 list[0].t = list[1].t = list[2].t = NULL;
3525 if (listids != NULL)
3526 vim_free(listids);
3527#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003528#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 fclose(debug);
3530#endif
3531
3532 return match;
3533}
3534
3535/*
3536 * Try match of "prog" with at regline["col"].
3537 * Returns 0 for failure, number of lines contained in the match otherwise.
3538 */
3539 static long
3540nfa_regtry(start, col)
3541 nfa_state_T *start;
3542 colnr_T col;
3543{
3544 int i;
3545 regsub_T sub, m;
3546#ifdef ENABLE_LOG
3547 FILE *f;
3548#endif
3549
3550 reginput = regline + col;
3551 need_clear_subexpr = TRUE;
3552
3553#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003554 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 if (f != NULL)
3556 {
3557 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3558 fprintf(f, " =======================================================\n");
3559#ifdef DEBUG
3560 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3561#endif
3562 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3563 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003564 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 fprintf(f, "\n\n");
3566 fclose(f);
3567 }
3568 else
3569 EMSG(_("Could not open temporary log file for writing "));
3570#endif
3571
3572 if (REG_MULTI)
3573 {
3574 /* Use 0xff to set lnum to -1 */
3575 vim_memset(sub.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3576 vim_memset(sub.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3577 vim_memset(m.startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3578 vim_memset(m.endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
3579 }
3580 else
3581 {
3582 vim_memset(sub.start, 0, sizeof(char_u *) * NSUBEXP);
3583 vim_memset(sub.end, 0, sizeof(char_u *) * NSUBEXP);
3584 vim_memset(m.start, 0, sizeof(char_u *) * NSUBEXP);
3585 vim_memset(m.end, 0, sizeof(char_u *) * NSUBEXP);
3586 }
3587
3588 if (nfa_regmatch(start, &sub, &m) == FALSE)
3589 return 0;
3590
3591 cleanup_subexpr();
3592 if (REG_MULTI)
3593 {
3594 for (i = 0; i < NSUBEXP; i++)
3595 {
3596 reg_startpos[i] = sub.startpos[i];
3597 reg_endpos[i] = sub.endpos[i];
3598 }
3599
3600 if (reg_startpos[0].lnum < 0)
3601 {
3602 reg_startpos[0].lnum = 0;
3603 reg_startpos[0].col = col;
3604 }
3605 if (reg_endpos[0].lnum < 0)
3606 {
3607 reg_endpos[0].lnum = reglnum;
3608 reg_endpos[0].col = (int)(reginput - regline);
3609 }
3610 else
3611 /* Use line number of "\ze". */
3612 reglnum = reg_endpos[0].lnum;
3613 }
3614 else
3615 {
3616 for (i = 0; i < NSUBEXP; i++)
3617 {
3618 reg_startp[i] = sub.start[i];
3619 reg_endp[i] = sub.end[i];
3620 }
3621
3622 if (reg_startp[0] == NULL)
3623 reg_startp[0] = regline + col;
3624 if (reg_endp[0] == NULL)
3625 reg_endp[0] = reginput;
3626 }
3627
3628 return 1 + reglnum;
3629}
3630
3631/*
3632 * Match a regexp against a string ("line" points to the string) or multiple
3633 * lines ("line" is NULL, use reg_getline()).
3634 *
3635 * Returns 0 for failure, number of lines contained in the match otherwise.
3636 */
3637 static long
3638nfa_regexec_both(line, col)
3639 char_u *line;
3640 colnr_T col; /* column to start looking for match */
3641{
3642 nfa_regprog_T *prog;
3643 long retval = 0L;
3644 int i;
3645
3646 if (REG_MULTI)
3647 {
3648 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3649 line = reg_getline((linenr_T)0); /* relative to the cursor */
3650 reg_startpos = reg_mmatch->startpos;
3651 reg_endpos = reg_mmatch->endpos;
3652 }
3653 else
3654 {
3655 prog = (nfa_regprog_T *)reg_match->regprog;
3656 reg_startp = reg_match->startp;
3657 reg_endp = reg_match->endp;
3658 }
3659
3660 /* Be paranoid... */
3661 if (prog == NULL || line == NULL)
3662 {
3663 EMSG(_(e_null));
3664 goto theend;
3665 }
3666
3667 /* If the start column is past the maximum column: no need to try. */
3668 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3669 goto theend;
3670
3671 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3672 if (prog->regflags & RF_ICASE)
3673 ireg_ic = TRUE;
3674 else if (prog->regflags & RF_NOICASE)
3675 ireg_ic = FALSE;
3676
3677#ifdef FEAT_MBYTE
3678 /* If pattern contains "\Z" overrule value of ireg_icombine */
3679 if (prog->regflags & RF_ICOMBINE)
3680 ireg_icombine = TRUE;
3681#endif
3682
3683 regline = line;
3684 reglnum = 0; /* relative to line */
3685
3686 nstate = prog->nstate;
3687
3688 for (i = 0; i < nstate; ++i)
3689 {
3690 prog->state[i].id = i;
3691 prog->state[i].lastlist = 0;
3692 prog->state[i].visits = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003693 }
3694
3695 retval = nfa_regtry(prog->start, col);
3696
3697theend:
3698 return retval;
3699}
3700
3701/*
3702 * Compile a regular expression into internal code for the NFA matcher.
3703 * Returns the program in allocated space. Returns NULL for an error.
3704 */
3705 static regprog_T *
3706nfa_regcomp(expr, re_flags)
3707 char_u *expr;
3708 int re_flags;
3709{
3710 nfa_regprog_T *prog;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003711 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712 int *postfix;
3713
3714 if (expr == NULL)
3715 return NULL;
3716
3717#ifdef DEBUG
3718 nfa_regengine.expr = expr;
3719#endif
3720
3721 init_class_tab();
3722
3723 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3724 return NULL;
3725
3726 /* Space for compiled regexp */
3727 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate_max;
3728 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3729 if (prog == NULL)
3730 goto fail;
3731 vim_memset(prog, 0, prog_size);
3732
3733 /* Build postfix form of the regexp. Needed to build the NFA
3734 * (and count its size) */
3735 postfix = re2post();
3736 if (postfix == NULL)
3737 goto fail; /* Cascaded (syntax?) error */
3738
3739 /*
3740 * In order to build the NFA, we parse the input regexp twice:
3741 * 1. first pass to count size (so we can allocate space)
3742 * 2. second to emit code
3743 */
3744#ifdef ENABLE_LOG
3745 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003746 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747
3748 if (f != NULL)
3749 {
3750 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3751 fclose(f);
3752 }
3753 }
3754#endif
3755
3756 /*
3757 * PASS 1
3758 * Count number of NFA states in "nstate". Do not build the NFA.
3759 */
3760 post2nfa(postfix, post_ptr, TRUE);
3761 state_ptr = prog->state;
3762
3763 /*
3764 * PASS 2
3765 * Build the NFA
3766 */
3767 prog->start = post2nfa(postfix, post_ptr, FALSE);
3768 if (prog->start == NULL)
3769 goto fail;
3770
3771 prog->regflags = regflags;
3772 prog->engine = &nfa_regengine;
3773 prog->nstate = nstate;
3774#ifdef ENABLE_LOG
3775 nfa_postfix_dump(expr, OK);
3776 nfa_dump(prog);
3777#endif
3778
3779out:
3780 vim_free(post_start);
3781 post_start = post_ptr = post_end = NULL;
3782 state_ptr = NULL;
3783 return (regprog_T *)prog;
3784
3785fail:
3786 vim_free(prog);
3787 prog = NULL;
3788#ifdef ENABLE_LOG
3789 nfa_postfix_dump(expr, FAIL);
3790#endif
3791#ifdef DEBUG
3792 nfa_regengine.expr = NULL;
3793#endif
3794 goto out;
3795}
3796
3797
3798/*
3799 * Match a regexp against a string.
3800 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
3801 * Uses curbuf for line count and 'iskeyword'.
3802 *
3803 * Return TRUE if there is a match, FALSE if not.
3804 */
3805 static int
3806nfa_regexec(rmp, line, col)
3807 regmatch_T *rmp;
3808 char_u *line; /* string to match against */
3809 colnr_T col; /* column to start looking for match */
3810{
3811 reg_match = rmp;
3812 reg_mmatch = NULL;
3813 reg_maxline = 0;
3814 reg_line_lbr = FALSE;
3815 reg_buf = curbuf;
3816 reg_win = NULL;
3817 ireg_ic = rmp->rm_ic;
3818#ifdef FEAT_MBYTE
3819 ireg_icombine = FALSE;
3820#endif
3821 ireg_maxcol = 0;
3822 return (nfa_regexec_both(line, col) != 0);
3823}
3824
3825#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3826 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
3827
3828static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3829
3830/*
3831 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
3832 */
3833 static int
3834nfa_regexec_nl(rmp, line, col)
3835 regmatch_T *rmp;
3836 char_u *line; /* string to match against */
3837 colnr_T col; /* column to start looking for match */
3838{
3839 reg_match = rmp;
3840 reg_mmatch = NULL;
3841 reg_maxline = 0;
3842 reg_line_lbr = TRUE;
3843 reg_buf = curbuf;
3844 reg_win = NULL;
3845 ireg_ic = rmp->rm_ic;
3846#ifdef FEAT_MBYTE
3847 ireg_icombine = FALSE;
3848#endif
3849 ireg_maxcol = 0;
3850 return (nfa_regexec_both(line, col) != 0);
3851}
3852#endif
3853
3854
3855/*
3856 * Match a regexp against multiple lines.
3857 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3858 * Uses curbuf for line count and 'iskeyword'.
3859 *
3860 * Return zero if there is no match. Return number of lines contained in the
3861 * match otherwise.
3862 *
3863 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
3864 *
3865 * ! Also NOTE : match may actually be in another line. e.g.:
3866 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
3867 *
3868 * +-------------------------+
3869 * |a |
3870 * |b |
3871 * |c |
3872 * | |
3873 * +-------------------------+
3874 *
3875 * then nfa_regexec_multi() returns 3. while the original
3876 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
3877 *
3878 * FIXME if this behavior is not compatible.
3879 */
3880 static long
3881nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
3882 regmmatch_T *rmp;
3883 win_T *win; /* window in which to search or NULL */
3884 buf_T *buf; /* buffer in which to search */
3885 linenr_T lnum; /* nr of line to start looking for match */
3886 colnr_T col; /* column to start looking for match */
3887 proftime_T *tm UNUSED; /* timeout limit or NULL */
3888{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003889 reg_match = NULL;
3890 reg_mmatch = rmp;
3891 reg_buf = buf;
3892 reg_win = win;
3893 reg_firstlnum = lnum;
3894 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3895 reg_line_lbr = FALSE;
3896 ireg_ic = rmp->rmm_ic;
3897#ifdef FEAT_MBYTE
3898 ireg_icombine = FALSE;
3899#endif
3900 ireg_maxcol = rmp->rmm_maxcol;
3901
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003902 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003903}
3904
3905#ifdef DEBUG
3906# undef ENABLE_LOG
3907#endif