blob: 18bcad09807a2b53f4be4f12a90d995bfa35fc78 [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 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020037
Bram Moolenaar417bad22013-06-07 14:08:30 +020038 NFA_START_COLL, /* [abc] start */
39 NFA_END_COLL, /* [abc] end */
40 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020041 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
42 NFA_RANGE, /* range of the two previous items
43 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020044 NFA_RANGE_MIN, /* low end of a range */
45 NFA_RANGE_MAX, /* high end of a range */
46
Bram Moolenaare7766ee2013-06-08 22:30:03 +020047 NFA_CONCAT, /* concatenate two previous items (postfix
48 * only) */
49 NFA_OR, /* \| (postfix only) */
50 NFA_STAR, /* greedy * (posfix only) */
51 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
52 NFA_QUEST, /* greedy \? (postfix only) */
53 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020054
55 NFA_BOL, /* ^ Begin line */
56 NFA_EOL, /* $ End line */
57 NFA_BOW, /* \< Begin word */
58 NFA_EOW, /* \> End word */
59 NFA_BOF, /* \%^ Begin file */
60 NFA_EOF, /* \%$ End file */
61 NFA_NEWL,
62 NFA_ZSTART, /* Used for \zs */
63 NFA_ZEND, /* Used for \ze */
64 NFA_NOPEN, /* Start of subexpression marked with \%( */
65 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
66 NFA_START_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020067 NFA_START_INVISIBLE_NEG,
Bram Moolenaar61602c52013-06-01 19:54:43 +020068 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020069 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020070 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020071 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020072 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020073 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020074 NFA_COMPOSING, /* Next nodes in NFA are part of the
75 composing multibyte char */
76 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020077 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078
79 /* The following are used only in the postfix form, not in the NFA */
80 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
81 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
82 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
83 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
84 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
85
Bram Moolenaar5714b802013-05-28 22:03:20 +020086 NFA_BACKREF1, /* \1 */
87 NFA_BACKREF2, /* \2 */
88 NFA_BACKREF3, /* \3 */
89 NFA_BACKREF4, /* \4 */
90 NFA_BACKREF5, /* \5 */
91 NFA_BACKREF6, /* \6 */
92 NFA_BACKREF7, /* \7 */
93 NFA_BACKREF8, /* \8 */
94 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020095#ifdef FEAT_SYN_HL
96 NFA_ZREF1, /* \z1 */
97 NFA_ZREF2, /* \z2 */
98 NFA_ZREF3, /* \z3 */
99 NFA_ZREF4, /* \z4 */
100 NFA_ZREF5, /* \z5 */
101 NFA_ZREF6, /* \z6 */
102 NFA_ZREF7, /* \z7 */
103 NFA_ZREF8, /* \z8 */
104 NFA_ZREF9, /* \z9 */
105#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200106 NFA_SKIP, /* Skip characters */
107
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200108 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200109 NFA_MOPEN1,
110 NFA_MOPEN2,
111 NFA_MOPEN3,
112 NFA_MOPEN4,
113 NFA_MOPEN5,
114 NFA_MOPEN6,
115 NFA_MOPEN7,
116 NFA_MOPEN8,
117 NFA_MOPEN9,
118
119 NFA_MCLOSE,
120 NFA_MCLOSE1,
121 NFA_MCLOSE2,
122 NFA_MCLOSE3,
123 NFA_MCLOSE4,
124 NFA_MCLOSE5,
125 NFA_MCLOSE6,
126 NFA_MCLOSE7,
127 NFA_MCLOSE8,
128 NFA_MCLOSE9,
129
130#ifdef FEAT_SYN_HL
131 NFA_ZOPEN,
132 NFA_ZOPEN1,
133 NFA_ZOPEN2,
134 NFA_ZOPEN3,
135 NFA_ZOPEN4,
136 NFA_ZOPEN5,
137 NFA_ZOPEN6,
138 NFA_ZOPEN7,
139 NFA_ZOPEN8,
140 NFA_ZOPEN9,
141
142 NFA_ZCLOSE,
143 NFA_ZCLOSE1,
144 NFA_ZCLOSE2,
145 NFA_ZCLOSE3,
146 NFA_ZCLOSE4,
147 NFA_ZCLOSE5,
148 NFA_ZCLOSE6,
149 NFA_ZCLOSE7,
150 NFA_ZCLOSE8,
151 NFA_ZCLOSE9,
152#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200153
154 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200155 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200156 NFA_IDENT, /* Match identifier char */
157 NFA_SIDENT, /* Match identifier char but no digit */
158 NFA_KWORD, /* Match keyword char */
159 NFA_SKWORD, /* Match word char but no digit */
160 NFA_FNAME, /* Match file name char */
161 NFA_SFNAME, /* Match file name char but no digit */
162 NFA_PRINT, /* Match printable char */
163 NFA_SPRINT, /* Match printable char but no digit */
164 NFA_WHITE, /* Match whitespace char */
165 NFA_NWHITE, /* Match non-whitespace char */
166 NFA_DIGIT, /* Match digit char */
167 NFA_NDIGIT, /* Match non-digit char */
168 NFA_HEX, /* Match hex char */
169 NFA_NHEX, /* Match non-hex char */
170 NFA_OCTAL, /* Match octal char */
171 NFA_NOCTAL, /* Match non-octal char */
172 NFA_WORD, /* Match word char */
173 NFA_NWORD, /* Match non-word char */
174 NFA_HEAD, /* Match head char */
175 NFA_NHEAD, /* Match non-head char */
176 NFA_ALPHA, /* Match alpha char */
177 NFA_NALPHA, /* Match non-alpha char */
178 NFA_LOWER, /* Match lowercase char */
179 NFA_NLOWER, /* Match non-lowercase char */
180 NFA_UPPER, /* Match uppercase char */
181 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200182
183 NFA_CURSOR, /* Match cursor pos */
184 NFA_LNUM, /* Match line number */
185 NFA_LNUM_GT, /* Match > line number */
186 NFA_LNUM_LT, /* Match < line number */
187 NFA_COL, /* Match cursor column */
188 NFA_COL_GT, /* Match > cursor column */
189 NFA_COL_LT, /* Match < cursor column */
190 NFA_VCOL, /* Match cursor virtual column */
191 NFA_VCOL_GT, /* Match > cursor virtual column */
192 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200193 NFA_MARK, /* Match mark */
194 NFA_MARK_GT, /* Match > mark */
195 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200196 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200198 NFA_FIRST_NL = NFA_ANY + ADD_NL,
199 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
200
201 /* Character classes [:alnum:] etc */
202 NFA_CLASS_ALNUM,
203 NFA_CLASS_ALPHA,
204 NFA_CLASS_BLANK,
205 NFA_CLASS_CNTRL,
206 NFA_CLASS_DIGIT,
207 NFA_CLASS_GRAPH,
208 NFA_CLASS_LOWER,
209 NFA_CLASS_PRINT,
210 NFA_CLASS_PUNCT,
211 NFA_CLASS_SPACE,
212 NFA_CLASS_UPPER,
213 NFA_CLASS_XDIGIT,
214 NFA_CLASS_TAB,
215 NFA_CLASS_RETURN,
216 NFA_CLASS_BACKSPACE,
217 NFA_CLASS_ESCAPE
218};
219
220/* Keep in sync with classchars. */
221static int nfa_classcodes[] = {
222 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
223 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
224 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
225 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
226 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
227 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
228 NFA_UPPER, NFA_NUPPER
229};
230
231static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
232
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200234static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200235
Bram Moolenaar428e9872013-05-30 17:05:39 +0200236/* NFA regexp \1 .. \9 encountered. */
237static int nfa_has_backref;
238
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200239#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200240/* NFA regexp has \z( ), set zsubexpr. */
241static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200242#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200243
Bram Moolenaar963fee22013-05-26 21:47:28 +0200244/* Number of sub expressions actually being used during execution. 1 if only
245 * the whole match (subexpr 0) is used. */
246static int nfa_nsubexpr;
247
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200248static int *post_start; /* holds the postfix form of r.e. */
249static int *post_end;
250static int *post_ptr;
251
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200252static int nstate; /* Number of states in the NFA. Also used when
253 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200254static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200255
Bram Moolenaar307aa162013-06-02 16:34:21 +0200256/* If not NULL match must end at this position */
257static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200258
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200259/* listid is global, so that it increases on recursive calls to
260 * nfa_regmatch(), which means we don't have to clear the lastlist field of
261 * all the states. */
262static int nfa_listid;
263static int nfa_alt_listid;
264
265/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
266static int nfa_ll_index = 0;
267
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200268static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200269static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
270static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200271static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200273static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200274static int nfa_regatom __ARGS((void));
275static int nfa_regpiece __ARGS((void));
276static int nfa_regconcat __ARGS((void));
277static int nfa_regbranch __ARGS((void));
278static int nfa_reg __ARGS((int paren));
279#ifdef DEBUG
280static void nfa_set_code __ARGS((int c));
281static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200282static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
283static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200284static void nfa_dump __ARGS((nfa_regprog_T *prog));
285#endif
286static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200287static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200288static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
289static int check_char_class __ARGS((int class, int c));
290static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200291static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
292static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200293static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200294static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
296static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200297static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200298static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
299static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
300
301/* helper functions used when doing re2post() ... regatom() parsing */
302#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200303 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304 return FAIL; \
305 *post_ptr++ = c; \
306 } while (0)
307
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308/*
309 * Initialize internal variables before NFA compilation.
310 * Return OK on success, FAIL otherwise.
311 */
312 static int
313nfa_regcomp_start(expr, re_flags)
314 char_u *expr;
315 int re_flags; /* see vim_regcomp() */
316{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200317 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200318 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200319
320 nstate = 0;
321 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200322 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200323 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200325 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200326 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200327 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200328
329 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200330 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200331
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200332 post_start = (int *)lalloc(postfix_size, TRUE);
333 if (post_start == NULL)
334 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200336 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200337 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200338 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200340 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341 regcomp_start(expr, re_flags);
342
343 return OK;
344}
345
346/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200347 * Figure out if the NFA state list starts with an anchor, must match at start
348 * of the line.
349 */
350 static int
351nfa_get_reganch(start, depth)
352 nfa_state_T *start;
353 int depth;
354{
355 nfa_state_T *p = start;
356
357 if (depth > 4)
358 return 0;
359
360 while (p != NULL)
361 {
362 switch (p->c)
363 {
364 case NFA_BOL:
365 case NFA_BOF:
366 return 1; /* yes! */
367
368 case NFA_ZSTART:
369 case NFA_ZEND:
370 case NFA_CURSOR:
371 case NFA_VISUAL:
372
373 case NFA_MOPEN:
374 case NFA_MOPEN1:
375 case NFA_MOPEN2:
376 case NFA_MOPEN3:
377 case NFA_MOPEN4:
378 case NFA_MOPEN5:
379 case NFA_MOPEN6:
380 case NFA_MOPEN7:
381 case NFA_MOPEN8:
382 case NFA_MOPEN9:
383 case NFA_NOPEN:
384#ifdef FEAT_SYN_HL
385 case NFA_ZOPEN:
386 case NFA_ZOPEN1:
387 case NFA_ZOPEN2:
388 case NFA_ZOPEN3:
389 case NFA_ZOPEN4:
390 case NFA_ZOPEN5:
391 case NFA_ZOPEN6:
392 case NFA_ZOPEN7:
393 case NFA_ZOPEN8:
394 case NFA_ZOPEN9:
395#endif
396 p = p->out;
397 break;
398
399 case NFA_SPLIT:
400 return nfa_get_reganch(p->out, depth + 1)
401 && nfa_get_reganch(p->out1, depth + 1);
402
403 default:
404 return 0; /* noooo */
405 }
406 }
407 return 0;
408}
409
410/*
411 * Figure out if the NFA state list starts with a character which must match
412 * at start of the match.
413 */
414 static int
415nfa_get_regstart(start, depth)
416 nfa_state_T *start;
417 int depth;
418{
419 nfa_state_T *p = start;
420
421 if (depth > 4)
422 return 0;
423
424 while (p != NULL)
425 {
426 switch (p->c)
427 {
428 /* all kinds of zero-width matches */
429 case NFA_BOL:
430 case NFA_BOF:
431 case NFA_BOW:
432 case NFA_EOW:
433 case NFA_ZSTART:
434 case NFA_ZEND:
435 case NFA_CURSOR:
436 case NFA_VISUAL:
437 case NFA_LNUM:
438 case NFA_LNUM_GT:
439 case NFA_LNUM_LT:
440 case NFA_COL:
441 case NFA_COL_GT:
442 case NFA_COL_LT:
443 case NFA_VCOL:
444 case NFA_VCOL_GT:
445 case NFA_VCOL_LT:
446 case NFA_MARK:
447 case NFA_MARK_GT:
448 case NFA_MARK_LT:
449
450 case NFA_MOPEN:
451 case NFA_MOPEN1:
452 case NFA_MOPEN2:
453 case NFA_MOPEN3:
454 case NFA_MOPEN4:
455 case NFA_MOPEN5:
456 case NFA_MOPEN6:
457 case NFA_MOPEN7:
458 case NFA_MOPEN8:
459 case NFA_MOPEN9:
460 case NFA_NOPEN:
461#ifdef FEAT_SYN_HL
462 case NFA_ZOPEN:
463 case NFA_ZOPEN1:
464 case NFA_ZOPEN2:
465 case NFA_ZOPEN3:
466 case NFA_ZOPEN4:
467 case NFA_ZOPEN5:
468 case NFA_ZOPEN6:
469 case NFA_ZOPEN7:
470 case NFA_ZOPEN8:
471 case NFA_ZOPEN9:
472#endif
473 p = p->out;
474 break;
475
476 case NFA_SPLIT:
477 {
478 int c1 = nfa_get_regstart(p->out, depth + 1);
479 int c2 = nfa_get_regstart(p->out1, depth + 1);
480
481 if (c1 == c2)
482 return c1; /* yes! */
483 return 0;
484 }
485
486 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200487 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200488 return p->c; /* yes! */
489 return 0;
490 }
491 }
492 return 0;
493}
494
495/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200497 * else. If so return a string in allocated memory with what must match after
498 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200499 */
500 static char_u *
501nfa_get_match_text(start)
502 nfa_state_T *start;
503{
504 nfa_state_T *p = start;
505 int len = 0;
506 char_u *ret;
507 char_u *s;
508
509 if (p->c != NFA_MOPEN)
510 return NULL; /* just in case */
511 p = p->out;
512 while (p->c > 0)
513 {
514 len += MB_CHAR2LEN(p->c);
515 p = p->out;
516 }
517 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
518 return NULL;
519
520 ret = alloc(len);
521 if (ret != NULL)
522 {
523 len = 0;
524 p = start->out->out; /* skip first char, it goes into regstart */
525 s = ret;
526 while (p->c > 0)
527 {
528#ifdef FEAT_MBYTE
529 if (has_mbyte)
530 s += (*mb_char2bytes)(p->c, s);
531 else
532#endif
533 *s++ = p->c;
534 p = p->out;
535 }
536 *s = NUL;
537 }
538 return ret;
539}
540
541/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200542 * Allocate more space for post_start. Called when
543 * running above the estimated number of states.
544 */
545 static int
546realloc_post_list()
547{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200548 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200549 int new_max = nstate_max + 1000;
550 int *new_start;
551 int *old_start;
552
553 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
554 if (new_start == NULL)
555 return FAIL;
556 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200557 old_start = post_start;
558 post_start = new_start;
559 post_ptr = new_start + (post_ptr - old_start);
560 post_end = post_start + new_max;
561 vim_free(old_start);
562 return OK;
563}
564
565/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200566 * Search between "start" and "end" and try to recognize a
567 * character class in expanded form. For example [0-9].
568 * On success, return the id the character class to be emitted.
569 * On failure, return 0 (=FAIL)
570 * Start points to the first char of the range, while end should point
571 * to the closing brace.
572 */
573 static int
574nfa_recognize_char_class(start, end, extra_newl)
575 char_u *start;
576 char_u *end;
577 int extra_newl;
578{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200579# define CLASS_not 0x80
580# define CLASS_af 0x40
581# define CLASS_AF 0x20
582# define CLASS_az 0x10
583# define CLASS_AZ 0x08
584# define CLASS_o7 0x04
585# define CLASS_o9 0x02
586# define CLASS_underscore 0x01
587
588 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200589 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200590 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200591
592 if (extra_newl == TRUE)
593 newl = TRUE;
594
595 if (*end != ']')
596 return FAIL;
597 p = start;
598 if (*p == '^')
599 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200600 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200601 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200602 }
603
604 while (p < end)
605 {
606 if (p + 2 < end && *(p + 1) == '-')
607 {
608 switch (*p)
609 {
610 case '0':
611 if (*(p + 2) == '9')
612 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200613 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200614 break;
615 }
616 else
617 if (*(p + 2) == '7')
618 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200619 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200620 break;
621 }
622 case 'a':
623 if (*(p + 2) == 'z')
624 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200625 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200626 break;
627 }
628 else
629 if (*(p + 2) == 'f')
630 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200631 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200632 break;
633 }
634 case 'A':
635 if (*(p + 2) == 'Z')
636 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200637 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 break;
639 }
640 else
641 if (*(p + 2) == 'F')
642 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 break;
645 }
646 /* FALLTHROUGH */
647 default:
648 return FAIL;
649 }
650 p += 3;
651 }
652 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
653 {
654 newl = TRUE;
655 p += 2;
656 }
657 else if (*p == '_')
658 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200659 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 p ++;
661 }
662 else if (*p == '\n')
663 {
664 newl = TRUE;
665 p ++;
666 }
667 else
668 return FAIL;
669 } /* while (p < end) */
670
671 if (p != end)
672 return FAIL;
673
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200674 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676
677 switch (config)
678 {
679 case CLASS_o9:
680 return extra_newl + NFA_DIGIT;
681 case CLASS_not | CLASS_o9:
682 return extra_newl + NFA_NDIGIT;
683 case CLASS_af | CLASS_AF | CLASS_o9:
684 return extra_newl + NFA_HEX;
685 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
686 return extra_newl + NFA_NHEX;
687 case CLASS_o7:
688 return extra_newl + NFA_OCTAL;
689 case CLASS_not | CLASS_o7:
690 return extra_newl + NFA_NOCTAL;
691 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
692 return extra_newl + NFA_WORD;
693 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
694 return extra_newl + NFA_NWORD;
695 case CLASS_az | CLASS_AZ | CLASS_underscore:
696 return extra_newl + NFA_HEAD;
697 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
698 return extra_newl + NFA_NHEAD;
699 case CLASS_az | CLASS_AZ:
700 return extra_newl + NFA_ALPHA;
701 case CLASS_not | CLASS_az | CLASS_AZ:
702 return extra_newl + NFA_NALPHA;
703 case CLASS_az:
704 return extra_newl + NFA_LOWER;
705 case CLASS_not | CLASS_az:
706 return extra_newl + NFA_NLOWER;
707 case CLASS_AZ:
708 return extra_newl + NFA_UPPER;
709 case CLASS_not | CLASS_AZ:
710 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200711 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200712 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200713}
714
715/*
716 * Produce the bytes for equivalence class "c".
717 * Currently only handles latin1, latin9 and utf-8.
718 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
719 * equivalent to 'a OR b OR c'
720 *
721 * NOTE! When changing this function, also update reg_equi_class()
722 */
723 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200724nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200725 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200726{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200727#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728
729#ifdef FEAT_MBYTE
730 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
731 || STRCMP(p_enc, "iso-8859-15") == 0)
732#endif
733 {
734 switch (c)
735 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200736 case 'A': case 0300: case 0301: case 0302:
737 case 0303: case 0304: case 0305:
738 EMIT2('A'); EMIT2(0300); EMIT2(0301);
739 EMIT2(0302); EMIT2(0303); EMIT2(0304);
740 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200741 return OK;
742
Bram Moolenaar417bad22013-06-07 14:08:30 +0200743 case 'C': case 0307:
744 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200745 return OK;
746
Bram Moolenaar417bad22013-06-07 14:08:30 +0200747 case 'E': case 0310: case 0311: case 0312: case 0313:
748 EMIT2('E'); EMIT2(0310); EMIT2(0311);
749 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200750 return OK;
751
Bram Moolenaar417bad22013-06-07 14:08:30 +0200752 case 'I': case 0314: case 0315: case 0316: case 0317:
753 EMIT2('I'); EMIT2(0314); EMIT2(0315);
754 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200755 return OK;
756
Bram Moolenaar417bad22013-06-07 14:08:30 +0200757 case 'N': case 0321:
758 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 return OK;
760
Bram Moolenaar417bad22013-06-07 14:08:30 +0200761 case 'O': case 0322: case 0323: case 0324: case 0325:
762 case 0326:
763 EMIT2('O'); EMIT2(0322); EMIT2(0323);
764 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200765 return OK;
766
Bram Moolenaar417bad22013-06-07 14:08:30 +0200767 case 'U': case 0331: case 0332: case 0333: case 0334:
768 EMIT2('U'); EMIT2(0331); EMIT2(0332);
769 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200770 return OK;
771
Bram Moolenaar417bad22013-06-07 14:08:30 +0200772 case 'Y': case 0335:
773 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 return OK;
775
Bram Moolenaar417bad22013-06-07 14:08:30 +0200776 case 'a': case 0340: case 0341: case 0342:
777 case 0343: case 0344: case 0345:
778 EMIT2('a'); EMIT2(0340); EMIT2(0341);
779 EMIT2(0342); EMIT2(0343); EMIT2(0344);
780 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200781 return OK;
782
Bram Moolenaar417bad22013-06-07 14:08:30 +0200783 case 'c': case 0347:
784 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200785 return OK;
786
Bram Moolenaar417bad22013-06-07 14:08:30 +0200787 case 'e': case 0350: case 0351: case 0352: case 0353:
788 EMIT2('e'); EMIT2(0350); EMIT2(0351);
789 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200790 return OK;
791
Bram Moolenaar417bad22013-06-07 14:08:30 +0200792 case 'i': case 0354: case 0355: case 0356: case 0357:
793 EMIT2('i'); EMIT2(0354); EMIT2(0355);
794 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200795 return OK;
796
Bram Moolenaar417bad22013-06-07 14:08:30 +0200797 case 'n': case 0361:
798 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200799 return OK;
800
Bram Moolenaar417bad22013-06-07 14:08:30 +0200801 case 'o': case 0362: case 0363: case 0364: case 0365:
802 case 0366:
803 EMIT2('o'); EMIT2(0362); EMIT2(0363);
804 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200805 return OK;
806
Bram Moolenaar417bad22013-06-07 14:08:30 +0200807 case 'u': case 0371: case 0372: case 0373: case 0374:
808 EMIT2('u'); EMIT2(0371); EMIT2(0372);
809 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200810 return OK;
811
Bram Moolenaar417bad22013-06-07 14:08:30 +0200812 case 'y': case 0375: case 0377:
813 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 return OK;
815
816 default:
817 return FAIL;
818 }
819 }
820
821 EMIT(c);
822 return OK;
823#undef EMIT2
824}
825
826/*
827 * Code to parse regular expression.
828 *
829 * We try to reuse parsing functions in regexp.c to
830 * minimize surprise and keep the syntax consistent.
831 */
832
833/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200834 * Parse the lowest level.
835 *
836 * An atom can be one of a long list of items. Many atoms match one character
837 * in the text. It is often an ordinary character or a character class.
838 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
839 * is only for syntax highlighting.
840 *
841 * atom ::= ordinary-atom
842 * or \( pattern \)
843 * or \%( pattern \)
844 * or \z( pattern \)
845 */
846 static int
847nfa_regatom()
848{
849 int c;
850 int charclass;
851 int equiclass;
852 int collclass;
853 int got_coll_char;
854 char_u *p;
855 char_u *endp;
856#ifdef FEAT_MBYTE
857 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200858#endif
859 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200860 int emit_range;
861 int negated;
862 int result;
863 int startc = -1;
864 int endc = -1;
865 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200866
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200867 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 switch (c)
869 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200870 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200871 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
872
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873 case Magic('^'):
874 EMIT(NFA_BOL);
875 break;
876
877 case Magic('$'):
878 EMIT(NFA_EOL);
879#if defined(FEAT_SYN_HL) || defined(PROTO)
880 had_eol = TRUE;
881#endif
882 break;
883
884 case Magic('<'):
885 EMIT(NFA_BOW);
886 break;
887
888 case Magic('>'):
889 EMIT(NFA_EOW);
890 break;
891
892 case Magic('_'):
893 c = no_Magic(getchr());
894 if (c == '^') /* "\_^" is start-of-line */
895 {
896 EMIT(NFA_BOL);
897 break;
898 }
899 if (c == '$') /* "\_$" is end-of-line */
900 {
901 EMIT(NFA_EOL);
902#if defined(FEAT_SYN_HL) || defined(PROTO)
903 had_eol = TRUE;
904#endif
905 break;
906 }
907
908 extra = ADD_NL;
909
910 /* "\_[" is collection plus newline */
911 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200912 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200913
914 /* "\_x" is character class plus newline */
915 /*FALLTHROUGH*/
916
917 /*
918 * Character classes.
919 */
920 case Magic('.'):
921 case Magic('i'):
922 case Magic('I'):
923 case Magic('k'):
924 case Magic('K'):
925 case Magic('f'):
926 case Magic('F'):
927 case Magic('p'):
928 case Magic('P'):
929 case Magic('s'):
930 case Magic('S'):
931 case Magic('d'):
932 case Magic('D'):
933 case Magic('x'):
934 case Magic('X'):
935 case Magic('o'):
936 case Magic('O'):
937 case Magic('w'):
938 case Magic('W'):
939 case Magic('h'):
940 case Magic('H'):
941 case Magic('a'):
942 case Magic('A'):
943 case Magic('l'):
944 case Magic('L'):
945 case Magic('u'):
946 case Magic('U'):
947 p = vim_strchr(classchars, no_Magic(c));
948 if (p == NULL)
949 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200950 EMSGN("INTERNAL: Unknown character class char: %ld", c);
951 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200952 }
953#ifdef FEAT_MBYTE
954 /* When '.' is followed by a composing char ignore the dot, so that
955 * the composing char is matched here. */
956 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
957 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200958 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200959 c = getchr();
960 goto nfa_do_multibyte;
961 }
962#endif
963 EMIT(nfa_classcodes[p - classchars]);
964 if (extra == ADD_NL)
965 {
966 EMIT(NFA_NEWL);
967 EMIT(NFA_OR);
968 regflags |= RF_HASNL;
969 }
970 break;
971
972 case Magic('n'):
973 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200974 /* In a string "\n" matches a newline character. */
975 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200976 else
977 {
978 /* In buffer text "\n" matches the end of a line. */
979 EMIT(NFA_NEWL);
980 regflags |= RF_HASNL;
981 }
982 break;
983
984 case Magic('('):
985 if (nfa_reg(REG_PAREN) == FAIL)
986 return FAIL; /* cascaded error */
987 break;
988
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200989 case Magic('|'):
990 case Magic('&'):
991 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200992 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200993 return FAIL;
994
995 case Magic('='):
996 case Magic('?'):
997 case Magic('+'):
998 case Magic('@'):
999 case Magic('*'):
1000 case Magic('{'):
1001 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001002 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 return FAIL;
1004
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001005 case Magic('~'):
1006 {
1007 char_u *lp;
1008
1009 /* Previous substitute pattern.
1010 * Generated as "\%(pattern\)". */
1011 if (reg_prev_sub == NULL)
1012 {
1013 EMSG(_(e_nopresub));
1014 return FAIL;
1015 }
1016 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1017 {
1018 EMIT(PTR2CHAR(lp));
1019 if (lp != reg_prev_sub)
1020 EMIT(NFA_CONCAT);
1021 }
1022 EMIT(NFA_NOPEN);
1023 break;
1024 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001025
Bram Moolenaar428e9872013-05-30 17:05:39 +02001026 case Magic('1'):
1027 case Magic('2'):
1028 case Magic('3'):
1029 case Magic('4'):
1030 case Magic('5'):
1031 case Magic('6'):
1032 case Magic('7'):
1033 case Magic('8'):
1034 case Magic('9'):
1035 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1036 nfa_has_backref = TRUE;
1037 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001038
1039 case Magic('z'):
1040 c = no_Magic(getchr());
1041 switch (c)
1042 {
1043 case 's':
1044 EMIT(NFA_ZSTART);
1045 break;
1046 case 'e':
1047 EMIT(NFA_ZEND);
1048 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001049 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001050#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001051 case '1':
1052 case '2':
1053 case '3':
1054 case '4':
1055 case '5':
1056 case '6':
1057 case '7':
1058 case '8':
1059 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001060 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001061 if (reg_do_extmatch != REX_USE)
1062 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001063 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1064 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001065 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001066 re_has_z = REX_USE;
1067 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001068 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001069 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001070 if (reg_do_extmatch != REX_SET)
1071 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001072 if (nfa_reg(REG_ZPAREN) == FAIL)
1073 return FAIL; /* cascaded error */
1074 re_has_z = REX_SET;
1075 break;
1076#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001078 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001079 no_Magic(c));
1080 return FAIL;
1081 }
1082 break;
1083
1084 case Magic('%'):
1085 c = no_Magic(getchr());
1086 switch (c)
1087 {
1088 /* () without a back reference */
1089 case '(':
1090 if (nfa_reg(REG_NPAREN) == FAIL)
1091 return FAIL;
1092 EMIT(NFA_NOPEN);
1093 break;
1094
1095 case 'd': /* %d123 decimal */
1096 case 'o': /* %o123 octal */
1097 case 'x': /* %xab hex 2 */
1098 case 'u': /* %uabcd hex 4 */
1099 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001100 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001101 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001102
Bram Moolenaar47196582013-05-25 22:04:23 +02001103 switch (c)
1104 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001105 case 'd': nr = getdecchrs(); break;
1106 case 'o': nr = getoctchrs(); break;
1107 case 'x': nr = gethexchrs(2); break;
1108 case 'u': nr = gethexchrs(4); break;
1109 case 'U': nr = gethexchrs(8); break;
1110 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001111 }
1112
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001113 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001114 EMSG2_RET_FAIL(
1115 _("E678: Invalid character after %s%%[dxouU]"),
1116 reg_magic == MAGIC_ALL);
1117 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001118 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001119 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120 break;
1121
1122 /* Catch \%^ and \%$ regardless of where they appear in the
1123 * pattern -- regardless of whether or not it makes sense. */
1124 case '^':
1125 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001126 break;
1127
1128 case '$':
1129 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001130 break;
1131
1132 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001133 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001134 break;
1135
1136 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001137 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138 break;
1139
1140 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001141 {
1142 int n;
1143
1144 /* \%[abc] */
1145 for (n = 0; (c = getchr()) != ']'; ++n)
1146 {
1147 if (c == NUL)
1148 EMSG2_RET_FAIL(_(e_missing_sb),
1149 reg_magic == MAGIC_ALL);
1150 EMIT(c);
1151 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001152 if (n == 0)
1153 EMSG2_RET_FAIL(_(e_empty_sb),
1154 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001155 EMIT(NFA_OPT_CHARS);
1156 EMIT(n);
1157 break;
1158 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001160 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001161 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001162 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001163 int cmp = c;
1164
1165 if (c == '<' || c == '>')
1166 c = getchr();
1167 while (VIM_ISDIGIT(c))
1168 {
1169 n = n * 10 + (c - '0');
1170 c = getchr();
1171 }
1172 if (c == 'l' || c == 'c' || c == 'v')
1173 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001174 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001175 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001176 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001177 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001178 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001179 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001180 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001181 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001182 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001183 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001184 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001185 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001186 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001187 break;
1188 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001189 else if (c == '\'' && n == 0)
1190 {
1191 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001192 EMIT(cmp == '<' ? NFA_MARK_LT :
1193 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001194 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001195 break;
1196 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001197 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001198 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1199 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001200 return FAIL;
1201 }
1202 break;
1203
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001204 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001205collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001207 * [abc] uses NFA_START_COLL - NFA_END_COLL
1208 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1209 * Each character is produced as a regular state, using
1210 * NFA_CONCAT to bind them together.
1211 * Besides normal characters there can be:
1212 * - character classes NFA_CLASS_*
1213 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 */
1215
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216 p = regparse;
1217 endp = skip_anyof(p);
1218 if (*endp == ']')
1219 {
1220 /*
1221 * Try to reverse engineer character classes. For example,
1222 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1223 * and perform the necessary substitutions in the NFA.
1224 */
1225 result = nfa_recognize_char_class(regparse, endp,
1226 extra == ADD_NL);
1227 if (result != FAIL)
1228 {
1229 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1230 EMIT(result);
1231 else /* must be char class + newline */
1232 {
1233 EMIT(result - ADD_NL);
1234 EMIT(NFA_NEWL);
1235 EMIT(NFA_OR);
1236 }
1237 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001238 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001239 return OK;
1240 }
1241 /*
1242 * Failed to recognize a character class. Use the simple
1243 * version that turns [abc] into 'a' OR 'b' OR 'c'
1244 */
1245 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001247 if (*regparse == '^') /* negated range */
1248 {
1249 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001250 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001251 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001253 else
1254 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 if (*regparse == '-')
1256 {
1257 startc = '-';
1258 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001259 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001260 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001261 }
1262 /* Emit the OR branches for each character in the [] */
1263 emit_range = FALSE;
1264 while (regparse < endp)
1265 {
1266 oldstartc = startc;
1267 startc = -1;
1268 got_coll_char = FALSE;
1269 if (*regparse == '[')
1270 {
1271 /* Check for [: :], [= =], [. .] */
1272 equiclass = collclass = 0;
1273 charclass = get_char_class(&regparse);
1274 if (charclass == CLASS_NONE)
1275 {
1276 equiclass = get_equi_class(&regparse);
1277 if (equiclass == 0)
1278 collclass = get_coll_element(&regparse);
1279 }
1280
1281 /* Character class like [:alpha:] */
1282 if (charclass != CLASS_NONE)
1283 {
1284 switch (charclass)
1285 {
1286 case CLASS_ALNUM:
1287 EMIT(NFA_CLASS_ALNUM);
1288 break;
1289 case CLASS_ALPHA:
1290 EMIT(NFA_CLASS_ALPHA);
1291 break;
1292 case CLASS_BLANK:
1293 EMIT(NFA_CLASS_BLANK);
1294 break;
1295 case CLASS_CNTRL:
1296 EMIT(NFA_CLASS_CNTRL);
1297 break;
1298 case CLASS_DIGIT:
1299 EMIT(NFA_CLASS_DIGIT);
1300 break;
1301 case CLASS_GRAPH:
1302 EMIT(NFA_CLASS_GRAPH);
1303 break;
1304 case CLASS_LOWER:
1305 EMIT(NFA_CLASS_LOWER);
1306 break;
1307 case CLASS_PRINT:
1308 EMIT(NFA_CLASS_PRINT);
1309 break;
1310 case CLASS_PUNCT:
1311 EMIT(NFA_CLASS_PUNCT);
1312 break;
1313 case CLASS_SPACE:
1314 EMIT(NFA_CLASS_SPACE);
1315 break;
1316 case CLASS_UPPER:
1317 EMIT(NFA_CLASS_UPPER);
1318 break;
1319 case CLASS_XDIGIT:
1320 EMIT(NFA_CLASS_XDIGIT);
1321 break;
1322 case CLASS_TAB:
1323 EMIT(NFA_CLASS_TAB);
1324 break;
1325 case CLASS_RETURN:
1326 EMIT(NFA_CLASS_RETURN);
1327 break;
1328 case CLASS_BACKSPACE:
1329 EMIT(NFA_CLASS_BACKSPACE);
1330 break;
1331 case CLASS_ESCAPE:
1332 EMIT(NFA_CLASS_ESCAPE);
1333 break;
1334 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001335 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 continue;
1337 }
1338 /* Try equivalence class [=a=] and the like */
1339 if (equiclass != 0)
1340 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001341 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342 if (result == FAIL)
1343 {
1344 /* should never happen */
1345 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1346 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001347 continue;
1348 }
1349 /* Try collating class like [. .] */
1350 if (collclass != 0)
1351 {
1352 startc = collclass; /* allow [.a.]-x as a range */
1353 /* Will emit the proper atom at the end of the
1354 * while loop. */
1355 }
1356 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001357 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1358 * start character. */
1359 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001360 {
1361 emit_range = TRUE;
1362 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001363 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 continue; /* reading the end of the range */
1365 }
1366
1367 /* Now handle simple and escaped characters.
1368 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1369 * accepts "\t", "\e", etc., but only when the 'l' flag in
1370 * 'cpoptions' is not included.
1371 * Posix doesn't recognize backslash at all.
1372 */
1373 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001374 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001375 && regparse + 1 <= endp
1376 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001377 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001378 && vim_strchr(REGEXP_ABBR, regparse[1])
1379 != NULL)
1380 )
1381 )
1382 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001383 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001384
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001385 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386 startc = reg_string ? NL : NFA_NEWL;
1387 else
1388 if (*regparse == 'd'
1389 || *regparse == 'o'
1390 || *regparse == 'x'
1391 || *regparse == 'u'
1392 || *regparse == 'U'
1393 )
1394 {
1395 /* TODO(RE) This needs more testing */
1396 startc = coll_get_char();
1397 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001398 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 }
1400 else
1401 {
1402 /* \r,\t,\e,\b */
1403 startc = backslash_trans(*regparse);
1404 }
1405 }
1406
1407 /* Normal printable char */
1408 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001409 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001410
1411 /* Previous char was '-', so this char is end of range. */
1412 if (emit_range)
1413 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001414 endc = startc;
1415 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 if (startc > endc)
1417 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001418
1419 if (endc > startc + 2)
1420 {
1421 /* Emit a range instead of the sequence of
1422 * individual characters. */
1423 if (startc == 0)
1424 /* \x00 is translated to \x0a, start at \x01. */
1425 EMIT(1);
1426 else
1427 --post_ptr; /* remove NFA_CONCAT */
1428 EMIT(endc);
1429 EMIT(NFA_RANGE);
1430 EMIT(NFA_CONCAT);
1431 }
1432 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001433#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001434 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001435 || (*mb_char2len)(endc) > 1))
1436 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001437 /* Emit the characters in the range.
1438 * "startc" was already emitted, so skip it.
1439 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440 for (c = startc + 1; c <= endc; c++)
1441 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001442 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001443 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 }
1446 else
1447#endif
1448 {
1449#ifdef EBCDIC
1450 int alpha_only = FALSE;
1451
1452 /* for alphabetical range skip the gaps
1453 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1454 if (isalpha(startc) && isalpha(endc))
1455 alpha_only = TRUE;
1456#endif
1457 /* Emit the range. "startc" was already emitted, so
1458 * skip it. */
1459 for (c = startc + 1; c <= endc; c++)
1460#ifdef EBCDIC
1461 if (!alpha_only || isalpha(startc))
1462#endif
1463 {
1464 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001465 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001466 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001468 emit_range = FALSE;
1469 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001470 }
1471 else
1472 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001473 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 * Normally, simply emit startc. But if we get char
1476 * code=0 from a collating char, then replace it with
1477 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001478 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001479 * the backtracking engine. */
1480 if (startc == NFA_NEWL)
1481 {
1482 /* Line break can't be matched as part of the
1483 * collection, add an OR below. But not for negated
1484 * range. */
1485 if (!negated)
1486 extra = ADD_NL;
1487 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001488 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001489 {
1490 if (got_coll_char == TRUE && startc == 0)
1491 EMIT(0x0a);
1492 else
1493 EMIT(startc);
1494 EMIT(NFA_CONCAT);
1495 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 }
1497
Bram Moolenaar51a29832013-05-28 22:30:35 +02001498 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 } /* while (p < endp) */
1500
Bram Moolenaar51a29832013-05-28 22:30:35 +02001501 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001502 if (*regparse == '-') /* if last, '-' is just a char */
1503 {
1504 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001505 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001507 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 /* skip the trailing ] */
1510 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001511 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001512
1513 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001515 EMIT(NFA_END_NEG_COLL);
1516 else
1517 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001518
1519 /* \_[] also matches \n but it's not negated */
1520 if (extra == ADD_NL)
1521 {
1522 EMIT(reg_string ? NL : NFA_NEWL);
1523 EMIT(NFA_OR);
1524 }
1525
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001527 } /* if exists closing ] */
1528
1529 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001531 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 default:
1534 {
1535#ifdef FEAT_MBYTE
1536 int plen;
1537
1538nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001539 /* plen is length of current char with composing chars */
1540 if (enc_utf8 && ((*mb_char2len)(c)
1541 != (plen = (*mb_ptr2len)(old_regparse))
1542 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001544 int i = 0;
1545
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001546 /* A base character plus composing characters, or just one
1547 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001548 * This requires creating a separate atom as if enclosing
1549 * the characters in (), where NFA_COMPOSING is the ( and
1550 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 * building the postfix form, not the NFA itself;
1552 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001553 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001554 for (;;)
1555 {
1556 EMIT(c);
1557 if (i > 0)
1558 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001559 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001560 break;
1561 c = utf_ptr2char(old_regparse + i);
1562 }
1563 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 regparse = old_regparse + plen;
1565 }
1566 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001567#endif
1568 {
1569 c = no_Magic(c);
1570 EMIT(c);
1571 }
1572 return OK;
1573 }
1574 }
1575
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001576 return OK;
1577}
1578
1579/*
1580 * Parse something followed by possible [*+=].
1581 *
1582 * A piece is an atom, possibly followed by a multi, an indication of how many
1583 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1584 * characters: "", "a", "aa", etc.
1585 *
1586 * piece ::= atom
1587 * or atom multi
1588 */
1589 static int
1590nfa_regpiece()
1591{
1592 int i;
1593 int op;
1594 int ret;
1595 long minval, maxval;
1596 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001597 parse_state_T old_state;
1598 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001600 int old_post_pos;
1601 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 int quest;
1603
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001604 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1605 * next. */
1606 save_parse_state(&old_state);
1607
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001608 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001609 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610
1611 ret = nfa_regatom();
1612 if (ret == FAIL)
1613 return FAIL; /* cascaded error */
1614
1615 op = peekchr();
1616 if (re_multi_type(op) == NOT_MULTI)
1617 return OK;
1618
1619 skipchr();
1620 switch (op)
1621 {
1622 case Magic('*'):
1623 EMIT(NFA_STAR);
1624 break;
1625
1626 case Magic('+'):
1627 /*
1628 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1629 * first and only submatch would be "aaa". But the backtracking
1630 * engine interprets the plus as "try matching one more time", and
1631 * a* matches a second time at the end of the input, the empty
1632 * string.
1633 * The submatch will the empty string.
1634 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001635 * In order to be consistent with the old engine, we replace
1636 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001638 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 curchr = -1;
1640 if (nfa_regatom() == FAIL)
1641 return FAIL;
1642 EMIT(NFA_STAR);
1643 EMIT(NFA_CONCAT);
1644 skipchr(); /* skip the \+ */
1645 break;
1646
1647 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001648 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001649 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001650 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 switch(op)
1652 {
1653 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001654 /* \@= */
1655 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001656 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001657 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001658 /* \@! */
1659 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001660 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001662 op = no_Magic(getchr());
1663 if (op == '=')
1664 /* \@<= */
1665 i = NFA_PREV_ATOM_JUST_BEFORE;
1666 else if (op == '!')
1667 /* \@<! */
1668 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1669 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001670 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001671 /* \@> */
1672 i = NFA_PREV_ATOM_LIKE_PATTERN;
1673 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001675 if (i == 0)
1676 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001677 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1678 return FAIL;
1679 }
1680 EMIT(i);
1681 if (i == NFA_PREV_ATOM_JUST_BEFORE
1682 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1683 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 break;
1685
1686 case Magic('?'):
1687 case Magic('='):
1688 EMIT(NFA_QUEST);
1689 break;
1690
1691 case Magic('{'):
1692 /* a{2,5} will expand to 'aaa?a?a?'
1693 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1694 * version of '?'
1695 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1696 * parenthesis have the same id
1697 */
1698
1699 greedy = TRUE;
1700 c2 = peekchr();
1701 if (c2 == '-' || c2 == Magic('-'))
1702 {
1703 skipchr();
1704 greedy = FALSE;
1705 }
1706 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001708
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001709 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1710 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001711 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001712 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001713 if (greedy)
1714 /* \{}, \{0,} */
1715 EMIT(NFA_STAR);
1716 else
1717 /* \{-}, \{-0,} */
1718 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001719 break;
1720 }
1721
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001722 /* Special case: x{0} or x{-0} */
1723 if (maxval == 0)
1724 {
1725 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001726 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001727 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1728 EMIT(NFA_SKIP_CHAR);
1729 return OK;
1730 }
1731
1732 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001733 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001734 /* Save parse state after the repeated atom and the \{} */
1735 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001736
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1738 for (i = 0; i < maxval; i++)
1739 {
1740 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001741 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001742 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743 if (nfa_regatom() == FAIL)
1744 return FAIL;
1745 /* after "minval" times, atoms are optional */
1746 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001747 {
1748 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001749 {
1750 if (greedy)
1751 EMIT(NFA_STAR);
1752 else
1753 EMIT(NFA_STAR_NONGREEDY);
1754 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001755 else
1756 EMIT(quest);
1757 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001758 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001759 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001760 if (i + 1 > minval && maxval == MAX_LIMIT)
1761 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001762 }
1763
1764 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001765 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 curchr = -1;
1767
1768 break;
1769
1770
1771 default:
1772 break;
1773 } /* end switch */
1774
1775 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001776 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778
1779 return OK;
1780}
1781
1782/*
1783 * Parse one or more pieces, concatenated. It matches a match for the
1784 * first piece, followed by a match for the second piece, etc. Example:
1785 * "f[0-9]b", first matches "f", then a digit and then "b".
1786 *
1787 * concat ::= piece
1788 * or piece piece
1789 * or piece piece piece
1790 * etc.
1791 */
1792 static int
1793nfa_regconcat()
1794{
1795 int cont = TRUE;
1796 int first = TRUE;
1797
1798 while (cont)
1799 {
1800 switch (peekchr())
1801 {
1802 case NUL:
1803 case Magic('|'):
1804 case Magic('&'):
1805 case Magic(')'):
1806 cont = FALSE;
1807 break;
1808
1809 case Magic('Z'):
1810#ifdef FEAT_MBYTE
1811 regflags |= RF_ICOMBINE;
1812#endif
1813 skipchr_keepstart();
1814 break;
1815 case Magic('c'):
1816 regflags |= RF_ICASE;
1817 skipchr_keepstart();
1818 break;
1819 case Magic('C'):
1820 regflags |= RF_NOICASE;
1821 skipchr_keepstart();
1822 break;
1823 case Magic('v'):
1824 reg_magic = MAGIC_ALL;
1825 skipchr_keepstart();
1826 curchr = -1;
1827 break;
1828 case Magic('m'):
1829 reg_magic = MAGIC_ON;
1830 skipchr_keepstart();
1831 curchr = -1;
1832 break;
1833 case Magic('M'):
1834 reg_magic = MAGIC_OFF;
1835 skipchr_keepstart();
1836 curchr = -1;
1837 break;
1838 case Magic('V'):
1839 reg_magic = MAGIC_NONE;
1840 skipchr_keepstart();
1841 curchr = -1;
1842 break;
1843
1844 default:
1845 if (nfa_regpiece() == FAIL)
1846 return FAIL;
1847 if (first == FALSE)
1848 EMIT(NFA_CONCAT);
1849 else
1850 first = FALSE;
1851 break;
1852 }
1853 }
1854
1855 return OK;
1856}
1857
1858/*
1859 * Parse a branch, one or more concats, separated by "\&". It matches the
1860 * last concat, but only if all the preceding concats also match at the same
1861 * position. Examples:
1862 * "foobeep\&..." matches "foo" in "foobeep".
1863 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1864 *
1865 * branch ::= concat
1866 * or concat \& concat
1867 * or concat \& concat \& concat
1868 * etc.
1869 */
1870 static int
1871nfa_regbranch()
1872{
1873 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001874 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001875
Bram Moolenaar16299b52013-05-30 18:45:23 +02001876 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877
1878 /* First branch, possibly the only one */
1879 if (nfa_regconcat() == FAIL)
1880 return FAIL;
1881
1882 ch = peekchr();
1883 /* Try next concats */
1884 while (ch == Magic('&'))
1885 {
1886 skipchr();
1887 EMIT(NFA_NOPEN);
1888 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001889 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890 if (nfa_regconcat() == FAIL)
1891 return FAIL;
1892 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001893 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894 EMIT(NFA_SKIP_CHAR);
1895 EMIT(NFA_CONCAT);
1896 ch = peekchr();
1897 }
1898
1899 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001900 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 EMIT(NFA_SKIP_CHAR);
1902
1903 return OK;
1904}
1905
1906/*
1907 * Parse a pattern, one or more branches, separated by "\|". It matches
1908 * anything that matches one of the branches. Example: "foo\|beep" matches
1909 * "foo" and matches "beep". If more than one branch matches, the first one
1910 * is used.
1911 *
1912 * pattern ::= branch
1913 * or branch \| branch
1914 * or branch \| branch \| branch
1915 * etc.
1916 */
1917 static int
1918nfa_reg(paren)
1919 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1920{
1921 int parno = 0;
1922
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 if (paren == REG_PAREN)
1924 {
1925 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 parno = regnpar++;
1928 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001929#ifdef FEAT_SYN_HL
1930 else if (paren == REG_ZPAREN)
1931 {
1932 /* Make a ZOPEN node. */
1933 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001934 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001935 parno = regnzpar++;
1936 }
1937#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938
1939 if (nfa_regbranch() == FAIL)
1940 return FAIL; /* cascaded error */
1941
1942 while (peekchr() == Magic('|'))
1943 {
1944 skipchr();
1945 if (nfa_regbranch() == FAIL)
1946 return FAIL; /* cascaded error */
1947 EMIT(NFA_OR);
1948 }
1949
1950 /* Check for proper termination. */
1951 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1952 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 if (paren == REG_NPAREN)
1954 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1955 else
1956 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1957 }
1958 else if (paren == REG_NOPAREN && peekchr() != NUL)
1959 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 if (peekchr() == Magic(')'))
1961 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1962 else
1963 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1964 }
1965 /*
1966 * Here we set the flag allowing back references to this set of
1967 * parentheses.
1968 */
1969 if (paren == REG_PAREN)
1970 {
1971 had_endbrace[parno] = TRUE; /* have seen the close paren */
1972 EMIT(NFA_MOPEN + parno);
1973 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001974#ifdef FEAT_SYN_HL
1975 else if (paren == REG_ZPAREN)
1976 EMIT(NFA_ZOPEN + parno);
1977#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001978
1979 return OK;
1980}
1981
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001982#ifdef DEBUG
1983static char_u code[50];
1984
1985 static void
1986nfa_set_code(c)
1987 int c;
1988{
1989 int addnl = FALSE;
1990
1991 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1992 {
1993 addnl = TRUE;
1994 c -= ADD_NL;
1995 }
1996
1997 STRCPY(code, "");
1998 switch (c)
1999 {
2000 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2001 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2002 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2003 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2004 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2005 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2006
Bram Moolenaar5714b802013-05-28 22:03:20 +02002007 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2008 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2009 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2010 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2011 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2012 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2013 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2014 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2015 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002016#ifdef FEAT_SYN_HL
2017 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2018 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2019 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2020 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2021 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2022 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2023 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2024 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2025 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2026#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002027 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2028
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029 case NFA_PREV_ATOM_NO_WIDTH:
2030 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002031 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2032 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002033 case NFA_PREV_ATOM_JUST_BEFORE:
2034 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2035 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2036 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002037 case NFA_PREV_ATOM_LIKE_PATTERN:
2038 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2039
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002040 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2041 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002043 case NFA_START_INVISIBLE_NEG:
2044 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002045 case NFA_START_INVISIBLE_BEFORE:
2046 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002047 case NFA_START_INVISIBLE_BEFORE_NEG:
2048 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002049 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002050 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002051 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002052 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2055 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002056 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002057
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002058 case NFA_MOPEN:
2059 case NFA_MOPEN1:
2060 case NFA_MOPEN2:
2061 case NFA_MOPEN3:
2062 case NFA_MOPEN4:
2063 case NFA_MOPEN5:
2064 case NFA_MOPEN6:
2065 case NFA_MOPEN7:
2066 case NFA_MOPEN8:
2067 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 STRCPY(code, "NFA_MOPEN(x)");
2069 code[10] = c - NFA_MOPEN + '0';
2070 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002071 case NFA_MCLOSE:
2072 case NFA_MCLOSE1:
2073 case NFA_MCLOSE2:
2074 case NFA_MCLOSE3:
2075 case NFA_MCLOSE4:
2076 case NFA_MCLOSE5:
2077 case NFA_MCLOSE6:
2078 case NFA_MCLOSE7:
2079 case NFA_MCLOSE8:
2080 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081 STRCPY(code, "NFA_MCLOSE(x)");
2082 code[11] = c - NFA_MCLOSE + '0';
2083 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002084#ifdef FEAT_SYN_HL
2085 case NFA_ZOPEN:
2086 case NFA_ZOPEN1:
2087 case NFA_ZOPEN2:
2088 case NFA_ZOPEN3:
2089 case NFA_ZOPEN4:
2090 case NFA_ZOPEN5:
2091 case NFA_ZOPEN6:
2092 case NFA_ZOPEN7:
2093 case NFA_ZOPEN8:
2094 case NFA_ZOPEN9:
2095 STRCPY(code, "NFA_ZOPEN(x)");
2096 code[10] = c - NFA_ZOPEN + '0';
2097 break;
2098 case NFA_ZCLOSE:
2099 case NFA_ZCLOSE1:
2100 case NFA_ZCLOSE2:
2101 case NFA_ZCLOSE3:
2102 case NFA_ZCLOSE4:
2103 case NFA_ZCLOSE5:
2104 case NFA_ZCLOSE6:
2105 case NFA_ZCLOSE7:
2106 case NFA_ZCLOSE8:
2107 case NFA_ZCLOSE9:
2108 STRCPY(code, "NFA_ZCLOSE(x)");
2109 code[11] = c - NFA_ZCLOSE + '0';
2110 break;
2111#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2113 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2114 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2115 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002116 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2117 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002118 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2119 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2120 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2121 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2122 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2123 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2124 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2125 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2126 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2127 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2128 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2129 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2130 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2131 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2132
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002133 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002134 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2135 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2136 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002137 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2138 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002139
2140 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2141 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2142 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2143 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2144 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2145 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2146 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2147
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002148 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2149 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2150 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2151 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2152 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2153 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2154 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2155 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2156 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2157 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2158 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2159 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2160 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2161 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2162 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2163 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2164
2165 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2166 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2167 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2168 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2169 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2170 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2171 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2172 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2173 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2174 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2175 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2176 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2177 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2178 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2179 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2180 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2181 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2182 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2183 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2184 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2185 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2186 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2187 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2188 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2189 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2190 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2191 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2192
2193 default:
2194 STRCPY(code, "CHAR(x)");
2195 code[5] = c;
2196 }
2197
2198 if (addnl == TRUE)
2199 STRCAT(code, " + NEWLINE ");
2200
2201}
2202
2203#ifdef ENABLE_LOG
2204static FILE *log_fd;
2205
2206/*
2207 * Print the postfix notation of the current regexp.
2208 */
2209 static void
2210nfa_postfix_dump(expr, retval)
2211 char_u *expr;
2212 int retval;
2213{
2214 int *p;
2215 FILE *f;
2216
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002217 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 if (f != NULL)
2219 {
2220 fprintf(f, "\n-------------------------\n");
2221 if (retval == FAIL)
2222 fprintf(f, ">>> NFA engine failed ... \n");
2223 else if (retval == OK)
2224 fprintf(f, ">>> NFA engine succeeded !\n");
2225 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002226 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002227 {
2228 nfa_set_code(*p);
2229 fprintf(f, "%s, ", code);
2230 }
2231 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002232 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 fprintf(f, "%d ", *p);
2234 fprintf(f, "\n\n");
2235 fclose(f);
2236 }
2237}
2238
2239/*
2240 * Print the NFA starting with a root node "state".
2241 */
2242 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002243nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002244 FILE *debugf;
2245 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002246{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002247 garray_T indent;
2248
2249 ga_init2(&indent, 1, 64);
2250 ga_append(&indent, '\0');
2251 nfa_print_state2(debugf, state, &indent);
2252 ga_clear(&indent);
2253}
2254
2255 static void
2256nfa_print_state2(debugf, state, indent)
2257 FILE *debugf;
2258 nfa_state_T *state;
2259 garray_T *indent;
2260{
2261 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002262
2263 if (state == NULL)
2264 return;
2265
2266 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002267
2268 /* Output indent */
2269 p = (char_u *)indent->ga_data;
2270 if (indent->ga_len >= 3)
2271 {
2272 int last = indent->ga_len - 3;
2273 char_u save[2];
2274
2275 STRNCPY(save, &p[last], 2);
2276 STRNCPY(&p[last], "+-", 2);
2277 fprintf(debugf, " %s", p);
2278 STRNCPY(&p[last], save, 2);
2279 }
2280 else
2281 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282
2283 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002284 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002285 code,
2286 state->c,
2287 abs(state->id),
2288 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 if (state->id < 0)
2290 return;
2291
2292 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002293
2294 /* grow indent for state->out */
2295 indent->ga_len -= 1;
2296 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002297 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002298 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002299 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002300 ga_append(indent, '\0');
2301
2302 nfa_print_state2(debugf, state->out, indent);
2303
2304 /* replace last part of indent for state->out1 */
2305 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002306 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002307 ga_append(indent, '\0');
2308
2309 nfa_print_state2(debugf, state->out1, indent);
2310
2311 /* shrink indent */
2312 indent->ga_len -= 3;
2313 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314}
2315
2316/*
2317 * Print the NFA state machine.
2318 */
2319 static void
2320nfa_dump(prog)
2321 nfa_regprog_T *prog;
2322{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002323 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324
2325 if (debugf != NULL)
2326 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002327 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002328
Bram Moolenaar473de612013-06-08 18:19:48 +02002329 if (prog->reganch)
2330 fprintf(debugf, "reganch: %d\n", prog->reganch);
2331 if (prog->regstart != NUL)
2332 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2333 prog->regstart, prog->regstart);
2334 if (prog->match_text != NULL)
2335 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002336
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002337 fclose(debugf);
2338 }
2339}
2340#endif /* ENABLE_LOG */
2341#endif /* DEBUG */
2342
2343/*
2344 * Parse r.e. @expr and convert it into postfix form.
2345 * Return the postfix string on success, NULL otherwise.
2346 */
2347 static int *
2348re2post()
2349{
2350 if (nfa_reg(REG_NOPAREN) == FAIL)
2351 return NULL;
2352 EMIT(NFA_MOPEN);
2353 return post_start;
2354}
2355
2356/* NB. Some of the code below is inspired by Russ's. */
2357
2358/*
2359 * Represents an NFA state plus zero or one or two arrows exiting.
2360 * if c == MATCH, no arrows out; matching state.
2361 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2362 * If c < 256, labeled arrow with character c to out.
2363 */
2364
2365static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2366
2367/*
2368 * Allocate and initialize nfa_state_T.
2369 */
2370 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002371alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002372 int c;
2373 nfa_state_T *out;
2374 nfa_state_T *out1;
2375{
2376 nfa_state_T *s;
2377
2378 if (istate >= nstate)
2379 return NULL;
2380
2381 s = &state_ptr[istate++];
2382
2383 s->c = c;
2384 s->out = out;
2385 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002386 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387
2388 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002389 s->lastlist[0] = 0;
2390 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002391
2392 return s;
2393}
2394
2395/*
2396 * A partially built NFA without the matching state filled in.
2397 * Frag_T.start points at the start state.
2398 * Frag_T.out is a list of places that need to be set to the
2399 * next state for this fragment.
2400 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002401
2402/* Since the out pointers in the list are always
2403 * uninitialized, we use the pointers themselves
2404 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002405typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002406union Ptrlist
2407{
2408 Ptrlist *next;
2409 nfa_state_T *s;
2410};
2411
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412struct Frag
2413{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002414 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415 Ptrlist *out;
2416};
2417typedef struct Frag Frag_T;
2418
2419static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2420static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2421static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2422static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2423static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2424static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2425
2426/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002427 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428 */
2429 static Frag_T
2430frag(start, out)
2431 nfa_state_T *start;
2432 Ptrlist *out;
2433{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002434 Frag_T n;
2435
2436 n.start = start;
2437 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002438 return n;
2439}
2440
2441/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 * Create singleton list containing just outp.
2443 */
2444 static Ptrlist *
2445list1(outp)
2446 nfa_state_T **outp;
2447{
2448 Ptrlist *l;
2449
2450 l = (Ptrlist *)outp;
2451 l->next = NULL;
2452 return l;
2453}
2454
2455/*
2456 * Patch the list of states at out to point to start.
2457 */
2458 static void
2459patch(l, s)
2460 Ptrlist *l;
2461 nfa_state_T *s;
2462{
2463 Ptrlist *next;
2464
2465 for (; l; l = next)
2466 {
2467 next = l->next;
2468 l->s = s;
2469 }
2470}
2471
2472
2473/*
2474 * Join the two lists l1 and l2, returning the combination.
2475 */
2476 static Ptrlist *
2477append(l1, l2)
2478 Ptrlist *l1;
2479 Ptrlist *l2;
2480{
2481 Ptrlist *oldl1;
2482
2483 oldl1 = l1;
2484 while (l1->next)
2485 l1 = l1->next;
2486 l1->next = l2;
2487 return oldl1;
2488}
2489
2490/*
2491 * Stack used for transforming postfix form into NFA.
2492 */
2493static Frag_T empty;
2494
2495 static void
2496st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002497 int *postfix UNUSED;
2498 int *end UNUSED;
2499 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002501#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502 FILE *df;
2503 int *p2;
2504
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002505 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 if (df)
2507 {
2508 fprintf(df, "Error popping the stack!\n");
2509#ifdef DEBUG
2510 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2511#endif
2512 fprintf(df, "Postfix form is: ");
2513#ifdef DEBUG
2514 for (p2 = postfix; p2 < end; p2++)
2515 {
2516 nfa_set_code(*p2);
2517 fprintf(df, "%s, ", code);
2518 }
2519 nfa_set_code(*p);
2520 fprintf(df, "\nCurrent position is: ");
2521 for (p2 = postfix; p2 <= p; p2 ++)
2522 {
2523 nfa_set_code(*p2);
2524 fprintf(df, "%s, ", code);
2525 }
2526#else
2527 for (p2 = postfix; p2 < end; p2++)
2528 {
2529 fprintf(df, "%d, ", *p2);
2530 }
2531 fprintf(df, "\nCurrent position is: ");
2532 for (p2 = postfix; p2 <= p; p2 ++)
2533 {
2534 fprintf(df, "%d, ", *p2);
2535 }
2536#endif
2537 fprintf(df, "\n--------------------------\n");
2538 fclose(df);
2539 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002540#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541 EMSG(_("E874: (NFA) Could not pop the stack !"));
2542}
2543
2544/*
2545 * Push an item onto the stack.
2546 */
2547 static void
2548st_push(s, p, stack_end)
2549 Frag_T s;
2550 Frag_T **p;
2551 Frag_T *stack_end;
2552{
2553 Frag_T *stackp = *p;
2554
2555 if (stackp >= stack_end)
2556 return;
2557 *stackp = s;
2558 *p = *p + 1;
2559}
2560
2561/*
2562 * Pop an item from the stack.
2563 */
2564 static Frag_T
2565st_pop(p, stack)
2566 Frag_T **p;
2567 Frag_T *stack;
2568{
2569 Frag_T *stackp;
2570
2571 *p = *p - 1;
2572 stackp = *p;
2573 if (stackp < stack)
2574 return empty;
2575 return **p;
2576}
2577
2578/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002579 * Estimate the maximum byte length of anything matching "state".
2580 * When unknown or unlimited return -1.
2581 */
2582 static int
2583nfa_max_width(startstate, depth)
2584 nfa_state_T *startstate;
2585 int depth;
2586{
2587 int l, r;
2588 nfa_state_T *state = startstate;
2589 int len = 0;
2590
2591 /* detect looping in a NFA_SPLIT */
2592 if (depth > 4)
2593 return -1;
2594
2595 for (;;)
2596 {
2597 switch (state->c)
2598 {
2599 case NFA_END_INVISIBLE:
2600 case NFA_END_INVISIBLE_NEG:
2601 /* the end, return what we have */
2602 return len;
2603
2604 case NFA_SPLIT:
2605 /* two alternatives, use the maximum */
2606 l = nfa_max_width(state->out, depth + 1);
2607 r = nfa_max_width(state->out1, depth + 1);
2608 if (l < 0 || r < 0)
2609 return -1;
2610 return len + (l > r ? l : r);
2611
2612 case NFA_ANY:
2613 case NFA_START_COLL:
2614 case NFA_START_NEG_COLL:
2615 /* matches some character, including composing chars */
2616#ifdef FEAT_MBYTE
2617 if (enc_utf8)
2618 len += MB_MAXBYTES;
2619 else if (has_mbyte)
2620 len += 2;
2621 else
2622#endif
2623 ++len;
2624 if (state->c != NFA_ANY)
2625 {
2626 /* skip over the characters */
2627 state = state->out1->out;
2628 continue;
2629 }
2630 break;
2631
2632 case NFA_DIGIT:
2633 case NFA_WHITE:
2634 case NFA_HEX:
2635 case NFA_OCTAL:
2636 /* ascii */
2637 ++len;
2638 break;
2639
2640 case NFA_IDENT:
2641 case NFA_SIDENT:
2642 case NFA_KWORD:
2643 case NFA_SKWORD:
2644 case NFA_FNAME:
2645 case NFA_SFNAME:
2646 case NFA_PRINT:
2647 case NFA_SPRINT:
2648 case NFA_NWHITE:
2649 case NFA_NDIGIT:
2650 case NFA_NHEX:
2651 case NFA_NOCTAL:
2652 case NFA_WORD:
2653 case NFA_NWORD:
2654 case NFA_HEAD:
2655 case NFA_NHEAD:
2656 case NFA_ALPHA:
2657 case NFA_NALPHA:
2658 case NFA_LOWER:
2659 case NFA_NLOWER:
2660 case NFA_UPPER:
2661 case NFA_NUPPER:
2662 /* possibly non-ascii */
2663#ifdef FEAT_MBYTE
2664 if (has_mbyte)
2665 len += 3;
2666 else
2667#endif
2668 ++len;
2669 break;
2670
2671 case NFA_START_INVISIBLE:
2672 case NFA_START_INVISIBLE_NEG:
2673 case NFA_START_INVISIBLE_BEFORE:
2674 case NFA_START_INVISIBLE_BEFORE_NEG:
2675 /* zero-width, out1 points to the END state */
2676 state = state->out1->out;
2677 continue;
2678
2679 case NFA_BACKREF1:
2680 case NFA_BACKREF2:
2681 case NFA_BACKREF3:
2682 case NFA_BACKREF4:
2683 case NFA_BACKREF5:
2684 case NFA_BACKREF6:
2685 case NFA_BACKREF7:
2686 case NFA_BACKREF8:
2687 case NFA_BACKREF9:
2688#ifdef FEAT_SYN_HL
2689 case NFA_ZREF1:
2690 case NFA_ZREF2:
2691 case NFA_ZREF3:
2692 case NFA_ZREF4:
2693 case NFA_ZREF5:
2694 case NFA_ZREF6:
2695 case NFA_ZREF7:
2696 case NFA_ZREF8:
2697 case NFA_ZREF9:
2698#endif
2699 case NFA_NEWL:
2700 case NFA_SKIP:
2701 /* unknown width */
2702 return -1;
2703
2704 case NFA_BOL:
2705 case NFA_EOL:
2706 case NFA_BOF:
2707 case NFA_EOF:
2708 case NFA_BOW:
2709 case NFA_EOW:
2710 case NFA_MOPEN:
2711 case NFA_MOPEN1:
2712 case NFA_MOPEN2:
2713 case NFA_MOPEN3:
2714 case NFA_MOPEN4:
2715 case NFA_MOPEN5:
2716 case NFA_MOPEN6:
2717 case NFA_MOPEN7:
2718 case NFA_MOPEN8:
2719 case NFA_MOPEN9:
2720#ifdef FEAT_SYN_HL
2721 case NFA_ZOPEN:
2722 case NFA_ZOPEN1:
2723 case NFA_ZOPEN2:
2724 case NFA_ZOPEN3:
2725 case NFA_ZOPEN4:
2726 case NFA_ZOPEN5:
2727 case NFA_ZOPEN6:
2728 case NFA_ZOPEN7:
2729 case NFA_ZOPEN8:
2730 case NFA_ZOPEN9:
2731 case NFA_ZCLOSE:
2732 case NFA_ZCLOSE1:
2733 case NFA_ZCLOSE2:
2734 case NFA_ZCLOSE3:
2735 case NFA_ZCLOSE4:
2736 case NFA_ZCLOSE5:
2737 case NFA_ZCLOSE6:
2738 case NFA_ZCLOSE7:
2739 case NFA_ZCLOSE8:
2740 case NFA_ZCLOSE9:
2741#endif
2742 case NFA_MCLOSE:
2743 case NFA_MCLOSE1:
2744 case NFA_MCLOSE2:
2745 case NFA_MCLOSE3:
2746 case NFA_MCLOSE4:
2747 case NFA_MCLOSE5:
2748 case NFA_MCLOSE6:
2749 case NFA_MCLOSE7:
2750 case NFA_MCLOSE8:
2751 case NFA_MCLOSE9:
2752 case NFA_NOPEN:
2753 case NFA_NCLOSE:
2754
2755 case NFA_LNUM_GT:
2756 case NFA_LNUM_LT:
2757 case NFA_COL_GT:
2758 case NFA_COL_LT:
2759 case NFA_VCOL_GT:
2760 case NFA_VCOL_LT:
2761 case NFA_MARK_GT:
2762 case NFA_MARK_LT:
2763 case NFA_VISUAL:
2764 case NFA_LNUM:
2765 case NFA_CURSOR:
2766 case NFA_COL:
2767 case NFA_VCOL:
2768 case NFA_MARK:
2769
2770 case NFA_ZSTART:
2771 case NFA_ZEND:
2772 case NFA_OPT_CHARS:
2773 case NFA_SKIP_CHAR:
2774 case NFA_START_PATTERN:
2775 case NFA_END_PATTERN:
2776 case NFA_COMPOSING:
2777 case NFA_END_COMPOSING:
2778 /* zero-width */
2779 break;
2780
2781 default:
2782 if (state->c < 0)
2783 /* don't know what this is */
2784 return -1;
2785 /* normal character */
2786 len += MB_CHAR2LEN(state->c);
2787 break;
2788 }
2789
2790 /* normal way to continue */
2791 state = state->out;
2792 }
2793
2794 /* unrecognized */
2795 return -1;
2796}
2797/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798 * Convert a postfix form into its equivalent NFA.
2799 * Return the NFA start state on success, NULL otherwise.
2800 */
2801 static nfa_state_T *
2802post2nfa(postfix, end, nfa_calc_size)
2803 int *postfix;
2804 int *end;
2805 int nfa_calc_size;
2806{
2807 int *p;
2808 int mopen;
2809 int mclose;
2810 Frag_T *stack = NULL;
2811 Frag_T *stackp = NULL;
2812 Frag_T *stack_end = NULL;
2813 Frag_T e1;
2814 Frag_T e2;
2815 Frag_T e;
2816 nfa_state_T *s;
2817 nfa_state_T *s1;
2818 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002819 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002820
2821 if (postfix == NULL)
2822 return NULL;
2823
Bram Moolenaar053bb602013-05-20 13:55:21 +02002824#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002825#define POP() st_pop(&stackp, stack); \
2826 if (stackp < stack) \
2827 { \
2828 st_error(postfix, end, p); \
2829 return NULL; \
2830 }
2831
2832 if (nfa_calc_size == FALSE)
2833 {
2834 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002835 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002837 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002838 }
2839
2840 for (p = postfix; p < end; ++p)
2841 {
2842 switch (*p)
2843 {
2844 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002845 /* Concatenation.
2846 * Pay attention: this operator does not exist in the r.e. itself
2847 * (it is implicit, really). It is added when r.e. is translated
2848 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849 if (nfa_calc_size == TRUE)
2850 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002851 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002852 break;
2853 }
2854 e2 = POP();
2855 e1 = POP();
2856 patch(e1.out, e2.start);
2857 PUSH(frag(e1.start, e2.out));
2858 break;
2859
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860 case NFA_OR:
2861 /* Alternation */
2862 if (nfa_calc_size == TRUE)
2863 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002864 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002865 break;
2866 }
2867 e2 = POP();
2868 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002869 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002870 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002871 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002872 PUSH(frag(s, append(e1.out, e2.out)));
2873 break;
2874
2875 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002876 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877 if (nfa_calc_size == TRUE)
2878 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002879 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002880 break;
2881 }
2882 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002883 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002885 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 patch(e.out, s);
2887 PUSH(frag(s, list1(&s->out1)));
2888 break;
2889
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002890 case NFA_STAR_NONGREEDY:
2891 /* Zero or more, prefer zero */
2892 if (nfa_calc_size == TRUE)
2893 {
2894 nstate++;
2895 break;
2896 }
2897 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002898 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002899 if (s == NULL)
2900 goto theend;
2901 patch(e.out, s);
2902 PUSH(frag(s, list1(&s->out)));
2903 break;
2904
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905 case NFA_QUEST:
2906 /* one or zero atoms=> greedy match */
2907 if (nfa_calc_size == TRUE)
2908 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002909 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002910 break;
2911 }
2912 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002913 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002915 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916 PUSH(frag(s, append(e.out, list1(&s->out1))));
2917 break;
2918
2919 case NFA_QUEST_NONGREEDY:
2920 /* zero or one atoms => non-greedy match */
2921 if (nfa_calc_size == TRUE)
2922 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002923 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002924 break;
2925 }
2926 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002927 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002928 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002929 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930 PUSH(frag(s, append(e.out, list1(&s->out))));
2931 break;
2932
Bram Moolenaar417bad22013-06-07 14:08:30 +02002933 case NFA_END_COLL:
2934 case NFA_END_NEG_COLL:
2935 /* On the stack is the sequence starting with NFA_START_COLL or
2936 * NFA_START_NEG_COLL and all possible characters. Patch it to
2937 * add the output to the start. */
2938 if (nfa_calc_size == TRUE)
2939 {
2940 nstate++;
2941 break;
2942 }
2943 e = POP();
2944 s = alloc_state(NFA_END_COLL, NULL, NULL);
2945 if (s == NULL)
2946 goto theend;
2947 patch(e.out, s);
2948 e.start->out1 = s;
2949 PUSH(frag(e.start, list1(&s->out)));
2950 break;
2951
2952 case NFA_RANGE:
2953 /* Before this are two characters, the low and high end of a
2954 * range. Turn them into two states with MIN and MAX. */
2955 if (nfa_calc_size == TRUE)
2956 {
2957 /* nstate += 0; */
2958 break;
2959 }
2960 e2 = POP();
2961 e1 = POP();
2962 e2.start->val = e2.start->c;
2963 e2.start->c = NFA_RANGE_MAX;
2964 e1.start->val = e1.start->c;
2965 e1.start->c = NFA_RANGE_MIN;
2966 patch(e1.out, e2.start);
2967 PUSH(frag(e1.start, e2.out));
2968 break;
2969
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002970 case NFA_SKIP_CHAR:
2971 /* Symbol of 0-length, Used in a repetition
2972 * with max/min count of 0 */
2973 if (nfa_calc_size == TRUE)
2974 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002975 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 break;
2977 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002978 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002979 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002980 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002981 PUSH(frag(s, list1(&s->out)));
2982 break;
2983
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002984 case NFA_OPT_CHARS:
2985 {
2986 int n;
2987
2988 /* \%[abc] */
2989 n = *++p; /* get number of characters */
2990 if (nfa_calc_size == TRUE)
2991 {
2992 nstate += n;
2993 break;
2994 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002995 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002996 e1.out = NULL; /* stores list with out1's */
2997 s1 = NULL; /* previous NFA_SPLIT to connect to */
2998 while (n-- > 0)
2999 {
3000 e = POP(); /* get character */
3001 s = alloc_state(NFA_SPLIT, e.start, NULL);
3002 if (s == NULL)
3003 goto theend;
3004 if (e1.out == NULL)
3005 e1 = e;
3006 patch(e.out, s1);
3007 append(e1.out, list1(&s->out1));
3008 s1 = s;
3009 }
3010 PUSH(frag(s, e1.out));
3011 break;
3012 }
3013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003015 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003016 case NFA_PREV_ATOM_JUST_BEFORE:
3017 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003018 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003019 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003020 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3021 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003022 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003023 int start_state;
3024 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003025 int n = 0;
3026 nfa_state_T *zend;
3027 nfa_state_T *skip;
3028
Bram Moolenaardecd9542013-06-07 16:31:50 +02003029 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003030 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003031 case NFA_PREV_ATOM_NO_WIDTH:
3032 start_state = NFA_START_INVISIBLE;
3033 end_state = NFA_END_INVISIBLE;
3034 break;
3035 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3036 start_state = NFA_START_INVISIBLE_NEG;
3037 end_state = NFA_END_INVISIBLE_NEG;
3038 break;
3039 case NFA_PREV_ATOM_JUST_BEFORE:
3040 start_state = NFA_START_INVISIBLE_BEFORE;
3041 end_state = NFA_END_INVISIBLE;
3042 break;
3043 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3044 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3045 end_state = NFA_END_INVISIBLE_NEG;
3046 break;
3047 case NFA_PREV_ATOM_LIKE_PATTERN:
3048 start_state = NFA_START_PATTERN;
3049 end_state = NFA_END_PATTERN;
3050 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003051 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003052
3053 if (before)
3054 n = *++p; /* get the count */
3055
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003056 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003057 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003058 * The \@<= operator: match for the preceding atom.
3059 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003060 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003061 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003062
3063 if (nfa_calc_size == TRUE)
3064 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003065 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003066 break;
3067 }
3068 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003069 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003070 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003071 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003072
Bram Moolenaar87953742013-06-05 18:52:40 +02003073 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003074 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003075 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003076 if (pattern)
3077 {
3078 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3079 skip = alloc_state(NFA_SKIP, NULL, NULL);
3080 zend = alloc_state(NFA_ZEND, s1, NULL);
3081 s1->out= skip;
3082 patch(e.out, zend);
3083 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003084 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003085 else
3086 {
3087 patch(e.out, s1);
3088 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003089 if (before)
3090 {
3091 if (n <= 0)
3092 /* See if we can guess the maximum width, it avoids a
3093 * lot of pointless tries. */
3094 n = nfa_max_width(e.start, 0);
3095 s->val = n; /* store the count */
3096 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003097 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003098 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003099 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003100
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003101#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003102 case NFA_COMPOSING: /* char with composing char */
3103#if 0
3104 /* TODO */
3105 if (regflags & RF_ICOMBINE)
3106 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003107 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003108 }
3109#endif
3110 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003111#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003112
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003113 case NFA_MOPEN: /* \( \) Submatch */
3114 case NFA_MOPEN1:
3115 case NFA_MOPEN2:
3116 case NFA_MOPEN3:
3117 case NFA_MOPEN4:
3118 case NFA_MOPEN5:
3119 case NFA_MOPEN6:
3120 case NFA_MOPEN7:
3121 case NFA_MOPEN8:
3122 case NFA_MOPEN9:
3123#ifdef FEAT_SYN_HL
3124 case NFA_ZOPEN: /* \z( \) Submatch */
3125 case NFA_ZOPEN1:
3126 case NFA_ZOPEN2:
3127 case NFA_ZOPEN3:
3128 case NFA_ZOPEN4:
3129 case NFA_ZOPEN5:
3130 case NFA_ZOPEN6:
3131 case NFA_ZOPEN7:
3132 case NFA_ZOPEN8:
3133 case NFA_ZOPEN9:
3134#endif
3135 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003136 if (nfa_calc_size == TRUE)
3137 {
3138 nstate += 2;
3139 break;
3140 }
3141
3142 mopen = *p;
3143 switch (*p)
3144 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003145 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3146#ifdef FEAT_SYN_HL
3147 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3148 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3149 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3150 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3151 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3152 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3153 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3154 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3155 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3156 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3157#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003158#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003159 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003161 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003162 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003163 mclose = *p + NSUBEXP;
3164 break;
3165 }
3166
3167 /* Allow "NFA_MOPEN" as a valid postfix representation for
3168 * the empty regexp "". In this case, the NFA will be
3169 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3170 * empty groups of parenthesis, and empty mbyte chars */
3171 if (stackp == stack)
3172 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003173 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003174 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003175 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003176 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003178 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 patch(list1(&s->out), s1);
3180 PUSH(frag(s, list1(&s1->out)));
3181 break;
3182 }
3183
3184 /* At least one node was emitted before NFA_MOPEN, so
3185 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3186 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003187 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003188 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003189 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003190
Bram Moolenaar525666f2013-06-02 16:40:55 +02003191 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003192 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003193 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003194 patch(e.out, s1);
3195
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003196#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003197 if (mopen == NFA_COMPOSING)
3198 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003199 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003200#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201
3202 PUSH(frag(s, list1(&s1->out)));
3203 break;
3204
Bram Moolenaar5714b802013-05-28 22:03:20 +02003205 case NFA_BACKREF1:
3206 case NFA_BACKREF2:
3207 case NFA_BACKREF3:
3208 case NFA_BACKREF4:
3209 case NFA_BACKREF5:
3210 case NFA_BACKREF6:
3211 case NFA_BACKREF7:
3212 case NFA_BACKREF8:
3213 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003214#ifdef FEAT_SYN_HL
3215 case NFA_ZREF1:
3216 case NFA_ZREF2:
3217 case NFA_ZREF3:
3218 case NFA_ZREF4:
3219 case NFA_ZREF5:
3220 case NFA_ZREF6:
3221 case NFA_ZREF7:
3222 case NFA_ZREF8:
3223 case NFA_ZREF9:
3224#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003225 if (nfa_calc_size == TRUE)
3226 {
3227 nstate += 2;
3228 break;
3229 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003230 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003231 if (s == NULL)
3232 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003233 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003234 if (s1 == NULL)
3235 goto theend;
3236 patch(list1(&s->out), s1);
3237 PUSH(frag(s, list1(&s1->out)));
3238 break;
3239
Bram Moolenaar423532e2013-05-29 21:14:42 +02003240 case NFA_LNUM:
3241 case NFA_LNUM_GT:
3242 case NFA_LNUM_LT:
3243 case NFA_VCOL:
3244 case NFA_VCOL_GT:
3245 case NFA_VCOL_LT:
3246 case NFA_COL:
3247 case NFA_COL_GT:
3248 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003249 case NFA_MARK:
3250 case NFA_MARK_GT:
3251 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003252 {
3253 int n = *++p; /* lnum, col or mark name */
3254
Bram Moolenaar423532e2013-05-29 21:14:42 +02003255 if (nfa_calc_size == TRUE)
3256 {
3257 nstate += 1;
3258 break;
3259 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003260 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003261 if (s == NULL)
3262 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003263 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003264 PUSH(frag(s, list1(&s->out)));
3265 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003266 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003267
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 case NFA_ZSTART:
3269 case NFA_ZEND:
3270 default:
3271 /* Operands */
3272 if (nfa_calc_size == TRUE)
3273 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003274 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 break;
3276 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003277 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003279 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003280 PUSH(frag(s, list1(&s->out)));
3281 break;
3282
3283 } /* switch(*p) */
3284
3285 } /* for(p = postfix; *p; ++p) */
3286
3287 if (nfa_calc_size == TRUE)
3288 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003289 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003290 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 }
3292
3293 e = POP();
3294 if (stackp != stack)
3295 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3296
3297 if (istate >= nstate)
3298 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 matchstate = &state_ptr[istate++]; /* the match state */
3301 matchstate->c = NFA_MATCH;
3302 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003303 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304
3305 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003306 ret = e.start;
3307
3308theend:
3309 vim_free(stack);
3310 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311
3312#undef POP1
3313#undef PUSH1
3314#undef POP2
3315#undef PUSH2
3316#undef POP
3317#undef PUSH
3318}
3319
3320/****************************************************************
3321 * NFA execution code.
3322 ****************************************************************/
3323
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003324typedef struct
3325{
3326 int in_use; /* number of subexpr with useful info */
3327
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003328 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003329 union
3330 {
3331 struct multipos
3332 {
3333 lpos_T start;
3334 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003335 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003336 struct linepos
3337 {
3338 char_u *start;
3339 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003340 } line[NSUBEXP];
3341 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003342} regsub_T;
3343
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003344typedef struct
3345{
3346 regsub_T norm; /* \( .. \) matches */
3347#ifdef FEAT_SYN_HL
3348 regsub_T synt; /* \z( .. \) matches */
3349#endif
3350} regsubs_T;
3351
Bram Moolenaara2d95102013-06-04 14:23:05 +02003352/* nfa_pim_T stores a Postponed Invisible Match. */
3353typedef struct nfa_pim_S nfa_pim_T;
3354struct nfa_pim_S
3355{
3356 nfa_state_T *state;
3357 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
3358 nfa_pim_T *pim; /* another PIM at the same position */
3359 regsubs_T subs; /* submatch info, only party used */
3360};
3361
3362/* Values for done in nfa_pim_T. */
3363#define NFA_PIM_TODO 0
3364#define NFA_PIM_MATCH 1
3365#define NFA_PIM_NOMATCH -1
3366
3367
Bram Moolenaar963fee22013-05-26 21:47:28 +02003368/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003369typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370{
3371 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003372 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003373 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003374 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003375} nfa_thread_T;
3376
Bram Moolenaar963fee22013-05-26 21:47:28 +02003377/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003378typedef struct
3379{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003380 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003381 int n; /* nr of states currently in "t" */
3382 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003383 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003384} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385
Bram Moolenaar5714b802013-05-28 22:03:20 +02003386#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003387static void log_subsexpr __ARGS((regsubs_T *subs));
3388static void log_subexpr __ARGS((regsub_T *sub));
3389
3390 static void
3391log_subsexpr(subs)
3392 regsubs_T *subs;
3393{
3394 log_subexpr(&subs->norm);
3395# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003396 if (nfa_has_zsubexpr)
3397 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003398# endif
3399}
3400
Bram Moolenaar5714b802013-05-28 22:03:20 +02003401 static void
3402log_subexpr(sub)
3403 regsub_T *sub;
3404{
3405 int j;
3406
3407 for (j = 0; j < sub->in_use; j++)
3408 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003409 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003410 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003411 sub->list.multi[j].start.col,
3412 (int)sub->list.multi[j].start.lnum,
3413 sub->list.multi[j].end.col,
3414 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003415 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003416 {
3417 char *s = (char *)sub->list.line[j].start;
3418 char *e = (char *)sub->list.line[j].end;
3419
Bram Moolenaar87953742013-06-05 18:52:40 +02003420 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003421 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003422 s == NULL ? "NULL" : s,
3423 e == NULL ? "NULL" : e);
3424 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003425}
3426#endif
3427
Bram Moolenaar963fee22013-05-26 21:47:28 +02003428/* Used during execution: whether a match has been found. */
3429static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003430
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003431static void clear_sub __ARGS((regsub_T *sub));
3432static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3433static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003434static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003435static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
3436static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003437static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003438static 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 +02003439
3440 static void
3441clear_sub(sub)
3442 regsub_T *sub;
3443{
3444 if (REG_MULTI)
3445 /* Use 0xff to set lnum to -1 */
3446 vim_memset(sub->list.multi, 0xff,
3447 sizeof(struct multipos) * nfa_nsubexpr);
3448 else
3449 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3450 sub->in_use = 0;
3451}
3452
3453/*
3454 * Copy the submatches from "from" to "to".
3455 */
3456 static void
3457copy_sub(to, from)
3458 regsub_T *to;
3459 regsub_T *from;
3460{
3461 to->in_use = from->in_use;
3462 if (from->in_use > 0)
3463 {
3464 /* Copy the match start and end positions. */
3465 if (REG_MULTI)
3466 mch_memmove(&to->list.multi[0],
3467 &from->list.multi[0],
3468 sizeof(struct multipos) * from->in_use);
3469 else
3470 mch_memmove(&to->list.line[0],
3471 &from->list.line[0],
3472 sizeof(struct linepos) * from->in_use);
3473 }
3474}
3475
3476/*
3477 * Like copy_sub() but exclude the main match.
3478 */
3479 static void
3480copy_sub_off(to, from)
3481 regsub_T *to;
3482 regsub_T *from;
3483{
3484 if (to->in_use < from->in_use)
3485 to->in_use = from->in_use;
3486 if (from->in_use > 1)
3487 {
3488 /* Copy the match start and end positions. */
3489 if (REG_MULTI)
3490 mch_memmove(&to->list.multi[1],
3491 &from->list.multi[1],
3492 sizeof(struct multipos) * (from->in_use - 1));
3493 else
3494 mch_memmove(&to->list.line[1],
3495 &from->list.line[1],
3496 sizeof(struct linepos) * (from->in_use - 1));
3497 }
3498}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499
Bram Moolenaar428e9872013-05-30 17:05:39 +02003500/*
3501 * Return TRUE if "sub1" and "sub2" have the same positions.
3502 */
3503 static int
3504sub_equal(sub1, sub2)
3505 regsub_T *sub1;
3506 regsub_T *sub2;
3507{
3508 int i;
3509 int todo;
3510 linenr_T s1, e1;
3511 linenr_T s2, e2;
3512 char_u *sp1, *ep1;
3513 char_u *sp2, *ep2;
3514
3515 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3516 if (REG_MULTI)
3517 {
3518 for (i = 0; i < todo; ++i)
3519 {
3520 if (i < sub1->in_use)
3521 {
3522 s1 = sub1->list.multi[i].start.lnum;
3523 e1 = sub1->list.multi[i].end.lnum;
3524 }
3525 else
3526 {
3527 s1 = 0;
3528 e1 = 0;
3529 }
3530 if (i < sub2->in_use)
3531 {
3532 s2 = sub2->list.multi[i].start.lnum;
3533 e2 = sub2->list.multi[i].end.lnum;
3534 }
3535 else
3536 {
3537 s2 = 0;
3538 e2 = 0;
3539 }
3540 if (s1 != s2 || e1 != e2)
3541 return FALSE;
3542 if (s1 != 0 && sub1->list.multi[i].start.col
3543 != sub2->list.multi[i].start.col)
3544 return FALSE;
3545 if (e1 != 0 && sub1->list.multi[i].end.col
3546 != sub2->list.multi[i].end.col)
3547 return FALSE;
3548 }
3549 }
3550 else
3551 {
3552 for (i = 0; i < todo; ++i)
3553 {
3554 if (i < sub1->in_use)
3555 {
3556 sp1 = sub1->list.line[i].start;
3557 ep1 = sub1->list.line[i].end;
3558 }
3559 else
3560 {
3561 sp1 = NULL;
3562 ep1 = NULL;
3563 }
3564 if (i < sub2->in_use)
3565 {
3566 sp2 = sub2->list.line[i].start;
3567 ep2 = sub2->list.line[i].end;
3568 }
3569 else
3570 {
3571 sp2 = NULL;
3572 ep2 = NULL;
3573 }
3574 if (sp1 != sp2 || ep1 != ep2)
3575 return FALSE;
3576 }
3577 }
3578
3579 return TRUE;
3580}
3581
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003582#ifdef ENABLE_LOG
3583 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003584report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003585{
3586 int col;
3587
3588 if (sub->in_use <= 0)
3589 col = -1;
3590 else if (REG_MULTI)
3591 col = sub->list.multi[0].start.col;
3592 else
3593 col = (int)(sub->list.line[0].start - regline);
3594 nfa_set_code(state->c);
3595 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3596 action, abs(state->id), lid, state->c, code, col);
3597}
3598#endif
3599
Bram Moolenaar43e02982013-06-07 17:31:29 +02003600/*
3601 * Return TRUE if the same state is already in list "l" with the same
3602 * positions as "subs".
3603 */
3604 static int
3605has_state_with_pos(l, state, subs)
3606 nfa_list_T *l; /* runtime state list */
3607 nfa_state_T *state; /* state to update */
3608 regsubs_T *subs; /* pointers to subexpressions */
3609{
3610 nfa_thread_T *thread;
3611 int i;
3612
3613 for (i = 0; i < l->n; ++i)
3614 {
3615 thread = &l->t[i];
3616 if (thread->state->id == state->id
3617 && sub_equal(&thread->subs.norm, &subs->norm)
3618#ifdef FEAT_SYN_HL
3619 && (!nfa_has_zsubexpr ||
3620 sub_equal(&thread->subs.synt, &subs->synt))
3621#endif
3622 )
3623 return TRUE;
3624 }
3625 return FALSE;
3626}
3627
3628/*
3629 * Return TRUE if "state" is already in list "l".
3630 */
3631 static int
3632state_in_list(l, state, subs)
3633 nfa_list_T *l; /* runtime state list */
3634 nfa_state_T *state; /* state to update */
3635 regsubs_T *subs; /* pointers to subexpressions */
3636{
3637 if (state->lastlist[nfa_ll_index] == l->id)
3638 {
3639 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3640 return TRUE;
3641 }
3642 return FALSE;
3643}
3644
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003646addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003647 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003649 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003650 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003652 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003653 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003654 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003655 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003656 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003657 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003658 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003659#ifdef ENABLE_LOG
3660 int did_print = FALSE;
3661#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003662
3663 if (l == NULL || state == NULL)
3664 return;
3665
3666 switch (state->c)
3667 {
3668 case NFA_SPLIT:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003670 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 case NFA_NCLOSE:
3672 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003673 case NFA_MCLOSE1:
3674 case NFA_MCLOSE2:
3675 case NFA_MCLOSE3:
3676 case NFA_MCLOSE4:
3677 case NFA_MCLOSE5:
3678 case NFA_MCLOSE6:
3679 case NFA_MCLOSE7:
3680 case NFA_MCLOSE8:
3681 case NFA_MCLOSE9:
3682#ifdef FEAT_SYN_HL
3683 case NFA_ZCLOSE:
3684 case NFA_ZCLOSE1:
3685 case NFA_ZCLOSE2:
3686 case NFA_ZCLOSE3:
3687 case NFA_ZCLOSE4:
3688 case NFA_ZCLOSE5:
3689 case NFA_ZCLOSE6:
3690 case NFA_ZCLOSE7:
3691 case NFA_ZCLOSE8:
3692 case NFA_ZCLOSE9:
3693#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003694 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003695 /* These nodes are not added themselves but their "out" and/or
3696 * "out1" may be added below. */
3697 break;
3698
3699 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003700 case NFA_MOPEN1:
3701 case NFA_MOPEN2:
3702 case NFA_MOPEN3:
3703 case NFA_MOPEN4:
3704 case NFA_MOPEN5:
3705 case NFA_MOPEN6:
3706 case NFA_MOPEN7:
3707 case NFA_MOPEN8:
3708 case NFA_MOPEN9:
3709#ifdef FEAT_SYN_HL
3710 case NFA_ZOPEN:
3711 case NFA_ZOPEN1:
3712 case NFA_ZOPEN2:
3713 case NFA_ZOPEN3:
3714 case NFA_ZOPEN4:
3715 case NFA_ZOPEN5:
3716 case NFA_ZOPEN6:
3717 case NFA_ZOPEN7:
3718 case NFA_ZOPEN8:
3719 case NFA_ZOPEN9:
3720#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003721 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003722 /* These nodes do not need to be added, but we need to bail out
3723 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003724 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003725 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003726 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003727 break;
3728
Bram Moolenaar307aa162013-06-02 16:34:21 +02003729 case NFA_BOL:
3730 case NFA_BOF:
3731 /* "^" won't match past end-of-line, don't bother trying.
3732 * Except when we are going to the next line for a look-behind
3733 * match. */
3734 if (reginput > regline
3735 && (nfa_endp == NULL
3736 || !REG_MULTI
3737 || reglnum == nfa_endp->se_u.pos.lnum))
3738 goto skip_add;
3739 /* FALLTHROUGH */
3740
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003742 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003743 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003744 /* This state is already in the list, don't add it again,
3745 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003746 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003747 {
3748skip_add:
3749#ifdef ENABLE_LOG
3750 nfa_set_code(state->c);
3751 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3752 abs(state->id), l->id, state->c, code);
3753#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003754 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003755 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003756
Bram Moolenaar43e02982013-06-07 17:31:29 +02003757 if (has_state_with_pos(l, state, subs))
3758 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003759 }
3760
Bram Moolenaara2d95102013-06-04 14:23:05 +02003761 /* when there are backreferences or look-behind matches the number
3762 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003763 if (nfa_has_backref && l->n == l->len)
3764 {
3765 int newlen = l->len * 3 / 2 + 50;
3766
3767 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3768 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003769 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003770
3771 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003772 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003773 thread = &l->t[l->n++];
3774 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003775 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003776 copy_sub(&thread->subs.norm, &subs->norm);
3777#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003778 if (nfa_has_zsubexpr)
3779 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003780#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003781#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003782 report_state("Adding", &thread->subs.norm, state, l->id);
3783 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003784#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003785 }
3786
3787#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003788 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003789 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003790#endif
3791 switch (state->c)
3792 {
3793 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003794 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795 break;
3796
3797 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003798 /* order matters here */
3799 addstate(l, state->out, subs, off);
3800 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003801 break;
3802
Bram Moolenaar5714b802013-05-28 22:03:20 +02003803 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 case NFA_NOPEN:
3805 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003806 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807 break;
3808
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003809 case NFA_MOPEN:
3810 case NFA_MOPEN1:
3811 case NFA_MOPEN2:
3812 case NFA_MOPEN3:
3813 case NFA_MOPEN4:
3814 case NFA_MOPEN5:
3815 case NFA_MOPEN6:
3816 case NFA_MOPEN7:
3817 case NFA_MOPEN8:
3818 case NFA_MOPEN9:
3819#ifdef FEAT_SYN_HL
3820 case NFA_ZOPEN:
3821 case NFA_ZOPEN1:
3822 case NFA_ZOPEN2:
3823 case NFA_ZOPEN3:
3824 case NFA_ZOPEN4:
3825 case NFA_ZOPEN5:
3826 case NFA_ZOPEN6:
3827 case NFA_ZOPEN7:
3828 case NFA_ZOPEN8:
3829 case NFA_ZOPEN9:
3830#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003832 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003833 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003834 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003835 sub = &subs->norm;
3836 }
3837#ifdef FEAT_SYN_HL
3838 else if (state->c >= NFA_ZOPEN)
3839 {
3840 subidx = state->c - NFA_ZOPEN;
3841 sub = &subs->synt;
3842 }
3843#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003844 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003845 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003846 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003847 sub = &subs->norm;
3848 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003850 /* Set the position (with "off") in the subexpression. Save and
3851 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003852 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003853 if (REG_MULTI)
3854 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003855 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003856 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003857 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003858 save_in_use = -1;
3859 }
3860 else
3861 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003862 save_in_use = sub->in_use;
3863 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003864 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003865 sub->list.multi[i].start.lnum = -1;
3866 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003867 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003868 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003869 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003870 if (off == -1)
3871 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003872 sub->list.multi[subidx].start.lnum = reglnum + 1;
3873 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003874 }
3875 else
3876 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003877 sub->list.multi[subidx].start.lnum = reglnum;
3878 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003879 (colnr_T)(reginput - regline + off);
3880 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881 }
3882 else
3883 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003884 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003885 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003886 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003887 save_in_use = -1;
3888 }
3889 else
3890 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003891 save_in_use = sub->in_use;
3892 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003893 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003894 sub->list.line[i].start = NULL;
3895 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003896 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003897 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003898 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003899 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900 }
3901
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003902 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003903
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003904 if (save_in_use == -1)
3905 {
3906 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003907 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003908 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003909 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003910 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003911 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003912 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003913 break;
3914
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003915 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003916 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003918 /* Do not overwrite the position set by \ze. If no \ze
3919 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003920 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003921 break;
3922 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003923 case NFA_MCLOSE1:
3924 case NFA_MCLOSE2:
3925 case NFA_MCLOSE3:
3926 case NFA_MCLOSE4:
3927 case NFA_MCLOSE5:
3928 case NFA_MCLOSE6:
3929 case NFA_MCLOSE7:
3930 case NFA_MCLOSE8:
3931 case NFA_MCLOSE9:
3932#ifdef FEAT_SYN_HL
3933 case NFA_ZCLOSE:
3934 case NFA_ZCLOSE1:
3935 case NFA_ZCLOSE2:
3936 case NFA_ZCLOSE3:
3937 case NFA_ZCLOSE4:
3938 case NFA_ZCLOSE5:
3939 case NFA_ZCLOSE6:
3940 case NFA_ZCLOSE7:
3941 case NFA_ZCLOSE8:
3942 case NFA_ZCLOSE9:
3943#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003944 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003945 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003946 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003947 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003948 sub = &subs->norm;
3949 }
3950#ifdef FEAT_SYN_HL
3951 else if (state->c >= NFA_ZCLOSE)
3952 {
3953 subidx = state->c - NFA_ZCLOSE;
3954 sub = &subs->synt;
3955 }
3956#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003957 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003958 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003959 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003960 sub = &subs->norm;
3961 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003962
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003963 /* We don't fill in gaps here, there must have been an MOPEN that
3964 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003965 save_in_use = sub->in_use;
3966 if (sub->in_use <= subidx)
3967 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003968 if (REG_MULTI)
3969 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003970 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003971 if (off == -1)
3972 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003973 sub->list.multi[subidx].end.lnum = reglnum + 1;
3974 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003975 }
3976 else
3977 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003978 sub->list.multi[subidx].end.lnum = reglnum;
3979 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003980 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003981 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003982 }
3983 else
3984 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003985 save_ptr = sub->list.line[subidx].end;
3986 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003987 }
3988
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003989 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003990
3991 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003992 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003993 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003994 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003995 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003996 break;
3997 }
3998}
3999
4000/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004001 * Like addstate(), but the new state(s) are put at position "*ip".
4002 * Used for zero-width matches, next state to use is the added one.
4003 * This makes sure the order of states to be tried does not change, which
4004 * matters for alternatives.
4005 */
4006 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004007addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004008 nfa_list_T *l; /* runtime state list */
4009 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004010 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004011 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004012 int *ip;
4013{
4014 int tlen = l->n;
4015 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004016 int listidx = *ip;
4017 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004018
4019 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004020 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02004021
Bram Moolenaara2d95102013-06-04 14:23:05 +02004022 /* fill in the "pim" field in the new states */
4023 if (pim != NULL)
4024 for (i = tlen; i < l->n; ++i)
4025 l->t[i].pim = pim;
4026
Bram Moolenaar4b417062013-05-25 20:19:50 +02004027 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004028 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004029 return;
4030
4031 /* re-order to put the new state at the current position */
4032 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 if (count == 1)
4034 {
4035 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004036 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 }
4038 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004039 {
4040 /* make space for new states, then move them from the
4041 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004042 mch_memmove(&(l->t[listidx + count]),
4043 &(l->t[listidx + 1]),
4044 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4045 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004046 &(l->t[l->n - 1]),
4047 sizeof(nfa_thread_T) * count);
4048 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004049 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004050 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004051}
4052
4053/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004054 * Check character class "class" against current character c.
4055 */
4056 static int
4057check_char_class(class, c)
4058 int class;
4059 int c;
4060{
4061 switch (class)
4062 {
4063 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004064 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004065 return OK;
4066 break;
4067 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004068 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004069 return OK;
4070 break;
4071 case NFA_CLASS_BLANK:
4072 if (c == ' ' || c == '\t')
4073 return OK;
4074 break;
4075 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004076 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004077 return OK;
4078 break;
4079 case NFA_CLASS_DIGIT:
4080 if (VIM_ISDIGIT(c))
4081 return OK;
4082 break;
4083 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004084 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004085 return OK;
4086 break;
4087 case NFA_CLASS_LOWER:
4088 if (MB_ISLOWER(c))
4089 return OK;
4090 break;
4091 case NFA_CLASS_PRINT:
4092 if (vim_isprintc(c))
4093 return OK;
4094 break;
4095 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004096 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004097 return OK;
4098 break;
4099 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004100 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004101 return OK;
4102 break;
4103 case NFA_CLASS_UPPER:
4104 if (MB_ISUPPER(c))
4105 return OK;
4106 break;
4107 case NFA_CLASS_XDIGIT:
4108 if (vim_isxdigit(c))
4109 return OK;
4110 break;
4111 case NFA_CLASS_TAB:
4112 if (c == '\t')
4113 return OK;
4114 break;
4115 case NFA_CLASS_RETURN:
4116 if (c == '\r')
4117 return OK;
4118 break;
4119 case NFA_CLASS_BACKSPACE:
4120 if (c == '\b')
4121 return OK;
4122 break;
4123 case NFA_CLASS_ESCAPE:
4124 if (c == '\033')
4125 return OK;
4126 break;
4127
4128 default:
4129 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004130 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4131 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004132 }
4133 return FAIL;
4134}
4135
Bram Moolenaar5714b802013-05-28 22:03:20 +02004136static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4137
4138/*
4139 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004140 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004141 */
4142 static int
4143match_backref(sub, subidx, bytelen)
4144 regsub_T *sub; /* pointers to subexpressions */
4145 int subidx;
4146 int *bytelen; /* out: length of match in bytes */
4147{
4148 int len;
4149
4150 if (sub->in_use <= subidx)
4151 {
4152retempty:
4153 /* backref was not set, match an empty string */
4154 *bytelen = 0;
4155 return TRUE;
4156 }
4157
4158 if (REG_MULTI)
4159 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004160 if (sub->list.multi[subidx].start.lnum < 0
4161 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004162 goto retempty;
4163 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004164 len = sub->list.multi[subidx].end.col
4165 - sub->list.multi[subidx].start.col;
4166 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004167 reginput, &len) == 0)
4168 {
4169 *bytelen = len;
4170 return TRUE;
4171 }
4172 }
4173 else
4174 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004175 if (sub->list.line[subidx].start == NULL
4176 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004177 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004178 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4179 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004180 {
4181 *bytelen = len;
4182 return TRUE;
4183 }
4184 }
4185 return FALSE;
4186}
4187
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004188#ifdef FEAT_SYN_HL
4189
4190static int match_zref __ARGS((int subidx, int *bytelen));
4191
4192/*
4193 * Check for a match with \z subexpression "subidx".
4194 * Return TRUE if it matches.
4195 */
4196 static int
4197match_zref(subidx, bytelen)
4198 int subidx;
4199 int *bytelen; /* out: length of match in bytes */
4200{
4201 int len;
4202
4203 cleanup_zsubexpr();
4204 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4205 {
4206 /* backref was not set, match an empty string */
4207 *bytelen = 0;
4208 return TRUE;
4209 }
4210
4211 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4212 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4213 {
4214 *bytelen = len;
4215 return TRUE;
4216 }
4217 return FALSE;
4218}
4219#endif
4220
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004221/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004222 * Save list IDs for all NFA states of "prog" into "list".
4223 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004224 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004225 */
4226 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004227nfa_save_listids(prog, list)
4228 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004229 int *list;
4230{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004231 int i;
4232 nfa_state_T *p;
4233
4234 /* Order in the list is reverse, it's a bit faster that way. */
4235 p = &prog->state[0];
4236 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004237 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004238 list[i] = p->lastlist[1];
4239 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004240 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004241 }
4242}
4243
4244/*
4245 * Restore list IDs from "list" to all NFA states.
4246 */
4247 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004248nfa_restore_listids(prog, list)
4249 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004250 int *list;
4251{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004252 int i;
4253 nfa_state_T *p;
4254
4255 p = &prog->state[0];
4256 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004258 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004259 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260 }
4261}
4262
Bram Moolenaar423532e2013-05-29 21:14:42 +02004263 static int
4264nfa_re_num_cmp(val, op, pos)
4265 long_u val;
4266 int op;
4267 long_u pos;
4268{
4269 if (op == 1) return pos > val;
4270 if (op == 2) return pos < val;
4271 return val == pos;
4272}
4273
Bram Moolenaarf46da702013-06-02 22:37:42 +02004274static 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 +02004275static 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 +02004276
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004277/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004278 * Recursively call nfa_regmatch()
4279 */
4280 static int
4281recursive_regmatch(state, prog, submatch, m, listids)
4282 nfa_state_T *state;
4283 nfa_regprog_T *prog;
4284 regsubs_T *submatch;
4285 regsubs_T *m;
4286 int **listids;
4287{
4288 char_u *save_reginput = reginput;
4289 char_u *save_regline = regline;
4290 int save_reglnum = reglnum;
4291 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004292 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004293 save_se_T *save_nfa_endp = nfa_endp;
4294 save_se_T endpos;
4295 save_se_T *endposp = NULL;
4296 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004297 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004298
Bram Moolenaardecd9542013-06-07 16:31:50 +02004299 if (state->c == NFA_START_INVISIBLE_BEFORE
4300 || state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004301 {
4302 /* The recursive match must end at the current position. */
4303 endposp = &endpos;
4304 if (REG_MULTI)
4305 {
4306 endpos.se_u.pos.col = (int)(reginput - regline);
4307 endpos.se_u.pos.lnum = reglnum;
4308 }
4309 else
4310 endpos.se_u.ptr = reginput;
4311
4312 /* Go back the specified number of bytes, or as far as the
4313 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004314 * not matching "\@<!". This is very ineffecient, limit the number of
4315 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004316 if (state->val <= 0)
4317 {
4318 if (REG_MULTI)
4319 {
4320 regline = reg_getline(--reglnum);
4321 if (regline == NULL)
4322 /* can't go before the first line */
4323 regline = reg_getline(++reglnum);
4324 }
4325 reginput = regline;
4326 }
4327 else
4328 {
4329 if (REG_MULTI && (int)(reginput - regline) < state->val)
4330 {
4331 /* Not enough bytes in this line, go to end of
4332 * previous line. */
4333 regline = reg_getline(--reglnum);
4334 if (regline == NULL)
4335 {
4336 /* can't go before the first line */
4337 regline = reg_getline(++reglnum);
4338 reginput = regline;
4339 }
4340 else
4341 reginput = regline + STRLEN(regline);
4342 }
4343 if ((int)(reginput - regline) >= state->val)
4344 {
4345 reginput -= state->val;
4346#ifdef FEAT_MBYTE
4347 if (has_mbyte)
4348 reginput -= mb_head_off(regline, reginput);
4349#endif
4350 }
4351 else
4352 reginput = regline;
4353 }
4354 }
4355
Bram Moolenaarf46da702013-06-02 22:37:42 +02004356#ifdef ENABLE_LOG
4357 if (log_fd != stderr)
4358 fclose(log_fd);
4359 log_fd = NULL;
4360#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004361 /* Have to clear the lastlist field of the NFA nodes, so that
4362 * nfa_regmatch() and addstate() can run properly after recursion. */
4363 if (nfa_ll_index == 1)
4364 {
4365 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4366 * values and clear them. */
4367 if (*listids == NULL)
4368 {
4369 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4370 if (*listids == NULL)
4371 {
4372 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4373 return 0;
4374 }
4375 }
4376 nfa_save_listids(prog, *listids);
4377 need_restore = TRUE;
4378 /* any value of nfa_listid will do */
4379 }
4380 else
4381 {
4382 /* First recursive nfa_regmatch() call, switch to the second lastlist
4383 * entry. Make sure nfa_listid is different from a previous recursive
4384 * call, because some states may still have this ID. */
4385 ++nfa_ll_index;
4386 if (nfa_listid <= nfa_alt_listid)
4387 nfa_listid = nfa_alt_listid;
4388 }
4389
4390 /* Call nfa_regmatch() to check if the current concat matches at this
4391 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004392 nfa_endp = endposp;
4393 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004394
4395 if (need_restore)
4396 nfa_restore_listids(prog, *listids);
4397 else
4398 {
4399 --nfa_ll_index;
4400 nfa_alt_listid = nfa_listid;
4401 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004402
4403 /* restore position in input text */
4404 reginput = save_reginput;
4405 regline = save_regline;
4406 reglnum = save_reglnum;
4407 nfa_match = save_nfa_match;
4408 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004409 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004410
4411#ifdef ENABLE_LOG
4412 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4413 if (log_fd != NULL)
4414 {
4415 fprintf(log_fd, "****************************\n");
4416 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4417 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4418 fprintf(log_fd, "****************************\n");
4419 }
4420 else
4421 {
4422 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4423 log_fd = stderr;
4424 }
4425#endif
4426
4427 return result;
4428}
4429
Bram Moolenaara2d95102013-06-04 14:23:05 +02004430static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004431static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004432static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004433
4434/*
4435 * Estimate the chance of a match with "state" failing.
4436 * NFA_ANY: 1
4437 * specific character: 99
4438 */
4439 static int
4440failure_chance(state, depth)
4441 nfa_state_T *state;
4442 int depth;
4443{
4444 int c = state->c;
4445 int l, r;
4446
4447 /* detect looping */
4448 if (depth > 4)
4449 return 1;
4450
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004451 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004452 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004453 case NFA_SPLIT:
4454 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4455 /* avoid recursive stuff */
4456 return 1;
4457 /* two alternatives, use the lowest failure chance */
4458 l = failure_chance(state->out, depth + 1);
4459 r = failure_chance(state->out1, depth + 1);
4460 return l < r ? l : r;
4461
4462 case NFA_ANY:
4463 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004464 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004465 case NFA_MATCH:
4466 /* empty match works always */
4467 return 0;
4468
4469 case NFA_BOL:
4470 case NFA_EOL:
4471 case NFA_BOF:
4472 case NFA_EOF:
4473 case NFA_NEWL:
4474 return 99;
4475
4476 case NFA_BOW:
4477 case NFA_EOW:
4478 return 90;
4479
4480 case NFA_MOPEN:
4481 case NFA_MOPEN1:
4482 case NFA_MOPEN2:
4483 case NFA_MOPEN3:
4484 case NFA_MOPEN4:
4485 case NFA_MOPEN5:
4486 case NFA_MOPEN6:
4487 case NFA_MOPEN7:
4488 case NFA_MOPEN8:
4489 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004490#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004491 case NFA_ZOPEN:
4492 case NFA_ZOPEN1:
4493 case NFA_ZOPEN2:
4494 case NFA_ZOPEN3:
4495 case NFA_ZOPEN4:
4496 case NFA_ZOPEN5:
4497 case NFA_ZOPEN6:
4498 case NFA_ZOPEN7:
4499 case NFA_ZOPEN8:
4500 case NFA_ZOPEN9:
4501 case NFA_ZCLOSE:
4502 case NFA_ZCLOSE1:
4503 case NFA_ZCLOSE2:
4504 case NFA_ZCLOSE3:
4505 case NFA_ZCLOSE4:
4506 case NFA_ZCLOSE5:
4507 case NFA_ZCLOSE6:
4508 case NFA_ZCLOSE7:
4509 case NFA_ZCLOSE8:
4510 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004511#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004512 case NFA_NOPEN:
4513 case NFA_MCLOSE:
4514 case NFA_MCLOSE1:
4515 case NFA_MCLOSE2:
4516 case NFA_MCLOSE3:
4517 case NFA_MCLOSE4:
4518 case NFA_MCLOSE5:
4519 case NFA_MCLOSE6:
4520 case NFA_MCLOSE7:
4521 case NFA_MCLOSE8:
4522 case NFA_MCLOSE9:
4523 case NFA_NCLOSE:
4524 return failure_chance(state->out, depth + 1);
4525
4526 case NFA_BACKREF1:
4527 case NFA_BACKREF2:
4528 case NFA_BACKREF3:
4529 case NFA_BACKREF4:
4530 case NFA_BACKREF5:
4531 case NFA_BACKREF6:
4532 case NFA_BACKREF7:
4533 case NFA_BACKREF8:
4534 case NFA_BACKREF9:
4535#ifdef FEAT_SYN_HL
4536 case NFA_ZREF1:
4537 case NFA_ZREF2:
4538 case NFA_ZREF3:
4539 case NFA_ZREF4:
4540 case NFA_ZREF5:
4541 case NFA_ZREF6:
4542 case NFA_ZREF7:
4543 case NFA_ZREF8:
4544 case NFA_ZREF9:
4545#endif
4546 /* backreferences don't match in many places */
4547 return 94;
4548
4549 case NFA_LNUM_GT:
4550 case NFA_LNUM_LT:
4551 case NFA_COL_GT:
4552 case NFA_COL_LT:
4553 case NFA_VCOL_GT:
4554 case NFA_VCOL_LT:
4555 case NFA_MARK_GT:
4556 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004557 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004558 /* before/after positions don't match very often */
4559 return 85;
4560
4561 case NFA_LNUM:
4562 return 90;
4563
4564 case NFA_CURSOR:
4565 case NFA_COL:
4566 case NFA_VCOL:
4567 case NFA_MARK:
4568 /* specific positions rarely match */
4569 return 98;
4570
4571 case NFA_COMPOSING:
4572 return 95;
4573
4574 default:
4575 if (c > 0)
4576 /* character match fails often */
4577 return 95;
4578 }
4579
4580 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004581 return 50;
4582}
4583
Bram Moolenaarf46da702013-06-02 22:37:42 +02004584/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004585 * Skip until the char "c" we know a match must start with.
4586 */
4587 static int
4588skip_to_start(c, colp)
4589 int c;
4590 colnr_T *colp;
4591{
4592 char_u *s;
4593
4594 /* Used often, do some work to avoid call overhead. */
4595 if (!ireg_ic
4596#ifdef FEAT_MBYTE
4597 && !has_mbyte
4598#endif
4599 )
4600 s = vim_strbyte(regline + *colp, c);
4601 else
4602 s = cstrchr(regline + *colp, c);
4603 if (s == NULL)
4604 return FAIL;
4605 *colp = (int)(s - regline);
4606 return OK;
4607}
4608
4609/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004610 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004611 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004612 * Returns zero for no match, 1 for a match.
4613 */
4614 static long
4615find_match_text(startcol, regstart, match_text)
4616 colnr_T startcol;
4617 int regstart;
4618 char_u *match_text;
4619{
4620 colnr_T col = startcol;
4621 int c1, c2;
4622 int len1, len2;
4623 int match;
4624
4625 for (;;)
4626 {
4627 match = TRUE;
4628 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4629 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4630 {
4631 c1 = PTR2CHAR(match_text + len1);
4632 c2 = PTR2CHAR(regline + col + len2);
4633 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4634 {
4635 match = FALSE;
4636 break;
4637 }
4638 len2 += MB_CHAR2LEN(c2);
4639 }
4640 if (match
4641#ifdef FEAT_MBYTE
4642 /* check that no composing char follows */
4643 && !(enc_utf8
4644 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4645#endif
4646 )
4647 {
4648 cleanup_subexpr();
4649 if (REG_MULTI)
4650 {
4651 reg_startpos[0].lnum = reglnum;
4652 reg_startpos[0].col = col;
4653 reg_endpos[0].lnum = reglnum;
4654 reg_endpos[0].col = col + len2;
4655 }
4656 else
4657 {
4658 reg_startp[0] = regline + col;
4659 reg_endp[0] = regline + col + len2;
4660 }
4661 return 1L;
4662 }
4663
4664 /* Try finding regstart after the current match. */
4665 col += MB_CHAR2LEN(regstart); /* skip regstart */
4666 if (skip_to_start(regstart, &col) == FAIL)
4667 break;
4668 }
4669 return 0L;
4670}
4671
4672/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004673 * Main matching routine.
4674 *
4675 * Run NFA to determine whether it matches reginput.
4676 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004677 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004678 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679 * Return TRUE if there is a match, FALSE otherwise.
4680 * Note: Caller must ensure that: start != NULL.
4681 */
4682 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004683nfa_regmatch(prog, start, submatch, m)
4684 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004686 regsubs_T *submatch;
4687 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004689 int result;
4690 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004691 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004692 int go_to_nextline = FALSE;
4693 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004694 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004695 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004696 nfa_list_T *thislist;
4697 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004699 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004700 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004701 int add_count;
4702 int add_off;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004703 garray_T pimlist;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004704 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004705#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004706 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004707
4708 if (debug == NULL)
4709 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004710 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 return FALSE;
4712 }
4713#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004714 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004715 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004716
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004717 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004718 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004719 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004720 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004721 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004722 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004723 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004725
4726#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004727 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004728 if (log_fd != NULL)
4729 {
4730 fprintf(log_fd, "**********************************\n");
4731 nfa_set_code(start->c);
4732 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4733 abs(start->id), code);
4734 fprintf(log_fd, "**********************************\n");
4735 }
4736 else
4737 {
4738 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4739 log_fd = stderr;
4740 }
4741#endif
4742
4743 thislist = &list[0];
4744 thislist->n = 0;
4745 nextlist = &list[1];
4746 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004747#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004748 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004749#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004750 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004751
4752 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4753 * it's the first MOPEN. */
4754 if (toplevel)
4755 {
4756 if (REG_MULTI)
4757 {
4758 m->norm.list.multi[0].start.lnum = reglnum;
4759 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4760 }
4761 else
4762 m->norm.list.line[0].start = reginput;
4763 m->norm.in_use = 1;
4764 addstate(thislist, start->out, m, 0);
4765 }
4766 else
4767 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004768
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004769#define ADD_STATE_IF_MATCH(state) \
4770 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004771 add_state = state->out; \
4772 add_off = clen; \
4773 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004774
4775 /*
4776 * Run for each character.
4777 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004778 for (;;)
4779 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004780 int curc;
4781 int clen;
4782
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004783#ifdef FEAT_MBYTE
4784 if (has_mbyte)
4785 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004786 curc = (*mb_ptr2char)(reginput);
4787 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004788 }
4789 else
4790#endif
4791 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004792 curc = *reginput;
4793 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004794 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004795 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004796 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004797 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004798 go_to_nextline = FALSE;
4799 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004800
4801 /* swap lists */
4802 thislist = &list[flag];
4803 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004804 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004805 ++nfa_listid;
4806 thislist->id = nfa_listid;
4807 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004808
Bram Moolenaara2d95102013-06-04 14:23:05 +02004809 pimlist.ga_len = 0;
4810
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004811#ifdef ENABLE_LOG
4812 fprintf(log_fd, "------------------------------------------\n");
4813 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004814 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004815 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004816 {
4817 int i;
4818
4819 for (i = 0; i < thislist->n; i++)
4820 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4821 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004822 fprintf(log_fd, "\n");
4823#endif
4824
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004825#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826 fprintf(debug, "\n-------------------\n");
4827#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004828 /*
4829 * If the state lists are empty we can stop.
4830 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004831 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004832 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004833
4834 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004835 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004836 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004837 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004838
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004839#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004840 nfa_set_code(t->state->c);
4841 fprintf(debug, "%s, ", code);
4842#endif
4843#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004844 {
4845 int col;
4846
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004847 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004848 col = -1;
4849 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004850 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004851 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004852 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004853 nfa_set_code(t->state->c);
4854 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4855 abs(t->state->id), (int)t->state->c, code, col);
4856 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004857#endif
4858
4859 /*
4860 * Handle the possible codes of the current state.
4861 * The most important is NFA_MATCH.
4862 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004863 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004864 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004865 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 switch (t->state->c)
4867 {
4868 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004869 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004870 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004871 copy_sub(&submatch->norm, &t->subs.norm);
4872#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004873 if (nfa_has_zsubexpr)
4874 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004875#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004877 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004879 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004880 * states at this position. When the list of states is going
4881 * to be empty quit without advancing, so that "reginput" is
4882 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004883 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004884 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004885 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004886 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004887
4888 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004889 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02004890 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004891 /*
4892 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004893 * NFA_START_INVISIBLE_BEFORE node.
4894 * They surround a zero-width group, used with "\@=", "\&",
4895 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004896 * If we got here, it means that the current "invisible" group
4897 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004898 * nfa_regmatch(). For a look-behind match only when it ends
4899 * in the position in "nfa_endp".
4900 * Submatches are stored in *m, and used in the parent call.
4901 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004902#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004903 if (nfa_endp != NULL)
4904 {
4905 if (REG_MULTI)
4906 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4907 (int)reglnum,
4908 (int)nfa_endp->se_u.pos.lnum,
4909 (int)(reginput - regline),
4910 nfa_endp->se_u.pos.col);
4911 else
4912 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4913 (int)(reginput - regline),
4914 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004915 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004916#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004917 /* If "nfa_endp" is set it's only a match if it ends at
4918 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004919 if (nfa_endp != NULL && (REG_MULTI
4920 ? (reglnum != nfa_endp->se_u.pos.lnum
4921 || (int)(reginput - regline)
4922 != nfa_endp->se_u.pos.col)
4923 : reginput != nfa_endp->se_u.ptr))
4924 break;
4925
4926 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02004927 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004928 {
4929 copy_sub(&m->norm, &t->subs.norm);
4930#ifdef FEAT_SYN_HL
4931 if (nfa_has_zsubexpr)
4932 copy_sub(&m->synt, &t->subs.synt);
4933#endif
4934 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004935#ifdef ENABLE_LOG
4936 fprintf(log_fd, "Match found:\n");
4937 log_subsexpr(m);
4938#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004939 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 break;
4941
4942 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004943 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004944 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004945 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004947 nfa_pim_T *pim;
4948 int cout = t->state->out1->out->c;
4949
4950 /* Do it directly when what follows is possibly end of
4951 * match (closing paren).
4952 * Postpone when it is \@<= or \@<!, these are expensive.
4953 * TODO: remove the check for t->pim and check multiple
4954 * where it's used?
4955 * Otherwise first do the one that has the highest chance
4956 * of failing. */
4957 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004958#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004959 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004960#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004961 || t->pim != NULL
4962 || (t->state->c != NFA_START_INVISIBLE_BEFORE
Bram Moolenaardecd9542013-06-07 16:31:50 +02004963 && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Bram Moolenaara2d95102013-06-04 14:23:05 +02004964 && failure_chance(t->state->out1->out, 0)
4965 < failure_chance(t->state->out, 0)))
4966 {
4967 /*
4968 * First try matching the invisible match, then what
4969 * follows.
4970 */
4971 result = recursive_regmatch(t->state, prog,
4972 submatch, m, &listids);
4973
Bram Moolenaardecd9542013-06-07 16:31:50 +02004974 /* for \@! and \@<! it is a match when the result is
4975 * FALSE */
4976 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
4977 || t->state->c
4978 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004979 {
4980 /* Copy submatch info from the recursive call */
4981 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004982#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004983 if (nfa_has_zsubexpr)
4984 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004985#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004986
Bram Moolenaara2d95102013-06-04 14:23:05 +02004987 /* t->state->out1 is the corresponding
4988 * END_INVISIBLE node; Add its out to the current
4989 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004990 add_here = TRUE;
4991 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004992 }
4993 }
4994 else
4995 {
4996 /*
4997 * First try matching what follows at the current
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004998 * position. Only if a match is found, before
4999 * addstate() is called, then verify the invisible
5000 * match matches. Add a nfa_pim_T to the following
5001 * states, it contains info about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005002 */
5003 if (ga_grow(&pimlist, 1) == FAIL)
5004 goto theend;
5005 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
5006 ++pimlist.ga_len;
5007 pim->state = t->state;
5008 pim->pim = NULL;
5009 pim->result = NFA_PIM_TODO;
5010
5011 /* t->state->out1 is the corresponding END_INVISIBLE
5012 * node; Add its out to the current list (zero-width
5013 * match). */
5014 addstate_here(thislist, t->state->out1->out, &t->subs,
5015 pim, &listidx);
5016 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005018 break;
5019
Bram Moolenaar87953742013-06-05 18:52:40 +02005020 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005021 {
5022 nfa_state_T *skip = NULL;
5023#ifdef ENABLE_LOG
5024 int skip_lid = 0;
5025#endif
5026
5027 /* There is no point in trying to match the pattern if the
5028 * output state is not going to be added to the list. */
5029 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5030 {
5031 skip = t->state->out1->out;
5032#ifdef ENABLE_LOG
5033 skip_lid = nextlist->id;
5034#endif
5035 }
5036 else if (state_in_list(nextlist,
5037 t->state->out1->out->out, &t->subs))
5038 {
5039 skip = t->state->out1->out->out;
5040#ifdef ENABLE_LOG
5041 skip_lid = nextlist->id;
5042#endif
5043 }
5044 else if(state_in_list(thislist,
5045 t->state->out1->out->out, &t->subs))
5046 {
5047 skip = t->state->out1->out->out;
5048#ifdef ENABLE_LOG
5049 skip_lid = thislist->id;
5050#endif
5051 }
5052 if (skip != NULL)
5053 {
5054#ifdef ENABLE_LOG
5055 nfa_set_code(skip->c);
5056 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5057 abs(skip->id), skip_lid, skip->c, code);
5058#endif
5059 break;
5060 }
5061
Bram Moolenaar87953742013-06-05 18:52:40 +02005062 /* First try matching the pattern. */
5063 result = recursive_regmatch(t->state, prog,
5064 submatch, m, &listids);
5065 if (result)
5066 {
5067 int bytelen;
5068
5069#ifdef ENABLE_LOG
5070 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5071 log_subsexpr(m);
5072#endif
5073 /* Copy submatch info from the recursive call */
5074 copy_sub_off(&t->subs.norm, &m->norm);
5075#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005076 if (nfa_has_zsubexpr)
5077 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005078#endif
5079 /* Now we need to skip over the matched text and then
5080 * continue with what follows. */
5081 if (REG_MULTI)
5082 /* TODO: multi-line match */
5083 bytelen = m->norm.list.multi[0].end.col
5084 - (int)(reginput - regline);
5085 else
5086 bytelen = (int)(m->norm.list.line[0].end - reginput);
5087
5088#ifdef ENABLE_LOG
5089 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5090#endif
5091 if (bytelen == 0)
5092 {
5093 /* empty match, output of corresponding
5094 * NFA_END_PATTERN/NFA_SKIP to be used at current
5095 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005096 add_here = TRUE;
5097 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005098 }
5099 else if (bytelen <= clen)
5100 {
5101 /* match current character, output of corresponding
5102 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005103 add_state = t->state->out1->out->out;
5104 add_off = clen;
5105 }
5106 else
5107 {
5108 /* skip over the matched characters, set character
5109 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005110 add_state = t->state->out1->out;
5111 add_off = bytelen;
5112 add_count = bytelen - clen;
5113 }
5114 }
5115 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005116 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005117
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005118 case NFA_BOL:
5119 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005120 {
5121 add_here = TRUE;
5122 add_state = t->state->out;
5123 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005124 break;
5125
5126 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005127 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005128 {
5129 add_here = TRUE;
5130 add_state = t->state->out;
5131 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005132 break;
5133
5134 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005135 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005136
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005137 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005138 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005139#ifdef FEAT_MBYTE
5140 else if (has_mbyte)
5141 {
5142 int this_class;
5143
5144 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005145 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005146 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005147 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005148 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005149 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005150 }
5151#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005152 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005153 || (reginput > regline
5154 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005155 result = FALSE;
5156 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005157 {
5158 add_here = TRUE;
5159 add_state = t->state->out;
5160 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005161 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005162
5163 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005164 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005165 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005166 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005167#ifdef FEAT_MBYTE
5168 else if (has_mbyte)
5169 {
5170 int this_class, prev_class;
5171
5172 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005173 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005174 prev_class = reg_prev_class();
5175 if (this_class == prev_class
5176 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005177 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005178 }
5179#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005180 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005181 || (reginput[0] != NUL
5182 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005183 result = FALSE;
5184 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005185 {
5186 add_here = TRUE;
5187 add_state = t->state->out;
5188 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005189 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005190
Bram Moolenaar4b780632013-05-31 22:14:52 +02005191 case NFA_BOF:
5192 if (reglnum == 0 && reginput == regline
5193 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005194 {
5195 add_here = TRUE;
5196 add_state = t->state->out;
5197 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005198 break;
5199
5200 case NFA_EOF:
5201 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005202 {
5203 add_here = TRUE;
5204 add_state = t->state->out;
5205 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005206 break;
5207
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005208#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005209 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005210 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005211 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005212 int len = 0;
5213 nfa_state_T *end;
5214 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005215 int cchars[MAX_MCO];
5216 int ccount = 0;
5217 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005218
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005219 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005220 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005221 if (utf_iscomposing(sta->c))
5222 {
5223 /* Only match composing character(s), ignore base
5224 * character. Used for ".{composing}" and "{composing}"
5225 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005226 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005227 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005228 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005229 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005230 /* If \Z was present, then ignore composing characters.
5231 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005232 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005233 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005234 else
5235 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005236 while (sta->c != NFA_END_COMPOSING)
5237 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005238 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005239
5240 /* Check base character matches first, unless ignored. */
5241 else if (len > 0 || mc == sta->c)
5242 {
5243 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005244 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005245 len += mb_char2len(mc);
5246 sta = sta->out;
5247 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005248
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005249 /* We don't care about the order of composing characters.
5250 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005251 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005252 {
5253 mc = mb_ptr2char(reginput + len);
5254 cchars[ccount++] = mc;
5255 len += mb_char2len(mc);
5256 if (ccount == MAX_MCO)
5257 break;
5258 }
5259
5260 /* Check that each composing char in the pattern matches a
5261 * composing char in the text. We do not check if all
5262 * composing chars are matched. */
5263 result = OK;
5264 while (sta->c != NFA_END_COMPOSING)
5265 {
5266 for (j = 0; j < ccount; ++j)
5267 if (cchars[j] == sta->c)
5268 break;
5269 if (j == ccount)
5270 {
5271 result = FAIL;
5272 break;
5273 }
5274 sta = sta->out;
5275 }
5276 }
5277 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005278 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005279
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005280 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005281 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005282 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005283 }
5284#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005285
5286 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005287 if (curc == NUL && !reg_line_lbr && REG_MULTI
5288 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005289 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005290 go_to_nextline = TRUE;
5291 /* Pass -1 for the offset, which means taking the position
5292 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005293 add_state = t->state->out;
5294 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005295 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005296 else if (curc == '\n' && reg_line_lbr)
5297 {
5298 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005299 add_state = t->state->out;
5300 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005301 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005302 break;
5303
Bram Moolenaar417bad22013-06-07 14:08:30 +02005304 case NFA_START_COLL:
5305 case NFA_START_NEG_COLL:
5306 {
5307 /* What follows is a list of characters, until NFA_END_COLL.
5308 * One of them must match or none of them must match. */
5309 nfa_state_T *state;
5310 int result_if_matched;
5311 int c1, c2;
5312
5313 /* Never match EOL. If it's part of the collection it is added
5314 * as a separate state with an OR. */
5315 if (curc == NUL)
5316 break;
5317
5318 state = t->state->out;
5319 result_if_matched = (t->state->c == NFA_START_COLL);
5320 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005321 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005322 if (state->c == NFA_END_COLL)
5323 {
5324 result = !result_if_matched;
5325 break;
5326 }
5327 if (state->c == NFA_RANGE_MIN)
5328 {
5329 c1 = state->val;
5330 state = state->out; /* advance to NFA_RANGE_MAX */
5331 c2 = state->val;
5332#ifdef ENABLE_LOG
5333 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5334 curc, c1, c2);
5335#endif
5336 if (curc >= c1 && curc <= c2)
5337 {
5338 result = result_if_matched;
5339 break;
5340 }
5341 if (ireg_ic)
5342 {
5343 int curc_low = MB_TOLOWER(curc);
5344 int done = FALSE;
5345
5346 for ( ; c1 <= c2; ++c1)
5347 if (MB_TOLOWER(c1) == curc_low)
5348 {
5349 result = result_if_matched;
5350 done = TRUE;
5351 break;
5352 }
5353 if (done)
5354 break;
5355 }
5356 }
5357 else if (state->c < 0 ? check_char_class(state->c, curc)
5358 : (curc == state->c
5359 || (ireg_ic && MB_TOLOWER(curc)
5360 == MB_TOLOWER(state->c))))
5361 {
5362 result = result_if_matched;
5363 break;
5364 }
5365 state = state->out;
5366 }
5367 if (result)
5368 {
5369 /* next state is in out of the NFA_END_COLL, out1 of
5370 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005371 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005372 add_off = clen;
5373 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005374 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005375 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005376
5377 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005378 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005379 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005380 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005381 add_state = t->state->out;
5382 add_off = clen;
5383 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005384 break;
5385
5386 /*
5387 * Character classes like \a for alpha, \d for digit etc.
5388 */
5389 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005390 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005391 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005392 break;
5393
5394 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005395 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005396 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005397 break;
5398
5399 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005400 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005401 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005402 break;
5403
5404 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005405 result = !VIM_ISDIGIT(curc)
5406 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005407 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005408 break;
5409
5410 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005411 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005412 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005413 break;
5414
5415 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005416 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005417 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005418 break;
5419
5420 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005421 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005422 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005423 break;
5424
5425 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005426 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005427 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005428 break;
5429
5430 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005431 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005432 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005433 break;
5434
5435 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005436 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005437 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005438 break;
5439
5440 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005441 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005442 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005443 break;
5444
5445 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005446 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005447 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005448 break;
5449
5450 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005451 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005452 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005453 break;
5454
5455 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005456 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005457 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005458 break;
5459
5460 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005461 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005462 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005463 break;
5464
5465 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005466 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005467 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005468 break;
5469
5470 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005471 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005472 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005473 break;
5474
5475 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005476 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005477 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005478 break;
5479
5480 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005481 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005482 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005483 break;
5484
5485 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005486 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005487 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005488 break;
5489
5490 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005491 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005492 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005493 break;
5494
5495 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005496 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005497 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005498 break;
5499
5500 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005501 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005502 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503 break;
5504
5505 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005506 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005507 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005508 break;
5509
5510 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005511 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005512 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513 break;
5514
5515 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005516 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005517 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518 break;
5519
Bram Moolenaar5714b802013-05-28 22:03:20 +02005520 case NFA_BACKREF1:
5521 case NFA_BACKREF2:
5522 case NFA_BACKREF3:
5523 case NFA_BACKREF4:
5524 case NFA_BACKREF5:
5525 case NFA_BACKREF6:
5526 case NFA_BACKREF7:
5527 case NFA_BACKREF8:
5528 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005529#ifdef FEAT_SYN_HL
5530 case NFA_ZREF1:
5531 case NFA_ZREF2:
5532 case NFA_ZREF3:
5533 case NFA_ZREF4:
5534 case NFA_ZREF5:
5535 case NFA_ZREF6:
5536 case NFA_ZREF7:
5537 case NFA_ZREF8:
5538 case NFA_ZREF9:
5539#endif
5540 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005541 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005542 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005543 int bytelen;
5544
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005545 if (t->state->c <= NFA_BACKREF9)
5546 {
5547 subidx = t->state->c - NFA_BACKREF1 + 1;
5548 result = match_backref(&t->subs.norm, subidx, &bytelen);
5549 }
5550#ifdef FEAT_SYN_HL
5551 else
5552 {
5553 subidx = t->state->c - NFA_ZREF1 + 1;
5554 result = match_zref(subidx, &bytelen);
5555 }
5556#endif
5557
Bram Moolenaar5714b802013-05-28 22:03:20 +02005558 if (result)
5559 {
5560 if (bytelen == 0)
5561 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005562 /* empty match always works, output of NFA_SKIP to be
5563 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005564 add_here = TRUE;
5565 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005566 }
5567 else if (bytelen <= clen)
5568 {
5569 /* match current character, jump ahead to out of
5570 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005571 add_state = t->state->out->out;
5572 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005573 }
5574 else
5575 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005576 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005577 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005578 add_state = t->state->out;
5579 add_off = bytelen;
5580 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005581 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005582 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005583 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005584 }
5585 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005586 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005587 if (t->count - clen <= 0)
5588 {
5589 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005590 add_state = t->state->out;
5591 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005592 }
5593 else
5594 {
5595 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005596 add_state = t->state;
5597 add_off = 0;
5598 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005599 }
5600 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005601
Bram Moolenaar423532e2013-05-29 21:14:42 +02005602 case NFA_LNUM:
5603 case NFA_LNUM_GT:
5604 case NFA_LNUM_LT:
5605 result = (REG_MULTI &&
5606 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5607 (long_u)(reglnum + reg_firstlnum)));
5608 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005609 {
5610 add_here = TRUE;
5611 add_state = t->state->out;
5612 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005613 break;
5614
5615 case NFA_COL:
5616 case NFA_COL_GT:
5617 case NFA_COL_LT:
5618 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5619 (long_u)(reginput - regline) + 1);
5620 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005621 {
5622 add_here = TRUE;
5623 add_state = t->state->out;
5624 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005625 break;
5626
5627 case NFA_VCOL:
5628 case NFA_VCOL_GT:
5629 case NFA_VCOL_LT:
5630 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5631 (long_u)win_linetabsize(
5632 reg_win == NULL ? curwin : reg_win,
5633 regline, (colnr_T)(reginput - regline)) + 1);
5634 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005635 {
5636 add_here = TRUE;
5637 add_state = t->state->out;
5638 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005639 break;
5640
Bram Moolenaar044aa292013-06-04 21:27:38 +02005641 case NFA_MARK:
5642 case NFA_MARK_GT:
5643 case NFA_MARK_LT:
5644 {
5645 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5646
5647 /* Compare the mark position to the match position. */
5648 result = (pos != NULL /* mark doesn't exist */
5649 && pos->lnum > 0 /* mark isn't set in reg_buf */
5650 && (pos->lnum == reglnum + reg_firstlnum
5651 ? (pos->col == (colnr_T)(reginput - regline)
5652 ? t->state->c == NFA_MARK
5653 : (pos->col < (colnr_T)(reginput - regline)
5654 ? t->state->c == NFA_MARK_GT
5655 : t->state->c == NFA_MARK_LT))
5656 : (pos->lnum < reglnum + reg_firstlnum
5657 ? t->state->c == NFA_MARK_GT
5658 : t->state->c == NFA_MARK_LT)));
5659 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005660 {
5661 add_here = TRUE;
5662 add_state = t->state->out;
5663 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005664 break;
5665 }
5666
Bram Moolenaar423532e2013-05-29 21:14:42 +02005667 case NFA_CURSOR:
5668 result = (reg_win != NULL
5669 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5670 && ((colnr_T)(reginput - regline)
5671 == reg_win->w_cursor.col));
5672 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005673 {
5674 add_here = TRUE;
5675 add_state = t->state->out;
5676 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005677 break;
5678
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005679 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005680#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005681 result = reg_match_visual();
5682 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005683 {
5684 add_here = TRUE;
5685 add_state = t->state->out;
5686 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005687#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005688 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005689
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005690 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005691 {
5692 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005693
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005694 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005695 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005696 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005697 result = (c == curc);
5698
5699 if (!result && ireg_ic)
5700 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005701#ifdef FEAT_MBYTE
5702 /* If there is a composing character which is not being
5703 * ignored there can be no match. Match with composing
5704 * character uses NFA_COMPOSING above. */
5705 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005706 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005707 result = FALSE;
5708#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005709 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005711 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005712
5713 } /* switch (t->state->c) */
5714
5715 if (add_state != NULL)
5716 {
5717 if (t->pim != NULL)
5718 {
5719 /* postponed invisible match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005720 if (t->pim->result == NFA_PIM_TODO)
5721 {
5722#ifdef ENABLE_LOG
5723 fprintf(log_fd, "\n");
5724 fprintf(log_fd, "==================================\n");
5725 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5726 fprintf(log_fd, "\n");
5727#endif
5728 result = recursive_regmatch(t->pim->state,
5729 prog, submatch, m, &listids);
5730 t->pim->result = result ? NFA_PIM_MATCH
5731 : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005732 /* for \@! and \@<! it is a match when the result is
5733 * FALSE */
5734 if (result != (t->pim->state->c
5735 == NFA_START_INVISIBLE_NEG
5736 || t->pim->state->c
5737 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005738 {
5739 /* Copy submatch info from the recursive call */
5740 copy_sub_off(&t->pim->subs.norm, &m->norm);
5741#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005742 if (nfa_has_zsubexpr)
5743 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005744#endif
5745 }
5746 }
5747 else
5748 {
5749 result = (t->pim->result == NFA_PIM_MATCH);
5750#ifdef ENABLE_LOG
5751 fprintf(log_fd, "\n");
5752 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5753 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5754 fprintf(log_fd, "\n");
5755#endif
5756 }
5757
Bram Moolenaardecd9542013-06-07 16:31:50 +02005758 /* for \@! and \@<! it is a match when result is FALSE */
5759 if (result != (t->pim->state->c == NFA_START_INVISIBLE_NEG
5760 || t->pim->state->c
5761 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005762 {
5763 /* Copy submatch info from the recursive call */
5764 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5765#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005766 if (nfa_has_zsubexpr)
5767 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005768#endif
5769 }
5770 else
5771 /* look-behind match failed, don't add the state */
5772 continue;
5773 }
5774
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005775 if (add_here)
5776 addstate_here(thislist, add_state, &t->subs, NULL, &listidx);
5777 else
5778 {
5779 addstate(nextlist, add_state, &t->subs, add_off);
5780 if (add_count > 0)
5781 nextlist->t[nextlist->n - 1].count = add_count;
5782 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005783 }
5784
5785 } /* for (thislist = thislist; thislist->state; thislist++) */
5786
Bram Moolenaare23febd2013-05-26 18:40:14 +02005787 /* Look for the start of a match in the current position by adding the
5788 * start state to the list of states.
5789 * The first found match is the leftmost one, thus the order of states
5790 * matters!
5791 * Do not add the start state in recursive calls of nfa_regmatch(),
5792 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005793 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005794 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005795 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005796 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02005797 && reglnum == 0
5798 && clen != 0
5799 && (ireg_maxcol == 0
5800 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005801 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005802 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005803 ? (reglnum < nfa_endp->se_u.pos.lnum
5804 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005805 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005806 < nfa_endp->se_u.pos.col))
5807 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005808 {
5809#ifdef ENABLE_LOG
5810 fprintf(log_fd, "(---) STARTSTATE\n");
5811#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005812 /* Inline optimized code for addstate() if we know the state is
5813 * the first MOPEN. */
5814 if (toplevel)
5815 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005816 int add = TRUE;
5817 int c;
5818
5819 if (prog->regstart != NUL && clen != 0)
5820 {
5821 if (nextlist->n == 0)
5822 {
5823 colnr_T col = (colnr_T)(reginput - regline) + clen;
5824
5825 /* Nextlist is empty, we can skip ahead to the
5826 * character that must appear at the start. */
5827 if (skip_to_start(prog->regstart, &col) == FAIL)
5828 break;
5829#ifdef ENABLE_LOG
5830 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
5831 col - ((colnr_T)(reginput - regline) + clen));
5832#endif
5833 reginput = regline + col - clen;
5834 }
5835 else
5836 {
5837 /* Checking if the required start character matches is
5838 * cheaper than adding a state that won't match. */
5839 c = PTR2CHAR(reginput + clen);
5840 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
5841 != MB_TOLOWER(prog->regstart)))
5842 {
5843#ifdef ENABLE_LOG
5844 fprintf(log_fd, " Skipping start state, regstart does not match\n");
5845#endif
5846 add = FALSE;
5847 }
5848 }
5849 }
5850
5851 if (add)
5852 {
5853 if (REG_MULTI)
5854 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005855 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005856 else
5857 m->norm.list.line[0].start = reginput + clen;
5858 addstate(nextlist, start->out, m, clen);
5859 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005860 }
5861 else
5862 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005863 }
5864
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005865#ifdef ENABLE_LOG
5866 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005867 {
5868 int i;
5869
5870 for (i = 0; i < thislist->n; i++)
5871 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5872 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005873 fprintf(log_fd, "\n");
5874#endif
5875
5876nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005877 /* Advance to the next character, or advance to the next line, or
5878 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005879 if (clen != 0)
5880 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005881 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5882 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005883 reg_nextline();
5884 else
5885 break;
5886 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005887
5888#ifdef ENABLE_LOG
5889 if (log_fd != stderr)
5890 fclose(log_fd);
5891 log_fd = NULL;
5892#endif
5893
5894theend:
5895 /* Free memory */
5896 vim_free(list[0].t);
5897 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005898 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005899 ga_clear(&pimlist);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005900#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005901#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005902 fclose(debug);
5903#endif
5904
Bram Moolenaar963fee22013-05-26 21:47:28 +02005905 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005906}
5907
5908/*
5909 * Try match of "prog" with at regline["col"].
5910 * Returns 0 for failure, number of lines contained in the match otherwise.
5911 */
5912 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005913nfa_regtry(prog, col)
5914 nfa_regprog_T *prog;
5915 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005916{
5917 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005918 regsubs_T subs, m;
5919 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005920#ifdef ENABLE_LOG
5921 FILE *f;
5922#endif
5923
5924 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005925
5926#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005927 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005928 if (f != NULL)
5929 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005930 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005931#ifdef DEBUG
5932 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5933#endif
5934 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005935 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005936 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005937 fprintf(f, "\n\n");
5938 fclose(f);
5939 }
5940 else
5941 EMSG(_("Could not open temporary log file for writing "));
5942#endif
5943
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005944 clear_sub(&subs.norm);
5945 clear_sub(&m.norm);
5946#ifdef FEAT_SYN_HL
5947 clear_sub(&subs.synt);
5948 clear_sub(&m.synt);
5949#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005950
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005951 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005952 return 0;
5953
5954 cleanup_subexpr();
5955 if (REG_MULTI)
5956 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005957 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005958 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005959 reg_startpos[i] = subs.norm.list.multi[i].start;
5960 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005961 }
5962
5963 if (reg_startpos[0].lnum < 0)
5964 {
5965 reg_startpos[0].lnum = 0;
5966 reg_startpos[0].col = col;
5967 }
5968 if (reg_endpos[0].lnum < 0)
5969 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005970 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005971 reg_endpos[0].lnum = reglnum;
5972 reg_endpos[0].col = (int)(reginput - regline);
5973 }
5974 else
5975 /* Use line number of "\ze". */
5976 reglnum = reg_endpos[0].lnum;
5977 }
5978 else
5979 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005980 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005981 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005982 reg_startp[i] = subs.norm.list.line[i].start;
5983 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005984 }
5985
5986 if (reg_startp[0] == NULL)
5987 reg_startp[0] = regline + col;
5988 if (reg_endp[0] == NULL)
5989 reg_endp[0] = reginput;
5990 }
5991
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005992#ifdef FEAT_SYN_HL
5993 /* Package any found \z(...\) matches for export. Default is none. */
5994 unref_extmatch(re_extmatch_out);
5995 re_extmatch_out = NULL;
5996
5997 if (prog->reghasz == REX_SET)
5998 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005999 cleanup_zsubexpr();
6000 re_extmatch_out = make_extmatch();
6001 for (i = 0; i < subs.synt.in_use; i++)
6002 {
6003 if (REG_MULTI)
6004 {
6005 struct multipos *mpos = &subs.synt.list.multi[i];
6006
6007 /* Only accept single line matches. */
6008 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6009 re_extmatch_out->matches[i] =
6010 vim_strnsave(reg_getline(mpos->start.lnum)
6011 + mpos->start.col,
6012 mpos->end.col - mpos->start.col);
6013 }
6014 else
6015 {
6016 struct linepos *lpos = &subs.synt.list.line[i];
6017
6018 if (lpos->start != NULL && lpos->end != NULL)
6019 re_extmatch_out->matches[i] =
6020 vim_strnsave(lpos->start,
6021 (int)(lpos->end - lpos->start));
6022 }
6023 }
6024 }
6025#endif
6026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006027 return 1 + reglnum;
6028}
6029
6030/*
6031 * Match a regexp against a string ("line" points to the string) or multiple
6032 * lines ("line" is NULL, use reg_getline()).
6033 *
6034 * Returns 0 for failure, number of lines contained in the match otherwise.
6035 */
6036 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006037nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006038 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006039 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006040{
6041 nfa_regprog_T *prog;
6042 long retval = 0L;
6043 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006044 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006045
6046 if (REG_MULTI)
6047 {
6048 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6049 line = reg_getline((linenr_T)0); /* relative to the cursor */
6050 reg_startpos = reg_mmatch->startpos;
6051 reg_endpos = reg_mmatch->endpos;
6052 }
6053 else
6054 {
6055 prog = (nfa_regprog_T *)reg_match->regprog;
6056 reg_startp = reg_match->startp;
6057 reg_endp = reg_match->endp;
6058 }
6059
6060 /* Be paranoid... */
6061 if (prog == NULL || line == NULL)
6062 {
6063 EMSG(_(e_null));
6064 goto theend;
6065 }
6066
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6068 if (prog->regflags & RF_ICASE)
6069 ireg_ic = TRUE;
6070 else if (prog->regflags & RF_NOICASE)
6071 ireg_ic = FALSE;
6072
6073#ifdef FEAT_MBYTE
6074 /* If pattern contains "\Z" overrule value of ireg_icombine */
6075 if (prog->regflags & RF_ICOMBINE)
6076 ireg_icombine = TRUE;
6077#endif
6078
6079 regline = line;
6080 reglnum = 0; /* relative to line */
6081
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006082 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006083 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006084 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006085 nfa_listid = 1;
6086 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006087#ifdef DEBUG
6088 nfa_regengine.expr = prog->pattern;
6089#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090
Bram Moolenaard89616e2013-06-06 18:46:06 +02006091 if (prog->reganch && col > 0)
6092 return 0L;
6093
Bram Moolenaar473de612013-06-08 18:19:48 +02006094 need_clear_subexpr = TRUE;
6095#ifdef FEAT_SYN_HL
6096 /* Clear the external match subpointers if necessary. */
6097 if (prog->reghasz == REX_SET)
6098 {
6099 nfa_has_zsubexpr = TRUE;
6100 need_clear_zsubexpr = TRUE;
6101 }
6102 else
6103 nfa_has_zsubexpr = FALSE;
6104#endif
6105
Bram Moolenaard89616e2013-06-06 18:46:06 +02006106 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006107 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006108 /* Skip ahead until a character we know the match must start with.
6109 * When there is none there is no match. */
6110 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006111 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006112
Bram Moolenaar473de612013-06-08 18:19:48 +02006113 /* If match_text is set it contains the full text that must match.
6114 * Nothing else to try. Doesn't handle combining chars well. */
6115 if (prog->match_text != NULL && !ireg_icombine)
6116 return find_match_text(col, prog->regstart, prog->match_text);
6117 }
6118
Bram Moolenaard89616e2013-06-06 18:46:06 +02006119 /* If the start column is past the maximum column: no need to try. */
6120 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6121 goto theend;
6122
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006123 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006124 for (i = 0; i < nstate; ++i)
6125 {
6126 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006127 prog->state[i].lastlist[0] = 0;
6128 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006129 }
6130
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006131 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006132
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006133#ifdef DEBUG
6134 nfa_regengine.expr = NULL;
6135#endif
6136
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006137theend:
6138 return retval;
6139}
6140
6141/*
6142 * Compile a regular expression into internal code for the NFA matcher.
6143 * Returns the program in allocated space. Returns NULL for an error.
6144 */
6145 static regprog_T *
6146nfa_regcomp(expr, re_flags)
6147 char_u *expr;
6148 int re_flags;
6149{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006150 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006151 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006152 int *postfix;
6153
6154 if (expr == NULL)
6155 return NULL;
6156
6157#ifdef DEBUG
6158 nfa_regengine.expr = expr;
6159#endif
6160
6161 init_class_tab();
6162
6163 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6164 return NULL;
6165
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006166 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006167 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006168 postfix = re2post();
6169 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006170 {
6171 /* TODO: only give this error for debugging? */
6172 if (post_ptr >= post_end)
6173 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006174 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006175 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006176
6177 /*
6178 * In order to build the NFA, we parse the input regexp twice:
6179 * 1. first pass to count size (so we can allocate space)
6180 * 2. second to emit code
6181 */
6182#ifdef ENABLE_LOG
6183 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006184 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006185
6186 if (f != NULL)
6187 {
6188 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6189 fclose(f);
6190 }
6191 }
6192#endif
6193
6194 /*
6195 * PASS 1
6196 * Count number of NFA states in "nstate". Do not build the NFA.
6197 */
6198 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006199
6200 /* Space for compiled regexp */
6201 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
6202 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6203 if (prog == NULL)
6204 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006205 state_ptr = prog->state;
6206
6207 /*
6208 * PASS 2
6209 * Build the NFA
6210 */
6211 prog->start = post2nfa(postfix, post_ptr, FALSE);
6212 if (prog->start == NULL)
6213 goto fail;
6214
6215 prog->regflags = regflags;
6216 prog->engine = &nfa_regengine;
6217 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006218 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006219 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006220 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006221
6222 prog->reganch = nfa_get_reganch(prog->start, 0);
6223 prog->regstart = nfa_get_regstart(prog->start, 0);
6224
Bram Moolenaar473de612013-06-08 18:19:48 +02006225 prog->match_text = nfa_get_match_text(prog->start);
6226
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006227#ifdef ENABLE_LOG
6228 nfa_postfix_dump(expr, OK);
6229 nfa_dump(prog);
6230#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006231#ifdef FEAT_SYN_HL
6232 /* Remember whether this pattern has any \z specials in it. */
6233 prog->reghasz = re_has_z;
6234#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006235#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006236 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006237 nfa_regengine.expr = NULL;
6238#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006239
6240out:
6241 vim_free(post_start);
6242 post_start = post_ptr = post_end = NULL;
6243 state_ptr = NULL;
6244 return (regprog_T *)prog;
6245
6246fail:
6247 vim_free(prog);
6248 prog = NULL;
6249#ifdef ENABLE_LOG
6250 nfa_postfix_dump(expr, FAIL);
6251#endif
6252#ifdef DEBUG
6253 nfa_regengine.expr = NULL;
6254#endif
6255 goto out;
6256}
6257
Bram Moolenaar473de612013-06-08 18:19:48 +02006258/*
6259 * Free a compiled regexp program, returned by nfa_regcomp().
6260 */
6261 static void
6262nfa_regfree(prog)
6263 regprog_T *prog;
6264{
6265 if (prog != NULL)
6266 {
6267 vim_free(((nfa_regprog_T *)prog)->match_text);
6268#ifdef DEBUG
6269 vim_free(((nfa_regprog_T *)prog)->pattern);
6270#endif
6271 vim_free(prog);
6272 }
6273}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006274
6275/*
6276 * Match a regexp against a string.
6277 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6278 * Uses curbuf for line count and 'iskeyword'.
6279 *
6280 * Return TRUE if there is a match, FALSE if not.
6281 */
6282 static int
6283nfa_regexec(rmp, line, col)
6284 regmatch_T *rmp;
6285 char_u *line; /* string to match against */
6286 colnr_T col; /* column to start looking for match */
6287{
6288 reg_match = rmp;
6289 reg_mmatch = NULL;
6290 reg_maxline = 0;
6291 reg_line_lbr = FALSE;
6292 reg_buf = curbuf;
6293 reg_win = NULL;
6294 ireg_ic = rmp->rm_ic;
6295#ifdef FEAT_MBYTE
6296 ireg_icombine = FALSE;
6297#endif
6298 ireg_maxcol = 0;
6299 return (nfa_regexec_both(line, col) != 0);
6300}
6301
6302#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6303 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6304
6305static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6306
6307/*
6308 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6309 */
6310 static int
6311nfa_regexec_nl(rmp, line, col)
6312 regmatch_T *rmp;
6313 char_u *line; /* string to match against */
6314 colnr_T col; /* column to start looking for match */
6315{
6316 reg_match = rmp;
6317 reg_mmatch = NULL;
6318 reg_maxline = 0;
6319 reg_line_lbr = TRUE;
6320 reg_buf = curbuf;
6321 reg_win = NULL;
6322 ireg_ic = rmp->rm_ic;
6323#ifdef FEAT_MBYTE
6324 ireg_icombine = FALSE;
6325#endif
6326 ireg_maxcol = 0;
6327 return (nfa_regexec_both(line, col) != 0);
6328}
6329#endif
6330
6331
6332/*
6333 * Match a regexp against multiple lines.
6334 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6335 * Uses curbuf for line count and 'iskeyword'.
6336 *
6337 * Return zero if there is no match. Return number of lines contained in the
6338 * match otherwise.
6339 *
6340 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6341 *
6342 * ! Also NOTE : match may actually be in another line. e.g.:
6343 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6344 *
6345 * +-------------------------+
6346 * |a |
6347 * |b |
6348 * |c |
6349 * | |
6350 * +-------------------------+
6351 *
6352 * then nfa_regexec_multi() returns 3. while the original
6353 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6354 *
6355 * FIXME if this behavior is not compatible.
6356 */
6357 static long
6358nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6359 regmmatch_T *rmp;
6360 win_T *win; /* window in which to search or NULL */
6361 buf_T *buf; /* buffer in which to search */
6362 linenr_T lnum; /* nr of line to start looking for match */
6363 colnr_T col; /* column to start looking for match */
6364 proftime_T *tm UNUSED; /* timeout limit or NULL */
6365{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 reg_match = NULL;
6367 reg_mmatch = rmp;
6368 reg_buf = buf;
6369 reg_win = win;
6370 reg_firstlnum = lnum;
6371 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6372 reg_line_lbr = FALSE;
6373 ireg_ic = rmp->rmm_ic;
6374#ifdef FEAT_MBYTE
6375 ireg_icombine = FALSE;
6376#endif
6377 ireg_maxcol = rmp->rmm_maxcol;
6378
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006379 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006380}
6381
6382#ifdef DEBUG
6383# undef ENABLE_LOG
6384#endif