blob: 471cdd0cdfc48a84b99acb59d6ea237196fd9eb5 [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. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200162static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200163
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;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200794 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200795 case '1':
796 case '2':
797 case '3':
798 case '4':
799 case '5':
800 case '6':
801 case '7':
802 case '8':
803 case '9':
804 case '(':
805 /* \z1...\z9 and \z( not yet supported */
806 return FAIL;
807 default:
808 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200809 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200810 no_Magic(c));
811 return FAIL;
812 }
813 break;
814
815 case Magic('%'):
816 c = no_Magic(getchr());
817 switch (c)
818 {
819 /* () without a back reference */
820 case '(':
821 if (nfa_reg(REG_NPAREN) == FAIL)
822 return FAIL;
823 EMIT(NFA_NOPEN);
824 break;
825
826 case 'd': /* %d123 decimal */
827 case 'o': /* %o123 octal */
828 case 'x': /* %xab hex 2 */
829 case 'u': /* %uabcd hex 4 */
830 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200831 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200832 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200833
Bram Moolenaar47196582013-05-25 22:04:23 +0200834 switch (c)
835 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200836 case 'd': nr = getdecchrs(); break;
837 case 'o': nr = getoctchrs(); break;
838 case 'x': nr = gethexchrs(2); break;
839 case 'u': nr = gethexchrs(4); break;
840 case 'U': nr = gethexchrs(8); break;
841 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200842 }
843
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200844 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200845 EMSG2_RET_FAIL(
846 _("E678: Invalid character after %s%%[dxouU]"),
847 reg_magic == MAGIC_ALL);
848 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200849 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200850 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200851 break;
852
853 /* Catch \%^ and \%$ regardless of where they appear in the
854 * pattern -- regardless of whether or not it makes sense. */
855 case '^':
856 EMIT(NFA_BOF);
857 /* Not yet supported */
858 return FAIL;
859 break;
860
861 case '$':
862 EMIT(NFA_EOF);
863 /* Not yet supported */
864 return FAIL;
865 break;
866
867 case '#':
868 /* not supported yet */
869 return FAIL;
870 break;
871
872 case 'V':
873 /* not supported yet */
874 return FAIL;
875 break;
876
877 case '[':
878 /* \%[abc] not supported yet */
879 return FAIL;
880
881 default:
882 /* not supported yet */
883 return FAIL;
884 }
885 break;
886
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200888collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200889 /*
890 * Glue is emitted between several atoms from the [].
891 * It is either NFA_OR, or NFA_CONCAT.
892 *
893 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
894 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
895 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
896 * notation)
897 *
898 */
899
900
901/* Emit negation atoms, if needed.
902 * The CONCAT below merges the NOT with the previous node. */
903#define TRY_NEG() \
904 if (negated == TRUE) \
905 { \
906 EMIT(NFA_NOT); \
907 }
908
909/* Emit glue between important nodes : CONCAT or OR. */
910#define EMIT_GLUE() \
911 if (first == FALSE) \
912 EMIT(glue); \
913 else \
914 first = FALSE;
915
916 p = regparse;
917 endp = skip_anyof(p);
918 if (*endp == ']')
919 {
920 /*
921 * Try to reverse engineer character classes. For example,
922 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
923 * and perform the necessary substitutions in the NFA.
924 */
925 result = nfa_recognize_char_class(regparse, endp,
926 extra == ADD_NL);
927 if (result != FAIL)
928 {
929 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
930 EMIT(result);
931 else /* must be char class + newline */
932 {
933 EMIT(result - ADD_NL);
934 EMIT(NFA_NEWL);
935 EMIT(NFA_OR);
936 }
937 regparse = endp;
938 nfa_inc(&regparse);
939 return OK;
940 }
941 /*
942 * Failed to recognize a character class. Use the simple
943 * version that turns [abc] into 'a' OR 'b' OR 'c'
944 */
945 startc = endc = oldstartc = -1;
946 first = TRUE; /* Emitting first atom in this sequence? */
947 negated = FALSE;
948 glue = NFA_OR;
949 if (*regparse == '^') /* negated range */
950 {
951 negated = TRUE;
952 glue = NFA_CONCAT;
953 nfa_inc(&regparse);
954 }
955 if (*regparse == '-')
956 {
957 startc = '-';
958 EMIT(startc);
959 TRY_NEG();
960 EMIT_GLUE();
961 nfa_inc(&regparse);
962 }
963 /* Emit the OR branches for each character in the [] */
964 emit_range = FALSE;
965 while (regparse < endp)
966 {
967 oldstartc = startc;
968 startc = -1;
969 got_coll_char = FALSE;
970 if (*regparse == '[')
971 {
972 /* Check for [: :], [= =], [. .] */
973 equiclass = collclass = 0;
974 charclass = get_char_class(&regparse);
975 if (charclass == CLASS_NONE)
976 {
977 equiclass = get_equi_class(&regparse);
978 if (equiclass == 0)
979 collclass = get_coll_element(&regparse);
980 }
981
982 /* Character class like [:alpha:] */
983 if (charclass != CLASS_NONE)
984 {
985 switch (charclass)
986 {
987 case CLASS_ALNUM:
988 EMIT(NFA_CLASS_ALNUM);
989 break;
990 case CLASS_ALPHA:
991 EMIT(NFA_CLASS_ALPHA);
992 break;
993 case CLASS_BLANK:
994 EMIT(NFA_CLASS_BLANK);
995 break;
996 case CLASS_CNTRL:
997 EMIT(NFA_CLASS_CNTRL);
998 break;
999 case CLASS_DIGIT:
1000 EMIT(NFA_CLASS_DIGIT);
1001 break;
1002 case CLASS_GRAPH:
1003 EMIT(NFA_CLASS_GRAPH);
1004 break;
1005 case CLASS_LOWER:
1006 EMIT(NFA_CLASS_LOWER);
1007 break;
1008 case CLASS_PRINT:
1009 EMIT(NFA_CLASS_PRINT);
1010 break;
1011 case CLASS_PUNCT:
1012 EMIT(NFA_CLASS_PUNCT);
1013 break;
1014 case CLASS_SPACE:
1015 EMIT(NFA_CLASS_SPACE);
1016 break;
1017 case CLASS_UPPER:
1018 EMIT(NFA_CLASS_UPPER);
1019 break;
1020 case CLASS_XDIGIT:
1021 EMIT(NFA_CLASS_XDIGIT);
1022 break;
1023 case CLASS_TAB:
1024 EMIT(NFA_CLASS_TAB);
1025 break;
1026 case CLASS_RETURN:
1027 EMIT(NFA_CLASS_RETURN);
1028 break;
1029 case CLASS_BACKSPACE:
1030 EMIT(NFA_CLASS_BACKSPACE);
1031 break;
1032 case CLASS_ESCAPE:
1033 EMIT(NFA_CLASS_ESCAPE);
1034 break;
1035 }
1036 TRY_NEG();
1037 EMIT_GLUE();
1038 continue;
1039 }
1040 /* Try equivalence class [=a=] and the like */
1041 if (equiclass != 0)
1042 {
1043 result = nfa_emit_equi_class(equiclass, negated);
1044 if (result == FAIL)
1045 {
1046 /* should never happen */
1047 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1048 }
1049 EMIT_GLUE();
1050 continue;
1051 }
1052 /* Try collating class like [. .] */
1053 if (collclass != 0)
1054 {
1055 startc = collclass; /* allow [.a.]-x as a range */
1056 /* Will emit the proper atom at the end of the
1057 * while loop. */
1058 }
1059 }
1060 /* Try a range like 'a-x' or '\t-z' */
1061 if (*regparse == '-')
1062 {
1063 emit_range = TRUE;
1064 startc = oldstartc;
1065 nfa_inc(&regparse);
1066 continue; /* reading the end of the range */
1067 }
1068
1069 /* Now handle simple and escaped characters.
1070 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1071 * accepts "\t", "\e", etc., but only when the 'l' flag in
1072 * 'cpoptions' is not included.
1073 * Posix doesn't recognize backslash at all.
1074 */
1075 if (*regparse == '\\'
1076 && !cpo_bsl
1077 && regparse + 1 <= endp
1078 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1079 || (!cpo_lit
1080 && vim_strchr(REGEXP_ABBR, regparse[1])
1081 != NULL)
1082 )
1083 )
1084 {
1085 nfa_inc(&regparse);
1086
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001087 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001088 startc = reg_string ? NL : NFA_NEWL;
1089 else
1090 if (*regparse == 'd'
1091 || *regparse == 'o'
1092 || *regparse == 'x'
1093 || *regparse == 'u'
1094 || *regparse == 'U'
1095 )
1096 {
1097 /* TODO(RE) This needs more testing */
1098 startc = coll_get_char();
1099 got_coll_char = TRUE;
1100 nfa_dec(&regparse);
1101 }
1102 else
1103 {
1104 /* \r,\t,\e,\b */
1105 startc = backslash_trans(*regparse);
1106 }
1107 }
1108
1109 /* Normal printable char */
1110 if (startc == -1)
1111#ifdef FEAT_MBYTE
1112 startc = (*mb_ptr2char)(regparse);
1113#else
1114 startc = *regparse;
1115#endif
1116
1117 /* Previous char was '-', so this char is end of range. */
1118 if (emit_range)
1119 {
1120 endc = startc; startc = oldstartc;
1121 if (startc > endc)
1122 EMSG_RET_FAIL(_(e_invrange));
1123#ifdef FEAT_MBYTE
1124 if (has_mbyte && ((*mb_char2len)(startc) > 1
1125 || (*mb_char2len)(endc) > 1))
1126 {
1127 if (endc > startc + 256)
1128 EMSG_RET_FAIL(_(e_invrange));
1129 /* Emit the range. "startc" was already emitted, so
1130 * skip it. */
1131 for (c = startc + 1; c <= endc; c++)
1132 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001133 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001134 TRY_NEG();
1135 EMIT_GLUE();
1136 }
1137 emit_range = FALSE;
1138 }
1139 else
1140#endif
1141 {
1142#ifdef EBCDIC
1143 int alpha_only = FALSE;
1144
1145 /* for alphabetical range skip the gaps
1146 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1147 if (isalpha(startc) && isalpha(endc))
1148 alpha_only = TRUE;
1149#endif
1150 /* Emit the range. "startc" was already emitted, so
1151 * skip it. */
1152 for (c = startc + 1; c <= endc; c++)
1153#ifdef EBCDIC
1154 if (!alpha_only || isalpha(startc))
1155#endif
1156 {
1157 EMIT(c);
1158 TRY_NEG();
1159 EMIT_GLUE();
1160 }
1161 emit_range = FALSE;
1162 }
1163 }
1164 else
1165 {
1166 /*
1167 * This char (startc) is not part of a range. Just
1168 * emit it.
1169 *
1170 * Normally, simply emit startc. But if we get char
1171 * code=0 from a collating char, then replace it with
1172 * 0x0a.
1173 *
1174 * This is needed to completely mimic the behaviour of
1175 * the backtracking engine.
1176 */
1177 if (got_coll_char == TRUE && startc == 0)
1178 EMIT(0x0a);
1179 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001180 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001181 TRY_NEG();
1182 EMIT_GLUE();
1183 }
1184
1185 nfa_inc(&regparse);
1186 } /* while (p < endp) */
1187
1188 nfa_dec(&regparse);
1189 if (*regparse == '-') /* if last, '-' is just a char */
1190 {
1191 EMIT('-');
1192 TRY_NEG();
1193 EMIT_GLUE();
1194 }
1195 nfa_inc(&regparse);
1196
1197 if (extra == ADD_NL) /* \_[] also matches \n */
1198 {
1199 EMIT(reg_string ? NL : NFA_NEWL);
1200 TRY_NEG();
1201 EMIT_GLUE();
1202 }
1203
1204 /* skip the trailing ] */
1205 regparse = endp;
1206 nfa_inc(&regparse);
1207 if (negated == TRUE)
1208 {
1209 /* Mark end of negated char range */
1210 EMIT(NFA_END_NEG_RANGE);
1211 EMIT(NFA_CONCAT);
1212 }
1213 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001214 } /* if exists closing ] */
1215
1216 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217 {
1218 syntax_error = TRUE;
1219 EMSG_RET_FAIL(_(e_missingbracket));
1220 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001221 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 default:
1224 {
1225#ifdef FEAT_MBYTE
1226 int plen;
1227
1228nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001229 /* plen is length of current char with composing chars */
1230 if (enc_utf8 && ((*mb_char2len)(c)
1231 != (plen = (*mb_ptr2len)(old_regparse))
1232 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001233 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001234 int i = 0;
1235
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001236 /* A base character plus composing characters, or just one
1237 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001238 * This requires creating a separate atom as if enclosing
1239 * the characters in (), where NFA_COMPOSING is the ( and
1240 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 * building the postfix form, not the NFA itself;
1242 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001243 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001244 for (;;)
1245 {
1246 EMIT(c);
1247 if (i > 0)
1248 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001249 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001250 break;
1251 c = utf_ptr2char(old_regparse + i);
1252 }
1253 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254 regparse = old_regparse + plen;
1255 }
1256 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257#endif
1258 {
1259 c = no_Magic(c);
1260 EMIT(c);
1261 }
1262 return OK;
1263 }
1264 }
1265
1266#undef TRY_NEG
1267#undef EMIT_GLUE
1268
1269 return OK;
1270}
1271
1272/*
1273 * Parse something followed by possible [*+=].
1274 *
1275 * A piece is an atom, possibly followed by a multi, an indication of how many
1276 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1277 * characters: "", "a", "aa", etc.
1278 *
1279 * piece ::= atom
1280 * or atom multi
1281 */
1282 static int
1283nfa_regpiece()
1284{
1285 int i;
1286 int op;
1287 int ret;
1288 long minval, maxval;
1289 int greedy = TRUE; /* Braces are prefixed with '-' ? */
1290 char_u *old_regparse, *new_regparse;
1291 int c2;
1292 int *old_post_ptr, *my_post_start;
1293 int old_regnpar;
1294 int quest;
1295
1296 /* Save the current position in the regexp, so that we can use it if
1297 * <atom>{m,n} is next. */
1298 old_regparse = regparse;
1299 /* Save current number of open parenthesis, so we can use it if
1300 * <atom>{m,n} is next */
1301 old_regnpar = regnpar;
1302 /* store current pos in the postfix form, for \{m,n} involving 0s */
1303 my_post_start = post_ptr;
1304
1305 ret = nfa_regatom();
1306 if (ret == FAIL)
1307 return FAIL; /* cascaded error */
1308
1309 op = peekchr();
1310 if (re_multi_type(op) == NOT_MULTI)
1311 return OK;
1312
1313 skipchr();
1314 switch (op)
1315 {
1316 case Magic('*'):
1317 EMIT(NFA_STAR);
1318 break;
1319
1320 case Magic('+'):
1321 /*
1322 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1323 * first and only submatch would be "aaa". But the backtracking
1324 * engine interprets the plus as "try matching one more time", and
1325 * a* matches a second time at the end of the input, the empty
1326 * string.
1327 * The submatch will the empty string.
1328 *
1329 * In order to be consistent with the old engine, we disable
1330 * NFA_PLUS, and replace <atom>+ with <atom><atom>*
1331 */
1332 /* EMIT(NFA_PLUS); */
1333 regnpar = old_regnpar;
1334 regparse = old_regparse;
1335 curchr = -1;
1336 if (nfa_regatom() == FAIL)
1337 return FAIL;
1338 EMIT(NFA_STAR);
1339 EMIT(NFA_CONCAT);
1340 skipchr(); /* skip the \+ */
1341 break;
1342
1343 case Magic('@'):
1344 op = no_Magic(getchr());
1345 switch(op)
1346 {
1347 case '=':
1348 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1349 break;
1350 case '!':
1351 case '<':
1352 case '>':
1353 /* Not supported yet */
1354 return FAIL;
1355 default:
1356 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +02001357 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 return FAIL;
1359 }
1360 break;
1361
1362 case Magic('?'):
1363 case Magic('='):
1364 EMIT(NFA_QUEST);
1365 break;
1366
1367 case Magic('{'):
1368 /* a{2,5} will expand to 'aaa?a?a?'
1369 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1370 * version of '?'
1371 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1372 * parenthesis have the same id
1373 */
1374
1375 greedy = TRUE;
1376 c2 = peekchr();
1377 if (c2 == '-' || c2 == Magic('-'))
1378 {
1379 skipchr();
1380 greedy = FALSE;
1381 }
1382 if (!read_limits(&minval, &maxval))
1383 {
1384 syntax_error = TRUE;
1385 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1386 }
1387 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1388 * <atom>* */
1389 if (minval == 0 && maxval == MAX_LIMIT && greedy)
1390 {
1391 EMIT(NFA_STAR);
1392 break;
1393 }
1394
1395 if (maxval > NFA_BRACES_MAXLIMIT)
1396 {
1397 /* This would yield a huge automaton and use too much memory.
1398 * Revert to old engine */
1399 return FAIL;
1400 }
1401
1402 /* Special case: x{0} or x{-0} */
1403 if (maxval == 0)
1404 {
1405 /* Ignore result of previous call to nfa_regatom() */
1406 post_ptr = my_post_start;
1407 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1408 EMIT(NFA_SKIP_CHAR);
1409 return OK;
1410 }
1411
1412 /* Ignore previous call to nfa_regatom() */
1413 post_ptr = my_post_start;
1414 /* Save pos after the repeated atom and the \{} */
1415 new_regparse = regparse;
1416
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1418 for (i = 0; i < maxval; i++)
1419 {
1420 /* Goto beginning of the repeated atom */
1421 regparse = old_regparse;
1422 curchr = -1;
1423 /* Restore count of parenthesis */
1424 regnpar = old_regnpar;
1425 old_post_ptr = post_ptr;
1426 if (nfa_regatom() == FAIL)
1427 return FAIL;
1428 /* after "minval" times, atoms are optional */
1429 if (i + 1 > minval)
1430 EMIT(quest);
1431 if (old_post_ptr != my_post_start)
1432 EMIT(NFA_CONCAT);
1433 }
1434
1435 /* Go to just after the repeated atom and the \{} */
1436 regparse = new_regparse;
1437 curchr = -1;
1438
1439 break;
1440
1441
1442 default:
1443 break;
1444 } /* end switch */
1445
1446 if (re_multi_type(peekchr()) != NOT_MULTI)
1447 {
1448 /* Can't have a multi follow a multi. */
1449 syntax_error = TRUE;
1450 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1451 }
1452
1453 return OK;
1454}
1455
1456/*
1457 * Parse one or more pieces, concatenated. It matches a match for the
1458 * first piece, followed by a match for the second piece, etc. Example:
1459 * "f[0-9]b", first matches "f", then a digit and then "b".
1460 *
1461 * concat ::= piece
1462 * or piece piece
1463 * or piece piece piece
1464 * etc.
1465 */
1466 static int
1467nfa_regconcat()
1468{
1469 int cont = TRUE;
1470 int first = TRUE;
1471
1472 while (cont)
1473 {
1474 switch (peekchr())
1475 {
1476 case NUL:
1477 case Magic('|'):
1478 case Magic('&'):
1479 case Magic(')'):
1480 cont = FALSE;
1481 break;
1482
1483 case Magic('Z'):
1484#ifdef FEAT_MBYTE
1485 regflags |= RF_ICOMBINE;
1486#endif
1487 skipchr_keepstart();
1488 break;
1489 case Magic('c'):
1490 regflags |= RF_ICASE;
1491 skipchr_keepstart();
1492 break;
1493 case Magic('C'):
1494 regflags |= RF_NOICASE;
1495 skipchr_keepstart();
1496 break;
1497 case Magic('v'):
1498 reg_magic = MAGIC_ALL;
1499 skipchr_keepstart();
1500 curchr = -1;
1501 break;
1502 case Magic('m'):
1503 reg_magic = MAGIC_ON;
1504 skipchr_keepstart();
1505 curchr = -1;
1506 break;
1507 case Magic('M'):
1508 reg_magic = MAGIC_OFF;
1509 skipchr_keepstart();
1510 curchr = -1;
1511 break;
1512 case Magic('V'):
1513 reg_magic = MAGIC_NONE;
1514 skipchr_keepstart();
1515 curchr = -1;
1516 break;
1517
1518 default:
1519 if (nfa_regpiece() == FAIL)
1520 return FAIL;
1521 if (first == FALSE)
1522 EMIT(NFA_CONCAT);
1523 else
1524 first = FALSE;
1525 break;
1526 }
1527 }
1528
1529 return OK;
1530}
1531
1532/*
1533 * Parse a branch, one or more concats, separated by "\&". It matches the
1534 * last concat, but only if all the preceding concats also match at the same
1535 * position. Examples:
1536 * "foobeep\&..." matches "foo" in "foobeep".
1537 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1538 *
1539 * branch ::= concat
1540 * or concat \& concat
1541 * or concat \& concat \& concat
1542 * etc.
1543 */
1544 static int
1545nfa_regbranch()
1546{
1547 int ch;
1548 int *old_post_ptr;
1549
1550 old_post_ptr = post_ptr;
1551
1552 /* First branch, possibly the only one */
1553 if (nfa_regconcat() == FAIL)
1554 return FAIL;
1555
1556 ch = peekchr();
1557 /* Try next concats */
1558 while (ch == Magic('&'))
1559 {
1560 skipchr();
1561 EMIT(NFA_NOPEN);
1562 EMIT(NFA_PREV_ATOM_NO_WIDTH);
1563 old_post_ptr = post_ptr;
1564 if (nfa_regconcat() == FAIL)
1565 return FAIL;
1566 /* if concat is empty, skip a input char. But do emit a node */
1567 if (old_post_ptr == post_ptr)
1568 EMIT(NFA_SKIP_CHAR);
1569 EMIT(NFA_CONCAT);
1570 ch = peekchr();
1571 }
1572
1573 /* Even if a branch is empty, emit one node for it */
1574 if (old_post_ptr == post_ptr)
1575 EMIT(NFA_SKIP_CHAR);
1576
1577 return OK;
1578}
1579
1580/*
1581 * Parse a pattern, one or more branches, separated by "\|". It matches
1582 * anything that matches one of the branches. Example: "foo\|beep" matches
1583 * "foo" and matches "beep". If more than one branch matches, the first one
1584 * is used.
1585 *
1586 * pattern ::= branch
1587 * or branch \| branch
1588 * or branch \| branch \| branch
1589 * etc.
1590 */
1591 static int
1592nfa_reg(paren)
1593 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1594{
1595 int parno = 0;
1596
1597#ifdef FEAT_SYN_HL
1598#endif
1599 if (paren == REG_PAREN)
1600 {
1601 if (regnpar >= NSUBEXP) /* Too many `(' */
1602 {
1603 syntax_error = TRUE;
1604 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1605 }
1606 parno = regnpar++;
1607 }
1608
1609 if (nfa_regbranch() == FAIL)
1610 return FAIL; /* cascaded error */
1611
1612 while (peekchr() == Magic('|'))
1613 {
1614 skipchr();
1615 if (nfa_regbranch() == FAIL)
1616 return FAIL; /* cascaded error */
1617 EMIT(NFA_OR);
1618 }
1619
1620 /* Check for proper termination. */
1621 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1622 {
1623 syntax_error = TRUE;
1624 if (paren == REG_NPAREN)
1625 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1626 else
1627 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1628 }
1629 else if (paren == REG_NOPAREN && peekchr() != NUL)
1630 {
1631 syntax_error = TRUE;
1632 if (peekchr() == Magic(')'))
1633 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1634 else
1635 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1636 }
1637 /*
1638 * Here we set the flag allowing back references to this set of
1639 * parentheses.
1640 */
1641 if (paren == REG_PAREN)
1642 {
1643 had_endbrace[parno] = TRUE; /* have seen the close paren */
1644 EMIT(NFA_MOPEN + parno);
1645 }
1646
1647 return OK;
1648}
1649
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001650#ifdef DEBUG
1651static char_u code[50];
1652
1653 static void
1654nfa_set_code(c)
1655 int c;
1656{
1657 int addnl = FALSE;
1658
1659 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1660 {
1661 addnl = TRUE;
1662 c -= ADD_NL;
1663 }
1664
1665 STRCPY(code, "");
1666 switch (c)
1667 {
1668 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1669 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1670 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1671 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1672 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1673 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1674
1675 case NFA_PREV_ATOM_NO_WIDTH:
1676 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
1677 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break;
1678 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break;
1679 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
1680 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1681
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1683 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1684
1685 case NFA_MOPEN + 0:
1686 case NFA_MOPEN + 1:
1687 case NFA_MOPEN + 2:
1688 case NFA_MOPEN + 3:
1689 case NFA_MOPEN + 4:
1690 case NFA_MOPEN + 5:
1691 case NFA_MOPEN + 6:
1692 case NFA_MOPEN + 7:
1693 case NFA_MOPEN + 8:
1694 case NFA_MOPEN + 9:
1695 STRCPY(code, "NFA_MOPEN(x)");
1696 code[10] = c - NFA_MOPEN + '0';
1697 break;
1698 case NFA_MCLOSE + 0:
1699 case NFA_MCLOSE + 1:
1700 case NFA_MCLOSE + 2:
1701 case NFA_MCLOSE + 3:
1702 case NFA_MCLOSE + 4:
1703 case NFA_MCLOSE + 5:
1704 case NFA_MCLOSE + 6:
1705 case NFA_MCLOSE + 7:
1706 case NFA_MCLOSE + 8:
1707 case NFA_MCLOSE + 9:
1708 STRCPY(code, "NFA_MCLOSE(x)");
1709 code[11] = c - NFA_MCLOSE + '0';
1710 break;
1711 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1712 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1713 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1714 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
1715 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
1716 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break;
1717 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1718 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1719 case NFA_OR: STRCPY(code, "NFA_OR"); break;
1720 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1721 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
1722 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1723 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1724 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1725 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1726 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1727 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1728 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1729 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1730 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1731 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1732 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1733 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1734 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1735 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1736 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1737 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1738 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1739
1740 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1741 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1742 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1743 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1744 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1745 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1746 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1747 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1748 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1749 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1750 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1751 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1752 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1753 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1754 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1755 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1756 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1757 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1758 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1759 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1760 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1761 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1762 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1763 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1764 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1765 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1766 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1767
1768 default:
1769 STRCPY(code, "CHAR(x)");
1770 code[5] = c;
1771 }
1772
1773 if (addnl == TRUE)
1774 STRCAT(code, " + NEWLINE ");
1775
1776}
1777
1778#ifdef ENABLE_LOG
1779static FILE *log_fd;
1780
1781/*
1782 * Print the postfix notation of the current regexp.
1783 */
1784 static void
1785nfa_postfix_dump(expr, retval)
1786 char_u *expr;
1787 int retval;
1788{
1789 int *p;
1790 FILE *f;
1791
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001792 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001793 if (f != NULL)
1794 {
1795 fprintf(f, "\n-------------------------\n");
1796 if (retval == FAIL)
1797 fprintf(f, ">>> NFA engine failed ... \n");
1798 else if (retval == OK)
1799 fprintf(f, ">>> NFA engine succeeded !\n");
1800 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001801 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001802 {
1803 nfa_set_code(*p);
1804 fprintf(f, "%s, ", code);
1805 }
1806 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001807 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001808 fprintf(f, "%d ", *p);
1809 fprintf(f, "\n\n");
1810 fclose(f);
1811 }
1812}
1813
1814/*
1815 * Print the NFA starting with a root node "state".
1816 */
1817 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001818nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001819 FILE *debugf;
1820 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001822 garray_T indent;
1823
1824 ga_init2(&indent, 1, 64);
1825 ga_append(&indent, '\0');
1826 nfa_print_state2(debugf, state, &indent);
1827 ga_clear(&indent);
1828}
1829
1830 static void
1831nfa_print_state2(debugf, state, indent)
1832 FILE *debugf;
1833 nfa_state_T *state;
1834 garray_T *indent;
1835{
1836 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837
1838 if (state == NULL)
1839 return;
1840
1841 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001842
1843 /* Output indent */
1844 p = (char_u *)indent->ga_data;
1845 if (indent->ga_len >= 3)
1846 {
1847 int last = indent->ga_len - 3;
1848 char_u save[2];
1849
1850 STRNCPY(save, &p[last], 2);
1851 STRNCPY(&p[last], "+-", 2);
1852 fprintf(debugf, " %s", p);
1853 STRNCPY(&p[last], save, 2);
1854 }
1855 else
1856 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001857
1858 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001859 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1860 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861 if (state->id < 0)
1862 return;
1863
1864 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001865
1866 /* grow indent for state->out */
1867 indent->ga_len -= 1;
1868 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001869 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001870 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001871 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001872 ga_append(indent, '\0');
1873
1874 nfa_print_state2(debugf, state->out, indent);
1875
1876 /* replace last part of indent for state->out1 */
1877 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001878 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001879 ga_append(indent, '\0');
1880
1881 nfa_print_state2(debugf, state->out1, indent);
1882
1883 /* shrink indent */
1884 indent->ga_len -= 3;
1885 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886}
1887
1888/*
1889 * Print the NFA state machine.
1890 */
1891 static void
1892nfa_dump(prog)
1893 nfa_regprog_T *prog;
1894{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001895 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896
1897 if (debugf != NULL)
1898 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001899 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 fclose(debugf);
1901 }
1902}
1903#endif /* ENABLE_LOG */
1904#endif /* DEBUG */
1905
1906/*
1907 * Parse r.e. @expr and convert it into postfix form.
1908 * Return the postfix string on success, NULL otherwise.
1909 */
1910 static int *
1911re2post()
1912{
1913 if (nfa_reg(REG_NOPAREN) == FAIL)
1914 return NULL;
1915 EMIT(NFA_MOPEN);
1916 return post_start;
1917}
1918
1919/* NB. Some of the code below is inspired by Russ's. */
1920
1921/*
1922 * Represents an NFA state plus zero or one or two arrows exiting.
1923 * if c == MATCH, no arrows out; matching state.
1924 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
1925 * If c < 256, labeled arrow with character c to out.
1926 */
1927
1928static nfa_state_T *state_ptr; /* points to nfa_prog->state */
1929
1930/*
1931 * Allocate and initialize nfa_state_T.
1932 */
1933 static nfa_state_T *
1934new_state(c, out, out1)
1935 int c;
1936 nfa_state_T *out;
1937 nfa_state_T *out1;
1938{
1939 nfa_state_T *s;
1940
1941 if (istate >= nstate)
1942 return NULL;
1943
1944 s = &state_ptr[istate++];
1945
1946 s->c = c;
1947 s->out = out;
1948 s->out1 = out1;
1949
1950 s->id = istate;
1951 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 s->visits = 0;
1953 s->negated = FALSE;
1954
1955 return s;
1956}
1957
1958/*
1959 * A partially built NFA without the matching state filled in.
1960 * Frag_T.start points at the start state.
1961 * Frag_T.out is a list of places that need to be set to the
1962 * next state for this fragment.
1963 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001964
1965/* Since the out pointers in the list are always
1966 * uninitialized, we use the pointers themselves
1967 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001969union Ptrlist
1970{
1971 Ptrlist *next;
1972 nfa_state_T *s;
1973};
1974
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001975struct Frag
1976{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02001977 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001978 Ptrlist *out;
1979};
1980typedef struct Frag Frag_T;
1981
1982static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
1983static Ptrlist *list1 __ARGS((nfa_state_T **outp));
1984static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
1985static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
1986static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
1987static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
1988
1989/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02001990 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001991 */
1992 static Frag_T
1993frag(start, out)
1994 nfa_state_T *start;
1995 Ptrlist *out;
1996{
Bram Moolenaar053bb602013-05-20 13:55:21 +02001997 Frag_T n;
1998
1999 n.start = start;
2000 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002001 return n;
2002}
2003
2004/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005 * Create singleton list containing just outp.
2006 */
2007 static Ptrlist *
2008list1(outp)
2009 nfa_state_T **outp;
2010{
2011 Ptrlist *l;
2012
2013 l = (Ptrlist *)outp;
2014 l->next = NULL;
2015 return l;
2016}
2017
2018/*
2019 * Patch the list of states at out to point to start.
2020 */
2021 static void
2022patch(l, s)
2023 Ptrlist *l;
2024 nfa_state_T *s;
2025{
2026 Ptrlist *next;
2027
2028 for (; l; l = next)
2029 {
2030 next = l->next;
2031 l->s = s;
2032 }
2033}
2034
2035
2036/*
2037 * Join the two lists l1 and l2, returning the combination.
2038 */
2039 static Ptrlist *
2040append(l1, l2)
2041 Ptrlist *l1;
2042 Ptrlist *l2;
2043{
2044 Ptrlist *oldl1;
2045
2046 oldl1 = l1;
2047 while (l1->next)
2048 l1 = l1->next;
2049 l1->next = l2;
2050 return oldl1;
2051}
2052
2053/*
2054 * Stack used for transforming postfix form into NFA.
2055 */
2056static Frag_T empty;
2057
2058 static void
2059st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002060 int *postfix UNUSED;
2061 int *end UNUSED;
2062 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002064#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 FILE *df;
2066 int *p2;
2067
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002068 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 if (df)
2070 {
2071 fprintf(df, "Error popping the stack!\n");
2072#ifdef DEBUG
2073 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2074#endif
2075 fprintf(df, "Postfix form is: ");
2076#ifdef DEBUG
2077 for (p2 = postfix; p2 < end; p2++)
2078 {
2079 nfa_set_code(*p2);
2080 fprintf(df, "%s, ", code);
2081 }
2082 nfa_set_code(*p);
2083 fprintf(df, "\nCurrent position is: ");
2084 for (p2 = postfix; p2 <= p; p2 ++)
2085 {
2086 nfa_set_code(*p2);
2087 fprintf(df, "%s, ", code);
2088 }
2089#else
2090 for (p2 = postfix; p2 < end; p2++)
2091 {
2092 fprintf(df, "%d, ", *p2);
2093 }
2094 fprintf(df, "\nCurrent position is: ");
2095 for (p2 = postfix; p2 <= p; p2 ++)
2096 {
2097 fprintf(df, "%d, ", *p2);
2098 }
2099#endif
2100 fprintf(df, "\n--------------------------\n");
2101 fclose(df);
2102 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002103#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 EMSG(_("E874: (NFA) Could not pop the stack !"));
2105}
2106
2107/*
2108 * Push an item onto the stack.
2109 */
2110 static void
2111st_push(s, p, stack_end)
2112 Frag_T s;
2113 Frag_T **p;
2114 Frag_T *stack_end;
2115{
2116 Frag_T *stackp = *p;
2117
2118 if (stackp >= stack_end)
2119 return;
2120 *stackp = s;
2121 *p = *p + 1;
2122}
2123
2124/*
2125 * Pop an item from the stack.
2126 */
2127 static Frag_T
2128st_pop(p, stack)
2129 Frag_T **p;
2130 Frag_T *stack;
2131{
2132 Frag_T *stackp;
2133
2134 *p = *p - 1;
2135 stackp = *p;
2136 if (stackp < stack)
2137 return empty;
2138 return **p;
2139}
2140
2141/*
2142 * Convert a postfix form into its equivalent NFA.
2143 * Return the NFA start state on success, NULL otherwise.
2144 */
2145 static nfa_state_T *
2146post2nfa(postfix, end, nfa_calc_size)
2147 int *postfix;
2148 int *end;
2149 int nfa_calc_size;
2150{
2151 int *p;
2152 int mopen;
2153 int mclose;
2154 Frag_T *stack = NULL;
2155 Frag_T *stackp = NULL;
2156 Frag_T *stack_end = NULL;
2157 Frag_T e1;
2158 Frag_T e2;
2159 Frag_T e;
2160 nfa_state_T *s;
2161 nfa_state_T *s1;
2162 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002163 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164
2165 if (postfix == NULL)
2166 return NULL;
2167
Bram Moolenaar053bb602013-05-20 13:55:21 +02002168#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169#define POP() st_pop(&stackp, stack); \
2170 if (stackp < stack) \
2171 { \
2172 st_error(postfix, end, p); \
2173 return NULL; \
2174 }
2175
2176 if (nfa_calc_size == FALSE)
2177 {
2178 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002179 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002181 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 }
2183
2184 for (p = postfix; p < end; ++p)
2185 {
2186 switch (*p)
2187 {
2188 case NFA_CONCAT:
2189 /* Catenation.
2190 * Pay attention: this operator does not exist
2191 * in the r.e. itself (it is implicit, really).
2192 * It is added when r.e. is translated to postfix
2193 * form in re2post().
2194 *
2195 * No new state added here. */
2196 if (nfa_calc_size == TRUE)
2197 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002198 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002199 break;
2200 }
2201 e2 = POP();
2202 e1 = POP();
2203 patch(e1.out, e2.start);
2204 PUSH(frag(e1.start, e2.out));
2205 break;
2206
2207 case NFA_NOT:
2208 /* Negation of a character */
2209 if (nfa_calc_size == TRUE)
2210 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002211 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212 break;
2213 }
2214 e1 = POP();
2215 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002216#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002217 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002219#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 PUSH(e1);
2221 break;
2222
2223 case NFA_OR:
2224 /* Alternation */
2225 if (nfa_calc_size == TRUE)
2226 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002227 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 break;
2229 }
2230 e2 = POP();
2231 e1 = POP();
2232 s = new_state(NFA_SPLIT, e1.start, e2.start);
2233 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002234 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235 PUSH(frag(s, append(e1.out, e2.out)));
2236 break;
2237
2238 case NFA_STAR:
2239 /* Zero or more */
2240 if (nfa_calc_size == TRUE)
2241 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002242 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 break;
2244 }
2245 e = POP();
2246 s = new_state(NFA_SPLIT, e.start, NULL);
2247 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002248 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002249 patch(e.out, s);
2250 PUSH(frag(s, list1(&s->out1)));
2251 break;
2252
2253 case NFA_QUEST:
2254 /* one or zero atoms=> greedy match */
2255 if (nfa_calc_size == TRUE)
2256 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002257 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002258 break;
2259 }
2260 e = POP();
2261 s = new_state(NFA_SPLIT, e.start, NULL);
2262 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002263 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002264 PUSH(frag(s, append(e.out, list1(&s->out1))));
2265 break;
2266
2267 case NFA_QUEST_NONGREEDY:
2268 /* zero or one atoms => non-greedy match */
2269 if (nfa_calc_size == TRUE)
2270 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002271 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272 break;
2273 }
2274 e = POP();
2275 s = new_state(NFA_SPLIT, NULL, e.start);
2276 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002277 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278 PUSH(frag(s, append(e.out, list1(&s->out))));
2279 break;
2280
2281 case NFA_PLUS:
2282 /* One or more */
2283 if (nfa_calc_size == TRUE)
2284 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002285 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 break;
2287 }
2288 e = POP();
2289 s = new_state(NFA_SPLIT, e.start, NULL);
2290 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002291 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002292 patch(e.out, s);
2293 PUSH(frag(e.start, list1(&s->out1)));
2294 break;
2295
2296 case NFA_SKIP_CHAR:
2297 /* Symbol of 0-length, Used in a repetition
2298 * with max/min count of 0 */
2299 if (nfa_calc_size == TRUE)
2300 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002301 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302 break;
2303 }
2304 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2305 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002306 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002307 PUSH(frag(s, list1(&s->out)));
2308 break;
2309
2310 case NFA_PREV_ATOM_NO_WIDTH:
2311 /* The \@= operator: match the preceding atom with 0 width.
2312 * Surrounds the preceding atom with START_INVISIBLE and
2313 * END_INVISIBLE, similarly to MOPEN.
2314 */
2315 /* TODO: Maybe this drops the speed? */
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002316 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317
2318 if (nfa_calc_size == TRUE)
2319 {
2320 nstate += 2;
2321 break;
2322 }
2323 e = POP();
2324 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2325 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002326 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002327 patch(e.out, s1);
2328
2329 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2330 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002331 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002332 PUSH(frag(s, list1(&s1->out)));
2333 break;
2334
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002335#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002336 case NFA_COMPOSING: /* char with composing char */
2337#if 0
2338 /* TODO */
2339 if (regflags & RF_ICOMBINE)
2340 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002341 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002342 }
2343#endif
2344 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002345#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002346
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347 case NFA_MOPEN + 0: /* Submatch */
2348 case NFA_MOPEN + 1:
2349 case NFA_MOPEN + 2:
2350 case NFA_MOPEN + 3:
2351 case NFA_MOPEN + 4:
2352 case NFA_MOPEN + 5:
2353 case NFA_MOPEN + 6:
2354 case NFA_MOPEN + 7:
2355 case NFA_MOPEN + 8:
2356 case NFA_MOPEN + 9:
2357 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 if (nfa_calc_size == TRUE)
2359 {
2360 nstate += 2;
2361 break;
2362 }
2363
2364 mopen = *p;
2365 switch (*p)
2366 {
2367 case NFA_NOPEN:
2368 mclose = NFA_NCLOSE;
2369 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002370#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 case NFA_COMPOSING:
2372 mclose = NFA_END_COMPOSING;
2373 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002374#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002375 default:
2376 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2377 mclose = *p + NSUBEXP;
2378 break;
2379 }
2380
2381 /* Allow "NFA_MOPEN" as a valid postfix representation for
2382 * the empty regexp "". In this case, the NFA will be
2383 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2384 * empty groups of parenthesis, and empty mbyte chars */
2385 if (stackp == stack)
2386 {
2387 s = new_state(mopen, NULL, NULL);
2388 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002389 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002390 s1 = new_state(mclose, NULL, NULL);
2391 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002392 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393 patch(list1(&s->out), s1);
2394 PUSH(frag(s, list1(&s1->out)));
2395 break;
2396 }
2397
2398 /* At least one node was emitted before NFA_MOPEN, so
2399 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2400 e = POP();
2401 s = new_state(mopen, e.start, NULL); /* `(' */
2402 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002403 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002404
2405 s1 = new_state(mclose, NULL, NULL); /* `)' */
2406 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002407 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002408 patch(e.out, s1);
2409
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002410#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002411 if (mopen == NFA_COMPOSING)
2412 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002413 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002414#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415
2416 PUSH(frag(s, list1(&s1->out)));
2417 break;
2418
2419 case NFA_ZSTART:
2420 case NFA_ZEND:
2421 default:
2422 /* Operands */
2423 if (nfa_calc_size == TRUE)
2424 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002425 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002426 break;
2427 }
2428 s = new_state(*p, NULL, NULL);
2429 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002430 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 PUSH(frag(s, list1(&s->out)));
2432 break;
2433
2434 } /* switch(*p) */
2435
2436 } /* for(p = postfix; *p; ++p) */
2437
2438 if (nfa_calc_size == TRUE)
2439 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002440 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002441 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 }
2443
2444 e = POP();
2445 if (stackp != stack)
2446 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2447
2448 if (istate >= nstate)
2449 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2450
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002451 matchstate = &state_ptr[istate++]; /* the match state */
2452 matchstate->c = NFA_MATCH;
2453 matchstate->out = matchstate->out1 = NULL;
2454
2455 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002456 ret = e.start;
2457
2458theend:
2459 vim_free(stack);
2460 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461
2462#undef POP1
2463#undef PUSH1
2464#undef POP2
2465#undef PUSH2
2466#undef POP
2467#undef PUSH
2468}
2469
2470/****************************************************************
2471 * NFA execution code.
2472 ****************************************************************/
2473
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002474typedef struct
2475{
2476 int in_use; /* number of subexpr with useful info */
2477
2478 /* When REG_MULTI is TRUE multilist is used, otherwise linelist. */
2479 union
2480 {
2481 struct multipos
2482 {
2483 lpos_T start;
2484 lpos_T end;
2485 } multilist[NSUBEXP];
2486 struct linepos
2487 {
2488 char_u *start;
2489 char_u *end;
2490 } linelist[NSUBEXP];
2491 };
2492} regsub_T;
2493
Bram Moolenaar963fee22013-05-26 21:47:28 +02002494/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002495typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496{
2497 nfa_state_T *state;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002498 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002499} nfa_thread_T;
2500
Bram Moolenaar963fee22013-05-26 21:47:28 +02002501/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502typedef struct
2503{
Bram Moolenaar4b417062013-05-25 20:19:50 +02002504 nfa_thread_T *t;
2505 int n;
2506} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002507
Bram Moolenaar963fee22013-05-26 21:47:28 +02002508/* Used during execution: whether a match has been found. */
2509static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002510
Bram Moolenaar963fee22013-05-26 21:47:28 +02002511static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int off, int lid));
Bram Moolenaar963fee22013-05-26 21:47:28 +02002512static 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 +02002513
2514 static void
Bram Moolenaar963fee22013-05-26 21:47:28 +02002515addstate(l, state, m, off, lid)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002516 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 nfa_state_T *state; /* state to update */
2518 regsub_T *m; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002519 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520 int lid;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002522 int subidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002523 nfa_thread_T *lastthread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002524 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002525 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002526 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002527 int i;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002528
2529 if (l == NULL || state == NULL)
2530 return;
2531
2532 switch (state->c)
2533 {
2534 case NFA_SPLIT:
2535 case NFA_NOT:
2536 case NFA_NOPEN:
2537 case NFA_NCLOSE:
2538 case NFA_MCLOSE:
2539 case NFA_MCLOSE + 1:
2540 case NFA_MCLOSE + 2:
2541 case NFA_MCLOSE + 3:
2542 case NFA_MCLOSE + 4:
2543 case NFA_MCLOSE + 5:
2544 case NFA_MCLOSE + 6:
2545 case NFA_MCLOSE + 7:
2546 case NFA_MCLOSE + 8:
2547 case NFA_MCLOSE + 9:
2548 /* Do not remember these nodes in list "thislist" or "nextlist" */
2549 break;
2550
2551 default:
2552 if (state->lastlist == lid)
2553 {
2554 if (++state->visits > 2)
2555 return;
2556 }
2557 else
2558 {
2559 /* add the state to the list */
2560 state->lastlist = lid;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002561 lastthread = &l->t[l->n++];
2562 lastthread->state = state;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002563 lastthread->sub.in_use = m->in_use;
2564 if (m->in_use > 0)
2565 {
2566 /* Copy the match start and end positions. */
2567 if (REG_MULTI)
2568 mch_memmove(&lastthread->sub.multilist[0],
2569 &m->multilist[0],
2570 sizeof(struct multipos) * m->in_use);
2571 else
2572 mch_memmove(&lastthread->sub.linelist[0],
2573 &m->linelist[0],
2574 sizeof(struct linepos) * m->in_use);
2575 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002576 }
2577 }
2578
2579#ifdef ENABLE_LOG
2580 nfa_set_code(state->c);
2581 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n",
2582 abs(state->id), code, state->c);
2583#endif
2584 switch (state->c)
2585 {
2586 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002587 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002588 break;
2589
2590 case NFA_SPLIT:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002591 addstate(l, state->out, m, off, lid);
2592 addstate(l, state->out1, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002593 break;
2594
2595 case NFA_SKIP_CHAR:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002596 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002597 break;
2598
2599#if 0
2600 case NFA_END_NEG_RANGE:
2601 /* Nothing to handle here. nfa_regmatch() will take care of it */
2602 break;
2603
2604 case NFA_NOT:
2605 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2606#ifdef ENABLE_LOG
2607 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2608#endif
2609 break;
2610
2611 case NFA_COMPOSING:
2612 /* nfa_regmatch() will match all the bytes of this composing char. */
2613 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614#endif
2615
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616 case NFA_NOPEN:
2617 case NFA_NCLOSE:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002618 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002619 break;
2620
2621 /* If this state is reached, then a recursive call of nfa_regmatch()
2622 * succeeded. the next call saves the found submatches in the
2623 * first state after the "invisible" branch. */
2624#if 0
2625 case NFA_END_INVISIBLE:
2626 break;
2627#endif
2628
2629 case NFA_MOPEN + 0:
2630 case NFA_MOPEN + 1:
2631 case NFA_MOPEN + 2:
2632 case NFA_MOPEN + 3:
2633 case NFA_MOPEN + 4:
2634 case NFA_MOPEN + 5:
2635 case NFA_MOPEN + 6:
2636 case NFA_MOPEN + 7:
2637 case NFA_MOPEN + 8:
2638 case NFA_MOPEN + 9:
2639 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002640 if (state->c == NFA_ZSTART)
2641 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002642 else
2643 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002644
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002645 /* Set the position (with "off") in the subexpression. Save and
2646 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 if (REG_MULTI)
2648 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002649 if (subidx < m->in_use)
2650 {
2651 save_lpos = m->multilist[subidx].start;
2652 save_in_use = -1;
2653 }
2654 else
2655 {
2656 save_in_use = m->in_use;
2657 for (i = m->in_use; i < subidx; ++i)
2658 {
2659 m->multilist[i].start.lnum = -1;
2660 m->multilist[i].end.lnum = -1;
2661 }
2662 m->in_use = subidx + 1;
2663 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002664 if (off == -1)
2665 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002666 m->multilist[subidx].start.lnum = reglnum + 1;
2667 m->multilist[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002668 }
2669 else
2670 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002671 m->multilist[subidx].start.lnum = reglnum;
2672 m->multilist[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002673 (colnr_T)(reginput - regline + off);
2674 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002675 }
2676 else
2677 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002678 if (subidx < m->in_use)
2679 {
2680 save_ptr = m->linelist[subidx].start;
2681 save_in_use = -1;
2682 }
2683 else
2684 {
2685 save_in_use = m->in_use;
2686 for (i = m->in_use; i < subidx; ++i)
2687 {
2688 m->linelist[i].start = NULL;
2689 m->linelist[i].end = NULL;
2690 }
2691 m->in_use = subidx + 1;
2692 }
Bram Moolenaar963fee22013-05-26 21:47:28 +02002693 m->linelist[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002694 }
2695
Bram Moolenaar963fee22013-05-26 21:47:28 +02002696 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002698 if (save_in_use == -1)
2699 {
2700 if (REG_MULTI)
2701 m->multilist[subidx].start = save_lpos;
2702 else
2703 m->linelist[subidx].start = save_ptr;
2704 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002705 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002706 m->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002707 break;
2708
2709 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02002710 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002711 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02002712 /* Do not overwrite the position set by \ze. If no \ze
2713 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar963fee22013-05-26 21:47:28 +02002714 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715 break;
2716 }
2717 case NFA_MCLOSE + 1:
2718 case NFA_MCLOSE + 2:
2719 case NFA_MCLOSE + 3:
2720 case NFA_MCLOSE + 4:
2721 case NFA_MCLOSE + 5:
2722 case NFA_MCLOSE + 6:
2723 case NFA_MCLOSE + 7:
2724 case NFA_MCLOSE + 8:
2725 case NFA_MCLOSE + 9:
2726 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727 if (state->c == NFA_ZEND)
2728 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002729 else
2730 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002732 /* We don't fill in gaps here, there must have been an MOPEN that
2733 * has done that. */
2734 save_in_use = m->in_use;
2735 if (m->in_use <= subidx)
2736 m->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737 if (REG_MULTI)
2738 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002739 save_lpos = m->multilist[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002740 if (off == -1)
2741 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002742 m->multilist[subidx].end.lnum = reglnum + 1;
2743 m->multilist[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002744 }
2745 else
2746 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002747 m->multilist[subidx].end.lnum = reglnum;
2748 m->multilist[subidx].end.col =
2749 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02002750 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 }
2752 else
2753 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02002754 save_ptr = m->linelist[subidx].end;
2755 m->linelist[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756 }
2757
Bram Moolenaar963fee22013-05-26 21:47:28 +02002758 addstate(l, state->out, m, off, lid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002759
2760 if (REG_MULTI)
Bram Moolenaar963fee22013-05-26 21:47:28 +02002761 m->multilist[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762 else
Bram Moolenaar963fee22013-05-26 21:47:28 +02002763 m->linelist[subidx].end = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002764 m->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 break;
2766 }
2767}
2768
2769/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02002770 * Like addstate(), but the new state(s) are put at position "*ip".
2771 * Used for zero-width matches, next state to use is the added one.
2772 * This makes sure the order of states to be tried does not change, which
2773 * matters for alternatives.
2774 */
2775 static void
Bram Moolenaar963fee22013-05-26 21:47:28 +02002776addstate_here(l, state, m, lid, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002777 nfa_list_T *l; /* runtime state list */
2778 nfa_state_T *state; /* state to update */
2779 regsub_T *m; /* pointers to subexpressions */
2780 int lid;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002781 int *ip;
2782{
2783 int tlen = l->n;
2784 int count;
2785 int i = *ip;
2786
2787 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar963fee22013-05-26 21:47:28 +02002788 addstate(l, state, m, 0, lid);
Bram Moolenaar4b417062013-05-25 20:19:50 +02002789
2790 /* when "*ip" was at the end of the list, nothing to do */
2791 if (i + 1 == tlen)
2792 return;
2793
2794 /* re-order to put the new state at the current position */
2795 count = l->n - tlen;
2796 if (count > 1)
2797 {
2798 /* make space for new states, then move them from the
2799 * end to the current position */
2800 mch_memmove(&(l->t[i + count]),
2801 &(l->t[i + 1]),
2802 sizeof(nfa_thread_T) * (l->n - i - 1));
2803 mch_memmove(&(l->t[i]),
2804 &(l->t[l->n - 1]),
2805 sizeof(nfa_thread_T) * count);
2806 }
2807 else
2808 {
2809 /* overwrite the current state */
2810 l->t[i] = l->t[l->n - 1];
2811 }
2812 --l->n;
2813 *ip = i - 1;
2814}
2815
2816/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817 * Check character class "class" against current character c.
2818 */
2819 static int
2820check_char_class(class, c)
2821 int class;
2822 int c;
2823{
2824 switch (class)
2825 {
2826 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002827 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 return OK;
2829 break;
2830 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002831 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 return OK;
2833 break;
2834 case NFA_CLASS_BLANK:
2835 if (c == ' ' || c == '\t')
2836 return OK;
2837 break;
2838 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002839 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840 return OK;
2841 break;
2842 case NFA_CLASS_DIGIT:
2843 if (VIM_ISDIGIT(c))
2844 return OK;
2845 break;
2846 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002847 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002848 return OK;
2849 break;
2850 case NFA_CLASS_LOWER:
2851 if (MB_ISLOWER(c))
2852 return OK;
2853 break;
2854 case NFA_CLASS_PRINT:
2855 if (vim_isprintc(c))
2856 return OK;
2857 break;
2858 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02002859 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860 return OK;
2861 break;
2862 case NFA_CLASS_SPACE:
2863 if ((c >=9 && c <= 13) || (c == ' '))
2864 return OK;
2865 break;
2866 case NFA_CLASS_UPPER:
2867 if (MB_ISUPPER(c))
2868 return OK;
2869 break;
2870 case NFA_CLASS_XDIGIT:
2871 if (vim_isxdigit(c))
2872 return OK;
2873 break;
2874 case NFA_CLASS_TAB:
2875 if (c == '\t')
2876 return OK;
2877 break;
2878 case NFA_CLASS_RETURN:
2879 if (c == '\r')
2880 return OK;
2881 break;
2882 case NFA_CLASS_BACKSPACE:
2883 if (c == '\b')
2884 return OK;
2885 break;
2886 case NFA_CLASS_ESCAPE:
2887 if (c == '\033')
2888 return OK;
2889 break;
2890
2891 default:
2892 /* should not be here :P */
2893 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
2894 }
2895 return FAIL;
2896}
2897
2898/*
2899 * Set all NFA nodes' list ID equal to -1.
2900 */
2901 static void
2902nfa_set_neg_listids(start)
2903 nfa_state_T *start;
2904{
2905 if (start == NULL)
2906 return;
2907 if (start->lastlist >= 0)
2908 {
2909 start->lastlist = -1;
2910 nfa_set_neg_listids(start->out);
2911 nfa_set_neg_listids(start->out1);
2912 }
2913}
2914
2915/*
2916 * Set all NFA nodes' list ID equal to 0.
2917 */
2918 static void
2919nfa_set_null_listids(start)
2920 nfa_state_T *start;
2921{
2922 if (start == NULL)
2923 return;
2924 if (start->lastlist == -1)
2925 {
2926 start->lastlist = 0;
2927 nfa_set_null_listids(start->out);
2928 nfa_set_null_listids(start->out1);
2929 }
2930}
2931
2932/*
2933 * Save list IDs for all NFA states in "list".
2934 */
2935 static void
2936nfa_save_listids(start, list)
2937 nfa_state_T *start;
2938 int *list;
2939{
2940 if (start == NULL)
2941 return;
2942 if (start->lastlist != -1)
2943 {
2944 list[abs(start->id)] = start->lastlist;
2945 start->lastlist = -1;
2946 nfa_save_listids(start->out, list);
2947 nfa_save_listids(start->out1, list);
2948 }
2949}
2950
2951/*
2952 * Restore list IDs from "list" to all NFA states.
2953 */
2954 static void
2955nfa_restore_listids(start, list)
2956 nfa_state_T *start;
2957 int *list;
2958{
2959 if (start == NULL)
2960 return;
2961 if (start->lastlist == -1)
2962 {
2963 start->lastlist = list[abs(start->id)];
2964 nfa_restore_listids(start->out, list);
2965 nfa_restore_listids(start->out1, list);
2966 }
2967}
2968
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002969static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m));
2970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002971/*
2972 * Main matching routine.
2973 *
2974 * Run NFA to determine whether it matches reginput.
2975 *
2976 * Return TRUE if there is a match, FALSE otherwise.
2977 * Note: Caller must ensure that: start != NULL.
2978 */
2979 static int
2980nfa_regmatch(start, submatch, m)
2981 nfa_state_T *start;
2982 regsub_T *submatch;
2983 regsub_T *m;
2984{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002985 int result;
2986 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002987 int flag = 0;
2988 int old_reglnum = -1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002989 int go_to_nextline = FALSE;
2990 nfa_thread_T *t;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002991 char_u *old_reginput = NULL;
2992 char_u *old_regline = NULL;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002993 nfa_list_T list[3];
2994 nfa_list_T *listtbl[2][2];
2995 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002996 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02002997 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002998 nfa_list_T *thislist;
2999 nfa_list_T *nextlist;
3000 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003001 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003002#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003003 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003004
3005 if (debug == NULL)
3006 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003007 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003008 return FALSE;
3009 }
3010#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003011 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003012
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003013 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003014 size = (nstate + 1) * sizeof(nfa_thread_T);
3015 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
3016 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
3017 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003018 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3019 goto theend;
3020 vim_memset(list[0].t, 0, size);
3021 vim_memset(list[1].t, 0, size);
3022 vim_memset(list[2].t, 0, size);
3023
3024#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003025 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003026 if (log_fd != NULL)
3027 {
3028 fprintf(log_fd, "**********************************\n");
3029 nfa_set_code(start->c);
3030 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3031 abs(start->id), code);
3032 fprintf(log_fd, "**********************************\n");
3033 }
3034 else
3035 {
3036 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3037 log_fd = stderr;
3038 }
3039#endif
3040
3041 thislist = &list[0];
3042 thislist->n = 0;
3043 nextlist = &list[1];
3044 nextlist->n = 0;
3045 neglist = &list[2];
3046 neglist->n = 0;
3047#ifdef ENABLE_LOG
3048 fprintf(log_fd, "(---) STARTSTATE\n");
3049#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003050 addstate(thislist, start, m, 0, listid);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003051
3052 /* There are two cases when the NFA advances: 1. input char matches the
3053 * NFA node and 2. input char does not match the NFA node, but the next
3054 * node is NFA_NOT. The following macro calls addstate() according to
3055 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3056 listtbl[0][0] = NULL;
3057 listtbl[0][1] = neglist;
3058 listtbl[1][0] = nextlist;
3059 listtbl[1][1] = NULL;
3060#define ADD_POS_NEG_STATE(node) \
3061 ll = listtbl[result ? 1 : 0][node->negated]; \
3062 if (ll != NULL) \
Bram Moolenaar963fee22013-05-26 21:47:28 +02003063 addstate(ll, node->out , &t->sub, clen, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003064
3065
3066 /*
3067 * Run for each character.
3068 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003069 for (;;)
3070 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003071 int curc;
3072 int clen;
3073
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003074#ifdef FEAT_MBYTE
3075 if (has_mbyte)
3076 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003077 curc = (*mb_ptr2char)(reginput);
3078 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003079 }
3080 else
3081#endif
3082 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003083 curc = *reginput;
3084 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003085 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003086 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003087 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003088 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003089 go_to_nextline = FALSE;
3090 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003091
3092 /* swap lists */
3093 thislist = &list[flag];
3094 nextlist = &list[flag ^= 1];
3095 nextlist->n = 0; /* `clear' nextlist */
3096 listtbl[1][0] = nextlist;
3097 ++listid;
3098
3099#ifdef ENABLE_LOG
3100 fprintf(log_fd, "------------------------------------------\n");
3101 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003102 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003103 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003104 {
3105 int i;
3106
3107 for (i = 0; i < thislist->n; i++)
3108 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3109 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003110 fprintf(log_fd, "\n");
3111#endif
3112
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003113#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 fprintf(debug, "\n-------------------\n");
3115#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003116 /*
3117 * If the state lists are empty we can stop.
3118 */
3119 if (thislist->n == 0 && neglist->n == 0)
3120 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003121
3122 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003123 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003124 {
3125 if (neglist->n > 0)
3126 {
3127 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003128 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003129 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003130 }
3131 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003132 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003133
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003134#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003135 nfa_set_code(t->state->c);
3136 fprintf(debug, "%s, ", code);
3137#endif
3138#ifdef ENABLE_LOG
3139 nfa_set_code(t->state->c);
3140 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id),
3141 code, (int)t->state->c);
3142#endif
3143
3144 /*
3145 * Handle the possible codes of the current state.
3146 * The most important is NFA_MATCH.
3147 */
3148 switch (t->state->c)
3149 {
3150 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003151 {
3152 int j;
3153
Bram Moolenaar963fee22013-05-26 21:47:28 +02003154 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003155 submatch->in_use = t->sub.in_use;
3156 if (REG_MULTI)
3157 for (j = 0; j < submatch->in_use; j++)
3158 {
3159 submatch->multilist[j].start = t->sub.multilist[j].start;
3160 submatch->multilist[j].end = t->sub.multilist[j].end;
3161 }
3162 else
3163 for (j = 0; j < submatch->in_use; j++)
3164 {
3165 submatch->linelist[j].start = t->sub.linelist[j].start;
3166 submatch->linelist[j].end = t->sub.linelist[j].end;
3167 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003168#ifdef ENABLE_LOG
Bram Moolenaar2cb8feb2013-05-26 23:13:07 +02003169 for (j = 0; j < t->sub.in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003170 if (REG_MULTI)
3171 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
3172 j,
Bram Moolenaar2cb8feb2013-05-26 23:13:07 +02003173 t->sub.multilist[j].start.col,
3174 (int)t->sub.multilist[j].start.lnum,
3175 t->sub.multilist[j].end.col,
3176 (int)t->sub.multilist[j].end.lnum);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 else
3178 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
3179 j,
Bram Moolenaar2cb8feb2013-05-26 23:13:07 +02003180 (char *)t->sub.linelist[j].start,
3181 (char *)t->sub.linelist[j].end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003182 fprintf(log_fd, "\n");
3183#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003184 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003185 * states at this position. When the list of states is going
3186 * to be empty quit without advancing, so that "reginput" is
3187 * correct. */
3188 if (nextlist->n == 0 && neglist->n == 0)
3189 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003190 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003191 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003192
3193 case NFA_END_INVISIBLE:
3194 /* This is only encountered after a NFA_START_INVISIBLE node.
3195 * They surround a zero-width group, used with "\@=" and "\&".
3196 * If we got here, it means that the current "invisible" group
3197 * finished successfully, so return control to the parent
3198 * nfa_regmatch(). Submatches are stored in *m, and used in
3199 * the parent call. */
3200 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003201 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003202 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 else
3204 {
3205 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003206 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 }
3208 break;
3209
3210 case NFA_START_INVISIBLE:
3211 /* Save global variables, and call nfa_regmatch() to check if
3212 * the current concat matches at this position. The concat
3213 * ends with the node NFA_END_INVISIBLE */
3214 old_reginput = reginput;
3215 old_regline = regline;
3216 old_reglnum = reglnum;
3217 if (listids == NULL)
3218 {
3219 listids = (int *) lalloc(sizeof(int) * nstate, TRUE);
3220 if (listids == NULL)
3221 {
3222 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3223 return 0;
3224 }
3225 }
3226#ifdef ENABLE_LOG
3227 if (log_fd != stderr)
3228 fclose(log_fd);
3229 log_fd = NULL;
3230#endif
3231 /* Have to clear the listid field of the NFA nodes, so that
3232 * nfa_regmatch() and addstate() can run properly after
3233 * recursion. */
3234 nfa_save_listids(start, listids);
3235 nfa_set_null_listids(start);
3236 result = nfa_regmatch(t->state->out, submatch, m);
3237 nfa_set_neg_listids(start);
3238 nfa_restore_listids(start, listids);
3239
3240#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003241 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003242 if (log_fd != NULL)
3243 {
3244 fprintf(log_fd, "****************************\n");
3245 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3246 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3247 fprintf(log_fd, "****************************\n");
3248 }
3249 else
3250 {
3251 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3252 log_fd = stderr;
3253 }
3254#endif
3255 if (result == TRUE)
3256 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003257 int j;
3258
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 /* Restore position in input text */
3260 reginput = old_reginput;
3261 regline = old_regline;
3262 reglnum = old_reglnum;
3263 /* Copy submatch info from the recursive call */
3264 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003265 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003267 t->sub.multilist[j].start = m->multilist[j].start;
3268 t->sub.multilist[j].end = m->multilist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 }
3270 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003271 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003273 t->sub.linelist[j].start = m->linelist[j].start;
3274 t->sub.linelist[j].end = m->linelist[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003276 t->sub.in_use = m->in_use;
3277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 /* t->state->out1 is the corresponding END_INVISIBLE node */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003279 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003280 listid, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 }
3282 else
3283 {
3284 /* continue with next input char */
3285 reginput = old_reginput;
3286 }
3287 break;
3288
3289 case NFA_BOL:
3290 if (reginput == regline)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003291 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003292 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003293 break;
3294
3295 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003296 if (curc == NUL)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003297 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003298 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300
3301 case NFA_BOW:
3302 {
3303 int bow = TRUE;
3304
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003305 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 bow = FALSE;
3307#ifdef FEAT_MBYTE
3308 else if (has_mbyte)
3309 {
3310 int this_class;
3311
3312 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003313 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 if (this_class <= 1)
3315 bow = FALSE;
3316 else if (reg_prev_class() == this_class)
3317 bow = FALSE;
3318 }
3319#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003320 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003321 || (reginput > regline
3322 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003323 bow = FALSE;
3324 if (bow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003325 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003326 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327 break;
3328 }
3329
3330 case NFA_EOW:
3331 {
3332 int eow = TRUE;
3333
3334 if (reginput == regline)
3335 eow = FALSE;
3336#ifdef FEAT_MBYTE
3337 else if (has_mbyte)
3338 {
3339 int this_class, prev_class;
3340
3341 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003342 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 prev_class = reg_prev_class();
3344 if (this_class == prev_class
3345 || prev_class == 0 || prev_class == 1)
3346 eow = FALSE;
3347 }
3348#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003349 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003350 || (reginput[0] != NUL
3351 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003352 eow = FALSE;
3353 if (eow)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003354 addstate_here(thislist, t->state->out, &t->sub, listid,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003355 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356 break;
3357 }
3358
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003359#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003361 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003362 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003363 int len = 0;
3364 nfa_state_T *end;
3365 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003366 int cchars[MAX_MCO];
3367 int ccount = 0;
3368 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003369
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003371 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003372 if (utf_iscomposing(sta->c))
3373 {
3374 /* Only match composing character(s), ignore base
3375 * character. Used for ".{composing}" and "{composing}"
3376 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003377 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003378 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003379 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003380 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003381 /* If \Z was present, then ignore composing characters.
3382 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003383 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003384 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003385 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003386 else
3387 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003388 while (sta->c != NFA_END_COMPOSING)
3389 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003391
3392 /* Check base character matches first, unless ignored. */
3393 else if (len > 0 || mc == sta->c)
3394 {
3395 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003396 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003397 len += mb_char2len(mc);
3398 sta = sta->out;
3399 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003401 /* We don't care about the order of composing characters.
3402 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003403 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003404 {
3405 mc = mb_ptr2char(reginput + len);
3406 cchars[ccount++] = mc;
3407 len += mb_char2len(mc);
3408 if (ccount == MAX_MCO)
3409 break;
3410 }
3411
3412 /* Check that each composing char in the pattern matches a
3413 * composing char in the text. We do not check if all
3414 * composing chars are matched. */
3415 result = OK;
3416 while (sta->c != NFA_END_COMPOSING)
3417 {
3418 for (j = 0; j < ccount; ++j)
3419 if (cchars[j] == sta->c)
3420 break;
3421 if (j == ccount)
3422 {
3423 result = FAIL;
3424 break;
3425 }
3426 sta = sta->out;
3427 }
3428 }
3429 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003430 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003431
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003432 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003433 ADD_POS_NEG_STATE(end);
3434 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003435 }
3436#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003437
3438 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003439 if (curc == NUL && !reg_line_lbr && REG_MULTI
3440 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003442 go_to_nextline = TRUE;
3443 /* Pass -1 for the offset, which means taking the position
3444 * at the start of the next line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003445 addstate(nextlist, t->state->out, &t->sub, -1, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003446 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003447 else if (curc == '\n' && reg_line_lbr)
3448 {
3449 /* match \n as if it is an ordinary character */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003450 addstate(nextlist, t->state->out, &t->sub, 1, listid + 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003451 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003452 break;
3453
3454 case NFA_CLASS_ALNUM:
3455 case NFA_CLASS_ALPHA:
3456 case NFA_CLASS_BLANK:
3457 case NFA_CLASS_CNTRL:
3458 case NFA_CLASS_DIGIT:
3459 case NFA_CLASS_GRAPH:
3460 case NFA_CLASS_LOWER:
3461 case NFA_CLASS_PRINT:
3462 case NFA_CLASS_PUNCT:
3463 case NFA_CLASS_SPACE:
3464 case NFA_CLASS_UPPER:
3465 case NFA_CLASS_XDIGIT:
3466 case NFA_CLASS_TAB:
3467 case NFA_CLASS_RETURN:
3468 case NFA_CLASS_BACKSPACE:
3469 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003470 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471 ADD_POS_NEG_STATE(t->state);
3472 break;
3473
3474 case NFA_END_NEG_RANGE:
3475 /* This follows a series of negated nodes, like:
3476 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003477 if (curc > 0)
3478 addstate(nextlist, t->state->out, &t->sub, clen,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003479 listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003480 break;
3481
3482 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003483 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003484 if (curc > 0)
3485 addstate(nextlist, t->state->out, &t->sub, clen,
Bram Moolenaar963fee22013-05-26 21:47:28 +02003486 listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 break;
3488
3489 /*
3490 * Character classes like \a for alpha, \d for digit etc.
3491 */
3492 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003493 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 ADD_POS_NEG_STATE(t->state);
3495 break;
3496
3497 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003498 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499 ADD_POS_NEG_STATE(t->state);
3500 break;
3501
3502 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003503 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 ADD_POS_NEG_STATE(t->state);
3505 break;
3506
3507 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003508 result = !VIM_ISDIGIT(curc)
3509 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 ADD_POS_NEG_STATE(t->state);
3511 break;
3512
3513 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003514 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003515 ADD_POS_NEG_STATE(t->state);
3516 break;
3517
3518 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003519 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 ADD_POS_NEG_STATE(t->state);
3521 break;
3522
3523 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003524 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003525 ADD_POS_NEG_STATE(t->state);
3526 break;
3527
3528 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003529 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530 ADD_POS_NEG_STATE(t->state);
3531 break;
3532
3533 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003534 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 ADD_POS_NEG_STATE(t->state);
3536 break;
3537
3538 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003539 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 ADD_POS_NEG_STATE(t->state);
3541 break;
3542
3543 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003544 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003545 ADD_POS_NEG_STATE(t->state);
3546 break;
3547
3548 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003549 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003550 ADD_POS_NEG_STATE(t->state);
3551 break;
3552
3553 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003554 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 ADD_POS_NEG_STATE(t->state);
3556 break;
3557
3558 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003559 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003560 ADD_POS_NEG_STATE(t->state);
3561 break;
3562
3563 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003564 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 ADD_POS_NEG_STATE(t->state);
3566 break;
3567
3568 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003569 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 ADD_POS_NEG_STATE(t->state);
3571 break;
3572
3573 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003574 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003575 ADD_POS_NEG_STATE(t->state);
3576 break;
3577
3578 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003579 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 ADD_POS_NEG_STATE(t->state);
3581 break;
3582
3583 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003584 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003585 ADD_POS_NEG_STATE(t->state);
3586 break;
3587
3588 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003589 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003590 ADD_POS_NEG_STATE(t->state);
3591 break;
3592
3593 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003594 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 ADD_POS_NEG_STATE(t->state);
3596 break;
3597
3598 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003599 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 ADD_POS_NEG_STATE(t->state);
3601 break;
3602
3603 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003604 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 ADD_POS_NEG_STATE(t->state);
3606 break;
3607
3608 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003609 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 ADD_POS_NEG_STATE(t->state);
3611 break;
3612
3613 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003614 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 ADD_POS_NEG_STATE(t->state);
3616 break;
3617
3618 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003619 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003620 ADD_POS_NEG_STATE(t->state);
3621 break;
3622
Bram Moolenaar12e40142013-05-21 15:33:41 +02003623 case NFA_MOPEN + 0:
3624 case NFA_MOPEN + 1:
3625 case NFA_MOPEN + 2:
3626 case NFA_MOPEN + 3:
3627 case NFA_MOPEN + 4:
3628 case NFA_MOPEN + 5:
3629 case NFA_MOPEN + 6:
3630 case NFA_MOPEN + 7:
3631 case NFA_MOPEN + 8:
3632 case NFA_MOPEN + 9:
3633 /* handled below */
3634 break;
3635
3636 case NFA_SKIP_CHAR:
3637 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003638 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02003639 /* TODO: should not happen? */
3640 break;
3641
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003643 {
3644 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02003645
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003646 /* TODO: put this in #ifdef later */
3647 if (c < -256)
3648 EMSGN("INTERNAL: Negative state char: %ld", c);
3649 if (is_Magic(c))
3650 c = un_Magic(c);
3651 result = (c == curc);
3652
3653 if (!result && ireg_ic)
3654 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003655#ifdef FEAT_MBYTE
3656 /* If there is a composing character which is not being
3657 * ignored there can be no match. Match with composing
3658 * character uses NFA_COMPOSING above. */
3659 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003660 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003661 result = FALSE;
3662#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003663 ADD_POS_NEG_STATE(t->state);
3664 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02003665 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003666 }
3667
3668 } /* for (thislist = thislist; thislist->state; thislist++) */
3669
Bram Moolenaare23febd2013-05-26 18:40:14 +02003670 /* Look for the start of a match in the current position by adding the
3671 * start state to the list of states.
3672 * The first found match is the leftmost one, thus the order of states
3673 * matters!
3674 * Do not add the start state in recursive calls of nfa_regmatch(),
3675 * because recursive calls should only start in the first position.
3676 * Also don't start a match past the first line. */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003677 if (nfa_match == FALSE && start->c == NFA_MOPEN + 0
Bram Moolenaare23febd2013-05-26 18:40:14 +02003678 && reglnum == 0 && clen != 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 {
3680#ifdef ENABLE_LOG
3681 fprintf(log_fd, "(---) STARTSTATE\n");
3682#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003683 addstate(nextlist, start, m, clen, listid + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003684 }
3685
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003686#ifdef ENABLE_LOG
3687 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003688 {
3689 int i;
3690
3691 for (i = 0; i < thislist->n; i++)
3692 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3693 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694 fprintf(log_fd, "\n");
3695#endif
3696
3697nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003698 /* Advance to the next character, or advance to the next line, or
3699 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003700 if (clen != 0)
3701 reginput += clen;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003702 else if (go_to_nextline)
3703 reg_nextline();
3704 else
3705 break;
3706 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003707
3708#ifdef ENABLE_LOG
3709 if (log_fd != stderr)
3710 fclose(log_fd);
3711 log_fd = NULL;
3712#endif
3713
3714theend:
3715 /* Free memory */
3716 vim_free(list[0].t);
3717 vim_free(list[1].t);
3718 vim_free(list[2].t);
3719 list[0].t = list[1].t = list[2].t = NULL;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003720 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003721#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003722#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003723 fclose(debug);
3724#endif
3725
Bram Moolenaar963fee22013-05-26 21:47:28 +02003726 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003727}
3728
3729/*
3730 * Try match of "prog" with at regline["col"].
3731 * Returns 0 for failure, number of lines contained in the match otherwise.
3732 */
3733 static long
3734nfa_regtry(start, col)
3735 nfa_state_T *start;
3736 colnr_T col;
3737{
3738 int i;
3739 regsub_T sub, m;
3740#ifdef ENABLE_LOG
3741 FILE *f;
3742#endif
3743
3744 reginput = regline + col;
3745 need_clear_subexpr = TRUE;
3746
3747#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003748 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749 if (f != NULL)
3750 {
3751 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
3752 fprintf(f, " =======================================================\n");
3753#ifdef DEBUG
3754 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
3755#endif
3756 fprintf(f, "\tInput text is \"%s\" \n", reginput);
3757 fprintf(f, " =======================================================\n\n\n\n\n\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02003758 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 fprintf(f, "\n\n");
3760 fclose(f);
3761 }
3762 else
3763 EMSG(_("Could not open temporary log file for writing "));
3764#endif
3765
3766 if (REG_MULTI)
3767 {
3768 /* Use 0xff to set lnum to -1 */
Bram Moolenaar963fee22013-05-26 21:47:28 +02003769 vim_memset(sub.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
3770 vim_memset(m.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003771 }
3772 else
3773 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003774 vim_memset(sub.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
3775 vim_memset(m.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003777 sub.in_use = 0;
3778 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779
3780 if (nfa_regmatch(start, &sub, &m) == FALSE)
3781 return 0;
3782
3783 cleanup_subexpr();
3784 if (REG_MULTI)
3785 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003786 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003787 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003788 reg_startpos[i] = sub.multilist[i].start;
3789 reg_endpos[i] = sub.multilist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790 }
3791
3792 if (reg_startpos[0].lnum < 0)
3793 {
3794 reg_startpos[0].lnum = 0;
3795 reg_startpos[0].col = col;
3796 }
3797 if (reg_endpos[0].lnum < 0)
3798 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003799 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 reg_endpos[0].lnum = reglnum;
3801 reg_endpos[0].col = (int)(reginput - regline);
3802 }
3803 else
3804 /* Use line number of "\ze". */
3805 reglnum = reg_endpos[0].lnum;
3806 }
3807 else
3808 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003809 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003810 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003811 reg_startp[i] = sub.linelist[i].start;
3812 reg_endp[i] = sub.linelist[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003813 }
3814
3815 if (reg_startp[0] == NULL)
3816 reg_startp[0] = regline + col;
3817 if (reg_endp[0] == NULL)
3818 reg_endp[0] = reginput;
3819 }
3820
3821 return 1 + reglnum;
3822}
3823
3824/*
3825 * Match a regexp against a string ("line" points to the string) or multiple
3826 * lines ("line" is NULL, use reg_getline()).
3827 *
3828 * Returns 0 for failure, number of lines contained in the match otherwise.
3829 */
3830 static long
3831nfa_regexec_both(line, col)
3832 char_u *line;
3833 colnr_T col; /* column to start looking for match */
3834{
3835 nfa_regprog_T *prog;
3836 long retval = 0L;
3837 int i;
3838
3839 if (REG_MULTI)
3840 {
3841 prog = (nfa_regprog_T *)reg_mmatch->regprog;
3842 line = reg_getline((linenr_T)0); /* relative to the cursor */
3843 reg_startpos = reg_mmatch->startpos;
3844 reg_endpos = reg_mmatch->endpos;
3845 }
3846 else
3847 {
3848 prog = (nfa_regprog_T *)reg_match->regprog;
3849 reg_startp = reg_match->startp;
3850 reg_endp = reg_match->endp;
3851 }
3852
3853 /* Be paranoid... */
3854 if (prog == NULL || line == NULL)
3855 {
3856 EMSG(_(e_null));
3857 goto theend;
3858 }
3859
3860 /* If the start column is past the maximum column: no need to try. */
3861 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3862 goto theend;
3863
3864 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3865 if (prog->regflags & RF_ICASE)
3866 ireg_ic = TRUE;
3867 else if (prog->regflags & RF_NOICASE)
3868 ireg_ic = FALSE;
3869
3870#ifdef FEAT_MBYTE
3871 /* If pattern contains "\Z" overrule value of ireg_icombine */
3872 if (prog->regflags & RF_ICOMBINE)
3873 ireg_icombine = TRUE;
3874#endif
3875
3876 regline = line;
3877 reglnum = 0; /* relative to line */
3878
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003879 nfa_has_zend = prog->has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003880 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003882 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003883 for (i = 0; i < nstate; ++i)
3884 {
3885 prog->state[i].id = i;
3886 prog->state[i].lastlist = 0;
3887 prog->state[i].visits = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003888 }
3889
3890 retval = nfa_regtry(prog->start, col);
3891
3892theend:
3893 return retval;
3894}
3895
3896/*
3897 * Compile a regular expression into internal code for the NFA matcher.
3898 * Returns the program in allocated space. Returns NULL for an error.
3899 */
3900 static regprog_T *
3901nfa_regcomp(expr, re_flags)
3902 char_u *expr;
3903 int re_flags;
3904{
Bram Moolenaaraae48832013-05-25 21:18:34 +02003905 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003906 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003907 int *postfix;
3908
3909 if (expr == NULL)
3910 return NULL;
3911
3912#ifdef DEBUG
3913 nfa_regengine.expr = expr;
3914#endif
3915
3916 init_class_tab();
3917
3918 if (nfa_regcomp_start(expr, re_flags) == FAIL)
3919 return NULL;
3920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003921 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02003922 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003923 postfix = re2post();
3924 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003925 {
3926 /* TODO: only give this error for debugging? */
3927 if (post_ptr >= post_end)
3928 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003929 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003930 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003931
3932 /*
3933 * In order to build the NFA, we parse the input regexp twice:
3934 * 1. first pass to count size (so we can allocate space)
3935 * 2. second to emit code
3936 */
3937#ifdef ENABLE_LOG
3938 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003939 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003940
3941 if (f != NULL)
3942 {
3943 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
3944 fclose(f);
3945 }
3946 }
3947#endif
3948
3949 /*
3950 * PASS 1
3951 * Count number of NFA states in "nstate". Do not build the NFA.
3952 */
3953 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02003954
3955 /* Space for compiled regexp */
3956 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
3957 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
3958 if (prog == NULL)
3959 goto fail;
3960 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961 state_ptr = prog->state;
3962
3963 /*
3964 * PASS 2
3965 * Build the NFA
3966 */
3967 prog->start = post2nfa(postfix, post_ptr, FALSE);
3968 if (prog->start == NULL)
3969 goto fail;
3970
3971 prog->regflags = regflags;
3972 prog->engine = &nfa_regengine;
3973 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003974 prog->has_zend = nfa_has_zend;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003975 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003976#ifdef ENABLE_LOG
3977 nfa_postfix_dump(expr, OK);
3978 nfa_dump(prog);
3979#endif
3980
3981out:
3982 vim_free(post_start);
3983 post_start = post_ptr = post_end = NULL;
3984 state_ptr = NULL;
3985 return (regprog_T *)prog;
3986
3987fail:
3988 vim_free(prog);
3989 prog = NULL;
3990#ifdef ENABLE_LOG
3991 nfa_postfix_dump(expr, FAIL);
3992#endif
3993#ifdef DEBUG
3994 nfa_regengine.expr = NULL;
3995#endif
3996 goto out;
3997}
3998
3999
4000/*
4001 * Match a regexp against a string.
4002 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4003 * Uses curbuf for line count and 'iskeyword'.
4004 *
4005 * Return TRUE if there is a match, FALSE if not.
4006 */
4007 static int
4008nfa_regexec(rmp, line, col)
4009 regmatch_T *rmp;
4010 char_u *line; /* string to match against */
4011 colnr_T col; /* column to start looking for match */
4012{
4013 reg_match = rmp;
4014 reg_mmatch = NULL;
4015 reg_maxline = 0;
4016 reg_line_lbr = FALSE;
4017 reg_buf = curbuf;
4018 reg_win = NULL;
4019 ireg_ic = rmp->rm_ic;
4020#ifdef FEAT_MBYTE
4021 ireg_icombine = FALSE;
4022#endif
4023 ireg_maxcol = 0;
4024 return (nfa_regexec_both(line, col) != 0);
4025}
4026
4027#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4028 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4029
4030static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4031
4032/*
4033 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4034 */
4035 static int
4036nfa_regexec_nl(rmp, line, col)
4037 regmatch_T *rmp;
4038 char_u *line; /* string to match against */
4039 colnr_T col; /* column to start looking for match */
4040{
4041 reg_match = rmp;
4042 reg_mmatch = NULL;
4043 reg_maxline = 0;
4044 reg_line_lbr = TRUE;
4045 reg_buf = curbuf;
4046 reg_win = NULL;
4047 ireg_ic = rmp->rm_ic;
4048#ifdef FEAT_MBYTE
4049 ireg_icombine = FALSE;
4050#endif
4051 ireg_maxcol = 0;
4052 return (nfa_regexec_both(line, col) != 0);
4053}
4054#endif
4055
4056
4057/*
4058 * Match a regexp against multiple lines.
4059 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4060 * Uses curbuf for line count and 'iskeyword'.
4061 *
4062 * Return zero if there is no match. Return number of lines contained in the
4063 * match otherwise.
4064 *
4065 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4066 *
4067 * ! Also NOTE : match may actually be in another line. e.g.:
4068 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4069 *
4070 * +-------------------------+
4071 * |a |
4072 * |b |
4073 * |c |
4074 * | |
4075 * +-------------------------+
4076 *
4077 * then nfa_regexec_multi() returns 3. while the original
4078 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4079 *
4080 * FIXME if this behavior is not compatible.
4081 */
4082 static long
4083nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4084 regmmatch_T *rmp;
4085 win_T *win; /* window in which to search or NULL */
4086 buf_T *buf; /* buffer in which to search */
4087 linenr_T lnum; /* nr of line to start looking for match */
4088 colnr_T col; /* column to start looking for match */
4089 proftime_T *tm UNUSED; /* timeout limit or NULL */
4090{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004091 reg_match = NULL;
4092 reg_mmatch = rmp;
4093 reg_buf = buf;
4094 reg_win = win;
4095 reg_firstlnum = lnum;
4096 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4097 reg_line_lbr = FALSE;
4098 ireg_ic = rmp->rmm_ic;
4099#ifdef FEAT_MBYTE
4100 ireg_icombine = FALSE;
4101#endif
4102 ireg_maxcol = rmp->rmm_maxcol;
4103
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004104 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004105}
4106
4107#ifdef DEBUG
4108# undef ENABLE_LOG
4109#endif