blob: 42030ac0b4dbc0d0d67f148964c5ef3ed09ffc31 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032enum
33{
34 NFA_SPLIT = -1024,
35 NFA_MATCH,
36 NFA_SKIP_CHAR, /* matches a 0-length char */
37 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
38
39 NFA_CONCAT,
40 NFA_OR,
Bram Moolenaar36b3a012013-06-01 12:40:20 +020041 NFA_STAR, /* greedy * */
42 NFA_STAR_NONGREEDY, /* non-greedy * */
43 NFA_QUEST, /* greedy \? */
44 NFA_QUEST_NONGREEDY, /* non-greedy \? */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020045 NFA_NOT, /* used for [^ab] negated char ranges */
46
47 NFA_BOL, /* ^ Begin line */
48 NFA_EOL, /* $ End line */
49 NFA_BOW, /* \< Begin word */
50 NFA_EOW, /* \> End word */
51 NFA_BOF, /* \%^ Begin file */
52 NFA_EOF, /* \%$ End file */
53 NFA_NEWL,
54 NFA_ZSTART, /* Used for \zs */
55 NFA_ZEND, /* Used for \ze */
56 NFA_NOPEN, /* Start of subexpression marked with \%( */
57 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
58 NFA_START_INVISIBLE,
Bram Moolenaar61602c52013-06-01 19:54:43 +020059 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaar87953742013-06-05 18:52:40 +020060 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020061 NFA_END_INVISIBLE,
Bram Moolenaar87953742013-06-05 18:52:40 +020062 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020063 NFA_COMPOSING, /* Next nodes in NFA are part of the
64 composing multibyte char */
65 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020066 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020067
68 /* The following are used only in the postfix form, not in the NFA */
69 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
70 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
71 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
72 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
73 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
74
Bram Moolenaar5714b802013-05-28 22:03:20 +020075 NFA_BACKREF1, /* \1 */
76 NFA_BACKREF2, /* \2 */
77 NFA_BACKREF3, /* \3 */
78 NFA_BACKREF4, /* \4 */
79 NFA_BACKREF5, /* \5 */
80 NFA_BACKREF6, /* \6 */
81 NFA_BACKREF7, /* \7 */
82 NFA_BACKREF8, /* \8 */
83 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +020084#ifdef FEAT_SYN_HL
85 NFA_ZREF1, /* \z1 */
86 NFA_ZREF2, /* \z2 */
87 NFA_ZREF3, /* \z3 */
88 NFA_ZREF4, /* \z4 */
89 NFA_ZREF5, /* \z5 */
90 NFA_ZREF6, /* \z6 */
91 NFA_ZREF7, /* \z7 */
92 NFA_ZREF8, /* \z8 */
93 NFA_ZREF9, /* \z9 */
94#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +020095 NFA_SKIP, /* Skip characters */
96
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020097 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +020098 NFA_MOPEN1,
99 NFA_MOPEN2,
100 NFA_MOPEN3,
101 NFA_MOPEN4,
102 NFA_MOPEN5,
103 NFA_MOPEN6,
104 NFA_MOPEN7,
105 NFA_MOPEN8,
106 NFA_MOPEN9,
107
108 NFA_MCLOSE,
109 NFA_MCLOSE1,
110 NFA_MCLOSE2,
111 NFA_MCLOSE3,
112 NFA_MCLOSE4,
113 NFA_MCLOSE5,
114 NFA_MCLOSE6,
115 NFA_MCLOSE7,
116 NFA_MCLOSE8,
117 NFA_MCLOSE9,
118
119#ifdef FEAT_SYN_HL
120 NFA_ZOPEN,
121 NFA_ZOPEN1,
122 NFA_ZOPEN2,
123 NFA_ZOPEN3,
124 NFA_ZOPEN4,
125 NFA_ZOPEN5,
126 NFA_ZOPEN6,
127 NFA_ZOPEN7,
128 NFA_ZOPEN8,
129 NFA_ZOPEN9,
130
131 NFA_ZCLOSE,
132 NFA_ZCLOSE1,
133 NFA_ZCLOSE2,
134 NFA_ZCLOSE3,
135 NFA_ZCLOSE4,
136 NFA_ZCLOSE5,
137 NFA_ZCLOSE6,
138 NFA_ZCLOSE7,
139 NFA_ZCLOSE8,
140 NFA_ZCLOSE9,
141#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200142
143 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200144 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200145 NFA_ANYOF, /* Match any character in this string. */
146 NFA_ANYBUT, /* Match any character not in this string. */
147 NFA_IDENT, /* Match identifier char */
148 NFA_SIDENT, /* Match identifier char but no digit */
149 NFA_KWORD, /* Match keyword char */
150 NFA_SKWORD, /* Match word char but no digit */
151 NFA_FNAME, /* Match file name char */
152 NFA_SFNAME, /* Match file name char but no digit */
153 NFA_PRINT, /* Match printable char */
154 NFA_SPRINT, /* Match printable char but no digit */
155 NFA_WHITE, /* Match whitespace char */
156 NFA_NWHITE, /* Match non-whitespace char */
157 NFA_DIGIT, /* Match digit char */
158 NFA_NDIGIT, /* Match non-digit char */
159 NFA_HEX, /* Match hex char */
160 NFA_NHEX, /* Match non-hex char */
161 NFA_OCTAL, /* Match octal char */
162 NFA_NOCTAL, /* Match non-octal char */
163 NFA_WORD, /* Match word char */
164 NFA_NWORD, /* Match non-word char */
165 NFA_HEAD, /* Match head char */
166 NFA_NHEAD, /* Match non-head char */
167 NFA_ALPHA, /* Match alpha char */
168 NFA_NALPHA, /* Match non-alpha char */
169 NFA_LOWER, /* Match lowercase char */
170 NFA_NLOWER, /* Match non-lowercase char */
171 NFA_UPPER, /* Match uppercase char */
172 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200173
174 NFA_CURSOR, /* Match cursor pos */
175 NFA_LNUM, /* Match line number */
176 NFA_LNUM_GT, /* Match > line number */
177 NFA_LNUM_LT, /* Match < line number */
178 NFA_COL, /* Match cursor column */
179 NFA_COL_GT, /* Match > cursor column */
180 NFA_COL_LT, /* Match < cursor column */
181 NFA_VCOL, /* Match cursor virtual column */
182 NFA_VCOL_GT, /* Match > cursor virtual column */
183 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200184 NFA_MARK, /* Match mark */
185 NFA_MARK_GT, /* Match > mark */
186 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200187 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200188
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200189 NFA_FIRST_NL = NFA_ANY + ADD_NL,
190 NFA_LAST_NL = NFA_NUPPER + ADD_NL,
191
192 /* Character classes [:alnum:] etc */
193 NFA_CLASS_ALNUM,
194 NFA_CLASS_ALPHA,
195 NFA_CLASS_BLANK,
196 NFA_CLASS_CNTRL,
197 NFA_CLASS_DIGIT,
198 NFA_CLASS_GRAPH,
199 NFA_CLASS_LOWER,
200 NFA_CLASS_PRINT,
201 NFA_CLASS_PUNCT,
202 NFA_CLASS_SPACE,
203 NFA_CLASS_UPPER,
204 NFA_CLASS_XDIGIT,
205 NFA_CLASS_TAB,
206 NFA_CLASS_RETURN,
207 NFA_CLASS_BACKSPACE,
208 NFA_CLASS_ESCAPE
209};
210
211/* Keep in sync with classchars. */
212static int nfa_classcodes[] = {
213 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
214 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
215 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
216 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
217 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
218 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
219 NFA_UPPER, NFA_NUPPER
220};
221
222static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
223
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200224/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200225static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200226
Bram Moolenaar428e9872013-05-30 17:05:39 +0200227/* NFA regexp \1 .. \9 encountered. */
228static int nfa_has_backref;
229
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200230#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200231/* NFA regexp has \z( ), set zsubexpr. */
232static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200233#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200234
Bram Moolenaar963fee22013-05-26 21:47:28 +0200235/* Number of sub expressions actually being used during execution. 1 if only
236 * the whole match (subexpr 0) is used. */
237static int nfa_nsubexpr;
238
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200239static int *post_start; /* holds the postfix form of r.e. */
240static int *post_end;
241static int *post_ptr;
242
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200243static int nstate; /* Number of states in the NFA. Also used when
244 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200245static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaar307aa162013-06-02 16:34:21 +0200247/* If not NULL match must end at this position */
248static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200250/* listid is global, so that it increases on recursive calls to
251 * nfa_regmatch(), which means we don't have to clear the lastlist field of
252 * all the states. */
253static int nfa_listid;
254static int nfa_alt_listid;
255
256/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
257static int nfa_ll_index = 0;
258
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200259static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200260static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
261static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
263static int nfa_emit_equi_class __ARGS((int c, int neg));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200264static int nfa_regatom __ARGS((void));
265static int nfa_regpiece __ARGS((void));
266static int nfa_regconcat __ARGS((void));
267static int nfa_regbranch __ARGS((void));
268static int nfa_reg __ARGS((int paren));
269#ifdef DEBUG
270static void nfa_set_code __ARGS((int c));
271static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200272static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
273static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200274static void nfa_dump __ARGS((nfa_regprog_T *prog));
275#endif
276static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200277static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200278static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
279static int check_char_class __ARGS((int class, int c));
280static void st_error __ARGS((int *postfix, int *end, int *p));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200281static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
282static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200283static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200284static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200285static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
286static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
287static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
288static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
289
290/* helper functions used when doing re2post() ... regatom() parsing */
291#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200292 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200293 return FAIL; \
294 *post_ptr++ = c; \
295 } while (0)
296
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297/*
298 * Initialize internal variables before NFA compilation.
299 * Return OK on success, FAIL otherwise.
300 */
301 static int
302nfa_regcomp_start(expr, re_flags)
303 char_u *expr;
304 int re_flags; /* see vim_regcomp() */
305{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200306 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200307 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308
309 nstate = 0;
310 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200311 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200312 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200314 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200315 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200316 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200317
318 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200319 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200321 post_start = (int *)lalloc(postfix_size, TRUE);
322 if (post_start == NULL)
323 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200325 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200327 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200328
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200329 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330 regcomp_start(expr, re_flags);
331
332 return OK;
333}
334
335/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200336 * Figure out if the NFA state list starts with an anchor, must match at start
337 * of the line.
338 */
339 static int
340nfa_get_reganch(start, depth)
341 nfa_state_T *start;
342 int depth;
343{
344 nfa_state_T *p = start;
345
346 if (depth > 4)
347 return 0;
348
349 while (p != NULL)
350 {
351 switch (p->c)
352 {
353 case NFA_BOL:
354 case NFA_BOF:
355 return 1; /* yes! */
356
357 case NFA_ZSTART:
358 case NFA_ZEND:
359 case NFA_CURSOR:
360 case NFA_VISUAL:
361
362 case NFA_MOPEN:
363 case NFA_MOPEN1:
364 case NFA_MOPEN2:
365 case NFA_MOPEN3:
366 case NFA_MOPEN4:
367 case NFA_MOPEN5:
368 case NFA_MOPEN6:
369 case NFA_MOPEN7:
370 case NFA_MOPEN8:
371 case NFA_MOPEN9:
372 case NFA_NOPEN:
373#ifdef FEAT_SYN_HL
374 case NFA_ZOPEN:
375 case NFA_ZOPEN1:
376 case NFA_ZOPEN2:
377 case NFA_ZOPEN3:
378 case NFA_ZOPEN4:
379 case NFA_ZOPEN5:
380 case NFA_ZOPEN6:
381 case NFA_ZOPEN7:
382 case NFA_ZOPEN8:
383 case NFA_ZOPEN9:
384#endif
385 p = p->out;
386 break;
387
388 case NFA_SPLIT:
389 return nfa_get_reganch(p->out, depth + 1)
390 && nfa_get_reganch(p->out1, depth + 1);
391
392 default:
393 return 0; /* noooo */
394 }
395 }
396 return 0;
397}
398
399/*
400 * Figure out if the NFA state list starts with a character which must match
401 * at start of the match.
402 */
403 static int
404nfa_get_regstart(start, depth)
405 nfa_state_T *start;
406 int depth;
407{
408 nfa_state_T *p = start;
409
410 if (depth > 4)
411 return 0;
412
413 while (p != NULL)
414 {
415 switch (p->c)
416 {
417 /* all kinds of zero-width matches */
418 case NFA_BOL:
419 case NFA_BOF:
420 case NFA_BOW:
421 case NFA_EOW:
422 case NFA_ZSTART:
423 case NFA_ZEND:
424 case NFA_CURSOR:
425 case NFA_VISUAL:
426 case NFA_LNUM:
427 case NFA_LNUM_GT:
428 case NFA_LNUM_LT:
429 case NFA_COL:
430 case NFA_COL_GT:
431 case NFA_COL_LT:
432 case NFA_VCOL:
433 case NFA_VCOL_GT:
434 case NFA_VCOL_LT:
435 case NFA_MARK:
436 case NFA_MARK_GT:
437 case NFA_MARK_LT:
438
439 case NFA_MOPEN:
440 case NFA_MOPEN1:
441 case NFA_MOPEN2:
442 case NFA_MOPEN3:
443 case NFA_MOPEN4:
444 case NFA_MOPEN5:
445 case NFA_MOPEN6:
446 case NFA_MOPEN7:
447 case NFA_MOPEN8:
448 case NFA_MOPEN9:
449 case NFA_NOPEN:
450#ifdef FEAT_SYN_HL
451 case NFA_ZOPEN:
452 case NFA_ZOPEN1:
453 case NFA_ZOPEN2:
454 case NFA_ZOPEN3:
455 case NFA_ZOPEN4:
456 case NFA_ZOPEN5:
457 case NFA_ZOPEN6:
458 case NFA_ZOPEN7:
459 case NFA_ZOPEN8:
460 case NFA_ZOPEN9:
461#endif
462 p = p->out;
463 break;
464
465 case NFA_SPLIT:
466 {
467 int c1 = nfa_get_regstart(p->out, depth + 1);
468 int c2 = nfa_get_regstart(p->out1, depth + 1);
469
470 if (c1 == c2)
471 return c1; /* yes! */
472 return 0;
473 }
474
475 default:
476 if (p->c > 0 && !p->negated)
477 return p->c; /* yes! */
478 return 0;
479 }
480 }
481 return 0;
482}
483
484/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200485 * Allocate more space for post_start. Called when
486 * running above the estimated number of states.
487 */
488 static int
489realloc_post_list()
490{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200491 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200492 int new_max = nstate_max + 1000;
493 int *new_start;
494 int *old_start;
495
496 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
497 if (new_start == NULL)
498 return FAIL;
499 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200500 old_start = post_start;
501 post_start = new_start;
502 post_ptr = new_start + (post_ptr - old_start);
503 post_end = post_start + new_max;
504 vim_free(old_start);
505 return OK;
506}
507
508/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200509 * Search between "start" and "end" and try to recognize a
510 * character class in expanded form. For example [0-9].
511 * On success, return the id the character class to be emitted.
512 * On failure, return 0 (=FAIL)
513 * Start points to the first char of the range, while end should point
514 * to the closing brace.
515 */
516 static int
517nfa_recognize_char_class(start, end, extra_newl)
518 char_u *start;
519 char_u *end;
520 int extra_newl;
521{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200522# define CLASS_not 0x80
523# define CLASS_af 0x40
524# define CLASS_AF 0x20
525# define CLASS_az 0x10
526# define CLASS_AZ 0x08
527# define CLASS_o7 0x04
528# define CLASS_o9 0x02
529# define CLASS_underscore 0x01
530
531 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200532 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200533 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200534
535 if (extra_newl == TRUE)
536 newl = TRUE;
537
538 if (*end != ']')
539 return FAIL;
540 p = start;
541 if (*p == '^')
542 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200543 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200544 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200545 }
546
547 while (p < end)
548 {
549 if (p + 2 < end && *(p + 1) == '-')
550 {
551 switch (*p)
552 {
553 case '0':
554 if (*(p + 2) == '9')
555 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200556 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200557 break;
558 }
559 else
560 if (*(p + 2) == '7')
561 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200562 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200563 break;
564 }
565 case 'a':
566 if (*(p + 2) == 'z')
567 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200568 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200569 break;
570 }
571 else
572 if (*(p + 2) == 'f')
573 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200574 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200575 break;
576 }
577 case 'A':
578 if (*(p + 2) == 'Z')
579 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200580 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200581 break;
582 }
583 else
584 if (*(p + 2) == 'F')
585 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200586 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200587 break;
588 }
589 /* FALLTHROUGH */
590 default:
591 return FAIL;
592 }
593 p += 3;
594 }
595 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
596 {
597 newl = TRUE;
598 p += 2;
599 }
600 else if (*p == '_')
601 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200602 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200603 p ++;
604 }
605 else if (*p == '\n')
606 {
607 newl = TRUE;
608 p ++;
609 }
610 else
611 return FAIL;
612 } /* while (p < end) */
613
614 if (p != end)
615 return FAIL;
616
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 if (newl == TRUE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200618 extra_newl = ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200619
620 switch (config)
621 {
622 case CLASS_o9:
623 return extra_newl + NFA_DIGIT;
624 case CLASS_not | CLASS_o9:
625 return extra_newl + NFA_NDIGIT;
626 case CLASS_af | CLASS_AF | CLASS_o9:
627 return extra_newl + NFA_HEX;
628 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
629 return extra_newl + NFA_NHEX;
630 case CLASS_o7:
631 return extra_newl + NFA_OCTAL;
632 case CLASS_not | CLASS_o7:
633 return extra_newl + NFA_NOCTAL;
634 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
635 return extra_newl + NFA_WORD;
636 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
637 return extra_newl + NFA_NWORD;
638 case CLASS_az | CLASS_AZ | CLASS_underscore:
639 return extra_newl + NFA_HEAD;
640 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
641 return extra_newl + NFA_NHEAD;
642 case CLASS_az | CLASS_AZ:
643 return extra_newl + NFA_ALPHA;
644 case CLASS_not | CLASS_az | CLASS_AZ:
645 return extra_newl + NFA_NALPHA;
646 case CLASS_az:
647 return extra_newl + NFA_LOWER;
648 case CLASS_not | CLASS_az:
649 return extra_newl + NFA_NLOWER;
650 case CLASS_AZ:
651 return extra_newl + NFA_UPPER;
652 case CLASS_not | CLASS_AZ:
653 return extra_newl + NFA_NUPPER;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200654 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200656}
657
658/*
659 * Produce the bytes for equivalence class "c".
660 * Currently only handles latin1, latin9 and utf-8.
661 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
662 * equivalent to 'a OR b OR c'
663 *
664 * NOTE! When changing this function, also update reg_equi_class()
665 */
666 static int
667nfa_emit_equi_class(c, neg)
668 int c;
669 int neg;
670{
671 int first = TRUE;
672 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
673#define EMIT2(c) \
674 EMIT(c); \
675 if (neg == TRUE) { \
676 EMIT(NFA_NOT); \
677 } \
678 if (first == FALSE) \
679 EMIT(glue); \
680 else \
681 first = FALSE; \
682
683#ifdef FEAT_MBYTE
684 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
685 || STRCMP(p_enc, "iso-8859-15") == 0)
686#endif
687 {
688 switch (c)
689 {
690 case 'A': case '\300': case '\301': case '\302':
691 case '\303': case '\304': case '\305':
692 EMIT2('A'); EMIT2('\300'); EMIT2('\301');
693 EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
694 EMIT2('\305');
695 return OK;
696
697 case 'C': case '\307':
698 EMIT2('C'); EMIT2('\307');
699 return OK;
700
701 case 'E': case '\310': case '\311': case '\312': case '\313':
702 EMIT2('E'); EMIT2('\310'); EMIT2('\311');
703 EMIT2('\312'); EMIT2('\313');
704 return OK;
705
706 case 'I': case '\314': case '\315': case '\316': case '\317':
707 EMIT2('I'); EMIT2('\314'); EMIT2('\315');
708 EMIT2('\316'); EMIT2('\317');
709 return OK;
710
711 case 'N': case '\321':
712 EMIT2('N'); EMIT2('\321');
713 return OK;
714
715 case 'O': case '\322': case '\323': case '\324': case '\325':
716 case '\326':
717 EMIT2('O'); EMIT2('\322'); EMIT2('\323');
718 EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
719 return OK;
720
721 case 'U': case '\331': case '\332': case '\333': case '\334':
722 EMIT2('U'); EMIT2('\331'); EMIT2('\332');
723 EMIT2('\333'); EMIT2('\334');
724 return OK;
725
726 case 'Y': case '\335':
727 EMIT2('Y'); EMIT2('\335');
728 return OK;
729
730 case 'a': case '\340': case '\341': case '\342':
731 case '\343': case '\344': case '\345':
732 EMIT2('a'); EMIT2('\340'); EMIT2('\341');
733 EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
734 EMIT2('\345');
735 return OK;
736
737 case 'c': case '\347':
738 EMIT2('c'); EMIT2('\347');
739 return OK;
740
741 case 'e': case '\350': case '\351': case '\352': case '\353':
742 EMIT2('e'); EMIT2('\350'); EMIT2('\351');
743 EMIT2('\352'); EMIT2('\353');
744 return OK;
745
746 case 'i': case '\354': case '\355': case '\356': case '\357':
747 EMIT2('i'); EMIT2('\354'); EMIT2('\355');
748 EMIT2('\356'); EMIT2('\357');
749 return OK;
750
751 case 'n': case '\361':
752 EMIT2('n'); EMIT2('\361');
753 return OK;
754
755 case 'o': case '\362': case '\363': case '\364': case '\365':
756 case '\366':
757 EMIT2('o'); EMIT2('\362'); EMIT2('\363');
758 EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
759 return OK;
760
761 case 'u': case '\371': case '\372': case '\373': case '\374':
762 EMIT2('u'); EMIT2('\371'); EMIT2('\372');
763 EMIT2('\373'); EMIT2('\374');
764 return OK;
765
766 case 'y': case '\375': case '\377':
767 EMIT2('y'); EMIT2('\375'); EMIT2('\377');
768 return OK;
769
770 default:
771 return FAIL;
772 }
773 }
774
775 EMIT(c);
776 return OK;
777#undef EMIT2
778}
779
780/*
781 * Code to parse regular expression.
782 *
783 * We try to reuse parsing functions in regexp.c to
784 * minimize surprise and keep the syntax consistent.
785 */
786
787/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 * Parse the lowest level.
789 *
790 * An atom can be one of a long list of items. Many atoms match one character
791 * in the text. It is often an ordinary character or a character class.
792 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
793 * is only for syntax highlighting.
794 *
795 * atom ::= ordinary-atom
796 * or \( pattern \)
797 * or \%( pattern \)
798 * or \z( pattern \)
799 */
800 static int
801nfa_regatom()
802{
803 int c;
804 int charclass;
805 int equiclass;
806 int collclass;
807 int got_coll_char;
808 char_u *p;
809 char_u *endp;
810#ifdef FEAT_MBYTE
811 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812#endif
813 int extra = 0;
814 int first;
815 int emit_range;
816 int negated;
817 int result;
818 int startc = -1;
819 int endc = -1;
820 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200821 int glue; /* ID that will "glue" nodes together */
822
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200824 switch (c)
825 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200826 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200827 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
828
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200829 case Magic('^'):
830 EMIT(NFA_BOL);
831 break;
832
833 case Magic('$'):
834 EMIT(NFA_EOL);
835#if defined(FEAT_SYN_HL) || defined(PROTO)
836 had_eol = TRUE;
837#endif
838 break;
839
840 case Magic('<'):
841 EMIT(NFA_BOW);
842 break;
843
844 case Magic('>'):
845 EMIT(NFA_EOW);
846 break;
847
848 case Magic('_'):
849 c = no_Magic(getchr());
850 if (c == '^') /* "\_^" is start-of-line */
851 {
852 EMIT(NFA_BOL);
853 break;
854 }
855 if (c == '$') /* "\_$" is end-of-line */
856 {
857 EMIT(NFA_EOL);
858#if defined(FEAT_SYN_HL) || defined(PROTO)
859 had_eol = TRUE;
860#endif
861 break;
862 }
863
864 extra = ADD_NL;
865
866 /* "\_[" is collection plus newline */
867 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200868 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200869
870 /* "\_x" is character class plus newline */
871 /*FALLTHROUGH*/
872
873 /*
874 * Character classes.
875 */
876 case Magic('.'):
877 case Magic('i'):
878 case Magic('I'):
879 case Magic('k'):
880 case Magic('K'):
881 case Magic('f'):
882 case Magic('F'):
883 case Magic('p'):
884 case Magic('P'):
885 case Magic('s'):
886 case Magic('S'):
887 case Magic('d'):
888 case Magic('D'):
889 case Magic('x'):
890 case Magic('X'):
891 case Magic('o'):
892 case Magic('O'):
893 case Magic('w'):
894 case Magic('W'):
895 case Magic('h'):
896 case Magic('H'):
897 case Magic('a'):
898 case Magic('A'):
899 case Magic('l'):
900 case Magic('L'):
901 case Magic('u'):
902 case Magic('U'):
903 p = vim_strchr(classchars, no_Magic(c));
904 if (p == NULL)
905 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200906 EMSGN("INTERNAL: Unknown character class char: %ld", c);
907 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200908 }
909#ifdef FEAT_MBYTE
910 /* When '.' is followed by a composing char ignore the dot, so that
911 * the composing char is matched here. */
912 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
913 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200914 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200915 c = getchr();
916 goto nfa_do_multibyte;
917 }
918#endif
919 EMIT(nfa_classcodes[p - classchars]);
920 if (extra == ADD_NL)
921 {
922 EMIT(NFA_NEWL);
923 EMIT(NFA_OR);
924 regflags |= RF_HASNL;
925 }
926 break;
927
928 case Magic('n'):
929 if (reg_string)
930 /* In a string "\n" matches a newline character. */
931 EMIT(NL);
932 else
933 {
934 /* In buffer text "\n" matches the end of a line. */
935 EMIT(NFA_NEWL);
936 regflags |= RF_HASNL;
937 }
938 break;
939
940 case Magic('('):
941 if (nfa_reg(REG_PAREN) == FAIL)
942 return FAIL; /* cascaded error */
943 break;
944
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200945 case Magic('|'):
946 case Magic('&'):
947 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +0200948 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200949 return FAIL;
950
951 case Magic('='):
952 case Magic('?'):
953 case Magic('+'):
954 case Magic('@'):
955 case Magic('*'):
956 case Magic('{'):
957 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +0200958 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200959 return FAIL;
960
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +0200961 case Magic('~'):
962 {
963 char_u *lp;
964
965 /* Previous substitute pattern.
966 * Generated as "\%(pattern\)". */
967 if (reg_prev_sub == NULL)
968 {
969 EMSG(_(e_nopresub));
970 return FAIL;
971 }
972 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
973 {
974 EMIT(PTR2CHAR(lp));
975 if (lp != reg_prev_sub)
976 EMIT(NFA_CONCAT);
977 }
978 EMIT(NFA_NOPEN);
979 break;
980 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200981
Bram Moolenaar428e9872013-05-30 17:05:39 +0200982 case Magic('1'):
983 case Magic('2'):
984 case Magic('3'):
985 case Magic('4'):
986 case Magic('5'):
987 case Magic('6'):
988 case Magic('7'):
989 case Magic('8'):
990 case Magic('9'):
991 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
992 nfa_has_backref = TRUE;
993 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200994
995 case Magic('z'):
996 c = no_Magic(getchr());
997 switch (c)
998 {
999 case 's':
1000 EMIT(NFA_ZSTART);
1001 break;
1002 case 'e':
1003 EMIT(NFA_ZEND);
1004 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001005 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001006#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001007 case '1':
1008 case '2':
1009 case '3':
1010 case '4':
1011 case '5':
1012 case '6':
1013 case '7':
1014 case '8':
1015 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001016 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001017 if (reg_do_extmatch != REX_USE)
1018 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001019 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1020 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001021 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001022 re_has_z = REX_USE;
1023 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001024 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001025 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001026 if (reg_do_extmatch != REX_SET)
1027 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001028 if (nfa_reg(REG_ZPAREN) == FAIL)
1029 return FAIL; /* cascaded error */
1030 re_has_z = REX_SET;
1031 break;
1032#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001033 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001034 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001035 no_Magic(c));
1036 return FAIL;
1037 }
1038 break;
1039
1040 case Magic('%'):
1041 c = no_Magic(getchr());
1042 switch (c)
1043 {
1044 /* () without a back reference */
1045 case '(':
1046 if (nfa_reg(REG_NPAREN) == FAIL)
1047 return FAIL;
1048 EMIT(NFA_NOPEN);
1049 break;
1050
1051 case 'd': /* %d123 decimal */
1052 case 'o': /* %o123 octal */
1053 case 'x': /* %xab hex 2 */
1054 case 'u': /* %uabcd hex 4 */
1055 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001056 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001057 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001058
Bram Moolenaar47196582013-05-25 22:04:23 +02001059 switch (c)
1060 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001061 case 'd': nr = getdecchrs(); break;
1062 case 'o': nr = getoctchrs(); break;
1063 case 'x': nr = gethexchrs(2); break;
1064 case 'u': nr = gethexchrs(4); break;
1065 case 'U': nr = gethexchrs(8); break;
1066 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001067 }
1068
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001069 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001070 EMSG2_RET_FAIL(
1071 _("E678: Invalid character after %s%%[dxouU]"),
1072 reg_magic == MAGIC_ALL);
1073 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001074 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001075 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001076 break;
1077
1078 /* Catch \%^ and \%$ regardless of where they appear in the
1079 * pattern -- regardless of whether or not it makes sense. */
1080 case '^':
1081 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001082 break;
1083
1084 case '$':
1085 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 break;
1087
1088 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001089 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001090 break;
1091
1092 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001093 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001094 break;
1095
1096 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001097 {
1098 int n;
1099
1100 /* \%[abc] */
1101 for (n = 0; (c = getchr()) != ']'; ++n)
1102 {
1103 if (c == NUL)
1104 EMSG2_RET_FAIL(_(e_missing_sb),
1105 reg_magic == MAGIC_ALL);
1106 EMIT(c);
1107 }
Bram Moolenaar2976c022013-06-05 21:30:37 +02001108 if (n == 0)
1109 EMSG2_RET_FAIL(_(e_empty_sb),
1110 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001111 EMIT(NFA_OPT_CHARS);
1112 EMIT(n);
1113 break;
1114 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001116 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001117 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001118 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001119 int cmp = c;
1120
1121 if (c == '<' || c == '>')
1122 c = getchr();
1123 while (VIM_ISDIGIT(c))
1124 {
1125 n = n * 10 + (c - '0');
1126 c = getchr();
1127 }
1128 if (c == 'l' || c == 'c' || c == 'v')
1129 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001130 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001131 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001132 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001133 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001134 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001135 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001136 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001137 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001138 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001139 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001140 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001141 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001142 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001143 break;
1144 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001145 else if (c == '\'' && n == 0)
1146 {
1147 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001148 EMIT(cmp == '<' ? NFA_MARK_LT :
1149 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001150 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001151 break;
1152 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001153 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001154 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1155 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001156 return FAIL;
1157 }
1158 break;
1159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001160 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001161collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 /*
1163 * Glue is emitted between several atoms from the [].
1164 * It is either NFA_OR, or NFA_CONCAT.
1165 *
1166 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
1167 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
1168 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
1169 * notation)
1170 *
1171 */
1172
1173
1174/* Emit negation atoms, if needed.
1175 * The CONCAT below merges the NOT with the previous node. */
1176#define TRY_NEG() \
1177 if (negated == TRUE) \
1178 { \
1179 EMIT(NFA_NOT); \
1180 }
1181
1182/* Emit glue between important nodes : CONCAT or OR. */
1183#define EMIT_GLUE() \
1184 if (first == FALSE) \
1185 EMIT(glue); \
1186 else \
1187 first = FALSE;
1188
1189 p = regparse;
1190 endp = skip_anyof(p);
1191 if (*endp == ']')
1192 {
1193 /*
1194 * Try to reverse engineer character classes. For example,
1195 * recognize that [0-9] stands for \d and [A-Za-z_] with \h,
1196 * and perform the necessary substitutions in the NFA.
1197 */
1198 result = nfa_recognize_char_class(regparse, endp,
1199 extra == ADD_NL);
1200 if (result != FAIL)
1201 {
1202 if (result >= NFA_DIGIT && result <= NFA_NUPPER)
1203 EMIT(result);
1204 else /* must be char class + newline */
1205 {
1206 EMIT(result - ADD_NL);
1207 EMIT(NFA_NEWL);
1208 EMIT(NFA_OR);
1209 }
1210 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001211 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 return OK;
1213 }
1214 /*
1215 * Failed to recognize a character class. Use the simple
1216 * version that turns [abc] into 'a' OR 'b' OR 'c'
1217 */
1218 startc = endc = oldstartc = -1;
1219 first = TRUE; /* Emitting first atom in this sequence? */
1220 negated = FALSE;
1221 glue = NFA_OR;
1222 if (*regparse == '^') /* negated range */
1223 {
1224 negated = TRUE;
1225 glue = NFA_CONCAT;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001226 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001227 }
1228 if (*regparse == '-')
1229 {
1230 startc = '-';
1231 EMIT(startc);
1232 TRY_NEG();
1233 EMIT_GLUE();
Bram Moolenaar51a29832013-05-28 22:30:35 +02001234 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001235 }
1236 /* Emit the OR branches for each character in the [] */
1237 emit_range = FALSE;
1238 while (regparse < endp)
1239 {
1240 oldstartc = startc;
1241 startc = -1;
1242 got_coll_char = FALSE;
1243 if (*regparse == '[')
1244 {
1245 /* Check for [: :], [= =], [. .] */
1246 equiclass = collclass = 0;
1247 charclass = get_char_class(&regparse);
1248 if (charclass == CLASS_NONE)
1249 {
1250 equiclass = get_equi_class(&regparse);
1251 if (equiclass == 0)
1252 collclass = get_coll_element(&regparse);
1253 }
1254
1255 /* Character class like [:alpha:] */
1256 if (charclass != CLASS_NONE)
1257 {
1258 switch (charclass)
1259 {
1260 case CLASS_ALNUM:
1261 EMIT(NFA_CLASS_ALNUM);
1262 break;
1263 case CLASS_ALPHA:
1264 EMIT(NFA_CLASS_ALPHA);
1265 break;
1266 case CLASS_BLANK:
1267 EMIT(NFA_CLASS_BLANK);
1268 break;
1269 case CLASS_CNTRL:
1270 EMIT(NFA_CLASS_CNTRL);
1271 break;
1272 case CLASS_DIGIT:
1273 EMIT(NFA_CLASS_DIGIT);
1274 break;
1275 case CLASS_GRAPH:
1276 EMIT(NFA_CLASS_GRAPH);
1277 break;
1278 case CLASS_LOWER:
1279 EMIT(NFA_CLASS_LOWER);
1280 break;
1281 case CLASS_PRINT:
1282 EMIT(NFA_CLASS_PRINT);
1283 break;
1284 case CLASS_PUNCT:
1285 EMIT(NFA_CLASS_PUNCT);
1286 break;
1287 case CLASS_SPACE:
1288 EMIT(NFA_CLASS_SPACE);
1289 break;
1290 case CLASS_UPPER:
1291 EMIT(NFA_CLASS_UPPER);
1292 break;
1293 case CLASS_XDIGIT:
1294 EMIT(NFA_CLASS_XDIGIT);
1295 break;
1296 case CLASS_TAB:
1297 EMIT(NFA_CLASS_TAB);
1298 break;
1299 case CLASS_RETURN:
1300 EMIT(NFA_CLASS_RETURN);
1301 break;
1302 case CLASS_BACKSPACE:
1303 EMIT(NFA_CLASS_BACKSPACE);
1304 break;
1305 case CLASS_ESCAPE:
1306 EMIT(NFA_CLASS_ESCAPE);
1307 break;
1308 }
1309 TRY_NEG();
1310 EMIT_GLUE();
1311 continue;
1312 }
1313 /* Try equivalence class [=a=] and the like */
1314 if (equiclass != 0)
1315 {
1316 result = nfa_emit_equi_class(equiclass, negated);
1317 if (result == FAIL)
1318 {
1319 /* should never happen */
1320 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1321 }
1322 EMIT_GLUE();
1323 continue;
1324 }
1325 /* Try collating class like [. .] */
1326 if (collclass != 0)
1327 {
1328 startc = collclass; /* allow [.a.]-x as a range */
1329 /* Will emit the proper atom at the end of the
1330 * while loop. */
1331 }
1332 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001333 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1334 * start character. */
1335 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 {
1337 emit_range = TRUE;
1338 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001339 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001340 continue; /* reading the end of the range */
1341 }
1342
1343 /* Now handle simple and escaped characters.
1344 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1345 * accepts "\t", "\e", etc., but only when the 'l' flag in
1346 * 'cpoptions' is not included.
1347 * Posix doesn't recognize backslash at all.
1348 */
1349 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001350 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001351 && regparse + 1 <= endp
1352 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001353 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001354 && vim_strchr(REGEXP_ABBR, regparse[1])
1355 != NULL)
1356 )
1357 )
1358 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001359 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001360
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001361 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362 startc = reg_string ? NL : NFA_NEWL;
1363 else
1364 if (*regparse == 'd'
1365 || *regparse == 'o'
1366 || *regparse == 'x'
1367 || *regparse == 'u'
1368 || *regparse == 'U'
1369 )
1370 {
1371 /* TODO(RE) This needs more testing */
1372 startc = coll_get_char();
1373 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001374 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001375 }
1376 else
1377 {
1378 /* \r,\t,\e,\b */
1379 startc = backslash_trans(*regparse);
1380 }
1381 }
1382
1383 /* Normal printable char */
1384 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001385 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001386
1387 /* Previous char was '-', so this char is end of range. */
1388 if (emit_range)
1389 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001390 endc = startc;
1391 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001392 if (startc > endc)
1393 EMSG_RET_FAIL(_(e_invrange));
1394#ifdef FEAT_MBYTE
1395 if (has_mbyte && ((*mb_char2len)(startc) > 1
1396 || (*mb_char2len)(endc) > 1))
1397 {
1398 if (endc > startc + 256)
1399 EMSG_RET_FAIL(_(e_invrange));
1400 /* Emit the range. "startc" was already emitted, so
1401 * skip it. */
1402 for (c = startc + 1; c <= endc; c++)
1403 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001404 EMIT(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 TRY_NEG();
1406 EMIT_GLUE();
1407 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408 }
1409 else
1410#endif
1411 {
1412#ifdef EBCDIC
1413 int alpha_only = FALSE;
1414
1415 /* for alphabetical range skip the gaps
1416 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1417 if (isalpha(startc) && isalpha(endc))
1418 alpha_only = TRUE;
1419#endif
1420 /* Emit the range. "startc" was already emitted, so
1421 * skip it. */
1422 for (c = startc + 1; c <= endc; c++)
1423#ifdef EBCDIC
1424 if (!alpha_only || isalpha(startc))
1425#endif
1426 {
1427 EMIT(c);
1428 TRY_NEG();
1429 EMIT_GLUE();
1430 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001431 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001432 emit_range = FALSE;
1433 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434 }
1435 else
1436 {
1437 /*
1438 * This char (startc) is not part of a range. Just
1439 * emit it.
1440 *
1441 * Normally, simply emit startc. But if we get char
1442 * code=0 from a collating char, then replace it with
1443 * 0x0a.
1444 *
1445 * This is needed to completely mimic the behaviour of
1446 * the backtracking engine.
1447 */
1448 if (got_coll_char == TRUE && startc == 0)
1449 EMIT(0x0a);
1450 else
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001451 EMIT(startc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 TRY_NEG();
1453 EMIT_GLUE();
1454 }
1455
Bram Moolenaar51a29832013-05-28 22:30:35 +02001456 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001457 } /* while (p < endp) */
1458
Bram Moolenaar51a29832013-05-28 22:30:35 +02001459 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001460 if (*regparse == '-') /* if last, '-' is just a char */
1461 {
1462 EMIT('-');
1463 TRY_NEG();
1464 EMIT_GLUE();
1465 }
Bram Moolenaar51a29832013-05-28 22:30:35 +02001466 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468 /* skip the trailing ] */
1469 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001470 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001471 if (negated == TRUE)
1472 {
1473 /* Mark end of negated char range */
1474 EMIT(NFA_END_NEG_RANGE);
1475 EMIT(NFA_CONCAT);
1476 }
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001477
1478 /* \_[] also matches \n but it's not negated */
1479 if (extra == ADD_NL)
1480 {
1481 EMIT(reg_string ? NL : NFA_NEWL);
1482 EMIT(NFA_OR);
1483 }
1484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001486 } /* if exists closing ] */
1487
1488 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001489 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001490 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001491
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 default:
1493 {
1494#ifdef FEAT_MBYTE
1495 int plen;
1496
1497nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001498 /* plen is length of current char with composing chars */
1499 if (enc_utf8 && ((*mb_char2len)(c)
1500 != (plen = (*mb_ptr2len)(old_regparse))
1501 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001502 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001503 int i = 0;
1504
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001505 /* A base character plus composing characters, or just one
1506 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001507 * This requires creating a separate atom as if enclosing
1508 * the characters in (), where NFA_COMPOSING is the ( and
1509 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 * building the postfix form, not the NFA itself;
1511 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001512 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001513 for (;;)
1514 {
1515 EMIT(c);
1516 if (i > 0)
1517 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001518 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001519 break;
1520 c = utf_ptr2char(old_regparse + i);
1521 }
1522 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 regparse = old_regparse + plen;
1524 }
1525 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526#endif
1527 {
1528 c = no_Magic(c);
1529 EMIT(c);
1530 }
1531 return OK;
1532 }
1533 }
1534
1535#undef TRY_NEG
1536#undef EMIT_GLUE
1537
1538 return OK;
1539}
1540
1541/*
1542 * Parse something followed by possible [*+=].
1543 *
1544 * A piece is an atom, possibly followed by a multi, an indication of how many
1545 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1546 * characters: "", "a", "aa", etc.
1547 *
1548 * piece ::= atom
1549 * or atom multi
1550 */
1551 static int
1552nfa_regpiece()
1553{
1554 int i;
1555 int op;
1556 int ret;
1557 long minval, maxval;
1558 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001559 parse_state_T old_state;
1560 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001562 int old_post_pos;
1563 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564 int quest;
1565
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001566 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1567 * next. */
1568 save_parse_state(&old_state);
1569
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001570 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001571 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572
1573 ret = nfa_regatom();
1574 if (ret == FAIL)
1575 return FAIL; /* cascaded error */
1576
1577 op = peekchr();
1578 if (re_multi_type(op) == NOT_MULTI)
1579 return OK;
1580
1581 skipchr();
1582 switch (op)
1583 {
1584 case Magic('*'):
1585 EMIT(NFA_STAR);
1586 break;
1587
1588 case Magic('+'):
1589 /*
1590 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1591 * first and only submatch would be "aaa". But the backtracking
1592 * engine interprets the plus as "try matching one more time", and
1593 * a* matches a second time at the end of the input, the empty
1594 * string.
1595 * The submatch will the empty string.
1596 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001597 * In order to be consistent with the old engine, we replace
1598 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001600 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001601 curchr = -1;
1602 if (nfa_regatom() == FAIL)
1603 return FAIL;
1604 EMIT(NFA_STAR);
1605 EMIT(NFA_CONCAT);
1606 skipchr(); /* skip the \+ */
1607 break;
1608
1609 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001610 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001611 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001612 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001613 switch(op)
1614 {
1615 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001616 /* \@= */
1617 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001618 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001619 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001620 /* \@! */
1621 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001622 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001624 op = no_Magic(getchr());
1625 if (op == '=')
1626 /* \@<= */
1627 i = NFA_PREV_ATOM_JUST_BEFORE;
1628 else if (op == '!')
1629 /* \@<! */
1630 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1631 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001632 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001633 /* \@> */
1634 i = NFA_PREV_ATOM_LIKE_PATTERN;
1635 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001636 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001637 if (i == 0)
1638 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001639 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1640 return FAIL;
1641 }
1642 EMIT(i);
1643 if (i == NFA_PREV_ATOM_JUST_BEFORE
1644 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1645 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001646 break;
1647
1648 case Magic('?'):
1649 case Magic('='):
1650 EMIT(NFA_QUEST);
1651 break;
1652
1653 case Magic('{'):
1654 /* a{2,5} will expand to 'aaa?a?a?'
1655 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1656 * version of '?'
1657 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1658 * parenthesis have the same id
1659 */
1660
1661 greedy = TRUE;
1662 c2 = peekchr();
1663 if (c2 == '-' || c2 == Magic('-'))
1664 {
1665 skipchr();
1666 greedy = FALSE;
1667 }
1668 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001670
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001671 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1672 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001673 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001675 if (greedy)
1676 /* \{}, \{0,} */
1677 EMIT(NFA_STAR);
1678 else
1679 /* \{-}, \{-0,} */
1680 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001681 break;
1682 }
1683
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 /* Special case: x{0} or x{-0} */
1685 if (maxval == 0)
1686 {
1687 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001688 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1690 EMIT(NFA_SKIP_CHAR);
1691 return OK;
1692 }
1693
1694 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001695 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001696 /* Save parse state after the repeated atom and the \{} */
1697 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001699 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1700 for (i = 0; i < maxval; i++)
1701 {
1702 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001703 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001704 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001705 if (nfa_regatom() == FAIL)
1706 return FAIL;
1707 /* after "minval" times, atoms are optional */
1708 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001709 {
1710 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001711 {
1712 if (greedy)
1713 EMIT(NFA_STAR);
1714 else
1715 EMIT(NFA_STAR_NONGREEDY);
1716 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001717 else
1718 EMIT(quest);
1719 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001720 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001722 if (i + 1 > minval && maxval == MAX_LIMIT)
1723 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001724 }
1725
1726 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001727 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001728 curchr = -1;
1729
1730 break;
1731
1732
1733 default:
1734 break;
1735 } /* end switch */
1736
1737 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740
1741 return OK;
1742}
1743
1744/*
1745 * Parse one or more pieces, concatenated. It matches a match for the
1746 * first piece, followed by a match for the second piece, etc. Example:
1747 * "f[0-9]b", first matches "f", then a digit and then "b".
1748 *
1749 * concat ::= piece
1750 * or piece piece
1751 * or piece piece piece
1752 * etc.
1753 */
1754 static int
1755nfa_regconcat()
1756{
1757 int cont = TRUE;
1758 int first = TRUE;
1759
1760 while (cont)
1761 {
1762 switch (peekchr())
1763 {
1764 case NUL:
1765 case Magic('|'):
1766 case Magic('&'):
1767 case Magic(')'):
1768 cont = FALSE;
1769 break;
1770
1771 case Magic('Z'):
1772#ifdef FEAT_MBYTE
1773 regflags |= RF_ICOMBINE;
1774#endif
1775 skipchr_keepstart();
1776 break;
1777 case Magic('c'):
1778 regflags |= RF_ICASE;
1779 skipchr_keepstart();
1780 break;
1781 case Magic('C'):
1782 regflags |= RF_NOICASE;
1783 skipchr_keepstart();
1784 break;
1785 case Magic('v'):
1786 reg_magic = MAGIC_ALL;
1787 skipchr_keepstart();
1788 curchr = -1;
1789 break;
1790 case Magic('m'):
1791 reg_magic = MAGIC_ON;
1792 skipchr_keepstart();
1793 curchr = -1;
1794 break;
1795 case Magic('M'):
1796 reg_magic = MAGIC_OFF;
1797 skipchr_keepstart();
1798 curchr = -1;
1799 break;
1800 case Magic('V'):
1801 reg_magic = MAGIC_NONE;
1802 skipchr_keepstart();
1803 curchr = -1;
1804 break;
1805
1806 default:
1807 if (nfa_regpiece() == FAIL)
1808 return FAIL;
1809 if (first == FALSE)
1810 EMIT(NFA_CONCAT);
1811 else
1812 first = FALSE;
1813 break;
1814 }
1815 }
1816
1817 return OK;
1818}
1819
1820/*
1821 * Parse a branch, one or more concats, separated by "\&". It matches the
1822 * last concat, but only if all the preceding concats also match at the same
1823 * position. Examples:
1824 * "foobeep\&..." matches "foo" in "foobeep".
1825 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1826 *
1827 * branch ::= concat
1828 * or concat \& concat
1829 * or concat \& concat \& concat
1830 * etc.
1831 */
1832 static int
1833nfa_regbranch()
1834{
1835 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001836 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837
Bram Moolenaar16299b52013-05-30 18:45:23 +02001838 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001839
1840 /* First branch, possibly the only one */
1841 if (nfa_regconcat() == FAIL)
1842 return FAIL;
1843
1844 ch = peekchr();
1845 /* Try next concats */
1846 while (ch == Magic('&'))
1847 {
1848 skipchr();
1849 EMIT(NFA_NOPEN);
1850 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001851 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001852 if (nfa_regconcat() == FAIL)
1853 return FAIL;
1854 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001855 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 EMIT(NFA_SKIP_CHAR);
1857 EMIT(NFA_CONCAT);
1858 ch = peekchr();
1859 }
1860
1861 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001862 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001863 EMIT(NFA_SKIP_CHAR);
1864
1865 return OK;
1866}
1867
1868/*
1869 * Parse a pattern, one or more branches, separated by "\|". It matches
1870 * anything that matches one of the branches. Example: "foo\|beep" matches
1871 * "foo" and matches "beep". If more than one branch matches, the first one
1872 * is used.
1873 *
1874 * pattern ::= branch
1875 * or branch \| branch
1876 * or branch \| branch \| branch
1877 * etc.
1878 */
1879 static int
1880nfa_reg(paren)
1881 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1882{
1883 int parno = 0;
1884
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885 if (paren == REG_PAREN)
1886 {
1887 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001888 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 parno = regnpar++;
1890 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001891#ifdef FEAT_SYN_HL
1892 else if (paren == REG_ZPAREN)
1893 {
1894 /* Make a ZOPEN node. */
1895 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001896 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001897 parno = regnzpar++;
1898 }
1899#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900
1901 if (nfa_regbranch() == FAIL)
1902 return FAIL; /* cascaded error */
1903
1904 while (peekchr() == Magic('|'))
1905 {
1906 skipchr();
1907 if (nfa_regbranch() == FAIL)
1908 return FAIL; /* cascaded error */
1909 EMIT(NFA_OR);
1910 }
1911
1912 /* Check for proper termination. */
1913 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1914 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 if (paren == REG_NPAREN)
1916 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1917 else
1918 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1919 }
1920 else if (paren == REG_NOPAREN && peekchr() != NUL)
1921 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001922 if (peekchr() == Magic(')'))
1923 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1924 else
1925 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1926 }
1927 /*
1928 * Here we set the flag allowing back references to this set of
1929 * parentheses.
1930 */
1931 if (paren == REG_PAREN)
1932 {
1933 had_endbrace[parno] = TRUE; /* have seen the close paren */
1934 EMIT(NFA_MOPEN + parno);
1935 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001936#ifdef FEAT_SYN_HL
1937 else if (paren == REG_ZPAREN)
1938 EMIT(NFA_ZOPEN + parno);
1939#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940
1941 return OK;
1942}
1943
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944#ifdef DEBUG
1945static char_u code[50];
1946
1947 static void
1948nfa_set_code(c)
1949 int c;
1950{
1951 int addnl = FALSE;
1952
1953 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
1954 {
1955 addnl = TRUE;
1956 c -= ADD_NL;
1957 }
1958
1959 STRCPY(code, "");
1960 switch (c)
1961 {
1962 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
1963 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
1964 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
1965 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
1966 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
1967 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
1968
Bram Moolenaar5714b802013-05-28 22:03:20 +02001969 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
1970 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
1971 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
1972 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
1973 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
1974 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
1975 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
1976 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
1977 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001978#ifdef FEAT_SYN_HL
1979 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
1980 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
1981 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
1982 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
1983 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
1984 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
1985 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
1986 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
1987 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
1988#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02001989 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
1990
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001991 case NFA_PREV_ATOM_NO_WIDTH:
1992 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001993 case NFA_PREV_ATOM_NO_WIDTH_NEG:
1994 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02001995 case NFA_PREV_ATOM_JUST_BEFORE:
1996 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
1997 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
1998 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02001999 case NFA_PREV_ATOM_LIKE_PATTERN:
2000 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2001
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002002 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2003 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002004 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002005 case NFA_START_INVISIBLE_BEFORE:
2006 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002007 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002009 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002010
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2012 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002013 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002015 case NFA_MOPEN:
2016 case NFA_MOPEN1:
2017 case NFA_MOPEN2:
2018 case NFA_MOPEN3:
2019 case NFA_MOPEN4:
2020 case NFA_MOPEN5:
2021 case NFA_MOPEN6:
2022 case NFA_MOPEN7:
2023 case NFA_MOPEN8:
2024 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002025 STRCPY(code, "NFA_MOPEN(x)");
2026 code[10] = c - NFA_MOPEN + '0';
2027 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002028 case NFA_MCLOSE:
2029 case NFA_MCLOSE1:
2030 case NFA_MCLOSE2:
2031 case NFA_MCLOSE3:
2032 case NFA_MCLOSE4:
2033 case NFA_MCLOSE5:
2034 case NFA_MCLOSE6:
2035 case NFA_MCLOSE7:
2036 case NFA_MCLOSE8:
2037 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 STRCPY(code, "NFA_MCLOSE(x)");
2039 code[11] = c - NFA_MCLOSE + '0';
2040 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002041#ifdef FEAT_SYN_HL
2042 case NFA_ZOPEN:
2043 case NFA_ZOPEN1:
2044 case NFA_ZOPEN2:
2045 case NFA_ZOPEN3:
2046 case NFA_ZOPEN4:
2047 case NFA_ZOPEN5:
2048 case NFA_ZOPEN6:
2049 case NFA_ZOPEN7:
2050 case NFA_ZOPEN8:
2051 case NFA_ZOPEN9:
2052 STRCPY(code, "NFA_ZOPEN(x)");
2053 code[10] = c - NFA_ZOPEN + '0';
2054 break;
2055 case NFA_ZCLOSE:
2056 case NFA_ZCLOSE1:
2057 case NFA_ZCLOSE2:
2058 case NFA_ZCLOSE3:
2059 case NFA_ZCLOSE4:
2060 case NFA_ZCLOSE5:
2061 case NFA_ZCLOSE6:
2062 case NFA_ZCLOSE7:
2063 case NFA_ZCLOSE8:
2064 case NFA_ZCLOSE9:
2065 STRCPY(code, "NFA_ZCLOSE(x)");
2066 code[11] = c - NFA_ZCLOSE + '0';
2067 break;
2068#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2070 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2071 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2072 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002073 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2074 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002075 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2076 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2077 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2078 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2079 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2080 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2081 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2082 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2083 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2084 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2085 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2086 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2087 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2088 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2089
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002091 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2092 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2093 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002094 case NFA_NOT: STRCPY(code, "NFA_NOT "); break;
2095 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2096 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break;
2098 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2099 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2100 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2101 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2102 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2103 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2104 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2105 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2106 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2107 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2108 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2109 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2110 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2111 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2112 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2113 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2114
2115 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2116 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2117 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2118 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2119 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2120 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2121 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2122 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2123 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2124 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2125 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2126 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2127 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2128 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2129 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2130 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2131 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2132 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2133 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2134 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2135 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2136 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2137 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2138 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2139 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2140 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2141 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
2142
2143 default:
2144 STRCPY(code, "CHAR(x)");
2145 code[5] = c;
2146 }
2147
2148 if (addnl == TRUE)
2149 STRCAT(code, " + NEWLINE ");
2150
2151}
2152
2153#ifdef ENABLE_LOG
2154static FILE *log_fd;
2155
2156/*
2157 * Print the postfix notation of the current regexp.
2158 */
2159 static void
2160nfa_postfix_dump(expr, retval)
2161 char_u *expr;
2162 int retval;
2163{
2164 int *p;
2165 FILE *f;
2166
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002167 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002168 if (f != NULL)
2169 {
2170 fprintf(f, "\n-------------------------\n");
2171 if (retval == FAIL)
2172 fprintf(f, ">>> NFA engine failed ... \n");
2173 else if (retval == OK)
2174 fprintf(f, ">>> NFA engine succeeded !\n");
2175 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaar745fc022013-05-20 22:20:02 +02002176 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002177 {
2178 nfa_set_code(*p);
2179 fprintf(f, "%s, ", code);
2180 }
2181 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaar745fc022013-05-20 22:20:02 +02002182 for (p = post_start; *p && p < post_end; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002183 fprintf(f, "%d ", *p);
2184 fprintf(f, "\n\n");
2185 fclose(f);
2186 }
2187}
2188
2189/*
2190 * Print the NFA starting with a root node "state".
2191 */
2192 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002193nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194 FILE *debugf;
2195 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002196{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002197 garray_T indent;
2198
2199 ga_init2(&indent, 1, 64);
2200 ga_append(&indent, '\0');
2201 nfa_print_state2(debugf, state, &indent);
2202 ga_clear(&indent);
2203}
2204
2205 static void
2206nfa_print_state2(debugf, state, indent)
2207 FILE *debugf;
2208 nfa_state_T *state;
2209 garray_T *indent;
2210{
2211 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212
2213 if (state == NULL)
2214 return;
2215
2216 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002217
2218 /* Output indent */
2219 p = (char_u *)indent->ga_data;
2220 if (indent->ga_len >= 3)
2221 {
2222 int last = indent->ga_len - 3;
2223 char_u save[2];
2224
2225 STRNCPY(save, &p[last], 2);
2226 STRNCPY(&p[last], "+-", 2);
2227 fprintf(debugf, " %s", p);
2228 STRNCPY(&p[last], save, 2);
2229 }
2230 else
2231 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002232
2233 nfa_set_code(state->c);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002234 fprintf(debugf, "%s%s (%d) (id=%d)\n",
2235 state->negated ? "NOT " : "", code, state->c, abs(state->id));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236 if (state->id < 0)
2237 return;
2238
2239 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002240
2241 /* grow indent for state->out */
2242 indent->ga_len -= 1;
2243 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002244 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002245 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002246 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002247 ga_append(indent, '\0');
2248
2249 nfa_print_state2(debugf, state->out, indent);
2250
2251 /* replace last part of indent for state->out1 */
2252 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002253 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002254 ga_append(indent, '\0');
2255
2256 nfa_print_state2(debugf, state->out1, indent);
2257
2258 /* shrink indent */
2259 indent->ga_len -= 3;
2260 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261}
2262
2263/*
2264 * Print the NFA state machine.
2265 */
2266 static void
2267nfa_dump(prog)
2268 nfa_regprog_T *prog;
2269{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002270 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002271
2272 if (debugf != NULL)
2273 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002274 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002275
2276 fprintf(debugf, "reganch: %d\n", prog->reganch);
2277 fprintf(debugf, "regstart: %d\n", prog->regstart);
2278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279 fclose(debugf);
2280 }
2281}
2282#endif /* ENABLE_LOG */
2283#endif /* DEBUG */
2284
2285/*
2286 * Parse r.e. @expr and convert it into postfix form.
2287 * Return the postfix string on success, NULL otherwise.
2288 */
2289 static int *
2290re2post()
2291{
2292 if (nfa_reg(REG_NOPAREN) == FAIL)
2293 return NULL;
2294 EMIT(NFA_MOPEN);
2295 return post_start;
2296}
2297
2298/* NB. Some of the code below is inspired by Russ's. */
2299
2300/*
2301 * Represents an NFA state plus zero or one or two arrows exiting.
2302 * if c == MATCH, no arrows out; matching state.
2303 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2304 * If c < 256, labeled arrow with character c to out.
2305 */
2306
2307static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2308
2309/*
2310 * Allocate and initialize nfa_state_T.
2311 */
2312 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002313alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314 int c;
2315 nfa_state_T *out;
2316 nfa_state_T *out1;
2317{
2318 nfa_state_T *s;
2319
2320 if (istate >= nstate)
2321 return NULL;
2322
2323 s = &state_ptr[istate++];
2324
2325 s->c = c;
2326 s->out = out;
2327 s->out1 = out1;
2328
2329 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002330 s->lastlist[0] = 0;
2331 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002332 s->negated = FALSE;
2333
2334 return s;
2335}
2336
2337/*
2338 * A partially built NFA without the matching state filled in.
2339 * Frag_T.start points at the start state.
2340 * Frag_T.out is a list of places that need to be set to the
2341 * next state for this fragment.
2342 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002343
2344/* Since the out pointers in the list are always
2345 * uninitialized, we use the pointers themselves
2346 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002348union Ptrlist
2349{
2350 Ptrlist *next;
2351 nfa_state_T *s;
2352};
2353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354struct Frag
2355{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002356 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 Ptrlist *out;
2358};
2359typedef struct Frag Frag_T;
2360
2361static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2362static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2363static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2364static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2365static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2366static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2367
2368/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002369 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 */
2371 static Frag_T
2372frag(start, out)
2373 nfa_state_T *start;
2374 Ptrlist *out;
2375{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002376 Frag_T n;
2377
2378 n.start = start;
2379 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002380 return n;
2381}
2382
2383/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384 * Create singleton list containing just outp.
2385 */
2386 static Ptrlist *
2387list1(outp)
2388 nfa_state_T **outp;
2389{
2390 Ptrlist *l;
2391
2392 l = (Ptrlist *)outp;
2393 l->next = NULL;
2394 return l;
2395}
2396
2397/*
2398 * Patch the list of states at out to point to start.
2399 */
2400 static void
2401patch(l, s)
2402 Ptrlist *l;
2403 nfa_state_T *s;
2404{
2405 Ptrlist *next;
2406
2407 for (; l; l = next)
2408 {
2409 next = l->next;
2410 l->s = s;
2411 }
2412}
2413
2414
2415/*
2416 * Join the two lists l1 and l2, returning the combination.
2417 */
2418 static Ptrlist *
2419append(l1, l2)
2420 Ptrlist *l1;
2421 Ptrlist *l2;
2422{
2423 Ptrlist *oldl1;
2424
2425 oldl1 = l1;
2426 while (l1->next)
2427 l1 = l1->next;
2428 l1->next = l2;
2429 return oldl1;
2430}
2431
2432/*
2433 * Stack used for transforming postfix form into NFA.
2434 */
2435static Frag_T empty;
2436
2437 static void
2438st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002439 int *postfix UNUSED;
2440 int *end UNUSED;
2441 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002443#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 FILE *df;
2445 int *p2;
2446
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002447 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 if (df)
2449 {
2450 fprintf(df, "Error popping the stack!\n");
2451#ifdef DEBUG
2452 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2453#endif
2454 fprintf(df, "Postfix form is: ");
2455#ifdef DEBUG
2456 for (p2 = postfix; p2 < end; p2++)
2457 {
2458 nfa_set_code(*p2);
2459 fprintf(df, "%s, ", code);
2460 }
2461 nfa_set_code(*p);
2462 fprintf(df, "\nCurrent position is: ");
2463 for (p2 = postfix; p2 <= p; p2 ++)
2464 {
2465 nfa_set_code(*p2);
2466 fprintf(df, "%s, ", code);
2467 }
2468#else
2469 for (p2 = postfix; p2 < end; p2++)
2470 {
2471 fprintf(df, "%d, ", *p2);
2472 }
2473 fprintf(df, "\nCurrent position is: ");
2474 for (p2 = postfix; p2 <= p; p2 ++)
2475 {
2476 fprintf(df, "%d, ", *p2);
2477 }
2478#endif
2479 fprintf(df, "\n--------------------------\n");
2480 fclose(df);
2481 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002482#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 EMSG(_("E874: (NFA) Could not pop the stack !"));
2484}
2485
2486/*
2487 * Push an item onto the stack.
2488 */
2489 static void
2490st_push(s, p, stack_end)
2491 Frag_T s;
2492 Frag_T **p;
2493 Frag_T *stack_end;
2494{
2495 Frag_T *stackp = *p;
2496
2497 if (stackp >= stack_end)
2498 return;
2499 *stackp = s;
2500 *p = *p + 1;
2501}
2502
2503/*
2504 * Pop an item from the stack.
2505 */
2506 static Frag_T
2507st_pop(p, stack)
2508 Frag_T **p;
2509 Frag_T *stack;
2510{
2511 Frag_T *stackp;
2512
2513 *p = *p - 1;
2514 stackp = *p;
2515 if (stackp < stack)
2516 return empty;
2517 return **p;
2518}
2519
2520/*
2521 * Convert a postfix form into its equivalent NFA.
2522 * Return the NFA start state on success, NULL otherwise.
2523 */
2524 static nfa_state_T *
2525post2nfa(postfix, end, nfa_calc_size)
2526 int *postfix;
2527 int *end;
2528 int nfa_calc_size;
2529{
2530 int *p;
2531 int mopen;
2532 int mclose;
2533 Frag_T *stack = NULL;
2534 Frag_T *stackp = NULL;
2535 Frag_T *stack_end = NULL;
2536 Frag_T e1;
2537 Frag_T e2;
2538 Frag_T e;
2539 nfa_state_T *s;
2540 nfa_state_T *s1;
2541 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002542 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543
2544 if (postfix == NULL)
2545 return NULL;
2546
Bram Moolenaar053bb602013-05-20 13:55:21 +02002547#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548#define POP() st_pop(&stackp, stack); \
2549 if (stackp < stack) \
2550 { \
2551 st_error(postfix, end, p); \
2552 return NULL; \
2553 }
2554
2555 if (nfa_calc_size == FALSE)
2556 {
2557 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002558 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002559 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002560 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002561 }
2562
2563 for (p = postfix; p < end; ++p)
2564 {
2565 switch (*p)
2566 {
2567 case NFA_CONCAT:
2568 /* Catenation.
2569 * Pay attention: this operator does not exist
2570 * in the r.e. itself (it is implicit, really).
2571 * It is added when r.e. is translated to postfix
2572 * form in re2post().
2573 *
2574 * No new state added here. */
2575 if (nfa_calc_size == TRUE)
2576 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002577 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002578 break;
2579 }
2580 e2 = POP();
2581 e1 = POP();
2582 patch(e1.out, e2.start);
2583 PUSH(frag(e1.start, e2.out));
2584 break;
2585
2586 case NFA_NOT:
2587 /* Negation of a character */
2588 if (nfa_calc_size == TRUE)
2589 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002590 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002591 break;
2592 }
2593 e1 = POP();
2594 e1.start->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002595#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002596 if (e1.start->c == NFA_COMPOSING)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002597 e1.start->out1->negated = TRUE;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002598#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 PUSH(e1);
2600 break;
2601
2602 case NFA_OR:
2603 /* Alternation */
2604 if (nfa_calc_size == TRUE)
2605 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002606 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002607 break;
2608 }
2609 e2 = POP();
2610 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002611 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002612 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002613 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614 PUSH(frag(s, append(e1.out, e2.out)));
2615 break;
2616
2617 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002618 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002619 if (nfa_calc_size == TRUE)
2620 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002621 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002622 break;
2623 }
2624 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002625 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002626 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002627 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628 patch(e.out, s);
2629 PUSH(frag(s, list1(&s->out1)));
2630 break;
2631
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002632 case NFA_STAR_NONGREEDY:
2633 /* Zero or more, prefer zero */
2634 if (nfa_calc_size == TRUE)
2635 {
2636 nstate++;
2637 break;
2638 }
2639 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002640 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002641 if (s == NULL)
2642 goto theend;
2643 patch(e.out, s);
2644 PUSH(frag(s, list1(&s->out)));
2645 break;
2646
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 case NFA_QUEST:
2648 /* one or zero atoms=> greedy match */
2649 if (nfa_calc_size == TRUE)
2650 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002651 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652 break;
2653 }
2654 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002655 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002656 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002657 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 PUSH(frag(s, append(e.out, list1(&s->out1))));
2659 break;
2660
2661 case NFA_QUEST_NONGREEDY:
2662 /* zero or one atoms => non-greedy match */
2663 if (nfa_calc_size == TRUE)
2664 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002665 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002666 break;
2667 }
2668 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002669 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002670 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002671 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672 PUSH(frag(s, append(e.out, list1(&s->out))));
2673 break;
2674
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002675 case NFA_SKIP_CHAR:
2676 /* Symbol of 0-length, Used in a repetition
2677 * with max/min count of 0 */
2678 if (nfa_calc_size == TRUE)
2679 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002680 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002681 break;
2682 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002683 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002684 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002685 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002686 PUSH(frag(s, list1(&s->out)));
2687 break;
2688
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002689 case NFA_OPT_CHARS:
2690 {
2691 int n;
2692
2693 /* \%[abc] */
2694 n = *++p; /* get number of characters */
2695 if (nfa_calc_size == TRUE)
2696 {
2697 nstate += n;
2698 break;
2699 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02002700 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002701 e1.out = NULL; /* stores list with out1's */
2702 s1 = NULL; /* previous NFA_SPLIT to connect to */
2703 while (n-- > 0)
2704 {
2705 e = POP(); /* get character */
2706 s = alloc_state(NFA_SPLIT, e.start, NULL);
2707 if (s == NULL)
2708 goto theend;
2709 if (e1.out == NULL)
2710 e1 = e;
2711 patch(e.out, s1);
2712 append(e1.out, list1(&s->out1));
2713 s1 = s;
2714 }
2715 PUSH(frag(s, e1.out));
2716 break;
2717 }
2718
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002719 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002720 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02002721 case NFA_PREV_ATOM_JUST_BEFORE:
2722 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02002723 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002724 {
2725 int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG
2726 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
2727 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
2728 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02002729 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
2730 int start_state = NFA_START_INVISIBLE;
2731 int end_state = NFA_END_INVISIBLE;
2732 int n = 0;
2733 nfa_state_T *zend;
2734 nfa_state_T *skip;
2735
2736 if (before)
2737 start_state = NFA_START_INVISIBLE_BEFORE;
2738 else if (pattern)
2739 {
2740 start_state = NFA_START_PATTERN;
2741 end_state = NFA_END_PATTERN;
2742 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002743
2744 if (before)
2745 n = *++p; /* get the count */
2746
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002747 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002748 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02002749 * The \@<= operator: match for the preceding atom.
2750 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002752 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002753
2754 if (nfa_calc_size == TRUE)
2755 {
Bram Moolenaar87953742013-06-05 18:52:40 +02002756 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002757 break;
2758 }
2759 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02002760 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002761 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002762 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002763
Bram Moolenaar87953742013-06-05 18:52:40 +02002764 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002765 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002766 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002767 if (neg)
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002768 {
2769 s->negated = TRUE;
2770 s1->negated = TRUE;
2771 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002772 if (before)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002773 s->val = n; /* store the count */
Bram Moolenaar87953742013-06-05 18:52:40 +02002774 if (pattern)
2775 {
2776 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
2777 skip = alloc_state(NFA_SKIP, NULL, NULL);
2778 zend = alloc_state(NFA_ZEND, s1, NULL);
2779 s1->out= skip;
2780 patch(e.out, zend);
2781 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02002782 }
Bram Moolenaar87953742013-06-05 18:52:40 +02002783 else
2784 {
2785 patch(e.out, s1);
2786 PUSH(frag(s, list1(&s1->out)));
2787 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002788 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002789 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002790
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002791#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002792 case NFA_COMPOSING: /* char with composing char */
2793#if 0
2794 /* TODO */
2795 if (regflags & RF_ICOMBINE)
2796 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002797 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002798 }
2799#endif
2800 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002801#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002802
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002803 case NFA_MOPEN: /* \( \) Submatch */
2804 case NFA_MOPEN1:
2805 case NFA_MOPEN2:
2806 case NFA_MOPEN3:
2807 case NFA_MOPEN4:
2808 case NFA_MOPEN5:
2809 case NFA_MOPEN6:
2810 case NFA_MOPEN7:
2811 case NFA_MOPEN8:
2812 case NFA_MOPEN9:
2813#ifdef FEAT_SYN_HL
2814 case NFA_ZOPEN: /* \z( \) Submatch */
2815 case NFA_ZOPEN1:
2816 case NFA_ZOPEN2:
2817 case NFA_ZOPEN3:
2818 case NFA_ZOPEN4:
2819 case NFA_ZOPEN5:
2820 case NFA_ZOPEN6:
2821 case NFA_ZOPEN7:
2822 case NFA_ZOPEN8:
2823 case NFA_ZOPEN9:
2824#endif
2825 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826 if (nfa_calc_size == TRUE)
2827 {
2828 nstate += 2;
2829 break;
2830 }
2831
2832 mopen = *p;
2833 switch (*p)
2834 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002835 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
2836#ifdef FEAT_SYN_HL
2837 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
2838 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
2839 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
2840 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
2841 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
2842 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
2843 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
2844 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
2845 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
2846 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
2847#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002848#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002849 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002850#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002852 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002853 mclose = *p + NSUBEXP;
2854 break;
2855 }
2856
2857 /* Allow "NFA_MOPEN" as a valid postfix representation for
2858 * the empty regexp "". In this case, the NFA will be
2859 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
2860 * empty groups of parenthesis, and empty mbyte chars */
2861 if (stackp == stack)
2862 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02002863 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002864 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002865 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002866 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002868 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869 patch(list1(&s->out), s1);
2870 PUSH(frag(s, list1(&s1->out)));
2871 break;
2872 }
2873
2874 /* At least one node was emitted before NFA_MOPEN, so
2875 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
2876 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002877 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002878 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002879 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002880
Bram Moolenaar525666f2013-06-02 16:40:55 +02002881 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002883 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 patch(e.out, s1);
2885
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002886#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002887 if (mopen == NFA_COMPOSING)
2888 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002890#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891
2892 PUSH(frag(s, list1(&s1->out)));
2893 break;
2894
Bram Moolenaar5714b802013-05-28 22:03:20 +02002895 case NFA_BACKREF1:
2896 case NFA_BACKREF2:
2897 case NFA_BACKREF3:
2898 case NFA_BACKREF4:
2899 case NFA_BACKREF5:
2900 case NFA_BACKREF6:
2901 case NFA_BACKREF7:
2902 case NFA_BACKREF8:
2903 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002904#ifdef FEAT_SYN_HL
2905 case NFA_ZREF1:
2906 case NFA_ZREF2:
2907 case NFA_ZREF3:
2908 case NFA_ZREF4:
2909 case NFA_ZREF5:
2910 case NFA_ZREF6:
2911 case NFA_ZREF7:
2912 case NFA_ZREF8:
2913 case NFA_ZREF9:
2914#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002915 if (nfa_calc_size == TRUE)
2916 {
2917 nstate += 2;
2918 break;
2919 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002920 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002921 if (s == NULL)
2922 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02002923 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02002924 if (s1 == NULL)
2925 goto theend;
2926 patch(list1(&s->out), s1);
2927 PUSH(frag(s, list1(&s1->out)));
2928 break;
2929
Bram Moolenaar423532e2013-05-29 21:14:42 +02002930 case NFA_LNUM:
2931 case NFA_LNUM_GT:
2932 case NFA_LNUM_LT:
2933 case NFA_VCOL:
2934 case NFA_VCOL_GT:
2935 case NFA_VCOL_LT:
2936 case NFA_COL:
2937 case NFA_COL_GT:
2938 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02002939 case NFA_MARK:
2940 case NFA_MARK_GT:
2941 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002942 {
2943 int n = *++p; /* lnum, col or mark name */
2944
Bram Moolenaar423532e2013-05-29 21:14:42 +02002945 if (nfa_calc_size == TRUE)
2946 {
2947 nstate += 1;
2948 break;
2949 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002950 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02002951 if (s == NULL)
2952 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002953 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002954 PUSH(frag(s, list1(&s->out)));
2955 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002956 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02002957
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002958 case NFA_ZSTART:
2959 case NFA_ZEND:
2960 default:
2961 /* Operands */
2962 if (nfa_calc_size == TRUE)
2963 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002964 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002965 break;
2966 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02002967 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002968 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002969 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002970 PUSH(frag(s, list1(&s->out)));
2971 break;
2972
2973 } /* switch(*p) */
2974
2975 } /* for(p = postfix; *p; ++p) */
2976
2977 if (nfa_calc_size == TRUE)
2978 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002979 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002980 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002981 }
2982
2983 e = POP();
2984 if (stackp != stack)
2985 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
2986
2987 if (istate >= nstate)
2988 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
2989
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002990 matchstate = &state_ptr[istate++]; /* the match state */
2991 matchstate->c = NFA_MATCH;
2992 matchstate->out = matchstate->out1 = NULL;
2993
2994 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002995 ret = e.start;
2996
2997theend:
2998 vim_free(stack);
2999 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000
3001#undef POP1
3002#undef PUSH1
3003#undef POP2
3004#undef PUSH2
3005#undef POP
3006#undef PUSH
3007}
3008
3009/****************************************************************
3010 * NFA execution code.
3011 ****************************************************************/
3012
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003013typedef struct
3014{
3015 int in_use; /* number of subexpr with useful info */
3016
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003017 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003018 union
3019 {
3020 struct multipos
3021 {
3022 lpos_T start;
3023 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003024 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003025 struct linepos
3026 {
3027 char_u *start;
3028 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003029 } line[NSUBEXP];
3030 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003031} regsub_T;
3032
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003033typedef struct
3034{
3035 regsub_T norm; /* \( .. \) matches */
3036#ifdef FEAT_SYN_HL
3037 regsub_T synt; /* \z( .. \) matches */
3038#endif
3039} regsubs_T;
3040
Bram Moolenaara2d95102013-06-04 14:23:05 +02003041/* nfa_pim_T stores a Postponed Invisible Match. */
3042typedef struct nfa_pim_S nfa_pim_T;
3043struct nfa_pim_S
3044{
3045 nfa_state_T *state;
3046 int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */
3047 nfa_pim_T *pim; /* another PIM at the same position */
3048 regsubs_T subs; /* submatch info, only party used */
3049};
3050
3051/* Values for done in nfa_pim_T. */
3052#define NFA_PIM_TODO 0
3053#define NFA_PIM_MATCH 1
3054#define NFA_PIM_NOMATCH -1
3055
3056
Bram Moolenaar963fee22013-05-26 21:47:28 +02003057/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003058typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003059{
3060 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003061 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003062 nfa_pim_T *pim; /* if not NULL: postponed invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003063 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003064} nfa_thread_T;
3065
Bram Moolenaar963fee22013-05-26 21:47:28 +02003066/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003067typedef struct
3068{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003069 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003070 int n; /* nr of states currently in "t" */
3071 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003072 int id; /* ID of the list */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003073} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003074
Bram Moolenaar5714b802013-05-28 22:03:20 +02003075#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003076static void log_subsexpr __ARGS((regsubs_T *subs));
3077static void log_subexpr __ARGS((regsub_T *sub));
3078
3079 static void
3080log_subsexpr(subs)
3081 regsubs_T *subs;
3082{
3083 log_subexpr(&subs->norm);
3084# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003085 if (nfa_has_zsubexpr)
3086 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003087# endif
3088}
3089
Bram Moolenaar5714b802013-05-28 22:03:20 +02003090 static void
3091log_subexpr(sub)
3092 regsub_T *sub;
3093{
3094 int j;
3095
3096 for (j = 0; j < sub->in_use; j++)
3097 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003098 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003099 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003100 sub->list.multi[j].start.col,
3101 (int)sub->list.multi[j].start.lnum,
3102 sub->list.multi[j].end.col,
3103 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003104 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003105 {
3106 char *s = (char *)sub->list.line[j].start;
3107 char *e = (char *)sub->list.line[j].end;
3108
Bram Moolenaar87953742013-06-05 18:52:40 +02003109 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003110 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003111 s == NULL ? "NULL" : s,
3112 e == NULL ? "NULL" : e);
3113 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003114}
3115#endif
3116
Bram Moolenaar963fee22013-05-26 21:47:28 +02003117/* Used during execution: whether a match has been found. */
3118static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003119
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003120static void clear_sub __ARGS((regsub_T *sub));
3121static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3122static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003123static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003124static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off));
Bram Moolenaara2d95102013-06-04 14:23:05 +02003125static 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 +02003126
3127 static void
3128clear_sub(sub)
3129 regsub_T *sub;
3130{
3131 if (REG_MULTI)
3132 /* Use 0xff to set lnum to -1 */
3133 vim_memset(sub->list.multi, 0xff,
3134 sizeof(struct multipos) * nfa_nsubexpr);
3135 else
3136 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3137 sub->in_use = 0;
3138}
3139
3140/*
3141 * Copy the submatches from "from" to "to".
3142 */
3143 static void
3144copy_sub(to, from)
3145 regsub_T *to;
3146 regsub_T *from;
3147{
3148 to->in_use = from->in_use;
3149 if (from->in_use > 0)
3150 {
3151 /* Copy the match start and end positions. */
3152 if (REG_MULTI)
3153 mch_memmove(&to->list.multi[0],
3154 &from->list.multi[0],
3155 sizeof(struct multipos) * from->in_use);
3156 else
3157 mch_memmove(&to->list.line[0],
3158 &from->list.line[0],
3159 sizeof(struct linepos) * from->in_use);
3160 }
3161}
3162
3163/*
3164 * Like copy_sub() but exclude the main match.
3165 */
3166 static void
3167copy_sub_off(to, from)
3168 regsub_T *to;
3169 regsub_T *from;
3170{
3171 if (to->in_use < from->in_use)
3172 to->in_use = from->in_use;
3173 if (from->in_use > 1)
3174 {
3175 /* Copy the match start and end positions. */
3176 if (REG_MULTI)
3177 mch_memmove(&to->list.multi[1],
3178 &from->list.multi[1],
3179 sizeof(struct multipos) * (from->in_use - 1));
3180 else
3181 mch_memmove(&to->list.line[1],
3182 &from->list.line[1],
3183 sizeof(struct linepos) * (from->in_use - 1));
3184 }
3185}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003186
Bram Moolenaar428e9872013-05-30 17:05:39 +02003187/*
3188 * Return TRUE if "sub1" and "sub2" have the same positions.
3189 */
3190 static int
3191sub_equal(sub1, sub2)
3192 regsub_T *sub1;
3193 regsub_T *sub2;
3194{
3195 int i;
3196 int todo;
3197 linenr_T s1, e1;
3198 linenr_T s2, e2;
3199 char_u *sp1, *ep1;
3200 char_u *sp2, *ep2;
3201
3202 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3203 if (REG_MULTI)
3204 {
3205 for (i = 0; i < todo; ++i)
3206 {
3207 if (i < sub1->in_use)
3208 {
3209 s1 = sub1->list.multi[i].start.lnum;
3210 e1 = sub1->list.multi[i].end.lnum;
3211 }
3212 else
3213 {
3214 s1 = 0;
3215 e1 = 0;
3216 }
3217 if (i < sub2->in_use)
3218 {
3219 s2 = sub2->list.multi[i].start.lnum;
3220 e2 = sub2->list.multi[i].end.lnum;
3221 }
3222 else
3223 {
3224 s2 = 0;
3225 e2 = 0;
3226 }
3227 if (s1 != s2 || e1 != e2)
3228 return FALSE;
3229 if (s1 != 0 && sub1->list.multi[i].start.col
3230 != sub2->list.multi[i].start.col)
3231 return FALSE;
3232 if (e1 != 0 && sub1->list.multi[i].end.col
3233 != sub2->list.multi[i].end.col)
3234 return FALSE;
3235 }
3236 }
3237 else
3238 {
3239 for (i = 0; i < todo; ++i)
3240 {
3241 if (i < sub1->in_use)
3242 {
3243 sp1 = sub1->list.line[i].start;
3244 ep1 = sub1->list.line[i].end;
3245 }
3246 else
3247 {
3248 sp1 = NULL;
3249 ep1 = NULL;
3250 }
3251 if (i < sub2->in_use)
3252 {
3253 sp2 = sub2->list.line[i].start;
3254 ep2 = sub2->list.line[i].end;
3255 }
3256 else
3257 {
3258 sp2 = NULL;
3259 ep2 = NULL;
3260 }
3261 if (sp1 != sp2 || ep1 != ep2)
3262 return FALSE;
3263 }
3264 }
3265
3266 return TRUE;
3267}
3268
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003269#ifdef ENABLE_LOG
3270 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003271report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003272{
3273 int col;
3274
3275 if (sub->in_use <= 0)
3276 col = -1;
3277 else if (REG_MULTI)
3278 col = sub->list.multi[0].start.col;
3279 else
3280 col = (int)(sub->list.line[0].start - regline);
3281 nfa_set_code(state->c);
3282 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n",
3283 action, abs(state->id), lid, state->c, code, col);
3284}
3285#endif
3286
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003287 static void
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003288addstate(l, state, subs, off)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003289 nfa_list_T *l; /* runtime state list */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003290 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003291 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar35b23862013-05-22 23:00:40 +02003292 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003293{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003294 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003295 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003296 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003297 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003298 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003299 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003300 regsub_T *sub;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003301#ifdef ENABLE_LOG
3302 int did_print = FALSE;
3303#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304
3305 if (l == NULL || state == NULL)
3306 return;
3307
3308 switch (state->c)
3309 {
3310 case NFA_SPLIT:
3311 case NFA_NOT:
3312 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003313 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 case NFA_NCLOSE:
3315 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003316 case NFA_MCLOSE1:
3317 case NFA_MCLOSE2:
3318 case NFA_MCLOSE3:
3319 case NFA_MCLOSE4:
3320 case NFA_MCLOSE5:
3321 case NFA_MCLOSE6:
3322 case NFA_MCLOSE7:
3323 case NFA_MCLOSE8:
3324 case NFA_MCLOSE9:
3325#ifdef FEAT_SYN_HL
3326 case NFA_ZCLOSE:
3327 case NFA_ZCLOSE1:
3328 case NFA_ZCLOSE2:
3329 case NFA_ZCLOSE3:
3330 case NFA_ZCLOSE4:
3331 case NFA_ZCLOSE5:
3332 case NFA_ZCLOSE6:
3333 case NFA_ZCLOSE7:
3334 case NFA_ZCLOSE8:
3335 case NFA_ZCLOSE9:
3336#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003337 case NFA_ZEND:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003338 /* These nodes are not added themselves but their "out" and/or
3339 * "out1" may be added below. */
3340 break;
3341
3342 case NFA_MOPEN:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003343 case NFA_MOPEN1:
3344 case NFA_MOPEN2:
3345 case NFA_MOPEN3:
3346 case NFA_MOPEN4:
3347 case NFA_MOPEN5:
3348 case NFA_MOPEN6:
3349 case NFA_MOPEN7:
3350 case NFA_MOPEN8:
3351 case NFA_MOPEN9:
3352#ifdef FEAT_SYN_HL
3353 case NFA_ZOPEN:
3354 case NFA_ZOPEN1:
3355 case NFA_ZOPEN2:
3356 case NFA_ZOPEN3:
3357 case NFA_ZOPEN4:
3358 case NFA_ZOPEN5:
3359 case NFA_ZOPEN6:
3360 case NFA_ZOPEN7:
3361 case NFA_ZOPEN8:
3362 case NFA_ZOPEN9:
3363#endif
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003364 case NFA_ZSTART:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003365 /* These nodes do not need to be added, but we need to bail out
3366 * when it was tried to be added to this list before. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003367 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003368 goto skip_add;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003369 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 break;
3371
Bram Moolenaar307aa162013-06-02 16:34:21 +02003372 case NFA_BOL:
3373 case NFA_BOF:
3374 /* "^" won't match past end-of-line, don't bother trying.
3375 * Except when we are going to the next line for a look-behind
3376 * match. */
3377 if (reginput > regline
3378 && (nfa_endp == NULL
3379 || !REG_MULTI
3380 || reglnum == nfa_endp->se_u.pos.lnum))
3381 goto skip_add;
3382 /* FALLTHROUGH */
3383
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003384 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003385 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003387 /* This state is already in the list, don't add it again,
3388 * unless it is an MOPEN that is used for a backreference. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003389 if (!nfa_has_backref)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003390 {
3391skip_add:
3392#ifdef ENABLE_LOG
3393 nfa_set_code(state->c);
3394 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
3395 abs(state->id), l->id, state->c, code);
3396#endif
Bram Moolenaar428e9872013-05-30 17:05:39 +02003397 return;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003398 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003399
3400 /* See if the same state is already in the list with the same
3401 * positions. */
3402 for (i = 0; i < l->n; ++i)
3403 {
3404 thread = &l->t[i];
3405 if (thread->state->id == state->id
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003406 && sub_equal(&thread->subs.norm, &subs->norm)
3407#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003408 && (!nfa_has_zsubexpr ||
3409 sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003410#endif
3411 )
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003412 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003413 }
3414 }
3415
Bram Moolenaara2d95102013-06-04 14:23:05 +02003416 /* when there are backreferences or look-behind matches the number
3417 * of states may be (a lot) bigger */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003418 if (nfa_has_backref && l->n == l->len)
3419 {
3420 int newlen = l->len * 3 / 2 + 50;
3421
3422 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
3423 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003425
3426 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003427 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003428 thread = &l->t[l->n++];
3429 thread->state = state;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003430 thread->pim = NULL;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003431 copy_sub(&thread->subs.norm, &subs->norm);
3432#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003433 if (nfa_has_zsubexpr)
3434 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003435#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003436#ifdef ENABLE_LOG
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003437 report_state("Adding", &thread->subs.norm, state, l->id);
3438 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003439#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003440 }
3441
3442#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003443 if (!did_print)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003444 report_state("Processing", &subs->norm, state, l->id);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003445#endif
3446 switch (state->c)
3447 {
3448 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02003449 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003450 break;
3451
3452 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003453 /* order matters here */
3454 addstate(l, state->out, subs, off);
3455 addstate(l, state->out1, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 break;
3457
Bram Moolenaar5714b802013-05-28 22:03:20 +02003458 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 case NFA_NOPEN:
3460 case NFA_NCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003461 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462 break;
3463
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003464 case NFA_MOPEN:
3465 case NFA_MOPEN1:
3466 case NFA_MOPEN2:
3467 case NFA_MOPEN3:
3468 case NFA_MOPEN4:
3469 case NFA_MOPEN5:
3470 case NFA_MOPEN6:
3471 case NFA_MOPEN7:
3472 case NFA_MOPEN8:
3473 case NFA_MOPEN9:
3474#ifdef FEAT_SYN_HL
3475 case NFA_ZOPEN:
3476 case NFA_ZOPEN1:
3477 case NFA_ZOPEN2:
3478 case NFA_ZOPEN3:
3479 case NFA_ZOPEN4:
3480 case NFA_ZOPEN5:
3481 case NFA_ZOPEN6:
3482 case NFA_ZOPEN7:
3483 case NFA_ZOPEN8:
3484 case NFA_ZOPEN9:
3485#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003486 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003488 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003490 sub = &subs->norm;
3491 }
3492#ifdef FEAT_SYN_HL
3493 else if (state->c >= NFA_ZOPEN)
3494 {
3495 subidx = state->c - NFA_ZOPEN;
3496 sub = &subs->synt;
3497 }
3498#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003499 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003500 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003501 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003502 sub = &subs->norm;
3503 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003505 /* Set the position (with "off") in the subexpression. Save and
3506 * restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaar4b6ebe62013-05-30 17:49:24 +02003507 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003508 if (REG_MULTI)
3509 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003510 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003511 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003512 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003513 save_in_use = -1;
3514 }
3515 else
3516 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003517 save_in_use = sub->in_use;
3518 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003519 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003520 sub->list.multi[i].start.lnum = -1;
3521 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003522 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003523 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003524 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02003525 if (off == -1)
3526 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003527 sub->list.multi[subidx].start.lnum = reglnum + 1;
3528 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003529 }
3530 else
3531 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003532 sub->list.multi[subidx].start.lnum = reglnum;
3533 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02003534 (colnr_T)(reginput - regline + off);
3535 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003536 }
3537 else
3538 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003539 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003540 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003541 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003542 save_in_use = -1;
3543 }
3544 else
3545 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02003546 save_in_use = sub->in_use;
3547 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003548 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003549 sub->list.line[i].start = NULL;
3550 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003551 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003552 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003553 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003554 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 }
3556
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003558
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003559 if (save_in_use == -1)
3560 {
3561 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003562 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003563 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003564 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003565 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003566 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02003567 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568 break;
3569
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003570 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02003571 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02003573 /* Do not overwrite the position set by \ze. If no \ze
3574 * encountered end will be set in nfa_regtry(). */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003575 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003576 break;
3577 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003578 case NFA_MCLOSE1:
3579 case NFA_MCLOSE2:
3580 case NFA_MCLOSE3:
3581 case NFA_MCLOSE4:
3582 case NFA_MCLOSE5:
3583 case NFA_MCLOSE6:
3584 case NFA_MCLOSE7:
3585 case NFA_MCLOSE8:
3586 case NFA_MCLOSE9:
3587#ifdef FEAT_SYN_HL
3588 case NFA_ZCLOSE:
3589 case NFA_ZCLOSE1:
3590 case NFA_ZCLOSE2:
3591 case NFA_ZCLOSE3:
3592 case NFA_ZCLOSE4:
3593 case NFA_ZCLOSE5:
3594 case NFA_ZCLOSE6:
3595 case NFA_ZCLOSE7:
3596 case NFA_ZCLOSE8:
3597 case NFA_ZCLOSE9:
3598#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003600 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003601 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003603 sub = &subs->norm;
3604 }
3605#ifdef FEAT_SYN_HL
3606 else if (state->c >= NFA_ZCLOSE)
3607 {
3608 subidx = state->c - NFA_ZCLOSE;
3609 sub = &subs->synt;
3610 }
3611#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02003612 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003613 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02003614 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003615 sub = &subs->norm;
3616 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003618 /* We don't fill in gaps here, there must have been an MOPEN that
3619 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003620 save_in_use = sub->in_use;
3621 if (sub->in_use <= subidx)
3622 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 if (REG_MULTI)
3624 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003625 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003626 if (off == -1)
3627 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003628 sub->list.multi[subidx].end.lnum = reglnum + 1;
3629 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02003630 }
3631 else
3632 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003633 sub->list.multi[subidx].end.lnum = reglnum;
3634 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02003635 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02003636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637 }
3638 else
3639 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003640 save_ptr = sub->list.line[subidx].end;
3641 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 }
3643
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003644 addstate(l, state->out, subs, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645
3646 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003647 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003649 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003650 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651 break;
3652 }
3653}
3654
3655/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02003656 * Like addstate(), but the new state(s) are put at position "*ip".
3657 * Used for zero-width matches, next state to use is the added one.
3658 * This makes sure the order of states to be tried does not change, which
3659 * matters for alternatives.
3660 */
3661 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02003662addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003663 nfa_list_T *l; /* runtime state list */
3664 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003665 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003666 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003667 int *ip;
3668{
3669 int tlen = l->n;
3670 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003671 int listidx = *ip;
3672 int i;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003673
3674 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003675 addstate(l, state, subs, 0);
Bram Moolenaar4b417062013-05-25 20:19:50 +02003676
Bram Moolenaara2d95102013-06-04 14:23:05 +02003677 /* fill in the "pim" field in the new states */
3678 if (pim != NULL)
3679 for (i = tlen; i < l->n; ++i)
3680 l->t[i].pim = pim;
3681
Bram Moolenaar4b417062013-05-25 20:19:50 +02003682 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003683 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003684 return;
3685
3686 /* re-order to put the new state at the current position */
3687 count = l->n - tlen;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003688 if (count == 1)
3689 {
3690 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003691 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02003692 }
3693 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02003694 {
3695 /* make space for new states, then move them from the
3696 * end to the current position */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003697 mch_memmove(&(l->t[listidx + count]),
3698 &(l->t[listidx + 1]),
3699 sizeof(nfa_thread_T) * (l->n - listidx - 1));
3700 mch_memmove(&(l->t[listidx]),
Bram Moolenaar4b417062013-05-25 20:19:50 +02003701 &(l->t[l->n - 1]),
3702 sizeof(nfa_thread_T) * count);
3703 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02003704 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02003705 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003706}
3707
3708/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709 * Check character class "class" against current character c.
3710 */
3711 static int
3712check_char_class(class, c)
3713 int class;
3714 int c;
3715{
3716 switch (class)
3717 {
3718 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003719 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720 return OK;
3721 break;
3722 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003723 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724 return OK;
3725 break;
3726 case NFA_CLASS_BLANK:
3727 if (c == ' ' || c == '\t')
3728 return OK;
3729 break;
3730 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003731 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003732 return OK;
3733 break;
3734 case NFA_CLASS_DIGIT:
3735 if (VIM_ISDIGIT(c))
3736 return OK;
3737 break;
3738 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003739 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003740 return OK;
3741 break;
3742 case NFA_CLASS_LOWER:
3743 if (MB_ISLOWER(c))
3744 return OK;
3745 break;
3746 case NFA_CLASS_PRINT:
3747 if (vim_isprintc(c))
3748 return OK;
3749 break;
3750 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02003751 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752 return OK;
3753 break;
3754 case NFA_CLASS_SPACE:
3755 if ((c >=9 && c <= 13) || (c == ' '))
3756 return OK;
3757 break;
3758 case NFA_CLASS_UPPER:
3759 if (MB_ISUPPER(c))
3760 return OK;
3761 break;
3762 case NFA_CLASS_XDIGIT:
3763 if (vim_isxdigit(c))
3764 return OK;
3765 break;
3766 case NFA_CLASS_TAB:
3767 if (c == '\t')
3768 return OK;
3769 break;
3770 case NFA_CLASS_RETURN:
3771 if (c == '\r')
3772 return OK;
3773 break;
3774 case NFA_CLASS_BACKSPACE:
3775 if (c == '\b')
3776 return OK;
3777 break;
3778 case NFA_CLASS_ESCAPE:
3779 if (c == '\033')
3780 return OK;
3781 break;
3782
3783 default:
3784 /* should not be here :P */
3785 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class "));
3786 }
3787 return FAIL;
3788}
3789
Bram Moolenaar5714b802013-05-28 22:03:20 +02003790static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
3791
3792/*
3793 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003794 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02003795 */
3796 static int
3797match_backref(sub, subidx, bytelen)
3798 regsub_T *sub; /* pointers to subexpressions */
3799 int subidx;
3800 int *bytelen; /* out: length of match in bytes */
3801{
3802 int len;
3803
3804 if (sub->in_use <= subidx)
3805 {
3806retempty:
3807 /* backref was not set, match an empty string */
3808 *bytelen = 0;
3809 return TRUE;
3810 }
3811
3812 if (REG_MULTI)
3813 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003814 if (sub->list.multi[subidx].start.lnum < 0
3815 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816 goto retempty;
3817 /* TODO: line breaks */
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003818 len = sub->list.multi[subidx].end.col
3819 - sub->list.multi[subidx].start.col;
3820 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
Bram Moolenaar5714b802013-05-28 22:03:20 +02003821 reginput, &len) == 0)
3822 {
3823 *bytelen = len;
3824 return TRUE;
3825 }
3826 }
3827 else
3828 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003829 if (sub->list.line[subidx].start == NULL
3830 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003831 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003832 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
3833 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003834 {
3835 *bytelen = len;
3836 return TRUE;
3837 }
3838 }
3839 return FALSE;
3840}
3841
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003842#ifdef FEAT_SYN_HL
3843
3844static int match_zref __ARGS((int subidx, int *bytelen));
3845
3846/*
3847 * Check for a match with \z subexpression "subidx".
3848 * Return TRUE if it matches.
3849 */
3850 static int
3851match_zref(subidx, bytelen)
3852 int subidx;
3853 int *bytelen; /* out: length of match in bytes */
3854{
3855 int len;
3856
3857 cleanup_zsubexpr();
3858 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
3859 {
3860 /* backref was not set, match an empty string */
3861 *bytelen = 0;
3862 return TRUE;
3863 }
3864
3865 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
3866 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
3867 {
3868 *bytelen = len;
3869 return TRUE;
3870 }
3871 return FALSE;
3872}
3873#endif
3874
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003875/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003876 * Save list IDs for all NFA states of "prog" into "list".
3877 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003878 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003879 */
3880 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003881nfa_save_listids(prog, list)
3882 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003883 int *list;
3884{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003885 int i;
3886 nfa_state_T *p;
3887
3888 /* Order in the list is reverse, it's a bit faster that way. */
3889 p = &prog->state[0];
3890 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003891 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003892 list[i] = p->lastlist[1];
3893 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003894 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003895 }
3896}
3897
3898/*
3899 * Restore list IDs from "list" to all NFA states.
3900 */
3901 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003902nfa_restore_listids(prog, list)
3903 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003904 int *list;
3905{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003906 int i;
3907 nfa_state_T *p;
3908
3909 p = &prog->state[0];
3910 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003911 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003912 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003913 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003914 }
3915}
3916
Bram Moolenaar423532e2013-05-29 21:14:42 +02003917 static int
3918nfa_re_num_cmp(val, op, pos)
3919 long_u val;
3920 int op;
3921 long_u pos;
3922{
3923 if (op == 1) return pos > val;
3924 if (op == 2) return pos < val;
3925 return val == pos;
3926}
3927
Bram Moolenaarf46da702013-06-02 22:37:42 +02003928static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003929static 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 +02003930
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003931/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02003932 * Recursively call nfa_regmatch()
3933 */
3934 static int
3935recursive_regmatch(state, prog, submatch, m, listids)
3936 nfa_state_T *state;
3937 nfa_regprog_T *prog;
3938 regsubs_T *submatch;
3939 regsubs_T *m;
3940 int **listids;
3941{
3942 char_u *save_reginput = reginput;
3943 char_u *save_regline = regline;
3944 int save_reglnum = reglnum;
3945 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003946 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003947 save_se_T *save_nfa_endp = nfa_endp;
3948 save_se_T endpos;
3949 save_se_T *endposp = NULL;
3950 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02003951 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02003952
3953 if (state->c == NFA_START_INVISIBLE_BEFORE)
3954 {
3955 /* The recursive match must end at the current position. */
3956 endposp = &endpos;
3957 if (REG_MULTI)
3958 {
3959 endpos.se_u.pos.col = (int)(reginput - regline);
3960 endpos.se_u.pos.lnum = reglnum;
3961 }
3962 else
3963 endpos.se_u.ptr = reginput;
3964
3965 /* Go back the specified number of bytes, or as far as the
3966 * start of the previous line, to try matching "\@<=" or
3967 * not matching "\@<!".
3968 * TODO: This is very inefficient! Would be better to
3969 * first check for a match with what follows. */
3970 if (state->val <= 0)
3971 {
3972 if (REG_MULTI)
3973 {
3974 regline = reg_getline(--reglnum);
3975 if (regline == NULL)
3976 /* can't go before the first line */
3977 regline = reg_getline(++reglnum);
3978 }
3979 reginput = regline;
3980 }
3981 else
3982 {
3983 if (REG_MULTI && (int)(reginput - regline) < state->val)
3984 {
3985 /* Not enough bytes in this line, go to end of
3986 * previous line. */
3987 regline = reg_getline(--reglnum);
3988 if (regline == NULL)
3989 {
3990 /* can't go before the first line */
3991 regline = reg_getline(++reglnum);
3992 reginput = regline;
3993 }
3994 else
3995 reginput = regline + STRLEN(regline);
3996 }
3997 if ((int)(reginput - regline) >= state->val)
3998 {
3999 reginput -= state->val;
4000#ifdef FEAT_MBYTE
4001 if (has_mbyte)
4002 reginput -= mb_head_off(regline, reginput);
4003#endif
4004 }
4005 else
4006 reginput = regline;
4007 }
4008 }
4009
Bram Moolenaarf46da702013-06-02 22:37:42 +02004010#ifdef ENABLE_LOG
4011 if (log_fd != stderr)
4012 fclose(log_fd);
4013 log_fd = NULL;
4014#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004015 /* Have to clear the lastlist field of the NFA nodes, so that
4016 * nfa_regmatch() and addstate() can run properly after recursion. */
4017 if (nfa_ll_index == 1)
4018 {
4019 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4020 * values and clear them. */
4021 if (*listids == NULL)
4022 {
4023 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4024 if (*listids == NULL)
4025 {
4026 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4027 return 0;
4028 }
4029 }
4030 nfa_save_listids(prog, *listids);
4031 need_restore = TRUE;
4032 /* any value of nfa_listid will do */
4033 }
4034 else
4035 {
4036 /* First recursive nfa_regmatch() call, switch to the second lastlist
4037 * entry. Make sure nfa_listid is different from a previous recursive
4038 * call, because some states may still have this ID. */
4039 ++nfa_ll_index;
4040 if (nfa_listid <= nfa_alt_listid)
4041 nfa_listid = nfa_alt_listid;
4042 }
4043
4044 /* Call nfa_regmatch() to check if the current concat matches at this
4045 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004046 nfa_endp = endposp;
4047 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004048
4049 if (need_restore)
4050 nfa_restore_listids(prog, *listids);
4051 else
4052 {
4053 --nfa_ll_index;
4054 nfa_alt_listid = nfa_listid;
4055 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004056
4057 /* restore position in input text */
4058 reginput = save_reginput;
4059 regline = save_regline;
4060 reglnum = save_reglnum;
4061 nfa_match = save_nfa_match;
4062 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004063 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004064
4065#ifdef ENABLE_LOG
4066 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4067 if (log_fd != NULL)
4068 {
4069 fprintf(log_fd, "****************************\n");
4070 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4071 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4072 fprintf(log_fd, "****************************\n");
4073 }
4074 else
4075 {
4076 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4077 log_fd = stderr;
4078 }
4079#endif
4080
4081 return result;
4082}
4083
Bram Moolenaara2d95102013-06-04 14:23:05 +02004084static int failure_chance __ARGS((nfa_state_T *state, int depth));
4085
4086/*
4087 * Estimate the chance of a match with "state" failing.
4088 * NFA_ANY: 1
4089 * specific character: 99
4090 */
4091 static int
4092failure_chance(state, depth)
4093 nfa_state_T *state;
4094 int depth;
4095{
4096 int c = state->c;
4097 int l, r;
4098
4099 /* detect looping */
4100 if (depth > 4)
4101 return 1;
4102
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004103 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004104 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004105 case NFA_SPLIT:
4106 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4107 /* avoid recursive stuff */
4108 return 1;
4109 /* two alternatives, use the lowest failure chance */
4110 l = failure_chance(state->out, depth + 1);
4111 r = failure_chance(state->out1, depth + 1);
4112 return l < r ? l : r;
4113
4114 case NFA_ANY:
4115 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004116 return 1;
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004117 case NFA_MATCH:
4118 /* empty match works always */
4119 return 0;
4120
4121 case NFA_BOL:
4122 case NFA_EOL:
4123 case NFA_BOF:
4124 case NFA_EOF:
4125 case NFA_NEWL:
4126 return 99;
4127
4128 case NFA_BOW:
4129 case NFA_EOW:
4130 return 90;
4131
4132 case NFA_MOPEN:
4133 case NFA_MOPEN1:
4134 case NFA_MOPEN2:
4135 case NFA_MOPEN3:
4136 case NFA_MOPEN4:
4137 case NFA_MOPEN5:
4138 case NFA_MOPEN6:
4139 case NFA_MOPEN7:
4140 case NFA_MOPEN8:
4141 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004142#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004143 case NFA_ZOPEN:
4144 case NFA_ZOPEN1:
4145 case NFA_ZOPEN2:
4146 case NFA_ZOPEN3:
4147 case NFA_ZOPEN4:
4148 case NFA_ZOPEN5:
4149 case NFA_ZOPEN6:
4150 case NFA_ZOPEN7:
4151 case NFA_ZOPEN8:
4152 case NFA_ZOPEN9:
4153 case NFA_ZCLOSE:
4154 case NFA_ZCLOSE1:
4155 case NFA_ZCLOSE2:
4156 case NFA_ZCLOSE3:
4157 case NFA_ZCLOSE4:
4158 case NFA_ZCLOSE5:
4159 case NFA_ZCLOSE6:
4160 case NFA_ZCLOSE7:
4161 case NFA_ZCLOSE8:
4162 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004163#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004164 case NFA_NOPEN:
4165 case NFA_MCLOSE:
4166 case NFA_MCLOSE1:
4167 case NFA_MCLOSE2:
4168 case NFA_MCLOSE3:
4169 case NFA_MCLOSE4:
4170 case NFA_MCLOSE5:
4171 case NFA_MCLOSE6:
4172 case NFA_MCLOSE7:
4173 case NFA_MCLOSE8:
4174 case NFA_MCLOSE9:
4175 case NFA_NCLOSE:
4176 return failure_chance(state->out, depth + 1);
4177
4178 case NFA_BACKREF1:
4179 case NFA_BACKREF2:
4180 case NFA_BACKREF3:
4181 case NFA_BACKREF4:
4182 case NFA_BACKREF5:
4183 case NFA_BACKREF6:
4184 case NFA_BACKREF7:
4185 case NFA_BACKREF8:
4186 case NFA_BACKREF9:
4187#ifdef FEAT_SYN_HL
4188 case NFA_ZREF1:
4189 case NFA_ZREF2:
4190 case NFA_ZREF3:
4191 case NFA_ZREF4:
4192 case NFA_ZREF5:
4193 case NFA_ZREF6:
4194 case NFA_ZREF7:
4195 case NFA_ZREF8:
4196 case NFA_ZREF9:
4197#endif
4198 /* backreferences don't match in many places */
4199 return 94;
4200
4201 case NFA_LNUM_GT:
4202 case NFA_LNUM_LT:
4203 case NFA_COL_GT:
4204 case NFA_COL_LT:
4205 case NFA_VCOL_GT:
4206 case NFA_VCOL_LT:
4207 case NFA_MARK_GT:
4208 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004209 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004210 /* before/after positions don't match very often */
4211 return 85;
4212
4213 case NFA_LNUM:
4214 return 90;
4215
4216 case NFA_CURSOR:
4217 case NFA_COL:
4218 case NFA_VCOL:
4219 case NFA_MARK:
4220 /* specific positions rarely match */
4221 return 98;
4222
4223 case NFA_COMPOSING:
4224 return 95;
4225
4226 default:
4227 if (c > 0)
4228 /* character match fails often */
4229 return 95;
4230 }
4231
4232 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004233 return 50;
4234}
4235
Bram Moolenaarf46da702013-06-02 22:37:42 +02004236/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004237 * Main matching routine.
4238 *
4239 * Run NFA to determine whether it matches reginput.
4240 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02004241 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02004242 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004243 * Return TRUE if there is a match, FALSE otherwise.
4244 * Note: Caller must ensure that: start != NULL.
4245 */
4246 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004247nfa_regmatch(prog, start, submatch, m)
4248 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004249 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004250 regsubs_T *submatch;
4251 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004253 int result;
4254 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004255 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004256 int go_to_nextline = FALSE;
4257 nfa_thread_T *t;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004258 nfa_list_T list[3];
4259 nfa_list_T *listtbl[2][2];
4260 nfa_list_T *ll;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004261 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004262 nfa_list_T *thislist;
4263 nfa_list_T *nextlist;
4264 nfa_list_T *neglist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004265 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004266 nfa_state_T *add_state;
4267 int add_count;
4268 int add_off;
4269 garray_T pimlist;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004270#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004271 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004272
4273 if (debug == NULL)
4274 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004275 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004276 return FALSE;
4277 }
4278#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004279 nfa_match = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004280 ga_init2(&pimlist, sizeof(nfa_pim_T), 5);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004281
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004282 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004283 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004284 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004285 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004286 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004287 list[1].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004288 list[2].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02004289 list[2].len = nstate + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL)
4291 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004292
4293#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004294 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295 if (log_fd != NULL)
4296 {
4297 fprintf(log_fd, "**********************************\n");
4298 nfa_set_code(start->c);
4299 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
4300 abs(start->id), code);
4301 fprintf(log_fd, "**********************************\n");
4302 }
4303 else
4304 {
4305 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4306 log_fd = stderr;
4307 }
4308#endif
4309
4310 thislist = &list[0];
4311 thislist->n = 0;
4312 nextlist = &list[1];
4313 nextlist->n = 0;
4314 neglist = &list[2];
4315 neglist->n = 0;
4316#ifdef ENABLE_LOG
4317 fprintf(log_fd, "(---) STARTSTATE\n");
4318#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004319 thislist->id = nfa_listid + 1;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004320 addstate(thislist, start, m, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321
4322 /* There are two cases when the NFA advances: 1. input char matches the
4323 * NFA node and 2. input char does not match the NFA node, but the next
4324 * node is NFA_NOT. The following macro calls addstate() according to
4325 * these rules. It is used A LOT, so use the "listtbl" table for speed */
4326 listtbl[0][0] = NULL;
4327 listtbl[0][1] = neglist;
4328 listtbl[1][0] = nextlist;
4329 listtbl[1][1] = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004330#define ADD_POS_NEG_STATE(state) \
4331 ll = listtbl[result ? 1 : 0][state->negated]; \
4332 if (ll != NULL) { \
4333 add_state = state->out; \
4334 add_off = clen; \
4335 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336
4337 /*
4338 * Run for each character.
4339 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02004340 for (;;)
4341 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004342 int curc;
4343 int clen;
4344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004345#ifdef FEAT_MBYTE
4346 if (has_mbyte)
4347 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004348 curc = (*mb_ptr2char)(reginput);
4349 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004350 }
4351 else
4352#endif
4353 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004354 curc = *reginput;
4355 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004357 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02004358 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004359 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004360 go_to_nextline = FALSE;
4361 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004362
4363 /* swap lists */
4364 thislist = &list[flag];
4365 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02004366 nextlist->n = 0; /* clear nextlist */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367 listtbl[1][0] = nextlist;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004368 ++nfa_listid;
4369 thislist->id = nfa_listid;
4370 nextlist->id = nfa_listid + 1;
4371 neglist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004372
Bram Moolenaara2d95102013-06-04 14:23:05 +02004373 pimlist.ga_len = 0;
4374
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375#ifdef ENABLE_LOG
4376 fprintf(log_fd, "------------------------------------------\n");
4377 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004378 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004379 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004380 {
4381 int i;
4382
4383 for (i = 0; i < thislist->n; i++)
4384 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
4385 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004386 fprintf(log_fd, "\n");
4387#endif
4388
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004389#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004390 fprintf(debug, "\n-------------------\n");
4391#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02004392 /*
4393 * If the state lists are empty we can stop.
4394 */
4395 if (thislist->n == 0 && neglist->n == 0)
4396 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004397
4398 /* compute nextlist */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004399 for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004400 {
4401 if (neglist->n > 0)
4402 {
4403 t = &neglist->t[0];
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +02004404 neglist->n--;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004405 listidx--;
Bram Moolenaard89616e2013-06-06 18:46:06 +02004406#ifdef ENABLE_LOG
4407 fprintf(log_fd, " using neglist entry, %d remaining\n",
4408 neglist->n);
4409#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004410 }
4411 else
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004412 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004413
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02004414#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004415 nfa_set_code(t->state->c);
4416 fprintf(debug, "%s, ", code);
4417#endif
4418#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004419 {
4420 int col;
4421
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004422 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004423 col = -1;
4424 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004425 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004426 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02004427 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004428 nfa_set_code(t->state->c);
4429 fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n",
4430 abs(t->state->id), (int)t->state->c, code, col);
4431 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004432#endif
4433
4434 /*
4435 * Handle the possible codes of the current state.
4436 * The most important is NFA_MATCH.
4437 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004438 add_state = NULL;
4439 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004440 switch (t->state->c)
4441 {
4442 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004443 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004444 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004445 copy_sub(&submatch->norm, &t->subs.norm);
4446#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004447 if (nfa_has_zsubexpr)
4448 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004449#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004450#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004451 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004452#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02004453 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004454 * states at this position. When the list of states is going
4455 * to be empty quit without advancing, so that "reginput" is
4456 * correct. */
4457 if (nextlist->n == 0 && neglist->n == 0)
4458 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004459 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02004460 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461
4462 case NFA_END_INVISIBLE:
Bram Moolenaar87953742013-06-05 18:52:40 +02004463 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02004464 /*
4465 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02004466 * NFA_START_INVISIBLE_BEFORE node.
4467 * They surround a zero-width group, used with "\@=", "\&",
4468 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 * If we got here, it means that the current "invisible" group
4470 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02004471 * nfa_regmatch(). For a look-behind match only when it ends
4472 * in the position in "nfa_endp".
4473 * Submatches are stored in *m, and used in the parent call.
4474 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02004475#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02004476 if (nfa_endp != NULL)
4477 {
4478 if (REG_MULTI)
4479 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
4480 (int)reglnum,
4481 (int)nfa_endp->se_u.pos.lnum,
4482 (int)(reginput - regline),
4483 nfa_endp->se_u.pos.col);
4484 else
4485 fprintf(log_fd, "Current col: %d, endp col: %d\n",
4486 (int)(reginput - regline),
4487 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004488 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004489#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02004490 /* If "nfa_endp" is set it's only a match if it ends at
4491 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004492 if (nfa_endp != NULL && (REG_MULTI
4493 ? (reglnum != nfa_endp->se_u.pos.lnum
4494 || (int)(reginput - regline)
4495 != nfa_endp->se_u.pos.col)
4496 : reginput != nfa_endp->se_u.ptr))
4497 break;
4498
4499 /* do not set submatches for \@! */
4500 if (!t->state->negated)
4501 {
4502 copy_sub(&m->norm, &t->subs.norm);
4503#ifdef FEAT_SYN_HL
4504 if (nfa_has_zsubexpr)
4505 copy_sub(&m->synt, &t->subs.synt);
4506#endif
4507 }
Bram Moolenaar87953742013-06-05 18:52:40 +02004508#ifdef ENABLE_LOG
4509 fprintf(log_fd, "Match found:\n");
4510 log_subsexpr(m);
4511#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02004512 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004513 break;
4514
4515 case NFA_START_INVISIBLE:
Bram Moolenaar61602c52013-06-01 19:54:43 +02004516 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004517 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02004518 nfa_pim_T *pim;
4519 int cout = t->state->out1->out->c;
4520
4521 /* Do it directly when what follows is possibly end of
4522 * match (closing paren).
4523 * Postpone when it is \@<= or \@<!, these are expensive.
4524 * TODO: remove the check for t->pim and check multiple
4525 * where it's used?
4526 * Otherwise first do the one that has the highest chance
4527 * of failing. */
4528 if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004529#ifdef FEAT_SYN_HL
Bram Moolenaara2d95102013-06-04 14:23:05 +02004530 || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9)
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004531#endif
Bram Moolenaara2d95102013-06-04 14:23:05 +02004532 || cout == NFA_NCLOSE
4533 || t->pim != NULL
4534 || (t->state->c != NFA_START_INVISIBLE_BEFORE
4535 && failure_chance(t->state->out1->out, 0)
4536 < failure_chance(t->state->out, 0)))
4537 {
4538 /*
4539 * First try matching the invisible match, then what
4540 * follows.
4541 */
4542 result = recursive_regmatch(t->state, prog,
4543 submatch, m, &listids);
4544
4545 /* for \@! it is a match when result is FALSE */
4546 if (result != t->state->negated)
4547 {
4548 /* Copy submatch info from the recursive call */
4549 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004550#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004551 if (nfa_has_zsubexpr)
4552 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004553#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004554
Bram Moolenaara2d95102013-06-04 14:23:05 +02004555 /* t->state->out1 is the corresponding
4556 * END_INVISIBLE node; Add its out to the current
4557 * list (zero-width match). */
4558 addstate_here(thislist, t->state->out1->out,
4559 &t->subs, t->pim, &listidx);
4560 }
4561 }
4562 else
4563 {
4564 /*
4565 * First try matching what follows at the current
4566 * position. Only if a match is found, addstate() is
4567 * called, then verify the invisible match matches.
4568 * Add a nfa_pim_T to the following states, it
4569 * contains info about the invisible match.
4570 */
4571 if (ga_grow(&pimlist, 1) == FAIL)
4572 goto theend;
4573 pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len;
4574 ++pimlist.ga_len;
4575 pim->state = t->state;
4576 pim->pim = NULL;
4577 pim->result = NFA_PIM_TODO;
4578
4579 /* t->state->out1 is the corresponding END_INVISIBLE
4580 * node; Add its out to the current list (zero-width
4581 * match). */
4582 addstate_here(thislist, t->state->out1->out, &t->subs,
4583 pim, &listidx);
4584 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004585 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004586 break;
4587
Bram Moolenaar87953742013-06-05 18:52:40 +02004588 case NFA_START_PATTERN:
4589 /* First try matching the pattern. */
4590 result = recursive_regmatch(t->state, prog,
4591 submatch, m, &listids);
4592 if (result)
4593 {
4594 int bytelen;
4595
4596#ifdef ENABLE_LOG
4597 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
4598 log_subsexpr(m);
4599#endif
4600 /* Copy submatch info from the recursive call */
4601 copy_sub_off(&t->subs.norm, &m->norm);
4602#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02004603 if (nfa_has_zsubexpr)
4604 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02004605#endif
4606 /* Now we need to skip over the matched text and then
4607 * continue with what follows. */
4608 if (REG_MULTI)
4609 /* TODO: multi-line match */
4610 bytelen = m->norm.list.multi[0].end.col
4611 - (int)(reginput - regline);
4612 else
4613 bytelen = (int)(m->norm.list.line[0].end - reginput);
4614
4615#ifdef ENABLE_LOG
4616 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
4617#endif
4618 if (bytelen == 0)
4619 {
4620 /* empty match, output of corresponding
4621 * NFA_END_PATTERN/NFA_SKIP to be used at current
4622 * position */
4623 addstate_here(thislist, t->state->out1->out->out,
4624 &t->subs, t->pim, &listidx);
4625 }
4626 else if (bytelen <= clen)
4627 {
4628 /* match current character, output of corresponding
4629 * NFA_END_PATTERN to be used at next position. */
4630 ll = nextlist;
4631 add_state = t->state->out1->out->out;
4632 add_off = clen;
4633 }
4634 else
4635 {
4636 /* skip over the matched characters, set character
4637 * count in NFA_SKIP */
4638 ll = nextlist;
4639 add_state = t->state->out1->out;
4640 add_off = bytelen;
4641 add_count = bytelen - clen;
4642 }
4643 }
4644 break;
4645
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646 case NFA_BOL:
4647 if (reginput == regline)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004648 addstate_here(thislist, t->state->out, &t->subs,
4649 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 break;
4651
4652 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004653 if (curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004654 addstate_here(thislist, t->state->out, &t->subs,
4655 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004656 break;
4657
4658 case NFA_BOW:
4659 {
4660 int bow = TRUE;
4661
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004662 if (curc == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004663 bow = FALSE;
4664#ifdef FEAT_MBYTE
4665 else if (has_mbyte)
4666 {
4667 int this_class;
4668
4669 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004670 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004671 if (this_class <= 1)
4672 bow = FALSE;
4673 else if (reg_prev_class() == this_class)
4674 bow = FALSE;
4675 }
4676#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004677 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004678 || (reginput > regline
4679 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680 bow = FALSE;
4681 if (bow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004682 addstate_here(thislist, t->state->out, &t->subs,
4683 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004684 break;
4685 }
4686
4687 case NFA_EOW:
4688 {
4689 int eow = TRUE;
4690
4691 if (reginput == regline)
4692 eow = FALSE;
4693#ifdef FEAT_MBYTE
4694 else if (has_mbyte)
4695 {
4696 int this_class, prev_class;
4697
4698 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004699 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004700 prev_class = reg_prev_class();
4701 if (this_class == prev_class
4702 || prev_class == 0 || prev_class == 1)
4703 eow = FALSE;
4704 }
4705#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004706 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004707 || (reginput[0] != NUL
4708 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004709 eow = FALSE;
4710 if (eow)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004711 addstate_here(thislist, t->state->out, &t->subs,
4712 t->pim, &listidx);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004713 break;
4714 }
4715
Bram Moolenaar4b780632013-05-31 22:14:52 +02004716 case NFA_BOF:
4717 if (reglnum == 0 && reginput == regline
4718 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaara2d95102013-06-04 14:23:05 +02004719 addstate_here(thislist, t->state->out, &t->subs,
4720 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004721 break;
4722
4723 case NFA_EOF:
4724 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004725 addstate_here(thislist, t->state->out, &t->subs,
4726 t->pim, &listidx);
Bram Moolenaar4b780632013-05-31 22:14:52 +02004727 break;
4728
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004729#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004731 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004732 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02004733 int len = 0;
4734 nfa_state_T *end;
4735 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004736 int cchars[MAX_MCO];
4737 int ccount = 0;
4738 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004739
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004741 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004742 if (utf_iscomposing(sta->c))
4743 {
4744 /* Only match composing character(s), ignore base
4745 * character. Used for ".{composing}" and "{composing}"
4746 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004747 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004748 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02004749 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004750 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02004751 /* If \Z was present, then ignore composing characters.
4752 * When ignoring the base character this always matches. */
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004753 /* TODO: How about negated? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004754 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004755 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004756 else
4757 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004758 while (sta->c != NFA_END_COMPOSING)
4759 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004760 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004761
4762 /* Check base character matches first, unless ignored. */
4763 else if (len > 0 || mc == sta->c)
4764 {
4765 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004766 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02004767 len += mb_char2len(mc);
4768 sta = sta->out;
4769 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004770
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004771 /* We don't care about the order of composing characters.
4772 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004773 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004774 {
4775 mc = mb_ptr2char(reginput + len);
4776 cchars[ccount++] = mc;
4777 len += mb_char2len(mc);
4778 if (ccount == MAX_MCO)
4779 break;
4780 }
4781
4782 /* Check that each composing char in the pattern matches a
4783 * composing char in the text. We do not check if all
4784 * composing chars are matched. */
4785 result = OK;
4786 while (sta->c != NFA_END_COMPOSING)
4787 {
4788 for (j = 0; j < ccount; ++j)
4789 if (cchars[j] == sta->c)
4790 break;
4791 if (j == ccount)
4792 {
4793 result = FAIL;
4794 break;
4795 }
4796 sta = sta->out;
4797 }
4798 }
4799 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02004800 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02004801
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004802 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004803 ADD_POS_NEG_STATE(end);
4804 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02004805 }
4806#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004807
4808 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004809 if (curc == NUL && !reg_line_lbr && REG_MULTI
4810 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004811 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02004812 go_to_nextline = TRUE;
4813 /* Pass -1 for the offset, which means taking the position
4814 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004815 ll = nextlist;
4816 add_state = t->state->out;
4817 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004818 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004819 else if (curc == '\n' && reg_line_lbr)
4820 {
4821 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004822 ll = nextlist;
4823 add_state = t->state->out;
4824 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02004825 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826 break;
4827
4828 case NFA_CLASS_ALNUM:
4829 case NFA_CLASS_ALPHA:
4830 case NFA_CLASS_BLANK:
4831 case NFA_CLASS_CNTRL:
4832 case NFA_CLASS_DIGIT:
4833 case NFA_CLASS_GRAPH:
4834 case NFA_CLASS_LOWER:
4835 case NFA_CLASS_PRINT:
4836 case NFA_CLASS_PUNCT:
4837 case NFA_CLASS_SPACE:
4838 case NFA_CLASS_UPPER:
4839 case NFA_CLASS_XDIGIT:
4840 case NFA_CLASS_TAB:
4841 case NFA_CLASS_RETURN:
4842 case NFA_CLASS_BACKSPACE:
4843 case NFA_CLASS_ESCAPE:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004844 result = check_char_class(t->state->c, curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004845 ADD_POS_NEG_STATE(t->state);
4846 break;
4847
4848 case NFA_END_NEG_RANGE:
4849 /* This follows a series of negated nodes, like:
Bram Moolenaard89616e2013-06-06 18:46:06 +02004850 * NOT CHAR(x), NOT CHAR(y), etc. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004851 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004852 {
4853 ll = nextlist;
4854 add_state = t->state->out;
4855 add_off = clen;
4856 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004857 break;
4858
4859 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02004860 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004861 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004862 {
4863 ll = nextlist;
4864 add_state = t->state->out;
4865 add_off = clen;
4866 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004867 break;
4868
4869 /*
4870 * Character classes like \a for alpha, \d for digit etc.
4871 */
4872 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004873 result = vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004874 ADD_POS_NEG_STATE(t->state);
4875 break;
4876
4877 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004878 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879 ADD_POS_NEG_STATE(t->state);
4880 break;
4881
4882 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02004883 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884 ADD_POS_NEG_STATE(t->state);
4885 break;
4886
4887 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004888 result = !VIM_ISDIGIT(curc)
4889 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004890 ADD_POS_NEG_STATE(t->state);
4891 break;
4892
4893 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004894 result = vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004895 ADD_POS_NEG_STATE(t->state);
4896 break;
4897
4898 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004899 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004900 ADD_POS_NEG_STATE(t->state);
4901 break;
4902
4903 case NFA_PRINT: /* \p */
Bram Moolenaar08050492013-05-21 12:43:56 +02004904 result = ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004905 ADD_POS_NEG_STATE(t->state);
4906 break;
4907
4908 case NFA_SPRINT: /* \P */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004909 result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004910 ADD_POS_NEG_STATE(t->state);
4911 break;
4912
4913 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004914 result = vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004915 ADD_POS_NEG_STATE(t->state);
4916 break;
4917
4918 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004919 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920 ADD_POS_NEG_STATE(t->state);
4921 break;
4922
4923 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004924 result = ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925 ADD_POS_NEG_STATE(t->state);
4926 break;
4927
4928 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004929 result = curc != NUL && !ri_digit(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930 ADD_POS_NEG_STATE(t->state);
4931 break;
4932
4933 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004934 result = ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004935 ADD_POS_NEG_STATE(t->state);
4936 break;
4937
4938 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004939 result = curc != NUL && !ri_hex(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004940 ADD_POS_NEG_STATE(t->state);
4941 break;
4942
4943 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004944 result = ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004945 ADD_POS_NEG_STATE(t->state);
4946 break;
4947
4948 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004949 result = curc != NUL && !ri_octal(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004950 ADD_POS_NEG_STATE(t->state);
4951 break;
4952
4953 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004954 result = ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004955 ADD_POS_NEG_STATE(t->state);
4956 break;
4957
4958 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004959 result = curc != NUL && !ri_word(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004960 ADD_POS_NEG_STATE(t->state);
4961 break;
4962
4963 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004964 result = ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004965 ADD_POS_NEG_STATE(t->state);
4966 break;
4967
4968 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004969 result = curc != NUL && !ri_head(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004970 ADD_POS_NEG_STATE(t->state);
4971 break;
4972
4973 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004974 result = ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004975 ADD_POS_NEG_STATE(t->state);
4976 break;
4977
4978 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004979 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004980 ADD_POS_NEG_STATE(t->state);
4981 break;
4982
4983 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004984 result = ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004985 ADD_POS_NEG_STATE(t->state);
4986 break;
4987
4988 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004989 result = curc != NUL && !ri_lower(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004990 ADD_POS_NEG_STATE(t->state);
4991 break;
4992
4993 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004994 result = ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004995 ADD_POS_NEG_STATE(t->state);
4996 break;
4997
4998 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02004999 result = curc != NUL && !ri_upper(curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005000 ADD_POS_NEG_STATE(t->state);
5001 break;
5002
Bram Moolenaar5714b802013-05-28 22:03:20 +02005003 case NFA_BACKREF1:
5004 case NFA_BACKREF2:
5005 case NFA_BACKREF3:
5006 case NFA_BACKREF4:
5007 case NFA_BACKREF5:
5008 case NFA_BACKREF6:
5009 case NFA_BACKREF7:
5010 case NFA_BACKREF8:
5011 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005012#ifdef FEAT_SYN_HL
5013 case NFA_ZREF1:
5014 case NFA_ZREF2:
5015 case NFA_ZREF3:
5016 case NFA_ZREF4:
5017 case NFA_ZREF5:
5018 case NFA_ZREF6:
5019 case NFA_ZREF7:
5020 case NFA_ZREF8:
5021 case NFA_ZREF9:
5022#endif
5023 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005024 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005025 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005026 int bytelen;
5027
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005028 if (t->state->c <= NFA_BACKREF9)
5029 {
5030 subidx = t->state->c - NFA_BACKREF1 + 1;
5031 result = match_backref(&t->subs.norm, subidx, &bytelen);
5032 }
5033#ifdef FEAT_SYN_HL
5034 else
5035 {
5036 subidx = t->state->c - NFA_ZREF1 + 1;
5037 result = match_zref(subidx, &bytelen);
5038 }
5039#endif
5040
Bram Moolenaar5714b802013-05-28 22:03:20 +02005041 if (result)
5042 {
5043 if (bytelen == 0)
5044 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005045 /* empty match always works, output of NFA_SKIP to be
5046 * used next */
5047 addstate_here(thislist, t->state->out->out, &t->subs,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005048 t->pim, &listidx);
Bram Moolenaar5714b802013-05-28 22:03:20 +02005049 }
5050 else if (bytelen <= clen)
5051 {
5052 /* match current character, jump ahead to out of
5053 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005054 ll = nextlist;
5055 add_state = t->state->out->out;
5056 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005057 }
5058 else
5059 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005060 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005061 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005062 ll = nextlist;
5063 add_state = t->state->out;
5064 add_off = bytelen;
5065 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005066 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005067 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005068 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005069 }
5070 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005071 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005072 if (t->count - clen <= 0)
5073 {
5074 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005075 ll = nextlist;
5076 add_state = t->state->out;
5077 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005078 }
5079 else
5080 {
5081 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005082 ll = nextlist;
5083 add_state = t->state;
5084 add_off = 0;
5085 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005086 }
5087 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005088
Bram Moolenaar423532e2013-05-29 21:14:42 +02005089 case NFA_LNUM:
5090 case NFA_LNUM_GT:
5091 case NFA_LNUM_LT:
5092 result = (REG_MULTI &&
5093 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
5094 (long_u)(reglnum + reg_firstlnum)));
5095 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005096 addstate_here(thislist, t->state->out, &t->subs,
5097 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005098 break;
5099
5100 case NFA_COL:
5101 case NFA_COL_GT:
5102 case NFA_COL_LT:
5103 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
5104 (long_u)(reginput - regline) + 1);
5105 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005106 addstate_here(thislist, t->state->out, &t->subs,
5107 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005108 break;
5109
5110 case NFA_VCOL:
5111 case NFA_VCOL_GT:
5112 case NFA_VCOL_LT:
5113 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
5114 (long_u)win_linetabsize(
5115 reg_win == NULL ? curwin : reg_win,
5116 regline, (colnr_T)(reginput - regline)) + 1);
5117 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005118 addstate_here(thislist, t->state->out, &t->subs,
5119 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005120 break;
5121
Bram Moolenaar044aa292013-06-04 21:27:38 +02005122 case NFA_MARK:
5123 case NFA_MARK_GT:
5124 case NFA_MARK_LT:
5125 {
5126 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
5127
5128 /* Compare the mark position to the match position. */
5129 result = (pos != NULL /* mark doesn't exist */
5130 && pos->lnum > 0 /* mark isn't set in reg_buf */
5131 && (pos->lnum == reglnum + reg_firstlnum
5132 ? (pos->col == (colnr_T)(reginput - regline)
5133 ? t->state->c == NFA_MARK
5134 : (pos->col < (colnr_T)(reginput - regline)
5135 ? t->state->c == NFA_MARK_GT
5136 : t->state->c == NFA_MARK_LT))
5137 : (pos->lnum < reglnum + reg_firstlnum
5138 ? t->state->c == NFA_MARK_GT
5139 : t->state->c == NFA_MARK_LT)));
5140 if (result)
5141 addstate_here(thislist, t->state->out, &t->subs,
5142 t->pim, &listidx);
5143 break;
5144 }
5145
Bram Moolenaar423532e2013-05-29 21:14:42 +02005146 case NFA_CURSOR:
5147 result = (reg_win != NULL
5148 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
5149 && ((colnr_T)(reginput - regline)
5150 == reg_win->w_cursor.col));
5151 if (result)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005152 addstate_here(thislist, t->state->out, &t->subs,
5153 t->pim, &listidx);
Bram Moolenaar423532e2013-05-29 21:14:42 +02005154 break;
5155
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005156 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02005157#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005158 result = reg_match_visual();
5159 if (result)
5160 addstate_here(thislist, t->state->out, &t->subs,
5161 t->pim, &listidx);
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02005162#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02005163 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02005164
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005165 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005166 {
5167 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005168
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005169 /* TODO: put this in #ifdef later */
5170 if (c < -256)
5171 EMSGN("INTERNAL: Negative state char: %ld", c);
5172 if (is_Magic(c))
5173 c = un_Magic(c);
5174 result = (c == curc);
5175
5176 if (!result && ireg_ic)
5177 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005178#ifdef FEAT_MBYTE
5179 /* If there is a composing character which is not being
5180 * ignored there can be no match. Match with composing
5181 * character uses NFA_COMPOSING above. */
5182 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005183 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005184 result = FALSE;
5185#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005186 ADD_POS_NEG_STATE(t->state);
5187 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02005188 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005189
5190 } /* switch (t->state->c) */
5191
5192 if (add_state != NULL)
5193 {
5194 if (t->pim != NULL)
5195 {
5196 /* postponed invisible match */
5197 /* TODO: also do t->pim->pim recursively? */
5198 if (t->pim->result == NFA_PIM_TODO)
5199 {
5200#ifdef ENABLE_LOG
5201 fprintf(log_fd, "\n");
5202 fprintf(log_fd, "==================================\n");
5203 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
5204 fprintf(log_fd, "\n");
5205#endif
5206 result = recursive_regmatch(t->pim->state,
5207 prog, submatch, m, &listids);
5208 t->pim->result = result ? NFA_PIM_MATCH
5209 : NFA_PIM_NOMATCH;
5210 /* for \@! it is a match when result is FALSE */
5211 if (result != t->pim->state->negated)
5212 {
5213 /* Copy submatch info from the recursive call */
5214 copy_sub_off(&t->pim->subs.norm, &m->norm);
5215#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005216 if (nfa_has_zsubexpr)
5217 copy_sub_off(&t->pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005218#endif
5219 }
5220 }
5221 else
5222 {
5223 result = (t->pim->result == NFA_PIM_MATCH);
5224#ifdef ENABLE_LOG
5225 fprintf(log_fd, "\n");
5226 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result);
5227 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5228 fprintf(log_fd, "\n");
5229#endif
5230 }
5231
5232 /* for \@! it is a match when result is FALSE */
5233 if (result != t->pim->state->negated)
5234 {
5235 /* Copy submatch info from the recursive call */
5236 copy_sub_off(&t->subs.norm, &t->pim->subs.norm);
5237#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005238 if (nfa_has_zsubexpr)
5239 copy_sub_off(&t->subs.synt, &t->pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005240#endif
5241 }
5242 else
5243 /* look-behind match failed, don't add the state */
5244 continue;
5245 }
5246
5247 addstate(ll, add_state, &t->subs, add_off);
5248 if (add_count > 0)
5249 nextlist->t[ll->n - 1].count = add_count;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005250 }
5251
5252 } /* for (thislist = thislist; thislist->state; thislist++) */
5253
Bram Moolenaare23febd2013-05-26 18:40:14 +02005254 /* Look for the start of a match in the current position by adding the
5255 * start state to the list of states.
5256 * The first found match is the leftmost one, thus the order of states
5257 * matters!
5258 * Do not add the start state in recursive calls of nfa_regmatch(),
5259 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02005260 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02005261 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005262 if (nfa_match == FALSE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005263 && ((start->c == NFA_MOPEN
Bram Moolenaar61602c52013-06-01 19:54:43 +02005264 && reglnum == 0
5265 && clen != 0
5266 && (ireg_maxcol == 0
5267 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02005268 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02005269 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02005270 ? (reglnum < nfa_endp->se_u.pos.lnum
5271 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02005272 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02005273 < nfa_endp->se_u.pos.col))
5274 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005275 {
5276#ifdef ENABLE_LOG
5277 fprintf(log_fd, "(---) STARTSTATE\n");
5278#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02005279 addstate(nextlist, start, m, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005280 }
5281
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005282#ifdef ENABLE_LOG
5283 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005284 {
5285 int i;
5286
5287 for (i = 0; i < thislist->n; i++)
5288 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5289 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005290 fprintf(log_fd, "\n");
5291#endif
5292
5293nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005294 /* Advance to the next character, or advance to the next line, or
5295 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005296 if (clen != 0)
5297 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02005298 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
5299 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02005300 reg_nextline();
5301 else
5302 break;
5303 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005304
5305#ifdef ENABLE_LOG
5306 if (log_fd != stderr)
5307 fclose(log_fd);
5308 log_fd = NULL;
5309#endif
5310
5311theend:
5312 /* Free memory */
5313 vim_free(list[0].t);
5314 vim_free(list[1].t);
5315 vim_free(list[2].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02005316 vim_free(listids);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005317 ga_clear(&pimlist);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005318#undef ADD_POS_NEG_STATE
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005319#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320 fclose(debug);
5321#endif
5322
Bram Moolenaar963fee22013-05-26 21:47:28 +02005323 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005324}
5325
5326/*
5327 * Try match of "prog" with at regline["col"].
5328 * Returns 0 for failure, number of lines contained in the match otherwise.
5329 */
5330 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005331nfa_regtry(prog, col)
5332 nfa_regprog_T *prog;
5333 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005334{
5335 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005336 regsubs_T subs, m;
5337 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005338#ifdef ENABLE_LOG
5339 FILE *f;
5340#endif
5341
5342 reginput = regline + col;
5343 need_clear_subexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005344#ifdef FEAT_SYN_HL
5345 /* Clear the external match subpointers if necessary. */
5346 if (prog->reghasz == REX_SET)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005347 {
5348 nfa_has_zsubexpr = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005349 need_clear_zsubexpr = TRUE;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005350 }
5351 else
5352 nfa_has_zsubexpr = FALSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005353#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005354
5355#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005356 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005357 if (f != NULL)
5358 {
Bram Moolenaar87953742013-06-05 18:52:40 +02005359 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005360#ifdef DEBUG
5361 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
5362#endif
5363 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02005364 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02005365 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005366 fprintf(f, "\n\n");
5367 fclose(f);
5368 }
5369 else
5370 EMSG(_("Could not open temporary log file for writing "));
5371#endif
5372
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005373 clear_sub(&subs.norm);
5374 clear_sub(&m.norm);
5375#ifdef FEAT_SYN_HL
5376 clear_sub(&subs.synt);
5377 clear_sub(&m.synt);
5378#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005379
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005380 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005381 return 0;
5382
5383 cleanup_subexpr();
5384 if (REG_MULTI)
5385 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005386 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005387 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005388 reg_startpos[i] = subs.norm.list.multi[i].start;
5389 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005390 }
5391
5392 if (reg_startpos[0].lnum < 0)
5393 {
5394 reg_startpos[0].lnum = 0;
5395 reg_startpos[0].col = col;
5396 }
5397 if (reg_endpos[0].lnum < 0)
5398 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02005399 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005400 reg_endpos[0].lnum = reglnum;
5401 reg_endpos[0].col = (int)(reginput - regline);
5402 }
5403 else
5404 /* Use line number of "\ze". */
5405 reglnum = reg_endpos[0].lnum;
5406 }
5407 else
5408 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005409 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005411 reg_startp[i] = subs.norm.list.line[i].start;
5412 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005413 }
5414
5415 if (reg_startp[0] == NULL)
5416 reg_startp[0] = regline + col;
5417 if (reg_endp[0] == NULL)
5418 reg_endp[0] = reginput;
5419 }
5420
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005421#ifdef FEAT_SYN_HL
5422 /* Package any found \z(...\) matches for export. Default is none. */
5423 unref_extmatch(re_extmatch_out);
5424 re_extmatch_out = NULL;
5425
5426 if (prog->reghasz == REX_SET)
5427 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005428 cleanup_zsubexpr();
5429 re_extmatch_out = make_extmatch();
5430 for (i = 0; i < subs.synt.in_use; i++)
5431 {
5432 if (REG_MULTI)
5433 {
5434 struct multipos *mpos = &subs.synt.list.multi[i];
5435
5436 /* Only accept single line matches. */
5437 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
5438 re_extmatch_out->matches[i] =
5439 vim_strnsave(reg_getline(mpos->start.lnum)
5440 + mpos->start.col,
5441 mpos->end.col - mpos->start.col);
5442 }
5443 else
5444 {
5445 struct linepos *lpos = &subs.synt.list.line[i];
5446
5447 if (lpos->start != NULL && lpos->end != NULL)
5448 re_extmatch_out->matches[i] =
5449 vim_strnsave(lpos->start,
5450 (int)(lpos->end - lpos->start));
5451 }
5452 }
5453 }
5454#endif
5455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005456 return 1 + reglnum;
5457}
5458
5459/*
5460 * Match a regexp against a string ("line" points to the string) or multiple
5461 * lines ("line" is NULL, use reg_getline()).
5462 *
5463 * Returns 0 for failure, number of lines contained in the match otherwise.
5464 */
5465 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02005466nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005467 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005468 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005469{
5470 nfa_regprog_T *prog;
5471 long retval = 0L;
5472 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005473 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474
5475 if (REG_MULTI)
5476 {
5477 prog = (nfa_regprog_T *)reg_mmatch->regprog;
5478 line = reg_getline((linenr_T)0); /* relative to the cursor */
5479 reg_startpos = reg_mmatch->startpos;
5480 reg_endpos = reg_mmatch->endpos;
5481 }
5482 else
5483 {
5484 prog = (nfa_regprog_T *)reg_match->regprog;
5485 reg_startp = reg_match->startp;
5486 reg_endp = reg_match->endp;
5487 }
5488
5489 /* Be paranoid... */
5490 if (prog == NULL || line == NULL)
5491 {
5492 EMSG(_(e_null));
5493 goto theend;
5494 }
5495
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005496 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
5497 if (prog->regflags & RF_ICASE)
5498 ireg_ic = TRUE;
5499 else if (prog->regflags & RF_NOICASE)
5500 ireg_ic = FALSE;
5501
5502#ifdef FEAT_MBYTE
5503 /* If pattern contains "\Z" overrule value of ireg_icombine */
5504 if (prog->regflags & RF_ICOMBINE)
5505 ireg_icombine = TRUE;
5506#endif
5507
5508 regline = line;
5509 reglnum = 0; /* relative to line */
5510
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005511 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005512 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005513 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005514 nfa_listid = 1;
5515 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005516#ifdef DEBUG
5517 nfa_regengine.expr = prog->pattern;
5518#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005519
Bram Moolenaard89616e2013-06-06 18:46:06 +02005520 if (prog->reganch && col > 0)
5521 return 0L;
5522
5523 if (prog->regstart != NUL)
5524 {
5525 char_u *s;
5526
5527 /* Skip until the char we know it must start with.
5528 * Used often, do some work to avoid call overhead. */
5529 if (!ireg_ic
5530#ifdef FEAT_MBYTE
5531 && !has_mbyte
5532#endif
5533 )
5534 s = vim_strbyte(regline + col, prog->regstart);
5535 else
5536 s = cstrchr(regline + col, prog->regstart);
5537 if (s == NULL)
5538 return 0L;
5539 col = (int)(s - regline);
5540 }
5541
5542 /* If the start column is past the maximum column: no need to try. */
5543 if (ireg_maxcol > 0 && col >= ireg_maxcol)
5544 goto theend;
5545
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005546 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005547 for (i = 0; i < nstate; ++i)
5548 {
5549 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005550 prog->state[i].lastlist[0] = 0;
5551 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552 }
5553
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005554 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005555
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005556#ifdef DEBUG
5557 nfa_regengine.expr = NULL;
5558#endif
5559
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560theend:
5561 return retval;
5562}
5563
5564/*
5565 * Compile a regular expression into internal code for the NFA matcher.
5566 * Returns the program in allocated space. Returns NULL for an error.
5567 */
5568 static regprog_T *
5569nfa_regcomp(expr, re_flags)
5570 char_u *expr;
5571 int re_flags;
5572{
Bram Moolenaaraae48832013-05-25 21:18:34 +02005573 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02005574 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005575 int *postfix;
5576
5577 if (expr == NULL)
5578 return NULL;
5579
5580#ifdef DEBUG
5581 nfa_regengine.expr = expr;
5582#endif
5583
5584 init_class_tab();
5585
5586 if (nfa_regcomp_start(expr, re_flags) == FAIL)
5587 return NULL;
5588
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005589 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02005590 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 postfix = re2post();
5592 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005593 {
5594 /* TODO: only give this error for debugging? */
5595 if (post_ptr >= post_end)
5596 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005598 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599
5600 /*
5601 * In order to build the NFA, we parse the input regexp twice:
5602 * 1. first pass to count size (so we can allocate space)
5603 * 2. second to emit code
5604 */
5605#ifdef ENABLE_LOG
5606 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005607 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005608
5609 if (f != NULL)
5610 {
5611 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
5612 fclose(f);
5613 }
5614 }
5615#endif
5616
5617 /*
5618 * PASS 1
5619 * Count number of NFA states in "nstate". Do not build the NFA.
5620 */
5621 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02005622
5623 /* Space for compiled regexp */
5624 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate;
5625 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
5626 if (prog == NULL)
5627 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005628 state_ptr = prog->state;
5629
5630 /*
5631 * PASS 2
5632 * Build the NFA
5633 */
5634 prog->start = post2nfa(postfix, post_ptr, FALSE);
5635 if (prog->start == NULL)
5636 goto fail;
5637
5638 prog->regflags = regflags;
5639 prog->engine = &nfa_regengine;
5640 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005641 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02005642 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02005643 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02005644
5645 prog->reganch = nfa_get_reganch(prog->start, 0);
5646 prog->regstart = nfa_get_regstart(prog->start, 0);
5647
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648#ifdef ENABLE_LOG
5649 nfa_postfix_dump(expr, OK);
5650 nfa_dump(prog);
5651#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005652#ifdef FEAT_SYN_HL
5653 /* Remember whether this pattern has any \z specials in it. */
5654 prog->reghasz = re_has_z;
5655#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005656#ifdef DEBUG
5657 prog->pattern = vim_strsave(expr); /* memory will leak */
5658 nfa_regengine.expr = NULL;
5659#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005660
5661out:
5662 vim_free(post_start);
5663 post_start = post_ptr = post_end = NULL;
5664 state_ptr = NULL;
5665 return (regprog_T *)prog;
5666
5667fail:
5668 vim_free(prog);
5669 prog = NULL;
5670#ifdef ENABLE_LOG
5671 nfa_postfix_dump(expr, FAIL);
5672#endif
5673#ifdef DEBUG
5674 nfa_regengine.expr = NULL;
5675#endif
5676 goto out;
5677}
5678
5679
5680/*
5681 * Match a regexp against a string.
5682 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
5683 * Uses curbuf for line count and 'iskeyword'.
5684 *
5685 * Return TRUE if there is a match, FALSE if not.
5686 */
5687 static int
5688nfa_regexec(rmp, line, col)
5689 regmatch_T *rmp;
5690 char_u *line; /* string to match against */
5691 colnr_T col; /* column to start looking for match */
5692{
5693 reg_match = rmp;
5694 reg_mmatch = NULL;
5695 reg_maxline = 0;
5696 reg_line_lbr = FALSE;
5697 reg_buf = curbuf;
5698 reg_win = NULL;
5699 ireg_ic = rmp->rm_ic;
5700#ifdef FEAT_MBYTE
5701 ireg_icombine = FALSE;
5702#endif
5703 ireg_maxcol = 0;
5704 return (nfa_regexec_both(line, col) != 0);
5705}
5706
5707#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
5708 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
5709
5710static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
5711
5712/*
5713 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
5714 */
5715 static int
5716nfa_regexec_nl(rmp, line, col)
5717 regmatch_T *rmp;
5718 char_u *line; /* string to match against */
5719 colnr_T col; /* column to start looking for match */
5720{
5721 reg_match = rmp;
5722 reg_mmatch = NULL;
5723 reg_maxline = 0;
5724 reg_line_lbr = TRUE;
5725 reg_buf = curbuf;
5726 reg_win = NULL;
5727 ireg_ic = rmp->rm_ic;
5728#ifdef FEAT_MBYTE
5729 ireg_icombine = FALSE;
5730#endif
5731 ireg_maxcol = 0;
5732 return (nfa_regexec_both(line, col) != 0);
5733}
5734#endif
5735
5736
5737/*
5738 * Match a regexp against multiple lines.
5739 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5740 * Uses curbuf for line count and 'iskeyword'.
5741 *
5742 * Return zero if there is no match. Return number of lines contained in the
5743 * match otherwise.
5744 *
5745 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
5746 *
5747 * ! Also NOTE : match may actually be in another line. e.g.:
5748 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
5749 *
5750 * +-------------------------+
5751 * |a |
5752 * |b |
5753 * |c |
5754 * | |
5755 * +-------------------------+
5756 *
5757 * then nfa_regexec_multi() returns 3. while the original
5758 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
5759 *
5760 * FIXME if this behavior is not compatible.
5761 */
5762 static long
5763nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
5764 regmmatch_T *rmp;
5765 win_T *win; /* window in which to search or NULL */
5766 buf_T *buf; /* buffer in which to search */
5767 linenr_T lnum; /* nr of line to start looking for match */
5768 colnr_T col; /* column to start looking for match */
5769 proftime_T *tm UNUSED; /* timeout limit or NULL */
5770{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005771 reg_match = NULL;
5772 reg_mmatch = rmp;
5773 reg_buf = buf;
5774 reg_win = win;
5775 reg_firstlnum = lnum;
5776 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
5777 reg_line_lbr = FALSE;
5778 ireg_ic = rmp->rmm_ic;
5779#ifdef FEAT_MBYTE
5780 ireg_icombine = FALSE;
5781#endif
5782 ireg_maxcol = rmp->rmm_maxcol;
5783
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005784 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005785}
5786
5787#ifdef DEBUG
5788# undef ENABLE_LOG
5789#endif