blob: 1bb82fddee7898db62a5b61fc352f82095352106 [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 Moolenaarf6de0322013-06-02 21:30:04 +0200240/* NFA regexp has \z( ), set zsubexpr. */
241static int nfa_has_zsubexpr;
242
Bram Moolenaar963fee22013-05-26 21:47:28 +0200243/* Number of sub expressions actually being used during execution. 1 if only
244 * the whole match (subexpr 0) is used. */
245static int nfa_nsubexpr;
246
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static int *post_start; /* holds the postfix form of r.e. */
248static int *post_end;
249static int *post_ptr;
250
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200251static int nstate; /* Number of states in the NFA. Also used when
252 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200253static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200254
Bram Moolenaar307aa162013-06-02 16:34:21 +0200255/* If not NULL match must end at this position */
256static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200258/* listid is global, so that it increases on recursive calls to
259 * nfa_regmatch(), which means we don't have to clear the lastlist field of
260 * all the states. */
261static int nfa_listid;
262static int nfa_alt_listid;
263
264/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
265static int nfa_ll_index = 0;
266
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200267static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
268static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
269static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200270static int nfa_regatom __ARGS((void));
271static int nfa_regpiece __ARGS((void));
272static int nfa_regconcat __ARGS((void));
273static int nfa_regbranch __ARGS((void));
274static int nfa_reg __ARGS((int paren));
275#ifdef DEBUG
276static void nfa_set_code __ARGS((int c));
277static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200278static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
279static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200280static void nfa_dump __ARGS((nfa_regprog_T *prog));
281#endif
282static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200283static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200284static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
285static int check_char_class __ARGS((int class, int c));
286static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200287static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
288static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200289static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200290static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200291static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
292static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
293static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
294static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
295
296/* helper functions used when doing re2post() ... regatom() parsing */
297#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200298 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200299 return FAIL; \
300 *post_ptr++ = c; \
301 } while (0)
302
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303/*
304 * Initialize internal variables before NFA compilation.
305 * Return OK on success, FAIL otherwise.
306 */
307 static int
308nfa_regcomp_start(expr, re_flags)
309 char_u *expr;
310 int re_flags; /* see vim_regcomp() */
311{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200312 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200313 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200314
315 nstate = 0;
316 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200317 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200318 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200319
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200320 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200321 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200322 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200323
324 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200325 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327 post_start = (int *)lalloc(postfix_size, TRUE);
328 if (post_start == NULL)
329 return FAIL;
330 vim_memset(post_start, 0, postfix_size);
331 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200332 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200333 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200334 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200336 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200337 regcomp_start(expr, re_flags);
338
339 return OK;
340}
341
342/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200343 * Allocate more space for post_start. Called when
344 * running above the estimated number of states.
345 */
346 static int
347realloc_post_list()
348{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200349 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200350 int new_max = nstate_max + 1000;
351 int *new_start;
352 int *old_start;
353
354 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
355 if (new_start == NULL)
356 return FAIL;
357 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
358 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
359 old_start = post_start;
360 post_start = new_start;
361 post_ptr = new_start + (post_ptr - old_start);
362 post_end = post_start + new_max;
363 vim_free(old_start);
364 return OK;
365}
366
367/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200368 * Search between "start" and "end" and try to recognize a
369 * character class in expanded form. For example [0-9].
370 * On success, return the id the character class to be emitted.
371 * On failure, return 0 (=FAIL)
372 * Start points to the first char of the range, while end should point
373 * to the closing brace.
374 */
375 static int
376nfa_recognize_char_class(start, end, extra_newl)
377 char_u *start;
378 char_u *end;
379 int extra_newl;
380{
381 int i;
382 /* Each of these variables takes up a char in "config[]",
383 * in the order they are here. */
384 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
385 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
386 char_u *p;
387#define NCONFIGS 16
388 int classid[NCONFIGS] = {
389 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
390 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
391 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
392 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
393 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200394 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200395 char_u config[NCONFIGS][9] = {
396 "000000100", /* digit */
397 "100000100", /* non digit */
398 "011000100", /* hex-digit */
399 "111000100", /* non hex-digit */
400 "000001000", /* octal-digit */
401 "100001000", /* [^0-7] */
402 "000110110", /* [0-9A-Za-z_] */
403 "100110110", /* [^0-9A-Za-z_] */
404 "000110010", /* head of word */
405 "100110010", /* not head of word */
406 "000110000", /* alphabetic char a-z */
407 "100110000", /* non alphabetic char */
408 "000100000", /* lowercase letter */
409 "100100000", /* non lowercase */
410 "000010000", /* uppercase */
411 "100010000" /* non uppercase */
412 };
413
414 if (extra_newl == TRUE)
415 newl = TRUE;
416
417 if (*end != ']')
418 return FAIL;
419 p = start;
420 if (*p == '^')
421 {
422 not = TRUE;
423 p ++;
424 }
425
426 while (p < end)
427 {
428 if (p + 2 < end && *(p + 1) == '-')
429 {
430 switch (*p)
431 {
432 case '0':
433 if (*(p + 2) == '9')
434 {
435 o9 = TRUE;
436 break;
437 }
438 else
439 if (*(p + 2) == '7')
440 {
441 o7 = 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 case 'A':
457 if (*(p + 2) == 'Z')
458 {
459 AZ = TRUE;
460 break;
461 }
462 else
463 if (*(p + 2) == 'F')
464 {
465 AF = TRUE;
466 break;
467 }
468 /* FALLTHROUGH */
469 default:
470 return FAIL;
471 }
472 p += 3;
473 }
474 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
475 {
476 newl = TRUE;
477 p += 2;
478 }
479 else if (*p == '_')
480 {
481 underscore = TRUE;
482 p ++;
483 }
484 else if (*p == '\n')
485 {
486 newl = TRUE;
487 p ++;
488 }
489 else
490 return FAIL;
491 } /* while (p < end) */
492
493 if (p != end)
494 return FAIL;
495
496 /* build the config that represents the ranges we gathered */
497 STRCPY(myconfig, "000000000");
498 if (not == TRUE)
499 myconfig[0] = '1';
500 if (af == TRUE)
501 myconfig[1] = '1';
502 if (AF == TRUE)
503 myconfig[2] = '1';
504 if (az == TRUE)
505 myconfig[3] = '1';
506 if (AZ == TRUE)
507 myconfig[4] = '1';
508 if (o7 == TRUE)
509 myconfig[5] = '1';
510 if (o9 == TRUE)
511 myconfig[6] = '1';
512 if (underscore == TRUE)
513 myconfig[7] = '1';
514 if (newl == TRUE)
515 {
516 myconfig[8] = '1';
517 extra_newl = ADD_NL;
518 }
519 /* try to recognize character classes */
520 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200521 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200522 return classid[i] + extra_newl;
523
524 /* fallthrough => no success so far */
525 return FAIL;
526
527#undef NCONFIGS
528}
529
530/*
531 * Produce the bytes for equivalence class "c".
532 * Currently only handles latin1, latin9 and utf-8.
533 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
534 * equivalent to 'a OR b OR c'
535 *
536 * NOTE! When changing this function, also update reg_equi_class()
537 */
538 static int
539nfa_emit_equi_class(c, neg)
540 int c;
541 int neg;
542{
543 int first = TRUE;
544 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
545#define EMIT2(c) \
546 EMIT(c); \
547 if (neg == TRUE) { \
548 EMIT(NFA_NOT); \
549 } \
550 if (first == FALSE) \
551 EMIT(glue); \
552 else \
553 first = FALSE; \
554
555#ifdef FEAT_MBYTE
556 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
557 || STRCMP(p_enc, "iso-8859-15") == 0)
558#endif
559 {
560 switch (c)
561 {
562 case 'A': case '\300': case '\301': case '\302':
563 case '\303': case '\304': case '\305':
564 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
565 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
566 EMIT2('\305');
567 return OK;
568
569 case 'C': case '\307':
570 EMIT2('C'); EMIT2('\307');
571 return OK;
572
573 case 'E': case '\310': case '\311': case '\312': case '\313':
574 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
575 EMIT2('\312'); EMIT2('\313');
576 return OK;
577
578 case 'I': case '\314': case '\315': case '\316': case '\317':
579 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
580 EMIT2('\316'); EMIT2('\317');
581 return OK;
582
583 case 'N': case '\321':
584 EMIT2('N'); EMIT2('\321');
585 return OK;
586
587 case 'O': case '\322': case '\323': case '\324': case '\325':
588 case '\326':
589 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
590 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
591 return OK;
592
593 case 'U': case '\331': case '\332': case '\333': case '\334':
594 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
595 EMIT2('\333'); EMIT2('\334');
596 return OK;
597
598 case 'Y': case '\335':
599 EMIT2('Y'); EMIT2('\335');
600 return OK;
601
602 case 'a': case '\340': case '\341': case '\342':
603 case '\343': case '\344': case '\345':
604 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
605 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
606 EMIT2('\345');
607 return OK;
608
609 case 'c': case '\347':
610 EMIT2('c'); EMIT2('\347');
611 return OK;
612
613 case 'e': case '\350': case '\351': case '\352': case '\353':
614 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
615 EMIT2('\352'); EMIT2('\353');
616 return OK;
617
618 case 'i': case '\354': case '\355': case '\356': case '\357':
619 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
620 EMIT2('\356'); EMIT2('\357');
621 return OK;
622
623 case 'n': case '\361':
624 EMIT2('n'); EMIT2('\361');
625 return OK;
626
627 case 'o': case '\362': case '\363': case '\364': case '\365':
628 case '\366':
629 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
630 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
631 return OK;
632
633 case 'u': case '\371': case '\372': case '\373': case '\374':
634 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
635 EMIT2('\373'); EMIT2('\374');
636 return OK;
637
638 case 'y': case '\375': case '\377':
639 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
640 return OK;
641
642 default:
643 return FAIL;
644 }
645 }
646
647 EMIT(c);
648 return OK;
649#undef EMIT2
650}
651
652/*
653 * Code to parse regular expression.
654 *
655 * We try to reuse parsing functions in regexp.c to
656 * minimize surprise and keep the syntax consistent.
657 */
658
659/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 * Parse the lowest level.
661 *
662 * An atom can be one of a long list of items. Many atoms match one character
663 * in the text. It is often an ordinary character or a character class.
664 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
665 * is only for syntax highlighting.
666 *
667 * atom ::= ordinary-atom
668 * or \( pattern \)
669 * or \%( pattern \)
670 * or \z( pattern \)
671 */
672 static int
673nfa_regatom()
674{
675 int c;
676 int charclass;
677 int equiclass;
678 int collclass;
679 int got_coll_char;
680 char_u *p;
681 char_u *endp;
682#ifdef FEAT_MBYTE
683 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200684#endif
685 int extra = 0;
686 int first;
687 int emit_range;
688 int negated;
689 int result;
690 int startc = -1;
691 int endc = -1;
692 int oldstartc = -1;
693 int cpo_lit; /* 'cpoptions' contains 'l' flag */
694 int cpo_bsl; /* 'cpoptions' contains '\' flag */
695 int glue; /* ID that will "glue" nodes together */
696
697 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
698 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
699
700 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200701 switch (c)
702 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200703 case NUL:
704 syntax_error = TRUE;
705 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
706
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200707 case Magic('^'):
708 EMIT(NFA_BOL);
709 break;
710
711 case Magic('$'):
712 EMIT(NFA_EOL);
713#if defined(FEAT_SYN_HL) || defined(PROTO)
714 had_eol = TRUE;
715#endif
716 break;
717
718 case Magic('<'):
719 EMIT(NFA_BOW);
720 break;
721
722 case Magic('>'):
723 EMIT(NFA_EOW);
724 break;
725
726 case Magic('_'):
727 c = no_Magic(getchr());
728 if (c == '^') /* "\_^" is start-of-line */
729 {
730 EMIT(NFA_BOL);
731 break;
732 }
733 if (c == '$') /* "\_$" is end-of-line */
734 {
735 EMIT(NFA_EOL);
736#if defined(FEAT_SYN_HL) || defined(PROTO)
737 had_eol = TRUE;
738#endif
739 break;
740 }
741
742 extra = ADD_NL;
743
744 /* "\_[" is collection plus newline */
745 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200746 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747
748 /* "\_x" is character class plus newline */
749 /*FALLTHROUGH*/
750
751 /*
752 * Character classes.
753 */
754 case Magic('.'):
755 case Magic('i'):
756 case Magic('I'):
757 case Magic('k'):
758 case Magic('K'):
759 case Magic('f'):
760 case Magic('F'):
761 case Magic('p'):
762 case Magic('P'):
763 case Magic('s'):
764 case Magic('S'):
765 case Magic('d'):
766 case Magic('D'):
767 case Magic('x'):
768 case Magic('X'):
769 case Magic('o'):
770 case Magic('O'):
771 case Magic('w'):
772 case Magic('W'):
773 case Magic('h'):
774 case Magic('H'):
775 case Magic('a'):
776 case Magic('A'):
777 case Magic('l'):
778 case Magic('L'):
779 case Magic('u'):
780 case Magic('U'):
781 p = vim_strchr(classchars, no_Magic(c));
782 if (p == NULL)
783 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200784 EMSGN("INTERNAL: Unknown character class char: %ld", c);
785 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200786 }
787#ifdef FEAT_MBYTE
788 /* When '.' is followed by a composing char ignore the dot, so that
789 * the composing char is matched here. */
790 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
791 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200792 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200793 c = getchr();
794 goto nfa_do_multibyte;
795 }
796#endif
797 EMIT(nfa_classcodes[p - classchars]);
798 if (extra == ADD_NL)
799 {
800 EMIT(NFA_NEWL);
801 EMIT(NFA_OR);
802 regflags |= RF_HASNL;
803 }
804 break;
805
806 case Magic('n'):
807 if (reg_string)
808 /* In a string "\n" matches a newline character. */
809 EMIT(NL);
810 else
811 {
812 /* In buffer text "\n" matches the end of a line. */
813 EMIT(NFA_NEWL);
814 regflags |= RF_HASNL;
815 }
816 break;
817
818 case Magic('('):
819 if (nfa_reg(REG_PAREN) == FAIL)
820 return FAIL; /* cascaded error */
821 break;
822
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 case Magic('|'):
824 case Magic('&'):
825 case Magic(')'):
826 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200827 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200828 return FAIL;
829
830 case Magic('='):
831 case Magic('?'):
832 case Magic('+'):
833 case Magic('@'):
834 case Magic('*'):
835 case Magic('{'):
836 /* these should follow an atom, not form an atom */
837 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200838 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200839 return FAIL;
840
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200841 case Magic('~'):
842 {
843 char_u *lp;
844
845 /* Previous substitute pattern.
846 * Generated as "\%(pattern\)". */
847 if (reg_prev_sub == NULL)
848 {
849 EMSG(_(e_nopresub));
850 return FAIL;
851 }
852 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
853 {
854 EMIT(PTR2CHAR(lp));
855 if (lp != reg_prev_sub)
856 EMIT(NFA_CONCAT);
857 }
858 EMIT(NFA_NOPEN);
859 break;
860 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200861
Bram Moolenaar428e9872013-05-30 17:05:39 +0200862 case Magic('1'):
863 case Magic('2'):
864 case Magic('3'):
865 case Magic('4'):
866 case Magic('5'):
867 case Magic('6'):
868 case Magic('7'):
869 case Magic('8'):
870 case Magic('9'):
871 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
872 nfa_has_backref = TRUE;
873 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200874
875 case Magic('z'):
876 c = no_Magic(getchr());
877 switch (c)
878 {
879 case 's':
880 EMIT(NFA_ZSTART);
881 break;
882 case 'e':
883 EMIT(NFA_ZEND);
884 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200885 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200886#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 case '1':
888 case '2':
889 case '3':
890 case '4':
891 case '5':
892 case '6':
893 case '7':
894 case '8':
895 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200896 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200897 if (reg_do_extmatch != REX_USE)
898 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200899 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
900 /* No need to set nfa_has_backref, the sub-matches don't
901 * change when \z1 .. \z9 maches or not. */
902 re_has_z = REX_USE;
903 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200904 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200905 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200906 if (reg_do_extmatch != REX_SET)
907 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200908 if (nfa_reg(REG_ZPAREN) == FAIL)
909 return FAIL; /* cascaded error */
910 re_has_z = REX_SET;
911 break;
912#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200913 default:
914 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200915 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200916 no_Magic(c));
917 return FAIL;
918 }
919 break;
920
921 case Magic('%'):
922 c = no_Magic(getchr());
923 switch (c)
924 {
925 /* () without a back reference */
926 case '(':
927 if (nfa_reg(REG_NPAREN) == FAIL)
928 return FAIL;
929 EMIT(NFA_NOPEN);
930 break;
931
932 case 'd': /* %d123 decimal */
933 case 'o': /* %o123 octal */
934 case 'x': /* %xab hex 2 */
935 case 'u': /* %uabcd hex 4 */
936 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200937 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200938 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200939
Bram Moolenaar47196582013-05-25 22:04:23 +0200940 switch (c)
941 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200942 case 'd': nr = getdecchrs(); break;
943 case 'o': nr = getoctchrs(); break;
944 case 'x': nr = gethexchrs(2); break;
945 case 'u': nr = gethexchrs(4); break;
946 case 'U': nr = gethexchrs(8); break;
947 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200948 }
949
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200950 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200951 EMSG2_RET_FAIL(
952 _("E678: Invalid character after %s%%[dxouU]"),
953 reg_magic == MAGIC_ALL);
954 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200955 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200956 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200957 break;
958
959 /* Catch \%^ and \%$ regardless of where they appear in the
960 * pattern -- regardless of whether or not it makes sense. */
961 case '^':
962 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200963 break;
964
965 case '$':
966 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200967 break;
968
969 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200970 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 break;
972
973 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200974 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200975 return FAIL;
976 break;
977
978 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200979 /* TODO: \%[abc] not supported yet */
980 return FAIL;
981
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200982 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200983 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200984 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200985 int cmp = c;
986
987 if (c == '<' || c == '>')
988 c = getchr();
989 while (VIM_ISDIGIT(c))
990 {
991 n = n * 10 + (c - '0');
992 c = getchr();
993 }
994 if (c == 'l' || c == 'c' || c == 'v')
995 {
996 EMIT(n);
997 if (c == 'l')
998 EMIT(cmp == '<' ? NFA_LNUM_LT :
999 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
1000 else if (c == 'c')
1001 EMIT(cmp == '<' ? NFA_COL_LT :
1002 cmp == '>' ? NFA_COL_GT : NFA_COL);
1003 else
1004 EMIT(cmp == '<' ? NFA_VCOL_LT :
1005 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
1006 break;
1007 }
1008 else if (c == '\'')
1009 /* TODO: \%'m not supported yet */
1010 return FAIL;
1011 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001012 syntax_error = TRUE;
1013 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1014 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001015 return FAIL;
1016 }
1017 break;
1018
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001019 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001020collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001021 /*
1022 * Glue is emitted between several atoms from the [].
1023 * It is either NFA_OR, or NFA_CONCAT.
1024 *
1025 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1026 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1027 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1028 * notation)
1029 *
1030 */
1031
1032
1033/* Emit negation atoms, if needed.
1034 * The CONCAT below merges the NOT with the previous node. */
1035#define TRY_NEG() \
1036 if (negated == TRUE) \
1037 { \
1038 EMIT(NFA_NOT); \
1039 }
1040
1041/* Emit glue between important nodes : CONCAT or OR. */
1042#define EMIT_GLUE() \
1043 if (first == FALSE) \
1044 EMIT(glue); \
1045 else \
1046 first = FALSE;
1047
1048 p = regparse;
1049 endp = skip_anyof(p);
1050 if (*endp == ']')
1051 {
1052 /*
1053 * Try to reverse engineer character classes. For example,
1054 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1055 * and perform the necessary substitutions in the NFA.
1056 */
1057 result = nfa_recognize_char_class(regparse, endp,
1058 extra == ADD_NL);
1059 if (result != FAIL)
1060 {
1061 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1062 EMIT(result);
1063 else /* must be char class + newline */
1064 {
1065 EMIT(result - ADD_NL);
1066 EMIT(NFA_NEWL);
1067 EMIT(NFA_OR);
1068 }
1069 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001070 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001071 return OK;
1072 }
1073 /*
1074 * Failed to recognize a character class. Use the simple
1075 * version that turns [abc] into 'a' OR 'b' OR 'c'
1076 */
1077 startc = endc = oldstartc = -1;
1078 first = TRUE; /* Emitting first atom in this sequence? */
1079 negated = FALSE;
1080 glue = NFA_OR;
1081 if (*regparse == '^') /* negated range */
1082 {
1083 negated = TRUE;
1084 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001085 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 }
1087 if (*regparse == '-')
1088 {
1089 startc = '-';
1090 EMIT(startc);
1091 TRY_NEG();
1092 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001093 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001094 }
1095 /* Emit the OR branches for each character in the [] */
1096 emit_range = FALSE;
1097 while (regparse < endp)
1098 {
1099 oldstartc = startc;
1100 startc = -1;
1101 got_coll_char = FALSE;
1102 if (*regparse == '[')
1103 {
1104 /* Check for [: :], [= =], [. .] */
1105 equiclass = collclass = 0;
1106 charclass = get_char_class(&regparse);
1107 if (charclass == CLASS_NONE)
1108 {
1109 equiclass = get_equi_class(&regparse);
1110 if (equiclass == 0)
1111 collclass = get_coll_element(&regparse);
1112 }
1113
1114 /* Character class like [:alpha:] */
1115 if (charclass != CLASS_NONE)
1116 {
1117 switch (charclass)
1118 {
1119 case CLASS_ALNUM:
1120 EMIT(NFA_CLASS_ALNUM);
1121 break;
1122 case CLASS_ALPHA:
1123 EMIT(NFA_CLASS_ALPHA);
1124 break;
1125 case CLASS_BLANK:
1126 EMIT(NFA_CLASS_BLANK);
1127 break;
1128 case CLASS_CNTRL:
1129 EMIT(NFA_CLASS_CNTRL);
1130 break;
1131 case CLASS_DIGIT:
1132 EMIT(NFA_CLASS_DIGIT);
1133 break;
1134 case CLASS_GRAPH:
1135 EMIT(NFA_CLASS_GRAPH);
1136 break;
1137 case CLASS_LOWER:
1138 EMIT(NFA_CLASS_LOWER);
1139 break;
1140 case CLASS_PRINT:
1141 EMIT(NFA_CLASS_PRINT);
1142 break;
1143 case CLASS_PUNCT:
1144 EMIT(NFA_CLASS_PUNCT);
1145 break;
1146 case CLASS_SPACE:
1147 EMIT(NFA_CLASS_SPACE);
1148 break;
1149 case CLASS_UPPER:
1150 EMIT(NFA_CLASS_UPPER);
1151 break;
1152 case CLASS_XDIGIT:
1153 EMIT(NFA_CLASS_XDIGIT);
1154 break;
1155 case CLASS_TAB:
1156 EMIT(NFA_CLASS_TAB);
1157 break;
1158 case CLASS_RETURN:
1159 EMIT(NFA_CLASS_RETURN);
1160 break;
1161 case CLASS_BACKSPACE:
1162 EMIT(NFA_CLASS_BACKSPACE);
1163 break;
1164 case CLASS_ESCAPE:
1165 EMIT(NFA_CLASS_ESCAPE);
1166 break;
1167 }
1168 TRY_NEG();
1169 EMIT_GLUE();
1170 continue;
1171 }
1172 /* Try equivalence class [=a=] and the like */
1173 if (equiclass != 0)
1174 {
1175 result = nfa_emit_equi_class(equiclass, negated);
1176 if (result == FAIL)
1177 {
1178 /* should never happen */
1179 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1180 }
1181 EMIT_GLUE();
1182 continue;
1183 }
1184 /* Try collating class like [. .] */
1185 if (collclass != 0)
1186 {
1187 startc = collclass; /* allow [.a.]-x as a range */
1188 /* Will emit the proper atom at the end of the
1189 * while loop. */
1190 }
1191 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001192 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1193 * start character. */
1194 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001195 {
1196 emit_range = TRUE;
1197 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001198 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001199 continue; /* reading the end of the range */
1200 }
1201
1202 /* Now handle simple and escaped characters.
1203 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1204 * accepts "\t", "\e", etc., but only when the 'l' flag in
1205 * 'cpoptions' is not included.
1206 * Posix doesn't recognize backslash at all.
1207 */
1208 if (*regparse == '\\'
1209 && !cpo_bsl
1210 && regparse + 1 <= endp
1211 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1212 || (!cpo_lit
1213 && vim_strchr(REGEXP_ABBR, regparse[1])
1214 != NULL)
1215 )
1216 )
1217 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001218 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001220 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 startc = reg_string ? NL : NFA_NEWL;
1222 else
1223 if (*regparse == 'd'
1224 || *regparse == 'o'
1225 || *regparse == 'x'
1226 || *regparse == 'u'
1227 || *regparse == 'U'
1228 )
1229 {
1230 /* TODO(RE) This needs more testing */
1231 startc = coll_get_char();
1232 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001233 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 }
1235 else
1236 {
1237 /* \r,\t,\e,\b */
1238 startc = backslash_trans(*regparse);
1239 }
1240 }
1241
1242 /* Normal printable char */
1243 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001244 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245
1246 /* Previous char was '-', so this char is end of range. */
1247 if (emit_range)
1248 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001249 endc = startc;
1250 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 if (startc > endc)
1252 EMSG_RET_FAIL(_(e_invrange));
1253#ifdef FEAT_MBYTE
1254 if (has_mbyte && ((*mb_char2len)(startc) > 1
1255 || (*mb_char2len)(endc) > 1))
1256 {
1257 if (endc > startc + 256)
1258 EMSG_RET_FAIL(_(e_invrange));
1259 /* Emit the range. "startc" was already emitted, so
1260 * skip it. */
1261 for (c = startc + 1; c <= endc; c++)
1262 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001263 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 TRY_NEG();
1265 EMIT_GLUE();
1266 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 }
1268 else
1269#endif
1270 {
1271#ifdef EBCDIC
1272 int alpha_only = FALSE;
1273
1274 /* for alphabetical range skip the gaps
1275 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1276 if (isalpha(startc) && isalpha(endc))
1277 alpha_only = TRUE;
1278#endif
1279 /* Emit the range. "startc" was already emitted, so
1280 * skip it. */
1281 for (c = startc + 1; c <= endc; c++)
1282#ifdef EBCDIC
1283 if (!alpha_only || isalpha(startc))
1284#endif
1285 {
1286 EMIT(c);
1287 TRY_NEG();
1288 EMIT_GLUE();
1289 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001290 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001291 emit_range = FALSE;
1292 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293 }
1294 else
1295 {
1296 /*
1297 * This char (startc) is not part of a range. Just
1298 * emit it.
1299 *
1300 * Normally, simply emit startc. But if we get char
1301 * code=0 from a collating char, then replace it with
1302 * 0x0a.
1303 *
1304 * This is needed to completely mimic the behaviour of
1305 * the backtracking engine.
1306 */
1307 if (got_coll_char == TRUE && startc == 0)
1308 EMIT(0x0a);
1309 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001310 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311 TRY_NEG();
1312 EMIT_GLUE();
1313 }
1314
Bram Moolenaar51a29832013-05-28 22:30:35 +02001315 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316 } /* while (p < endp) */
1317
Bram Moolenaar51a29832013-05-28 22:30:35 +02001318 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 if (*regparse == '-') /* if last, '-' is just a char */
1320 {
1321 EMIT('-');
1322 TRY_NEG();
1323 EMIT_GLUE();
1324 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001325 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001327 /* skip the trailing ] */
1328 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001329 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330 if (negated == TRUE)
1331 {
1332 /* Mark end of negated char range */
1333 EMIT(NFA_END_NEG_RANGE);
1334 EMIT(NFA_CONCAT);
1335 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001336
1337 /* \_[] also matches \n but it's not negated */
1338 if (extra == ADD_NL)
1339 {
1340 EMIT(reg_string ? NL : NFA_NEWL);
1341 EMIT(NFA_OR);
1342 }
1343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001345 } /* if exists closing ] */
1346
1347 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 {
1349 syntax_error = TRUE;
1350 EMSG_RET_FAIL(_(e_missingbracket));
1351 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001352 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 default:
1355 {
1356#ifdef FEAT_MBYTE
1357 int plen;
1358
1359nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001360 /* plen is length of current char with composing chars */
1361 if (enc_utf8 && ((*mb_char2len)(c)
1362 != (plen = (*mb_ptr2len)(old_regparse))
1363 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001365 int i = 0;
1366
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001367 /* A base character plus composing characters, or just one
1368 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001369 * This requires creating a separate atom as if enclosing
1370 * the characters in (), where NFA_COMPOSING is the ( and
1371 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 * building the postfix form, not the NFA itself;
1373 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001374 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001375 for (;;)
1376 {
1377 EMIT(c);
1378 if (i > 0)
1379 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001380 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001381 break;
1382 c = utf_ptr2char(old_regparse + i);
1383 }
1384 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385 regparse = old_regparse + plen;
1386 }
1387 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388#endif
1389 {
1390 c = no_Magic(c);
1391 EMIT(c);
1392 }
1393 return OK;
1394 }
1395 }
1396
1397#undef TRY_NEG
1398#undef EMIT_GLUE
1399
1400 return OK;
1401}
1402
1403/*
1404 * Parse something followed by possible [*+=].
1405 *
1406 * A piece is an atom, possibly followed by a multi, an indication of how many
1407 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1408 * characters: "", "a", "aa", etc.
1409 *
1410 * piece ::= atom
1411 * or atom multi
1412 */
1413 static int
1414nfa_regpiece()
1415{
1416 int i;
1417 int op;
1418 int ret;
1419 long minval, maxval;
1420 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001421 parse_state_T old_state;
1422 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001424 int old_post_pos;
1425 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001426 int quest;
1427
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001428 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1429 * next. */
1430 save_parse_state(&old_state);
1431
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001432 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001433 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434
1435 ret = nfa_regatom();
1436 if (ret == FAIL)
1437 return FAIL; /* cascaded error */
1438
1439 op = peekchr();
1440 if (re_multi_type(op) == NOT_MULTI)
1441 return OK;
1442
1443 skipchr();
1444 switch (op)
1445 {
1446 case Magic('*'):
1447 EMIT(NFA_STAR);
1448 break;
1449
1450 case Magic('+'):
1451 /*
1452 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1453 * first and only submatch would be "aaa". But the backtracking
1454 * engine interprets the plus as "try matching one more time", and
1455 * a* matches a second time at the end of the input, the empty
1456 * string.
1457 * The submatch will the empty string.
1458 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001459 * In order to be consistent with the old engine, we replace
1460 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001462 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001463 curchr = -1;
1464 if (nfa_regatom() == FAIL)
1465 return FAIL;
1466 EMIT(NFA_STAR);
1467 EMIT(NFA_CONCAT);
1468 skipchr(); /* skip the \+ */
1469 break;
1470
1471 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001472 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001473 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001474 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 switch(op)
1476 {
1477 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001478 /* \@= */
1479 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001481 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001482 /* \@! */
1483 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001484 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001486 op = no_Magic(getchr());
1487 if (op == '=')
1488 /* \@<= */
1489 i = NFA_PREV_ATOM_JUST_BEFORE;
1490 else if (op == '!')
1491 /* \@<! */
1492 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1493 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001495 /* \@> Not supported yet */
1496 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 return FAIL;
1498 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001499 if (i == 0)
1500 {
1501 syntax_error = TRUE;
1502 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1503 return FAIL;
1504 }
1505 EMIT(i);
1506 if (i == NFA_PREV_ATOM_JUST_BEFORE
1507 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1508 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 break;
1510
1511 case Magic('?'):
1512 case Magic('='):
1513 EMIT(NFA_QUEST);
1514 break;
1515
1516 case Magic('{'):
1517 /* a{2,5} will expand to 'aaa?a?a?'
1518 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1519 * version of '?'
1520 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1521 * parenthesis have the same id
1522 */
1523
1524 greedy = TRUE;
1525 c2 = peekchr();
1526 if (c2 == '-' || c2 == Magic('-'))
1527 {
1528 skipchr();
1529 greedy = FALSE;
1530 }
1531 if (!read_limits(&minval, &maxval))
1532 {
1533 syntax_error = TRUE;
1534 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1535 }
1536 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1537 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001538 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001540 if (greedy)
1541 /* \{}, \{0,} */
1542 EMIT(NFA_STAR);
1543 else
1544 /* \{-}, \{-0,} */
1545 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001546 break;
1547 }
1548
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 /* Special case: x{0} or x{-0} */
1550 if (maxval == 0)
1551 {
1552 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001553 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1555 EMIT(NFA_SKIP_CHAR);
1556 return OK;
1557 }
1558
1559 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001560 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001561 /* Save parse state after the repeated atom and the \{} */
1562 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1565 for (i = 0; i < maxval; i++)
1566 {
1567 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001568 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001569 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001570 if (nfa_regatom() == FAIL)
1571 return FAIL;
1572 /* after "minval" times, atoms are optional */
1573 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001574 {
1575 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001576 {
1577 if (greedy)
1578 EMIT(NFA_STAR);
1579 else
1580 EMIT(NFA_STAR_NONGREEDY);
1581 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001582 else
1583 EMIT(quest);
1584 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001585 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001586 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001587 if (i + 1 > minval && maxval == MAX_LIMIT)
1588 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001589 }
1590
1591 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001592 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 curchr = -1;
1594
1595 break;
1596
1597
1598 default:
1599 break;
1600 } /* end switch */
1601
1602 if (re_multi_type(peekchr()) != NOT_MULTI)
1603 {
1604 /* Can't have a multi follow a multi. */
1605 syntax_error = TRUE;
1606 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1607 }
1608
1609 return OK;
1610}
1611
1612/*
1613 * Parse one or more pieces, concatenated. It matches a match for the
1614 * first piece, followed by a match for the second piece, etc. Example:
1615 * "f[0-9]b", first matches "f", then a digit and then "b".
1616 *
1617 * concat ::= piece
1618 * or piece piece
1619 * or piece piece piece
1620 * etc.
1621 */
1622 static int
1623nfa_regconcat()
1624{
1625 int cont = TRUE;
1626 int first = TRUE;
1627
1628 while (cont)
1629 {
1630 switch (peekchr())
1631 {
1632 case NUL:
1633 case Magic('|'):
1634 case Magic('&'):
1635 case Magic(')'):
1636 cont = FALSE;
1637 break;
1638
1639 case Magic('Z'):
1640#ifdef FEAT_MBYTE
1641 regflags |= RF_ICOMBINE;
1642#endif
1643 skipchr_keepstart();
1644 break;
1645 case Magic('c'):
1646 regflags |= RF_ICASE;
1647 skipchr_keepstart();
1648 break;
1649 case Magic('C'):
1650 regflags |= RF_NOICASE;
1651 skipchr_keepstart();
1652 break;
1653 case Magic('v'):
1654 reg_magic = MAGIC_ALL;
1655 skipchr_keepstart();
1656 curchr = -1;
1657 break;
1658 case Magic('m'):
1659 reg_magic = MAGIC_ON;
1660 skipchr_keepstart();
1661 curchr = -1;
1662 break;
1663 case Magic('M'):
1664 reg_magic = MAGIC_OFF;
1665 skipchr_keepstart();
1666 curchr = -1;
1667 break;
1668 case Magic('V'):
1669 reg_magic = MAGIC_NONE;
1670 skipchr_keepstart();
1671 curchr = -1;
1672 break;
1673
1674 default:
1675 if (nfa_regpiece() == FAIL)
1676 return FAIL;
1677 if (first == FALSE)
1678 EMIT(NFA_CONCAT);
1679 else
1680 first = FALSE;
1681 break;
1682 }
1683 }
1684
1685 return OK;
1686}
1687
1688/*
1689 * Parse a branch, one or more concats, separated by "\&". It matches the
1690 * last concat, but only if all the preceding concats also match at the same
1691 * position. Examples:
1692 * "foobeep\&..." matches "foo" in "foobeep".
1693 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1694 *
1695 * branch ::= concat
1696 * or concat \& concat
1697 * or concat \& concat \& concat
1698 * etc.
1699 */
1700 static int
1701nfa_regbranch()
1702{
1703 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001704 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705
Bram Moolenaar16299b52013-05-30 18:45:23 +02001706 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707
1708 /* First branch, possibly the only one */
1709 if (nfa_regconcat() == FAIL)
1710 return FAIL;
1711
1712 ch = peekchr();
1713 /* Try next concats */
1714 while (ch == Magic('&'))
1715 {
1716 skipchr();
1717 EMIT(NFA_NOPEN);
1718 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001719 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001720 if (nfa_regconcat() == FAIL)
1721 return FAIL;
1722 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001723 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 EMIT(NFA_SKIP_CHAR);
1725 EMIT(NFA_CONCAT);
1726 ch = peekchr();
1727 }
1728
1729 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001730 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001731 EMIT(NFA_SKIP_CHAR);
1732
1733 return OK;
1734}
1735
1736/*
1737 * Parse a pattern, one or more branches, separated by "\|". It matches
1738 * anything that matches one of the branches. Example: "foo\|beep" matches
1739 * "foo" and matches "beep". If more than one branch matches, the first one
1740 * is used.
1741 *
1742 * pattern ::= branch
1743 * or branch \| branch
1744 * or branch \| branch \| branch
1745 * etc.
1746 */
1747 static int
1748nfa_reg(paren)
1749 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1750{
1751 int parno = 0;
1752
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001753 if (paren == REG_PAREN)
1754 {
1755 if (regnpar >= NSUBEXP) /* Too many `(' */
1756 {
1757 syntax_error = TRUE;
1758 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1759 }
1760 parno = regnpar++;
1761 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001762#ifdef FEAT_SYN_HL
1763 else if (paren == REG_ZPAREN)
1764 {
1765 /* Make a ZOPEN node. */
1766 if (regnzpar >= NSUBEXP)
1767 {
1768 syntax_error = TRUE;
1769 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1770 }
1771 parno = regnzpar++;
1772 }
1773#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774
1775 if (nfa_regbranch() == FAIL)
1776 return FAIL; /* cascaded error */
1777
1778 while (peekchr() == Magic('|'))
1779 {
1780 skipchr();
1781 if (nfa_regbranch() == FAIL)
1782 return FAIL; /* cascaded error */
1783 EMIT(NFA_OR);
1784 }
1785
1786 /* Check for proper termination. */
1787 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1788 {
1789 syntax_error = TRUE;
1790 if (paren == REG_NPAREN)
1791 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1792 else
1793 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1794 }
1795 else if (paren == REG_NOPAREN && peekchr() != NUL)
1796 {
1797 syntax_error = TRUE;
1798 if (peekchr() == Magic(')'))
1799 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1800 else
1801 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1802 }
1803 /*
1804 * Here we set the flag allowing back references to this set of
1805 * parentheses.
1806 */
1807 if (paren == REG_PAREN)
1808 {
1809 had_endbrace[parno] = TRUE; /* have seen the close paren */
1810 EMIT(NFA_MOPEN + parno);
1811 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001812#ifdef FEAT_SYN_HL
1813 else if (paren == REG_ZPAREN)
1814 EMIT(NFA_ZOPEN + parno);
1815#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816
1817 return OK;
1818}
1819
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820#ifdef DEBUG
1821static char_u code[50];
1822
1823 static void
1824nfa_set_code(c)
1825 int c;
1826{
1827 int addnl = FALSE;
1828
1829 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1830 {
1831 addnl = TRUE;
1832 c -= ADD_NL;
1833 }
1834
1835 STRCPY(code, "");
1836 switch (c)
1837 {
1838 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1839 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1840 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1841 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1842 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1843 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1844
Bram Moolenaar5714b802013-05-28 22:03:20 +02001845 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1846 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1847 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1848 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1849 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1850 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1851 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1852 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1853 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001854#ifdef FEAT_SYN_HL
1855 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1856 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1857 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1858 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1859 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1860 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1861 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1862 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1863 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1864#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001865 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1866
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001867 case NFA_PREV_ATOM_NO_WIDTH:
1868 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001869 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1870 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001871 case NFA_PREV_ATOM_JUST_BEFORE:
1872 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1873 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1874 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001875 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1876 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001878 case NFA_START_INVISIBLE_BEFORE:
1879 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1883 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1884
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001885 case NFA_MOPEN:
1886 case NFA_MOPEN1:
1887 case NFA_MOPEN2:
1888 case NFA_MOPEN3:
1889 case NFA_MOPEN4:
1890 case NFA_MOPEN5:
1891 case NFA_MOPEN6:
1892 case NFA_MOPEN7:
1893 case NFA_MOPEN8:
1894 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 STRCPY(code, "NFA_MOPEN(x)");
1896 code[10] = c - NFA_MOPEN + '0';
1897 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001898 case NFA_MCLOSE:
1899 case NFA_MCLOSE1:
1900 case NFA_MCLOSE2:
1901 case NFA_MCLOSE3:
1902 case NFA_MCLOSE4:
1903 case NFA_MCLOSE5:
1904 case NFA_MCLOSE6:
1905 case NFA_MCLOSE7:
1906 case NFA_MCLOSE8:
1907 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 STRCPY(code, "NFA_MCLOSE(x)");
1909 code[11] = c - NFA_MCLOSE + '0';
1910 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001911#ifdef FEAT_SYN_HL
1912 case NFA_ZOPEN:
1913 case NFA_ZOPEN1:
1914 case NFA_ZOPEN2:
1915 case NFA_ZOPEN3:
1916 case NFA_ZOPEN4:
1917 case NFA_ZOPEN5:
1918 case NFA_ZOPEN6:
1919 case NFA_ZOPEN7:
1920 case NFA_ZOPEN8:
1921 case NFA_ZOPEN9:
1922 STRCPY(code, "NFA_ZOPEN(x)");
1923 code[10] = c - NFA_ZOPEN + '0';
1924 break;
1925 case NFA_ZCLOSE:
1926 case NFA_ZCLOSE1:
1927 case NFA_ZCLOSE2:
1928 case NFA_ZCLOSE3:
1929 case NFA_ZCLOSE4:
1930 case NFA_ZCLOSE5:
1931 case NFA_ZCLOSE6:
1932 case NFA_ZCLOSE7:
1933 case NFA_ZCLOSE8:
1934 case NFA_ZCLOSE9:
1935 STRCPY(code, "NFA_ZCLOSE(x)");
1936 code[11] = c - NFA_ZCLOSE + '0';
1937 break;
1938#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1940 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1941 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1942 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001943 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1944 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001946 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1947 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1948 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001949 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1950 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1951 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1953 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1954 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1955 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1956 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1957 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1958 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1959 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1960 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1961 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1962 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1963 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1964 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1965 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1966 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1967 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1968 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1969
1970 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1971 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1972 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1973 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1974 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1975 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1976 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1977 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1978 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1979 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1980 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1981 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1982 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1983 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1984 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1985 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1986 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1987 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1988 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1989 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1990 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1991 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1992 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1993 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1994 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1995 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1996 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1997
1998 default:
1999 STRCPY(code, "CHAR(x)");
2000 code[5] = c;
2001 }
2002
2003 if (addnl == TRUE)
2004 STRCAT(code, " + NEWLINE ");
2005
2006}
2007
2008#ifdef ENABLE_LOG
2009static FILE *log_fd;
2010
2011/*
2012 * Print the postfix notation of the current regexp.
2013 */
2014 static void
2015nfa_postfix_dump(expr, retval)
2016 char_u *expr;
2017 int retval;
2018{
2019 int *p;
2020 FILE *f;
2021
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002022 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 if (f != NULL)
2024 {
2025 fprintf(f, "\n-------------------------\n");
2026 if (retval == FAIL)
2027 fprintf(f, ">>> NFA engine failed ... \n");
2028 else if (retval == OK)
2029 fprintf(f, ">>> NFA engine succeeded !\n");
2030 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002031 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002032 {
2033 nfa_set_code(*p);
2034 fprintf(f, "%s, ", code);
2035 }
2036 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002037 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 fprintf(f, "%d ", *p);
2039 fprintf(f, "\n\n");
2040 fclose(f);
2041 }
2042}
2043
2044/*
2045 * Print the NFA starting with a root node "state".
2046 */
2047 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002048nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 FILE *debugf;
2050 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002051{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002052 garray_T indent;
2053
2054 ga_init2(&indent, 1, 64);
2055 ga_append(&indent, '\0');
2056 nfa_print_state2(debugf, state, &indent);
2057 ga_clear(&indent);
2058}
2059
2060 static void
2061nfa_print_state2(debugf, state, indent)
2062 FILE *debugf;
2063 nfa_state_T *state;
2064 garray_T *indent;
2065{
2066 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067
2068 if (state == NULL)
2069 return;
2070
2071 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002072
2073 /* Output indent */
2074 p = (char_u *)indent->ga_data;
2075 if (indent->ga_len >= 3)
2076 {
2077 int last = indent->ga_len - 3;
2078 char_u save[2];
2079
2080 STRNCPY(save, &p[last], 2);
2081 STRNCPY(&p[last], "+-", 2);
2082 fprintf(debugf, " %s", p);
2083 STRNCPY(&p[last], save, 2);
2084 }
2085 else
2086 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087
2088 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002089 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2090 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002091 if (state->id < 0)
2092 return;
2093
2094 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002095
2096 /* grow indent for state->out */
2097 indent->ga_len -= 1;
2098 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002099 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002100 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002101 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002102 ga_append(indent, '\0');
2103
2104 nfa_print_state2(debugf, state->out, indent);
2105
2106 /* replace last part of indent for state->out1 */
2107 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002108 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002109 ga_append(indent, '\0');
2110
2111 nfa_print_state2(debugf, state->out1, indent);
2112
2113 /* shrink indent */
2114 indent->ga_len -= 3;
2115 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002116}
2117
2118/*
2119 * Print the NFA state machine.
2120 */
2121 static void
2122nfa_dump(prog)
2123 nfa_regprog_T *prog;
2124{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002125 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126
2127 if (debugf != NULL)
2128 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002129 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002130 fclose(debugf);
2131 }
2132}
2133#endif /* ENABLE_LOG */
2134#endif /* DEBUG */
2135
2136/*
2137 * Parse r.e. @expr and convert it into postfix form.
2138 * Return the postfix string on success, NULL otherwise.
2139 */
2140 static int *
2141re2post()
2142{
2143 if (nfa_reg(REG_NOPAREN) == FAIL)
2144 return NULL;
2145 EMIT(NFA_MOPEN);
2146 return post_start;
2147}
2148
2149/* NB. Some of the code below is inspired by Russ's. */
2150
2151/*
2152 * Represents an NFA state plus zero or one or two arrows exiting.
2153 * if c == MATCH, no arrows out; matching state.
2154 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2155 * If c < 256, labeled arrow with character c to out.
2156 */
2157
2158static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2159
2160/*
2161 * Allocate and initialize nfa_state_T.
2162 */
2163 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002164alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 int c;
2166 nfa_state_T *out;
2167 nfa_state_T *out1;
2168{
2169 nfa_state_T *s;
2170
2171 if (istate >= nstate)
2172 return NULL;
2173
2174 s = &state_ptr[istate++];
2175
2176 s->c = c;
2177 s->out = out;
2178 s->out1 = out1;
2179
2180 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002181 s->lastlist[0] = 0;
2182 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002183 s->negated = FALSE;
2184
2185 return s;
2186}
2187
2188/*
2189 * A partially built NFA without the matching state filled in.
2190 * Frag_T.start points at the start state.
2191 * Frag_T.out is a list of places that need to be set to the
2192 * next state for this fragment.
2193 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002194
2195/* Since the out pointers in the list are always
2196 * uninitialized, we use the pointers themselves
2197 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002199union Ptrlist
2200{
2201 Ptrlist *next;
2202 nfa_state_T *s;
2203};
2204
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002205struct Frag
2206{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002207 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002208 Ptrlist *out;
2209};
2210typedef struct Frag Frag_T;
2211
2212static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2213static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2214static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2215static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2216static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2217static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2218
2219/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002220 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221 */
2222 static Frag_T
2223frag(start, out)
2224 nfa_state_T *start;
2225 Ptrlist *out;
2226{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002227 Frag_T n;
2228
2229 n.start = start;
2230 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002231 return n;
2232}
2233
2234/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235 * Create singleton list containing just outp.
2236 */
2237 static Ptrlist *
2238list1(outp)
2239 nfa_state_T **outp;
2240{
2241 Ptrlist *l;
2242
2243 l = (Ptrlist *)outp;
2244 l->next = NULL;
2245 return l;
2246}
2247
2248/*
2249 * Patch the list of states at out to point to start.
2250 */
2251 static void
2252patch(l, s)
2253 Ptrlist *l;
2254 nfa_state_T *s;
2255{
2256 Ptrlist *next;
2257
2258 for (; l; l = next)
2259 {
2260 next = l->next;
2261 l->s = s;
2262 }
2263}
2264
2265
2266/*
2267 * Join the two lists l1 and l2, returning the combination.
2268 */
2269 static Ptrlist *
2270append(l1, l2)
2271 Ptrlist *l1;
2272 Ptrlist *l2;
2273{
2274 Ptrlist *oldl1;
2275
2276 oldl1 = l1;
2277 while (l1->next)
2278 l1 = l1->next;
2279 l1->next = l2;
2280 return oldl1;
2281}
2282
2283/*
2284 * Stack used for transforming postfix form into NFA.
2285 */
2286static Frag_T empty;
2287
2288 static void
2289st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002290 int *postfix UNUSED;
2291 int *end UNUSED;
2292 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002294#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 FILE *df;
2296 int *p2;
2297
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002298 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002299 if (df)
2300 {
2301 fprintf(df, "Error popping the stack!\n");
2302#ifdef DEBUG
2303 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2304#endif
2305 fprintf(df, "Postfix form is: ");
2306#ifdef DEBUG
2307 for (p2 = postfix; p2 < end; p2++)
2308 {
2309 nfa_set_code(*p2);
2310 fprintf(df, "%s, ", code);
2311 }
2312 nfa_set_code(*p);
2313 fprintf(df, "\nCurrent position is: ");
2314 for (p2 = postfix; p2 <= p; p2 ++)
2315 {
2316 nfa_set_code(*p2);
2317 fprintf(df, "%s, ", code);
2318 }
2319#else
2320 for (p2 = postfix; p2 < end; p2++)
2321 {
2322 fprintf(df, "%d, ", *p2);
2323 }
2324 fprintf(df, "\nCurrent position is: ");
2325 for (p2 = postfix; p2 <= p; p2 ++)
2326 {
2327 fprintf(df, "%d, ", *p2);
2328 }
2329#endif
2330 fprintf(df, "\n--------------------------\n");
2331 fclose(df);
2332 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002333#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 EMSG(_("E874: (NFA) Could not pop the stack !"));
2335}
2336
2337/*
2338 * Push an item onto the stack.
2339 */
2340 static void
2341st_push(s, p, stack_end)
2342 Frag_T s;
2343 Frag_T **p;
2344 Frag_T *stack_end;
2345{
2346 Frag_T *stackp = *p;
2347
2348 if (stackp >= stack_end)
2349 return;
2350 *stackp = s;
2351 *p = *p + 1;
2352}
2353
2354/*
2355 * Pop an item from the stack.
2356 */
2357 static Frag_T
2358st_pop(p, stack)
2359 Frag_T **p;
2360 Frag_T *stack;
2361{
2362 Frag_T *stackp;
2363
2364 *p = *p - 1;
2365 stackp = *p;
2366 if (stackp < stack)
2367 return empty;
2368 return **p;
2369}
2370
2371/*
2372 * Convert a postfix form into its equivalent NFA.
2373 * Return the NFA start state on success, NULL otherwise.
2374 */
2375 static nfa_state_T *
2376post2nfa(postfix, end, nfa_calc_size)
2377 int *postfix;
2378 int *end;
2379 int nfa_calc_size;
2380{
2381 int *p;
2382 int mopen;
2383 int mclose;
2384 Frag_T *stack = NULL;
2385 Frag_T *stackp = NULL;
2386 Frag_T *stack_end = NULL;
2387 Frag_T e1;
2388 Frag_T e2;
2389 Frag_T e;
2390 nfa_state_T *s;
2391 nfa_state_T *s1;
2392 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002393 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002394
2395 if (postfix == NULL)
2396 return NULL;
2397
Bram Moolenaar053bb602013-05-20 13:55:21 +02002398#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002399#define POP() st_pop(&stackp, stack); \
2400 if (stackp < stack) \
2401 { \
2402 st_error(postfix, end, p); \
2403 return NULL; \
2404 }
2405
2406 if (nfa_calc_size == FALSE)
2407 {
2408 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002409 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002410 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002411 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412 }
2413
2414 for (p = postfix; p < end; ++p)
2415 {
2416 switch (*p)
2417 {
2418 case NFA_CONCAT:
2419 /* Catenation.
2420 * Pay attention: this operator does not exist
2421 * in the r.e. itself (it is implicit, really).
2422 * It is added when r.e. is translated to postfix
2423 * form in re2post().
2424 *
2425 * No new state added here. */
2426 if (nfa_calc_size == TRUE)
2427 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002428 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002429 break;
2430 }
2431 e2 = POP();
2432 e1 = POP();
2433 patch(e1.out, e2.start);
2434 PUSH(frag(e1.start, e2.out));
2435 break;
2436
2437 case NFA_NOT:
2438 /* Negation of a character */
2439 if (nfa_calc_size == TRUE)
2440 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002441 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 break;
2443 }
2444 e1 = POP();
2445 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002446#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002447 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002449#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 PUSH(e1);
2451 break;
2452
2453 case NFA_OR:
2454 /* Alternation */
2455 if (nfa_calc_size == TRUE)
2456 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002457 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 break;
2459 }
2460 e2 = POP();
2461 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002462 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002464 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465 PUSH(frag(s, append(e1.out, e2.out)));
2466 break;
2467
2468 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002469 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002470 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();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002476 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002477 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002478 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 patch(e.out, s);
2480 PUSH(frag(s, list1(&s->out1)));
2481 break;
2482
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002483 case NFA_STAR_NONGREEDY:
2484 /* Zero or more, prefer zero */
2485 if (nfa_calc_size == TRUE)
2486 {
2487 nstate++;
2488 break;
2489 }
2490 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002491 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002492 if (s == NULL)
2493 goto theend;
2494 patch(e.out, s);
2495 PUSH(frag(s, list1(&s->out)));
2496 break;
2497
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002498 case NFA_QUEST:
2499 /* one or zero atoms=> greedy match */
2500 if (nfa_calc_size == TRUE)
2501 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002502 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503 break;
2504 }
2505 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002506 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002507 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002508 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002509 PUSH(frag(s, append(e.out, list1(&s->out1))));
2510 break;
2511
2512 case NFA_QUEST_NONGREEDY:
2513 /* zero or one atoms => non-greedy match */
2514 if (nfa_calc_size == TRUE)
2515 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002516 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 break;
2518 }
2519 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002520 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002522 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002523 PUSH(frag(s, append(e.out, list1(&s->out))));
2524 break;
2525
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002526 case NFA_SKIP_CHAR:
2527 /* Symbol of 0-length, Used in a repetition
2528 * with max/min count of 0 */
2529 if (nfa_calc_size == TRUE)
2530 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002531 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002532 break;
2533 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002534 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002535 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002536 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 PUSH(frag(s, list1(&s->out)));
2538 break;
2539
2540 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002541 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002542 case NFA_PREV_ATOM_JUST_BEFORE:
2543 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002544 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002545 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002546 * The \@<= operator: match for the preceding atom.
2547 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002549 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550
2551 if (nfa_calc_size == TRUE)
2552 {
2553 nstate += 2;
2554 break;
2555 }
2556 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002557 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002559 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560 patch(e.out, s1);
2561
Bram Moolenaar525666f2013-06-02 16:40:55 +02002562 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002563 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002564 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002565 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2566 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002567 {
2568 s->negated = TRUE;
2569 s1->negated = TRUE;
2570 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002571 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2572 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2573 {
2574 s->val = *++p; /* get the count */
2575 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2576 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002577
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002578 PUSH(frag(s, list1(&s1->out)));
2579 break;
2580
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002581#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002582 case NFA_COMPOSING: /* char with composing char */
2583#if 0
2584 /* TODO */
2585 if (regflags & RF_ICOMBINE)
2586 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002587 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002588 }
2589#endif
2590 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002591#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002592
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002593 case NFA_MOPEN: /* \( \) Submatch */
2594 case NFA_MOPEN1:
2595 case NFA_MOPEN2:
2596 case NFA_MOPEN3:
2597 case NFA_MOPEN4:
2598 case NFA_MOPEN5:
2599 case NFA_MOPEN6:
2600 case NFA_MOPEN7:
2601 case NFA_MOPEN8:
2602 case NFA_MOPEN9:
2603#ifdef FEAT_SYN_HL
2604 case NFA_ZOPEN: /* \z( \) Submatch */
2605 case NFA_ZOPEN1:
2606 case NFA_ZOPEN2:
2607 case NFA_ZOPEN3:
2608 case NFA_ZOPEN4:
2609 case NFA_ZOPEN5:
2610 case NFA_ZOPEN6:
2611 case NFA_ZOPEN7:
2612 case NFA_ZOPEN8:
2613 case NFA_ZOPEN9:
2614#endif
2615 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616 if (nfa_calc_size == TRUE)
2617 {
2618 nstate += 2;
2619 break;
2620 }
2621
2622 mopen = *p;
2623 switch (*p)
2624 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002625 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2626#ifdef FEAT_SYN_HL
2627 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2628 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2629 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2630 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2631 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2632 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2633 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2634 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2635 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2636 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2637#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002638#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002639 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002640#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002642 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 mclose = *p + NSUBEXP;
2644 break;
2645 }
2646
2647 /* Allow "NFA_MOPEN" as a valid postfix representation for
2648 * the empty regexp "". In this case, the NFA will be
2649 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2650 * empty groups of parenthesis, and empty mbyte chars */
2651 if (stackp == stack)
2652 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002653 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002655 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002656 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002657 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002658 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659 patch(list1(&s->out), s1);
2660 PUSH(frag(s, list1(&s1->out)));
2661 break;
2662 }
2663
2664 /* At least one node was emitted before NFA_MOPEN, so
2665 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2666 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002667 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002668 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002669 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670
Bram Moolenaar525666f2013-06-02 16:40:55 +02002671 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002673 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674 patch(e.out, s1);
2675
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002676#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002677 if (mopen == NFA_COMPOSING)
2678 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002679 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002680#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002681
2682 PUSH(frag(s, list1(&s1->out)));
2683 break;
2684
Bram Moolenaar5714b802013-05-28 22:03:20 +02002685 case NFA_BACKREF1:
2686 case NFA_BACKREF2:
2687 case NFA_BACKREF3:
2688 case NFA_BACKREF4:
2689 case NFA_BACKREF5:
2690 case NFA_BACKREF6:
2691 case NFA_BACKREF7:
2692 case NFA_BACKREF8:
2693 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002694#ifdef FEAT_SYN_HL
2695 case NFA_ZREF1:
2696 case NFA_ZREF2:
2697 case NFA_ZREF3:
2698 case NFA_ZREF4:
2699 case NFA_ZREF5:
2700 case NFA_ZREF6:
2701 case NFA_ZREF7:
2702 case NFA_ZREF8:
2703 case NFA_ZREF9:
2704#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002705 if (nfa_calc_size == TRUE)
2706 {
2707 nstate += 2;
2708 break;
2709 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002710 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002711 if (s == NULL)
2712 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002713 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002714 if (s1 == NULL)
2715 goto theend;
2716 patch(list1(&s->out), s1);
2717 PUSH(frag(s, list1(&s1->out)));
2718 break;
2719
Bram Moolenaar423532e2013-05-29 21:14:42 +02002720 case NFA_LNUM:
2721 case NFA_LNUM_GT:
2722 case NFA_LNUM_LT:
2723 case NFA_VCOL:
2724 case NFA_VCOL_GT:
2725 case NFA_VCOL_LT:
2726 case NFA_COL:
2727 case NFA_COL_GT:
2728 case NFA_COL_LT:
2729 if (nfa_calc_size == TRUE)
2730 {
2731 nstate += 1;
2732 break;
2733 }
2734 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002735 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002736 if (s == NULL)
2737 goto theend;
2738 s->val = e1.start->c;
2739 PUSH(frag(s, list1(&s->out)));
2740 break;
2741
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 case NFA_ZSTART:
2743 case NFA_ZEND:
2744 default:
2745 /* Operands */
2746 if (nfa_calc_size == TRUE)
2747 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002748 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002749 break;
2750 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002751 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002753 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002754 PUSH(frag(s, list1(&s->out)));
2755 break;
2756
2757 } /* switch(*p) */
2758
2759 } /* for(p = postfix; *p; ++p) */
2760
2761 if (nfa_calc_size == TRUE)
2762 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002763 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002764 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 }
2766
2767 e = POP();
2768 if (stackp != stack)
2769 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2770
2771 if (istate >= nstate)
2772 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2773
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002774 matchstate = &state_ptr[istate++]; /* the match state */
2775 matchstate->c = NFA_MATCH;
2776 matchstate->out = matchstate->out1 = NULL;
2777
2778 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002779 ret = e.start;
2780
2781theend:
2782 vim_free(stack);
2783 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784
2785#undef POP1
2786#undef PUSH1
2787#undef POP2
2788#undef PUSH2
2789#undef POP
2790#undef PUSH
2791}
2792
2793/****************************************************************
2794 * NFA execution code.
2795 ****************************************************************/
2796
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002797typedef struct
2798{
2799 int in_use; /* number of subexpr with useful info */
2800
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002801 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002802 union
2803 {
2804 struct multipos
2805 {
2806 lpos_T start;
2807 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002808 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002809 struct linepos
2810 {
2811 char_u *start;
2812 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002813 } line[NSUBEXP];
2814 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002815} regsub_T;
2816
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002817typedef struct
2818{
2819 regsub_T norm; /* \( .. \) matches */
2820#ifdef FEAT_SYN_HL
2821 regsub_T synt; /* \z( .. \) matches */
2822#endif
2823} regsubs_T;
2824
Bram Moolenaar963fee22013-05-26 21:47:28 +02002825/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002826typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827{
2828 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002829 int count;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002830 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002831} nfa_thread_T;
2832
Bram Moolenaar963fee22013-05-26 21:47:28 +02002833/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834typedef struct
2835{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002836 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002837 int n; /* nr of states currently in "t" */
2838 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002839 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002840} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002841
Bram Moolenaar5714b802013-05-28 22:03:20 +02002842#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002843static void log_subsexpr __ARGS((regsubs_T *subs));
2844static void log_subexpr __ARGS((regsub_T *sub));
2845
2846 static void
2847log_subsexpr(subs)
2848 regsubs_T *subs;
2849{
2850 log_subexpr(&subs->norm);
2851# ifdef FEAT_SYN_HL
2852 log_subexpr(&subs->synt);
2853# endif
2854}
2855
Bram Moolenaar5714b802013-05-28 22:03:20 +02002856 static void
2857log_subexpr(sub)
2858 regsub_T *sub;
2859{
2860 int j;
2861
2862 for (j = 0; j < sub->in_use; j++)
2863 if (REG_MULTI)
2864 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2865 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002866 sub->list.multi[j].start.col,
2867 (int)sub->list.multi[j].start.lnum,
2868 sub->list.multi[j].end.col,
2869 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002870 else
2871 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2872 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002873 (char *)sub->list.line[j].start,
2874 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002875 fprintf(log_fd, "\n");
2876}
2877#endif
2878
Bram Moolenaar963fee22013-05-26 21:47:28 +02002879/* Used during execution: whether a match has been found. */
2880static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002881
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002882static void clear_sub __ARGS((regsub_T *sub));
2883static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2884static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002885static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002886static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
2887static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int *ip));
2888
2889 static void
2890clear_sub(sub)
2891 regsub_T *sub;
2892{
2893 if (REG_MULTI)
2894 /* Use 0xff to set lnum to -1 */
2895 vim_memset(sub->list.multi, 0xff,
2896 sizeof(struct multipos) * nfa_nsubexpr);
2897 else
2898 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2899 sub->in_use = 0;
2900}
2901
2902/*
2903 * Copy the submatches from "from" to "to".
2904 */
2905 static void
2906copy_sub(to, from)
2907 regsub_T *to;
2908 regsub_T *from;
2909{
2910 to->in_use = from->in_use;
2911 if (from->in_use > 0)
2912 {
2913 /* Copy the match start and end positions. */
2914 if (REG_MULTI)
2915 mch_memmove(&to->list.multi[0],
2916 &from->list.multi[0],
2917 sizeof(struct multipos) * from->in_use);
2918 else
2919 mch_memmove(&to->list.line[0],
2920 &from->list.line[0],
2921 sizeof(struct linepos) * from->in_use);
2922 }
2923}
2924
2925/*
2926 * Like copy_sub() but exclude the main match.
2927 */
2928 static void
2929copy_sub_off(to, from)
2930 regsub_T *to;
2931 regsub_T *from;
2932{
2933 if (to->in_use < from->in_use)
2934 to->in_use = from->in_use;
2935 if (from->in_use > 1)
2936 {
2937 /* Copy the match start and end positions. */
2938 if (REG_MULTI)
2939 mch_memmove(&to->list.multi[1],
2940 &from->list.multi[1],
2941 sizeof(struct multipos) * (from->in_use - 1));
2942 else
2943 mch_memmove(&to->list.line[1],
2944 &from->list.line[1],
2945 sizeof(struct linepos) * (from->in_use - 1));
2946 }
2947}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002948
Bram Moolenaar428e9872013-05-30 17:05:39 +02002949/*
2950 * Return TRUE if "sub1" and "sub2" have the same positions.
2951 */
2952 static int
2953sub_equal(sub1, sub2)
2954 regsub_T *sub1;
2955 regsub_T *sub2;
2956{
2957 int i;
2958 int todo;
2959 linenr_T s1, e1;
2960 linenr_T s2, e2;
2961 char_u *sp1, *ep1;
2962 char_u *sp2, *ep2;
2963
2964 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2965 if (REG_MULTI)
2966 {
2967 for (i = 0; i < todo; ++i)
2968 {
2969 if (i < sub1->in_use)
2970 {
2971 s1 = sub1->list.multi[i].start.lnum;
2972 e1 = sub1->list.multi[i].end.lnum;
2973 }
2974 else
2975 {
2976 s1 = 0;
2977 e1 = 0;
2978 }
2979 if (i < sub2->in_use)
2980 {
2981 s2 = sub2->list.multi[i].start.lnum;
2982 e2 = sub2->list.multi[i].end.lnum;
2983 }
2984 else
2985 {
2986 s2 = 0;
2987 e2 = 0;
2988 }
2989 if (s1 != s2 || e1 != e2)
2990 return FALSE;
2991 if (s1 != 0 && sub1->list.multi[i].start.col
2992 != sub2->list.multi[i].start.col)
2993 return FALSE;
2994 if (e1 != 0 && sub1->list.multi[i].end.col
2995 != sub2->list.multi[i].end.col)
2996 return FALSE;
2997 }
2998 }
2999 else
3000 {
3001 for (i = 0; i < todo; ++i)
3002 {
3003 if (i < sub1->in_use)
3004 {
3005 sp1 = sub1->list.line[i].start;
3006 ep1 = sub1->list.line[i].end;
3007 }
3008 else
3009 {
3010 sp1 = NULL;
3011 ep1 = NULL;
3012 }
3013 if (i < sub2->in_use)
3014 {
3015 sp2 = sub2->list.line[i].start;
3016 ep2 = sub2->list.line[i].end;
3017 }
3018 else
3019 {
3020 sp2 = NULL;
3021 ep2 = NULL;
3022 }
3023 if (sp1 != sp2 || ep1 != ep2)
3024 return FALSE;
3025 }
3026 }
3027
3028 return TRUE;
3029}
3030
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003031#ifdef ENABLE_LOG
3032 static void
3033report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid);
3034{
3035 int col;
3036
3037 if (sub->in_use <= 0)
3038 col = -1;
3039 else if (REG_MULTI)
3040 col = sub->list.multi[0].start.col;
3041 else
3042 col = (int)(sub->list.line[0].start - regline);
3043 nfa_set_code(state->c);
3044 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3045 action, abs(state->id), lid, state->c, code, col);
3046}
3047#endif
3048
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003049 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003050addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003051 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003052 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003053 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003054 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003055{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003056 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003057 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003058 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003059 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003060 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003061 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003062 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003063#ifdef ENABLE_LOG
3064 int did_print = FALSE;
3065#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003066
3067 if (l == NULL || state == NULL)
3068 return;
3069
3070 switch (state->c)
3071 {
3072 case NFA_SPLIT:
3073 case NFA_NOT:
3074 case NFA_NOPEN:
3075 case NFA_NCLOSE:
3076 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003077 case NFA_MCLOSE1:
3078 case NFA_MCLOSE2:
3079 case NFA_MCLOSE3:
3080 case NFA_MCLOSE4:
3081 case NFA_MCLOSE5:
3082 case NFA_MCLOSE6:
3083 case NFA_MCLOSE7:
3084 case NFA_MCLOSE8:
3085 case NFA_MCLOSE9:
3086#ifdef FEAT_SYN_HL
3087 case NFA_ZCLOSE:
3088 case NFA_ZCLOSE1:
3089 case NFA_ZCLOSE2:
3090 case NFA_ZCLOSE3:
3091 case NFA_ZCLOSE4:
3092 case NFA_ZCLOSE5:
3093 case NFA_ZCLOSE6:
3094 case NFA_ZCLOSE7:
3095 case NFA_ZCLOSE8:
3096 case NFA_ZCLOSE9:
3097#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003098 /* These nodes are not added themselves but their "out" and/or
3099 * "out1" may be added below. */
3100 break;
3101
3102 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003103 case NFA_MOPEN1:
3104 case NFA_MOPEN2:
3105 case NFA_MOPEN3:
3106 case NFA_MOPEN4:
3107 case NFA_MOPEN5:
3108 case NFA_MOPEN6:
3109 case NFA_MOPEN7:
3110 case NFA_MOPEN8:
3111 case NFA_MOPEN9:
3112#ifdef FEAT_SYN_HL
3113 case NFA_ZOPEN:
3114 case NFA_ZOPEN1:
3115 case NFA_ZOPEN2:
3116 case NFA_ZOPEN3:
3117 case NFA_ZOPEN4:
3118 case NFA_ZOPEN5:
3119 case NFA_ZOPEN6:
3120 case NFA_ZOPEN7:
3121 case NFA_ZOPEN8:
3122 case NFA_ZOPEN9:
3123#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003124 /* These nodes do not need to be added, but we need to bail out
3125 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003126 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003127 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003128 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003129 break;
3130
Bram Moolenaar307aa162013-06-02 16:34:21 +02003131 case NFA_BOL:
3132 case NFA_BOF:
3133 /* "^" won't match past end-of-line, don't bother trying.
3134 * Except when we are going to the next line for a look-behind
3135 * match. */
3136 if (reginput > regline
3137 && (nfa_endp == NULL
3138 || !REG_MULTI
3139 || reglnum == nfa_endp->se_u.pos.lnum))
3140 goto skip_add;
3141 /* FALLTHROUGH */
3142
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003143 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003144 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003145 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003146 /* This state is already in the list, don't add it again,
3147 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003148 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003149 {
3150skip_add:
3151#ifdef ENABLE_LOG
3152 nfa_set_code(state->c);
3153 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3154 abs(state->id), l->id, state->c, code);
3155#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003156 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003157 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003158
3159 /* See if the same state is already in the list with the same
3160 * positions. */
3161 for (i = 0; i < l->n; ++i)
3162 {
3163 thread = &l->t[i];
3164 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003165 && sub_equal(&thread->subs.norm, &subs->norm)
3166#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003167 && (!nfa_has_zsubexpr ||
3168 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003169#endif
3170 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003171 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003172 }
3173 }
3174
3175 /* when there are backreferences the number of states may be (a
3176 * lot) bigger */
3177 if (nfa_has_backref && l->n == l->len)
3178 {
3179 int newlen = l->len * 3 / 2 + 50;
3180
3181 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3182 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003183 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003184
3185 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003186 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003187 thread = &l->t[l->n++];
3188 thread->state = state;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003189 copy_sub(&thread->subs.norm, &subs->norm);
3190#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003191 if (nfa_has_zsubexpr)
3192 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003193#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003194#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003195 report_state("Adding", &thread->subs.norm, state, l->id);
3196 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003197#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003198 }
3199
3200#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003201 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003202 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203#endif
3204 switch (state->c)
3205 {
3206 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003207 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003208 break;
3209
3210 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003211 /* order matters here */
3212 addstate(l, state->out, subs, off);
3213 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003214 break;
3215
Bram Moolenaar5714b802013-05-28 22:03:20 +02003216 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217 case NFA_NOPEN:
3218 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003219 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 break;
3221
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003222 case NFA_MOPEN:
3223 case NFA_MOPEN1:
3224 case NFA_MOPEN2:
3225 case NFA_MOPEN3:
3226 case NFA_MOPEN4:
3227 case NFA_MOPEN5:
3228 case NFA_MOPEN6:
3229 case NFA_MOPEN7:
3230 case NFA_MOPEN8:
3231 case NFA_MOPEN9:
3232#ifdef FEAT_SYN_HL
3233 case NFA_ZOPEN:
3234 case NFA_ZOPEN1:
3235 case NFA_ZOPEN2:
3236 case NFA_ZOPEN3:
3237 case NFA_ZOPEN4:
3238 case NFA_ZOPEN5:
3239 case NFA_ZOPEN6:
3240 case NFA_ZOPEN7:
3241 case NFA_ZOPEN8:
3242 case NFA_ZOPEN9:
3243#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003246 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003248 sub = &subs->norm;
3249 }
3250#ifdef FEAT_SYN_HL
3251 else if (state->c >= NFA_ZOPEN)
3252 {
3253 subidx = state->c - NFA_ZOPEN;
3254 sub = &subs->synt;
3255 }
3256#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003257 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003258 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003259 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003260 sub = &subs->norm;
3261 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003263 /* Set the position (with "off") in the subexpression. Save and
3264 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003265 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 if (REG_MULTI)
3267 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003268 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003269 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003270 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003271 save_in_use = -1;
3272 }
3273 else
3274 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003275 save_in_use = sub->in_use;
3276 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003277 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003278 sub->list.multi[i].start.lnum = -1;
3279 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003280 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003281 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003282 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003283 if (off == -1)
3284 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003285 sub->list.multi[subidx].start.lnum = reglnum + 1;
3286 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003287 }
3288 else
3289 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003290 sub->list.multi[subidx].start.lnum = reglnum;
3291 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003292 (colnr_T)(reginput - regline + off);
3293 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 }
3295 else
3296 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003297 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003298 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003299 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003300 save_in_use = -1;
3301 }
3302 else
3303 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003304 save_in_use = sub->in_use;
3305 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003306 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003307 sub->list.line[i].start = NULL;
3308 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003309 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003310 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003311 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003312 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 }
3314
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003315 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003317 if (save_in_use == -1)
3318 {
3319 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003320 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003321 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003322 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003323 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003324 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003325 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003326 break;
3327
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003328 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003329 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003331 /* Do not overwrite the position set by \ze. If no \ze
3332 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003333 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 break;
3335 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003336 case NFA_MCLOSE1:
3337 case NFA_MCLOSE2:
3338 case NFA_MCLOSE3:
3339 case NFA_MCLOSE4:
3340 case NFA_MCLOSE5:
3341 case NFA_MCLOSE6:
3342 case NFA_MCLOSE7:
3343 case NFA_MCLOSE8:
3344 case NFA_MCLOSE9:
3345#ifdef FEAT_SYN_HL
3346 case NFA_ZCLOSE:
3347 case NFA_ZCLOSE1:
3348 case NFA_ZCLOSE2:
3349 case NFA_ZCLOSE3:
3350 case NFA_ZCLOSE4:
3351 case NFA_ZCLOSE5:
3352 case NFA_ZCLOSE6:
3353 case NFA_ZCLOSE7:
3354 case NFA_ZCLOSE8:
3355 case NFA_ZCLOSE9:
3356#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003359 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003361 sub = &subs->norm;
3362 }
3363#ifdef FEAT_SYN_HL
3364 else if (state->c >= NFA_ZCLOSE)
3365 {
3366 subidx = state->c - NFA_ZCLOSE;
3367 sub = &subs->synt;
3368 }
3369#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003370 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003371 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003372 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003373 sub = &subs->norm;
3374 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003376 /* We don't fill in gaps here, there must have been an MOPEN that
3377 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003378 save_in_use = sub->in_use;
3379 if (sub->in_use <= subidx)
3380 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003381 if (REG_MULTI)
3382 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003383 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003384 if (off == -1)
3385 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003386 sub->list.multi[subidx].end.lnum = reglnum + 1;
3387 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003388 }
3389 else
3390 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003391 sub->list.multi[subidx].end.lnum = reglnum;
3392 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003393 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003394 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003395 }
3396 else
3397 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003398 save_ptr = sub->list.line[subidx].end;
3399 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 }
3401
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003402 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003403
3404 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003405 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003407 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003408 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409 break;
3410 }
3411}
3412
3413/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003414 * Like addstate(), but the new state(s) are put at position "*ip".
3415 * Used for zero-width matches, next state to use is the added one.
3416 * This makes sure the order of states to be tried does not change, which
3417 * matters for alternatives.
3418 */
3419 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003420addstate_here(l, state, subs, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003421 nfa_list_T *l; /* runtime state list */
3422 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003423 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003424 int *ip;
3425{
3426 int tlen = l->n;
3427 int count;
3428 int i = *ip;
3429
3430 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003431 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003432
3433 /* when "*ip" was at the end of the list, nothing to do */
3434 if (i + 1 == tlen)
3435 return;
3436
3437 /* re-order to put the new state at the current position */
3438 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003439 if (count == 1)
3440 {
3441 /* overwrite the current state */
3442 l->t[i] = l->t[l->n - 1];
3443 }
3444 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003445 {
3446 /* make space for new states, then move them from the
3447 * end to the current position */
3448 mch_memmove(&(l->t[i + count]),
3449 &(l->t[i + 1]),
3450 sizeof(nfa_thread_T) * (l->n - i - 1));
3451 mch_memmove(&(l->t[i]),
3452 &(l->t[l->n - 1]),
3453 sizeof(nfa_thread_T) * count);
3454 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003455 --l->n;
3456 *ip = i - 1;
3457}
3458
3459/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 * Check character class "class" against current character c.
3461 */
3462 static int
3463check_char_class(class, c)
3464 int class;
3465 int c;
3466{
3467 switch (class)
3468 {
3469 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003470 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471 return OK;
3472 break;
3473 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003474 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003475 return OK;
3476 break;
3477 case NFA_CLASS_BLANK:
3478 if (c == ' ' || c == '\t')
3479 return OK;
3480 break;
3481 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003482 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003483 return OK;
3484 break;
3485 case NFA_CLASS_DIGIT:
3486 if (VIM_ISDIGIT(c))
3487 return OK;
3488 break;
3489 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003490 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003491 return OK;
3492 break;
3493 case NFA_CLASS_LOWER:
3494 if (MB_ISLOWER(c))
3495 return OK;
3496 break;
3497 case NFA_CLASS_PRINT:
3498 if (vim_isprintc(c))
3499 return OK;
3500 break;
3501 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003502 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503 return OK;
3504 break;
3505 case NFA_CLASS_SPACE:
3506 if ((c >=9 && c <= 13) || (c == ' '))
3507 return OK;
3508 break;
3509 case NFA_CLASS_UPPER:
3510 if (MB_ISUPPER(c))
3511 return OK;
3512 break;
3513 case NFA_CLASS_XDIGIT:
3514 if (vim_isxdigit(c))
3515 return OK;
3516 break;
3517 case NFA_CLASS_TAB:
3518 if (c == '\t')
3519 return OK;
3520 break;
3521 case NFA_CLASS_RETURN:
3522 if (c == '\r')
3523 return OK;
3524 break;
3525 case NFA_CLASS_BACKSPACE:
3526 if (c == '\b')
3527 return OK;
3528 break;
3529 case NFA_CLASS_ESCAPE:
3530 if (c == '\033')
3531 return OK;
3532 break;
3533
3534 default:
3535 /* should not be here :P */
3536 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3537 }
3538 return FAIL;
3539}
3540
Bram Moolenaar5714b802013-05-28 22:03:20 +02003541static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3542
3543/*
3544 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003545 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003546 */
3547 static int
3548match_backref(sub, subidx, bytelen)
3549 regsub_T *sub; /* pointers to subexpressions */
3550 int subidx;
3551 int *bytelen; /* out: length of match in bytes */
3552{
3553 int len;
3554
3555 if (sub->in_use <= subidx)
3556 {
3557retempty:
3558 /* backref was not set, match an empty string */
3559 *bytelen = 0;
3560 return TRUE;
3561 }
3562
3563 if (REG_MULTI)
3564 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003565 if (sub->list.multi[subidx].start.lnum < 0
3566 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003567 goto retempty;
3568 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003569 len = sub->list.multi[subidx].end.col
3570 - sub->list.multi[subidx].start.col;
3571 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003572 reginput, &len) == 0)
3573 {
3574 *bytelen = len;
3575 return TRUE;
3576 }
3577 }
3578 else
3579 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003580 if (sub->list.line[subidx].start == NULL
3581 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003582 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003583 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3584 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003585 {
3586 *bytelen = len;
3587 return TRUE;
3588 }
3589 }
3590 return FALSE;
3591}
3592
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003593#ifdef FEAT_SYN_HL
3594
3595static int match_zref __ARGS((int subidx, int *bytelen));
3596
3597/*
3598 * Check for a match with \z subexpression "subidx".
3599 * Return TRUE if it matches.
3600 */
3601 static int
3602match_zref(subidx, bytelen)
3603 int subidx;
3604 int *bytelen; /* out: length of match in bytes */
3605{
3606 int len;
3607
3608 cleanup_zsubexpr();
3609 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3610 {
3611 /* backref was not set, match an empty string */
3612 *bytelen = 0;
3613 return TRUE;
3614 }
3615
3616 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3617 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3618 {
3619 *bytelen = len;
3620 return TRUE;
3621 }
3622 return FALSE;
3623}
3624#endif
3625
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003627 * Save list IDs for all NFA states of "prog" into "list".
3628 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003629 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 */
3631 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003632nfa_save_listids(prog, list)
3633 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634 int *list;
3635{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003636 int i;
3637 nfa_state_T *p;
3638
3639 /* Order in the list is reverse, it's a bit faster that way. */
3640 p = &prog->state[0];
3641 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003643 list[i] = p->lastlist[1];
3644 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003645 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003646 }
3647}
3648
3649/*
3650 * Restore list IDs from "list" to all NFA states.
3651 */
3652 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003653nfa_restore_listids(prog, list)
3654 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 int *list;
3656{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003657 int i;
3658 nfa_state_T *p;
3659
3660 p = &prog->state[0];
3661 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003662 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003663 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003664 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665 }
3666}
3667
Bram Moolenaar423532e2013-05-29 21:14:42 +02003668 static int
3669nfa_re_num_cmp(val, op, pos)
3670 long_u val;
3671 int op;
3672 long_u pos;
3673{
3674 if (op == 1) return pos > val;
3675 if (op == 2) return pos < val;
3676 return val == pos;
3677}
3678
Bram Moolenaarf46da702013-06-02 22:37:42 +02003679static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003680static int nfa_regmatch __ARGS((nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m));
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003681
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003682/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003683 * Recursively call nfa_regmatch()
3684 */
3685 static int
3686recursive_regmatch(state, prog, submatch, m, listids)
3687 nfa_state_T *state;
3688 nfa_regprog_T *prog;
3689 regsubs_T *submatch;
3690 regsubs_T *m;
3691 int **listids;
3692{
3693 char_u *save_reginput = reginput;
3694 char_u *save_regline = regline;
3695 int save_reglnum = reglnum;
3696 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003697 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003698 save_se_T *save_nfa_endp = nfa_endp;
3699 save_se_T endpos;
3700 save_se_T *endposp = NULL;
3701 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003702 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003703
3704 if (state->c == NFA_START_INVISIBLE_BEFORE)
3705 {
3706 /* The recursive match must end at the current position. */
3707 endposp = &endpos;
3708 if (REG_MULTI)
3709 {
3710 endpos.se_u.pos.col = (int)(reginput - regline);
3711 endpos.se_u.pos.lnum = reglnum;
3712 }
3713 else
3714 endpos.se_u.ptr = reginput;
3715
3716 /* Go back the specified number of bytes, or as far as the
3717 * start of the previous line, to try matching "\@<=" or
3718 * not matching "\@<!".
3719 * TODO: This is very inefficient! Would be better to
3720 * first check for a match with what follows. */
3721 if (state->val <= 0)
3722 {
3723 if (REG_MULTI)
3724 {
3725 regline = reg_getline(--reglnum);
3726 if (regline == NULL)
3727 /* can't go before the first line */
3728 regline = reg_getline(++reglnum);
3729 }
3730 reginput = regline;
3731 }
3732 else
3733 {
3734 if (REG_MULTI && (int)(reginput - regline) < state->val)
3735 {
3736 /* Not enough bytes in this line, go to end of
3737 * previous line. */
3738 regline = reg_getline(--reglnum);
3739 if (regline == NULL)
3740 {
3741 /* can't go before the first line */
3742 regline = reg_getline(++reglnum);
3743 reginput = regline;
3744 }
3745 else
3746 reginput = regline + STRLEN(regline);
3747 }
3748 if ((int)(reginput - regline) >= state->val)
3749 {
3750 reginput -= state->val;
3751#ifdef FEAT_MBYTE
3752 if (has_mbyte)
3753 reginput -= mb_head_off(regline, reginput);
3754#endif
3755 }
3756 else
3757 reginput = regline;
3758 }
3759 }
3760
Bram Moolenaarf46da702013-06-02 22:37:42 +02003761#ifdef ENABLE_LOG
3762 if (log_fd != stderr)
3763 fclose(log_fd);
3764 log_fd = NULL;
3765#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003766 /* Have to clear the lastlist field of the NFA nodes, so that
3767 * nfa_regmatch() and addstate() can run properly after recursion. */
3768 if (nfa_ll_index == 1)
3769 {
3770 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3771 * values and clear them. */
3772 if (*listids == NULL)
3773 {
3774 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3775 if (*listids == NULL)
3776 {
3777 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3778 return 0;
3779 }
3780 }
3781 nfa_save_listids(prog, *listids);
3782 need_restore = TRUE;
3783 /* any value of nfa_listid will do */
3784 }
3785 else
3786 {
3787 /* First recursive nfa_regmatch() call, switch to the second lastlist
3788 * entry. Make sure nfa_listid is different from a previous recursive
3789 * call, because some states may still have this ID. */
3790 ++nfa_ll_index;
3791 if (nfa_listid <= nfa_alt_listid)
3792 nfa_listid = nfa_alt_listid;
3793 }
3794
3795 /* Call nfa_regmatch() to check if the current concat matches at this
3796 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003797 nfa_endp = endposp;
3798 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003799
3800 if (need_restore)
3801 nfa_restore_listids(prog, *listids);
3802 else
3803 {
3804 --nfa_ll_index;
3805 nfa_alt_listid = nfa_listid;
3806 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003807
3808 /* restore position in input text */
3809 reginput = save_reginput;
3810 regline = save_regline;
3811 reglnum = save_reglnum;
3812 nfa_match = save_nfa_match;
3813 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003814 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003815
3816#ifdef ENABLE_LOG
3817 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3818 if (log_fd != NULL)
3819 {
3820 fprintf(log_fd, "****************************\n");
3821 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3822 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3823 fprintf(log_fd, "****************************\n");
3824 }
3825 else
3826 {
3827 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3828 log_fd = stderr;
3829 }
3830#endif
3831
3832 return result;
3833}
3834
3835/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836 * Main matching routine.
3837 *
3838 * Run NFA to determine whether it matches reginput.
3839 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003840 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003841 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003842 * Return TRUE if there is a match, FALSE otherwise.
3843 * Note: Caller must ensure that: start != NULL.
3844 */
3845 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003846nfa_regmatch(prog, start, submatch, m)
3847 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003848 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003849 regsubs_T *submatch;
3850 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003852 int result;
3853 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003855 int go_to_nextline = FALSE;
3856 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003857 nfa_list_T list[3];
3858 nfa_list_T *listtbl[2][2];
3859 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003860 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003861 nfa_list_T *thislist;
3862 nfa_list_T *nextlist;
3863 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003865#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003866 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003867
3868 if (debug == NULL)
3869 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003870 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003871 return FALSE;
3872 }
3873#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003874 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003876 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003877 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003878 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3879 list[0].len = nstate + 1;
3880 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3881 list[1].len = nstate + 1;
3882 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3883 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003884 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3885 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003886
3887#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003888 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003889 if (log_fd != NULL)
3890 {
3891 fprintf(log_fd, "**********************************\n");
3892 nfa_set_code(start->c);
3893 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3894 abs(start->id), code);
3895 fprintf(log_fd, "**********************************\n");
3896 }
3897 else
3898 {
3899 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3900 log_fd = stderr;
3901 }
3902#endif
3903
3904 thislist = &list[0];
3905 thislist->n = 0;
3906 nextlist = &list[1];
3907 nextlist->n = 0;
3908 neglist = &list[2];
3909 neglist->n = 0;
3910#ifdef ENABLE_LOG
3911 fprintf(log_fd, "(---) STARTSTATE\n");
3912#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003913 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003914 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915
3916 /* There are two cases when the NFA advances: 1. input char matches the
3917 * NFA node and 2. input char does not match the NFA node, but the next
3918 * node is NFA_NOT. The following macro calls addstate() according to
3919 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3920 listtbl[0][0] = NULL;
3921 listtbl[0][1] = neglist;
3922 listtbl[1][0] = nextlist;
3923 listtbl[1][1] = NULL;
3924#define ADD_POS_NEG_STATE(node) \
3925 ll = listtbl[result ? 1 : 0][node->negated]; \
3926 if (ll != NULL) \
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003927 addstate(ll, node->out , &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003928
3929
3930 /*
3931 * Run for each character.
3932 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003933 for (;;)
3934 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003935 int curc;
3936 int clen;
3937
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003938#ifdef FEAT_MBYTE
3939 if (has_mbyte)
3940 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003941 curc = (*mb_ptr2char)(reginput);
3942 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003943 }
3944 else
3945#endif
3946 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003947 curc = *reginput;
3948 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003949 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003950 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003951 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003952 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003953 go_to_nextline = FALSE;
3954 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003955
3956 /* swap lists */
3957 thislist = &list[flag];
3958 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003959 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003960 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003961 ++nfa_listid;
3962 thislist->id = nfa_listid;
3963 nextlist->id = nfa_listid + 1;
3964 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965
3966#ifdef ENABLE_LOG
3967 fprintf(log_fd, "------------------------------------------\n");
3968 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003969 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003970 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003971 {
3972 int i;
3973
3974 for (i = 0; i < thislist->n; i++)
3975 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3976 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003977 fprintf(log_fd, "\n");
3978#endif
3979
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003980#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003981 fprintf(debug, "\n-------------------\n");
3982#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003983 /*
3984 * If the state lists are empty we can stop.
3985 */
3986 if (thislist->n == 0 && neglist->n == 0)
3987 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003988
3989 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003990 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003991 {
3992 if (neglist->n > 0)
3993 {
3994 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003995 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003996 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997 }
3998 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003999 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004000
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004001#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002 nfa_set_code(t->state->c);
4003 fprintf(debug, "%s, ", code);
4004#endif
4005#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004006 {
4007 int col;
4008
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004009 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004010 col = -1;
4011 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004012 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004013 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004014 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004015 nfa_set_code(t->state->c);
4016 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4017 abs(t->state->id), (int)t->state->c, code, col);
4018 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004019#endif
4020
4021 /*
4022 * Handle the possible codes of the current state.
4023 * The most important is NFA_MATCH.
4024 */
4025 switch (t->state->c)
4026 {
4027 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004028 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004029 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004030 copy_sub(&submatch->norm, &t->subs.norm);
4031#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004032 if (nfa_has_zsubexpr)
4033 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004034#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004035#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004036 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004038 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004039 * states at this position. When the list of states is going
4040 * to be empty quit without advancing, so that "reginput" is
4041 * correct. */
4042 if (nextlist->n == 0 && neglist->n == 0)
4043 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004044 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004045 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046
4047 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004048 /*
4049 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004050 * NFA_START_INVISIBLE_BEFORE node.
4051 * They surround a zero-width group, used with "\@=", "\&",
4052 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004053 * If we got here, it means that the current "invisible" group
4054 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004055 * nfa_regmatch(). For a look-behind match only when it ends
4056 * in the position in "nfa_endp".
4057 * Submatches are stored in *m, and used in the parent call.
4058 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004059#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004060 if (nfa_endp != NULL)
4061 {
4062 if (REG_MULTI)
4063 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4064 (int)reglnum,
4065 (int)nfa_endp->se_u.pos.lnum,
4066 (int)(reginput - regline),
4067 nfa_endp->se_u.pos.col);
4068 else
4069 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4070 (int)(reginput - regline),
4071 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004073#endif
4074 /* It's only a match if it ends at "nfa_endp" */
4075 if (nfa_endp != NULL && (REG_MULTI
4076 ? (reglnum != nfa_endp->se_u.pos.lnum
4077 || (int)(reginput - regline)
4078 != nfa_endp->se_u.pos.col)
4079 : reginput != nfa_endp->se_u.ptr))
4080 break;
4081
4082 /* do not set submatches for \@! */
4083 if (!t->state->negated)
4084 {
4085 copy_sub(&m->norm, &t->subs.norm);
4086#ifdef FEAT_SYN_HL
4087 if (nfa_has_zsubexpr)
4088 copy_sub(&m->synt, &t->subs.synt);
4089#endif
4090 }
4091 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004092 break;
4093
4094 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004095 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004096 result = recursive_regmatch(t->state, prog, submatch, m,
4097 &listids);
Bram Moolenaar61602c52013-06-01 19:54:43 +02004098
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02004099 /* for \@! it is a match when result is FALSE */
4100 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004102 /* Copy submatch info from the recursive call */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004103 copy_sub_off(&t->subs.norm, &m->norm);
4104#ifdef FEAT_SYN_HL
4105 copy_sub_off(&t->subs.synt, &m->synt);
4106#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004107
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004108 /* t->state->out1 is the corresponding END_INVISIBLE node;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004109 * Add its out to the current list (zero-width match). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004110 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004111 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004112 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 break;
4114
4115 case NFA_BOL:
4116 if (reginput == regline)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004117 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004118 break;
4119
4120 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004121 if (curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004122 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004123 break;
4124
4125 case NFA_BOW:
4126 {
4127 int bow = TRUE;
4128
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004129 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004130 bow = FALSE;
4131#ifdef FEAT_MBYTE
4132 else if (has_mbyte)
4133 {
4134 int this_class;
4135
4136 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004137 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004138 if (this_class <= 1)
4139 bow = FALSE;
4140 else if (reg_prev_class() == this_class)
4141 bow = FALSE;
4142 }
4143#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004144 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004145 || (reginput > regline
4146 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004147 bow = FALSE;
4148 if (bow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004149 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004150 break;
4151 }
4152
4153 case NFA_EOW:
4154 {
4155 int eow = TRUE;
4156
4157 if (reginput == regline)
4158 eow = FALSE;
4159#ifdef FEAT_MBYTE
4160 else if (has_mbyte)
4161 {
4162 int this_class, prev_class;
4163
4164 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004165 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004166 prev_class = reg_prev_class();
4167 if (this_class == prev_class
4168 || prev_class == 0 || prev_class == 1)
4169 eow = FALSE;
4170 }
4171#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004172 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004173 || (reginput[0] != NUL
4174 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004175 eow = FALSE;
4176 if (eow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004177 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004178 break;
4179 }
4180
Bram Moolenaar4b780632013-05-31 22:14:52 +02004181 case NFA_BOF:
4182 if (reglnum == 0 && reginput == regline
4183 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004184 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004185 break;
4186
4187 case NFA_EOF:
4188 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004189 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004190 break;
4191
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004192#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004193 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004194 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004195 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004196 int len = 0;
4197 nfa_state_T *end;
4198 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004199 int cchars[MAX_MCO];
4200 int ccount = 0;
4201 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004202
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004203 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004204 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004205 if (utf_iscomposing(sta->c))
4206 {
4207 /* Only match composing character(s), ignore base
4208 * character. Used for ".{composing}" and "{composing}"
4209 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004210 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004211 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004212 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004213 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004214 /* If \Z was present, then ignore composing characters.
4215 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004216 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004217 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004218 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004219 else
4220 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004221 while (sta->c != NFA_END_COMPOSING)
4222 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004223 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004224
4225 /* Check base character matches first, unless ignored. */
4226 else if (len > 0 || mc == sta->c)
4227 {
4228 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004229 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004230 len += mb_char2len(mc);
4231 sta = sta->out;
4232 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004234 /* We don't care about the order of composing characters.
4235 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004236 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004237 {
4238 mc = mb_ptr2char(reginput + len);
4239 cchars[ccount++] = mc;
4240 len += mb_char2len(mc);
4241 if (ccount == MAX_MCO)
4242 break;
4243 }
4244
4245 /* Check that each composing char in the pattern matches a
4246 * composing char in the text. We do not check if all
4247 * composing chars are matched. */
4248 result = OK;
4249 while (sta->c != NFA_END_COMPOSING)
4250 {
4251 for (j = 0; j < ccount; ++j)
4252 if (cchars[j] == sta->c)
4253 break;
4254 if (j == ccount)
4255 {
4256 result = FAIL;
4257 break;
4258 }
4259 sta = sta->out;
4260 }
4261 }
4262 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004263 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004264
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004265 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004266 ADD_POS_NEG_STATE(end);
4267 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004268 }
4269#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004270
4271 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004272 if (curc == NUL && !reg_line_lbr && REG_MULTI
4273 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004275 go_to_nextline = TRUE;
4276 /* Pass -1 for the offset, which means taking the position
4277 * at the start of the next line. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004278 addstate(nextlist, t->state->out, &t->subs, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004279 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004280 else if (curc == '\n' && reg_line_lbr)
4281 {
4282 /* match \n as if it is an ordinary character */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004283 addstate(nextlist, t->state->out, &t->subs, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004284 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004285 break;
4286
4287 case NFA_CLASS_ALNUM:
4288 case NFA_CLASS_ALPHA:
4289 case NFA_CLASS_BLANK:
4290 case NFA_CLASS_CNTRL:
4291 case NFA_CLASS_DIGIT:
4292 case NFA_CLASS_GRAPH:
4293 case NFA_CLASS_LOWER:
4294 case NFA_CLASS_PRINT:
4295 case NFA_CLASS_PUNCT:
4296 case NFA_CLASS_SPACE:
4297 case NFA_CLASS_UPPER:
4298 case NFA_CLASS_XDIGIT:
4299 case NFA_CLASS_TAB:
4300 case NFA_CLASS_RETURN:
4301 case NFA_CLASS_BACKSPACE:
4302 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004303 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304 ADD_POS_NEG_STATE(t->state);
4305 break;
4306
4307 case NFA_END_NEG_RANGE:
4308 /* This follows a series of negated nodes, like:
4309 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004310 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004311 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004312 break;
4313
4314 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004315 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004316 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004317 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004318 break;
4319
4320 /*
4321 * Character classes like \a for alpha, \d for digit etc.
4322 */
4323 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004324 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004325 ADD_POS_NEG_STATE(t->state);
4326 break;
4327
4328 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004329 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 ADD_POS_NEG_STATE(t->state);
4331 break;
4332
4333 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004334 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335 ADD_POS_NEG_STATE(t->state);
4336 break;
4337
4338 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004339 result = !VIM_ISDIGIT(curc)
4340 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341 ADD_POS_NEG_STATE(t->state);
4342 break;
4343
4344 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004345 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004346 ADD_POS_NEG_STATE(t->state);
4347 break;
4348
4349 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004350 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004351 ADD_POS_NEG_STATE(t->state);
4352 break;
4353
4354 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004355 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356 ADD_POS_NEG_STATE(t->state);
4357 break;
4358
4359 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004360 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361 ADD_POS_NEG_STATE(t->state);
4362 break;
4363
4364 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004365 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004366 ADD_POS_NEG_STATE(t->state);
4367 break;
4368
4369 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004370 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004371 ADD_POS_NEG_STATE(t->state);
4372 break;
4373
4374 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004375 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004376 ADD_POS_NEG_STATE(t->state);
4377 break;
4378
4379 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004380 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381 ADD_POS_NEG_STATE(t->state);
4382 break;
4383
4384 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004385 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004386 ADD_POS_NEG_STATE(t->state);
4387 break;
4388
4389 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004390 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004391 ADD_POS_NEG_STATE(t->state);
4392 break;
4393
4394 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004395 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396 ADD_POS_NEG_STATE(t->state);
4397 break;
4398
4399 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004400 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004401 ADD_POS_NEG_STATE(t->state);
4402 break;
4403
4404 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004405 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004406 ADD_POS_NEG_STATE(t->state);
4407 break;
4408
4409 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004410 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004411 ADD_POS_NEG_STATE(t->state);
4412 break;
4413
4414 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004415 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004416 ADD_POS_NEG_STATE(t->state);
4417 break;
4418
4419 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004420 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004421 ADD_POS_NEG_STATE(t->state);
4422 break;
4423
4424 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004425 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004426 ADD_POS_NEG_STATE(t->state);
4427 break;
4428
4429 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004430 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004431 ADD_POS_NEG_STATE(t->state);
4432 break;
4433
4434 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004435 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004436 ADD_POS_NEG_STATE(t->state);
4437 break;
4438
4439 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004440 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004441 ADD_POS_NEG_STATE(t->state);
4442 break;
4443
4444 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004445 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004446 ADD_POS_NEG_STATE(t->state);
4447 break;
4448
4449 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004450 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004451 ADD_POS_NEG_STATE(t->state);
4452 break;
4453
Bram Moolenaar5714b802013-05-28 22:03:20 +02004454 case NFA_BACKREF1:
4455 case NFA_BACKREF2:
4456 case NFA_BACKREF3:
4457 case NFA_BACKREF4:
4458 case NFA_BACKREF5:
4459 case NFA_BACKREF6:
4460 case NFA_BACKREF7:
4461 case NFA_BACKREF8:
4462 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004463#ifdef FEAT_SYN_HL
4464 case NFA_ZREF1:
4465 case NFA_ZREF2:
4466 case NFA_ZREF3:
4467 case NFA_ZREF4:
4468 case NFA_ZREF5:
4469 case NFA_ZREF6:
4470 case NFA_ZREF7:
4471 case NFA_ZREF8:
4472 case NFA_ZREF9:
4473#endif
4474 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004475 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004476 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004477 int bytelen;
4478
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004479 if (t->state->c <= NFA_BACKREF9)
4480 {
4481 subidx = t->state->c - NFA_BACKREF1 + 1;
4482 result = match_backref(&t->subs.norm, subidx, &bytelen);
4483 }
4484#ifdef FEAT_SYN_HL
4485 else
4486 {
4487 subidx = t->state->c - NFA_ZREF1 + 1;
4488 result = match_zref(subidx, &bytelen);
4489 }
4490#endif
4491
Bram Moolenaar5714b802013-05-28 22:03:20 +02004492 if (result)
4493 {
4494 if (bytelen == 0)
4495 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004496 /* empty match always works, output of NFA_SKIP to be
4497 * used next */
4498 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004499 &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004500 }
4501 else if (bytelen <= clen)
4502 {
4503 /* match current character, jump ahead to out of
4504 * NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004505 addstate(nextlist, t->state->out->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004506#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004507 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004508#endif
4509 }
4510 else
4511 {
4512 /* skip ofer the matched characters, set character
4513 * count in NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004514 addstate(nextlist, t->state->out, &t->subs, bytelen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004515 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4516#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004517 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004518#endif
4519 }
4520
4521 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004522 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004523 }
4524 case NFA_SKIP:
4525 /* charater of previous matching \1 .. \9 */
4526 if (t->count - clen <= 0)
4527 {
4528 /* end of match, go to what follows */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004529 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004530#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004531 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004532#endif
4533 }
4534 else
4535 {
4536 /* add state again with decremented count */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004537 addstate(nextlist, t->state, &t->subs, 0);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004538 nextlist->t[nextlist->n - 1].count = t->count - clen;
4539#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004540 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004541#endif
4542 }
4543 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004544
4545 case NFA_SKIP_CHAR:
4546 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004547 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004548 /* TODO: should not happen? */
4549 break;
4550
Bram Moolenaar423532e2013-05-29 21:14:42 +02004551 case NFA_LNUM:
4552 case NFA_LNUM_GT:
4553 case NFA_LNUM_LT:
4554 result = (REG_MULTI &&
4555 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4556 (long_u)(reglnum + reg_firstlnum)));
4557 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004558 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004559 break;
4560
4561 case NFA_COL:
4562 case NFA_COL_GT:
4563 case NFA_COL_LT:
4564 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4565 (long_u)(reginput - regline) + 1);
4566 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004567 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004568 break;
4569
4570 case NFA_VCOL:
4571 case NFA_VCOL_GT:
4572 case NFA_VCOL_LT:
4573 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4574 (long_u)win_linetabsize(
4575 reg_win == NULL ? curwin : reg_win,
4576 regline, (colnr_T)(reginput - regline)) + 1);
4577 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004578 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004579 break;
4580
4581 case NFA_CURSOR:
4582 result = (reg_win != NULL
4583 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4584 && ((colnr_T)(reginput - regline)
4585 == reg_win->w_cursor.col));
4586 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004587 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004588 break;
4589
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004591 {
4592 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004593
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004594 /* TODO: put this in #ifdef later */
4595 if (c < -256)
4596 EMSGN("INTERNAL: Negative state char: %ld", c);
4597 if (is_Magic(c))
4598 c = un_Magic(c);
4599 result = (c == curc);
4600
4601 if (!result && ireg_ic)
4602 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004603#ifdef FEAT_MBYTE
4604 /* If there is a composing character which is not being
4605 * ignored there can be no match. Match with composing
4606 * character uses NFA_COMPOSING above. */
4607 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004608 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004609 result = FALSE;
4610#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611 ADD_POS_NEG_STATE(t->state);
4612 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004613 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004614 }
4615
4616 } /* for (thislist = thislist; thislist->state; thislist++) */
4617
Bram Moolenaare23febd2013-05-26 18:40:14 +02004618 /* Look for the start of a match in the current position by adding the
4619 * start state to the list of states.
4620 * The first found match is the leftmost one, thus the order of states
4621 * matters!
4622 * Do not add the start state in recursive calls of nfa_regmatch(),
4623 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004624 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004625 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004626 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004627 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004628 && reglnum == 0
4629 && clen != 0
4630 && (ireg_maxcol == 0
4631 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004632 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004633 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004634 ? (reglnum < nfa_endp->se_u.pos.lnum
4635 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004636 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004637 < nfa_endp->se_u.pos.col))
4638 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639 {
4640#ifdef ENABLE_LOG
4641 fprintf(log_fd, "(---) STARTSTATE\n");
4642#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004643 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004644 }
4645
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646#ifdef ENABLE_LOG
4647 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004648 {
4649 int i;
4650
4651 for (i = 0; i < thislist->n; i++)
4652 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4653 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004654 fprintf(log_fd, "\n");
4655#endif
4656
4657nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004658 /* Advance to the next character, or advance to the next line, or
4659 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004660 if (clen != 0)
4661 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004662 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4663 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004664 reg_nextline();
4665 else
4666 break;
4667 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004668
4669#ifdef ENABLE_LOG
4670 if (log_fd != stderr)
4671 fclose(log_fd);
4672 log_fd = NULL;
4673#endif
4674
4675theend:
4676 /* Free memory */
4677 vim_free(list[0].t);
4678 vim_free(list[1].t);
4679 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004680 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004681#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004682#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 fclose(debug);
4684#endif
4685
Bram Moolenaar963fee22013-05-26 21:47:28 +02004686 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004687}
4688
4689/*
4690 * Try match of "prog" with at regline["col"].
4691 * Returns 0 for failure, number of lines contained in the match otherwise.
4692 */
4693 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004694nfa_regtry(prog, col)
4695 nfa_regprog_T *prog;
4696 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004697{
4698 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004699 regsubs_T subs, m;
4700 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701#ifdef ENABLE_LOG
4702 FILE *f;
4703#endif
4704
4705 reginput = regline + col;
4706 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004707#ifdef FEAT_SYN_HL
4708 /* Clear the external match subpointers if necessary. */
4709 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004710 {
4711 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004712 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004713 }
4714 else
4715 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004716#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004717
4718#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004719 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004720 if (f != NULL)
4721 {
4722 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4723 fprintf(f, " =======================================================\n");
4724#ifdef DEBUG
4725 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4726#endif
4727 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004728 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004729 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 fprintf(f, "\n\n");
4731 fclose(f);
4732 }
4733 else
4734 EMSG(_("Could not open temporary log file for writing "));
4735#endif
4736
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004737 clear_sub(&subs.norm);
4738 clear_sub(&m.norm);
4739#ifdef FEAT_SYN_HL
4740 clear_sub(&subs.synt);
4741 clear_sub(&m.synt);
4742#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004744 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004745 return 0;
4746
4747 cleanup_subexpr();
4748 if (REG_MULTI)
4749 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004750 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004751 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004752 reg_startpos[i] = subs.norm.list.multi[i].start;
4753 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004754 }
4755
4756 if (reg_startpos[0].lnum < 0)
4757 {
4758 reg_startpos[0].lnum = 0;
4759 reg_startpos[0].col = col;
4760 }
4761 if (reg_endpos[0].lnum < 0)
4762 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004763 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004764 reg_endpos[0].lnum = reglnum;
4765 reg_endpos[0].col = (int)(reginput - regline);
4766 }
4767 else
4768 /* Use line number of "\ze". */
4769 reglnum = reg_endpos[0].lnum;
4770 }
4771 else
4772 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004773 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004774 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004775 reg_startp[i] = subs.norm.list.line[i].start;
4776 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004777 }
4778
4779 if (reg_startp[0] == NULL)
4780 reg_startp[0] = regline + col;
4781 if (reg_endp[0] == NULL)
4782 reg_endp[0] = reginput;
4783 }
4784
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004785#ifdef FEAT_SYN_HL
4786 /* Package any found \z(...\) matches for export. Default is none. */
4787 unref_extmatch(re_extmatch_out);
4788 re_extmatch_out = NULL;
4789
4790 if (prog->reghasz == REX_SET)
4791 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004792 cleanup_zsubexpr();
4793 re_extmatch_out = make_extmatch();
4794 for (i = 0; i < subs.synt.in_use; i++)
4795 {
4796 if (REG_MULTI)
4797 {
4798 struct multipos *mpos = &subs.synt.list.multi[i];
4799
4800 /* Only accept single line matches. */
4801 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
4802 re_extmatch_out->matches[i] =
4803 vim_strnsave(reg_getline(mpos->start.lnum)
4804 + mpos->start.col,
4805 mpos->end.col - mpos->start.col);
4806 }
4807 else
4808 {
4809 struct linepos *lpos = &subs.synt.list.line[i];
4810
4811 if (lpos->start != NULL && lpos->end != NULL)
4812 re_extmatch_out->matches[i] =
4813 vim_strnsave(lpos->start,
4814 (int)(lpos->end - lpos->start));
4815 }
4816 }
4817 }
4818#endif
4819
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004820 return 1 + reglnum;
4821}
4822
4823/*
4824 * Match a regexp against a string ("line" points to the string) or multiple
4825 * lines ("line" is NULL, use reg_getline()).
4826 *
4827 * Returns 0 for failure, number of lines contained in the match otherwise.
4828 */
4829 static long
4830nfa_regexec_both(line, col)
4831 char_u *line;
4832 colnr_T col; /* column to start looking for match */
4833{
4834 nfa_regprog_T *prog;
4835 long retval = 0L;
4836 int i;
4837
4838 if (REG_MULTI)
4839 {
4840 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4841 line = reg_getline((linenr_T)0); /* relative to the cursor */
4842 reg_startpos = reg_mmatch->startpos;
4843 reg_endpos = reg_mmatch->endpos;
4844 }
4845 else
4846 {
4847 prog = (nfa_regprog_T *)reg_match->regprog;
4848 reg_startp = reg_match->startp;
4849 reg_endp = reg_match->endp;
4850 }
4851
4852 /* Be paranoid... */
4853 if (prog == NULL || line == NULL)
4854 {
4855 EMSG(_(e_null));
4856 goto theend;
4857 }
4858
4859 /* If the start column is past the maximum column: no need to try. */
4860 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4861 goto theend;
4862
4863 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4864 if (prog->regflags & RF_ICASE)
4865 ireg_ic = TRUE;
4866 else if (prog->regflags & RF_NOICASE)
4867 ireg_ic = FALSE;
4868
4869#ifdef FEAT_MBYTE
4870 /* If pattern contains "\Z" overrule value of ireg_icombine */
4871 if (prog->regflags & RF_ICOMBINE)
4872 ireg_icombine = TRUE;
4873#endif
4874
4875 regline = line;
4876 reglnum = 0; /* relative to line */
4877
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004878 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004879 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004880 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004881 nfa_listid = 1;
4882 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004883#ifdef DEBUG
4884 nfa_regengine.expr = prog->pattern;
4885#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004886
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004887 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004888 for (i = 0; i < nstate; ++i)
4889 {
4890 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004891 prog->state[i].lastlist[0] = 0;
4892 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004893 }
4894
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004895 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004896
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004897#ifdef DEBUG
4898 nfa_regengine.expr = NULL;
4899#endif
4900
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901theend:
4902 return retval;
4903}
4904
4905/*
4906 * Compile a regular expression into internal code for the NFA matcher.
4907 * Returns the program in allocated space. Returns NULL for an error.
4908 */
4909 static regprog_T *
4910nfa_regcomp(expr, re_flags)
4911 char_u *expr;
4912 int re_flags;
4913{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004914 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004915 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004916 int *postfix;
4917
4918 if (expr == NULL)
4919 return NULL;
4920
4921#ifdef DEBUG
4922 nfa_regengine.expr = expr;
4923#endif
4924
4925 init_class_tab();
4926
4927 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4928 return NULL;
4929
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004931 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004932 postfix = re2post();
4933 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004934 {
4935 /* TODO: only give this error for debugging? */
4936 if (post_ptr >= post_end)
4937 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004938 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004939 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940
4941 /*
4942 * In order to build the NFA, we parse the input regexp twice:
4943 * 1. first pass to count size (so we can allocate space)
4944 * 2. second to emit code
4945 */
4946#ifdef ENABLE_LOG
4947 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004948 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004949
4950 if (f != NULL)
4951 {
4952 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4953 fclose(f);
4954 }
4955 }
4956#endif
4957
4958 /*
4959 * PASS 1
4960 * Count number of NFA states in "nstate". Do not build the NFA.
4961 */
4962 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004963
4964 /* Space for compiled regexp */
4965 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4966 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4967 if (prog == NULL)
4968 goto fail;
4969 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004970 state_ptr = prog->state;
4971
4972 /*
4973 * PASS 2
4974 * Build the NFA
4975 */
4976 prog->start = post2nfa(postfix, post_ptr, FALSE);
4977 if (prog->start == NULL)
4978 goto fail;
4979
4980 prog->regflags = regflags;
4981 prog->engine = &nfa_regengine;
4982 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004983 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004984 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004985 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004986#ifdef ENABLE_LOG
4987 nfa_postfix_dump(expr, OK);
4988 nfa_dump(prog);
4989#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004990#ifdef FEAT_SYN_HL
4991 /* Remember whether this pattern has any \z specials in it. */
4992 prog->reghasz = re_has_z;
4993#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004994#ifdef DEBUG
4995 prog->pattern = vim_strsave(expr); /* memory will leak */
4996 nfa_regengine.expr = NULL;
4997#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998
4999out:
5000 vim_free(post_start);
5001 post_start = post_ptr = post_end = NULL;
5002 state_ptr = NULL;
5003 return (regprog_T *)prog;
5004
5005fail:
5006 vim_free(prog);
5007 prog = NULL;
5008#ifdef ENABLE_LOG
5009 nfa_postfix_dump(expr, FAIL);
5010#endif
5011#ifdef DEBUG
5012 nfa_regengine.expr = NULL;
5013#endif
5014 goto out;
5015}
5016
5017
5018/*
5019 * Match a regexp against a string.
5020 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5021 * Uses curbuf for line count and 'iskeyword'.
5022 *
5023 * Return TRUE if there is a match, FALSE if not.
5024 */
5025 static int
5026nfa_regexec(rmp, line, col)
5027 regmatch_T *rmp;
5028 char_u *line; /* string to match against */
5029 colnr_T col; /* column to start looking for match */
5030{
5031 reg_match = rmp;
5032 reg_mmatch = NULL;
5033 reg_maxline = 0;
5034 reg_line_lbr = FALSE;
5035 reg_buf = curbuf;
5036 reg_win = NULL;
5037 ireg_ic = rmp->rm_ic;
5038#ifdef FEAT_MBYTE
5039 ireg_icombine = FALSE;
5040#endif
5041 ireg_maxcol = 0;
5042 return (nfa_regexec_both(line, col) != 0);
5043}
5044
5045#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5046 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5047
5048static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5049
5050/*
5051 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5052 */
5053 static int
5054nfa_regexec_nl(rmp, line, col)
5055 regmatch_T *rmp;
5056 char_u *line; /* string to match against */
5057 colnr_T col; /* column to start looking for match */
5058{
5059 reg_match = rmp;
5060 reg_mmatch = NULL;
5061 reg_maxline = 0;
5062 reg_line_lbr = TRUE;
5063 reg_buf = curbuf;
5064 reg_win = NULL;
5065 ireg_ic = rmp->rm_ic;
5066#ifdef FEAT_MBYTE
5067 ireg_icombine = FALSE;
5068#endif
5069 ireg_maxcol = 0;
5070 return (nfa_regexec_both(line, col) != 0);
5071}
5072#endif
5073
5074
5075/*
5076 * Match a regexp against multiple lines.
5077 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5078 * Uses curbuf for line count and 'iskeyword'.
5079 *
5080 * Return zero if there is no match. Return number of lines contained in the
5081 * match otherwise.
5082 *
5083 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5084 *
5085 * ! Also NOTE : match may actually be in another line. e.g.:
5086 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5087 *
5088 * +-------------------------+
5089 * |a |
5090 * |b |
5091 * |c |
5092 * | |
5093 * +-------------------------+
5094 *
5095 * then nfa_regexec_multi() returns 3. while the original
5096 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5097 *
5098 * FIXME if this behavior is not compatible.
5099 */
5100 static long
5101nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5102 regmmatch_T *rmp;
5103 win_T *win; /* window in which to search or NULL */
5104 buf_T *buf; /* buffer in which to search */
5105 linenr_T lnum; /* nr of line to start looking for match */
5106 colnr_T col; /* column to start looking for match */
5107 proftime_T *tm UNUSED; /* timeout limit or NULL */
5108{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005109 reg_match = NULL;
5110 reg_mmatch = rmp;
5111 reg_buf = buf;
5112 reg_win = win;
5113 reg_firstlnum = lnum;
5114 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5115 reg_line_lbr = FALSE;
5116 ireg_ic = rmp->rmm_ic;
5117#ifdef FEAT_MBYTE
5118 ireg_icombine = FALSE;
5119#endif
5120 ireg_maxcol = rmp->rmm_maxcol;
5121
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005122 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005123}
5124
5125#ifdef DEBUG
5126# undef ENABLE_LOG
5127#endif