blob: 2c2e9dee3ecbd9a313679aca11dcf68f8ed47ae4 [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}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002797
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002798/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002799 * Convert a postfix form into its equivalent NFA.
2800 * Return the NFA start state on success, NULL otherwise.
2801 */
2802 static nfa_state_T *
2803post2nfa(postfix, end, nfa_calc_size)
2804 int *postfix;
2805 int *end;
2806 int nfa_calc_size;
2807{
2808 int *p;
2809 int mopen;
2810 int mclose;
2811 Frag_T *stack = NULL;
2812 Frag_T *stackp = NULL;
2813 Frag_T *stack_end = NULL;
2814 Frag_T e1;
2815 Frag_T e2;
2816 Frag_T e;
2817 nfa_state_T *s;
2818 nfa_state_T *s1;
2819 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002820 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821
2822 if (postfix == NULL)
2823 return NULL;
2824
Bram Moolenaar053bb602013-05-20 13:55:21 +02002825#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826#define POP() st_pop(&stackp, stack); \
2827 if (stackp < stack) \
2828 { \
2829 st_error(postfix, end, p); \
2830 return NULL; \
2831 }
2832
2833 if (nfa_calc_size == FALSE)
2834 {
2835 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002836 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002837 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002838 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839 }
2840
2841 for (p = postfix; p < end; ++p)
2842 {
2843 switch (*p)
2844 {
2845 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002846 /* Concatenation.
2847 * Pay attention: this operator does not exist in the r.e. itself
2848 * (it is implicit, really). It is added when r.e. is translated
2849 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002850 if (nfa_calc_size == TRUE)
2851 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002852 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002853 break;
2854 }
2855 e2 = POP();
2856 e1 = POP();
2857 patch(e1.out, e2.start);
2858 PUSH(frag(e1.start, e2.out));
2859 break;
2860
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002861 case NFA_OR:
2862 /* Alternation */
2863 if (nfa_calc_size == TRUE)
2864 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002865 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002866 break;
2867 }
2868 e2 = POP();
2869 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002870 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002872 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873 PUSH(frag(s, append(e1.out, e2.out)));
2874 break;
2875
2876 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002877 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002878 if (nfa_calc_size == TRUE)
2879 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002880 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881 break;
2882 }
2883 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002884 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002885 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002886 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002887 patch(e.out, s);
2888 PUSH(frag(s, list1(&s->out1)));
2889 break;
2890
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002891 case NFA_STAR_NONGREEDY:
2892 /* Zero or more, prefer zero */
2893 if (nfa_calc_size == TRUE)
2894 {
2895 nstate++;
2896 break;
2897 }
2898 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002899 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002900 if (s == NULL)
2901 goto theend;
2902 patch(e.out, s);
2903 PUSH(frag(s, list1(&s->out)));
2904 break;
2905
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906 case NFA_QUEST:
2907 /* one or zero atoms=> greedy match */
2908 if (nfa_calc_size == TRUE)
2909 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002910 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002911 break;
2912 }
2913 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002914 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002915 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002916 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 PUSH(frag(s, append(e.out, list1(&s->out1))));
2918 break;
2919
2920 case NFA_QUEST_NONGREEDY:
2921 /* zero or one atoms => non-greedy match */
2922 if (nfa_calc_size == TRUE)
2923 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002924 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002925 break;
2926 }
2927 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002928 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002929 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002930 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931 PUSH(frag(s, append(e.out, list1(&s->out))));
2932 break;
2933
Bram Moolenaar417bad22013-06-07 14:08:30 +02002934 case NFA_END_COLL:
2935 case NFA_END_NEG_COLL:
2936 /* On the stack is the sequence starting with NFA_START_COLL or
2937 * NFA_START_NEG_COLL and all possible characters. Patch it to
2938 * add the output to the start. */
2939 if (nfa_calc_size == TRUE)
2940 {
2941 nstate++;
2942 break;
2943 }
2944 e = POP();
2945 s = alloc_state(NFA_END_COLL, NULL, NULL);
2946 if (s == NULL)
2947 goto theend;
2948 patch(e.out, s);
2949 e.start->out1 = s;
2950 PUSH(frag(e.start, list1(&s->out)));
2951 break;
2952
2953 case NFA_RANGE:
2954 /* Before this are two characters, the low and high end of a
2955 * range. Turn them into two states with MIN and MAX. */
2956 if (nfa_calc_size == TRUE)
2957 {
2958 /* nstate += 0; */
2959 break;
2960 }
2961 e2 = POP();
2962 e1 = POP();
2963 e2.start->val = e2.start->c;
2964 e2.start->c = NFA_RANGE_MAX;
2965 e1.start->val = e1.start->c;
2966 e1.start->c = NFA_RANGE_MIN;
2967 patch(e1.out, e2.start);
2968 PUSH(frag(e1.start, e2.out));
2969 break;
2970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002971 case NFA_SKIP_CHAR:
2972 /* Symbol of 0-length, Used in a repetition
2973 * with max/min count of 0 */
2974 if (nfa_calc_size == TRUE)
2975 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002976 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002977 break;
2978 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002979 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002980 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002981 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002982 PUSH(frag(s, list1(&s->out)));
2983 break;
2984
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002985 case NFA_OPT_CHARS:
2986 {
2987 int n;
2988
2989 /* \%[abc] */
2990 n = *++p; /* get number of characters */
2991 if (nfa_calc_size == TRUE)
2992 {
2993 nstate += n;
2994 break;
2995 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002996 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002997 e1.out = NULL; /* stores list with out1's */
2998 s1 = NULL; /* previous NFA_SPLIT to connect to */
2999 while (n-- > 0)
3000 {
3001 e = POP(); /* get character */
3002 s = alloc_state(NFA_SPLIT, e.start, NULL);
3003 if (s == NULL)
3004 goto theend;
3005 if (e1.out == NULL)
3006 e1 = e;
3007 patch(e.out, s1);
3008 append(e1.out, list1(&s->out1));
3009 s1 = s;
3010 }
3011 PUSH(frag(s, e1.out));
3012 break;
3013 }
3014
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003015 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003016 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003017 case NFA_PREV_ATOM_JUST_BEFORE:
3018 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003019 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003020 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003021 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3022 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003023 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003024 int start_state;
3025 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003026 int n = 0;
3027 nfa_state_T *zend;
3028 nfa_state_T *skip;
3029
Bram Moolenaardecd9542013-06-07 16:31:50 +02003030 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003031 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003032 case NFA_PREV_ATOM_NO_WIDTH:
3033 start_state = NFA_START_INVISIBLE;
3034 end_state = NFA_END_INVISIBLE;
3035 break;
3036 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3037 start_state = NFA_START_INVISIBLE_NEG;
3038 end_state = NFA_END_INVISIBLE_NEG;
3039 break;
3040 case NFA_PREV_ATOM_JUST_BEFORE:
3041 start_state = NFA_START_INVISIBLE_BEFORE;
3042 end_state = NFA_END_INVISIBLE;
3043 break;
3044 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3045 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3046 end_state = NFA_END_INVISIBLE_NEG;
3047 break;
3048 case NFA_PREV_ATOM_LIKE_PATTERN:
3049 start_state = NFA_START_PATTERN;
3050 end_state = NFA_END_PATTERN;
3051 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003052 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003053
3054 if (before)
3055 n = *++p; /* get the count */
3056
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003057 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003058 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003059 * The \@<= operator: match for the preceding atom.
3060 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003061 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003062 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003063
3064 if (nfa_calc_size == TRUE)
3065 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003066 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003067 break;
3068 }
3069 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003070 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003071 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003072 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003073
Bram Moolenaar87953742013-06-05 18:52:40 +02003074 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003075 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003076 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003077 if (pattern)
3078 {
3079 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3080 skip = alloc_state(NFA_SKIP, NULL, NULL);
3081 zend = alloc_state(NFA_ZEND, s1, NULL);
3082 s1->out= skip;
3083 patch(e.out, zend);
3084 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003085 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003086 else
3087 {
3088 patch(e.out, s1);
3089 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003090 if (before)
3091 {
3092 if (n <= 0)
3093 /* See if we can guess the maximum width, it avoids a
3094 * lot of pointless tries. */
3095 n = nfa_max_width(e.start, 0);
3096 s->val = n; /* store the count */
3097 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003098 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003099 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003100 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003101
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003102#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003103 case NFA_COMPOSING: /* char with composing char */
3104#if 0
3105 /* TODO */
3106 if (regflags & RF_ICOMBINE)
3107 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003108 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003109 }
3110#endif
3111 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003112#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003113
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003114 case NFA_MOPEN: /* \( \) Submatch */
3115 case NFA_MOPEN1:
3116 case NFA_MOPEN2:
3117 case NFA_MOPEN3:
3118 case NFA_MOPEN4:
3119 case NFA_MOPEN5:
3120 case NFA_MOPEN6:
3121 case NFA_MOPEN7:
3122 case NFA_MOPEN8:
3123 case NFA_MOPEN9:
3124#ifdef FEAT_SYN_HL
3125 case NFA_ZOPEN: /* \z( \) Submatch */
3126 case NFA_ZOPEN1:
3127 case NFA_ZOPEN2:
3128 case NFA_ZOPEN3:
3129 case NFA_ZOPEN4:
3130 case NFA_ZOPEN5:
3131 case NFA_ZOPEN6:
3132 case NFA_ZOPEN7:
3133 case NFA_ZOPEN8:
3134 case NFA_ZOPEN9:
3135#endif
3136 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003137 if (nfa_calc_size == TRUE)
3138 {
3139 nstate += 2;
3140 break;
3141 }
3142
3143 mopen = *p;
3144 switch (*p)
3145 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003146 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3147#ifdef FEAT_SYN_HL
3148 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3149 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3150 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3151 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3152 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3153 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3154 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3155 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3156 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3157 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3158#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003159#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003160 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003161#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003163 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003164 mclose = *p + NSUBEXP;
3165 break;
3166 }
3167
3168 /* Allow "NFA_MOPEN" as a valid postfix representation for
3169 * the empty regexp "". In this case, the NFA will be
3170 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3171 * empty groups of parenthesis, and empty mbyte chars */
3172 if (stackp == stack)
3173 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003174 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003175 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003176 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003177 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003178 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003179 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 patch(list1(&s->out), s1);
3181 PUSH(frag(s, list1(&s1->out)));
3182 break;
3183 }
3184
3185 /* At least one node was emitted before NFA_MOPEN, so
3186 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3187 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003188 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003190 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003191
Bram Moolenaar525666f2013-06-02 16:40:55 +02003192 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003194 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 patch(e.out, s1);
3196
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003197#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003198 if (mopen == NFA_COMPOSING)
3199 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003201#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202
3203 PUSH(frag(s, list1(&s1->out)));
3204 break;
3205
Bram Moolenaar5714b802013-05-28 22:03:20 +02003206 case NFA_BACKREF1:
3207 case NFA_BACKREF2:
3208 case NFA_BACKREF3:
3209 case NFA_BACKREF4:
3210 case NFA_BACKREF5:
3211 case NFA_BACKREF6:
3212 case NFA_BACKREF7:
3213 case NFA_BACKREF8:
3214 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003215#ifdef FEAT_SYN_HL
3216 case NFA_ZREF1:
3217 case NFA_ZREF2:
3218 case NFA_ZREF3:
3219 case NFA_ZREF4:
3220 case NFA_ZREF5:
3221 case NFA_ZREF6:
3222 case NFA_ZREF7:
3223 case NFA_ZREF8:
3224 case NFA_ZREF9:
3225#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003226 if (nfa_calc_size == TRUE)
3227 {
3228 nstate += 2;
3229 break;
3230 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003231 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003232 if (s == NULL)
3233 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003234 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003235 if (s1 == NULL)
3236 goto theend;
3237 patch(list1(&s->out), s1);
3238 PUSH(frag(s, list1(&s1->out)));
3239 break;
3240
Bram Moolenaar423532e2013-05-29 21:14:42 +02003241 case NFA_LNUM:
3242 case NFA_LNUM_GT:
3243 case NFA_LNUM_LT:
3244 case NFA_VCOL:
3245 case NFA_VCOL_GT:
3246 case NFA_VCOL_LT:
3247 case NFA_COL:
3248 case NFA_COL_GT:
3249 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003250 case NFA_MARK:
3251 case NFA_MARK_GT:
3252 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003253 {
3254 int n = *++p; /* lnum, col or mark name */
3255
Bram Moolenaar423532e2013-05-29 21:14:42 +02003256 if (nfa_calc_size == TRUE)
3257 {
3258 nstate += 1;
3259 break;
3260 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003261 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003262 if (s == NULL)
3263 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003264 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003265 PUSH(frag(s, list1(&s->out)));
3266 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003267 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003268
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 case NFA_ZSTART:
3270 case NFA_ZEND:
3271 default:
3272 /* Operands */
3273 if (nfa_calc_size == TRUE)
3274 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003275 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 break;
3277 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003278 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003279 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003280 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 PUSH(frag(s, list1(&s->out)));
3282 break;
3283
3284 } /* switch(*p) */
3285
3286 } /* for(p = postfix; *p; ++p) */
3287
3288 if (nfa_calc_size == TRUE)
3289 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003290 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003291 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 }
3293
3294 e = POP();
3295 if (stackp != stack)
3296 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3297
3298 if (istate >= nstate)
3299 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3300
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 matchstate = &state_ptr[istate++]; /* the match state */
3302 matchstate->c = NFA_MATCH;
3303 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003304 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305
3306 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003307 ret = e.start;
3308
3309theend:
3310 vim_free(stack);
3311 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312
3313#undef POP1
3314#undef PUSH1
3315#undef POP2
3316#undef PUSH2
3317#undef POP
3318#undef PUSH
3319}
3320
3321/****************************************************************
3322 * NFA execution code.
3323 ****************************************************************/
3324
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003325typedef struct
3326{
3327 int in_use; /* number of subexpr with useful info */
3328
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003329 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003330 union
3331 {
3332 struct multipos
3333 {
3334 lpos_T start;
3335 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003336 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003337 struct linepos
3338 {
3339 char_u *start;
3340 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003341 } line[NSUBEXP];
3342 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003343} regsub_T;
3344
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003345typedef struct
3346{
3347 regsub_T norm; /* \( .. \) matches */
3348#ifdef FEAT_SYN_HL
3349 regsub_T synt; /* \z( .. \) matches */
3350#endif
3351} regsubs_T;
3352
Bram Moolenaara2d95102013-06-04 14:23:05 +02003353/* nfa_pim_T stores a Postponed Invisible Match. */
3354typedef struct nfa_pim_S nfa_pim_T;
3355struct nfa_pim_S
3356{
3357 nfa_state_T *state;
3358 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
3359 nfa_pim_T *pim; /* another PIM at the same position */
3360 regsubs_T subs; /* submatch info, only party used */
3361};
3362
3363/* Values for done in nfa_pim_T. */
3364#define NFA_PIM_TODO 0
3365#define NFA_PIM_MATCH 1
3366#define NFA_PIM_NOMATCH -1
3367
3368
Bram Moolenaar963fee22013-05-26 21:47:28 +02003369/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003370typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371{
3372 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003373 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003374 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003375 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003376} nfa_thread_T;
3377
Bram Moolenaar963fee22013-05-26 21:47:28 +02003378/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003379typedef struct
3380{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003381 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003382 int n; /* nr of states currently in "t" */
3383 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003384 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003385} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386
Bram Moolenaar5714b802013-05-28 22:03:20 +02003387#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003388static void log_subsexpr __ARGS((regsubs_T *subs));
3389static void log_subexpr __ARGS((regsub_T *sub));
3390
3391 static void
3392log_subsexpr(subs)
3393 regsubs_T *subs;
3394{
3395 log_subexpr(&subs->norm);
3396# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003397 if (nfa_has_zsubexpr)
3398 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003399# endif
3400}
3401
Bram Moolenaar5714b802013-05-28 22:03:20 +02003402 static void
3403log_subexpr(sub)
3404 regsub_T *sub;
3405{
3406 int j;
3407
3408 for (j = 0; j < sub->in_use; j++)
3409 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003410 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003411 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003412 sub->list.multi[j].start.col,
3413 (int)sub->list.multi[j].start.lnum,
3414 sub->list.multi[j].end.col,
3415 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003416 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003417 {
3418 char *s = (char *)sub->list.line[j].start;
3419 char *e = (char *)sub->list.line[j].end;
3420
Bram Moolenaar87953742013-06-05 18:52:40 +02003421 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003422 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003423 s == NULL ? "NULL" : s,
3424 e == NULL ? "NULL" : e);
3425 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003426}
3427#endif
3428
Bram Moolenaar963fee22013-05-26 21:47:28 +02003429/* Used during execution: whether a match has been found. */
3430static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003431
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003432static void clear_sub __ARGS((regsub_T *sub));
3433static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3434static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003435static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003436static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003437static int match_follows __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003438static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003439static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003440static 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 +02003441
3442 static void
3443clear_sub(sub)
3444 regsub_T *sub;
3445{
3446 if (REG_MULTI)
3447 /* Use 0xff to set lnum to -1 */
3448 vim_memset(sub->list.multi, 0xff,
3449 sizeof(struct multipos) * nfa_nsubexpr);
3450 else
3451 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3452 sub->in_use = 0;
3453}
3454
3455/*
3456 * Copy the submatches from "from" to "to".
3457 */
3458 static void
3459copy_sub(to, from)
3460 regsub_T *to;
3461 regsub_T *from;
3462{
3463 to->in_use = from->in_use;
3464 if (from->in_use > 0)
3465 {
3466 /* Copy the match start and end positions. */
3467 if (REG_MULTI)
3468 mch_memmove(&to->list.multi[0],
3469 &from->list.multi[0],
3470 sizeof(struct multipos) * from->in_use);
3471 else
3472 mch_memmove(&to->list.line[0],
3473 &from->list.line[0],
3474 sizeof(struct linepos) * from->in_use);
3475 }
3476}
3477
3478/*
3479 * Like copy_sub() but exclude the main match.
3480 */
3481 static void
3482copy_sub_off(to, from)
3483 regsub_T *to;
3484 regsub_T *from;
3485{
3486 if (to->in_use < from->in_use)
3487 to->in_use = from->in_use;
3488 if (from->in_use > 1)
3489 {
3490 /* Copy the match start and end positions. */
3491 if (REG_MULTI)
3492 mch_memmove(&to->list.multi[1],
3493 &from->list.multi[1],
3494 sizeof(struct multipos) * (from->in_use - 1));
3495 else
3496 mch_memmove(&to->list.line[1],
3497 &from->list.line[1],
3498 sizeof(struct linepos) * (from->in_use - 1));
3499 }
3500}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501
Bram Moolenaar428e9872013-05-30 17:05:39 +02003502/*
3503 * Return TRUE if "sub1" and "sub2" have the same positions.
3504 */
3505 static int
3506sub_equal(sub1, sub2)
3507 regsub_T *sub1;
3508 regsub_T *sub2;
3509{
3510 int i;
3511 int todo;
3512 linenr_T s1, e1;
3513 linenr_T s2, e2;
3514 char_u *sp1, *ep1;
3515 char_u *sp2, *ep2;
3516
3517 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3518 if (REG_MULTI)
3519 {
3520 for (i = 0; i < todo; ++i)
3521 {
3522 if (i < sub1->in_use)
3523 {
3524 s1 = sub1->list.multi[i].start.lnum;
3525 e1 = sub1->list.multi[i].end.lnum;
3526 }
3527 else
3528 {
3529 s1 = 0;
3530 e1 = 0;
3531 }
3532 if (i < sub2->in_use)
3533 {
3534 s2 = sub2->list.multi[i].start.lnum;
3535 e2 = sub2->list.multi[i].end.lnum;
3536 }
3537 else
3538 {
3539 s2 = 0;
3540 e2 = 0;
3541 }
3542 if (s1 != s2 || e1 != e2)
3543 return FALSE;
3544 if (s1 != 0 && sub1->list.multi[i].start.col
3545 != sub2->list.multi[i].start.col)
3546 return FALSE;
3547 if (e1 != 0 && sub1->list.multi[i].end.col
3548 != sub2->list.multi[i].end.col)
3549 return FALSE;
3550 }
3551 }
3552 else
3553 {
3554 for (i = 0; i < todo; ++i)
3555 {
3556 if (i < sub1->in_use)
3557 {
3558 sp1 = sub1->list.line[i].start;
3559 ep1 = sub1->list.line[i].end;
3560 }
3561 else
3562 {
3563 sp1 = NULL;
3564 ep1 = NULL;
3565 }
3566 if (i < sub2->in_use)
3567 {
3568 sp2 = sub2->list.line[i].start;
3569 ep2 = sub2->list.line[i].end;
3570 }
3571 else
3572 {
3573 sp2 = NULL;
3574 ep2 = NULL;
3575 }
3576 if (sp1 != sp2 || ep1 != ep2)
3577 return FALSE;
3578 }
3579 }
3580
3581 return TRUE;
3582}
3583
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003584#ifdef ENABLE_LOG
3585 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003586report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003587{
3588 int col;
3589
3590 if (sub->in_use <= 0)
3591 col = -1;
3592 else if (REG_MULTI)
3593 col = sub->list.multi[0].start.col;
3594 else
3595 col = (int)(sub->list.line[0].start - regline);
3596 nfa_set_code(state->c);
3597 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3598 action, abs(state->id), lid, state->c, code, col);
3599}
3600#endif
3601
Bram Moolenaar43e02982013-06-07 17:31:29 +02003602/*
3603 * Return TRUE if the same state is already in list "l" with the same
3604 * positions as "subs".
3605 */
3606 static int
3607has_state_with_pos(l, state, subs)
3608 nfa_list_T *l; /* runtime state list */
3609 nfa_state_T *state; /* state to update */
3610 regsubs_T *subs; /* pointers to subexpressions */
3611{
3612 nfa_thread_T *thread;
3613 int i;
3614
3615 for (i = 0; i < l->n; ++i)
3616 {
3617 thread = &l->t[i];
3618 if (thread->state->id == state->id
3619 && sub_equal(&thread->subs.norm, &subs->norm)
3620#ifdef FEAT_SYN_HL
3621 && (!nfa_has_zsubexpr ||
3622 sub_equal(&thread->subs.synt, &subs->synt))
3623#endif
3624 )
3625 return TRUE;
3626 }
3627 return FALSE;
3628}
3629
3630/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003631 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3632 */
3633 static int
3634match_follows(startstate, depth)
3635 nfa_state_T *startstate;
3636 int depth;
3637{
3638 nfa_state_T *state = startstate;
3639
3640 /* avoid too much recursion */
3641 if (depth > 10)
3642 return FALSE;
3643
3644 for (;;)
3645 {
3646 switch (state->c)
3647 {
3648 case NFA_MATCH:
3649 return TRUE;
3650
3651 case NFA_SPLIT:
3652 return match_follows(state->out, depth + 1)
3653 || match_follows(state->out1, depth + 1);
3654
3655 case NFA_START_INVISIBLE:
3656 case NFA_START_INVISIBLE_BEFORE:
3657 case NFA_START_INVISIBLE_NEG:
3658 case NFA_START_INVISIBLE_BEFORE_NEG:
3659 case NFA_COMPOSING:
3660 /* skip ahead to next state */
3661 state = state->out1->out;
3662 break;
3663
3664 case NFA_ANY:
3665 case NFA_IDENT:
3666 case NFA_SIDENT:
3667 case NFA_KWORD:
3668 case NFA_SKWORD:
3669 case NFA_FNAME:
3670 case NFA_SFNAME:
3671 case NFA_PRINT:
3672 case NFA_SPRINT:
3673 case NFA_WHITE:
3674 case NFA_NWHITE:
3675 case NFA_DIGIT:
3676 case NFA_NDIGIT:
3677 case NFA_HEX:
3678 case NFA_NHEX:
3679 case NFA_OCTAL:
3680 case NFA_NOCTAL:
3681 case NFA_WORD:
3682 case NFA_NWORD:
3683 case NFA_HEAD:
3684 case NFA_NHEAD:
3685 case NFA_ALPHA:
3686 case NFA_NALPHA:
3687 case NFA_LOWER:
3688 case NFA_NLOWER:
3689 case NFA_UPPER:
3690 case NFA_NUPPER:
3691 case NFA_START_COLL:
3692 case NFA_START_NEG_COLL:
3693 case NFA_NEWL:
3694 /* state will advance input */
3695 return FALSE;
3696
3697 default:
3698 if (state->c > 0)
3699 /* state will advance input */
3700 return FALSE;
3701
3702 /* Others: zero-width or possibly zero-width, might still find
3703 * a match at the same position, keep looking. */
3704 break;
3705 }
3706 state = state->out;
3707 }
3708 return FALSE;
3709}
3710
3711
3712/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003713 * Return TRUE if "state" is already in list "l".
3714 */
3715 static int
3716state_in_list(l, state, subs)
3717 nfa_list_T *l; /* runtime state list */
3718 nfa_state_T *state; /* state to update */
3719 regsubs_T *subs; /* pointers to subexpressions */
3720{
3721 if (state->lastlist[nfa_ll_index] == l->id)
3722 {
3723 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3724 return TRUE;
3725 }
3726 return FALSE;
3727}
3728
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003730addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003731 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003732 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003733 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003734 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003736 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003737 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003738 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003739 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003740 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003741 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003742 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003743#ifdef ENABLE_LOG
3744 int did_print = FALSE;
3745#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003746
3747 if (l == NULL || state == NULL)
3748 return;
3749
3750 switch (state->c)
3751 {
3752 case NFA_SPLIT:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003753 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003754 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003755 case NFA_NCLOSE:
3756 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003757 case NFA_MCLOSE1:
3758 case NFA_MCLOSE2:
3759 case NFA_MCLOSE3:
3760 case NFA_MCLOSE4:
3761 case NFA_MCLOSE5:
3762 case NFA_MCLOSE6:
3763 case NFA_MCLOSE7:
3764 case NFA_MCLOSE8:
3765 case NFA_MCLOSE9:
3766#ifdef FEAT_SYN_HL
3767 case NFA_ZCLOSE:
3768 case NFA_ZCLOSE1:
3769 case NFA_ZCLOSE2:
3770 case NFA_ZCLOSE3:
3771 case NFA_ZCLOSE4:
3772 case NFA_ZCLOSE5:
3773 case NFA_ZCLOSE6:
3774 case NFA_ZCLOSE7:
3775 case NFA_ZCLOSE8:
3776 case NFA_ZCLOSE9:
3777#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003778 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003779 /* These nodes are not added themselves but their "out" and/or
3780 * "out1" may be added below. */
3781 break;
3782
3783 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003784 case NFA_MOPEN1:
3785 case NFA_MOPEN2:
3786 case NFA_MOPEN3:
3787 case NFA_MOPEN4:
3788 case NFA_MOPEN5:
3789 case NFA_MOPEN6:
3790 case NFA_MOPEN7:
3791 case NFA_MOPEN8:
3792 case NFA_MOPEN9:
3793#ifdef FEAT_SYN_HL
3794 case NFA_ZOPEN:
3795 case NFA_ZOPEN1:
3796 case NFA_ZOPEN2:
3797 case NFA_ZOPEN3:
3798 case NFA_ZOPEN4:
3799 case NFA_ZOPEN5:
3800 case NFA_ZOPEN6:
3801 case NFA_ZOPEN7:
3802 case NFA_ZOPEN8:
3803 case NFA_ZOPEN9:
3804#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003805 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003806 /* These nodes do not need to be added, but we need to bail out
3807 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003808 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003809 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003810 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003811 break;
3812
Bram Moolenaar307aa162013-06-02 16:34:21 +02003813 case NFA_BOL:
3814 case NFA_BOF:
3815 /* "^" won't match past end-of-line, don't bother trying.
3816 * Except when we are going to the next line for a look-behind
3817 * match. */
3818 if (reginput > regline
3819 && (nfa_endp == NULL
3820 || !REG_MULTI
3821 || reglnum == nfa_endp->se_u.pos.lnum))
3822 goto skip_add;
3823 /* FALLTHROUGH */
3824
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003825 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003826 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003827 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003828 /* This state is already in the list, don't add it again,
3829 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003830 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003831 {
3832skip_add:
3833#ifdef ENABLE_LOG
3834 nfa_set_code(state->c);
3835 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3836 abs(state->id), l->id, state->c, code);
3837#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003838 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003839 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003840
Bram Moolenaar43e02982013-06-07 17:31:29 +02003841 if (has_state_with_pos(l, state, subs))
3842 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003843 }
3844
Bram Moolenaara2d95102013-06-04 14:23:05 +02003845 /* when there are backreferences or look-behind matches the number
3846 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003847 if (nfa_has_backref && l->n == l->len)
3848 {
3849 int newlen = l->len * 3 / 2 + 50;
3850
3851 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3852 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003853 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003854
3855 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003856 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003857 thread = &l->t[l->n++];
3858 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003859 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003860 copy_sub(&thread->subs.norm, &subs->norm);
3861#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003862 if (nfa_has_zsubexpr)
3863 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003864#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003865#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003866 report_state("Adding", &thread->subs.norm, state, l->id);
3867 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003868#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003869 }
3870
3871#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003872 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003873 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003874#endif
3875 switch (state->c)
3876 {
3877 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003878 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003879 break;
3880
3881 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003882 /* order matters here */
3883 addstate(l, state->out, subs, off);
3884 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003885 break;
3886
Bram Moolenaar5714b802013-05-28 22:03:20 +02003887 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003888 case NFA_NOPEN:
3889 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003890 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003891 break;
3892
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003893 case NFA_MOPEN:
3894 case NFA_MOPEN1:
3895 case NFA_MOPEN2:
3896 case NFA_MOPEN3:
3897 case NFA_MOPEN4:
3898 case NFA_MOPEN5:
3899 case NFA_MOPEN6:
3900 case NFA_MOPEN7:
3901 case NFA_MOPEN8:
3902 case NFA_MOPEN9:
3903#ifdef FEAT_SYN_HL
3904 case NFA_ZOPEN:
3905 case NFA_ZOPEN1:
3906 case NFA_ZOPEN2:
3907 case NFA_ZOPEN3:
3908 case NFA_ZOPEN4:
3909 case NFA_ZOPEN5:
3910 case NFA_ZOPEN6:
3911 case NFA_ZOPEN7:
3912 case NFA_ZOPEN8:
3913 case NFA_ZOPEN9:
3914#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003916 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003917 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003919 sub = &subs->norm;
3920 }
3921#ifdef FEAT_SYN_HL
3922 else if (state->c >= NFA_ZOPEN)
3923 {
3924 subidx = state->c - NFA_ZOPEN;
3925 sub = &subs->synt;
3926 }
3927#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003928 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003929 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003930 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003931 sub = &subs->norm;
3932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003933
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003934 /* Set the position (with "off") in the subexpression. Save and
3935 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003936 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003937 if (REG_MULTI)
3938 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003939 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003940 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003941 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003942 save_in_use = -1;
3943 }
3944 else
3945 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003946 save_in_use = sub->in_use;
3947 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003948 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003949 sub->list.multi[i].start.lnum = -1;
3950 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003951 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003952 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003953 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003954 if (off == -1)
3955 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003956 sub->list.multi[subidx].start.lnum = reglnum + 1;
3957 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003958 }
3959 else
3960 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003961 sub->list.multi[subidx].start.lnum = reglnum;
3962 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003963 (colnr_T)(reginput - regline + off);
3964 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003965 }
3966 else
3967 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003968 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003969 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003970 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003971 save_in_use = -1;
3972 }
3973 else
3974 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003975 save_in_use = sub->in_use;
3976 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003977 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003978 sub->list.line[i].start = NULL;
3979 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003980 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003981 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003982 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003983 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003984 }
3985
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003986 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003987
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003988 if (save_in_use == -1)
3989 {
3990 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003991 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003992 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003993 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003994 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003995 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003996 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997 break;
3998
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003999 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004000 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004001 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004002 /* Do not overwrite the position set by \ze. If no \ze
4003 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004004 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004005 break;
4006 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004007 case NFA_MCLOSE1:
4008 case NFA_MCLOSE2:
4009 case NFA_MCLOSE3:
4010 case NFA_MCLOSE4:
4011 case NFA_MCLOSE5:
4012 case NFA_MCLOSE6:
4013 case NFA_MCLOSE7:
4014 case NFA_MCLOSE8:
4015 case NFA_MCLOSE9:
4016#ifdef FEAT_SYN_HL
4017 case NFA_ZCLOSE:
4018 case NFA_ZCLOSE1:
4019 case NFA_ZCLOSE2:
4020 case NFA_ZCLOSE3:
4021 case NFA_ZCLOSE4:
4022 case NFA_ZCLOSE5:
4023 case NFA_ZCLOSE6:
4024 case NFA_ZCLOSE7:
4025 case NFA_ZCLOSE8:
4026 case NFA_ZCLOSE9:
4027#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004028 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004029 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004030 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004031 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004032 sub = &subs->norm;
4033 }
4034#ifdef FEAT_SYN_HL
4035 else if (state->c >= NFA_ZCLOSE)
4036 {
4037 subidx = state->c - NFA_ZCLOSE;
4038 sub = &subs->synt;
4039 }
4040#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004041 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004042 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004043 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004044 sub = &subs->norm;
4045 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004047 /* We don't fill in gaps here, there must have been an MOPEN that
4048 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004049 save_in_use = sub->in_use;
4050 if (sub->in_use <= subidx)
4051 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004052 if (REG_MULTI)
4053 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004054 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004055 if (off == -1)
4056 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004057 sub->list.multi[subidx].end.lnum = reglnum + 1;
4058 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004059 }
4060 else
4061 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004062 sub->list.multi[subidx].end.lnum = reglnum;
4063 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004064 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004065 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004066 }
4067 else
4068 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004069 save_ptr = sub->list.line[subidx].end;
4070 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004071 }
4072
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004073 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004074
4075 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004076 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004077 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004078 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004079 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004080 break;
4081 }
4082}
4083
4084/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004085 * Like addstate(), but the new state(s) are put at position "*ip".
4086 * Used for zero-width matches, next state to use is the added one.
4087 * This makes sure the order of states to be tried does not change, which
4088 * matters for alternatives.
4089 */
4090 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004091addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004092 nfa_list_T *l; /* runtime state list */
4093 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004094 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004095 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004096 int *ip;
4097{
4098 int tlen = l->n;
4099 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004100 int listidx = *ip;
4101 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004102
4103 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004104 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02004105
Bram Moolenaara2d95102013-06-04 14:23:05 +02004106 /* fill in the "pim" field in the new states */
4107 if (pim != NULL)
4108 for (i = tlen; i < l->n; ++i)
4109 l->t[i].pim = pim;
4110
Bram Moolenaar4b417062013-05-25 20:19:50 +02004111 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004112 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004113 return;
4114
4115 /* re-order to put the new state at the current position */
4116 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004117 if (count == 1)
4118 {
4119 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004120 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004121 }
4122 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004123 {
4124 /* make space for new states, then move them from the
4125 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004126 mch_memmove(&(l->t[listidx + count]),
4127 &(l->t[listidx + 1]),
4128 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4129 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004130 &(l->t[l->n - 1]),
4131 sizeof(nfa_thread_T) * count);
4132 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004133 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004134 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004135}
4136
4137/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004138 * Check character class "class" against current character c.
4139 */
4140 static int
4141check_char_class(class, c)
4142 int class;
4143 int c;
4144{
4145 switch (class)
4146 {
4147 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004148 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004149 return OK;
4150 break;
4151 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004152 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004153 return OK;
4154 break;
4155 case NFA_CLASS_BLANK:
4156 if (c == ' ' || c == '\t')
4157 return OK;
4158 break;
4159 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004160 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004161 return OK;
4162 break;
4163 case NFA_CLASS_DIGIT:
4164 if (VIM_ISDIGIT(c))
4165 return OK;
4166 break;
4167 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004168 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004169 return OK;
4170 break;
4171 case NFA_CLASS_LOWER:
4172 if (MB_ISLOWER(c))
4173 return OK;
4174 break;
4175 case NFA_CLASS_PRINT:
4176 if (vim_isprintc(c))
4177 return OK;
4178 break;
4179 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004180 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004181 return OK;
4182 break;
4183 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004184 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004185 return OK;
4186 break;
4187 case NFA_CLASS_UPPER:
4188 if (MB_ISUPPER(c))
4189 return OK;
4190 break;
4191 case NFA_CLASS_XDIGIT:
4192 if (vim_isxdigit(c))
4193 return OK;
4194 break;
4195 case NFA_CLASS_TAB:
4196 if (c == '\t')
4197 return OK;
4198 break;
4199 case NFA_CLASS_RETURN:
4200 if (c == '\r')
4201 return OK;
4202 break;
4203 case NFA_CLASS_BACKSPACE:
4204 if (c == '\b')
4205 return OK;
4206 break;
4207 case NFA_CLASS_ESCAPE:
4208 if (c == '\033')
4209 return OK;
4210 break;
4211
4212 default:
4213 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004214 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4215 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004216 }
4217 return FAIL;
4218}
4219
Bram Moolenaar5714b802013-05-28 22:03:20 +02004220static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4221
4222/*
4223 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004224 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004225 */
4226 static int
4227match_backref(sub, subidx, bytelen)
4228 regsub_T *sub; /* pointers to subexpressions */
4229 int subidx;
4230 int *bytelen; /* out: length of match in bytes */
4231{
4232 int len;
4233
4234 if (sub->in_use <= subidx)
4235 {
4236retempty:
4237 /* backref was not set, match an empty string */
4238 *bytelen = 0;
4239 return TRUE;
4240 }
4241
4242 if (REG_MULTI)
4243 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004244 if (sub->list.multi[subidx].start.lnum < 0
4245 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004246 goto retempty;
4247 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004248 len = sub->list.multi[subidx].end.col
4249 - sub->list.multi[subidx].start.col;
4250 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004251 reginput, &len) == 0)
4252 {
4253 *bytelen = len;
4254 return TRUE;
4255 }
4256 }
4257 else
4258 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004259 if (sub->list.line[subidx].start == NULL
4260 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004261 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004262 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4263 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004264 {
4265 *bytelen = len;
4266 return TRUE;
4267 }
4268 }
4269 return FALSE;
4270}
4271
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004272#ifdef FEAT_SYN_HL
4273
4274static int match_zref __ARGS((int subidx, int *bytelen));
4275
4276/*
4277 * Check for a match with \z subexpression "subidx".
4278 * Return TRUE if it matches.
4279 */
4280 static int
4281match_zref(subidx, bytelen)
4282 int subidx;
4283 int *bytelen; /* out: length of match in bytes */
4284{
4285 int len;
4286
4287 cleanup_zsubexpr();
4288 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4289 {
4290 /* backref was not set, match an empty string */
4291 *bytelen = 0;
4292 return TRUE;
4293 }
4294
4295 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4296 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4297 {
4298 *bytelen = len;
4299 return TRUE;
4300 }
4301 return FALSE;
4302}
4303#endif
4304
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004305/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004306 * Save list IDs for all NFA states of "prog" into "list".
4307 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004308 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004309 */
4310 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004311nfa_save_listids(prog, list)
4312 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004313 int *list;
4314{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004315 int i;
4316 nfa_state_T *p;
4317
4318 /* Order in the list is reverse, it's a bit faster that way. */
4319 p = &prog->state[0];
4320 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004322 list[i] = p->lastlist[1];
4323 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004324 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004325 }
4326}
4327
4328/*
4329 * Restore list IDs from "list" to all NFA states.
4330 */
4331 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004332nfa_restore_listids(prog, list)
4333 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334 int *list;
4335{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004336 int i;
4337 nfa_state_T *p;
4338
4339 p = &prog->state[0];
4340 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004342 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004343 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004344 }
4345}
4346
Bram Moolenaar423532e2013-05-29 21:14:42 +02004347 static int
4348nfa_re_num_cmp(val, op, pos)
4349 long_u val;
4350 int op;
4351 long_u pos;
4352{
4353 if (op == 1) return pos > val;
4354 if (op == 2) return pos < val;
4355 return val == pos;
4356}
4357
Bram Moolenaarf46da702013-06-02 22:37:42 +02004358static 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 +02004359static 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 +02004360
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004361/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004362 * Recursively call nfa_regmatch()
4363 */
4364 static int
4365recursive_regmatch(state, prog, submatch, m, listids)
4366 nfa_state_T *state;
4367 nfa_regprog_T *prog;
4368 regsubs_T *submatch;
4369 regsubs_T *m;
4370 int **listids;
4371{
4372 char_u *save_reginput = reginput;
4373 char_u *save_regline = regline;
4374 int save_reglnum = reglnum;
4375 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004376 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004377 save_se_T *save_nfa_endp = nfa_endp;
4378 save_se_T endpos;
4379 save_se_T *endposp = NULL;
4380 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004381 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004382
Bram Moolenaardecd9542013-06-07 16:31:50 +02004383 if (state->c == NFA_START_INVISIBLE_BEFORE
4384 || state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004385 {
4386 /* The recursive match must end at the current position. */
4387 endposp = &endpos;
4388 if (REG_MULTI)
4389 {
4390 endpos.se_u.pos.col = (int)(reginput - regline);
4391 endpos.se_u.pos.lnum = reglnum;
4392 }
4393 else
4394 endpos.se_u.ptr = reginput;
4395
4396 /* Go back the specified number of bytes, or as far as the
4397 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004398 * not matching "\@<!". This is very ineffecient, limit the number of
4399 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004400 if (state->val <= 0)
4401 {
4402 if (REG_MULTI)
4403 {
4404 regline = reg_getline(--reglnum);
4405 if (regline == NULL)
4406 /* can't go before the first line */
4407 regline = reg_getline(++reglnum);
4408 }
4409 reginput = regline;
4410 }
4411 else
4412 {
4413 if (REG_MULTI && (int)(reginput - regline) < state->val)
4414 {
4415 /* Not enough bytes in this line, go to end of
4416 * previous line. */
4417 regline = reg_getline(--reglnum);
4418 if (regline == NULL)
4419 {
4420 /* can't go before the first line */
4421 regline = reg_getline(++reglnum);
4422 reginput = regline;
4423 }
4424 else
4425 reginput = regline + STRLEN(regline);
4426 }
4427 if ((int)(reginput - regline) >= state->val)
4428 {
4429 reginput -= state->val;
4430#ifdef FEAT_MBYTE
4431 if (has_mbyte)
4432 reginput -= mb_head_off(regline, reginput);
4433#endif
4434 }
4435 else
4436 reginput = regline;
4437 }
4438 }
4439
Bram Moolenaarf46da702013-06-02 22:37:42 +02004440#ifdef ENABLE_LOG
4441 if (log_fd != stderr)
4442 fclose(log_fd);
4443 log_fd = NULL;
4444#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004445 /* Have to clear the lastlist field of the NFA nodes, so that
4446 * nfa_regmatch() and addstate() can run properly after recursion. */
4447 if (nfa_ll_index == 1)
4448 {
4449 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4450 * values and clear them. */
4451 if (*listids == NULL)
4452 {
4453 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4454 if (*listids == NULL)
4455 {
4456 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4457 return 0;
4458 }
4459 }
4460 nfa_save_listids(prog, *listids);
4461 need_restore = TRUE;
4462 /* any value of nfa_listid will do */
4463 }
4464 else
4465 {
4466 /* First recursive nfa_regmatch() call, switch to the second lastlist
4467 * entry. Make sure nfa_listid is different from a previous recursive
4468 * call, because some states may still have this ID. */
4469 ++nfa_ll_index;
4470 if (nfa_listid <= nfa_alt_listid)
4471 nfa_listid = nfa_alt_listid;
4472 }
4473
4474 /* Call nfa_regmatch() to check if the current concat matches at this
4475 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004476 nfa_endp = endposp;
4477 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004478
4479 if (need_restore)
4480 nfa_restore_listids(prog, *listids);
4481 else
4482 {
4483 --nfa_ll_index;
4484 nfa_alt_listid = nfa_listid;
4485 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004486
4487 /* restore position in input text */
4488 reginput = save_reginput;
4489 regline = save_regline;
4490 reglnum = save_reglnum;
4491 nfa_match = save_nfa_match;
4492 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004493 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004494
4495#ifdef ENABLE_LOG
4496 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4497 if (log_fd != NULL)
4498 {
4499 fprintf(log_fd, "****************************\n");
4500 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4501 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4502 fprintf(log_fd, "****************************\n");
4503 }
4504 else
4505 {
4506 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4507 log_fd = stderr;
4508 }
4509#endif
4510
4511 return result;
4512}
4513
Bram Moolenaara2d95102013-06-04 14:23:05 +02004514static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004515static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004516static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004517
4518/*
4519 * Estimate the chance of a match with "state" failing.
4520 * NFA_ANY: 1
4521 * specific character: 99
4522 */
4523 static int
4524failure_chance(state, depth)
4525 nfa_state_T *state;
4526 int depth;
4527{
4528 int c = state->c;
4529 int l, r;
4530
4531 /* detect looping */
4532 if (depth > 4)
4533 return 1;
4534
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004535 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004536 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004537 case NFA_SPLIT:
4538 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4539 /* avoid recursive stuff */
4540 return 1;
4541 /* two alternatives, use the lowest failure chance */
4542 l = failure_chance(state->out, depth + 1);
4543 r = failure_chance(state->out1, depth + 1);
4544 return l < r ? l : r;
4545
4546 case NFA_ANY:
4547 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004548 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004549 case NFA_MATCH:
4550 /* empty match works always */
4551 return 0;
4552
4553 case NFA_BOL:
4554 case NFA_EOL:
4555 case NFA_BOF:
4556 case NFA_EOF:
4557 case NFA_NEWL:
4558 return 99;
4559
4560 case NFA_BOW:
4561 case NFA_EOW:
4562 return 90;
4563
4564 case NFA_MOPEN:
4565 case NFA_MOPEN1:
4566 case NFA_MOPEN2:
4567 case NFA_MOPEN3:
4568 case NFA_MOPEN4:
4569 case NFA_MOPEN5:
4570 case NFA_MOPEN6:
4571 case NFA_MOPEN7:
4572 case NFA_MOPEN8:
4573 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004574#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004575 case NFA_ZOPEN:
4576 case NFA_ZOPEN1:
4577 case NFA_ZOPEN2:
4578 case NFA_ZOPEN3:
4579 case NFA_ZOPEN4:
4580 case NFA_ZOPEN5:
4581 case NFA_ZOPEN6:
4582 case NFA_ZOPEN7:
4583 case NFA_ZOPEN8:
4584 case NFA_ZOPEN9:
4585 case NFA_ZCLOSE:
4586 case NFA_ZCLOSE1:
4587 case NFA_ZCLOSE2:
4588 case NFA_ZCLOSE3:
4589 case NFA_ZCLOSE4:
4590 case NFA_ZCLOSE5:
4591 case NFA_ZCLOSE6:
4592 case NFA_ZCLOSE7:
4593 case NFA_ZCLOSE8:
4594 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004595#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004596 case NFA_NOPEN:
4597 case NFA_MCLOSE:
4598 case NFA_MCLOSE1:
4599 case NFA_MCLOSE2:
4600 case NFA_MCLOSE3:
4601 case NFA_MCLOSE4:
4602 case NFA_MCLOSE5:
4603 case NFA_MCLOSE6:
4604 case NFA_MCLOSE7:
4605 case NFA_MCLOSE8:
4606 case NFA_MCLOSE9:
4607 case NFA_NCLOSE:
4608 return failure_chance(state->out, depth + 1);
4609
4610 case NFA_BACKREF1:
4611 case NFA_BACKREF2:
4612 case NFA_BACKREF3:
4613 case NFA_BACKREF4:
4614 case NFA_BACKREF5:
4615 case NFA_BACKREF6:
4616 case NFA_BACKREF7:
4617 case NFA_BACKREF8:
4618 case NFA_BACKREF9:
4619#ifdef FEAT_SYN_HL
4620 case NFA_ZREF1:
4621 case NFA_ZREF2:
4622 case NFA_ZREF3:
4623 case NFA_ZREF4:
4624 case NFA_ZREF5:
4625 case NFA_ZREF6:
4626 case NFA_ZREF7:
4627 case NFA_ZREF8:
4628 case NFA_ZREF9:
4629#endif
4630 /* backreferences don't match in many places */
4631 return 94;
4632
4633 case NFA_LNUM_GT:
4634 case NFA_LNUM_LT:
4635 case NFA_COL_GT:
4636 case NFA_COL_LT:
4637 case NFA_VCOL_GT:
4638 case NFA_VCOL_LT:
4639 case NFA_MARK_GT:
4640 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004641 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004642 /* before/after positions don't match very often */
4643 return 85;
4644
4645 case NFA_LNUM:
4646 return 90;
4647
4648 case NFA_CURSOR:
4649 case NFA_COL:
4650 case NFA_VCOL:
4651 case NFA_MARK:
4652 /* specific positions rarely match */
4653 return 98;
4654
4655 case NFA_COMPOSING:
4656 return 95;
4657
4658 default:
4659 if (c > 0)
4660 /* character match fails often */
4661 return 95;
4662 }
4663
4664 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004665 return 50;
4666}
4667
Bram Moolenaarf46da702013-06-02 22:37:42 +02004668/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004669 * Skip until the char "c" we know a match must start with.
4670 */
4671 static int
4672skip_to_start(c, colp)
4673 int c;
4674 colnr_T *colp;
4675{
4676 char_u *s;
4677
4678 /* Used often, do some work to avoid call overhead. */
4679 if (!ireg_ic
4680#ifdef FEAT_MBYTE
4681 && !has_mbyte
4682#endif
4683 )
4684 s = vim_strbyte(regline + *colp, c);
4685 else
4686 s = cstrchr(regline + *colp, c);
4687 if (s == NULL)
4688 return FAIL;
4689 *colp = (int)(s - regline);
4690 return OK;
4691}
4692
4693/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004694 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004695 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004696 * Returns zero for no match, 1 for a match.
4697 */
4698 static long
4699find_match_text(startcol, regstart, match_text)
4700 colnr_T startcol;
4701 int regstart;
4702 char_u *match_text;
4703{
4704 colnr_T col = startcol;
4705 int c1, c2;
4706 int len1, len2;
4707 int match;
4708
4709 for (;;)
4710 {
4711 match = TRUE;
4712 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4713 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4714 {
4715 c1 = PTR2CHAR(match_text + len1);
4716 c2 = PTR2CHAR(regline + col + len2);
4717 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4718 {
4719 match = FALSE;
4720 break;
4721 }
4722 len2 += MB_CHAR2LEN(c2);
4723 }
4724 if (match
4725#ifdef FEAT_MBYTE
4726 /* check that no composing char follows */
4727 && !(enc_utf8
4728 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4729#endif
4730 )
4731 {
4732 cleanup_subexpr();
4733 if (REG_MULTI)
4734 {
4735 reg_startpos[0].lnum = reglnum;
4736 reg_startpos[0].col = col;
4737 reg_endpos[0].lnum = reglnum;
4738 reg_endpos[0].col = col + len2;
4739 }
4740 else
4741 {
4742 reg_startp[0] = regline + col;
4743 reg_endp[0] = regline + col + len2;
4744 }
4745 return 1L;
4746 }
4747
4748 /* Try finding regstart after the current match. */
4749 col += MB_CHAR2LEN(regstart); /* skip regstart */
4750 if (skip_to_start(regstart, &col) == FAIL)
4751 break;
4752 }
4753 return 0L;
4754}
4755
4756/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004757 * Main matching routine.
4758 *
4759 * Run NFA to determine whether it matches reginput.
4760 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004761 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004762 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004763 * Return TRUE if there is a match, FALSE otherwise.
4764 * Note: Caller must ensure that: start != NULL.
4765 */
4766 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004767nfa_regmatch(prog, start, submatch, m)
4768 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004769 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004770 regsubs_T *submatch;
4771 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004772{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004773 int result;
4774 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004775 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004776 int go_to_nextline = FALSE;
4777 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004778 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004779 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004780 nfa_list_T *thislist;
4781 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004782 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004783 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004784 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004785 int add_count;
4786 int add_off;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004787 garray_T pimlist;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004788 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004789#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004790 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004791
4792 if (debug == NULL)
4793 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004794 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004795 return FALSE;
4796 }
4797#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004798 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004799 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004800
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004801 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004802 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004803 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004804 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004805 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004806 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004807 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004808 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004809
4810#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004811 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004812 if (log_fd != NULL)
4813 {
4814 fprintf(log_fd, "**********************************\n");
4815 nfa_set_code(start->c);
4816 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4817 abs(start->id), code);
4818 fprintf(log_fd, "**********************************\n");
4819 }
4820 else
4821 {
4822 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4823 log_fd = stderr;
4824 }
4825#endif
4826
4827 thislist = &list[0];
4828 thislist->n = 0;
4829 nextlist = &list[1];
4830 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004831#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004832 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004833#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004834 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004835
4836 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4837 * it's the first MOPEN. */
4838 if (toplevel)
4839 {
4840 if (REG_MULTI)
4841 {
4842 m->norm.list.multi[0].start.lnum = reglnum;
4843 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4844 }
4845 else
4846 m->norm.list.line[0].start = reginput;
4847 m->norm.in_use = 1;
4848 addstate(thislist, start->out, m, 0);
4849 }
4850 else
4851 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004852
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004853#define ADD_STATE_IF_MATCH(state) \
4854 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004855 add_state = state->out; \
4856 add_off = clen; \
4857 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858
4859 /*
4860 * Run for each character.
4861 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004862 for (;;)
4863 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004864 int curc;
4865 int clen;
4866
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004867#ifdef FEAT_MBYTE
4868 if (has_mbyte)
4869 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004870 curc = (*mb_ptr2char)(reginput);
4871 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 }
4873 else
4874#endif
4875 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004876 curc = *reginput;
4877 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004879 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004880 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004881 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004882 go_to_nextline = FALSE;
4883 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884
4885 /* swap lists */
4886 thislist = &list[flag];
4887 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004888 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004889 ++nfa_listid;
4890 thislist->id = nfa_listid;
4891 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004892
Bram Moolenaara2d95102013-06-04 14:23:05 +02004893 pimlist.ga_len = 0;
4894
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004895#ifdef ENABLE_LOG
4896 fprintf(log_fd, "------------------------------------------\n");
4897 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004898 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004899 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004900 {
4901 int i;
4902
4903 for (i = 0; i < thislist->n; i++)
4904 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4905 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906 fprintf(log_fd, "\n");
4907#endif
4908
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004909#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004910 fprintf(debug, "\n-------------------\n");
4911#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004912 /*
4913 * If the state lists are empty we can stop.
4914 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004915 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004916 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917
4918 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004919 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004921 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004923#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004924 nfa_set_code(t->state->c);
4925 fprintf(debug, "%s, ", code);
4926#endif
4927#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004928 {
4929 int col;
4930
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004931 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004932 col = -1;
4933 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004934 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004935 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004936 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004937 nfa_set_code(t->state->c);
4938 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4939 abs(t->state->id), (int)t->state->c, code, col);
4940 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004941#endif
4942
4943 /*
4944 * Handle the possible codes of the current state.
4945 * The most important is NFA_MATCH.
4946 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004947 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004948 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004949 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004950 switch (t->state->c)
4951 {
4952 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004953 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004954 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004955 copy_sub(&submatch->norm, &t->subs.norm);
4956#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004957 if (nfa_has_zsubexpr)
4958 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004959#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004960#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004961 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004962#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004963 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004964 * states at this position. When the list of states is going
4965 * to be empty quit without advancing, so that "reginput" is
4966 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004967 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004968 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004969 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004970 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004971
4972 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004973 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02004974 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004975 /*
4976 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004977 * NFA_START_INVISIBLE_BEFORE node.
4978 * They surround a zero-width group, used with "\@=", "\&",
4979 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004980 * If we got here, it means that the current "invisible" group
4981 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004982 * nfa_regmatch(). For a look-behind match only when it ends
4983 * in the position in "nfa_endp".
4984 * Submatches are stored in *m, and used in the parent call.
4985 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004986#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004987 if (nfa_endp != NULL)
4988 {
4989 if (REG_MULTI)
4990 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4991 (int)reglnum,
4992 (int)nfa_endp->se_u.pos.lnum,
4993 (int)(reginput - regline),
4994 nfa_endp->se_u.pos.col);
4995 else
4996 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4997 (int)(reginput - regline),
4998 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004999 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005000#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005001 /* If "nfa_endp" is set it's only a match if it ends at
5002 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005003 if (nfa_endp != NULL && (REG_MULTI
5004 ? (reglnum != nfa_endp->se_u.pos.lnum
5005 || (int)(reginput - regline)
5006 != nfa_endp->se_u.pos.col)
5007 : reginput != nfa_endp->se_u.ptr))
5008 break;
5009
5010 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005011 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005012 {
5013 copy_sub(&m->norm, &t->subs.norm);
5014#ifdef FEAT_SYN_HL
5015 if (nfa_has_zsubexpr)
5016 copy_sub(&m->synt, &t->subs.synt);
5017#endif
5018 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005019#ifdef ENABLE_LOG
5020 fprintf(log_fd, "Match found:\n");
5021 log_subsexpr(m);
5022#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005023 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005024 break;
5025
5026 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005027 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005028 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005029 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005031 nfa_pim_T *pim;
5032 int cout = t->state->out1->out->c;
5033
5034 /* Do it directly when what follows is possibly end of
5035 * match (closing paren).
5036 * Postpone when it is \@<= or \@<!, these are expensive.
5037 * TODO: remove the check for t->pim and check multiple
5038 * where it's used?
5039 * Otherwise first do the one that has the highest chance
5040 * of failing. */
5041 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005042#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02005043 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005044#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02005045 || t->pim != NULL
5046 || (t->state->c != NFA_START_INVISIBLE_BEFORE
Bram Moolenaardecd9542013-06-07 16:31:50 +02005047 && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Bram Moolenaara2d95102013-06-04 14:23:05 +02005048 && failure_chance(t->state->out1->out, 0)
5049 < failure_chance(t->state->out, 0)))
5050 {
5051 /*
5052 * First try matching the invisible match, then what
5053 * follows.
5054 */
5055 result = recursive_regmatch(t->state, prog,
5056 submatch, m, &listids);
5057
Bram Moolenaardecd9542013-06-07 16:31:50 +02005058 /* for \@! and \@<! it is a match when the result is
5059 * FALSE */
5060 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
5061 || t->state->c
5062 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005063 {
5064 /* Copy submatch info from the recursive call */
5065 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005066#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005067 if (nfa_has_zsubexpr)
5068 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005069#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005070
Bram Moolenaara2d95102013-06-04 14:23:05 +02005071 /* t->state->out1 is the corresponding
5072 * END_INVISIBLE node; Add its out to the current
5073 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005074 add_here = TRUE;
5075 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005076 }
5077 }
5078 else
5079 {
5080 /*
5081 * First try matching what follows at the current
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005082 * position. Only if a match is found, before
5083 * addstate() is called, then verify the invisible
5084 * match matches. Add a nfa_pim_T to the following
5085 * states, it contains info about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005086 */
5087 if (ga_grow(&pimlist, 1) == FAIL)
5088 goto theend;
5089 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
5090 ++pimlist.ga_len;
5091 pim->state = t->state;
5092 pim->pim = NULL;
5093 pim->result = NFA_PIM_TODO;
5094
5095 /* t->state->out1 is the corresponding END_INVISIBLE
5096 * node; Add its out to the current list (zero-width
5097 * match). */
5098 addstate_here(thislist, t->state->out1->out, &t->subs,
5099 pim, &listidx);
5100 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005101 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005102 break;
5103
Bram Moolenaar87953742013-06-05 18:52:40 +02005104 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005105 {
5106 nfa_state_T *skip = NULL;
5107#ifdef ENABLE_LOG
5108 int skip_lid = 0;
5109#endif
5110
5111 /* There is no point in trying to match the pattern if the
5112 * output state is not going to be added to the list. */
5113 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5114 {
5115 skip = t->state->out1->out;
5116#ifdef ENABLE_LOG
5117 skip_lid = nextlist->id;
5118#endif
5119 }
5120 else if (state_in_list(nextlist,
5121 t->state->out1->out->out, &t->subs))
5122 {
5123 skip = t->state->out1->out->out;
5124#ifdef ENABLE_LOG
5125 skip_lid = nextlist->id;
5126#endif
5127 }
5128 else if(state_in_list(thislist,
5129 t->state->out1->out->out, &t->subs))
5130 {
5131 skip = t->state->out1->out->out;
5132#ifdef ENABLE_LOG
5133 skip_lid = thislist->id;
5134#endif
5135 }
5136 if (skip != NULL)
5137 {
5138#ifdef ENABLE_LOG
5139 nfa_set_code(skip->c);
5140 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5141 abs(skip->id), skip_lid, skip->c, code);
5142#endif
5143 break;
5144 }
5145
Bram Moolenaar87953742013-06-05 18:52:40 +02005146 /* First try matching the pattern. */
5147 result = recursive_regmatch(t->state, prog,
5148 submatch, m, &listids);
5149 if (result)
5150 {
5151 int bytelen;
5152
5153#ifdef ENABLE_LOG
5154 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5155 log_subsexpr(m);
5156#endif
5157 /* Copy submatch info from the recursive call */
5158 copy_sub_off(&t->subs.norm, &m->norm);
5159#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005160 if (nfa_has_zsubexpr)
5161 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005162#endif
5163 /* Now we need to skip over the matched text and then
5164 * continue with what follows. */
5165 if (REG_MULTI)
5166 /* TODO: multi-line match */
5167 bytelen = m->norm.list.multi[0].end.col
5168 - (int)(reginput - regline);
5169 else
5170 bytelen = (int)(m->norm.list.line[0].end - reginput);
5171
5172#ifdef ENABLE_LOG
5173 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5174#endif
5175 if (bytelen == 0)
5176 {
5177 /* empty match, output of corresponding
5178 * NFA_END_PATTERN/NFA_SKIP to be used at current
5179 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005180 add_here = TRUE;
5181 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005182 }
5183 else if (bytelen <= clen)
5184 {
5185 /* match current character, output of corresponding
5186 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005187 add_state = t->state->out1->out->out;
5188 add_off = clen;
5189 }
5190 else
5191 {
5192 /* skip over the matched characters, set character
5193 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005194 add_state = t->state->out1->out;
5195 add_off = bytelen;
5196 add_count = bytelen - clen;
5197 }
5198 }
5199 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005200 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005201
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005202 case NFA_BOL:
5203 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005204 {
5205 add_here = TRUE;
5206 add_state = t->state->out;
5207 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005208 break;
5209
5210 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005211 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005212 {
5213 add_here = TRUE;
5214 add_state = t->state->out;
5215 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005216 break;
5217
5218 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005219 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005220
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005221 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005222 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005223#ifdef FEAT_MBYTE
5224 else if (has_mbyte)
5225 {
5226 int this_class;
5227
5228 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005229 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005230 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005231 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005232 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005233 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005234 }
5235#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005236 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005237 || (reginput > regline
5238 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005239 result = FALSE;
5240 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005241 {
5242 add_here = TRUE;
5243 add_state = t->state->out;
5244 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005245 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005246
5247 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005248 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005249 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005250 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005251#ifdef FEAT_MBYTE
5252 else if (has_mbyte)
5253 {
5254 int this_class, prev_class;
5255
5256 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005257 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005258 prev_class = reg_prev_class();
5259 if (this_class == prev_class
5260 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005261 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005262 }
5263#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005264 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005265 || (reginput[0] != NUL
5266 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005267 result = FALSE;
5268 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005269 {
5270 add_here = TRUE;
5271 add_state = t->state->out;
5272 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005273 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005274
Bram Moolenaar4b780632013-05-31 22:14:52 +02005275 case NFA_BOF:
5276 if (reglnum == 0 && reginput == regline
5277 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005278 {
5279 add_here = TRUE;
5280 add_state = t->state->out;
5281 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005282 break;
5283
5284 case NFA_EOF:
5285 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005286 {
5287 add_here = TRUE;
5288 add_state = t->state->out;
5289 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005290 break;
5291
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005292#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005293 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005294 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005295 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005296 int len = 0;
5297 nfa_state_T *end;
5298 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005299 int cchars[MAX_MCO];
5300 int ccount = 0;
5301 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005302
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005303 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005304 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005305 if (utf_iscomposing(sta->c))
5306 {
5307 /* Only match composing character(s), ignore base
5308 * character. Used for ".{composing}" and "{composing}"
5309 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005310 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005311 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005312 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005313 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005314 /* If \Z was present, then ignore composing characters.
5315 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005316 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005317 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005318 else
5319 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005320 while (sta->c != NFA_END_COMPOSING)
5321 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005322 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005323
5324 /* Check base character matches first, unless ignored. */
5325 else if (len > 0 || mc == sta->c)
5326 {
5327 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005328 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005329 len += mb_char2len(mc);
5330 sta = sta->out;
5331 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005332
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005333 /* We don't care about the order of composing characters.
5334 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005335 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005336 {
5337 mc = mb_ptr2char(reginput + len);
5338 cchars[ccount++] = mc;
5339 len += mb_char2len(mc);
5340 if (ccount == MAX_MCO)
5341 break;
5342 }
5343
5344 /* Check that each composing char in the pattern matches a
5345 * composing char in the text. We do not check if all
5346 * composing chars are matched. */
5347 result = OK;
5348 while (sta->c != NFA_END_COMPOSING)
5349 {
5350 for (j = 0; j < ccount; ++j)
5351 if (cchars[j] == sta->c)
5352 break;
5353 if (j == ccount)
5354 {
5355 result = FAIL;
5356 break;
5357 }
5358 sta = sta->out;
5359 }
5360 }
5361 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005362 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005363
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005364 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005365 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005366 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005367 }
5368#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005369
5370 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005371 if (curc == NUL && !reg_line_lbr && REG_MULTI
5372 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005373 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005374 go_to_nextline = TRUE;
5375 /* Pass -1 for the offset, which means taking the position
5376 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005377 add_state = t->state->out;
5378 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005379 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005380 else if (curc == '\n' && reg_line_lbr)
5381 {
5382 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005383 add_state = t->state->out;
5384 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005385 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005386 break;
5387
Bram Moolenaar417bad22013-06-07 14:08:30 +02005388 case NFA_START_COLL:
5389 case NFA_START_NEG_COLL:
5390 {
5391 /* What follows is a list of characters, until NFA_END_COLL.
5392 * One of them must match or none of them must match. */
5393 nfa_state_T *state;
5394 int result_if_matched;
5395 int c1, c2;
5396
5397 /* Never match EOL. If it's part of the collection it is added
5398 * as a separate state with an OR. */
5399 if (curc == NUL)
5400 break;
5401
5402 state = t->state->out;
5403 result_if_matched = (t->state->c == NFA_START_COLL);
5404 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005405 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005406 if (state->c == NFA_END_COLL)
5407 {
5408 result = !result_if_matched;
5409 break;
5410 }
5411 if (state->c == NFA_RANGE_MIN)
5412 {
5413 c1 = state->val;
5414 state = state->out; /* advance to NFA_RANGE_MAX */
5415 c2 = state->val;
5416#ifdef ENABLE_LOG
5417 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5418 curc, c1, c2);
5419#endif
5420 if (curc >= c1 && curc <= c2)
5421 {
5422 result = result_if_matched;
5423 break;
5424 }
5425 if (ireg_ic)
5426 {
5427 int curc_low = MB_TOLOWER(curc);
5428 int done = FALSE;
5429
5430 for ( ; c1 <= c2; ++c1)
5431 if (MB_TOLOWER(c1) == curc_low)
5432 {
5433 result = result_if_matched;
5434 done = TRUE;
5435 break;
5436 }
5437 if (done)
5438 break;
5439 }
5440 }
5441 else if (state->c < 0 ? check_char_class(state->c, curc)
5442 : (curc == state->c
5443 || (ireg_ic && MB_TOLOWER(curc)
5444 == MB_TOLOWER(state->c))))
5445 {
5446 result = result_if_matched;
5447 break;
5448 }
5449 state = state->out;
5450 }
5451 if (result)
5452 {
5453 /* next state is in out of the NFA_END_COLL, out1 of
5454 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005455 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005456 add_off = clen;
5457 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005458 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005459 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005460
5461 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005462 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005463 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005464 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005465 add_state = t->state->out;
5466 add_off = clen;
5467 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005468 break;
5469
5470 /*
5471 * Character classes like \a for alpha, \d for digit etc.
5472 */
5473 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005474 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005475 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 break;
5477
5478 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005479 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005480 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005481 break;
5482
5483 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005484 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005485 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005486 break;
5487
5488 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005489 result = !VIM_ISDIGIT(curc)
5490 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005491 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005492 break;
5493
5494 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005495 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005496 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005497 break;
5498
5499 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005500 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005501 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005502 break;
5503
5504 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005505 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005506 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005507 break;
5508
5509 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005510 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005511 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 break;
5513
5514 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005515 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005516 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005517 break;
5518
5519 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005520 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005521 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005522 break;
5523
5524 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005525 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005526 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005527 break;
5528
5529 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005530 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005531 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532 break;
5533
5534 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005535 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005536 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005537 break;
5538
5539 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005540 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005541 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005542 break;
5543
5544 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005545 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005546 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005547 break;
5548
5549 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005550 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005551 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552 break;
5553
5554 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005555 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005556 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557 break;
5558
5559 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005560 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005561 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562 break;
5563
5564 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005565 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005566 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005567 break;
5568
5569 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005570 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005571 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572 break;
5573
5574 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005575 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005576 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577 break;
5578
5579 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005580 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005581 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582 break;
5583
5584 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005585 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005586 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005587 break;
5588
5589 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005590 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005591 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005592 break;
5593
5594 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005595 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005596 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597 break;
5598
5599 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005600 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005601 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005602 break;
5603
Bram Moolenaar5714b802013-05-28 22:03:20 +02005604 case NFA_BACKREF1:
5605 case NFA_BACKREF2:
5606 case NFA_BACKREF3:
5607 case NFA_BACKREF4:
5608 case NFA_BACKREF5:
5609 case NFA_BACKREF6:
5610 case NFA_BACKREF7:
5611 case NFA_BACKREF8:
5612 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005613#ifdef FEAT_SYN_HL
5614 case NFA_ZREF1:
5615 case NFA_ZREF2:
5616 case NFA_ZREF3:
5617 case NFA_ZREF4:
5618 case NFA_ZREF5:
5619 case NFA_ZREF6:
5620 case NFA_ZREF7:
5621 case NFA_ZREF8:
5622 case NFA_ZREF9:
5623#endif
5624 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005625 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005626 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005627 int bytelen;
5628
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005629 if (t->state->c <= NFA_BACKREF9)
5630 {
5631 subidx = t->state->c - NFA_BACKREF1 + 1;
5632 result = match_backref(&t->subs.norm, subidx, &bytelen);
5633 }
5634#ifdef FEAT_SYN_HL
5635 else
5636 {
5637 subidx = t->state->c - NFA_ZREF1 + 1;
5638 result = match_zref(subidx, &bytelen);
5639 }
5640#endif
5641
Bram Moolenaar5714b802013-05-28 22:03:20 +02005642 if (result)
5643 {
5644 if (bytelen == 0)
5645 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005646 /* empty match always works, output of NFA_SKIP to be
5647 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005648 add_here = TRUE;
5649 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005650 }
5651 else if (bytelen <= clen)
5652 {
5653 /* match current character, jump ahead to out of
5654 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005655 add_state = t->state->out->out;
5656 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005657 }
5658 else
5659 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005660 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005661 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005662 add_state = t->state->out;
5663 add_off = bytelen;
5664 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005665 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005666 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005667 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005668 }
5669 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005670 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005671 if (t->count - clen <= 0)
5672 {
5673 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005674 add_state = t->state->out;
5675 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005676 }
5677 else
5678 {
5679 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005680 add_state = t->state;
5681 add_off = 0;
5682 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005683 }
5684 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005685
Bram Moolenaar423532e2013-05-29 21:14:42 +02005686 case NFA_LNUM:
5687 case NFA_LNUM_GT:
5688 case NFA_LNUM_LT:
5689 result = (REG_MULTI &&
5690 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5691 (long_u)(reglnum + reg_firstlnum)));
5692 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005693 {
5694 add_here = TRUE;
5695 add_state = t->state->out;
5696 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005697 break;
5698
5699 case NFA_COL:
5700 case NFA_COL_GT:
5701 case NFA_COL_LT:
5702 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5703 (long_u)(reginput - regline) + 1);
5704 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005705 {
5706 add_here = TRUE;
5707 add_state = t->state->out;
5708 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005709 break;
5710
5711 case NFA_VCOL:
5712 case NFA_VCOL_GT:
5713 case NFA_VCOL_LT:
5714 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5715 (long_u)win_linetabsize(
5716 reg_win == NULL ? curwin : reg_win,
5717 regline, (colnr_T)(reginput - regline)) + 1);
5718 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005719 {
5720 add_here = TRUE;
5721 add_state = t->state->out;
5722 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005723 break;
5724
Bram Moolenaar044aa292013-06-04 21:27:38 +02005725 case NFA_MARK:
5726 case NFA_MARK_GT:
5727 case NFA_MARK_LT:
5728 {
5729 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5730
5731 /* Compare the mark position to the match position. */
5732 result = (pos != NULL /* mark doesn't exist */
5733 && pos->lnum > 0 /* mark isn't set in reg_buf */
5734 && (pos->lnum == reglnum + reg_firstlnum
5735 ? (pos->col == (colnr_T)(reginput - regline)
5736 ? t->state->c == NFA_MARK
5737 : (pos->col < (colnr_T)(reginput - regline)
5738 ? t->state->c == NFA_MARK_GT
5739 : t->state->c == NFA_MARK_LT))
5740 : (pos->lnum < reglnum + reg_firstlnum
5741 ? t->state->c == NFA_MARK_GT
5742 : t->state->c == NFA_MARK_LT)));
5743 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005744 {
5745 add_here = TRUE;
5746 add_state = t->state->out;
5747 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005748 break;
5749 }
5750
Bram Moolenaar423532e2013-05-29 21:14:42 +02005751 case NFA_CURSOR:
5752 result = (reg_win != NULL
5753 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5754 && ((colnr_T)(reginput - regline)
5755 == reg_win->w_cursor.col));
5756 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005757 {
5758 add_here = TRUE;
5759 add_state = t->state->out;
5760 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005761 break;
5762
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005763 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005764#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005765 result = reg_match_visual();
5766 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005767 {
5768 add_here = TRUE;
5769 add_state = t->state->out;
5770 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005771#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005772 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005773
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005774 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005775 {
5776 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005777
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005778 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005779 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005780 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005781 result = (c == curc);
5782
5783 if (!result && ireg_ic)
5784 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005785#ifdef FEAT_MBYTE
5786 /* If there is a composing character which is not being
5787 * ignored there can be no match. Match with composing
5788 * character uses NFA_COMPOSING above. */
5789 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005790 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005791 result = FALSE;
5792#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005793 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005794 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005795 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005796
5797 } /* switch (t->state->c) */
5798
5799 if (add_state != NULL)
5800 {
Bram Moolenaar1e02e662013-06-08 23:26:27 +02005801 /* Handle the postponed invisible match before advancing to
5802 * the next character and for a zero-width match if the match
5803 * might end without advancing. */
5804 if (t->pim != NULL && (!add_here || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005805 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005806 if (t->pim->result == NFA_PIM_TODO)
5807 {
5808#ifdef ENABLE_LOG
5809 fprintf(log_fd, "\n");
5810 fprintf(log_fd, "==================================\n");
5811 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5812 fprintf(log_fd, "\n");
5813#endif
5814 result = recursive_regmatch(t->pim->state,
5815 prog, submatch, m, &listids);
5816 t->pim->result = result ? NFA_PIM_MATCH
5817 : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005818 /* for \@! and \@<! it is a match when the result is
5819 * FALSE */
5820 if (result != (t->pim->state->c
5821 == NFA_START_INVISIBLE_NEG
5822 || t->pim->state->c
5823 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005824 {
5825 /* Copy submatch info from the recursive call */
5826 copy_sub_off(&t->pim->subs.norm, &m->norm);
5827#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005828 if (nfa_has_zsubexpr)
5829 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005830#endif
5831 }
5832 }
5833 else
5834 {
5835 result = (t->pim->result == NFA_PIM_MATCH);
5836#ifdef ENABLE_LOG
5837 fprintf(log_fd, "\n");
5838 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5839 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5840 fprintf(log_fd, "\n");
5841#endif
5842 }
5843
Bram Moolenaardecd9542013-06-07 16:31:50 +02005844 /* for \@! and \@<! it is a match when result is FALSE */
5845 if (result != (t->pim->state->c == NFA_START_INVISIBLE_NEG
5846 || t->pim->state->c
5847 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005848 {
5849 /* Copy submatch info from the recursive call */
5850 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5851#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005852 if (nfa_has_zsubexpr)
5853 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005854#endif
5855 }
5856 else
5857 /* look-behind match failed, don't add the state */
5858 continue;
5859 }
5860
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005861 if (add_here)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02005862 addstate_here(thislist, add_state, &t->subs, t->pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005863 else
5864 {
5865 addstate(nextlist, add_state, &t->subs, add_off);
5866 if (add_count > 0)
5867 nextlist->t[nextlist->n - 1].count = add_count;
5868 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005869 }
5870
5871 } /* for (thislist = thislist; thislist->state; thislist++) */
5872
Bram Moolenaare23febd2013-05-26 18:40:14 +02005873 /* Look for the start of a match in the current position by adding the
5874 * start state to the list of states.
5875 * The first found match is the leftmost one, thus the order of states
5876 * matters!
5877 * Do not add the start state in recursive calls of nfa_regmatch(),
5878 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005879 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005880 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005881 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005882 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02005883 && reglnum == 0
5884 && clen != 0
5885 && (ireg_maxcol == 0
5886 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005887 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005888 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005889 ? (reglnum < nfa_endp->se_u.pos.lnum
5890 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005891 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005892 < nfa_endp->se_u.pos.col))
5893 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005894 {
5895#ifdef ENABLE_LOG
5896 fprintf(log_fd, "(---) STARTSTATE\n");
5897#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005898 /* Inline optimized code for addstate() if we know the state is
5899 * the first MOPEN. */
5900 if (toplevel)
5901 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005902 int add = TRUE;
5903 int c;
5904
5905 if (prog->regstart != NUL && clen != 0)
5906 {
5907 if (nextlist->n == 0)
5908 {
5909 colnr_T col = (colnr_T)(reginput - regline) + clen;
5910
5911 /* Nextlist is empty, we can skip ahead to the
5912 * character that must appear at the start. */
5913 if (skip_to_start(prog->regstart, &col) == FAIL)
5914 break;
5915#ifdef ENABLE_LOG
5916 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
5917 col - ((colnr_T)(reginput - regline) + clen));
5918#endif
5919 reginput = regline + col - clen;
5920 }
5921 else
5922 {
5923 /* Checking if the required start character matches is
5924 * cheaper than adding a state that won't match. */
5925 c = PTR2CHAR(reginput + clen);
5926 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
5927 != MB_TOLOWER(prog->regstart)))
5928 {
5929#ifdef ENABLE_LOG
5930 fprintf(log_fd, " Skipping start state, regstart does not match\n");
5931#endif
5932 add = FALSE;
5933 }
5934 }
5935 }
5936
5937 if (add)
5938 {
5939 if (REG_MULTI)
5940 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005941 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005942 else
5943 m->norm.list.line[0].start = reginput + clen;
5944 addstate(nextlist, start->out, m, clen);
5945 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005946 }
5947 else
5948 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005949 }
5950
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005951#ifdef ENABLE_LOG
5952 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005953 {
5954 int i;
5955
5956 for (i = 0; i < thislist->n; i++)
5957 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5958 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005959 fprintf(log_fd, "\n");
5960#endif
5961
5962nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005963 /* Advance to the next character, or advance to the next line, or
5964 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005965 if (clen != 0)
5966 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005967 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5968 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005969 reg_nextline();
5970 else
5971 break;
5972 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005973
5974#ifdef ENABLE_LOG
5975 if (log_fd != stderr)
5976 fclose(log_fd);
5977 log_fd = NULL;
5978#endif
5979
5980theend:
5981 /* Free memory */
5982 vim_free(list[0].t);
5983 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005984 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005985 ga_clear(&pimlist);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005986#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005987#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005988 fclose(debug);
5989#endif
5990
Bram Moolenaar963fee22013-05-26 21:47:28 +02005991 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005992}
5993
5994/*
5995 * Try match of "prog" with at regline["col"].
5996 * Returns 0 for failure, number of lines contained in the match otherwise.
5997 */
5998 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005999nfa_regtry(prog, col)
6000 nfa_regprog_T *prog;
6001 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006002{
6003 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006004 regsubs_T subs, m;
6005 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006006#ifdef ENABLE_LOG
6007 FILE *f;
6008#endif
6009
6010 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006011
6012#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006013 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006014 if (f != NULL)
6015 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006016 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006017#ifdef DEBUG
6018 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6019#endif
6020 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006021 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006022 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006023 fprintf(f, "\n\n");
6024 fclose(f);
6025 }
6026 else
6027 EMSG(_("Could not open temporary log file for writing "));
6028#endif
6029
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006030 clear_sub(&subs.norm);
6031 clear_sub(&m.norm);
6032#ifdef FEAT_SYN_HL
6033 clear_sub(&subs.synt);
6034 clear_sub(&m.synt);
6035#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006036
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006037 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006038 return 0;
6039
6040 cleanup_subexpr();
6041 if (REG_MULTI)
6042 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006043 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006044 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006045 reg_startpos[i] = subs.norm.list.multi[i].start;
6046 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006047 }
6048
6049 if (reg_startpos[0].lnum < 0)
6050 {
6051 reg_startpos[0].lnum = 0;
6052 reg_startpos[0].col = col;
6053 }
6054 if (reg_endpos[0].lnum < 0)
6055 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006056 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006057 reg_endpos[0].lnum = reglnum;
6058 reg_endpos[0].col = (int)(reginput - regline);
6059 }
6060 else
6061 /* Use line number of "\ze". */
6062 reglnum = reg_endpos[0].lnum;
6063 }
6064 else
6065 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006066 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006068 reg_startp[i] = subs.norm.list.line[i].start;
6069 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006070 }
6071
6072 if (reg_startp[0] == NULL)
6073 reg_startp[0] = regline + col;
6074 if (reg_endp[0] == NULL)
6075 reg_endp[0] = reginput;
6076 }
6077
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006078#ifdef FEAT_SYN_HL
6079 /* Package any found \z(...\) matches for export. Default is none. */
6080 unref_extmatch(re_extmatch_out);
6081 re_extmatch_out = NULL;
6082
6083 if (prog->reghasz == REX_SET)
6084 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006085 cleanup_zsubexpr();
6086 re_extmatch_out = make_extmatch();
6087 for (i = 0; i < subs.synt.in_use; i++)
6088 {
6089 if (REG_MULTI)
6090 {
6091 struct multipos *mpos = &subs.synt.list.multi[i];
6092
6093 /* Only accept single line matches. */
6094 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6095 re_extmatch_out->matches[i] =
6096 vim_strnsave(reg_getline(mpos->start.lnum)
6097 + mpos->start.col,
6098 mpos->end.col - mpos->start.col);
6099 }
6100 else
6101 {
6102 struct linepos *lpos = &subs.synt.list.line[i];
6103
6104 if (lpos->start != NULL && lpos->end != NULL)
6105 re_extmatch_out->matches[i] =
6106 vim_strnsave(lpos->start,
6107 (int)(lpos->end - lpos->start));
6108 }
6109 }
6110 }
6111#endif
6112
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006113 return 1 + reglnum;
6114}
6115
6116/*
6117 * Match a regexp against a string ("line" points to the string) or multiple
6118 * lines ("line" is NULL, use reg_getline()).
6119 *
6120 * Returns 0 for failure, number of lines contained in the match otherwise.
6121 */
6122 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006123nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006124 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006125 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126{
6127 nfa_regprog_T *prog;
6128 long retval = 0L;
6129 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006130 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006131
6132 if (REG_MULTI)
6133 {
6134 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6135 line = reg_getline((linenr_T)0); /* relative to the cursor */
6136 reg_startpos = reg_mmatch->startpos;
6137 reg_endpos = reg_mmatch->endpos;
6138 }
6139 else
6140 {
6141 prog = (nfa_regprog_T *)reg_match->regprog;
6142 reg_startp = reg_match->startp;
6143 reg_endp = reg_match->endp;
6144 }
6145
6146 /* Be paranoid... */
6147 if (prog == NULL || line == NULL)
6148 {
6149 EMSG(_(e_null));
6150 goto theend;
6151 }
6152
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006153 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6154 if (prog->regflags & RF_ICASE)
6155 ireg_ic = TRUE;
6156 else if (prog->regflags & RF_NOICASE)
6157 ireg_ic = FALSE;
6158
6159#ifdef FEAT_MBYTE
6160 /* If pattern contains "\Z" overrule value of ireg_icombine */
6161 if (prog->regflags & RF_ICOMBINE)
6162 ireg_icombine = TRUE;
6163#endif
6164
6165 regline = line;
6166 reglnum = 0; /* relative to line */
6167
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006168 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006169 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006170 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006171 nfa_listid = 1;
6172 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006173#ifdef DEBUG
6174 nfa_regengine.expr = prog->pattern;
6175#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006176
Bram Moolenaard89616e2013-06-06 18:46:06 +02006177 if (prog->reganch && col > 0)
6178 return 0L;
6179
Bram Moolenaar473de612013-06-08 18:19:48 +02006180 need_clear_subexpr = TRUE;
6181#ifdef FEAT_SYN_HL
6182 /* Clear the external match subpointers if necessary. */
6183 if (prog->reghasz == REX_SET)
6184 {
6185 nfa_has_zsubexpr = TRUE;
6186 need_clear_zsubexpr = TRUE;
6187 }
6188 else
6189 nfa_has_zsubexpr = FALSE;
6190#endif
6191
Bram Moolenaard89616e2013-06-06 18:46:06 +02006192 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006193 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006194 /* Skip ahead until a character we know the match must start with.
6195 * When there is none there is no match. */
6196 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006197 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006198
Bram Moolenaar473de612013-06-08 18:19:48 +02006199 /* If match_text is set it contains the full text that must match.
6200 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006201 if (prog->match_text != NULL
6202#ifdef FEAT_MBYTE
6203 && !ireg_icombine
6204#endif
6205 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006206 return find_match_text(col, prog->regstart, prog->match_text);
6207 }
6208
Bram Moolenaard89616e2013-06-06 18:46:06 +02006209 /* If the start column is past the maximum column: no need to try. */
6210 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6211 goto theend;
6212
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006213 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006214 for (i = 0; i < nstate; ++i)
6215 {
6216 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006217 prog->state[i].lastlist[0] = 0;
6218 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006219 }
6220
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006221 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006222
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006223#ifdef DEBUG
6224 nfa_regengine.expr = NULL;
6225#endif
6226
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006227theend:
6228 return retval;
6229}
6230
6231/*
6232 * Compile a regular expression into internal code for the NFA matcher.
6233 * Returns the program in allocated space. Returns NULL for an error.
6234 */
6235 static regprog_T *
6236nfa_regcomp(expr, re_flags)
6237 char_u *expr;
6238 int re_flags;
6239{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006240 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006241 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006242 int *postfix;
6243
6244 if (expr == NULL)
6245 return NULL;
6246
6247#ifdef DEBUG
6248 nfa_regengine.expr = expr;
6249#endif
6250
6251 init_class_tab();
6252
6253 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6254 return NULL;
6255
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006256 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006257 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006258 postfix = re2post();
6259 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006260 {
6261 /* TODO: only give this error for debugging? */
6262 if (post_ptr >= post_end)
6263 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006264 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006265 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006266
6267 /*
6268 * In order to build the NFA, we parse the input regexp twice:
6269 * 1. first pass to count size (so we can allocate space)
6270 * 2. second to emit code
6271 */
6272#ifdef ENABLE_LOG
6273 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006274 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006275
6276 if (f != NULL)
6277 {
6278 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6279 fclose(f);
6280 }
6281 }
6282#endif
6283
6284 /*
6285 * PASS 1
6286 * Count number of NFA states in "nstate". Do not build the NFA.
6287 */
6288 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006289
6290 /* Space for compiled regexp */
6291 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
6292 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6293 if (prog == NULL)
6294 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006295 state_ptr = prog->state;
6296
6297 /*
6298 * PASS 2
6299 * Build the NFA
6300 */
6301 prog->start = post2nfa(postfix, post_ptr, FALSE);
6302 if (prog->start == NULL)
6303 goto fail;
6304
6305 prog->regflags = regflags;
6306 prog->engine = &nfa_regengine;
6307 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006308 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006309 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006310 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006311
6312 prog->reganch = nfa_get_reganch(prog->start, 0);
6313 prog->regstart = nfa_get_regstart(prog->start, 0);
6314
Bram Moolenaar473de612013-06-08 18:19:48 +02006315 prog->match_text = nfa_get_match_text(prog->start);
6316
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006317#ifdef ENABLE_LOG
6318 nfa_postfix_dump(expr, OK);
6319 nfa_dump(prog);
6320#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006321#ifdef FEAT_SYN_HL
6322 /* Remember whether this pattern has any \z specials in it. */
6323 prog->reghasz = re_has_z;
6324#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006325#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006326 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006327 nfa_regengine.expr = NULL;
6328#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329
6330out:
6331 vim_free(post_start);
6332 post_start = post_ptr = post_end = NULL;
6333 state_ptr = NULL;
6334 return (regprog_T *)prog;
6335
6336fail:
6337 vim_free(prog);
6338 prog = NULL;
6339#ifdef ENABLE_LOG
6340 nfa_postfix_dump(expr, FAIL);
6341#endif
6342#ifdef DEBUG
6343 nfa_regengine.expr = NULL;
6344#endif
6345 goto out;
6346}
6347
Bram Moolenaar473de612013-06-08 18:19:48 +02006348/*
6349 * Free a compiled regexp program, returned by nfa_regcomp().
6350 */
6351 static void
6352nfa_regfree(prog)
6353 regprog_T *prog;
6354{
6355 if (prog != NULL)
6356 {
6357 vim_free(((nfa_regprog_T *)prog)->match_text);
6358#ifdef DEBUG
6359 vim_free(((nfa_regprog_T *)prog)->pattern);
6360#endif
6361 vim_free(prog);
6362 }
6363}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006364
6365/*
6366 * Match a regexp against a string.
6367 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6368 * Uses curbuf for line count and 'iskeyword'.
6369 *
6370 * Return TRUE if there is a match, FALSE if not.
6371 */
6372 static int
6373nfa_regexec(rmp, line, col)
6374 regmatch_T *rmp;
6375 char_u *line; /* string to match against */
6376 colnr_T col; /* column to start looking for match */
6377{
6378 reg_match = rmp;
6379 reg_mmatch = NULL;
6380 reg_maxline = 0;
6381 reg_line_lbr = FALSE;
6382 reg_buf = curbuf;
6383 reg_win = NULL;
6384 ireg_ic = rmp->rm_ic;
6385#ifdef FEAT_MBYTE
6386 ireg_icombine = FALSE;
6387#endif
6388 ireg_maxcol = 0;
6389 return (nfa_regexec_both(line, col) != 0);
6390}
6391
6392#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6393 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6394
6395static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6396
6397/*
6398 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6399 */
6400 static int
6401nfa_regexec_nl(rmp, line, col)
6402 regmatch_T *rmp;
6403 char_u *line; /* string to match against */
6404 colnr_T col; /* column to start looking for match */
6405{
6406 reg_match = rmp;
6407 reg_mmatch = NULL;
6408 reg_maxline = 0;
6409 reg_line_lbr = TRUE;
6410 reg_buf = curbuf;
6411 reg_win = NULL;
6412 ireg_ic = rmp->rm_ic;
6413#ifdef FEAT_MBYTE
6414 ireg_icombine = FALSE;
6415#endif
6416 ireg_maxcol = 0;
6417 return (nfa_regexec_both(line, col) != 0);
6418}
6419#endif
6420
6421
6422/*
6423 * Match a regexp against multiple lines.
6424 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6425 * Uses curbuf for line count and 'iskeyword'.
6426 *
6427 * Return zero if there is no match. Return number of lines contained in the
6428 * match otherwise.
6429 *
6430 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6431 *
6432 * ! Also NOTE : match may actually be in another line. e.g.:
6433 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6434 *
6435 * +-------------------------+
6436 * |a |
6437 * |b |
6438 * |c |
6439 * | |
6440 * +-------------------------+
6441 *
6442 * then nfa_regexec_multi() returns 3. while the original
6443 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6444 *
6445 * FIXME if this behavior is not compatible.
6446 */
6447 static long
6448nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6449 regmmatch_T *rmp;
6450 win_T *win; /* window in which to search or NULL */
6451 buf_T *buf; /* buffer in which to search */
6452 linenr_T lnum; /* nr of line to start looking for match */
6453 colnr_T col; /* column to start looking for match */
6454 proftime_T *tm UNUSED; /* timeout limit or NULL */
6455{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006456 reg_match = NULL;
6457 reg_mmatch = rmp;
6458 reg_buf = buf;
6459 reg_win = win;
6460 reg_firstlnum = lnum;
6461 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6462 reg_line_lbr = FALSE;
6463 ireg_ic = rmp->rmm_ic;
6464#ifdef FEAT_MBYTE
6465 ireg_icombine = FALSE;
6466#endif
6467 ireg_maxcol = rmp->rmm_maxcol;
6468
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006469 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006470}
6471
6472#ifdef DEBUG
6473# undef ENABLE_LOG
6474#endif