blob: 0e56809e0b4cc16bad7045d81ab76fa430f415d8 [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
258static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
259static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
260static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200261static int nfa_regatom __ARGS((void));
262static int nfa_regpiece __ARGS((void));
263static int nfa_regconcat __ARGS((void));
264static int nfa_regbranch __ARGS((void));
265static int nfa_reg __ARGS((int paren));
266#ifdef DEBUG
267static void nfa_set_code __ARGS((int c));
268static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200269static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
270static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271static void nfa_dump __ARGS((nfa_regprog_T *prog));
272#endif
273static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200274static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
276static int check_char_class __ARGS((int class, int c));
277static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200278static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
279static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200280static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200281static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200282static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
283static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
284static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
285static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
286
287/* helper functions used when doing re2post() ... regatom() parsing */
288#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200289 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290 return FAIL; \
291 *post_ptr++ = c; \
292 } while (0)
293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200294/*
295 * Initialize internal variables before NFA compilation.
296 * Return OK on success, FAIL otherwise.
297 */
298 static int
299nfa_regcomp_start(expr, re_flags)
300 char_u *expr;
301 int re_flags; /* see vim_regcomp() */
302{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200303 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200304 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305
306 nstate = 0;
307 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200308 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200309 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200311 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200312 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200313 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200314
315 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200316 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200318 post_start = (int *)lalloc(postfix_size, TRUE);
319 if (post_start == NULL)
320 return FAIL;
321 vim_memset(post_start, 0, postfix_size);
322 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200323 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200325 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200327 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328 regcomp_start(expr, re_flags);
329
330 return OK;
331}
332
333/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200334 * Allocate more space for post_start. Called when
335 * running above the estimated number of states.
336 */
337 static int
338realloc_post_list()
339{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200340 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200341 int new_max = nstate_max + 1000;
342 int *new_start;
343 int *old_start;
344
345 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
346 if (new_start == NULL)
347 return FAIL;
348 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
349 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
350 old_start = post_start;
351 post_start = new_start;
352 post_ptr = new_start + (post_ptr - old_start);
353 post_end = post_start + new_max;
354 vim_free(old_start);
355 return OK;
356}
357
358/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 * Search between "start" and "end" and try to recognize a
360 * character class in expanded form. For example [0-9].
361 * On success, return the id the character class to be emitted.
362 * On failure, return 0 (=FAIL)
363 * Start points to the first char of the range, while end should point
364 * to the closing brace.
365 */
366 static int
367nfa_recognize_char_class(start, end, extra_newl)
368 char_u *start;
369 char_u *end;
370 int extra_newl;
371{
372 int i;
373 /* Each of these variables takes up a char in "config[]",
374 * in the order they are here. */
375 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
376 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
377 char_u *p;
378#define NCONFIGS 16
379 int classid[NCONFIGS] = {
380 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
381 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
382 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
383 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
384 };
Bram Moolenaarba404472013-05-19 22:31:18 +0200385 char_u myconfig[10];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200386 char_u config[NCONFIGS][9] = {
387 "000000100", /* digit */
388 "100000100", /* non digit */
389 "011000100", /* hex-digit */
390 "111000100", /* non hex-digit */
391 "000001000", /* octal-digit */
392 "100001000", /* [^0-7] */
393 "000110110", /* [0-9A-Za-z_] */
394 "100110110", /* [^0-9A-Za-z_] */
395 "000110010", /* head of word */
396 "100110010", /* not head of word */
397 "000110000", /* alphabetic char a-z */
398 "100110000", /* non alphabetic char */
399 "000100000", /* lowercase letter */
400 "100100000", /* non lowercase */
401 "000010000", /* uppercase */
402 "100010000" /* non uppercase */
403 };
404
405 if (extra_newl == TRUE)
406 newl = TRUE;
407
408 if (*end != ']')
409 return FAIL;
410 p = start;
411 if (*p == '^')
412 {
413 not = TRUE;
414 p ++;
415 }
416
417 while (p < end)
418 {
419 if (p + 2 < end && *(p + 1) == '-')
420 {
421 switch (*p)
422 {
423 case '0':
424 if (*(p + 2) == '9')
425 {
426 o9 = TRUE;
427 break;
428 }
429 else
430 if (*(p + 2) == '7')
431 {
432 o7 = TRUE;
433 break;
434 }
435 case 'a':
436 if (*(p + 2) == 'z')
437 {
438 az = TRUE;
439 break;
440 }
441 else
442 if (*(p + 2) == 'f')
443 {
444 af = TRUE;
445 break;
446 }
447 case 'A':
448 if (*(p + 2) == 'Z')
449 {
450 AZ = TRUE;
451 break;
452 }
453 else
454 if (*(p + 2) == 'F')
455 {
456 AF = TRUE;
457 break;
458 }
459 /* FALLTHROUGH */
460 default:
461 return FAIL;
462 }
463 p += 3;
464 }
465 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
466 {
467 newl = TRUE;
468 p += 2;
469 }
470 else if (*p == '_')
471 {
472 underscore = TRUE;
473 p ++;
474 }
475 else if (*p == '\n')
476 {
477 newl = TRUE;
478 p ++;
479 }
480 else
481 return FAIL;
482 } /* while (p < end) */
483
484 if (p != end)
485 return FAIL;
486
487 /* build the config that represents the ranges we gathered */
488 STRCPY(myconfig, "000000000");
489 if (not == TRUE)
490 myconfig[0] = '1';
491 if (af == TRUE)
492 myconfig[1] = '1';
493 if (AF == TRUE)
494 myconfig[2] = '1';
495 if (az == TRUE)
496 myconfig[3] = '1';
497 if (AZ == TRUE)
498 myconfig[4] = '1';
499 if (o7 == TRUE)
500 myconfig[5] = '1';
501 if (o9 == TRUE)
502 myconfig[6] = '1';
503 if (underscore == TRUE)
504 myconfig[7] = '1';
505 if (newl == TRUE)
506 {
507 myconfig[8] = '1';
508 extra_newl = ADD_NL;
509 }
510 /* try to recognize character classes */
511 for (i = 0; i < NCONFIGS; i++)
Bram Moolenaarba404472013-05-19 22:31:18 +0200512 if (STRNCMP(myconfig, config[i], 8) == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200513 return classid[i] + extra_newl;
514
515 /* fallthrough => no success so far */
516 return FAIL;
517
518#undef NCONFIGS
519}
520
521/*
522 * Produce the bytes for equivalence class "c".
523 * Currently only handles latin1, latin9 and utf-8.
524 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
525 * equivalent to 'a OR b OR c'
526 *
527 * NOTE! When changing this function, also update reg_equi_class()
528 */
529 static int
530nfa_emit_equi_class(c, neg)
531 int c;
532 int neg;
533{
534 int first = TRUE;
535 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
536#define EMIT2(c) \
537 EMIT(c); \
538 if (neg == TRUE) { \
539 EMIT(NFA_NOT); \
540 } \
541 if (first == FALSE) \
542 EMIT(glue); \
543 else \
544 first = FALSE; \
545
546#ifdef FEAT_MBYTE
547 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
548 || STRCMP(p_enc, "iso-8859-15") == 0)
549#endif
550 {
551 switch (c)
552 {
553 case 'A': case '\300': case '\301': case '\302':
554 case '\303': case '\304': case '\305':
555 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
556 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
557 EMIT2('\305');
558 return OK;
559
560 case 'C': case '\307':
561 EMIT2('C'); EMIT2('\307');
562 return OK;
563
564 case 'E': case '\310': case '\311': case '\312': case '\313':
565 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
566 EMIT2('\312'); EMIT2('\313');
567 return OK;
568
569 case 'I': case '\314': case '\315': case '\316': case '\317':
570 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
571 EMIT2('\316'); EMIT2('\317');
572 return OK;
573
574 case 'N': case '\321':
575 EMIT2('N'); EMIT2('\321');
576 return OK;
577
578 case 'O': case '\322': case '\323': case '\324': case '\325':
579 case '\326':
580 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
581 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
582 return OK;
583
584 case 'U': case '\331': case '\332': case '\333': case '\334':
585 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
586 EMIT2('\333'); EMIT2('\334');
587 return OK;
588
589 case 'Y': case '\335':
590 EMIT2('Y'); EMIT2('\335');
591 return OK;
592
593 case 'a': case '\340': case '\341': case '\342':
594 case '\343': case '\344': case '\345':
595 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
596 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
597 EMIT2('\345');
598 return OK;
599
600 case 'c': case '\347':
601 EMIT2('c'); EMIT2('\347');
602 return OK;
603
604 case 'e': case '\350': case '\351': case '\352': case '\353':
605 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
606 EMIT2('\352'); EMIT2('\353');
607 return OK;
608
609 case 'i': case '\354': case '\355': case '\356': case '\357':
610 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
611 EMIT2('\356'); EMIT2('\357');
612 return OK;
613
614 case 'n': case '\361':
615 EMIT2('n'); EMIT2('\361');
616 return OK;
617
618 case 'o': case '\362': case '\363': case '\364': case '\365':
619 case '\366':
620 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
621 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
622 return OK;
623
624 case 'u': case '\371': case '\372': case '\373': case '\374':
625 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
626 EMIT2('\373'); EMIT2('\374');
627 return OK;
628
629 case 'y': case '\375': case '\377':
630 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
631 return OK;
632
633 default:
634 return FAIL;
635 }
636 }
637
638 EMIT(c);
639 return OK;
640#undef EMIT2
641}
642
643/*
644 * Code to parse regular expression.
645 *
646 * We try to reuse parsing functions in regexp.c to
647 * minimize surprise and keep the syntax consistent.
648 */
649
650/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 * Parse the lowest level.
652 *
653 * An atom can be one of a long list of items. Many atoms match one character
654 * in the text. It is often an ordinary character or a character class.
655 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
656 * is only for syntax highlighting.
657 *
658 * atom ::= ordinary-atom
659 * or \( pattern \)
660 * or \%( pattern \)
661 * or \z( pattern \)
662 */
663 static int
664nfa_regatom()
665{
666 int c;
667 int charclass;
668 int equiclass;
669 int collclass;
670 int got_coll_char;
671 char_u *p;
672 char_u *endp;
673#ifdef FEAT_MBYTE
674 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675#endif
676 int extra = 0;
677 int first;
678 int emit_range;
679 int negated;
680 int result;
681 int startc = -1;
682 int endc = -1;
683 int oldstartc = -1;
684 int cpo_lit; /* 'cpoptions' contains 'l' flag */
685 int cpo_bsl; /* 'cpoptions' contains '\' flag */
686 int glue; /* ID that will "glue" nodes together */
687
688 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
689 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
690
691 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692 switch (c)
693 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200694 case NUL:
695 syntax_error = TRUE;
696 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
697
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 case Magic('^'):
699 EMIT(NFA_BOL);
700 break;
701
702 case Magic('$'):
703 EMIT(NFA_EOL);
704#if defined(FEAT_SYN_HL) || defined(PROTO)
705 had_eol = TRUE;
706#endif
707 break;
708
709 case Magic('<'):
710 EMIT(NFA_BOW);
711 break;
712
713 case Magic('>'):
714 EMIT(NFA_EOW);
715 break;
716
717 case Magic('_'):
718 c = no_Magic(getchr());
719 if (c == '^') /* "\_^" is start-of-line */
720 {
721 EMIT(NFA_BOL);
722 break;
723 }
724 if (c == '$') /* "\_$" is end-of-line */
725 {
726 EMIT(NFA_EOL);
727#if defined(FEAT_SYN_HL) || defined(PROTO)
728 had_eol = TRUE;
729#endif
730 break;
731 }
732
733 extra = ADD_NL;
734
735 /* "\_[" is collection plus newline */
736 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200737 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200738
739 /* "\_x" is character class plus newline */
740 /*FALLTHROUGH*/
741
742 /*
743 * Character classes.
744 */
745 case Magic('.'):
746 case Magic('i'):
747 case Magic('I'):
748 case Magic('k'):
749 case Magic('K'):
750 case Magic('f'):
751 case Magic('F'):
752 case Magic('p'):
753 case Magic('P'):
754 case Magic('s'):
755 case Magic('S'):
756 case Magic('d'):
757 case Magic('D'):
758 case Magic('x'):
759 case Magic('X'):
760 case Magic('o'):
761 case Magic('O'):
762 case Magic('w'):
763 case Magic('W'):
764 case Magic('h'):
765 case Magic('H'):
766 case Magic('a'):
767 case Magic('A'):
768 case Magic('l'):
769 case Magic('L'):
770 case Magic('u'):
771 case Magic('U'):
772 p = vim_strchr(classchars, no_Magic(c));
773 if (p == NULL)
774 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200775 EMSGN("INTERNAL: Unknown character class char: %ld", c);
776 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200777 }
778#ifdef FEAT_MBYTE
779 /* When '.' is followed by a composing char ignore the dot, so that
780 * the composing char is matched here. */
781 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
782 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200783 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200784 c = getchr();
785 goto nfa_do_multibyte;
786 }
787#endif
788 EMIT(nfa_classcodes[p - classchars]);
789 if (extra == ADD_NL)
790 {
791 EMIT(NFA_NEWL);
792 EMIT(NFA_OR);
793 regflags |= RF_HASNL;
794 }
795 break;
796
797 case Magic('n'):
798 if (reg_string)
799 /* In a string "\n" matches a newline character. */
800 EMIT(NL);
801 else
802 {
803 /* In buffer text "\n" matches the end of a line. */
804 EMIT(NFA_NEWL);
805 regflags |= RF_HASNL;
806 }
807 break;
808
809 case Magic('('):
810 if (nfa_reg(REG_PAREN) == FAIL)
811 return FAIL; /* cascaded error */
812 break;
813
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 case Magic('|'):
815 case Magic('&'):
816 case Magic(')'):
817 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200818 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200819 return FAIL;
820
821 case Magic('='):
822 case Magic('?'):
823 case Magic('+'):
824 case Magic('@'):
825 case Magic('*'):
826 case Magic('{'):
827 /* these should follow an atom, not form an atom */
828 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200829 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830 return FAIL;
831
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200832 case Magic('~'):
833 {
834 char_u *lp;
835
836 /* Previous substitute pattern.
837 * Generated as "\%(pattern\)". */
838 if (reg_prev_sub == NULL)
839 {
840 EMSG(_(e_nopresub));
841 return FAIL;
842 }
843 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
844 {
845 EMIT(PTR2CHAR(lp));
846 if (lp != reg_prev_sub)
847 EMIT(NFA_CONCAT);
848 }
849 EMIT(NFA_NOPEN);
850 break;
851 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200852
Bram Moolenaar428e9872013-05-30 17:05:39 +0200853 case Magic('1'):
854 case Magic('2'):
855 case Magic('3'):
856 case Magic('4'):
857 case Magic('5'):
858 case Magic('6'):
859 case Magic('7'):
860 case Magic('8'):
861 case Magic('9'):
862 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
863 nfa_has_backref = TRUE;
864 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200865
866 case Magic('z'):
867 c = no_Magic(getchr());
868 switch (c)
869 {
870 case 's':
871 EMIT(NFA_ZSTART);
872 break;
873 case 'e':
874 EMIT(NFA_ZEND);
875 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200876 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200877#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200878 case '1':
879 case '2':
880 case '3':
881 case '4':
882 case '5':
883 case '6':
884 case '7':
885 case '8':
886 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200887 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200888 if (reg_do_extmatch != REX_USE)
889 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200890 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
891 /* No need to set nfa_has_backref, the sub-matches don't
892 * change when \z1 .. \z9 maches or not. */
893 re_has_z = REX_USE;
894 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200895 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200896 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200897 if (reg_do_extmatch != REX_SET)
898 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200899 if (nfa_reg(REG_ZPAREN) == FAIL)
900 return FAIL; /* cascaded error */
901 re_has_z = REX_SET;
902 break;
903#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200904 default:
905 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200906 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200907 no_Magic(c));
908 return FAIL;
909 }
910 break;
911
912 case Magic('%'):
913 c = no_Magic(getchr());
914 switch (c)
915 {
916 /* () without a back reference */
917 case '(':
918 if (nfa_reg(REG_NPAREN) == FAIL)
919 return FAIL;
920 EMIT(NFA_NOPEN);
921 break;
922
923 case 'd': /* %d123 decimal */
924 case 'o': /* %o123 octal */
925 case 'x': /* %xab hex 2 */
926 case 'u': /* %uabcd hex 4 */
927 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200928 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200929 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200930
Bram Moolenaar47196582013-05-25 22:04:23 +0200931 switch (c)
932 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200933 case 'd': nr = getdecchrs(); break;
934 case 'o': nr = getoctchrs(); break;
935 case 'x': nr = gethexchrs(2); break;
936 case 'u': nr = gethexchrs(4); break;
937 case 'U': nr = gethexchrs(8); break;
938 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200939 }
940
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200941 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200942 EMSG2_RET_FAIL(
943 _("E678: Invalid character after %s%%[dxouU]"),
944 reg_magic == MAGIC_ALL);
945 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200946 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200947 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200948 break;
949
950 /* Catch \%^ and \%$ regardless of where they appear in the
951 * pattern -- regardless of whether or not it makes sense. */
952 case '^':
953 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200954 break;
955
956 case '$':
957 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 break;
959
960 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200961 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200962 break;
963
964 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200965 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200966 return FAIL;
967 break;
968
969 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200970 /* TODO: \%[abc] not supported yet */
971 return FAIL;
972
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200973 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200974 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200975 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200976 int cmp = c;
977
978 if (c == '<' || c == '>')
979 c = getchr();
980 while (VIM_ISDIGIT(c))
981 {
982 n = n * 10 + (c - '0');
983 c = getchr();
984 }
985 if (c == 'l' || c == 'c' || c == 'v')
986 {
987 EMIT(n);
988 if (c == 'l')
989 EMIT(cmp == '<' ? NFA_LNUM_LT :
990 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
991 else if (c == 'c')
992 EMIT(cmp == '<' ? NFA_COL_LT :
993 cmp == '>' ? NFA_COL_GT : NFA_COL);
994 else
995 EMIT(cmp == '<' ? NFA_VCOL_LT :
996 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
997 break;
998 }
999 else if (c == '\'')
1000 /* TODO: \%'m not supported yet */
1001 return FAIL;
1002 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001003 syntax_error = TRUE;
1004 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1005 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001006 return FAIL;
1007 }
1008 break;
1009
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001010 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001011collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001012 /*
1013 * Glue is emitted between several atoms from the [].
1014 * It is either NFA_OR, or NFA_CONCAT.
1015 *
1016 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1017 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1018 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1019 * notation)
1020 *
1021 */
1022
1023
1024/* Emit negation atoms, if needed.
1025 * The CONCAT below merges the NOT with the previous node. */
1026#define TRY_NEG() \
1027 if (negated == TRUE) \
1028 { \
1029 EMIT(NFA_NOT); \
1030 }
1031
1032/* Emit glue between important nodes : CONCAT or OR. */
1033#define EMIT_GLUE() \
1034 if (first == FALSE) \
1035 EMIT(glue); \
1036 else \
1037 first = FALSE;
1038
1039 p = regparse;
1040 endp = skip_anyof(p);
1041 if (*endp == ']')
1042 {
1043 /*
1044 * Try to reverse engineer character classes. For example,
1045 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1046 * and perform the necessary substitutions in the NFA.
1047 */
1048 result = nfa_recognize_char_class(regparse, endp,
1049 extra == ADD_NL);
1050 if (result != FAIL)
1051 {
1052 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1053 EMIT(result);
1054 else /* must be char class + newline */
1055 {
1056 EMIT(result - ADD_NL);
1057 EMIT(NFA_NEWL);
1058 EMIT(NFA_OR);
1059 }
1060 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001061 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001062 return OK;
1063 }
1064 /*
1065 * Failed to recognize a character class. Use the simple
1066 * version that turns [abc] into 'a' OR 'b' OR 'c'
1067 */
1068 startc = endc = oldstartc = -1;
1069 first = TRUE; /* Emitting first atom in this sequence? */
1070 negated = FALSE;
1071 glue = NFA_OR;
1072 if (*regparse == '^') /* negated range */
1073 {
1074 negated = TRUE;
1075 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001076 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 }
1078 if (*regparse == '-')
1079 {
1080 startc = '-';
1081 EMIT(startc);
1082 TRY_NEG();
1083 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001084 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 }
1086 /* Emit the OR branches for each character in the [] */
1087 emit_range = FALSE;
1088 while (regparse < endp)
1089 {
1090 oldstartc = startc;
1091 startc = -1;
1092 got_coll_char = FALSE;
1093 if (*regparse == '[')
1094 {
1095 /* Check for [: :], [= =], [. .] */
1096 equiclass = collclass = 0;
1097 charclass = get_char_class(&regparse);
1098 if (charclass == CLASS_NONE)
1099 {
1100 equiclass = get_equi_class(&regparse);
1101 if (equiclass == 0)
1102 collclass = get_coll_element(&regparse);
1103 }
1104
1105 /* Character class like [:alpha:] */
1106 if (charclass != CLASS_NONE)
1107 {
1108 switch (charclass)
1109 {
1110 case CLASS_ALNUM:
1111 EMIT(NFA_CLASS_ALNUM);
1112 break;
1113 case CLASS_ALPHA:
1114 EMIT(NFA_CLASS_ALPHA);
1115 break;
1116 case CLASS_BLANK:
1117 EMIT(NFA_CLASS_BLANK);
1118 break;
1119 case CLASS_CNTRL:
1120 EMIT(NFA_CLASS_CNTRL);
1121 break;
1122 case CLASS_DIGIT:
1123 EMIT(NFA_CLASS_DIGIT);
1124 break;
1125 case CLASS_GRAPH:
1126 EMIT(NFA_CLASS_GRAPH);
1127 break;
1128 case CLASS_LOWER:
1129 EMIT(NFA_CLASS_LOWER);
1130 break;
1131 case CLASS_PRINT:
1132 EMIT(NFA_CLASS_PRINT);
1133 break;
1134 case CLASS_PUNCT:
1135 EMIT(NFA_CLASS_PUNCT);
1136 break;
1137 case CLASS_SPACE:
1138 EMIT(NFA_CLASS_SPACE);
1139 break;
1140 case CLASS_UPPER:
1141 EMIT(NFA_CLASS_UPPER);
1142 break;
1143 case CLASS_XDIGIT:
1144 EMIT(NFA_CLASS_XDIGIT);
1145 break;
1146 case CLASS_TAB:
1147 EMIT(NFA_CLASS_TAB);
1148 break;
1149 case CLASS_RETURN:
1150 EMIT(NFA_CLASS_RETURN);
1151 break;
1152 case CLASS_BACKSPACE:
1153 EMIT(NFA_CLASS_BACKSPACE);
1154 break;
1155 case CLASS_ESCAPE:
1156 EMIT(NFA_CLASS_ESCAPE);
1157 break;
1158 }
1159 TRY_NEG();
1160 EMIT_GLUE();
1161 continue;
1162 }
1163 /* Try equivalence class [=a=] and the like */
1164 if (equiclass != 0)
1165 {
1166 result = nfa_emit_equi_class(equiclass, negated);
1167 if (result == FAIL)
1168 {
1169 /* should never happen */
1170 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1171 }
1172 EMIT_GLUE();
1173 continue;
1174 }
1175 /* Try collating class like [. .] */
1176 if (collclass != 0)
1177 {
1178 startc = collclass; /* allow [.a.]-x as a range */
1179 /* Will emit the proper atom at the end of the
1180 * while loop. */
1181 }
1182 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001183 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1184 * start character. */
1185 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001186 {
1187 emit_range = TRUE;
1188 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001189 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190 continue; /* reading the end of the range */
1191 }
1192
1193 /* Now handle simple and escaped characters.
1194 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1195 * accepts "\t", "\e", etc., but only when the 'l' flag in
1196 * 'cpoptions' is not included.
1197 * Posix doesn't recognize backslash at all.
1198 */
1199 if (*regparse == '\\'
1200 && !cpo_bsl
1201 && regparse + 1 <= endp
1202 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1203 || (!cpo_lit
1204 && vim_strchr(REGEXP_ABBR, regparse[1])
1205 != NULL)
1206 )
1207 )
1208 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001209 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001211 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 startc = reg_string ? NL : NFA_NEWL;
1213 else
1214 if (*regparse == 'd'
1215 || *regparse == 'o'
1216 || *regparse == 'x'
1217 || *regparse == 'u'
1218 || *regparse == 'U'
1219 )
1220 {
1221 /* TODO(RE) This needs more testing */
1222 startc = coll_get_char();
1223 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001224 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 }
1226 else
1227 {
1228 /* \r,\t,\e,\b */
1229 startc = backslash_trans(*regparse);
1230 }
1231 }
1232
1233 /* Normal printable char */
1234 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001235 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236
1237 /* Previous char was '-', so this char is end of range. */
1238 if (emit_range)
1239 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001240 endc = startc;
1241 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001242 if (startc > endc)
1243 EMSG_RET_FAIL(_(e_invrange));
1244#ifdef FEAT_MBYTE
1245 if (has_mbyte && ((*mb_char2len)(startc) > 1
1246 || (*mb_char2len)(endc) > 1))
1247 {
1248 if (endc > startc + 256)
1249 EMSG_RET_FAIL(_(e_invrange));
1250 /* Emit the range. "startc" was already emitted, so
1251 * skip it. */
1252 for (c = startc + 1; c <= endc; c++)
1253 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001254 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 TRY_NEG();
1256 EMIT_GLUE();
1257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258 }
1259 else
1260#endif
1261 {
1262#ifdef EBCDIC
1263 int alpha_only = FALSE;
1264
1265 /* for alphabetical range skip the gaps
1266 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1267 if (isalpha(startc) && isalpha(endc))
1268 alpha_only = TRUE;
1269#endif
1270 /* Emit the range. "startc" was already emitted, so
1271 * skip it. */
1272 for (c = startc + 1; c <= endc; c++)
1273#ifdef EBCDIC
1274 if (!alpha_only || isalpha(startc))
1275#endif
1276 {
1277 EMIT(c);
1278 TRY_NEG();
1279 EMIT_GLUE();
1280 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001281 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001282 emit_range = FALSE;
1283 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001284 }
1285 else
1286 {
1287 /*
1288 * This char (startc) is not part of a range. Just
1289 * emit it.
1290 *
1291 * Normally, simply emit startc. But if we get char
1292 * code=0 from a collating char, then replace it with
1293 * 0x0a.
1294 *
1295 * This is needed to completely mimic the behaviour of
1296 * the backtracking engine.
1297 */
1298 if (got_coll_char == TRUE && startc == 0)
1299 EMIT(0x0a);
1300 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001301 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001302 TRY_NEG();
1303 EMIT_GLUE();
1304 }
1305
Bram Moolenaar51a29832013-05-28 22:30:35 +02001306 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307 } /* while (p < endp) */
1308
Bram Moolenaar51a29832013-05-28 22:30:35 +02001309 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001310 if (*regparse == '-') /* if last, '-' is just a char */
1311 {
1312 EMIT('-');
1313 TRY_NEG();
1314 EMIT_GLUE();
1315 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001316 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 /* skip the trailing ] */
1319 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001320 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 if (negated == TRUE)
1322 {
1323 /* Mark end of negated char range */
1324 EMIT(NFA_END_NEG_RANGE);
1325 EMIT(NFA_CONCAT);
1326 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001327
1328 /* \_[] also matches \n but it's not negated */
1329 if (extra == ADD_NL)
1330 {
1331 EMIT(reg_string ? NL : NFA_NEWL);
1332 EMIT(NFA_OR);
1333 }
1334
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001335 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001336 } /* if exists closing ] */
1337
1338 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339 {
1340 syntax_error = TRUE;
1341 EMSG_RET_FAIL(_(e_missingbracket));
1342 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001343 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 default:
1346 {
1347#ifdef FEAT_MBYTE
1348 int plen;
1349
1350nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001351 /* plen is length of current char with composing chars */
1352 if (enc_utf8 && ((*mb_char2len)(c)
1353 != (plen = (*mb_ptr2len)(old_regparse))
1354 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001356 int i = 0;
1357
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001358 /* A base character plus composing characters, or just one
1359 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001360 * This requires creating a separate atom as if enclosing
1361 * the characters in (), where NFA_COMPOSING is the ( and
1362 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001363 * building the postfix form, not the NFA itself;
1364 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001365 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001366 for (;;)
1367 {
1368 EMIT(c);
1369 if (i > 0)
1370 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001371 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001372 break;
1373 c = utf_ptr2char(old_regparse + i);
1374 }
1375 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001376 regparse = old_regparse + plen;
1377 }
1378 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001379#endif
1380 {
1381 c = no_Magic(c);
1382 EMIT(c);
1383 }
1384 return OK;
1385 }
1386 }
1387
1388#undef TRY_NEG
1389#undef EMIT_GLUE
1390
1391 return OK;
1392}
1393
1394/*
1395 * Parse something followed by possible [*+=].
1396 *
1397 * A piece is an atom, possibly followed by a multi, an indication of how many
1398 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1399 * characters: "", "a", "aa", etc.
1400 *
1401 * piece ::= atom
1402 * or atom multi
1403 */
1404 static int
1405nfa_regpiece()
1406{
1407 int i;
1408 int op;
1409 int ret;
1410 long minval, maxval;
1411 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001412 parse_state_T old_state;
1413 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001414 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001415 int old_post_pos;
1416 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 int quest;
1418
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001419 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1420 * next. */
1421 save_parse_state(&old_state);
1422
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001424 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001425
1426 ret = nfa_regatom();
1427 if (ret == FAIL)
1428 return FAIL; /* cascaded error */
1429
1430 op = peekchr();
1431 if (re_multi_type(op) == NOT_MULTI)
1432 return OK;
1433
1434 skipchr();
1435 switch (op)
1436 {
1437 case Magic('*'):
1438 EMIT(NFA_STAR);
1439 break;
1440
1441 case Magic('+'):
1442 /*
1443 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1444 * first and only submatch would be "aaa". But the backtracking
1445 * engine interprets the plus as "try matching one more time", and
1446 * a* matches a second time at the end of the input, the empty
1447 * string.
1448 * The submatch will the empty string.
1449 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001450 * In order to be consistent with the old engine, we replace
1451 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001453 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001454 curchr = -1;
1455 if (nfa_regatom() == FAIL)
1456 return FAIL;
1457 EMIT(NFA_STAR);
1458 EMIT(NFA_CONCAT);
1459 skipchr(); /* skip the \+ */
1460 break;
1461
1462 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001463 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001465 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001466 switch(op)
1467 {
1468 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001469 /* \@= */
1470 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001471 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001472 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001473 /* \@! */
1474 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001475 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001476 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001477 op = no_Magic(getchr());
1478 if (op == '=')
1479 /* \@<= */
1480 i = NFA_PREV_ATOM_JUST_BEFORE;
1481 else if (op == '!')
1482 /* \@<! */
1483 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1484 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001486 /* \@> Not supported yet */
1487 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001488 return FAIL;
1489 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001490 if (i == 0)
1491 {
1492 syntax_error = TRUE;
1493 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1494 return FAIL;
1495 }
1496 EMIT(i);
1497 if (i == NFA_PREV_ATOM_JUST_BEFORE
1498 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1499 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 break;
1501
1502 case Magic('?'):
1503 case Magic('='):
1504 EMIT(NFA_QUEST);
1505 break;
1506
1507 case Magic('{'):
1508 /* a{2,5} will expand to 'aaa?a?a?'
1509 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1510 * version of '?'
1511 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1512 * parenthesis have the same id
1513 */
1514
1515 greedy = TRUE;
1516 c2 = peekchr();
1517 if (c2 == '-' || c2 == Magic('-'))
1518 {
1519 skipchr();
1520 greedy = FALSE;
1521 }
1522 if (!read_limits(&minval, &maxval))
1523 {
1524 syntax_error = TRUE;
1525 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1526 }
1527 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1528 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001529 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001531 if (greedy)
1532 /* \{}, \{0,} */
1533 EMIT(NFA_STAR);
1534 else
1535 /* \{-}, \{-0,} */
1536 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 break;
1538 }
1539
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 /* Special case: x{0} or x{-0} */
1541 if (maxval == 0)
1542 {
1543 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001544 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1546 EMIT(NFA_SKIP_CHAR);
1547 return OK;
1548 }
1549
1550 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001551 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001552 /* Save parse state after the repeated atom and the \{} */
1553 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1556 for (i = 0; i < maxval; i++)
1557 {
1558 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001559 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001560 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561 if (nfa_regatom() == FAIL)
1562 return FAIL;
1563 /* after "minval" times, atoms are optional */
1564 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001565 {
1566 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001567 {
1568 if (greedy)
1569 EMIT(NFA_STAR);
1570 else
1571 EMIT(NFA_STAR_NONGREEDY);
1572 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001573 else
1574 EMIT(quest);
1575 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001576 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001577 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001578 if (i + 1 > minval && maxval == MAX_LIMIT)
1579 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001580 }
1581
1582 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001583 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001584 curchr = -1;
1585
1586 break;
1587
1588
1589 default:
1590 break;
1591 } /* end switch */
1592
1593 if (re_multi_type(peekchr()) != NOT_MULTI)
1594 {
1595 /* Can't have a multi follow a multi. */
1596 syntax_error = TRUE;
1597 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1598 }
1599
1600 return OK;
1601}
1602
1603/*
1604 * Parse one or more pieces, concatenated. It matches a match for the
1605 * first piece, followed by a match for the second piece, etc. Example:
1606 * "f[0-9]b", first matches "f", then a digit and then "b".
1607 *
1608 * concat ::= piece
1609 * or piece piece
1610 * or piece piece piece
1611 * etc.
1612 */
1613 static int
1614nfa_regconcat()
1615{
1616 int cont = TRUE;
1617 int first = TRUE;
1618
1619 while (cont)
1620 {
1621 switch (peekchr())
1622 {
1623 case NUL:
1624 case Magic('|'):
1625 case Magic('&'):
1626 case Magic(')'):
1627 cont = FALSE;
1628 break;
1629
1630 case Magic('Z'):
1631#ifdef FEAT_MBYTE
1632 regflags |= RF_ICOMBINE;
1633#endif
1634 skipchr_keepstart();
1635 break;
1636 case Magic('c'):
1637 regflags |= RF_ICASE;
1638 skipchr_keepstart();
1639 break;
1640 case Magic('C'):
1641 regflags |= RF_NOICASE;
1642 skipchr_keepstart();
1643 break;
1644 case Magic('v'):
1645 reg_magic = MAGIC_ALL;
1646 skipchr_keepstart();
1647 curchr = -1;
1648 break;
1649 case Magic('m'):
1650 reg_magic = MAGIC_ON;
1651 skipchr_keepstart();
1652 curchr = -1;
1653 break;
1654 case Magic('M'):
1655 reg_magic = MAGIC_OFF;
1656 skipchr_keepstart();
1657 curchr = -1;
1658 break;
1659 case Magic('V'):
1660 reg_magic = MAGIC_NONE;
1661 skipchr_keepstart();
1662 curchr = -1;
1663 break;
1664
1665 default:
1666 if (nfa_regpiece() == FAIL)
1667 return FAIL;
1668 if (first == FALSE)
1669 EMIT(NFA_CONCAT);
1670 else
1671 first = FALSE;
1672 break;
1673 }
1674 }
1675
1676 return OK;
1677}
1678
1679/*
1680 * Parse a branch, one or more concats, separated by "\&". It matches the
1681 * last concat, but only if all the preceding concats also match at the same
1682 * position. Examples:
1683 * "foobeep\&..." matches "foo" in "foobeep".
1684 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1685 *
1686 * branch ::= concat
1687 * or concat \& concat
1688 * or concat \& concat \& concat
1689 * etc.
1690 */
1691 static int
1692nfa_regbranch()
1693{
1694 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001695 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001696
Bram Moolenaar16299b52013-05-30 18:45:23 +02001697 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698
1699 /* First branch, possibly the only one */
1700 if (nfa_regconcat() == FAIL)
1701 return FAIL;
1702
1703 ch = peekchr();
1704 /* Try next concats */
1705 while (ch == Magic('&'))
1706 {
1707 skipchr();
1708 EMIT(NFA_NOPEN);
1709 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001710 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001711 if (nfa_regconcat() == FAIL)
1712 return FAIL;
1713 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001714 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001715 EMIT(NFA_SKIP_CHAR);
1716 EMIT(NFA_CONCAT);
1717 ch = peekchr();
1718 }
1719
1720 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001721 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001722 EMIT(NFA_SKIP_CHAR);
1723
1724 return OK;
1725}
1726
1727/*
1728 * Parse a pattern, one or more branches, separated by "\|". It matches
1729 * anything that matches one of the branches. Example: "foo\|beep" matches
1730 * "foo" and matches "beep". If more than one branch matches, the first one
1731 * is used.
1732 *
1733 * pattern ::= branch
1734 * or branch \| branch
1735 * or branch \| branch \| branch
1736 * etc.
1737 */
1738 static int
1739nfa_reg(paren)
1740 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1741{
1742 int parno = 0;
1743
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 if (paren == REG_PAREN)
1745 {
1746 if (regnpar >= NSUBEXP) /* Too many `(' */
1747 {
1748 syntax_error = TRUE;
1749 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1750 }
1751 parno = regnpar++;
1752 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001753#ifdef FEAT_SYN_HL
1754 else if (paren == REG_ZPAREN)
1755 {
1756 /* Make a ZOPEN node. */
1757 if (regnzpar >= NSUBEXP)
1758 {
1759 syntax_error = TRUE;
1760 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1761 }
1762 parno = regnzpar++;
1763 }
1764#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765
1766 if (nfa_regbranch() == FAIL)
1767 return FAIL; /* cascaded error */
1768
1769 while (peekchr() == Magic('|'))
1770 {
1771 skipchr();
1772 if (nfa_regbranch() == FAIL)
1773 return FAIL; /* cascaded error */
1774 EMIT(NFA_OR);
1775 }
1776
1777 /* Check for proper termination. */
1778 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1779 {
1780 syntax_error = TRUE;
1781 if (paren == REG_NPAREN)
1782 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1783 else
1784 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1785 }
1786 else if (paren == REG_NOPAREN && peekchr() != NUL)
1787 {
1788 syntax_error = TRUE;
1789 if (peekchr() == Magic(')'))
1790 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1791 else
1792 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1793 }
1794 /*
1795 * Here we set the flag allowing back references to this set of
1796 * parentheses.
1797 */
1798 if (paren == REG_PAREN)
1799 {
1800 had_endbrace[parno] = TRUE; /* have seen the close paren */
1801 EMIT(NFA_MOPEN + parno);
1802 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001803#ifdef FEAT_SYN_HL
1804 else if (paren == REG_ZPAREN)
1805 EMIT(NFA_ZOPEN + parno);
1806#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001807
1808 return OK;
1809}
1810
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811#ifdef DEBUG
1812static char_u code[50];
1813
1814 static void
1815nfa_set_code(c)
1816 int c;
1817{
1818 int addnl = FALSE;
1819
1820 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1821 {
1822 addnl = TRUE;
1823 c -= ADD_NL;
1824 }
1825
1826 STRCPY(code, "");
1827 switch (c)
1828 {
1829 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1830 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1831 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1832 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1833 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1834 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1835
Bram Moolenaar5714b802013-05-28 22:03:20 +02001836 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1837 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1838 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1839 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1840 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1841 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1842 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1843 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1844 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001845#ifdef FEAT_SYN_HL
1846 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1847 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1848 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1849 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1850 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1851 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1852 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1853 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1854 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1855#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001856 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1857
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 case NFA_PREV_ATOM_NO_WIDTH:
1859 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001860 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1861 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001862 case NFA_PREV_ATOM_JUST_BEFORE:
1863 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1864 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1865 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001866 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1867 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001869 case NFA_START_INVISIBLE_BEFORE:
1870 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1872
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001873 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1874 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1875
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001876 case NFA_MOPEN:
1877 case NFA_MOPEN1:
1878 case NFA_MOPEN2:
1879 case NFA_MOPEN3:
1880 case NFA_MOPEN4:
1881 case NFA_MOPEN5:
1882 case NFA_MOPEN6:
1883 case NFA_MOPEN7:
1884 case NFA_MOPEN8:
1885 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886 STRCPY(code, "NFA_MOPEN(x)");
1887 code[10] = c - NFA_MOPEN + '0';
1888 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001889 case NFA_MCLOSE:
1890 case NFA_MCLOSE1:
1891 case NFA_MCLOSE2:
1892 case NFA_MCLOSE3:
1893 case NFA_MCLOSE4:
1894 case NFA_MCLOSE5:
1895 case NFA_MCLOSE6:
1896 case NFA_MCLOSE7:
1897 case NFA_MCLOSE8:
1898 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 STRCPY(code, "NFA_MCLOSE(x)");
1900 code[11] = c - NFA_MCLOSE + '0';
1901 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001902#ifdef FEAT_SYN_HL
1903 case NFA_ZOPEN:
1904 case NFA_ZOPEN1:
1905 case NFA_ZOPEN2:
1906 case NFA_ZOPEN3:
1907 case NFA_ZOPEN4:
1908 case NFA_ZOPEN5:
1909 case NFA_ZOPEN6:
1910 case NFA_ZOPEN7:
1911 case NFA_ZOPEN8:
1912 case NFA_ZOPEN9:
1913 STRCPY(code, "NFA_ZOPEN(x)");
1914 code[10] = c - NFA_ZOPEN + '0';
1915 break;
1916 case NFA_ZCLOSE:
1917 case NFA_ZCLOSE1:
1918 case NFA_ZCLOSE2:
1919 case NFA_ZCLOSE3:
1920 case NFA_ZCLOSE4:
1921 case NFA_ZCLOSE5:
1922 case NFA_ZCLOSE6:
1923 case NFA_ZCLOSE7:
1924 case NFA_ZCLOSE8:
1925 case NFA_ZCLOSE9:
1926 STRCPY(code, "NFA_ZCLOSE(x)");
1927 code[11] = c - NFA_ZCLOSE + '0';
1928 break;
1929#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1931 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1932 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1933 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001934 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1935 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001937 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1938 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1939 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1941 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1942 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1944 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1945 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1946 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1947 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1948 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1949 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1950 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1951 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1952 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1953 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1954 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1955 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1956 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1957 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1958 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1959 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1960
1961 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1962 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1963 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1964 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1965 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1966 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1967 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1968 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1969 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1970 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1971 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1972 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1973 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1974 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1975 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1976 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1977 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1978 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1979 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1980 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1981 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1982 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1983 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1984 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1985 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1986 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1987 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1988
1989 default:
1990 STRCPY(code, "CHAR(x)");
1991 code[5] = c;
1992 }
1993
1994 if (addnl == TRUE)
1995 STRCAT(code, " + NEWLINE ");
1996
1997}
1998
1999#ifdef ENABLE_LOG
2000static FILE *log_fd;
2001
2002/*
2003 * Print the postfix notation of the current regexp.
2004 */
2005 static void
2006nfa_postfix_dump(expr, retval)
2007 char_u *expr;
2008 int retval;
2009{
2010 int *p;
2011 FILE *f;
2012
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002013 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 if (f != NULL)
2015 {
2016 fprintf(f, "\n-------------------------\n");
2017 if (retval == FAIL)
2018 fprintf(f, ">>> NFA engine failed ... \n");
2019 else if (retval == OK)
2020 fprintf(f, ">>> NFA engine succeeded !\n");
2021 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002022 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 {
2024 nfa_set_code(*p);
2025 fprintf(f, "%s, ", code);
2026 }
2027 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002028 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029 fprintf(f, "%d ", *p);
2030 fprintf(f, "\n\n");
2031 fclose(f);
2032 }
2033}
2034
2035/*
2036 * Print the NFA starting with a root node "state".
2037 */
2038 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002039nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040 FILE *debugf;
2041 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002043 garray_T indent;
2044
2045 ga_init2(&indent, 1, 64);
2046 ga_append(&indent, '\0');
2047 nfa_print_state2(debugf, state, &indent);
2048 ga_clear(&indent);
2049}
2050
2051 static void
2052nfa_print_state2(debugf, state, indent)
2053 FILE *debugf;
2054 nfa_state_T *state;
2055 garray_T *indent;
2056{
2057 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058
2059 if (state == NULL)
2060 return;
2061
2062 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002063
2064 /* Output indent */
2065 p = (char_u *)indent->ga_data;
2066 if (indent->ga_len >= 3)
2067 {
2068 int last = indent->ga_len - 3;
2069 char_u save[2];
2070
2071 STRNCPY(save, &p[last], 2);
2072 STRNCPY(&p[last], "+-", 2);
2073 fprintf(debugf, " %s", p);
2074 STRNCPY(&p[last], save, 2);
2075 }
2076 else
2077 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078
2079 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002080 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2081 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 if (state->id < 0)
2083 return;
2084
2085 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002086
2087 /* grow indent for state->out */
2088 indent->ga_len -= 1;
2089 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002090 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002091 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002092 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002093 ga_append(indent, '\0');
2094
2095 nfa_print_state2(debugf, state->out, indent);
2096
2097 /* replace last part of indent for state->out1 */
2098 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002099 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002100 ga_append(indent, '\0');
2101
2102 nfa_print_state2(debugf, state->out1, indent);
2103
2104 /* shrink indent */
2105 indent->ga_len -= 3;
2106 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002107}
2108
2109/*
2110 * Print the NFA state machine.
2111 */
2112 static void
2113nfa_dump(prog)
2114 nfa_regprog_T *prog;
2115{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002116 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002117
2118 if (debugf != NULL)
2119 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002120 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002121 fclose(debugf);
2122 }
2123}
2124#endif /* ENABLE_LOG */
2125#endif /* DEBUG */
2126
2127/*
2128 * Parse r.e. @expr and convert it into postfix form.
2129 * Return the postfix string on success, NULL otherwise.
2130 */
2131 static int *
2132re2post()
2133{
2134 if (nfa_reg(REG_NOPAREN) == FAIL)
2135 return NULL;
2136 EMIT(NFA_MOPEN);
2137 return post_start;
2138}
2139
2140/* NB. Some of the code below is inspired by Russ's. */
2141
2142/*
2143 * Represents an NFA state plus zero or one or two arrows exiting.
2144 * if c == MATCH, no arrows out; matching state.
2145 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2146 * If c < 256, labeled arrow with character c to out.
2147 */
2148
2149static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2150
2151/*
2152 * Allocate and initialize nfa_state_T.
2153 */
2154 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002155alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002156 int c;
2157 nfa_state_T *out;
2158 nfa_state_T *out1;
2159{
2160 nfa_state_T *s;
2161
2162 if (istate >= nstate)
2163 return NULL;
2164
2165 s = &state_ptr[istate++];
2166
2167 s->c = c;
2168 s->out = out;
2169 s->out1 = out1;
2170
2171 s->id = istate;
2172 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 s->negated = FALSE;
2174
2175 return s;
2176}
2177
2178/*
2179 * A partially built NFA without the matching state filled in.
2180 * Frag_T.start points at the start state.
2181 * Frag_T.out is a list of places that need to be set to the
2182 * next state for this fragment.
2183 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002184
2185/* Since the out pointers in the list are always
2186 * uninitialized, we use the pointers themselves
2187 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002188typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002189union Ptrlist
2190{
2191 Ptrlist *next;
2192 nfa_state_T *s;
2193};
2194
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002195struct Frag
2196{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002197 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198 Ptrlist *out;
2199};
2200typedef struct Frag Frag_T;
2201
2202static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2203static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2204static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2205static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2206static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2207static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2208
2209/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002210 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002211 */
2212 static Frag_T
2213frag(start, out)
2214 nfa_state_T *start;
2215 Ptrlist *out;
2216{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002217 Frag_T n;
2218
2219 n.start = start;
2220 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221 return n;
2222}
2223
2224/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002225 * Create singleton list containing just outp.
2226 */
2227 static Ptrlist *
2228list1(outp)
2229 nfa_state_T **outp;
2230{
2231 Ptrlist *l;
2232
2233 l = (Ptrlist *)outp;
2234 l->next = NULL;
2235 return l;
2236}
2237
2238/*
2239 * Patch the list of states at out to point to start.
2240 */
2241 static void
2242patch(l, s)
2243 Ptrlist *l;
2244 nfa_state_T *s;
2245{
2246 Ptrlist *next;
2247
2248 for (; l; l = next)
2249 {
2250 next = l->next;
2251 l->s = s;
2252 }
2253}
2254
2255
2256/*
2257 * Join the two lists l1 and l2, returning the combination.
2258 */
2259 static Ptrlist *
2260append(l1, l2)
2261 Ptrlist *l1;
2262 Ptrlist *l2;
2263{
2264 Ptrlist *oldl1;
2265
2266 oldl1 = l1;
2267 while (l1->next)
2268 l1 = l1->next;
2269 l1->next = l2;
2270 return oldl1;
2271}
2272
2273/*
2274 * Stack used for transforming postfix form into NFA.
2275 */
2276static Frag_T empty;
2277
2278 static void
2279st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002280 int *postfix UNUSED;
2281 int *end UNUSED;
2282 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002284#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 FILE *df;
2286 int *p2;
2287
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002288 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 if (df)
2290 {
2291 fprintf(df, "Error popping the stack!\n");
2292#ifdef DEBUG
2293 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2294#endif
2295 fprintf(df, "Postfix form is: ");
2296#ifdef DEBUG
2297 for (p2 = postfix; p2 < end; p2++)
2298 {
2299 nfa_set_code(*p2);
2300 fprintf(df, "%s, ", code);
2301 }
2302 nfa_set_code(*p);
2303 fprintf(df, "\nCurrent position is: ");
2304 for (p2 = postfix; p2 <= p; p2 ++)
2305 {
2306 nfa_set_code(*p2);
2307 fprintf(df, "%s, ", code);
2308 }
2309#else
2310 for (p2 = postfix; p2 < end; p2++)
2311 {
2312 fprintf(df, "%d, ", *p2);
2313 }
2314 fprintf(df, "\nCurrent position is: ");
2315 for (p2 = postfix; p2 <= p; p2 ++)
2316 {
2317 fprintf(df, "%d, ", *p2);
2318 }
2319#endif
2320 fprintf(df, "\n--------------------------\n");
2321 fclose(df);
2322 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002323#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324 EMSG(_("E874: (NFA) Could not pop the stack !"));
2325}
2326
2327/*
2328 * Push an item onto the stack.
2329 */
2330 static void
2331st_push(s, p, stack_end)
2332 Frag_T s;
2333 Frag_T **p;
2334 Frag_T *stack_end;
2335{
2336 Frag_T *stackp = *p;
2337
2338 if (stackp >= stack_end)
2339 return;
2340 *stackp = s;
2341 *p = *p + 1;
2342}
2343
2344/*
2345 * Pop an item from the stack.
2346 */
2347 static Frag_T
2348st_pop(p, stack)
2349 Frag_T **p;
2350 Frag_T *stack;
2351{
2352 Frag_T *stackp;
2353
2354 *p = *p - 1;
2355 stackp = *p;
2356 if (stackp < stack)
2357 return empty;
2358 return **p;
2359}
2360
2361/*
2362 * Convert a postfix form into its equivalent NFA.
2363 * Return the NFA start state on success, NULL otherwise.
2364 */
2365 static nfa_state_T *
2366post2nfa(postfix, end, nfa_calc_size)
2367 int *postfix;
2368 int *end;
2369 int nfa_calc_size;
2370{
2371 int *p;
2372 int mopen;
2373 int mclose;
2374 Frag_T *stack = NULL;
2375 Frag_T *stackp = NULL;
2376 Frag_T *stack_end = NULL;
2377 Frag_T e1;
2378 Frag_T e2;
2379 Frag_T e;
2380 nfa_state_T *s;
2381 nfa_state_T *s1;
2382 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002383 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384
2385 if (postfix == NULL)
2386 return NULL;
2387
Bram Moolenaar053bb602013-05-20 13:55:21 +02002388#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389#define POP() st_pop(&stackp, stack); \
2390 if (stackp < stack) \
2391 { \
2392 st_error(postfix, end, p); \
2393 return NULL; \
2394 }
2395
2396 if (nfa_calc_size == FALSE)
2397 {
2398 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002399 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002400 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002401 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002402 }
2403
2404 for (p = postfix; p < end; ++p)
2405 {
2406 switch (*p)
2407 {
2408 case NFA_CONCAT:
2409 /* Catenation.
2410 * Pay attention: this operator does not exist
2411 * in the r.e. itself (it is implicit, really).
2412 * It is added when r.e. is translated to postfix
2413 * form in re2post().
2414 *
2415 * No new state added here. */
2416 if (nfa_calc_size == TRUE)
2417 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002418 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002419 break;
2420 }
2421 e2 = POP();
2422 e1 = POP();
2423 patch(e1.out, e2.start);
2424 PUSH(frag(e1.start, e2.out));
2425 break;
2426
2427 case NFA_NOT:
2428 /* Negation of a character */
2429 if (nfa_calc_size == TRUE)
2430 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002431 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432 break;
2433 }
2434 e1 = POP();
2435 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002436#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002437 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002438 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002439#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440 PUSH(e1);
2441 break;
2442
2443 case NFA_OR:
2444 /* Alternation */
2445 if (nfa_calc_size == TRUE)
2446 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002447 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 break;
2449 }
2450 e2 = POP();
2451 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002452 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002454 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002455 PUSH(frag(s, append(e1.out, e2.out)));
2456 break;
2457
2458 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002459 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002460 if (nfa_calc_size == TRUE)
2461 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002462 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 break;
2464 }
2465 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002466 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002468 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 patch(e.out, s);
2470 PUSH(frag(s, list1(&s->out1)));
2471 break;
2472
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002473 case NFA_STAR_NONGREEDY:
2474 /* Zero or more, prefer zero */
2475 if (nfa_calc_size == TRUE)
2476 {
2477 nstate++;
2478 break;
2479 }
2480 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002481 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002482 if (s == NULL)
2483 goto theend;
2484 patch(e.out, s);
2485 PUSH(frag(s, list1(&s->out)));
2486 break;
2487
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002488 case NFA_QUEST:
2489 /* one or zero atoms=> greedy match */
2490 if (nfa_calc_size == TRUE)
2491 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002492 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002493 break;
2494 }
2495 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002496 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002497 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002498 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 PUSH(frag(s, append(e.out, list1(&s->out1))));
2500 break;
2501
2502 case NFA_QUEST_NONGREEDY:
2503 /* zero or one atoms => non-greedy match */
2504 if (nfa_calc_size == TRUE)
2505 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002506 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002507 break;
2508 }
2509 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002510 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002511 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002512 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002513 PUSH(frag(s, append(e.out, list1(&s->out))));
2514 break;
2515
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516 case NFA_SKIP_CHAR:
2517 /* Symbol of 0-length, Used in a repetition
2518 * with max/min count of 0 */
2519 if (nfa_calc_size == TRUE)
2520 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002521 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 break;
2523 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002524 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002525 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002526 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002527 PUSH(frag(s, list1(&s->out)));
2528 break;
2529
2530 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002531 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002532 case NFA_PREV_ATOM_JUST_BEFORE:
2533 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002534 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002535 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002536 * The \@<= operator: match for the preceding atom.
2537 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002539 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002540
2541 if (nfa_calc_size == TRUE)
2542 {
2543 nstate += 2;
2544 break;
2545 }
2546 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002547 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002549 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550 patch(e.out, s1);
2551
Bram Moolenaar525666f2013-06-02 16:40:55 +02002552 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002553 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002554 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002555 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2556 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002557 {
2558 s->negated = TRUE;
2559 s1->negated = TRUE;
2560 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002561 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2562 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2563 {
2564 s->val = *++p; /* get the count */
2565 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2566 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002567
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002568 PUSH(frag(s, list1(&s1->out)));
2569 break;
2570
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002571#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002572 case NFA_COMPOSING: /* char with composing char */
2573#if 0
2574 /* TODO */
2575 if (regflags & RF_ICOMBINE)
2576 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002577 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002578 }
2579#endif
2580 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002581#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002582
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002583 case NFA_MOPEN: /* \( \) Submatch */
2584 case NFA_MOPEN1:
2585 case NFA_MOPEN2:
2586 case NFA_MOPEN3:
2587 case NFA_MOPEN4:
2588 case NFA_MOPEN5:
2589 case NFA_MOPEN6:
2590 case NFA_MOPEN7:
2591 case NFA_MOPEN8:
2592 case NFA_MOPEN9:
2593#ifdef FEAT_SYN_HL
2594 case NFA_ZOPEN: /* \z( \) Submatch */
2595 case NFA_ZOPEN1:
2596 case NFA_ZOPEN2:
2597 case NFA_ZOPEN3:
2598 case NFA_ZOPEN4:
2599 case NFA_ZOPEN5:
2600 case NFA_ZOPEN6:
2601 case NFA_ZOPEN7:
2602 case NFA_ZOPEN8:
2603 case NFA_ZOPEN9:
2604#endif
2605 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002606 if (nfa_calc_size == TRUE)
2607 {
2608 nstate += 2;
2609 break;
2610 }
2611
2612 mopen = *p;
2613 switch (*p)
2614 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002615 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2616#ifdef FEAT_SYN_HL
2617 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2618 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2619 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2620 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2621 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2622 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2623 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2624 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2625 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2626 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2627#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002628#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002629 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002630#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002631 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002632 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633 mclose = *p + NSUBEXP;
2634 break;
2635 }
2636
2637 /* Allow "NFA_MOPEN" as a valid postfix representation for
2638 * the empty regexp "". In this case, the NFA will be
2639 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2640 * empty groups of parenthesis, and empty mbyte chars */
2641 if (stackp == stack)
2642 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002643 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002644 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002645 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002646 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002648 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649 patch(list1(&s->out), s1);
2650 PUSH(frag(s, list1(&s1->out)));
2651 break;
2652 }
2653
2654 /* At least one node was emitted before NFA_MOPEN, so
2655 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2656 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002657 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002659 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002660
Bram Moolenaar525666f2013-06-02 16:40:55 +02002661 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002663 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002664 patch(e.out, s1);
2665
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002666#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002667 if (mopen == NFA_COMPOSING)
2668 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002669 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002670#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002671
2672 PUSH(frag(s, list1(&s1->out)));
2673 break;
2674
Bram Moolenaar5714b802013-05-28 22:03:20 +02002675 case NFA_BACKREF1:
2676 case NFA_BACKREF2:
2677 case NFA_BACKREF3:
2678 case NFA_BACKREF4:
2679 case NFA_BACKREF5:
2680 case NFA_BACKREF6:
2681 case NFA_BACKREF7:
2682 case NFA_BACKREF8:
2683 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002684#ifdef FEAT_SYN_HL
2685 case NFA_ZREF1:
2686 case NFA_ZREF2:
2687 case NFA_ZREF3:
2688 case NFA_ZREF4:
2689 case NFA_ZREF5:
2690 case NFA_ZREF6:
2691 case NFA_ZREF7:
2692 case NFA_ZREF8:
2693 case NFA_ZREF9:
2694#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002695 if (nfa_calc_size == TRUE)
2696 {
2697 nstate += 2;
2698 break;
2699 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002700 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002701 if (s == NULL)
2702 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002703 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002704 if (s1 == NULL)
2705 goto theend;
2706 patch(list1(&s->out), s1);
2707 PUSH(frag(s, list1(&s1->out)));
2708 break;
2709
Bram Moolenaar423532e2013-05-29 21:14:42 +02002710 case NFA_LNUM:
2711 case NFA_LNUM_GT:
2712 case NFA_LNUM_LT:
2713 case NFA_VCOL:
2714 case NFA_VCOL_GT:
2715 case NFA_VCOL_LT:
2716 case NFA_COL:
2717 case NFA_COL_GT:
2718 case NFA_COL_LT:
2719 if (nfa_calc_size == TRUE)
2720 {
2721 nstate += 1;
2722 break;
2723 }
2724 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002725 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002726 if (s == NULL)
2727 goto theend;
2728 s->val = e1.start->c;
2729 PUSH(frag(s, list1(&s->out)));
2730 break;
2731
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732 case NFA_ZSTART:
2733 case NFA_ZEND:
2734 default:
2735 /* Operands */
2736 if (nfa_calc_size == TRUE)
2737 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002738 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002739 break;
2740 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002741 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002743 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 PUSH(frag(s, list1(&s->out)));
2745 break;
2746
2747 } /* switch(*p) */
2748
2749 } /* for(p = postfix; *p; ++p) */
2750
2751 if (nfa_calc_size == TRUE)
2752 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002753 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002754 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755 }
2756
2757 e = POP();
2758 if (stackp != stack)
2759 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2760
2761 if (istate >= nstate)
2762 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2763
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002764 matchstate = &state_ptr[istate++]; /* the match state */
2765 matchstate->c = NFA_MATCH;
2766 matchstate->out = matchstate->out1 = NULL;
2767
2768 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002769 ret = e.start;
2770
2771theend:
2772 vim_free(stack);
2773 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002774
2775#undef POP1
2776#undef PUSH1
2777#undef POP2
2778#undef PUSH2
2779#undef POP
2780#undef PUSH
2781}
2782
2783/****************************************************************
2784 * NFA execution code.
2785 ****************************************************************/
2786
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002787typedef struct
2788{
2789 int in_use; /* number of subexpr with useful info */
2790
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002791 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002792 union
2793 {
2794 struct multipos
2795 {
2796 lpos_T start;
2797 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002798 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002799 struct linepos
2800 {
2801 char_u *start;
2802 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002803 } line[NSUBEXP];
2804 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002805} regsub_T;
2806
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002807typedef struct
2808{
2809 regsub_T norm; /* \( .. \) matches */
2810#ifdef FEAT_SYN_HL
2811 regsub_T synt; /* \z( .. \) matches */
2812#endif
2813} regsubs_T;
2814
Bram Moolenaar963fee22013-05-26 21:47:28 +02002815/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002816typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817{
2818 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002819 int count;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002820 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002821} nfa_thread_T;
2822
Bram Moolenaar963fee22013-05-26 21:47:28 +02002823/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824typedef struct
2825{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002826 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002827 int n; /* nr of states currently in "t" */
2828 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002829 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002830} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831
Bram Moolenaar5714b802013-05-28 22:03:20 +02002832#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002833static void log_subsexpr __ARGS((regsubs_T *subs));
2834static void log_subexpr __ARGS((regsub_T *sub));
2835
2836 static void
2837log_subsexpr(subs)
2838 regsubs_T *subs;
2839{
2840 log_subexpr(&subs->norm);
2841# ifdef FEAT_SYN_HL
2842 log_subexpr(&subs->synt);
2843# endif
2844}
2845
Bram Moolenaar5714b802013-05-28 22:03:20 +02002846 static void
2847log_subexpr(sub)
2848 regsub_T *sub;
2849{
2850 int j;
2851
2852 for (j = 0; j < sub->in_use; j++)
2853 if (REG_MULTI)
2854 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2855 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002856 sub->list.multi[j].start.col,
2857 (int)sub->list.multi[j].start.lnum,
2858 sub->list.multi[j].end.col,
2859 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002860 else
2861 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2862 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002863 (char *)sub->list.line[j].start,
2864 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002865 fprintf(log_fd, "\n");
2866}
2867#endif
2868
Bram Moolenaar963fee22013-05-26 21:47:28 +02002869/* Used during execution: whether a match has been found. */
2870static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002871
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002872static void clear_sub __ARGS((regsub_T *sub));
2873static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2874static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002875static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002876static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
2877static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int *ip));
2878
2879 static void
2880clear_sub(sub)
2881 regsub_T *sub;
2882{
2883 if (REG_MULTI)
2884 /* Use 0xff to set lnum to -1 */
2885 vim_memset(sub->list.multi, 0xff,
2886 sizeof(struct multipos) * nfa_nsubexpr);
2887 else
2888 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2889 sub->in_use = 0;
2890}
2891
2892/*
2893 * Copy the submatches from "from" to "to".
2894 */
2895 static void
2896copy_sub(to, from)
2897 regsub_T *to;
2898 regsub_T *from;
2899{
2900 to->in_use = from->in_use;
2901 if (from->in_use > 0)
2902 {
2903 /* Copy the match start and end positions. */
2904 if (REG_MULTI)
2905 mch_memmove(&to->list.multi[0],
2906 &from->list.multi[0],
2907 sizeof(struct multipos) * from->in_use);
2908 else
2909 mch_memmove(&to->list.line[0],
2910 &from->list.line[0],
2911 sizeof(struct linepos) * from->in_use);
2912 }
2913}
2914
2915/*
2916 * Like copy_sub() but exclude the main match.
2917 */
2918 static void
2919copy_sub_off(to, from)
2920 regsub_T *to;
2921 regsub_T *from;
2922{
2923 if (to->in_use < from->in_use)
2924 to->in_use = from->in_use;
2925 if (from->in_use > 1)
2926 {
2927 /* Copy the match start and end positions. */
2928 if (REG_MULTI)
2929 mch_memmove(&to->list.multi[1],
2930 &from->list.multi[1],
2931 sizeof(struct multipos) * (from->in_use - 1));
2932 else
2933 mch_memmove(&to->list.line[1],
2934 &from->list.line[1],
2935 sizeof(struct linepos) * (from->in_use - 1));
2936 }
2937}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938
Bram Moolenaar428e9872013-05-30 17:05:39 +02002939/*
2940 * Return TRUE if "sub1" and "sub2" have the same positions.
2941 */
2942 static int
2943sub_equal(sub1, sub2)
2944 regsub_T *sub1;
2945 regsub_T *sub2;
2946{
2947 int i;
2948 int todo;
2949 linenr_T s1, e1;
2950 linenr_T s2, e2;
2951 char_u *sp1, *ep1;
2952 char_u *sp2, *ep2;
2953
2954 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2955 if (REG_MULTI)
2956 {
2957 for (i = 0; i < todo; ++i)
2958 {
2959 if (i < sub1->in_use)
2960 {
2961 s1 = sub1->list.multi[i].start.lnum;
2962 e1 = sub1->list.multi[i].end.lnum;
2963 }
2964 else
2965 {
2966 s1 = 0;
2967 e1 = 0;
2968 }
2969 if (i < sub2->in_use)
2970 {
2971 s2 = sub2->list.multi[i].start.lnum;
2972 e2 = sub2->list.multi[i].end.lnum;
2973 }
2974 else
2975 {
2976 s2 = 0;
2977 e2 = 0;
2978 }
2979 if (s1 != s2 || e1 != e2)
2980 return FALSE;
2981 if (s1 != 0 && sub1->list.multi[i].start.col
2982 != sub2->list.multi[i].start.col)
2983 return FALSE;
2984 if (e1 != 0 && sub1->list.multi[i].end.col
2985 != sub2->list.multi[i].end.col)
2986 return FALSE;
2987 }
2988 }
2989 else
2990 {
2991 for (i = 0; i < todo; ++i)
2992 {
2993 if (i < sub1->in_use)
2994 {
2995 sp1 = sub1->list.line[i].start;
2996 ep1 = sub1->list.line[i].end;
2997 }
2998 else
2999 {
3000 sp1 = NULL;
3001 ep1 = NULL;
3002 }
3003 if (i < sub2->in_use)
3004 {
3005 sp2 = sub2->list.line[i].start;
3006 ep2 = sub2->list.line[i].end;
3007 }
3008 else
3009 {
3010 sp2 = NULL;
3011 ep2 = NULL;
3012 }
3013 if (sp1 != sp2 || ep1 != ep2)
3014 return FALSE;
3015 }
3016 }
3017
3018 return TRUE;
3019}
3020
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003021#ifdef ENABLE_LOG
3022 static void
3023report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid);
3024{
3025 int col;
3026
3027 if (sub->in_use <= 0)
3028 col = -1;
3029 else if (REG_MULTI)
3030 col = sub->list.multi[0].start.col;
3031 else
3032 col = (int)(sub->list.line[0].start - regline);
3033 nfa_set_code(state->c);
3034 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3035 action, abs(state->id), lid, state->c, code, col);
3036}
3037#endif
3038
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003039 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003040addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003041 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003042 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003043 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003044 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003045{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003046 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003047 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003048 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003049 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003050 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003051 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003052 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003053#ifdef ENABLE_LOG
3054 int did_print = FALSE;
3055#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003056
3057 if (l == NULL || state == NULL)
3058 return;
3059
3060 switch (state->c)
3061 {
3062 case NFA_SPLIT:
3063 case NFA_NOT:
3064 case NFA_NOPEN:
3065 case NFA_NCLOSE:
3066 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003067 case NFA_MCLOSE1:
3068 case NFA_MCLOSE2:
3069 case NFA_MCLOSE3:
3070 case NFA_MCLOSE4:
3071 case NFA_MCLOSE5:
3072 case NFA_MCLOSE6:
3073 case NFA_MCLOSE7:
3074 case NFA_MCLOSE8:
3075 case NFA_MCLOSE9:
3076#ifdef FEAT_SYN_HL
3077 case NFA_ZCLOSE:
3078 case NFA_ZCLOSE1:
3079 case NFA_ZCLOSE2:
3080 case NFA_ZCLOSE3:
3081 case NFA_ZCLOSE4:
3082 case NFA_ZCLOSE5:
3083 case NFA_ZCLOSE6:
3084 case NFA_ZCLOSE7:
3085 case NFA_ZCLOSE8:
3086 case NFA_ZCLOSE9:
3087#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003088 /* These nodes are not added themselves but their "out" and/or
3089 * "out1" may be added below. */
3090 break;
3091
3092 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003093 case NFA_MOPEN1:
3094 case NFA_MOPEN2:
3095 case NFA_MOPEN3:
3096 case NFA_MOPEN4:
3097 case NFA_MOPEN5:
3098 case NFA_MOPEN6:
3099 case NFA_MOPEN7:
3100 case NFA_MOPEN8:
3101 case NFA_MOPEN9:
3102#ifdef FEAT_SYN_HL
3103 case NFA_ZOPEN:
3104 case NFA_ZOPEN1:
3105 case NFA_ZOPEN2:
3106 case NFA_ZOPEN3:
3107 case NFA_ZOPEN4:
3108 case NFA_ZOPEN5:
3109 case NFA_ZOPEN6:
3110 case NFA_ZOPEN7:
3111 case NFA_ZOPEN8:
3112 case NFA_ZOPEN9:
3113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003114 /* These nodes do not need to be added, but we need to bail out
3115 * when it was tried to be added to this list before. */
3116 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003117 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003118 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003119 break;
3120
Bram Moolenaar307aa162013-06-02 16:34:21 +02003121 case NFA_BOL:
3122 case NFA_BOF:
3123 /* "^" won't match past end-of-line, don't bother trying.
3124 * Except when we are going to the next line for a look-behind
3125 * match. */
3126 if (reginput > regline
3127 && (nfa_endp == NULL
3128 || !REG_MULTI
3129 || reglnum == nfa_endp->se_u.pos.lnum))
3130 goto skip_add;
3131 /* FALLTHROUGH */
3132
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003133 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003134 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003135 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003136 /* This state is already in the list, don't add it again,
3137 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003138 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003139 {
3140skip_add:
3141#ifdef ENABLE_LOG
3142 nfa_set_code(state->c);
3143 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3144 abs(state->id), l->id, state->c, code);
3145#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003146 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003147 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003148
3149 /* See if the same state is already in the list with the same
3150 * positions. */
3151 for (i = 0; i < l->n; ++i)
3152 {
3153 thread = &l->t[i];
3154 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003155 && sub_equal(&thread->subs.norm, &subs->norm)
3156#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003157 && (!nfa_has_zsubexpr ||
3158 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003159#endif
3160 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003161 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003162 }
3163 }
3164
3165 /* when there are backreferences the number of states may be (a
3166 * lot) bigger */
3167 if (nfa_has_backref && l->n == l->len)
3168 {
3169 int newlen = l->len * 3 / 2 + 50;
3170
3171 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3172 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003173 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003174
3175 /* add the state to the list */
3176 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003177 thread = &l->t[l->n++];
3178 thread->state = state;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003179 copy_sub(&thread->subs.norm, &subs->norm);
3180#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003181 if (nfa_has_zsubexpr)
3182 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003183#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003184#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003185 report_state("Adding", &thread->subs.norm, state, l->id);
3186 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003187#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003188 }
3189
3190#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003191 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003192 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193#endif
3194 switch (state->c)
3195 {
3196 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003197 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003198 break;
3199
3200 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003201 /* order matters here */
3202 addstate(l, state->out, subs, off);
3203 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 break;
3205
Bram Moolenaar5714b802013-05-28 22:03:20 +02003206 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 case NFA_NOPEN:
3208 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003209 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003210 break;
3211
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003212 case NFA_MOPEN:
3213 case NFA_MOPEN1:
3214 case NFA_MOPEN2:
3215 case NFA_MOPEN3:
3216 case NFA_MOPEN4:
3217 case NFA_MOPEN5:
3218 case NFA_MOPEN6:
3219 case NFA_MOPEN7:
3220 case NFA_MOPEN8:
3221 case NFA_MOPEN9:
3222#ifdef FEAT_SYN_HL
3223 case NFA_ZOPEN:
3224 case NFA_ZOPEN1:
3225 case NFA_ZOPEN2:
3226 case NFA_ZOPEN3:
3227 case NFA_ZOPEN4:
3228 case NFA_ZOPEN5:
3229 case NFA_ZOPEN6:
3230 case NFA_ZOPEN7:
3231 case NFA_ZOPEN8:
3232 case NFA_ZOPEN9:
3233#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003235 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003236 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003238 sub = &subs->norm;
3239 }
3240#ifdef FEAT_SYN_HL
3241 else if (state->c >= NFA_ZOPEN)
3242 {
3243 subidx = state->c - NFA_ZOPEN;
3244 sub = &subs->synt;
3245 }
3246#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003247 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003248 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003249 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003250 sub = &subs->norm;
3251 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003253 /* Set the position (with "off") in the subexpression. Save and
3254 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003255 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 if (REG_MULTI)
3257 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003258 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003259 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003260 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003261 save_in_use = -1;
3262 }
3263 else
3264 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003265 save_in_use = sub->in_use;
3266 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003267 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003268 sub->list.multi[i].start.lnum = -1;
3269 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003270 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003271 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003272 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003273 if (off == -1)
3274 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003275 sub->list.multi[subidx].start.lnum = reglnum + 1;
3276 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003277 }
3278 else
3279 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003280 sub->list.multi[subidx].start.lnum = reglnum;
3281 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003282 (colnr_T)(reginput - regline + off);
3283 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 }
3285 else
3286 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003287 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003288 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003289 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003290 save_in_use = -1;
3291 }
3292 else
3293 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003294 save_in_use = sub->in_use;
3295 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003296 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003297 sub->list.line[i].start = NULL;
3298 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003299 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003300 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003301 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003302 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003303 }
3304
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003305 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003307 if (save_in_use == -1)
3308 {
3309 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003310 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003311 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003312 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003313 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003315 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 break;
3317
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003318 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003319 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003321 /* Do not overwrite the position set by \ze. If no \ze
3322 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003323 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003324 break;
3325 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003326 case NFA_MCLOSE1:
3327 case NFA_MCLOSE2:
3328 case NFA_MCLOSE3:
3329 case NFA_MCLOSE4:
3330 case NFA_MCLOSE5:
3331 case NFA_MCLOSE6:
3332 case NFA_MCLOSE7:
3333 case NFA_MCLOSE8:
3334 case NFA_MCLOSE9:
3335#ifdef FEAT_SYN_HL
3336 case NFA_ZCLOSE:
3337 case NFA_ZCLOSE1:
3338 case NFA_ZCLOSE2:
3339 case NFA_ZCLOSE3:
3340 case NFA_ZCLOSE4:
3341 case NFA_ZCLOSE5:
3342 case NFA_ZCLOSE6:
3343 case NFA_ZCLOSE7:
3344 case NFA_ZCLOSE8:
3345 case NFA_ZCLOSE9:
3346#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003347 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003349 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003351 sub = &subs->norm;
3352 }
3353#ifdef FEAT_SYN_HL
3354 else if (state->c >= NFA_ZCLOSE)
3355 {
3356 subidx = state->c - NFA_ZCLOSE;
3357 sub = &subs->synt;
3358 }
3359#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003360 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003361 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003362 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003363 sub = &subs->norm;
3364 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003366 /* We don't fill in gaps here, there must have been an MOPEN that
3367 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003368 save_in_use = sub->in_use;
3369 if (sub->in_use <= subidx)
3370 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 if (REG_MULTI)
3372 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003373 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003374 if (off == -1)
3375 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003376 sub->list.multi[subidx].end.lnum = reglnum + 1;
3377 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003378 }
3379 else
3380 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003381 sub->list.multi[subidx].end.lnum = reglnum;
3382 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003383 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003384 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385 }
3386 else
3387 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003388 save_ptr = sub->list.line[subidx].end;
3389 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390 }
3391
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003392 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393
3394 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003395 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003396 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003397 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003398 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003399 break;
3400 }
3401}
3402
3403/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003404 * Like addstate(), but the new state(s) are put at position "*ip".
3405 * Used for zero-width matches, next state to use is the added one.
3406 * This makes sure the order of states to be tried does not change, which
3407 * matters for alternatives.
3408 */
3409 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003410addstate_here(l, state, subs, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003411 nfa_list_T *l; /* runtime state list */
3412 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003413 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003414 int *ip;
3415{
3416 int tlen = l->n;
3417 int count;
3418 int i = *ip;
3419
3420 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003421 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003422
3423 /* when "*ip" was at the end of the list, nothing to do */
3424 if (i + 1 == tlen)
3425 return;
3426
3427 /* re-order to put the new state at the current position */
3428 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003429 if (count == 1)
3430 {
3431 /* overwrite the current state */
3432 l->t[i] = l->t[l->n - 1];
3433 }
3434 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003435 {
3436 /* make space for new states, then move them from the
3437 * end to the current position */
3438 mch_memmove(&(l->t[i + count]),
3439 &(l->t[i + 1]),
3440 sizeof(nfa_thread_T) * (l->n - i - 1));
3441 mch_memmove(&(l->t[i]),
3442 &(l->t[l->n - 1]),
3443 sizeof(nfa_thread_T) * count);
3444 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003445 --l->n;
3446 *ip = i - 1;
3447}
3448
3449/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003450 * Check character class "class" against current character c.
3451 */
3452 static int
3453check_char_class(class, c)
3454 int class;
3455 int c;
3456{
3457 switch (class)
3458 {
3459 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003460 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 return OK;
3462 break;
3463 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003464 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 return OK;
3466 break;
3467 case NFA_CLASS_BLANK:
3468 if (c == ' ' || c == '\t')
3469 return OK;
3470 break;
3471 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003472 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003473 return OK;
3474 break;
3475 case NFA_CLASS_DIGIT:
3476 if (VIM_ISDIGIT(c))
3477 return OK;
3478 break;
3479 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003480 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481 return OK;
3482 break;
3483 case NFA_CLASS_LOWER:
3484 if (MB_ISLOWER(c))
3485 return OK;
3486 break;
3487 case NFA_CLASS_PRINT:
3488 if (vim_isprintc(c))
3489 return OK;
3490 break;
3491 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003492 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 return OK;
3494 break;
3495 case NFA_CLASS_SPACE:
3496 if ((c >=9 && c <= 13) || (c == ' '))
3497 return OK;
3498 break;
3499 case NFA_CLASS_UPPER:
3500 if (MB_ISUPPER(c))
3501 return OK;
3502 break;
3503 case NFA_CLASS_XDIGIT:
3504 if (vim_isxdigit(c))
3505 return OK;
3506 break;
3507 case NFA_CLASS_TAB:
3508 if (c == '\t')
3509 return OK;
3510 break;
3511 case NFA_CLASS_RETURN:
3512 if (c == '\r')
3513 return OK;
3514 break;
3515 case NFA_CLASS_BACKSPACE:
3516 if (c == '\b')
3517 return OK;
3518 break;
3519 case NFA_CLASS_ESCAPE:
3520 if (c == '\033')
3521 return OK;
3522 break;
3523
3524 default:
3525 /* should not be here :P */
3526 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3527 }
3528 return FAIL;
3529}
3530
Bram Moolenaar5714b802013-05-28 22:03:20 +02003531static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3532
3533/*
3534 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003535 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003536 */
3537 static int
3538match_backref(sub, subidx, bytelen)
3539 regsub_T *sub; /* pointers to subexpressions */
3540 int subidx;
3541 int *bytelen; /* out: length of match in bytes */
3542{
3543 int len;
3544
3545 if (sub->in_use <= subidx)
3546 {
3547retempty:
3548 /* backref was not set, match an empty string */
3549 *bytelen = 0;
3550 return TRUE;
3551 }
3552
3553 if (REG_MULTI)
3554 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003555 if (sub->list.multi[subidx].start.lnum < 0
3556 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003557 goto retempty;
3558 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003559 len = sub->list.multi[subidx].end.col
3560 - sub->list.multi[subidx].start.col;
3561 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003562 reginput, &len) == 0)
3563 {
3564 *bytelen = len;
3565 return TRUE;
3566 }
3567 }
3568 else
3569 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003570 if (sub->list.line[subidx].start == NULL
3571 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003572 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003573 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3574 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003575 {
3576 *bytelen = len;
3577 return TRUE;
3578 }
3579 }
3580 return FALSE;
3581}
3582
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003583#ifdef FEAT_SYN_HL
3584
3585static int match_zref __ARGS((int subidx, int *bytelen));
3586
3587/*
3588 * Check for a match with \z subexpression "subidx".
3589 * Return TRUE if it matches.
3590 */
3591 static int
3592match_zref(subidx, bytelen)
3593 int subidx;
3594 int *bytelen; /* out: length of match in bytes */
3595{
3596 int len;
3597
3598 cleanup_zsubexpr();
3599 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3600 {
3601 /* backref was not set, match an empty string */
3602 *bytelen = 0;
3603 return TRUE;
3604 }
3605
3606 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3607 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3608 {
3609 *bytelen = len;
3610 return TRUE;
3611 }
3612 return FALSE;
3613}
3614#endif
3615
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003617 * Save list IDs for all NFA states of "prog" into "list".
3618 * Also reset the IDs to zero.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 */
3620 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003621nfa_save_listids(prog, list)
3622 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 int *list;
3624{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003625 int i;
3626 nfa_state_T *p;
3627
3628 /* Order in the list is reverse, it's a bit faster that way. */
3629 p = &prog->state[0];
3630 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003631 {
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003632 list[i] = p->lastlist;
3633 p->lastlist = 0;
3634 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635 }
3636}
3637
3638/*
3639 * Restore list IDs from "list" to all NFA states.
3640 */
3641 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003642nfa_restore_listids(prog, list)
3643 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 int *list;
3645{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003646 int i;
3647 nfa_state_T *p;
3648
3649 p = &prog->state[0];
3650 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651 {
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003652 p->lastlist = list[i];
3653 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003654 }
3655}
3656
Bram Moolenaar423532e2013-05-29 21:14:42 +02003657 static int
3658nfa_re_num_cmp(val, op, pos)
3659 long_u val;
3660 int op;
3661 long_u pos;
3662{
3663 if (op == 1) return pos > val;
3664 if (op == 2) return pos < val;
3665 return val == pos;
3666}
3667
Bram Moolenaarf46da702013-06-02 22:37:42 +02003668static 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 +02003669static 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 +02003670
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003672 * Recursively call nfa_regmatch()
3673 */
3674 static int
3675recursive_regmatch(state, prog, submatch, m, listids)
3676 nfa_state_T *state;
3677 nfa_regprog_T *prog;
3678 regsubs_T *submatch;
3679 regsubs_T *m;
3680 int **listids;
3681{
3682 char_u *save_reginput = reginput;
3683 char_u *save_regline = regline;
3684 int save_reglnum = reglnum;
3685 int save_nfa_match = nfa_match;
3686 save_se_T *save_nfa_endp = nfa_endp;
3687 save_se_T endpos;
3688 save_se_T *endposp = NULL;
3689 int result;
3690
3691 if (state->c == NFA_START_INVISIBLE_BEFORE)
3692 {
3693 /* The recursive match must end at the current position. */
3694 endposp = &endpos;
3695 if (REG_MULTI)
3696 {
3697 endpos.se_u.pos.col = (int)(reginput - regline);
3698 endpos.se_u.pos.lnum = reglnum;
3699 }
3700 else
3701 endpos.se_u.ptr = reginput;
3702
3703 /* Go back the specified number of bytes, or as far as the
3704 * start of the previous line, to try matching "\@<=" or
3705 * not matching "\@<!".
3706 * TODO: This is very inefficient! Would be better to
3707 * first check for a match with what follows. */
3708 if (state->val <= 0)
3709 {
3710 if (REG_MULTI)
3711 {
3712 regline = reg_getline(--reglnum);
3713 if (regline == NULL)
3714 /* can't go before the first line */
3715 regline = reg_getline(++reglnum);
3716 }
3717 reginput = regline;
3718 }
3719 else
3720 {
3721 if (REG_MULTI && (int)(reginput - regline) < state->val)
3722 {
3723 /* Not enough bytes in this line, go to end of
3724 * previous line. */
3725 regline = reg_getline(--reglnum);
3726 if (regline == NULL)
3727 {
3728 /* can't go before the first line */
3729 regline = reg_getline(++reglnum);
3730 reginput = regline;
3731 }
3732 else
3733 reginput = regline + STRLEN(regline);
3734 }
3735 if ((int)(reginput - regline) >= state->val)
3736 {
3737 reginput -= state->val;
3738#ifdef FEAT_MBYTE
3739 if (has_mbyte)
3740 reginput -= mb_head_off(regline, reginput);
3741#endif
3742 }
3743 else
3744 reginput = regline;
3745 }
3746 }
3747
3748 /* Call nfa_regmatch() to check if the current concat matches
3749 * at this position. The concat ends with the node
3750 * NFA_END_INVISIBLE */
3751 if (*listids == NULL)
3752 {
3753 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3754 if (*listids == NULL)
3755 {
3756 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3757 return 0;
3758 }
3759 }
3760#ifdef ENABLE_LOG
3761 if (log_fd != stderr)
3762 fclose(log_fd);
3763 log_fd = NULL;
3764#endif
3765 /* Have to clear the listid field of the NFA nodes, so that
3766 * nfa_regmatch() and addstate() can run properly after
3767 * recursion. */
3768 nfa_save_listids(prog, *listids);
3769 nfa_endp = endposp;
3770 result = nfa_regmatch(prog, state->out, submatch, m);
3771 nfa_restore_listids(prog, *listids);
3772
3773 /* restore position in input text */
3774 reginput = save_reginput;
3775 regline = save_regline;
3776 reglnum = save_reglnum;
3777 nfa_match = save_nfa_match;
3778 nfa_endp = save_nfa_endp;
3779
3780#ifdef ENABLE_LOG
3781 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3782 if (log_fd != NULL)
3783 {
3784 fprintf(log_fd, "****************************\n");
3785 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3786 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3787 fprintf(log_fd, "****************************\n");
3788 }
3789 else
3790 {
3791 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3792 log_fd = stderr;
3793 }
3794#endif
3795
3796 return result;
3797}
3798
3799/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 * Main matching routine.
3801 *
3802 * Run NFA to determine whether it matches reginput.
3803 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003804 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003805 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003806 * Return TRUE if there is a match, FALSE otherwise.
3807 * Note: Caller must ensure that: start != NULL.
3808 */
3809 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003810nfa_regmatch(prog, start, submatch, m)
3811 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003812 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003813 regsubs_T *submatch;
3814 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003815{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003816 int result;
3817 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003818 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003819 int go_to_nextline = FALSE;
3820 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003821 nfa_list_T list[3];
3822 nfa_list_T *listtbl[2][2];
3823 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003824 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003825 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003826 nfa_list_T *thislist;
3827 nfa_list_T *nextlist;
3828 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003829 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003830#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003831 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003832
3833 if (debug == NULL)
3834 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003835 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836 return FALSE;
3837 }
3838#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003839 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003840
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003841 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003842 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003843 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3844 list[0].len = nstate + 1;
3845 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3846 list[1].len = nstate + 1;
3847 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3848 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3850 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851
3852#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003853 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854 if (log_fd != NULL)
3855 {
3856 fprintf(log_fd, "**********************************\n");
3857 nfa_set_code(start->c);
3858 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3859 abs(start->id), code);
3860 fprintf(log_fd, "**********************************\n");
3861 }
3862 else
3863 {
3864 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3865 log_fd = stderr;
3866 }
3867#endif
3868
3869 thislist = &list[0];
3870 thislist->n = 0;
3871 nextlist = &list[1];
3872 nextlist->n = 0;
3873 neglist = &list[2];
3874 neglist->n = 0;
3875#ifdef ENABLE_LOG
3876 fprintf(log_fd, "(---) STARTSTATE\n");
3877#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003878 thislist->id = listid;
3879 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003880
3881 /* There are two cases when the NFA advances: 1. input char matches the
3882 * NFA node and 2. input char does not match the NFA node, but the next
3883 * node is NFA_NOT. The following macro calls addstate() according to
3884 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3885 listtbl[0][0] = NULL;
3886 listtbl[0][1] = neglist;
3887 listtbl[1][0] = nextlist;
3888 listtbl[1][1] = NULL;
3889#define ADD_POS_NEG_STATE(node) \
3890 ll = listtbl[result ? 1 : 0][node->negated]; \
3891 if (ll != NULL) \
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003892 addstate(ll, node->out , &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003893
3894
3895 /*
3896 * Run for each character.
3897 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003898 for (;;)
3899 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003900 int curc;
3901 int clen;
3902
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003903#ifdef FEAT_MBYTE
3904 if (has_mbyte)
3905 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003906 curc = (*mb_ptr2char)(reginput);
3907 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003908 }
3909 else
3910#endif
3911 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003912 curc = *reginput;
3913 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003914 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003915 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003916 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003917 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003918 go_to_nextline = FALSE;
3919 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003920
3921 /* swap lists */
3922 thislist = &list[flag];
3923 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003924 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003925 listtbl[1][0] = nextlist;
3926 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003927 thislist->id = listid;
3928 nextlist->id = listid + 1;
3929 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003930
3931#ifdef ENABLE_LOG
3932 fprintf(log_fd, "------------------------------------------\n");
3933 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003934 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003935 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003936 {
3937 int i;
3938
3939 for (i = 0; i < thislist->n; i++)
3940 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3941 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003942 fprintf(log_fd, "\n");
3943#endif
3944
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003945#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003946 fprintf(debug, "\n-------------------\n");
3947#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003948 /*
3949 * If the state lists are empty we can stop.
3950 */
3951 if (thislist->n == 0 && neglist->n == 0)
3952 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003953
3954 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003955 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003956 {
3957 if (neglist->n > 0)
3958 {
3959 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003960 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003961 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003962 }
3963 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003964 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003966#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003967 nfa_set_code(t->state->c);
3968 fprintf(debug, "%s, ", code);
3969#endif
3970#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003971 {
3972 int col;
3973
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003974 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003975 col = -1;
3976 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003977 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003978 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003979 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003980 nfa_set_code(t->state->c);
3981 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3982 abs(t->state->id), (int)t->state->c, code, col);
3983 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003984#endif
3985
3986 /*
3987 * Handle the possible codes of the current state.
3988 * The most important is NFA_MATCH.
3989 */
3990 switch (t->state->c)
3991 {
3992 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003993 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003994 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003995 copy_sub(&submatch->norm, &t->subs.norm);
3996#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003997 if (nfa_has_zsubexpr)
3998 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003999#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004000#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004001 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004003 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004004 * states at this position. When the list of states is going
4005 * to be empty quit without advancing, so that "reginput" is
4006 * correct. */
4007 if (nextlist->n == 0 && neglist->n == 0)
4008 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004009 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004010 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004011
4012 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004013 /*
4014 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004015 * NFA_START_INVISIBLE_BEFORE node.
4016 * They surround a zero-width group, used with "\@=", "\&",
4017 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018 * If we got here, it means that the current "invisible" group
4019 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004020 * nfa_regmatch(). For a look-behind match only when it ends
4021 * in the position in "nfa_endp".
4022 * Submatches are stored in *m, and used in the parent call.
4023 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004024#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004025 if (nfa_endp != NULL)
4026 {
4027 if (REG_MULTI)
4028 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4029 (int)reglnum,
4030 (int)nfa_endp->se_u.pos.lnum,
4031 (int)(reginput - regline),
4032 nfa_endp->se_u.pos.col);
4033 else
4034 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4035 (int)(reginput - regline),
4036 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004038#endif
4039 /* It's only a match if it ends at "nfa_endp" */
4040 if (nfa_endp != NULL && (REG_MULTI
4041 ? (reglnum != nfa_endp->se_u.pos.lnum
4042 || (int)(reginput - regline)
4043 != nfa_endp->se_u.pos.col)
4044 : reginput != nfa_endp->se_u.ptr))
4045 break;
4046
4047 /* do not set submatches for \@! */
4048 if (!t->state->negated)
4049 {
4050 copy_sub(&m->norm, &t->subs.norm);
4051#ifdef FEAT_SYN_HL
4052 if (nfa_has_zsubexpr)
4053 copy_sub(&m->synt, &t->subs.synt);
4054#endif
4055 }
4056 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004057 break;
4058
4059 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004060 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004061 result = recursive_regmatch(t->state, prog, submatch, m,
4062 &listids);
Bram Moolenaar61602c52013-06-01 19:54:43 +02004063
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02004064 /* for \@! it is a match when result is FALSE */
4065 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004066 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 /* Copy submatch info from the recursive call */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004068 copy_sub_off(&t->subs.norm, &m->norm);
4069#ifdef FEAT_SYN_HL
4070 copy_sub_off(&t->subs.synt, &m->synt);
4071#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004072
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004073 /* t->state->out1 is the corresponding END_INVISIBLE node;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004074 * Add its out to the current list (zero-width match). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004075 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004076 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004077 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004078 break;
4079
4080 case NFA_BOL:
4081 if (reginput == regline)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004082 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004083 break;
4084
4085 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004086 if (curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004087 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004088 break;
4089
4090 case NFA_BOW:
4091 {
4092 int bow = TRUE;
4093
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004094 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 bow = FALSE;
4096#ifdef FEAT_MBYTE
4097 else if (has_mbyte)
4098 {
4099 int this_class;
4100
4101 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004102 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004103 if (this_class <= 1)
4104 bow = FALSE;
4105 else if (reg_prev_class() == this_class)
4106 bow = FALSE;
4107 }
4108#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004109 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004110 || (reginput > regline
4111 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004112 bow = FALSE;
4113 if (bow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004114 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004115 break;
4116 }
4117
4118 case NFA_EOW:
4119 {
4120 int eow = TRUE;
4121
4122 if (reginput == regline)
4123 eow = FALSE;
4124#ifdef FEAT_MBYTE
4125 else if (has_mbyte)
4126 {
4127 int this_class, prev_class;
4128
4129 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004130 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131 prev_class = reg_prev_class();
4132 if (this_class == prev_class
4133 || prev_class == 0 || prev_class == 1)
4134 eow = FALSE;
4135 }
4136#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004137 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004138 || (reginput[0] != NUL
4139 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004140 eow = FALSE;
4141 if (eow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004142 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004143 break;
4144 }
4145
Bram Moolenaar4b780632013-05-31 22:14:52 +02004146 case NFA_BOF:
4147 if (reglnum == 0 && reginput == regline
4148 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004149 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004150 break;
4151
4152 case NFA_EOF:
4153 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004154 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004155 break;
4156
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004157#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004158 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004159 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004160 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004161 int len = 0;
4162 nfa_state_T *end;
4163 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004164 int cchars[MAX_MCO];
4165 int ccount = 0;
4166 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004169 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004170 if (utf_iscomposing(sta->c))
4171 {
4172 /* Only match composing character(s), ignore base
4173 * character. Used for ".{composing}" and "{composing}"
4174 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004175 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004176 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004177 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004178 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004179 /* If \Z was present, then ignore composing characters.
4180 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004181 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004182 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004183 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004184 else
4185 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004186 while (sta->c != NFA_END_COMPOSING)
4187 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004188 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004189
4190 /* Check base character matches first, unless ignored. */
4191 else if (len > 0 || mc == sta->c)
4192 {
4193 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004194 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004195 len += mb_char2len(mc);
4196 sta = sta->out;
4197 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004198
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004199 /* We don't care about the order of composing characters.
4200 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004201 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004202 {
4203 mc = mb_ptr2char(reginput + len);
4204 cchars[ccount++] = mc;
4205 len += mb_char2len(mc);
4206 if (ccount == MAX_MCO)
4207 break;
4208 }
4209
4210 /* Check that each composing char in the pattern matches a
4211 * composing char in the text. We do not check if all
4212 * composing chars are matched. */
4213 result = OK;
4214 while (sta->c != NFA_END_COMPOSING)
4215 {
4216 for (j = 0; j < ccount; ++j)
4217 if (cchars[j] == sta->c)
4218 break;
4219 if (j == ccount)
4220 {
4221 result = FAIL;
4222 break;
4223 }
4224 sta = sta->out;
4225 }
4226 }
4227 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004228 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004229
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004230 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004231 ADD_POS_NEG_STATE(end);
4232 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004233 }
4234#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235
4236 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004237 if (curc == NUL && !reg_line_lbr && REG_MULTI
4238 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004239 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004240 go_to_nextline = TRUE;
4241 /* Pass -1 for the offset, which means taking the position
4242 * at the start of the next line. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004243 addstate(nextlist, t->state->out, &t->subs, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004244 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004245 else if (curc == '\n' && reg_line_lbr)
4246 {
4247 /* match \n as if it is an ordinary character */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004248 addstate(nextlist, t->state->out, &t->subs, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004249 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004250 break;
4251
4252 case NFA_CLASS_ALNUM:
4253 case NFA_CLASS_ALPHA:
4254 case NFA_CLASS_BLANK:
4255 case NFA_CLASS_CNTRL:
4256 case NFA_CLASS_DIGIT:
4257 case NFA_CLASS_GRAPH:
4258 case NFA_CLASS_LOWER:
4259 case NFA_CLASS_PRINT:
4260 case NFA_CLASS_PUNCT:
4261 case NFA_CLASS_SPACE:
4262 case NFA_CLASS_UPPER:
4263 case NFA_CLASS_XDIGIT:
4264 case NFA_CLASS_TAB:
4265 case NFA_CLASS_RETURN:
4266 case NFA_CLASS_BACKSPACE:
4267 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004268 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 ADD_POS_NEG_STATE(t->state);
4270 break;
4271
4272 case NFA_END_NEG_RANGE:
4273 /* This follows a series of negated nodes, like:
4274 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004275 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004276 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004277 break;
4278
4279 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004280 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004281 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004282 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004283 break;
4284
4285 /*
4286 * Character classes like \a for alpha, \d for digit etc.
4287 */
4288 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004289 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290 ADD_POS_NEG_STATE(t->state);
4291 break;
4292
4293 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004294 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295 ADD_POS_NEG_STATE(t->state);
4296 break;
4297
4298 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004299 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004300 ADD_POS_NEG_STATE(t->state);
4301 break;
4302
4303 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004304 result = !VIM_ISDIGIT(curc)
4305 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306 ADD_POS_NEG_STATE(t->state);
4307 break;
4308
4309 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004310 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004311 ADD_POS_NEG_STATE(t->state);
4312 break;
4313
4314 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004315 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004316 ADD_POS_NEG_STATE(t->state);
4317 break;
4318
4319 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004320 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 ADD_POS_NEG_STATE(t->state);
4322 break;
4323
4324 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004325 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326 ADD_POS_NEG_STATE(t->state);
4327 break;
4328
4329 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004330 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004331 ADD_POS_NEG_STATE(t->state);
4332 break;
4333
4334 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004335 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 ADD_POS_NEG_STATE(t->state);
4337 break;
4338
4339 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004340 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341 ADD_POS_NEG_STATE(t->state);
4342 break;
4343
4344 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004345 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004346 ADD_POS_NEG_STATE(t->state);
4347 break;
4348
4349 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004350 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004351 ADD_POS_NEG_STATE(t->state);
4352 break;
4353
4354 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004355 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356 ADD_POS_NEG_STATE(t->state);
4357 break;
4358
4359 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004360 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361 ADD_POS_NEG_STATE(t->state);
4362 break;
4363
4364 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004365 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004366 ADD_POS_NEG_STATE(t->state);
4367 break;
4368
4369 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004370 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004371 ADD_POS_NEG_STATE(t->state);
4372 break;
4373
4374 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004375 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004376 ADD_POS_NEG_STATE(t->state);
4377 break;
4378
4379 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004380 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381 ADD_POS_NEG_STATE(t->state);
4382 break;
4383
4384 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004385 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004386 ADD_POS_NEG_STATE(t->state);
4387 break;
4388
4389 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004390 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004391 ADD_POS_NEG_STATE(t->state);
4392 break;
4393
4394 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004395 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396 ADD_POS_NEG_STATE(t->state);
4397 break;
4398
4399 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004400 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004401 ADD_POS_NEG_STATE(t->state);
4402 break;
4403
4404 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004405 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004406 ADD_POS_NEG_STATE(t->state);
4407 break;
4408
4409 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004410 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004411 ADD_POS_NEG_STATE(t->state);
4412 break;
4413
4414 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004415 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004416 ADD_POS_NEG_STATE(t->state);
4417 break;
4418
Bram Moolenaar5714b802013-05-28 22:03:20 +02004419 case NFA_BACKREF1:
4420 case NFA_BACKREF2:
4421 case NFA_BACKREF3:
4422 case NFA_BACKREF4:
4423 case NFA_BACKREF5:
4424 case NFA_BACKREF6:
4425 case NFA_BACKREF7:
4426 case NFA_BACKREF8:
4427 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004428#ifdef FEAT_SYN_HL
4429 case NFA_ZREF1:
4430 case NFA_ZREF2:
4431 case NFA_ZREF3:
4432 case NFA_ZREF4:
4433 case NFA_ZREF5:
4434 case NFA_ZREF6:
4435 case NFA_ZREF7:
4436 case NFA_ZREF8:
4437 case NFA_ZREF9:
4438#endif
4439 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004440 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004441 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004442 int bytelen;
4443
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004444 if (t->state->c <= NFA_BACKREF9)
4445 {
4446 subidx = t->state->c - NFA_BACKREF1 + 1;
4447 result = match_backref(&t->subs.norm, subidx, &bytelen);
4448 }
4449#ifdef FEAT_SYN_HL
4450 else
4451 {
4452 subidx = t->state->c - NFA_ZREF1 + 1;
4453 result = match_zref(subidx, &bytelen);
4454 }
4455#endif
4456
Bram Moolenaar5714b802013-05-28 22:03:20 +02004457 if (result)
4458 {
4459 if (bytelen == 0)
4460 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004461 /* empty match always works, output of NFA_SKIP to be
4462 * used next */
4463 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004464 &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004465 }
4466 else if (bytelen <= clen)
4467 {
4468 /* match current character, jump ahead to out of
4469 * NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004470 addstate(nextlist, t->state->out->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004471#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004472 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004473#endif
4474 }
4475 else
4476 {
4477 /* skip ofer the matched characters, set character
4478 * count in NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004479 addstate(nextlist, t->state->out, &t->subs, bytelen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004480 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4481#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004482 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004483#endif
4484 }
4485
4486 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004487 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004488 }
4489 case NFA_SKIP:
4490 /* charater of previous matching \1 .. \9 */
4491 if (t->count - clen <= 0)
4492 {
4493 /* end of match, go to what follows */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004494 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004495#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004496 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004497#endif
4498 }
4499 else
4500 {
4501 /* add state again with decremented count */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004502 addstate(nextlist, t->state, &t->subs, 0);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004503 nextlist->t[nextlist->n - 1].count = t->count - clen;
4504#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004505 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004506#endif
4507 }
4508 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004509
4510 case NFA_SKIP_CHAR:
4511 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004512 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004513 /* TODO: should not happen? */
4514 break;
4515
Bram Moolenaar423532e2013-05-29 21:14:42 +02004516 case NFA_LNUM:
4517 case NFA_LNUM_GT:
4518 case NFA_LNUM_LT:
4519 result = (REG_MULTI &&
4520 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4521 (long_u)(reglnum + reg_firstlnum)));
4522 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004523 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004524 break;
4525
4526 case NFA_COL:
4527 case NFA_COL_GT:
4528 case NFA_COL_LT:
4529 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4530 (long_u)(reginput - regline) + 1);
4531 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004532 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004533 break;
4534
4535 case NFA_VCOL:
4536 case NFA_VCOL_GT:
4537 case NFA_VCOL_LT:
4538 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4539 (long_u)win_linetabsize(
4540 reg_win == NULL ? curwin : reg_win,
4541 regline, (colnr_T)(reginput - regline)) + 1);
4542 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004543 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004544 break;
4545
4546 case NFA_CURSOR:
4547 result = (reg_win != NULL
4548 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4549 && ((colnr_T)(reginput - regline)
4550 == reg_win->w_cursor.col));
4551 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004552 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004553 break;
4554
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004556 {
4557 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004558
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004559 /* TODO: put this in #ifdef later */
4560 if (c < -256)
4561 EMSGN("INTERNAL: Negative state char: %ld", c);
4562 if (is_Magic(c))
4563 c = un_Magic(c);
4564 result = (c == curc);
4565
4566 if (!result && ireg_ic)
4567 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004568#ifdef FEAT_MBYTE
4569 /* If there is a composing character which is not being
4570 * ignored there can be no match. Match with composing
4571 * character uses NFA_COMPOSING above. */
4572 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004573 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004574 result = FALSE;
4575#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004576 ADD_POS_NEG_STATE(t->state);
4577 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004578 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004579 }
4580
4581 } /* for (thislist = thislist; thislist->state; thislist++) */
4582
Bram Moolenaare23febd2013-05-26 18:40:14 +02004583 /* Look for the start of a match in the current position by adding the
4584 * start state to the list of states.
4585 * The first found match is the leftmost one, thus the order of states
4586 * matters!
4587 * Do not add the start state in recursive calls of nfa_regmatch(),
4588 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004589 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004590 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004591 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004592 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004593 && reglnum == 0
4594 && clen != 0
4595 && (ireg_maxcol == 0
4596 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004597 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004598 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004599 ? (reglnum < nfa_endp->se_u.pos.lnum
4600 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004601 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004602 < nfa_endp->se_u.pos.col))
4603 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604 {
4605#ifdef ENABLE_LOG
4606 fprintf(log_fd, "(---) STARTSTATE\n");
4607#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004608 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 }
4610
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611#ifdef ENABLE_LOG
4612 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004613 {
4614 int i;
4615
4616 for (i = 0; i < thislist->n; i++)
4617 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4618 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004619 fprintf(log_fd, "\n");
4620#endif
4621
4622nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004623 /* Advance to the next character, or advance to the next line, or
4624 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004625 if (clen != 0)
4626 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004627 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4628 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004629 reg_nextline();
4630 else
4631 break;
4632 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004633
4634#ifdef ENABLE_LOG
4635 if (log_fd != stderr)
4636 fclose(log_fd);
4637 log_fd = NULL;
4638#endif
4639
4640theend:
4641 /* Free memory */
4642 vim_free(list[0].t);
4643 vim_free(list[1].t);
4644 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004645 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004647#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004648 fclose(debug);
4649#endif
4650
Bram Moolenaar963fee22013-05-26 21:47:28 +02004651 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652}
4653
4654/*
4655 * Try match of "prog" with at regline["col"].
4656 * Returns 0 for failure, number of lines contained in the match otherwise.
4657 */
4658 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004659nfa_regtry(prog, col)
4660 nfa_regprog_T *prog;
4661 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004662{
4663 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004664 regsubs_T subs, m;
4665 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004666#ifdef ENABLE_LOG
4667 FILE *f;
4668#endif
4669
4670 reginput = regline + col;
4671 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004672#ifdef FEAT_SYN_HL
4673 /* Clear the external match subpointers if necessary. */
4674 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004675 {
4676 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004677 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004678 }
4679 else
4680 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004681#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682
4683#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004684 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 if (f != NULL)
4686 {
4687 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4688 fprintf(f, " =======================================================\n");
4689#ifdef DEBUG
4690 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4691#endif
4692 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004693 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004694 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 fprintf(f, "\n\n");
4696 fclose(f);
4697 }
4698 else
4699 EMSG(_("Could not open temporary log file for writing "));
4700#endif
4701
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004702 clear_sub(&subs.norm);
4703 clear_sub(&m.norm);
4704#ifdef FEAT_SYN_HL
4705 clear_sub(&subs.synt);
4706 clear_sub(&m.synt);
4707#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004709 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004710 return 0;
4711
4712 cleanup_subexpr();
4713 if (REG_MULTI)
4714 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004715 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004716 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004717 reg_startpos[i] = subs.norm.list.multi[i].start;
4718 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719 }
4720
4721 if (reg_startpos[0].lnum < 0)
4722 {
4723 reg_startpos[0].lnum = 0;
4724 reg_startpos[0].col = col;
4725 }
4726 if (reg_endpos[0].lnum < 0)
4727 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004728 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004729 reg_endpos[0].lnum = reglnum;
4730 reg_endpos[0].col = (int)(reginput - regline);
4731 }
4732 else
4733 /* Use line number of "\ze". */
4734 reglnum = reg_endpos[0].lnum;
4735 }
4736 else
4737 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004738 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004739 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004740 reg_startp[i] = subs.norm.list.line[i].start;
4741 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004742 }
4743
4744 if (reg_startp[0] == NULL)
4745 reg_startp[0] = regline + col;
4746 if (reg_endp[0] == NULL)
4747 reg_endp[0] = reginput;
4748 }
4749
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004750#ifdef FEAT_SYN_HL
4751 /* Package any found \z(...\) matches for export. Default is none. */
4752 unref_extmatch(re_extmatch_out);
4753 re_extmatch_out = NULL;
4754
4755 if (prog->reghasz == REX_SET)
4756 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004757 cleanup_zsubexpr();
4758 re_extmatch_out = make_extmatch();
4759 for (i = 0; i < subs.synt.in_use; i++)
4760 {
4761 if (REG_MULTI)
4762 {
4763 struct multipos *mpos = &subs.synt.list.multi[i];
4764
4765 /* Only accept single line matches. */
4766 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
4767 re_extmatch_out->matches[i] =
4768 vim_strnsave(reg_getline(mpos->start.lnum)
4769 + mpos->start.col,
4770 mpos->end.col - mpos->start.col);
4771 }
4772 else
4773 {
4774 struct linepos *lpos = &subs.synt.list.line[i];
4775
4776 if (lpos->start != NULL && lpos->end != NULL)
4777 re_extmatch_out->matches[i] =
4778 vim_strnsave(lpos->start,
4779 (int)(lpos->end - lpos->start));
4780 }
4781 }
4782 }
4783#endif
4784
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004785 return 1 + reglnum;
4786}
4787
4788/*
4789 * Match a regexp against a string ("line" points to the string) or multiple
4790 * lines ("line" is NULL, use reg_getline()).
4791 *
4792 * Returns 0 for failure, number of lines contained in the match otherwise.
4793 */
4794 static long
4795nfa_regexec_both(line, col)
4796 char_u *line;
4797 colnr_T col; /* column to start looking for match */
4798{
4799 nfa_regprog_T *prog;
4800 long retval = 0L;
4801 int i;
4802
4803 if (REG_MULTI)
4804 {
4805 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4806 line = reg_getline((linenr_T)0); /* relative to the cursor */
4807 reg_startpos = reg_mmatch->startpos;
4808 reg_endpos = reg_mmatch->endpos;
4809 }
4810 else
4811 {
4812 prog = (nfa_regprog_T *)reg_match->regprog;
4813 reg_startp = reg_match->startp;
4814 reg_endp = reg_match->endp;
4815 }
4816
4817 /* Be paranoid... */
4818 if (prog == NULL || line == NULL)
4819 {
4820 EMSG(_(e_null));
4821 goto theend;
4822 }
4823
4824 /* If the start column is past the maximum column: no need to try. */
4825 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4826 goto theend;
4827
4828 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4829 if (prog->regflags & RF_ICASE)
4830 ireg_ic = TRUE;
4831 else if (prog->regflags & RF_NOICASE)
4832 ireg_ic = FALSE;
4833
4834#ifdef FEAT_MBYTE
4835 /* If pattern contains "\Z" overrule value of ireg_icombine */
4836 if (prog->regflags & RF_ICOMBINE)
4837 ireg_icombine = TRUE;
4838#endif
4839
4840 regline = line;
4841 reglnum = 0; /* relative to line */
4842
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004843 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004844 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004845 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004846#ifdef DEBUG
4847 nfa_regengine.expr = prog->pattern;
4848#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004849
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004850 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004851 for (i = 0; i < nstate; ++i)
4852 {
4853 prog->state[i].id = i;
4854 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004855 }
4856
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004857 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004859#ifdef DEBUG
4860 nfa_regengine.expr = NULL;
4861#endif
4862
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863theend:
4864 return retval;
4865}
4866
4867/*
4868 * Compile a regular expression into internal code for the NFA matcher.
4869 * Returns the program in allocated space. Returns NULL for an error.
4870 */
4871 static regprog_T *
4872nfa_regcomp(expr, re_flags)
4873 char_u *expr;
4874 int re_flags;
4875{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004876 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004877 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878 int *postfix;
4879
4880 if (expr == NULL)
4881 return NULL;
4882
4883#ifdef DEBUG
4884 nfa_regengine.expr = expr;
4885#endif
4886
4887 init_class_tab();
4888
4889 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4890 return NULL;
4891
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004892 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004893 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004894 postfix = re2post();
4895 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004896 {
4897 /* TODO: only give this error for debugging? */
4898 if (post_ptr >= post_end)
4899 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004900 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004901 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004902
4903 /*
4904 * In order to build the NFA, we parse the input regexp twice:
4905 * 1. first pass to count size (so we can allocate space)
4906 * 2. second to emit code
4907 */
4908#ifdef ENABLE_LOG
4909 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004910 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911
4912 if (f != NULL)
4913 {
4914 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4915 fclose(f);
4916 }
4917 }
4918#endif
4919
4920 /*
4921 * PASS 1
4922 * Count number of NFA states in "nstate". Do not build the NFA.
4923 */
4924 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004925
4926 /* Space for compiled regexp */
4927 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4928 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4929 if (prog == NULL)
4930 goto fail;
4931 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004932 state_ptr = prog->state;
4933
4934 /*
4935 * PASS 2
4936 * Build the NFA
4937 */
4938 prog->start = post2nfa(postfix, post_ptr, FALSE);
4939 if (prog->start == NULL)
4940 goto fail;
4941
4942 prog->regflags = regflags;
4943 prog->engine = &nfa_regengine;
4944 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004945 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004946 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004947 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004948#ifdef ENABLE_LOG
4949 nfa_postfix_dump(expr, OK);
4950 nfa_dump(prog);
4951#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004952#ifdef FEAT_SYN_HL
4953 /* Remember whether this pattern has any \z specials in it. */
4954 prog->reghasz = re_has_z;
4955#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004956#ifdef DEBUG
4957 prog->pattern = vim_strsave(expr); /* memory will leak */
4958 nfa_regengine.expr = NULL;
4959#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004960
4961out:
4962 vim_free(post_start);
4963 post_start = post_ptr = post_end = NULL;
4964 state_ptr = NULL;
4965 return (regprog_T *)prog;
4966
4967fail:
4968 vim_free(prog);
4969 prog = NULL;
4970#ifdef ENABLE_LOG
4971 nfa_postfix_dump(expr, FAIL);
4972#endif
4973#ifdef DEBUG
4974 nfa_regengine.expr = NULL;
4975#endif
4976 goto out;
4977}
4978
4979
4980/*
4981 * Match a regexp against a string.
4982 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4983 * Uses curbuf for line count and 'iskeyword'.
4984 *
4985 * Return TRUE if there is a match, FALSE if not.
4986 */
4987 static int
4988nfa_regexec(rmp, line, col)
4989 regmatch_T *rmp;
4990 char_u *line; /* string to match against */
4991 colnr_T col; /* column to start looking for match */
4992{
4993 reg_match = rmp;
4994 reg_mmatch = NULL;
4995 reg_maxline = 0;
4996 reg_line_lbr = FALSE;
4997 reg_buf = curbuf;
4998 reg_win = NULL;
4999 ireg_ic = rmp->rm_ic;
5000#ifdef FEAT_MBYTE
5001 ireg_icombine = FALSE;
5002#endif
5003 ireg_maxcol = 0;
5004 return (nfa_regexec_both(line, col) != 0);
5005}
5006
5007#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5008 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5009
5010static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5011
5012/*
5013 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5014 */
5015 static int
5016nfa_regexec_nl(rmp, line, col)
5017 regmatch_T *rmp;
5018 char_u *line; /* string to match against */
5019 colnr_T col; /* column to start looking for match */
5020{
5021 reg_match = rmp;
5022 reg_mmatch = NULL;
5023 reg_maxline = 0;
5024 reg_line_lbr = TRUE;
5025 reg_buf = curbuf;
5026 reg_win = NULL;
5027 ireg_ic = rmp->rm_ic;
5028#ifdef FEAT_MBYTE
5029 ireg_icombine = FALSE;
5030#endif
5031 ireg_maxcol = 0;
5032 return (nfa_regexec_both(line, col) != 0);
5033}
5034#endif
5035
5036
5037/*
5038 * Match a regexp against multiple lines.
5039 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5040 * Uses curbuf for line count and 'iskeyword'.
5041 *
5042 * Return zero if there is no match. Return number of lines contained in the
5043 * match otherwise.
5044 *
5045 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5046 *
5047 * ! Also NOTE : match may actually be in another line. e.g.:
5048 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5049 *
5050 * +-------------------------+
5051 * |a |
5052 * |b |
5053 * |c |
5054 * | |
5055 * +-------------------------+
5056 *
5057 * then nfa_regexec_multi() returns 3. while the original
5058 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5059 *
5060 * FIXME if this behavior is not compatible.
5061 */
5062 static long
5063nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5064 regmmatch_T *rmp;
5065 win_T *win; /* window in which to search or NULL */
5066 buf_T *buf; /* buffer in which to search */
5067 linenr_T lnum; /* nr of line to start looking for match */
5068 colnr_T col; /* column to start looking for match */
5069 proftime_T *tm UNUSED; /* timeout limit or NULL */
5070{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005071 reg_match = NULL;
5072 reg_mmatch = rmp;
5073 reg_buf = buf;
5074 reg_win = win;
5075 reg_firstlnum = lnum;
5076 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5077 reg_line_lbr = FALSE;
5078 ireg_ic = rmp->rmm_ic;
5079#ifdef FEAT_MBYTE
5080 ireg_icombine = FALSE;
5081#endif
5082 ireg_maxcol = rmp->rmm_maxcol;
5083
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005084 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085}
5086
5087#ifdef DEBUG
5088# undef ENABLE_LOG
5089#endif