blob: 0e804c2ee8e0e577c1374b359726b515cf9b22b8 [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 */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200181 NFA_MARK, /* Match mark */
182 NFA_MARK_GT, /* Match > mark */
183 NFA_MARK_LT, /* Match < mark */
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200184#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200185 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200186#endif
Bram Moolenaar423532e2013-05-29 21:14:42 +0200187
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200188 NFA_FIRST_NL = NFA_ANY + ADD_NL,
189 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
190
191 /* Character classes [:alnum:] etc */
192 NFA_CLASS_ALNUM,
193 NFA_CLASS_ALPHA,
194 NFA_CLASS_BLANK,
195 NFA_CLASS_CNTRL,
196 NFA_CLASS_DIGIT,
197 NFA_CLASS_GRAPH,
198 NFA_CLASS_LOWER,
199 NFA_CLASS_PRINT,
200 NFA_CLASS_PUNCT,
201 NFA_CLASS_SPACE,
202 NFA_CLASS_UPPER,
203 NFA_CLASS_XDIGIT,
204 NFA_CLASS_TAB,
205 NFA_CLASS_RETURN,
206 NFA_CLASS_BACKSPACE,
207 NFA_CLASS_ESCAPE
208};
209
210/* Keep in sync with classchars. */
211static int nfa_classcodes[] = {
212 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
213 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
214 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
215 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
216 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
217 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
218 NFA_UPPER, NFA_NUPPER
219};
220
221static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
222
223/*
224 * NFA errors can be of 3 types:
225 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails
226 * silently and revert the to backtracking engine.
227 * syntax_error = FALSE;
228 * *** Regexp syntax errors, when the input regexp is not syntactically correct.
229 * The NFA engine displays an error message, and nothing else happens.
230 * syntax_error = TRUE
231 * *** Unsupported features, when the input regexp uses an operator that is not
232 * implemented in the NFA. The NFA engine fails silently, and reverts to the
233 * old backtracking engine.
234 * syntax_error = FALSE
235 * "The NFA fails" means that "compiling the regexp with the NFA fails":
236 * nfa_regcomp() returns FAIL.
237 */
238static int syntax_error = FALSE;
239
240/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200241static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200242
Bram Moolenaar428e9872013-05-30 17:05:39 +0200243/* NFA regexp \1 .. \9 encountered. */
244static int nfa_has_backref;
245
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200246#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200247/* NFA regexp has \z( ), set zsubexpr. */
248static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200249#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200250
Bram Moolenaar963fee22013-05-26 21:47:28 +0200251/* Number of sub expressions actually being used during execution. 1 if only
252 * the whole match (subexpr 0) is used. */
253static int nfa_nsubexpr;
254
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200255static int *post_start; /* holds the postfix form of r.e. */
256static int *post_end;
257static int *post_ptr;
258
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200259static int nstate; /* Number of states in the NFA. Also used when
260 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200261static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262
Bram Moolenaar307aa162013-06-02 16:34:21 +0200263/* If not NULL match must end at this position */
264static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200266/* listid is global, so that it increases on recursive calls to
267 * nfa_regmatch(), which means we don't have to clear the lastlist field of
268 * all the states. */
269static int nfa_listid;
270static int nfa_alt_listid;
271
272/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
273static int nfa_ll_index = 0;
274
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
276static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
277static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200278static int nfa_regatom __ARGS((void));
279static int nfa_regpiece __ARGS((void));
280static int nfa_regconcat __ARGS((void));
281static int nfa_regbranch __ARGS((void));
282static int nfa_reg __ARGS((int paren));
283#ifdef DEBUG
284static void nfa_set_code __ARGS((int c));
285static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200286static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
287static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200288static void nfa_dump __ARGS((nfa_regprog_T *prog));
289#endif
290static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200291static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
293static int check_char_class __ARGS((int class, int c));
294static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200295static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
296static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200297static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200298static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200299static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
300static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
301static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
302static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
303
304/* helper functions used when doing re2post() ... regatom() parsing */
305#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200306 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307 return FAIL; \
308 *post_ptr++ = c; \
309 } while (0)
310
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311/*
312 * Initialize internal variables before NFA compilation.
313 * Return OK on success, FAIL otherwise.
314 */
315 static int
316nfa_regcomp_start(expr, re_flags)
317 char_u *expr;
318 int re_flags; /* see vim_regcomp() */
319{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200320 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200321 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
323 nstate = 0;
324 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200325 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200326 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200328 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200329 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200330 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200331
332 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200333 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200334
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335 post_start = (int *)lalloc(postfix_size, TRUE);
336 if (post_start == NULL)
337 return FAIL;
338 vim_memset(post_start, 0, postfix_size);
339 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200340 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200342 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200344 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200345 regcomp_start(expr, re_flags);
346
347 return OK;
348}
349
350/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200351 * Allocate more space for post_start. Called when
352 * running above the estimated number of states.
353 */
354 static int
355realloc_post_list()
356{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200357 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200358 int new_max = nstate_max + 1000;
359 int *new_start;
360 int *old_start;
361
362 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
363 if (new_start == NULL)
364 return FAIL;
365 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
366 vim_memset(new_start + nstate_max, 0, 1000 * sizeof(int));
367 old_start = post_start;
368 post_start = new_start;
369 post_ptr = new_start + (post_ptr - old_start);
370 post_end = post_start + new_max;
371 vim_free(old_start);
372 return OK;
373}
374
375/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200376 * Search between "start" and "end" and try to recognize a
377 * character class in expanded form. For example [0-9].
378 * On success, return the id the character class to be emitted.
379 * On failure, return 0 (=FAIL)
380 * Start points to the first char of the range, while end should point
381 * to the closing brace.
382 */
383 static int
384nfa_recognize_char_class(start, end, extra_newl)
385 char_u *start;
386 char_u *end;
387 int extra_newl;
388{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200389# define CLASS_not 0x80
390# define CLASS_af 0x40
391# define CLASS_AF 0x20
392# define CLASS_az 0x10
393# define CLASS_AZ 0x08
394# define CLASS_o7 0x04
395# define CLASS_o9 0x02
396# define CLASS_underscore 0x01
397
398 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200399 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200400 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200401
402 if (extra_newl == TRUE)
403 newl = TRUE;
404
405 if (*end != ']')
406 return FAIL;
407 p = start;
408 if (*p == '^')
409 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200410 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200411 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200412 }
413
414 while (p < end)
415 {
416 if (p + 2 < end && *(p + 1) == '-')
417 {
418 switch (*p)
419 {
420 case '0':
421 if (*(p + 2) == '9')
422 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200423 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200424 break;
425 }
426 else
427 if (*(p + 2) == '7')
428 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200429 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200430 break;
431 }
432 case 'a':
433 if (*(p + 2) == 'z')
434 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200435 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200436 break;
437 }
438 else
439 if (*(p + 2) == 'f')
440 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200441 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200442 break;
443 }
444 case 'A':
445 if (*(p + 2) == 'Z')
446 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200447 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200448 break;
449 }
450 else
451 if (*(p + 2) == 'F')
452 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200453 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200454 break;
455 }
456 /* FALLTHROUGH */
457 default:
458 return FAIL;
459 }
460 p += 3;
461 }
462 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
463 {
464 newl = TRUE;
465 p += 2;
466 }
467 else if (*p == '_')
468 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200469 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200470 p ++;
471 }
472 else if (*p == '\n')
473 {
474 newl = TRUE;
475 p ++;
476 }
477 else
478 return FAIL;
479 } /* while (p < end) */
480
481 if (p != end)
482 return FAIL;
483
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200484 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200485 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200486
487 switch (config)
488 {
489 case CLASS_o9:
490 return extra_newl + NFA_DIGIT;
491 case CLASS_not | CLASS_o9:
492 return extra_newl + NFA_NDIGIT;
493 case CLASS_af | CLASS_AF | CLASS_o9:
494 return extra_newl + NFA_HEX;
495 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
496 return extra_newl + NFA_NHEX;
497 case CLASS_o7:
498 return extra_newl + NFA_OCTAL;
499 case CLASS_not | CLASS_o7:
500 return extra_newl + NFA_NOCTAL;
501 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
502 return extra_newl + NFA_WORD;
503 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
504 return extra_newl + NFA_NWORD;
505 case CLASS_az | CLASS_AZ | CLASS_underscore:
506 return extra_newl + NFA_HEAD;
507 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
508 return extra_newl + NFA_NHEAD;
509 case CLASS_az | CLASS_AZ:
510 return extra_newl + NFA_ALPHA;
511 case CLASS_not | CLASS_az | CLASS_AZ:
512 return extra_newl + NFA_NALPHA;
513 case CLASS_az:
514 return extra_newl + NFA_LOWER;
515 case CLASS_not | CLASS_az:
516 return extra_newl + NFA_NLOWER;
517 case CLASS_AZ:
518 return extra_newl + NFA_UPPER;
519 case CLASS_not | CLASS_AZ:
520 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200521 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200522 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200523}
524
525/*
526 * Produce the bytes for equivalence class "c".
527 * Currently only handles latin1, latin9 and utf-8.
528 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
529 * equivalent to 'a OR b OR c'
530 *
531 * NOTE! When changing this function, also update reg_equi_class()
532 */
533 static int
534nfa_emit_equi_class(c, neg)
535 int c;
536 int neg;
537{
538 int first = TRUE;
539 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
540#define EMIT2(c) \
541 EMIT(c); \
542 if (neg == TRUE) { \
543 EMIT(NFA_NOT); \
544 } \
545 if (first == FALSE) \
546 EMIT(glue); \
547 else \
548 first = FALSE; \
549
550#ifdef FEAT_MBYTE
551 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
552 || STRCMP(p_enc, "iso-8859-15") == 0)
553#endif
554 {
555 switch (c)
556 {
557 case 'A': case '\300': case '\301': case '\302':
558 case '\303': case '\304': case '\305':
559 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
560 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
561 EMIT2('\305');
562 return OK;
563
564 case 'C': case '\307':
565 EMIT2('C'); EMIT2('\307');
566 return OK;
567
568 case 'E': case '\310': case '\311': case '\312': case '\313':
569 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
570 EMIT2('\312'); EMIT2('\313');
571 return OK;
572
573 case 'I': case '\314': case '\315': case '\316': case '\317':
574 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
575 EMIT2('\316'); EMIT2('\317');
576 return OK;
577
578 case 'N': case '\321':
579 EMIT2('N'); EMIT2('\321');
580 return OK;
581
582 case 'O': case '\322': case '\323': case '\324': case '\325':
583 case '\326':
584 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
585 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
586 return OK;
587
588 case 'U': case '\331': case '\332': case '\333': case '\334':
589 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
590 EMIT2('\333'); EMIT2('\334');
591 return OK;
592
593 case 'Y': case '\335':
594 EMIT2('Y'); EMIT2('\335');
595 return OK;
596
597 case 'a': case '\340': case '\341': case '\342':
598 case '\343': case '\344': case '\345':
599 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
600 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
601 EMIT2('\345');
602 return OK;
603
604 case 'c': case '\347':
605 EMIT2('c'); EMIT2('\347');
606 return OK;
607
608 case 'e': case '\350': case '\351': case '\352': case '\353':
609 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
610 EMIT2('\352'); EMIT2('\353');
611 return OK;
612
613 case 'i': case '\354': case '\355': case '\356': case '\357':
614 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
615 EMIT2('\356'); EMIT2('\357');
616 return OK;
617
618 case 'n': case '\361':
619 EMIT2('n'); EMIT2('\361');
620 return OK;
621
622 case 'o': case '\362': case '\363': case '\364': case '\365':
623 case '\366':
624 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
625 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
626 return OK;
627
628 case 'u': case '\371': case '\372': case '\373': case '\374':
629 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
630 EMIT2('\373'); EMIT2('\374');
631 return OK;
632
633 case 'y': case '\375': case '\377':
634 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
635 return OK;
636
637 default:
638 return FAIL;
639 }
640 }
641
642 EMIT(c);
643 return OK;
644#undef EMIT2
645}
646
647/*
648 * Code to parse regular expression.
649 *
650 * We try to reuse parsing functions in regexp.c to
651 * minimize surprise and keep the syntax consistent.
652 */
653
654/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655 * Parse the lowest level.
656 *
657 * An atom can be one of a long list of items. Many atoms match one character
658 * in the text. It is often an ordinary character or a character class.
659 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
660 * is only for syntax highlighting.
661 *
662 * atom ::= ordinary-atom
663 * or \( pattern \)
664 * or \%( pattern \)
665 * or \z( pattern \)
666 */
667 static int
668nfa_regatom()
669{
670 int c;
671 int charclass;
672 int equiclass;
673 int collclass;
674 int got_coll_char;
675 char_u *p;
676 char_u *endp;
677#ifdef FEAT_MBYTE
678 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679#endif
680 int extra = 0;
681 int first;
682 int emit_range;
683 int negated;
684 int result;
685 int startc = -1;
686 int endc = -1;
687 int oldstartc = -1;
688 int cpo_lit; /* 'cpoptions' contains 'l' flag */
689 int cpo_bsl; /* 'cpoptions' contains '\' flag */
690 int glue; /* ID that will "glue" nodes together */
691
692 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
693 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
694
695 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696 switch (c)
697 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200698 case NUL:
699 syntax_error = TRUE;
700 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
701
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200702 case Magic('^'):
703 EMIT(NFA_BOL);
704 break;
705
706 case Magic('$'):
707 EMIT(NFA_EOL);
708#if defined(FEAT_SYN_HL) || defined(PROTO)
709 had_eol = TRUE;
710#endif
711 break;
712
713 case Magic('<'):
714 EMIT(NFA_BOW);
715 break;
716
717 case Magic('>'):
718 EMIT(NFA_EOW);
719 break;
720
721 case Magic('_'):
722 c = no_Magic(getchr());
723 if (c == '^') /* "\_^" is start-of-line */
724 {
725 EMIT(NFA_BOL);
726 break;
727 }
728 if (c == '$') /* "\_$" is end-of-line */
729 {
730 EMIT(NFA_EOL);
731#if defined(FEAT_SYN_HL) || defined(PROTO)
732 had_eol = TRUE;
733#endif
734 break;
735 }
736
737 extra = ADD_NL;
738
739 /* "\_[" is collection plus newline */
740 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200741 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200742
743 /* "\_x" is character class plus newline */
744 /*FALLTHROUGH*/
745
746 /*
747 * Character classes.
748 */
749 case Magic('.'):
750 case Magic('i'):
751 case Magic('I'):
752 case Magic('k'):
753 case Magic('K'):
754 case Magic('f'):
755 case Magic('F'):
756 case Magic('p'):
757 case Magic('P'):
758 case Magic('s'):
759 case Magic('S'):
760 case Magic('d'):
761 case Magic('D'):
762 case Magic('x'):
763 case Magic('X'):
764 case Magic('o'):
765 case Magic('O'):
766 case Magic('w'):
767 case Magic('W'):
768 case Magic('h'):
769 case Magic('H'):
770 case Magic('a'):
771 case Magic('A'):
772 case Magic('l'):
773 case Magic('L'):
774 case Magic('u'):
775 case Magic('U'):
776 p = vim_strchr(classchars, no_Magic(c));
777 if (p == NULL)
778 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200779 EMSGN("INTERNAL: Unknown character class char: %ld", c);
780 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 }
782#ifdef FEAT_MBYTE
783 /* When '.' is followed by a composing char ignore the dot, so that
784 * the composing char is matched here. */
785 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
786 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200787 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 c = getchr();
789 goto nfa_do_multibyte;
790 }
791#endif
792 EMIT(nfa_classcodes[p - classchars]);
793 if (extra == ADD_NL)
794 {
795 EMIT(NFA_NEWL);
796 EMIT(NFA_OR);
797 regflags |= RF_HASNL;
798 }
799 break;
800
801 case Magic('n'):
802 if (reg_string)
803 /* In a string "\n" matches a newline character. */
804 EMIT(NL);
805 else
806 {
807 /* In buffer text "\n" matches the end of a line. */
808 EMIT(NFA_NEWL);
809 regflags |= RF_HASNL;
810 }
811 break;
812
813 case Magic('('):
814 if (nfa_reg(REG_PAREN) == FAIL)
815 return FAIL; /* cascaded error */
816 break;
817
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200818 case Magic('|'):
819 case Magic('&'):
820 case Magic(')'):
821 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200822 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 return FAIL;
824
825 case Magic('='):
826 case Magic('?'):
827 case Magic('+'):
828 case Magic('@'):
829 case Magic('*'):
830 case Magic('{'):
831 /* these should follow an atom, not form an atom */
832 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200833 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200834 return FAIL;
835
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200836 case Magic('~'):
837 {
838 char_u *lp;
839
840 /* Previous substitute pattern.
841 * Generated as "\%(pattern\)". */
842 if (reg_prev_sub == NULL)
843 {
844 EMSG(_(e_nopresub));
845 return FAIL;
846 }
847 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
848 {
849 EMIT(PTR2CHAR(lp));
850 if (lp != reg_prev_sub)
851 EMIT(NFA_CONCAT);
852 }
853 EMIT(NFA_NOPEN);
854 break;
855 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200856
Bram Moolenaar428e9872013-05-30 17:05:39 +0200857 case Magic('1'):
858 case Magic('2'):
859 case Magic('3'):
860 case Magic('4'):
861 case Magic('5'):
862 case Magic('6'):
863 case Magic('7'):
864 case Magic('8'):
865 case Magic('9'):
866 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
867 nfa_has_backref = TRUE;
868 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869
870 case Magic('z'):
871 c = no_Magic(getchr());
872 switch (c)
873 {
874 case 's':
875 EMIT(NFA_ZSTART);
876 break;
877 case 'e':
878 EMIT(NFA_ZEND);
879 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200880 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200881#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200882 case '1':
883 case '2':
884 case '3':
885 case '4':
886 case '5':
887 case '6':
888 case '7':
889 case '8':
890 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200891 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200892 if (reg_do_extmatch != REX_USE)
893 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200894 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
895 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +0200896 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200897 re_has_z = REX_USE;
898 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200899 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200900 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200901 if (reg_do_extmatch != REX_SET)
902 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200903 if (nfa_reg(REG_ZPAREN) == FAIL)
904 return FAIL; /* cascaded error */
905 re_has_z = REX_SET;
906 break;
907#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200908 default:
909 syntax_error = TRUE;
Bram Moolenaarba404472013-05-19 22:31:18 +0200910 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200911 no_Magic(c));
912 return FAIL;
913 }
914 break;
915
916 case Magic('%'):
917 c = no_Magic(getchr());
918 switch (c)
919 {
920 /* () without a back reference */
921 case '(':
922 if (nfa_reg(REG_NPAREN) == FAIL)
923 return FAIL;
924 EMIT(NFA_NOPEN);
925 break;
926
927 case 'd': /* %d123 decimal */
928 case 'o': /* %o123 octal */
929 case 'x': /* %xab hex 2 */
930 case 'u': /* %uabcd hex 4 */
931 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +0200932 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200933 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200934
Bram Moolenaar47196582013-05-25 22:04:23 +0200935 switch (c)
936 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200937 case 'd': nr = getdecchrs(); break;
938 case 'o': nr = getoctchrs(); break;
939 case 'x': nr = gethexchrs(2); break;
940 case 'u': nr = gethexchrs(4); break;
941 case 'U': nr = gethexchrs(8); break;
942 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +0200943 }
944
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200945 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +0200946 EMSG2_RET_FAIL(
947 _("E678: Invalid character after %s%%[dxouU]"),
948 reg_magic == MAGIC_ALL);
949 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +0200950 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +0200951 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200952 break;
953
954 /* Catch \%^ and \%$ regardless of where they appear in the
955 * pattern -- regardless of whether or not it makes sense. */
956 case '^':
957 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 break;
959
960 case '$':
961 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200962 break;
963
964 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +0200965 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200966 break;
967
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200968#ifdef FEAT_VISUAL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200969 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200970 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +0200972#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200973
974 case '[':
Bram Moolenaar5714b802013-05-28 22:03:20 +0200975 /* TODO: \%[abc] not supported yet */
976 return FAIL;
977
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200978 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +0200979 {
Bram Moolenaar021e1472013-05-30 19:18:31 +0200980 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +0200981 int cmp = c;
982
983 if (c == '<' || c == '>')
984 c = getchr();
985 while (VIM_ISDIGIT(c))
986 {
987 n = n * 10 + (c - '0');
988 c = getchr();
989 }
990 if (c == 'l' || c == 'c' || c == 'v')
991 {
992 EMIT(n);
993 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200994 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200995 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +0200996 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +0200997 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +0200998 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200999 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001000 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001001 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001002 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001003 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001004 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001005 break;
1006 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001007 else if (c == '\'' && n == 0)
1008 {
1009 /* \%'m \%<'m \%>'m */
1010 EMIT(getchr());
1011 EMIT(cmp == '<' ? NFA_MARK_LT :
1012 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
1013 break;
1014 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001015 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001016 syntax_error = TRUE;
1017 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1018 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001019 return FAIL;
1020 }
1021 break;
1022
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001023 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001024collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001025 /*
1026 * Glue is emitted between several atoms from the [].
1027 * It is either NFA_OR, or NFA_CONCAT.
1028 *
1029 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1030 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1031 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1032 * notation)
1033 *
1034 */
1035
1036
1037/* Emit negation atoms, if needed.
1038 * The CONCAT below merges the NOT with the previous node. */
1039#define TRY_NEG() \
1040 if (negated == TRUE) \
1041 { \
1042 EMIT(NFA_NOT); \
1043 }
1044
1045/* Emit glue between important nodes : CONCAT or OR. */
1046#define EMIT_GLUE() \
1047 if (first == FALSE) \
1048 EMIT(glue); \
1049 else \
1050 first = FALSE;
1051
1052 p = regparse;
1053 endp = skip_anyof(p);
1054 if (*endp == ']')
1055 {
1056 /*
1057 * Try to reverse engineer character classes. For example,
1058 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1059 * and perform the necessary substitutions in the NFA.
1060 */
1061 result = nfa_recognize_char_class(regparse, endp,
1062 extra == ADD_NL);
1063 if (result != FAIL)
1064 {
1065 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1066 EMIT(result);
1067 else /* must be char class + newline */
1068 {
1069 EMIT(result - ADD_NL);
1070 EMIT(NFA_NEWL);
1071 EMIT(NFA_OR);
1072 }
1073 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001074 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001075 return OK;
1076 }
1077 /*
1078 * Failed to recognize a character class. Use the simple
1079 * version that turns [abc] into 'a' OR 'b' OR 'c'
1080 */
1081 startc = endc = oldstartc = -1;
1082 first = TRUE; /* Emitting first atom in this sequence? */
1083 negated = FALSE;
1084 glue = NFA_OR;
1085 if (*regparse == '^') /* negated range */
1086 {
1087 negated = TRUE;
1088 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001089 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001090 }
1091 if (*regparse == '-')
1092 {
1093 startc = '-';
1094 EMIT(startc);
1095 TRY_NEG();
1096 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001097 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001098 }
1099 /* Emit the OR branches for each character in the [] */
1100 emit_range = FALSE;
1101 while (regparse < endp)
1102 {
1103 oldstartc = startc;
1104 startc = -1;
1105 got_coll_char = FALSE;
1106 if (*regparse == '[')
1107 {
1108 /* Check for [: :], [= =], [. .] */
1109 equiclass = collclass = 0;
1110 charclass = get_char_class(&regparse);
1111 if (charclass == CLASS_NONE)
1112 {
1113 equiclass = get_equi_class(&regparse);
1114 if (equiclass == 0)
1115 collclass = get_coll_element(&regparse);
1116 }
1117
1118 /* Character class like [:alpha:] */
1119 if (charclass != CLASS_NONE)
1120 {
1121 switch (charclass)
1122 {
1123 case CLASS_ALNUM:
1124 EMIT(NFA_CLASS_ALNUM);
1125 break;
1126 case CLASS_ALPHA:
1127 EMIT(NFA_CLASS_ALPHA);
1128 break;
1129 case CLASS_BLANK:
1130 EMIT(NFA_CLASS_BLANK);
1131 break;
1132 case CLASS_CNTRL:
1133 EMIT(NFA_CLASS_CNTRL);
1134 break;
1135 case CLASS_DIGIT:
1136 EMIT(NFA_CLASS_DIGIT);
1137 break;
1138 case CLASS_GRAPH:
1139 EMIT(NFA_CLASS_GRAPH);
1140 break;
1141 case CLASS_LOWER:
1142 EMIT(NFA_CLASS_LOWER);
1143 break;
1144 case CLASS_PRINT:
1145 EMIT(NFA_CLASS_PRINT);
1146 break;
1147 case CLASS_PUNCT:
1148 EMIT(NFA_CLASS_PUNCT);
1149 break;
1150 case CLASS_SPACE:
1151 EMIT(NFA_CLASS_SPACE);
1152 break;
1153 case CLASS_UPPER:
1154 EMIT(NFA_CLASS_UPPER);
1155 break;
1156 case CLASS_XDIGIT:
1157 EMIT(NFA_CLASS_XDIGIT);
1158 break;
1159 case CLASS_TAB:
1160 EMIT(NFA_CLASS_TAB);
1161 break;
1162 case CLASS_RETURN:
1163 EMIT(NFA_CLASS_RETURN);
1164 break;
1165 case CLASS_BACKSPACE:
1166 EMIT(NFA_CLASS_BACKSPACE);
1167 break;
1168 case CLASS_ESCAPE:
1169 EMIT(NFA_CLASS_ESCAPE);
1170 break;
1171 }
1172 TRY_NEG();
1173 EMIT_GLUE();
1174 continue;
1175 }
1176 /* Try equivalence class [=a=] and the like */
1177 if (equiclass != 0)
1178 {
1179 result = nfa_emit_equi_class(equiclass, negated);
1180 if (result == FAIL)
1181 {
1182 /* should never happen */
1183 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1184 }
1185 EMIT_GLUE();
1186 continue;
1187 }
1188 /* Try collating class like [. .] */
1189 if (collclass != 0)
1190 {
1191 startc = collclass; /* allow [.a.]-x as a range */
1192 /* Will emit the proper atom at the end of the
1193 * while loop. */
1194 }
1195 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001196 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1197 * start character. */
1198 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001199 {
1200 emit_range = TRUE;
1201 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001202 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 continue; /* reading the end of the range */
1204 }
1205
1206 /* Now handle simple and escaped characters.
1207 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1208 * accepts "\t", "\e", etc., but only when the 'l' flag in
1209 * 'cpoptions' is not included.
1210 * Posix doesn't recognize backslash at all.
1211 */
1212 if (*regparse == '\\'
1213 && !cpo_bsl
1214 && regparse + 1 <= endp
1215 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1216 || (!cpo_lit
1217 && vim_strchr(REGEXP_ABBR, regparse[1])
1218 != NULL)
1219 )
1220 )
1221 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001222 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001224 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 startc = reg_string ? NL : NFA_NEWL;
1226 else
1227 if (*regparse == 'd'
1228 || *regparse == 'o'
1229 || *regparse == 'x'
1230 || *regparse == 'u'
1231 || *regparse == 'U'
1232 )
1233 {
1234 /* TODO(RE) This needs more testing */
1235 startc = coll_get_char();
1236 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001237 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 }
1239 else
1240 {
1241 /* \r,\t,\e,\b */
1242 startc = backslash_trans(*regparse);
1243 }
1244 }
1245
1246 /* Normal printable char */
1247 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001248 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001249
1250 /* Previous char was '-', so this char is end of range. */
1251 if (emit_range)
1252 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001253 endc = startc;
1254 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 if (startc > endc)
1256 EMSG_RET_FAIL(_(e_invrange));
1257#ifdef FEAT_MBYTE
1258 if (has_mbyte && ((*mb_char2len)(startc) > 1
1259 || (*mb_char2len)(endc) > 1))
1260 {
1261 if (endc > startc + 256)
1262 EMSG_RET_FAIL(_(e_invrange));
1263 /* Emit the range. "startc" was already emitted, so
1264 * skip it. */
1265 for (c = startc + 1; c <= endc; c++)
1266 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001267 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001268 TRY_NEG();
1269 EMIT_GLUE();
1270 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271 }
1272 else
1273#endif
1274 {
1275#ifdef EBCDIC
1276 int alpha_only = FALSE;
1277
1278 /* for alphabetical range skip the gaps
1279 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1280 if (isalpha(startc) && isalpha(endc))
1281 alpha_only = TRUE;
1282#endif
1283 /* Emit the range. "startc" was already emitted, so
1284 * skip it. */
1285 for (c = startc + 1; c <= endc; c++)
1286#ifdef EBCDIC
1287 if (!alpha_only || isalpha(startc))
1288#endif
1289 {
1290 EMIT(c);
1291 TRY_NEG();
1292 EMIT_GLUE();
1293 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001294 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001295 emit_range = FALSE;
1296 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001297 }
1298 else
1299 {
1300 /*
1301 * This char (startc) is not part of a range. Just
1302 * emit it.
1303 *
1304 * Normally, simply emit startc. But if we get char
1305 * code=0 from a collating char, then replace it with
1306 * 0x0a.
1307 *
1308 * This is needed to completely mimic the behaviour of
1309 * the backtracking engine.
1310 */
1311 if (got_coll_char == TRUE && startc == 0)
1312 EMIT(0x0a);
1313 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001314 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 TRY_NEG();
1316 EMIT_GLUE();
1317 }
1318
Bram Moolenaar51a29832013-05-28 22:30:35 +02001319 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 } /* while (p < endp) */
1321
Bram Moolenaar51a29832013-05-28 22:30:35 +02001322 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001323 if (*regparse == '-') /* if last, '-' is just a char */
1324 {
1325 EMIT('-');
1326 TRY_NEG();
1327 EMIT_GLUE();
1328 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001329 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 /* skip the trailing ] */
1332 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001333 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001334 if (negated == TRUE)
1335 {
1336 /* Mark end of negated char range */
1337 EMIT(NFA_END_NEG_RANGE);
1338 EMIT(NFA_CONCAT);
1339 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001340
1341 /* \_[] also matches \n but it's not negated */
1342 if (extra == ADD_NL)
1343 {
1344 EMIT(reg_string ? NL : NFA_NEWL);
1345 EMIT(NFA_OR);
1346 }
1347
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001349 } /* if exists closing ] */
1350
1351 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001352 {
1353 syntax_error = TRUE;
1354 EMSG_RET_FAIL(_(e_missingbracket));
1355 }
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001356 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 default:
1359 {
1360#ifdef FEAT_MBYTE
1361 int plen;
1362
1363nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001364 /* plen is length of current char with composing chars */
1365 if (enc_utf8 && ((*mb_char2len)(c)
1366 != (plen = (*mb_ptr2len)(old_regparse))
1367 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001368 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001369 int i = 0;
1370
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001371 /* A base character plus composing characters, or just one
1372 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001373 * This requires creating a separate atom as if enclosing
1374 * the characters in (), where NFA_COMPOSING is the ( and
1375 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001376 * building the postfix form, not the NFA itself;
1377 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001378 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001379 for (;;)
1380 {
1381 EMIT(c);
1382 if (i > 0)
1383 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001384 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001385 break;
1386 c = utf_ptr2char(old_regparse + i);
1387 }
1388 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001389 regparse = old_regparse + plen;
1390 }
1391 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001392#endif
1393 {
1394 c = no_Magic(c);
1395 EMIT(c);
1396 }
1397 return OK;
1398 }
1399 }
1400
1401#undef TRY_NEG
1402#undef EMIT_GLUE
1403
1404 return OK;
1405}
1406
1407/*
1408 * Parse something followed by possible [*+=].
1409 *
1410 * A piece is an atom, possibly followed by a multi, an indication of how many
1411 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1412 * characters: "", "a", "aa", etc.
1413 *
1414 * piece ::= atom
1415 * or atom multi
1416 */
1417 static int
1418nfa_regpiece()
1419{
1420 int i;
1421 int op;
1422 int ret;
1423 long minval, maxval;
1424 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001425 parse_state_T old_state;
1426 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001427 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001428 int old_post_pos;
1429 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001430 int quest;
1431
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001432 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1433 * next. */
1434 save_parse_state(&old_state);
1435
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001436 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001437 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001438
1439 ret = nfa_regatom();
1440 if (ret == FAIL)
1441 return FAIL; /* cascaded error */
1442
1443 op = peekchr();
1444 if (re_multi_type(op) == NOT_MULTI)
1445 return OK;
1446
1447 skipchr();
1448 switch (op)
1449 {
1450 case Magic('*'):
1451 EMIT(NFA_STAR);
1452 break;
1453
1454 case Magic('+'):
1455 /*
1456 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1457 * first and only submatch would be "aaa". But the backtracking
1458 * engine interprets the plus as "try matching one more time", and
1459 * a* matches a second time at the end of the input, the empty
1460 * string.
1461 * The submatch will the empty string.
1462 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001463 * In order to be consistent with the old engine, we replace
1464 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001466 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 curchr = -1;
1468 if (nfa_regatom() == FAIL)
1469 return FAIL;
1470 EMIT(NFA_STAR);
1471 EMIT(NFA_CONCAT);
1472 skipchr(); /* skip the \+ */
1473 break;
1474
1475 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001476 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001477 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001478 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001479 switch(op)
1480 {
1481 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001482 /* \@= */
1483 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001484 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001485 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001486 /* \@! */
1487 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001488 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001489 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001490 op = no_Magic(getchr());
1491 if (op == '=')
1492 /* \@<= */
1493 i = NFA_PREV_ATOM_JUST_BEFORE;
1494 else if (op == '!')
1495 /* \@<! */
1496 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1497 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 case '>':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001499 /* \@> Not supported yet */
1500 /* i = NFA_PREV_ATOM_LIKE_PATTERN; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 return FAIL;
1502 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001503 if (i == 0)
1504 {
1505 syntax_error = TRUE;
1506 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1507 return FAIL;
1508 }
1509 EMIT(i);
1510 if (i == NFA_PREV_ATOM_JUST_BEFORE
1511 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1512 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 break;
1514
1515 case Magic('?'):
1516 case Magic('='):
1517 EMIT(NFA_QUEST);
1518 break;
1519
1520 case Magic('{'):
1521 /* a{2,5} will expand to 'aaa?a?a?'
1522 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1523 * version of '?'
1524 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1525 * parenthesis have the same id
1526 */
1527
1528 greedy = TRUE;
1529 c2 = peekchr();
1530 if (c2 == '-' || c2 == Magic('-'))
1531 {
1532 skipchr();
1533 greedy = FALSE;
1534 }
1535 if (!read_limits(&minval, &maxval))
1536 {
1537 syntax_error = TRUE;
1538 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
1539 }
1540 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1541 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001542 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001544 if (greedy)
1545 /* \{}, \{0,} */
1546 EMIT(NFA_STAR);
1547 else
1548 /* \{-}, \{-0,} */
1549 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 break;
1551 }
1552
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 /* Special case: x{0} or x{-0} */
1554 if (maxval == 0)
1555 {
1556 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001557 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001558 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1559 EMIT(NFA_SKIP_CHAR);
1560 return OK;
1561 }
1562
1563 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001564 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001565 /* Save parse state after the repeated atom and the \{} */
1566 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001567
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001568 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1569 for (i = 0; i < maxval; i++)
1570 {
1571 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001572 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001573 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001574 if (nfa_regatom() == FAIL)
1575 return FAIL;
1576 /* after "minval" times, atoms are optional */
1577 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001578 {
1579 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001580 {
1581 if (greedy)
1582 EMIT(NFA_STAR);
1583 else
1584 EMIT(NFA_STAR_NONGREEDY);
1585 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001586 else
1587 EMIT(quest);
1588 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001589 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001590 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001591 if (i + 1 > minval && maxval == MAX_LIMIT)
1592 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 }
1594
1595 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001596 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001597 curchr = -1;
1598
1599 break;
1600
1601
1602 default:
1603 break;
1604 } /* end switch */
1605
1606 if (re_multi_type(peekchr()) != NOT_MULTI)
1607 {
1608 /* Can't have a multi follow a multi. */
1609 syntax_error = TRUE;
1610 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
1611 }
1612
1613 return OK;
1614}
1615
1616/*
1617 * Parse one or more pieces, concatenated. It matches a match for the
1618 * first piece, followed by a match for the second piece, etc. Example:
1619 * "f[0-9]b", first matches "f", then a digit and then "b".
1620 *
1621 * concat ::= piece
1622 * or piece piece
1623 * or piece piece piece
1624 * etc.
1625 */
1626 static int
1627nfa_regconcat()
1628{
1629 int cont = TRUE;
1630 int first = TRUE;
1631
1632 while (cont)
1633 {
1634 switch (peekchr())
1635 {
1636 case NUL:
1637 case Magic('|'):
1638 case Magic('&'):
1639 case Magic(')'):
1640 cont = FALSE;
1641 break;
1642
1643 case Magic('Z'):
1644#ifdef FEAT_MBYTE
1645 regflags |= RF_ICOMBINE;
1646#endif
1647 skipchr_keepstart();
1648 break;
1649 case Magic('c'):
1650 regflags |= RF_ICASE;
1651 skipchr_keepstart();
1652 break;
1653 case Magic('C'):
1654 regflags |= RF_NOICASE;
1655 skipchr_keepstart();
1656 break;
1657 case Magic('v'):
1658 reg_magic = MAGIC_ALL;
1659 skipchr_keepstart();
1660 curchr = -1;
1661 break;
1662 case Magic('m'):
1663 reg_magic = MAGIC_ON;
1664 skipchr_keepstart();
1665 curchr = -1;
1666 break;
1667 case Magic('M'):
1668 reg_magic = MAGIC_OFF;
1669 skipchr_keepstart();
1670 curchr = -1;
1671 break;
1672 case Magic('V'):
1673 reg_magic = MAGIC_NONE;
1674 skipchr_keepstart();
1675 curchr = -1;
1676 break;
1677
1678 default:
1679 if (nfa_regpiece() == FAIL)
1680 return FAIL;
1681 if (first == FALSE)
1682 EMIT(NFA_CONCAT);
1683 else
1684 first = FALSE;
1685 break;
1686 }
1687 }
1688
1689 return OK;
1690}
1691
1692/*
1693 * Parse a branch, one or more concats, separated by "\&". It matches the
1694 * last concat, but only if all the preceding concats also match at the same
1695 * position. Examples:
1696 * "foobeep\&..." matches "foo" in "foobeep".
1697 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1698 *
1699 * branch ::= concat
1700 * or concat \& concat
1701 * or concat \& concat \& concat
1702 * etc.
1703 */
1704 static int
1705nfa_regbranch()
1706{
1707 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001708 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001709
Bram Moolenaar16299b52013-05-30 18:45:23 +02001710 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001711
1712 /* First branch, possibly the only one */
1713 if (nfa_regconcat() == FAIL)
1714 return FAIL;
1715
1716 ch = peekchr();
1717 /* Try next concats */
1718 while (ch == Magic('&'))
1719 {
1720 skipchr();
1721 EMIT(NFA_NOPEN);
1722 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001723 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 if (nfa_regconcat() == FAIL)
1725 return FAIL;
1726 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001727 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001728 EMIT(NFA_SKIP_CHAR);
1729 EMIT(NFA_CONCAT);
1730 ch = peekchr();
1731 }
1732
1733 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001734 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 EMIT(NFA_SKIP_CHAR);
1736
1737 return OK;
1738}
1739
1740/*
1741 * Parse a pattern, one or more branches, separated by "\|". It matches
1742 * anything that matches one of the branches. Example: "foo\|beep" matches
1743 * "foo" and matches "beep". If more than one branch matches, the first one
1744 * is used.
1745 *
1746 * pattern ::= branch
1747 * or branch \| branch
1748 * or branch \| branch \| branch
1749 * etc.
1750 */
1751 static int
1752nfa_reg(paren)
1753 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1754{
1755 int parno = 0;
1756
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001757 if (paren == REG_PAREN)
1758 {
1759 if (regnpar >= NSUBEXP) /* Too many `(' */
1760 {
1761 syntax_error = TRUE;
1762 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
1763 }
1764 parno = regnpar++;
1765 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001766#ifdef FEAT_SYN_HL
1767 else if (paren == REG_ZPAREN)
1768 {
1769 /* Make a ZOPEN node. */
1770 if (regnzpar >= NSUBEXP)
1771 {
1772 syntax_error = TRUE;
1773 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
1774 }
1775 parno = regnzpar++;
1776 }
1777#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778
1779 if (nfa_regbranch() == FAIL)
1780 return FAIL; /* cascaded error */
1781
1782 while (peekchr() == Magic('|'))
1783 {
1784 skipchr();
1785 if (nfa_regbranch() == FAIL)
1786 return FAIL; /* cascaded error */
1787 EMIT(NFA_OR);
1788 }
1789
1790 /* Check for proper termination. */
1791 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1792 {
1793 syntax_error = TRUE;
1794 if (paren == REG_NPAREN)
1795 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1796 else
1797 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1798 }
1799 else if (paren == REG_NOPAREN && peekchr() != NUL)
1800 {
1801 syntax_error = TRUE;
1802 if (peekchr() == Magic(')'))
1803 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1804 else
1805 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1806 }
1807 /*
1808 * Here we set the flag allowing back references to this set of
1809 * parentheses.
1810 */
1811 if (paren == REG_PAREN)
1812 {
1813 had_endbrace[parno] = TRUE; /* have seen the close paren */
1814 EMIT(NFA_MOPEN + parno);
1815 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001816#ifdef FEAT_SYN_HL
1817 else if (paren == REG_ZPAREN)
1818 EMIT(NFA_ZOPEN + parno);
1819#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820
1821 return OK;
1822}
1823
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001824#ifdef DEBUG
1825static char_u code[50];
1826
1827 static void
1828nfa_set_code(c)
1829 int c;
1830{
1831 int addnl = FALSE;
1832
1833 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1834 {
1835 addnl = TRUE;
1836 c -= ADD_NL;
1837 }
1838
1839 STRCPY(code, "");
1840 switch (c)
1841 {
1842 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1843 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1844 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1845 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1846 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1847 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1848
Bram Moolenaar5714b802013-05-28 22:03:20 +02001849 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1850 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1851 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1852 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1853 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1854 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1855 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1856 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1857 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001858#ifdef FEAT_SYN_HL
1859 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1860 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1861 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1862 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1863 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1864 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1865 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1866 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1867 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1868#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001869 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 case NFA_PREV_ATOM_NO_WIDTH:
1872 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001873 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1874 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001875 case NFA_PREV_ATOM_JUST_BEFORE:
1876 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1877 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1878 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02001879 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
1880 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001882 case NFA_START_INVISIBLE_BEFORE:
1883 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
1885
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
1887 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
1888
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001889 case NFA_MOPEN:
1890 case NFA_MOPEN1:
1891 case NFA_MOPEN2:
1892 case NFA_MOPEN3:
1893 case NFA_MOPEN4:
1894 case NFA_MOPEN5:
1895 case NFA_MOPEN6:
1896 case NFA_MOPEN7:
1897 case NFA_MOPEN8:
1898 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 STRCPY(code, "NFA_MOPEN(x)");
1900 code[10] = c - NFA_MOPEN + '0';
1901 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001902 case NFA_MCLOSE:
1903 case NFA_MCLOSE1:
1904 case NFA_MCLOSE2:
1905 case NFA_MCLOSE3:
1906 case NFA_MCLOSE4:
1907 case NFA_MCLOSE5:
1908 case NFA_MCLOSE6:
1909 case NFA_MCLOSE7:
1910 case NFA_MCLOSE8:
1911 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 STRCPY(code, "NFA_MCLOSE(x)");
1913 code[11] = c - NFA_MCLOSE + '0';
1914 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001915#ifdef FEAT_SYN_HL
1916 case NFA_ZOPEN:
1917 case NFA_ZOPEN1:
1918 case NFA_ZOPEN2:
1919 case NFA_ZOPEN3:
1920 case NFA_ZOPEN4:
1921 case NFA_ZOPEN5:
1922 case NFA_ZOPEN6:
1923 case NFA_ZOPEN7:
1924 case NFA_ZOPEN8:
1925 case NFA_ZOPEN9:
1926 STRCPY(code, "NFA_ZOPEN(x)");
1927 code[10] = c - NFA_ZOPEN + '0';
1928 break;
1929 case NFA_ZCLOSE:
1930 case NFA_ZCLOSE1:
1931 case NFA_ZCLOSE2:
1932 case NFA_ZCLOSE3:
1933 case NFA_ZCLOSE4:
1934 case NFA_ZCLOSE5:
1935 case NFA_ZCLOSE6:
1936 case NFA_ZCLOSE7:
1937 case NFA_ZCLOSE8:
1938 case NFA_ZCLOSE9:
1939 STRCPY(code, "NFA_ZCLOSE(x)");
1940 code[11] = c - NFA_ZCLOSE + '0';
1941 break;
1942#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
1944 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
1945 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
1946 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02001947 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
1948 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02001949 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
1950 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
1951 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
1952 case NFA_COL: STRCPY(code, "NFA_COL "); break;
1953 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
1954 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
1955 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
1956 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
1957 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
1958 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
1959 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
1960 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
1961 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001962#ifdef FEAT_VISUAL
Bram Moolenaar044aa292013-06-04 21:27:38 +02001963 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02001964#endif
Bram Moolenaar044aa292013-06-04 21:27:38 +02001965
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001967 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
1968 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
1969 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
1971 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
1972 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001973 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
1974 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
1975 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
1976 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
1977 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
1978 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
1979 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
1980 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
1981 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
1982 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
1983 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
1984 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
1985 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
1986 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
1987 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
1988 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
1989 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
1990
1991 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
1992 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
1993 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
1994 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
1995 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
1996 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
1997 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
1998 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
1999 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2000 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2001 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2002 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2003 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2004 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2005 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2006 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2007 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2008 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2009 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2010 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2011 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2012 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2013 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2014 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2015 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2016 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2017 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2018
2019 default:
2020 STRCPY(code, "CHAR(x)");
2021 code[5] = c;
2022 }
2023
2024 if (addnl == TRUE)
2025 STRCAT(code, " + NEWLINE ");
2026
2027}
2028
2029#ifdef ENABLE_LOG
2030static FILE *log_fd;
2031
2032/*
2033 * Print the postfix notation of the current regexp.
2034 */
2035 static void
2036nfa_postfix_dump(expr, retval)
2037 char_u *expr;
2038 int retval;
2039{
2040 int *p;
2041 FILE *f;
2042
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002043 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 if (f != NULL)
2045 {
2046 fprintf(f, "\n-------------------------\n");
2047 if (retval == FAIL)
2048 fprintf(f, ">>> NFA engine failed ... \n");
2049 else if (retval == OK)
2050 fprintf(f, ">>> NFA engine succeeded !\n");
2051 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002052 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 {
2054 nfa_set_code(*p);
2055 fprintf(f, "%s, ", code);
2056 }
2057 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002058 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 fprintf(f, "%d ", *p);
2060 fprintf(f, "\n\n");
2061 fclose(f);
2062 }
2063}
2064
2065/*
2066 * Print the NFA starting with a root node "state".
2067 */
2068 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002069nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 FILE *debugf;
2071 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002073 garray_T indent;
2074
2075 ga_init2(&indent, 1, 64);
2076 ga_append(&indent, '\0');
2077 nfa_print_state2(debugf, state, &indent);
2078 ga_clear(&indent);
2079}
2080
2081 static void
2082nfa_print_state2(debugf, state, indent)
2083 FILE *debugf;
2084 nfa_state_T *state;
2085 garray_T *indent;
2086{
2087 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088
2089 if (state == NULL)
2090 return;
2091
2092 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002093
2094 /* Output indent */
2095 p = (char_u *)indent->ga_data;
2096 if (indent->ga_len >= 3)
2097 {
2098 int last = indent->ga_len - 3;
2099 char_u save[2];
2100
2101 STRNCPY(save, &p[last], 2);
2102 STRNCPY(&p[last], "+-", 2);
2103 fprintf(debugf, " %s", p);
2104 STRNCPY(&p[last], save, 2);
2105 }
2106 else
2107 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108
2109 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002110 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2111 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 if (state->id < 0)
2113 return;
2114
2115 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002116
2117 /* grow indent for state->out */
2118 indent->ga_len -= 1;
2119 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002120 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002121 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002122 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002123 ga_append(indent, '\0');
2124
2125 nfa_print_state2(debugf, state->out, indent);
2126
2127 /* replace last part of indent for state->out1 */
2128 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002129 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002130 ga_append(indent, '\0');
2131
2132 nfa_print_state2(debugf, state->out1, indent);
2133
2134 /* shrink indent */
2135 indent->ga_len -= 3;
2136 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002137}
2138
2139/*
2140 * Print the NFA state machine.
2141 */
2142 static void
2143nfa_dump(prog)
2144 nfa_regprog_T *prog;
2145{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002146 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147
2148 if (debugf != NULL)
2149 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002150 nfa_print_state(debugf, prog->start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002151 fclose(debugf);
2152 }
2153}
2154#endif /* ENABLE_LOG */
2155#endif /* DEBUG */
2156
2157/*
2158 * Parse r.e. @expr and convert it into postfix form.
2159 * Return the postfix string on success, NULL otherwise.
2160 */
2161 static int *
2162re2post()
2163{
2164 if (nfa_reg(REG_NOPAREN) == FAIL)
2165 return NULL;
2166 EMIT(NFA_MOPEN);
2167 return post_start;
2168}
2169
2170/* NB. Some of the code below is inspired by Russ's. */
2171
2172/*
2173 * Represents an NFA state plus zero or one or two arrows exiting.
2174 * if c == MATCH, no arrows out; matching state.
2175 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2176 * If c < 256, labeled arrow with character c to out.
2177 */
2178
2179static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2180
2181/*
2182 * Allocate and initialize nfa_state_T.
2183 */
2184 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002185alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002186 int c;
2187 nfa_state_T *out;
2188 nfa_state_T *out1;
2189{
2190 nfa_state_T *s;
2191
2192 if (istate >= nstate)
2193 return NULL;
2194
2195 s = &state_ptr[istate++];
2196
2197 s->c = c;
2198 s->out = out;
2199 s->out1 = out1;
2200
2201 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002202 s->lastlist[0] = 0;
2203 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204 s->negated = FALSE;
2205
2206 return s;
2207}
2208
2209/*
2210 * A partially built NFA without the matching state filled in.
2211 * Frag_T.start points at the start state.
2212 * Frag_T.out is a list of places that need to be set to the
2213 * next state for this fragment.
2214 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002215
2216/* Since the out pointers in the list are always
2217 * uninitialized, we use the pointers themselves
2218 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002220union Ptrlist
2221{
2222 Ptrlist *next;
2223 nfa_state_T *s;
2224};
2225
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002226struct Frag
2227{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002228 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 Ptrlist *out;
2230};
2231typedef struct Frag Frag_T;
2232
2233static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2234static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2235static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2236static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2237static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2238static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2239
2240/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002241 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002242 */
2243 static Frag_T
2244frag(start, out)
2245 nfa_state_T *start;
2246 Ptrlist *out;
2247{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002248 Frag_T n;
2249
2250 n.start = start;
2251 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252 return n;
2253}
2254
2255/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002256 * Create singleton list containing just outp.
2257 */
2258 static Ptrlist *
2259list1(outp)
2260 nfa_state_T **outp;
2261{
2262 Ptrlist *l;
2263
2264 l = (Ptrlist *)outp;
2265 l->next = NULL;
2266 return l;
2267}
2268
2269/*
2270 * Patch the list of states at out to point to start.
2271 */
2272 static void
2273patch(l, s)
2274 Ptrlist *l;
2275 nfa_state_T *s;
2276{
2277 Ptrlist *next;
2278
2279 for (; l; l = next)
2280 {
2281 next = l->next;
2282 l->s = s;
2283 }
2284}
2285
2286
2287/*
2288 * Join the two lists l1 and l2, returning the combination.
2289 */
2290 static Ptrlist *
2291append(l1, l2)
2292 Ptrlist *l1;
2293 Ptrlist *l2;
2294{
2295 Ptrlist *oldl1;
2296
2297 oldl1 = l1;
2298 while (l1->next)
2299 l1 = l1->next;
2300 l1->next = l2;
2301 return oldl1;
2302}
2303
2304/*
2305 * Stack used for transforming postfix form into NFA.
2306 */
2307static Frag_T empty;
2308
2309 static void
2310st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002311 int *postfix UNUSED;
2312 int *end UNUSED;
2313 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002315#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002316 FILE *df;
2317 int *p2;
2318
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002319 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320 if (df)
2321 {
2322 fprintf(df, "Error popping the stack!\n");
2323#ifdef DEBUG
2324 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2325#endif
2326 fprintf(df, "Postfix form is: ");
2327#ifdef DEBUG
2328 for (p2 = postfix; p2 < end; p2++)
2329 {
2330 nfa_set_code(*p2);
2331 fprintf(df, "%s, ", code);
2332 }
2333 nfa_set_code(*p);
2334 fprintf(df, "\nCurrent position is: ");
2335 for (p2 = postfix; p2 <= p; p2 ++)
2336 {
2337 nfa_set_code(*p2);
2338 fprintf(df, "%s, ", code);
2339 }
2340#else
2341 for (p2 = postfix; p2 < end; p2++)
2342 {
2343 fprintf(df, "%d, ", *p2);
2344 }
2345 fprintf(df, "\nCurrent position is: ");
2346 for (p2 = postfix; p2 <= p; p2 ++)
2347 {
2348 fprintf(df, "%d, ", *p2);
2349 }
2350#endif
2351 fprintf(df, "\n--------------------------\n");
2352 fclose(df);
2353 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002354#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 EMSG(_("E874: (NFA) Could not pop the stack !"));
2356}
2357
2358/*
2359 * Push an item onto the stack.
2360 */
2361 static void
2362st_push(s, p, stack_end)
2363 Frag_T s;
2364 Frag_T **p;
2365 Frag_T *stack_end;
2366{
2367 Frag_T *stackp = *p;
2368
2369 if (stackp >= stack_end)
2370 return;
2371 *stackp = s;
2372 *p = *p + 1;
2373}
2374
2375/*
2376 * Pop an item from the stack.
2377 */
2378 static Frag_T
2379st_pop(p, stack)
2380 Frag_T **p;
2381 Frag_T *stack;
2382{
2383 Frag_T *stackp;
2384
2385 *p = *p - 1;
2386 stackp = *p;
2387 if (stackp < stack)
2388 return empty;
2389 return **p;
2390}
2391
2392/*
2393 * Convert a postfix form into its equivalent NFA.
2394 * Return the NFA start state on success, NULL otherwise.
2395 */
2396 static nfa_state_T *
2397post2nfa(postfix, end, nfa_calc_size)
2398 int *postfix;
2399 int *end;
2400 int nfa_calc_size;
2401{
2402 int *p;
2403 int mopen;
2404 int mclose;
2405 Frag_T *stack = NULL;
2406 Frag_T *stackp = NULL;
2407 Frag_T *stack_end = NULL;
2408 Frag_T e1;
2409 Frag_T e2;
2410 Frag_T e;
2411 nfa_state_T *s;
2412 nfa_state_T *s1;
2413 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002414 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415
2416 if (postfix == NULL)
2417 return NULL;
2418
Bram Moolenaar053bb602013-05-20 13:55:21 +02002419#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420#define POP() st_pop(&stackp, stack); \
2421 if (stackp < stack) \
2422 { \
2423 st_error(postfix, end, p); \
2424 return NULL; \
2425 }
2426
2427 if (nfa_calc_size == FALSE)
2428 {
2429 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002430 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002431 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002432 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433 }
2434
2435 for (p = postfix; p < end; ++p)
2436 {
2437 switch (*p)
2438 {
2439 case NFA_CONCAT:
2440 /* Catenation.
2441 * Pay attention: this operator does not exist
2442 * in the r.e. itself (it is implicit, really).
2443 * It is added when r.e. is translated to postfix
2444 * form in re2post().
2445 *
2446 * No new state added here. */
2447 if (nfa_calc_size == TRUE)
2448 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002449 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450 break;
2451 }
2452 e2 = POP();
2453 e1 = POP();
2454 patch(e1.out, e2.start);
2455 PUSH(frag(e1.start, e2.out));
2456 break;
2457
2458 case NFA_NOT:
2459 /* Negation of a character */
2460 if (nfa_calc_size == TRUE)
2461 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002462 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 break;
2464 }
2465 e1 = POP();
2466 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002467#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002468 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002470#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 PUSH(e1);
2472 break;
2473
2474 case NFA_OR:
2475 /* Alternation */
2476 if (nfa_calc_size == TRUE)
2477 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002478 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 break;
2480 }
2481 e2 = POP();
2482 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002483 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002485 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 PUSH(frag(s, append(e1.out, e2.out)));
2487 break;
2488
2489 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002490 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002491 if (nfa_calc_size == TRUE)
2492 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002493 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002494 break;
2495 }
2496 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002497 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002498 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002499 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500 patch(e.out, s);
2501 PUSH(frag(s, list1(&s->out1)));
2502 break;
2503
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002504 case NFA_STAR_NONGREEDY:
2505 /* Zero or more, prefer zero */
2506 if (nfa_calc_size == TRUE)
2507 {
2508 nstate++;
2509 break;
2510 }
2511 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002512 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002513 if (s == NULL)
2514 goto theend;
2515 patch(e.out, s);
2516 PUSH(frag(s, list1(&s->out)));
2517 break;
2518
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 case NFA_QUEST:
2520 /* one or zero atoms=> greedy match */
2521 if (nfa_calc_size == TRUE)
2522 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002523 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002524 break;
2525 }
2526 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002527 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002528 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002529 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530 PUSH(frag(s, append(e.out, list1(&s->out1))));
2531 break;
2532
2533 case NFA_QUEST_NONGREEDY:
2534 /* zero or one atoms => non-greedy match */
2535 if (nfa_calc_size == TRUE)
2536 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002537 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538 break;
2539 }
2540 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002541 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002543 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 PUSH(frag(s, append(e.out, list1(&s->out))));
2545 break;
2546
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 case NFA_SKIP_CHAR:
2548 /* Symbol of 0-length, Used in a repetition
2549 * with max/min count of 0 */
2550 if (nfa_calc_size == TRUE)
2551 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002552 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002553 break;
2554 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002555 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002557 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 PUSH(frag(s, list1(&s->out)));
2559 break;
2560
2561 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002562 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002563 case NFA_PREV_ATOM_JUST_BEFORE:
2564 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002565 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002566 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002567 * The \@<= operator: match for the preceding atom.
2568 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002569 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002570 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571
2572 if (nfa_calc_size == TRUE)
2573 {
2574 nstate += 2;
2575 break;
2576 }
2577 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002578 s1 = alloc_state(NFA_END_INVISIBLE, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002579 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002580 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002581 patch(e.out, s1);
2582
Bram Moolenaar525666f2013-06-02 16:40:55 +02002583 s = alloc_state(NFA_START_INVISIBLE, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002584 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002585 goto theend;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002586 if (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2587 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002588 {
2589 s->negated = TRUE;
2590 s1->negated = TRUE;
2591 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002592 if (*p == NFA_PREV_ATOM_JUST_BEFORE
2593 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2594 {
2595 s->val = *++p; /* get the count */
2596 ++s->c; /* NFA_START_INVISIBLE -> NFA_START_INVISIBLE_BEFORE */
2597 }
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 PUSH(frag(s, list1(&s1->out)));
2600 break;
2601
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002602#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002603 case NFA_COMPOSING: /* char with composing char */
2604#if 0
2605 /* TODO */
2606 if (regflags & RF_ICOMBINE)
2607 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002608 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002609 }
2610#endif
2611 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002612#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002613
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002614 case NFA_MOPEN: /* \( \) Submatch */
2615 case NFA_MOPEN1:
2616 case NFA_MOPEN2:
2617 case NFA_MOPEN3:
2618 case NFA_MOPEN4:
2619 case NFA_MOPEN5:
2620 case NFA_MOPEN6:
2621 case NFA_MOPEN7:
2622 case NFA_MOPEN8:
2623 case NFA_MOPEN9:
2624#ifdef FEAT_SYN_HL
2625 case NFA_ZOPEN: /* \z( \) Submatch */
2626 case NFA_ZOPEN1:
2627 case NFA_ZOPEN2:
2628 case NFA_ZOPEN3:
2629 case NFA_ZOPEN4:
2630 case NFA_ZOPEN5:
2631 case NFA_ZOPEN6:
2632 case NFA_ZOPEN7:
2633 case NFA_ZOPEN8:
2634 case NFA_ZOPEN9:
2635#endif
2636 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002637 if (nfa_calc_size == TRUE)
2638 {
2639 nstate += 2;
2640 break;
2641 }
2642
2643 mopen = *p;
2644 switch (*p)
2645 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002646 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2647#ifdef FEAT_SYN_HL
2648 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2649 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2650 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2651 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2652 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2653 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2654 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2655 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2656 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2657 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2658#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002659#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002660 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002661#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002663 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002664 mclose = *p + NSUBEXP;
2665 break;
2666 }
2667
2668 /* Allow "NFA_MOPEN" as a valid postfix representation for
2669 * the empty regexp "". In this case, the NFA will be
2670 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2671 * empty groups of parenthesis, and empty mbyte chars */
2672 if (stackp == stack)
2673 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002674 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002675 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002676 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002677 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002678 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002679 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002680 patch(list1(&s->out), s1);
2681 PUSH(frag(s, list1(&s1->out)));
2682 break;
2683 }
2684
2685 /* At least one node was emitted before NFA_MOPEN, so
2686 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2687 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002688 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002689 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002690 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002691
Bram Moolenaar525666f2013-06-02 16:40:55 +02002692 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002693 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002694 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002695 patch(e.out, s1);
2696
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002697#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002698 if (mopen == NFA_COMPOSING)
2699 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002700 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002701#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002702
2703 PUSH(frag(s, list1(&s1->out)));
2704 break;
2705
Bram Moolenaar5714b802013-05-28 22:03:20 +02002706 case NFA_BACKREF1:
2707 case NFA_BACKREF2:
2708 case NFA_BACKREF3:
2709 case NFA_BACKREF4:
2710 case NFA_BACKREF5:
2711 case NFA_BACKREF6:
2712 case NFA_BACKREF7:
2713 case NFA_BACKREF8:
2714 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002715#ifdef FEAT_SYN_HL
2716 case NFA_ZREF1:
2717 case NFA_ZREF2:
2718 case NFA_ZREF3:
2719 case NFA_ZREF4:
2720 case NFA_ZREF5:
2721 case NFA_ZREF6:
2722 case NFA_ZREF7:
2723 case NFA_ZREF8:
2724 case NFA_ZREF9:
2725#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002726 if (nfa_calc_size == TRUE)
2727 {
2728 nstate += 2;
2729 break;
2730 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002731 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002732 if (s == NULL)
2733 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002734 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002735 if (s1 == NULL)
2736 goto theend;
2737 patch(list1(&s->out), s1);
2738 PUSH(frag(s, list1(&s1->out)));
2739 break;
2740
Bram Moolenaar423532e2013-05-29 21:14:42 +02002741 case NFA_LNUM:
2742 case NFA_LNUM_GT:
2743 case NFA_LNUM_LT:
2744 case NFA_VCOL:
2745 case NFA_VCOL_GT:
2746 case NFA_VCOL_LT:
2747 case NFA_COL:
2748 case NFA_COL_GT:
2749 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002750 case NFA_MARK:
2751 case NFA_MARK_GT:
2752 case NFA_MARK_LT:
Bram Moolenaar423532e2013-05-29 21:14:42 +02002753 if (nfa_calc_size == TRUE)
2754 {
2755 nstate += 1;
2756 break;
2757 }
2758 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002759 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002760 if (s == NULL)
2761 goto theend;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002762 s->val = e1.start->c; /* lnum, col or mark name */
Bram Moolenaar423532e2013-05-29 21:14:42 +02002763 PUSH(frag(s, list1(&s->out)));
2764 break;
2765
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002766 case NFA_ZSTART:
2767 case NFA_ZEND:
2768 default:
2769 /* Operands */
2770 if (nfa_calc_size == TRUE)
2771 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002772 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002773 break;
2774 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002775 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002777 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002778 PUSH(frag(s, list1(&s->out)));
2779 break;
2780
2781 } /* switch(*p) */
2782
2783 } /* for(p = postfix; *p; ++p) */
2784
2785 if (nfa_calc_size == TRUE)
2786 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002787 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002788 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002789 }
2790
2791 e = POP();
2792 if (stackp != stack)
2793 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2794
2795 if (istate >= nstate)
2796 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2797
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798 matchstate = &state_ptr[istate++]; /* the match state */
2799 matchstate->c = NFA_MATCH;
2800 matchstate->out = matchstate->out1 = NULL;
2801
2802 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002803 ret = e.start;
2804
2805theend:
2806 vim_free(stack);
2807 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002808
2809#undef POP1
2810#undef PUSH1
2811#undef POP2
2812#undef PUSH2
2813#undef POP
2814#undef PUSH
2815}
2816
2817/****************************************************************
2818 * NFA execution code.
2819 ****************************************************************/
2820
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002821typedef struct
2822{
2823 int in_use; /* number of subexpr with useful info */
2824
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002825 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002826 union
2827 {
2828 struct multipos
2829 {
2830 lpos_T start;
2831 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002832 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002833 struct linepos
2834 {
2835 char_u *start;
2836 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002837 } line[NSUBEXP];
2838 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02002839} regsub_T;
2840
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002841typedef struct
2842{
2843 regsub_T norm; /* \( .. \) matches */
2844#ifdef FEAT_SYN_HL
2845 regsub_T synt; /* \z( .. \) matches */
2846#endif
2847} regsubs_T;
2848
Bram Moolenaara2d95102013-06-04 14:23:05 +02002849/* nfa_pim_T stores a Postponed Invisible Match. */
2850typedef struct nfa_pim_S nfa_pim_T;
2851struct nfa_pim_S
2852{
2853 nfa_state_T *state;
2854 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
2855 nfa_pim_T *pim; /* another PIM at the same position */
2856 regsubs_T subs; /* submatch info, only party used */
2857};
2858
2859/* Values for done in nfa_pim_T. */
2860#define NFA_PIM_TODO 0
2861#define NFA_PIM_MATCH 1
2862#define NFA_PIM_NOMATCH -1
2863
2864
Bram Moolenaar963fee22013-05-26 21:47:28 +02002865/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002866typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867{
2868 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02002869 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02002870 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002871 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002872} nfa_thread_T;
2873
Bram Moolenaar963fee22013-05-26 21:47:28 +02002874/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875typedef struct
2876{
Bram Moolenaar5714b802013-05-28 22:03:20 +02002877 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02002878 int n; /* nr of states currently in "t" */
2879 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02002880 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02002881} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882
Bram Moolenaar5714b802013-05-28 22:03:20 +02002883#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002884static void log_subsexpr __ARGS((regsubs_T *subs));
2885static void log_subexpr __ARGS((regsub_T *sub));
2886
2887 static void
2888log_subsexpr(subs)
2889 regsubs_T *subs;
2890{
2891 log_subexpr(&subs->norm);
2892# ifdef FEAT_SYN_HL
2893 log_subexpr(&subs->synt);
2894# endif
2895}
2896
Bram Moolenaar5714b802013-05-28 22:03:20 +02002897 static void
2898log_subexpr(sub)
2899 regsub_T *sub;
2900{
2901 int j;
2902
2903 for (j = 0; j < sub->in_use; j++)
2904 if (REG_MULTI)
2905 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d",
2906 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002907 sub->list.multi[j].start.col,
2908 (int)sub->list.multi[j].start.lnum,
2909 sub->list.multi[j].end.col,
2910 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002911 else
2912 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"",
2913 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02002914 (char *)sub->list.line[j].start,
2915 (char *)sub->list.line[j].end);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002916 fprintf(log_fd, "\n");
2917}
2918#endif
2919
Bram Moolenaar963fee22013-05-26 21:47:28 +02002920/* Used during execution: whether a match has been found. */
2921static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02002922
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002923static void clear_sub __ARGS((regsub_T *sub));
2924static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
2925static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02002926static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002927static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02002928static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002929
2930 static void
2931clear_sub(sub)
2932 regsub_T *sub;
2933{
2934 if (REG_MULTI)
2935 /* Use 0xff to set lnum to -1 */
2936 vim_memset(sub->list.multi, 0xff,
2937 sizeof(struct multipos) * nfa_nsubexpr);
2938 else
2939 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
2940 sub->in_use = 0;
2941}
2942
2943/*
2944 * Copy the submatches from "from" to "to".
2945 */
2946 static void
2947copy_sub(to, from)
2948 regsub_T *to;
2949 regsub_T *from;
2950{
2951 to->in_use = from->in_use;
2952 if (from->in_use > 0)
2953 {
2954 /* Copy the match start and end positions. */
2955 if (REG_MULTI)
2956 mch_memmove(&to->list.multi[0],
2957 &from->list.multi[0],
2958 sizeof(struct multipos) * from->in_use);
2959 else
2960 mch_memmove(&to->list.line[0],
2961 &from->list.line[0],
2962 sizeof(struct linepos) * from->in_use);
2963 }
2964}
2965
2966/*
2967 * Like copy_sub() but exclude the main match.
2968 */
2969 static void
2970copy_sub_off(to, from)
2971 regsub_T *to;
2972 regsub_T *from;
2973{
2974 if (to->in_use < from->in_use)
2975 to->in_use = from->in_use;
2976 if (from->in_use > 1)
2977 {
2978 /* Copy the match start and end positions. */
2979 if (REG_MULTI)
2980 mch_memmove(&to->list.multi[1],
2981 &from->list.multi[1],
2982 sizeof(struct multipos) * (from->in_use - 1));
2983 else
2984 mch_memmove(&to->list.line[1],
2985 &from->list.line[1],
2986 sizeof(struct linepos) * (from->in_use - 1));
2987 }
2988}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002989
Bram Moolenaar428e9872013-05-30 17:05:39 +02002990/*
2991 * Return TRUE if "sub1" and "sub2" have the same positions.
2992 */
2993 static int
2994sub_equal(sub1, sub2)
2995 regsub_T *sub1;
2996 regsub_T *sub2;
2997{
2998 int i;
2999 int todo;
3000 linenr_T s1, e1;
3001 linenr_T s2, e2;
3002 char_u *sp1, *ep1;
3003 char_u *sp2, *ep2;
3004
3005 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3006 if (REG_MULTI)
3007 {
3008 for (i = 0; i < todo; ++i)
3009 {
3010 if (i < sub1->in_use)
3011 {
3012 s1 = sub1->list.multi[i].start.lnum;
3013 e1 = sub1->list.multi[i].end.lnum;
3014 }
3015 else
3016 {
3017 s1 = 0;
3018 e1 = 0;
3019 }
3020 if (i < sub2->in_use)
3021 {
3022 s2 = sub2->list.multi[i].start.lnum;
3023 e2 = sub2->list.multi[i].end.lnum;
3024 }
3025 else
3026 {
3027 s2 = 0;
3028 e2 = 0;
3029 }
3030 if (s1 != s2 || e1 != e2)
3031 return FALSE;
3032 if (s1 != 0 && sub1->list.multi[i].start.col
3033 != sub2->list.multi[i].start.col)
3034 return FALSE;
3035 if (e1 != 0 && sub1->list.multi[i].end.col
3036 != sub2->list.multi[i].end.col)
3037 return FALSE;
3038 }
3039 }
3040 else
3041 {
3042 for (i = 0; i < todo; ++i)
3043 {
3044 if (i < sub1->in_use)
3045 {
3046 sp1 = sub1->list.line[i].start;
3047 ep1 = sub1->list.line[i].end;
3048 }
3049 else
3050 {
3051 sp1 = NULL;
3052 ep1 = NULL;
3053 }
3054 if (i < sub2->in_use)
3055 {
3056 sp2 = sub2->list.line[i].start;
3057 ep2 = sub2->list.line[i].end;
3058 }
3059 else
3060 {
3061 sp2 = NULL;
3062 ep2 = NULL;
3063 }
3064 if (sp1 != sp2 || ep1 != ep2)
3065 return FALSE;
3066 }
3067 }
3068
3069 return TRUE;
3070}
3071
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003072#ifdef ENABLE_LOG
3073 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003074report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003075{
3076 int col;
3077
3078 if (sub->in_use <= 0)
3079 col = -1;
3080 else if (REG_MULTI)
3081 col = sub->list.multi[0].start.col;
3082 else
3083 col = (int)(sub->list.line[0].start - regline);
3084 nfa_set_code(state->c);
3085 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3086 action, abs(state->id), lid, state->c, code, col);
3087}
3088#endif
3089
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003090 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003091addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003092 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003093 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003094 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003095 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003096{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003097 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003098 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003099 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003100 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003101 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003102 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003103 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003104#ifdef ENABLE_LOG
3105 int did_print = FALSE;
3106#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003107
3108 if (l == NULL || state == NULL)
3109 return;
3110
3111 switch (state->c)
3112 {
3113 case NFA_SPLIT:
3114 case NFA_NOT:
3115 case NFA_NOPEN:
3116 case NFA_NCLOSE:
3117 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003118 case NFA_MCLOSE1:
3119 case NFA_MCLOSE2:
3120 case NFA_MCLOSE3:
3121 case NFA_MCLOSE4:
3122 case NFA_MCLOSE5:
3123 case NFA_MCLOSE6:
3124 case NFA_MCLOSE7:
3125 case NFA_MCLOSE8:
3126 case NFA_MCLOSE9:
3127#ifdef FEAT_SYN_HL
3128 case NFA_ZCLOSE:
3129 case NFA_ZCLOSE1:
3130 case NFA_ZCLOSE2:
3131 case NFA_ZCLOSE3:
3132 case NFA_ZCLOSE4:
3133 case NFA_ZCLOSE5:
3134 case NFA_ZCLOSE6:
3135 case NFA_ZCLOSE7:
3136 case NFA_ZCLOSE8:
3137 case NFA_ZCLOSE9:
3138#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003139 /* These nodes are not added themselves but their "out" and/or
3140 * "out1" may be added below. */
3141 break;
3142
3143 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003144 case NFA_MOPEN1:
3145 case NFA_MOPEN2:
3146 case NFA_MOPEN3:
3147 case NFA_MOPEN4:
3148 case NFA_MOPEN5:
3149 case NFA_MOPEN6:
3150 case NFA_MOPEN7:
3151 case NFA_MOPEN8:
3152 case NFA_MOPEN9:
3153#ifdef FEAT_SYN_HL
3154 case NFA_ZOPEN:
3155 case NFA_ZOPEN1:
3156 case NFA_ZOPEN2:
3157 case NFA_ZOPEN3:
3158 case NFA_ZOPEN4:
3159 case NFA_ZOPEN5:
3160 case NFA_ZOPEN6:
3161 case NFA_ZOPEN7:
3162 case NFA_ZOPEN8:
3163 case NFA_ZOPEN9:
3164#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003165 /* These nodes do not need to be added, but we need to bail out
3166 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003167 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003168 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003169 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003170 break;
3171
Bram Moolenaar307aa162013-06-02 16:34:21 +02003172 case NFA_BOL:
3173 case NFA_BOF:
3174 /* "^" won't match past end-of-line, don't bother trying.
3175 * Except when we are going to the next line for a look-behind
3176 * match. */
3177 if (reginput > regline
3178 && (nfa_endp == NULL
3179 || !REG_MULTI
3180 || reglnum == nfa_endp->se_u.pos.lnum))
3181 goto skip_add;
3182 /* FALLTHROUGH */
3183
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003184 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003185 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003186 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003187 /* This state is already in the list, don't add it again,
3188 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003189 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003190 {
3191skip_add:
3192#ifdef ENABLE_LOG
3193 nfa_set_code(state->c);
3194 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3195 abs(state->id), l->id, state->c, code);
3196#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003197 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003198 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003199
3200 /* See if the same state is already in the list with the same
3201 * positions. */
3202 for (i = 0; i < l->n; ++i)
3203 {
3204 thread = &l->t[i];
3205 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003206 && sub_equal(&thread->subs.norm, &subs->norm)
3207#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003208 && (!nfa_has_zsubexpr ||
3209 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003210#endif
3211 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003212 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003213 }
3214 }
3215
Bram Moolenaara2d95102013-06-04 14:23:05 +02003216 /* when there are backreferences or look-behind matches the number
3217 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003218 if (nfa_has_backref && l->n == l->len)
3219 {
3220 int newlen = l->len * 3 / 2 + 50;
3221
3222 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3223 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003225
3226 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003227 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003228 thread = &l->t[l->n++];
3229 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003230 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003231 copy_sub(&thread->subs.norm, &subs->norm);
3232#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003233 if (nfa_has_zsubexpr)
3234 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003235#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003236#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003237 report_state("Adding", &thread->subs.norm, state, l->id);
3238 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003239#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 }
3241
3242#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003243 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003244 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245#endif
3246 switch (state->c)
3247 {
3248 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003249 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003250 break;
3251
3252 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003253 /* order matters here */
3254 addstate(l, state->out, subs, off);
3255 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 break;
3257
Bram Moolenaar5714b802013-05-28 22:03:20 +02003258 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 case NFA_NOPEN:
3260 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003261 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262 break;
3263
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003264 case NFA_MOPEN:
3265 case NFA_MOPEN1:
3266 case NFA_MOPEN2:
3267 case NFA_MOPEN3:
3268 case NFA_MOPEN4:
3269 case NFA_MOPEN5:
3270 case NFA_MOPEN6:
3271 case NFA_MOPEN7:
3272 case NFA_MOPEN8:
3273 case NFA_MOPEN9:
3274#ifdef FEAT_SYN_HL
3275 case NFA_ZOPEN:
3276 case NFA_ZOPEN1:
3277 case NFA_ZOPEN2:
3278 case NFA_ZOPEN3:
3279 case NFA_ZOPEN4:
3280 case NFA_ZOPEN5:
3281 case NFA_ZOPEN6:
3282 case NFA_ZOPEN7:
3283 case NFA_ZOPEN8:
3284 case NFA_ZOPEN9:
3285#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003287 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003288 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003290 sub = &subs->norm;
3291 }
3292#ifdef FEAT_SYN_HL
3293 else if (state->c >= NFA_ZOPEN)
3294 {
3295 subidx = state->c - NFA_ZOPEN;
3296 sub = &subs->synt;
3297 }
3298#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003299 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003300 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003301 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003302 sub = &subs->norm;
3303 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003305 /* Set the position (with "off") in the subexpression. Save and
3306 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003307 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 if (REG_MULTI)
3309 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003310 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003311 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003312 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003313 save_in_use = -1;
3314 }
3315 else
3316 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003317 save_in_use = sub->in_use;
3318 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003319 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003320 sub->list.multi[i].start.lnum = -1;
3321 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003322 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003323 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003324 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003325 if (off == -1)
3326 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003327 sub->list.multi[subidx].start.lnum = reglnum + 1;
3328 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003329 }
3330 else
3331 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003332 sub->list.multi[subidx].start.lnum = reglnum;
3333 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003334 (colnr_T)(reginput - regline + off);
3335 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336 }
3337 else
3338 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003339 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003340 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003341 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003342 save_in_use = -1;
3343 }
3344 else
3345 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003346 save_in_use = sub->in_use;
3347 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003348 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003349 sub->list.line[i].start = NULL;
3350 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003351 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003352 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003353 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003354 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003355 }
3356
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003357 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003359 if (save_in_use == -1)
3360 {
3361 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003362 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003363 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003364 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003365 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003367 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 break;
3369
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003370 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003371 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003373 /* Do not overwrite the position set by \ze. If no \ze
3374 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003375 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376 break;
3377 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003378 case NFA_MCLOSE1:
3379 case NFA_MCLOSE2:
3380 case NFA_MCLOSE3:
3381 case NFA_MCLOSE4:
3382 case NFA_MCLOSE5:
3383 case NFA_MCLOSE6:
3384 case NFA_MCLOSE7:
3385 case NFA_MCLOSE8:
3386 case NFA_MCLOSE9:
3387#ifdef FEAT_SYN_HL
3388 case NFA_ZCLOSE:
3389 case NFA_ZCLOSE1:
3390 case NFA_ZCLOSE2:
3391 case NFA_ZCLOSE3:
3392 case NFA_ZCLOSE4:
3393 case NFA_ZCLOSE5:
3394 case NFA_ZCLOSE6:
3395 case NFA_ZCLOSE7:
3396 case NFA_ZCLOSE8:
3397 case NFA_ZCLOSE9:
3398#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003399 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003401 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003403 sub = &subs->norm;
3404 }
3405#ifdef FEAT_SYN_HL
3406 else if (state->c >= NFA_ZCLOSE)
3407 {
3408 subidx = state->c - NFA_ZCLOSE;
3409 sub = &subs->synt;
3410 }
3411#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003412 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003413 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003414 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003415 sub = &subs->norm;
3416 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003418 /* We don't fill in gaps here, there must have been an MOPEN that
3419 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003420 save_in_use = sub->in_use;
3421 if (sub->in_use <= subidx)
3422 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423 if (REG_MULTI)
3424 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003425 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003426 if (off == -1)
3427 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003428 sub->list.multi[subidx].end.lnum = reglnum + 1;
3429 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003430 }
3431 else
3432 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003433 sub->list.multi[subidx].end.lnum = reglnum;
3434 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003435 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003436 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003437 }
3438 else
3439 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003440 save_ptr = sub->list.line[subidx].end;
3441 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003442 }
3443
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003444 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445
3446 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003447 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003449 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003450 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003451 break;
3452 }
3453}
3454
3455/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003456 * Like addstate(), but the new state(s) are put at position "*ip".
3457 * Used for zero-width matches, next state to use is the added one.
3458 * This makes sure the order of states to be tried does not change, which
3459 * matters for alternatives.
3460 */
3461 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003462addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003463 nfa_list_T *l; /* runtime state list */
3464 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003465 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003466 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003467 int *ip;
3468{
3469 int tlen = l->n;
3470 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003471 int listidx = *ip;
3472 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003473
3474 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003475 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003476
Bram Moolenaara2d95102013-06-04 14:23:05 +02003477 /* fill in the "pim" field in the new states */
3478 if (pim != NULL)
3479 for (i = tlen; i < l->n; ++i)
3480 l->t[i].pim = pim;
3481
Bram Moolenaar4b417062013-05-25 20:19:50 +02003482 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003483 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003484 return;
3485
3486 /* re-order to put the new state at the current position */
3487 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003488 if (count == 1)
3489 {
3490 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003491 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003492 }
3493 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003494 {
3495 /* make space for new states, then move them from the
3496 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003497 mch_memmove(&(l->t[listidx + count]),
3498 &(l->t[listidx + 1]),
3499 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3500 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003501 &(l->t[l->n - 1]),
3502 sizeof(nfa_thread_T) * count);
3503 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003504 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003505 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003506}
3507
3508/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 * Check character class "class" against current character c.
3510 */
3511 static int
3512check_char_class(class, c)
3513 int class;
3514 int c;
3515{
3516 switch (class)
3517 {
3518 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003519 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 return OK;
3521 break;
3522 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003523 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 return OK;
3525 break;
3526 case NFA_CLASS_BLANK:
3527 if (c == ' ' || c == '\t')
3528 return OK;
3529 break;
3530 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003531 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532 return OK;
3533 break;
3534 case NFA_CLASS_DIGIT:
3535 if (VIM_ISDIGIT(c))
3536 return OK;
3537 break;
3538 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003539 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 return OK;
3541 break;
3542 case NFA_CLASS_LOWER:
3543 if (MB_ISLOWER(c))
3544 return OK;
3545 break;
3546 case NFA_CLASS_PRINT:
3547 if (vim_isprintc(c))
3548 return OK;
3549 break;
3550 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003551 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003552 return OK;
3553 break;
3554 case NFA_CLASS_SPACE:
3555 if ((c >=9 && c <= 13) || (c == ' '))
3556 return OK;
3557 break;
3558 case NFA_CLASS_UPPER:
3559 if (MB_ISUPPER(c))
3560 return OK;
3561 break;
3562 case NFA_CLASS_XDIGIT:
3563 if (vim_isxdigit(c))
3564 return OK;
3565 break;
3566 case NFA_CLASS_TAB:
3567 if (c == '\t')
3568 return OK;
3569 break;
3570 case NFA_CLASS_RETURN:
3571 if (c == '\r')
3572 return OK;
3573 break;
3574 case NFA_CLASS_BACKSPACE:
3575 if (c == '\b')
3576 return OK;
3577 break;
3578 case NFA_CLASS_ESCAPE:
3579 if (c == '\033')
3580 return OK;
3581 break;
3582
3583 default:
3584 /* should not be here :P */
3585 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3586 }
3587 return FAIL;
3588}
3589
Bram Moolenaar5714b802013-05-28 22:03:20 +02003590static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3591
3592/*
3593 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003594 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003595 */
3596 static int
3597match_backref(sub, subidx, bytelen)
3598 regsub_T *sub; /* pointers to subexpressions */
3599 int subidx;
3600 int *bytelen; /* out: length of match in bytes */
3601{
3602 int len;
3603
3604 if (sub->in_use <= subidx)
3605 {
3606retempty:
3607 /* backref was not set, match an empty string */
3608 *bytelen = 0;
3609 return TRUE;
3610 }
3611
3612 if (REG_MULTI)
3613 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003614 if (sub->list.multi[subidx].start.lnum < 0
3615 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003616 goto retempty;
3617 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003618 len = sub->list.multi[subidx].end.col
3619 - sub->list.multi[subidx].start.col;
3620 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003621 reginput, &len) == 0)
3622 {
3623 *bytelen = len;
3624 return TRUE;
3625 }
3626 }
3627 else
3628 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003629 if (sub->list.line[subidx].start == NULL
3630 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003631 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003632 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3633 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003634 {
3635 *bytelen = len;
3636 return TRUE;
3637 }
3638 }
3639 return FALSE;
3640}
3641
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003642#ifdef FEAT_SYN_HL
3643
3644static int match_zref __ARGS((int subidx, int *bytelen));
3645
3646/*
3647 * Check for a match with \z subexpression "subidx".
3648 * Return TRUE if it matches.
3649 */
3650 static int
3651match_zref(subidx, bytelen)
3652 int subidx;
3653 int *bytelen; /* out: length of match in bytes */
3654{
3655 int len;
3656
3657 cleanup_zsubexpr();
3658 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3659 {
3660 /* backref was not set, match an empty string */
3661 *bytelen = 0;
3662 return TRUE;
3663 }
3664
3665 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3666 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3667 {
3668 *bytelen = len;
3669 return TRUE;
3670 }
3671 return FALSE;
3672}
3673#endif
3674
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003676 * Save list IDs for all NFA states of "prog" into "list".
3677 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003678 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 */
3680 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003681nfa_save_listids(prog, list)
3682 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003683 int *list;
3684{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003685 int i;
3686 nfa_state_T *p;
3687
3688 /* Order in the list is reverse, it's a bit faster that way. */
3689 p = &prog->state[0];
3690 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003691 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003692 list[i] = p->lastlist[1];
3693 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003694 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695 }
3696}
3697
3698/*
3699 * Restore list IDs from "list" to all NFA states.
3700 */
3701 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003702nfa_restore_listids(prog, list)
3703 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704 int *list;
3705{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003706 int i;
3707 nfa_state_T *p;
3708
3709 p = &prog->state[0];
3710 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003711 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003712 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003713 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714 }
3715}
3716
Bram Moolenaar423532e2013-05-29 21:14:42 +02003717 static int
3718nfa_re_num_cmp(val, op, pos)
3719 long_u val;
3720 int op;
3721 long_u pos;
3722{
3723 if (op == 1) return pos > val;
3724 if (op == 2) return pos < val;
3725 return val == pos;
3726}
3727
Bram Moolenaarf46da702013-06-02 22:37:42 +02003728static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003729static 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 +02003730
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003731/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003732 * Recursively call nfa_regmatch()
3733 */
3734 static int
3735recursive_regmatch(state, prog, submatch, m, listids)
3736 nfa_state_T *state;
3737 nfa_regprog_T *prog;
3738 regsubs_T *submatch;
3739 regsubs_T *m;
3740 int **listids;
3741{
3742 char_u *save_reginput = reginput;
3743 char_u *save_regline = regline;
3744 int save_reglnum = reglnum;
3745 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003746 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003747 save_se_T *save_nfa_endp = nfa_endp;
3748 save_se_T endpos;
3749 save_se_T *endposp = NULL;
3750 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003751 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003752
3753 if (state->c == NFA_START_INVISIBLE_BEFORE)
3754 {
3755 /* The recursive match must end at the current position. */
3756 endposp = &endpos;
3757 if (REG_MULTI)
3758 {
3759 endpos.se_u.pos.col = (int)(reginput - regline);
3760 endpos.se_u.pos.lnum = reglnum;
3761 }
3762 else
3763 endpos.se_u.ptr = reginput;
3764
3765 /* Go back the specified number of bytes, or as far as the
3766 * start of the previous line, to try matching "\@<=" or
3767 * not matching "\@<!".
3768 * TODO: This is very inefficient! Would be better to
3769 * first check for a match with what follows. */
3770 if (state->val <= 0)
3771 {
3772 if (REG_MULTI)
3773 {
3774 regline = reg_getline(--reglnum);
3775 if (regline == NULL)
3776 /* can't go before the first line */
3777 regline = reg_getline(++reglnum);
3778 }
3779 reginput = regline;
3780 }
3781 else
3782 {
3783 if (REG_MULTI && (int)(reginput - regline) < state->val)
3784 {
3785 /* Not enough bytes in this line, go to end of
3786 * previous line. */
3787 regline = reg_getline(--reglnum);
3788 if (regline == NULL)
3789 {
3790 /* can't go before the first line */
3791 regline = reg_getline(++reglnum);
3792 reginput = regline;
3793 }
3794 else
3795 reginput = regline + STRLEN(regline);
3796 }
3797 if ((int)(reginput - regline) >= state->val)
3798 {
3799 reginput -= state->val;
3800#ifdef FEAT_MBYTE
3801 if (has_mbyte)
3802 reginput -= mb_head_off(regline, reginput);
3803#endif
3804 }
3805 else
3806 reginput = regline;
3807 }
3808 }
3809
Bram Moolenaarf46da702013-06-02 22:37:42 +02003810#ifdef ENABLE_LOG
3811 if (log_fd != stderr)
3812 fclose(log_fd);
3813 log_fd = NULL;
3814#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003815 /* Have to clear the lastlist field of the NFA nodes, so that
3816 * nfa_regmatch() and addstate() can run properly after recursion. */
3817 if (nfa_ll_index == 1)
3818 {
3819 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
3820 * values and clear them. */
3821 if (*listids == NULL)
3822 {
3823 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
3824 if (*listids == NULL)
3825 {
3826 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
3827 return 0;
3828 }
3829 }
3830 nfa_save_listids(prog, *listids);
3831 need_restore = TRUE;
3832 /* any value of nfa_listid will do */
3833 }
3834 else
3835 {
3836 /* First recursive nfa_regmatch() call, switch to the second lastlist
3837 * entry. Make sure nfa_listid is different from a previous recursive
3838 * call, because some states may still have this ID. */
3839 ++nfa_ll_index;
3840 if (nfa_listid <= nfa_alt_listid)
3841 nfa_listid = nfa_alt_listid;
3842 }
3843
3844 /* Call nfa_regmatch() to check if the current concat matches at this
3845 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02003846 nfa_endp = endposp;
3847 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003848
3849 if (need_restore)
3850 nfa_restore_listids(prog, *listids);
3851 else
3852 {
3853 --nfa_ll_index;
3854 nfa_alt_listid = nfa_listid;
3855 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02003856
3857 /* restore position in input text */
3858 reginput = save_reginput;
3859 regline = save_regline;
3860 reglnum = save_reglnum;
3861 nfa_match = save_nfa_match;
3862 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003863 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003864
3865#ifdef ENABLE_LOG
3866 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
3867 if (log_fd != NULL)
3868 {
3869 fprintf(log_fd, "****************************\n");
3870 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
3871 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
3872 fprintf(log_fd, "****************************\n");
3873 }
3874 else
3875 {
3876 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3877 log_fd = stderr;
3878 }
3879#endif
3880
3881 return result;
3882}
3883
Bram Moolenaara2d95102013-06-04 14:23:05 +02003884static int failure_chance __ARGS((nfa_state_T *state, int depth));
3885
3886/*
3887 * Estimate the chance of a match with "state" failing.
3888 * NFA_ANY: 1
3889 * specific character: 99
3890 */
3891 static int
3892failure_chance(state, depth)
3893 nfa_state_T *state;
3894 int depth;
3895{
3896 int c = state->c;
3897 int l, r;
3898
3899 /* detect looping */
3900 if (depth > 4)
3901 return 1;
3902
3903 if (c == NFA_SPLIT)
3904 {
3905 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
3906 return 1;
3907 l = failure_chance(state->out, depth + 1);
3908 r = failure_chance(state->out1, depth + 1);
3909 return l < r ? l : r;
3910 }
3911 if (c == NFA_ANY)
3912 return 1;
3913 if (c > 0)
3914 return 99;
3915 if ((c >= NFA_MOPEN && c <= NFA_MOPEN9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003916#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02003917 || (c >= NFA_ZOPEN && c <= NFA_ZOPEN9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02003918#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02003919 || c == NFA_NOPEN)
3920 return failure_chance(state->out, depth + 1);
3921 /* something else */
3922 return 50;
3923}
3924
Bram Moolenaarf46da702013-06-02 22:37:42 +02003925/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003926 * Main matching routine.
3927 *
3928 * Run NFA to determine whether it matches reginput.
3929 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02003930 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003931 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003932 * Return TRUE if there is a match, FALSE otherwise.
3933 * Note: Caller must ensure that: start != NULL.
3934 */
3935 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003936nfa_regmatch(prog, start, submatch, m)
3937 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003938 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003939 regsubs_T *submatch;
3940 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003941{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003942 int result;
3943 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003944 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003945 int go_to_nextline = FALSE;
3946 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003947 nfa_list_T list[3];
3948 nfa_list_T *listtbl[2][2];
3949 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02003950 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003951 nfa_list_T *thislist;
3952 nfa_list_T *nextlist;
3953 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003954 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003955 nfa_state_T *add_state;
3956 int add_count;
3957 int add_off;
3958 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02003959#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003960 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003961
3962 if (debug == NULL)
3963 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003964 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965 return FALSE;
3966 }
3967#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003968 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003969 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003970
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003971 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003972 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar428e9872013-05-30 17:05:39 +02003973 list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3974 list[0].len = nstate + 1;
3975 list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3976 list[1].len = nstate + 1;
3977 list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE);
3978 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003979 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
3980 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003981
3982#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003983 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003984 if (log_fd != NULL)
3985 {
3986 fprintf(log_fd, "**********************************\n");
3987 nfa_set_code(start->c);
3988 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
3989 abs(start->id), code);
3990 fprintf(log_fd, "**********************************\n");
3991 }
3992 else
3993 {
3994 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
3995 log_fd = stderr;
3996 }
3997#endif
3998
3999 thislist = &list[0];
4000 thislist->n = 0;
4001 nextlist = &list[1];
4002 nextlist->n = 0;
4003 neglist = &list[2];
4004 neglist->n = 0;
4005#ifdef ENABLE_LOG
4006 fprintf(log_fd, "(---) STARTSTATE\n");
4007#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004008 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004009 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004010
4011 /* There are two cases when the NFA advances: 1. input char matches the
4012 * NFA node and 2. input char does not match the NFA node, but the next
4013 * node is NFA_NOT. The following macro calls addstate() according to
4014 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4015 listtbl[0][0] = NULL;
4016 listtbl[0][1] = neglist;
4017 listtbl[1][0] = nextlist;
4018 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004019#define ADD_POS_NEG_STATE(state) \
4020 ll = listtbl[result ? 1 : 0][state->negated]; \
4021 if (ll != NULL) { \
4022 add_state = state->out; \
4023 add_off = clen; \
4024 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004025
4026 /*
4027 * Run for each character.
4028 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004029 for (;;)
4030 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004031 int curc;
4032 int clen;
4033
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004034#ifdef FEAT_MBYTE
4035 if (has_mbyte)
4036 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004037 curc = (*mb_ptr2char)(reginput);
4038 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004039 }
4040 else
4041#endif
4042 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004043 curc = *reginput;
4044 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004045 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004046 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004047 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004048 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004049 go_to_nextline = FALSE;
4050 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004051
4052 /* swap lists */
4053 thislist = &list[flag];
4054 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004055 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004056 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004057 ++nfa_listid;
4058 thislist->id = nfa_listid;
4059 nextlist->id = nfa_listid + 1;
4060 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004061
Bram Moolenaara2d95102013-06-04 14:23:05 +02004062 pimlist.ga_len = 0;
4063
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004064#ifdef ENABLE_LOG
4065 fprintf(log_fd, "------------------------------------------\n");
4066 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004067 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004068 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004069 {
4070 int i;
4071
4072 for (i = 0; i < thislist->n; i++)
4073 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4074 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004075 fprintf(log_fd, "\n");
4076#endif
4077
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004078#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004079 fprintf(debug, "\n-------------------\n");
4080#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004081 /*
4082 * If the state lists are empty we can stop.
4083 */
4084 if (thislist->n == 0 && neglist->n == 0)
4085 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004086
4087 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004088 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 {
4090 if (neglist->n > 0)
4091 {
4092 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004093 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004094 listidx--;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004095 }
4096 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004097 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004098
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004099#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004100 nfa_set_code(t->state->c);
4101 fprintf(debug, "%s, ", code);
4102#endif
4103#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004104 {
4105 int col;
4106
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004107 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004108 col = -1;
4109 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004110 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004111 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004112 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004113 nfa_set_code(t->state->c);
4114 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4115 abs(t->state->id), (int)t->state->c, code, col);
4116 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117#endif
4118
4119 /*
4120 * Handle the possible codes of the current state.
4121 * The most important is NFA_MATCH.
4122 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004123 add_state = NULL;
4124 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004125 switch (t->state->c)
4126 {
4127 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004128 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004129 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004130 copy_sub(&submatch->norm, &t->subs.norm);
4131#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004132 if (nfa_has_zsubexpr)
4133 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004134#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004135#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004136 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004137#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004138 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004139 * states at this position. When the list of states is going
4140 * to be empty quit without advancing, so that "reginput" is
4141 * correct. */
4142 if (nextlist->n == 0 && neglist->n == 0)
4143 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004144 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004145 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004146
4147 case NFA_END_INVISIBLE:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004148 /*
4149 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004150 * NFA_START_INVISIBLE_BEFORE node.
4151 * They surround a zero-width group, used with "\@=", "\&",
4152 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004153 * If we got here, it means that the current "invisible" group
4154 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004155 * nfa_regmatch(). For a look-behind match only when it ends
4156 * in the position in "nfa_endp".
4157 * Submatches are stored in *m, and used in the parent call.
4158 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004159#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004160 if (nfa_endp != NULL)
4161 {
4162 if (REG_MULTI)
4163 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4164 (int)reglnum,
4165 (int)nfa_endp->se_u.pos.lnum,
4166 (int)(reginput - regline),
4167 nfa_endp->se_u.pos.col);
4168 else
4169 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4170 (int)(reginput - regline),
4171 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004172 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004173#endif
4174 /* It's only a match if it ends at "nfa_endp" */
4175 if (nfa_endp != NULL && (REG_MULTI
4176 ? (reglnum != nfa_endp->se_u.pos.lnum
4177 || (int)(reginput - regline)
4178 != nfa_endp->se_u.pos.col)
4179 : reginput != nfa_endp->se_u.ptr))
4180 break;
4181
4182 /* do not set submatches for \@! */
4183 if (!t->state->negated)
4184 {
4185 copy_sub(&m->norm, &t->subs.norm);
4186#ifdef FEAT_SYN_HL
4187 if (nfa_has_zsubexpr)
4188 copy_sub(&m->synt, &t->subs.synt);
4189#endif
4190 }
4191 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004192 break;
4193
4194 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004195 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2d95102013-06-04 14:23:05 +02004196 /* If invisible match has a higher chance to fail, do it
4197 * right away. Otherwise postpone it until what follows is
4198 * matching and causes addstate(nextlist, ..) to be called.
4199 * This is indicated by the "pim" field. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004200 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004201 nfa_pim_T *pim;
4202 int cout = t->state->out1->out->c;
4203
4204 /* Do it directly when what follows is possibly end of
4205 * match (closing paren).
4206 * Postpone when it is \@<= or \@<!, these are expensive.
4207 * TODO: remove the check for t->pim and check multiple
4208 * where it's used?
4209 * Otherwise first do the one that has the highest chance
4210 * of failing. */
4211 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004212#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004213 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004214#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004215 || cout == NFA_NCLOSE
4216 || t->pim != NULL
4217 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4218 && failure_chance(t->state->out1->out, 0)
4219 < failure_chance(t->state->out, 0)))
4220 {
4221 /*
4222 * First try matching the invisible match, then what
4223 * follows.
4224 */
4225 result = recursive_regmatch(t->state, prog,
4226 submatch, m, &listids);
4227
4228 /* for \@! it is a match when result is FALSE */
4229 if (result != t->state->negated)
4230 {
4231 /* Copy submatch info from the recursive call */
4232 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004233#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004234 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004235#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004236
Bram Moolenaara2d95102013-06-04 14:23:05 +02004237 /* t->state->out1 is the corresponding
4238 * END_INVISIBLE node; Add its out to the current
4239 * list (zero-width match). */
4240 addstate_here(thislist, t->state->out1->out,
4241 &t->subs, t->pim, &listidx);
4242 }
4243 }
4244 else
4245 {
4246 /*
4247 * First try matching what follows at the current
4248 * position. Only if a match is found, addstate() is
4249 * called, then verify the invisible match matches.
4250 * Add a nfa_pim_T to the following states, it
4251 * contains info about the invisible match.
4252 */
4253 if (ga_grow(&pimlist, 1) == FAIL)
4254 goto theend;
4255 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4256 ++pimlist.ga_len;
4257 pim->state = t->state;
4258 pim->pim = NULL;
4259 pim->result = NFA_PIM_TODO;
4260
4261 /* t->state->out1 is the corresponding END_INVISIBLE
4262 * node; Add its out to the current list (zero-width
4263 * match). */
4264 addstate_here(thislist, t->state->out1->out, &t->subs,
4265 pim, &listidx);
4266 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004267 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 break;
4269
4270 case NFA_BOL:
4271 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004272 addstate_here(thislist, t->state->out, &t->subs,
4273 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004274 break;
4275
4276 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004277 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004278 addstate_here(thislist, t->state->out, &t->subs,
4279 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004280 break;
4281
4282 case NFA_BOW:
4283 {
4284 int bow = TRUE;
4285
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004286 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004287 bow = FALSE;
4288#ifdef FEAT_MBYTE
4289 else if (has_mbyte)
4290 {
4291 int this_class;
4292
4293 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004294 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295 if (this_class <= 1)
4296 bow = FALSE;
4297 else if (reg_prev_class() == this_class)
4298 bow = FALSE;
4299 }
4300#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004301 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004302 || (reginput > regline
4303 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304 bow = FALSE;
4305 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004306 addstate_here(thislist, t->state->out, &t->subs,
4307 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308 break;
4309 }
4310
4311 case NFA_EOW:
4312 {
4313 int eow = TRUE;
4314
4315 if (reginput == regline)
4316 eow = FALSE;
4317#ifdef FEAT_MBYTE
4318 else if (has_mbyte)
4319 {
4320 int this_class, prev_class;
4321
4322 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004323 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004324 prev_class = reg_prev_class();
4325 if (this_class == prev_class
4326 || prev_class == 0 || prev_class == 1)
4327 eow = FALSE;
4328 }
4329#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004330 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004331 || (reginput[0] != NUL
4332 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333 eow = FALSE;
4334 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004335 addstate_here(thislist, t->state->out, &t->subs,
4336 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004337 break;
4338 }
4339
Bram Moolenaar4b780632013-05-31 22:14:52 +02004340 case NFA_BOF:
4341 if (reglnum == 0 && reginput == regline
4342 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004343 addstate_here(thislist, t->state->out, &t->subs,
4344 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004345 break;
4346
4347 case NFA_EOF:
4348 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004349 addstate_here(thislist, t->state->out, &t->subs,
4350 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004351 break;
4352
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004353#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004355 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004356 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004357 int len = 0;
4358 nfa_state_T *end;
4359 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004360 int cchars[MAX_MCO];
4361 int ccount = 0;
4362 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004363
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004364 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004365 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004366 if (utf_iscomposing(sta->c))
4367 {
4368 /* Only match composing character(s), ignore base
4369 * character. Used for ".{composing}" and "{composing}"
4370 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004371 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004372 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004373 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004374 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004375 /* If \Z was present, then ignore composing characters.
4376 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004377 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004378 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004379 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004380 else
4381 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004382 while (sta->c != NFA_END_COMPOSING)
4383 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004384 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004385
4386 /* Check base character matches first, unless ignored. */
4387 else if (len > 0 || mc == sta->c)
4388 {
4389 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004390 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004391 len += mb_char2len(mc);
4392 sta = sta->out;
4393 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004394
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004395 /* We don't care about the order of composing characters.
4396 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004397 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004398 {
4399 mc = mb_ptr2char(reginput + len);
4400 cchars[ccount++] = mc;
4401 len += mb_char2len(mc);
4402 if (ccount == MAX_MCO)
4403 break;
4404 }
4405
4406 /* Check that each composing char in the pattern matches a
4407 * composing char in the text. We do not check if all
4408 * composing chars are matched. */
4409 result = OK;
4410 while (sta->c != NFA_END_COMPOSING)
4411 {
4412 for (j = 0; j < ccount; ++j)
4413 if (cchars[j] == sta->c)
4414 break;
4415 if (j == ccount)
4416 {
4417 result = FAIL;
4418 break;
4419 }
4420 sta = sta->out;
4421 }
4422 }
4423 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004424 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004425
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004426 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004427 ADD_POS_NEG_STATE(end);
4428 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004429 }
4430#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004431
4432 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004433 if (curc == NUL && !reg_line_lbr && REG_MULTI
4434 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004435 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004436 go_to_nextline = TRUE;
4437 /* Pass -1 for the offset, which means taking the position
4438 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004439 ll = nextlist;
4440 add_state = t->state->out;
4441 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004442 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004443 else if (curc == '\n' && reg_line_lbr)
4444 {
4445 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004446 ll = nextlist;
4447 add_state = t->state->out;
4448 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004449 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004450 break;
4451
4452 case NFA_CLASS_ALNUM:
4453 case NFA_CLASS_ALPHA:
4454 case NFA_CLASS_BLANK:
4455 case NFA_CLASS_CNTRL:
4456 case NFA_CLASS_DIGIT:
4457 case NFA_CLASS_GRAPH:
4458 case NFA_CLASS_LOWER:
4459 case NFA_CLASS_PRINT:
4460 case NFA_CLASS_PUNCT:
4461 case NFA_CLASS_SPACE:
4462 case NFA_CLASS_UPPER:
4463 case NFA_CLASS_XDIGIT:
4464 case NFA_CLASS_TAB:
4465 case NFA_CLASS_RETURN:
4466 case NFA_CLASS_BACKSPACE:
4467 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004468 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 ADD_POS_NEG_STATE(t->state);
4470 break;
4471
4472 case NFA_END_NEG_RANGE:
4473 /* This follows a series of negated nodes, like:
4474 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004475 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004476 {
4477 ll = nextlist;
4478 add_state = t->state->out;
4479 add_off = clen;
4480 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004481 break;
4482
4483 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004484 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004485 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004486 {
4487 ll = nextlist;
4488 add_state = t->state->out;
4489 add_off = clen;
4490 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004491 break;
4492
4493 /*
4494 * Character classes like \a for alpha, \d for digit etc.
4495 */
4496 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004497 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498 ADD_POS_NEG_STATE(t->state);
4499 break;
4500
4501 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004502 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503 ADD_POS_NEG_STATE(t->state);
4504 break;
4505
4506 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004507 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004508 ADD_POS_NEG_STATE(t->state);
4509 break;
4510
4511 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004512 result = !VIM_ISDIGIT(curc)
4513 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004514 ADD_POS_NEG_STATE(t->state);
4515 break;
4516
4517 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004518 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 ADD_POS_NEG_STATE(t->state);
4520 break;
4521
4522 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004523 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 ADD_POS_NEG_STATE(t->state);
4525 break;
4526
4527 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004528 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 ADD_POS_NEG_STATE(t->state);
4530 break;
4531
4532 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004533 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004534 ADD_POS_NEG_STATE(t->state);
4535 break;
4536
4537 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004538 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 ADD_POS_NEG_STATE(t->state);
4540 break;
4541
4542 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004543 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544 ADD_POS_NEG_STATE(t->state);
4545 break;
4546
4547 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004548 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549 ADD_POS_NEG_STATE(t->state);
4550 break;
4551
4552 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004553 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 ADD_POS_NEG_STATE(t->state);
4555 break;
4556
4557 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004558 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 ADD_POS_NEG_STATE(t->state);
4560 break;
4561
4562 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004563 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004564 ADD_POS_NEG_STATE(t->state);
4565 break;
4566
4567 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004568 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004569 ADD_POS_NEG_STATE(t->state);
4570 break;
4571
4572 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004573 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574 ADD_POS_NEG_STATE(t->state);
4575 break;
4576
4577 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004578 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004579 ADD_POS_NEG_STATE(t->state);
4580 break;
4581
4582 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004583 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004584 ADD_POS_NEG_STATE(t->state);
4585 break;
4586
4587 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004588 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589 ADD_POS_NEG_STATE(t->state);
4590 break;
4591
4592 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004593 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004594 ADD_POS_NEG_STATE(t->state);
4595 break;
4596
4597 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004598 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004599 ADD_POS_NEG_STATE(t->state);
4600 break;
4601
4602 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004603 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604 ADD_POS_NEG_STATE(t->state);
4605 break;
4606
4607 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004608 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 ADD_POS_NEG_STATE(t->state);
4610 break;
4611
4612 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004613 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004614 ADD_POS_NEG_STATE(t->state);
4615 break;
4616
4617 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004618 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004619 ADD_POS_NEG_STATE(t->state);
4620 break;
4621
4622 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004623 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004624 ADD_POS_NEG_STATE(t->state);
4625 break;
4626
Bram Moolenaar5714b802013-05-28 22:03:20 +02004627 case NFA_BACKREF1:
4628 case NFA_BACKREF2:
4629 case NFA_BACKREF3:
4630 case NFA_BACKREF4:
4631 case NFA_BACKREF5:
4632 case NFA_BACKREF6:
4633 case NFA_BACKREF7:
4634 case NFA_BACKREF8:
4635 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004636#ifdef FEAT_SYN_HL
4637 case NFA_ZREF1:
4638 case NFA_ZREF2:
4639 case NFA_ZREF3:
4640 case NFA_ZREF4:
4641 case NFA_ZREF5:
4642 case NFA_ZREF6:
4643 case NFA_ZREF7:
4644 case NFA_ZREF8:
4645 case NFA_ZREF9:
4646#endif
4647 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004648 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004649 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004650 int bytelen;
4651
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004652 if (t->state->c <= NFA_BACKREF9)
4653 {
4654 subidx = t->state->c - NFA_BACKREF1 + 1;
4655 result = match_backref(&t->subs.norm, subidx, &bytelen);
4656 }
4657#ifdef FEAT_SYN_HL
4658 else
4659 {
4660 subidx = t->state->c - NFA_ZREF1 + 1;
4661 result = match_zref(subidx, &bytelen);
4662 }
4663#endif
4664
Bram Moolenaar5714b802013-05-28 22:03:20 +02004665 if (result)
4666 {
4667 if (bytelen == 0)
4668 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02004669 /* empty match always works, output of NFA_SKIP to be
4670 * used next */
4671 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02004672 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004673 }
4674 else if (bytelen <= clen)
4675 {
4676 /* match current character, jump ahead to out of
4677 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004678 ll = nextlist;
4679 add_state = t->state->out->out;
4680 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004681#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004682 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004683#endif
4684 }
4685 else
4686 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02004687 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02004688 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004689 ll = nextlist;
4690 add_state = t->state->out;
4691 add_off = bytelen;
4692 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004693#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004694 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004695#endif
4696 }
4697
4698 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02004699 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004700 }
4701 case NFA_SKIP:
4702 /* charater of previous matching \1 .. \9 */
4703 if (t->count - clen <= 0)
4704 {
4705 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004706 ll = nextlist;
4707 add_state = t->state->out;
4708 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004709#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004710 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004711#endif
4712 }
4713 else
4714 {
4715 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004716 ll = nextlist;
4717 add_state = t->state;
4718 add_off = 0;
4719 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004720#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004721 log_subsexpr(&nextlist->t[nextlist->n - 1].subs);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004722#endif
4723 }
4724 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004725
4726 case NFA_SKIP_CHAR:
4727 case NFA_ZSTART:
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004728 case NFA_ZEND:
Bram Moolenaar12e40142013-05-21 15:33:41 +02004729 /* TODO: should not happen? */
4730 break;
4731
Bram Moolenaar423532e2013-05-29 21:14:42 +02004732 case NFA_LNUM:
4733 case NFA_LNUM_GT:
4734 case NFA_LNUM_LT:
4735 result = (REG_MULTI &&
4736 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
4737 (long_u)(reglnum + reg_firstlnum)));
4738 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004739 addstate_here(thislist, t->state->out, &t->subs,
4740 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004741 break;
4742
4743 case NFA_COL:
4744 case NFA_COL_GT:
4745 case NFA_COL_LT:
4746 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
4747 (long_u)(reginput - regline) + 1);
4748 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004749 addstate_here(thislist, t->state->out, &t->subs,
4750 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004751 break;
4752
4753 case NFA_VCOL:
4754 case NFA_VCOL_GT:
4755 case NFA_VCOL_LT:
4756 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
4757 (long_u)win_linetabsize(
4758 reg_win == NULL ? curwin : reg_win,
4759 regline, (colnr_T)(reginput - regline)) + 1);
4760 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004761 addstate_here(thislist, t->state->out, &t->subs,
4762 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004763 break;
4764
Bram Moolenaar044aa292013-06-04 21:27:38 +02004765 case NFA_MARK:
4766 case NFA_MARK_GT:
4767 case NFA_MARK_LT:
4768 {
4769 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
4770
4771 /* Compare the mark position to the match position. */
4772 result = (pos != NULL /* mark doesn't exist */
4773 && pos->lnum > 0 /* mark isn't set in reg_buf */
4774 && (pos->lnum == reglnum + reg_firstlnum
4775 ? (pos->col == (colnr_T)(reginput - regline)
4776 ? t->state->c == NFA_MARK
4777 : (pos->col < (colnr_T)(reginput - regline)
4778 ? t->state->c == NFA_MARK_GT
4779 : t->state->c == NFA_MARK_LT))
4780 : (pos->lnum < reglnum + reg_firstlnum
4781 ? t->state->c == NFA_MARK_GT
4782 : t->state->c == NFA_MARK_LT)));
4783 if (result)
4784 addstate_here(thislist, t->state->out, &t->subs,
4785 t->pim, &listidx);
4786 break;
4787 }
4788
Bram Moolenaar423532e2013-05-29 21:14:42 +02004789 case NFA_CURSOR:
4790 result = (reg_win != NULL
4791 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
4792 && ((colnr_T)(reginput - regline)
4793 == reg_win->w_cursor.col));
4794 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004795 addstate_here(thislist, t->state->out, &t->subs,
4796 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02004797 break;
4798
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02004799#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004800 case NFA_VISUAL:
4801 result = reg_match_visual();
4802 if (result)
4803 addstate_here(thislist, t->state->out, &t->subs,
4804 t->pim, &listidx);
4805 break;
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02004806#endif
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004807
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004808 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004809 {
4810 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02004811
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004812 /* TODO: put this in #ifdef later */
4813 if (c < -256)
4814 EMSGN("INTERNAL: Negative state char: %ld", c);
4815 if (is_Magic(c))
4816 c = un_Magic(c);
4817 result = (c == curc);
4818
4819 if (!result && ireg_ic)
4820 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004821#ifdef FEAT_MBYTE
4822 /* If there is a composing character which is not being
4823 * ignored there can be no match. Match with composing
4824 * character uses NFA_COMPOSING above. */
4825 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004826 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004827 result = FALSE;
4828#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004829 ADD_POS_NEG_STATE(t->state);
4830 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02004831 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02004832
4833 } /* switch (t->state->c) */
4834
4835 if (add_state != NULL)
4836 {
4837 if (t->pim != NULL)
4838 {
4839 /* postponed invisible match */
4840 /* TODO: also do t->pim->pim recursively? */
4841 if (t->pim->result == NFA_PIM_TODO)
4842 {
4843#ifdef ENABLE_LOG
4844 fprintf(log_fd, "\n");
4845 fprintf(log_fd, "==================================\n");
4846 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
4847 fprintf(log_fd, "\n");
4848#endif
4849 result = recursive_regmatch(t->pim->state,
4850 prog, submatch, m, &listids);
4851 t->pim->result = result ? NFA_PIM_MATCH
4852 : NFA_PIM_NOMATCH;
4853 /* for \@! it is a match when result is FALSE */
4854 if (result != t->pim->state->negated)
4855 {
4856 /* Copy submatch info from the recursive call */
4857 copy_sub_off(&t->pim->subs.norm, &m->norm);
4858#ifdef FEAT_SYN_HL
4859 copy_sub_off(&t->pim->subs.synt, &m->synt);
4860#endif
4861 }
4862 }
4863 else
4864 {
4865 result = (t->pim->result == NFA_PIM_MATCH);
4866#ifdef ENABLE_LOG
4867 fprintf(log_fd, "\n");
4868 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
4869 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4870 fprintf(log_fd, "\n");
4871#endif
4872 }
4873
4874 /* for \@! it is a match when result is FALSE */
4875 if (result != t->pim->state->negated)
4876 {
4877 /* Copy submatch info from the recursive call */
4878 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
4879#ifdef FEAT_SYN_HL
4880 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
4881#endif
4882 }
4883 else
4884 /* look-behind match failed, don't add the state */
4885 continue;
4886 }
4887
4888 addstate(ll, add_state, &t->subs, add_off);
4889 if (add_count > 0)
4890 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004891 }
4892
4893 } /* for (thislist = thislist; thislist->state; thislist++) */
4894
Bram Moolenaare23febd2013-05-26 18:40:14 +02004895 /* Look for the start of a match in the current position by adding the
4896 * start state to the list of states.
4897 * The first found match is the leftmost one, thus the order of states
4898 * matters!
4899 * Do not add the start state in recursive calls of nfa_regmatch(),
4900 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02004901 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02004902 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004903 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004904 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02004905 && reglnum == 0
4906 && clen != 0
4907 && (ireg_maxcol == 0
4908 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02004909 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02004910 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02004911 ? (reglnum < nfa_endp->se_u.pos.lnum
4912 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02004913 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02004914 < nfa_endp->se_u.pos.col))
4915 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004916 {
4917#ifdef ENABLE_LOG
4918 fprintf(log_fd, "(---) STARTSTATE\n");
4919#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02004920 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004921 }
4922
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004923#ifdef ENABLE_LOG
4924 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004925 {
4926 int i;
4927
4928 for (i = 0; i < thislist->n; i++)
4929 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4930 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004931 fprintf(log_fd, "\n");
4932#endif
4933
4934nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004935 /* Advance to the next character, or advance to the next line, or
4936 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004937 if (clen != 0)
4938 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02004939 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
4940 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02004941 reg_nextline();
4942 else
4943 break;
4944 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004945
4946#ifdef ENABLE_LOG
4947 if (log_fd != stderr)
4948 fclose(log_fd);
4949 log_fd = NULL;
4950#endif
4951
4952theend:
4953 /* Free memory */
4954 vim_free(list[0].t);
4955 vim_free(list[1].t);
4956 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02004957 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004958 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004959#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004960#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004961 fclose(debug);
4962#endif
4963
Bram Moolenaar963fee22013-05-26 21:47:28 +02004964 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004965}
4966
4967/*
4968 * Try match of "prog" with at regline["col"].
4969 * Returns 0 for failure, number of lines contained in the match otherwise.
4970 */
4971 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004972nfa_regtry(prog, col)
4973 nfa_regprog_T *prog;
4974 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004975{
4976 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004977 regsubs_T subs, m;
4978 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004979#ifdef ENABLE_LOG
4980 FILE *f;
4981#endif
4982
4983 reginput = regline + col;
4984 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004985#ifdef FEAT_SYN_HL
4986 /* Clear the external match subpointers if necessary. */
4987 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004988 {
4989 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004990 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004991 }
4992 else
4993 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004994#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004995
4996#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004997 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998 if (f != NULL)
4999 {
5000 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n");
5001 fprintf(f, " =======================================================\n");
5002#ifdef DEBUG
5003 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5004#endif
5005 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005006 fprintf(f, " =======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005007 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005008 fprintf(f, "\n\n");
5009 fclose(f);
5010 }
5011 else
5012 EMSG(_("Could not open temporary log file for writing "));
5013#endif
5014
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005015 clear_sub(&subs.norm);
5016 clear_sub(&m.norm);
5017#ifdef FEAT_SYN_HL
5018 clear_sub(&subs.synt);
5019 clear_sub(&m.synt);
5020#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005021
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005022 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005023 return 0;
5024
5025 cleanup_subexpr();
5026 if (REG_MULTI)
5027 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005028 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005030 reg_startpos[i] = subs.norm.list.multi[i].start;
5031 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032 }
5033
5034 if (reg_startpos[0].lnum < 0)
5035 {
5036 reg_startpos[0].lnum = 0;
5037 reg_startpos[0].col = col;
5038 }
5039 if (reg_endpos[0].lnum < 0)
5040 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005041 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005042 reg_endpos[0].lnum = reglnum;
5043 reg_endpos[0].col = (int)(reginput - regline);
5044 }
5045 else
5046 /* Use line number of "\ze". */
5047 reglnum = reg_endpos[0].lnum;
5048 }
5049 else
5050 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005051 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005052 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005053 reg_startp[i] = subs.norm.list.line[i].start;
5054 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 }
5056
5057 if (reg_startp[0] == NULL)
5058 reg_startp[0] = regline + col;
5059 if (reg_endp[0] == NULL)
5060 reg_endp[0] = reginput;
5061 }
5062
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005063#ifdef FEAT_SYN_HL
5064 /* Package any found \z(...\) matches for export. Default is none. */
5065 unref_extmatch(re_extmatch_out);
5066 re_extmatch_out = NULL;
5067
5068 if (prog->reghasz == REX_SET)
5069 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005070 cleanup_zsubexpr();
5071 re_extmatch_out = make_extmatch();
5072 for (i = 0; i < subs.synt.in_use; i++)
5073 {
5074 if (REG_MULTI)
5075 {
5076 struct multipos *mpos = &subs.synt.list.multi[i];
5077
5078 /* Only accept single line matches. */
5079 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5080 re_extmatch_out->matches[i] =
5081 vim_strnsave(reg_getline(mpos->start.lnum)
5082 + mpos->start.col,
5083 mpos->end.col - mpos->start.col);
5084 }
5085 else
5086 {
5087 struct linepos *lpos = &subs.synt.list.line[i];
5088
5089 if (lpos->start != NULL && lpos->end != NULL)
5090 re_extmatch_out->matches[i] =
5091 vim_strnsave(lpos->start,
5092 (int)(lpos->end - lpos->start));
5093 }
5094 }
5095 }
5096#endif
5097
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005098 return 1 + reglnum;
5099}
5100
5101/*
5102 * Match a regexp against a string ("line" points to the string) or multiple
5103 * lines ("line" is NULL, use reg_getline()).
5104 *
5105 * Returns 0 for failure, number of lines contained in the match otherwise.
5106 */
5107 static long
5108nfa_regexec_both(line, col)
5109 char_u *line;
5110 colnr_T col; /* column to start looking for match */
5111{
5112 nfa_regprog_T *prog;
5113 long retval = 0L;
5114 int i;
5115
5116 if (REG_MULTI)
5117 {
5118 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5119 line = reg_getline((linenr_T)0); /* relative to the cursor */
5120 reg_startpos = reg_mmatch->startpos;
5121 reg_endpos = reg_mmatch->endpos;
5122 }
5123 else
5124 {
5125 prog = (nfa_regprog_T *)reg_match->regprog;
5126 reg_startp = reg_match->startp;
5127 reg_endp = reg_match->endp;
5128 }
5129
5130 /* Be paranoid... */
5131 if (prog == NULL || line == NULL)
5132 {
5133 EMSG(_(e_null));
5134 goto theend;
5135 }
5136
5137 /* If the start column is past the maximum column: no need to try. */
5138 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5139 goto theend;
5140
5141 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5142 if (prog->regflags & RF_ICASE)
5143 ireg_ic = TRUE;
5144 else if (prog->regflags & RF_NOICASE)
5145 ireg_ic = FALSE;
5146
5147#ifdef FEAT_MBYTE
5148 /* If pattern contains "\Z" overrule value of ireg_icombine */
5149 if (prog->regflags & RF_ICOMBINE)
5150 ireg_icombine = TRUE;
5151#endif
5152
5153 regline = line;
5154 reglnum = 0; /* relative to line */
5155
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005156 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005157 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005158 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005159 nfa_listid = 1;
5160 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005161#ifdef DEBUG
5162 nfa_regengine.expr = prog->pattern;
5163#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005164
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005165 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005166 for (i = 0; i < nstate; ++i)
5167 {
5168 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005169 prog->state[i].lastlist[0] = 0;
5170 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005171 }
5172
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005173 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005174
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005175#ifdef DEBUG
5176 nfa_regengine.expr = NULL;
5177#endif
5178
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005179theend:
5180 return retval;
5181}
5182
5183/*
5184 * Compile a regular expression into internal code for the NFA matcher.
5185 * Returns the program in allocated space. Returns NULL for an error.
5186 */
5187 static regprog_T *
5188nfa_regcomp(expr, re_flags)
5189 char_u *expr;
5190 int re_flags;
5191{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005192 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005193 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005194 int *postfix;
5195
5196 if (expr == NULL)
5197 return NULL;
5198
5199#ifdef DEBUG
5200 nfa_regengine.expr = expr;
5201#endif
5202
5203 init_class_tab();
5204
5205 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5206 return NULL;
5207
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005208 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005209 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005210 postfix = re2post();
5211 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005212 {
5213 /* TODO: only give this error for debugging? */
5214 if (post_ptr >= post_end)
5215 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005216 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005217 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005218
5219 /*
5220 * In order to build the NFA, we parse the input regexp twice:
5221 * 1. first pass to count size (so we can allocate space)
5222 * 2. second to emit code
5223 */
5224#ifdef ENABLE_LOG
5225 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005226 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005227
5228 if (f != NULL)
5229 {
5230 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5231 fclose(f);
5232 }
5233 }
5234#endif
5235
5236 /*
5237 * PASS 1
5238 * Count number of NFA states in "nstate". Do not build the NFA.
5239 */
5240 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005241
5242 /* Space for compiled regexp */
5243 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5244 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5245 if (prog == NULL)
5246 goto fail;
5247 vim_memset(prog, 0, prog_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005248 state_ptr = prog->state;
5249
5250 /*
5251 * PASS 2
5252 * Build the NFA
5253 */
5254 prog->start = post2nfa(postfix, post_ptr, FALSE);
5255 if (prog->start == NULL)
5256 goto fail;
5257
5258 prog->regflags = regflags;
5259 prog->engine = &nfa_regengine;
5260 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005261 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005262 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005263 prog->nsubexp = regnpar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005264#ifdef ENABLE_LOG
5265 nfa_postfix_dump(expr, OK);
5266 nfa_dump(prog);
5267#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005268#ifdef FEAT_SYN_HL
5269 /* Remember whether this pattern has any \z specials in it. */
5270 prog->reghasz = re_has_z;
5271#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005272#ifdef DEBUG
5273 prog->pattern = vim_strsave(expr); /* memory will leak */
5274 nfa_regengine.expr = NULL;
5275#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005276
5277out:
5278 vim_free(post_start);
5279 post_start = post_ptr = post_end = NULL;
5280 state_ptr = NULL;
5281 return (regprog_T *)prog;
5282
5283fail:
5284 vim_free(prog);
5285 prog = NULL;
5286#ifdef ENABLE_LOG
5287 nfa_postfix_dump(expr, FAIL);
5288#endif
5289#ifdef DEBUG
5290 nfa_regengine.expr = NULL;
5291#endif
5292 goto out;
5293}
5294
5295
5296/*
5297 * Match a regexp against a string.
5298 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5299 * Uses curbuf for line count and 'iskeyword'.
5300 *
5301 * Return TRUE if there is a match, FALSE if not.
5302 */
5303 static int
5304nfa_regexec(rmp, line, col)
5305 regmatch_T *rmp;
5306 char_u *line; /* string to match against */
5307 colnr_T col; /* column to start looking for match */
5308{
5309 reg_match = rmp;
5310 reg_mmatch = NULL;
5311 reg_maxline = 0;
5312 reg_line_lbr = FALSE;
5313 reg_buf = curbuf;
5314 reg_win = NULL;
5315 ireg_ic = rmp->rm_ic;
5316#ifdef FEAT_MBYTE
5317 ireg_icombine = FALSE;
5318#endif
5319 ireg_maxcol = 0;
5320 return (nfa_regexec_both(line, col) != 0);
5321}
5322
5323#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5324 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5325
5326static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5327
5328/*
5329 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5330 */
5331 static int
5332nfa_regexec_nl(rmp, line, col)
5333 regmatch_T *rmp;
5334 char_u *line; /* string to match against */
5335 colnr_T col; /* column to start looking for match */
5336{
5337 reg_match = rmp;
5338 reg_mmatch = NULL;
5339 reg_maxline = 0;
5340 reg_line_lbr = TRUE;
5341 reg_buf = curbuf;
5342 reg_win = NULL;
5343 ireg_ic = rmp->rm_ic;
5344#ifdef FEAT_MBYTE
5345 ireg_icombine = FALSE;
5346#endif
5347 ireg_maxcol = 0;
5348 return (nfa_regexec_both(line, col) != 0);
5349}
5350#endif
5351
5352
5353/*
5354 * Match a regexp against multiple lines.
5355 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5356 * Uses curbuf for line count and 'iskeyword'.
5357 *
5358 * Return zero if there is no match. Return number of lines contained in the
5359 * match otherwise.
5360 *
5361 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5362 *
5363 * ! Also NOTE : match may actually be in another line. e.g.:
5364 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5365 *
5366 * +-------------------------+
5367 * |a |
5368 * |b |
5369 * |c |
5370 * | |
5371 * +-------------------------+
5372 *
5373 * then nfa_regexec_multi() returns 3. while the original
5374 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5375 *
5376 * FIXME if this behavior is not compatible.
5377 */
5378 static long
5379nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5380 regmmatch_T *rmp;
5381 win_T *win; /* window in which to search or NULL */
5382 buf_T *buf; /* buffer in which to search */
5383 linenr_T lnum; /* nr of line to start looking for match */
5384 colnr_T col; /* column to start looking for match */
5385 proftime_T *tm UNUSED; /* timeout limit or NULL */
5386{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005387 reg_match = NULL;
5388 reg_mmatch = rmp;
5389 reg_buf = buf;
5390 reg_win = win;
5391 reg_firstlnum = lnum;
5392 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5393 reg_line_lbr = FALSE;
5394 ireg_ic = rmp->rmm_ic;
5395#ifdef FEAT_MBYTE
5396 ireg_icombine = FALSE;
5397#endif
5398 ireg_maxcol = rmp->rmm_maxcol;
5399
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005400 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401}
5402
5403#ifdef DEBUG
5404# undef ENABLE_LOG
5405#endif