blob: ca37b1073be6efd5178b69e3607b16d67d14b7e4 [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
Bram Moolenaar963fee22013-05-26 21:47:28 +0200164/* Number of sub expressions actually being used during execution. 1 if only
165 * the whole match (subexpr 0) is used. */
166static int nfa_nsubexpr;
167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200168static int *post_start; /* holds the postfix form of r.e. */
169static int *post_end;
170static int *post_ptr;
171
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200172static int nstate; /* Number of states in the NFA. Also used when
173 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200174static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200175
176
177static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
178static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
179static int nfa_emit_equi_class __ARGS((int c, int neg));
180static void nfa_inc __ARGS((char_u **p));
181static void nfa_dec __ARGS((char_u **p));
182static int nfa_regatom __ARGS((void));
183static int nfa_regpiece __ARGS((void));
184static int nfa_regconcat __ARGS((void));
185static int nfa_regbranch __ARGS((void));
186static int nfa_reg __ARGS((int paren));
187#ifdef DEBUG
188static void nfa_set_code __ARGS((int c));
189static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200190static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
191static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200192static void nfa_dump __ARGS((nfa_regprog_T *prog));
193#endif
194static int *re2post __ARGS((void));
195static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
196static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
197static int check_char_class __ARGS((int class, int c));
198static void st_error __ARGS((int *postfix, int *end, int *p));
199static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
200static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
201static void nfa_set_null_listids __ARGS((nfa_state_T *start));
202static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
203static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
204static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
205static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
206static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
207static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
208
209/* helper functions used when doing re2post() ... regatom() parsing */
210#define EMIT(c) do { \
211 if (post_ptr >= post_end) \
212 return FAIL; \
213 *post_ptr++ = c; \
214 } while (0)
215
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200216/*
217 * Initialize internal variables before NFA compilation.
218 * Return OK on success, FAIL otherwise.
219 */
220 static int
221nfa_regcomp_start(expr, re_flags)
222 char_u *expr;
223 int re_flags; /* see vim_regcomp() */
224{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200225 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200226 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200227
228 nstate = 0;
229 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200230 /* A reasonable estimation for maximum size */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200231 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200232
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200233 /* Some items blow up in size, such as [A-z]. Add more space for that.
234 * TODO: some patterns may still fail. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200235 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200236
237 /* Size for postfix representation of expr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200238 postfix_size = sizeof(*post_start) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200239
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200240 post_start = (int *)lalloc(postfix_size, TRUE);
241 if (post_start == NULL)
242 return FAIL;
243 vim_memset(post_start, 0, postfix_size);
244 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200245 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246 nfa_has_zend = FALSE;
247
248 regcomp_start(expr, re_flags);
249
250 return OK;
251}
252
253/*
254 * Search between "start" and "end" and try to recognize a
255 * character class in expanded form. For example [0-9].
256 * On success, return the id the character class to be emitted.
257 * On failure, return 0 (=FAIL)
258 * Start points to the first char of the range, while end should point
259 * to the closing brace.
260 */
261 static int
262nfa_recognize_char_class(start, end, extra_newl)
263 char_u *start;
264 char_u *end;
265 int extra_newl;
266{
267 int i;
268 /* Each of these variables takes up a char in "config[]",
269 * in the order they are here. */
270 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
271 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
272 char_u *p;
273#define NCONFIGS 16
274 int classid[NCONFIGS] = {
275 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
276 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
277 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
278 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
279 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200280 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200281 char_u config[NCONFIGS][9] = {
282 "000000100", /* digit */
283 "100000100", /* non digit */
284 "011000100", /* hex-digit */
285 "111000100", /* non hex-digit */
286 "000001000", /* octal-digit */
287 "100001000", /* [^0-7] */
288 "000110110", /* [0-9A-Za-z_] */
289 "100110110", /* [^0-9A-Za-z_] */
290 "000110010", /* head of word */
291 "100110010", /* not head of word */
292 "000110000", /* alphabetic char a-z */
293 "100110000", /* non alphabetic char */
294 "000100000", /* lowercase letter */
295 "100100000", /* non lowercase */
296 "000010000", /* uppercase */
297 "100010000" /* non uppercase */
298 };
299
300 if (extra_newl == TRUE)
301 newl = TRUE;
302
303 if (*end != ']')
304 return FAIL;
305 p = start;
306 if (*p == '^')
307 {
308 not = TRUE;
309 p ++;
310 }
311
312 while (p < end)
313 {
314 if (p + 2 < end && *(p + 1) == '-')
315 {
316 switch (*p)
317 {
318 case '0':
319 if (*(p + 2) == '9')
320 {
321 o9 = TRUE;
322 break;
323 }
324 else
325 if (*(p + 2) == '7')
326 {
327 o7 = TRUE;
328 break;
329 }
330 case 'a':
331 if (*(p + 2) == 'z')
332 {
333 az = TRUE;
334 break;
335 }
336 else
337 if (*(p + 2) == 'f')
338 {
339 af = TRUE;
340 break;
341 }
342 case 'A':
343 if (*(p + 2) == 'Z')
344 {
345 AZ = TRUE;
346 break;
347 }
348 else
349 if (*(p + 2) == 'F')
350 {
351 AF = TRUE;
352 break;
353 }
354 /* FALLTHROUGH */
355 default:
356 return FAIL;
357 }
358 p += 3;
359 }
360 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
361 {
362 newl = TRUE;
363 p += 2;
364 }
365 else if (*p == '_')
366 {
367 underscore = TRUE;
368 p ++;
369 }
370 else if (*p == '\n')
371 {
372 newl = TRUE;
373 p ++;
374 }
375 else
376 return FAIL;
377 } /* while (p < end) */
378
379 if (p != end)
380 return FAIL;
381
382 /* build the config that represents the ranges we gathered */
383 STRCPY(myconfig, "000000000");
384 if (not == TRUE)
385 myconfig[0] = '1';
386 if (af == TRUE)
387 myconfig[1] = '1';
388 if (AF == TRUE)
389 myconfig[2] = '1';
390 if (az == TRUE)
391 myconfig[3] = '1';
392 if (AZ == TRUE)
393 myconfig[4] = '1';
394 if (o7 == TRUE)
395 myconfig[5] = '1';
396 if (o9 == TRUE)
397 myconfig[6] = '1';
398 if (underscore == TRUE)
399 myconfig[7] = '1';
400 if (newl == TRUE)
401 {
402 myconfig[8] = '1';
403 extra_newl = ADD_NL;
404 }
405 /* try to recognize character classes */
406 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200407 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200408 return classid[i] + extra_newl;
409
410 /* fallthrough => no success so far */
411 return FAIL;
412
413#undef NCONFIGS
414}
415
416/*
417 * Produce the bytes for equivalence class "c".
418 * Currently only handles latin1, latin9 and utf-8.
419 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
420 * equivalent to 'a OR b OR c'
421 *
422 * NOTE! When changing this function, also update reg_equi_class()
423 */
424 static int
425nfa_emit_equi_class(c, neg)
426 int c;
427 int neg;
428{
429 int first = TRUE;
430 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
431#define EMIT2(c) \
432 EMIT(c); \
433 if (neg == TRUE) { \
434 EMIT(NFA_NOT); \
435 } \
436 if (first == FALSE) \
437 EMIT(glue); \
438 else \
439 first = FALSE; \
440
441#ifdef FEAT_MBYTE
442 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
443 || STRCMP(p_enc, "iso-8859-15") == 0)
444#endif
445 {
446 switch (c)
447 {
448 case 'A': case '\300': case '\301': case '\302':
449 case '\303': case '\304': case '\305':
450 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
451 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
452 EMIT2('\305');
453 return OK;
454
455 case 'C': case '\307':
456 EMIT2('C'); EMIT2('\307');
457 return OK;
458
459 case 'E': case '\310': case '\311': case '\312': case '\313':
460 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
461 EMIT2('\312'); EMIT2('\313');
462 return OK;
463
464 case 'I': case '\314': case '\315': case '\316': case '\317':
465 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
466 EMIT2('\316'); EMIT2('\317');
467 return OK;
468
469 case 'N': case '\321':
470 EMIT2('N'); EMIT2('\321');
471 return OK;
472
473 case 'O': case '\322': case '\323': case '\324': case '\325':
474 case '\326':
475 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
476 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
477 return OK;
478
479 case 'U': case '\331': case '\332': case '\333': case '\334':
480 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
481 EMIT2('\333'); EMIT2('\334');
482 return OK;
483
484 case 'Y': case '\335':
485 EMIT2('Y'); EMIT2('\335');
486 return OK;
487
488 case 'a': case '\340': case '\341': case '\342':
489 case '\343': case '\344': case '\345':
490 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
491 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
492 EMIT2('\345');
493 return OK;
494
495 case 'c': case '\347':
496 EMIT2('c'); EMIT2('\347');
497 return OK;
498
499 case 'e': case '\350': case '\351': case '\352': case '\353':
500 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
501 EMIT2('\352'); EMIT2('\353');
502 return OK;
503
504 case 'i': case '\354': case '\355': case '\356': case '\357':
505 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
506 EMIT2('\356'); EMIT2('\357');
507 return OK;
508
509 case 'n': case '\361':
510 EMIT2('n'); EMIT2('\361');
511 return OK;
512
513 case 'o': case '\362': case '\363': case '\364': case '\365':
514 case '\366':
515 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
516 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
517 return OK;
518
519 case 'u': case '\371': case '\372': case '\373': case '\374':
520 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
521 EMIT2('\373'); EMIT2('\374');
522 return OK;
523
524 case 'y': case '\375': case '\377':
525 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
526 return OK;
527
528 default:
529 return FAIL;
530 }
531 }
532
533 EMIT(c);
534 return OK;
535#undef EMIT2
536}
537
538/*
539 * Code to parse regular expression.
540 *
541 * We try to reuse parsing functions in regexp.c to
542 * minimize surprise and keep the syntax consistent.
543 */
544
545/*
546 * Increments the pointer "p" by one (multi-byte) character.
547 */
548 static void
549nfa_inc(p)
550 char_u **p;
551{
552#ifdef FEAT_MBYTE
553 if (has_mbyte)
554 mb_ptr2char_adv(p);
555 else
556#endif
557 *p = *p + 1;
558}
559
560/*
561 * Decrements the pointer "p" by one (multi-byte) character.
562 */
563 static void
564nfa_dec(p)
565 char_u **p;
566{
567#ifdef FEAT_MBYTE
568 char_u *p2, *oldp;
569
570 if (has_mbyte)
571 {
572 oldp = *p;
573 /* Try to find the multibyte char that advances to the current
574 * position. */
575 do
576 {
577 *p = *p - 1;
578 p2 = *p;
579 mb_ptr2char_adv(&p2);
580 } while (p2 != oldp);
581 }
582#else
583 *p = *p - 1;
584#endif
585}
586
587/*
588 * Parse the lowest level.
589 *
590 * An atom can be one of a long list of items. Many atoms match one character
591 * in the text. It is often an ordinary character or a character class.
592 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
593 * is only for syntax highlighting.
594 *
595 * atom ::= ordinary-atom
596 * or \( pattern \)
597 * or \%( pattern \)
598 * or \z( pattern \)
599 */
600 static int
601nfa_regatom()
602{
603 int c;
604 int charclass;
605 int equiclass;
606 int collclass;
607 int got_coll_char;
608 char_u *p;
609 char_u *endp;
610#ifdef FEAT_MBYTE
611 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200612#endif
613 int extra = 0;
614 int first;
615 int emit_range;
616 int negated;
617 int result;
618 int startc = -1;
619 int endc = -1;
620 int oldstartc = -1;
621 int cpo_lit; /* 'cpoptions' contains 'l' flag */
622 int cpo_bsl; /* 'cpoptions' contains '\' flag */
623 int glue; /* ID that will "glue" nodes together */
624
625 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
626 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
627
628 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 switch (c)
630 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200631 case NUL:
632 syntax_error = TRUE;
633 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
634
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 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
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 case Magic('|'):
751 case Magic('&'):
752 case Magic(')'):
753 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200754 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200755 return FAIL;
756
757 case Magic('='):
758 case Magic('?'):
759 case Magic('+'):
760 case Magic('@'):
761 case Magic('*'):
762 case Magic('{'):
763 /* these should follow an atom, not form an atom */
764 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200765 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200766 return FAIL;
767
768 case Magic('~'): /* previous substitute pattern */
769 /* Not supported yet */
770 return FAIL;
771
772 case Magic('1'):
773 case Magic('2'):
774 case Magic('3'):
775 case Magic('4'):
776 case Magic('5'):
777 case Magic('6'):
778 case Magic('7'):
779 case Magic('8'):
780 case Magic('9'):
781 /* not supported yet */
782 return FAIL;
783
784 case Magic('z'):
785 c = no_Magic(getchr());
786 switch (c)
787 {
788 case 's':
789 EMIT(NFA_ZSTART);
790 break;
791 case 'e':
792 EMIT(NFA_ZEND);
793 nfa_has_zend = TRUE;
794 /* TODO: Currently \ze does not work properly. */
795 return FAIL;
796 /* break; */
797 case '1':
798 case '2':
799 case '3':
800 case '4':
801 case '5':
802 case '6':
803 case '7':
804 case '8':
805 case '9':
806 case '(':
807 /* \z1...\z9 and \z( not yet supported */
808 return FAIL;
809 default:
810 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200811 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 no_Magic(c));
813 return FAIL;
814 }
815 break;
816
817 case Magic('%'):
818 c = no_Magic(getchr());
819 switch (c)
820 {
821 /* () without a back reference */
822 case '(':
823 if (nfa_reg(REG_NPAREN) == FAIL)
824 return FAIL;
825 EMIT(NFA_NOPEN);
826 break;
827
828 case 'd': /* %d123 decimal */
829 case 'o': /* %o123 octal */
830 case 'x': /* %xab hex 2 */
831 case 'u': /* %uabcd hex 4 */
832 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200833 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200834 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200835
Bram Moolenaar47196582013-05-25 22:04:23 +0200836 switch (c)
837 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200838 case 'd': nr = getdecchrs(); break;
839 case 'o': nr = getoctchrs(); break;
840 case 'x': nr = gethexchrs(2); break;
841 case 'u': nr = gethexchrs(4); break;
842 case 'U': nr = gethexchrs(8); break;
843 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200844 }
845
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200846 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200847 EMSG2_RET_FAIL(
848 _("E678: Invalid character after %s%%[dxouU]"),
849 reg_magic == MAGIC_ALL);
850 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200851 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200852 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200853 break;
854
855 /* Catch \%^ and \%$ regardless of where they appear in the
856 * pattern -- regardless of whether or not it makes sense. */
857 case '^':
858 EMIT(NFA_BOF);
859 /* Not yet supported */
860 return FAIL;
861 break;
862
863 case '$':
864 EMIT(NFA_EOF);
865 /* Not yet supported */
866 return FAIL;
867 break;
868
869 case '#':
870 /* not supported yet */
871 return FAIL;
872 break;
873
874 case 'V':
875 /* not supported yet */
876 return FAIL;
877 break;
878
879 case '[':
880 /* \%[abc] not supported yet */
881 return FAIL;
882
883 default:
884 /* not supported yet */
885 return FAIL;
886 }
887 break;
888
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200889 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200890collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200891 /*
892 * Glue is emitted between several atoms from the [].
893 * It is either NFA_OR, or NFA_CONCAT.
894 *
895 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
896 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
897 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
898 * notation)
899 *
900 */
901
902
903/* Emit negation atoms, if needed.
904 * The CONCAT below merges the NOT with the previous node. */
905#define TRY_NEG() \
906 if (negated == TRUE) \
907 { \
908 EMIT(NFA_NOT); \
909 }
910
911/* Emit glue between important nodes : CONCAT or OR. */
912#define EMIT_GLUE() \
913 if (first == FALSE) \
914 EMIT(glue); \
915 else \
916 first = FALSE;
917
918 p = regparse;
919 endp = skip_anyof(p);
920 if (*endp == ']')
921 {
922 /*
923 * Try to reverse engineer character classes. For example,
924 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
925 * and perform the necessary substitutions in the NFA.
926 */
927 result = nfa_recognize_char_class(regparse, endp,
928 extra == ADD_NL);
929 if (result != FAIL)
930 {
931 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
932 EMIT(result);
933 else /* must be char class + newline */
934 {
935 EMIT(result - ADD_NL);
936 EMIT(NFA_NEWL);
937 EMIT(NFA_OR);
938 }
939 regparse = endp;
940 nfa_inc(&regparse);
941 return OK;
942 }
943 /*
944 * Failed to recognize a character class. Use the simple
945 * version that turns [abc] into 'a' OR 'b' OR 'c'
946 */
947 startc = endc = oldstartc = -1;
948 first = TRUE; /* Emitting first atom in this sequence? */
949 negated = FALSE;
950 glue = NFA_OR;
951 if (*regparse == '^') /* negated range */
952 {
953 negated = TRUE;
954 glue = NFA_CONCAT;
955 nfa_inc(&regparse);
956 }
957 if (*regparse == '-')
958 {
959 startc = '-';
960 EMIT(startc);
961 TRY_NEG();
962 EMIT_GLUE();
963 nfa_inc(&regparse);
964 }
965 /* Emit the OR branches for each character in the [] */
966 emit_range = FALSE;
967 while (regparse < endp)
968 {
969 oldstartc = startc;
970 startc = -1;
971 got_coll_char = FALSE;
972 if (*regparse == '[')
973 {
974 /* Check for [: :], [= =], [. .] */
975 equiclass = collclass = 0;
976 charclass = get_char_class(&regparse);
977 if (charclass == CLASS_NONE)
978 {
979 equiclass = get_equi_class(&regparse);
980 if (equiclass == 0)
981 collclass = get_coll_element(&regparse);
982 }
983
984 /* Character class like [:alpha:] */
985 if (charclass != CLASS_NONE)
986 {
987 switch (charclass)
988 {
989 case CLASS_ALNUM:
990 EMIT(NFA_CLASS_ALNUM);
991 break;
992 case CLASS_ALPHA:
993 EMIT(NFA_CLASS_ALPHA);
994 break;
995 case CLASS_BLANK:
996 EMIT(NFA_CLASS_BLANK);
997 break;
998 case CLASS_CNTRL:
999 EMIT(NFA_CLASS_CNTRL);
1000 break;
1001 case CLASS_DIGIT:
1002 EMIT(NFA_CLASS_DIGIT);
1003 break;
1004 case CLASS_GRAPH:
1005 EMIT(NFA_CLASS_GRAPH);
1006 break;
1007 case CLASS_LOWER:
1008 EMIT(NFA_CLASS_LOWER);
1009 break;
1010 case CLASS_PRINT:
1011 EMIT(NFA_CLASS_PRINT);
1012 break;
1013 case CLASS_PUNCT:
1014 EMIT(NFA_CLASS_PUNCT);
1015 break;
1016 case CLASS_SPACE:
1017 EMIT(NFA_CLASS_SPACE);
1018 break;
1019 case CLASS_UPPER:
1020 EMIT(NFA_CLASS_UPPER);
1021 break;
1022 case CLASS_XDIGIT:
1023 EMIT(NFA_CLASS_XDIGIT);
1024 break;
1025 case CLASS_TAB:
1026 EMIT(NFA_CLASS_TAB);
1027 break;
1028 case CLASS_RETURN:
1029 EMIT(NFA_CLASS_RETURN);
1030 break;
1031 case CLASS_BACKSPACE:
1032 EMIT(NFA_CLASS_BACKSPACE);
1033 break;
1034 case CLASS_ESCAPE:
1035 EMIT(NFA_CLASS_ESCAPE);
1036 break;
1037 }
1038 TRY_NEG();
1039 EMIT_GLUE();
1040 continue;
1041 }
1042 /* Try equivalence class [=a=] and the like */
1043 if (equiclass != 0)
1044 {
1045 result = nfa_emit_equi_class(equiclass, negated);
1046 if (result == FAIL)
1047 {
1048 /* should never happen */
1049 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1050 }
1051 EMIT_GLUE();
1052 continue;
1053 }
1054 /* Try collating class like [. .] */
1055 if (collclass != 0)
1056 {
1057 startc = collclass; /* allow [.a.]-x as a range */
1058 /* Will emit the proper atom at the end of the
1059 * while loop. */
1060 }
1061 }
1062 /* Try a range like 'a-x' or '\t-z' */
1063 if (*regparse == '-')
1064 {
1065 emit_range = TRUE;
1066 startc = oldstartc;
1067 nfa_inc(&regparse);
1068 continue; /* reading the end of the range */
1069 }
1070
1071 /* Now handle simple and escaped characters.
1072 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1073 * accepts "\t", "\e", etc., but only when the 'l' flag in
1074 * 'cpoptions' is not included.
1075 * Posix doesn't recognize backslash at all.
1076 */
1077 if (*regparse == '\\'
1078 && !cpo_bsl
1079 && regparse + 1 <= endp
1080 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1081 || (!cpo_lit
1082 && vim_strchr(REGEXP_ABBR, regparse[1])
1083 != NULL)
1084 )
1085 )
1086 {
1087 nfa_inc(&regparse);
1088
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001089 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001090 startc = reg_string ? NL : NFA_NEWL;
1091 else
1092 if (*regparse == 'd'
1093 || *regparse == 'o'
1094 || *regparse == 'x'
1095 || *regparse == 'u'
1096 || *regparse == 'U'
1097 )
1098 {
1099 /* TODO(RE) This needs more testing */
1100 startc = coll_get_char();
1101 got_coll_char = TRUE;
1102 nfa_dec(&regparse);
1103 }
1104 else
1105 {
1106 /* \r,\t,\e,\b */
1107 startc = backslash_trans(*regparse);
1108 }
1109 }
1110
1111 /* Normal printable char */
1112 if (startc == -1)
1113#ifdef FEAT_MBYTE
1114 startc = (*mb_ptr2char)(regparse);
1115#else
1116 startc = *regparse;
1117#endif
1118
1119 /* Previous char was '-', so this char is end of range. */
1120 if (emit_range)
1121 {
1122 endc = startc; startc = oldstartc;
1123 if (startc > endc)
1124 EMSG_RET_FAIL(_(e_invrange));
1125#ifdef FEAT_MBYTE
1126 if (has_mbyte && ((*mb_char2len)(startc) > 1
1127 || (*mb_char2len)(endc) > 1))
1128 {
1129 if (endc > startc + 256)
1130 EMSG_RET_FAIL(_(e_invrange));
1131 /* Emit the range. "startc" was already emitted, so
1132 * skip it. */
1133 for (c = startc + 1; c <= endc; c++)
1134 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001135 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001136 TRY_NEG();
1137 EMIT_GLUE();
1138 }
1139 emit_range = FALSE;
1140 }
1141 else
1142#endif
1143 {
1144#ifdef EBCDIC
1145 int alpha_only = FALSE;
1146
1147 /* for alphabetical range skip the gaps
1148 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1149 if (isalpha(startc) && isalpha(endc))
1150 alpha_only = TRUE;
1151#endif
1152 /* Emit the range. "startc" was already emitted, so
1153 * skip it. */
1154 for (c = startc + 1; c <= endc; c++)
1155#ifdef EBCDIC
1156 if (!alpha_only || isalpha(startc))
1157#endif
1158 {
1159 EMIT(c);
1160 TRY_NEG();
1161 EMIT_GLUE();
1162 }
1163 emit_range = FALSE;
1164 }
1165 }
1166 else
1167 {
1168 /*
1169 * This char (startc) is not part of a range. Just
1170 * emit it.
1171 *
1172 * Normally, simply emit startc. But if we get char
1173 * code=0 from a collating char, then replace it with
1174 * 0x0a.
1175 *
1176 * This is needed to completely mimic the behaviour of
1177 * the backtracking engine.
1178 */
1179 if (got_coll_char == TRUE && startc == 0)
1180 EMIT(0x0a);
1181 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001182 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001183 TRY_NEG();
1184 EMIT_GLUE();
1185 }
1186
1187 nfa_inc(&regparse);
1188 } /* while (p < endp) */
1189
1190 nfa_dec(&regparse);
1191 if (*regparse == '-') /* if last, '-' is just a char */
1192 {
1193 EMIT('-');
1194 TRY_NEG();
1195 EMIT_GLUE();
1196 }
1197 nfa_inc(&regparse);
1198
1199 if (extra == ADD_NL) /* \_[] also matches \n */
1200 {
1201 EMIT(reg_string ? NL : NFA_NEWL);
1202 TRY_NEG();
1203 EMIT_GLUE();
1204 }
1205
1206 /* skip the trailing ] */
1207 regparse = endp;
1208 nfa_inc(&regparse);
1209 if (negated == TRUE)
1210 {
1211 /* Mark end of negated char range */
1212 EMIT(NFA_END_NEG_RANGE);
1213 EMIT(NFA_CONCAT);
1214 }
1215 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001216 } /* if exists closing ] */
1217
1218 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219 {
1220 syntax_error = TRUE;
1221 EMSG_RET_FAIL(_(e_missingbracket));
1222 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001223 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 default:
1226 {
1227#ifdef FEAT_MBYTE
1228 int plen;
1229
1230nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001231 /* plen is length of current char with composing chars */
1232 if (enc_utf8 && ((*mb_char2len)(c)
1233 != (plen = (*mb_ptr2len)(old_regparse))
1234 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001236 int i = 0;
1237
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001238 /* A base character plus composing characters, or just one
1239 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001240 * This requires creating a separate atom as if enclosing
1241 * the characters in (), where NFA_COMPOSING is the ( and
1242 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 * building the postfix form, not the NFA itself;
1244 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001245 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001246 for (;;)
1247 {
1248 EMIT(c);
1249 if (i > 0)
1250 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001251 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001252 break;
1253 c = utf_ptr2char(old_regparse + i);
1254 }
1255 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256 regparse = old_regparse + plen;
1257 }
1258 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259#endif
1260 {
1261 c = no_Magic(c);
1262 EMIT(c);
1263 }
1264 return OK;
1265 }
1266 }
1267
1268#undef TRY_NEG
1269#undef EMIT_GLUE
1270
1271 return OK;
1272}
1273
1274/*
1275 * Parse something followed by possible [*+=].
1276 *
1277 * A piece is an atom, possibly followed by a multi, an indication of how many
1278 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1279 * characters: "", "a", "aa", etc.
1280 *
1281 * piece ::= atom
1282 * or atom multi
1283 */
1284 static int
1285nfa_regpiece()
1286{
1287 int i;
1288 int op;
1289 int ret;
1290 long minval, maxval;
1291 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1292 char_u *old_regparse, *new_regparse;
1293 int c2;
1294 int *old_post_ptr, *my_post_start;
1295 int old_regnpar;
1296 int quest;
1297
1298 /* Save the current position in the regexp, so that we can use it if
1299 * <atom>{m,n} is next. */
1300 old_regparse = regparse;
1301 /* Save current number of open parenthesis, so we can use it if
1302 * <atom>{m,n} is next */
1303 old_regnpar = regnpar;
1304 /* store current pos in the postfix form, for \{m,n} involving 0s */
1305 my_post_start = post_ptr;
1306
1307 ret = nfa_regatom();
1308 if (ret == FAIL)
1309 return FAIL; /* cascaded error */
1310
1311 op = peekchr();
1312 if (re_multi_type(op) == NOT_MULTI)
1313 return OK;
1314
1315 skipchr();
1316 switch (op)
1317 {
1318 case Magic('*'):
1319 EMIT(NFA_STAR);
1320 break;
1321
1322 case Magic('+'):
1323 /*
1324 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1325 * first and only submatch would be "aaa". But the backtracking
1326 * engine interprets the plus as "try matching one more time", and
1327 * a* matches a second time at the end of the input, the empty
1328 * string.
1329 * The submatch will the empty string.
1330 *
1331 * In order to be consistent with the old engine, we disable
1332 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1333 */
1334 /* EMIT(NFA_PLUS); */
1335 regnpar = old_regnpar;
1336 regparse = old_regparse;
1337 curchr = -1;
1338 if (nfa_regatom() == FAIL)
1339 return FAIL;
1340 EMIT(NFA_STAR);
1341 EMIT(NFA_CONCAT);
1342 skipchr(); /* skip the \+ */
1343 break;
1344
1345 case Magic('@'):
1346 op = no_Magic(getchr());
1347 switch(op)
1348 {
1349 case '=':
1350 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1351 break;
1352 case '!':
1353 case '<':
1354 case '>':
1355 /* Not supported yet */
1356 return FAIL;
1357 default:
1358 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001359 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001360 return FAIL;
1361 }
1362 break;
1363
1364 case Magic('?'):
1365 case Magic('='):
1366 EMIT(NFA_QUEST);
1367 break;
1368
1369 case Magic('{'):
1370 /* a{2,5} will expand to 'aaa?a?a?'
1371 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1372 * version of '?'
1373 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1374 * parenthesis have the same id
1375 */
1376
1377 greedy = TRUE;
1378 c2 = peekchr();
1379 if (c2 == '-' || c2 == Magic('-'))
1380 {
1381 skipchr();
1382 greedy = FALSE;
1383 }
1384 if (!read_limits(&minval, &maxval))
1385 {
1386 syntax_error = TRUE;
1387 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1388 }
1389 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1390 * <atom>* */
1391 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1392 {
1393 EMIT(NFA_STAR);
1394 break;
1395 }
1396
1397 if (maxval > NFA_BRACES_MAXLIMIT)
1398 {
1399 /* This would yield a huge automaton and use too much memory.
1400 * Revert to old engine */
1401 return FAIL;
1402 }
1403
1404 /* Special case: x{0} or x{-0} */
1405 if (maxval == 0)
1406 {
1407 /* Ignore result of previous call to nfa_regatom() */
1408 post_ptr = my_post_start;
1409 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1410 EMIT(NFA_SKIP_CHAR);
1411 return OK;
1412 }
1413
1414 /* Ignore previous call to nfa_regatom() */
1415 post_ptr = my_post_start;
1416 /* Save pos after the repeated atom and the \{} */
1417 new_regparse = regparse;
1418
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001419 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1420 for (i = 0; i < maxval; i++)
1421 {
1422 /* Goto beginning of the repeated atom */
1423 regparse = old_regparse;
1424 curchr = -1;
1425 /* Restore count of parenthesis */
1426 regnpar = old_regnpar;
1427 old_post_ptr = post_ptr;
1428 if (nfa_regatom() == FAIL)
1429 return FAIL;
1430 /* after "minval" times, atoms are optional */
1431 if (i + 1 > minval)
1432 EMIT(quest);
1433 if (old_post_ptr != my_post_start)
1434 EMIT(NFA_CONCAT);
1435 }
1436
1437 /* Go to just after the repeated atom and the \{} */
1438 regparse = new_regparse;
1439 curchr = -1;
1440
1441 break;
1442
1443
1444 default:
1445 break;
1446 } /* end switch */
1447
1448 if (re_multi_type(peekchr()) != NOT_MULTI)
1449 {
1450 /* Can't have a multi follow a multi. */
1451 syntax_error = TRUE;
1452 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1453 }
1454
1455 return OK;
1456}
1457
1458/*
1459 * Parse one or more pieces, concatenated. It matches a match for the
1460 * first piece, followed by a match for the second piece, etc. Example:
1461 * "f[0-9]b", first matches "f", then a digit and then "b".
1462 *
1463 * concat ::= piece
1464 * or piece piece
1465 * or piece piece piece
1466 * etc.
1467 */
1468 static int
1469nfa_regconcat()
1470{
1471 int cont = TRUE;
1472 int first = TRUE;
1473
1474 while (cont)
1475 {
1476 switch (peekchr())
1477 {
1478 case NUL:
1479 case Magic('|'):
1480 case Magic('&'):
1481 case Magic(')'):
1482 cont = FALSE;
1483 break;
1484
1485 case Magic('Z'):
1486#ifdef FEAT_MBYTE
1487 regflags |= RF_ICOMBINE;
1488#endif
1489 skipchr_keepstart();
1490 break;
1491 case Magic('c'):
1492 regflags |= RF_ICASE;
1493 skipchr_keepstart();
1494 break;
1495 case Magic('C'):
1496 regflags |= RF_NOICASE;
1497 skipchr_keepstart();
1498 break;
1499 case Magic('v'):
1500 reg_magic = MAGIC_ALL;
1501 skipchr_keepstart();
1502 curchr = -1;
1503 break;
1504 case Magic('m'):
1505 reg_magic = MAGIC_ON;
1506 skipchr_keepstart();
1507 curchr = -1;
1508 break;
1509 case Magic('M'):
1510 reg_magic = MAGIC_OFF;
1511 skipchr_keepstart();
1512 curchr = -1;
1513 break;
1514 case Magic('V'):
1515 reg_magic = MAGIC_NONE;
1516 skipchr_keepstart();
1517 curchr = -1;
1518 break;
1519
1520 default:
1521 if (nfa_regpiece() == FAIL)
1522 return FAIL;
1523 if (first == FALSE)
1524 EMIT(NFA_CONCAT);
1525 else
1526 first = FALSE;
1527 break;
1528 }
1529 }
1530
1531 return OK;
1532}
1533
1534/*
1535 * Parse a branch, one or more concats, separated by "\&". It matches the
1536 * last concat, but only if all the preceding concats also match at the same
1537 * position. Examples:
1538 * "foobeep\&..." matches "foo" in "foobeep".
1539 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1540 *
1541 * branch ::= concat
1542 * or concat \& concat
1543 * or concat \& concat \& concat
1544 * etc.
1545 */
1546 static int
1547nfa_regbranch()
1548{
1549 int ch;
1550 int *old_post_ptr;
1551
1552 old_post_ptr = post_ptr;
1553
1554 /* First branch, possibly the only one */
1555 if (nfa_regconcat() == FAIL)
1556 return FAIL;
1557
1558 ch = peekchr();
1559 /* Try next concats */
1560 while (ch == Magic('&'))
1561 {
1562 skipchr();
1563 EMIT(NFA_NOPEN);
1564 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1565 old_post_ptr = post_ptr;
1566 if (nfa_regconcat() == FAIL)
1567 return FAIL;
1568 /* if concat is empty, skip a input char. But do emit a node */
1569 if (old_post_ptr == post_ptr)
1570 EMIT(NFA_SKIP_CHAR);
1571 EMIT(NFA_CONCAT);
1572 ch = peekchr();
1573 }
1574
1575 /* Even if a branch is empty, emit one node for it */
1576 if (old_post_ptr == post_ptr)
1577 EMIT(NFA_SKIP_CHAR);
1578
1579 return OK;
1580}
1581
1582/*
1583 * Parse a pattern, one or more branches, separated by "\|". It matches
1584 * anything that matches one of the branches. Example: "foo\|beep" matches
1585 * "foo" and matches "beep". If more than one branch matches, the first one
1586 * is used.
1587 *
1588 * pattern ::= branch
1589 * or branch \| branch
1590 * or branch \| branch \| branch
1591 * etc.
1592 */
1593 static int
1594nfa_reg(paren)
1595 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1596{
1597 int parno = 0;
1598
1599#ifdef FEAT_SYN_HL
1600#endif
1601 if (paren == REG_PAREN)
1602 {
1603 if (regnpar >= NSUBEXP) /* Too many `(' */
1604 {
1605 syntax_error = TRUE;
1606 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1607 }
1608 parno = regnpar++;
1609 }
1610
1611 if (nfa_regbranch() == FAIL)
1612 return FAIL; /* cascaded error */
1613
1614 while (peekchr() == Magic('|'))
1615 {
1616 skipchr();
1617 if (nfa_regbranch() == FAIL)
1618 return FAIL; /* cascaded error */
1619 EMIT(NFA_OR);
1620 }
1621
1622 /* Check for proper termination. */
1623 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1624 {
1625 syntax_error = TRUE;
1626 if (paren == REG_NPAREN)
1627 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1628 else
1629 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1630 }
1631 else if (paren == REG_NOPAREN && peekchr() != NUL)
1632 {
1633 syntax_error = TRUE;
1634 if (peekchr() == Magic(')'))
1635 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1636 else
1637 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1638 }
1639 /*
1640 * Here we set the flag allowing back references to this set of
1641 * parentheses.
1642 */
1643 if (paren == REG_PAREN)
1644 {
1645 had_endbrace[parno] = TRUE; /* have seen the close paren */
1646 EMIT(NFA_MOPEN + parno);
1647 }
1648
1649 return OK;
1650}
1651
Bram Moolenaar963fee22013-05-26 21:47:28 +02001652typedef union
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653{
Bram Moolenaar963fee22013-05-26 21:47:28 +02001654 struct multipos
1655 {
1656 lpos_T start;
1657 lpos_T end;
1658 } multilist[NSUBEXP];
1659 struct linepos
1660 {
1661 char_u *start;
1662 char_u *end;
1663 } linelist[NSUBEXP];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001664} regsub_T;
1665
1666static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
1667
1668#ifdef DEBUG
1669static char_u code[50];
1670
1671 static void
1672nfa_set_code(c)
1673 int c;
1674{
1675 int addnl = FALSE;
1676
1677 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1678 {
1679 addnl = TRUE;
1680 c -= ADD_NL;
1681 }
1682
1683 STRCPY(code, "");
1684 switch (c)
1685 {
1686 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1687 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1688 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1689 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1690 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1691 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1692
1693 case NFA_PREV_ATOM_NO_WIDTH:
1694 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1695 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1696 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1697 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1698 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1699
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001700 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1701 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1702
1703 case NFA_MOPEN + 0:
1704 case NFA_MOPEN + 1:
1705 case NFA_MOPEN + 2:
1706 case NFA_MOPEN + 3:
1707 case NFA_MOPEN + 4:
1708 case NFA_MOPEN + 5:
1709 case NFA_MOPEN + 6:
1710 case NFA_MOPEN + 7:
1711 case NFA_MOPEN + 8:
1712 case NFA_MOPEN + 9:
1713 STRCPY(code, "NFA_MOPEN(x)");
1714 code[10] = c - NFA_MOPEN + '0';
1715 break;
1716 case NFA_MCLOSE + 0:
1717 case NFA_MCLOSE + 1:
1718 case NFA_MCLOSE + 2:
1719 case NFA_MCLOSE + 3:
1720 case NFA_MCLOSE + 4:
1721 case NFA_MCLOSE + 5:
1722 case NFA_MCLOSE + 6:
1723 case NFA_MCLOSE + 7:
1724 case NFA_MCLOSE + 8:
1725 case NFA_MCLOSE + 9:
1726 STRCPY(code, "NFA_MCLOSE(x)");
1727 code[11] = c - NFA_MCLOSE + '0';
1728 break;
1729 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1730 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1731 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1732 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1733 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1734 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1735 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1736 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1737 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1738 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1739 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1740 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1741 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1742 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1743 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1744 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1745 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1746 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1747 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1748 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1749 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1750 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1751 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1752 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1753 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1754 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1755 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1756 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1757
1758 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1759 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1760 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1761 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1762 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1763 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1764 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1765 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1766 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1767 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1768 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1769 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1770 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1771 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1772 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1773 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1774 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1775 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1776 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1777 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1778 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1779 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1780 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1781 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1782 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1783 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1784 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1785
1786 default:
1787 STRCPY(code, "CHAR(x)");
1788 code[5] = c;
1789 }
1790
1791 if (addnl == TRUE)
1792 STRCAT(code, " + NEWLINE ");
1793
1794}
1795
1796#ifdef ENABLE_LOG
1797static FILE *log_fd;
1798
1799/*
1800 * Print the postfix notation of the current regexp.
1801 */
1802 static void
1803nfa_postfix_dump(expr, retval)
1804 char_u *expr;
1805 int retval;
1806{
1807 int *p;
1808 FILE *f;
1809
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001810 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811 if (f != NULL)
1812 {
1813 fprintf(f, "\n-------------------------\n");
1814 if (retval == FAIL)
1815 fprintf(f, ">>> NFA engine failed ... \n");
1816 else if (retval == OK)
1817 fprintf(f, ">>> NFA engine succeeded !\n");
1818 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001819 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820 {
1821 nfa_set_code(*p);
1822 fprintf(f, "%s, ", code);
1823 }
1824 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001825 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001826 fprintf(f, "%d ", *p);
1827 fprintf(f, "\n\n");
1828 fclose(f);
1829 }
1830}
1831
1832/*
1833 * Print the NFA starting with a root node "state".
1834 */
1835 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001836nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837 FILE *debugf;
1838 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001839{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001840 garray_T indent;
1841
1842 ga_init2(&indent, 1, 64);
1843 ga_append(&indent, '\0');
1844 nfa_print_state2(debugf, state, &indent);
1845 ga_clear(&indent);
1846}
1847
1848 static void
1849nfa_print_state2(debugf, state, indent)
1850 FILE *debugf;
1851 nfa_state_T *state;
1852 garray_T *indent;
1853{
1854 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855
1856 if (state == NULL)
1857 return;
1858
1859 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001860
1861 /* Output indent */
1862 p = (char_u *)indent->ga_data;
1863 if (indent->ga_len >= 3)
1864 {
1865 int last = indent->ga_len - 3;
1866 char_u save[2];
1867
1868 STRNCPY(save, &p[last], 2);
1869 STRNCPY(&p[last], "+-", 2);
1870 fprintf(debugf, " %s", p);
1871 STRNCPY(&p[last], save, 2);
1872 }
1873 else
1874 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001875
1876 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001877 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1878 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879 if (state->id < 0)
1880 return;
1881
1882 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001883
1884 /* grow indent for state->out */
1885 indent->ga_len -= 1;
1886 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001887 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001888 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001889 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001890 ga_append(indent, '\0');
1891
1892 nfa_print_state2(debugf, state->out, indent);
1893
1894 /* replace last part of indent for state->out1 */
1895 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001896 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001897 ga_append(indent, '\0');
1898
1899 nfa_print_state2(debugf, state->out1, indent);
1900
1901 /* shrink indent */
1902 indent->ga_len -= 3;
1903 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904}
1905
1906/*
1907 * Print the NFA state machine.
1908 */
1909 static void
1910nfa_dump(prog)
1911 nfa_regprog_T *prog;
1912{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001913 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914
1915 if (debugf != NULL)
1916 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001917 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001918 fclose(debugf);
1919 }
1920}
1921#endif /* ENABLE_LOG */
1922#endif /* DEBUG */
1923
1924/*
1925 * Parse r.e. @expr and convert it into postfix form.
1926 * Return the postfix string on success, NULL otherwise.
1927 */
1928 static int *
1929re2post()
1930{
1931 if (nfa_reg(REG_NOPAREN) == FAIL)
1932 return NULL;
1933 EMIT(NFA_MOPEN);
1934 return post_start;
1935}
1936
1937/* NB. Some of the code below is inspired by Russ's. */
1938
1939/*
1940 * Represents an NFA state plus zero or one or two arrows exiting.
1941 * if c == MATCH, no arrows out; matching state.
1942 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1943 * If c < 256, labeled arrow with character c to out.
1944 */
1945
1946static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1947
1948/*
1949 * Allocate and initialize nfa_state_T.
1950 */
1951 static nfa_state_T *
1952new_state(c, out, out1)
1953 int c;
1954 nfa_state_T *out;
1955 nfa_state_T *out1;
1956{
1957 nfa_state_T *s;
1958
1959 if (istate >= nstate)
1960 return NULL;
1961
1962 s = &state_ptr[istate++];
1963
1964 s->c = c;
1965 s->out = out;
1966 s->out1 = out1;
1967
1968 s->id = istate;
1969 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970 s->visits = 0;
1971 s->negated = FALSE;
1972
1973 return s;
1974}
1975
1976/*
1977 * A partially built NFA without the matching state filled in.
1978 * Frag_T.start points at the start state.
1979 * Frag_T.out is a list of places that need to be set to the
1980 * next state for this fragment.
1981 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001982
1983/* Since the out pointers in the list are always
1984 * uninitialized, we use the pointers themselves
1985 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001986typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001987union Ptrlist
1988{
1989 Ptrlist *next;
1990 nfa_state_T *s;
1991};
1992
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001993struct Frag
1994{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001995 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001996 Ptrlist *out;
1997};
1998typedef struct Frag Frag_T;
1999
2000static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2001static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2002static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2003static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2004static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2005static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2006
2007/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002008 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009 */
2010 static Frag_T
2011frag(start, out)
2012 nfa_state_T *start;
2013 Ptrlist *out;
2014{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002015 Frag_T n;
2016
2017 n.start = start;
2018 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002019 return n;
2020}
2021
2022/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 * Create singleton list containing just outp.
2024 */
2025 static Ptrlist *
2026list1(outp)
2027 nfa_state_T **outp;
2028{
2029 Ptrlist *l;
2030
2031 l = (Ptrlist *)outp;
2032 l->next = NULL;
2033 return l;
2034}
2035
2036/*
2037 * Patch the list of states at out to point to start.
2038 */
2039 static void
2040patch(l, s)
2041 Ptrlist *l;
2042 nfa_state_T *s;
2043{
2044 Ptrlist *next;
2045
2046 for (; l; l = next)
2047 {
2048 next = l->next;
2049 l->s = s;
2050 }
2051}
2052
2053
2054/*
2055 * Join the two lists l1 and l2, returning the combination.
2056 */
2057 static Ptrlist *
2058append(l1, l2)
2059 Ptrlist *l1;
2060 Ptrlist *l2;
2061{
2062 Ptrlist *oldl1;
2063
2064 oldl1 = l1;
2065 while (l1->next)
2066 l1 = l1->next;
2067 l1->next = l2;
2068 return oldl1;
2069}
2070
2071/*
2072 * Stack used for transforming postfix form into NFA.
2073 */
2074static Frag_T empty;
2075
2076 static void
2077st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002078 int *postfix UNUSED;
2079 int *end UNUSED;
2080 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002082#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 FILE *df;
2084 int *p2;
2085
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002086 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 if (df)
2088 {
2089 fprintf(df, "Error popping the stack!\n");
2090#ifdef DEBUG
2091 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2092#endif
2093 fprintf(df, "Postfix form is: ");
2094#ifdef DEBUG
2095 for (p2 = postfix; p2 < end; p2++)
2096 {
2097 nfa_set_code(*p2);
2098 fprintf(df, "%s, ", code);
2099 }
2100 nfa_set_code(*p);
2101 fprintf(df, "\nCurrent position is: ");
2102 for (p2 = postfix; p2 <= p; p2 ++)
2103 {
2104 nfa_set_code(*p2);
2105 fprintf(df, "%s, ", code);
2106 }
2107#else
2108 for (p2 = postfix; p2 < end; p2++)
2109 {
2110 fprintf(df, "%d, ", *p2);
2111 }
2112 fprintf(df, "\nCurrent position is: ");
2113 for (p2 = postfix; p2 <= p; p2 ++)
2114 {
2115 fprintf(df, "%d, ", *p2);
2116 }
2117#endif
2118 fprintf(df, "\n--------------------------\n");
2119 fclose(df);
2120 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002121#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 EMSG(_("E874: (NFA) Could not pop the stack !"));
2123}
2124
2125/*
2126 * Push an item onto the stack.
2127 */
2128 static void
2129st_push(s, p, stack_end)
2130 Frag_T s;
2131 Frag_T **p;
2132 Frag_T *stack_end;
2133{
2134 Frag_T *stackp = *p;
2135
2136 if (stackp >= stack_end)
2137 return;
2138 *stackp = s;
2139 *p = *p + 1;
2140}
2141
2142/*
2143 * Pop an item from the stack.
2144 */
2145 static Frag_T
2146st_pop(p, stack)
2147 Frag_T **p;
2148 Frag_T *stack;
2149{
2150 Frag_T *stackp;
2151
2152 *p = *p - 1;
2153 stackp = *p;
2154 if (stackp < stack)
2155 return empty;
2156 return **p;
2157}
2158
2159/*
2160 * Convert a postfix form into its equivalent NFA.
2161 * Return the NFA start state on success, NULL otherwise.
2162 */
2163 static nfa_state_T *
2164post2nfa(postfix, end, nfa_calc_size)
2165 int *postfix;
2166 int *end;
2167 int nfa_calc_size;
2168{
2169 int *p;
2170 int mopen;
2171 int mclose;
2172 Frag_T *stack = NULL;
2173 Frag_T *stackp = NULL;
2174 Frag_T *stack_end = NULL;
2175 Frag_T e1;
2176 Frag_T e2;
2177 Frag_T e;
2178 nfa_state_T *s;
2179 nfa_state_T *s1;
2180 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002181 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182
2183 if (postfix == NULL)
2184 return NULL;
2185
Bram Moolenaar053bb602013-05-20 13:55:21 +02002186#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187#define POP() st_pop(&stackp, stack); \
2188 if (stackp < stack) \
2189 { \
2190 st_error(postfix, end, p); \
2191 return NULL; \
2192 }
2193
2194 if (nfa_calc_size == FALSE)
2195 {
2196 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002197 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002199 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002200 }
2201
2202 for (p = postfix; p < end; ++p)
2203 {
2204 switch (*p)
2205 {
2206 case NFA_CONCAT:
2207 /* Catenation.
2208 * Pay attention: this operator does not exist
2209 * in the r.e. itself (it is implicit, really).
2210 * It is added when r.e. is translated to postfix
2211 * form in re2post().
2212 *
2213 * No new state added here. */
2214 if (nfa_calc_size == TRUE)
2215 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002216 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 break;
2218 }
2219 e2 = POP();
2220 e1 = POP();
2221 patch(e1.out, e2.start);
2222 PUSH(frag(e1.start, e2.out));
2223 break;
2224
2225 case NFA_NOT:
2226 /* Negation of a character */
2227 if (nfa_calc_size == TRUE)
2228 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002229 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002230 break;
2231 }
2232 e1 = POP();
2233 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002234#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002235 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002237#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002238 PUSH(e1);
2239 break;
2240
2241 case NFA_OR:
2242 /* Alternation */
2243 if (nfa_calc_size == TRUE)
2244 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002245 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246 break;
2247 }
2248 e2 = POP();
2249 e1 = POP();
2250 s = new_state(NFA_SPLIT, e1.start, e2.start);
2251 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002252 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002253 PUSH(frag(s, append(e1.out, e2.out)));
2254 break;
2255
2256 case NFA_STAR:
2257 /* Zero or more */
2258 if (nfa_calc_size == TRUE)
2259 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002260 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261 break;
2262 }
2263 e = POP();
2264 s = new_state(NFA_SPLIT, e.start, NULL);
2265 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002266 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002267 patch(e.out, s);
2268 PUSH(frag(s, list1(&s->out1)));
2269 break;
2270
2271 case NFA_QUEST:
2272 /* one or zero atoms=> greedy match */
2273 if (nfa_calc_size == TRUE)
2274 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002275 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276 break;
2277 }
2278 e = POP();
2279 s = new_state(NFA_SPLIT, e.start, NULL);
2280 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002281 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282 PUSH(frag(s, append(e.out, list1(&s->out1))));
2283 break;
2284
2285 case NFA_QUEST_NONGREEDY:
2286 /* zero or one atoms => non-greedy match */
2287 if (nfa_calc_size == TRUE)
2288 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002289 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002290 break;
2291 }
2292 e = POP();
2293 s = new_state(NFA_SPLIT, NULL, e.start);
2294 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002295 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296 PUSH(frag(s, append(e.out, list1(&s->out))));
2297 break;
2298
2299 case NFA_PLUS:
2300 /* One or more */
2301 if (nfa_calc_size == TRUE)
2302 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002303 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 break;
2305 }
2306 e = POP();
2307 s = new_state(NFA_SPLIT, e.start, NULL);
2308 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002309 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002310 patch(e.out, s);
2311 PUSH(frag(e.start, list1(&s->out1)));
2312 break;
2313
2314 case NFA_SKIP_CHAR:
2315 /* Symbol of 0-length, Used in a repetition
2316 * with max/min count of 0 */
2317 if (nfa_calc_size == TRUE)
2318 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002319 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320 break;
2321 }
2322 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2323 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002324 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 PUSH(frag(s, list1(&s->out)));
2326 break;
2327
2328 case NFA_PREV_ATOM_NO_WIDTH:
2329 /* The \@= operator: match the preceding atom with 0 width.
2330 * Surrounds the preceding atom with START_INVISIBLE and
2331 * END_INVISIBLE, similarly to MOPEN.
2332 */
2333 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002334 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002335
2336 if (nfa_calc_size == TRUE)
2337 {
2338 nstate += 2;
2339 break;
2340 }
2341 e = POP();
2342 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2343 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002344 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345 patch(e.out, s1);
2346
2347 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2348 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002349 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002350 PUSH(frag(s, list1(&s1->out)));
2351 break;
2352
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002353#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002354 case NFA_COMPOSING: /* char with composing char */
2355#if 0
2356 /* TODO */
2357 if (regflags & RF_ICOMBINE)
2358 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002359 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002360 }
2361#endif
2362 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002363#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002364
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 case NFA_MOPEN + 0: /* Submatch */
2366 case NFA_MOPEN + 1:
2367 case NFA_MOPEN + 2:
2368 case NFA_MOPEN + 3:
2369 case NFA_MOPEN + 4:
2370 case NFA_MOPEN + 5:
2371 case NFA_MOPEN + 6:
2372 case NFA_MOPEN + 7:
2373 case NFA_MOPEN + 8:
2374 case NFA_MOPEN + 9:
2375 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376 if (nfa_calc_size == TRUE)
2377 {
2378 nstate += 2;
2379 break;
2380 }
2381
2382 mopen = *p;
2383 switch (*p)
2384 {
2385 case NFA_NOPEN:
2386 mclose = NFA_NCLOSE;
2387 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002388#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389 case NFA_COMPOSING:
2390 mclose = NFA_END_COMPOSING;
2391 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002392#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393 default:
2394 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2395 mclose = *p + NSUBEXP;
2396 break;
2397 }
2398
2399 /* Allow "NFA_MOPEN" as a valid postfix representation for
2400 * the empty regexp "". In this case, the NFA will be
2401 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2402 * empty groups of parenthesis, and empty mbyte chars */
2403 if (stackp == stack)
2404 {
2405 s = new_state(mopen, NULL, NULL);
2406 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002407 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002408 s1 = new_state(mclose, NULL, NULL);
2409 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002410 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002411 patch(list1(&s->out), s1);
2412 PUSH(frag(s, list1(&s1->out)));
2413 break;
2414 }
2415
2416 /* At least one node was emitted before NFA_MOPEN, so
2417 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2418 e = POP();
2419 s = new_state(mopen, e.start, NULL); /* `(' */
2420 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002421 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002422
2423 s1 = new_state(mclose, NULL, NULL); /* `)' */
2424 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002425 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002426 patch(e.out, s1);
2427
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002428#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002429 if (mopen == NFA_COMPOSING)
2430 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002432#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433
2434 PUSH(frag(s, list1(&s1->out)));
2435 break;
2436
2437 case NFA_ZSTART:
2438 case NFA_ZEND:
2439 default:
2440 /* Operands */
2441 if (nfa_calc_size == TRUE)
2442 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002443 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 break;
2445 }
2446 s = new_state(*p, NULL, NULL);
2447 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002448 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002449 PUSH(frag(s, list1(&s->out)));
2450 break;
2451
2452 } /* switch(*p) */
2453
2454 } /* for(p = postfix; *p; ++p) */
2455
2456 if (nfa_calc_size == TRUE)
2457 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002458 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002459 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460 }
2461
2462 e = POP();
2463 if (stackp != stack)
2464 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2465
2466 if (istate >= nstate)
2467 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2468
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 matchstate = &state_ptr[istate++]; /* the match state */
2470 matchstate->c = NFA_MATCH;
2471 matchstate->out = matchstate->out1 = NULL;
2472
2473 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002474 ret = e.start;
2475
2476theend:
2477 vim_free(stack);
2478 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479
2480#undef POP1
2481#undef PUSH1
2482#undef POP2
2483#undef PUSH2
2484#undef POP
2485#undef PUSH
2486}
2487
2488/****************************************************************
2489 * NFA execution code.
2490 ****************************************************************/
2491
Bram Moolenaar963fee22013-05-26 21:47:28 +02002492/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002493typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494{
2495 nfa_state_T *state;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002496 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002497} nfa_thread_T;
2498
Bram Moolenaar963fee22013-05-26 21:47:28 +02002499/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500typedef struct
2501{
Bram Moolenaar4b417062013-05-25 20:19:50 +02002502 nfa_thread_T *t;
2503 int n;
2504} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505
Bram Moolenaar963fee22013-05-26 21:47:28 +02002506/* Used during execution: whether a match has been found. */
2507static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002508
Bram Moolenaar963fee22013-05-26 21:47:28 +02002509static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int off, int lid));
2510
2511static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int lid, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002512
2513 static void
Bram Moolenaar963fee22013-05-26 21:47:28 +02002514addstate(l, state, m, off, lid)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002515 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516 nfa_state_T *state; /* state to update */
2517 regsub_T *m; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002518 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 int lid;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002521 int subidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002522 nfa_thread_T *lastthread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002523 lpos_T save_lpos;
2524 char_u *save_ptr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002525
2526 if (l == NULL || state == NULL)
2527 return;
2528
2529 switch (state->c)
2530 {
2531 case NFA_SPLIT:
2532 case NFA_NOT:
2533 case NFA_NOPEN:
2534 case NFA_NCLOSE:
2535 case NFA_MCLOSE:
2536 case NFA_MCLOSE + 1:
2537 case NFA_MCLOSE + 2:
2538 case NFA_MCLOSE + 3:
2539 case NFA_MCLOSE + 4:
2540 case NFA_MCLOSE + 5:
2541 case NFA_MCLOSE + 6:
2542 case NFA_MCLOSE + 7:
2543 case NFA_MCLOSE + 8:
2544 case NFA_MCLOSE + 9:
2545 /* Do not remember these nodes in list "thislist" or "nextlist" */
2546 break;
2547
2548 default:
2549 if (state->lastlist == lid)
2550 {
2551 if (++state->visits > 2)
2552 return;
2553 }
2554 else
2555 {
2556 /* add the state to the list */
2557 state->lastlist = lid;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002558 lastthread = &l->t[l->n++];
2559 lastthread->state = state;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002560
2561 /* Copy the match start and end positions. */
2562 if (REG_MULTI)
2563 mch_memmove(&lastthread->sub.multilist[0],
2564 &m->multilist[0],
2565 sizeof(struct multipos) * nfa_nsubexpr);
2566 else
2567 mch_memmove(&lastthread->sub.linelist[0],
2568 &m->linelist[0],
2569 sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002570 }
2571 }
2572
2573#ifdef ENABLE_LOG
2574 nfa_set_code(state->c);
2575 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n",
2576 abs(state->id), code, state->c);
2577#endif
2578 switch (state->c)
2579 {
2580 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002581 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002582 break;
2583
2584 case NFA_SPLIT:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002585 addstate(l, state->out, m, off, lid);
2586 addstate(l, state->out1, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002587 break;
2588
2589 case NFA_SKIP_CHAR:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002590 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002591 break;
2592
2593#if 0
2594 case NFA_END_NEG_RANGE:
2595 /* Nothing to handle here. nfa_regmatch() will take care of it */
2596 break;
2597
2598 case NFA_NOT:
2599 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2600#ifdef ENABLE_LOG
2601 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2602#endif
2603 break;
2604
2605 case NFA_COMPOSING:
2606 /* nfa_regmatch() will match all the bytes of this composing char. */
2607 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608#endif
2609
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610 case NFA_NOPEN:
2611 case NFA_NCLOSE:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002612 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002613 break;
2614
2615 /* If this state is reached, then a recursive call of nfa_regmatch()
2616 * succeeded. the next call saves the found submatches in the
2617 * first state after the "invisible" branch. */
2618#if 0
2619 case NFA_END_INVISIBLE:
2620 break;
2621#endif
2622
2623 case NFA_MOPEN + 0:
2624 case NFA_MOPEN + 1:
2625 case NFA_MOPEN + 2:
2626 case NFA_MOPEN + 3:
2627 case NFA_MOPEN + 4:
2628 case NFA_MOPEN + 5:
2629 case NFA_MOPEN + 6:
2630 case NFA_MOPEN + 7:
2631 case NFA_MOPEN + 8:
2632 case NFA_MOPEN + 9:
2633 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002634 if (state->c == NFA_ZSTART)
2635 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002636 else
2637 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638
2639 if (REG_MULTI)
2640 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002641 save_lpos = m->multilist[subidx].start;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002642 if (off == -1)
2643 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002644 m->multilist[subidx].start.lnum = reglnum + 1;
2645 m->multilist[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002646 }
2647 else
2648 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002649 m->multilist[subidx].start.lnum = reglnum;
2650 m->multilist[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002651 (colnr_T)(reginput - regline + off);
2652 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002653 }
2654 else
2655 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002656 save_ptr = m->linelist[subidx].start;
2657 m->linelist[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 }
2659
Bram Moolenaar963fee22013-05-26 21:47:28 +02002660 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002661
2662 if (REG_MULTI)
Bram Moolenaar963fee22013-05-26 21:47:28 +02002663 m->multilist[subidx].start = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002664 else
Bram Moolenaar963fee22013-05-26 21:47:28 +02002665 m->linelist[subidx].start = save_ptr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002666 break;
2667
2668 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002669 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002671 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672 break;
2673 }
2674 case NFA_MCLOSE + 1:
2675 case NFA_MCLOSE + 2:
2676 case NFA_MCLOSE + 3:
2677 case NFA_MCLOSE + 4:
2678 case NFA_MCLOSE + 5:
2679 case NFA_MCLOSE + 6:
2680 case NFA_MCLOSE + 7:
2681 case NFA_MCLOSE + 8:
2682 case NFA_MCLOSE + 9:
2683 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002684 if (state->c == NFA_ZEND)
2685 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002686 else
2687 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002688
2689 if (REG_MULTI)
2690 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002691 save_lpos = m->multilist[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002692 if (off == -1)
2693 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002694 m->multilist[subidx].end.lnum = reglnum + 1;
2695 m->multilist[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002696 }
2697 else
2698 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002699 m->multilist[subidx].end.lnum = reglnum;
2700 m->multilist[subidx].end.col =
2701 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002702 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002703 }
2704 else
2705 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002706 save_ptr = m->linelist[subidx].end;
2707 m->linelist[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002708 }
2709
Bram Moolenaar963fee22013-05-26 21:47:28 +02002710 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002711
2712 if (REG_MULTI)
Bram Moolenaar963fee22013-05-26 21:47:28 +02002713 m->multilist[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002714 else
Bram Moolenaar963fee22013-05-26 21:47:28 +02002715 m->linelist[subidx].end = save_ptr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002716 break;
2717 }
2718}
2719
2720/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002721 * Like addstate(), but the new state(s) are put at position "*ip".
2722 * Used for zero-width matches, next state to use is the added one.
2723 * This makes sure the order of states to be tried does not change, which
2724 * matters for alternatives.
2725 */
2726 static void
Bram Moolenaar963fee22013-05-26 21:47:28 +02002727addstate_here(l, state, m, lid, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002728 nfa_list_T *l; /* runtime state list */
2729 nfa_state_T *state; /* state to update */
2730 regsub_T *m; /* pointers to subexpressions */
2731 int lid;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002732 int *ip;
2733{
2734 int tlen = l->n;
2735 int count;
2736 int i = *ip;
2737
2738 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar963fee22013-05-26 21:47:28 +02002739 addstate(l, state, m, 0, lid);
Bram Moolenaar4b417062013-05-25 20:19:50 +02002740
2741 /* when "*ip" was at the end of the list, nothing to do */
2742 if (i + 1 == tlen)
2743 return;
2744
2745 /* re-order to put the new state at the current position */
2746 count = l->n - tlen;
2747 if (count > 1)
2748 {
2749 /* make space for new states, then move them from the
2750 * end to the current position */
2751 mch_memmove(&(l->t[i + count]),
2752 &(l->t[i + 1]),
2753 sizeof(nfa_thread_T) * (l->n - i - 1));
2754 mch_memmove(&(l->t[i]),
2755 &(l->t[l->n - 1]),
2756 sizeof(nfa_thread_T) * count);
2757 }
2758 else
2759 {
2760 /* overwrite the current state */
2761 l->t[i] = l->t[l->n - 1];
2762 }
2763 --l->n;
2764 *ip = i - 1;
2765}
2766
2767/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002768 * Check character class "class" against current character c.
2769 */
2770 static int
2771check_char_class(class, c)
2772 int class;
2773 int c;
2774{
2775 switch (class)
2776 {
2777 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002778 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002779 return OK;
2780 break;
2781 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002782 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002783 return OK;
2784 break;
2785 case NFA_CLASS_BLANK:
2786 if (c == ' ' || c == '\t')
2787 return OK;
2788 break;
2789 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002790 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791 return OK;
2792 break;
2793 case NFA_CLASS_DIGIT:
2794 if (VIM_ISDIGIT(c))
2795 return OK;
2796 break;
2797 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002798 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002799 return OK;
2800 break;
2801 case NFA_CLASS_LOWER:
2802 if (MB_ISLOWER(c))
2803 return OK;
2804 break;
2805 case NFA_CLASS_PRINT:
2806 if (vim_isprintc(c))
2807 return OK;
2808 break;
2809 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002810 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811 return OK;
2812 break;
2813 case NFA_CLASS_SPACE:
2814 if ((c >=9 && c <= 13) || (c == ' '))
2815 return OK;
2816 break;
2817 case NFA_CLASS_UPPER:
2818 if (MB_ISUPPER(c))
2819 return OK;
2820 break;
2821 case NFA_CLASS_XDIGIT:
2822 if (vim_isxdigit(c))
2823 return OK;
2824 break;
2825 case NFA_CLASS_TAB:
2826 if (c == '\t')
2827 return OK;
2828 break;
2829 case NFA_CLASS_RETURN:
2830 if (c == '\r')
2831 return OK;
2832 break;
2833 case NFA_CLASS_BACKSPACE:
2834 if (c == '\b')
2835 return OK;
2836 break;
2837 case NFA_CLASS_ESCAPE:
2838 if (c == '\033')
2839 return OK;
2840 break;
2841
2842 default:
2843 /* should not be here :P */
2844 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2845 }
2846 return FAIL;
2847}
2848
2849/*
2850 * Set all NFA nodes' list ID equal to -1.
2851 */
2852 static void
2853nfa_set_neg_listids(start)
2854 nfa_state_T *start;
2855{
2856 if (start == NULL)
2857 return;
2858 if (start->lastlist >= 0)
2859 {
2860 start->lastlist = -1;
2861 nfa_set_neg_listids(start->out);
2862 nfa_set_neg_listids(start->out1);
2863 }
2864}
2865
2866/*
2867 * Set all NFA nodes' list ID equal to 0.
2868 */
2869 static void
2870nfa_set_null_listids(start)
2871 nfa_state_T *start;
2872{
2873 if (start == NULL)
2874 return;
2875 if (start->lastlist == -1)
2876 {
2877 start->lastlist = 0;
2878 nfa_set_null_listids(start->out);
2879 nfa_set_null_listids(start->out1);
2880 }
2881}
2882
2883/*
2884 * Save list IDs for all NFA states in "list".
2885 */
2886 static void
2887nfa_save_listids(start, list)
2888 nfa_state_T *start;
2889 int *list;
2890{
2891 if (start == NULL)
2892 return;
2893 if (start->lastlist != -1)
2894 {
2895 list[abs(start->id)] = start->lastlist;
2896 start->lastlist = -1;
2897 nfa_save_listids(start->out, list);
2898 nfa_save_listids(start->out1, list);
2899 }
2900}
2901
2902/*
2903 * Restore list IDs from "list" to all NFA states.
2904 */
2905 static void
2906nfa_restore_listids(start, list)
2907 nfa_state_T *start;
2908 int *list;
2909{
2910 if (start == NULL)
2911 return;
2912 if (start->lastlist == -1)
2913 {
2914 start->lastlist = list[abs(start->id)];
2915 nfa_restore_listids(start->out, list);
2916 nfa_restore_listids(start->out1, list);
2917 }
2918}
2919
2920/*
2921 * Main matching routine.
2922 *
2923 * Run NFA to determine whether it matches reginput.
2924 *
2925 * Return TRUE if there is a match, FALSE otherwise.
2926 * Note: Caller must ensure that: start != NULL.
2927 */
2928 static int
2929nfa_regmatch(start, submatch, m)
2930 nfa_state_T *start;
2931 regsub_T *submatch;
2932 regsub_T *m;
2933{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 int result;
2935 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936 int flag = 0;
2937 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002938 int go_to_nextline = FALSE;
2939 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 char_u *old_reginput = NULL;
2941 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002942 nfa_list_T list[3];
2943 nfa_list_T *listtbl[2][2];
2944 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02002946 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002947 nfa_list_T *thislist;
2948 nfa_list_T *nextlist;
2949 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950 int *listids = NULL;
2951 int j = 0;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02002952#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002953 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002954
2955 if (debug == NULL)
2956 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002957 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002958 return FALSE;
2959 }
2960#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02002961 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002962
2963 /* Allocate memory for the lists of nodes */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002964 size = (nstate + 1) * sizeof(nfa_thread_T);
2965 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
2966 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
2967 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002968 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
2969 goto theend;
2970 vim_memset(list[0].t, 0, size);
2971 vim_memset(list[1].t, 0, size);
2972 vim_memset(list[2].t, 0, size);
2973
2974#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002975 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 if (log_fd != NULL)
2977 {
2978 fprintf(log_fd, "**********************************\n");
2979 nfa_set_code(start->c);
2980 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
2981 abs(start->id), code);
2982 fprintf(log_fd, "**********************************\n");
2983 }
2984 else
2985 {
2986 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
2987 log_fd = stderr;
2988 }
2989#endif
2990
2991 thislist = &list[0];
2992 thislist->n = 0;
2993 nextlist = &list[1];
2994 nextlist->n = 0;
2995 neglist = &list[2];
2996 neglist->n = 0;
2997#ifdef ENABLE_LOG
2998 fprintf(log_fd, "(---) STARTSTATE\n");
2999#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003000 addstate(thislist, start, m, 0, listid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003001
3002 /* There are two cases when the NFA advances: 1. input char matches the
3003 * NFA node and 2. input char does not match the NFA node, but the next
3004 * node is NFA_NOT. The following macro calls addstate() according to
3005 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3006 listtbl[0][0] = NULL;
3007 listtbl[0][1] = neglist;
3008 listtbl[1][0] = nextlist;
3009 listtbl[1][1] = NULL;
3010#define ADD_POS_NEG_STATE(node) \
3011 ll = listtbl[result ? 1 : 0][node->negated]; \
3012 if (ll != NULL) \
Bram Moolenaar963fee22013-05-26 21:47:28 +02003013 addstate(ll, node->out , &t->sub, clen, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014
3015
3016 /*
3017 * Run for each character.
3018 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003019 for (;;)
3020 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003021 int curc;
3022 int clen;
3023
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003024#ifdef FEAT_MBYTE
3025 if (has_mbyte)
3026 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003027 curc = (*mb_ptr2char)(reginput);
3028 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003029 }
3030 else
3031#endif
3032 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003033 curc = *reginput;
3034 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003035 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003036 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003037 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003038 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003039 go_to_nextline = FALSE;
3040 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003041
3042 /* swap lists */
3043 thislist = &list[flag];
3044 nextlist = &list[flag ^= 1];
3045 nextlist->n = 0; /* `clear' nextlist */
3046 listtbl[1][0] = nextlist;
3047 ++listid;
3048
3049#ifdef ENABLE_LOG
3050 fprintf(log_fd, "------------------------------------------\n");
3051 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003052 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003053 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003054 {
3055 int i;
3056
3057 for (i = 0; i < thislist->n; i++)
3058 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3059 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003060 fprintf(log_fd, "\n");
3061#endif
3062
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003063#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003064 fprintf(debug, "\n-------------------\n");
3065#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003066 /*
3067 * If the state lists are empty we can stop.
3068 */
3069 if (thislist->n == 0 && neglist->n == 0)
3070 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003071
3072 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003073 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003074 {
3075 if (neglist->n > 0)
3076 {
3077 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003078 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003079 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003080 }
3081 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003082 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003083
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003084#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003085 nfa_set_code(t->state->c);
3086 fprintf(debug, "%s, ", code);
3087#endif
3088#ifdef ENABLE_LOG
3089 nfa_set_code(t->state->c);
3090 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3091 code, (int)t->state->c);
3092#endif
3093
3094 /*
3095 * Handle the possible codes of the current state.
3096 * The most important is NFA_MATCH.
3097 */
3098 switch (t->state->c)
3099 {
3100 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003101 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003102 *submatch = t->sub;
3103#ifdef ENABLE_LOG
3104 for (j = 0; j < 4; j++)
3105 if (REG_MULTI)
3106 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
3107 j,
3108 t->sub.startpos[j].col,
3109 (int)t->sub.startpos[j].lnum,
3110 t->sub.endpos[j].col,
3111 (int)t->sub.endpos[j].lnum);
3112 else
3113 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
3114 j,
3115 (char *)t->sub.start[j],
3116 (char *)t->sub.end[j]);
3117 fprintf(log_fd, "\n");
3118#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003119 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003120 * states at this position. When the list of states is going
3121 * to be empty quit without advancing, so that "reginput" is
3122 * correct. */
3123 if (nextlist->n == 0 && neglist->n == 0)
3124 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003125 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003126
3127 case NFA_END_INVISIBLE:
3128 /* This is only encountered after a NFA_START_INVISIBLE node.
3129 * They surround a zero-width group, used with "\@=" and "\&".
3130 * If we got here, it means that the current "invisible" group
3131 * finished successfully, so return control to the parent
3132 * nfa_regmatch(). Submatches are stored in *m, and used in
3133 * the parent call. */
3134 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003135 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003136 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003137 else
3138 {
3139 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003140 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003141 }
3142 break;
3143
3144 case NFA_START_INVISIBLE:
3145 /* Save global variables, and call nfa_regmatch() to check if
3146 * the current concat matches at this position. The concat
3147 * ends with the node NFA_END_INVISIBLE */
3148 old_reginput = reginput;
3149 old_regline = regline;
3150 old_reglnum = reglnum;
3151 if (listids == NULL)
3152 {
3153 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3154 if (listids == NULL)
3155 {
3156 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3157 return 0;
3158 }
3159 }
3160#ifdef ENABLE_LOG
3161 if (log_fd != stderr)
3162 fclose(log_fd);
3163 log_fd = NULL;
3164#endif
3165 /* Have to clear the listid field of the NFA nodes, so that
3166 * nfa_regmatch() and addstate() can run properly after
3167 * recursion. */
3168 nfa_save_listids(start, listids);
3169 nfa_set_null_listids(start);
3170 result = nfa_regmatch(t->state->out, submatch, m);
3171 nfa_set_neg_listids(start);
3172 nfa_restore_listids(start, listids);
3173
3174#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003175 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003176 if (log_fd != NULL)
3177 {
3178 fprintf(log_fd, "****************************\n");
3179 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3180 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3181 fprintf(log_fd, "****************************\n");
3182 }
3183 else
3184 {
3185 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3186 log_fd = stderr;
3187 }
3188#endif
3189 if (result == TRUE)
3190 {
3191 /* Restore position in input text */
3192 reginput = old_reginput;
3193 regline = old_regline;
3194 reglnum = old_reglnum;
3195 /* Copy submatch info from the recursive call */
3196 if (REG_MULTI)
Bram Moolenaar963fee22013-05-26 21:47:28 +02003197 for (j = 1; j < nfa_nsubexpr; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003198 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003199 t->sub.multilist[j].start = m->multilist[j].start;
3200 t->sub.multilist[j].end = m->multilist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201 }
3202 else
Bram Moolenaar963fee22013-05-26 21:47:28 +02003203 for (j = 1; j < nfa_nsubexpr; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003205 t->sub.linelist[j].start = m->linelist[j].start;
3206 t->sub.linelist[j].end = m->linelist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 }
3208 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003209 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003210 listid, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 }
3212 else
3213 {
3214 /* continue with next input char */
3215 reginput = old_reginput;
3216 }
3217 break;
3218
3219 case NFA_BOL:
3220 if (reginput == regline)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003221 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003222 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223 break;
3224
3225 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003226 if (curc == NUL)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003227 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003228 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003229 break;
3230
3231 case NFA_BOW:
3232 {
3233 int bow = TRUE;
3234
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003235 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236 bow = FALSE;
3237#ifdef FEAT_MBYTE
3238 else if (has_mbyte)
3239 {
3240 int this_class;
3241
3242 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003243 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 if (this_class <= 1)
3245 bow = FALSE;
3246 else if (reg_prev_class() == this_class)
3247 bow = FALSE;
3248 }
3249#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003250 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003251 || (reginput > regline
3252 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253 bow = FALSE;
3254 if (bow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003255 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003256 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003257 break;
3258 }
3259
3260 case NFA_EOW:
3261 {
3262 int eow = TRUE;
3263
3264 if (reginput == regline)
3265 eow = FALSE;
3266#ifdef FEAT_MBYTE
3267 else if (has_mbyte)
3268 {
3269 int this_class, prev_class;
3270
3271 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003272 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 prev_class = reg_prev_class();
3274 if (this_class == prev_class
3275 || prev_class == 0 || prev_class == 1)
3276 eow = FALSE;
3277 }
3278#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003279 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003280 || (reginput[0] != NUL
3281 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003282 eow = FALSE;
3283 if (eow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003284 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003285 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 break;
3287 }
3288
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003289#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003290 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003291 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003292 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003293 int len = 0;
3294 nfa_state_T *end;
3295 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003296 int cchars[MAX_MCO];
3297 int ccount = 0;
3298 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003301 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003302 if (utf_iscomposing(sta->c))
3303 {
3304 /* Only match composing character(s), ignore base
3305 * character. Used for ".{composing}" and "{composing}"
3306 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003307 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003308 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003309 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003311 /* If \Z was present, then ignore composing characters.
3312 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003313 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003314 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003315 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003316 else
3317 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003318 while (sta->c != NFA_END_COMPOSING)
3319 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003321
3322 /* Check base character matches first, unless ignored. */
3323 else if (len > 0 || mc == sta->c)
3324 {
3325 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003326 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003327 len += mb_char2len(mc);
3328 sta = sta->out;
3329 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003331 /* We don't care about the order of composing characters.
3332 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003333 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003334 {
3335 mc = mb_ptr2char(reginput + len);
3336 cchars[ccount++] = mc;
3337 len += mb_char2len(mc);
3338 if (ccount == MAX_MCO)
3339 break;
3340 }
3341
3342 /* Check that each composing char in the pattern matches a
3343 * composing char in the text. We do not check if all
3344 * composing chars are matched. */
3345 result = OK;
3346 while (sta->c != NFA_END_COMPOSING)
3347 {
3348 for (j = 0; j < ccount; ++j)
3349 if (cchars[j] == sta->c)
3350 break;
3351 if (j == ccount)
3352 {
3353 result = FAIL;
3354 break;
3355 }
3356 sta = sta->out;
3357 }
3358 }
3359 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003360 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003361
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003362 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 ADD_POS_NEG_STATE(end);
3364 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003365 }
3366#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367
3368 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003369 if (curc == NUL && !reg_line_lbr && REG_MULTI
3370 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003372 go_to_nextline = TRUE;
3373 /* Pass -1 for the offset, which means taking the position
3374 * at the start of the next line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003375 addstate(nextlist, t->state->out, &t->sub, -1, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003377 else if (curc == '\n' && reg_line_lbr)
3378 {
3379 /* match \n as if it is an ordinary character */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003380 addstate(nextlist, t->state->out, &t->sub, 1, listid + 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003381 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003382 break;
3383
3384 case NFA_CLASS_ALNUM:
3385 case NFA_CLASS_ALPHA:
3386 case NFA_CLASS_BLANK:
3387 case NFA_CLASS_CNTRL:
3388 case NFA_CLASS_DIGIT:
3389 case NFA_CLASS_GRAPH:
3390 case NFA_CLASS_LOWER:
3391 case NFA_CLASS_PRINT:
3392 case NFA_CLASS_PUNCT:
3393 case NFA_CLASS_SPACE:
3394 case NFA_CLASS_UPPER:
3395 case NFA_CLASS_XDIGIT:
3396 case NFA_CLASS_TAB:
3397 case NFA_CLASS_RETURN:
3398 case NFA_CLASS_BACKSPACE:
3399 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003400 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003401 ADD_POS_NEG_STATE(t->state);
3402 break;
3403
3404 case NFA_END_NEG_RANGE:
3405 /* This follows a series of negated nodes, like:
3406 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003407 if (curc > 0)
3408 addstate(nextlist, t->state->out, &t->sub, clen,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003409 listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410 break;
3411
3412 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003413 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003414 if (curc > 0)
3415 addstate(nextlist, t->state->out, &t->sub, clen,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003416 listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 break;
3418
3419 /*
3420 * Character classes like \a for alpha, \d for digit etc.
3421 */
3422 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003423 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 ADD_POS_NEG_STATE(t->state);
3425 break;
3426
3427 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003428 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 ADD_POS_NEG_STATE(t->state);
3430 break;
3431
3432 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003433 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434 ADD_POS_NEG_STATE(t->state);
3435 break;
3436
3437 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003438 result = !VIM_ISDIGIT(curc)
3439 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003440 ADD_POS_NEG_STATE(t->state);
3441 break;
3442
3443 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003444 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445 ADD_POS_NEG_STATE(t->state);
3446 break;
3447
3448 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003449 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003450 ADD_POS_NEG_STATE(t->state);
3451 break;
3452
3453 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003454 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 ADD_POS_NEG_STATE(t->state);
3456 break;
3457
3458 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003459 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 ADD_POS_NEG_STATE(t->state);
3461 break;
3462
3463 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003464 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 ADD_POS_NEG_STATE(t->state);
3466 break;
3467
3468 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003469 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003470 ADD_POS_NEG_STATE(t->state);
3471 break;
3472
3473 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003474 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 ADD_POS_NEG_STATE(t->state);
3476 break;
3477
3478 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003479 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003480 ADD_POS_NEG_STATE(t->state);
3481 break;
3482
3483 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003484 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 ADD_POS_NEG_STATE(t->state);
3486 break;
3487
3488 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003489 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003490 ADD_POS_NEG_STATE(t->state);
3491 break;
3492
3493 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003494 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003495 ADD_POS_NEG_STATE(t->state);
3496 break;
3497
3498 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003499 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003500 ADD_POS_NEG_STATE(t->state);
3501 break;
3502
3503 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003504 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505 ADD_POS_NEG_STATE(t->state);
3506 break;
3507
3508 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003509 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 ADD_POS_NEG_STATE(t->state);
3511 break;
3512
3513 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003514 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003515 ADD_POS_NEG_STATE(t->state);
3516 break;
3517
3518 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003519 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 ADD_POS_NEG_STATE(t->state);
3521 break;
3522
3523 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003524 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003525 ADD_POS_NEG_STATE(t->state);
3526 break;
3527
3528 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003529 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530 ADD_POS_NEG_STATE(t->state);
3531 break;
3532
3533 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003534 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 ADD_POS_NEG_STATE(t->state);
3536 break;
3537
3538 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003539 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 ADD_POS_NEG_STATE(t->state);
3541 break;
3542
3543 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003544 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003545 ADD_POS_NEG_STATE(t->state);
3546 break;
3547
3548 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003549 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003550 ADD_POS_NEG_STATE(t->state);
3551 break;
3552
Bram Moolenaar12e40142013-05-21 15:33:41 +02003553 case NFA_MOPEN + 0:
3554 case NFA_MOPEN + 1:
3555 case NFA_MOPEN + 2:
3556 case NFA_MOPEN + 3:
3557 case NFA_MOPEN + 4:
3558 case NFA_MOPEN + 5:
3559 case NFA_MOPEN + 6:
3560 case NFA_MOPEN + 7:
3561 case NFA_MOPEN + 8:
3562 case NFA_MOPEN + 9:
3563 /* handled below */
3564 break;
3565
3566 case NFA_SKIP_CHAR:
3567 case NFA_ZSTART:
3568 /* TODO: should not happen? */
3569 break;
3570
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003572 {
3573 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003574
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003575 /* TODO: put this in #ifdef later */
3576 if (c < -256)
3577 EMSGN("INTERNAL: Negative state char: %ld", c);
3578 if (is_Magic(c))
3579 c = un_Magic(c);
3580 result = (c == curc);
3581
3582 if (!result && ireg_ic)
3583 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003584#ifdef FEAT_MBYTE
3585 /* If there is a composing character which is not being
3586 * ignored there can be no match. Match with composing
3587 * character uses NFA_COMPOSING above. */
3588 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003589 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003590 result = FALSE;
3591#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 ADD_POS_NEG_STATE(t->state);
3593 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003594 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 }
3596
3597 } /* for (thislist = thislist; thislist->state; thislist++) */
3598
Bram Moolenaare23febd2013-05-26 18:40:14 +02003599 /* Look for the start of a match in the current position by adding the
3600 * start state to the list of states.
3601 * The first found match is the leftmost one, thus the order of states
3602 * matters!
3603 * Do not add the start state in recursive calls of nfa_regmatch(),
3604 * because recursive calls should only start in the first position.
3605 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003606 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaare23febd2013-05-26 18:40:14 +02003607 && reglnum == 0 && clen != 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 {
3609#ifdef ENABLE_LOG
3610 fprintf(log_fd, "(---) STARTSTATE\n");
3611#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003612 addstate(nextlist, start, m, clen, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 }
3614
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615#ifdef ENABLE_LOG
3616 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003617 {
3618 int i;
3619
3620 for (i = 0; i < thislist->n; i++)
3621 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3622 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 fprintf(log_fd, "\n");
3624#endif
3625
3626nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003627 /* Advance to the next character, or advance to the next line, or
3628 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003629 if (clen != 0)
3630 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003631 else if (go_to_nextline)
3632 reg_nextline();
3633 else
3634 break;
3635 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636
3637#ifdef ENABLE_LOG
3638 if (log_fd != stderr)
3639 fclose(log_fd);
3640 log_fd = NULL;
3641#endif
3642
3643theend:
3644 /* Free memory */
3645 vim_free(list[0].t);
3646 vim_free(list[1].t);
3647 vim_free(list[2].t);
3648 list[0].t = list[1].t = list[2].t = NULL;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003649 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003650#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003651#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003652 fclose(debug);
3653#endif
3654
Bram Moolenaar963fee22013-05-26 21:47:28 +02003655 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003656}
3657
3658/*
3659 * Try match of "prog" with at regline["col"].
3660 * Returns 0 for failure, number of lines contained in the match otherwise.
3661 */
3662 static long
3663nfa_regtry(start, col)
3664 nfa_state_T *start;
3665 colnr_T col;
3666{
3667 int i;
3668 regsub_T sub, m;
3669#ifdef ENABLE_LOG
3670 FILE *f;
3671#endif
3672
3673 reginput = regline + col;
3674 need_clear_subexpr = TRUE;
3675
3676#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003677 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 if (f != NULL)
3679 {
3680 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3681 fprintf(f, " =======================================================\n");
3682#ifdef DEBUG
3683 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3684#endif
3685 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3686 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003687 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 fprintf(f, "\n\n");
3689 fclose(f);
3690 }
3691 else
3692 EMSG(_("Could not open temporary log file for writing "));
3693#endif
3694
3695 if (REG_MULTI)
3696 {
3697 /* Use 0xff to set lnum to -1 */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003698 vim_memset(sub.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
3699 vim_memset(m.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 }
3701 else
3702 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003703 vim_memset(sub.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
3704 vim_memset(m.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 }
3706
3707 if (nfa_regmatch(start, &sub, &m) == FALSE)
3708 return 0;
3709
3710 cleanup_subexpr();
3711 if (REG_MULTI)
3712 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003713 for (i = 0; i < nfa_nsubexpr; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003715 reg_startpos[i] = sub.multilist[i].start;
3716 reg_endpos[i] = sub.multilist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 }
3718
3719 if (reg_startpos[0].lnum < 0)
3720 {
3721 reg_startpos[0].lnum = 0;
3722 reg_startpos[0].col = col;
3723 }
3724 if (reg_endpos[0].lnum < 0)
3725 {
3726 reg_endpos[0].lnum = reglnum;
3727 reg_endpos[0].col = (int)(reginput - regline);
3728 }
3729 else
3730 /* Use line number of "\ze". */
3731 reglnum = reg_endpos[0].lnum;
3732 }
3733 else
3734 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003735 for (i = 0; i < nfa_nsubexpr; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003737 reg_startp[i] = sub.linelist[i].start;
3738 reg_endp[i] = sub.linelist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 }
3740
3741 if (reg_startp[0] == NULL)
3742 reg_startp[0] = regline + col;
3743 if (reg_endp[0] == NULL)
3744 reg_endp[0] = reginput;
3745 }
3746
3747 return 1 + reglnum;
3748}
3749
3750/*
3751 * Match a regexp against a string ("line" points to the string) or multiple
3752 * lines ("line" is NULL, use reg_getline()).
3753 *
3754 * Returns 0 for failure, number of lines contained in the match otherwise.
3755 */
3756 static long
3757nfa_regexec_both(line, col)
3758 char_u *line;
3759 colnr_T col; /* column to start looking for match */
3760{
3761 nfa_regprog_T *prog;
3762 long retval = 0L;
3763 int i;
3764
3765 if (REG_MULTI)
3766 {
3767 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3768 line = reg_getline((linenr_T)0); /* relative to the cursor */
3769 reg_startpos = reg_mmatch->startpos;
3770 reg_endpos = reg_mmatch->endpos;
3771 }
3772 else
3773 {
3774 prog = (nfa_regprog_T *)reg_match->regprog;
3775 reg_startp = reg_match->startp;
3776 reg_endp = reg_match->endp;
3777 }
3778
3779 /* Be paranoid... */
3780 if (prog == NULL || line == NULL)
3781 {
3782 EMSG(_(e_null));
3783 goto theend;
3784 }
3785
3786 /* If the start column is past the maximum column: no need to try. */
3787 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3788 goto theend;
3789
3790 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3791 if (prog->regflags & RF_ICASE)
3792 ireg_ic = TRUE;
3793 else if (prog->regflags & RF_NOICASE)
3794 ireg_ic = FALSE;
3795
3796#ifdef FEAT_MBYTE
3797 /* If pattern contains "\Z" overrule value of ireg_icombine */
3798 if (prog->regflags & RF_ICOMBINE)
3799 ireg_icombine = TRUE;
3800#endif
3801
3802 regline = line;
3803 reglnum = 0; /* relative to line */
3804
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003805 nfa_has_zend = prog->has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003806 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003808 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003809 for (i = 0; i < nstate; ++i)
3810 {
3811 prog->state[i].id = i;
3812 prog->state[i].lastlist = 0;
3813 prog->state[i].visits = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003814 }
3815
3816 retval = nfa_regtry(prog->start, col);
3817
3818theend:
3819 return retval;
3820}
3821
3822/*
3823 * Compile a regular expression into internal code for the NFA matcher.
3824 * Returns the program in allocated space. Returns NULL for an error.
3825 */
3826 static regprog_T *
3827nfa_regcomp(expr, re_flags)
3828 char_u *expr;
3829 int re_flags;
3830{
Bram Moolenaaraae48832013-05-25 21:18:34 +02003831 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003832 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003833 int *postfix;
3834
3835 if (expr == NULL)
3836 return NULL;
3837
3838#ifdef DEBUG
3839 nfa_regengine.expr = expr;
3840#endif
3841
3842 init_class_tab();
3843
3844 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3845 return NULL;
3846
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003847 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02003848 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 postfix = re2post();
3850 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003851 {
3852 /* TODO: only give this error for debugging? */
3853 if (post_ptr >= post_end)
3854 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003855 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003856 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003857
3858 /*
3859 * In order to build the NFA, we parse the input regexp twice:
3860 * 1. first pass to count size (so we can allocate space)
3861 * 2. second to emit code
3862 */
3863#ifdef ENABLE_LOG
3864 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003865 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003866
3867 if (f != NULL)
3868 {
3869 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3870 fclose(f);
3871 }
3872 }
3873#endif
3874
3875 /*
3876 * PASS 1
3877 * Count number of NFA states in "nstate". Do not build the NFA.
3878 */
3879 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02003880
3881 /* Space for compiled regexp */
3882 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
3883 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3884 if (prog == NULL)
3885 goto fail;
3886 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003887 state_ptr = prog->state;
3888
3889 /*
3890 * PASS 2
3891 * Build the NFA
3892 */
3893 prog->start = post2nfa(postfix, post_ptr, FALSE);
3894 if (prog->start == NULL)
3895 goto fail;
3896
3897 prog->regflags = regflags;
3898 prog->engine = &nfa_regengine;
3899 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003900 prog->has_zend = nfa_has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003901 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003902#ifdef ENABLE_LOG
3903 nfa_postfix_dump(expr, OK);
3904 nfa_dump(prog);
3905#endif
3906
3907out:
3908 vim_free(post_start);
3909 post_start = post_ptr = post_end = NULL;
3910 state_ptr = NULL;
3911 return (regprog_T *)prog;
3912
3913fail:
3914 vim_free(prog);
3915 prog = NULL;
3916#ifdef ENABLE_LOG
3917 nfa_postfix_dump(expr, FAIL);
3918#endif
3919#ifdef DEBUG
3920 nfa_regengine.expr = NULL;
3921#endif
3922 goto out;
3923}
3924
3925
3926/*
3927 * Match a regexp against a string.
3928 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
3929 * Uses curbuf for line count and 'iskeyword'.
3930 *
3931 * Return TRUE if there is a match, FALSE if not.
3932 */
3933 static int
3934nfa_regexec(rmp, line, col)
3935 regmatch_T *rmp;
3936 char_u *line; /* string to match against */
3937 colnr_T col; /* column to start looking for match */
3938{
3939 reg_match = rmp;
3940 reg_mmatch = NULL;
3941 reg_maxline = 0;
3942 reg_line_lbr = FALSE;
3943 reg_buf = curbuf;
3944 reg_win = NULL;
3945 ireg_ic = rmp->rm_ic;
3946#ifdef FEAT_MBYTE
3947 ireg_icombine = FALSE;
3948#endif
3949 ireg_maxcol = 0;
3950 return (nfa_regexec_both(line, col) != 0);
3951}
3952
3953#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3954 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
3955
3956static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3957
3958/*
3959 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
3960 */
3961 static int
3962nfa_regexec_nl(rmp, line, col)
3963 regmatch_T *rmp;
3964 char_u *line; /* string to match against */
3965 colnr_T col; /* column to start looking for match */
3966{
3967 reg_match = rmp;
3968 reg_mmatch = NULL;
3969 reg_maxline = 0;
3970 reg_line_lbr = TRUE;
3971 reg_buf = curbuf;
3972 reg_win = NULL;
3973 ireg_ic = rmp->rm_ic;
3974#ifdef FEAT_MBYTE
3975 ireg_icombine = FALSE;
3976#endif
3977 ireg_maxcol = 0;
3978 return (nfa_regexec_both(line, col) != 0);
3979}
3980#endif
3981
3982
3983/*
3984 * Match a regexp against multiple lines.
3985 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3986 * Uses curbuf for line count and 'iskeyword'.
3987 *
3988 * Return zero if there is no match. Return number of lines contained in the
3989 * match otherwise.
3990 *
3991 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
3992 *
3993 * ! Also NOTE : match may actually be in another line. e.g.:
3994 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
3995 *
3996 * +-------------------------+
3997 * |a |
3998 * |b |
3999 * |c |
4000 * | |
4001 * +-------------------------+
4002 *
4003 * then nfa_regexec_multi() returns 3. while the original
4004 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4005 *
4006 * FIXME if this behavior is not compatible.
4007 */
4008 static long
4009nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4010 regmmatch_T *rmp;
4011 win_T *win; /* window in which to search or NULL */
4012 buf_T *buf; /* buffer in which to search */
4013 linenr_T lnum; /* nr of line to start looking for match */
4014 colnr_T col; /* column to start looking for match */
4015 proftime_T *tm UNUSED; /* timeout limit or NULL */
4016{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004017 reg_match = NULL;
4018 reg_mmatch = rmp;
4019 reg_buf = buf;
4020 reg_win = win;
4021 reg_firstlnum = lnum;
4022 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4023 reg_line_lbr = FALSE;
4024 ireg_ic = rmp->rmm_ic;
4025#ifdef FEAT_MBYTE
4026 ireg_icombine = FALSE;
4027#endif
4028 ireg_maxcol = rmp->rmm_maxcol;
4029
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004030 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004031}
4032
4033#ifdef DEBUG
4034# undef ENABLE_LOG
4035#endif