blob: 9a89b49ecf3809844350b23abc831292d96b24da [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.
4590 * NFA_ANY: 1
4591 * specific character: 99
4592 */
4593 static int
4594failure_chance(state, depth)
4595 nfa_state_T *state;
4596 int depth;
4597{
4598 int c = state->c;
4599 int l, r;
4600
4601 /* detect looping */
4602 if (depth > 4)
4603 return 1;
4604
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004605 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004606 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004607 case NFA_SPLIT:
4608 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4609 /* avoid recursive stuff */
4610 return 1;
4611 /* two alternatives, use the lowest failure chance */
4612 l = failure_chance(state->out, depth + 1);
4613 r = failure_chance(state->out1, depth + 1);
4614 return l < r ? l : r;
4615
4616 case NFA_ANY:
4617 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004618 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004619 case NFA_MATCH:
4620 /* empty match works always */
4621 return 0;
4622
4623 case NFA_BOL:
4624 case NFA_EOL:
4625 case NFA_BOF:
4626 case NFA_EOF:
4627 case NFA_NEWL:
4628 return 99;
4629
4630 case NFA_BOW:
4631 case NFA_EOW:
4632 return 90;
4633
4634 case NFA_MOPEN:
4635 case NFA_MOPEN1:
4636 case NFA_MOPEN2:
4637 case NFA_MOPEN3:
4638 case NFA_MOPEN4:
4639 case NFA_MOPEN5:
4640 case NFA_MOPEN6:
4641 case NFA_MOPEN7:
4642 case NFA_MOPEN8:
4643 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004644#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004645 case NFA_ZOPEN:
4646 case NFA_ZOPEN1:
4647 case NFA_ZOPEN2:
4648 case NFA_ZOPEN3:
4649 case NFA_ZOPEN4:
4650 case NFA_ZOPEN5:
4651 case NFA_ZOPEN6:
4652 case NFA_ZOPEN7:
4653 case NFA_ZOPEN8:
4654 case NFA_ZOPEN9:
4655 case NFA_ZCLOSE:
4656 case NFA_ZCLOSE1:
4657 case NFA_ZCLOSE2:
4658 case NFA_ZCLOSE3:
4659 case NFA_ZCLOSE4:
4660 case NFA_ZCLOSE5:
4661 case NFA_ZCLOSE6:
4662 case NFA_ZCLOSE7:
4663 case NFA_ZCLOSE8:
4664 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004665#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004666 case NFA_NOPEN:
4667 case NFA_MCLOSE:
4668 case NFA_MCLOSE1:
4669 case NFA_MCLOSE2:
4670 case NFA_MCLOSE3:
4671 case NFA_MCLOSE4:
4672 case NFA_MCLOSE5:
4673 case NFA_MCLOSE6:
4674 case NFA_MCLOSE7:
4675 case NFA_MCLOSE8:
4676 case NFA_MCLOSE9:
4677 case NFA_NCLOSE:
4678 return failure_chance(state->out, depth + 1);
4679
4680 case NFA_BACKREF1:
4681 case NFA_BACKREF2:
4682 case NFA_BACKREF3:
4683 case NFA_BACKREF4:
4684 case NFA_BACKREF5:
4685 case NFA_BACKREF6:
4686 case NFA_BACKREF7:
4687 case NFA_BACKREF8:
4688 case NFA_BACKREF9:
4689#ifdef FEAT_SYN_HL
4690 case NFA_ZREF1:
4691 case NFA_ZREF2:
4692 case NFA_ZREF3:
4693 case NFA_ZREF4:
4694 case NFA_ZREF5:
4695 case NFA_ZREF6:
4696 case NFA_ZREF7:
4697 case NFA_ZREF8:
4698 case NFA_ZREF9:
4699#endif
4700 /* backreferences don't match in many places */
4701 return 94;
4702
4703 case NFA_LNUM_GT:
4704 case NFA_LNUM_LT:
4705 case NFA_COL_GT:
4706 case NFA_COL_LT:
4707 case NFA_VCOL_GT:
4708 case NFA_VCOL_LT:
4709 case NFA_MARK_GT:
4710 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004711 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004712 /* before/after positions don't match very often */
4713 return 85;
4714
4715 case NFA_LNUM:
4716 return 90;
4717
4718 case NFA_CURSOR:
4719 case NFA_COL:
4720 case NFA_VCOL:
4721 case NFA_MARK:
4722 /* specific positions rarely match */
4723 return 98;
4724
4725 case NFA_COMPOSING:
4726 return 95;
4727
4728 default:
4729 if (c > 0)
4730 /* character match fails often */
4731 return 95;
4732 }
4733
4734 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004735 return 50;
4736}
4737
Bram Moolenaarf46da702013-06-02 22:37:42 +02004738/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004739 * Skip until the char "c" we know a match must start with.
4740 */
4741 static int
4742skip_to_start(c, colp)
4743 int c;
4744 colnr_T *colp;
4745{
4746 char_u *s;
4747
4748 /* Used often, do some work to avoid call overhead. */
4749 if (!ireg_ic
4750#ifdef FEAT_MBYTE
4751 && !has_mbyte
4752#endif
4753 )
4754 s = vim_strbyte(regline + *colp, c);
4755 else
4756 s = cstrchr(regline + *colp, c);
4757 if (s == NULL)
4758 return FAIL;
4759 *colp = (int)(s - regline);
4760 return OK;
4761}
4762
4763/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004764 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004765 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004766 * Returns zero for no match, 1 for a match.
4767 */
4768 static long
4769find_match_text(startcol, regstart, match_text)
4770 colnr_T startcol;
4771 int regstart;
4772 char_u *match_text;
4773{
4774 colnr_T col = startcol;
4775 int c1, c2;
4776 int len1, len2;
4777 int match;
4778
4779 for (;;)
4780 {
4781 match = TRUE;
4782 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4783 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4784 {
4785 c1 = PTR2CHAR(match_text + len1);
4786 c2 = PTR2CHAR(regline + col + len2);
4787 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4788 {
4789 match = FALSE;
4790 break;
4791 }
4792 len2 += MB_CHAR2LEN(c2);
4793 }
4794 if (match
4795#ifdef FEAT_MBYTE
4796 /* check that no composing char follows */
4797 && !(enc_utf8
4798 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
4799#endif
4800 )
4801 {
4802 cleanup_subexpr();
4803 if (REG_MULTI)
4804 {
4805 reg_startpos[0].lnum = reglnum;
4806 reg_startpos[0].col = col;
4807 reg_endpos[0].lnum = reglnum;
4808 reg_endpos[0].col = col + len2;
4809 }
4810 else
4811 {
4812 reg_startp[0] = regline + col;
4813 reg_endp[0] = regline + col + len2;
4814 }
4815 return 1L;
4816 }
4817
4818 /* Try finding regstart after the current match. */
4819 col += MB_CHAR2LEN(regstart); /* skip regstart */
4820 if (skip_to_start(regstart, &col) == FAIL)
4821 break;
4822 }
4823 return 0L;
4824}
4825
4826/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004827 * Main matching routine.
4828 *
4829 * Run NFA to determine whether it matches reginput.
4830 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004831 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004832 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004833 * Return TRUE if there is a match, FALSE otherwise.
4834 * Note: Caller must ensure that: start != NULL.
4835 */
4836 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004837nfa_regmatch(prog, start, submatch, m)
4838 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004839 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004840 regsubs_T *submatch;
4841 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004842{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004843 int result;
4844 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004845 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004846 int go_to_nextline = FALSE;
4847 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004848 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004849 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004850 nfa_list_T *thislist;
4851 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004852 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004853 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02004854 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004855 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02004856 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004857 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004858#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004859 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004860
4861 if (debug == NULL)
4862 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004863 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864 return FALSE;
4865 }
4866#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004867 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004868
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004869 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004870 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004871 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004872 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004873 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004874 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004875 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004877
4878#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004879 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880 if (log_fd != NULL)
4881 {
4882 fprintf(log_fd, "**********************************\n");
4883 nfa_set_code(start->c);
4884 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4885 abs(start->id), code);
4886 fprintf(log_fd, "**********************************\n");
4887 }
4888 else
4889 {
4890 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4891 log_fd = stderr;
4892 }
4893#endif
4894
4895 thislist = &list[0];
4896 thislist->n = 0;
4897 nextlist = &list[1];
4898 nextlist->n = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004899#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004900 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004902 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004903
4904 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
4905 * it's the first MOPEN. */
4906 if (toplevel)
4907 {
4908 if (REG_MULTI)
4909 {
4910 m->norm.list.multi[0].start.lnum = reglnum;
4911 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
4912 }
4913 else
4914 m->norm.list.line[0].start = reginput;
4915 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004916 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02004917 }
4918 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004919 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004921#define ADD_STATE_IF_MATCH(state) \
4922 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02004923 add_state = state->out; \
4924 add_off = clen; \
4925 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004926
4927 /*
4928 * Run for each character.
4929 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004930 for (;;)
4931 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004932 int curc;
4933 int clen;
4934
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004935#ifdef FEAT_MBYTE
4936 if (has_mbyte)
4937 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004938 curc = (*mb_ptr2char)(reginput);
4939 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 }
4941 else
4942#endif
4943 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004944 curc = *reginput;
4945 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004947 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004948 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004949 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004950 go_to_nextline = FALSE;
4951 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004952
4953 /* swap lists */
4954 thislist = &list[flag];
4955 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004956 nextlist->n = 0; /* clear nextlist */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004957 ++nfa_listid;
4958 thislist->id = nfa_listid;
4959 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004960
4961#ifdef ENABLE_LOG
4962 fprintf(log_fd, "------------------------------------------\n");
4963 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004964 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004965 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004966 {
4967 int i;
4968
4969 for (i = 0; i < thislist->n; i++)
4970 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4971 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004972 fprintf(log_fd, "\n");
4973#endif
4974
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004975#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004976 fprintf(debug, "\n-------------------\n");
4977#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004978 /*
4979 * If the state lists are empty we can stop.
4980 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004981 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004982 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004983
4984 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004985 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004986 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02004987 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004988
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004989#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004990 nfa_set_code(t->state->c);
4991 fprintf(debug, "%s, ", code);
4992#endif
4993#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004994 {
4995 int col;
4996
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004997 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004998 col = -1;
4999 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005000 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005001 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005002 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005003 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005004 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5005 abs(t->state->id), (int)t->state->c, code, col,
5006 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005007 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005008#endif
5009
5010 /*
5011 * Handle the possible codes of the current state.
5012 * The most important is NFA_MATCH.
5013 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005014 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005015 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005016 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017 switch (t->state->c)
5018 {
5019 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005020 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005021 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005022 copy_sub(&submatch->norm, &t->subs.norm);
5023#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005024 if (nfa_has_zsubexpr)
5025 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005026#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005028 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005030 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005031 * states at this position. When the list of states is going
5032 * to be empty quit without advancing, so that "reginput" is
5033 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005034 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005035 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005036 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005037 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005038
5039 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005040 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005041 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005042 /*
5043 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005044 * NFA_START_INVISIBLE_BEFORE node.
5045 * They surround a zero-width group, used with "\@=", "\&",
5046 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005047 * If we got here, it means that the current "invisible" group
5048 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005049 * nfa_regmatch(). For a look-behind match only when it ends
5050 * in the position in "nfa_endp".
5051 * Submatches are stored in *m, and used in the parent call.
5052 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005053#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005054 if (nfa_endp != NULL)
5055 {
5056 if (REG_MULTI)
5057 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5058 (int)reglnum,
5059 (int)nfa_endp->se_u.pos.lnum,
5060 (int)(reginput - regline),
5061 nfa_endp->se_u.pos.col);
5062 else
5063 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5064 (int)(reginput - regline),
5065 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005066 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005067#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005068 /* If "nfa_endp" is set it's only a match if it ends at
5069 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005070 if (nfa_endp != NULL && (REG_MULTI
5071 ? (reglnum != nfa_endp->se_u.pos.lnum
5072 || (int)(reginput - regline)
5073 != nfa_endp->se_u.pos.col)
5074 : reginput != nfa_endp->se_u.ptr))
5075 break;
5076
5077 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005078 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005079 {
5080 copy_sub(&m->norm, &t->subs.norm);
5081#ifdef FEAT_SYN_HL
5082 if (nfa_has_zsubexpr)
5083 copy_sub(&m->synt, &t->subs.synt);
5084#endif
5085 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005086#ifdef ENABLE_LOG
5087 fprintf(log_fd, "Match found:\n");
5088 log_subsexpr(m);
5089#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005090 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005091 break;
5092
5093 case NFA_START_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005094 case NFA_START_INVISIBLE_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005095 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005096 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005097 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005098 int cout = t->state->out1->out->c;
5099
5100 /* Do it directly when what follows is possibly end of
5101 * match (closing paren).
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005102 * Do it directly if there already is a PIM.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005103 * Postpone when it is \@<= or \@<!, these are expensive.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005104 * Otherwise first do the one that has the highest chance
5105 * of failing. */
5106 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005107#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02005108 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005109#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005110 || t->pim.result != NFA_PIM_UNUSED
Bram Moolenaara2d95102013-06-04 14:23:05 +02005111 || (t->state->c != NFA_START_INVISIBLE_BEFORE
Bram Moolenaardecd9542013-06-07 16:31:50 +02005112 && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG
Bram Moolenaara2d95102013-06-04 14:23:05 +02005113 && failure_chance(t->state->out1->out, 0)
5114 < failure_chance(t->state->out, 0)))
5115 {
5116 /*
5117 * First try matching the invisible match, then what
5118 * follows.
5119 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005120 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005121 submatch, m, &listids);
5122
Bram Moolenaardecd9542013-06-07 16:31:50 +02005123 /* for \@! and \@<! it is a match when the result is
5124 * FALSE */
5125 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
5126 || t->state->c
5127 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005128 {
5129 /* Copy submatch info from the recursive call */
5130 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005131#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005132 if (nfa_has_zsubexpr)
5133 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005134#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005135
Bram Moolenaara2d95102013-06-04 14:23:05 +02005136 /* t->state->out1 is the corresponding
5137 * END_INVISIBLE node; Add its out to the current
5138 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005139 add_here = TRUE;
5140 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005141 }
5142 }
5143 else
5144 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005145 nfa_pim_T pim;
5146
Bram Moolenaara2d95102013-06-04 14:23:05 +02005147 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005148 * First try matching what follows. Only if a match
5149 * is found verify the invisible match matches. Add a
5150 * nfa_pim_T to the following states, it contains info
5151 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005152 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005153 pim.state = t->state;
5154 pim.result = NFA_PIM_TODO;
5155 pim.subs.norm.in_use = 0;
5156#ifdef FEAT_SYN_HL
5157 pim.subs.synt.in_use = 0;
5158#endif
5159 if (REG_MULTI)
5160 {
5161 pim.end.pos.col = (int)(reginput - regline);
5162 pim.end.pos.lnum = reglnum;
5163 }
5164 else
5165 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005166
5167 /* t->state->out1 is the corresponding END_INVISIBLE
5168 * node; Add its out to the current list (zero-width
5169 * match). */
5170 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005171 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005172 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005173 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005174 break;
5175
Bram Moolenaar87953742013-06-05 18:52:40 +02005176 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005177 {
5178 nfa_state_T *skip = NULL;
5179#ifdef ENABLE_LOG
5180 int skip_lid = 0;
5181#endif
5182
5183 /* There is no point in trying to match the pattern if the
5184 * output state is not going to be added to the list. */
5185 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5186 {
5187 skip = t->state->out1->out;
5188#ifdef ENABLE_LOG
5189 skip_lid = nextlist->id;
5190#endif
5191 }
5192 else if (state_in_list(nextlist,
5193 t->state->out1->out->out, &t->subs))
5194 {
5195 skip = t->state->out1->out->out;
5196#ifdef ENABLE_LOG
5197 skip_lid = nextlist->id;
5198#endif
5199 }
5200 else if(state_in_list(thislist,
5201 t->state->out1->out->out, &t->subs))
5202 {
5203 skip = t->state->out1->out->out;
5204#ifdef ENABLE_LOG
5205 skip_lid = thislist->id;
5206#endif
5207 }
5208 if (skip != NULL)
5209 {
5210#ifdef ENABLE_LOG
5211 nfa_set_code(skip->c);
5212 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5213 abs(skip->id), skip_lid, skip->c, code);
5214#endif
5215 break;
5216 }
5217
Bram Moolenaar87953742013-06-05 18:52:40 +02005218 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005219 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005220 submatch, m, &listids);
5221 if (result)
5222 {
5223 int bytelen;
5224
5225#ifdef ENABLE_LOG
5226 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5227 log_subsexpr(m);
5228#endif
5229 /* Copy submatch info from the recursive call */
5230 copy_sub_off(&t->subs.norm, &m->norm);
5231#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005232 if (nfa_has_zsubexpr)
5233 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005234#endif
5235 /* Now we need to skip over the matched text and then
5236 * continue with what follows. */
5237 if (REG_MULTI)
5238 /* TODO: multi-line match */
5239 bytelen = m->norm.list.multi[0].end.col
5240 - (int)(reginput - regline);
5241 else
5242 bytelen = (int)(m->norm.list.line[0].end - reginput);
5243
5244#ifdef ENABLE_LOG
5245 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5246#endif
5247 if (bytelen == 0)
5248 {
5249 /* empty match, output of corresponding
5250 * NFA_END_PATTERN/NFA_SKIP to be used at current
5251 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005252 add_here = TRUE;
5253 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005254 }
5255 else if (bytelen <= clen)
5256 {
5257 /* match current character, output of corresponding
5258 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005259 add_state = t->state->out1->out->out;
5260 add_off = clen;
5261 }
5262 else
5263 {
5264 /* skip over the matched characters, set character
5265 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005266 add_state = t->state->out1->out;
5267 add_off = bytelen;
5268 add_count = bytelen - clen;
5269 }
5270 }
5271 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005272 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005273
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005274 case NFA_BOL:
5275 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005276 {
5277 add_here = TRUE;
5278 add_state = t->state->out;
5279 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005280 break;
5281
5282 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005283 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005284 {
5285 add_here = TRUE;
5286 add_state = t->state->out;
5287 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005288 break;
5289
5290 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005291 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005292
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005293 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005294 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005295#ifdef FEAT_MBYTE
5296 else if (has_mbyte)
5297 {
5298 int this_class;
5299
5300 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005301 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005302 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005303 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005304 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005305 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005306 }
5307#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005308 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005309 || (reginput > regline
5310 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005311 result = FALSE;
5312 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005313 {
5314 add_here = TRUE;
5315 add_state = t->state->out;
5316 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005317 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005318
5319 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005320 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005321 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005322 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005323#ifdef FEAT_MBYTE
5324 else if (has_mbyte)
5325 {
5326 int this_class, prev_class;
5327
5328 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005329 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005330 prev_class = reg_prev_class();
5331 if (this_class == prev_class
5332 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005333 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005334 }
5335#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005336 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005337 || (reginput[0] != NUL
5338 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005339 result = FALSE;
5340 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005341 {
5342 add_here = TRUE;
5343 add_state = t->state->out;
5344 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005345 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005346
Bram Moolenaar4b780632013-05-31 22:14:52 +02005347 case NFA_BOF:
5348 if (reglnum == 0 && reginput == regline
5349 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005350 {
5351 add_here = TRUE;
5352 add_state = t->state->out;
5353 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005354 break;
5355
5356 case NFA_EOF:
5357 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005358 {
5359 add_here = TRUE;
5360 add_state = t->state->out;
5361 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005362 break;
5363
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005364#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005365 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005366 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005367 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005368 int len = 0;
5369 nfa_state_T *end;
5370 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005371 int cchars[MAX_MCO];
5372 int ccount = 0;
5373 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005374
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005375 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005376 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005377 if (utf_iscomposing(sta->c))
5378 {
5379 /* Only match composing character(s), ignore base
5380 * character. Used for ".{composing}" and "{composing}"
5381 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005382 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005383 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005384 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005385 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005386 /* If \Z was present, then ignore composing characters.
5387 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005388 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005389 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005390 else
5391 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005392 while (sta->c != NFA_END_COMPOSING)
5393 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005394 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005395
5396 /* Check base character matches first, unless ignored. */
5397 else if (len > 0 || mc == sta->c)
5398 {
5399 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005400 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005401 len += mb_char2len(mc);
5402 sta = sta->out;
5403 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005404
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005405 /* We don't care about the order of composing characters.
5406 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005407 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005408 {
5409 mc = mb_ptr2char(reginput + len);
5410 cchars[ccount++] = mc;
5411 len += mb_char2len(mc);
5412 if (ccount == MAX_MCO)
5413 break;
5414 }
5415
5416 /* Check that each composing char in the pattern matches a
5417 * composing char in the text. We do not check if all
5418 * composing chars are matched. */
5419 result = OK;
5420 while (sta->c != NFA_END_COMPOSING)
5421 {
5422 for (j = 0; j < ccount; ++j)
5423 if (cchars[j] == sta->c)
5424 break;
5425 if (j == ccount)
5426 {
5427 result = FAIL;
5428 break;
5429 }
5430 sta = sta->out;
5431 }
5432 }
5433 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005434 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005435
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005436 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005437 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005438 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005439 }
5440#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005441
5442 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005443 if (curc == NUL && !reg_line_lbr && REG_MULTI
5444 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005445 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005446 go_to_nextline = TRUE;
5447 /* Pass -1 for the offset, which means taking the position
5448 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005449 add_state = t->state->out;
5450 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005451 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005452 else if (curc == '\n' && reg_line_lbr)
5453 {
5454 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005455 add_state = t->state->out;
5456 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005457 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005458 break;
5459
Bram Moolenaar417bad22013-06-07 14:08:30 +02005460 case NFA_START_COLL:
5461 case NFA_START_NEG_COLL:
5462 {
5463 /* What follows is a list of characters, until NFA_END_COLL.
5464 * One of them must match or none of them must match. */
5465 nfa_state_T *state;
5466 int result_if_matched;
5467 int c1, c2;
5468
5469 /* Never match EOL. If it's part of the collection it is added
5470 * as a separate state with an OR. */
5471 if (curc == NUL)
5472 break;
5473
5474 state = t->state->out;
5475 result_if_matched = (t->state->c == NFA_START_COLL);
5476 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005477 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005478 if (state->c == NFA_END_COLL)
5479 {
5480 result = !result_if_matched;
5481 break;
5482 }
5483 if (state->c == NFA_RANGE_MIN)
5484 {
5485 c1 = state->val;
5486 state = state->out; /* advance to NFA_RANGE_MAX */
5487 c2 = state->val;
5488#ifdef ENABLE_LOG
5489 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5490 curc, c1, c2);
5491#endif
5492 if (curc >= c1 && curc <= c2)
5493 {
5494 result = result_if_matched;
5495 break;
5496 }
5497 if (ireg_ic)
5498 {
5499 int curc_low = MB_TOLOWER(curc);
5500 int done = FALSE;
5501
5502 for ( ; c1 <= c2; ++c1)
5503 if (MB_TOLOWER(c1) == curc_low)
5504 {
5505 result = result_if_matched;
5506 done = TRUE;
5507 break;
5508 }
5509 if (done)
5510 break;
5511 }
5512 }
5513 else if (state->c < 0 ? check_char_class(state->c, curc)
5514 : (curc == state->c
5515 || (ireg_ic && MB_TOLOWER(curc)
5516 == MB_TOLOWER(state->c))))
5517 {
5518 result = result_if_matched;
5519 break;
5520 }
5521 state = state->out;
5522 }
5523 if (result)
5524 {
5525 /* next state is in out of the NFA_END_COLL, out1 of
5526 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005527 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005528 add_off = clen;
5529 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005530 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005531 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532
5533 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005534 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005535 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005536 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005537 add_state = t->state->out;
5538 add_off = clen;
5539 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005540 break;
5541
5542 /*
5543 * Character classes like \a for alpha, \d for digit etc.
5544 */
5545 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005546 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005547 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005548 break;
5549
5550 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005551 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005552 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553 break;
5554
5555 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005556 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005557 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005558 break;
5559
5560 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005561 result = !VIM_ISDIGIT(curc)
5562 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005563 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564 break;
5565
5566 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005567 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005568 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005569 break;
5570
5571 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005572 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005573 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574 break;
5575
5576 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02005577 result = ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005578 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005579 break;
5580
5581 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005582 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005583 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005584 break;
5585
5586 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005587 result = vim_iswhite(curc);
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_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005592 result = curc != NUL && !vim_iswhite(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_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005597 result = ri_digit(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_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005602 result = curc != NUL && !ri_digit(curc);
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_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005607 result = ri_hex(curc);
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_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005612 result = curc != NUL && !ri_hex(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_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005617 result = ri_octal(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_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005622 result = curc != NUL && !ri_octal(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_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005627 result = ri_word(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_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005632 result = curc != NUL && !ri_word(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_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005637 result = ri_head(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_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005642 result = curc != NUL && !ri_head(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_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005647 result = ri_alpha(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_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005652 result = curc != NUL && !ri_alpha(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_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005657 result = ri_lower(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_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005662 result = curc != NUL && !ri_lower(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_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005667 result = ri_upper(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_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 result = curc != NUL && !ri_upper(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
Bram Moolenaar5714b802013-05-28 22:03:20 +02005676 case NFA_BACKREF1:
5677 case NFA_BACKREF2:
5678 case NFA_BACKREF3:
5679 case NFA_BACKREF4:
5680 case NFA_BACKREF5:
5681 case NFA_BACKREF6:
5682 case NFA_BACKREF7:
5683 case NFA_BACKREF8:
5684 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005685#ifdef FEAT_SYN_HL
5686 case NFA_ZREF1:
5687 case NFA_ZREF2:
5688 case NFA_ZREF3:
5689 case NFA_ZREF4:
5690 case NFA_ZREF5:
5691 case NFA_ZREF6:
5692 case NFA_ZREF7:
5693 case NFA_ZREF8:
5694 case NFA_ZREF9:
5695#endif
5696 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005697 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005698 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005699 int bytelen;
5700
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005701 if (t->state->c <= NFA_BACKREF9)
5702 {
5703 subidx = t->state->c - NFA_BACKREF1 + 1;
5704 result = match_backref(&t->subs.norm, subidx, &bytelen);
5705 }
5706#ifdef FEAT_SYN_HL
5707 else
5708 {
5709 subidx = t->state->c - NFA_ZREF1 + 1;
5710 result = match_zref(subidx, &bytelen);
5711 }
5712#endif
5713
Bram Moolenaar5714b802013-05-28 22:03:20 +02005714 if (result)
5715 {
5716 if (bytelen == 0)
5717 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005718 /* empty match always works, output of NFA_SKIP to be
5719 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005720 add_here = TRUE;
5721 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005722 }
5723 else if (bytelen <= clen)
5724 {
5725 /* match current character, jump ahead to out of
5726 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005727 add_state = t->state->out->out;
5728 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005729 }
5730 else
5731 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005732 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005733 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005734 add_state = t->state->out;
5735 add_off = bytelen;
5736 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005737 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005738 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005739 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005740 }
5741 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005742 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005743 if (t->count - clen <= 0)
5744 {
5745 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005746 add_state = t->state->out;
5747 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005748 }
5749 else
5750 {
5751 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005752 add_state = t->state;
5753 add_off = 0;
5754 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005755 }
5756 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005757
Bram Moolenaar423532e2013-05-29 21:14:42 +02005758 case NFA_LNUM:
5759 case NFA_LNUM_GT:
5760 case NFA_LNUM_LT:
5761 result = (REG_MULTI &&
5762 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5763 (long_u)(reglnum + reg_firstlnum)));
5764 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005765 {
5766 add_here = TRUE;
5767 add_state = t->state->out;
5768 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005769 break;
5770
5771 case NFA_COL:
5772 case NFA_COL_GT:
5773 case NFA_COL_LT:
5774 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5775 (long_u)(reginput - regline) + 1);
5776 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005777 {
5778 add_here = TRUE;
5779 add_state = t->state->out;
5780 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005781 break;
5782
5783 case NFA_VCOL:
5784 case NFA_VCOL_GT:
5785 case NFA_VCOL_LT:
5786 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5787 (long_u)win_linetabsize(
5788 reg_win == NULL ? curwin : reg_win,
5789 regline, (colnr_T)(reginput - regline)) + 1);
5790 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005791 {
5792 add_here = TRUE;
5793 add_state = t->state->out;
5794 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005795 break;
5796
Bram Moolenaar044aa292013-06-04 21:27:38 +02005797 case NFA_MARK:
5798 case NFA_MARK_GT:
5799 case NFA_MARK_LT:
5800 {
5801 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5802
5803 /* Compare the mark position to the match position. */
5804 result = (pos != NULL /* mark doesn't exist */
5805 && pos->lnum > 0 /* mark isn't set in reg_buf */
5806 && (pos->lnum == reglnum + reg_firstlnum
5807 ? (pos->col == (colnr_T)(reginput - regline)
5808 ? t->state->c == NFA_MARK
5809 : (pos->col < (colnr_T)(reginput - regline)
5810 ? t->state->c == NFA_MARK_GT
5811 : t->state->c == NFA_MARK_LT))
5812 : (pos->lnum < reglnum + reg_firstlnum
5813 ? t->state->c == NFA_MARK_GT
5814 : t->state->c == NFA_MARK_LT)));
5815 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005816 {
5817 add_here = TRUE;
5818 add_state = t->state->out;
5819 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02005820 break;
5821 }
5822
Bram Moolenaar423532e2013-05-29 21:14:42 +02005823 case NFA_CURSOR:
5824 result = (reg_win != NULL
5825 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5826 && ((colnr_T)(reginput - regline)
5827 == reg_win->w_cursor.col));
5828 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005829 {
5830 add_here = TRUE;
5831 add_state = t->state->out;
5832 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02005833 break;
5834
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005835 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005836#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005837 result = reg_match_visual();
5838 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005839 {
5840 add_here = TRUE;
5841 add_state = t->state->out;
5842 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005843#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005844 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005845
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005846 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005847 {
5848 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005849
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005850 /* TODO: put this in #ifdef later */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005851 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005852 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005853 result = (c == curc);
5854
5855 if (!result && ireg_ic)
5856 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005857#ifdef FEAT_MBYTE
5858 /* If there is a composing character which is not being
5859 * ignored there can be no match. Match with composing
5860 * character uses NFA_COMPOSING above. */
5861 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005862 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005863 result = FALSE;
5864#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005865 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005866 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005867 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005868
5869 } /* switch (t->state->c) */
5870
5871 if (add_state != NULL)
5872 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005873 nfa_pim_T *pim;
5874
5875 if (t->pim.result == NFA_PIM_UNUSED)
5876 pim = NULL;
5877 else
5878 pim = &t->pim;
5879
5880 /* Handle the postponed invisible match if the match might end
5881 * without advancing and before the end of the line. */
5882 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005883 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005884 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005885 {
5886#ifdef ENABLE_LOG
5887 fprintf(log_fd, "\n");
5888 fprintf(log_fd, "==================================\n");
5889 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5890 fprintf(log_fd, "\n");
5891#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005892 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005893 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005894 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02005895 /* for \@! and \@<! it is a match when the result is
5896 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005897 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
5898 || pim->state->c
Bram Moolenaardecd9542013-06-07 16:31:50 +02005899 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005900 {
5901 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005902 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005903#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005904 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005905 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005906#endif
5907 }
5908 }
5909 else
5910 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005911 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005912#ifdef ENABLE_LOG
5913 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005914 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005915 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5916 fprintf(log_fd, "\n");
5917#endif
5918 }
5919
Bram Moolenaardecd9542013-06-07 16:31:50 +02005920 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005921 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
5922 || pim->state->c
Bram Moolenaardecd9542013-06-07 16:31:50 +02005923 == NFA_START_INVISIBLE_BEFORE_NEG))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005924 {
5925 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005926 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005927#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005928 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005929 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005930#endif
5931 }
5932 else
5933 /* look-behind match failed, don't add the state */
5934 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005935
5936 /* Postponed invisible match was handled, don't add it to
5937 * following states. */
5938 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005939 }
5940
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005941 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005942 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005943 else
5944 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005945 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005946 if (add_count > 0)
5947 nextlist->t[nextlist->n - 1].count = add_count;
5948 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005949 }
5950
5951 } /* for (thislist = thislist; thislist->state; thislist++) */
5952
Bram Moolenaare23febd2013-05-26 18:40:14 +02005953 /* Look for the start of a match in the current position by adding the
5954 * start state to the list of states.
5955 * The first found match is the leftmost one, thus the order of states
5956 * matters!
5957 * Do not add the start state in recursive calls of nfa_regmatch(),
5958 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005959 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005960 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005961 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005962 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02005963 && reglnum == 0
5964 && clen != 0
5965 && (ireg_maxcol == 0
5966 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005967 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005968 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005969 ? (reglnum < nfa_endp->se_u.pos.lnum
5970 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005971 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005972 < nfa_endp->se_u.pos.col))
5973 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005974 {
5975#ifdef ENABLE_LOG
5976 fprintf(log_fd, "(---) STARTSTATE\n");
5977#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005978 /* Inline optimized code for addstate() if we know the state is
5979 * the first MOPEN. */
5980 if (toplevel)
5981 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005982 int add = TRUE;
5983 int c;
5984
5985 if (prog->regstart != NUL && clen != 0)
5986 {
5987 if (nextlist->n == 0)
5988 {
5989 colnr_T col = (colnr_T)(reginput - regline) + clen;
5990
5991 /* Nextlist is empty, we can skip ahead to the
5992 * character that must appear at the start. */
5993 if (skip_to_start(prog->regstart, &col) == FAIL)
5994 break;
5995#ifdef ENABLE_LOG
5996 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
5997 col - ((colnr_T)(reginput - regline) + clen));
5998#endif
5999 reginput = regline + col - clen;
6000 }
6001 else
6002 {
6003 /* Checking if the required start character matches is
6004 * cheaper than adding a state that won't match. */
6005 c = PTR2CHAR(reginput + clen);
6006 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6007 != MB_TOLOWER(prog->regstart)))
6008 {
6009#ifdef ENABLE_LOG
6010 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6011#endif
6012 add = FALSE;
6013 }
6014 }
6015 }
6016
6017 if (add)
6018 {
6019 if (REG_MULTI)
6020 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006021 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006022 else
6023 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006024 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006025 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006026 }
6027 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006028 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006029 }
6030
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006031#ifdef ENABLE_LOG
6032 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006033 {
6034 int i;
6035
6036 for (i = 0; i < thislist->n; i++)
6037 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6038 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006039 fprintf(log_fd, "\n");
6040#endif
6041
6042nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006043 /* Advance to the next character, or advance to the next line, or
6044 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006045 if (clen != 0)
6046 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006047 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6048 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006049 reg_nextline();
6050 else
6051 break;
6052 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006053
6054#ifdef ENABLE_LOG
6055 if (log_fd != stderr)
6056 fclose(log_fd);
6057 log_fd = NULL;
6058#endif
6059
6060theend:
6061 /* Free memory */
6062 vim_free(list[0].t);
6063 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006064 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006065#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006066#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 fclose(debug);
6068#endif
6069
Bram Moolenaar963fee22013-05-26 21:47:28 +02006070 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006071}
6072
6073/*
6074 * Try match of "prog" with at regline["col"].
6075 * Returns 0 for failure, number of lines contained in the match otherwise.
6076 */
6077 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006078nfa_regtry(prog, col)
6079 nfa_regprog_T *prog;
6080 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006081{
6082 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006083 regsubs_T subs, m;
6084 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006085#ifdef ENABLE_LOG
6086 FILE *f;
6087#endif
6088
6089 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090
6091#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006092 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 if (f != NULL)
6094 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006095 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006096#ifdef DEBUG
6097 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6098#endif
6099 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006100 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006101 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 fprintf(f, "\n\n");
6103 fclose(f);
6104 }
6105 else
6106 EMSG(_("Could not open temporary log file for writing "));
6107#endif
6108
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006109 clear_sub(&subs.norm);
6110 clear_sub(&m.norm);
6111#ifdef FEAT_SYN_HL
6112 clear_sub(&subs.synt);
6113 clear_sub(&m.synt);
6114#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006116 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117 return 0;
6118
6119 cleanup_subexpr();
6120 if (REG_MULTI)
6121 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006122 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006123 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006124 reg_startpos[i] = subs.norm.list.multi[i].start;
6125 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126 }
6127
6128 if (reg_startpos[0].lnum < 0)
6129 {
6130 reg_startpos[0].lnum = 0;
6131 reg_startpos[0].col = col;
6132 }
6133 if (reg_endpos[0].lnum < 0)
6134 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006135 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006136 reg_endpos[0].lnum = reglnum;
6137 reg_endpos[0].col = (int)(reginput - regline);
6138 }
6139 else
6140 /* Use line number of "\ze". */
6141 reglnum = reg_endpos[0].lnum;
6142 }
6143 else
6144 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006145 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006146 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006147 reg_startp[i] = subs.norm.list.line[i].start;
6148 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006149 }
6150
6151 if (reg_startp[0] == NULL)
6152 reg_startp[0] = regline + col;
6153 if (reg_endp[0] == NULL)
6154 reg_endp[0] = reginput;
6155 }
6156
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006157#ifdef FEAT_SYN_HL
6158 /* Package any found \z(...\) matches for export. Default is none. */
6159 unref_extmatch(re_extmatch_out);
6160 re_extmatch_out = NULL;
6161
6162 if (prog->reghasz == REX_SET)
6163 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006164 cleanup_zsubexpr();
6165 re_extmatch_out = make_extmatch();
6166 for (i = 0; i < subs.synt.in_use; i++)
6167 {
6168 if (REG_MULTI)
6169 {
6170 struct multipos *mpos = &subs.synt.list.multi[i];
6171
6172 /* Only accept single line matches. */
6173 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6174 re_extmatch_out->matches[i] =
6175 vim_strnsave(reg_getline(mpos->start.lnum)
6176 + mpos->start.col,
6177 mpos->end.col - mpos->start.col);
6178 }
6179 else
6180 {
6181 struct linepos *lpos = &subs.synt.list.line[i];
6182
6183 if (lpos->start != NULL && lpos->end != NULL)
6184 re_extmatch_out->matches[i] =
6185 vim_strnsave(lpos->start,
6186 (int)(lpos->end - lpos->start));
6187 }
6188 }
6189 }
6190#endif
6191
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006192 return 1 + reglnum;
6193}
6194
6195/*
6196 * Match a regexp against a string ("line" points to the string) or multiple
6197 * lines ("line" is NULL, use reg_getline()).
6198 *
6199 * Returns 0 for failure, number of lines contained in the match otherwise.
6200 */
6201 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006202nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006203 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006204 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006205{
6206 nfa_regprog_T *prog;
6207 long retval = 0L;
6208 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006209 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006210
6211 if (REG_MULTI)
6212 {
6213 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6214 line = reg_getline((linenr_T)0); /* relative to the cursor */
6215 reg_startpos = reg_mmatch->startpos;
6216 reg_endpos = reg_mmatch->endpos;
6217 }
6218 else
6219 {
6220 prog = (nfa_regprog_T *)reg_match->regprog;
6221 reg_startp = reg_match->startp;
6222 reg_endp = reg_match->endp;
6223 }
6224
6225 /* Be paranoid... */
6226 if (prog == NULL || line == NULL)
6227 {
6228 EMSG(_(e_null));
6229 goto theend;
6230 }
6231
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006232 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6233 if (prog->regflags & RF_ICASE)
6234 ireg_ic = TRUE;
6235 else if (prog->regflags & RF_NOICASE)
6236 ireg_ic = FALSE;
6237
6238#ifdef FEAT_MBYTE
6239 /* If pattern contains "\Z" overrule value of ireg_icombine */
6240 if (prog->regflags & RF_ICOMBINE)
6241 ireg_icombine = TRUE;
6242#endif
6243
6244 regline = line;
6245 reglnum = 0; /* relative to line */
6246
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006247 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006248 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006249 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006250 nfa_listid = 1;
6251 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006252#ifdef DEBUG
6253 nfa_regengine.expr = prog->pattern;
6254#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006255
Bram Moolenaard89616e2013-06-06 18:46:06 +02006256 if (prog->reganch && col > 0)
6257 return 0L;
6258
Bram Moolenaar473de612013-06-08 18:19:48 +02006259 need_clear_subexpr = TRUE;
6260#ifdef FEAT_SYN_HL
6261 /* Clear the external match subpointers if necessary. */
6262 if (prog->reghasz == REX_SET)
6263 {
6264 nfa_has_zsubexpr = TRUE;
6265 need_clear_zsubexpr = TRUE;
6266 }
6267 else
6268 nfa_has_zsubexpr = FALSE;
6269#endif
6270
Bram Moolenaard89616e2013-06-06 18:46:06 +02006271 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006272 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006273 /* Skip ahead until a character we know the match must start with.
6274 * When there is none there is no match. */
6275 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006276 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006277
Bram Moolenaar473de612013-06-08 18:19:48 +02006278 /* If match_text is set it contains the full text that must match.
6279 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006280 if (prog->match_text != NULL
6281#ifdef FEAT_MBYTE
6282 && !ireg_icombine
6283#endif
6284 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006285 return find_match_text(col, prog->regstart, prog->match_text);
6286 }
6287
Bram Moolenaard89616e2013-06-06 18:46:06 +02006288 /* If the start column is past the maximum column: no need to try. */
6289 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6290 goto theend;
6291
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006292 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006293 for (i = 0; i < nstate; ++i)
6294 {
6295 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006296 prog->state[i].lastlist[0] = 0;
6297 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006298 }
6299
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006300 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006301
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006302#ifdef DEBUG
6303 nfa_regengine.expr = NULL;
6304#endif
6305
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006306theend:
6307 return retval;
6308}
6309
6310/*
6311 * Compile a regular expression into internal code for the NFA matcher.
6312 * Returns the program in allocated space. Returns NULL for an error.
6313 */
6314 static regprog_T *
6315nfa_regcomp(expr, re_flags)
6316 char_u *expr;
6317 int re_flags;
6318{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006319 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006320 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006321 int *postfix;
6322
6323 if (expr == NULL)
6324 return NULL;
6325
6326#ifdef DEBUG
6327 nfa_regengine.expr = expr;
6328#endif
6329
6330 init_class_tab();
6331
6332 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6333 return NULL;
6334
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006335 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006336 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006337 postfix = re2post();
6338 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006339 {
6340 /* TODO: only give this error for debugging? */
6341 if (post_ptr >= post_end)
6342 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006343 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006344 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006345
6346 /*
6347 * In order to build the NFA, we parse the input regexp twice:
6348 * 1. first pass to count size (so we can allocate space)
6349 * 2. second to emit code
6350 */
6351#ifdef ENABLE_LOG
6352 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006353 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006354
6355 if (f != NULL)
6356 {
6357 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6358 fclose(f);
6359 }
6360 }
6361#endif
6362
6363 /*
6364 * PASS 1
6365 * Count number of NFA states in "nstate". Do not build the NFA.
6366 */
6367 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006368
6369 /* Space for compiled regexp */
6370 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
6371 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6372 if (prog == NULL)
6373 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006374 state_ptr = prog->state;
6375
6376 /*
6377 * PASS 2
6378 * Build the NFA
6379 */
6380 prog->start = post2nfa(postfix, post_ptr, FALSE);
6381 if (prog->start == NULL)
6382 goto fail;
6383
6384 prog->regflags = regflags;
6385 prog->engine = &nfa_regengine;
6386 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006387 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006388 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006389 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006390
6391 prog->reganch = nfa_get_reganch(prog->start, 0);
6392 prog->regstart = nfa_get_regstart(prog->start, 0);
6393
Bram Moolenaar473de612013-06-08 18:19:48 +02006394 prog->match_text = nfa_get_match_text(prog->start);
6395
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396#ifdef ENABLE_LOG
6397 nfa_postfix_dump(expr, OK);
6398 nfa_dump(prog);
6399#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006400#ifdef FEAT_SYN_HL
6401 /* Remember whether this pattern has any \z specials in it. */
6402 prog->reghasz = re_has_z;
6403#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006404#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006405 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006406 nfa_regengine.expr = NULL;
6407#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408
6409out:
6410 vim_free(post_start);
6411 post_start = post_ptr = post_end = NULL;
6412 state_ptr = NULL;
6413 return (regprog_T *)prog;
6414
6415fail:
6416 vim_free(prog);
6417 prog = NULL;
6418#ifdef ENABLE_LOG
6419 nfa_postfix_dump(expr, FAIL);
6420#endif
6421#ifdef DEBUG
6422 nfa_regengine.expr = NULL;
6423#endif
6424 goto out;
6425}
6426
Bram Moolenaar473de612013-06-08 18:19:48 +02006427/*
6428 * Free a compiled regexp program, returned by nfa_regcomp().
6429 */
6430 static void
6431nfa_regfree(prog)
6432 regprog_T *prog;
6433{
6434 if (prog != NULL)
6435 {
6436 vim_free(((nfa_regprog_T *)prog)->match_text);
6437#ifdef DEBUG
6438 vim_free(((nfa_regprog_T *)prog)->pattern);
6439#endif
6440 vim_free(prog);
6441 }
6442}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006443
6444/*
6445 * Match a regexp against a string.
6446 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6447 * Uses curbuf for line count and 'iskeyword'.
6448 *
6449 * Return TRUE if there is a match, FALSE if not.
6450 */
6451 static int
6452nfa_regexec(rmp, line, col)
6453 regmatch_T *rmp;
6454 char_u *line; /* string to match against */
6455 colnr_T col; /* column to start looking for match */
6456{
6457 reg_match = rmp;
6458 reg_mmatch = NULL;
6459 reg_maxline = 0;
6460 reg_line_lbr = FALSE;
6461 reg_buf = curbuf;
6462 reg_win = NULL;
6463 ireg_ic = rmp->rm_ic;
6464#ifdef FEAT_MBYTE
6465 ireg_icombine = FALSE;
6466#endif
6467 ireg_maxcol = 0;
6468 return (nfa_regexec_both(line, col) != 0);
6469}
6470
6471#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6472 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6473
6474static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6475
6476/*
6477 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6478 */
6479 static int
6480nfa_regexec_nl(rmp, line, col)
6481 regmatch_T *rmp;
6482 char_u *line; /* string to match against */
6483 colnr_T col; /* column to start looking for match */
6484{
6485 reg_match = rmp;
6486 reg_mmatch = NULL;
6487 reg_maxline = 0;
6488 reg_line_lbr = TRUE;
6489 reg_buf = curbuf;
6490 reg_win = NULL;
6491 ireg_ic = rmp->rm_ic;
6492#ifdef FEAT_MBYTE
6493 ireg_icombine = FALSE;
6494#endif
6495 ireg_maxcol = 0;
6496 return (nfa_regexec_both(line, col) != 0);
6497}
6498#endif
6499
6500
6501/*
6502 * Match a regexp against multiple lines.
6503 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6504 * Uses curbuf for line count and 'iskeyword'.
6505 *
6506 * Return zero if there is no match. Return number of lines contained in the
6507 * match otherwise.
6508 *
6509 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6510 *
6511 * ! Also NOTE : match may actually be in another line. e.g.:
6512 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6513 *
6514 * +-------------------------+
6515 * |a |
6516 * |b |
6517 * |c |
6518 * | |
6519 * +-------------------------+
6520 *
6521 * then nfa_regexec_multi() returns 3. while the original
6522 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6523 *
6524 * FIXME if this behavior is not compatible.
6525 */
6526 static long
6527nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6528 regmmatch_T *rmp;
6529 win_T *win; /* window in which to search or NULL */
6530 buf_T *buf; /* buffer in which to search */
6531 linenr_T lnum; /* nr of line to start looking for match */
6532 colnr_T col; /* column to start looking for match */
6533 proftime_T *tm UNUSED; /* timeout limit or NULL */
6534{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006535 reg_match = NULL;
6536 reg_mmatch = rmp;
6537 reg_buf = buf;
6538 reg_win = win;
6539 reg_firstlnum = lnum;
6540 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6541 reg_line_lbr = FALSE;
6542 ireg_ic = rmp->rmm_ic;
6543#ifdef FEAT_MBYTE
6544 ireg_icombine = FALSE;
6545#endif
6546 ireg_maxcol = rmp->rmm_maxcol;
6547
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006548 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006549}
6550
6551#ifdef DEBUG
6552# undef ENABLE_LOG
6553#endif