blob: f9b18886145cad516b7b7d5432e40528494b608c [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
832 case Magic('~'): /* previous substitute pattern */
Bram Moolenaar5714b802013-05-28 22:03:20 +0200833 /* TODO: Not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200834 return FAIL;
835
Bram Moolenaar428e9872013-05-30 17:05:39 +0200836 case Magic('1'):
837 case Magic('2'):
838 case Magic('3'):
839 case Magic('4'):
840 case Magic('5'):
841 case Magic('6'):
842 case Magic('7'):
843 case Magic('8'):
844 case Magic('9'):
845 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
846 nfa_has_backref = TRUE;
847 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200848
849 case Magic('z'):
850 c = no_Magic(getchr());
851 switch (c)
852 {
853 case 's':
854 EMIT(NFA_ZSTART);
855 break;
856 case 'e':
857 EMIT(NFA_ZEND);
858 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200859 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200860#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200861 case '1':
862 case '2':
863 case '3':
864 case '4':
865 case '5':
866 case '6':
867 case '7':
868 case '8':
869 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200870 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200871 if (reg_do_extmatch != REX_USE)
872 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200873 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
874 /* No need to set nfa_has_backref, the sub-matches don't
875 * change when \z1 .. \z9 maches or not. */
876 re_has_z = REX_USE;
877 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200878 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200879 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200880 if (reg_do_extmatch != REX_SET)
881 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200882 if (nfa_reg(REG_ZPAREN) == FAIL)
883 return FAIL; /* cascaded error */
884 re_has_z = REX_SET;
885 break;
886#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200887 default:
888 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200889 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200890 no_Magic(c));
891 return FAIL;
892 }
893 break;
894
895 case Magic('%'):
896 c = no_Magic(getchr());
897 switch (c)
898 {
899 /* () without a back reference */
900 case '(':
901 if (nfa_reg(REG_NPAREN) == FAIL)
902 return FAIL;
903 EMIT(NFA_NOPEN);
904 break;
905
906 case 'd': /* %d123 decimal */
907 case 'o': /* %o123 octal */
908 case 'x': /* %xab hex 2 */
909 case 'u': /* %uabcd hex 4 */
910 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200911 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200912 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200913
Bram Moolenaar47196582013-05-25 22:04:23 +0200914 switch (c)
915 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200916 case 'd': nr = getdecchrs(); break;
917 case 'o': nr = getoctchrs(); break;
918 case 'x': nr = gethexchrs(2); break;
919 case 'u': nr = gethexchrs(4); break;
920 case 'U': nr = gethexchrs(8); break;
921 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200922 }
923
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200924 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200925 EMSG2_RET_FAIL(
926 _("E678: Invalid character after %s%%[dxouU]"),
927 reg_magic == MAGIC_ALL);
928 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200929 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200930 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200931 break;
932
933 /* Catch \%^ and \%$ regardless of where they appear in the
934 * pattern -- regardless of whether or not it makes sense. */
935 case '^':
936 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200937 break;
938
939 case '$':
940 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200941 break;
942
943 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200944 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200945 break;
946
947 case 'V':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200948 /* TODO: not supported yet */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200949 return FAIL;
950 break;
951
952 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200953 /* TODO: \%[abc] not supported yet */
954 return FAIL;
955
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200956 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200957 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200958 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200959 int cmp = c;
960
961 if (c == '<' || c == '>')
962 c = getchr();
963 while (VIM_ISDIGIT(c))
964 {
965 n = n * 10 + (c - '0');
966 c = getchr();
967 }
968 if (c == 'l' || c == 'c' || c == 'v')
969 {
970 EMIT(n);
971 if (c == 'l')
972 EMIT(cmp == '<' ? NFA_LNUM_LT :
973 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
974 else if (c == 'c')
975 EMIT(cmp == '<' ? NFA_COL_LT :
976 cmp == '>' ? NFA_COL_GT : NFA_COL);
977 else
978 EMIT(cmp == '<' ? NFA_VCOL_LT :
979 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
980 break;
981 }
982 else if (c == '\'')
983 /* TODO: \%'m not supported yet */
984 return FAIL;
985 }
Bram Moolenaar5714b802013-05-28 22:03:20 +0200986 syntax_error = TRUE;
987 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
988 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200989 return FAIL;
990 }
991 break;
992
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200993 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200994collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200995 /*
996 * Glue is emitted between several atoms from the [].
997 * It is either NFA_OR, or NFA_CONCAT.
998 *
999 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1000 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1001 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1002 * notation)
1003 *
1004 */
1005
1006
1007/* Emit negation atoms, if needed.
1008 * The CONCAT below merges the NOT with the previous node. */
1009#define TRY_NEG() \
1010 if (negated == TRUE) \
1011 { \
1012 EMIT(NFA_NOT); \
1013 }
1014
1015/* Emit glue between important nodes : CONCAT or OR. */
1016#define EMIT_GLUE() \
1017 if (first == FALSE) \
1018 EMIT(glue); \
1019 else \
1020 first = FALSE;
1021
1022 p = regparse;
1023 endp = skip_anyof(p);
1024 if (*endp == ']')
1025 {
1026 /*
1027 * Try to reverse engineer character classes. For example,
1028 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1029 * and perform the necessary substitutions in the NFA.
1030 */
1031 result = nfa_recognize_char_class(regparse, endp,
1032 extra == ADD_NL);
1033 if (result != FAIL)
1034 {
1035 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1036 EMIT(result);
1037 else /* must be char class + newline */
1038 {
1039 EMIT(result - ADD_NL);
1040 EMIT(NFA_NEWL);
1041 EMIT(NFA_OR);
1042 }
1043 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001044 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001045 return OK;
1046 }
1047 /*
1048 * Failed to recognize a character class. Use the simple
1049 * version that turns [abc] into 'a' OR 'b' OR 'c'
1050 */
1051 startc = endc = oldstartc = -1;
1052 first = TRUE; /* Emitting first atom in this sequence? */
1053 negated = FALSE;
1054 glue = NFA_OR;
1055 if (*regparse == '^') /* negated range */
1056 {
1057 negated = TRUE;
1058 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001059 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001060 }
1061 if (*regparse == '-')
1062 {
1063 startc = '-';
1064 EMIT(startc);
1065 TRY_NEG();
1066 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001067 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001068 }
1069 /* Emit the OR branches for each character in the [] */
1070 emit_range = FALSE;
1071 while (regparse < endp)
1072 {
1073 oldstartc = startc;
1074 startc = -1;
1075 got_coll_char = FALSE;
1076 if (*regparse == '[')
1077 {
1078 /* Check for [: :], [= =], [. .] */
1079 equiclass = collclass = 0;
1080 charclass = get_char_class(&regparse);
1081 if (charclass == CLASS_NONE)
1082 {
1083 equiclass = get_equi_class(&regparse);
1084 if (equiclass == 0)
1085 collclass = get_coll_element(&regparse);
1086 }
1087
1088 /* Character class like [:alpha:] */
1089 if (charclass != CLASS_NONE)
1090 {
1091 switch (charclass)
1092 {
1093 case CLASS_ALNUM:
1094 EMIT(NFA_CLASS_ALNUM);
1095 break;
1096 case CLASS_ALPHA:
1097 EMIT(NFA_CLASS_ALPHA);
1098 break;
1099 case CLASS_BLANK:
1100 EMIT(NFA_CLASS_BLANK);
1101 break;
1102 case CLASS_CNTRL:
1103 EMIT(NFA_CLASS_CNTRL);
1104 break;
1105 case CLASS_DIGIT:
1106 EMIT(NFA_CLASS_DIGIT);
1107 break;
1108 case CLASS_GRAPH:
1109 EMIT(NFA_CLASS_GRAPH);
1110 break;
1111 case CLASS_LOWER:
1112 EMIT(NFA_CLASS_LOWER);
1113 break;
1114 case CLASS_PRINT:
1115 EMIT(NFA_CLASS_PRINT);
1116 break;
1117 case CLASS_PUNCT:
1118 EMIT(NFA_CLASS_PUNCT);
1119 break;
1120 case CLASS_SPACE:
1121 EMIT(NFA_CLASS_SPACE);
1122 break;
1123 case CLASS_UPPER:
1124 EMIT(NFA_CLASS_UPPER);
1125 break;
1126 case CLASS_XDIGIT:
1127 EMIT(NFA_CLASS_XDIGIT);
1128 break;
1129 case CLASS_TAB:
1130 EMIT(NFA_CLASS_TAB);
1131 break;
1132 case CLASS_RETURN:
1133 EMIT(NFA_CLASS_RETURN);
1134 break;
1135 case CLASS_BACKSPACE:
1136 EMIT(NFA_CLASS_BACKSPACE);
1137 break;
1138 case CLASS_ESCAPE:
1139 EMIT(NFA_CLASS_ESCAPE);
1140 break;
1141 }
1142 TRY_NEG();
1143 EMIT_GLUE();
1144 continue;
1145 }
1146 /* Try equivalence class [=a=] and the like */
1147 if (equiclass != 0)
1148 {
1149 result = nfa_emit_equi_class(equiclass, negated);
1150 if (result == FAIL)
1151 {
1152 /* should never happen */
1153 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1154 }
1155 EMIT_GLUE();
1156 continue;
1157 }
1158 /* Try collating class like [. .] */
1159 if (collclass != 0)
1160 {
1161 startc = collclass; /* allow [.a.]-x as a range */
1162 /* Will emit the proper atom at the end of the
1163 * while loop. */
1164 }
1165 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001166 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1167 * start character. */
1168 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169 {
1170 emit_range = TRUE;
1171 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001172 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001173 continue; /* reading the end of the range */
1174 }
1175
1176 /* Now handle simple and escaped characters.
1177 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1178 * accepts "\t", "\e", etc., but only when the 'l' flag in
1179 * 'cpoptions' is not included.
1180 * Posix doesn't recognize backslash at all.
1181 */
1182 if (*regparse == '\\'
1183 && !cpo_bsl
1184 && regparse + 1 <= endp
1185 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1186 || (!cpo_lit
1187 && vim_strchr(REGEXP_ABBR, regparse[1])
1188 != NULL)
1189 )
1190 )
1191 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001192 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001193
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001194 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001195 startc = reg_string ? NL : NFA_NEWL;
1196 else
1197 if (*regparse == 'd'
1198 || *regparse == 'o'
1199 || *regparse == 'x'
1200 || *regparse == 'u'
1201 || *regparse == 'U'
1202 )
1203 {
1204 /* TODO(RE) This needs more testing */
1205 startc = coll_get_char();
1206 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001207 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 }
1209 else
1210 {
1211 /* \r,\t,\e,\b */
1212 startc = backslash_trans(*regparse);
1213 }
1214 }
1215
1216 /* Normal printable char */
1217 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001218 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219
1220 /* Previous char was '-', so this char is end of range. */
1221 if (emit_range)
1222 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001223 endc = startc;
1224 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 if (startc > endc)
1226 EMSG_RET_FAIL(_(e_invrange));
1227#ifdef FEAT_MBYTE
1228 if (has_mbyte && ((*mb_char2len)(startc) > 1
1229 || (*mb_char2len)(endc) > 1))
1230 {
1231 if (endc > startc + 256)
1232 EMSG_RET_FAIL(_(e_invrange));
1233 /* Emit the range. "startc" was already emitted, so
1234 * skip it. */
1235 for (c = startc + 1; c <= endc; c++)
1236 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001237 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 TRY_NEG();
1239 EMIT_GLUE();
1240 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 }
1242 else
1243#endif
1244 {
1245#ifdef EBCDIC
1246 int alpha_only = FALSE;
1247
1248 /* for alphabetical range skip the gaps
1249 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1250 if (isalpha(startc) && isalpha(endc))
1251 alpha_only = TRUE;
1252#endif
1253 /* Emit the range. "startc" was already emitted, so
1254 * skip it. */
1255 for (c = startc + 1; c <= endc; c++)
1256#ifdef EBCDIC
1257 if (!alpha_only || isalpha(startc))
1258#endif
1259 {
1260 EMIT(c);
1261 TRY_NEG();
1262 EMIT_GLUE();
1263 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001265 emit_range = FALSE;
1266 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 }
1268 else
1269 {
1270 /*
1271 * This char (startc) is not part of a range. Just
1272 * emit it.
1273 *
1274 * Normally, simply emit startc. But if we get char
1275 * code=0 from a collating char, then replace it with
1276 * 0x0a.
1277 *
1278 * This is needed to completely mimic the behaviour of
1279 * the backtracking engine.
1280 */
1281 if (got_coll_char == TRUE && startc == 0)
1282 EMIT(0x0a);
1283 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001284 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285 TRY_NEG();
1286 EMIT_GLUE();
1287 }
1288
Bram Moolenaar51a29832013-05-28 22:30:35 +02001289 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001290 } /* while (p < endp) */
1291
Bram Moolenaar51a29832013-05-28 22:30:35 +02001292 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293 if (*regparse == '-') /* if last, '-' is just a char */
1294 {
1295 EMIT('-');
1296 TRY_NEG();
1297 EMIT_GLUE();
1298 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001299 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001300
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001301 /* skip the trailing ] */
1302 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001303 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001304 if (negated == TRUE)
1305 {
1306 /* Mark end of negated char range */
1307 EMIT(NFA_END_NEG_RANGE);
1308 EMIT(NFA_CONCAT);
1309 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001310
1311 /* \_[] also matches \n but it's not negated */
1312 if (extra == ADD_NL)
1313 {
1314 EMIT(reg_string ? NL : NFA_NEWL);
1315 EMIT(NFA_OR);
1316 }
1317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001319 } /* if exists closing ] */
1320
1321 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001322 {
1323 syntax_error = TRUE;
1324 EMSG_RET_FAIL(_(e_missingbracket));
1325 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001326 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001327
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328 default:
1329 {
1330#ifdef FEAT_MBYTE
1331 int plen;
1332
1333nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001334 /* plen is length of current char with composing chars */
1335 if (enc_utf8 && ((*mb_char2len)(c)
1336 != (plen = (*mb_ptr2len)(old_regparse))
1337 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001338 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001339 int i = 0;
1340
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001341 /* A base character plus composing characters, or just one
1342 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001343 * This requires creating a separate atom as if enclosing
1344 * the characters in (), where NFA_COMPOSING is the ( and
1345 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001346 * building the postfix form, not the NFA itself;
1347 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001348 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001349 for (;;)
1350 {
1351 EMIT(c);
1352 if (i > 0)
1353 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001354 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001355 break;
1356 c = utf_ptr2char(old_regparse + i);
1357 }
1358 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 regparse = old_regparse + plen;
1360 }
1361 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362#endif
1363 {
1364 c = no_Magic(c);
1365 EMIT(c);
1366 }
1367 return OK;
1368 }
1369 }
1370
1371#undef TRY_NEG
1372#undef EMIT_GLUE
1373
1374 return OK;
1375}
1376
1377/*
1378 * Parse something followed by possible [*+=].
1379 *
1380 * A piece is an atom, possibly followed by a multi, an indication of how many
1381 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1382 * characters: "", "a", "aa", etc.
1383 *
1384 * piece ::= atom
1385 * or atom multi
1386 */
1387 static int
1388nfa_regpiece()
1389{
1390 int i;
1391 int op;
1392 int ret;
1393 long minval, maxval;
1394 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001395 parse_state_T old_state;
1396 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001397 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001398 int old_post_pos;
1399 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001400 int quest;
1401
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001402 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1403 * next. */
1404 save_parse_state(&old_state);
1405
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001407 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408
1409 ret = nfa_regatom();
1410 if (ret == FAIL)
1411 return FAIL; /* cascaded error */
1412
1413 op = peekchr();
1414 if (re_multi_type(op) == NOT_MULTI)
1415 return OK;
1416
1417 skipchr();
1418 switch (op)
1419 {
1420 case Magic('*'):
1421 EMIT(NFA_STAR);
1422 break;
1423
1424 case Magic('+'):
1425 /*
1426 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1427 * first and only submatch would be "aaa". But the backtracking
1428 * engine interprets the plus as "try matching one more time", and
1429 * a* matches a second time at the end of the input, the empty
1430 * string.
1431 * The submatch will the empty string.
1432 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001433 * In order to be consistent with the old engine, we replace
1434 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001435 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001436 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 curchr = -1;
1438 if (nfa_regatom() == FAIL)
1439 return FAIL;
1440 EMIT(NFA_STAR);
1441 EMIT(NFA_CONCAT);
1442 skipchr(); /* skip the \+ */
1443 break;
1444
1445 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001446 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001448 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001449 switch(op)
1450 {
1451 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001452 /* \@= */
1453 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001454 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001455 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001456 /* \@! */
1457 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001458 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001459 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001460 op = no_Magic(getchr());
1461 if (op == '=')
1462 /* \@<= */
1463 i = NFA_PREV_ATOM_JUST_BEFORE;
1464 else if (op == '!')
1465 /* \@<! */
1466 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1467 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001469 /* \@> Not supported yet */
1470 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001471 return FAIL;
1472 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001473 if (i == 0)
1474 {
1475 syntax_error = TRUE;
1476 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1477 return FAIL;
1478 }
1479 EMIT(i);
1480 if (i == NFA_PREV_ATOM_JUST_BEFORE
1481 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1482 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 break;
1484
1485 case Magic('?'):
1486 case Magic('='):
1487 EMIT(NFA_QUEST);
1488 break;
1489
1490 case Magic('{'):
1491 /* a{2,5} will expand to 'aaa?a?a?'
1492 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1493 * version of '?'
1494 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1495 * parenthesis have the same id
1496 */
1497
1498 greedy = TRUE;
1499 c2 = peekchr();
1500 if (c2 == '-' || c2 == Magic('-'))
1501 {
1502 skipchr();
1503 greedy = FALSE;
1504 }
1505 if (!read_limits(&minval, &maxval))
1506 {
1507 syntax_error = TRUE;
1508 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1509 }
1510 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1511 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001512 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001514 if (greedy)
1515 /* \{}, \{0,} */
1516 EMIT(NFA_STAR);
1517 else
1518 /* \{-}, \{-0,} */
1519 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 break;
1521 }
1522
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 /* Special case: x{0} or x{-0} */
1524 if (maxval == 0)
1525 {
1526 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001527 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1529 EMIT(NFA_SKIP_CHAR);
1530 return OK;
1531 }
1532
1533 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001534 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001535 /* Save parse state after the repeated atom and the \{} */
1536 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001538 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1539 for (i = 0; i < maxval; i++)
1540 {
1541 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001542 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001543 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001544 if (nfa_regatom() == FAIL)
1545 return FAIL;
1546 /* after "minval" times, atoms are optional */
1547 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001548 {
1549 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001550 {
1551 if (greedy)
1552 EMIT(NFA_STAR);
1553 else
1554 EMIT(NFA_STAR_NONGREEDY);
1555 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001556 else
1557 EMIT(quest);
1558 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001559 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001560 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001561 if (i + 1 > minval && maxval == MAX_LIMIT)
1562 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 }
1564
1565 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001566 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001567 curchr = -1;
1568
1569 break;
1570
1571
1572 default:
1573 break;
1574 } /* end switch */
1575
1576 if (re_multi_type(peekchr()) != NOT_MULTI)
1577 {
1578 /* Can't have a multi follow a multi. */
1579 syntax_error = TRUE;
1580 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1581 }
1582
1583 return OK;
1584}
1585
1586/*
1587 * Parse one or more pieces, concatenated. It matches a match for the
1588 * first piece, followed by a match for the second piece, etc. Example:
1589 * "f[0-9]b", first matches "f", then a digit and then "b".
1590 *
1591 * concat ::= piece
1592 * or piece piece
1593 * or piece piece piece
1594 * etc.
1595 */
1596 static int
1597nfa_regconcat()
1598{
1599 int cont = TRUE;
1600 int first = TRUE;
1601
1602 while (cont)
1603 {
1604 switch (peekchr())
1605 {
1606 case NUL:
1607 case Magic('|'):
1608 case Magic('&'):
1609 case Magic(')'):
1610 cont = FALSE;
1611 break;
1612
1613 case Magic('Z'):
1614#ifdef FEAT_MBYTE
1615 regflags |= RF_ICOMBINE;
1616#endif
1617 skipchr_keepstart();
1618 break;
1619 case Magic('c'):
1620 regflags |= RF_ICASE;
1621 skipchr_keepstart();
1622 break;
1623 case Magic('C'):
1624 regflags |= RF_NOICASE;
1625 skipchr_keepstart();
1626 break;
1627 case Magic('v'):
1628 reg_magic = MAGIC_ALL;
1629 skipchr_keepstart();
1630 curchr = -1;
1631 break;
1632 case Magic('m'):
1633 reg_magic = MAGIC_ON;
1634 skipchr_keepstart();
1635 curchr = -1;
1636 break;
1637 case Magic('M'):
1638 reg_magic = MAGIC_OFF;
1639 skipchr_keepstart();
1640 curchr = -1;
1641 break;
1642 case Magic('V'):
1643 reg_magic = MAGIC_NONE;
1644 skipchr_keepstart();
1645 curchr = -1;
1646 break;
1647
1648 default:
1649 if (nfa_regpiece() == FAIL)
1650 return FAIL;
1651 if (first == FALSE)
1652 EMIT(NFA_CONCAT);
1653 else
1654 first = FALSE;
1655 break;
1656 }
1657 }
1658
1659 return OK;
1660}
1661
1662/*
1663 * Parse a branch, one or more concats, separated by "\&". It matches the
1664 * last concat, but only if all the preceding concats also match at the same
1665 * position. Examples:
1666 * "foobeep\&..." matches "foo" in "foobeep".
1667 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1668 *
1669 * branch ::= concat
1670 * or concat \& concat
1671 * or concat \& concat \& concat
1672 * etc.
1673 */
1674 static int
1675nfa_regbranch()
1676{
1677 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001678 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001679
Bram Moolenaar16299b52013-05-30 18:45:23 +02001680 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681
1682 /* First branch, possibly the only one */
1683 if (nfa_regconcat() == FAIL)
1684 return FAIL;
1685
1686 ch = peekchr();
1687 /* Try next concats */
1688 while (ch == Magic('&'))
1689 {
1690 skipchr();
1691 EMIT(NFA_NOPEN);
1692 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001693 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 if (nfa_regconcat() == FAIL)
1695 return FAIL;
1696 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001697 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698 EMIT(NFA_SKIP_CHAR);
1699 EMIT(NFA_CONCAT);
1700 ch = peekchr();
1701 }
1702
1703 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001704 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705 EMIT(NFA_SKIP_CHAR);
1706
1707 return OK;
1708}
1709
1710/*
1711 * Parse a pattern, one or more branches, separated by "\|". It matches
1712 * anything that matches one of the branches. Example: "foo\|beep" matches
1713 * "foo" and matches "beep". If more than one branch matches, the first one
1714 * is used.
1715 *
1716 * pattern ::= branch
1717 * or branch \| branch
1718 * or branch \| branch \| branch
1719 * etc.
1720 */
1721 static int
1722nfa_reg(paren)
1723 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1724{
1725 int parno = 0;
1726
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001727 if (paren == REG_PAREN)
1728 {
1729 if (regnpar >= NSUBEXP) /* Too many `(' */
1730 {
1731 syntax_error = TRUE;
1732 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1733 }
1734 parno = regnpar++;
1735 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001736#ifdef FEAT_SYN_HL
1737 else if (paren == REG_ZPAREN)
1738 {
1739 /* Make a ZOPEN node. */
1740 if (regnzpar >= NSUBEXP)
1741 {
1742 syntax_error = TRUE;
1743 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1744 }
1745 parno = regnzpar++;
1746 }
1747#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001748
1749 if (nfa_regbranch() == FAIL)
1750 return FAIL; /* cascaded error */
1751
1752 while (peekchr() == Magic('|'))
1753 {
1754 skipchr();
1755 if (nfa_regbranch() == FAIL)
1756 return FAIL; /* cascaded error */
1757 EMIT(NFA_OR);
1758 }
1759
1760 /* Check for proper termination. */
1761 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1762 {
1763 syntax_error = TRUE;
1764 if (paren == REG_NPAREN)
1765 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1766 else
1767 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1768 }
1769 else if (paren == REG_NOPAREN && peekchr() != NUL)
1770 {
1771 syntax_error = TRUE;
1772 if (peekchr() == Magic(')'))
1773 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1774 else
1775 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1776 }
1777 /*
1778 * Here we set the flag allowing back references to this set of
1779 * parentheses.
1780 */
1781 if (paren == REG_PAREN)
1782 {
1783 had_endbrace[parno] = TRUE; /* have seen the close paren */
1784 EMIT(NFA_MOPEN + parno);
1785 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001786#ifdef FEAT_SYN_HL
1787 else if (paren == REG_ZPAREN)
1788 EMIT(NFA_ZOPEN + parno);
1789#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001790
1791 return OK;
1792}
1793
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794#ifdef DEBUG
1795static char_u code[50];
1796
1797 static void
1798nfa_set_code(c)
1799 int c;
1800{
1801 int addnl = FALSE;
1802
1803 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1804 {
1805 addnl = TRUE;
1806 c -= ADD_NL;
1807 }
1808
1809 STRCPY(code, "");
1810 switch (c)
1811 {
1812 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1813 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1814 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1815 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1816 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1817 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1818
Bram Moolenaar5714b802013-05-28 22:03:20 +02001819 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1820 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1821 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1822 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1823 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1824 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1825 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1826 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1827 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001828#ifdef FEAT_SYN_HL
1829 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1830 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1831 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1832 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1833 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1834 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1835 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1836 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1837 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1838#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001839 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1840
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001841 case NFA_PREV_ATOM_NO_WIDTH:
1842 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001843 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1844 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001845 case NFA_PREV_ATOM_JUST_BEFORE:
1846 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1847 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1848 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001849 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1850 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001852 case NFA_START_INVISIBLE_BEFORE:
1853 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001854 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1855
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1857 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1858
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001859 case NFA_MOPEN:
1860 case NFA_MOPEN1:
1861 case NFA_MOPEN2:
1862 case NFA_MOPEN3:
1863 case NFA_MOPEN4:
1864 case NFA_MOPEN5:
1865 case NFA_MOPEN6:
1866 case NFA_MOPEN7:
1867 case NFA_MOPEN8:
1868 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 STRCPY(code, "NFA_MOPEN(x)");
1870 code[10] = c - NFA_MOPEN + '0';
1871 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001872 case NFA_MCLOSE:
1873 case NFA_MCLOSE1:
1874 case NFA_MCLOSE2:
1875 case NFA_MCLOSE3:
1876 case NFA_MCLOSE4:
1877 case NFA_MCLOSE5:
1878 case NFA_MCLOSE6:
1879 case NFA_MCLOSE7:
1880 case NFA_MCLOSE8:
1881 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 STRCPY(code, "NFA_MCLOSE(x)");
1883 code[11] = c - NFA_MCLOSE + '0';
1884 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001885#ifdef FEAT_SYN_HL
1886 case NFA_ZOPEN:
1887 case NFA_ZOPEN1:
1888 case NFA_ZOPEN2:
1889 case NFA_ZOPEN3:
1890 case NFA_ZOPEN4:
1891 case NFA_ZOPEN5:
1892 case NFA_ZOPEN6:
1893 case NFA_ZOPEN7:
1894 case NFA_ZOPEN8:
1895 case NFA_ZOPEN9:
1896 STRCPY(code, "NFA_ZOPEN(x)");
1897 code[10] = c - NFA_ZOPEN + '0';
1898 break;
1899 case NFA_ZCLOSE:
1900 case NFA_ZCLOSE1:
1901 case NFA_ZCLOSE2:
1902 case NFA_ZCLOSE3:
1903 case NFA_ZCLOSE4:
1904 case NFA_ZCLOSE5:
1905 case NFA_ZCLOSE6:
1906 case NFA_ZCLOSE7:
1907 case NFA_ZCLOSE8:
1908 case NFA_ZCLOSE9:
1909 STRCPY(code, "NFA_ZCLOSE(x)");
1910 code[11] = c - NFA_ZCLOSE + '0';
1911 break;
1912#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1914 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1915 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1916 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001917 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1918 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001920 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1921 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1922 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1924 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1925 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1927 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1928 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1929 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1930 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1931 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1932 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1933 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1934 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1935 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1936 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1937 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1938 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1939 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1940 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1941 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1942 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1943
1944 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1945 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1946 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1947 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1948 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1949 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1950 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1951 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1952 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
1953 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
1954 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
1955 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
1956 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
1957 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
1958 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
1959 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
1960 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
1961 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
1962 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
1963 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
1964 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
1965 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
1966 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
1967 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
1968 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
1969 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
1970 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
1971
1972 default:
1973 STRCPY(code, "CHAR(x)");
1974 code[5] = c;
1975 }
1976
1977 if (addnl == TRUE)
1978 STRCAT(code, " + NEWLINE ");
1979
1980}
1981
1982#ifdef ENABLE_LOG
1983static FILE *log_fd;
1984
1985/*
1986 * Print the postfix notation of the current regexp.
1987 */
1988 static void
1989nfa_postfix_dump(expr, retval)
1990 char_u *expr;
1991 int retval;
1992{
1993 int *p;
1994 FILE *f;
1995
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02001996 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001997 if (f != NULL)
1998 {
1999 fprintf(f, "\n-------------------------\n");
2000 if (retval == FAIL)
2001 fprintf(f, ">>> NFA engine failed ... \n");
2002 else if (retval == OK)
2003 fprintf(f, ">>> NFA engine succeeded !\n");
2004 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002005 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002006 {
2007 nfa_set_code(*p);
2008 fprintf(f, "%s, ", code);
2009 }
2010 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002011 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002012 fprintf(f, "%d ", *p);
2013 fprintf(f, "\n\n");
2014 fclose(f);
2015 }
2016}
2017
2018/*
2019 * Print the NFA starting with a root node "state".
2020 */
2021 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002022nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 FILE *debugf;
2024 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002025{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002026 garray_T indent;
2027
2028 ga_init2(&indent, 1, 64);
2029 ga_append(&indent, '\0');
2030 nfa_print_state2(debugf, state, &indent);
2031 ga_clear(&indent);
2032}
2033
2034 static void
2035nfa_print_state2(debugf, state, indent)
2036 FILE *debugf;
2037 nfa_state_T *state;
2038 garray_T *indent;
2039{
2040 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002041
2042 if (state == NULL)
2043 return;
2044
2045 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002046
2047 /* Output indent */
2048 p = (char_u *)indent->ga_data;
2049 if (indent->ga_len >= 3)
2050 {
2051 int last = indent->ga_len - 3;
2052 char_u save[2];
2053
2054 STRNCPY(save, &p[last], 2);
2055 STRNCPY(&p[last], "+-", 2);
2056 fprintf(debugf, " %s", p);
2057 STRNCPY(&p[last], save, 2);
2058 }
2059 else
2060 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061
2062 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002063 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2064 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 if (state->id < 0)
2066 return;
2067
2068 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002069
2070 /* grow indent for state->out */
2071 indent->ga_len -= 1;
2072 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002073 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002074 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002075 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002076 ga_append(indent, '\0');
2077
2078 nfa_print_state2(debugf, state->out, indent);
2079
2080 /* replace last part of indent for state->out1 */
2081 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002082 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002083 ga_append(indent, '\0');
2084
2085 nfa_print_state2(debugf, state->out1, indent);
2086
2087 /* shrink indent */
2088 indent->ga_len -= 3;
2089 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090}
2091
2092/*
2093 * Print the NFA state machine.
2094 */
2095 static void
2096nfa_dump(prog)
2097 nfa_regprog_T *prog;
2098{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002099 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100
2101 if (debugf != NULL)
2102 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002103 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 fclose(debugf);
2105 }
2106}
2107#endif /* ENABLE_LOG */
2108#endif /* DEBUG */
2109
2110/*
2111 * Parse r.e. @expr and convert it into postfix form.
2112 * Return the postfix string on success, NULL otherwise.
2113 */
2114 static int *
2115re2post()
2116{
2117 if (nfa_reg(REG_NOPAREN) == FAIL)
2118 return NULL;
2119 EMIT(NFA_MOPEN);
2120 return post_start;
2121}
2122
2123/* NB. Some of the code below is inspired by Russ's. */
2124
2125/*
2126 * Represents an NFA state plus zero or one or two arrows exiting.
2127 * if c == MATCH, no arrows out; matching state.
2128 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2129 * If c < 256, labeled arrow with character c to out.
2130 */
2131
2132static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2133
2134/*
2135 * Allocate and initialize nfa_state_T.
2136 */
2137 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002138alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002139 int c;
2140 nfa_state_T *out;
2141 nfa_state_T *out1;
2142{
2143 nfa_state_T *s;
2144
2145 if (istate >= nstate)
2146 return NULL;
2147
2148 s = &state_ptr[istate++];
2149
2150 s->c = c;
2151 s->out = out;
2152 s->out1 = out1;
2153
2154 s->id = istate;
2155 s->lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002156 s->negated = FALSE;
2157
2158 return s;
2159}
2160
2161/*
2162 * A partially built NFA without the matching state filled in.
2163 * Frag_T.start points at the start state.
2164 * Frag_T.out is a list of places that need to be set to the
2165 * next state for this fragment.
2166 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002167
2168/* Since the out pointers in the list are always
2169 * uninitialized, we use the pointers themselves
2170 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002171typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002172union Ptrlist
2173{
2174 Ptrlist *next;
2175 nfa_state_T *s;
2176};
2177
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178struct Frag
2179{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002180 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181 Ptrlist *out;
2182};
2183typedef struct Frag Frag_T;
2184
2185static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2186static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2187static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2188static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2189static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2190static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2191
2192/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002193 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194 */
2195 static Frag_T
2196frag(start, out)
2197 nfa_state_T *start;
2198 Ptrlist *out;
2199{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002200 Frag_T n;
2201
2202 n.start = start;
2203 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204 return n;
2205}
2206
2207/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002208 * Create singleton list containing just outp.
2209 */
2210 static Ptrlist *
2211list1(outp)
2212 nfa_state_T **outp;
2213{
2214 Ptrlist *l;
2215
2216 l = (Ptrlist *)outp;
2217 l->next = NULL;
2218 return l;
2219}
2220
2221/*
2222 * Patch the list of states at out to point to start.
2223 */
2224 static void
2225patch(l, s)
2226 Ptrlist *l;
2227 nfa_state_T *s;
2228{
2229 Ptrlist *next;
2230
2231 for (; l; l = next)
2232 {
2233 next = l->next;
2234 l->s = s;
2235 }
2236}
2237
2238
2239/*
2240 * Join the two lists l1 and l2, returning the combination.
2241 */
2242 static Ptrlist *
2243append(l1, l2)
2244 Ptrlist *l1;
2245 Ptrlist *l2;
2246{
2247 Ptrlist *oldl1;
2248
2249 oldl1 = l1;
2250 while (l1->next)
2251 l1 = l1->next;
2252 l1->next = l2;
2253 return oldl1;
2254}
2255
2256/*
2257 * Stack used for transforming postfix form into NFA.
2258 */
2259static Frag_T empty;
2260
2261 static void
2262st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002263 int *postfix UNUSED;
2264 int *end UNUSED;
2265 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002266{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002267#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002268 FILE *df;
2269 int *p2;
2270
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002271 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272 if (df)
2273 {
2274 fprintf(df, "Error popping the stack!\n");
2275#ifdef DEBUG
2276 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2277#endif
2278 fprintf(df, "Postfix form is: ");
2279#ifdef DEBUG
2280 for (p2 = postfix; p2 < end; p2++)
2281 {
2282 nfa_set_code(*p2);
2283 fprintf(df, "%s, ", code);
2284 }
2285 nfa_set_code(*p);
2286 fprintf(df, "\nCurrent position is: ");
2287 for (p2 = postfix; p2 <= p; p2 ++)
2288 {
2289 nfa_set_code(*p2);
2290 fprintf(df, "%s, ", code);
2291 }
2292#else
2293 for (p2 = postfix; p2 < end; p2++)
2294 {
2295 fprintf(df, "%d, ", *p2);
2296 }
2297 fprintf(df, "\nCurrent position is: ");
2298 for (p2 = postfix; p2 <= p; p2 ++)
2299 {
2300 fprintf(df, "%d, ", *p2);
2301 }
2302#endif
2303 fprintf(df, "\n--------------------------\n");
2304 fclose(df);
2305 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002306#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002307 EMSG(_("E874: (NFA) Could not pop the stack !"));
2308}
2309
2310/*
2311 * Push an item onto the stack.
2312 */
2313 static void
2314st_push(s, p, stack_end)
2315 Frag_T s;
2316 Frag_T **p;
2317 Frag_T *stack_end;
2318{
2319 Frag_T *stackp = *p;
2320
2321 if (stackp >= stack_end)
2322 return;
2323 *stackp = s;
2324 *p = *p + 1;
2325}
2326
2327/*
2328 * Pop an item from the stack.
2329 */
2330 static Frag_T
2331st_pop(p, stack)
2332 Frag_T **p;
2333 Frag_T *stack;
2334{
2335 Frag_T *stackp;
2336
2337 *p = *p - 1;
2338 stackp = *p;
2339 if (stackp < stack)
2340 return empty;
2341 return **p;
2342}
2343
2344/*
2345 * Convert a postfix form into its equivalent NFA.
2346 * Return the NFA start state on success, NULL otherwise.
2347 */
2348 static nfa_state_T *
2349post2nfa(postfix, end, nfa_calc_size)
2350 int *postfix;
2351 int *end;
2352 int nfa_calc_size;
2353{
2354 int *p;
2355 int mopen;
2356 int mclose;
2357 Frag_T *stack = NULL;
2358 Frag_T *stackp = NULL;
2359 Frag_T *stack_end = NULL;
2360 Frag_T e1;
2361 Frag_T e2;
2362 Frag_T e;
2363 nfa_state_T *s;
2364 nfa_state_T *s1;
2365 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002366 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002367
2368 if (postfix == NULL)
2369 return NULL;
2370
Bram Moolenaar053bb602013-05-20 13:55:21 +02002371#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372#define POP() st_pop(&stackp, stack); \
2373 if (stackp < stack) \
2374 { \
2375 st_error(postfix, end, p); \
2376 return NULL; \
2377 }
2378
2379 if (nfa_calc_size == FALSE)
2380 {
2381 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002382 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002384 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385 }
2386
2387 for (p = postfix; p < end; ++p)
2388 {
2389 switch (*p)
2390 {
2391 case NFA_CONCAT:
2392 /* Catenation.
2393 * Pay attention: this operator does not exist
2394 * in the r.e. itself (it is implicit, really).
2395 * It is added when r.e. is translated to postfix
2396 * form in re2post().
2397 *
2398 * No new state added here. */
2399 if (nfa_calc_size == TRUE)
2400 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002401 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002402 break;
2403 }
2404 e2 = POP();
2405 e1 = POP();
2406 patch(e1.out, e2.start);
2407 PUSH(frag(e1.start, e2.out));
2408 break;
2409
2410 case NFA_NOT:
2411 /* Negation of a character */
2412 if (nfa_calc_size == TRUE)
2413 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002414 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415 break;
2416 }
2417 e1 = POP();
2418 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002419#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002420 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002422#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002423 PUSH(e1);
2424 break;
2425
2426 case NFA_OR:
2427 /* Alternation */
2428 if (nfa_calc_size == TRUE)
2429 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002430 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 break;
2432 }
2433 e2 = POP();
2434 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002435 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002437 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002438 PUSH(frag(s, append(e1.out, e2.out)));
2439 break;
2440
2441 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002442 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 if (nfa_calc_size == TRUE)
2444 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002445 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446 break;
2447 }
2448 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002449 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002451 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 patch(e.out, s);
2453 PUSH(frag(s, list1(&s->out1)));
2454 break;
2455
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002456 case NFA_STAR_NONGREEDY:
2457 /* Zero or more, prefer zero */
2458 if (nfa_calc_size == TRUE)
2459 {
2460 nstate++;
2461 break;
2462 }
2463 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002464 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002465 if (s == NULL)
2466 goto theend;
2467 patch(e.out, s);
2468 PUSH(frag(s, list1(&s->out)));
2469 break;
2470
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 case NFA_QUEST:
2472 /* one or zero atoms=> greedy match */
2473 if (nfa_calc_size == TRUE)
2474 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002475 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002476 break;
2477 }
2478 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002479 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002481 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002482 PUSH(frag(s, append(e.out, list1(&s->out1))));
2483 break;
2484
2485 case NFA_QUEST_NONGREEDY:
2486 /* zero or one atoms => non-greedy match */
2487 if (nfa_calc_size == TRUE)
2488 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002489 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002490 break;
2491 }
2492 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002493 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002495 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496 PUSH(frag(s, append(e.out, list1(&s->out))));
2497 break;
2498
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 case NFA_SKIP_CHAR:
2500 /* Symbol of 0-length, Used in a repetition
2501 * with max/min count of 0 */
2502 if (nfa_calc_size == TRUE)
2503 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002504 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505 break;
2506 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002507 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002509 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002510 PUSH(frag(s, list1(&s->out)));
2511 break;
2512
2513 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002514 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002515 case NFA_PREV_ATOM_JUST_BEFORE:
2516 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002517 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002518 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002519 * The \@<= operator: match for the preceding atom.
2520 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002522 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002523
2524 if (nfa_calc_size == TRUE)
2525 {
2526 nstate += 2;
2527 break;
2528 }
2529 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002530 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002532 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 patch(e.out, s1);
2534
Bram Moolenaar525666f2013-06-02 16:40:55 +02002535 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002536 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002537 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002538 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2539 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002540 {
2541 s->negated = TRUE;
2542 s1->negated = TRUE;
2543 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002544 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2545 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2546 {
2547 s->val = *++p; /* get the count */
2548 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2549 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002550
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002551 PUSH(frag(s, list1(&s1->out)));
2552 break;
2553
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002554#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002555 case NFA_COMPOSING: /* char with composing char */
2556#if 0
2557 /* TODO */
2558 if (regflags & RF_ICOMBINE)
2559 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002560 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002561 }
2562#endif
2563 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002564#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002565
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002566 case NFA_MOPEN: /* \( \) Submatch */
2567 case NFA_MOPEN1:
2568 case NFA_MOPEN2:
2569 case NFA_MOPEN3:
2570 case NFA_MOPEN4:
2571 case NFA_MOPEN5:
2572 case NFA_MOPEN6:
2573 case NFA_MOPEN7:
2574 case NFA_MOPEN8:
2575 case NFA_MOPEN9:
2576#ifdef FEAT_SYN_HL
2577 case NFA_ZOPEN: /* \z( \) Submatch */
2578 case NFA_ZOPEN1:
2579 case NFA_ZOPEN2:
2580 case NFA_ZOPEN3:
2581 case NFA_ZOPEN4:
2582 case NFA_ZOPEN5:
2583 case NFA_ZOPEN6:
2584 case NFA_ZOPEN7:
2585 case NFA_ZOPEN8:
2586 case NFA_ZOPEN9:
2587#endif
2588 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002589 if (nfa_calc_size == TRUE)
2590 {
2591 nstate += 2;
2592 break;
2593 }
2594
2595 mopen = *p;
2596 switch (*p)
2597 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002598 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2599#ifdef FEAT_SYN_HL
2600 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2601 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2602 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2603 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2604 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2605 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2606 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2607 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2608 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2609 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2610#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002611#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002612 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002613#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002615 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616 mclose = *p + NSUBEXP;
2617 break;
2618 }
2619
2620 /* Allow "NFA_MOPEN" as a valid postfix representation for
2621 * the empty regexp "". In this case, the NFA will be
2622 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2623 * empty groups of parenthesis, and empty mbyte chars */
2624 if (stackp == stack)
2625 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002626 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002627 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002628 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002629 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002630 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002631 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002632 patch(list1(&s->out), s1);
2633 PUSH(frag(s, list1(&s1->out)));
2634 break;
2635 }
2636
2637 /* At least one node was emitted before NFA_MOPEN, so
2638 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2639 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002640 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002642 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643
Bram Moolenaar525666f2013-06-02 16:40:55 +02002644 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002646 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 patch(e.out, s1);
2648
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002649#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002650 if (mopen == NFA_COMPOSING)
2651 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002653#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654
2655 PUSH(frag(s, list1(&s1->out)));
2656 break;
2657
Bram Moolenaar5714b802013-05-28 22:03:20 +02002658 case NFA_BACKREF1:
2659 case NFA_BACKREF2:
2660 case NFA_BACKREF3:
2661 case NFA_BACKREF4:
2662 case NFA_BACKREF5:
2663 case NFA_BACKREF6:
2664 case NFA_BACKREF7:
2665 case NFA_BACKREF8:
2666 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002667#ifdef FEAT_SYN_HL
2668 case NFA_ZREF1:
2669 case NFA_ZREF2:
2670 case NFA_ZREF3:
2671 case NFA_ZREF4:
2672 case NFA_ZREF5:
2673 case NFA_ZREF6:
2674 case NFA_ZREF7:
2675 case NFA_ZREF8:
2676 case NFA_ZREF9:
2677#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002678 if (nfa_calc_size == TRUE)
2679 {
2680 nstate += 2;
2681 break;
2682 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002683 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002684 if (s == NULL)
2685 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002686 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002687 if (s1 == NULL)
2688 goto theend;
2689 patch(list1(&s->out), s1);
2690 PUSH(frag(s, list1(&s1->out)));
2691 break;
2692
Bram Moolenaar423532e2013-05-29 21:14:42 +02002693 case NFA_LNUM:
2694 case NFA_LNUM_GT:
2695 case NFA_LNUM_LT:
2696 case NFA_VCOL:
2697 case NFA_VCOL_GT:
2698 case NFA_VCOL_LT:
2699 case NFA_COL:
2700 case NFA_COL_GT:
2701 case NFA_COL_LT:
2702 if (nfa_calc_size == TRUE)
2703 {
2704 nstate += 1;
2705 break;
2706 }
2707 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002708 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002709 if (s == NULL)
2710 goto theend;
2711 s->val = e1.start->c;
2712 PUSH(frag(s, list1(&s->out)));
2713 break;
2714
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715 case NFA_ZSTART:
2716 case NFA_ZEND:
2717 default:
2718 /* Operands */
2719 if (nfa_calc_size == TRUE)
2720 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002721 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002722 break;
2723 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002724 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002726 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727 PUSH(frag(s, list1(&s->out)));
2728 break;
2729
2730 } /* switch(*p) */
2731
2732 } /* for(p = postfix; *p; ++p) */
2733
2734 if (nfa_calc_size == TRUE)
2735 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002736 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002737 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 }
2739
2740 e = POP();
2741 if (stackp != stack)
2742 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2743
2744 if (istate >= nstate)
2745 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2746
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002747 matchstate = &state_ptr[istate++]; /* the match state */
2748 matchstate->c = NFA_MATCH;
2749 matchstate->out = matchstate->out1 = NULL;
2750
2751 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002752 ret = e.start;
2753
2754theend:
2755 vim_free(stack);
2756 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002757
2758#undef POP1
2759#undef PUSH1
2760#undef POP2
2761#undef PUSH2
2762#undef POP
2763#undef PUSH
2764}
2765
2766/****************************************************************
2767 * NFA execution code.
2768 ****************************************************************/
2769
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002770typedef struct
2771{
2772 int in_use; /* number of subexpr with useful info */
2773
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002774 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002775 union
2776 {
2777 struct multipos
2778 {
2779 lpos_T start;
2780 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002781 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002782 struct linepos
2783 {
2784 char_u *start;
2785 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002786 } line[NSUBEXP];
2787 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002788} regsub_T;
2789
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002790typedef struct
2791{
2792 regsub_T norm; /* \( .. \) matches */
2793#ifdef FEAT_SYN_HL
2794 regsub_T synt; /* \z( .. \) matches */
2795#endif
2796} regsubs_T;
2797
Bram Moolenaar963fee22013-05-26 21:47:28 +02002798/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002799typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800{
2801 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002802 int count;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002803 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002804} nfa_thread_T;
2805
Bram Moolenaar963fee22013-05-26 21:47:28 +02002806/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002807typedef struct
2808{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002809 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002810 int n; /* nr of states currently in "t" */
2811 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002812 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002813} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002814
Bram Moolenaar5714b802013-05-28 22:03:20 +02002815#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002816static void log_subsexpr __ARGS((regsubs_T *subs));
2817static void log_subexpr __ARGS((regsub_T *sub));
2818
2819 static void
2820log_subsexpr(subs)
2821 regsubs_T *subs;
2822{
2823 log_subexpr(&subs->norm);
2824# ifdef FEAT_SYN_HL
2825 log_subexpr(&subs->synt);
2826# endif
2827}
2828
Bram Moolenaar5714b802013-05-28 22:03:20 +02002829 static void
2830log_subexpr(sub)
2831 regsub_T *sub;
2832{
2833 int j;
2834
2835 for (j = 0; j < sub->in_use; j++)
2836 if (REG_MULTI)
2837 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2838 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002839 sub->list.multi[j].start.col,
2840 (int)sub->list.multi[j].start.lnum,
2841 sub->list.multi[j].end.col,
2842 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002843 else
2844 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2845 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002846 (char *)sub->list.line[j].start,
2847 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002848 fprintf(log_fd, "\n");
2849}
2850#endif
2851
Bram Moolenaar963fee22013-05-26 21:47:28 +02002852/* Used during execution: whether a match has been found. */
2853static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002854
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002855static void clear_sub __ARGS((regsub_T *sub));
2856static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2857static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002858static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002859static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
2860static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int *ip));
2861
2862 static void
2863clear_sub(sub)
2864 regsub_T *sub;
2865{
2866 if (REG_MULTI)
2867 /* Use 0xff to set lnum to -1 */
2868 vim_memset(sub->list.multi, 0xff,
2869 sizeof(struct multipos) * nfa_nsubexpr);
2870 else
2871 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2872 sub->in_use = 0;
2873}
2874
2875/*
2876 * Copy the submatches from "from" to "to".
2877 */
2878 static void
2879copy_sub(to, from)
2880 regsub_T *to;
2881 regsub_T *from;
2882{
2883 to->in_use = from->in_use;
2884 if (from->in_use > 0)
2885 {
2886 /* Copy the match start and end positions. */
2887 if (REG_MULTI)
2888 mch_memmove(&to->list.multi[0],
2889 &from->list.multi[0],
2890 sizeof(struct multipos) * from->in_use);
2891 else
2892 mch_memmove(&to->list.line[0],
2893 &from->list.line[0],
2894 sizeof(struct linepos) * from->in_use);
2895 }
2896}
2897
2898/*
2899 * Like copy_sub() but exclude the main match.
2900 */
2901 static void
2902copy_sub_off(to, from)
2903 regsub_T *to;
2904 regsub_T *from;
2905{
2906 if (to->in_use < from->in_use)
2907 to->in_use = from->in_use;
2908 if (from->in_use > 1)
2909 {
2910 /* Copy the match start and end positions. */
2911 if (REG_MULTI)
2912 mch_memmove(&to->list.multi[1],
2913 &from->list.multi[1],
2914 sizeof(struct multipos) * (from->in_use - 1));
2915 else
2916 mch_memmove(&to->list.line[1],
2917 &from->list.line[1],
2918 sizeof(struct linepos) * (from->in_use - 1));
2919 }
2920}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921
Bram Moolenaar428e9872013-05-30 17:05:39 +02002922/*
2923 * Return TRUE if "sub1" and "sub2" have the same positions.
2924 */
2925 static int
2926sub_equal(sub1, sub2)
2927 regsub_T *sub1;
2928 regsub_T *sub2;
2929{
2930 int i;
2931 int todo;
2932 linenr_T s1, e1;
2933 linenr_T s2, e2;
2934 char_u *sp1, *ep1;
2935 char_u *sp2, *ep2;
2936
2937 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
2938 if (REG_MULTI)
2939 {
2940 for (i = 0; i < todo; ++i)
2941 {
2942 if (i < sub1->in_use)
2943 {
2944 s1 = sub1->list.multi[i].start.lnum;
2945 e1 = sub1->list.multi[i].end.lnum;
2946 }
2947 else
2948 {
2949 s1 = 0;
2950 e1 = 0;
2951 }
2952 if (i < sub2->in_use)
2953 {
2954 s2 = sub2->list.multi[i].start.lnum;
2955 e2 = sub2->list.multi[i].end.lnum;
2956 }
2957 else
2958 {
2959 s2 = 0;
2960 e2 = 0;
2961 }
2962 if (s1 != s2 || e1 != e2)
2963 return FALSE;
2964 if (s1 != 0 && sub1->list.multi[i].start.col
2965 != sub2->list.multi[i].start.col)
2966 return FALSE;
2967 if (e1 != 0 && sub1->list.multi[i].end.col
2968 != sub2->list.multi[i].end.col)
2969 return FALSE;
2970 }
2971 }
2972 else
2973 {
2974 for (i = 0; i < todo; ++i)
2975 {
2976 if (i < sub1->in_use)
2977 {
2978 sp1 = sub1->list.line[i].start;
2979 ep1 = sub1->list.line[i].end;
2980 }
2981 else
2982 {
2983 sp1 = NULL;
2984 ep1 = NULL;
2985 }
2986 if (i < sub2->in_use)
2987 {
2988 sp2 = sub2->list.line[i].start;
2989 ep2 = sub2->list.line[i].end;
2990 }
2991 else
2992 {
2993 sp2 = NULL;
2994 ep2 = NULL;
2995 }
2996 if (sp1 != sp2 || ep1 != ep2)
2997 return FALSE;
2998 }
2999 }
3000
3001 return TRUE;
3002}
3003
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003004#ifdef ENABLE_LOG
3005 static void
3006report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid);
3007{
3008 int col;
3009
3010 if (sub->in_use <= 0)
3011 col = -1;
3012 else if (REG_MULTI)
3013 col = sub->list.multi[0].start.col;
3014 else
3015 col = (int)(sub->list.line[0].start - regline);
3016 nfa_set_code(state->c);
3017 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3018 action, abs(state->id), lid, state->c, code, col);
3019}
3020#endif
3021
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003022 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003023addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003024 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003025 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003026 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003027 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003028{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003029 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003030 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003031 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003032 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003033 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003034 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003035 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003036#ifdef ENABLE_LOG
3037 int did_print = FALSE;
3038#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003039
3040 if (l == NULL || state == NULL)
3041 return;
3042
3043 switch (state->c)
3044 {
3045 case NFA_SPLIT:
3046 case NFA_NOT:
3047 case NFA_NOPEN:
3048 case NFA_NCLOSE:
3049 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003050 case NFA_MCLOSE1:
3051 case NFA_MCLOSE2:
3052 case NFA_MCLOSE3:
3053 case NFA_MCLOSE4:
3054 case NFA_MCLOSE5:
3055 case NFA_MCLOSE6:
3056 case NFA_MCLOSE7:
3057 case NFA_MCLOSE8:
3058 case NFA_MCLOSE9:
3059#ifdef FEAT_SYN_HL
3060 case NFA_ZCLOSE:
3061 case NFA_ZCLOSE1:
3062 case NFA_ZCLOSE2:
3063 case NFA_ZCLOSE3:
3064 case NFA_ZCLOSE4:
3065 case NFA_ZCLOSE5:
3066 case NFA_ZCLOSE6:
3067 case NFA_ZCLOSE7:
3068 case NFA_ZCLOSE8:
3069 case NFA_ZCLOSE9:
3070#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003071 /* These nodes are not added themselves but their "out" and/or
3072 * "out1" may be added below. */
3073 break;
3074
3075 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003076 case NFA_MOPEN1:
3077 case NFA_MOPEN2:
3078 case NFA_MOPEN3:
3079 case NFA_MOPEN4:
3080 case NFA_MOPEN5:
3081 case NFA_MOPEN6:
3082 case NFA_MOPEN7:
3083 case NFA_MOPEN8:
3084 case NFA_MOPEN9:
3085#ifdef FEAT_SYN_HL
3086 case NFA_ZOPEN:
3087 case NFA_ZOPEN1:
3088 case NFA_ZOPEN2:
3089 case NFA_ZOPEN3:
3090 case NFA_ZOPEN4:
3091 case NFA_ZOPEN5:
3092 case NFA_ZOPEN6:
3093 case NFA_ZOPEN7:
3094 case NFA_ZOPEN8:
3095 case NFA_ZOPEN9:
3096#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003097 /* These nodes do not need to be added, but we need to bail out
3098 * when it was tried to be added to this list before. */
3099 if (state->lastlist == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003100 goto skip_add;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003101 state->lastlist = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003102 break;
3103
Bram Moolenaar307aa162013-06-02 16:34:21 +02003104 case NFA_BOL:
3105 case NFA_BOF:
3106 /* "^" won't match past end-of-line, don't bother trying.
3107 * Except when we are going to the next line for a look-behind
3108 * match. */
3109 if (reginput > regline
3110 && (nfa_endp == NULL
3111 || !REG_MULTI
3112 || reglnum == nfa_endp->se_u.pos.lnum))
3113 goto skip_add;
3114 /* FALLTHROUGH */
3115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003116 default:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003117 if (state->lastlist == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003119 /* This state is already in the list, don't add it again,
3120 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003121 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003122 {
3123skip_add:
3124#ifdef ENABLE_LOG
3125 nfa_set_code(state->c);
3126 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3127 abs(state->id), l->id, state->c, code);
3128#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003129 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003130 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003131
3132 /* See if the same state is already in the list with the same
3133 * positions. */
3134 for (i = 0; i < l->n; ++i)
3135 {
3136 thread = &l->t[i];
3137 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003138 && sub_equal(&thread->subs.norm, &subs->norm)
3139#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003140 && (!nfa_has_zsubexpr ||
3141 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003142#endif
3143 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003144 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003145 }
3146 }
3147
3148 /* when there are backreferences the number of states may be (a
3149 * lot) bigger */
3150 if (nfa_has_backref && l->n == l->len)
3151 {
3152 int newlen = l->len * 3 / 2 + 50;
3153
3154 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3155 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003156 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003157
3158 /* add the state to the list */
3159 state->lastlist = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003160 thread = &l->t[l->n++];
3161 thread->state = state;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003162 copy_sub(&thread->subs.norm, &subs->norm);
3163#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003164 if (nfa_has_zsubexpr)
3165 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003166#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003167#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003168 report_state("Adding", &thread->subs.norm, state, l->id);
3169 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003170#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003171 }
3172
3173#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003174 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003175 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003176#endif
3177 switch (state->c)
3178 {
3179 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003180 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003181 break;
3182
3183 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003184 /* order matters here */
3185 addstate(l, state->out, subs, off);
3186 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003187 break;
3188
Bram Moolenaar5714b802013-05-28 22:03:20 +02003189 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003190 case NFA_NOPEN:
3191 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003192 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 break;
3194
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003195 case NFA_MOPEN:
3196 case NFA_MOPEN1:
3197 case NFA_MOPEN2:
3198 case NFA_MOPEN3:
3199 case NFA_MOPEN4:
3200 case NFA_MOPEN5:
3201 case NFA_MOPEN6:
3202 case NFA_MOPEN7:
3203 case NFA_MOPEN8:
3204 case NFA_MOPEN9:
3205#ifdef FEAT_SYN_HL
3206 case NFA_ZOPEN:
3207 case NFA_ZOPEN1:
3208 case NFA_ZOPEN2:
3209 case NFA_ZOPEN3:
3210 case NFA_ZOPEN4:
3211 case NFA_ZOPEN5:
3212 case NFA_ZOPEN6:
3213 case NFA_ZOPEN7:
3214 case NFA_ZOPEN8:
3215 case NFA_ZOPEN9:
3216#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003217 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003219 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003221 sub = &subs->norm;
3222 }
3223#ifdef FEAT_SYN_HL
3224 else if (state->c >= NFA_ZOPEN)
3225 {
3226 subidx = state->c - NFA_ZOPEN;
3227 sub = &subs->synt;
3228 }
3229#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003230 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003231 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003232 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003233 sub = &subs->norm;
3234 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003235
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003236 /* Set the position (with "off") in the subexpression. Save and
3237 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003238 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 if (REG_MULTI)
3240 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003241 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003242 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003243 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003244 save_in_use = -1;
3245 }
3246 else
3247 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003248 save_in_use = sub->in_use;
3249 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003250 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003251 sub->list.multi[i].start.lnum = -1;
3252 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003253 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003254 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003255 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003256 if (off == -1)
3257 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003258 sub->list.multi[subidx].start.lnum = reglnum + 1;
3259 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003260 }
3261 else
3262 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003263 sub->list.multi[subidx].start.lnum = reglnum;
3264 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003265 (colnr_T)(reginput - regline + off);
3266 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267 }
3268 else
3269 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003270 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003271 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003272 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003273 save_in_use = -1;
3274 }
3275 else
3276 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003277 save_in_use = sub->in_use;
3278 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003279 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003280 sub->list.line[i].start = NULL;
3281 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003282 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003283 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003284 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003285 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 }
3287
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003288 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003290 if (save_in_use == -1)
3291 {
3292 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003293 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003294 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003295 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003296 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003298 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003301 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003302 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003303 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003304 /* Do not overwrite the position set by \ze. If no \ze
3305 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003306 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 break;
3308 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003309 case NFA_MCLOSE1:
3310 case NFA_MCLOSE2:
3311 case NFA_MCLOSE3:
3312 case NFA_MCLOSE4:
3313 case NFA_MCLOSE5:
3314 case NFA_MCLOSE6:
3315 case NFA_MCLOSE7:
3316 case NFA_MCLOSE8:
3317 case NFA_MCLOSE9:
3318#ifdef FEAT_SYN_HL
3319 case NFA_ZCLOSE:
3320 case NFA_ZCLOSE1:
3321 case NFA_ZCLOSE2:
3322 case NFA_ZCLOSE3:
3323 case NFA_ZCLOSE4:
3324 case NFA_ZCLOSE5:
3325 case NFA_ZCLOSE6:
3326 case NFA_ZCLOSE7:
3327 case NFA_ZCLOSE8:
3328 case NFA_ZCLOSE9:
3329#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003331 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003332 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003334 sub = &subs->norm;
3335 }
3336#ifdef FEAT_SYN_HL
3337 else if (state->c >= NFA_ZCLOSE)
3338 {
3339 subidx = state->c - NFA_ZCLOSE;
3340 sub = &subs->synt;
3341 }
3342#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003343 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003344 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003345 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003346 sub = &subs->norm;
3347 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003349 /* We don't fill in gaps here, there must have been an MOPEN that
3350 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003351 save_in_use = sub->in_use;
3352 if (sub->in_use <= subidx)
3353 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 if (REG_MULTI)
3355 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003356 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003357 if (off == -1)
3358 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003359 sub->list.multi[subidx].end.lnum = reglnum + 1;
3360 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003361 }
3362 else
3363 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003364 sub->list.multi[subidx].end.lnum = reglnum;
3365 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003366 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003367 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 }
3369 else
3370 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003371 save_ptr = sub->list.line[subidx].end;
3372 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003373 }
3374
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003375 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376
3377 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003378 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003379 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003380 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003381 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003382 break;
3383 }
3384}
3385
3386/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003387 * Like addstate(), but the new state(s) are put at position "*ip".
3388 * Used for zero-width matches, next state to use is the added one.
3389 * This makes sure the order of states to be tried does not change, which
3390 * matters for alternatives.
3391 */
3392 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003393addstate_here(l, state, subs, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003394 nfa_list_T *l; /* runtime state list */
3395 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003396 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003397 int *ip;
3398{
3399 int tlen = l->n;
3400 int count;
3401 int i = *ip;
3402
3403 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003404 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003405
3406 /* when "*ip" was at the end of the list, nothing to do */
3407 if (i + 1 == tlen)
3408 return;
3409
3410 /* re-order to put the new state at the current position */
3411 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003412 if (count == 1)
3413 {
3414 /* overwrite the current state */
3415 l->t[i] = l->t[l->n - 1];
3416 }
3417 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003418 {
3419 /* make space for new states, then move them from the
3420 * end to the current position */
3421 mch_memmove(&(l->t[i + count]),
3422 &(l->t[i + 1]),
3423 sizeof(nfa_thread_T) * (l->n - i - 1));
3424 mch_memmove(&(l->t[i]),
3425 &(l->t[l->n - 1]),
3426 sizeof(nfa_thread_T) * count);
3427 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003428 --l->n;
3429 *ip = i - 1;
3430}
3431
3432/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003433 * Check character class "class" against current character c.
3434 */
3435 static int
3436check_char_class(class, c)
3437 int class;
3438 int c;
3439{
3440 switch (class)
3441 {
3442 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003443 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 return OK;
3445 break;
3446 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003447 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448 return OK;
3449 break;
3450 case NFA_CLASS_BLANK:
3451 if (c == ' ' || c == '\t')
3452 return OK;
3453 break;
3454 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003455 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 return OK;
3457 break;
3458 case NFA_CLASS_DIGIT:
3459 if (VIM_ISDIGIT(c))
3460 return OK;
3461 break;
3462 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003463 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464 return OK;
3465 break;
3466 case NFA_CLASS_LOWER:
3467 if (MB_ISLOWER(c))
3468 return OK;
3469 break;
3470 case NFA_CLASS_PRINT:
3471 if (vim_isprintc(c))
3472 return OK;
3473 break;
3474 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003475 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003476 return OK;
3477 break;
3478 case NFA_CLASS_SPACE:
3479 if ((c >=9 && c <= 13) || (c == ' '))
3480 return OK;
3481 break;
3482 case NFA_CLASS_UPPER:
3483 if (MB_ISUPPER(c))
3484 return OK;
3485 break;
3486 case NFA_CLASS_XDIGIT:
3487 if (vim_isxdigit(c))
3488 return OK;
3489 break;
3490 case NFA_CLASS_TAB:
3491 if (c == '\t')
3492 return OK;
3493 break;
3494 case NFA_CLASS_RETURN:
3495 if (c == '\r')
3496 return OK;
3497 break;
3498 case NFA_CLASS_BACKSPACE:
3499 if (c == '\b')
3500 return OK;
3501 break;
3502 case NFA_CLASS_ESCAPE:
3503 if (c == '\033')
3504 return OK;
3505 break;
3506
3507 default:
3508 /* should not be here :P */
3509 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3510 }
3511 return FAIL;
3512}
3513
Bram Moolenaar5714b802013-05-28 22:03:20 +02003514static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3515
3516/*
3517 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003518 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003519 */
3520 static int
3521match_backref(sub, subidx, bytelen)
3522 regsub_T *sub; /* pointers to subexpressions */
3523 int subidx;
3524 int *bytelen; /* out: length of match in bytes */
3525{
3526 int len;
3527
3528 if (sub->in_use <= subidx)
3529 {
3530retempty:
3531 /* backref was not set, match an empty string */
3532 *bytelen = 0;
3533 return TRUE;
3534 }
3535
3536 if (REG_MULTI)
3537 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003538 if (sub->list.multi[subidx].start.lnum < 0
3539 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003540 goto retempty;
3541 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003542 len = sub->list.multi[subidx].end.col
3543 - sub->list.multi[subidx].start.col;
3544 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003545 reginput, &len) == 0)
3546 {
3547 *bytelen = len;
3548 return TRUE;
3549 }
3550 }
3551 else
3552 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003553 if (sub->list.line[subidx].start == NULL
3554 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003555 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003556 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3557 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003558 {
3559 *bytelen = len;
3560 return TRUE;
3561 }
3562 }
3563 return FALSE;
3564}
3565
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003566#ifdef FEAT_SYN_HL
3567
3568static int match_zref __ARGS((int subidx, int *bytelen));
3569
3570/*
3571 * Check for a match with \z subexpression "subidx".
3572 * Return TRUE if it matches.
3573 */
3574 static int
3575match_zref(subidx, bytelen)
3576 int subidx;
3577 int *bytelen; /* out: length of match in bytes */
3578{
3579 int len;
3580
3581 cleanup_zsubexpr();
3582 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3583 {
3584 /* backref was not set, match an empty string */
3585 *bytelen = 0;
3586 return TRUE;
3587 }
3588
3589 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3590 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3591 {
3592 *bytelen = len;
3593 return TRUE;
3594 }
3595 return FALSE;
3596}
3597#endif
3598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003600 * Save list IDs for all NFA states of "prog" into "list".
3601 * Also reset the IDs to zero.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 */
3603 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003604nfa_save_listids(prog, list)
3605 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003606 int *list;
3607{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003608 int i;
3609 nfa_state_T *p;
3610
3611 /* Order in the list is reverse, it's a bit faster that way. */
3612 p = &prog->state[0];
3613 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003614 {
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003615 list[i] = p->lastlist;
3616 p->lastlist = 0;
3617 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003618 }
3619}
3620
3621/*
3622 * Restore list IDs from "list" to all NFA states.
3623 */
3624 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003625nfa_restore_listids(prog, list)
3626 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627 int *list;
3628{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003629 int i;
3630 nfa_state_T *p;
3631
3632 p = &prog->state[0];
3633 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634 {
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003635 p->lastlist = list[i];
3636 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637 }
3638}
3639
Bram Moolenaar423532e2013-05-29 21:14:42 +02003640 static int
3641nfa_re_num_cmp(val, op, pos)
3642 long_u val;
3643 int op;
3644 long_u pos;
3645{
3646 if (op == 1) return pos > val;
3647 if (op == 2) return pos < val;
3648 return val == pos;
3649}
3650
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003651static 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 +02003652
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003653/*
3654 * Main matching routine.
3655 *
3656 * Run NFA to determine whether it matches reginput.
3657 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003658 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003659 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 * Return TRUE if there is a match, FALSE otherwise.
3661 * Note: Caller must ensure that: start != NULL.
3662 */
3663 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003664nfa_regmatch(prog, start, submatch, m)
3665 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003666 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003667 regsubs_T *submatch;
3668 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003670 int result;
3671 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003673 int go_to_nextline = FALSE;
3674 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003675 nfa_list_T list[3];
3676 nfa_list_T *listtbl[2][2];
3677 nfa_list_T *ll;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 int listid = 1;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003679 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003680 nfa_list_T *thislist;
3681 nfa_list_T *nextlist;
3682 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003683 int *listids = NULL;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003684#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003685 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003686
3687 if (debug == NULL)
3688 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003689 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003690 return FALSE;
3691 }
3692#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003693 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003695 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003696 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003697 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3698 list[0].len = nstate + 1;
3699 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3700 list[1].len = nstate + 1;
3701 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3702 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3704 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705
3706#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003707 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003708 if (log_fd != NULL)
3709 {
3710 fprintf(log_fd, "**********************************\n");
3711 nfa_set_code(start->c);
3712 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3713 abs(start->id), code);
3714 fprintf(log_fd, "**********************************\n");
3715 }
3716 else
3717 {
3718 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3719 log_fd = stderr;
3720 }
3721#endif
3722
3723 thislist = &list[0];
3724 thislist->n = 0;
3725 nextlist = &list[1];
3726 nextlist->n = 0;
3727 neglist = &list[2];
3728 neglist->n = 0;
3729#ifdef ENABLE_LOG
3730 fprintf(log_fd, "(---) STARTSTATE\n");
3731#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003732 thislist->id = listid;
3733 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003734
3735 /* There are two cases when the NFA advances: 1. input char matches the
3736 * NFA node and 2. input char does not match the NFA node, but the next
3737 * node is NFA_NOT. The following macro calls addstate() according to
3738 * these rules. It is used A LOT, so use the "listtbl" table for speed */
3739 listtbl[0][0] = NULL;
3740 listtbl[0][1] = neglist;
3741 listtbl[1][0] = nextlist;
3742 listtbl[1][1] = NULL;
3743#define ADD_POS_NEG_STATE(node) \
3744 ll = listtbl[result ? 1 : 0][node->negated]; \
3745 if (ll != NULL) \
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003746 addstate(ll, node->out , &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747
3748
3749 /*
3750 * Run for each character.
3751 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003752 for (;;)
3753 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003754 int curc;
3755 int clen;
3756
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757#ifdef FEAT_MBYTE
3758 if (has_mbyte)
3759 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003760 curc = (*mb_ptr2char)(reginput);
3761 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003762 }
3763 else
3764#endif
3765 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003766 curc = *reginput;
3767 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003769 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02003770 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003771 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003772 go_to_nextline = FALSE;
3773 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774
3775 /* swap lists */
3776 thislist = &list[flag];
3777 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02003778 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779 listtbl[1][0] = nextlist;
3780 ++listid;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003781 thislist->id = listid;
3782 nextlist->id = listid + 1;
3783 neglist->id = listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003784
3785#ifdef ENABLE_LOG
3786 fprintf(log_fd, "------------------------------------------\n");
3787 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003788 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003789 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003790 {
3791 int i;
3792
3793 for (i = 0; i < thislist->n; i++)
3794 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
3795 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003796 fprintf(log_fd, "\n");
3797#endif
3798
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003799#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 fprintf(debug, "\n-------------------\n");
3801#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02003802 /*
3803 * If the state lists are empty we can stop.
3804 */
3805 if (thislist->n == 0 && neglist->n == 0)
3806 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807
3808 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003809 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003810 {
3811 if (neglist->n > 0)
3812 {
3813 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02003814 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003815 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003816 }
3817 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003818 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003819
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003820#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003821 nfa_set_code(t->state->c);
3822 fprintf(debug, "%s, ", code);
3823#endif
3824#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003825 {
3826 int col;
3827
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003828 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003829 col = -1;
3830 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003831 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003832 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02003833 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003834 nfa_set_code(t->state->c);
3835 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
3836 abs(t->state->id), (int)t->state->c, code, col);
3837 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003838#endif
3839
3840 /*
3841 * Handle the possible codes of the current state.
3842 * The most important is NFA_MATCH.
3843 */
3844 switch (t->state->c)
3845 {
3846 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003847 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003848 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003849 copy_sub(&submatch->norm, &t->subs.norm);
3850#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003851 if (nfa_has_zsubexpr)
3852 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003853#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003855 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003856#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02003857 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003858 * states at this position. When the list of states is going
3859 * to be empty quit without advancing, so that "reginput" is
3860 * correct. */
3861 if (nextlist->n == 0 && neglist->n == 0)
3862 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003863 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02003864 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003865
3866 case NFA_END_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003867 /* This is only encountered after a NFA_START_INVISIBLE or
3868 * NFA_START_INVISIBLE_BEFORE node.
3869 * They surround a zero-width group, used with "\@=", "\&",
3870 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003871 * If we got here, it means that the current "invisible" group
3872 * finished successfully, so return control to the parent
3873 * nfa_regmatch(). Submatches are stored in *m, and used in
3874 * the parent call. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003875 if (start->c == NFA_MOPEN)
Bram Moolenaar61602c52013-06-01 19:54:43 +02003876 /* TODO: do we ever get here? */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003877 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003878 else
3879 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003880#ifdef ENABLE_LOG
Bram Moolenaar307aa162013-06-02 16:34:21 +02003881 if (nfa_endp != NULL)
Bram Moolenaar61602c52013-06-01 19:54:43 +02003882 {
3883 if (REG_MULTI)
3884 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
3885 (int)reglnum,
Bram Moolenaar307aa162013-06-02 16:34:21 +02003886 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar61602c52013-06-01 19:54:43 +02003887 (int)(reginput - regline),
Bram Moolenaar307aa162013-06-02 16:34:21 +02003888 nfa_endp->se_u.pos.col);
Bram Moolenaar61602c52013-06-01 19:54:43 +02003889 else
3890 fprintf(log_fd, "Current col: %d, endp col: %d\n",
3891 (int)(reginput - regline),
Bram Moolenaar307aa162013-06-02 16:34:21 +02003892 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003893 }
3894#endif
Bram Moolenaar307aa162013-06-02 16:34:21 +02003895 /* It's only a match if it ends at "nfa_endp" */
3896 if (nfa_endp != NULL && (REG_MULTI
3897 ? (reglnum != nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02003898 || (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02003899 != nfa_endp->se_u.pos.col)
3900 : reginput != nfa_endp->se_u.ptr))
Bram Moolenaar61602c52013-06-01 19:54:43 +02003901 break;
3902
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003903 /* do not set submatches for \@! */
3904 if (!t->state->negated)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003905 {
3906 copy_sub(&m->norm, &t->subs.norm);
3907#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003908 if (nfa_has_zsubexpr)
3909 copy_sub(&m->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003910#endif
3911 }
Bram Moolenaar963fee22013-05-26 21:47:28 +02003912 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003913 }
3914 break;
3915
3916 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003917 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003918 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02003919 char_u *save_reginput = reginput;
3920 char_u *save_regline = regline;
3921 int save_reglnum = reglnum;
3922 int save_nfa_match = nfa_match;
Bram Moolenaar307aa162013-06-02 16:34:21 +02003923 save_se_T *save_nfa_endp = nfa_endp;
Bram Moolenaar61602c52013-06-01 19:54:43 +02003924 save_se_T endpos;
3925 save_se_T *endposp = NULL;
3926
3927 if (t->state->c == NFA_START_INVISIBLE_BEFORE)
3928 {
3929 /* The recursive match must end at the current position. */
3930 endposp = &endpos;
3931 if (REG_MULTI)
3932 {
3933 endpos.se_u.pos.col = (int)(reginput - regline);
3934 endpos.se_u.pos.lnum = reglnum;
3935 }
3936 else
3937 endpos.se_u.ptr = reginput;
3938
3939 /* Go back the specified number of bytes, or as far as the
3940 * start of the previous line, to try matching "\@<=" or
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003941 * not matching "\@<!".
3942 * TODO: This is very inefficient! Would be better to
3943 * first check for a match with what follows. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003944 if (t->state->val <= 0)
3945 {
3946 if (REG_MULTI)
3947 {
3948 regline = reg_getline(--reglnum);
3949 if (regline == NULL)
3950 /* can't go before the first line */
3951 regline = reg_getline(++reglnum);
3952 }
3953 reginput = regline;
3954 }
3955 else
3956 {
3957 if (REG_MULTI
3958 && (int)(reginput - regline) < t->state->val)
3959 {
3960 /* Not enough bytes in this line, go to end of
3961 * previous line. */
3962 regline = reg_getline(--reglnum);
3963 if (regline == NULL)
3964 {
3965 /* can't go before the first line */
3966 regline = reg_getline(++reglnum);
3967 reginput = regline;
3968 }
3969 else
3970 reginput = regline + STRLEN(regline);
3971 }
3972 if ((int)(reginput - regline) >= t->state->val)
3973 {
3974 reginput -= t->state->val;
3975#ifdef FEAT_MBYTE
3976 if (has_mbyte)
3977 reginput -= mb_head_off(regline, reginput);
3978#endif
3979 }
3980 else
3981 reginput = regline;
3982 }
3983 }
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003984
3985 /* Call nfa_regmatch() to check if the current concat matches
3986 * at this position. The concat ends with the node
3987 * NFA_END_INVISIBLE */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003988 if (listids == NULL)
3989 {
Bram Moolenaar14f55c62013-05-31 21:45:09 +02003990 listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003991 if (listids == NULL)
3992 {
3993 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3994 return 0;
3995 }
3996 }
3997#ifdef ENABLE_LOG
3998 if (log_fd != stderr)
3999 fclose(log_fd);
4000 log_fd = NULL;
4001#endif
4002 /* Have to clear the listid field of the NFA nodes, so that
4003 * nfa_regmatch() and addstate() can run properly after
4004 * recursion. */
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004005 nfa_save_listids(prog, listids);
Bram Moolenaar307aa162013-06-02 16:34:21 +02004006 nfa_endp = endposp;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004007 result = nfa_regmatch(prog, t->state->out, submatch, m);
4008 nfa_restore_listids(prog, listids);
Bram Moolenaar14f55c62013-05-31 21:45:09 +02004009
4010 /* restore position in input text */
4011 reginput = save_reginput;
4012 regline = save_regline;
4013 reglnum = save_reglnum;
4014 nfa_match = save_nfa_match;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004015 nfa_endp = save_nfa_endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004016
4017#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004018 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004019 if (log_fd != NULL)
4020 {
4021 fprintf(log_fd, "****************************\n");
4022 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4023 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4024 fprintf(log_fd, "****************************\n");
4025 }
4026 else
4027 {
4028 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4029 log_fd = stderr;
4030 }
4031#endif
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02004032 /* for \@! it is a match when result is FALSE */
4033 if (result != t->state->negated)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004034 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004035 /* Copy submatch info from the recursive call */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004036 copy_sub_off(&t->subs.norm, &m->norm);
4037#ifdef FEAT_SYN_HL
4038 copy_sub_off(&t->subs.synt, &m->synt);
4039#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004040
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004041 /* t->state->out1 is the corresponding END_INVISIBLE node;
4042 * Add it to the current list (zero-width match). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004043 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004044 &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004045 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046 break;
Bram Moolenaar14f55c62013-05-31 21:45:09 +02004047 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048
4049 case NFA_BOL:
4050 if (reginput == regline)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004051 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004052 break;
4053
4054 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004055 if (curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004056 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004057 break;
4058
4059 case NFA_BOW:
4060 {
4061 int bow = TRUE;
4062
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004063 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004064 bow = FALSE;
4065#ifdef FEAT_MBYTE
4066 else if (has_mbyte)
4067 {
4068 int this_class;
4069
4070 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004071 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072 if (this_class <= 1)
4073 bow = FALSE;
4074 else if (reg_prev_class() == this_class)
4075 bow = FALSE;
4076 }
4077#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004078 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004079 || (reginput > regline
4080 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004081 bow = FALSE;
4082 if (bow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004083 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004084 break;
4085 }
4086
4087 case NFA_EOW:
4088 {
4089 int eow = TRUE;
4090
4091 if (reginput == regline)
4092 eow = FALSE;
4093#ifdef FEAT_MBYTE
4094 else if (has_mbyte)
4095 {
4096 int this_class, prev_class;
4097
4098 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004099 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004100 prev_class = reg_prev_class();
4101 if (this_class == prev_class
4102 || prev_class == 0 || prev_class == 1)
4103 eow = FALSE;
4104 }
4105#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004106 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004107 || (reginput[0] != NUL
4108 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004109 eow = FALSE;
4110 if (eow)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004111 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004112 break;
4113 }
4114
Bram Moolenaar4b780632013-05-31 22:14:52 +02004115 case NFA_BOF:
4116 if (reglnum == 0 && reginput == regline
4117 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004118 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004119 break;
4120
4121 case NFA_EOF:
4122 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004123 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004124 break;
4125
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004126#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004127 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004128 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004129 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004130 int len = 0;
4131 nfa_state_T *end;
4132 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004133 int cchars[MAX_MCO];
4134 int ccount = 0;
4135 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004136
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004138 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004139 if (utf_iscomposing(sta->c))
4140 {
4141 /* Only match composing character(s), ignore base
4142 * character. Used for ".{composing}" and "{composing}"
4143 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004144 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004145 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004146 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004147 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004148 /* If \Z was present, then ignore composing characters.
4149 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004150 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004151 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004152 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004153 else
4154 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004155 while (sta->c != NFA_END_COMPOSING)
4156 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004157 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004158
4159 /* Check base character matches first, unless ignored. */
4160 else if (len > 0 || mc == sta->c)
4161 {
4162 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004163 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004164 len += mb_char2len(mc);
4165 sta = sta->out;
4166 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004167
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004168 /* We don't care about the order of composing characters.
4169 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004170 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004171 {
4172 mc = mb_ptr2char(reginput + len);
4173 cchars[ccount++] = mc;
4174 len += mb_char2len(mc);
4175 if (ccount == MAX_MCO)
4176 break;
4177 }
4178
4179 /* Check that each composing char in the pattern matches a
4180 * composing char in the text. We do not check if all
4181 * composing chars are matched. */
4182 result = OK;
4183 while (sta->c != NFA_END_COMPOSING)
4184 {
4185 for (j = 0; j < ccount; ++j)
4186 if (cchars[j] == sta->c)
4187 break;
4188 if (j == ccount)
4189 {
4190 result = FAIL;
4191 break;
4192 }
4193 sta = sta->out;
4194 }
4195 }
4196 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004197 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004198
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004199 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004200 ADD_POS_NEG_STATE(end);
4201 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004202 }
4203#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004204
4205 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004206 if (curc == NUL && !reg_line_lbr && REG_MULTI
4207 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004209 go_to_nextline = TRUE;
4210 /* Pass -1 for the offset, which means taking the position
4211 * at the start of the next line. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004212 addstate(nextlist, t->state->out, &t->subs, -1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004213 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004214 else if (curc == '\n' && reg_line_lbr)
4215 {
4216 /* match \n as if it is an ordinary character */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004217 addstate(nextlist, t->state->out, &t->subs, 1);
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004218 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004219 break;
4220
4221 case NFA_CLASS_ALNUM:
4222 case NFA_CLASS_ALPHA:
4223 case NFA_CLASS_BLANK:
4224 case NFA_CLASS_CNTRL:
4225 case NFA_CLASS_DIGIT:
4226 case NFA_CLASS_GRAPH:
4227 case NFA_CLASS_LOWER:
4228 case NFA_CLASS_PRINT:
4229 case NFA_CLASS_PUNCT:
4230 case NFA_CLASS_SPACE:
4231 case NFA_CLASS_UPPER:
4232 case NFA_CLASS_XDIGIT:
4233 case NFA_CLASS_TAB:
4234 case NFA_CLASS_RETURN:
4235 case NFA_CLASS_BACKSPACE:
4236 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004237 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004238 ADD_POS_NEG_STATE(t->state);
4239 break;
4240
4241 case NFA_END_NEG_RANGE:
4242 /* This follows a series of negated nodes, like:
4243 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004244 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004245 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004246 break;
4247
4248 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004249 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004250 if (curc > 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004251 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252 break;
4253
4254 /*
4255 * Character classes like \a for alpha, \d for digit etc.
4256 */
4257 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004258 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004259 ADD_POS_NEG_STATE(t->state);
4260 break;
4261
4262 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004263 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264 ADD_POS_NEG_STATE(t->state);
4265 break;
4266
4267 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004268 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 ADD_POS_NEG_STATE(t->state);
4270 break;
4271
4272 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004273 result = !VIM_ISDIGIT(curc)
4274 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 ADD_POS_NEG_STATE(t->state);
4276 break;
4277
4278 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004279 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004280 ADD_POS_NEG_STATE(t->state);
4281 break;
4282
4283 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004284 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004285 ADD_POS_NEG_STATE(t->state);
4286 break;
4287
4288 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004289 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290 ADD_POS_NEG_STATE(t->state);
4291 break;
4292
4293 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004294 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295 ADD_POS_NEG_STATE(t->state);
4296 break;
4297
4298 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004299 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004300 ADD_POS_NEG_STATE(t->state);
4301 break;
4302
4303 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004304 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004305 ADD_POS_NEG_STATE(t->state);
4306 break;
4307
4308 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004309 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004310 ADD_POS_NEG_STATE(t->state);
4311 break;
4312
4313 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004314 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004315 ADD_POS_NEG_STATE(t->state);
4316 break;
4317
4318 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004319 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004320 ADD_POS_NEG_STATE(t->state);
4321 break;
4322
4323 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004324 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004325 ADD_POS_NEG_STATE(t->state);
4326 break;
4327
4328 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004329 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 ADD_POS_NEG_STATE(t->state);
4331 break;
4332
4333 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004334 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335 ADD_POS_NEG_STATE(t->state);
4336 break;
4337
4338 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004339 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004340 ADD_POS_NEG_STATE(t->state);
4341 break;
4342
4343 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004344 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345 ADD_POS_NEG_STATE(t->state);
4346 break;
4347
4348 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004349 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350 ADD_POS_NEG_STATE(t->state);
4351 break;
4352
4353 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004354 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355 ADD_POS_NEG_STATE(t->state);
4356 break;
4357
4358 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004359 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004360 ADD_POS_NEG_STATE(t->state);
4361 break;
4362
4363 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004364 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004365 ADD_POS_NEG_STATE(t->state);
4366 break;
4367
4368 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004369 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004370 ADD_POS_NEG_STATE(t->state);
4371 break;
4372
4373 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004374 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375 ADD_POS_NEG_STATE(t->state);
4376 break;
4377
4378 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004379 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004380 ADD_POS_NEG_STATE(t->state);
4381 break;
4382
4383 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004384 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004385 ADD_POS_NEG_STATE(t->state);
4386 break;
4387
Bram Moolenaar5714b802013-05-28 22:03:20 +02004388 case NFA_BACKREF1:
4389 case NFA_BACKREF2:
4390 case NFA_BACKREF3:
4391 case NFA_BACKREF4:
4392 case NFA_BACKREF5:
4393 case NFA_BACKREF6:
4394 case NFA_BACKREF7:
4395 case NFA_BACKREF8:
4396 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004397#ifdef FEAT_SYN_HL
4398 case NFA_ZREF1:
4399 case NFA_ZREF2:
4400 case NFA_ZREF3:
4401 case NFA_ZREF4:
4402 case NFA_ZREF5:
4403 case NFA_ZREF6:
4404 case NFA_ZREF7:
4405 case NFA_ZREF8:
4406 case NFA_ZREF9:
4407#endif
4408 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004409 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004410 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004411 int bytelen;
4412
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004413 if (t->state->c <= NFA_BACKREF9)
4414 {
4415 subidx = t->state->c - NFA_BACKREF1 + 1;
4416 result = match_backref(&t->subs.norm, subidx, &bytelen);
4417 }
4418#ifdef FEAT_SYN_HL
4419 else
4420 {
4421 subidx = t->state->c - NFA_ZREF1 + 1;
4422 result = match_zref(subidx, &bytelen);
4423 }
4424#endif
4425
Bram Moolenaar5714b802013-05-28 22:03:20 +02004426 if (result)
4427 {
4428 if (bytelen == 0)
4429 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004430 /* empty match always works, output of NFA_SKIP to be
4431 * used next */
4432 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004433 &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004434 }
4435 else if (bytelen <= clen)
4436 {
4437 /* match current character, jump ahead to out of
4438 * NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004439 addstate(nextlist, t->state->out->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004440#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004441 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004442#endif
4443 }
4444 else
4445 {
4446 /* skip ofer the matched characters, set character
4447 * count in NFA_SKIP */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004448 addstate(nextlist, t->state->out, &t->subs, bytelen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004449 nextlist->t[nextlist->n - 1].count = bytelen - clen;
4450#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004451 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004452#endif
4453 }
4454
4455 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004456 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004457 }
4458 case NFA_SKIP:
4459 /* charater of previous matching \1 .. \9 */
4460 if (t->count - clen <= 0)
4461 {
4462 /* end of match, go to what follows */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004463 addstate(nextlist, t->state->out, &t->subs, clen);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004464#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004465 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004466#endif
4467 }
4468 else
4469 {
4470 /* add state again with decremented count */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004471 addstate(nextlist, t->state, &t->subs, 0);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004472 nextlist->t[nextlist->n - 1].count = t->count - clen;
4473#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004474 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004475#endif
4476 }
4477 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004478
4479 case NFA_SKIP_CHAR:
4480 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004481 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004482 /* TODO: should not happen? */
4483 break;
4484
Bram Moolenaar423532e2013-05-29 21:14:42 +02004485 case NFA_LNUM:
4486 case NFA_LNUM_GT:
4487 case NFA_LNUM_LT:
4488 result = (REG_MULTI &&
4489 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4490 (long_u)(reglnum + reg_firstlnum)));
4491 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004492 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004493 break;
4494
4495 case NFA_COL:
4496 case NFA_COL_GT:
4497 case NFA_COL_LT:
4498 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4499 (long_u)(reginput - regline) + 1);
4500 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004501 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004502 break;
4503
4504 case NFA_VCOL:
4505 case NFA_VCOL_GT:
4506 case NFA_VCOL_LT:
4507 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4508 (long_u)win_linetabsize(
4509 reg_win == NULL ? curwin : reg_win,
4510 regline, (colnr_T)(reginput - regline)) + 1);
4511 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004512 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004513 break;
4514
4515 case NFA_CURSOR:
4516 result = (reg_win != NULL
4517 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4518 && ((colnr_T)(reginput - regline)
4519 == reg_win->w_cursor.col));
4520 if (result)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004521 addstate_here(thislist, t->state->out, &t->subs, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004522 break;
4523
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004525 {
4526 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004527
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004528 /* TODO: put this in #ifdef later */
4529 if (c < -256)
4530 EMSGN("INTERNAL: Negative state char: %ld", c);
4531 if (is_Magic(c))
4532 c = un_Magic(c);
4533 result = (c == curc);
4534
4535 if (!result && ireg_ic)
4536 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004537#ifdef FEAT_MBYTE
4538 /* If there is a composing character which is not being
4539 * ignored there can be no match. Match with composing
4540 * character uses NFA_COMPOSING above. */
4541 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004542 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004543 result = FALSE;
4544#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004545 ADD_POS_NEG_STATE(t->state);
4546 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004547 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 }
4549
4550 } /* for (thislist = thislist; thislist->state; thislist++) */
4551
Bram Moolenaare23febd2013-05-26 18:40:14 +02004552 /* Look for the start of a match in the current position by adding the
4553 * start state to the list of states.
4554 * The first found match is the leftmost one, thus the order of states
4555 * matters!
4556 * Do not add the start state in recursive calls of nfa_regmatch(),
4557 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004558 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004559 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004560 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004561 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004562 && reglnum == 0
4563 && clen != 0
4564 && (ireg_maxcol == 0
4565 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004566 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004567 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004568 ? (reglnum < nfa_endp->se_u.pos.lnum
4569 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004570 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004571 < nfa_endp->se_u.pos.col))
4572 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004573 {
4574#ifdef ENABLE_LOG
4575 fprintf(log_fd, "(---) STARTSTATE\n");
4576#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004577 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578 }
4579
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580#ifdef ENABLE_LOG
4581 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004582 {
4583 int i;
4584
4585 for (i = 0; i < thislist->n; i++)
4586 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4587 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588 fprintf(log_fd, "\n");
4589#endif
4590
4591nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004592 /* Advance to the next character, or advance to the next line, or
4593 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004594 if (clen != 0)
4595 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004596 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4597 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004598 reg_nextline();
4599 else
4600 break;
4601 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004602
4603#ifdef ENABLE_LOG
4604 if (log_fd != stderr)
4605 fclose(log_fd);
4606 log_fd = NULL;
4607#endif
4608
4609theend:
4610 /* Free memory */
4611 vim_free(list[0].t);
4612 vim_free(list[1].t);
4613 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004614 vim_free(listids);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004615#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004616#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004617 fclose(debug);
4618#endif
4619
Bram Moolenaar963fee22013-05-26 21:47:28 +02004620 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004621}
4622
4623/*
4624 * Try match of "prog" with at regline["col"].
4625 * Returns 0 for failure, number of lines contained in the match otherwise.
4626 */
4627 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004628nfa_regtry(prog, col)
4629 nfa_regprog_T *prog;
4630 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004631{
4632 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004633 regsubs_T subs, m;
4634 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635#ifdef ENABLE_LOG
4636 FILE *f;
4637#endif
4638
4639 reginput = regline + col;
4640 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004641#ifdef FEAT_SYN_HL
4642 /* Clear the external match subpointers if necessary. */
4643 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004644 {
4645 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004646 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004647 }
4648 else
4649 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004650#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651
4652#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004653 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004654 if (f != NULL)
4655 {
4656 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
4657 fprintf(f, " =======================================================\n");
4658#ifdef DEBUG
4659 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
4660#endif
4661 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004662 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02004663 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004664 fprintf(f, "\n\n");
4665 fclose(f);
4666 }
4667 else
4668 EMSG(_("Could not open temporary log file for writing "));
4669#endif
4670
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004671 clear_sub(&subs.norm);
4672 clear_sub(&m.norm);
4673#ifdef FEAT_SYN_HL
4674 clear_sub(&subs.synt);
4675 clear_sub(&m.synt);
4676#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004678 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679 return 0;
4680
4681 cleanup_subexpr();
4682 if (REG_MULTI)
4683 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004684 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004686 reg_startpos[i] = subs.norm.list.multi[i].start;
4687 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 }
4689
4690 if (reg_startpos[0].lnum < 0)
4691 {
4692 reg_startpos[0].lnum = 0;
4693 reg_startpos[0].col = col;
4694 }
4695 if (reg_endpos[0].lnum < 0)
4696 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004697 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 reg_endpos[0].lnum = reglnum;
4699 reg_endpos[0].col = (int)(reginput - regline);
4700 }
4701 else
4702 /* Use line number of "\ze". */
4703 reglnum = reg_endpos[0].lnum;
4704 }
4705 else
4706 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004707 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004709 reg_startp[i] = subs.norm.list.line[i].start;
4710 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 }
4712
4713 if (reg_startp[0] == NULL)
4714 reg_startp[0] = regline + col;
4715 if (reg_endp[0] == NULL)
4716 reg_endp[0] = reginput;
4717 }
4718
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004719#ifdef FEAT_SYN_HL
4720 /* Package any found \z(...\) matches for export. Default is none. */
4721 unref_extmatch(re_extmatch_out);
4722 re_extmatch_out = NULL;
4723
4724 if (prog->reghasz == REX_SET)
4725 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004726 cleanup_zsubexpr();
4727 re_extmatch_out = make_extmatch();
4728 for (i = 0; i < subs.synt.in_use; i++)
4729 {
4730 if (REG_MULTI)
4731 {
4732 struct multipos *mpos = &subs.synt.list.multi[i];
4733
4734 /* Only accept single line matches. */
4735 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
4736 re_extmatch_out->matches[i] =
4737 vim_strnsave(reg_getline(mpos->start.lnum)
4738 + mpos->start.col,
4739 mpos->end.col - mpos->start.col);
4740 }
4741 else
4742 {
4743 struct linepos *lpos = &subs.synt.list.line[i];
4744
4745 if (lpos->start != NULL && lpos->end != NULL)
4746 re_extmatch_out->matches[i] =
4747 vim_strnsave(lpos->start,
4748 (int)(lpos->end - lpos->start));
4749 }
4750 }
4751 }
4752#endif
4753
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004754 return 1 + reglnum;
4755}
4756
4757/*
4758 * Match a regexp against a string ("line" points to the string) or multiple
4759 * lines ("line" is NULL, use reg_getline()).
4760 *
4761 * Returns 0 for failure, number of lines contained in the match otherwise.
4762 */
4763 static long
4764nfa_regexec_both(line, col)
4765 char_u *line;
4766 colnr_T col; /* column to start looking for match */
4767{
4768 nfa_regprog_T *prog;
4769 long retval = 0L;
4770 int i;
4771
4772 if (REG_MULTI)
4773 {
4774 prog = (nfa_regprog_T *)reg_mmatch->regprog;
4775 line = reg_getline((linenr_T)0); /* relative to the cursor */
4776 reg_startpos = reg_mmatch->startpos;
4777 reg_endpos = reg_mmatch->endpos;
4778 }
4779 else
4780 {
4781 prog = (nfa_regprog_T *)reg_match->regprog;
4782 reg_startp = reg_match->startp;
4783 reg_endp = reg_match->endp;
4784 }
4785
4786 /* Be paranoid... */
4787 if (prog == NULL || line == NULL)
4788 {
4789 EMSG(_(e_null));
4790 goto theend;
4791 }
4792
4793 /* If the start column is past the maximum column: no need to try. */
4794 if (ireg_maxcol > 0 && col >= ireg_maxcol)
4795 goto theend;
4796
4797 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
4798 if (prog->regflags & RF_ICASE)
4799 ireg_ic = TRUE;
4800 else if (prog->regflags & RF_NOICASE)
4801 ireg_ic = FALSE;
4802
4803#ifdef FEAT_MBYTE
4804 /* If pattern contains "\Z" overrule value of ireg_icombine */
4805 if (prog->regflags & RF_ICOMBINE)
4806 ireg_icombine = TRUE;
4807#endif
4808
4809 regline = line;
4810 reglnum = 0; /* relative to line */
4811
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004812 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004813 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004814 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004815#ifdef DEBUG
4816 nfa_regengine.expr = prog->pattern;
4817#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004818
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004819 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004820 for (i = 0; i < nstate; ++i)
4821 {
4822 prog->state[i].id = i;
4823 prog->state[i].lastlist = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004824 }
4825
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004826 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004827
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004828#ifdef DEBUG
4829 nfa_regengine.expr = NULL;
4830#endif
4831
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004832theend:
4833 return retval;
4834}
4835
4836/*
4837 * Compile a regular expression into internal code for the NFA matcher.
4838 * Returns the program in allocated space. Returns NULL for an error.
4839 */
4840 static regprog_T *
4841nfa_regcomp(expr, re_flags)
4842 char_u *expr;
4843 int re_flags;
4844{
Bram Moolenaaraae48832013-05-25 21:18:34 +02004845 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02004846 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004847 int *postfix;
4848
4849 if (expr == NULL)
4850 return NULL;
4851
4852#ifdef DEBUG
4853 nfa_regengine.expr = expr;
4854#endif
4855
4856 init_class_tab();
4857
4858 if (nfa_regcomp_start(expr, re_flags) == FAIL)
4859 return NULL;
4860
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004861 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02004862 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863 postfix = re2post();
4864 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004865 {
4866 /* TODO: only give this error for debugging? */
4867 if (post_ptr >= post_end)
4868 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004869 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004870 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004871
4872 /*
4873 * In order to build the NFA, we parse the input regexp twice:
4874 * 1. first pass to count size (so we can allocate space)
4875 * 2. second to emit code
4876 */
4877#ifdef ENABLE_LOG
4878 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004879 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880
4881 if (f != NULL)
4882 {
4883 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
4884 fclose(f);
4885 }
4886 }
4887#endif
4888
4889 /*
4890 * PASS 1
4891 * Count number of NFA states in "nstate". Do not build the NFA.
4892 */
4893 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02004894
4895 /* Space for compiled regexp */
4896 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
4897 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
4898 if (prog == NULL)
4899 goto fail;
4900 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901 state_ptr = prog->state;
4902
4903 /*
4904 * PASS 2
4905 * Build the NFA
4906 */
4907 prog->start = post2nfa(postfix, post_ptr, FALSE);
4908 if (prog->start == NULL)
4909 goto fail;
4910
4911 prog->regflags = regflags;
4912 prog->engine = &nfa_regengine;
4913 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004914 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004915 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004916 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917#ifdef ENABLE_LOG
4918 nfa_postfix_dump(expr, OK);
4919 nfa_dump(prog);
4920#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004921#ifdef FEAT_SYN_HL
4922 /* Remember whether this pattern has any \z specials in it. */
4923 prog->reghasz = re_has_z;
4924#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004925#ifdef DEBUG
4926 prog->pattern = vim_strsave(expr); /* memory will leak */
4927 nfa_regengine.expr = NULL;
4928#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004929
4930out:
4931 vim_free(post_start);
4932 post_start = post_ptr = post_end = NULL;
4933 state_ptr = NULL;
4934 return (regprog_T *)prog;
4935
4936fail:
4937 vim_free(prog);
4938 prog = NULL;
4939#ifdef ENABLE_LOG
4940 nfa_postfix_dump(expr, FAIL);
4941#endif
4942#ifdef DEBUG
4943 nfa_regengine.expr = NULL;
4944#endif
4945 goto out;
4946}
4947
4948
4949/*
4950 * Match a regexp against a string.
4951 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
4952 * Uses curbuf for line count and 'iskeyword'.
4953 *
4954 * Return TRUE if there is a match, FALSE if not.
4955 */
4956 static int
4957nfa_regexec(rmp, line, col)
4958 regmatch_T *rmp;
4959 char_u *line; /* string to match against */
4960 colnr_T col; /* column to start looking for match */
4961{
4962 reg_match = rmp;
4963 reg_mmatch = NULL;
4964 reg_maxline = 0;
4965 reg_line_lbr = FALSE;
4966 reg_buf = curbuf;
4967 reg_win = NULL;
4968 ireg_ic = rmp->rm_ic;
4969#ifdef FEAT_MBYTE
4970 ireg_icombine = FALSE;
4971#endif
4972 ireg_maxcol = 0;
4973 return (nfa_regexec_both(line, col) != 0);
4974}
4975
4976#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
4977 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
4978
4979static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
4980
4981/*
4982 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
4983 */
4984 static int
4985nfa_regexec_nl(rmp, line, col)
4986 regmatch_T *rmp;
4987 char_u *line; /* string to match against */
4988 colnr_T col; /* column to start looking for match */
4989{
4990 reg_match = rmp;
4991 reg_mmatch = NULL;
4992 reg_maxline = 0;
4993 reg_line_lbr = TRUE;
4994 reg_buf = curbuf;
4995 reg_win = NULL;
4996 ireg_ic = rmp->rm_ic;
4997#ifdef FEAT_MBYTE
4998 ireg_icombine = FALSE;
4999#endif
5000 ireg_maxcol = 0;
5001 return (nfa_regexec_both(line, col) != 0);
5002}
5003#endif
5004
5005
5006/*
5007 * Match a regexp against multiple lines.
5008 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5009 * Uses curbuf for line count and 'iskeyword'.
5010 *
5011 * Return zero if there is no match. Return number of lines contained in the
5012 * match otherwise.
5013 *
5014 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5015 *
5016 * ! Also NOTE : match may actually be in another line. e.g.:
5017 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5018 *
5019 * +-------------------------+
5020 * |a |
5021 * |b |
5022 * |c |
5023 * | |
5024 * +-------------------------+
5025 *
5026 * then nfa_regexec_multi() returns 3. while the original
5027 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5028 *
5029 * FIXME if this behavior is not compatible.
5030 */
5031 static long
5032nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5033 regmmatch_T *rmp;
5034 win_T *win; /* window in which to search or NULL */
5035 buf_T *buf; /* buffer in which to search */
5036 linenr_T lnum; /* nr of line to start looking for match */
5037 colnr_T col; /* column to start looking for match */
5038 proftime_T *tm UNUSED; /* timeout limit or NULL */
5039{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040 reg_match = NULL;
5041 reg_mmatch = rmp;
5042 reg_buf = buf;
5043 reg_win = win;
5044 reg_firstlnum = lnum;
5045 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5046 reg_line_lbr = FALSE;
5047 ireg_ic = rmp->rmm_ic;
5048#ifdef FEAT_MBYTE
5049 ireg_icombine = FALSE;
5050#endif
5051 ireg_maxcol = rmp->rmm_maxcol;
5052
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005053 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005054}
5055
5056#ifdef DEBUG
5057# undef ENABLE_LOG
5058#endif