blob: df82ebf68f93ac9b486c38b3f14ea76444cf8904 [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;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003048 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003049 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{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003357 int result; /* NFA_PIM_*, see below */
3358 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003359 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003360 union
3361 {
3362 lpos_T pos;
3363 char_u *ptr;
3364 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003365};
3366
3367/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003368#define NFA_PIM_UNUSED 0 /* pim not used */
3369#define NFA_PIM_TODO 1 /* pim not done yet */
3370#define NFA_PIM_MATCH 2 /* pim executed, matches */
3371#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003372
3373
Bram Moolenaar963fee22013-05-26 21:47:28 +02003374/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003375typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376{
3377 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003378 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003379 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3380 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003381 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003382} nfa_thread_T;
3383
Bram Moolenaar963fee22013-05-26 21:47:28 +02003384/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385typedef struct
3386{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003387 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003388 int n; /* nr of states currently in "t" */
3389 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003390 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003391} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003392
Bram Moolenaar5714b802013-05-28 22:03:20 +02003393#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003394static void log_subsexpr __ARGS((regsubs_T *subs));
3395static void log_subexpr __ARGS((regsub_T *sub));
3396
3397 static void
3398log_subsexpr(subs)
3399 regsubs_T *subs;
3400{
3401 log_subexpr(&subs->norm);
3402# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003403 if (nfa_has_zsubexpr)
3404 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003405# endif
3406}
3407
Bram Moolenaar5714b802013-05-28 22:03:20 +02003408 static void
3409log_subexpr(sub)
3410 regsub_T *sub;
3411{
3412 int j;
3413
3414 for (j = 0; j < sub->in_use; j++)
3415 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003416 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003417 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003418 sub->list.multi[j].start.col,
3419 (int)sub->list.multi[j].start.lnum,
3420 sub->list.multi[j].end.col,
3421 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003422 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003423 {
3424 char *s = (char *)sub->list.line[j].start;
3425 char *e = (char *)sub->list.line[j].end;
3426
Bram Moolenaar87953742013-06-05 18:52:40 +02003427 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003428 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003429 s == NULL ? "NULL" : s,
3430 e == NULL ? "NULL" : e);
3431 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003432}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003433
3434 static char *
3435pim_info(nfa_pim_T *pim)
3436{
3437 static char buf[30];
3438
3439 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3440 buf[0] = NUL;
3441 else
3442 {
3443 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3444 : (int)(pim->end.ptr - reginput));
3445 }
3446 return buf;
3447}
3448
Bram Moolenaar5714b802013-05-28 22:03:20 +02003449#endif
3450
Bram Moolenaar963fee22013-05-26 21:47:28 +02003451/* Used during execution: whether a match has been found. */
3452static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003453
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003454static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003455static void clear_sub __ARGS((regsub_T *sub));
3456static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3457static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003458static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003459static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003460static int match_follows __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003461static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003462static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003463static 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 +02003464
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003465/*
3466 * Copy postponed invisible match info from "from" to "to".
3467 */
3468 static void
3469copy_pim(to, from)
3470 nfa_pim_T *to;
3471 nfa_pim_T *from;
3472{
3473 to->result = from->result;
3474 to->state = from->state;
3475 copy_sub(&to->subs.norm, &from->subs.norm);
3476#ifdef FEAT_SYN_HL
3477 if (nfa_has_zsubexpr)
3478 copy_sub(&to->subs.synt, &from->subs.synt);
3479#endif
3480 to->end = from->end;
3481}
3482
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003483 static void
3484clear_sub(sub)
3485 regsub_T *sub;
3486{
3487 if (REG_MULTI)
3488 /* Use 0xff to set lnum to -1 */
3489 vim_memset(sub->list.multi, 0xff,
3490 sizeof(struct multipos) * nfa_nsubexpr);
3491 else
3492 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3493 sub->in_use = 0;
3494}
3495
3496/*
3497 * Copy the submatches from "from" to "to".
3498 */
3499 static void
3500copy_sub(to, from)
3501 regsub_T *to;
3502 regsub_T *from;
3503{
3504 to->in_use = from->in_use;
3505 if (from->in_use > 0)
3506 {
3507 /* Copy the match start and end positions. */
3508 if (REG_MULTI)
3509 mch_memmove(&to->list.multi[0],
3510 &from->list.multi[0],
3511 sizeof(struct multipos) * from->in_use);
3512 else
3513 mch_memmove(&to->list.line[0],
3514 &from->list.line[0],
3515 sizeof(struct linepos) * from->in_use);
3516 }
3517}
3518
3519/*
3520 * Like copy_sub() but exclude the main match.
3521 */
3522 static void
3523copy_sub_off(to, from)
3524 regsub_T *to;
3525 regsub_T *from;
3526{
3527 if (to->in_use < from->in_use)
3528 to->in_use = from->in_use;
3529 if (from->in_use > 1)
3530 {
3531 /* Copy the match start and end positions. */
3532 if (REG_MULTI)
3533 mch_memmove(&to->list.multi[1],
3534 &from->list.multi[1],
3535 sizeof(struct multipos) * (from->in_use - 1));
3536 else
3537 mch_memmove(&to->list.line[1],
3538 &from->list.line[1],
3539 sizeof(struct linepos) * (from->in_use - 1));
3540 }
3541}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542
Bram Moolenaar428e9872013-05-30 17:05:39 +02003543/*
3544 * Return TRUE if "sub1" and "sub2" have the same positions.
3545 */
3546 static int
3547sub_equal(sub1, sub2)
3548 regsub_T *sub1;
3549 regsub_T *sub2;
3550{
3551 int i;
3552 int todo;
3553 linenr_T s1, e1;
3554 linenr_T s2, e2;
3555 char_u *sp1, *ep1;
3556 char_u *sp2, *ep2;
3557
3558 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3559 if (REG_MULTI)
3560 {
3561 for (i = 0; i < todo; ++i)
3562 {
3563 if (i < sub1->in_use)
3564 {
3565 s1 = sub1->list.multi[i].start.lnum;
3566 e1 = sub1->list.multi[i].end.lnum;
3567 }
3568 else
3569 {
3570 s1 = 0;
3571 e1 = 0;
3572 }
3573 if (i < sub2->in_use)
3574 {
3575 s2 = sub2->list.multi[i].start.lnum;
3576 e2 = sub2->list.multi[i].end.lnum;
3577 }
3578 else
3579 {
3580 s2 = 0;
3581 e2 = 0;
3582 }
3583 if (s1 != s2 || e1 != e2)
3584 return FALSE;
3585 if (s1 != 0 && sub1->list.multi[i].start.col
3586 != sub2->list.multi[i].start.col)
3587 return FALSE;
3588 if (e1 != 0 && sub1->list.multi[i].end.col
3589 != sub2->list.multi[i].end.col)
3590 return FALSE;
3591 }
3592 }
3593 else
3594 {
3595 for (i = 0; i < todo; ++i)
3596 {
3597 if (i < sub1->in_use)
3598 {
3599 sp1 = sub1->list.line[i].start;
3600 ep1 = sub1->list.line[i].end;
3601 }
3602 else
3603 {
3604 sp1 = NULL;
3605 ep1 = NULL;
3606 }
3607 if (i < sub2->in_use)
3608 {
3609 sp2 = sub2->list.line[i].start;
3610 ep2 = sub2->list.line[i].end;
3611 }
3612 else
3613 {
3614 sp2 = NULL;
3615 ep2 = NULL;
3616 }
3617 if (sp1 != sp2 || ep1 != ep2)
3618 return FALSE;
3619 }
3620 }
3621
3622 return TRUE;
3623}
3624
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003625#ifdef ENABLE_LOG
3626 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003627report_state(char *action,
3628 regsub_T *sub,
3629 nfa_state_T *state,
3630 int lid,
3631 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003632{
3633 int col;
3634
3635 if (sub->in_use <= 0)
3636 col = -1;
3637 else if (REG_MULTI)
3638 col = sub->list.multi[0].start.col;
3639 else
3640 col = (int)(sub->list.line[0].start - regline);
3641 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003642 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3643 action, abs(state->id), lid, state->c, code, col,
3644 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003645}
3646#endif
3647
Bram Moolenaar43e02982013-06-07 17:31:29 +02003648/*
3649 * Return TRUE if the same state is already in list "l" with the same
3650 * positions as "subs".
3651 */
3652 static int
3653has_state_with_pos(l, state, subs)
3654 nfa_list_T *l; /* runtime state list */
3655 nfa_state_T *state; /* state to update */
3656 regsubs_T *subs; /* pointers to subexpressions */
3657{
3658 nfa_thread_T *thread;
3659 int i;
3660
3661 for (i = 0; i < l->n; ++i)
3662 {
3663 thread = &l->t[i];
3664 if (thread->state->id == state->id
3665 && sub_equal(&thread->subs.norm, &subs->norm)
3666#ifdef FEAT_SYN_HL
3667 && (!nfa_has_zsubexpr ||
3668 sub_equal(&thread->subs.synt, &subs->synt))
3669#endif
3670 )
3671 return TRUE;
3672 }
3673 return FALSE;
3674}
3675
3676/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003677 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3678 */
3679 static int
3680match_follows(startstate, depth)
3681 nfa_state_T *startstate;
3682 int depth;
3683{
3684 nfa_state_T *state = startstate;
3685
3686 /* avoid too much recursion */
3687 if (depth > 10)
3688 return FALSE;
3689
3690 for (;;)
3691 {
3692 switch (state->c)
3693 {
3694 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003695 case NFA_MCLOSE:
3696 case NFA_END_INVISIBLE:
3697 case NFA_END_INVISIBLE_NEG:
3698 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003699 return TRUE;
3700
3701 case NFA_SPLIT:
3702 return match_follows(state->out, depth + 1)
3703 || match_follows(state->out1, depth + 1);
3704
3705 case NFA_START_INVISIBLE:
3706 case NFA_START_INVISIBLE_BEFORE:
3707 case NFA_START_INVISIBLE_NEG:
3708 case NFA_START_INVISIBLE_BEFORE_NEG:
3709 case NFA_COMPOSING:
3710 /* skip ahead to next state */
3711 state = state->out1->out;
3712 break;
3713
3714 case NFA_ANY:
3715 case NFA_IDENT:
3716 case NFA_SIDENT:
3717 case NFA_KWORD:
3718 case NFA_SKWORD:
3719 case NFA_FNAME:
3720 case NFA_SFNAME:
3721 case NFA_PRINT:
3722 case NFA_SPRINT:
3723 case NFA_WHITE:
3724 case NFA_NWHITE:
3725 case NFA_DIGIT:
3726 case NFA_NDIGIT:
3727 case NFA_HEX:
3728 case NFA_NHEX:
3729 case NFA_OCTAL:
3730 case NFA_NOCTAL:
3731 case NFA_WORD:
3732 case NFA_NWORD:
3733 case NFA_HEAD:
3734 case NFA_NHEAD:
3735 case NFA_ALPHA:
3736 case NFA_NALPHA:
3737 case NFA_LOWER:
3738 case NFA_NLOWER:
3739 case NFA_UPPER:
3740 case NFA_NUPPER:
3741 case NFA_START_COLL:
3742 case NFA_START_NEG_COLL:
3743 case NFA_NEWL:
3744 /* state will advance input */
3745 return FALSE;
3746
3747 default:
3748 if (state->c > 0)
3749 /* state will advance input */
3750 return FALSE;
3751
3752 /* Others: zero-width or possibly zero-width, might still find
3753 * a match at the same position, keep looking. */
3754 break;
3755 }
3756 state = state->out;
3757 }
3758 return FALSE;
3759}
3760
3761
3762/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003763 * Return TRUE if "state" is already in list "l".
3764 */
3765 static int
3766state_in_list(l, state, subs)
3767 nfa_list_T *l; /* runtime state list */
3768 nfa_state_T *state; /* state to update */
3769 regsubs_T *subs; /* pointers to subexpressions */
3770{
3771 if (state->lastlist[nfa_ll_index] == l->id)
3772 {
3773 if (!nfa_has_backref || has_state_with_pos(l, state, subs))
3774 return TRUE;
3775 }
3776 return FALSE;
3777}
3778
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003780addstate(l, state, subs, pim, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003781 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003782 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003783 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003784 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003785 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003787 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003788 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003789 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003790 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003791 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003792 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003793 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003794#ifdef ENABLE_LOG
3795 int did_print = FALSE;
3796#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003797
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798 switch (state->c)
3799 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 case NFA_NCLOSE:
3801 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003802 case NFA_MCLOSE1:
3803 case NFA_MCLOSE2:
3804 case NFA_MCLOSE3:
3805 case NFA_MCLOSE4:
3806 case NFA_MCLOSE5:
3807 case NFA_MCLOSE6:
3808 case NFA_MCLOSE7:
3809 case NFA_MCLOSE8:
3810 case NFA_MCLOSE9:
3811#ifdef FEAT_SYN_HL
3812 case NFA_ZCLOSE:
3813 case NFA_ZCLOSE1:
3814 case NFA_ZCLOSE2:
3815 case NFA_ZCLOSE3:
3816 case NFA_ZCLOSE4:
3817 case NFA_ZCLOSE5:
3818 case NFA_ZCLOSE6:
3819 case NFA_ZCLOSE7:
3820 case NFA_ZCLOSE8:
3821 case NFA_ZCLOSE9:
3822#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003823 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003824 case NFA_SPLIT:
3825 case NFA_NOPEN:
3826 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003827 /* These nodes are not added themselves but their "out" and/or
3828 * "out1" may be added below. */
3829 break;
3830
3831 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003832 case NFA_MOPEN1:
3833 case NFA_MOPEN2:
3834 case NFA_MOPEN3:
3835 case NFA_MOPEN4:
3836 case NFA_MOPEN5:
3837 case NFA_MOPEN6:
3838 case NFA_MOPEN7:
3839 case NFA_MOPEN8:
3840 case NFA_MOPEN9:
3841#ifdef FEAT_SYN_HL
3842 case NFA_ZOPEN:
3843 case NFA_ZOPEN1:
3844 case NFA_ZOPEN2:
3845 case NFA_ZOPEN3:
3846 case NFA_ZOPEN4:
3847 case NFA_ZOPEN5:
3848 case NFA_ZOPEN6:
3849 case NFA_ZOPEN7:
3850 case NFA_ZOPEN8:
3851 case NFA_ZOPEN9:
3852#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003853 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003854 /* These nodes do not need to be added, but we need to bail out
3855 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003856 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003857 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003858 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003859 break;
3860
Bram Moolenaar307aa162013-06-02 16:34:21 +02003861 case NFA_BOL:
3862 case NFA_BOF:
3863 /* "^" won't match past end-of-line, don't bother trying.
3864 * Except when we are going to the next line for a look-behind
3865 * match. */
3866 if (reginput > regline
3867 && (nfa_endp == NULL
3868 || !REG_MULTI
3869 || reglnum == nfa_endp->se_u.pos.lnum))
3870 goto skip_add;
3871 /* FALLTHROUGH */
3872
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003873 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003874 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003876 /* This state is already in the list, don't add it again,
3877 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003878 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003879 {
3880skip_add:
3881#ifdef ENABLE_LOG
3882 nfa_set_code(state->c);
3883 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3884 abs(state->id), l->id, state->c, code);
3885#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003886 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003887 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003888
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003889 /* Do not add the state again when it exists with the same
3890 * positions. */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003891 if (has_state_with_pos(l, state, subs))
3892 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003893 }
3894
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003895 /* When there are backreferences the number of states may be (a
3896 * lot) bigger than anticipated. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003897 if (nfa_has_backref && l->n == l->len)
3898 {
3899 int newlen = l->len * 3 / 2 + 50;
3900
3901 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3902 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003903 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003904
3905 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003906 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003907 thread = &l->t[l->n++];
3908 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003909 if (pim == NULL)
3910 thread->pim.result = NFA_PIM_UNUSED;
3911 else
3912 copy_pim(&thread->pim, pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003913 copy_sub(&thread->subs.norm, &subs->norm);
3914#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003915 if (nfa_has_zsubexpr)
3916 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003917#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003918#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003919 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003920 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003921#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922 }
3923
3924#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003925 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003926 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003927#endif
3928 switch (state->c)
3929 {
3930 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003931 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003932 break;
3933
3934 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003935 /* order matters here */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003936 addstate(l, state->out, subs, pim, off);
3937 addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003938 break;
3939
Bram Moolenaar5714b802013-05-28 22:03:20 +02003940 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003941 case NFA_NOPEN:
3942 case NFA_NCLOSE:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003943 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003944 break;
3945
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003946 case NFA_MOPEN:
3947 case NFA_MOPEN1:
3948 case NFA_MOPEN2:
3949 case NFA_MOPEN3:
3950 case NFA_MOPEN4:
3951 case NFA_MOPEN5:
3952 case NFA_MOPEN6:
3953 case NFA_MOPEN7:
3954 case NFA_MOPEN8:
3955 case NFA_MOPEN9:
3956#ifdef FEAT_SYN_HL
3957 case NFA_ZOPEN:
3958 case NFA_ZOPEN1:
3959 case NFA_ZOPEN2:
3960 case NFA_ZOPEN3:
3961 case NFA_ZOPEN4:
3962 case NFA_ZOPEN5:
3963 case NFA_ZOPEN6:
3964 case NFA_ZOPEN7:
3965 case NFA_ZOPEN8:
3966 case NFA_ZOPEN9:
3967#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003968 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003969 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003970 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003971 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003972 sub = &subs->norm;
3973 }
3974#ifdef FEAT_SYN_HL
3975 else if (state->c >= NFA_ZOPEN)
3976 {
3977 subidx = state->c - NFA_ZOPEN;
3978 sub = &subs->synt;
3979 }
3980#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003981 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003982 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003983 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003984 sub = &subs->norm;
3985 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003986
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003987 /* Set the position (with "off" added) in the subexpression. Save
3988 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003989 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003990 if (REG_MULTI)
3991 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003992 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003993 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003994 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003995 save_in_use = -1;
3996 }
3997 else
3998 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003999 save_in_use = sub->in_use;
4000 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004001 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004002 sub->list.multi[i].start.lnum = -1;
4003 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004004 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004005 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004006 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004007 if (off == -1)
4008 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004009 sub->list.multi[subidx].start.lnum = reglnum + 1;
4010 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004011 }
4012 else
4013 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004014 sub->list.multi[subidx].start.lnum = reglnum;
4015 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004016 (colnr_T)(reginput - regline + off);
4017 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018 }
4019 else
4020 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004021 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004022 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004023 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004024 save_in_use = -1;
4025 }
4026 else
4027 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004028 save_in_use = sub->in_use;
4029 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004030 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004031 sub->list.line[i].start = NULL;
4032 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004033 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004034 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004035 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004036 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004037 }
4038
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004039 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004040
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004041 if (save_in_use == -1)
4042 {
4043 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004044 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004045 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004046 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004047 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004049 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004050 break;
4051
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004052 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004053 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004054 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004055 /* Do not overwrite the position set by \ze. If no \ze
4056 * encountered end will be set in nfa_regtry(). */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004057 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004058 break;
4059 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004060 case NFA_MCLOSE1:
4061 case NFA_MCLOSE2:
4062 case NFA_MCLOSE3:
4063 case NFA_MCLOSE4:
4064 case NFA_MCLOSE5:
4065 case NFA_MCLOSE6:
4066 case NFA_MCLOSE7:
4067 case NFA_MCLOSE8:
4068 case NFA_MCLOSE9:
4069#ifdef FEAT_SYN_HL
4070 case NFA_ZCLOSE:
4071 case NFA_ZCLOSE1:
4072 case NFA_ZCLOSE2:
4073 case NFA_ZCLOSE3:
4074 case NFA_ZCLOSE4:
4075 case NFA_ZCLOSE5:
4076 case NFA_ZCLOSE6:
4077 case NFA_ZCLOSE7:
4078 case NFA_ZCLOSE8:
4079 case NFA_ZCLOSE9:
4080#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004081 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004082 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004083 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004084 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004085 sub = &subs->norm;
4086 }
4087#ifdef FEAT_SYN_HL
4088 else if (state->c >= NFA_ZCLOSE)
4089 {
4090 subidx = state->c - NFA_ZCLOSE;
4091 sub = &subs->synt;
4092 }
4093#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004094 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004095 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004096 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004097 sub = &subs->norm;
4098 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004099
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004100 /* We don't fill in gaps here, there must have been an MOPEN that
4101 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004102 save_in_use = sub->in_use;
4103 if (sub->in_use <= subidx)
4104 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004105 if (REG_MULTI)
4106 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004107 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004108 if (off == -1)
4109 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004110 sub->list.multi[subidx].end.lnum = reglnum + 1;
4111 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004112 }
4113 else
4114 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004115 sub->list.multi[subidx].end.lnum = reglnum;
4116 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004117 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004118 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004119 }
4120 else
4121 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004122 save_ptr = sub->list.line[subidx].end;
4123 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004124 }
4125
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004126 addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004127
4128 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004129 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004130 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004131 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004132 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004133 break;
4134 }
4135}
4136
4137/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004138 * Like addstate(), but the new state(s) are put at position "*ip".
4139 * Used for zero-width matches, next state to use is the added one.
4140 * This makes sure the order of states to be tried does not change, which
4141 * matters for alternatives.
4142 */
4143 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004144addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004145 nfa_list_T *l; /* runtime state list */
4146 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004147 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004148 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004149 int *ip;
4150{
4151 int tlen = l->n;
4152 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004153 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004154
4155 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004156 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004157
Bram Moolenaar4b417062013-05-25 20:19:50 +02004158 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004159 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004160 return;
4161
4162 /* re-order to put the new state at the current position */
4163 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004164 if (count == 1)
4165 {
4166 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004167 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004168 }
4169 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004170 {
4171 /* make space for new states, then move them from the
4172 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004173 mch_memmove(&(l->t[listidx + count]),
4174 &(l->t[listidx + 1]),
4175 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4176 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02004177 &(l->t[l->n - 1]),
4178 sizeof(nfa_thread_T) * count);
4179 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004180 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004181 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004182}
4183
4184/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004185 * Check character class "class" against current character c.
4186 */
4187 static int
4188check_char_class(class, c)
4189 int class;
4190 int c;
4191{
4192 switch (class)
4193 {
4194 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004195 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004196 return OK;
4197 break;
4198 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004199 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004200 return OK;
4201 break;
4202 case NFA_CLASS_BLANK:
4203 if (c == ' ' || c == '\t')
4204 return OK;
4205 break;
4206 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004207 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208 return OK;
4209 break;
4210 case NFA_CLASS_DIGIT:
4211 if (VIM_ISDIGIT(c))
4212 return OK;
4213 break;
4214 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004215 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004216 return OK;
4217 break;
4218 case NFA_CLASS_LOWER:
4219 if (MB_ISLOWER(c))
4220 return OK;
4221 break;
4222 case NFA_CLASS_PRINT:
4223 if (vim_isprintc(c))
4224 return OK;
4225 break;
4226 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004227 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004228 return OK;
4229 break;
4230 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004231 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004232 return OK;
4233 break;
4234 case NFA_CLASS_UPPER:
4235 if (MB_ISUPPER(c))
4236 return OK;
4237 break;
4238 case NFA_CLASS_XDIGIT:
4239 if (vim_isxdigit(c))
4240 return OK;
4241 break;
4242 case NFA_CLASS_TAB:
4243 if (c == '\t')
4244 return OK;
4245 break;
4246 case NFA_CLASS_RETURN:
4247 if (c == '\r')
4248 return OK;
4249 break;
4250 case NFA_CLASS_BACKSPACE:
4251 if (c == '\b')
4252 return OK;
4253 break;
4254 case NFA_CLASS_ESCAPE:
4255 if (c == '\033')
4256 return OK;
4257 break;
4258
4259 default:
4260 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004261 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4262 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004263 }
4264 return FAIL;
4265}
4266
Bram Moolenaar5714b802013-05-28 22:03:20 +02004267static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
4268
4269/*
4270 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004271 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004272 */
4273 static int
4274match_backref(sub, subidx, bytelen)
4275 regsub_T *sub; /* pointers to subexpressions */
4276 int subidx;
4277 int *bytelen; /* out: length of match in bytes */
4278{
4279 int len;
4280
4281 if (sub->in_use <= subidx)
4282 {
4283retempty:
4284 /* backref was not set, match an empty string */
4285 *bytelen = 0;
4286 return TRUE;
4287 }
4288
4289 if (REG_MULTI)
4290 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004291 if (sub->list.multi[subidx].start.lnum < 0
4292 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004293 goto retempty;
4294 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004295 len = sub->list.multi[subidx].end.col
4296 - sub->list.multi[subidx].start.col;
4297 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02004298 reginput, &len) == 0)
4299 {
4300 *bytelen = len;
4301 return TRUE;
4302 }
4303 }
4304 else
4305 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004306 if (sub->list.line[subidx].start == NULL
4307 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004308 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004309 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4310 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004311 {
4312 *bytelen = len;
4313 return TRUE;
4314 }
4315 }
4316 return FALSE;
4317}
4318
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004319#ifdef FEAT_SYN_HL
4320
4321static int match_zref __ARGS((int subidx, int *bytelen));
4322
4323/*
4324 * Check for a match with \z subexpression "subidx".
4325 * Return TRUE if it matches.
4326 */
4327 static int
4328match_zref(subidx, bytelen)
4329 int subidx;
4330 int *bytelen; /* out: length of match in bytes */
4331{
4332 int len;
4333
4334 cleanup_zsubexpr();
4335 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4336 {
4337 /* backref was not set, match an empty string */
4338 *bytelen = 0;
4339 return TRUE;
4340 }
4341
4342 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4343 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4344 {
4345 *bytelen = len;
4346 return TRUE;
4347 }
4348 return FALSE;
4349}
4350#endif
4351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004352/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004353 * Save list IDs for all NFA states of "prog" into "list".
4354 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004355 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356 */
4357 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004358nfa_save_listids(prog, list)
4359 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004360 int *list;
4361{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004362 int i;
4363 nfa_state_T *p;
4364
4365 /* Order in the list is reverse, it's a bit faster that way. */
4366 p = &prog->state[0];
4367 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004369 list[i] = p->lastlist[1];
4370 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004371 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004372 }
4373}
4374
4375/*
4376 * Restore list IDs from "list" to all NFA states.
4377 */
4378 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004379nfa_restore_listids(prog, list)
4380 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381 int *list;
4382{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004383 int i;
4384 nfa_state_T *p;
4385
4386 p = &prog->state[0];
4387 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004389 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004390 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004391 }
4392}
4393
Bram Moolenaar423532e2013-05-29 21:14:42 +02004394 static int
4395nfa_re_num_cmp(val, op, pos)
4396 long_u val;
4397 int op;
4398 long_u pos;
4399{
4400 if (op == 1) return pos > val;
4401 if (op == 2) return pos < val;
4402 return val == pos;
4403}
4404
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004405static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004406static 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 +02004407
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004409 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004410 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4411 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004412 */
4413 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004414recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004415 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004416 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004417 nfa_regprog_T *prog;
4418 regsubs_T *submatch;
4419 regsubs_T *m;
4420 int **listids;
4421{
4422 char_u *save_reginput = reginput;
4423 char_u *save_regline = regline;
4424 int save_reglnum = reglnum;
4425 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004426 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004427 save_se_T *save_nfa_endp = nfa_endp;
4428 save_se_T endpos;
4429 save_se_T *endposp = NULL;
4430 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004431 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004432
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004433 if (pim != NULL)
4434 {
4435 /* start at the position where the postponed match was */
4436 if (REG_MULTI)
4437 reginput = regline + pim->end.pos.col;
4438 else
4439 reginput = pim->end.ptr;
4440 }
4441
Bram Moolenaardecd9542013-06-07 16:31:50 +02004442 if (state->c == NFA_START_INVISIBLE_BEFORE
4443 || state->c == NFA_START_INVISIBLE_BEFORE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004444 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004445 /* The recursive match must end at the current position. When "pim" is
4446 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004447 endposp = &endpos;
4448 if (REG_MULTI)
4449 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004450 if (pim == NULL)
4451 {
4452 endpos.se_u.pos.col = (int)(reginput - regline);
4453 endpos.se_u.pos.lnum = reglnum;
4454 }
4455 else
4456 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004457 }
4458 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004459 {
4460 if (pim == NULL)
4461 endpos.se_u.ptr = reginput;
4462 else
4463 endpos.se_u.ptr = pim->end.ptr;
4464 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004465
4466 /* Go back the specified number of bytes, or as far as the
4467 * start of the previous line, to try matching "\@<=" or
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004468 * not matching "\@<!". This is very ineffecient, limit the number of
4469 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004470 if (state->val <= 0)
4471 {
4472 if (REG_MULTI)
4473 {
4474 regline = reg_getline(--reglnum);
4475 if (regline == NULL)
4476 /* can't go before the first line */
4477 regline = reg_getline(++reglnum);
4478 }
4479 reginput = regline;
4480 }
4481 else
4482 {
4483 if (REG_MULTI && (int)(reginput - regline) < state->val)
4484 {
4485 /* Not enough bytes in this line, go to end of
4486 * previous line. */
4487 regline = reg_getline(--reglnum);
4488 if (regline == NULL)
4489 {
4490 /* can't go before the first line */
4491 regline = reg_getline(++reglnum);
4492 reginput = regline;
4493 }
4494 else
4495 reginput = regline + STRLEN(regline);
4496 }
4497 if ((int)(reginput - regline) >= state->val)
4498 {
4499 reginput -= state->val;
4500#ifdef FEAT_MBYTE
4501 if (has_mbyte)
4502 reginput -= mb_head_off(regline, reginput);
4503#endif
4504 }
4505 else
4506 reginput = regline;
4507 }
4508 }
4509
Bram Moolenaarf46da702013-06-02 22:37:42 +02004510#ifdef ENABLE_LOG
4511 if (log_fd != stderr)
4512 fclose(log_fd);
4513 log_fd = NULL;
4514#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004515 /* Have to clear the lastlist field of the NFA nodes, so that
4516 * nfa_regmatch() and addstate() can run properly after recursion. */
4517 if (nfa_ll_index == 1)
4518 {
4519 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4520 * values and clear them. */
4521 if (*listids == NULL)
4522 {
4523 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4524 if (*listids == NULL)
4525 {
4526 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4527 return 0;
4528 }
4529 }
4530 nfa_save_listids(prog, *listids);
4531 need_restore = TRUE;
4532 /* any value of nfa_listid will do */
4533 }
4534 else
4535 {
4536 /* First recursive nfa_regmatch() call, switch to the second lastlist
4537 * entry. Make sure nfa_listid is different from a previous recursive
4538 * call, because some states may still have this ID. */
4539 ++nfa_ll_index;
4540 if (nfa_listid <= nfa_alt_listid)
4541 nfa_listid = nfa_alt_listid;
4542 }
4543
4544 /* Call nfa_regmatch() to check if the current concat matches at this
4545 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004546 nfa_endp = endposp;
4547 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004548
4549 if (need_restore)
4550 nfa_restore_listids(prog, *listids);
4551 else
4552 {
4553 --nfa_ll_index;
4554 nfa_alt_listid = nfa_listid;
4555 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004556
4557 /* restore position in input text */
4558 reginput = save_reginput;
4559 regline = save_regline;
4560 reglnum = save_reglnum;
4561 nfa_match = save_nfa_match;
4562 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004563 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004564
4565#ifdef ENABLE_LOG
4566 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4567 if (log_fd != NULL)
4568 {
4569 fprintf(log_fd, "****************************\n");
4570 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4571 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4572 fprintf(log_fd, "****************************\n");
4573 }
4574 else
4575 {
4576 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4577 log_fd = stderr;
4578 }
4579#endif
4580
4581 return result;
4582}
4583
Bram Moolenaara2d95102013-06-04 14:23:05 +02004584static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004585static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004586static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004587
4588/*
4589 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004590 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004591 * NFA_ANY: 1
4592 * specific character: 99
4593 */
4594 static int
4595failure_chance(state, depth)
4596 nfa_state_T *state;
4597 int depth;
4598{
4599 int c = state->c;
4600 int l, r;
4601
4602 /* detect looping */
4603 if (depth > 4)
4604 return 1;
4605
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004606 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004607 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004608 case NFA_SPLIT:
4609 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4610 /* avoid recursive stuff */
4611 return 1;
4612 /* two alternatives, use the lowest failure chance */
4613 l = failure_chance(state->out, depth + 1);
4614 r = failure_chance(state->out1, depth + 1);
4615 return l < r ? l : r;
4616
4617 case NFA_ANY:
4618 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004619 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004620
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004621 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004622 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004623 /* empty match works always */
4624 return 0;
4625
4626 case NFA_BOL:
4627 case NFA_EOL:
4628 case NFA_BOF:
4629 case NFA_EOF:
4630 case NFA_NEWL:
4631 return 99;
4632
4633 case NFA_BOW:
4634 case NFA_EOW:
4635 return 90;
4636
4637 case NFA_MOPEN:
4638 case NFA_MOPEN1:
4639 case NFA_MOPEN2:
4640 case NFA_MOPEN3:
4641 case NFA_MOPEN4:
4642 case NFA_MOPEN5:
4643 case NFA_MOPEN6:
4644 case NFA_MOPEN7:
4645 case NFA_MOPEN8:
4646 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004647#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004648 case NFA_ZOPEN:
4649 case NFA_ZOPEN1:
4650 case NFA_ZOPEN2:
4651 case NFA_ZOPEN3:
4652 case NFA_ZOPEN4:
4653 case NFA_ZOPEN5:
4654 case NFA_ZOPEN6:
4655 case NFA_ZOPEN7:
4656 case NFA_ZOPEN8:
4657 case NFA_ZOPEN9:
4658 case NFA_ZCLOSE:
4659 case NFA_ZCLOSE1:
4660 case NFA_ZCLOSE2:
4661 case NFA_ZCLOSE3:
4662 case NFA_ZCLOSE4:
4663 case NFA_ZCLOSE5:
4664 case NFA_ZCLOSE6:
4665 case NFA_ZCLOSE7:
4666 case NFA_ZCLOSE8:
4667 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004668#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004669 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004670 case NFA_MCLOSE1:
4671 case NFA_MCLOSE2:
4672 case NFA_MCLOSE3:
4673 case NFA_MCLOSE4:
4674 case NFA_MCLOSE5:
4675 case NFA_MCLOSE6:
4676 case NFA_MCLOSE7:
4677 case NFA_MCLOSE8:
4678 case NFA_MCLOSE9:
4679 case NFA_NCLOSE:
4680 return failure_chance(state->out, depth + 1);
4681
4682 case NFA_BACKREF1:
4683 case NFA_BACKREF2:
4684 case NFA_BACKREF3:
4685 case NFA_BACKREF4:
4686 case NFA_BACKREF5:
4687 case NFA_BACKREF6:
4688 case NFA_BACKREF7:
4689 case NFA_BACKREF8:
4690 case NFA_BACKREF9:
4691#ifdef FEAT_SYN_HL
4692 case NFA_ZREF1:
4693 case NFA_ZREF2:
4694 case NFA_ZREF3:
4695 case NFA_ZREF4:
4696 case NFA_ZREF5:
4697 case NFA_ZREF6:
4698 case NFA_ZREF7:
4699 case NFA_ZREF8:
4700 case NFA_ZREF9:
4701#endif
4702 /* backreferences don't match in many places */
4703 return 94;
4704
4705 case NFA_LNUM_GT:
4706 case NFA_LNUM_LT:
4707 case NFA_COL_GT:
4708 case NFA_COL_LT:
4709 case NFA_VCOL_GT:
4710 case NFA_VCOL_LT:
4711 case NFA_MARK_GT:
4712 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004713 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004714 /* before/after positions don't match very often */
4715 return 85;
4716
4717 case NFA_LNUM:
4718 return 90;
4719
4720 case NFA_CURSOR:
4721 case NFA_COL:
4722 case NFA_VCOL:
4723 case NFA_MARK:
4724 /* specific positions rarely match */
4725 return 98;
4726
4727 case NFA_COMPOSING:
4728 return 95;
4729
4730 default:
4731 if (c > 0)
4732 /* character match fails often */
4733 return 95;
4734 }
4735
4736 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004737 return 50;
4738}
4739
Bram Moolenaarf46da702013-06-02 22:37:42 +02004740/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004741 * Skip until the char "c" we know a match must start with.
4742 */
4743 static int
4744skip_to_start(c, colp)
4745 int c;
4746 colnr_T *colp;
4747{
4748 char_u *s;
4749
4750 /* Used often, do some work to avoid call overhead. */
4751 if (!ireg_ic
4752#ifdef FEAT_MBYTE
4753 && !has_mbyte
4754#endif
4755 )
4756 s = vim_strbyte(regline + *colp, c);
4757 else
4758 s = cstrchr(regline + *colp, c);
4759 if (s == NULL)
4760 return FAIL;
4761 *colp = (int)(s - regline);
4762 return OK;
4763}
4764
4765/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004766 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004767 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004768 * Returns zero for no match, 1 for a match.
4769 */
4770 static long
4771find_match_text(startcol, regstart, match_text)
4772 colnr_T startcol;
4773 int regstart;
4774 char_u *match_text;
4775{
4776 colnr_T col = startcol;
4777 int c1, c2;
4778 int len1, len2;
4779 int match;
4780
4781 for (;;)
4782 {
4783 match = TRUE;
4784 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4785 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4786 {
4787 c1 = PTR2CHAR(match_text + len1);
4788 c2 = PTR2CHAR(regline + col + len2);
4789 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4790 {
4791 match = FALSE;
4792 break;
4793 }
4794 len2 += MB_CHAR2LEN(c2);
4795 }
4796 if (match
4797#ifdef FEAT_MBYTE
4798 /* check that no composing char follows */
4799 && !(enc_utf8
4800 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4801#endif
4802 )
4803 {
4804 cleanup_subexpr();
4805 if (REG_MULTI)
4806 {
4807 reg_startpos[0].lnum = reglnum;
4808 reg_startpos[0].col = col;
4809 reg_endpos[0].lnum = reglnum;
4810 reg_endpos[0].col = col + len2;
4811 }
4812 else
4813 {
4814 reg_startp[0] = regline + col;
4815 reg_endp[0] = regline + col + len2;
4816 }
4817 return 1L;
4818 }
4819
4820 /* Try finding regstart after the current match. */
4821 col += MB_CHAR2LEN(regstart); /* skip regstart */
4822 if (skip_to_start(regstart, &col) == FAIL)
4823 break;
4824 }
4825 return 0L;
4826}
4827
4828/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004829 * Main matching routine.
4830 *
4831 * Run NFA to determine whether it matches reginput.
4832 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004833 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004834 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004835 * Return TRUE if there is a match, FALSE otherwise.
4836 * Note: Caller must ensure that: start != NULL.
4837 */
4838 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004839nfa_regmatch(prog, start, submatch, m)
4840 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004841 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004842 regsubs_T *submatch;
4843 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004844{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004845 int result;
4846 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004847 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004848 int go_to_nextline = FALSE;
4849 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004850 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004851 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004852 nfa_list_T *thislist;
4853 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004854 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004855 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004856 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004857 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004858 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004859 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004860#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004861 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004862
4863 if (debug == NULL)
4864 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004865 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 return FALSE;
4867 }
4868#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004869 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004871 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004872 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004873 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004874 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004875 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004876 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004877 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879
4880#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004881 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004882 if (log_fd != NULL)
4883 {
4884 fprintf(log_fd, "**********************************\n");
4885 nfa_set_code(start->c);
4886 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4887 abs(start->id), code);
4888 fprintf(log_fd, "**********************************\n");
4889 }
4890 else
4891 {
4892 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4893 log_fd = stderr;
4894 }
4895#endif
4896
4897 thislist = &list[0];
4898 thislist->n = 0;
4899 nextlist = &list[1];
4900 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004902 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004903#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004904 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004905
4906 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4907 * it's the first MOPEN. */
4908 if (toplevel)
4909 {
4910 if (REG_MULTI)
4911 {
4912 m->norm.list.multi[0].start.lnum = reglnum;
4913 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4914 }
4915 else
4916 m->norm.list.line[0].start = reginput;
4917 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004918 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004919 }
4920 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004921 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004923#define ADD_STATE_IF_MATCH(state) \
4924 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004925 add_state = state->out; \
4926 add_off = clen; \
4927 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004928
4929 /*
4930 * Run for each character.
4931 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004932 for (;;)
4933 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004934 int curc;
4935 int clen;
4936
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004937#ifdef FEAT_MBYTE
4938 if (has_mbyte)
4939 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004940 curc = (*mb_ptr2char)(reginput);
4941 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004942 }
4943 else
4944#endif
4945 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004946 curc = *reginput;
4947 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004948 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004949 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004950 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004951 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004952 go_to_nextline = FALSE;
4953 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004954
4955 /* swap lists */
4956 thislist = &list[flag];
4957 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004958 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004959 ++nfa_listid;
4960 thislist->id = nfa_listid;
4961 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004962
4963#ifdef ENABLE_LOG
4964 fprintf(log_fd, "------------------------------------------\n");
4965 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004966 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004967 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004968 {
4969 int i;
4970
4971 for (i = 0; i < thislist->n; i++)
4972 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4973 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004974 fprintf(log_fd, "\n");
4975#endif
4976
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004977#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004978 fprintf(debug, "\n-------------------\n");
4979#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004980 /*
4981 * If the state lists are empty we can stop.
4982 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004983 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004984 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004985
4986 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004987 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004988 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004989 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004990
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004991#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004992 nfa_set_code(t->state->c);
4993 fprintf(debug, "%s, ", code);
4994#endif
4995#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004996 {
4997 int col;
4998
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004999 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005000 col = -1;
5001 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005002 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005003 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005004 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005005 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005006 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5007 abs(t->state->id), (int)t->state->c, code, col,
5008 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005009 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005010#endif
5011
5012 /*
5013 * Handle the possible codes of the current state.
5014 * The most important is NFA_MATCH.
5015 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005016 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005017 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005018 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005019 switch (t->state->c)
5020 {
5021 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005022 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005023 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005024 copy_sub(&submatch->norm, &t->subs.norm);
5025#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005026 if (nfa_has_zsubexpr)
5027 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005028#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005030 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005031#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005032 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005033 * states at this position. When the list of states is going
5034 * to be empty quit without advancing, so that "reginput" is
5035 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005036 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005037 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005038 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005039 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005040
5041 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005042 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005043 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005044 /*
5045 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005046 * NFA_START_INVISIBLE_BEFORE node.
5047 * They surround a zero-width group, used with "\@=", "\&",
5048 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005049 * If we got here, it means that the current "invisible" group
5050 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005051 * nfa_regmatch(). For a look-behind match only when it ends
5052 * in the position in "nfa_endp".
5053 * Submatches are stored in *m, and used in the parent call.
5054 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005055#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005056 if (nfa_endp != NULL)
5057 {
5058 if (REG_MULTI)
5059 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5060 (int)reglnum,
5061 (int)nfa_endp->se_u.pos.lnum,
5062 (int)(reginput - regline),
5063 nfa_endp->se_u.pos.col);
5064 else
5065 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5066 (int)(reginput - regline),
5067 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005069#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005070 /* If "nfa_endp" is set it's only a match if it ends at
5071 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005072 if (nfa_endp != NULL && (REG_MULTI
5073 ? (reglnum != nfa_endp->se_u.pos.lnum
5074 || (int)(reginput - regline)
5075 != nfa_endp->se_u.pos.col)
5076 : reginput != nfa_endp->se_u.ptr))
5077 break;
5078
5079 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005080 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005081 {
5082 copy_sub(&m->norm, &t->subs.norm);
5083#ifdef FEAT_SYN_HL
5084 if (nfa_has_zsubexpr)
5085 copy_sub(&m->synt, &t->subs.synt);
5086#endif
5087 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005088#ifdef ENABLE_LOG
5089 fprintf(log_fd, "Match found:\n");
5090 log_subsexpr(m);
5091#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005093 break;
5094
5095 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005096 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005097 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005098 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005099 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005100 int directly = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005101
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005102#ifdef ENABLE_LOG
5103 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5104 failure_chance(t->state->out, 0),
5105 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005106#endif
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005107 /* Do it directly when what follows is possibly the end of
5108 * the match.
5109 * Do it directly if there already is a PIM.
5110 * Postpone when the invisible match is expensive or has a
5111 * lower chance of failing. */
5112 if (match_follows(t->state->out1->out, 0)
5113 || t->pim.result != NFA_PIM_UNUSED)
5114 directly = TRUE;
5115 else
5116 {
5117 int ch_invisible = failure_chance(t->state->out, 0);
5118 int ch_follows = failure_chance(t->state->out1->out, 0);
5119
5120 if (t->state->c == NFA_START_INVISIBLE_BEFORE
5121 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG)
5122 {
5123 /* "before" matches are very expensive when
5124 * unbounded, always prefer what follows then,
5125 * unless what follows will always match.
5126 * Otherwise strongly prefer what follows. */
5127 if (t->state->val <= 0 && ch_follows > 0)
5128 directly = FALSE;
5129 else
5130 directly = ch_follows * 10 < ch_invisible;
5131 }
5132 else
5133 {
5134 /* normal invisible, first do the one with the
5135 * highest failure chance */
5136 directly = ch_follows < ch_invisible;
5137 }
5138 }
5139 if (directly)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005140 {
5141 /*
5142 * First try matching the invisible match, then what
5143 * follows.
5144 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005145 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005146 submatch, m, &listids);
5147
Bram Moolenaardecd9542013-06-07 16:31:50 +02005148 /* for \@! and \@<! it is a match when the result is
5149 * FALSE */
5150 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
5151 || t->state->c
5152 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005153 {
5154 /* Copy submatch info from the recursive call */
5155 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005156#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005157 if (nfa_has_zsubexpr)
5158 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005159#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005160
Bram Moolenaara2d95102013-06-04 14:23:05 +02005161 /* t->state->out1 is the corresponding
5162 * END_INVISIBLE node; Add its out to the current
5163 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005164 add_here = TRUE;
5165 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005166 }
5167 }
5168 else
5169 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005170 nfa_pim_T pim;
5171
Bram Moolenaara2d95102013-06-04 14:23:05 +02005172 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005173 * First try matching what follows. Only if a match
5174 * is found verify the invisible match matches. Add a
5175 * nfa_pim_T to the following states, it contains info
5176 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005177 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005178 pim.state = t->state;
5179 pim.result = NFA_PIM_TODO;
5180 pim.subs.norm.in_use = 0;
5181#ifdef FEAT_SYN_HL
5182 pim.subs.synt.in_use = 0;
5183#endif
5184 if (REG_MULTI)
5185 {
5186 pim.end.pos.col = (int)(reginput - regline);
5187 pim.end.pos.lnum = reglnum;
5188 }
5189 else
5190 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005191
5192 /* t->state->out1 is the corresponding END_INVISIBLE
5193 * node; Add its out to the current list (zero-width
5194 * match). */
5195 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005196 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005197 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005198 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005199 break;
5200
Bram Moolenaar87953742013-06-05 18:52:40 +02005201 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005202 {
5203 nfa_state_T *skip = NULL;
5204#ifdef ENABLE_LOG
5205 int skip_lid = 0;
5206#endif
5207
5208 /* There is no point in trying to match the pattern if the
5209 * output state is not going to be added to the list. */
5210 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5211 {
5212 skip = t->state->out1->out;
5213#ifdef ENABLE_LOG
5214 skip_lid = nextlist->id;
5215#endif
5216 }
5217 else if (state_in_list(nextlist,
5218 t->state->out1->out->out, &t->subs))
5219 {
5220 skip = t->state->out1->out->out;
5221#ifdef ENABLE_LOG
5222 skip_lid = nextlist->id;
5223#endif
5224 }
5225 else if(state_in_list(thislist,
5226 t->state->out1->out->out, &t->subs))
5227 {
5228 skip = t->state->out1->out->out;
5229#ifdef ENABLE_LOG
5230 skip_lid = thislist->id;
5231#endif
5232 }
5233 if (skip != NULL)
5234 {
5235#ifdef ENABLE_LOG
5236 nfa_set_code(skip->c);
5237 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5238 abs(skip->id), skip_lid, skip->c, code);
5239#endif
5240 break;
5241 }
5242
Bram Moolenaar87953742013-06-05 18:52:40 +02005243 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005244 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005245 submatch, m, &listids);
5246 if (result)
5247 {
5248 int bytelen;
5249
5250#ifdef ENABLE_LOG
5251 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5252 log_subsexpr(m);
5253#endif
5254 /* Copy submatch info from the recursive call */
5255 copy_sub_off(&t->subs.norm, &m->norm);
5256#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005257 if (nfa_has_zsubexpr)
5258 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005259#endif
5260 /* Now we need to skip over the matched text and then
5261 * continue with what follows. */
5262 if (REG_MULTI)
5263 /* TODO: multi-line match */
5264 bytelen = m->norm.list.multi[0].end.col
5265 - (int)(reginput - regline);
5266 else
5267 bytelen = (int)(m->norm.list.line[0].end - reginput);
5268
5269#ifdef ENABLE_LOG
5270 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5271#endif
5272 if (bytelen == 0)
5273 {
5274 /* empty match, output of corresponding
5275 * NFA_END_PATTERN/NFA_SKIP to be used at current
5276 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005277 add_here = TRUE;
5278 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005279 }
5280 else if (bytelen <= clen)
5281 {
5282 /* match current character, output of corresponding
5283 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005284 add_state = t->state->out1->out->out;
5285 add_off = clen;
5286 }
5287 else
5288 {
5289 /* skip over the matched characters, set character
5290 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005291 add_state = t->state->out1->out;
5292 add_off = bytelen;
5293 add_count = bytelen - clen;
5294 }
5295 }
5296 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005297 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005298
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005299 case NFA_BOL:
5300 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005301 {
5302 add_here = TRUE;
5303 add_state = t->state->out;
5304 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005305 break;
5306
5307 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005308 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005309 {
5310 add_here = TRUE;
5311 add_state = t->state->out;
5312 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005313 break;
5314
5315 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005316 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005317
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005318 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005319 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320#ifdef FEAT_MBYTE
5321 else if (has_mbyte)
5322 {
5323 int this_class;
5324
5325 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005326 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005327 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005328 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005329 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005330 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005331 }
5332#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005333 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005334 || (reginput > regline
5335 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005336 result = FALSE;
5337 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005338 {
5339 add_here = TRUE;
5340 add_state = t->state->out;
5341 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005342 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005343
5344 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005345 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005346 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005347 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005348#ifdef FEAT_MBYTE
5349 else if (has_mbyte)
5350 {
5351 int this_class, prev_class;
5352
5353 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005354 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005355 prev_class = reg_prev_class();
5356 if (this_class == prev_class
5357 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005358 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005359 }
5360#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005361 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005362 || (reginput[0] != NUL
5363 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005364 result = FALSE;
5365 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005366 {
5367 add_here = TRUE;
5368 add_state = t->state->out;
5369 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005370 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005371
Bram Moolenaar4b780632013-05-31 22:14:52 +02005372 case NFA_BOF:
5373 if (reglnum == 0 && reginput == regline
5374 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005375 {
5376 add_here = TRUE;
5377 add_state = t->state->out;
5378 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005379 break;
5380
5381 case NFA_EOF:
5382 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005383 {
5384 add_here = TRUE;
5385 add_state = t->state->out;
5386 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005387 break;
5388
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005389#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005390 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005391 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005392 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005393 int len = 0;
5394 nfa_state_T *end;
5395 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005396 int cchars[MAX_MCO];
5397 int ccount = 0;
5398 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005399
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005400 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005401 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005402 if (utf_iscomposing(sta->c))
5403 {
5404 /* Only match composing character(s), ignore base
5405 * character. Used for ".{composing}" and "{composing}"
5406 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005407 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005408 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005409 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005411 /* If \Z was present, then ignore composing characters.
5412 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005413 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005414 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005415 else
5416 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005417 while (sta->c != NFA_END_COMPOSING)
5418 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005419 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005420
5421 /* Check base character matches first, unless ignored. */
5422 else if (len > 0 || mc == sta->c)
5423 {
5424 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005425 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005426 len += mb_char2len(mc);
5427 sta = sta->out;
5428 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005429
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005430 /* We don't care about the order of composing characters.
5431 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005432 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005433 {
5434 mc = mb_ptr2char(reginput + len);
5435 cchars[ccount++] = mc;
5436 len += mb_char2len(mc);
5437 if (ccount == MAX_MCO)
5438 break;
5439 }
5440
5441 /* Check that each composing char in the pattern matches a
5442 * composing char in the text. We do not check if all
5443 * composing chars are matched. */
5444 result = OK;
5445 while (sta->c != NFA_END_COMPOSING)
5446 {
5447 for (j = 0; j < ccount; ++j)
5448 if (cchars[j] == sta->c)
5449 break;
5450 if (j == ccount)
5451 {
5452 result = FAIL;
5453 break;
5454 }
5455 sta = sta->out;
5456 }
5457 }
5458 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005459 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005460
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005461 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005462 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005463 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005464 }
5465#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005466
5467 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005468 if (curc == NUL && !reg_line_lbr && REG_MULTI
5469 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005470 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005471 go_to_nextline = TRUE;
5472 /* Pass -1 for the offset, which means taking the position
5473 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005474 add_state = t->state->out;
5475 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005477 else if (curc == '\n' && reg_line_lbr)
5478 {
5479 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005480 add_state = t->state->out;
5481 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005482 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005483 break;
5484
Bram Moolenaar417bad22013-06-07 14:08:30 +02005485 case NFA_START_COLL:
5486 case NFA_START_NEG_COLL:
5487 {
5488 /* What follows is a list of characters, until NFA_END_COLL.
5489 * One of them must match or none of them must match. */
5490 nfa_state_T *state;
5491 int result_if_matched;
5492 int c1, c2;
5493
5494 /* Never match EOL. If it's part of the collection it is added
5495 * as a separate state with an OR. */
5496 if (curc == NUL)
5497 break;
5498
5499 state = t->state->out;
5500 result_if_matched = (t->state->c == NFA_START_COLL);
5501 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005502 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005503 if (state->c == NFA_END_COLL)
5504 {
5505 result = !result_if_matched;
5506 break;
5507 }
5508 if (state->c == NFA_RANGE_MIN)
5509 {
5510 c1 = state->val;
5511 state = state->out; /* advance to NFA_RANGE_MAX */
5512 c2 = state->val;
5513#ifdef ENABLE_LOG
5514 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5515 curc, c1, c2);
5516#endif
5517 if (curc >= c1 && curc <= c2)
5518 {
5519 result = result_if_matched;
5520 break;
5521 }
5522 if (ireg_ic)
5523 {
5524 int curc_low = MB_TOLOWER(curc);
5525 int done = FALSE;
5526
5527 for ( ; c1 <= c2; ++c1)
5528 if (MB_TOLOWER(c1) == curc_low)
5529 {
5530 result = result_if_matched;
5531 done = TRUE;
5532 break;
5533 }
5534 if (done)
5535 break;
5536 }
5537 }
5538 else if (state->c < 0 ? check_char_class(state->c, curc)
5539 : (curc == state->c
5540 || (ireg_ic && MB_TOLOWER(curc)
5541 == MB_TOLOWER(state->c))))
5542 {
5543 result = result_if_matched;
5544 break;
5545 }
5546 state = state->out;
5547 }
5548 if (result)
5549 {
5550 /* next state is in out of the NFA_END_COLL, out1 of
5551 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005552 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005553 add_off = clen;
5554 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005555 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005556 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557
5558 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005559 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005560 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005561 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005562 add_state = t->state->out;
5563 add_off = clen;
5564 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565 break;
5566
5567 /*
5568 * Character classes like \a for alpha, \d for digit etc.
5569 */
5570 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005571 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005572 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005573 break;
5574
5575 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005576 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005577 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578 break;
5579
5580 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005581 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005582 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005583 break;
5584
5585 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005586 result = !VIM_ISDIGIT(curc)
5587 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005588 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005589 break;
5590
5591 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005592 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005593 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005594 break;
5595
5596 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005597 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005598 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599 break;
5600
5601 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005602 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005603 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604 break;
5605
5606 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005607 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005608 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005609 break;
5610
5611 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005612 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005613 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614 break;
5615
5616 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005617 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005618 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005619 break;
5620
5621 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005622 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005623 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624 break;
5625
5626 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005627 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005628 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005629 break;
5630
5631 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005632 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005633 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005634 break;
5635
5636 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005637 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005638 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005639 break;
5640
5641 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005642 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005643 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644 break;
5645
5646 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005647 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005648 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005649 break;
5650
5651 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005652 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005653 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005654 break;
5655
5656 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005657 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005658 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005659 break;
5660
5661 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005662 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005663 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005664 break;
5665
5666 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005667 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005668 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669 break;
5670
5671 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005673 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674 break;
5675
5676 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005677 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005678 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005679 break;
5680
5681 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005682 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005683 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005684 break;
5685
5686 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005687 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005688 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005689 break;
5690
5691 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005692 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005693 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005694 break;
5695
5696 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005697 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005698 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005699 break;
5700
Bram Moolenaar5714b802013-05-28 22:03:20 +02005701 case NFA_BACKREF1:
5702 case NFA_BACKREF2:
5703 case NFA_BACKREF3:
5704 case NFA_BACKREF4:
5705 case NFA_BACKREF5:
5706 case NFA_BACKREF6:
5707 case NFA_BACKREF7:
5708 case NFA_BACKREF8:
5709 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005710#ifdef FEAT_SYN_HL
5711 case NFA_ZREF1:
5712 case NFA_ZREF2:
5713 case NFA_ZREF3:
5714 case NFA_ZREF4:
5715 case NFA_ZREF5:
5716 case NFA_ZREF6:
5717 case NFA_ZREF7:
5718 case NFA_ZREF8:
5719 case NFA_ZREF9:
5720#endif
5721 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005722 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005723 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005724 int bytelen;
5725
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005726 if (t->state->c <= NFA_BACKREF9)
5727 {
5728 subidx = t->state->c - NFA_BACKREF1 + 1;
5729 result = match_backref(&t->subs.norm, subidx, &bytelen);
5730 }
5731#ifdef FEAT_SYN_HL
5732 else
5733 {
5734 subidx = t->state->c - NFA_ZREF1 + 1;
5735 result = match_zref(subidx, &bytelen);
5736 }
5737#endif
5738
Bram Moolenaar5714b802013-05-28 22:03:20 +02005739 if (result)
5740 {
5741 if (bytelen == 0)
5742 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005743 /* empty match always works, output of NFA_SKIP to be
5744 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005745 add_here = TRUE;
5746 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005747 }
5748 else if (bytelen <= clen)
5749 {
5750 /* match current character, jump ahead to out of
5751 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005752 add_state = t->state->out->out;
5753 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005754 }
5755 else
5756 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005757 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005758 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005759 add_state = t->state->out;
5760 add_off = bytelen;
5761 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005762 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005763 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005764 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005765 }
5766 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005767 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005768 if (t->count - clen <= 0)
5769 {
5770 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005771 add_state = t->state->out;
5772 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005773 }
5774 else
5775 {
5776 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005777 add_state = t->state;
5778 add_off = 0;
5779 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005780 }
5781 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005782
Bram Moolenaar423532e2013-05-29 21:14:42 +02005783 case NFA_LNUM:
5784 case NFA_LNUM_GT:
5785 case NFA_LNUM_LT:
5786 result = (REG_MULTI &&
5787 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5788 (long_u)(reglnum + reg_firstlnum)));
5789 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005790 {
5791 add_here = TRUE;
5792 add_state = t->state->out;
5793 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005794 break;
5795
5796 case NFA_COL:
5797 case NFA_COL_GT:
5798 case NFA_COL_LT:
5799 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5800 (long_u)(reginput - regline) + 1);
5801 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005802 {
5803 add_here = TRUE;
5804 add_state = t->state->out;
5805 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005806 break;
5807
5808 case NFA_VCOL:
5809 case NFA_VCOL_GT:
5810 case NFA_VCOL_LT:
5811 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5812 (long_u)win_linetabsize(
5813 reg_win == NULL ? curwin : reg_win,
5814 regline, (colnr_T)(reginput - regline)) + 1);
5815 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005816 {
5817 add_here = TRUE;
5818 add_state = t->state->out;
5819 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005820 break;
5821
Bram Moolenaar044aa292013-06-04 21:27:38 +02005822 case NFA_MARK:
5823 case NFA_MARK_GT:
5824 case NFA_MARK_LT:
5825 {
5826 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5827
5828 /* Compare the mark position to the match position. */
5829 result = (pos != NULL /* mark doesn't exist */
5830 && pos->lnum > 0 /* mark isn't set in reg_buf */
5831 && (pos->lnum == reglnum + reg_firstlnum
5832 ? (pos->col == (colnr_T)(reginput - regline)
5833 ? t->state->c == NFA_MARK
5834 : (pos->col < (colnr_T)(reginput - regline)
5835 ? t->state->c == NFA_MARK_GT
5836 : t->state->c == NFA_MARK_LT))
5837 : (pos->lnum < reglnum + reg_firstlnum
5838 ? t->state->c == NFA_MARK_GT
5839 : t->state->c == NFA_MARK_LT)));
5840 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005841 {
5842 add_here = TRUE;
5843 add_state = t->state->out;
5844 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005845 break;
5846 }
5847
Bram Moolenaar423532e2013-05-29 21:14:42 +02005848 case NFA_CURSOR:
5849 result = (reg_win != NULL
5850 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5851 && ((colnr_T)(reginput - regline)
5852 == reg_win->w_cursor.col));
5853 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005854 {
5855 add_here = TRUE;
5856 add_state = t->state->out;
5857 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005858 break;
5859
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005860 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005861#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005862 result = reg_match_visual();
5863 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005864 {
5865 add_here = TRUE;
5866 add_state = t->state->out;
5867 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005868#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005869 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005871 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005872 {
5873 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005874
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005875 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005876 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005877 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005878 result = (c == curc);
5879
5880 if (!result && ireg_ic)
5881 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005882#ifdef FEAT_MBYTE
5883 /* If there is a composing character which is not being
5884 * ignored there can be no match. Match with composing
5885 * character uses NFA_COMPOSING above. */
5886 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005887 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005888 result = FALSE;
5889#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005890 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005891 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005892 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005893
5894 } /* switch (t->state->c) */
5895
5896 if (add_state != NULL)
5897 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005898 nfa_pim_T *pim;
5899
5900 if (t->pim.result == NFA_PIM_UNUSED)
5901 pim = NULL;
5902 else
5903 pim = &t->pim;
5904
5905 /* Handle the postponed invisible match if the match might end
5906 * without advancing and before the end of the line. */
5907 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005908 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005909 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005910 {
5911#ifdef ENABLE_LOG
5912 fprintf(log_fd, "\n");
5913 fprintf(log_fd, "==================================\n");
5914 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5915 fprintf(log_fd, "\n");
5916#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005917 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005918 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005919 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005920 /* for \@! and \@<! it is a match when the result is
5921 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005922 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
5923 || pim->state->c
Bram Moolenaardecd9542013-06-07 16:31:50 +02005924 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005925 {
5926 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005927 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005928#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005929 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005930 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005931#endif
5932 }
5933 }
5934 else
5935 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005936 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005937#ifdef ENABLE_LOG
5938 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005939 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005940 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5941 fprintf(log_fd, "\n");
5942#endif
5943 }
5944
Bram Moolenaardecd9542013-06-07 16:31:50 +02005945 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005946 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
5947 || pim->state->c
Bram Moolenaardecd9542013-06-07 16:31:50 +02005948 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005949 {
5950 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005951 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005952#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005953 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005954 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005955#endif
5956 }
5957 else
5958 /* look-behind match failed, don't add the state */
5959 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005960
5961 /* Postponed invisible match was handled, don't add it to
5962 * following states. */
5963 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005964 }
5965
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005966 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005967 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005968 else
5969 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005970 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005971 if (add_count > 0)
5972 nextlist->t[nextlist->n - 1].count = add_count;
5973 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005974 }
5975
5976 } /* for (thislist = thislist; thislist->state; thislist++) */
5977
Bram Moolenaare23febd2013-05-26 18:40:14 +02005978 /* Look for the start of a match in the current position by adding the
5979 * start state to the list of states.
5980 * The first found match is the leftmost one, thus the order of states
5981 * matters!
5982 * Do not add the start state in recursive calls of nfa_regmatch(),
5983 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005984 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005985 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005986 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005987 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02005988 && reglnum == 0
5989 && clen != 0
5990 && (ireg_maxcol == 0
5991 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005992 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005993 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005994 ? (reglnum < nfa_endp->se_u.pos.lnum
5995 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005996 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005997 < nfa_endp->se_u.pos.col))
5998 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005999 {
6000#ifdef ENABLE_LOG
6001 fprintf(log_fd, "(---) STARTSTATE\n");
6002#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006003 /* Inline optimized code for addstate() if we know the state is
6004 * the first MOPEN. */
6005 if (toplevel)
6006 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006007 int add = TRUE;
6008 int c;
6009
6010 if (prog->regstart != NUL && clen != 0)
6011 {
6012 if (nextlist->n == 0)
6013 {
6014 colnr_T col = (colnr_T)(reginput - regline) + clen;
6015
6016 /* Nextlist is empty, we can skip ahead to the
6017 * character that must appear at the start. */
6018 if (skip_to_start(prog->regstart, &col) == FAIL)
6019 break;
6020#ifdef ENABLE_LOG
6021 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6022 col - ((colnr_T)(reginput - regline) + clen));
6023#endif
6024 reginput = regline + col - clen;
6025 }
6026 else
6027 {
6028 /* Checking if the required start character matches is
6029 * cheaper than adding a state that won't match. */
6030 c = PTR2CHAR(reginput + clen);
6031 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6032 != MB_TOLOWER(prog->regstart)))
6033 {
6034#ifdef ENABLE_LOG
6035 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6036#endif
6037 add = FALSE;
6038 }
6039 }
6040 }
6041
6042 if (add)
6043 {
6044 if (REG_MULTI)
6045 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006046 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006047 else
6048 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006049 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006050 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006051 }
6052 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006053 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 }
6055
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006056#ifdef ENABLE_LOG
6057 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006058 {
6059 int i;
6060
6061 for (i = 0; i < thislist->n; i++)
6062 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6063 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006064 fprintf(log_fd, "\n");
6065#endif
6066
6067nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006068 /* Advance to the next character, or advance to the next line, or
6069 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006070 if (clen != 0)
6071 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006072 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6073 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006074 reg_nextline();
6075 else
6076 break;
6077 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078
6079#ifdef ENABLE_LOG
6080 if (log_fd != stderr)
6081 fclose(log_fd);
6082 log_fd = NULL;
6083#endif
6084
6085theend:
6086 /* Free memory */
6087 vim_free(list[0].t);
6088 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006089 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006090#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006091#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006092 fclose(debug);
6093#endif
6094
Bram Moolenaar963fee22013-05-26 21:47:28 +02006095 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006096}
6097
6098/*
6099 * Try match of "prog" with at regline["col"].
6100 * Returns 0 for failure, number of lines contained in the match otherwise.
6101 */
6102 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006103nfa_regtry(prog, col)
6104 nfa_regprog_T *prog;
6105 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006106{
6107 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006108 regsubs_T subs, m;
6109 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006110#ifdef ENABLE_LOG
6111 FILE *f;
6112#endif
6113
6114 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115
6116#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006117 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006118 if (f != NULL)
6119 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006120 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006121#ifdef DEBUG
6122 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6123#endif
6124 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006125 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006126 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006127 fprintf(f, "\n\n");
6128 fclose(f);
6129 }
6130 else
6131 EMSG(_("Could not open temporary log file for writing "));
6132#endif
6133
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006134 clear_sub(&subs.norm);
6135 clear_sub(&m.norm);
6136#ifdef FEAT_SYN_HL
6137 clear_sub(&subs.synt);
6138 clear_sub(&m.synt);
6139#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006140
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006141 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006142 return 0;
6143
6144 cleanup_subexpr();
6145 if (REG_MULTI)
6146 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006147 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006148 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006149 reg_startpos[i] = subs.norm.list.multi[i].start;
6150 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006151 }
6152
6153 if (reg_startpos[0].lnum < 0)
6154 {
6155 reg_startpos[0].lnum = 0;
6156 reg_startpos[0].col = col;
6157 }
6158 if (reg_endpos[0].lnum < 0)
6159 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006160 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006161 reg_endpos[0].lnum = reglnum;
6162 reg_endpos[0].col = (int)(reginput - regline);
6163 }
6164 else
6165 /* Use line number of "\ze". */
6166 reglnum = reg_endpos[0].lnum;
6167 }
6168 else
6169 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006170 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006172 reg_startp[i] = subs.norm.list.line[i].start;
6173 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006174 }
6175
6176 if (reg_startp[0] == NULL)
6177 reg_startp[0] = regline + col;
6178 if (reg_endp[0] == NULL)
6179 reg_endp[0] = reginput;
6180 }
6181
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006182#ifdef FEAT_SYN_HL
6183 /* Package any found \z(...\) matches for export. Default is none. */
6184 unref_extmatch(re_extmatch_out);
6185 re_extmatch_out = NULL;
6186
6187 if (prog->reghasz == REX_SET)
6188 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006189 cleanup_zsubexpr();
6190 re_extmatch_out = make_extmatch();
6191 for (i = 0; i < subs.synt.in_use; i++)
6192 {
6193 if (REG_MULTI)
6194 {
6195 struct multipos *mpos = &subs.synt.list.multi[i];
6196
6197 /* Only accept single line matches. */
6198 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6199 re_extmatch_out->matches[i] =
6200 vim_strnsave(reg_getline(mpos->start.lnum)
6201 + mpos->start.col,
6202 mpos->end.col - mpos->start.col);
6203 }
6204 else
6205 {
6206 struct linepos *lpos = &subs.synt.list.line[i];
6207
6208 if (lpos->start != NULL && lpos->end != NULL)
6209 re_extmatch_out->matches[i] =
6210 vim_strnsave(lpos->start,
6211 (int)(lpos->end - lpos->start));
6212 }
6213 }
6214 }
6215#endif
6216
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006217 return 1 + reglnum;
6218}
6219
6220/*
6221 * Match a regexp against a string ("line" points to the string) or multiple
6222 * lines ("line" is NULL, use reg_getline()).
6223 *
6224 * Returns 0 for failure, number of lines contained in the match otherwise.
6225 */
6226 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006227nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006228 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006229 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006230{
6231 nfa_regprog_T *prog;
6232 long retval = 0L;
6233 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006234 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006235
6236 if (REG_MULTI)
6237 {
6238 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6239 line = reg_getline((linenr_T)0); /* relative to the cursor */
6240 reg_startpos = reg_mmatch->startpos;
6241 reg_endpos = reg_mmatch->endpos;
6242 }
6243 else
6244 {
6245 prog = (nfa_regprog_T *)reg_match->regprog;
6246 reg_startp = reg_match->startp;
6247 reg_endp = reg_match->endp;
6248 }
6249
6250 /* Be paranoid... */
6251 if (prog == NULL || line == NULL)
6252 {
6253 EMSG(_(e_null));
6254 goto theend;
6255 }
6256
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006257 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6258 if (prog->regflags & RF_ICASE)
6259 ireg_ic = TRUE;
6260 else if (prog->regflags & RF_NOICASE)
6261 ireg_ic = FALSE;
6262
6263#ifdef FEAT_MBYTE
6264 /* If pattern contains "\Z" overrule value of ireg_icombine */
6265 if (prog->regflags & RF_ICOMBINE)
6266 ireg_icombine = TRUE;
6267#endif
6268
6269 regline = line;
6270 reglnum = 0; /* relative to line */
6271
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006272 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006273 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006274 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006275 nfa_listid = 1;
6276 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006277#ifdef DEBUG
6278 nfa_regengine.expr = prog->pattern;
6279#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006280
Bram Moolenaard89616e2013-06-06 18:46:06 +02006281 if (prog->reganch && col > 0)
6282 return 0L;
6283
Bram Moolenaar473de612013-06-08 18:19:48 +02006284 need_clear_subexpr = TRUE;
6285#ifdef FEAT_SYN_HL
6286 /* Clear the external match subpointers if necessary. */
6287 if (prog->reghasz == REX_SET)
6288 {
6289 nfa_has_zsubexpr = TRUE;
6290 need_clear_zsubexpr = TRUE;
6291 }
6292 else
6293 nfa_has_zsubexpr = FALSE;
6294#endif
6295
Bram Moolenaard89616e2013-06-06 18:46:06 +02006296 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006297 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006298 /* Skip ahead until a character we know the match must start with.
6299 * When there is none there is no match. */
6300 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006301 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006302
Bram Moolenaar473de612013-06-08 18:19:48 +02006303 /* If match_text is set it contains the full text that must match.
6304 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006305 if (prog->match_text != NULL
6306#ifdef FEAT_MBYTE
6307 && !ireg_icombine
6308#endif
6309 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006310 return find_match_text(col, prog->regstart, prog->match_text);
6311 }
6312
Bram Moolenaard89616e2013-06-06 18:46:06 +02006313 /* If the start column is past the maximum column: no need to try. */
6314 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6315 goto theend;
6316
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006317 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318 for (i = 0; i < nstate; ++i)
6319 {
6320 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006321 prog->state[i].lastlist[0] = 0;
6322 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323 }
6324
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006325 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006326
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006327#ifdef DEBUG
6328 nfa_regengine.expr = NULL;
6329#endif
6330
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006331theend:
6332 return retval;
6333}
6334
6335/*
6336 * Compile a regular expression into internal code for the NFA matcher.
6337 * Returns the program in allocated space. Returns NULL for an error.
6338 */
6339 static regprog_T *
6340nfa_regcomp(expr, re_flags)
6341 char_u *expr;
6342 int re_flags;
6343{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006344 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006345 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006346 int *postfix;
6347
6348 if (expr == NULL)
6349 return NULL;
6350
6351#ifdef DEBUG
6352 nfa_regengine.expr = expr;
6353#endif
6354
6355 init_class_tab();
6356
6357 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6358 return NULL;
6359
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006360 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006361 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006362 postfix = re2post();
6363 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006364 {
6365 /* TODO: only give this error for debugging? */
6366 if (post_ptr >= post_end)
6367 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006368 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006369 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006370
6371 /*
6372 * In order to build the NFA, we parse the input regexp twice:
6373 * 1. first pass to count size (so we can allocate space)
6374 * 2. second to emit code
6375 */
6376#ifdef ENABLE_LOG
6377 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006378 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379
6380 if (f != NULL)
6381 {
6382 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6383 fclose(f);
6384 }
6385 }
6386#endif
6387
6388 /*
6389 * PASS 1
6390 * Count number of NFA states in "nstate". Do not build the NFA.
6391 */
6392 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006393
Bram Moolenaar16619a22013-06-11 18:42:36 +02006394 /* allocate the regprog with space for the compiled regexp */
6395 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006396 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6397 if (prog == NULL)
6398 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 state_ptr = prog->state;
6400
6401 /*
6402 * PASS 2
6403 * Build the NFA
6404 */
6405 prog->start = post2nfa(postfix, post_ptr, FALSE);
6406 if (prog->start == NULL)
6407 goto fail;
6408
6409 prog->regflags = regflags;
6410 prog->engine = &nfa_regengine;
6411 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006412 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006413 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006414 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006415
6416 prog->reganch = nfa_get_reganch(prog->start, 0);
6417 prog->regstart = nfa_get_regstart(prog->start, 0);
6418
Bram Moolenaar473de612013-06-08 18:19:48 +02006419 prog->match_text = nfa_get_match_text(prog->start);
6420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421#ifdef ENABLE_LOG
6422 nfa_postfix_dump(expr, OK);
6423 nfa_dump(prog);
6424#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006425#ifdef FEAT_SYN_HL
6426 /* Remember whether this pattern has any \z specials in it. */
6427 prog->reghasz = re_has_z;
6428#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006429#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006430 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006431 nfa_regengine.expr = NULL;
6432#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006433
6434out:
6435 vim_free(post_start);
6436 post_start = post_ptr = post_end = NULL;
6437 state_ptr = NULL;
6438 return (regprog_T *)prog;
6439
6440fail:
6441 vim_free(prog);
6442 prog = NULL;
6443#ifdef ENABLE_LOG
6444 nfa_postfix_dump(expr, FAIL);
6445#endif
6446#ifdef DEBUG
6447 nfa_regengine.expr = NULL;
6448#endif
6449 goto out;
6450}
6451
Bram Moolenaar473de612013-06-08 18:19:48 +02006452/*
6453 * Free a compiled regexp program, returned by nfa_regcomp().
6454 */
6455 static void
6456nfa_regfree(prog)
6457 regprog_T *prog;
6458{
6459 if (prog != NULL)
6460 {
6461 vim_free(((nfa_regprog_T *)prog)->match_text);
6462#ifdef DEBUG
6463 vim_free(((nfa_regprog_T *)prog)->pattern);
6464#endif
6465 vim_free(prog);
6466 }
6467}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006468
6469/*
6470 * Match a regexp against a string.
6471 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6472 * Uses curbuf for line count and 'iskeyword'.
6473 *
6474 * Return TRUE if there is a match, FALSE if not.
6475 */
6476 static int
6477nfa_regexec(rmp, line, col)
6478 regmatch_T *rmp;
6479 char_u *line; /* string to match against */
6480 colnr_T col; /* column to start looking for match */
6481{
6482 reg_match = rmp;
6483 reg_mmatch = NULL;
6484 reg_maxline = 0;
6485 reg_line_lbr = FALSE;
6486 reg_buf = curbuf;
6487 reg_win = NULL;
6488 ireg_ic = rmp->rm_ic;
6489#ifdef FEAT_MBYTE
6490 ireg_icombine = FALSE;
6491#endif
6492 ireg_maxcol = 0;
6493 return (nfa_regexec_both(line, col) != 0);
6494}
6495
6496#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6497 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6498
6499static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6500
6501/*
6502 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6503 */
6504 static int
6505nfa_regexec_nl(rmp, line, col)
6506 regmatch_T *rmp;
6507 char_u *line; /* string to match against */
6508 colnr_T col; /* column to start looking for match */
6509{
6510 reg_match = rmp;
6511 reg_mmatch = NULL;
6512 reg_maxline = 0;
6513 reg_line_lbr = TRUE;
6514 reg_buf = curbuf;
6515 reg_win = NULL;
6516 ireg_ic = rmp->rm_ic;
6517#ifdef FEAT_MBYTE
6518 ireg_icombine = FALSE;
6519#endif
6520 ireg_maxcol = 0;
6521 return (nfa_regexec_both(line, col) != 0);
6522}
6523#endif
6524
6525
6526/*
6527 * Match a regexp against multiple lines.
6528 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6529 * Uses curbuf for line count and 'iskeyword'.
6530 *
6531 * Return zero if there is no match. Return number of lines contained in the
6532 * match otherwise.
6533 *
6534 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6535 *
6536 * ! Also NOTE : match may actually be in another line. e.g.:
6537 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6538 *
6539 * +-------------------------+
6540 * |a |
6541 * |b |
6542 * |c |
6543 * | |
6544 * +-------------------------+
6545 *
6546 * then nfa_regexec_multi() returns 3. while the original
6547 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6548 *
6549 * FIXME if this behavior is not compatible.
6550 */
6551 static long
6552nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6553 regmmatch_T *rmp;
6554 win_T *win; /* window in which to search or NULL */
6555 buf_T *buf; /* buffer in which to search */
6556 linenr_T lnum; /* nr of line to start looking for match */
6557 colnr_T col; /* column to start looking for match */
6558 proftime_T *tm UNUSED; /* timeout limit or NULL */
6559{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006560 reg_match = NULL;
6561 reg_mmatch = rmp;
6562 reg_buf = buf;
6563 reg_win = win;
6564 reg_firstlnum = lnum;
6565 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6566 reg_line_lbr = FALSE;
6567 ireg_ic = rmp->rmm_ic;
6568#ifdef FEAT_MBYTE
6569 ireg_icombine = FALSE;
6570#endif
6571 ireg_maxcol = rmp->rmm_maxcol;
6572
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006573 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006574}
6575
6576#ifdef DEBUG
6577# undef ENABLE_LOG
6578#endif