blob: 8abdbb5e90dd0f5002a681574001ca78ef7240eb [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 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020081#ifdef FEAT_SYN_HL
82 NFA_ZREF1, /* \z1 */
83 NFA_ZREF2, /* \z2 */
84 NFA_ZREF3, /* \z3 */
85 NFA_ZREF4, /* \z4 */
86 NFA_ZREF5, /* \z5 */
87 NFA_ZREF6, /* \z6 */
88 NFA_ZREF7, /* \z7 */
89 NFA_ZREF8, /* \z8 */
90 NFA_ZREF9, /* \z9 */
91#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020092 NFA_SKIP, /* Skip characters */
93
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020094 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020095 NFA_MOPEN1,
96 NFA_MOPEN2,
97 NFA_MOPEN3,
98 NFA_MOPEN4,
99 NFA_MOPEN5,
100 NFA_MOPEN6,
101 NFA_MOPEN7,
102 NFA_MOPEN8,
103 NFA_MOPEN9,
104
105 NFA_MCLOSE,
106 NFA_MCLOSE1,
107 NFA_MCLOSE2,
108 NFA_MCLOSE3,
109 NFA_MCLOSE4,
110 NFA_MCLOSE5,
111 NFA_MCLOSE6,
112 NFA_MCLOSE7,
113 NFA_MCLOSE8,
114 NFA_MCLOSE9,
115
116#ifdef FEAT_SYN_HL
117 NFA_ZOPEN,
118 NFA_ZOPEN1,
119 NFA_ZOPEN2,
120 NFA_ZOPEN3,
121 NFA_ZOPEN4,
122 NFA_ZOPEN5,
123 NFA_ZOPEN6,
124 NFA_ZOPEN7,
125 NFA_ZOPEN8,
126 NFA_ZOPEN9,
127
128 NFA_ZCLOSE,
129 NFA_ZCLOSE1,
130 NFA_ZCLOSE2,
131 NFA_ZCLOSE3,
132 NFA_ZCLOSE4,
133 NFA_ZCLOSE5,
134 NFA_ZCLOSE6,
135 NFA_ZCLOSE7,
136 NFA_ZCLOSE8,
137 NFA_ZCLOSE9,
138#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200139
140 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200141 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200142 NFA_ANYOF, /* Match any character in this string. */
143 NFA_ANYBUT, /* Match any character not in this string. */
144 NFA_IDENT, /* Match identifier char */
145 NFA_SIDENT, /* Match identifier char but no digit */
146 NFA_KWORD, /* Match keyword char */
147 NFA_SKWORD, /* Match word char but no digit */
148 NFA_FNAME, /* Match file name char */
149 NFA_SFNAME, /* Match file name char but no digit */
150 NFA_PRINT, /* Match printable char */
151 NFA_SPRINT, /* Match printable char but no digit */
152 NFA_WHITE, /* Match whitespace char */
153 NFA_NWHITE, /* Match non-whitespace char */
154 NFA_DIGIT, /* Match digit char */
155 NFA_NDIGIT, /* Match non-digit char */
156 NFA_HEX, /* Match hex char */
157 NFA_NHEX, /* Match non-hex char */
158 NFA_OCTAL, /* Match octal char */
159 NFA_NOCTAL, /* Match non-octal char */
160 NFA_WORD, /* Match word char */
161 NFA_NWORD, /* Match non-word char */
162 NFA_HEAD, /* Match head char */
163 NFA_NHEAD, /* Match non-head char */
164 NFA_ALPHA, /* Match alpha char */
165 NFA_NALPHA, /* Match non-alpha char */
166 NFA_LOWER, /* Match lowercase char */
167 NFA_NLOWER, /* Match non-lowercase char */
168 NFA_UPPER, /* Match uppercase char */
169 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200170
171 NFA_CURSOR, /* Match cursor pos */
172 NFA_LNUM, /* Match line number */
173 NFA_LNUM_GT, /* Match > line number */
174 NFA_LNUM_LT, /* Match < line number */
175 NFA_COL, /* Match cursor column */
176 NFA_COL_GT, /* Match > cursor column */
177 NFA_COL_LT, /* Match < cursor column */
178 NFA_VCOL, /* Match cursor virtual column */
179 NFA_VCOL_GT, /* Match > cursor virtual column */
180 NFA_VCOL_LT, /* Match < cursor virtual column */
181
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200182 NFA_FIRST_NL = NFA_ANY + ADD_NL,
183 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
184
185 /* Character classes [:alnum:] etc */
186 NFA_CLASS_ALNUM,
187 NFA_CLASS_ALPHA,
188 NFA_CLASS_BLANK,
189 NFA_CLASS_CNTRL,
190 NFA_CLASS_DIGIT,
191 NFA_CLASS_GRAPH,
192 NFA_CLASS_LOWER,
193 NFA_CLASS_PRINT,
194 NFA_CLASS_PUNCT,
195 NFA_CLASS_SPACE,
196 NFA_CLASS_UPPER,
197 NFA_CLASS_XDIGIT,
198 NFA_CLASS_TAB,
199 NFA_CLASS_RETURN,
200 NFA_CLASS_BACKSPACE,
201 NFA_CLASS_ESCAPE
202};
203
204/* Keep in sync with classchars. */
205static int nfa_classcodes[] = {
206 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
207 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
208 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
209 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
210 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
211 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
212 NFA_UPPER, NFA_NUPPER
213};
214
215static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
216
217/*
218 * NFA errors can be of 3 types:
219 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
220 * silently and revert the to backtracking engine.
221 * syntax_error = FALSE;
222 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
223 * The NFA engine displays an error message, and nothing else happens.
224 * syntax_error = TRUE
225 * *** Unsupported features, when the input regexp uses an operator that is not
226 * implemented in the NFA. The NFA engine fails silently, and reverts to the
227 * old backtracking engine.
228 * syntax_error = FALSE
229 * "The NFA fails" means that "compiling the regexp with the NFA fails":
230 * nfa_regcomp() returns FAIL.
231 */
232static int syntax_error = FALSE;
233
234/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200235static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236
Bram Moolenaar428e9872013-05-30 17:05:39 +0200237/* NFA regexp \1 .. \9 encountered. */
238static int nfa_has_backref;
239
Bram Moolenaar963fee22013-05-26 21:47:28 +0200240/* Number of sub expressions actually being used during execution. 1 if only
241 * the whole match (subexpr 0) is used. */
242static int nfa_nsubexpr;
243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static int *post_start; /* holds the postfix form of r.e. */
245static int *post_end;
246static int *post_ptr;
247
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200248static int nstate; /* Number of states in the NFA. Also used when
249 * executing. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250static int istate; /* Index in the state vector, used in new_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200251
252
253static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
254static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
255static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200256static int nfa_regatom __ARGS((void));
257static int nfa_regpiece __ARGS((void));
258static int nfa_regconcat __ARGS((void));
259static int nfa_regbranch __ARGS((void));
260static int nfa_reg __ARGS((int paren));
261#ifdef DEBUG
262static void nfa_set_code __ARGS((int c));
263static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200264static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
265static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266static void nfa_dump __ARGS((nfa_regprog_T *prog));
267#endif
268static int *re2post __ARGS((void));
269static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
270static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
271static int check_char_class __ARGS((int class, int c));
272static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200273static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
274static void nfa_set_null_listids __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
276static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200277static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200278static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
280static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
281static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
282static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
283
284/* helper functions used when doing re2post() ... regatom() parsing */
285#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200286 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287 return FAIL; \
288 *post_ptr++ = c; \
289 } while (0)
290
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200291/*
292 * Initialize internal variables before NFA compilation.
293 * Return OK on success, FAIL otherwise.
294 */
295 static int
296nfa_regcomp_start(expr, re_flags)
297 char_u *expr;
298 int re_flags; /* see vim_regcomp() */
299{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200300 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200301 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302
303 nstate = 0;
304 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200305 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200306 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200308 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200309 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200310 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200311
312 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200313 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200314
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315 post_start = (int *)lalloc(postfix_size, TRUE);
316 if (post_start == NULL)
317 return FAIL;
318 vim_memset(post_start, 0, postfix_size);
319 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200320 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200321 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200322 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200323
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200324 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200325 regcomp_start(expr, re_flags);
326
327 return OK;
328}
329
330/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200331 * Allocate more space for post_start. Called when
332 * running above the estimated number of states.
333 */
334 static int
335realloc_post_list()
336{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200337 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200338 int new_max = nstate_max + 1000;
339 int *new_start;
340 int *old_start;
341
342 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
343 if (new_start == NULL)
344 return FAIL;
345 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
346 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
347 old_start = post_start;
348 post_start = new_start;
349 post_ptr = new_start + (post_ptr - old_start);
350 post_end = post_start + new_max;
351 vim_free(old_start);
352 return OK;
353}
354
355/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200356 * Search between "start" and "end" and try to recognize a
357 * character class in expanded form. For example [0-9].
358 * On success, return the id the character class to be emitted.
359 * On failure, return 0 (=FAIL)
360 * Start points to the first char of the range, while end should point
361 * to the closing brace.
362 */
363 static int
364nfa_recognize_char_class(start, end, extra_newl)
365 char_u *start;
366 char_u *end;
367 int extra_newl;
368{
369 int i;
370 /* Each of these variables takes up a char in "config[]",
371 * in the order they are here. */
372 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
373 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
374 char_u *p;
375#define NCONFIGS 16
376 int classid[NCONFIGS] = {
377 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
378 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
379 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
380 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
381 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200382 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200383 char_u config[NCONFIGS][9] = {
384 "000000100", /* digit */
385 "100000100", /* non digit */
386 "011000100", /* hex-digit */
387 "111000100", /* non hex-digit */
388 "000001000", /* octal-digit */
389 "100001000", /* [^0-7] */
390 "000110110", /* [0-9A-Za-z_] */
391 "100110110", /* [^0-9A-Za-z_] */
392 "000110010", /* head of word */
393 "100110010", /* not head of word */
394 "000110000", /* alphabetic char a-z */
395 "100110000", /* non alphabetic char */
396 "000100000", /* lowercase letter */
397 "100100000", /* non lowercase */
398 "000010000", /* uppercase */
399 "100010000" /* non uppercase */
400 };
401
402 if (extra_newl == TRUE)
403 newl = TRUE;
404
405 if (*end != ']')
406 return FAIL;
407 p = start;
408 if (*p == '^')
409 {
410 not = TRUE;
411 p ++;
412 }
413
414 while (p < end)
415 {
416 if (p + 2 < end && *(p + 1) == '-')
417 {
418 switch (*p)
419 {
420 case '0':
421 if (*(p + 2) == '9')
422 {
423 o9 = TRUE;
424 break;
425 }
426 else
427 if (*(p + 2) == '7')
428 {
429 o7 = TRUE;
430 break;
431 }
432 case 'a':
433 if (*(p + 2) == 'z')
434 {
435 az = TRUE;
436 break;
437 }
438 else
439 if (*(p + 2) == 'f')
440 {
441 af = TRUE;
442 break;
443 }
444 case 'A':
445 if (*(p + 2) == 'Z')
446 {
447 AZ = TRUE;
448 break;
449 }
450 else
451 if (*(p + 2) == 'F')
452 {
453 AF = TRUE;
454 break;
455 }
456 /* FALLTHROUGH */
457 default:
458 return FAIL;
459 }
460 p += 3;
461 }
462 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
463 {
464 newl = TRUE;
465 p += 2;
466 }
467 else if (*p == '_')
468 {
469 underscore = TRUE;
470 p ++;
471 }
472 else if (*p == '\n')
473 {
474 newl = TRUE;
475 p ++;
476 }
477 else
478 return FAIL;
479 } /* while (p < end) */
480
481 if (p != end)
482 return FAIL;
483
484 /* build the config that represents the ranges we gathered */
485 STRCPY(myconfig, "000000000");
486 if (not == TRUE)
487 myconfig[0] = '1';
488 if (af == TRUE)
489 myconfig[1] = '1';
490 if (AF == TRUE)
491 myconfig[2] = '1';
492 if (az == TRUE)
493 myconfig[3] = '1';
494 if (AZ == TRUE)
495 myconfig[4] = '1';
496 if (o7 == TRUE)
497 myconfig[5] = '1';
498 if (o9 == TRUE)
499 myconfig[6] = '1';
500 if (underscore == TRUE)
501 myconfig[7] = '1';
502 if (newl == TRUE)
503 {
504 myconfig[8] = '1';
505 extra_newl = ADD_NL;
506 }
507 /* try to recognize character classes */
508 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200509 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200510 return classid[i] + extra_newl;
511
512 /* fallthrough => no success so far */
513 return FAIL;
514
515#undef NCONFIGS
516}
517
518/*
519 * Produce the bytes for equivalence class "c".
520 * Currently only handles latin1, latin9 and utf-8.
521 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
522 * equivalent to 'a OR b OR c'
523 *
524 * NOTE! When changing this function, also update reg_equi_class()
525 */
526 static int
527nfa_emit_equi_class(c, neg)
528 int c;
529 int neg;
530{
531 int first = TRUE;
532 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
533#define EMIT2(c) \
534 EMIT(c); \
535 if (neg == TRUE) { \
536 EMIT(NFA_NOT); \
537 } \
538 if (first == FALSE) \
539 EMIT(glue); \
540 else \
541 first = FALSE; \
542
543#ifdef FEAT_MBYTE
544 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
545 || STRCMP(p_enc, "iso-8859-15") == 0)
546#endif
547 {
548 switch (c)
549 {
550 case 'A': case '\300': case '\301': case '\302':
551 case '\303': case '\304': case '\305':
552 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
553 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
554 EMIT2('\305');
555 return OK;
556
557 case 'C': case '\307':
558 EMIT2('C'); EMIT2('\307');
559 return OK;
560
561 case 'E': case '\310': case '\311': case '\312': case '\313':
562 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
563 EMIT2('\312'); EMIT2('\313');
564 return OK;
565
566 case 'I': case '\314': case '\315': case '\316': case '\317':
567 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
568 EMIT2('\316'); EMIT2('\317');
569 return OK;
570
571 case 'N': case '\321':
572 EMIT2('N'); EMIT2('\321');
573 return OK;
574
575 case 'O': case '\322': case '\323': case '\324': case '\325':
576 case '\326':
577 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
578 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
579 return OK;
580
581 case 'U': case '\331': case '\332': case '\333': case '\334':
582 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
583 EMIT2('\333'); EMIT2('\334');
584 return OK;
585
586 case 'Y': case '\335':
587 EMIT2('Y'); EMIT2('\335');
588 return OK;
589
590 case 'a': case '\340': case '\341': case '\342':
591 case '\343': case '\344': case '\345':
592 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
593 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
594 EMIT2('\345');
595 return OK;
596
597 case 'c': case '\347':
598 EMIT2('c'); EMIT2('\347');
599 return OK;
600
601 case 'e': case '\350': case '\351': case '\352': case '\353':
602 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
603 EMIT2('\352'); EMIT2('\353');
604 return OK;
605
606 case 'i': case '\354': case '\355': case '\356': case '\357':
607 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
608 EMIT2('\356'); EMIT2('\357');
609 return OK;
610
611 case 'n': case '\361':
612 EMIT2('n'); EMIT2('\361');
613 return OK;
614
615 case 'o': case '\362': case '\363': case '\364': case '\365':
616 case '\366':
617 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
618 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
619 return OK;
620
621 case 'u': case '\371': case '\372': case '\373': case '\374':
622 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
623 EMIT2('\373'); EMIT2('\374');
624 return OK;
625
626 case 'y': case '\375': case '\377':
627 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
628 return OK;
629
630 default:
631 return FAIL;
632 }
633 }
634
635 EMIT(c);
636 return OK;
637#undef EMIT2
638}
639
640/*
641 * Code to parse regular expression.
642 *
643 * We try to reuse parsing functions in regexp.c to
644 * minimize surprise and keep the syntax consistent.
645 */
646
647/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200648 * Parse the lowest level.
649 *
650 * An atom can be one of a long list of items. Many atoms match one character
651 * in the text. It is often an ordinary character or a character class.
652 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
653 * is only for syntax highlighting.
654 *
655 * atom ::= ordinary-atom
656 * or \( pattern \)
657 * or \%( pattern \)
658 * or \z( pattern \)
659 */
660 static int
661nfa_regatom()
662{
663 int c;
664 int charclass;
665 int equiclass;
666 int collclass;
667 int got_coll_char;
668 char_u *p;
669 char_u *endp;
670#ifdef FEAT_MBYTE
671 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200672#endif
673 int extra = 0;
674 int first;
675 int emit_range;
676 int negated;
677 int result;
678 int startc = -1;
679 int endc = -1;
680 int oldstartc = -1;
681 int cpo_lit; /* 'cpoptions' contains 'l' flag */
682 int cpo_bsl; /* 'cpoptions' contains '\' flag */
683 int glue; /* ID that will "glue" nodes together */
684
685 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
686 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
687
688 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689 switch (c)
690 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200691 case NUL:
692 syntax_error = TRUE;
693 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
694
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695 case Magic('^'):
696 EMIT(NFA_BOL);
697 break;
698
699 case Magic('$'):
700 EMIT(NFA_EOL);
701#if defined(FEAT_SYN_HL) || defined(PROTO)
702 had_eol = TRUE;
703#endif
704 break;
705
706 case Magic('<'):
707 EMIT(NFA_BOW);
708 break;
709
710 case Magic('>'):
711 EMIT(NFA_EOW);
712 break;
713
714 case Magic('_'):
715 c = no_Magic(getchr());
716 if (c == '^') /* "\_^" is start-of-line */
717 {
718 EMIT(NFA_BOL);
719 break;
720 }
721 if (c == '$') /* "\_$" is end-of-line */
722 {
723 EMIT(NFA_EOL);
724#if defined(FEAT_SYN_HL) || defined(PROTO)
725 had_eol = TRUE;
726#endif
727 break;
728 }
729
730 extra = ADD_NL;
731
732 /* "\_[" is collection plus newline */
733 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200734 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200735
736 /* "\_x" is character class plus newline */
737 /*FALLTHROUGH*/
738
739 /*
740 * Character classes.
741 */
742 case Magic('.'):
743 case Magic('i'):
744 case Magic('I'):
745 case Magic('k'):
746 case Magic('K'):
747 case Magic('f'):
748 case Magic('F'):
749 case Magic('p'):
750 case Magic('P'):
751 case Magic('s'):
752 case Magic('S'):
753 case Magic('d'):
754 case Magic('D'):
755 case Magic('x'):
756 case Magic('X'):
757 case Magic('o'):
758 case Magic('O'):
759 case Magic('w'):
760 case Magic('W'):
761 case Magic('h'):
762 case Magic('H'):
763 case Magic('a'):
764 case Magic('A'):
765 case Magic('l'):
766 case Magic('L'):
767 case Magic('u'):
768 case Magic('U'):
769 p = vim_strchr(classchars, no_Magic(c));
770 if (p == NULL)
771 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200772 EMSGN("INTERNAL: Unknown character class char: %ld", c);
773 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 }
775#ifdef FEAT_MBYTE
776 /* When '.' is followed by a composing char ignore the dot, so that
777 * the composing char is matched here. */
778 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
779 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200780 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 c = getchr();
782 goto nfa_do_multibyte;
783 }
784#endif
785 EMIT(nfa_classcodes[p - classchars]);
786 if (extra == ADD_NL)
787 {
788 EMIT(NFA_NEWL);
789 EMIT(NFA_OR);
790 regflags |= RF_HASNL;
791 }
792 break;
793
794 case Magic('n'):
795 if (reg_string)
796 /* In a string "\n" matches a newline character. */
797 EMIT(NL);
798 else
799 {
800 /* In buffer text "\n" matches the end of a line. */
801 EMIT(NFA_NEWL);
802 regflags |= RF_HASNL;
803 }
804 break;
805
806 case Magic('('):
807 if (nfa_reg(REG_PAREN) == FAIL)
808 return FAIL; /* cascaded error */
809 break;
810
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200811 case Magic('|'):
812 case Magic('&'):
813 case Magic(')'):
814 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200815 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200816 return FAIL;
817
818 case Magic('='):
819 case Magic('?'):
820 case Magic('+'):
821 case Magic('@'):
822 case Magic('*'):
823 case Magic('{'):
824 /* these should follow an atom, not form an atom */
825 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200826 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200827 return FAIL;
828
829 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200830 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200831 return FAIL;
832
Bram Moolenaar428e9872013-05-30 17:05:39 +0200833 case Magic('1'):
834 case Magic('2'):
835 case Magic('3'):
836 case Magic('4'):
837 case Magic('5'):
838 case Magic('6'):
839 case Magic('7'):
840 case Magic('8'):
841 case Magic('9'):
842 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
843 nfa_has_backref = TRUE;
844 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200845
846 case Magic('z'):
847 c = no_Magic(getchr());
848 switch (c)
849 {
850 case 's':
851 EMIT(NFA_ZSTART);
852 break;
853 case 'e':
854 EMIT(NFA_ZEND);
855 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200856 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200857#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200858 case '1':
859 case '2':
860 case '3':
861 case '4':
862 case '5':
863 case '6':
864 case '7':
865 case '8':
866 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200867 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200868 if (reg_do_extmatch != REX_USE)
869 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200870 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
871 /* No need to set nfa_has_backref, the sub-matches don't
872 * change when \z1 .. \z9 maches or not. */
873 re_has_z = REX_USE;
874 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200875 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200876 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200877 if (reg_do_extmatch != REX_SET)
878 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200879 if (nfa_reg(REG_ZPAREN) == FAIL)
880 return FAIL; /* cascaded error */
881 re_has_z = REX_SET;
882 break;
883#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 default:
885 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200886 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 no_Magic(c));
888 return FAIL;
889 }
890 break;
891
892 case Magic('%'):
893 c = no_Magic(getchr());
894 switch (c)
895 {
896 /* () without a back reference */
897 case '(':
898 if (nfa_reg(REG_NPAREN) == FAIL)
899 return FAIL;
900 EMIT(NFA_NOPEN);
901 break;
902
903 case 'd': /* %d123 decimal */
904 case 'o': /* %o123 octal */
905 case 'x': /* %xab hex 2 */
906 case 'u': /* %uabcd hex 4 */
907 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200908 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200909 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200910
Bram Moolenaar47196582013-05-25 22:04:23 +0200911 switch (c)
912 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200913 case 'd': nr = getdecchrs(); break;
914 case 'o': nr = getoctchrs(); break;
915 case 'x': nr = gethexchrs(2); break;
916 case 'u': nr = gethexchrs(4); break;
917 case 'U': nr = gethexchrs(8); break;
918 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200919 }
920
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200921 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200922 EMSG2_RET_FAIL(
923 _("E678: Invalid character after %s%%[dxouU]"),
924 reg_magic == MAGIC_ALL);
925 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200926 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200927 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200928 break;
929
930 /* Catch \%^ and \%$ regardless of where they appear in the
931 * pattern -- regardless of whether or not it makes sense. */
932 case '^':
933 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200934 break;
935
936 case '$':
937 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200938 break;
939
940 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200941 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200942 break;
943
944 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200945 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200946 return FAIL;
947 break;
948
949 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200950 /* TODO: \%[abc] not supported yet */
951 return FAIL;
952
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200953 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200954 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200955 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200956 int cmp = c;
957
958 if (c == '<' || c == '>')
959 c = getchr();
960 while (VIM_ISDIGIT(c))
961 {
962 n = n * 10 + (c - '0');
963 c = getchr();
964 }
965 if (c == 'l' || c == 'c' || c == 'v')
966 {
967 EMIT(n);
968 if (c == 'l')
969 EMIT(cmp == '<' ? NFA_LNUM_LT :
970 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
971 else if (c == 'c')
972 EMIT(cmp == '<' ? NFA_COL_LT :
973 cmp == '>' ? NFA_COL_GT : NFA_COL);
974 else
975 EMIT(cmp == '<' ? NFA_VCOL_LT :
976 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
977 break;
978 }
979 else if (c == '\'')
980 /* TODO: \%'m not supported yet */
981 return FAIL;
982 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200983 syntax_error = TRUE;
984 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
985 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200986 return FAIL;
987 }
988 break;
989
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200990 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200991collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200992 /*
993 * Glue is emitted between several atoms from the [].
994 * It is either NFA_OR, or NFA_CONCAT.
995 *
996 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
997 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
998 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
999 * notation)
1000 *
1001 */
1002
1003
1004/* Emit negation atoms, if needed.
1005 * The CONCAT below merges the NOT with the previous node. */
1006#define TRY_NEG() \
1007 if (negated == TRUE) \
1008 { \
1009 EMIT(NFA_NOT); \
1010 }
1011
1012/* Emit glue between important nodes : CONCAT or OR. */
1013#define EMIT_GLUE() \
1014 if (first == FALSE) \
1015 EMIT(glue); \
1016 else \
1017 first = FALSE;
1018
1019 p = regparse;
1020 endp = skip_anyof(p);
1021 if (*endp == ']')
1022 {
1023 /*
1024 * Try to reverse engineer character classes. For example,
1025 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1026 * and perform the necessary substitutions in the NFA.
1027 */
1028 result = nfa_recognize_char_class(regparse, endp,
1029 extra == ADD_NL);
1030 if (result != FAIL)
1031 {
1032 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1033 EMIT(result);
1034 else /* must be char class + newline */
1035 {
1036 EMIT(result - ADD_NL);
1037 EMIT(NFA_NEWL);
1038 EMIT(NFA_OR);
1039 }
1040 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001041 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043 }
1044 /*
1045 * Failed to recognize a character class. Use the simple
1046 * version that turns [abc] into 'a' OR 'b' OR 'c'
1047 */
1048 startc = endc = oldstartc = -1;
1049 first = TRUE; /* Emitting first atom in this sequence? */
1050 negated = FALSE;
1051 glue = NFA_OR;
1052 if (*regparse == '^') /* negated range */
1053 {
1054 negated = TRUE;
1055 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001056 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001057 }
1058 if (*regparse == '-')
1059 {
1060 startc = '-';
1061 EMIT(startc);
1062 TRY_NEG();
1063 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001064 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001065 }
1066 /* Emit the OR branches for each character in the [] */
1067 emit_range = FALSE;
1068 while (regparse < endp)
1069 {
1070 oldstartc = startc;
1071 startc = -1;
1072 got_coll_char = FALSE;
1073 if (*regparse == '[')
1074 {
1075 /* Check for [: :], [= =], [. .] */
1076 equiclass = collclass = 0;
1077 charclass = get_char_class(&regparse);
1078 if (charclass == CLASS_NONE)
1079 {
1080 equiclass = get_equi_class(&regparse);
1081 if (equiclass == 0)
1082 collclass = get_coll_element(&regparse);
1083 }
1084
1085 /* Character class like [:alpha:] */
1086 if (charclass != CLASS_NONE)
1087 {
1088 switch (charclass)
1089 {
1090 case CLASS_ALNUM:
1091 EMIT(NFA_CLASS_ALNUM);
1092 break;
1093 case CLASS_ALPHA:
1094 EMIT(NFA_CLASS_ALPHA);
1095 break;
1096 case CLASS_BLANK:
1097 EMIT(NFA_CLASS_BLANK);
1098 break;
1099 case CLASS_CNTRL:
1100 EMIT(NFA_CLASS_CNTRL);
1101 break;
1102 case CLASS_DIGIT:
1103 EMIT(NFA_CLASS_DIGIT);
1104 break;
1105 case CLASS_GRAPH:
1106 EMIT(NFA_CLASS_GRAPH);
1107 break;
1108 case CLASS_LOWER:
1109 EMIT(NFA_CLASS_LOWER);
1110 break;
1111 case CLASS_PRINT:
1112 EMIT(NFA_CLASS_PRINT);
1113 break;
1114 case CLASS_PUNCT:
1115 EMIT(NFA_CLASS_PUNCT);
1116 break;
1117 case CLASS_SPACE:
1118 EMIT(NFA_CLASS_SPACE);
1119 break;
1120 case CLASS_UPPER:
1121 EMIT(NFA_CLASS_UPPER);
1122 break;
1123 case CLASS_XDIGIT:
1124 EMIT(NFA_CLASS_XDIGIT);
1125 break;
1126 case CLASS_TAB:
1127 EMIT(NFA_CLASS_TAB);
1128 break;
1129 case CLASS_RETURN:
1130 EMIT(NFA_CLASS_RETURN);
1131 break;
1132 case CLASS_BACKSPACE:
1133 EMIT(NFA_CLASS_BACKSPACE);
1134 break;
1135 case CLASS_ESCAPE:
1136 EMIT(NFA_CLASS_ESCAPE);
1137 break;
1138 }
1139 TRY_NEG();
1140 EMIT_GLUE();
1141 continue;
1142 }
1143 /* Try equivalence class [=a=] and the like */
1144 if (equiclass != 0)
1145 {
1146 result = nfa_emit_equi_class(equiclass, negated);
1147 if (result == FAIL)
1148 {
1149 /* should never happen */
1150 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1151 }
1152 EMIT_GLUE();
1153 continue;
1154 }
1155 /* Try collating class like [. .] */
1156 if (collclass != 0)
1157 {
1158 startc = collclass; /* allow [.a.]-x as a range */
1159 /* Will emit the proper atom at the end of the
1160 * while loop. */
1161 }
1162 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001163 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1164 * start character. */
1165 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166 {
1167 emit_range = TRUE;
1168 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001169 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001170 continue; /* reading the end of the range */
1171 }
1172
1173 /* Now handle simple and escaped characters.
1174 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1175 * accepts "\t", "\e", etc., but only when the 'l' flag in
1176 * 'cpoptions' is not included.
1177 * Posix doesn't recognize backslash at all.
1178 */
1179 if (*regparse == '\\'
1180 && !cpo_bsl
1181 && regparse + 1 <= endp
1182 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1183 || (!cpo_lit
1184 && vim_strchr(REGEXP_ABBR, regparse[1])
1185 != NULL)
1186 )
1187 )
1188 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001189 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001191 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001192 startc = reg_string ? NL : NFA_NEWL;
1193 else
1194 if (*regparse == 'd'
1195 || *regparse == 'o'
1196 || *regparse == 'x'
1197 || *regparse == 'u'
1198 || *regparse == 'U'
1199 )
1200 {
1201 /* TODO(RE) This needs more testing */
1202 startc = coll_get_char();
1203 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001204 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 }
1206 else
1207 {
1208 /* \r,\t,\e,\b */
1209 startc = backslash_trans(*regparse);
1210 }
1211 }
1212
1213 /* Normal printable char */
1214 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001215 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216
1217 /* Previous char was '-', so this char is end of range. */
1218 if (emit_range)
1219 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001220 endc = startc;
1221 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222 if (startc > endc)
1223 EMSG_RET_FAIL(_(e_invrange));
1224#ifdef FEAT_MBYTE
1225 if (has_mbyte && ((*mb_char2len)(startc) > 1
1226 || (*mb_char2len)(endc) > 1))
1227 {
1228 if (endc > startc + 256)
1229 EMSG_RET_FAIL(_(e_invrange));
1230 /* Emit the range. "startc" was already emitted, so
1231 * skip it. */
1232 for (c = startc + 1; c <= endc; c++)
1233 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001234 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235 TRY_NEG();
1236 EMIT_GLUE();
1237 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 }
1239 else
1240#endif
1241 {
1242#ifdef EBCDIC
1243 int alpha_only = FALSE;
1244
1245 /* for alphabetical range skip the gaps
1246 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1247 if (isalpha(startc) && isalpha(endc))
1248 alpha_only = TRUE;
1249#endif
1250 /* Emit the range. "startc" was already emitted, so
1251 * skip it. */
1252 for (c = startc + 1; c <= endc; c++)
1253#ifdef EBCDIC
1254 if (!alpha_only || isalpha(startc))
1255#endif
1256 {
1257 EMIT(c);
1258 TRY_NEG();
1259 EMIT_GLUE();
1260 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001261 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001262 emit_range = FALSE;
1263 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 }
1265 else
1266 {
1267 /*
1268 * This char (startc) is not part of a range. Just
1269 * emit it.
1270 *
1271 * Normally, simply emit startc. But if we get char
1272 * code=0 from a collating char, then replace it with
1273 * 0x0a.
1274 *
1275 * This is needed to completely mimic the behaviour of
1276 * the backtracking engine.
1277 */
1278 if (got_coll_char == TRUE && startc == 0)
1279 EMIT(0x0a);
1280 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001281 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 TRY_NEG();
1283 EMIT_GLUE();
1284 }
1285
Bram Moolenaar51a29832013-05-28 22:30:35 +02001286 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001287 } /* while (p < endp) */
1288
Bram Moolenaar51a29832013-05-28 22:30:35 +02001289 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001290 if (*regparse == '-') /* if last, '-' is just a char */
1291 {
1292 EMIT('-');
1293 TRY_NEG();
1294 EMIT_GLUE();
1295 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001296 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001297
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001298 /* skip the trailing ] */
1299 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001300 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001301 if (negated == TRUE)
1302 {
1303 /* Mark end of negated char range */
1304 EMIT(NFA_END_NEG_RANGE);
1305 EMIT(NFA_CONCAT);
1306 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001307
1308 /* \_[] also matches \n but it's not negated */
1309 if (extra == ADD_NL)
1310 {
1311 EMIT(reg_string ? NL : NFA_NEWL);
1312 EMIT(NFA_OR);
1313 }
1314
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001316 } /* if exists closing ] */
1317
1318 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 {
1320 syntax_error = TRUE;
1321 EMSG_RET_FAIL(_(e_missingbracket));
1322 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001323 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001324
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001325 default:
1326 {
1327#ifdef FEAT_MBYTE
1328 int plen;
1329
1330nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001331 /* plen is length of current char with composing chars */
1332 if (enc_utf8 && ((*mb_char2len)(c)
1333 != (plen = (*mb_ptr2len)(old_regparse))
1334 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001335 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001336 int i = 0;
1337
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001338 /* A base character plus composing characters, or just one
1339 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001340 * This requires creating a separate atom as if enclosing
1341 * the characters in (), where NFA_COMPOSING is the ( and
1342 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 * building the postfix form, not the NFA itself;
1344 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001345 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001346 for (;;)
1347 {
1348 EMIT(c);
1349 if (i > 0)
1350 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001351 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001352 break;
1353 c = utf_ptr2char(old_regparse + i);
1354 }
1355 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001356 regparse = old_regparse + plen;
1357 }
1358 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359#endif
1360 {
1361 c = no_Magic(c);
1362 EMIT(c);
1363 }
1364 return OK;
1365 }
1366 }
1367
1368#undef TRY_NEG
1369#undef EMIT_GLUE
1370
1371 return OK;
1372}
1373
1374/*
1375 * Parse something followed by possible [*+=].
1376 *
1377 * A piece is an atom, possibly followed by a multi, an indication of how many
1378 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1379 * characters: "", "a", "aa", etc.
1380 *
1381 * piece ::= atom
1382 * or atom multi
1383 */
1384 static int
1385nfa_regpiece()
1386{
1387 int i;
1388 int op;
1389 int ret;
1390 long minval, maxval;
1391 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001392 parse_state_T old_state;
1393 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001394 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001395 int old_post_pos;
1396 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001397 int quest;
1398
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001399 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1400 * next. */
1401 save_parse_state(&old_state);
1402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001404 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405
1406 ret = nfa_regatom();
1407 if (ret == FAIL)
1408 return FAIL; /* cascaded error */
1409
1410 op = peekchr();
1411 if (re_multi_type(op) == NOT_MULTI)
1412 return OK;
1413
1414 skipchr();
1415 switch (op)
1416 {
1417 case Magic('*'):
1418 EMIT(NFA_STAR);
1419 break;
1420
1421 case Magic('+'):
1422 /*
1423 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1424 * first and only submatch would be "aaa". But the backtracking
1425 * engine interprets the plus as "try matching one more time", and
1426 * a* matches a second time at the end of the input, the empty
1427 * string.
1428 * The submatch will the empty string.
1429 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001430 * In order to be consistent with the old engine, we replace
1431 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001432 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001433 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434 curchr = -1;
1435 if (nfa_regatom() == FAIL)
1436 return FAIL;
1437 EMIT(NFA_STAR);
1438 EMIT(NFA_CONCAT);
1439 skipchr(); /* skip the \+ */
1440 break;
1441
1442 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001443 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001445 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001446 switch(op)
1447 {
1448 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001449 /* \@= */
1450 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001451 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001452 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001453 /* \@! */
1454 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001455 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001456 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001457 op = no_Magic(getchr());
1458 if (op == '=')
1459 /* \@<= */
1460 i = NFA_PREV_ATOM_JUST_BEFORE;
1461 else if (op == '!')
1462 /* \@<! */
1463 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1464 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001466 /* \@> Not supported yet */
1467 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468 return FAIL;
1469 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001470 if (i == 0)
1471 {
1472 syntax_error = TRUE;
1473 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1474 return FAIL;
1475 }
1476 EMIT(i);
1477 if (i == NFA_PREV_ATOM_JUST_BEFORE
1478 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1479 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 break;
1481
1482 case Magic('?'):
1483 case Magic('='):
1484 EMIT(NFA_QUEST);
1485 break;
1486
1487 case Magic('{'):
1488 /* a{2,5} will expand to 'aaa?a?a?'
1489 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1490 * version of '?'
1491 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1492 * parenthesis have the same id
1493 */
1494
1495 greedy = TRUE;
1496 c2 = peekchr();
1497 if (c2 == '-' || c2 == Magic('-'))
1498 {
1499 skipchr();
1500 greedy = FALSE;
1501 }
1502 if (!read_limits(&minval, &maxval))
1503 {
1504 syntax_error = TRUE;
1505 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1506 }
1507 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1508 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001509 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001511 if (greedy)
1512 /* \{}, \{0,} */
1513 EMIT(NFA_STAR);
1514 else
1515 /* \{-}, \{-0,} */
1516 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001517 break;
1518 }
1519
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 /* Special case: x{0} or x{-0} */
1521 if (maxval == 0)
1522 {
1523 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001524 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1526 EMIT(NFA_SKIP_CHAR);
1527 return OK;
1528 }
1529
1530 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001531 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001532 /* Save parse state after the repeated atom and the \{} */
1533 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001534
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1536 for (i = 0; i < maxval; i++)
1537 {
1538 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001539 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001540 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001541 if (nfa_regatom() == FAIL)
1542 return FAIL;
1543 /* after "minval" times, atoms are optional */
1544 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001545 {
1546 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001547 {
1548 if (greedy)
1549 EMIT(NFA_STAR);
1550 else
1551 EMIT(NFA_STAR_NONGREEDY);
1552 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001553 else
1554 EMIT(quest);
1555 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001556 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001557 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001558 if (i + 1 > minval && maxval == MAX_LIMIT)
1559 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001560 }
1561
1562 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001563 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 curchr = -1;
1565
1566 break;
1567
1568
1569 default:
1570 break;
1571 } /* end switch */
1572
1573 if (re_multi_type(peekchr()) != NOT_MULTI)
1574 {
1575 /* Can't have a multi follow a multi. */
1576 syntax_error = TRUE;
1577 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1578 }
1579
1580 return OK;
1581}
1582
1583/*
1584 * Parse one or more pieces, concatenated. It matches a match for the
1585 * first piece, followed by a match for the second piece, etc. Example:
1586 * "f[0-9]b", first matches "f", then a digit and then "b".
1587 *
1588 * concat ::= piece
1589 * or piece piece
1590 * or piece piece piece
1591 * etc.
1592 */
1593 static int
1594nfa_regconcat()
1595{
1596 int cont = TRUE;
1597 int first = TRUE;
1598
1599 while (cont)
1600 {
1601 switch (peekchr())
1602 {
1603 case NUL:
1604 case Magic('|'):
1605 case Magic('&'):
1606 case Magic(')'):
1607 cont = FALSE;
1608 break;
1609
1610 case Magic('Z'):
1611#ifdef FEAT_MBYTE
1612 regflags |= RF_ICOMBINE;
1613#endif
1614 skipchr_keepstart();
1615 break;
1616 case Magic('c'):
1617 regflags |= RF_ICASE;
1618 skipchr_keepstart();
1619 break;
1620 case Magic('C'):
1621 regflags |= RF_NOICASE;
1622 skipchr_keepstart();
1623 break;
1624 case Magic('v'):
1625 reg_magic = MAGIC_ALL;
1626 skipchr_keepstart();
1627 curchr = -1;
1628 break;
1629 case Magic('m'):
1630 reg_magic = MAGIC_ON;
1631 skipchr_keepstart();
1632 curchr = -1;
1633 break;
1634 case Magic('M'):
1635 reg_magic = MAGIC_OFF;
1636 skipchr_keepstart();
1637 curchr = -1;
1638 break;
1639 case Magic('V'):
1640 reg_magic = MAGIC_NONE;
1641 skipchr_keepstart();
1642 curchr = -1;
1643 break;
1644
1645 default:
1646 if (nfa_regpiece() == FAIL)
1647 return FAIL;
1648 if (first == FALSE)
1649 EMIT(NFA_CONCAT);
1650 else
1651 first = FALSE;
1652 break;
1653 }
1654 }
1655
1656 return OK;
1657}
1658
1659/*
1660 * Parse a branch, one or more concats, separated by "\&". It matches the
1661 * last concat, but only if all the preceding concats also match at the same
1662 * position. Examples:
1663 * "foobeep\&..." matches "foo" in "foobeep".
1664 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1665 *
1666 * branch ::= concat
1667 * or concat \& concat
1668 * or concat \& concat \& concat
1669 * etc.
1670 */
1671 static int
1672nfa_regbranch()
1673{
1674 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001675 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676
Bram Moolenaar16299b52013-05-30 18:45:23 +02001677 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678
1679 /* First branch, possibly the only one */
1680 if (nfa_regconcat() == FAIL)
1681 return FAIL;
1682
1683 ch = peekchr();
1684 /* Try next concats */
1685 while (ch == Magic('&'))
1686 {
1687 skipchr();
1688 EMIT(NFA_NOPEN);
1689 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001690 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 if (nfa_regconcat() == FAIL)
1692 return FAIL;
1693 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001694 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001695 EMIT(NFA_SKIP_CHAR);
1696 EMIT(NFA_CONCAT);
1697 ch = peekchr();
1698 }
1699
1700 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001701 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001702 EMIT(NFA_SKIP_CHAR);
1703
1704 return OK;
1705}
1706
1707/*
1708 * Parse a pattern, one or more branches, separated by "\|". It matches
1709 * anything that matches one of the branches. Example: "foo\|beep" matches
1710 * "foo" and matches "beep". If more than one branch matches, the first one
1711 * is used.
1712 *
1713 * pattern ::= branch
1714 * or branch \| branch
1715 * or branch \| branch \| branch
1716 * etc.
1717 */
1718 static int
1719nfa_reg(paren)
1720 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1721{
1722 int parno = 0;
1723
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 if (paren == REG_PAREN)
1725 {
1726 if (regnpar >= NSUBEXP) /* Too many `(' */
1727 {
1728 syntax_error = TRUE;
1729 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1730 }
1731 parno = regnpar++;
1732 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001733#ifdef FEAT_SYN_HL
1734 else if (paren == REG_ZPAREN)
1735 {
1736 /* Make a ZOPEN node. */
1737 if (regnzpar >= NSUBEXP)
1738 {
1739 syntax_error = TRUE;
1740 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1741 }
1742 parno = regnzpar++;
1743 }
1744#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745
1746 if (nfa_regbranch() == FAIL)
1747 return FAIL; /* cascaded error */
1748
1749 while (peekchr() == Magic('|'))
1750 {
1751 skipchr();
1752 if (nfa_regbranch() == FAIL)
1753 return FAIL; /* cascaded error */
1754 EMIT(NFA_OR);
1755 }
1756
1757 /* Check for proper termination. */
1758 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1759 {
1760 syntax_error = TRUE;
1761 if (paren == REG_NPAREN)
1762 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1763 else
1764 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1765 }
1766 else if (paren == REG_NOPAREN && peekchr() != NUL)
1767 {
1768 syntax_error = TRUE;
1769 if (peekchr() == Magic(')'))
1770 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1771 else
1772 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1773 }
1774 /*
1775 * Here we set the flag allowing back references to this set of
1776 * parentheses.
1777 */
1778 if (paren == REG_PAREN)
1779 {
1780 had_endbrace[parno] = TRUE; /* have seen the close paren */
1781 EMIT(NFA_MOPEN + parno);
1782 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001783#ifdef FEAT_SYN_HL
1784 else if (paren == REG_ZPAREN)
1785 EMIT(NFA_ZOPEN + parno);
1786#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001787
1788 return OK;
1789}
1790
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001791#ifdef DEBUG
1792static char_u code[50];
1793
1794 static void
1795nfa_set_code(c)
1796 int c;
1797{
1798 int addnl = FALSE;
1799
1800 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1801 {
1802 addnl = TRUE;
1803 c -= ADD_NL;
1804 }
1805
1806 STRCPY(code, "");
1807 switch (c)
1808 {
1809 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1810 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1811 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1812 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1813 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1814 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1815
Bram Moolenaar5714b802013-05-28 22:03:20 +02001816 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1817 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1818 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1819 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1820 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1821 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1822 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1823 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1824 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001825#ifdef FEAT_SYN_HL
1826 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1827 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1828 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1829 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1830 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1831 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1832 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1833 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1834 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1835#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001836 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1837
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 case NFA_PREV_ATOM_NO_WIDTH:
1839 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001840 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1841 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001842 case NFA_PREV_ATOM_JUST_BEFORE:
1843 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1844 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1845 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001846 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1847 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001849 case NFA_START_INVISIBLE_BEFORE:
1850 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1852
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001853 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1854 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1855
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001856 case NFA_MOPEN:
1857 case NFA_MOPEN1:
1858 case NFA_MOPEN2:
1859 case NFA_MOPEN3:
1860 case NFA_MOPEN4:
1861 case NFA_MOPEN5:
1862 case NFA_MOPEN6:
1863 case NFA_MOPEN7:
1864 case NFA_MOPEN8:
1865 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 STRCPY(code, "NFA_MOPEN(x)");
1867 code[10] = c - NFA_MOPEN + '0';
1868 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001869 case NFA_MCLOSE:
1870 case NFA_MCLOSE1:
1871 case NFA_MCLOSE2:
1872 case NFA_MCLOSE3:
1873 case NFA_MCLOSE4:
1874 case NFA_MCLOSE5:
1875 case NFA_MCLOSE6:
1876 case NFA_MCLOSE7:
1877 case NFA_MCLOSE8:
1878 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879 STRCPY(code, "NFA_MCLOSE(x)");
1880 code[11] = c - NFA_MCLOSE + '0';
1881 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001882#ifdef FEAT_SYN_HL
1883 case NFA_ZOPEN:
1884 case NFA_ZOPEN1:
1885 case NFA_ZOPEN2:
1886 case NFA_ZOPEN3:
1887 case NFA_ZOPEN4:
1888 case NFA_ZOPEN5:
1889 case NFA_ZOPEN6:
1890 case NFA_ZOPEN7:
1891 case NFA_ZOPEN8:
1892 case NFA_ZOPEN9:
1893 STRCPY(code, "NFA_ZOPEN(x)");
1894 code[10] = c - NFA_ZOPEN + '0';
1895 break;
1896 case NFA_ZCLOSE:
1897 case NFA_ZCLOSE1:
1898 case NFA_ZCLOSE2:
1899 case NFA_ZCLOSE3:
1900 case NFA_ZCLOSE4:
1901 case NFA_ZCLOSE5:
1902 case NFA_ZCLOSE6:
1903 case NFA_ZCLOSE7:
1904 case NFA_ZCLOSE8:
1905 case NFA_ZCLOSE9:
1906 STRCPY(code, "NFA_ZCLOSE(x)");
1907 code[11] = c - NFA_ZCLOSE + '0';
1908 break;
1909#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1911 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1912 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1913 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001914 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1915 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001916 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001917 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1918 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1919 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001920 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1921 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1922 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1924 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1925 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1926 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1927 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1928 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1929 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1930 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1931 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1932 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1933 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1934 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1935 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1936 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1937 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1938 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1939 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1940
1941 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1942 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1943 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1944 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1945 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1946 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1947 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1948 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1949 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1950 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1951 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1952 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1953 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1954 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1955 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1956 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1957 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1958 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1959 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1960 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1961 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1962 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1963 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1964 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1965 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1966 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1967 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1968
1969 default:
1970 STRCPY(code, "CHAR(x)");
1971 code[5] = c;
1972 }
1973
1974 if (addnl == TRUE)
1975 STRCAT(code, " + NEWLINE ");
1976
1977}
1978
1979#ifdef ENABLE_LOG
1980static FILE *log_fd;
1981
1982/*
1983 * Print the postfix notation of the current regexp.
1984 */
1985 static void
1986nfa_postfix_dump(expr, retval)
1987 char_u *expr;
1988 int retval;
1989{
1990 int *p;
1991 FILE *f;
1992
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001993 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001994 if (f != NULL)
1995 {
1996 fprintf(f, "\n-------------------------\n");
1997 if (retval == FAIL)
1998 fprintf(f, ">>> NFA engine failed ... \n");
1999 else if (retval == OK)
2000 fprintf(f, ">>> NFA engine succeeded !\n");
2001 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002002 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003 {
2004 nfa_set_code(*p);
2005 fprintf(f, "%s, ", code);
2006 }
2007 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002008 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009 fprintf(f, "%d ", *p);
2010 fprintf(f, "\n\n");
2011 fclose(f);
2012 }
2013}
2014
2015/*
2016 * Print the NFA starting with a root node "state".
2017 */
2018 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002019nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002020 FILE *debugf;
2021 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002022{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002023 garray_T indent;
2024
2025 ga_init2(&indent, 1, 64);
2026 ga_append(&indent, '\0');
2027 nfa_print_state2(debugf, state, &indent);
2028 ga_clear(&indent);
2029}
2030
2031 static void
2032nfa_print_state2(debugf, state, indent)
2033 FILE *debugf;
2034 nfa_state_T *state;
2035 garray_T *indent;
2036{
2037 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038
2039 if (state == NULL)
2040 return;
2041
2042 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002043
2044 /* Output indent */
2045 p = (char_u *)indent->ga_data;
2046 if (indent->ga_len >= 3)
2047 {
2048 int last = indent->ga_len - 3;
2049 char_u save[2];
2050
2051 STRNCPY(save, &p[last], 2);
2052 STRNCPY(&p[last], "+-", 2);
2053 fprintf(debugf, " %s", p);
2054 STRNCPY(&p[last], save, 2);
2055 }
2056 else
2057 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058
2059 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002060 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2061 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002062 if (state->id < 0)
2063 return;
2064
2065 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002066
2067 /* grow indent for state->out */
2068 indent->ga_len -= 1;
2069 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002070 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002071 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002072 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002073 ga_append(indent, '\0');
2074
2075 nfa_print_state2(debugf, state->out, indent);
2076
2077 /* replace last part of indent for state->out1 */
2078 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002079 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002080 ga_append(indent, '\0');
2081
2082 nfa_print_state2(debugf, state->out1, indent);
2083
2084 /* shrink indent */
2085 indent->ga_len -= 3;
2086 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087}
2088
2089/*
2090 * Print the NFA state machine.
2091 */
2092 static void
2093nfa_dump(prog)
2094 nfa_regprog_T *prog;
2095{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002096 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097
2098 if (debugf != NULL)
2099 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002100 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002101 fclose(debugf);
2102 }
2103}
2104#endif /* ENABLE_LOG */
2105#endif /* DEBUG */
2106
2107/*
2108 * Parse r.e. @expr and convert it into postfix form.
2109 * Return the postfix string on success, NULL otherwise.
2110 */
2111 static int *
2112re2post()
2113{
2114 if (nfa_reg(REG_NOPAREN) == FAIL)
2115 return NULL;
2116 EMIT(NFA_MOPEN);
2117 return post_start;
2118}
2119
2120/* NB. Some of the code below is inspired by Russ's. */
2121
2122/*
2123 * Represents an NFA state plus zero or one or two arrows exiting.
2124 * if c == MATCH, no arrows out; matching state.
2125 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2126 * If c < 256, labeled arrow with character c to out.
2127 */
2128
2129static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2130
2131/*
2132 * Allocate and initialize nfa_state_T.
2133 */
2134 static nfa_state_T *
2135new_state(c, out, out1)
2136 int c;
2137 nfa_state_T *out;
2138 nfa_state_T *out1;
2139{
2140 nfa_state_T *s;
2141
2142 if (istate >= nstate)
2143 return NULL;
2144
2145 s = &state_ptr[istate++];
2146
2147 s->c = c;
2148 s->out = out;
2149 s->out1 = out1;
2150
2151 s->id = istate;
2152 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002153 s->negated = FALSE;
2154
2155 return s;
2156}
2157
2158/*
2159 * A partially built NFA without the matching state filled in.
2160 * Frag_T.start points at the start state.
2161 * Frag_T.out is a list of places that need to be set to the
2162 * next state for this fragment.
2163 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002164
2165/* Since the out pointers in the list are always
2166 * uninitialized, we use the pointers themselves
2167 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002168typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002169union Ptrlist
2170{
2171 Ptrlist *next;
2172 nfa_state_T *s;
2173};
2174
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002175struct Frag
2176{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002177 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178 Ptrlist *out;
2179};
2180typedef struct Frag Frag_T;
2181
2182static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2183static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2184static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2185static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2186static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2187static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2188
2189/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002190 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002191 */
2192 static Frag_T
2193frag(start, out)
2194 nfa_state_T *start;
2195 Ptrlist *out;
2196{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002197 Frag_T n;
2198
2199 n.start = start;
2200 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 return n;
2202}
2203
2204/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002205 * Create singleton list containing just outp.
2206 */
2207 static Ptrlist *
2208list1(outp)
2209 nfa_state_T **outp;
2210{
2211 Ptrlist *l;
2212
2213 l = (Ptrlist *)outp;
2214 l->next = NULL;
2215 return l;
2216}
2217
2218/*
2219 * Patch the list of states at out to point to start.
2220 */
2221 static void
2222patch(l, s)
2223 Ptrlist *l;
2224 nfa_state_T *s;
2225{
2226 Ptrlist *next;
2227
2228 for (; l; l = next)
2229 {
2230 next = l->next;
2231 l->s = s;
2232 }
2233}
2234
2235
2236/*
2237 * Join the two lists l1 and l2, returning the combination.
2238 */
2239 static Ptrlist *
2240append(l1, l2)
2241 Ptrlist *l1;
2242 Ptrlist *l2;
2243{
2244 Ptrlist *oldl1;
2245
2246 oldl1 = l1;
2247 while (l1->next)
2248 l1 = l1->next;
2249 l1->next = l2;
2250 return oldl1;
2251}
2252
2253/*
2254 * Stack used for transforming postfix form into NFA.
2255 */
2256static Frag_T empty;
2257
2258 static void
2259st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002260 int *postfix UNUSED;
2261 int *end UNUSED;
2262 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002263{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002264#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002265 FILE *df;
2266 int *p2;
2267
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002268 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002269 if (df)
2270 {
2271 fprintf(df, "Error popping the stack!\n");
2272#ifdef DEBUG
2273 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2274#endif
2275 fprintf(df, "Postfix form is: ");
2276#ifdef DEBUG
2277 for (p2 = postfix; p2 < end; p2++)
2278 {
2279 nfa_set_code(*p2);
2280 fprintf(df, "%s, ", code);
2281 }
2282 nfa_set_code(*p);
2283 fprintf(df, "\nCurrent position is: ");
2284 for (p2 = postfix; p2 <= p; p2 ++)
2285 {
2286 nfa_set_code(*p2);
2287 fprintf(df, "%s, ", code);
2288 }
2289#else
2290 for (p2 = postfix; p2 < end; p2++)
2291 {
2292 fprintf(df, "%d, ", *p2);
2293 }
2294 fprintf(df, "\nCurrent position is: ");
2295 for (p2 = postfix; p2 <= p; p2 ++)
2296 {
2297 fprintf(df, "%d, ", *p2);
2298 }
2299#endif
2300 fprintf(df, "\n--------------------------\n");
2301 fclose(df);
2302 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002303#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 EMSG(_("E874: (NFA) Could not pop the stack !"));
2305}
2306
2307/*
2308 * Push an item onto the stack.
2309 */
2310 static void
2311st_push(s, p, stack_end)
2312 Frag_T s;
2313 Frag_T **p;
2314 Frag_T *stack_end;
2315{
2316 Frag_T *stackp = *p;
2317
2318 if (stackp >= stack_end)
2319 return;
2320 *stackp = s;
2321 *p = *p + 1;
2322}
2323
2324/*
2325 * Pop an item from the stack.
2326 */
2327 static Frag_T
2328st_pop(p, stack)
2329 Frag_T **p;
2330 Frag_T *stack;
2331{
2332 Frag_T *stackp;
2333
2334 *p = *p - 1;
2335 stackp = *p;
2336 if (stackp < stack)
2337 return empty;
2338 return **p;
2339}
2340
2341/*
2342 * Convert a postfix form into its equivalent NFA.
2343 * Return the NFA start state on success, NULL otherwise.
2344 */
2345 static nfa_state_T *
2346post2nfa(postfix, end, nfa_calc_size)
2347 int *postfix;
2348 int *end;
2349 int nfa_calc_size;
2350{
2351 int *p;
2352 int mopen;
2353 int mclose;
2354 Frag_T *stack = NULL;
2355 Frag_T *stackp = NULL;
2356 Frag_T *stack_end = NULL;
2357 Frag_T e1;
2358 Frag_T e2;
2359 Frag_T e;
2360 nfa_state_T *s;
2361 nfa_state_T *s1;
2362 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002363 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364
2365 if (postfix == NULL)
2366 return NULL;
2367
Bram Moolenaar053bb602013-05-20 13:55:21 +02002368#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369#define POP() st_pop(&stackp, stack); \
2370 if (stackp < stack) \
2371 { \
2372 st_error(postfix, end, p); \
2373 return NULL; \
2374 }
2375
2376 if (nfa_calc_size == FALSE)
2377 {
2378 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002379 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002380 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002381 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002382 }
2383
2384 for (p = postfix; p < end; ++p)
2385 {
2386 switch (*p)
2387 {
2388 case NFA_CONCAT:
2389 /* Catenation.
2390 * Pay attention: this operator does not exist
2391 * in the r.e. itself (it is implicit, really).
2392 * It is added when r.e. is translated to postfix
2393 * form in re2post().
2394 *
2395 * No new state added here. */
2396 if (nfa_calc_size == TRUE)
2397 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002398 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002399 break;
2400 }
2401 e2 = POP();
2402 e1 = POP();
2403 patch(e1.out, e2.start);
2404 PUSH(frag(e1.start, e2.out));
2405 break;
2406
2407 case NFA_NOT:
2408 /* Negation of a character */
2409 if (nfa_calc_size == TRUE)
2410 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002411 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412 break;
2413 }
2414 e1 = POP();
2415 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002416#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002417 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002418 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002419#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420 PUSH(e1);
2421 break;
2422
2423 case NFA_OR:
2424 /* Alternation */
2425 if (nfa_calc_size == TRUE)
2426 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002427 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428 break;
2429 }
2430 e2 = POP();
2431 e1 = POP();
2432 s = new_state(NFA_SPLIT, e1.start, e2.start);
2433 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002434 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002435 PUSH(frag(s, append(e1.out, e2.out)));
2436 break;
2437
2438 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002439 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440 if (nfa_calc_size == TRUE)
2441 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002442 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 break;
2444 }
2445 e = POP();
2446 s = new_state(NFA_SPLIT, e.start, NULL);
2447 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002448 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002449 patch(e.out, s);
2450 PUSH(frag(s, list1(&s->out1)));
2451 break;
2452
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002453 case NFA_STAR_NONGREEDY:
2454 /* Zero or more, prefer zero */
2455 if (nfa_calc_size == TRUE)
2456 {
2457 nstate++;
2458 break;
2459 }
2460 e = POP();
2461 s = new_state(NFA_SPLIT, NULL, e.start);
2462 if (s == NULL)
2463 goto theend;
2464 patch(e.out, s);
2465 PUSH(frag(s, list1(&s->out)));
2466 break;
2467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002468 case NFA_QUEST:
2469 /* one or zero atoms=> greedy match */
2470 if (nfa_calc_size == TRUE)
2471 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002472 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 break;
2474 }
2475 e = POP();
2476 s = new_state(NFA_SPLIT, e.start, NULL);
2477 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002478 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 PUSH(frag(s, append(e.out, list1(&s->out1))));
2480 break;
2481
2482 case NFA_QUEST_NONGREEDY:
2483 /* zero or one atoms => non-greedy match */
2484 if (nfa_calc_size == TRUE)
2485 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002486 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002487 break;
2488 }
2489 e = POP();
2490 s = new_state(NFA_SPLIT, NULL, e.start);
2491 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002492 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002493 PUSH(frag(s, append(e.out, list1(&s->out))));
2494 break;
2495
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496 case NFA_SKIP_CHAR:
2497 /* Symbol of 0-length, Used in a repetition
2498 * with max/min count of 0 */
2499 if (nfa_calc_size == TRUE)
2500 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002501 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502 break;
2503 }
2504 s = new_state(NFA_SKIP_CHAR, NULL, NULL);
2505 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002506 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002507 PUSH(frag(s, list1(&s->out)));
2508 break;
2509
2510 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002511 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002512 case NFA_PREV_ATOM_JUST_BEFORE:
2513 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002514 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002515 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002516 * The \@<= operator: match for the preceding atom.
2517 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002519 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520
2521 if (nfa_calc_size == TRUE)
2522 {
2523 nstate += 2;
2524 break;
2525 }
2526 e = POP();
2527 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL);
2528 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002529 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530 patch(e.out, s1);
2531
2532 s = new_state(NFA_START_INVISIBLE, e.start, s1);
2533 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002534 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002535 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2536 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002537 {
2538 s->negated = TRUE;
2539 s1->negated = TRUE;
2540 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002541 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2542 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2543 {
2544 s->val = *++p; /* get the count */
2545 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2546 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002547
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 PUSH(frag(s, list1(&s1->out)));
2549 break;
2550
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002551#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002552 case NFA_COMPOSING: /* char with composing char */
2553#if 0
2554 /* TODO */
2555 if (regflags & RF_ICOMBINE)
2556 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002557 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002558 }
2559#endif
2560 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002561#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002562
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002563 case NFA_MOPEN: /* \( \) Submatch */
2564 case NFA_MOPEN1:
2565 case NFA_MOPEN2:
2566 case NFA_MOPEN3:
2567 case NFA_MOPEN4:
2568 case NFA_MOPEN5:
2569 case NFA_MOPEN6:
2570 case NFA_MOPEN7:
2571 case NFA_MOPEN8:
2572 case NFA_MOPEN9:
2573#ifdef FEAT_SYN_HL
2574 case NFA_ZOPEN: /* \z( \) Submatch */
2575 case NFA_ZOPEN1:
2576 case NFA_ZOPEN2:
2577 case NFA_ZOPEN3:
2578 case NFA_ZOPEN4:
2579 case NFA_ZOPEN5:
2580 case NFA_ZOPEN6:
2581 case NFA_ZOPEN7:
2582 case NFA_ZOPEN8:
2583 case NFA_ZOPEN9:
2584#endif
2585 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586 if (nfa_calc_size == TRUE)
2587 {
2588 nstate += 2;
2589 break;
2590 }
2591
2592 mopen = *p;
2593 switch (*p)
2594 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002595 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2596#ifdef FEAT_SYN_HL
2597 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2598 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2599 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2600 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2601 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2602 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2603 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2604 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2605 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2606 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2607#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002608#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002609 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002610#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002611 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002612 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002613 mclose = *p + NSUBEXP;
2614 break;
2615 }
2616
2617 /* Allow "NFA_MOPEN" as a valid postfix representation for
2618 * the empty regexp "". In this case, the NFA will be
2619 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2620 * empty groups of parenthesis, and empty mbyte chars */
2621 if (stackp == stack)
2622 {
2623 s = new_state(mopen, NULL, NULL);
2624 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002625 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002626 s1 = new_state(mclose, NULL, NULL);
2627 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002628 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002629 patch(list1(&s->out), s1);
2630 PUSH(frag(s, list1(&s1->out)));
2631 break;
2632 }
2633
2634 /* At least one node was emitted before NFA_MOPEN, so
2635 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2636 e = POP();
2637 s = new_state(mopen, e.start, NULL); /* `(' */
2638 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002639 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002640
2641 s1 = new_state(mclose, NULL, NULL); /* `)' */
2642 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002643 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002644 patch(e.out, s1);
2645
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002646#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002647 if (mopen == NFA_COMPOSING)
2648 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002650#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651
2652 PUSH(frag(s, list1(&s1->out)));
2653 break;
2654
Bram Moolenaar5714b802013-05-28 22:03:20 +02002655 case NFA_BACKREF1:
2656 case NFA_BACKREF2:
2657 case NFA_BACKREF3:
2658 case NFA_BACKREF4:
2659 case NFA_BACKREF5:
2660 case NFA_BACKREF6:
2661 case NFA_BACKREF7:
2662 case NFA_BACKREF8:
2663 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002664#ifdef FEAT_SYN_HL
2665 case NFA_ZREF1:
2666 case NFA_ZREF2:
2667 case NFA_ZREF3:
2668 case NFA_ZREF4:
2669 case NFA_ZREF5:
2670 case NFA_ZREF6:
2671 case NFA_ZREF7:
2672 case NFA_ZREF8:
2673 case NFA_ZREF9:
2674#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002675 if (nfa_calc_size == TRUE)
2676 {
2677 nstate += 2;
2678 break;
2679 }
2680 s = new_state(*p, NULL, NULL);
2681 if (s == NULL)
2682 goto theend;
2683 s1 = new_state(NFA_SKIP, NULL, NULL);
2684 if (s1 == NULL)
2685 goto theend;
2686 patch(list1(&s->out), s1);
2687 PUSH(frag(s, list1(&s1->out)));
2688 break;
2689
Bram Moolenaar423532e2013-05-29 21:14:42 +02002690 case NFA_LNUM:
2691 case NFA_LNUM_GT:
2692 case NFA_LNUM_LT:
2693 case NFA_VCOL:
2694 case NFA_VCOL_GT:
2695 case NFA_VCOL_LT:
2696 case NFA_COL:
2697 case NFA_COL_GT:
2698 case NFA_COL_LT:
2699 if (nfa_calc_size == TRUE)
2700 {
2701 nstate += 1;
2702 break;
2703 }
2704 e1 = POP();
2705 s = new_state(*p, NULL, NULL);
2706 if (s == NULL)
2707 goto theend;
2708 s->val = e1.start->c;
2709 PUSH(frag(s, list1(&s->out)));
2710 break;
2711
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712 case NFA_ZSTART:
2713 case NFA_ZEND:
2714 default:
2715 /* Operands */
2716 if (nfa_calc_size == TRUE)
2717 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002718 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002719 break;
2720 }
2721 s = new_state(*p, NULL, NULL);
2722 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002723 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002724 PUSH(frag(s, list1(&s->out)));
2725 break;
2726
2727 } /* switch(*p) */
2728
2729 } /* for(p = postfix; *p; ++p) */
2730
2731 if (nfa_calc_size == TRUE)
2732 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002733 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002734 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002735 }
2736
2737 e = POP();
2738 if (stackp != stack)
2739 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2740
2741 if (istate >= nstate)
2742 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 matchstate = &state_ptr[istate++]; /* the match state */
2745 matchstate->c = NFA_MATCH;
2746 matchstate->out = matchstate->out1 = NULL;
2747
2748 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002749 ret = e.start;
2750
2751theend:
2752 vim_free(stack);
2753 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002754
2755#undef POP1
2756#undef PUSH1
2757#undef POP2
2758#undef PUSH2
2759#undef POP
2760#undef PUSH
2761}
2762
2763/****************************************************************
2764 * NFA execution code.
2765 ****************************************************************/
2766
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002767typedef struct
2768{
2769 int in_use; /* number of subexpr with useful info */
2770
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002771 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002772 union
2773 {
2774 struct multipos
2775 {
2776 lpos_T start;
2777 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002778 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002779 struct linepos
2780 {
2781 char_u *start;
2782 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002783 } line[NSUBEXP];
2784 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002785} regsub_T;
2786
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002787typedef struct
2788{
2789 regsub_T norm; /* \( .. \) matches */
2790#ifdef FEAT_SYN_HL
2791 regsub_T synt; /* \z( .. \) matches */
2792#endif
2793} regsubs_T;
2794
Bram Moolenaar963fee22013-05-26 21:47:28 +02002795/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002796typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002797{
2798 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002799 int count;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002800 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002801} nfa_thread_T;
2802
Bram Moolenaar963fee22013-05-26 21:47:28 +02002803/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002804typedef struct
2805{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002806 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002807 int n; /* nr of states currently in "t" */
2808 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002809 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002810} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811
Bram Moolenaar5714b802013-05-28 22:03:20 +02002812#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002813static void log_subsexpr __ARGS((regsubs_T *subs));
2814static void log_subexpr __ARGS((regsub_T *sub));
2815
2816 static void
2817log_subsexpr(subs)
2818 regsubs_T *subs;
2819{
2820 log_subexpr(&subs->norm);
2821# ifdef FEAT_SYN_HL
2822 log_subexpr(&subs->synt);
2823# endif
2824}
2825
Bram Moolenaar5714b802013-05-28 22:03:20 +02002826 static void
2827log_subexpr(sub)
2828 regsub_T *sub;
2829{
2830 int j;
2831
2832 for (j = 0; j < sub->in_use; j++)
2833 if (REG_MULTI)
2834 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2835 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002836 sub->list.multi[j].start.col,
2837 (int)sub->list.multi[j].start.lnum,
2838 sub->list.multi[j].end.col,
2839 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002840 else
2841 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2842 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002843 (char *)sub->list.line[j].start,
2844 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002845 fprintf(log_fd, "\n");
2846}
2847#endif
2848
Bram Moolenaar963fee22013-05-26 21:47:28 +02002849/* Used during execution: whether a match has been found. */
2850static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002851
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002852static void clear_sub __ARGS((regsub_T *sub));
2853static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2854static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002855static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002856static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
2857static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int *ip));
2858
2859 static void
2860clear_sub(sub)
2861 regsub_T *sub;
2862{
2863 if (REG_MULTI)
2864 /* Use 0xff to set lnum to -1 */
2865 vim_memset(sub->list.multi, 0xff,
2866 sizeof(struct multipos) * nfa_nsubexpr);
2867 else
2868 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2869 sub->in_use = 0;
2870}
2871
2872/*
2873 * Copy the submatches from "from" to "to".
2874 */
2875 static void
2876copy_sub(to, from)
2877 regsub_T *to;
2878 regsub_T *from;
2879{
2880 to->in_use = from->in_use;
2881 if (from->in_use > 0)
2882 {
2883 /* Copy the match start and end positions. */
2884 if (REG_MULTI)
2885 mch_memmove(&to->list.multi[0],
2886 &from->list.multi[0],
2887 sizeof(struct multipos) * from->in_use);
2888 else
2889 mch_memmove(&to->list.line[0],
2890 &from->list.line[0],
2891 sizeof(struct linepos) * from->in_use);
2892 }
2893}
2894
2895/*
2896 * Like copy_sub() but exclude the main match.
2897 */
2898 static void
2899copy_sub_off(to, from)
2900 regsub_T *to;
2901 regsub_T *from;
2902{
2903 if (to->in_use < from->in_use)
2904 to->in_use = from->in_use;
2905 if (from->in_use > 1)
2906 {
2907 /* Copy the match start and end positions. */
2908 if (REG_MULTI)
2909 mch_memmove(&to->list.multi[1],
2910 &from->list.multi[1],
2911 sizeof(struct multipos) * (from->in_use - 1));
2912 else
2913 mch_memmove(&to->list.line[1],
2914 &from->list.line[1],
2915 sizeof(struct linepos) * (from->in_use - 1));
2916 }
2917}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918
Bram Moolenaar428e9872013-05-30 17:05:39 +02002919/*
2920 * Return TRUE if "sub1" and "sub2" have the same positions.
2921 */
2922 static int
2923sub_equal(sub1, sub2)
2924 regsub_T *sub1;
2925 regsub_T *sub2;
2926{
2927 int i;
2928 int todo;
2929 linenr_T s1, e1;
2930 linenr_T s2, e2;
2931 char_u *sp1, *ep1;
2932 char_u *sp2, *ep2;
2933
2934 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2935 if (REG_MULTI)
2936 {
2937 for (i = 0; i < todo; ++i)
2938 {
2939 if (i < sub1->in_use)
2940 {
2941 s1 = sub1->list.multi[i].start.lnum;
2942 e1 = sub1->list.multi[i].end.lnum;
2943 }
2944 else
2945 {
2946 s1 = 0;
2947 e1 = 0;
2948 }
2949 if (i < sub2->in_use)
2950 {
2951 s2 = sub2->list.multi[i].start.lnum;
2952 e2 = sub2->list.multi[i].end.lnum;
2953 }
2954 else
2955 {
2956 s2 = 0;
2957 e2 = 0;
2958 }
2959 if (s1 != s2 || e1 != e2)
2960 return FALSE;
2961 if (s1 != 0 && sub1->list.multi[i].start.col
2962 != sub2->list.multi[i].start.col)
2963 return FALSE;
2964 if (e1 != 0 && sub1->list.multi[i].end.col
2965 != sub2->list.multi[i].end.col)
2966 return FALSE;
2967 }
2968 }
2969 else
2970 {
2971 for (i = 0; i < todo; ++i)
2972 {
2973 if (i < sub1->in_use)
2974 {
2975 sp1 = sub1->list.line[i].start;
2976 ep1 = sub1->list.line[i].end;
2977 }
2978 else
2979 {
2980 sp1 = NULL;
2981 ep1 = NULL;
2982 }
2983 if (i < sub2->in_use)
2984 {
2985 sp2 = sub2->list.line[i].start;
2986 ep2 = sub2->list.line[i].end;
2987 }
2988 else
2989 {
2990 sp2 = NULL;
2991 ep2 = NULL;
2992 }
2993 if (sp1 != sp2 || ep1 != ep2)
2994 return FALSE;
2995 }
2996 }
2997
2998 return TRUE;
2999}
3000
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003001 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003002addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003003 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003004 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003005 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003006 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003007{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003008 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003009 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003010 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003011 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003012 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003013 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003014 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003015#ifdef ENABLE_LOG
3016 int did_print = FALSE;
3017#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003018
3019 if (l == NULL || state == NULL)
3020 return;
3021
3022 switch (state->c)
3023 {
3024 case NFA_SPLIT:
3025 case NFA_NOT:
3026 case NFA_NOPEN:
3027 case NFA_NCLOSE:
3028 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003029 case NFA_MCLOSE1:
3030 case NFA_MCLOSE2:
3031 case NFA_MCLOSE3:
3032 case NFA_MCLOSE4:
3033 case NFA_MCLOSE5:
3034 case NFA_MCLOSE6:
3035 case NFA_MCLOSE7:
3036 case NFA_MCLOSE8:
3037 case NFA_MCLOSE9:
3038#ifdef FEAT_SYN_HL
3039 case NFA_ZCLOSE:
3040 case NFA_ZCLOSE1:
3041 case NFA_ZCLOSE2:
3042 case NFA_ZCLOSE3:
3043 case NFA_ZCLOSE4:
3044 case NFA_ZCLOSE5:
3045 case NFA_ZCLOSE6:
3046 case NFA_ZCLOSE7:
3047 case NFA_ZCLOSE8:
3048 case NFA_ZCLOSE9:
3049#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003050 /* These nodes are not added themselves but their "out" and/or
3051 * "out1" may be added below. */
3052 break;
3053
3054 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003055 case NFA_MOPEN1:
3056 case NFA_MOPEN2:
3057 case NFA_MOPEN3:
3058 case NFA_MOPEN4:
3059 case NFA_MOPEN5:
3060 case NFA_MOPEN6:
3061 case NFA_MOPEN7:
3062 case NFA_MOPEN8:
3063 case NFA_MOPEN9:
3064#ifdef FEAT_SYN_HL
3065 case NFA_ZOPEN:
3066 case NFA_ZOPEN1:
3067 case NFA_ZOPEN2:
3068 case NFA_ZOPEN3:
3069 case NFA_ZOPEN4:
3070 case NFA_ZOPEN5:
3071 case NFA_ZOPEN6:
3072 case NFA_ZOPEN7:
3073 case NFA_ZOPEN8:
3074 case NFA_ZOPEN9:
3075#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003076 /* These nodes do not need to be added, but we need to bail out
3077 * when it was tried to be added to this list before. */
3078 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003079 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003080 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003081 break;
3082
3083 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003084 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003085 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003086 /* This state is already in the list, don't add it again,
3087 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003088 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003089 {
3090skip_add:
3091#ifdef ENABLE_LOG
3092 nfa_set_code(state->c);
3093 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3094 abs(state->id), l->id, state->c, code);
3095#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003096 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003097 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003098
3099 /* See if the same state is already in the list with the same
3100 * positions. */
3101 for (i = 0; i < l->n; ++i)
3102 {
3103 thread = &l->t[i];
3104 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003105 && sub_equal(&thread->subs.norm, &subs->norm)
3106#ifdef FEAT_SYN_HL
3107 && sub_equal(&thread->subs.synt, &subs->synt)
3108#endif
3109 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003110 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003111 }
3112 }
3113
3114 /* when there are backreferences the number of states may be (a
3115 * lot) bigger */
3116 if (nfa_has_backref && l->n == l->len)
3117 {
3118 int newlen = l->len * 3 / 2 + 50;
3119
3120 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3121 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003123
3124 /* add the state to the list */
3125 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003126 thread = &l->t[l->n++];
3127 thread->state = state;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003128 copy_sub(&thread->subs.norm, &subs->norm);
3129#ifdef FEAT_SYN_HL
3130 copy_sub(&thread->subs.synt, &subs->synt);
3131#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003132#ifdef ENABLE_LOG
3133 {
3134 int col;
3135
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003136 if (thread->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003137 col = -1;
3138 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003139 col = thread->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003140 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003141 col = (int)(thread->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003142 nfa_set_code(state->c);
3143 fprintf(log_fd, "> Adding state %d to list %d. char %d: %s (start col %d)\n",
3144 abs(state->id), l->id, state->c, code, col);
3145 did_print = TRUE;
3146 }
3147#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003148 }
3149
3150#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003151 if (!did_print)
3152 {
3153 int col;
3154
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003155 if (subs->norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003156 col = -1;
3157 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003158 col = subs->norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003159 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003160 col = (int)(subs->norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003161 nfa_set_code(state->c);
3162 fprintf(log_fd, "> Processing state %d for list %d. char %d: %s (start col %d)\n",
3163 abs(state->id), l->id, state->c, code, col);
3164 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003165#endif
3166 switch (state->c)
3167 {
3168 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003169 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003170 break;
3171
3172 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003173 /* order matters here */
3174 addstate(l, state->out, subs, off);
3175 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003176 break;
3177
Bram Moolenaar5714b802013-05-28 22:03:20 +02003178 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 case NFA_NOPEN:
3180 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003181 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003182 break;
3183
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003184 case NFA_MOPEN:
3185 case NFA_MOPEN1:
3186 case NFA_MOPEN2:
3187 case NFA_MOPEN3:
3188 case NFA_MOPEN4:
3189 case NFA_MOPEN5:
3190 case NFA_MOPEN6:
3191 case NFA_MOPEN7:
3192 case NFA_MOPEN8:
3193 case NFA_MOPEN9:
3194#ifdef FEAT_SYN_HL
3195 case NFA_ZOPEN:
3196 case NFA_ZOPEN1:
3197 case NFA_ZOPEN2:
3198 case NFA_ZOPEN3:
3199 case NFA_ZOPEN4:
3200 case NFA_ZOPEN5:
3201 case NFA_ZOPEN6:
3202 case NFA_ZOPEN7:
3203 case NFA_ZOPEN8:
3204 case NFA_ZOPEN9:
3205#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003208 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003210 sub = &subs->norm;
3211 }
3212#ifdef FEAT_SYN_HL
3213 else if (state->c >= NFA_ZOPEN)
3214 {
3215 subidx = state->c - NFA_ZOPEN;
3216 sub = &subs->synt;
3217 }
3218#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003219 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003220 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003221 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003222 sub = &subs->norm;
3223 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003225 /* Set the position (with "off") in the subexpression. Save and
3226 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003227 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228 if (REG_MULTI)
3229 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003230 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003231 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003232 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003233 save_in_use = -1;
3234 }
3235 else
3236 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003237 save_in_use = sub->in_use;
3238 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003239 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003240 sub->list.multi[i].start.lnum = -1;
3241 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003242 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003243 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003244 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003245 if (off == -1)
3246 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003247 sub->list.multi[subidx].start.lnum = reglnum + 1;
3248 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003249 }
3250 else
3251 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003252 sub->list.multi[subidx].start.lnum = reglnum;
3253 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003254 (colnr_T)(reginput - regline + off);
3255 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 }
3257 else
3258 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003259 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003260 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003261 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003262 save_in_use = -1;
3263 }
3264 else
3265 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003266 save_in_use = sub->in_use;
3267 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003268 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003269 sub->list.line[i].start = NULL;
3270 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003271 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003272 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003273 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003274 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 }
3276
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003277 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003279 if (save_in_use == -1)
3280 {
3281 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003282 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003283 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003284 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003285 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003287 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 break;
3289
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003290 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003291 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003293 /* Do not overwrite the position set by \ze. If no \ze
3294 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003295 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 break;
3297 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003298 case NFA_MCLOSE1:
3299 case NFA_MCLOSE2:
3300 case NFA_MCLOSE3:
3301 case NFA_MCLOSE4:
3302 case NFA_MCLOSE5:
3303 case NFA_MCLOSE6:
3304 case NFA_MCLOSE7:
3305 case NFA_MCLOSE8:
3306 case NFA_MCLOSE9:
3307#ifdef FEAT_SYN_HL
3308 case NFA_ZCLOSE:
3309 case NFA_ZCLOSE1:
3310 case NFA_ZCLOSE2:
3311 case NFA_ZCLOSE3:
3312 case NFA_ZCLOSE4:
3313 case NFA_ZCLOSE5:
3314 case NFA_ZCLOSE6:
3315 case NFA_ZCLOSE7:
3316 case NFA_ZCLOSE8:
3317 case NFA_ZCLOSE9:
3318#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003321 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003323 sub = &subs->norm;
3324 }
3325#ifdef FEAT_SYN_HL
3326 else if (state->c >= NFA_ZCLOSE)
3327 {
3328 subidx = state->c - NFA_ZCLOSE;
3329 sub = &subs->synt;
3330 }
3331#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003332 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003333 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003334 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003335 sub = &subs->norm;
3336 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003337
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003338 /* We don't fill in gaps here, there must have been an MOPEN that
3339 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003340 save_in_use = sub->in_use;
3341 if (sub->in_use <= subidx)
3342 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003343 if (REG_MULTI)
3344 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003345 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003346 if (off == -1)
3347 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003348 sub->list.multi[subidx].end.lnum = reglnum + 1;
3349 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003350 }
3351 else
3352 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003353 sub->list.multi[subidx].end.lnum = reglnum;
3354 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003355 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003356 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 }
3358 else
3359 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003360 save_ptr = sub->list.line[subidx].end;
3361 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 }
3363
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003364 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365
3366 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003367 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003369 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003370 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 break;
3372 }
3373}
3374
3375/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003376 * Like addstate(), but the new state(s) are put at position "*ip".
3377 * Used for zero-width matches, next state to use is the added one.
3378 * This makes sure the order of states to be tried does not change, which
3379 * matters for alternatives.
3380 */
3381 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003382addstate_here(l, state, subs, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003383 nfa_list_T *l; /* runtime state list */
3384 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003385 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003386 int *ip;
3387{
3388 int tlen = l->n;
3389 int count;
3390 int i = *ip;
3391
3392 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003393 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003394
3395 /* when "*ip" was at the end of the list, nothing to do */
3396 if (i + 1 == tlen)
3397 return;
3398
3399 /* re-order to put the new state at the current position */
3400 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003401 if (count == 1)
3402 {
3403 /* overwrite the current state */
3404 l->t[i] = l->t[l->n - 1];
3405 }
3406 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003407 {
3408 /* make space for new states, then move them from the
3409 * end to the current position */
3410 mch_memmove(&(l->t[i + count]),
3411 &(l->t[i + 1]),
3412 sizeof(nfa_thread_T) * (l->n - i - 1));
3413 mch_memmove(&(l->t[i]),
3414 &(l->t[l->n - 1]),
3415 sizeof(nfa_thread_T) * count);
3416 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003417 --l->n;
3418 *ip = i - 1;
3419}
3420
3421/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 * Check character class "class" against current character c.
3423 */
3424 static int
3425check_char_class(class, c)
3426 int class;
3427 int c;
3428{
3429 switch (class)
3430 {
3431 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003432 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003433 return OK;
3434 break;
3435 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003436 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003437 return OK;
3438 break;
3439 case NFA_CLASS_BLANK:
3440 if (c == ' ' || c == '\t')
3441 return OK;
3442 break;
3443 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003444 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445 return OK;
3446 break;
3447 case NFA_CLASS_DIGIT:
3448 if (VIM_ISDIGIT(c))
3449 return OK;
3450 break;
3451 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003452 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453 return OK;
3454 break;
3455 case NFA_CLASS_LOWER:
3456 if (MB_ISLOWER(c))
3457 return OK;
3458 break;
3459 case NFA_CLASS_PRINT:
3460 if (vim_isprintc(c))
3461 return OK;
3462 break;
3463 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003464 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 return OK;
3466 break;
3467 case NFA_CLASS_SPACE:
3468 if ((c >=9 && c <= 13) || (c == ' '))
3469 return OK;
3470 break;
3471 case NFA_CLASS_UPPER:
3472 if (MB_ISUPPER(c))
3473 return OK;
3474 break;
3475 case NFA_CLASS_XDIGIT:
3476 if (vim_isxdigit(c))
3477 return OK;
3478 break;
3479 case NFA_CLASS_TAB:
3480 if (c == '\t')
3481 return OK;
3482 break;
3483 case NFA_CLASS_RETURN:
3484 if (c == '\r')
3485 return OK;
3486 break;
3487 case NFA_CLASS_BACKSPACE:
3488 if (c == '\b')
3489 return OK;
3490 break;
3491 case NFA_CLASS_ESCAPE:
3492 if (c == '\033')
3493 return OK;
3494 break;
3495
3496 default:
3497 /* should not be here :P */
3498 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3499 }
3500 return FAIL;
3501}
3502
Bram Moolenaar5714b802013-05-28 22:03:20 +02003503static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3504
3505/*
3506 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003507 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003508 */
3509 static int
3510match_backref(sub, subidx, bytelen)
3511 regsub_T *sub; /* pointers to subexpressions */
3512 int subidx;
3513 int *bytelen; /* out: length of match in bytes */
3514{
3515 int len;
3516
3517 if (sub->in_use <= subidx)
3518 {
3519retempty:
3520 /* backref was not set, match an empty string */
3521 *bytelen = 0;
3522 return TRUE;
3523 }
3524
3525 if (REG_MULTI)
3526 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003527 if (sub->list.multi[subidx].start.lnum < 0
3528 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003529 goto retempty;
3530 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003531 len = sub->list.multi[subidx].end.col
3532 - sub->list.multi[subidx].start.col;
3533 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003534 reginput, &len) == 0)
3535 {
3536 *bytelen = len;
3537 return TRUE;
3538 }
3539 }
3540 else
3541 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003542 if (sub->list.line[subidx].start == NULL
3543 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003544 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003545 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3546 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003547 {
3548 *bytelen = len;
3549 return TRUE;
3550 }
3551 }
3552 return FALSE;
3553}
3554
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003555#ifdef FEAT_SYN_HL
3556
3557static int match_zref __ARGS((int subidx, int *bytelen));
3558
3559/*
3560 * Check for a match with \z subexpression "subidx".
3561 * Return TRUE if it matches.
3562 */
3563 static int
3564match_zref(subidx, bytelen)
3565 int subidx;
3566 int *bytelen; /* out: length of match in bytes */
3567{
3568 int len;
3569
3570 cleanup_zsubexpr();
3571 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3572 {
3573 /* backref was not set, match an empty string */
3574 *bytelen = 0;
3575 return TRUE;
3576 }
3577
3578 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3579 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3580 {
3581 *bytelen = len;
3582 return TRUE;
3583 }
3584 return FALSE;
3585}
3586#endif
3587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588/*
3589 * Set all NFA nodes' list ID equal to -1.
3590 */
3591 static void
3592nfa_set_neg_listids(start)
3593 nfa_state_T *start;
3594{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003595 if (start != NULL && start->lastlist >= 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596 {
3597 start->lastlist = -1;
3598 nfa_set_neg_listids(start->out);
3599 nfa_set_neg_listids(start->out1);
3600 }
3601}
3602
3603/*
3604 * Set all NFA nodes' list ID equal to 0.
3605 */
3606 static void
3607nfa_set_null_listids(start)
3608 nfa_state_T *start;
3609{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003610 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 {
3612 start->lastlist = 0;
3613 nfa_set_null_listids(start->out);
3614 nfa_set_null_listids(start->out1);
3615 }
3616}
3617
3618/*
3619 * Save list IDs for all NFA states in "list".
3620 */
3621 static void
3622nfa_save_listids(start, list)
3623 nfa_state_T *start;
3624 int *list;
3625{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003626 if (start != NULL && start->lastlist != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627 {
3628 list[abs(start->id)] = start->lastlist;
3629 start->lastlist = -1;
3630 nfa_save_listids(start->out, list);
3631 nfa_save_listids(start->out1, list);
3632 }
3633}
3634
3635/*
3636 * Restore list IDs from "list" to all NFA states.
3637 */
3638 static void
3639nfa_restore_listids(start, list)
3640 nfa_state_T *start;
3641 int *list;
3642{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003643 if (start != NULL && start->lastlist == -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 {
3645 start->lastlist = list[abs(start->id)];
3646 nfa_restore_listids(start->out, list);
3647 nfa_restore_listids(start->out1, list);
3648 }
3649}
3650
Bram Moolenaar423532e2013-05-29 21:14:42 +02003651 static int
3652nfa_re_num_cmp(val, op, pos)
3653 long_u val;
3654 int op;
3655 long_u pos;
3656{
3657 if (op == 1) return pos > val;
3658 if (op == 2) return pos < val;
3659 return val == pos;
3660}
3661
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003662static int nfa_regmatch __ARGS((nfa_state_T *start, regsubs_T *submatch, regsubs_T *m, save_se_T *endp));
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003663
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003664/*
3665 * Main matching routine.
3666 *
3667 * Run NFA to determine whether it matches reginput.
3668 *
Bram Moolenaar61602c52013-06-01 19:54:43 +02003669 * When "endp" is not NULL it is a required end-of-match position.
3670 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 * Return TRUE if there is a match, FALSE otherwise.
3672 * Note: Caller must ensure that: start != NULL.
3673 */
3674 static int
Bram Moolenaar61602c52013-06-01 19:54:43 +02003675nfa_regmatch(start, submatch, m, endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003677 regsubs_T *submatch;
3678 regsubs_T *m;
Bram Moolenaar61602c52013-06-01 19:54:43 +02003679 save_se_T *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003681 int result;
3682 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003683 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003684 int go_to_nextline = FALSE;
3685 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003686 nfa_list_T list[3];
3687 nfa_list_T *listtbl[2][2];
3688 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003690 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003691 nfa_list_T *thislist;
3692 nfa_list_T *nextlist;
3693 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003695#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003696 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697
3698 if (debug == NULL)
3699 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003700 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003701 return FALSE;
3702 }
3703#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003704 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003706 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003707 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003708 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3709 list[0].len = nstate + 1;
3710 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3711 list[1].len = nstate + 1;
3712 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3713 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3715 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003716
3717#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003718 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003719 if (log_fd != NULL)
3720 {
3721 fprintf(log_fd, "**********************************\n");
3722 nfa_set_code(start->c);
3723 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3724 abs(start->id), code);
3725 fprintf(log_fd, "**********************************\n");
3726 }
3727 else
3728 {
3729 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3730 log_fd = stderr;
3731 }
3732#endif
3733
3734 thislist = &list[0];
3735 thislist->n = 0;
3736 nextlist = &list[1];
3737 nextlist->n = 0;
3738 neglist = &list[2];
3739 neglist->n = 0;
3740#ifdef ENABLE_LOG
3741 fprintf(log_fd, "(---) STARTSTATE\n");
3742#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003743 thislist->id = listid;
3744 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003745
3746 /* There are two cases when the NFA advances: 1. input char matches the
3747 * NFA node and 2. input char does not match the NFA node, but the next
3748 * node is NFA_NOT. The following macro calls addstate() according to
3749 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3750 listtbl[0][0] = NULL;
3751 listtbl[0][1] = neglist;
3752 listtbl[1][0] = nextlist;
3753 listtbl[1][1] = NULL;
3754#define ADD_POS_NEG_STATE(node) \
3755 ll = listtbl[result ? 1 : 0][node->negated]; \
3756 if (ll != NULL) \
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003757 addstate(ll, node->out , &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003758
3759
3760 /*
3761 * Run for each character.
3762 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003763 for (;;)
3764 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003765 int curc;
3766 int clen;
3767
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768#ifdef FEAT_MBYTE
3769 if (has_mbyte)
3770 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003771 curc = (*mb_ptr2char)(reginput);
3772 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003773 }
3774 else
3775#endif
3776 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003777 curc = *reginput;
3778 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003780 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003781 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003782 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003783 go_to_nextline = FALSE;
3784 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003785
3786 /* swap lists */
3787 thislist = &list[flag];
3788 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003789 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790 listtbl[1][0] = nextlist;
3791 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003792 thislist->id = listid;
3793 nextlist->id = listid + 1;
3794 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795
3796#ifdef ENABLE_LOG
3797 fprintf(log_fd, "------------------------------------------\n");
3798 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003799 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003801 {
3802 int i;
3803
3804 for (i = 0; i < thislist->n; i++)
3805 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3806 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807 fprintf(log_fd, "\n");
3808#endif
3809
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003810#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003811 fprintf(debug, "\n-------------------\n");
3812#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003813 /*
3814 * If the state lists are empty we can stop.
3815 */
3816 if (thislist->n == 0 && neglist->n == 0)
3817 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003818
3819 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003820 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003821 {
3822 if (neglist->n > 0)
3823 {
3824 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003825 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003826 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003827 }
3828 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003829 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003830
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003831#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003832 nfa_set_code(t->state->c);
3833 fprintf(debug, "%s, ", code);
3834#endif
3835#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003836 {
3837 int col;
3838
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003839 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003840 col = -1;
3841 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003842 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003843 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003844 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003845 nfa_set_code(t->state->c);
3846 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3847 abs(t->state->id), (int)t->state->c, code, col);
3848 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849#endif
3850
3851 /*
3852 * Handle the possible codes of the current state.
3853 * The most important is NFA_MATCH.
3854 */
3855 switch (t->state->c)
3856 {
3857 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003858 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003859 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003860 copy_sub(&submatch->norm, &t->subs.norm);
3861#ifdef FEAT_SYN_HL
3862 copy_sub(&submatch->synt, &t->subs.synt);
3863#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003865 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003866#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003867 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003868 * states at this position. When the list of states is going
3869 * to be empty quit without advancing, so that "reginput" is
3870 * correct. */
3871 if (nextlist->n == 0 && neglist->n == 0)
3872 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003873 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003874 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875
3876 case NFA_END_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003877 /* This is only encountered after a NFA_START_INVISIBLE or
3878 * NFA_START_INVISIBLE_BEFORE node.
3879 * They surround a zero-width group, used with "\@=", "\&",
3880 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881 * If we got here, it means that the current "invisible" group
3882 * finished successfully, so return control to the parent
3883 * nfa_regmatch(). Submatches are stored in *m, and used in
3884 * the parent call. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003885 if (start->c == NFA_MOPEN)
Bram Moolenaar61602c52013-06-01 19:54:43 +02003886 /* TODO: do we ever get here? */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003887 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003888 else
3889 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003890#ifdef ENABLE_LOG
3891 if (endp != NULL)
3892 {
3893 if (REG_MULTI)
3894 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
3895 (int)reglnum,
3896 (int)endp->se_u.pos.lnum,
3897 (int)(reginput - regline),
3898 endp->se_u.pos.col);
3899 else
3900 fprintf(log_fd, "Current col: %d, endp col: %d\n",
3901 (int)(reginput - regline),
3902 (int)(endp->se_u.ptr - reginput));
3903 }
3904#endif
3905 /* It's only a match if it ends at "endp" */
3906 if (endp != NULL && (REG_MULTI
3907 ? (reglnum != endp->se_u.pos.lnum
3908 || (int)(reginput - regline)
3909 != endp->se_u.pos.col)
3910 : reginput != endp->se_u.ptr))
3911 break;
3912
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003913 /* do not set submatches for \@! */
3914 if (!t->state->negated)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003915 {
3916 copy_sub(&m->norm, &t->subs.norm);
3917#ifdef FEAT_SYN_HL
3918 copy_sub(&m->synt, &t->subs.synt);
3919#endif
3920 }
Bram Moolenaar963fee22013-05-26 21:47:28 +02003921 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 }
3923 break;
3924
3925 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003926 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003927 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003928 char_u *save_reginput = reginput;
3929 char_u *save_regline = regline;
3930 int save_reglnum = reglnum;
3931 int save_nfa_match = nfa_match;
3932 save_se_T endpos;
3933 save_se_T *endposp = NULL;
3934
3935 if (t->state->c == NFA_START_INVISIBLE_BEFORE)
3936 {
3937 /* The recursive match must end at the current position. */
3938 endposp = &endpos;
3939 if (REG_MULTI)
3940 {
3941 endpos.se_u.pos.col = (int)(reginput - regline);
3942 endpos.se_u.pos.lnum = reglnum;
3943 }
3944 else
3945 endpos.se_u.ptr = reginput;
3946
3947 /* Go back the specified number of bytes, or as far as the
3948 * start of the previous line, to try matching "\@<=" or
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003949 * not matching "\@<!".
3950 * TODO: This is very inefficient! Would be better to
3951 * first check for a match with what follows. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003952 if (t->state->val <= 0)
3953 {
3954 if (REG_MULTI)
3955 {
3956 regline = reg_getline(--reglnum);
3957 if (regline == NULL)
3958 /* can't go before the first line */
3959 regline = reg_getline(++reglnum);
3960 }
3961 reginput = regline;
3962 }
3963 else
3964 {
3965 if (REG_MULTI
3966 && (int)(reginput - regline) < t->state->val)
3967 {
3968 /* Not enough bytes in this line, go to end of
3969 * previous line. */
3970 regline = reg_getline(--reglnum);
3971 if (regline == NULL)
3972 {
3973 /* can't go before the first line */
3974 regline = reg_getline(++reglnum);
3975 reginput = regline;
3976 }
3977 else
3978 reginput = regline + STRLEN(regline);
3979 }
3980 if ((int)(reginput - regline) >= t->state->val)
3981 {
3982 reginput -= t->state->val;
3983#ifdef FEAT_MBYTE
3984 if (has_mbyte)
3985 reginput -= mb_head_off(regline, reginput);
3986#endif
3987 }
3988 else
3989 reginput = regline;
3990 }
3991 }
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003992
3993 /* Call nfa_regmatch() to check if the current concat matches
3994 * at this position. The concat ends with the node
3995 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003996 if (listids == NULL)
3997 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003998 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003999 if (listids == NULL)
4000 {
4001 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4002 return 0;
4003 }
4004 }
4005#ifdef ENABLE_LOG
4006 if (log_fd != stderr)
4007 fclose(log_fd);
4008 log_fd = NULL;
4009#endif
4010 /* Have to clear the listid field of the NFA nodes, so that
4011 * nfa_regmatch() and addstate() can run properly after
4012 * recursion. */
4013 nfa_save_listids(start, listids);
4014 nfa_set_null_listids(start);
Bram Moolenaar61602c52013-06-01 19:54:43 +02004015 result = nfa_regmatch(t->state->out, submatch, m, endposp);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004016 nfa_set_neg_listids(start);
4017 nfa_restore_listids(start, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02004018
4019 /* restore position in input text */
4020 reginput = save_reginput;
4021 regline = save_regline;
4022 reglnum = save_reglnum;
4023 nfa_match = save_nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004024
4025#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004026 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004027 if (log_fd != NULL)
4028 {
4029 fprintf(log_fd, "****************************\n");
4030 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4031 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4032 fprintf(log_fd, "****************************\n");
4033 }
4034 else
4035 {
4036 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4037 log_fd = stderr;
4038 }
4039#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02004040 /* for \@! it is a match when result is FALSE */
4041 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004042 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004043 /* Copy submatch info from the recursive call */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004044 copy_sub_off(&t->subs.norm, &m->norm);
4045#ifdef FEAT_SYN_HL
4046 copy_sub_off(&t->subs.synt, &m->synt);
4047#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004048
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004049 /* t->state->out1 is the corresponding END_INVISIBLE node;
4050 * Add it to the current list (zero-width match). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004051 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004052 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004053 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004054 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02004055 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004056
4057 case NFA_BOL:
4058 if (reginput == regline)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004059 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004060 break;
4061
4062 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004063 if (curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004064 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004065 break;
4066
4067 case NFA_BOW:
4068 {
4069 int bow = TRUE;
4070
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004071 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072 bow = FALSE;
4073#ifdef FEAT_MBYTE
4074 else if (has_mbyte)
4075 {
4076 int this_class;
4077
4078 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004079 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004080 if (this_class <= 1)
4081 bow = FALSE;
4082 else if (reg_prev_class() == this_class)
4083 bow = FALSE;
4084 }
4085#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004086 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004087 || (reginput > regline
4088 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 bow = FALSE;
4090 if (bow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004091 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004092 break;
4093 }
4094
4095 case NFA_EOW:
4096 {
4097 int eow = TRUE;
4098
4099 if (reginput == regline)
4100 eow = FALSE;
4101#ifdef FEAT_MBYTE
4102 else if (has_mbyte)
4103 {
4104 int this_class, prev_class;
4105
4106 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004107 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004108 prev_class = reg_prev_class();
4109 if (this_class == prev_class
4110 || prev_class == 0 || prev_class == 1)
4111 eow = FALSE;
4112 }
4113#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004114 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004115 || (reginput[0] != NUL
4116 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117 eow = FALSE;
4118 if (eow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004119 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004120 break;
4121 }
4122
Bram Moolenaar4b780632013-05-31 22:14:52 +02004123 case NFA_BOF:
4124 if (reglnum == 0 && reginput == regline
4125 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004126 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004127 break;
4128
4129 case NFA_EOF:
4130 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004131 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004132 break;
4133
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004134#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004136 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004137 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004138 int len = 0;
4139 nfa_state_T *end;
4140 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004141 int cchars[MAX_MCO];
4142 int ccount = 0;
4143 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004145 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004146 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004147 if (utf_iscomposing(sta->c))
4148 {
4149 /* Only match composing character(s), ignore base
4150 * character. Used for ".{composing}" and "{composing}"
4151 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004152 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004153 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004154 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004155 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004156 /* If \Z was present, then ignore composing characters.
4157 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004158 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004159 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004160 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004161 else
4162 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004163 while (sta->c != NFA_END_COMPOSING)
4164 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004165 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004166
4167 /* Check base character matches first, unless ignored. */
4168 else if (len > 0 || mc == sta->c)
4169 {
4170 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004171 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004172 len += mb_char2len(mc);
4173 sta = sta->out;
4174 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004176 /* We don't care about the order of composing characters.
4177 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004178 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004179 {
4180 mc = mb_ptr2char(reginput + len);
4181 cchars[ccount++] = mc;
4182 len += mb_char2len(mc);
4183 if (ccount == MAX_MCO)
4184 break;
4185 }
4186
4187 /* Check that each composing char in the pattern matches a
4188 * composing char in the text. We do not check if all
4189 * composing chars are matched. */
4190 result = OK;
4191 while (sta->c != NFA_END_COMPOSING)
4192 {
4193 for (j = 0; j < ccount; ++j)
4194 if (cchars[j] == sta->c)
4195 break;
4196 if (j == ccount)
4197 {
4198 result = FAIL;
4199 break;
4200 }
4201 sta = sta->out;
4202 }
4203 }
4204 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004205 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004206
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004207 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208 ADD_POS_NEG_STATE(end);
4209 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004210 }
4211#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004212
4213 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004214 if (curc == NUL && !reg_line_lbr && REG_MULTI
4215 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004216 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004217 go_to_nextline = TRUE;
4218 /* Pass -1 for the offset, which means taking the position
4219 * at the start of the next line. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004220 addstate(nextlist, t->state->out, &t->subs, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004221 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004222 else if (curc == '\n' && reg_line_lbr)
4223 {
4224 /* match \n as if it is an ordinary character */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004225 addstate(nextlist, t->state->out, &t->subs, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004226 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004227 break;
4228
4229 case NFA_CLASS_ALNUM:
4230 case NFA_CLASS_ALPHA:
4231 case NFA_CLASS_BLANK:
4232 case NFA_CLASS_CNTRL:
4233 case NFA_CLASS_DIGIT:
4234 case NFA_CLASS_GRAPH:
4235 case NFA_CLASS_LOWER:
4236 case NFA_CLASS_PRINT:
4237 case NFA_CLASS_PUNCT:
4238 case NFA_CLASS_SPACE:
4239 case NFA_CLASS_UPPER:
4240 case NFA_CLASS_XDIGIT:
4241 case NFA_CLASS_TAB:
4242 case NFA_CLASS_RETURN:
4243 case NFA_CLASS_BACKSPACE:
4244 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004245 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004246 ADD_POS_NEG_STATE(t->state);
4247 break;
4248
4249 case NFA_END_NEG_RANGE:
4250 /* This follows a series of negated nodes, like:
4251 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004252 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004253 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004254 break;
4255
4256 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004257 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004258 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004259 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260 break;
4261
4262 /*
4263 * Character classes like \a for alpha, \d for digit etc.
4264 */
4265 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004266 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004267 ADD_POS_NEG_STATE(t->state);
4268 break;
4269
4270 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004271 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004272 ADD_POS_NEG_STATE(t->state);
4273 break;
4274
4275 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004276 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004277 ADD_POS_NEG_STATE(t->state);
4278 break;
4279
4280 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004281 result = !VIM_ISDIGIT(curc)
4282 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004283 ADD_POS_NEG_STATE(t->state);
4284 break;
4285
4286 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004287 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004288 ADD_POS_NEG_STATE(t->state);
4289 break;
4290
4291 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004292 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004293 ADD_POS_NEG_STATE(t->state);
4294 break;
4295
4296 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004297 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298 ADD_POS_NEG_STATE(t->state);
4299 break;
4300
4301 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004302 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004303 ADD_POS_NEG_STATE(t->state);
4304 break;
4305
4306 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004307 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308 ADD_POS_NEG_STATE(t->state);
4309 break;
4310
4311 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004312 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004313 ADD_POS_NEG_STATE(t->state);
4314 break;
4315
4316 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004317 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004318 ADD_POS_NEG_STATE(t->state);
4319 break;
4320
4321 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004322 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323 ADD_POS_NEG_STATE(t->state);
4324 break;
4325
4326 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004327 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004328 ADD_POS_NEG_STATE(t->state);
4329 break;
4330
4331 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004332 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333 ADD_POS_NEG_STATE(t->state);
4334 break;
4335
4336 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004337 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004338 ADD_POS_NEG_STATE(t->state);
4339 break;
4340
4341 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004342 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004343 ADD_POS_NEG_STATE(t->state);
4344 break;
4345
4346 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004347 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004348 ADD_POS_NEG_STATE(t->state);
4349 break;
4350
4351 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004352 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004353 ADD_POS_NEG_STATE(t->state);
4354 break;
4355
4356 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004357 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004358 ADD_POS_NEG_STATE(t->state);
4359 break;
4360
4361 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004362 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004363 ADD_POS_NEG_STATE(t->state);
4364 break;
4365
4366 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004367 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368 ADD_POS_NEG_STATE(t->state);
4369 break;
4370
4371 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004372 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004373 ADD_POS_NEG_STATE(t->state);
4374 break;
4375
4376 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004377 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004378 ADD_POS_NEG_STATE(t->state);
4379 break;
4380
4381 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004382 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004383 ADD_POS_NEG_STATE(t->state);
4384 break;
4385
4386 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004387 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388 ADD_POS_NEG_STATE(t->state);
4389 break;
4390
4391 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004392 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 ADD_POS_NEG_STATE(t->state);
4394 break;
4395
Bram Moolenaar5714b802013-05-28 22:03:20 +02004396 case NFA_BACKREF1:
4397 case NFA_BACKREF2:
4398 case NFA_BACKREF3:
4399 case NFA_BACKREF4:
4400 case NFA_BACKREF5:
4401 case NFA_BACKREF6:
4402 case NFA_BACKREF7:
4403 case NFA_BACKREF8:
4404 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004405#ifdef FEAT_SYN_HL
4406 case NFA_ZREF1:
4407 case NFA_ZREF2:
4408 case NFA_ZREF3:
4409 case NFA_ZREF4:
4410 case NFA_ZREF5:
4411 case NFA_ZREF6:
4412 case NFA_ZREF7:
4413 case NFA_ZREF8:
4414 case NFA_ZREF9:
4415#endif
4416 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004417 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004418 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004419 int bytelen;
4420
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004421 if (t->state->c <= NFA_BACKREF9)
4422 {
4423 subidx = t->state->c - NFA_BACKREF1 + 1;
4424 result = match_backref(&t->subs.norm, subidx, &bytelen);
4425 }
4426#ifdef FEAT_SYN_HL
4427 else
4428 {
4429 subidx = t->state->c - NFA_ZREF1 + 1;
4430 result = match_zref(subidx, &bytelen);
4431 }
4432#endif
4433
Bram Moolenaar5714b802013-05-28 22:03:20 +02004434 if (result)
4435 {
4436 if (bytelen == 0)
4437 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004438 /* empty match always works, output of NFA_SKIP to be
4439 * used next */
4440 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004441 &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004442 }
4443 else if (bytelen <= clen)
4444 {
4445 /* match current character, jump ahead to out of
4446 * NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004447 addstate(nextlist, t->state->out->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004448#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004449 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004450#endif
4451 }
4452 else
4453 {
4454 /* skip ofer the matched characters, set character
4455 * count in NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004456 addstate(nextlist, t->state->out, &t->subs, bytelen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004457 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4458#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004459 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004460#endif
4461 }
4462
4463 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004464 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004465 }
4466 case NFA_SKIP:
4467 /* charater of previous matching \1 .. \9 */
4468 if (t->count - clen <= 0)
4469 {
4470 /* end of match, go to what follows */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004471 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004472#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004473 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004474#endif
4475 }
4476 else
4477 {
4478 /* add state again with decremented count */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004479 addstate(nextlist, t->state, &t->subs, 0);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004480 nextlist->t[nextlist->n - 1].count = t->count - clen;
4481#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004482 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004483#endif
4484 }
4485 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004486
4487 case NFA_SKIP_CHAR:
4488 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004489 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004490 /* TODO: should not happen? */
4491 break;
4492
Bram Moolenaar423532e2013-05-29 21:14:42 +02004493 case NFA_LNUM:
4494 case NFA_LNUM_GT:
4495 case NFA_LNUM_LT:
4496 result = (REG_MULTI &&
4497 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4498 (long_u)(reglnum + reg_firstlnum)));
4499 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004500 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004501 break;
4502
4503 case NFA_COL:
4504 case NFA_COL_GT:
4505 case NFA_COL_LT:
4506 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4507 (long_u)(reginput - regline) + 1);
4508 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004509 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004510 break;
4511
4512 case NFA_VCOL:
4513 case NFA_VCOL_GT:
4514 case NFA_VCOL_LT:
4515 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4516 (long_u)win_linetabsize(
4517 reg_win == NULL ? curwin : reg_win,
4518 regline, (colnr_T)(reginput - regline)) + 1);
4519 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004520 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004521 break;
4522
4523 case NFA_CURSOR:
4524 result = (reg_win != NULL
4525 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4526 && ((colnr_T)(reginput - regline)
4527 == reg_win->w_cursor.col));
4528 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004529 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004530 break;
4531
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004532 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004533 {
4534 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004535
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004536 /* TODO: put this in #ifdef later */
4537 if (c < -256)
4538 EMSGN("INTERNAL: Negative state char: %ld", c);
4539 if (is_Magic(c))
4540 c = un_Magic(c);
4541 result = (c == curc);
4542
4543 if (!result && ireg_ic)
4544 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004545#ifdef FEAT_MBYTE
4546 /* If there is a composing character which is not being
4547 * ignored there can be no match. Match with composing
4548 * character uses NFA_COMPOSING above. */
4549 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004550 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004551 result = FALSE;
4552#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 ADD_POS_NEG_STATE(t->state);
4554 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004555 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004556 }
4557
4558 } /* for (thislist = thislist; thislist->state; thislist++) */
4559
Bram Moolenaare23febd2013-05-26 18:40:14 +02004560 /* Look for the start of a match in the current position by adding the
4561 * start state to the list of states.
4562 * The first found match is the leftmost one, thus the order of states
4563 * matters!
4564 * Do not add the start state in recursive calls of nfa_regmatch(),
4565 * because recursive calls should only start in the first position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004566 * Unless "endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004567 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004568 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004569 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004570 && reglnum == 0
4571 && clen != 0
4572 && (ireg_maxcol == 0
4573 || (colnr_T)(reginput - regline) < ireg_maxcol))
4574 || (endp != NULL
4575 && (REG_MULTI
4576 ? (reglnum < endp->se_u.pos.lnum
4577 || (reglnum == endp->se_u.pos.lnum
4578 && (int)(reginput - regline)
4579 < endp->se_u.pos.col))
4580 : reginput < endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004581 {
4582#ifdef ENABLE_LOG
4583 fprintf(log_fd, "(---) STARTSTATE\n");
4584#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004585 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004586 }
4587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588#ifdef ENABLE_LOG
4589 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004590 {
4591 int i;
4592
4593 for (i = 0; i < thislist->n; i++)
4594 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4595 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004596 fprintf(log_fd, "\n");
4597#endif
4598
4599nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004600 /* Advance to the next character, or advance to the next line, or
4601 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004602 if (clen != 0)
4603 reginput += clen;
Bram Moolenaar61602c52013-06-01 19:54:43 +02004604 else if (go_to_nextline || (endp != NULL && REG_MULTI
4605 && reglnum < endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004606 reg_nextline();
4607 else
4608 break;
4609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004610
4611#ifdef ENABLE_LOG
4612 if (log_fd != stderr)
4613 fclose(log_fd);
4614 log_fd = NULL;
4615#endif
4616
4617theend:
4618 /* Free memory */
4619 vim_free(list[0].t);
4620 vim_free(list[1].t);
4621 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004622 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004623#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004624#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004625 fclose(debug);
4626#endif
4627
Bram Moolenaar963fee22013-05-26 21:47:28 +02004628 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629}
4630
4631/*
4632 * Try match of "prog" with at regline["col"].
4633 * Returns 0 for failure, number of lines contained in the match otherwise.
4634 */
4635 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004636nfa_regtry(prog, col)
4637 nfa_regprog_T *prog;
4638 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639{
4640 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004641 regsubs_T subs, m;
4642 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004643#ifdef ENABLE_LOG
4644 FILE *f;
4645#endif
4646
4647 reginput = regline + col;
4648 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004649#ifdef FEAT_SYN_HL
4650 /* Clear the external match subpointers if necessary. */
4651 if (prog->reghasz == REX_SET)
4652 need_clear_zsubexpr = TRUE;
4653#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004654
4655#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004656 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004657 if (f != NULL)
4658 {
4659 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4660 fprintf(f, " =======================================================\n");
4661#ifdef DEBUG
4662 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4663#endif
4664 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004665 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004666 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004667 fprintf(f, "\n\n");
4668 fclose(f);
4669 }
4670 else
4671 EMSG(_("Could not open temporary log file for writing "));
4672#endif
4673
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004674 clear_sub(&subs.norm);
4675 clear_sub(&m.norm);
4676#ifdef FEAT_SYN_HL
4677 clear_sub(&subs.synt);
4678 clear_sub(&m.synt);
4679#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004681 if (nfa_regmatch(start, &subs, &m, NULL) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682 return 0;
4683
4684 cleanup_subexpr();
4685 if (REG_MULTI)
4686 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004687 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004689 reg_startpos[i] = subs.norm.list.multi[i].start;
4690 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004691 }
4692
4693 if (reg_startpos[0].lnum < 0)
4694 {
4695 reg_startpos[0].lnum = 0;
4696 reg_startpos[0].col = col;
4697 }
4698 if (reg_endpos[0].lnum < 0)
4699 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004700 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701 reg_endpos[0].lnum = reglnum;
4702 reg_endpos[0].col = (int)(reginput - regline);
4703 }
4704 else
4705 /* Use line number of "\ze". */
4706 reglnum = reg_endpos[0].lnum;
4707 }
4708 else
4709 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004710 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004712 reg_startp[i] = subs.norm.list.line[i].start;
4713 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714 }
4715
4716 if (reg_startp[0] == NULL)
4717 reg_startp[0] = regline + col;
4718 if (reg_endp[0] == NULL)
4719 reg_endp[0] = reginput;
4720 }
4721
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004722#ifdef FEAT_SYN_HL
4723 /* Package any found \z(...\) matches for export. Default is none. */
4724 unref_extmatch(re_extmatch_out);
4725 re_extmatch_out = NULL;
4726
4727 if (prog->reghasz == REX_SET)
4728 {
4729 int i;
4730
4731 cleanup_zsubexpr();
4732 re_extmatch_out = make_extmatch();
4733 for (i = 0; i < subs.synt.in_use; i++)
4734 {
4735 if (REG_MULTI)
4736 {
4737 struct multipos *mpos = &subs.synt.list.multi[i];
4738
4739 /* Only accept single line matches. */
4740 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
4741 re_extmatch_out->matches[i] =
4742 vim_strnsave(reg_getline(mpos->start.lnum)
4743 + mpos->start.col,
4744 mpos->end.col - mpos->start.col);
4745 }
4746 else
4747 {
4748 struct linepos *lpos = &subs.synt.list.line[i];
4749
4750 if (lpos->start != NULL && lpos->end != NULL)
4751 re_extmatch_out->matches[i] =
4752 vim_strnsave(lpos->start,
4753 (int)(lpos->end - lpos->start));
4754 }
4755 }
4756 }
4757#endif
4758
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004759 return 1 + reglnum;
4760}
4761
4762/*
4763 * Match a regexp against a string ("line" points to the string) or multiple
4764 * lines ("line" is NULL, use reg_getline()).
4765 *
4766 * Returns 0 for failure, number of lines contained in the match otherwise.
4767 */
4768 static long
4769nfa_regexec_both(line, col)
4770 char_u *line;
4771 colnr_T col; /* column to start looking for match */
4772{
4773 nfa_regprog_T *prog;
4774 long retval = 0L;
4775 int i;
4776
4777 if (REG_MULTI)
4778 {
4779 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4780 line = reg_getline((linenr_T)0); /* relative to the cursor */
4781 reg_startpos = reg_mmatch->startpos;
4782 reg_endpos = reg_mmatch->endpos;
4783 }
4784 else
4785 {
4786 prog = (nfa_regprog_T *)reg_match->regprog;
4787 reg_startp = reg_match->startp;
4788 reg_endp = reg_match->endp;
4789 }
4790
4791 /* Be paranoid... */
4792 if (prog == NULL || line == NULL)
4793 {
4794 EMSG(_(e_null));
4795 goto theend;
4796 }
4797
4798 /* If the start column is past the maximum column: no need to try. */
4799 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4800 goto theend;
4801
4802 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4803 if (prog->regflags & RF_ICASE)
4804 ireg_ic = TRUE;
4805 else if (prog->regflags & RF_NOICASE)
4806 ireg_ic = FALSE;
4807
4808#ifdef FEAT_MBYTE
4809 /* If pattern contains "\Z" overrule value of ireg_icombine */
4810 if (prog->regflags & RF_ICOMBINE)
4811 ireg_icombine = TRUE;
4812#endif
4813
4814 regline = line;
4815 reglnum = 0; /* relative to line */
4816
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004817 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004818 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004819 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004820#ifdef DEBUG
4821 nfa_regengine.expr = prog->pattern;
4822#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004823
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004824 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004825 for (i = 0; i < nstate; ++i)
4826 {
4827 prog->state[i].id = i;
4828 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004829 }
4830
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004831 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004832
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004833#ifdef DEBUG
4834 nfa_regengine.expr = NULL;
4835#endif
4836
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004837theend:
4838 return retval;
4839}
4840
4841/*
4842 * Compile a regular expression into internal code for the NFA matcher.
4843 * Returns the program in allocated space. Returns NULL for an error.
4844 */
4845 static regprog_T *
4846nfa_regcomp(expr, re_flags)
4847 char_u *expr;
4848 int re_flags;
4849{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004850 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004851 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004852 int *postfix;
4853
4854 if (expr == NULL)
4855 return NULL;
4856
4857#ifdef DEBUG
4858 nfa_regengine.expr = expr;
4859#endif
4860
4861 init_class_tab();
4862
4863 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4864 return NULL;
4865
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004867 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004868 postfix = re2post();
4869 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004870 {
4871 /* TODO: only give this error for debugging? */
4872 if (post_ptr >= post_end)
4873 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004874 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004875 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876
4877 /*
4878 * In order to build the NFA, we parse the input regexp twice:
4879 * 1. first pass to count size (so we can allocate space)
4880 * 2. second to emit code
4881 */
4882#ifdef ENABLE_LOG
4883 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004884 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004885
4886 if (f != NULL)
4887 {
4888 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4889 fclose(f);
4890 }
4891 }
4892#endif
4893
4894 /*
4895 * PASS 1
4896 * Count number of NFA states in "nstate". Do not build the NFA.
4897 */
4898 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004899
4900 /* Space for compiled regexp */
4901 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4902 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4903 if (prog == NULL)
4904 goto fail;
4905 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906 state_ptr = prog->state;
4907
4908 /*
4909 * PASS 2
4910 * Build the NFA
4911 */
4912 prog->start = post2nfa(postfix, post_ptr, FALSE);
4913 if (prog->start == NULL)
4914 goto fail;
4915
4916 prog->regflags = regflags;
4917 prog->engine = &nfa_regengine;
4918 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004919 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004920 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004921 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922#ifdef ENABLE_LOG
4923 nfa_postfix_dump(expr, OK);
4924 nfa_dump(prog);
4925#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004926#ifdef FEAT_SYN_HL
4927 /* Remember whether this pattern has any \z specials in it. */
4928 prog->reghasz = re_has_z;
4929#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004930#ifdef DEBUG
4931 prog->pattern = vim_strsave(expr); /* memory will leak */
4932 nfa_regengine.expr = NULL;
4933#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004934
4935out:
4936 vim_free(post_start);
4937 post_start = post_ptr = post_end = NULL;
4938 state_ptr = NULL;
4939 return (regprog_T *)prog;
4940
4941fail:
4942 vim_free(prog);
4943 prog = NULL;
4944#ifdef ENABLE_LOG
4945 nfa_postfix_dump(expr, FAIL);
4946#endif
4947#ifdef DEBUG
4948 nfa_regengine.expr = NULL;
4949#endif
4950 goto out;
4951}
4952
4953
4954/*
4955 * Match a regexp against a string.
4956 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4957 * Uses curbuf for line count and 'iskeyword'.
4958 *
4959 * Return TRUE if there is a match, FALSE if not.
4960 */
4961 static int
4962nfa_regexec(rmp, line, col)
4963 regmatch_T *rmp;
4964 char_u *line; /* string to match against */
4965 colnr_T col; /* column to start looking for match */
4966{
4967 reg_match = rmp;
4968 reg_mmatch = NULL;
4969 reg_maxline = 0;
4970 reg_line_lbr = FALSE;
4971 reg_buf = curbuf;
4972 reg_win = NULL;
4973 ireg_ic = rmp->rm_ic;
4974#ifdef FEAT_MBYTE
4975 ireg_icombine = FALSE;
4976#endif
4977 ireg_maxcol = 0;
4978 return (nfa_regexec_both(line, col) != 0);
4979}
4980
4981#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4982 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4983
4984static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4985
4986/*
4987 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4988 */
4989 static int
4990nfa_regexec_nl(rmp, line, col)
4991 regmatch_T *rmp;
4992 char_u *line; /* string to match against */
4993 colnr_T col; /* column to start looking for match */
4994{
4995 reg_match = rmp;
4996 reg_mmatch = NULL;
4997 reg_maxline = 0;
4998 reg_line_lbr = TRUE;
4999 reg_buf = curbuf;
5000 reg_win = NULL;
5001 ireg_ic = rmp->rm_ic;
5002#ifdef FEAT_MBYTE
5003 ireg_icombine = FALSE;
5004#endif
5005 ireg_maxcol = 0;
5006 return (nfa_regexec_both(line, col) != 0);
5007}
5008#endif
5009
5010
5011/*
5012 * Match a regexp against multiple lines.
5013 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5014 * Uses curbuf for line count and 'iskeyword'.
5015 *
5016 * Return zero if there is no match. Return number of lines contained in the
5017 * match otherwise.
5018 *
5019 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5020 *
5021 * ! Also NOTE : match may actually be in another line. e.g.:
5022 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5023 *
5024 * +-------------------------+
5025 * |a |
5026 * |b |
5027 * |c |
5028 * | |
5029 * +-------------------------+
5030 *
5031 * then nfa_regexec_multi() returns 3. while the original
5032 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5033 *
5034 * FIXME if this behavior is not compatible.
5035 */
5036 static long
5037nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5038 regmmatch_T *rmp;
5039 win_T *win; /* window in which to search or NULL */
5040 buf_T *buf; /* buffer in which to search */
5041 linenr_T lnum; /* nr of line to start looking for match */
5042 colnr_T col; /* column to start looking for match */
5043 proftime_T *tm UNUSED; /* timeout limit or NULL */
5044{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005045 reg_match = NULL;
5046 reg_mmatch = rmp;
5047 reg_buf = buf;
5048 reg_win = win;
5049 reg_firstlnum = lnum;
5050 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5051 reg_line_lbr = FALSE;
5052 ireg_ic = rmp->rmm_ic;
5053#ifdef FEAT_MBYTE
5054 ireg_icombine = FALSE;
5055#endif
5056 ireg_maxcol = rmp->rmm_maxcol;
5057
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005058 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005059}
5060
5061#ifdef DEBUG
5062# undef ENABLE_LOG
5063#endif