blob: 17865d6d2cfe9a85a45d6974995f912a7c0a5a10 [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
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
37 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
38
39 NFA_CONCAT,
40 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020041 NFA_STAR, /* greedy * */
42 NFA_STAR_NONGREEDY, /* non-greedy * */
43 NFA_QUEST, /* greedy \? */
44 NFA_QUEST_NONGREEDY, /* non-greedy \? */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020045 NFA_NOT, /* used for [^ab] negated char ranges */
46
47 NFA_BOL, /* ^ Begin line */
48 NFA_EOL, /* $ End line */
49 NFA_BOW, /* \< Begin word */
50 NFA_EOW, /* \> End word */
51 NFA_BOF, /* \%^ Begin file */
52 NFA_EOF, /* \%$ End file */
53 NFA_NEWL,
54 NFA_ZSTART, /* Used for \zs */
55 NFA_ZEND, /* Used for \ze */
56 NFA_NOPEN, /* Start of subexpression marked with \%( */
57 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
58 NFA_START_INVISIBLE,
Bram Moolenaar61602c52013-06-01 19:54:43 +020059 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020060 NFA_END_INVISIBLE,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_COMPOSING, /* Next nodes in NFA are part of the
62 composing multibyte char */
63 NFA_END_COMPOSING, /* End of a composing char in the NFA */
64
65 /* The following are used only in the postfix form, not in the NFA */
66 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
67 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
68 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
69 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
70 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
71
Bram Moolenaar5714b802013-05-28 22:03:20 +020072 NFA_BACKREF1, /* \1 */
73 NFA_BACKREF2, /* \2 */
74 NFA_BACKREF3, /* \3 */
75 NFA_BACKREF4, /* \4 */
76 NFA_BACKREF5, /* \5 */
77 NFA_BACKREF6, /* \6 */
78 NFA_BACKREF7, /* \7 */
79 NFA_BACKREF8, /* \8 */
80 NFA_BACKREF9, /* \9 */
81 NFA_SKIP, /* Skip characters */
82
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020083 NFA_MOPEN,
84 NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
85
86 /* NFA_FIRST_NL */
87 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
88 NFA_ANYOF, /* Match any character in this string. */
89 NFA_ANYBUT, /* Match any character not in this string. */
90 NFA_IDENT, /* Match identifier char */
91 NFA_SIDENT, /* Match identifier char but no digit */
92 NFA_KWORD, /* Match keyword char */
93 NFA_SKWORD, /* Match word char but no digit */
94 NFA_FNAME, /* Match file name char */
95 NFA_SFNAME, /* Match file name char but no digit */
96 NFA_PRINT, /* Match printable char */
97 NFA_SPRINT, /* Match printable char but no digit */
98 NFA_WHITE, /* Match whitespace char */
99 NFA_NWHITE, /* Match non-whitespace char */
100 NFA_DIGIT, /* Match digit char */
101 NFA_NDIGIT, /* Match non-digit char */
102 NFA_HEX, /* Match hex char */
103 NFA_NHEX, /* Match non-hex char */
104 NFA_OCTAL, /* Match octal char */
105 NFA_NOCTAL, /* Match non-octal char */
106 NFA_WORD, /* Match word char */
107 NFA_NWORD, /* Match non-word char */
108 NFA_HEAD, /* Match head char */
109 NFA_NHEAD, /* Match non-head char */
110 NFA_ALPHA, /* Match alpha char */
111 NFA_NALPHA, /* Match non-alpha char */
112 NFA_LOWER, /* Match lowercase char */
113 NFA_NLOWER, /* Match non-lowercase char */
114 NFA_UPPER, /* Match uppercase char */
115 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200116
117 NFA_CURSOR, /* Match cursor pos */
118 NFA_LNUM, /* Match line number */
119 NFA_LNUM_GT, /* Match > line number */
120 NFA_LNUM_LT, /* Match < line number */
121 NFA_COL, /* Match cursor column */
122 NFA_COL_GT, /* Match > cursor column */
123 NFA_COL_LT, /* Match < cursor column */
124 NFA_VCOL, /* Match cursor virtual column */
125 NFA_VCOL_GT, /* Match > cursor virtual column */
126 NFA_VCOL_LT, /* Match < cursor virtual column */
127
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200128 NFA_FIRST_NL = NFA_ANY + ADD_NL,
129 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
130
131 /* Character classes [:alnum:] etc */
132 NFA_CLASS_ALNUM,
133 NFA_CLASS_ALPHA,
134 NFA_CLASS_BLANK,
135 NFA_CLASS_CNTRL,
136 NFA_CLASS_DIGIT,
137 NFA_CLASS_GRAPH,
138 NFA_CLASS_LOWER,
139 NFA_CLASS_PRINT,
140 NFA_CLASS_PUNCT,
141 NFA_CLASS_SPACE,
142 NFA_CLASS_UPPER,
143 NFA_CLASS_XDIGIT,
144 NFA_CLASS_TAB,
145 NFA_CLASS_RETURN,
146 NFA_CLASS_BACKSPACE,
147 NFA_CLASS_ESCAPE
148};
149
150/* Keep in sync with classchars. */
151static int nfa_classcodes[] = {
152 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
153 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
154 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
155 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
156 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
157 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
158 NFA_UPPER, NFA_NUPPER
159};
160
161static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
162
163/*
164 * NFA errors can be of 3 types:
165 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
166 * silently and revert the to backtracking engine.
167 * syntax_error = FALSE;
168 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
169 * The NFA engine displays an error message, and nothing else happens.
170 * syntax_error = TRUE
171 * *** Unsupported features, when the input regexp uses an operator that is not
172 * implemented in the NFA. The NFA engine fails silently, and reverts to the
173 * old backtracking engine.
174 * syntax_error = FALSE
175 * "The NFA fails" means that "compiling the regexp with the NFA fails":
176 * nfa_regcomp() returns FAIL.
177 */
178static int syntax_error = FALSE;
179
180/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200181static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200182
Bram Moolenaar428e9872013-05-30 17:05:39 +0200183/* NFA regexp \1 .. \9 encountered. */
184static int nfa_has_backref;
185
Bram Moolenaar963fee22013-05-26 21:47:28 +0200186/* Number of sub expressions actually being used during execution. 1 if only
187 * the whole match (subexpr 0) is used. */
188static int nfa_nsubexpr;
189
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200190static int *post_start; /* holds the postfix form of r.e. */
191static int *post_end;
192static int *post_ptr;
193
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200194static int nstate; /* Number of states in the NFA. Also used when
195 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200196static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200197
198
199static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
200static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
201static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200202static int nfa_regatom __ARGS((void));
203static int nfa_regpiece __ARGS((void));
204static int nfa_regconcat __ARGS((void));
205static int nfa_regbranch __ARGS((void));
206static int nfa_reg __ARGS((int paren));
207#ifdef DEBUG
208static void nfa_set_code __ARGS((int c));
209static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200210static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
211static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200212static void nfa_dump __ARGS((nfa_regprog_T *prog));
213#endif
214static int *re2post __ARGS((void));
215static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
216static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
217static int check_char_class __ARGS((int class, int c));
218static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200219static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
220static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200221static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
222static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200223static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200224static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
225static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
226static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
227static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
228static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
229
230/* helper functions used when doing re2post() ... regatom() parsing */
231#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200232 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233 return FAIL; \
234 *post_ptr++ = c; \
235 } while (0)
236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200237/*
238 * Initialize internal variables before NFA compilation.
239 * Return OK on success, FAIL otherwise.
240 */
241 static int
242nfa_regcomp_start(expr, re_flags)
243 char_u *expr;
244 int re_flags; /* see vim_regcomp() */
245{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200246 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200247 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200248
249 nstate = 0;
250 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200251 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200252 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200254 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200255 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200256 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200257
258 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200259 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200260
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200261 post_start = (int *)lalloc(postfix_size, TRUE);
262 if (post_start == NULL)
263 return FAIL;
264 vim_memset(post_start, 0, postfix_size);
265 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200266 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200267 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200268 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269
270 regcomp_start(expr, re_flags);
271
272 return OK;
273}
274
275/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200276 * Allocate more space for post_start. Called when
277 * running above the estimated number of states.
278 */
279 static int
280realloc_post_list()
281{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200282 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200283 int new_max = nstate_max + 1000;
284 int *new_start;
285 int *old_start;
286
287 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
288 if (new_start == NULL)
289 return FAIL;
290 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
291 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
292 old_start = post_start;
293 post_start = new_start;
294 post_ptr = new_start + (post_ptr - old_start);
295 post_end = post_start + new_max;
296 vim_free(old_start);
297 return OK;
298}
299
300/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200301 * Search between "start" and "end" and try to recognize a
302 * character class in expanded form. For example [0-9].
303 * On success, return the id the character class to be emitted.
304 * On failure, return 0 (=FAIL)
305 * Start points to the first char of the range, while end should point
306 * to the closing brace.
307 */
308 static int
309nfa_recognize_char_class(start, end, extra_newl)
310 char_u *start;
311 char_u *end;
312 int extra_newl;
313{
314 int i;
315 /* Each of these variables takes up a char in "config[]",
316 * in the order they are here. */
317 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
318 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
319 char_u *p;
320#define NCONFIGS 16
321 int classid[NCONFIGS] = {
322 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
323 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
324 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
325 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
326 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200327 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328 char_u config[NCONFIGS][9] = {
329 "000000100", /* digit */
330 "100000100", /* non digit */
331 "011000100", /* hex-digit */
332 "111000100", /* non hex-digit */
333 "000001000", /* octal-digit */
334 "100001000", /* [^0-7] */
335 "000110110", /* [0-9A-Za-z_] */
336 "100110110", /* [^0-9A-Za-z_] */
337 "000110010", /* head of word */
338 "100110010", /* not head of word */
339 "000110000", /* alphabetic char a-z */
340 "100110000", /* non alphabetic char */
341 "000100000", /* lowercase letter */
342 "100100000", /* non lowercase */
343 "000010000", /* uppercase */
344 "100010000" /* non uppercase */
345 };
346
347 if (extra_newl == TRUE)
348 newl = TRUE;
349
350 if (*end != ']')
351 return FAIL;
352 p = start;
353 if (*p == '^')
354 {
355 not = TRUE;
356 p ++;
357 }
358
359 while (p < end)
360 {
361 if (p + 2 < end && *(p + 1) == '-')
362 {
363 switch (*p)
364 {
365 case '0':
366 if (*(p + 2) == '9')
367 {
368 o9 = TRUE;
369 break;
370 }
371 else
372 if (*(p + 2) == '7')
373 {
374 o7 = TRUE;
375 break;
376 }
377 case 'a':
378 if (*(p + 2) == 'z')
379 {
380 az = TRUE;
381 break;
382 }
383 else
384 if (*(p + 2) == 'f')
385 {
386 af = TRUE;
387 break;
388 }
389 case 'A':
390 if (*(p + 2) == 'Z')
391 {
392 AZ = TRUE;
393 break;
394 }
395 else
396 if (*(p + 2) == 'F')
397 {
398 AF = TRUE;
399 break;
400 }
401 /* FALLTHROUGH */
402 default:
403 return FAIL;
404 }
405 p += 3;
406 }
407 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
408 {
409 newl = TRUE;
410 p += 2;
411 }
412 else if (*p == '_')
413 {
414 underscore = TRUE;
415 p ++;
416 }
417 else if (*p == '\n')
418 {
419 newl = TRUE;
420 p ++;
421 }
422 else
423 return FAIL;
424 } /* while (p < end) */
425
426 if (p != end)
427 return FAIL;
428
429 /* build the config that represents the ranges we gathered */
430 STRCPY(myconfig, "000000000");
431 if (not == TRUE)
432 myconfig[0] = '1';
433 if (af == TRUE)
434 myconfig[1] = '1';
435 if (AF == TRUE)
436 myconfig[2] = '1';
437 if (az == TRUE)
438 myconfig[3] = '1';
439 if (AZ == TRUE)
440 myconfig[4] = '1';
441 if (o7 == TRUE)
442 myconfig[5] = '1';
443 if (o9 == TRUE)
444 myconfig[6] = '1';
445 if (underscore == TRUE)
446 myconfig[7] = '1';
447 if (newl == TRUE)
448 {
449 myconfig[8] = '1';
450 extra_newl = ADD_NL;
451 }
452 /* try to recognize character classes */
453 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200454 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200455 return classid[i] + extra_newl;
456
457 /* fallthrough => no success so far */
458 return FAIL;
459
460#undef NCONFIGS
461}
462
463/*
464 * Produce the bytes for equivalence class "c".
465 * Currently only handles latin1, latin9 and utf-8.
466 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
467 * equivalent to 'a OR b OR c'
468 *
469 * NOTE! When changing this function, also update reg_equi_class()
470 */
471 static int
472nfa_emit_equi_class(c, neg)
473 int c;
474 int neg;
475{
476 int first = TRUE;
477 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
478#define EMIT2(c) \
479 EMIT(c); \
480 if (neg == TRUE) { \
481 EMIT(NFA_NOT); \
482 } \
483 if (first == FALSE) \
484 EMIT(glue); \
485 else \
486 first = FALSE; \
487
488#ifdef FEAT_MBYTE
489 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
490 || STRCMP(p_enc, "iso-8859-15") == 0)
491#endif
492 {
493 switch (c)
494 {
495 case 'A': case '\300': case '\301': case '\302':
496 case '\303': case '\304': case '\305':
497 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
498 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
499 EMIT2('\305');
500 return OK;
501
502 case 'C': case '\307':
503 EMIT2('C'); EMIT2('\307');
504 return OK;
505
506 case 'E': case '\310': case '\311': case '\312': case '\313':
507 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
508 EMIT2('\312'); EMIT2('\313');
509 return OK;
510
511 case 'I': case '\314': case '\315': case '\316': case '\317':
512 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
513 EMIT2('\316'); EMIT2('\317');
514 return OK;
515
516 case 'N': case '\321':
517 EMIT2('N'); EMIT2('\321');
518 return OK;
519
520 case 'O': case '\322': case '\323': case '\324': case '\325':
521 case '\326':
522 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
523 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
524 return OK;
525
526 case 'U': case '\331': case '\332': case '\333': case '\334':
527 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
528 EMIT2('\333'); EMIT2('\334');
529 return OK;
530
531 case 'Y': case '\335':
532 EMIT2('Y'); EMIT2('\335');
533 return OK;
534
535 case 'a': case '\340': case '\341': case '\342':
536 case '\343': case '\344': case '\345':
537 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
538 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
539 EMIT2('\345');
540 return OK;
541
542 case 'c': case '\347':
543 EMIT2('c'); EMIT2('\347');
544 return OK;
545
546 case 'e': case '\350': case '\351': case '\352': case '\353':
547 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
548 EMIT2('\352'); EMIT2('\353');
549 return OK;
550
551 case 'i': case '\354': case '\355': case '\356': case '\357':
552 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
553 EMIT2('\356'); EMIT2('\357');
554 return OK;
555
556 case 'n': case '\361':
557 EMIT2('n'); EMIT2('\361');
558 return OK;
559
560 case 'o': case '\362': case '\363': case '\364': case '\365':
561 case '\366':
562 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
563 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
564 return OK;
565
566 case 'u': case '\371': case '\372': case '\373': case '\374':
567 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
568 EMIT2('\373'); EMIT2('\374');
569 return OK;
570
571 case 'y': case '\375': case '\377':
572 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
573 return OK;
574
575 default:
576 return FAIL;
577 }
578 }
579
580 EMIT(c);
581 return OK;
582#undef EMIT2
583}
584
585/*
586 * Code to parse regular expression.
587 *
588 * We try to reuse parsing functions in regexp.c to
589 * minimize surprise and keep the syntax consistent.
590 */
591
592/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593 * Parse the lowest level.
594 *
595 * An atom can be one of a long list of items. Many atoms match one character
596 * in the text. It is often an ordinary character or a character class.
597 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
598 * is only for syntax highlighting.
599 *
600 * atom ::= ordinary-atom
601 * or \( pattern \)
602 * or \%( pattern \)
603 * or \z( pattern \)
604 */
605 static int
606nfa_regatom()
607{
608 int c;
609 int charclass;
610 int equiclass;
611 int collclass;
612 int got_coll_char;
613 char_u *p;
614 char_u *endp;
615#ifdef FEAT_MBYTE
616 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617#endif
618 int extra = 0;
619 int first;
620 int emit_range;
621 int negated;
622 int result;
623 int startc = -1;
624 int endc = -1;
625 int oldstartc = -1;
626 int cpo_lit; /* 'cpoptions' contains 'l' flag */
627 int cpo_bsl; /* 'cpoptions' contains '\' flag */
628 int glue; /* ID that will "glue" nodes together */
629
630 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
631 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
632
633 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 switch (c)
635 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200636 case NUL:
637 syntax_error = TRUE;
638 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
639
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200640 case Magic('^'):
641 EMIT(NFA_BOL);
642 break;
643
644 case Magic('$'):
645 EMIT(NFA_EOL);
646#if defined(FEAT_SYN_HL) || defined(PROTO)
647 had_eol = TRUE;
648#endif
649 break;
650
651 case Magic('<'):
652 EMIT(NFA_BOW);
653 break;
654
655 case Magic('>'):
656 EMIT(NFA_EOW);
657 break;
658
659 case Magic('_'):
660 c = no_Magic(getchr());
661 if (c == '^') /* "\_^" is start-of-line */
662 {
663 EMIT(NFA_BOL);
664 break;
665 }
666 if (c == '$') /* "\_$" is end-of-line */
667 {
668 EMIT(NFA_EOL);
669#if defined(FEAT_SYN_HL) || defined(PROTO)
670 had_eol = TRUE;
671#endif
672 break;
673 }
674
675 extra = ADD_NL;
676
677 /* "\_[" is collection plus newline */
678 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200679 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680
681 /* "\_x" is character class plus newline */
682 /*FALLTHROUGH*/
683
684 /*
685 * Character classes.
686 */
687 case Magic('.'):
688 case Magic('i'):
689 case Magic('I'):
690 case Magic('k'):
691 case Magic('K'):
692 case Magic('f'):
693 case Magic('F'):
694 case Magic('p'):
695 case Magic('P'):
696 case Magic('s'):
697 case Magic('S'):
698 case Magic('d'):
699 case Magic('D'):
700 case Magic('x'):
701 case Magic('X'):
702 case Magic('o'):
703 case Magic('O'):
704 case Magic('w'):
705 case Magic('W'):
706 case Magic('h'):
707 case Magic('H'):
708 case Magic('a'):
709 case Magic('A'):
710 case Magic('l'):
711 case Magic('L'):
712 case Magic('u'):
713 case Magic('U'):
714 p = vim_strchr(classchars, no_Magic(c));
715 if (p == NULL)
716 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200717 EMSGN("INTERNAL: Unknown character class char: %ld", c);
718 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200719 }
720#ifdef FEAT_MBYTE
721 /* When '.' is followed by a composing char ignore the dot, so that
722 * the composing char is matched here. */
723 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
724 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200725 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200726 c = getchr();
727 goto nfa_do_multibyte;
728 }
729#endif
730 EMIT(nfa_classcodes[p - classchars]);
731 if (extra == ADD_NL)
732 {
733 EMIT(NFA_NEWL);
734 EMIT(NFA_OR);
735 regflags |= RF_HASNL;
736 }
737 break;
738
739 case Magic('n'):
740 if (reg_string)
741 /* In a string "\n" matches a newline character. */
742 EMIT(NL);
743 else
744 {
745 /* In buffer text "\n" matches the end of a line. */
746 EMIT(NFA_NEWL);
747 regflags |= RF_HASNL;
748 }
749 break;
750
751 case Magic('('):
752 if (nfa_reg(REG_PAREN) == FAIL)
753 return FAIL; /* cascaded error */
754 break;
755
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200756 case Magic('|'):
757 case Magic('&'):
758 case Magic(')'):
759 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200760 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200761 return FAIL;
762
763 case Magic('='):
764 case Magic('?'):
765 case Magic('+'):
766 case Magic('@'):
767 case Magic('*'):
768 case Magic('{'):
769 /* these should follow an atom, not form an atom */
770 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200771 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200772 return FAIL;
773
774 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200775 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200776 return FAIL;
777
Bram Moolenaar428e9872013-05-30 17:05:39 +0200778 case Magic('1'):
779 case Magic('2'):
780 case Magic('3'):
781 case Magic('4'):
782 case Magic('5'):
783 case Magic('6'):
784 case Magic('7'):
785 case Magic('8'):
786 case Magic('9'):
787 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
788 nfa_has_backref = TRUE;
789 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200790
791 case Magic('z'):
792 c = no_Magic(getchr());
793 switch (c)
794 {
795 case 's':
796 EMIT(NFA_ZSTART);
797 break;
798 case 'e':
799 EMIT(NFA_ZEND);
800 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200801 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200802 case '1':
803 case '2':
804 case '3':
805 case '4':
806 case '5':
807 case '6':
808 case '7':
809 case '8':
810 case '9':
811 case '(':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200812 /* TODO: \z1...\z9 and \z( not yet supported */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200813 return FAIL;
814 default:
815 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200816 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 no_Magic(c));
818 return FAIL;
819 }
820 break;
821
822 case Magic('%'):
823 c = no_Magic(getchr());
824 switch (c)
825 {
826 /* () without a back reference */
827 case '(':
828 if (nfa_reg(REG_NPAREN) == FAIL)
829 return FAIL;
830 EMIT(NFA_NOPEN);
831 break;
832
833 case 'd': /* %d123 decimal */
834 case 'o': /* %o123 octal */
835 case 'x': /* %xab hex 2 */
836 case 'u': /* %uabcd hex 4 */
837 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200838 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200839 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200840
Bram Moolenaar47196582013-05-25 22:04:23 +0200841 switch (c)
842 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200843 case 'd': nr = getdecchrs(); break;
844 case 'o': nr = getoctchrs(); break;
845 case 'x': nr = gethexchrs(2); break;
846 case 'u': nr = gethexchrs(4); break;
847 case 'U': nr = gethexchrs(8); break;
848 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200849 }
850
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200851 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200852 EMSG2_RET_FAIL(
853 _("E678: Invalid character after %s%%[dxouU]"),
854 reg_magic == MAGIC_ALL);
855 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200856 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200857 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200858 break;
859
860 /* Catch \%^ and \%$ regardless of where they appear in the
861 * pattern -- regardless of whether or not it makes sense. */
862 case '^':
863 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200864 break;
865
866 case '$':
867 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 break;
869
870 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200871 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200872 break;
873
874 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200875 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876 return FAIL;
877 break;
878
879 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200880 /* TODO: \%[abc] not supported yet */
881 return FAIL;
882
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200883 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200884 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200885 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200886 int cmp = c;
887
888 if (c == '<' || c == '>')
889 c = getchr();
890 while (VIM_ISDIGIT(c))
891 {
892 n = n * 10 + (c - '0');
893 c = getchr();
894 }
895 if (c == 'l' || c == 'c' || c == 'v')
896 {
897 EMIT(n);
898 if (c == 'l')
899 EMIT(cmp == '<' ? NFA_LNUM_LT :
900 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
901 else if (c == 'c')
902 EMIT(cmp == '<' ? NFA_COL_LT :
903 cmp == '>' ? NFA_COL_GT : NFA_COL);
904 else
905 EMIT(cmp == '<' ? NFA_VCOL_LT :
906 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
907 break;
908 }
909 else if (c == '\'')
910 /* TODO: \%'m not supported yet */
911 return FAIL;
912 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200913 syntax_error = TRUE;
914 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
915 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200916 return FAIL;
917 }
918 break;
919
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200920 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200921collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200922 /*
923 * Glue is emitted between several atoms from the [].
924 * It is either NFA_OR, or NFA_CONCAT.
925 *
926 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
927 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
928 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
929 * notation)
930 *
931 */
932
933
934/* Emit negation atoms, if needed.
935 * The CONCAT below merges the NOT with the previous node. */
936#define TRY_NEG() \
937 if (negated == TRUE) \
938 { \
939 EMIT(NFA_NOT); \
940 }
941
942/* Emit glue between important nodes : CONCAT or OR. */
943#define EMIT_GLUE() \
944 if (first == FALSE) \
945 EMIT(glue); \
946 else \
947 first = FALSE;
948
949 p = regparse;
950 endp = skip_anyof(p);
951 if (*endp == ']')
952 {
953 /*
954 * Try to reverse engineer character classes. For example,
955 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
956 * and perform the necessary substitutions in the NFA.
957 */
958 result = nfa_recognize_char_class(regparse, endp,
959 extra == ADD_NL);
960 if (result != FAIL)
961 {
962 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
963 EMIT(result);
964 else /* must be char class + newline */
965 {
966 EMIT(result - ADD_NL);
967 EMIT(NFA_NEWL);
968 EMIT(NFA_OR);
969 }
970 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200971 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200972 return OK;
973 }
974 /*
975 * Failed to recognize a character class. Use the simple
976 * version that turns [abc] into 'a' OR 'b' OR 'c'
977 */
978 startc = endc = oldstartc = -1;
979 first = TRUE; /* Emitting first atom in this sequence? */
980 negated = FALSE;
981 glue = NFA_OR;
982 if (*regparse == '^') /* negated range */
983 {
984 negated = TRUE;
985 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +0200986 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200987 }
988 if (*regparse == '-')
989 {
990 startc = '-';
991 EMIT(startc);
992 TRY_NEG();
993 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +0200994 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200995 }
996 /* Emit the OR branches for each character in the [] */
997 emit_range = FALSE;
998 while (regparse < endp)
999 {
1000 oldstartc = startc;
1001 startc = -1;
1002 got_coll_char = FALSE;
1003 if (*regparse == '[')
1004 {
1005 /* Check for [: :], [= =], [. .] */
1006 equiclass = collclass = 0;
1007 charclass = get_char_class(&regparse);
1008 if (charclass == CLASS_NONE)
1009 {
1010 equiclass = get_equi_class(&regparse);
1011 if (equiclass == 0)
1012 collclass = get_coll_element(&regparse);
1013 }
1014
1015 /* Character class like [:alpha:] */
1016 if (charclass != CLASS_NONE)
1017 {
1018 switch (charclass)
1019 {
1020 case CLASS_ALNUM:
1021 EMIT(NFA_CLASS_ALNUM);
1022 break;
1023 case CLASS_ALPHA:
1024 EMIT(NFA_CLASS_ALPHA);
1025 break;
1026 case CLASS_BLANK:
1027 EMIT(NFA_CLASS_BLANK);
1028 break;
1029 case CLASS_CNTRL:
1030 EMIT(NFA_CLASS_CNTRL);
1031 break;
1032 case CLASS_DIGIT:
1033 EMIT(NFA_CLASS_DIGIT);
1034 break;
1035 case CLASS_GRAPH:
1036 EMIT(NFA_CLASS_GRAPH);
1037 break;
1038 case CLASS_LOWER:
1039 EMIT(NFA_CLASS_LOWER);
1040 break;
1041 case CLASS_PRINT:
1042 EMIT(NFA_CLASS_PRINT);
1043 break;
1044 case CLASS_PUNCT:
1045 EMIT(NFA_CLASS_PUNCT);
1046 break;
1047 case CLASS_SPACE:
1048 EMIT(NFA_CLASS_SPACE);
1049 break;
1050 case CLASS_UPPER:
1051 EMIT(NFA_CLASS_UPPER);
1052 break;
1053 case CLASS_XDIGIT:
1054 EMIT(NFA_CLASS_XDIGIT);
1055 break;
1056 case CLASS_TAB:
1057 EMIT(NFA_CLASS_TAB);
1058 break;
1059 case CLASS_RETURN:
1060 EMIT(NFA_CLASS_RETURN);
1061 break;
1062 case CLASS_BACKSPACE:
1063 EMIT(NFA_CLASS_BACKSPACE);
1064 break;
1065 case CLASS_ESCAPE:
1066 EMIT(NFA_CLASS_ESCAPE);
1067 break;
1068 }
1069 TRY_NEG();
1070 EMIT_GLUE();
1071 continue;
1072 }
1073 /* Try equivalence class [=a=] and the like */
1074 if (equiclass != 0)
1075 {
1076 result = nfa_emit_equi_class(equiclass, negated);
1077 if (result == FAIL)
1078 {
1079 /* should never happen */
1080 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1081 }
1082 EMIT_GLUE();
1083 continue;
1084 }
1085 /* Try collating class like [. .] */
1086 if (collclass != 0)
1087 {
1088 startc = collclass; /* allow [.a.]-x as a range */
1089 /* Will emit the proper atom at the end of the
1090 * while loop. */
1091 }
1092 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001093 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1094 * start character. */
1095 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001096 {
1097 emit_range = TRUE;
1098 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001099 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001100 continue; /* reading the end of the range */
1101 }
1102
1103 /* Now handle simple and escaped characters.
1104 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1105 * accepts "\t", "\e", etc., but only when the 'l' flag in
1106 * 'cpoptions' is not included.
1107 * Posix doesn't recognize backslash at all.
1108 */
1109 if (*regparse == '\\'
1110 && !cpo_bsl
1111 && regparse + 1 <= endp
1112 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1113 || (!cpo_lit
1114 && vim_strchr(REGEXP_ABBR, regparse[1])
1115 != NULL)
1116 )
1117 )
1118 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001119 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001121 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001122 startc = reg_string ? NL : NFA_NEWL;
1123 else
1124 if (*regparse == 'd'
1125 || *regparse == 'o'
1126 || *regparse == 'x'
1127 || *regparse == 'u'
1128 || *regparse == 'U'
1129 )
1130 {
1131 /* TODO(RE) This needs more testing */
1132 startc = coll_get_char();
1133 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001134 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001135 }
1136 else
1137 {
1138 /* \r,\t,\e,\b */
1139 startc = backslash_trans(*regparse);
1140 }
1141 }
1142
1143 /* Normal printable char */
1144 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001145 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001146
1147 /* Previous char was '-', so this char is end of range. */
1148 if (emit_range)
1149 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001150 endc = startc;
1151 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001152 if (startc > endc)
1153 EMSG_RET_FAIL(_(e_invrange));
1154#ifdef FEAT_MBYTE
1155 if (has_mbyte && ((*mb_char2len)(startc) > 1
1156 || (*mb_char2len)(endc) > 1))
1157 {
1158 if (endc > startc + 256)
1159 EMSG_RET_FAIL(_(e_invrange));
1160 /* Emit the range. "startc" was already emitted, so
1161 * skip it. */
1162 for (c = startc + 1; c <= endc; c++)
1163 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001164 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001165 TRY_NEG();
1166 EMIT_GLUE();
1167 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001168 }
1169 else
1170#endif
1171 {
1172#ifdef EBCDIC
1173 int alpha_only = FALSE;
1174
1175 /* for alphabetical range skip the gaps
1176 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1177 if (isalpha(startc) && isalpha(endc))
1178 alpha_only = TRUE;
1179#endif
1180 /* Emit the range. "startc" was already emitted, so
1181 * skip it. */
1182 for (c = startc + 1; c <= endc; c++)
1183#ifdef EBCDIC
1184 if (!alpha_only || isalpha(startc))
1185#endif
1186 {
1187 EMIT(c);
1188 TRY_NEG();
1189 EMIT_GLUE();
1190 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001191 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001192 emit_range = FALSE;
1193 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001194 }
1195 else
1196 {
1197 /*
1198 * This char (startc) is not part of a range. Just
1199 * emit it.
1200 *
1201 * Normally, simply emit startc. But if we get char
1202 * code=0 from a collating char, then replace it with
1203 * 0x0a.
1204 *
1205 * This is needed to completely mimic the behaviour of
1206 * the backtracking engine.
1207 */
1208 if (got_coll_char == TRUE && startc == 0)
1209 EMIT(0x0a);
1210 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001211 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 TRY_NEG();
1213 EMIT_GLUE();
1214 }
1215
Bram Moolenaar51a29832013-05-28 22:30:35 +02001216 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217 } /* while (p < endp) */
1218
Bram Moolenaar51a29832013-05-28 22:30:35 +02001219 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001220 if (*regparse == '-') /* if last, '-' is just a char */
1221 {
1222 EMIT('-');
1223 TRY_NEG();
1224 EMIT_GLUE();
1225 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001226 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228 /* skip the trailing ] */
1229 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001230 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001231 if (negated == TRUE)
1232 {
1233 /* Mark end of negated char range */
1234 EMIT(NFA_END_NEG_RANGE);
1235 EMIT(NFA_CONCAT);
1236 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001237
1238 /* \_[] also matches \n but it's not negated */
1239 if (extra == ADD_NL)
1240 {
1241 EMIT(reg_string ? NL : NFA_NEWL);
1242 EMIT(NFA_OR);
1243 }
1244
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001246 } /* if exists closing ] */
1247
1248 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001249 {
1250 syntax_error = TRUE;
1251 EMSG_RET_FAIL(_(e_missingbracket));
1252 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001253 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001254
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 default:
1256 {
1257#ifdef FEAT_MBYTE
1258 int plen;
1259
1260nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001261 /* plen is length of current char with composing chars */
1262 if (enc_utf8 && ((*mb_char2len)(c)
1263 != (plen = (*mb_ptr2len)(old_regparse))
1264 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001265 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001266 int i = 0;
1267
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001268 /* A base character plus composing characters, or just one
1269 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001270 * This requires creating a separate atom as if enclosing
1271 * the characters in (), where NFA_COMPOSING is the ( and
1272 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 * building the postfix form, not the NFA itself;
1274 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001275 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001276 for (;;)
1277 {
1278 EMIT(c);
1279 if (i > 0)
1280 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001281 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001282 break;
1283 c = utf_ptr2char(old_regparse + i);
1284 }
1285 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001286 regparse = old_regparse + plen;
1287 }
1288 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001289#endif
1290 {
1291 c = no_Magic(c);
1292 EMIT(c);
1293 }
1294 return OK;
1295 }
1296 }
1297
1298#undef TRY_NEG
1299#undef EMIT_GLUE
1300
1301 return OK;
1302}
1303
1304/*
1305 * Parse something followed by possible [*+=].
1306 *
1307 * A piece is an atom, possibly followed by a multi, an indication of how many
1308 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1309 * characters: "", "a", "aa", etc.
1310 *
1311 * piece ::= atom
1312 * or atom multi
1313 */
1314 static int
1315nfa_regpiece()
1316{
1317 int i;
1318 int op;
1319 int ret;
1320 long minval, maxval;
1321 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001322 parse_state_T old_state;
1323 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001324 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001325 int old_post_pos;
1326 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001327 int quest;
1328
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001329 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1330 * next. */
1331 save_parse_state(&old_state);
1332
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001333 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001334 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001335
1336 ret = nfa_regatom();
1337 if (ret == FAIL)
1338 return FAIL; /* cascaded error */
1339
1340 op = peekchr();
1341 if (re_multi_type(op) == NOT_MULTI)
1342 return OK;
1343
1344 skipchr();
1345 switch (op)
1346 {
1347 case Magic('*'):
1348 EMIT(NFA_STAR);
1349 break;
1350
1351 case Magic('+'):
1352 /*
1353 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1354 * first and only submatch would be "aaa". But the backtracking
1355 * engine interprets the plus as "try matching one more time", and
1356 * a* matches a second time at the end of the input, the empty
1357 * string.
1358 * The submatch will the empty string.
1359 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001360 * In order to be consistent with the old engine, we replace
1361 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001363 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 curchr = -1;
1365 if (nfa_regatom() == FAIL)
1366 return FAIL;
1367 EMIT(NFA_STAR);
1368 EMIT(NFA_CONCAT);
1369 skipchr(); /* skip the \+ */
1370 break;
1371
1372 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001373 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001374 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001375 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001376 switch(op)
1377 {
1378 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001379 /* \@= */
1380 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001381 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001382 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001383 /* \@! */
1384 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001385 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001387 op = no_Magic(getchr());
1388 if (op == '=')
1389 /* \@<= */
1390 i = NFA_PREV_ATOM_JUST_BEFORE;
1391 else if (op == '!')
1392 /* \@<! */
1393 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1394 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001395 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001396 /* \@> Not supported yet */
1397 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001398 return FAIL;
1399 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001400 if (i == 0)
1401 {
1402 syntax_error = TRUE;
1403 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1404 return FAIL;
1405 }
1406 EMIT(i);
1407 if (i == NFA_PREV_ATOM_JUST_BEFORE
1408 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1409 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001410 break;
1411
1412 case Magic('?'):
1413 case Magic('='):
1414 EMIT(NFA_QUEST);
1415 break;
1416
1417 case Magic('{'):
1418 /* a{2,5} will expand to 'aaa?a?a?'
1419 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1420 * version of '?'
1421 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1422 * parenthesis have the same id
1423 */
1424
1425 greedy = TRUE;
1426 c2 = peekchr();
1427 if (c2 == '-' || c2 == Magic('-'))
1428 {
1429 skipchr();
1430 greedy = FALSE;
1431 }
1432 if (!read_limits(&minval, &maxval))
1433 {
1434 syntax_error = TRUE;
1435 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1436 }
1437 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1438 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001439 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001441 if (greedy)
1442 /* \{}, \{0,} */
1443 EMIT(NFA_STAR);
1444 else
1445 /* \{-}, \{-0,} */
1446 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 break;
1448 }
1449
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 /* Special case: x{0} or x{-0} */
1451 if (maxval == 0)
1452 {
1453 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001454 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001455 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1456 EMIT(NFA_SKIP_CHAR);
1457 return OK;
1458 }
1459
1460 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001461 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001462 /* Save parse state after the repeated atom and the \{} */
1463 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1466 for (i = 0; i < maxval; i++)
1467 {
1468 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001469 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001470 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001471 if (nfa_regatom() == FAIL)
1472 return FAIL;
1473 /* after "minval" times, atoms are optional */
1474 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001475 {
1476 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001477 {
1478 if (greedy)
1479 EMIT(NFA_STAR);
1480 else
1481 EMIT(NFA_STAR_NONGREEDY);
1482 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001483 else
1484 EMIT(quest);
1485 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001486 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001488 if (i + 1 > minval && maxval == MAX_LIMIT)
1489 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001490 }
1491
1492 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001493 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 curchr = -1;
1495
1496 break;
1497
1498
1499 default:
1500 break;
1501 } /* end switch */
1502
1503 if (re_multi_type(peekchr()) != NOT_MULTI)
1504 {
1505 /* Can't have a multi follow a multi. */
1506 syntax_error = TRUE;
1507 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1508 }
1509
1510 return OK;
1511}
1512
1513/*
1514 * Parse one or more pieces, concatenated. It matches a match for the
1515 * first piece, followed by a match for the second piece, etc. Example:
1516 * "f[0-9]b", first matches "f", then a digit and then "b".
1517 *
1518 * concat ::= piece
1519 * or piece piece
1520 * or piece piece piece
1521 * etc.
1522 */
1523 static int
1524nfa_regconcat()
1525{
1526 int cont = TRUE;
1527 int first = TRUE;
1528
1529 while (cont)
1530 {
1531 switch (peekchr())
1532 {
1533 case NUL:
1534 case Magic('|'):
1535 case Magic('&'):
1536 case Magic(')'):
1537 cont = FALSE;
1538 break;
1539
1540 case Magic('Z'):
1541#ifdef FEAT_MBYTE
1542 regflags |= RF_ICOMBINE;
1543#endif
1544 skipchr_keepstart();
1545 break;
1546 case Magic('c'):
1547 regflags |= RF_ICASE;
1548 skipchr_keepstart();
1549 break;
1550 case Magic('C'):
1551 regflags |= RF_NOICASE;
1552 skipchr_keepstart();
1553 break;
1554 case Magic('v'):
1555 reg_magic = MAGIC_ALL;
1556 skipchr_keepstart();
1557 curchr = -1;
1558 break;
1559 case Magic('m'):
1560 reg_magic = MAGIC_ON;
1561 skipchr_keepstart();
1562 curchr = -1;
1563 break;
1564 case Magic('M'):
1565 reg_magic = MAGIC_OFF;
1566 skipchr_keepstart();
1567 curchr = -1;
1568 break;
1569 case Magic('V'):
1570 reg_magic = MAGIC_NONE;
1571 skipchr_keepstart();
1572 curchr = -1;
1573 break;
1574
1575 default:
1576 if (nfa_regpiece() == FAIL)
1577 return FAIL;
1578 if (first == FALSE)
1579 EMIT(NFA_CONCAT);
1580 else
1581 first = FALSE;
1582 break;
1583 }
1584 }
1585
1586 return OK;
1587}
1588
1589/*
1590 * Parse a branch, one or more concats, separated by "\&". It matches the
1591 * last concat, but only if all the preceding concats also match at the same
1592 * position. Examples:
1593 * "foobeep\&..." matches "foo" in "foobeep".
1594 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1595 *
1596 * branch ::= concat
1597 * or concat \& concat
1598 * or concat \& concat \& concat
1599 * etc.
1600 */
1601 static int
1602nfa_regbranch()
1603{
1604 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001605 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001606
Bram Moolenaar16299b52013-05-30 18:45:23 +02001607 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001608
1609 /* First branch, possibly the only one */
1610 if (nfa_regconcat() == FAIL)
1611 return FAIL;
1612
1613 ch = peekchr();
1614 /* Try next concats */
1615 while (ch == Magic('&'))
1616 {
1617 skipchr();
1618 EMIT(NFA_NOPEN);
1619 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001620 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001621 if (nfa_regconcat() == FAIL)
1622 return FAIL;
1623 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001624 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001625 EMIT(NFA_SKIP_CHAR);
1626 EMIT(NFA_CONCAT);
1627 ch = peekchr();
1628 }
1629
1630 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001631 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001632 EMIT(NFA_SKIP_CHAR);
1633
1634 return OK;
1635}
1636
1637/*
1638 * Parse a pattern, one or more branches, separated by "\|". It matches
1639 * anything that matches one of the branches. Example: "foo\|beep" matches
1640 * "foo" and matches "beep". If more than one branch matches, the first one
1641 * is used.
1642 *
1643 * pattern ::= branch
1644 * or branch \| branch
1645 * or branch \| branch \| branch
1646 * etc.
1647 */
1648 static int
1649nfa_reg(paren)
1650 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1651{
1652 int parno = 0;
1653
1654#ifdef FEAT_SYN_HL
1655#endif
1656 if (paren == REG_PAREN)
1657 {
1658 if (regnpar >= NSUBEXP) /* Too many `(' */
1659 {
1660 syntax_error = TRUE;
1661 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1662 }
1663 parno = regnpar++;
1664 }
1665
1666 if (nfa_regbranch() == FAIL)
1667 return FAIL; /* cascaded error */
1668
1669 while (peekchr() == Magic('|'))
1670 {
1671 skipchr();
1672 if (nfa_regbranch() == FAIL)
1673 return FAIL; /* cascaded error */
1674 EMIT(NFA_OR);
1675 }
1676
1677 /* Check for proper termination. */
1678 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1679 {
1680 syntax_error = TRUE;
1681 if (paren == REG_NPAREN)
1682 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1683 else
1684 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1685 }
1686 else if (paren == REG_NOPAREN && peekchr() != NUL)
1687 {
1688 syntax_error = TRUE;
1689 if (peekchr() == Magic(')'))
1690 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1691 else
1692 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1693 }
1694 /*
1695 * Here we set the flag allowing back references to this set of
1696 * parentheses.
1697 */
1698 if (paren == REG_PAREN)
1699 {
1700 had_endbrace[parno] = TRUE; /* have seen the close paren */
1701 EMIT(NFA_MOPEN + parno);
1702 }
1703
1704 return OK;
1705}
1706
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707#ifdef DEBUG
1708static char_u code[50];
1709
1710 static void
1711nfa_set_code(c)
1712 int c;
1713{
1714 int addnl = FALSE;
1715
1716 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1717 {
1718 addnl = TRUE;
1719 c -= ADD_NL;
1720 }
1721
1722 STRCPY(code, "");
1723 switch (c)
1724 {
1725 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1726 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1727 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1728 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1729 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1730 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1731
Bram Moolenaar5714b802013-05-28 22:03:20 +02001732 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1733 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1734 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1735 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1736 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1737 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1738 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1739 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1740 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
1741 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1742
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743 case NFA_PREV_ATOM_NO_WIDTH:
1744 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001745 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1746 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001747 case NFA_PREV_ATOM_JUST_BEFORE:
1748 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1749 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1750 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001751 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1752 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001753 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001754 case NFA_START_INVISIBLE_BEFORE:
1755 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001756 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1757
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001758 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1759 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1760
1761 case NFA_MOPEN + 0:
1762 case NFA_MOPEN + 1:
1763 case NFA_MOPEN + 2:
1764 case NFA_MOPEN + 3:
1765 case NFA_MOPEN + 4:
1766 case NFA_MOPEN + 5:
1767 case NFA_MOPEN + 6:
1768 case NFA_MOPEN + 7:
1769 case NFA_MOPEN + 8:
1770 case NFA_MOPEN + 9:
1771 STRCPY(code, "NFA_MOPEN(x)");
1772 code[10] = c - NFA_MOPEN + '0';
1773 break;
1774 case NFA_MCLOSE + 0:
1775 case NFA_MCLOSE + 1:
1776 case NFA_MCLOSE + 2:
1777 case NFA_MCLOSE + 3:
1778 case NFA_MCLOSE + 4:
1779 case NFA_MCLOSE + 5:
1780 case NFA_MCLOSE + 6:
1781 case NFA_MCLOSE + 7:
1782 case NFA_MCLOSE + 8:
1783 case NFA_MCLOSE + 9:
1784 STRCPY(code, "NFA_MCLOSE(x)");
1785 code[11] = c - NFA_MCLOSE + '0';
1786 break;
1787 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1788 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1789 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1790 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001791 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1792 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001793 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001794 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1795 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1796 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1798 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1799 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1801 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1802 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1803 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1804 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1805 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1806 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1807 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1808 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1809 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1810 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1811 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1812 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1813 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1814 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1815 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1816 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1817
1818 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1819 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1820 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1821 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1822 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1823 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1824 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1825 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1826 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1827 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1828 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1829 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1830 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1831 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1832 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1833 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1834 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1835 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1836 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1837 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1838 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1839 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1840 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1841 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1842 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1843 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1844 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1845
1846 default:
1847 STRCPY(code, "CHAR(x)");
1848 code[5] = c;
1849 }
1850
1851 if (addnl == TRUE)
1852 STRCAT(code, " + NEWLINE ");
1853
1854}
1855
1856#ifdef ENABLE_LOG
1857static FILE *log_fd;
1858
1859/*
1860 * Print the postfix notation of the current regexp.
1861 */
1862 static void
1863nfa_postfix_dump(expr, retval)
1864 char_u *expr;
1865 int retval;
1866{
1867 int *p;
1868 FILE *f;
1869
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001870 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 if (f != NULL)
1872 {
1873 fprintf(f, "\n-------------------------\n");
1874 if (retval == FAIL)
1875 fprintf(f, ">>> NFA engine failed ... \n");
1876 else if (retval == OK)
1877 fprintf(f, ">>> NFA engine succeeded !\n");
1878 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02001879 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 {
1881 nfa_set_code(*p);
1882 fprintf(f, "%s, ", code);
1883 }
1884 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02001885 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886 fprintf(f, "%d ", *p);
1887 fprintf(f, "\n\n");
1888 fclose(f);
1889 }
1890}
1891
1892/*
1893 * Print the NFA starting with a root node "state".
1894 */
1895 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02001896nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 FILE *debugf;
1898 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899{
Bram Moolenaar152e7892013-05-25 12:28:11 +02001900 garray_T indent;
1901
1902 ga_init2(&indent, 1, 64);
1903 ga_append(&indent, '\0');
1904 nfa_print_state2(debugf, state, &indent);
1905 ga_clear(&indent);
1906}
1907
1908 static void
1909nfa_print_state2(debugf, state, indent)
1910 FILE *debugf;
1911 nfa_state_T *state;
1912 garray_T *indent;
1913{
1914 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915
1916 if (state == NULL)
1917 return;
1918
1919 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02001920
1921 /* Output indent */
1922 p = (char_u *)indent->ga_data;
1923 if (indent->ga_len >= 3)
1924 {
1925 int last = indent->ga_len - 3;
1926 char_u save[2];
1927
1928 STRNCPY(save, &p[last], 2);
1929 STRNCPY(&p[last], "+-", 2);
1930 fprintf(debugf, " %s", p);
1931 STRNCPY(&p[last], save, 2);
1932 }
1933 else
1934 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935
1936 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02001937 fprintf(debugf, "%s%s (%d) (id=%d)\n",
1938 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 if (state->id < 0)
1940 return;
1941
1942 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02001943
1944 /* grow indent for state->out */
1945 indent->ga_len -= 1;
1946 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001947 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001948 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001949 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001950 ga_append(indent, '\0');
1951
1952 nfa_print_state2(debugf, state->out, indent);
1953
1954 /* replace last part of indent for state->out1 */
1955 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02001956 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02001957 ga_append(indent, '\0');
1958
1959 nfa_print_state2(debugf, state->out1, indent);
1960
1961 /* shrink indent */
1962 indent->ga_len -= 3;
1963 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964}
1965
1966/*
1967 * Print the NFA state machine.
1968 */
1969 static void
1970nfa_dump(prog)
1971 nfa_regprog_T *prog;
1972{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001973 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001974
1975 if (debugf != NULL)
1976 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02001977 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001978 fclose(debugf);
1979 }
1980}
1981#endif /* ENABLE_LOG */
1982#endif /* DEBUG */
1983
1984/*
1985 * Parse r.e. @expr and convert it into postfix form.
1986 * Return the postfix string on success, NULL otherwise.
1987 */
1988 static int *
1989re2post()
1990{
1991 if (nfa_reg(REG_NOPAREN) == FAIL)
1992 return NULL;
1993 EMIT(NFA_MOPEN);
1994 return post_start;
1995}
1996
1997/* NB. Some of the code below is inspired by Russ's. */
1998
1999/*
2000 * Represents an NFA state plus zero or one or two arrows exiting.
2001 * if c == MATCH, no arrows out; matching state.
2002 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2003 * If c < 256, labeled arrow with character c to out.
2004 */
2005
2006static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2007
2008/*
2009 * Allocate and initialize nfa_state_T.
2010 */
2011 static nfa_state_T *
2012new_state(c, out, out1)
2013 int c;
2014 nfa_state_T *out;
2015 nfa_state_T *out1;
2016{
2017 nfa_state_T *s;
2018
2019 if (istate >= nstate)
2020 return NULL;
2021
2022 s = &state_ptr[istate++];
2023
2024 s->c = c;
2025 s->out = out;
2026 s->out1 = out1;
2027
2028 s->id = istate;
2029 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030 s->negated = FALSE;
2031
2032 return s;
2033}
2034
2035/*
2036 * A partially built NFA without the matching state filled in.
2037 * Frag_T.start points at the start state.
2038 * Frag_T.out is a list of places that need to be set to the
2039 * next state for this fragment.
2040 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002041
2042/* Since the out pointers in the list are always
2043 * uninitialized, we use the pointers themselves
2044 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002045typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002046union Ptrlist
2047{
2048 Ptrlist *next;
2049 nfa_state_T *s;
2050};
2051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002052struct Frag
2053{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002054 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002055 Ptrlist *out;
2056};
2057typedef struct Frag Frag_T;
2058
2059static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2060static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2061static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2062static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2063static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2064static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2065
2066/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002067 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 */
2069 static Frag_T
2070frag(start, out)
2071 nfa_state_T *start;
2072 Ptrlist *out;
2073{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002074 Frag_T n;
2075
2076 n.start = start;
2077 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078 return n;
2079}
2080
2081/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 * Create singleton list containing just outp.
2083 */
2084 static Ptrlist *
2085list1(outp)
2086 nfa_state_T **outp;
2087{
2088 Ptrlist *l;
2089
2090 l = (Ptrlist *)outp;
2091 l->next = NULL;
2092 return l;
2093}
2094
2095/*
2096 * Patch the list of states at out to point to start.
2097 */
2098 static void
2099patch(l, s)
2100 Ptrlist *l;
2101 nfa_state_T *s;
2102{
2103 Ptrlist *next;
2104
2105 for (; l; l = next)
2106 {
2107 next = l->next;
2108 l->s = s;
2109 }
2110}
2111
2112
2113/*
2114 * Join the two lists l1 and l2, returning the combination.
2115 */
2116 static Ptrlist *
2117append(l1, l2)
2118 Ptrlist *l1;
2119 Ptrlist *l2;
2120{
2121 Ptrlist *oldl1;
2122
2123 oldl1 = l1;
2124 while (l1->next)
2125 l1 = l1->next;
2126 l1->next = l2;
2127 return oldl1;
2128}
2129
2130/*
2131 * Stack used for transforming postfix form into NFA.
2132 */
2133static Frag_T empty;
2134
2135 static void
2136st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002137 int *postfix UNUSED;
2138 int *end UNUSED;
2139 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002141#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002142 FILE *df;
2143 int *p2;
2144
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002145 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002146 if (df)
2147 {
2148 fprintf(df, "Error popping the stack!\n");
2149#ifdef DEBUG
2150 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2151#endif
2152 fprintf(df, "Postfix form is: ");
2153#ifdef DEBUG
2154 for (p2 = postfix; p2 < end; p2++)
2155 {
2156 nfa_set_code(*p2);
2157 fprintf(df, "%s, ", code);
2158 }
2159 nfa_set_code(*p);
2160 fprintf(df, "\nCurrent position is: ");
2161 for (p2 = postfix; p2 <= p; p2 ++)
2162 {
2163 nfa_set_code(*p2);
2164 fprintf(df, "%s, ", code);
2165 }
2166#else
2167 for (p2 = postfix; p2 < end; p2++)
2168 {
2169 fprintf(df, "%d, ", *p2);
2170 }
2171 fprintf(df, "\nCurrent position is: ");
2172 for (p2 = postfix; p2 <= p; p2 ++)
2173 {
2174 fprintf(df, "%d, ", *p2);
2175 }
2176#endif
2177 fprintf(df, "\n--------------------------\n");
2178 fclose(df);
2179 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002180#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181 EMSG(_("E874: (NFA) Could not pop the stack !"));
2182}
2183
2184/*
2185 * Push an item onto the stack.
2186 */
2187 static void
2188st_push(s, p, stack_end)
2189 Frag_T s;
2190 Frag_T **p;
2191 Frag_T *stack_end;
2192{
2193 Frag_T *stackp = *p;
2194
2195 if (stackp >= stack_end)
2196 return;
2197 *stackp = s;
2198 *p = *p + 1;
2199}
2200
2201/*
2202 * Pop an item from the stack.
2203 */
2204 static Frag_T
2205st_pop(p, stack)
2206 Frag_T **p;
2207 Frag_T *stack;
2208{
2209 Frag_T *stackp;
2210
2211 *p = *p - 1;
2212 stackp = *p;
2213 if (stackp < stack)
2214 return empty;
2215 return **p;
2216}
2217
2218/*
2219 * Convert a postfix form into its equivalent NFA.
2220 * Return the NFA start state on success, NULL otherwise.
2221 */
2222 static nfa_state_T *
2223post2nfa(postfix, end, nfa_calc_size)
2224 int *postfix;
2225 int *end;
2226 int nfa_calc_size;
2227{
2228 int *p;
2229 int mopen;
2230 int mclose;
2231 Frag_T *stack = NULL;
2232 Frag_T *stackp = NULL;
2233 Frag_T *stack_end = NULL;
2234 Frag_T e1;
2235 Frag_T e2;
2236 Frag_T e;
2237 nfa_state_T *s;
2238 nfa_state_T *s1;
2239 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002240 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002241
2242 if (postfix == NULL)
2243 return NULL;
2244
Bram Moolenaar053bb602013-05-20 13:55:21 +02002245#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246#define POP() st_pop(&stackp, stack); \
2247 if (stackp < stack) \
2248 { \
2249 st_error(postfix, end, p); \
2250 return NULL; \
2251 }
2252
2253 if (nfa_calc_size == FALSE)
2254 {
2255 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002256 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002258 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 }
2260
2261 for (p = postfix; p < end; ++p)
2262 {
2263 switch (*p)
2264 {
2265 case NFA_CONCAT:
2266 /* Catenation.
2267 * Pay attention: this operator does not exist
2268 * in the r.e. itself (it is implicit, really).
2269 * It is added when r.e. is translated to postfix
2270 * form in re2post().
2271 *
2272 * No new state added here. */
2273 if (nfa_calc_size == TRUE)
2274 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002275 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276 break;
2277 }
2278 e2 = POP();
2279 e1 = POP();
2280 patch(e1.out, e2.start);
2281 PUSH(frag(e1.start, e2.out));
2282 break;
2283
2284 case NFA_NOT:
2285 /* Negation of a character */
2286 if (nfa_calc_size == TRUE)
2287 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002288 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 break;
2290 }
2291 e1 = POP();
2292 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002293#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002294 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002296#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297 PUSH(e1);
2298 break;
2299
2300 case NFA_OR:
2301 /* Alternation */
2302 if (nfa_calc_size == TRUE)
2303 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002304 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002305 break;
2306 }
2307 e2 = POP();
2308 e1 = POP();
2309 s = new_state(NFA_SPLIT, e1.start, e2.start);
2310 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002311 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002312 PUSH(frag(s, append(e1.out, e2.out)));
2313 break;
2314
2315 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002316 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317 if (nfa_calc_size == TRUE)
2318 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002319 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320 break;
2321 }
2322 e = POP();
2323 s = new_state(NFA_SPLIT, e.start, NULL);
2324 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002325 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326 patch(e.out, s);
2327 PUSH(frag(s, list1(&s->out1)));
2328 break;
2329
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002330 case NFA_STAR_NONGREEDY:
2331 /* Zero or more, prefer zero */
2332 if (nfa_calc_size == TRUE)
2333 {
2334 nstate++;
2335 break;
2336 }
2337 e = POP();
2338 s = new_state(NFA_SPLIT, NULL, e.start);
2339 if (s == NULL)
2340 goto theend;
2341 patch(e.out, s);
2342 PUSH(frag(s, list1(&s->out)));
2343 break;
2344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345 case NFA_QUEST:
2346 /* one or zero atoms=> greedy match */
2347 if (nfa_calc_size == TRUE)
2348 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002349 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002350 break;
2351 }
2352 e = POP();
2353 s = new_state(NFA_SPLIT, e.start, NULL);
2354 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002355 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002356 PUSH(frag(s, append(e.out, list1(&s->out1))));
2357 break;
2358
2359 case NFA_QUEST_NONGREEDY:
2360 /* zero or one atoms => non-greedy match */
2361 if (nfa_calc_size == TRUE)
2362 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002363 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364 break;
2365 }
2366 e = POP();
2367 s = new_state(NFA_SPLIT, NULL, e.start);
2368 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002369 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 PUSH(frag(s, append(e.out, list1(&s->out))));
2371 break;
2372
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002373 case NFA_SKIP_CHAR:
2374 /* Symbol of 0-length, Used in a repetition
2375 * with max/min count of 0 */
2376 if (nfa_calc_size == TRUE)
2377 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002378 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379 break;
2380 }
2381 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2382 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002383 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384 PUSH(frag(s, list1(&s->out)));
2385 break;
2386
2387 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002388 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002389 case NFA_PREV_ATOM_JUST_BEFORE:
2390 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002391 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002392 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002393 * The \@<= operator: match for the preceding atom.
2394 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002396 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002397
2398 if (nfa_calc_size == TRUE)
2399 {
2400 nstate += 2;
2401 break;
2402 }
2403 e = POP();
2404 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2405 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002406 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002407 patch(e.out, s1);
2408
2409 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2410 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002411 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002412 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2413 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002414 {
2415 s->negated = TRUE;
2416 s1->negated = TRUE;
2417 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002418 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2419 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2420 {
2421 s->val = *++p; /* get the count */
2422 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2423 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002424
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 PUSH(frag(s, list1(&s1->out)));
2426 break;
2427
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002428#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002429 case NFA_COMPOSING: /* char with composing char */
2430#if 0
2431 /* TODO */
2432 if (regflags & RF_ICOMBINE)
2433 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002434 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002435 }
2436#endif
2437 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002438#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002439
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440 case NFA_MOPEN + 0: /* Submatch */
2441 case NFA_MOPEN + 1:
2442 case NFA_MOPEN + 2:
2443 case NFA_MOPEN + 3:
2444 case NFA_MOPEN + 4:
2445 case NFA_MOPEN + 5:
2446 case NFA_MOPEN + 6:
2447 case NFA_MOPEN + 7:
2448 case NFA_MOPEN + 8:
2449 case NFA_MOPEN + 9:
2450 case NFA_NOPEN: /* \%( "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002451 if (nfa_calc_size == TRUE)
2452 {
2453 nstate += 2;
2454 break;
2455 }
2456
2457 mopen = *p;
2458 switch (*p)
2459 {
2460 case NFA_NOPEN:
2461 mclose = NFA_NCLOSE;
2462 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002463#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002464 case NFA_COMPOSING:
2465 mclose = NFA_END_COMPOSING;
2466 break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002467#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002468 default:
2469 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */
2470 mclose = *p + NSUBEXP;
2471 break;
2472 }
2473
2474 /* Allow "NFA_MOPEN" as a valid postfix representation for
2475 * the empty regexp "". In this case, the NFA will be
2476 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2477 * empty groups of parenthesis, and empty mbyte chars */
2478 if (stackp == stack)
2479 {
2480 s = new_state(mopen, NULL, NULL);
2481 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002482 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 s1 = new_state(mclose, NULL, NULL);
2484 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002485 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 patch(list1(&s->out), s1);
2487 PUSH(frag(s, list1(&s1->out)));
2488 break;
2489 }
2490
2491 /* At least one node was emitted before NFA_MOPEN, so
2492 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2493 e = POP();
2494 s = new_state(mopen, e.start, NULL); /* `(' */
2495 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002496 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002497
2498 s1 = new_state(mclose, NULL, NULL); /* `)' */
2499 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002500 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002501 patch(e.out, s1);
2502
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002503#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002504 if (mopen == NFA_COMPOSING)
2505 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002507#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508
2509 PUSH(frag(s, list1(&s1->out)));
2510 break;
2511
Bram Moolenaar5714b802013-05-28 22:03:20 +02002512 case NFA_BACKREF1:
2513 case NFA_BACKREF2:
2514 case NFA_BACKREF3:
2515 case NFA_BACKREF4:
2516 case NFA_BACKREF5:
2517 case NFA_BACKREF6:
2518 case NFA_BACKREF7:
2519 case NFA_BACKREF8:
2520 case NFA_BACKREF9:
2521 if (nfa_calc_size == TRUE)
2522 {
2523 nstate += 2;
2524 break;
2525 }
2526 s = new_state(*p, NULL, NULL);
2527 if (s == NULL)
2528 goto theend;
2529 s1 = new_state(NFA_SKIP, NULL, NULL);
2530 if (s1 == NULL)
2531 goto theend;
2532 patch(list1(&s->out), s1);
2533 PUSH(frag(s, list1(&s1->out)));
2534 break;
2535
Bram Moolenaar423532e2013-05-29 21:14:42 +02002536 case NFA_LNUM:
2537 case NFA_LNUM_GT:
2538 case NFA_LNUM_LT:
2539 case NFA_VCOL:
2540 case NFA_VCOL_GT:
2541 case NFA_VCOL_LT:
2542 case NFA_COL:
2543 case NFA_COL_GT:
2544 case NFA_COL_LT:
2545 if (nfa_calc_size == TRUE)
2546 {
2547 nstate += 1;
2548 break;
2549 }
2550 e1 = POP();
2551 s = new_state(*p, NULL, NULL);
2552 if (s == NULL)
2553 goto theend;
2554 s->val = e1.start->c;
2555 PUSH(frag(s, list1(&s->out)));
2556 break;
2557
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 case NFA_ZSTART:
2559 case NFA_ZEND:
2560 default:
2561 /* Operands */
2562 if (nfa_calc_size == TRUE)
2563 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002564 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002565 break;
2566 }
2567 s = new_state(*p, NULL, NULL);
2568 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002569 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002570 PUSH(frag(s, list1(&s->out)));
2571 break;
2572
2573 } /* switch(*p) */
2574
2575 } /* for(p = postfix; *p; ++p) */
2576
2577 if (nfa_calc_size == TRUE)
2578 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002579 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002580 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002581 }
2582
2583 e = POP();
2584 if (stackp != stack)
2585 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2586
2587 if (istate >= nstate)
2588 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2589
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002590 matchstate = &state_ptr[istate++]; /* the match state */
2591 matchstate->c = NFA_MATCH;
2592 matchstate->out = matchstate->out1 = NULL;
2593
2594 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002595 ret = e.start;
2596
2597theend:
2598 vim_free(stack);
2599 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002600
2601#undef POP1
2602#undef PUSH1
2603#undef POP2
2604#undef PUSH2
2605#undef POP
2606#undef PUSH
2607}
2608
2609/****************************************************************
2610 * NFA execution code.
2611 ****************************************************************/
2612
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002613typedef struct
2614{
2615 int in_use; /* number of subexpr with useful info */
2616
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002617 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002618 union
2619 {
2620 struct multipos
2621 {
2622 lpos_T start;
2623 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002624 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002625 struct linepos
2626 {
2627 char_u *start;
2628 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002629 } line[NSUBEXP];
2630 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002631} regsub_T;
2632
Bram Moolenaar963fee22013-05-26 21:47:28 +02002633/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002634typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635{
2636 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002637 int count;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002638 regsub_T sub; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002639} nfa_thread_T;
2640
Bram Moolenaar963fee22013-05-26 21:47:28 +02002641/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002642typedef struct
2643{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002644 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002645 int n; /* nr of states currently in "t" */
2646 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002647 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002648} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649
Bram Moolenaar5714b802013-05-28 22:03:20 +02002650#ifdef ENABLE_LOG
2651 static void
2652log_subexpr(sub)
2653 regsub_T *sub;
2654{
2655 int j;
2656
2657 for (j = 0; j < sub->in_use; j++)
2658 if (REG_MULTI)
2659 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2660 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002661 sub->list.multi[j].start.col,
2662 (int)sub->list.multi[j].start.lnum,
2663 sub->list.multi[j].end.col,
2664 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002665 else
2666 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2667 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002668 (char *)sub->list.line[j].start,
2669 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002670 fprintf(log_fd, "\n");
2671}
2672#endif
2673
Bram Moolenaar963fee22013-05-26 21:47:28 +02002674/* Used during execution: whether a match has been found. */
2675static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002676
Bram Moolenaar428e9872013-05-30 17:05:39 +02002677static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar5714b802013-05-28 22:03:20 +02002678static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off));
2679static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002680
Bram Moolenaar428e9872013-05-30 17:05:39 +02002681/*
2682 * Return TRUE if "sub1" and "sub2" have the same positions.
2683 */
2684 static int
2685sub_equal(sub1, sub2)
2686 regsub_T *sub1;
2687 regsub_T *sub2;
2688{
2689 int i;
2690 int todo;
2691 linenr_T s1, e1;
2692 linenr_T s2, e2;
2693 char_u *sp1, *ep1;
2694 char_u *sp2, *ep2;
2695
2696 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2697 if (REG_MULTI)
2698 {
2699 for (i = 0; i < todo; ++i)
2700 {
2701 if (i < sub1->in_use)
2702 {
2703 s1 = sub1->list.multi[i].start.lnum;
2704 e1 = sub1->list.multi[i].end.lnum;
2705 }
2706 else
2707 {
2708 s1 = 0;
2709 e1 = 0;
2710 }
2711 if (i < sub2->in_use)
2712 {
2713 s2 = sub2->list.multi[i].start.lnum;
2714 e2 = sub2->list.multi[i].end.lnum;
2715 }
2716 else
2717 {
2718 s2 = 0;
2719 e2 = 0;
2720 }
2721 if (s1 != s2 || e1 != e2)
2722 return FALSE;
2723 if (s1 != 0 && sub1->list.multi[i].start.col
2724 != sub2->list.multi[i].start.col)
2725 return FALSE;
2726 if (e1 != 0 && sub1->list.multi[i].end.col
2727 != sub2->list.multi[i].end.col)
2728 return FALSE;
2729 }
2730 }
2731 else
2732 {
2733 for (i = 0; i < todo; ++i)
2734 {
2735 if (i < sub1->in_use)
2736 {
2737 sp1 = sub1->list.line[i].start;
2738 ep1 = sub1->list.line[i].end;
2739 }
2740 else
2741 {
2742 sp1 = NULL;
2743 ep1 = NULL;
2744 }
2745 if (i < sub2->in_use)
2746 {
2747 sp2 = sub2->list.line[i].start;
2748 ep2 = sub2->list.line[i].end;
2749 }
2750 else
2751 {
2752 sp2 = NULL;
2753 ep2 = NULL;
2754 }
2755 if (sp1 != sp2 || ep1 != ep2)
2756 return FALSE;
2757 }
2758 }
2759
2760 return TRUE;
2761}
2762
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002763 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02002764addstate(l, state, sub, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02002765 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002766 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002767 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02002768 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002769{
Bram Moolenaar963fee22013-05-26 21:47:28 +02002770 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002771 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002772 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002773 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002774 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002775 int i;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002776#ifdef ENABLE_LOG
2777 int did_print = FALSE;
2778#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002779
2780 if (l == NULL || state == NULL)
2781 return;
2782
2783 switch (state->c)
2784 {
2785 case NFA_SPLIT:
2786 case NFA_NOT:
2787 case NFA_NOPEN:
2788 case NFA_NCLOSE:
2789 case NFA_MCLOSE:
2790 case NFA_MCLOSE + 1:
2791 case NFA_MCLOSE + 2:
2792 case NFA_MCLOSE + 3:
2793 case NFA_MCLOSE + 4:
2794 case NFA_MCLOSE + 5:
2795 case NFA_MCLOSE + 6:
2796 case NFA_MCLOSE + 7:
2797 case NFA_MCLOSE + 8:
2798 case NFA_MCLOSE + 9:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002799 /* These nodes are not added themselves but their "out" and/or
2800 * "out1" may be added below. */
2801 break;
2802
2803 case NFA_MOPEN:
2804 case NFA_MOPEN + 1:
2805 case NFA_MOPEN + 2:
2806 case NFA_MOPEN + 3:
2807 case NFA_MOPEN + 4:
2808 case NFA_MOPEN + 5:
2809 case NFA_MOPEN + 6:
2810 case NFA_MOPEN + 7:
2811 case NFA_MOPEN + 8:
2812 case NFA_MOPEN + 9:
2813 /* These nodes do not need to be added, but we need to bail out
2814 * when it was tried to be added to this list before. */
2815 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002816 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002817 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002818 break;
2819
2820 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002821 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002823 /* This state is already in the list, don't add it again,
2824 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002825 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002826 {
2827skip_add:
2828#ifdef ENABLE_LOG
2829 nfa_set_code(state->c);
2830 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
2831 abs(state->id), l->id, state->c, code);
2832#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02002833 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002834 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02002835
2836 /* See if the same state is already in the list with the same
2837 * positions. */
2838 for (i = 0; i < l->n; ++i)
2839 {
2840 thread = &l->t[i];
2841 if (thread->state->id == state->id
2842 && sub_equal(&thread->sub, sub))
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002843 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002844 }
2845 }
2846
2847 /* when there are backreferences the number of states may be (a
2848 * lot) bigger */
2849 if (nfa_has_backref && l->n == l->len)
2850 {
2851 int newlen = l->len * 3 / 2 + 50;
2852
2853 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
2854 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002856
2857 /* add the state to the list */
2858 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02002859 thread = &l->t[l->n++];
2860 thread->state = state;
2861 thread->sub.in_use = sub->in_use;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002862 if (sub->in_use > 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002863 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002864 /* Copy the match start and end positions. */
2865 if (REG_MULTI)
Bram Moolenaar428e9872013-05-30 17:05:39 +02002866 mch_memmove(&thread->sub.list.multi[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002867 &sub->list.multi[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002868 sizeof(struct multipos) * sub->in_use);
2869 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02002870 mch_memmove(&thread->sub.list.line[0],
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002871 &sub->list.line[0],
Bram Moolenaar5714b802013-05-28 22:03:20 +02002872 sizeof(struct linepos) * sub->in_use);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002874#ifdef ENABLE_LOG
2875 {
2876 int col;
2877
2878 if (thread->sub.in_use <= 0)
2879 col = -1;
2880 else if (REG_MULTI)
2881 col = thread->sub.list.multi[0].start.col;
2882 else
2883 col = (int)(thread->sub.list.line[0].start - regline);
2884 nfa_set_code(state->c);
2885 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
2886 abs(state->id), l->id, state->c, code, col);
2887 did_print = TRUE;
2888 }
2889#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002890 }
2891
2892#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002893 if (!did_print)
2894 {
2895 int col;
2896
2897 if (sub->in_use <= 0)
2898 col = -1;
2899 else if (REG_MULTI)
2900 col = sub->list.multi[0].start.col;
2901 else
2902 col = (int)(sub->list.line[0].start - regline);
2903 nfa_set_code(state->c);
2904 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
2905 abs(state->id), l->id, state->c, code, col);
2906 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002907#endif
2908 switch (state->c)
2909 {
2910 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02002911 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002912 break;
2913
2914 case NFA_SPLIT:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002915 addstate(l, state->out, sub, off);
2916 addstate(l, state->out1, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 break;
2918
2919#if 0
2920 case NFA_END_NEG_RANGE:
2921 /* Nothing to handle here. nfa_regmatch() will take care of it */
2922 break;
2923
2924 case NFA_NOT:
2925 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !"));
2926#ifdef ENABLE_LOG
2927 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n");
2928#endif
2929 break;
2930
2931 case NFA_COMPOSING:
2932 /* nfa_regmatch() will match all the bytes of this composing char. */
2933 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934#endif
2935
Bram Moolenaar5714b802013-05-28 22:03:20 +02002936 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937 case NFA_NOPEN:
2938 case NFA_NCLOSE:
Bram Moolenaar5714b802013-05-28 22:03:20 +02002939 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 break;
2941
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002942 case NFA_MOPEN + 0:
2943 case NFA_MOPEN + 1:
2944 case NFA_MOPEN + 2:
2945 case NFA_MOPEN + 3:
2946 case NFA_MOPEN + 4:
2947 case NFA_MOPEN + 5:
2948 case NFA_MOPEN + 6:
2949 case NFA_MOPEN + 7:
2950 case NFA_MOPEN + 8:
2951 case NFA_MOPEN + 9:
2952 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953 if (state->c == NFA_ZSTART)
2954 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02002955 else
2956 subidx = state->c - NFA_MOPEN;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002957
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002958 /* Set the position (with "off") in the subexpression. Save and
2959 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02002960 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002961 if (REG_MULTI)
2962 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002963 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002964 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002965 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002966 save_in_use = -1;
2967 }
2968 else
2969 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002970 save_in_use = sub->in_use;
2971 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002972 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002973 sub->list.multi[i].start.lnum = -1;
2974 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002975 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02002976 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002977 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02002978 if (off == -1)
2979 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002980 sub->list.multi[subidx].start.lnum = reglnum + 1;
2981 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02002982 }
2983 else
2984 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002985 sub->list.multi[subidx].start.lnum = reglnum;
2986 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02002987 (colnr_T)(reginput - regline + off);
2988 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002989 }
2990 else
2991 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002992 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002993 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002994 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002995 save_in_use = -1;
2996 }
2997 else
2998 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02002999 save_in_use = sub->in_use;
3000 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003001 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003002 sub->list.line[i].start = NULL;
3003 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003004 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003005 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003006 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003007 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003008 }
3009
Bram Moolenaar5714b802013-05-28 22:03:20 +02003010 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003011
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003012 if (save_in_use == -1)
3013 {
3014 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003015 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003016 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003017 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003018 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003019 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003020 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003021 break;
3022
3023 case NFA_MCLOSE + 0:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003024 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003025 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003026 /* Do not overwrite the position set by \ze. If no \ze
3027 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003028 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003029 break;
3030 }
3031 case NFA_MCLOSE + 1:
3032 case NFA_MCLOSE + 2:
3033 case NFA_MCLOSE + 3:
3034 case NFA_MCLOSE + 4:
3035 case NFA_MCLOSE + 5:
3036 case NFA_MCLOSE + 6:
3037 case NFA_MCLOSE + 7:
3038 case NFA_MCLOSE + 8:
3039 case NFA_MCLOSE + 9:
3040 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003041 if (state->c == NFA_ZEND)
3042 subidx = 0;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003043 else
3044 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003046 /* We don't fill in gaps here, there must have been an MOPEN that
3047 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003048 save_in_use = sub->in_use;
3049 if (sub->in_use <= subidx)
3050 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003051 if (REG_MULTI)
3052 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003053 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003054 if (off == -1)
3055 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003056 sub->list.multi[subidx].end.lnum = reglnum + 1;
3057 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003058 }
3059 else
3060 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003061 sub->list.multi[subidx].end.lnum = reglnum;
3062 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003063 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003064 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003065 }
3066 else
3067 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003068 save_ptr = sub->list.line[subidx].end;
3069 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003070 }
3071
Bram Moolenaar5714b802013-05-28 22:03:20 +02003072 addstate(l, state->out, sub, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003073
3074 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003075 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003076 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003077 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003078 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003079 break;
3080 }
3081}
3082
3083/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003084 * Like addstate(), but the new state(s) are put at position "*ip".
3085 * Used for zero-width matches, next state to use is the added one.
3086 * This makes sure the order of states to be tried does not change, which
3087 * matters for alternatives.
3088 */
3089 static void
Bram Moolenaar5714b802013-05-28 22:03:20 +02003090addstate_here(l, state, sub, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003091 nfa_list_T *l; /* runtime state list */
3092 nfa_state_T *state; /* state to update */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003093 regsub_T *sub; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003094 int *ip;
3095{
3096 int tlen = l->n;
3097 int count;
3098 int i = *ip;
3099
3100 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003101 addstate(l, state, sub, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003102
3103 /* when "*ip" was at the end of the list, nothing to do */
3104 if (i + 1 == tlen)
3105 return;
3106
3107 /* re-order to put the new state at the current position */
3108 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003109 if (count == 1)
3110 {
3111 /* overwrite the current state */
3112 l->t[i] = l->t[l->n - 1];
3113 }
3114 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003115 {
3116 /* make space for new states, then move them from the
3117 * end to the current position */
3118 mch_memmove(&(l->t[i + count]),
3119 &(l->t[i + 1]),
3120 sizeof(nfa_thread_T) * (l->n - i - 1));
3121 mch_memmove(&(l->t[i]),
3122 &(l->t[l->n - 1]),
3123 sizeof(nfa_thread_T) * count);
3124 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003125 --l->n;
3126 *ip = i - 1;
3127}
3128
3129/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003130 * Check character class "class" against current character c.
3131 */
3132 static int
3133check_char_class(class, c)
3134 int class;
3135 int c;
3136{
3137 switch (class)
3138 {
3139 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003140 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003141 return OK;
3142 break;
3143 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003144 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003145 return OK;
3146 break;
3147 case NFA_CLASS_BLANK:
3148 if (c == ' ' || c == '\t')
3149 return OK;
3150 break;
3151 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003152 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003153 return OK;
3154 break;
3155 case NFA_CLASS_DIGIT:
3156 if (VIM_ISDIGIT(c))
3157 return OK;
3158 break;
3159 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003160 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003161 return OK;
3162 break;
3163 case NFA_CLASS_LOWER:
3164 if (MB_ISLOWER(c))
3165 return OK;
3166 break;
3167 case NFA_CLASS_PRINT:
3168 if (vim_isprintc(c))
3169 return OK;
3170 break;
3171 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003172 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003173 return OK;
3174 break;
3175 case NFA_CLASS_SPACE:
3176 if ((c >=9 && c <= 13) || (c == ' '))
3177 return OK;
3178 break;
3179 case NFA_CLASS_UPPER:
3180 if (MB_ISUPPER(c))
3181 return OK;
3182 break;
3183 case NFA_CLASS_XDIGIT:
3184 if (vim_isxdigit(c))
3185 return OK;
3186 break;
3187 case NFA_CLASS_TAB:
3188 if (c == '\t')
3189 return OK;
3190 break;
3191 case NFA_CLASS_RETURN:
3192 if (c == '\r')
3193 return OK;
3194 break;
3195 case NFA_CLASS_BACKSPACE:
3196 if (c == '\b')
3197 return OK;
3198 break;
3199 case NFA_CLASS_ESCAPE:
3200 if (c == '\033')
3201 return OK;
3202 break;
3203
3204 default:
3205 /* should not be here :P */
3206 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3207 }
3208 return FAIL;
3209}
3210
Bram Moolenaar5714b802013-05-28 22:03:20 +02003211static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3212
3213/*
3214 * Check for a match with subexpression "subidx".
3215 * return TRUE if it matches.
3216 */
3217 static int
3218match_backref(sub, subidx, bytelen)
3219 regsub_T *sub; /* pointers to subexpressions */
3220 int subidx;
3221 int *bytelen; /* out: length of match in bytes */
3222{
3223 int len;
3224
3225 if (sub->in_use <= subidx)
3226 {
3227retempty:
3228 /* backref was not set, match an empty string */
3229 *bytelen = 0;
3230 return TRUE;
3231 }
3232
3233 if (REG_MULTI)
3234 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003235 if (sub->list.multi[subidx].start.lnum < 0
3236 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003237 goto retempty;
3238 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003239 len = sub->list.multi[subidx].end.col
3240 - sub->list.multi[subidx].start.col;
3241 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003242 reginput, &len) == 0)
3243 {
3244 *bytelen = len;
3245 return TRUE;
3246 }
3247 }
3248 else
3249 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003250 if (sub->list.line[subidx].start == NULL
3251 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003252 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003253 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3254 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003255 {
3256 *bytelen = len;
3257 return TRUE;
3258 }
3259 }
3260 return FALSE;
3261}
3262
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263/*
3264 * Set all NFA nodes' list ID equal to -1.
3265 */
3266 static void
3267nfa_set_neg_listids(start)
3268 nfa_state_T *start;
3269{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003270 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 {
3272 start->lastlist = -1;
3273 nfa_set_neg_listids(start->out);
3274 nfa_set_neg_listids(start->out1);
3275 }
3276}
3277
3278/*
3279 * Set all NFA nodes' list ID equal to 0.
3280 */
3281 static void
3282nfa_set_null_listids(start)
3283 nfa_state_T *start;
3284{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003285 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 {
3287 start->lastlist = 0;
3288 nfa_set_null_listids(start->out);
3289 nfa_set_null_listids(start->out1);
3290 }
3291}
3292
3293/*
3294 * Save list IDs for all NFA states in "list".
3295 */
3296 static void
3297nfa_save_listids(start, list)
3298 nfa_state_T *start;
3299 int *list;
3300{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003301 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 {
3303 list[abs(start->id)] = start->lastlist;
3304 start->lastlist = -1;
3305 nfa_save_listids(start->out, list);
3306 nfa_save_listids(start->out1, list);
3307 }
3308}
3309
3310/*
3311 * Restore list IDs from "list" to all NFA states.
3312 */
3313 static void
3314nfa_restore_listids(start, list)
3315 nfa_state_T *start;
3316 int *list;
3317{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003318 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 {
3320 start->lastlist = list[abs(start->id)];
3321 nfa_restore_listids(start->out, list);
3322 nfa_restore_listids(start->out1, list);
3323 }
3324}
3325
Bram Moolenaar423532e2013-05-29 21:14:42 +02003326 static int
3327nfa_re_num_cmp(val, op, pos)
3328 long_u val;
3329 int op;
3330 long_u pos;
3331{
3332 if (op == 1) return pos > val;
3333 if (op == 2) return pos < val;
3334 return val == pos;
3335}
3336
Bram Moolenaar61602c52013-06-01 19:54:43 +02003337static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m, save_se_T *endp));
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339/*
3340 * Main matching routine.
3341 *
3342 * Run NFA to determine whether it matches reginput.
3343 *
Bram Moolenaar61602c52013-06-01 19:54:43 +02003344 * When "endp" is not NULL it is a required end-of-match position.
3345 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 * Return TRUE if there is a match, FALSE otherwise.
3347 * Note: Caller must ensure that: start != NULL.
3348 */
3349 static int
Bram Moolenaar61602c52013-06-01 19:54:43 +02003350nfa_regmatch(start, submatch, m, endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003351 nfa_state_T *start;
3352 regsub_T *submatch;
3353 regsub_T *m;
Bram Moolenaar61602c52013-06-01 19:54:43 +02003354 save_se_T *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003355{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356 int result;
3357 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003359 int go_to_nextline = FALSE;
3360 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003361 nfa_list_T list[3];
3362 nfa_list_T *listtbl[2][2];
3363 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003365 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003366 nfa_list_T *thislist;
3367 nfa_list_T *nextlist;
3368 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003370#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003371 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372
3373 if (debug == NULL)
3374 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003375 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376 return FALSE;
3377 }
3378#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003379 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003380
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003381 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003382 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003383 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3384 list[0].len = nstate + 1;
3385 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3386 list[1].len = nstate + 1;
3387 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3388 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003389 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3390 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391
3392#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003393 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003394 if (log_fd != NULL)
3395 {
3396 fprintf(log_fd, "**********************************\n");
3397 nfa_set_code(start->c);
3398 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3399 abs(start->id), code);
3400 fprintf(log_fd, "**********************************\n");
3401 }
3402 else
3403 {
3404 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3405 log_fd = stderr;
3406 }
3407#endif
3408
3409 thislist = &list[0];
3410 thislist->n = 0;
3411 nextlist = &list[1];
3412 nextlist->n = 0;
3413 neglist = &list[2];
3414 neglist->n = 0;
3415#ifdef ENABLE_LOG
3416 fprintf(log_fd, "(---) STARTSTATE\n");
3417#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003418 thislist->id = listid;
3419 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003420
3421 /* There are two cases when the NFA advances: 1. input char matches the
3422 * NFA node and 2. input char does not match the NFA node, but the next
3423 * node is NFA_NOT. The following macro calls addstate() according to
3424 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3425 listtbl[0][0] = NULL;
3426 listtbl[0][1] = neglist;
3427 listtbl[1][0] = nextlist;
3428 listtbl[1][1] = NULL;
3429#define ADD_POS_NEG_STATE(node) \
3430 ll = listtbl[result ? 1 : 0][node->negated]; \
3431 if (ll != NULL) \
Bram Moolenaar5714b802013-05-28 22:03:20 +02003432 addstate(ll, node->out , &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003433
3434
3435 /*
3436 * Run for each character.
3437 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003438 for (;;)
3439 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003440 int curc;
3441 int clen;
3442
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003443#ifdef FEAT_MBYTE
3444 if (has_mbyte)
3445 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003446 curc = (*mb_ptr2char)(reginput);
3447 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448 }
3449 else
3450#endif
3451 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003452 curc = *reginput;
3453 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003454 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003455 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003456 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003457 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003458 go_to_nextline = FALSE;
3459 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460
3461 /* swap lists */
3462 thislist = &list[flag];
3463 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003464 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 listtbl[1][0] = nextlist;
3466 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003467 thislist->id = listid;
3468 nextlist->id = listid + 1;
3469 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003470
3471#ifdef ENABLE_LOG
3472 fprintf(log_fd, "------------------------------------------\n");
3473 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003474 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003476 {
3477 int i;
3478
3479 for (i = 0; i < thislist->n; i++)
3480 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3481 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 fprintf(log_fd, "\n");
3483#endif
3484
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003485#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003486 fprintf(debug, "\n-------------------\n");
3487#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003488 /*
3489 * If the state lists are empty we can stop.
3490 */
3491 if (thislist->n == 0 && neglist->n == 0)
3492 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493
3494 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003495 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496 {
3497 if (neglist->n > 0)
3498 {
3499 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003500 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003501 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 }
3503 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003504 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003506#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003507 nfa_set_code(t->state->c);
3508 fprintf(debug, "%s, ", code);
3509#endif
3510#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003511 {
3512 int col;
3513
3514 if (t->sub.in_use <= 0)
3515 col = -1;
3516 else if (REG_MULTI)
3517 col = t->sub.list.multi[0].start.col;
3518 else
3519 col = (int)(t->sub.list.line[0].start - regline);
3520 nfa_set_code(t->state->c);
3521 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3522 abs(t->state->id), (int)t->state->c, code, col);
3523 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524#endif
3525
3526 /*
3527 * Handle the possible codes of the current state.
3528 * The most important is NFA_MATCH.
3529 */
3530 switch (t->state->c)
3531 {
3532 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003533 {
3534 int j;
3535
Bram Moolenaar963fee22013-05-26 21:47:28 +02003536 nfa_match = TRUE;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003537 submatch->in_use = t->sub.in_use;
3538 if (REG_MULTI)
3539 for (j = 0; j < submatch->in_use; j++)
3540 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003541 submatch->list.multi[j].start =
3542 t->sub.list.multi[j].start;
3543 submatch->list.multi[j].end = t->sub.list.multi[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003544 }
3545 else
3546 for (j = 0; j < submatch->in_use; j++)
3547 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003548 submatch->list.line[j].start =
3549 t->sub.list.line[j].start;
3550 submatch->list.line[j].end = t->sub.list.line[j].end;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003551 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003552#ifdef ENABLE_LOG
Bram Moolenaar5714b802013-05-28 22:03:20 +02003553 log_subexpr(&t->sub);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003555 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003556 * states at this position. When the list of states is going
3557 * to be empty quit without advancing, so that "reginput" is
3558 * correct. */
3559 if (nextlist->n == 0 && neglist->n == 0)
3560 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003561 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003562 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003563
3564 case NFA_END_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003565 /* This is only encountered after a NFA_START_INVISIBLE or
3566 * NFA_START_INVISIBLE_BEFORE node.
3567 * They surround a zero-width group, used with "\@=", "\&",
3568 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003569 * If we got here, it means that the current "invisible" group
3570 * finished successfully, so return control to the parent
3571 * nfa_regmatch(). Submatches are stored in *m, and used in
3572 * the parent call. */
3573 if (start->c == NFA_MOPEN + 0)
Bram Moolenaar61602c52013-06-01 19:54:43 +02003574 /* TODO: do we ever get here? */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003575 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003576 else
3577 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003578#ifdef ENABLE_LOG
3579 if (endp != NULL)
3580 {
3581 if (REG_MULTI)
3582 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
3583 (int)reglnum,
3584 (int)endp->se_u.pos.lnum,
3585 (int)(reginput - regline),
3586 endp->se_u.pos.col);
3587 else
3588 fprintf(log_fd, "Current col: %d, endp col: %d\n",
3589 (int)(reginput - regline),
3590 (int)(endp->se_u.ptr - reginput));
3591 }
3592#endif
3593 /* It's only a match if it ends at "endp" */
3594 if (endp != NULL && (REG_MULTI
3595 ? (reglnum != endp->se_u.pos.lnum
3596 || (int)(reginput - regline)
3597 != endp->se_u.pos.col)
3598 : reginput != endp->se_u.ptr))
3599 break;
3600
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003601 /* do not set submatches for \@! */
3602 if (!t->state->negated)
3603 /* TODO: only copy positions in use. */
3604 *m = t->sub;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003605 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003606 }
3607 break;
3608
3609 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003610 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003611 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003612 char_u *save_reginput = reginput;
3613 char_u *save_regline = regline;
3614 int save_reglnum = reglnum;
3615 int save_nfa_match = nfa_match;
3616 save_se_T endpos;
3617 save_se_T *endposp = NULL;
3618
3619 if (t->state->c == NFA_START_INVISIBLE_BEFORE)
3620 {
3621 /* The recursive match must end at the current position. */
3622 endposp = &endpos;
3623 if (REG_MULTI)
3624 {
3625 endpos.se_u.pos.col = (int)(reginput - regline);
3626 endpos.se_u.pos.lnum = reglnum;
3627 }
3628 else
3629 endpos.se_u.ptr = reginput;
3630
3631 /* Go back the specified number of bytes, or as far as the
3632 * start of the previous line, to try matching "\@<=" or
3633 * not matching "\@<!". */
3634 if (t->state->val <= 0)
3635 {
3636 if (REG_MULTI)
3637 {
3638 regline = reg_getline(--reglnum);
3639 if (regline == NULL)
3640 /* can't go before the first line */
3641 regline = reg_getline(++reglnum);
3642 }
3643 reginput = regline;
3644 }
3645 else
3646 {
3647 if (REG_MULTI
3648 && (int)(reginput - regline) < t->state->val)
3649 {
3650 /* Not enough bytes in this line, go to end of
3651 * previous line. */
3652 regline = reg_getline(--reglnum);
3653 if (regline == NULL)
3654 {
3655 /* can't go before the first line */
3656 regline = reg_getline(++reglnum);
3657 reginput = regline;
3658 }
3659 else
3660 reginput = regline + STRLEN(regline);
3661 }
3662 if ((int)(reginput - regline) >= t->state->val)
3663 {
3664 reginput -= t->state->val;
3665#ifdef FEAT_MBYTE
3666 if (has_mbyte)
3667 reginput -= mb_head_off(regline, reginput);
3668#endif
3669 }
3670 else
3671 reginput = regline;
3672 }
3673 }
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003674
3675 /* Call nfa_regmatch() to check if the current concat matches
3676 * at this position. The concat ends with the node
3677 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 if (listids == NULL)
3679 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003680 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003681 if (listids == NULL)
3682 {
3683 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3684 return 0;
3685 }
3686 }
3687#ifdef ENABLE_LOG
3688 if (log_fd != stderr)
3689 fclose(log_fd);
3690 log_fd = NULL;
3691#endif
3692 /* Have to clear the listid field of the NFA nodes, so that
3693 * nfa_regmatch() and addstate() can run properly after
3694 * recursion. */
3695 nfa_save_listids(start, listids);
3696 nfa_set_null_listids(start);
Bram Moolenaar61602c52013-06-01 19:54:43 +02003697 result = nfa_regmatch(t->state->out, submatch, m, endposp);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698 nfa_set_neg_listids(start);
3699 nfa_restore_listids(start, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003700
3701 /* restore position in input text */
3702 reginput = save_reginput;
3703 regline = save_regline;
3704 reglnum = save_reglnum;
3705 nfa_match = save_nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003706
3707#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003708 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709 if (log_fd != NULL)
3710 {
3711 fprintf(log_fd, "****************************\n");
3712 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3713 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3714 fprintf(log_fd, "****************************\n");
3715 }
3716 else
3717 {
3718 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3719 log_fd = stderr;
3720 }
3721#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003722 /* for \@! it is a match when result is FALSE */
3723 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 {
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003725 int j;
3726
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003727 /* Copy submatch info from the recursive call */
3728 if (REG_MULTI)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003729 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003730 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003731 t->sub.list.multi[j].start = m->list.multi[j].start;
3732 t->sub.list.multi[j].end = m->list.multi[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003733 }
3734 else
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003735 for (j = 1; j < m->in_use; j++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003737 t->sub.list.line[j].start = m->list.line[j].start;
3738 t->sub.list.line[j].end = m->list.line[j].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003740 if (m->in_use > t->sub.in_use)
3741 t->sub.in_use = m->in_use;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003742
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003743 /* t->state->out1 is the corresponding END_INVISIBLE node;
3744 * Add it to the current list (zero-width match). */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003745 addstate_here(thislist, t->state->out1->out, &t->sub,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003746 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003748 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003749 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750
3751 case NFA_BOL:
3752 if (reginput == regline)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003753 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754 break;
3755
3756 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003757 if (curc == NUL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003758 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 break;
3760
3761 case NFA_BOW:
3762 {
3763 int bow = TRUE;
3764
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003765 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003766 bow = FALSE;
3767#ifdef FEAT_MBYTE
3768 else if (has_mbyte)
3769 {
3770 int this_class;
3771
3772 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003773 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774 if (this_class <= 1)
3775 bow = FALSE;
3776 else if (reg_prev_class() == this_class)
3777 bow = FALSE;
3778 }
3779#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003780 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003781 || (reginput > regline
3782 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783 bow = FALSE;
3784 if (bow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003785 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786 break;
3787 }
3788
3789 case NFA_EOW:
3790 {
3791 int eow = TRUE;
3792
3793 if (reginput == regline)
3794 eow = FALSE;
3795#ifdef FEAT_MBYTE
3796 else if (has_mbyte)
3797 {
3798 int this_class, prev_class;
3799
3800 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003801 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003802 prev_class = reg_prev_class();
3803 if (this_class == prev_class
3804 || prev_class == 0 || prev_class == 1)
3805 eow = FALSE;
3806 }
3807#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003808 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003809 || (reginput[0] != NUL
3810 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003811 eow = FALSE;
3812 if (eow)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003813 addstate_here(thislist, t->state->out, &t->sub, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003814 break;
3815 }
3816
Bram Moolenaar4b780632013-05-31 22:14:52 +02003817 case NFA_BOF:
3818 if (reglnum == 0 && reginput == regline
3819 && (!REG_MULTI || reg_firstlnum == 1))
3820 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3821 break;
3822
3823 case NFA_EOF:
3824 if (reglnum == reg_maxline && curc == NUL)
3825 addstate_here(thislist, t->state->out, &t->sub, &listidx);
3826 break;
3827
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003828#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003829 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003830 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003831 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003832 int len = 0;
3833 nfa_state_T *end;
3834 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003835 int cchars[MAX_MCO];
3836 int ccount = 0;
3837 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003838
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003839 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003840 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003841 if (utf_iscomposing(sta->c))
3842 {
3843 /* Only match composing character(s), ignore base
3844 * character. Used for ".{composing}" and "{composing}"
3845 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003846 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003847 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02003848 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02003850 /* If \Z was present, then ignore composing characters.
3851 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003852 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003853 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003854 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003855 else
3856 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003857 while (sta->c != NFA_END_COMPOSING)
3858 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003859 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003860
3861 /* Check base character matches first, unless ignored. */
3862 else if (len > 0 || mc == sta->c)
3863 {
3864 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003865 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003866 len += mb_char2len(mc);
3867 sta = sta->out;
3868 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003869
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003870 /* We don't care about the order of composing characters.
3871 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003872 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003873 {
3874 mc = mb_ptr2char(reginput + len);
3875 cchars[ccount++] = mc;
3876 len += mb_char2len(mc);
3877 if (ccount == MAX_MCO)
3878 break;
3879 }
3880
3881 /* Check that each composing char in the pattern matches a
3882 * composing char in the text. We do not check if all
3883 * composing chars are matched. */
3884 result = OK;
3885 while (sta->c != NFA_END_COMPOSING)
3886 {
3887 for (j = 0; j < ccount; ++j)
3888 if (cchars[j] == sta->c)
3889 break;
3890 if (j == ccount)
3891 {
3892 result = FAIL;
3893 break;
3894 }
3895 sta = sta->out;
3896 }
3897 }
3898 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02003899 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02003900
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003901 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003902 ADD_POS_NEG_STATE(end);
3903 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003904 }
3905#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003906
3907 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003908 if (curc == NUL && !reg_line_lbr && REG_MULTI
3909 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02003911 go_to_nextline = TRUE;
3912 /* Pass -1 for the offset, which means taking the position
3913 * at the start of the next line. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003914 addstate(nextlist, t->state->out, &t->sub, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003916 else if (curc == '\n' && reg_line_lbr)
3917 {
3918 /* match \n as if it is an ordinary character */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003919 addstate(nextlist, t->state->out, &t->sub, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003920 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003921 break;
3922
3923 case NFA_CLASS_ALNUM:
3924 case NFA_CLASS_ALPHA:
3925 case NFA_CLASS_BLANK:
3926 case NFA_CLASS_CNTRL:
3927 case NFA_CLASS_DIGIT:
3928 case NFA_CLASS_GRAPH:
3929 case NFA_CLASS_LOWER:
3930 case NFA_CLASS_PRINT:
3931 case NFA_CLASS_PUNCT:
3932 case NFA_CLASS_SPACE:
3933 case NFA_CLASS_UPPER:
3934 case NFA_CLASS_XDIGIT:
3935 case NFA_CLASS_TAB:
3936 case NFA_CLASS_RETURN:
3937 case NFA_CLASS_BACKSPACE:
3938 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003939 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003940 ADD_POS_NEG_STATE(t->state);
3941 break;
3942
3943 case NFA_END_NEG_RANGE:
3944 /* This follows a series of negated nodes, like:
3945 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003946 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003947 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003948 break;
3949
3950 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02003951 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003952 if (curc > 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003953 addstate(nextlist, t->state->out, &t->sub, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003954 break;
3955
3956 /*
3957 * Character classes like \a for alpha, \d for digit etc.
3958 */
3959 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003960 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961 ADD_POS_NEG_STATE(t->state);
3962 break;
3963
3964 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003965 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003966 ADD_POS_NEG_STATE(t->state);
3967 break;
3968
3969 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02003970 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003971 ADD_POS_NEG_STATE(t->state);
3972 break;
3973
3974 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003975 result = !VIM_ISDIGIT(curc)
3976 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003977 ADD_POS_NEG_STATE(t->state);
3978 break;
3979
3980 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003981 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003982 ADD_POS_NEG_STATE(t->state);
3983 break;
3984
3985 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003986 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003987 ADD_POS_NEG_STATE(t->state);
3988 break;
3989
3990 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02003991 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003992 ADD_POS_NEG_STATE(t->state);
3993 break;
3994
3995 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003996 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997 ADD_POS_NEG_STATE(t->state);
3998 break;
3999
4000 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004001 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002 ADD_POS_NEG_STATE(t->state);
4003 break;
4004
4005 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004006 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004007 ADD_POS_NEG_STATE(t->state);
4008 break;
4009
4010 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004011 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004012 ADD_POS_NEG_STATE(t->state);
4013 break;
4014
4015 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004016 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004017 ADD_POS_NEG_STATE(t->state);
4018 break;
4019
4020 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004021 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004022 ADD_POS_NEG_STATE(t->state);
4023 break;
4024
4025 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004026 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004027 ADD_POS_NEG_STATE(t->state);
4028 break;
4029
4030 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004031 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004032 ADD_POS_NEG_STATE(t->state);
4033 break;
4034
4035 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004036 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037 ADD_POS_NEG_STATE(t->state);
4038 break;
4039
4040 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004041 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004042 ADD_POS_NEG_STATE(t->state);
4043 break;
4044
4045 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004046 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004047 ADD_POS_NEG_STATE(t->state);
4048 break;
4049
4050 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004051 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004052 ADD_POS_NEG_STATE(t->state);
4053 break;
4054
4055 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004056 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004057 ADD_POS_NEG_STATE(t->state);
4058 break;
4059
4060 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004061 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004062 ADD_POS_NEG_STATE(t->state);
4063 break;
4064
4065 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004066 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 ADD_POS_NEG_STATE(t->state);
4068 break;
4069
4070 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004071 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072 ADD_POS_NEG_STATE(t->state);
4073 break;
4074
4075 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004076 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004077 ADD_POS_NEG_STATE(t->state);
4078 break;
4079
4080 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004081 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004082 ADD_POS_NEG_STATE(t->state);
4083 break;
4084
4085 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004086 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004087 ADD_POS_NEG_STATE(t->state);
4088 break;
4089
Bram Moolenaar5714b802013-05-28 22:03:20 +02004090 case NFA_BACKREF1:
4091 case NFA_BACKREF2:
4092 case NFA_BACKREF3:
4093 case NFA_BACKREF4:
4094 case NFA_BACKREF5:
4095 case NFA_BACKREF6:
4096 case NFA_BACKREF7:
4097 case NFA_BACKREF8:
4098 case NFA_BACKREF9:
4099 /* \1 .. \9 */
4100 {
4101 int subidx = t->state->c - NFA_BACKREF1 + 1;
4102 int bytelen;
4103
4104 result = match_backref(&t->sub, subidx, &bytelen);
4105 if (result)
4106 {
4107 if (bytelen == 0)
4108 {
4109 /* empty match always works, add NFA_SKIP with zero to
4110 * be used next */
4111 addstate_here(thislist, t->state->out, &t->sub,
4112 &listidx);
4113 thislist->t[listidx + 1].count = 0;
4114 }
4115 else if (bytelen <= clen)
4116 {
4117 /* match current character, jump ahead to out of
4118 * NFA_SKIP */
4119 addstate(nextlist, t->state->out->out, &t->sub, clen);
4120#ifdef ENABLE_LOG
4121 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4122#endif
4123 }
4124 else
4125 {
4126 /* skip ofer the matched characters, set character
4127 * count in NFA_SKIP */
4128 addstate(nextlist, t->state->out, &t->sub, bytelen);
4129 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4130#ifdef ENABLE_LOG
4131 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4132#endif
4133 }
4134
4135 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004136 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004137 }
4138 case NFA_SKIP:
4139 /* charater of previous matching \1 .. \9 */
4140 if (t->count - clen <= 0)
4141 {
4142 /* end of match, go to what follows */
4143 addstate(nextlist, t->state->out, &t->sub, clen);
4144#ifdef ENABLE_LOG
4145 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4146#endif
4147 }
4148 else
4149 {
4150 /* add state again with decremented count */
4151 addstate(nextlist, t->state, &t->sub, 0);
4152 nextlist->t[nextlist->n - 1].count = t->count - clen;
4153#ifdef ENABLE_LOG
4154 log_subexpr(&nextlist->t[nextlist->n - 1].sub);
4155#endif
4156 }
4157 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004158
4159 case NFA_SKIP_CHAR:
4160 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004161 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004162 /* TODO: should not happen? */
4163 break;
4164
Bram Moolenaar423532e2013-05-29 21:14:42 +02004165 case NFA_LNUM:
4166 case NFA_LNUM_GT:
4167 case NFA_LNUM_LT:
4168 result = (REG_MULTI &&
4169 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4170 (long_u)(reglnum + reg_firstlnum)));
4171 if (result)
4172 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4173 break;
4174
4175 case NFA_COL:
4176 case NFA_COL_GT:
4177 case NFA_COL_LT:
4178 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4179 (long_u)(reginput - regline) + 1);
4180 if (result)
4181 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4182 break;
4183
4184 case NFA_VCOL:
4185 case NFA_VCOL_GT:
4186 case NFA_VCOL_LT:
4187 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4188 (long_u)win_linetabsize(
4189 reg_win == NULL ? curwin : reg_win,
4190 regline, (colnr_T)(reginput - regline)) + 1);
4191 if (result)
4192 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4193 break;
4194
4195 case NFA_CURSOR:
4196 result = (reg_win != NULL
4197 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4198 && ((colnr_T)(reginput - regline)
4199 == reg_win->w_cursor.col));
4200 if (result)
4201 addstate_here(thislist, t->state->out, &t->sub, &listidx);
4202 break;
4203
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004204 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004205 {
4206 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004207
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004208 /* TODO: put this in #ifdef later */
4209 if (c < -256)
4210 EMSGN("INTERNAL: Negative state char: %ld", c);
4211 if (is_Magic(c))
4212 c = un_Magic(c);
4213 result = (c == curc);
4214
4215 if (!result && ireg_ic)
4216 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004217#ifdef FEAT_MBYTE
4218 /* If there is a composing character which is not being
4219 * ignored there can be no match. Match with composing
4220 * character uses NFA_COMPOSING above. */
4221 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004222 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004223 result = FALSE;
4224#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225 ADD_POS_NEG_STATE(t->state);
4226 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004227 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004228 }
4229
4230 } /* for (thislist = thislist; thislist->state; thislist++) */
4231
Bram Moolenaare23febd2013-05-26 18:40:14 +02004232 /* Look for the start of a match in the current position by adding the
4233 * start state to the list of states.
4234 * The first found match is the leftmost one, thus the order of states
4235 * matters!
4236 * Do not add the start state in recursive calls of nfa_regmatch(),
4237 * because recursive calls should only start in the first position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004238 * Unless "endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004239 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004240 if (nfa_match == FALSE
4241 && ((start->c == NFA_MOPEN + 0
4242 && reglnum == 0
4243 && clen != 0
4244 && (ireg_maxcol == 0
4245 || (colnr_T)(reginput - regline) < ireg_maxcol))
4246 || (endp != NULL
4247 && (REG_MULTI
4248 ? (reglnum < endp->se_u.pos.lnum
4249 || (reglnum == endp->se_u.pos.lnum
4250 && (int)(reginput - regline)
4251 < endp->se_u.pos.col))
4252 : reginput < endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004253 {
4254#ifdef ENABLE_LOG
4255 fprintf(log_fd, "(---) STARTSTATE\n");
4256#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004257 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258 }
4259
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260#ifdef ENABLE_LOG
4261 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004262 {
4263 int i;
4264
4265 for (i = 0; i < thislist->n; i++)
4266 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4267 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 fprintf(log_fd, "\n");
4269#endif
4270
4271nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004272 /* Advance to the next character, or advance to the next line, or
4273 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004274 if (clen != 0)
4275 reginput += clen;
Bram Moolenaar61602c52013-06-01 19:54:43 +02004276 else if (go_to_nextline || (endp != NULL && REG_MULTI
4277 && reglnum < endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004278 reg_nextline();
4279 else
4280 break;
4281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004282
4283#ifdef ENABLE_LOG
4284 if (log_fd != stderr)
4285 fclose(log_fd);
4286 log_fd = NULL;
4287#endif
4288
4289theend:
4290 /* Free memory */
4291 vim_free(list[0].t);
4292 vim_free(list[1].t);
4293 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004294 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004296#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004297 fclose(debug);
4298#endif
4299
Bram Moolenaar963fee22013-05-26 21:47:28 +02004300 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004301}
4302
4303/*
4304 * Try match of "prog" with at regline["col"].
4305 * Returns 0 for failure, number of lines contained in the match otherwise.
4306 */
4307 static long
4308nfa_regtry(start, col)
4309 nfa_state_T *start;
4310 colnr_T col;
4311{
4312 int i;
4313 regsub_T sub, m;
4314#ifdef ENABLE_LOG
4315 FILE *f;
4316#endif
4317
4318 reginput = regline + col;
4319 need_clear_subexpr = TRUE;
4320
4321#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004322 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323 if (f != NULL)
4324 {
4325 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4326 fprintf(f, " =======================================================\n");
4327#ifdef DEBUG
4328 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4329#endif
4330 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004331 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004332 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333 fprintf(f, "\n\n");
4334 fclose(f);
4335 }
4336 else
4337 EMSG(_("Could not open temporary log file for writing "));
4338#endif
4339
4340 if (REG_MULTI)
4341 {
4342 /* Use 0xff to set lnum to -1 */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004343 vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
4344 vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345 }
4346 else
4347 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004348 vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4349 vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350 }
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004351 sub.in_use = 0;
4352 m.in_use = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004353
Bram Moolenaar61602c52013-06-01 19:54:43 +02004354 if (nfa_regmatch(start, &sub, &m, NULL) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355 return 0;
4356
4357 cleanup_subexpr();
4358 if (REG_MULTI)
4359 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004360 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004362 reg_startpos[i] = sub.list.multi[i].start;
4363 reg_endpos[i] = sub.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004364 }
4365
4366 if (reg_startpos[0].lnum < 0)
4367 {
4368 reg_startpos[0].lnum = 0;
4369 reg_startpos[0].col = col;
4370 }
4371 if (reg_endpos[0].lnum < 0)
4372 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004373 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004374 reg_endpos[0].lnum = reglnum;
4375 reg_endpos[0].col = (int)(reginput - regline);
4376 }
4377 else
4378 /* Use line number of "\ze". */
4379 reglnum = reg_endpos[0].lnum;
4380 }
4381 else
4382 {
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004383 for (i = 0; i < sub.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004384 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004385 reg_startp[i] = sub.list.line[i].start;
4386 reg_endp[i] = sub.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004387 }
4388
4389 if (reg_startp[0] == NULL)
4390 reg_startp[0] = regline + col;
4391 if (reg_endp[0] == NULL)
4392 reg_endp[0] = reginput;
4393 }
4394
4395 return 1 + reglnum;
4396}
4397
4398/*
4399 * Match a regexp against a string ("line" points to the string) or multiple
4400 * lines ("line" is NULL, use reg_getline()).
4401 *
4402 * Returns 0 for failure, number of lines contained in the match otherwise.
4403 */
4404 static long
4405nfa_regexec_both(line, col)
4406 char_u *line;
4407 colnr_T col; /* column to start looking for match */
4408{
4409 nfa_regprog_T *prog;
4410 long retval = 0L;
4411 int i;
4412
4413 if (REG_MULTI)
4414 {
4415 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4416 line = reg_getline((linenr_T)0); /* relative to the cursor */
4417 reg_startpos = reg_mmatch->startpos;
4418 reg_endpos = reg_mmatch->endpos;
4419 }
4420 else
4421 {
4422 prog = (nfa_regprog_T *)reg_match->regprog;
4423 reg_startp = reg_match->startp;
4424 reg_endp = reg_match->endp;
4425 }
4426
4427 /* Be paranoid... */
4428 if (prog == NULL || line == NULL)
4429 {
4430 EMSG(_(e_null));
4431 goto theend;
4432 }
4433
4434 /* If the start column is past the maximum column: no need to try. */
4435 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4436 goto theend;
4437
4438 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4439 if (prog->regflags & RF_ICASE)
4440 ireg_ic = TRUE;
4441 else if (prog->regflags & RF_NOICASE)
4442 ireg_ic = FALSE;
4443
4444#ifdef FEAT_MBYTE
4445 /* If pattern contains "\Z" overrule value of ireg_icombine */
4446 if (prog->regflags & RF_ICOMBINE)
4447 ireg_icombine = TRUE;
4448#endif
4449
4450 regline = line;
4451 reglnum = 0; /* relative to line */
4452
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004453 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004454 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004455 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004456
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004457 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004458 for (i = 0; i < nstate; ++i)
4459 {
4460 prog->state[i].id = i;
4461 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004462 }
4463
4464 retval = nfa_regtry(prog->start, col);
4465
4466theend:
4467 return retval;
4468}
4469
4470/*
4471 * Compile a regular expression into internal code for the NFA matcher.
4472 * Returns the program in allocated space. Returns NULL for an error.
4473 */
4474 static regprog_T *
4475nfa_regcomp(expr, re_flags)
4476 char_u *expr;
4477 int re_flags;
4478{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004479 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004480 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004481 int *postfix;
4482
4483 if (expr == NULL)
4484 return NULL;
4485
4486#ifdef DEBUG
4487 nfa_regengine.expr = expr;
4488#endif
4489
4490 init_class_tab();
4491
4492 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4493 return NULL;
4494
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004495 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004496 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004497 postfix = re2post();
4498 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004499 {
4500 /* TODO: only give this error for debugging? */
4501 if (post_ptr >= post_end)
4502 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004504 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004505
4506 /*
4507 * In order to build the NFA, we parse the input regexp twice:
4508 * 1. first pass to count size (so we can allocate space)
4509 * 2. second to emit code
4510 */
4511#ifdef ENABLE_LOG
4512 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004513 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004514
4515 if (f != NULL)
4516 {
4517 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4518 fclose(f);
4519 }
4520 }
4521#endif
4522
4523 /*
4524 * PASS 1
4525 * Count number of NFA states in "nstate". Do not build the NFA.
4526 */
4527 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004528
4529 /* Space for compiled regexp */
4530 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4531 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4532 if (prog == NULL)
4533 goto fail;
4534 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004535 state_ptr = prog->state;
4536
4537 /*
4538 * PASS 2
4539 * Build the NFA
4540 */
4541 prog->start = post2nfa(postfix, post_ptr, FALSE);
4542 if (prog->start == NULL)
4543 goto fail;
4544
4545 prog->regflags = regflags;
4546 prog->engine = &nfa_regengine;
4547 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004548 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004549 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004550 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004551#ifdef ENABLE_LOG
4552 nfa_postfix_dump(expr, OK);
4553 nfa_dump(prog);
4554#endif
4555
4556out:
4557 vim_free(post_start);
4558 post_start = post_ptr = post_end = NULL;
4559 state_ptr = NULL;
4560 return (regprog_T *)prog;
4561
4562fail:
4563 vim_free(prog);
4564 prog = NULL;
4565#ifdef ENABLE_LOG
4566 nfa_postfix_dump(expr, FAIL);
4567#endif
4568#ifdef DEBUG
4569 nfa_regengine.expr = NULL;
4570#endif
4571 goto out;
4572}
4573
4574
4575/*
4576 * Match a regexp against a string.
4577 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4578 * Uses curbuf for line count and 'iskeyword'.
4579 *
4580 * Return TRUE if there is a match, FALSE if not.
4581 */
4582 static int
4583nfa_regexec(rmp, line, col)
4584 regmatch_T *rmp;
4585 char_u *line; /* string to match against */
4586 colnr_T col; /* column to start looking for match */
4587{
4588 reg_match = rmp;
4589 reg_mmatch = NULL;
4590 reg_maxline = 0;
4591 reg_line_lbr = FALSE;
4592 reg_buf = curbuf;
4593 reg_win = NULL;
4594 ireg_ic = rmp->rm_ic;
4595#ifdef FEAT_MBYTE
4596 ireg_icombine = FALSE;
4597#endif
4598 ireg_maxcol = 0;
4599 return (nfa_regexec_both(line, col) != 0);
4600}
4601
4602#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4603 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4604
4605static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4606
4607/*
4608 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4609 */
4610 static int
4611nfa_regexec_nl(rmp, line, col)
4612 regmatch_T *rmp;
4613 char_u *line; /* string to match against */
4614 colnr_T col; /* column to start looking for match */
4615{
4616 reg_match = rmp;
4617 reg_mmatch = NULL;
4618 reg_maxline = 0;
4619 reg_line_lbr = TRUE;
4620 reg_buf = curbuf;
4621 reg_win = NULL;
4622 ireg_ic = rmp->rm_ic;
4623#ifdef FEAT_MBYTE
4624 ireg_icombine = FALSE;
4625#endif
4626 ireg_maxcol = 0;
4627 return (nfa_regexec_both(line, col) != 0);
4628}
4629#endif
4630
4631
4632/*
4633 * Match a regexp against multiple lines.
4634 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
4635 * Uses curbuf for line count and 'iskeyword'.
4636 *
4637 * Return zero if there is no match. Return number of lines contained in the
4638 * match otherwise.
4639 *
4640 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
4641 *
4642 * ! Also NOTE : match may actually be in another line. e.g.:
4643 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
4644 *
4645 * +-------------------------+
4646 * |a |
4647 * |b |
4648 * |c |
4649 * | |
4650 * +-------------------------+
4651 *
4652 * then nfa_regexec_multi() returns 3. while the original
4653 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
4654 *
4655 * FIXME if this behavior is not compatible.
4656 */
4657 static long
4658nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
4659 regmmatch_T *rmp;
4660 win_T *win; /* window in which to search or NULL */
4661 buf_T *buf; /* buffer in which to search */
4662 linenr_T lnum; /* nr of line to start looking for match */
4663 colnr_T col; /* column to start looking for match */
4664 proftime_T *tm UNUSED; /* timeout limit or NULL */
4665{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004666 reg_match = NULL;
4667 reg_mmatch = rmp;
4668 reg_buf = buf;
4669 reg_win = win;
4670 reg_firstlnum = lnum;
4671 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
4672 reg_line_lbr = FALSE;
4673 ireg_ic = rmp->rmm_ic;
4674#ifdef FEAT_MBYTE
4675 ireg_icombine = FALSE;
4676#endif
4677 ireg_maxcol = rmp->rmm_maxcol;
4678
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004679 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680}
4681
4682#ifdef DEBUG
4683# undef ENABLE_LOG
4684#endif