blob: 863ad6cacafe18f536df1908380e99b3bffa0738 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
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 Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaar0270f382018-07-17 05:43:58 +0200247// Variables only used in nfa_regcomp() and descendants.
248static int nfa_re_flags; // re_flags passed to nfa_regcomp()
249static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250static int *post_end;
251static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200252static int nstate; // Number of states in the NFA.
253static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200254
Bram Moolenaar307aa162013-06-02 16:34:21 +0200255/* If not NULL match must end at this position */
256static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200258/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
259static int nfa_ll_index = 0;
260
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100261static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100262static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200263#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100264static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100266static int match_follows(nfa_state_T *startstate, int depth);
267static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268
269/* helper functions used when doing re2post() ... regatom() parsing */
270#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200271 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272 return FAIL; \
273 *post_ptr++ = c; \
274 } while (0)
275
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276/*
277 * Initialize internal variables before NFA compilation.
278 * Return OK on success, FAIL otherwise.
279 */
280 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100281nfa_regcomp_start(
282 char_u *expr,
283 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200284{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200285 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200286 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287
288 nstate = 0;
289 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200290 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200291 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200293 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200294 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200295 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200296
297 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200298 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300 post_start = (int *)lalloc(postfix_size, TRUE);
301 if (post_start == NULL)
302 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200304 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200305 rex.nfa_has_zend = FALSE;
306 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200308 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200309 regcomp_start(expr, re_flags);
310
311 return OK;
312}
313
314/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200315 * Figure out if the NFA state list starts with an anchor, must match at start
316 * of the line.
317 */
318 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100319nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200320{
321 nfa_state_T *p = start;
322
323 if (depth > 4)
324 return 0;
325
326 while (p != NULL)
327 {
328 switch (p->c)
329 {
330 case NFA_BOL:
331 case NFA_BOF:
332 return 1; /* yes! */
333
334 case NFA_ZSTART:
335 case NFA_ZEND:
336 case NFA_CURSOR:
337 case NFA_VISUAL:
338
339 case NFA_MOPEN:
340 case NFA_MOPEN1:
341 case NFA_MOPEN2:
342 case NFA_MOPEN3:
343 case NFA_MOPEN4:
344 case NFA_MOPEN5:
345 case NFA_MOPEN6:
346 case NFA_MOPEN7:
347 case NFA_MOPEN8:
348 case NFA_MOPEN9:
349 case NFA_NOPEN:
350#ifdef FEAT_SYN_HL
351 case NFA_ZOPEN:
352 case NFA_ZOPEN1:
353 case NFA_ZOPEN2:
354 case NFA_ZOPEN3:
355 case NFA_ZOPEN4:
356 case NFA_ZOPEN5:
357 case NFA_ZOPEN6:
358 case NFA_ZOPEN7:
359 case NFA_ZOPEN8:
360 case NFA_ZOPEN9:
361#endif
362 p = p->out;
363 break;
364
365 case NFA_SPLIT:
366 return nfa_get_reganch(p->out, depth + 1)
367 && nfa_get_reganch(p->out1, depth + 1);
368
369 default:
370 return 0; /* noooo */
371 }
372 }
373 return 0;
374}
375
376/*
377 * Figure out if the NFA state list starts with a character which must match
378 * at start of the match.
379 */
380 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100381nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200382{
383 nfa_state_T *p = start;
384
385 if (depth > 4)
386 return 0;
387
388 while (p != NULL)
389 {
390 switch (p->c)
391 {
392 /* all kinds of zero-width matches */
393 case NFA_BOL:
394 case NFA_BOF:
395 case NFA_BOW:
396 case NFA_EOW:
397 case NFA_ZSTART:
398 case NFA_ZEND:
399 case NFA_CURSOR:
400 case NFA_VISUAL:
401 case NFA_LNUM:
402 case NFA_LNUM_GT:
403 case NFA_LNUM_LT:
404 case NFA_COL:
405 case NFA_COL_GT:
406 case NFA_COL_LT:
407 case NFA_VCOL:
408 case NFA_VCOL_GT:
409 case NFA_VCOL_LT:
410 case NFA_MARK:
411 case NFA_MARK_GT:
412 case NFA_MARK_LT:
413
414 case NFA_MOPEN:
415 case NFA_MOPEN1:
416 case NFA_MOPEN2:
417 case NFA_MOPEN3:
418 case NFA_MOPEN4:
419 case NFA_MOPEN5:
420 case NFA_MOPEN6:
421 case NFA_MOPEN7:
422 case NFA_MOPEN8:
423 case NFA_MOPEN9:
424 case NFA_NOPEN:
425#ifdef FEAT_SYN_HL
426 case NFA_ZOPEN:
427 case NFA_ZOPEN1:
428 case NFA_ZOPEN2:
429 case NFA_ZOPEN3:
430 case NFA_ZOPEN4:
431 case NFA_ZOPEN5:
432 case NFA_ZOPEN6:
433 case NFA_ZOPEN7:
434 case NFA_ZOPEN8:
435 case NFA_ZOPEN9:
436#endif
437 p = p->out;
438 break;
439
440 case NFA_SPLIT:
441 {
442 int c1 = nfa_get_regstart(p->out, depth + 1);
443 int c2 = nfa_get_regstart(p->out1, depth + 1);
444
445 if (c1 == c2)
446 return c1; /* yes! */
447 return 0;
448 }
449
450 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200451 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200452 return p->c; /* yes! */
453 return 0;
454 }
455 }
456 return 0;
457}
458
459/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200460 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200461 * else. If so return a string in allocated memory with what must match after
462 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200463 */
464 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100465nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200466{
467 nfa_state_T *p = start;
468 int len = 0;
469 char_u *ret;
470 char_u *s;
471
472 if (p->c != NFA_MOPEN)
473 return NULL; /* just in case */
474 p = p->out;
475 while (p->c > 0)
476 {
477 len += MB_CHAR2LEN(p->c);
478 p = p->out;
479 }
480 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
481 return NULL;
482
483 ret = alloc(len);
484 if (ret != NULL)
485 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200486 p = start->out->out; /* skip first char, it goes into regstart */
487 s = ret;
488 while (p->c > 0)
489 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200490 if (has_mbyte)
491 s += (*mb_char2bytes)(p->c, s);
492 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200493 *s++ = p->c;
494 p = p->out;
495 }
496 *s = NUL;
497 }
498 return ret;
499}
500
501/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200502 * Allocate more space for post_start. Called when
503 * running above the estimated number of states.
504 */
505 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100506realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200507{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200508 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200509 int new_max = nstate_max + 1000;
510 int *new_start;
511 int *old_start;
512
513 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
514 if (new_start == NULL)
515 return FAIL;
516 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200517 old_start = post_start;
518 post_start = new_start;
519 post_ptr = new_start + (post_ptr - old_start);
520 post_end = post_start + new_max;
521 vim_free(old_start);
522 return OK;
523}
524
525/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200526 * Search between "start" and "end" and try to recognize a
527 * character class in expanded form. For example [0-9].
528 * On success, return the id the character class to be emitted.
529 * On failure, return 0 (=FAIL)
530 * Start points to the first char of the range, while end should point
531 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200532 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
533 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200534 */
535 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100536nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200537{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200538# define CLASS_not 0x80
539# define CLASS_af 0x40
540# define CLASS_AF 0x20
541# define CLASS_az 0x10
542# define CLASS_AZ 0x08
543# define CLASS_o7 0x04
544# define CLASS_o9 0x02
545# define CLASS_underscore 0x01
546
547 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200548 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200549 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200550
551 if (extra_newl == TRUE)
552 newl = TRUE;
553
554 if (*end != ']')
555 return FAIL;
556 p = start;
557 if (*p == '^')
558 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200559 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200560 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200561 }
562
563 while (p < end)
564 {
565 if (p + 2 < end && *(p + 1) == '-')
566 {
567 switch (*p)
568 {
569 case '0':
570 if (*(p + 2) == '9')
571 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200572 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200573 break;
574 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200575 if (*(p + 2) == '7')
576 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200577 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200578 break;
579 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200580 return FAIL;
581
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 case 'a':
583 if (*(p + 2) == 'z')
584 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200585 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200586 break;
587 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200588 if (*(p + 2) == 'f')
589 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200590 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200591 break;
592 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200593 return FAIL;
594
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200595 case 'A':
596 if (*(p + 2) == 'Z')
597 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200598 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200599 break;
600 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200601 if (*(p + 2) == 'F')
602 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200603 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 break;
605 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200606 return FAIL;
607
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200608 default:
609 return FAIL;
610 }
611 p += 3;
612 }
613 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
614 {
615 newl = TRUE;
616 p += 2;
617 }
618 else if (*p == '_')
619 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200620 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621 p ++;
622 }
623 else if (*p == '\n')
624 {
625 newl = TRUE;
626 p ++;
627 }
628 else
629 return FAIL;
630 } /* while (p < end) */
631
632 if (p != end)
633 return FAIL;
634
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200636 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200637
638 switch (config)
639 {
640 case CLASS_o9:
641 return extra_newl + NFA_DIGIT;
642 case CLASS_not | CLASS_o9:
643 return extra_newl + NFA_NDIGIT;
644 case CLASS_af | CLASS_AF | CLASS_o9:
645 return extra_newl + NFA_HEX;
646 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
647 return extra_newl + NFA_NHEX;
648 case CLASS_o7:
649 return extra_newl + NFA_OCTAL;
650 case CLASS_not | CLASS_o7:
651 return extra_newl + NFA_NOCTAL;
652 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
653 return extra_newl + NFA_WORD;
654 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
655 return extra_newl + NFA_NWORD;
656 case CLASS_az | CLASS_AZ | CLASS_underscore:
657 return extra_newl + NFA_HEAD;
658 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
659 return extra_newl + NFA_NHEAD;
660 case CLASS_az | CLASS_AZ:
661 return extra_newl + NFA_ALPHA;
662 case CLASS_not | CLASS_az | CLASS_AZ:
663 return extra_newl + NFA_NALPHA;
664 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200665 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200666 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200667 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200668 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200669 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200670 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200671 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200672 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200673 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200674}
675
676/*
677 * Produce the bytes for equivalence class "c".
678 * Currently only handles latin1, latin9 and utf-8.
679 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
680 * equivalent to 'a OR b OR c'
681 *
682 * NOTE! When changing this function, also update reg_equi_class()
683 */
684 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100685nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200686{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200687#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100688#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200690 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
691 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200693#ifdef EBCDIC
694# define A_circumflex 0x62
695# define A_diaeresis 0x63
696# define A_grave 0x64
697# define A_acute 0x65
698# define A_virguilla 0x66
699# define A_ring 0x67
700# define C_cedilla 0x68
701# define E_acute 0x71
702# define E_circumflex 0x72
703# define E_diaeresis 0x73
704# define E_grave 0x74
705# define I_acute 0x75
706# define I_circumflex 0x76
707# define I_diaeresis 0x77
708# define I_grave 0x78
709# define N_virguilla 0x69
710# define O_circumflex 0xeb
711# define O_diaeresis 0xec
712# define O_grave 0xed
713# define O_acute 0xee
714# define O_virguilla 0xef
715# define O_slash 0x80
716# define U_circumflex 0xfb
717# define U_diaeresis 0xfc
718# define U_grave 0xfd
719# define U_acute 0xfe
720# define Y_acute 0xba
721# define a_grave 0x42
722# define a_acute 0x43
723# define a_circumflex 0x44
724# define a_virguilla 0x45
725# define a_diaeresis 0x46
726# define a_ring 0x47
727# define c_cedilla 0x48
728# define e_grave 0x51
729# define e_acute 0x52
730# define e_circumflex 0x53
731# define e_diaeresis 0x54
732# define i_grave 0x55
733# define i_acute 0x56
734# define i_circumflex 0x57
735# define i_diaeresis 0x58
736# define n_virguilla 0x49
737# define o_grave 0xcb
738# define o_acute 0xcc
739# define o_circumflex 0xcd
740# define o_virguilla 0xce
741# define o_diaeresis 0xcf
742# define o_slash 0x70
743# define u_grave 0xdb
744# define u_acute 0xdc
745# define u_circumflex 0xdd
746# define u_diaeresis 0xde
747# define y_acute 0x8d
748# define y_diaeresis 0xdf
749#else
750# define A_grave 0xc0
751# define A_acute 0xc1
752# define A_circumflex 0xc2
753# define A_virguilla 0xc3
754# define A_diaeresis 0xc4
755# define A_ring 0xc5
756# define C_cedilla 0xc7
757# define E_grave 0xc8
758# define E_acute 0xc9
759# define E_circumflex 0xca
760# define E_diaeresis 0xcb
761# define I_grave 0xcc
762# define I_acute 0xcd
763# define I_circumflex 0xce
764# define I_diaeresis 0xcf
765# define N_virguilla 0xd1
766# define O_grave 0xd2
767# define O_acute 0xd3
768# define O_circumflex 0xd4
769# define O_virguilla 0xd5
770# define O_diaeresis 0xd6
771# define O_slash 0xd8
772# define U_grave 0xd9
773# define U_acute 0xda
774# define U_circumflex 0xdb
775# define U_diaeresis 0xdc
776# define Y_acute 0xdd
777# define a_grave 0xe0
778# define a_acute 0xe1
779# define a_circumflex 0xe2
780# define a_virguilla 0xe3
781# define a_diaeresis 0xe4
782# define a_ring 0xe5
783# define c_cedilla 0xe7
784# define e_grave 0xe8
785# define e_acute 0xe9
786# define e_circumflex 0xea
787# define e_diaeresis 0xeb
788# define i_grave 0xec
789# define i_acute 0xed
790# define i_circumflex 0xee
791# define i_diaeresis 0xef
792# define n_virguilla 0xf1
793# define o_grave 0xf2
794# define o_acute 0xf3
795# define o_circumflex 0xf4
796# define o_virguilla 0xf5
797# define o_diaeresis 0xf6
798# define o_slash 0xf8
799# define u_grave 0xf9
800# define u_acute 0xfa
801# define u_circumflex 0xfb
802# define u_diaeresis 0xfc
803# define y_acute 0xfd
804# define y_diaeresis 0xff
805#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200806 switch (c)
807 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200808 case 'A': case A_grave: case A_acute: case A_circumflex:
809 case A_virguilla: case A_diaeresis: case A_ring:
810 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
811 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
812 CASEMBC(0x1ea2)
813 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
814 EMIT2(A_circumflex); EMIT2(A_virguilla);
815 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200816 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
817 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
818 EMITMBC(0x1ea2)
819 return OK;
820
821 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
822 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 return OK;
824
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200825 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
826 CASEMBC(0x10a) CASEMBC(0x10c)
827 EMIT2('C'); EMIT2(C_cedilla);
828 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200829 EMITMBC(0x10a) EMITMBC(0x10c)
830 return OK;
831
832 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200833 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200834 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
835 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200836 return OK;
837
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200838 case 'E': case E_grave: case E_acute: case E_circumflex:
839 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
840 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
841 CASEMBC(0x1eba) CASEMBC(0x1ebc)
842 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
843 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200844 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
845 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
846 EMITMBC(0x1ebc)
847 return OK;
848
849 case 'F': CASEMBC(0x1e1e)
850 EMIT2('F'); EMITMBC(0x1e1e)
851 return OK;
852
853 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200854 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
855 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200856 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
857 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
858 EMITMBC(0x1f4) EMITMBC(0x1e20)
859 return OK;
860
861 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200862 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200863 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
864 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200865 return OK;
866
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200867 case 'I': case I_grave: case I_acute: case I_circumflex:
868 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
869 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
870 CASEMBC(0x1cf) CASEMBC(0x1ec8)
871 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
872 EMIT2(I_circumflex); EMIT2(I_diaeresis);
873 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200874 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
875 EMITMBC(0x1cf) EMITMBC(0x1ec8)
876 return OK;
877
878 case 'J': CASEMBC(0x134)
879 EMIT2('J'); EMITMBC(0x134)
880 return OK;
881
882 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200883 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200884 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
885 EMITMBC(0x1e34)
886 return OK;
887
888 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200889 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200890 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
891 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
892 return OK;
893
894 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
895 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200896 return OK;
897
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200898 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
899 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
900 EMIT2('N'); EMIT2(N_virguilla);
901 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200902 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200903 return OK;
904
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200905 case 'O': case O_grave: case O_acute: case O_circumflex:
906 case O_virguilla: case O_diaeresis: case O_slash:
907 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
908 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
909 CASEMBC(0x1ec) CASEMBC(0x1ece)
910 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
911 EMIT2(O_circumflex); EMIT2(O_virguilla);
912 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200913 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
914 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
915 EMITMBC(0x1ec) EMITMBC(0x1ece)
916 return OK;
917
918 case 'P': case 0x1e54: case 0x1e56:
919 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
920 return OK;
921
922 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200923 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200924 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
925 EMITMBC(0x1e58) EMITMBC(0x1e5e)
926 return OK;
927
928 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200930 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
931 EMITMBC(0x160) EMITMBC(0x1e60)
932 return OK;
933
934 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200935 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
937 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200938 return OK;
939
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200940 case 'U': case U_grave: case U_acute: case U_diaeresis:
941 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
942 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
943 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
944 CASEMBC(0x1ee6)
945 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
946 EMIT2(U_diaeresis); EMIT2(U_circumflex);
947 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200948 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
949 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
950 EMITMBC(0x1ee6)
951 return OK;
952
953 case 'V': CASEMBC(0x1e7c)
954 EMIT2('V'); EMITMBC(0x1e7c)
955 return OK;
956
957 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200958 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200959 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
960 EMITMBC(0x1e84) EMITMBC(0x1e86)
961 return OK;
962
963 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
964 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 return OK;
966
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200967 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
968 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
969 CASEMBC(0x1ef8)
970 EMIT2('Y'); EMIT2(Y_acute);
971 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200972 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
973 EMITMBC(0x1ef8)
974 return OK;
975
976 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200977 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200978 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
979 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200980 return OK;
981
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200982 case 'a': case a_grave: case a_acute: case a_circumflex:
983 case a_virguilla: case a_diaeresis: case a_ring:
984 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
985 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
986 CASEMBC(0x1ea3)
987 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
988 EMIT2(a_circumflex); EMIT2(a_virguilla);
989 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200990 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
991 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
992 EMITMBC(0x1ea3)
993 return OK;
994
995 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
996 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200997 return OK;
998
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200999 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1000 CASEMBC(0x10b) CASEMBC(0x10d)
1001 EMIT2('c'); EMIT2(c_cedilla);
1002 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001003 EMITMBC(0x10b) EMITMBC(0x10d)
1004 return OK;
1005
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001006 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001007 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001008 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1009 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001010 return OK;
1011
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001012 case 'e': case e_grave: case e_acute: case e_circumflex:
1013 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1014 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1015 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1016 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1017 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1018 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001019 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1020 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1021 return OK;
1022
1023 case 'f': CASEMBC(0x1e1f)
1024 EMIT2('f'); EMITMBC(0x1e1f)
1025 return OK;
1026
1027 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001028 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1029 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001030 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1031 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1032 EMITMBC(0x1f5) EMITMBC(0x1e21)
1033 return OK;
1034
1035 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001036 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001037 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1038 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001039 return OK;
1040
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001041 case 'i': case i_grave: case i_acute: case i_circumflex:
1042 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1043 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1044 CASEMBC(0x1ec9)
1045 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1046 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1047 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001048 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1049 EMITMBC(0x1ec9)
1050 return OK;
1051
1052 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1053 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1054 return OK;
1055
1056 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001057 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001058 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1059 EMITMBC(0x1e35)
1060 return OK;
1061
1062 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001063 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001064 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1065 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1066 return OK;
1067
1068 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1069 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001070 return OK;
1071
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001072 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1073 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1074 CASEMBC(0x1e49)
1075 EMIT2('n'); EMIT2(n_virguilla);
1076 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001077 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1078 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001079 return OK;
1080
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001081 case 'o': case o_grave: case o_acute: case o_circumflex:
1082 case o_virguilla: case o_diaeresis: case o_slash:
1083 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1084 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1085 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1086 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1087 EMIT2(o_circumflex); EMIT2(o_virguilla);
1088 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001089 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1090 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1091 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1092 return OK;
1093
1094 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1095 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1096 return OK;
1097
1098 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001099 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001100 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1101 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1102 return OK;
1103
1104 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001105 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001106 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1107 EMITMBC(0x161) EMITMBC(0x1e61)
1108 return OK;
1109
1110 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001111 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001112 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1113 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001114 return OK;
1115
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001116 case 'u': case u_grave: case u_acute: case u_circumflex:
1117 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1118 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1119 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1120 CASEMBC(0x1ee7)
1121 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1122 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1123 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001124 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1125 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1126 EMITMBC(0x1ee7)
1127 return OK;
1128
1129 case 'v': CASEMBC(0x1e7d)
1130 EMIT2('v'); EMITMBC(0x1e7d)
1131 return OK;
1132
1133 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001134 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001135 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1136 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1137 return OK;
1138
1139 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1140 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 return OK;
1142
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001143 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1144 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1145 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1146 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1147 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001148 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1149 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001150 return OK;
1151
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001152 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001153 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001154 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1155 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1156 return OK;
1157
1158 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001159 }
1160 }
1161
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001163 return OK;
1164#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001165#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166}
1167
1168/*
1169 * Code to parse regular expression.
1170 *
1171 * We try to reuse parsing functions in regexp.c to
1172 * minimize surprise and keep the syntax consistent.
1173 */
1174
1175/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001176 * Parse the lowest level.
1177 *
1178 * An atom can be one of a long list of items. Many atoms match one character
1179 * in the text. It is often an ordinary character or a character class.
1180 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1181 * is only for syntax highlighting.
1182 *
1183 * atom ::= ordinary-atom
1184 * or \( pattern \)
1185 * or \%( pattern \)
1186 * or \z( pattern \)
1187 */
1188 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001189nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190{
1191 int c;
1192 int charclass;
1193 int equiclass;
1194 int collclass;
1195 int got_coll_char;
1196 char_u *p;
1197 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001198 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001199 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001200 int emit_range;
1201 int negated;
1202 int result;
1203 int startc = -1;
1204 int endc = -1;
1205 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001206 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001207
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001209 switch (c)
1210 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001211 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001212 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 case Magic('^'):
1215 EMIT(NFA_BOL);
1216 break;
1217
1218 case Magic('$'):
1219 EMIT(NFA_EOL);
1220#if defined(FEAT_SYN_HL) || defined(PROTO)
1221 had_eol = TRUE;
1222#endif
1223 break;
1224
1225 case Magic('<'):
1226 EMIT(NFA_BOW);
1227 break;
1228
1229 case Magic('>'):
1230 EMIT(NFA_EOW);
1231 break;
1232
1233 case Magic('_'):
1234 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001235 if (c == NUL)
1236 EMSG_RET_FAIL(_(e_nul_found));
1237
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 if (c == '^') /* "\_^" is start-of-line */
1239 {
1240 EMIT(NFA_BOL);
1241 break;
1242 }
1243 if (c == '$') /* "\_$" is end-of-line */
1244 {
1245 EMIT(NFA_EOL);
1246#if defined(FEAT_SYN_HL) || defined(PROTO)
1247 had_eol = TRUE;
1248#endif
1249 break;
1250 }
1251
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001252 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253
1254 /* "\_[" is collection plus newline */
1255 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001256 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257
1258 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001259 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260
1261 /*
1262 * Character classes.
1263 */
1264 case Magic('.'):
1265 case Magic('i'):
1266 case Magic('I'):
1267 case Magic('k'):
1268 case Magic('K'):
1269 case Magic('f'):
1270 case Magic('F'):
1271 case Magic('p'):
1272 case Magic('P'):
1273 case Magic('s'):
1274 case Magic('S'):
1275 case Magic('d'):
1276 case Magic('D'):
1277 case Magic('x'):
1278 case Magic('X'):
1279 case Magic('o'):
1280 case Magic('O'):
1281 case Magic('w'):
1282 case Magic('W'):
1283 case Magic('h'):
1284 case Magic('H'):
1285 case Magic('a'):
1286 case Magic('A'):
1287 case Magic('l'):
1288 case Magic('L'):
1289 case Magic('u'):
1290 case Magic('U'):
1291 p = vim_strchr(classchars, no_Magic(c));
1292 if (p == NULL)
1293 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001294 if (extra == NFA_ADD_NL)
1295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001296 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001297 rc_did_emsg = TRUE;
1298 return FAIL;
1299 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001300 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001301 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001302 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001303
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001304 /* When '.' is followed by a composing char ignore the dot, so that
1305 * the composing char is matched here. */
1306 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1307 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001308 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001309 c = getchr();
1310 goto nfa_do_multibyte;
1311 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001313 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001314 {
1315 EMIT(NFA_NEWL);
1316 EMIT(NFA_OR);
1317 regflags |= RF_HASNL;
1318 }
1319 break;
1320
1321 case Magic('n'):
1322 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001323 /* In a string "\n" matches a newline character. */
1324 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001325 else
1326 {
1327 /* In buffer text "\n" matches the end of a line. */
1328 EMIT(NFA_NEWL);
1329 regflags |= RF_HASNL;
1330 }
1331 break;
1332
1333 case Magic('('):
1334 if (nfa_reg(REG_PAREN) == FAIL)
1335 return FAIL; /* cascaded error */
1336 break;
1337
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001338 case Magic('|'):
1339 case Magic('&'):
1340 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001341 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342 return FAIL;
1343
1344 case Magic('='):
1345 case Magic('?'):
1346 case Magic('+'):
1347 case Magic('@'):
1348 case Magic('*'):
1349 case Magic('{'):
1350 /* these should follow an atom, not form an atom */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001352 return FAIL;
1353
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001354 case Magic('~'):
1355 {
1356 char_u *lp;
1357
1358 /* Previous substitute pattern.
1359 * Generated as "\%(pattern\)". */
1360 if (reg_prev_sub == NULL)
1361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001363 return FAIL;
1364 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001365 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001366 {
1367 EMIT(PTR2CHAR(lp));
1368 if (lp != reg_prev_sub)
1369 EMIT(NFA_CONCAT);
1370 }
1371 EMIT(NFA_NOPEN);
1372 break;
1373 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001374
Bram Moolenaar428e9872013-05-30 17:05:39 +02001375 case Magic('1'):
1376 case Magic('2'):
1377 case Magic('3'):
1378 case Magic('4'):
1379 case Magic('5'):
1380 case Magic('6'):
1381 case Magic('7'):
1382 case Magic('8'):
1383 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001384 {
1385 int refnum = no_Magic(c) - '1';
1386
1387 if (!seen_endbrace(refnum + 1))
1388 return FAIL;
1389 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001390 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001391 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001392 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001393
1394 case Magic('z'):
1395 c = no_Magic(getchr());
1396 switch (c)
1397 {
1398 case 's':
1399 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001400 if (re_mult_next("\\zs") == FAIL)
1401 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001402 break;
1403 case 'e':
1404 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001405 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001406 if (re_mult_next("\\ze") == FAIL)
1407 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001408 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001409#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001410 case '1':
1411 case '2':
1412 case '3':
1413 case '4':
1414 case '5':
1415 case '6':
1416 case '7':
1417 case '8':
1418 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001419 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001420 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001421 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001422 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar0270f382018-07-17 05:43:58 +02001423 /* No need to set rex.nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001424 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001425 re_has_z = REX_USE;
1426 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001427 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001428 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001429 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001430 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001431 if (nfa_reg(REG_ZPAREN) == FAIL)
1432 return FAIL; /* cascaded error */
1433 re_has_z = REX_SET;
1434 break;
1435#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001436 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001437 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001438 no_Magic(c));
1439 return FAIL;
1440 }
1441 break;
1442
1443 case Magic('%'):
1444 c = no_Magic(getchr());
1445 switch (c)
1446 {
1447 /* () without a back reference */
1448 case '(':
1449 if (nfa_reg(REG_NPAREN) == FAIL)
1450 return FAIL;
1451 EMIT(NFA_NOPEN);
1452 break;
1453
1454 case 'd': /* %d123 decimal */
1455 case 'o': /* %o123 octal */
1456 case 'x': /* %xab hex 2 */
1457 case 'u': /* %uabcd hex 4 */
1458 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001459 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001460 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001461
Bram Moolenaar47196582013-05-25 22:04:23 +02001462 switch (c)
1463 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001464 case 'd': nr = getdecchrs(); break;
1465 case 'o': nr = getoctchrs(); break;
1466 case 'x': nr = gethexchrs(2); break;
1467 case 'u': nr = gethexchrs(4); break;
1468 case 'U': nr = gethexchrs(8); break;
1469 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001470 }
1471
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001472 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001473 EMSG2_RET_FAIL(
1474 _("E678: Invalid character after %s%%[dxouU]"),
1475 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001476 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001477 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001478 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001479 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001480 break;
1481
1482 /* Catch \%^ and \%$ regardless of where they appear in the
1483 * pattern -- regardless of whether or not it makes sense. */
1484 case '^':
1485 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 break;
1487
1488 case '$':
1489 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001490 break;
1491
1492 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001493 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 break;
1495
1496 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001497 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 break;
1499
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001500 case 'C':
1501 EMIT(NFA_ANY_COMPOSING);
1502 break;
1503
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001505 {
1506 int n;
1507
1508 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001509 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001510 {
1511 if (c == NUL)
1512 EMSG2_RET_FAIL(_(e_missing_sb),
1513 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001514 /* recursive call! */
1515 if (nfa_regatom() == FAIL)
1516 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001517 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001518 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001519 if (n == 0)
1520 EMSG2_RET_FAIL(_(e_empty_sb),
1521 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001522 EMIT(NFA_OPT_CHARS);
1523 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001524
1525 /* Emit as "\%(\%[abc]\)" to be able to handle
1526 * "\%[abc]*" which would cause the empty string to be
1527 * matched an unlimited number of times. NFA_NOPEN is
1528 * added only once at a position, while NFA_SPLIT is
1529 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001530 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001531 * a lot. */
1532 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001533 break;
1534 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001535
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001536 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001537 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001538 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001539 int cmp = c;
1540
1541 if (c == '<' || c == '>')
1542 c = getchr();
1543 while (VIM_ISDIGIT(c))
1544 {
1545 n = n * 10 + (c - '0');
1546 c = getchr();
1547 }
1548 if (c == 'l' || c == 'c' || c == 'v')
1549 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001550 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001551 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001552 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001553 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001554 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001555 if (save_prev_at_start)
1556 at_start = TRUE;
1557 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001559 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001560 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001561 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001562 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001563 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001564 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001565 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001566#if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
1567 if (n > INT_MAX)
1568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001569 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001570 return FAIL;
1571 }
1572#endif
1573 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001574 break;
1575 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001576 else if (c == '\'' && n == 0)
1577 {
1578 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001579 EMIT(cmp == '<' ? NFA_MARK_LT :
1580 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001581 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001582 break;
1583 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001584 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001585 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001586 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001587 return FAIL;
1588 }
1589 break;
1590
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001591 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001592collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001594 * [abc] uses NFA_START_COLL - NFA_END_COLL
1595 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1596 * Each character is produced as a regular state, using
1597 * NFA_CONCAT to bind them together.
1598 * Besides normal characters there can be:
1599 * - character classes NFA_CLASS_*
1600 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001601 */
1602
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001603 p = regparse;
1604 endp = skip_anyof(p);
1605 if (*endp == ']')
1606 {
1607 /*
1608 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001609 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 * and perform the necessary substitutions in the NFA.
1611 */
1612 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001613 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001614 if (result != FAIL)
1615 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001616 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001618 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 EMIT(NFA_NEWL);
1620 EMIT(NFA_OR);
1621 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001622 else
1623 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001624 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001625 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 return OK;
1627 }
1628 /*
1629 * Failed to recognize a character class. Use the simple
1630 * version that turns [abc] into 'a' OR 'b' OR 'c'
1631 */
1632 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001633 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001634 if (*regparse == '^') /* negated range */
1635 {
1636 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001637 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001638 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001640 else
1641 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 if (*regparse == '-')
1643 {
1644 startc = '-';
1645 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001646 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001647 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001648 }
1649 /* Emit the OR branches for each character in the [] */
1650 emit_range = FALSE;
1651 while (regparse < endp)
1652 {
1653 oldstartc = startc;
1654 startc = -1;
1655 got_coll_char = FALSE;
1656 if (*regparse == '[')
1657 {
1658 /* Check for [: :], [= =], [. .] */
1659 equiclass = collclass = 0;
1660 charclass = get_char_class(&regparse);
1661 if (charclass == CLASS_NONE)
1662 {
1663 equiclass = get_equi_class(&regparse);
1664 if (equiclass == 0)
1665 collclass = get_coll_element(&regparse);
1666 }
1667
1668 /* Character class like [:alpha:] */
1669 if (charclass != CLASS_NONE)
1670 {
1671 switch (charclass)
1672 {
1673 case CLASS_ALNUM:
1674 EMIT(NFA_CLASS_ALNUM);
1675 break;
1676 case CLASS_ALPHA:
1677 EMIT(NFA_CLASS_ALPHA);
1678 break;
1679 case CLASS_BLANK:
1680 EMIT(NFA_CLASS_BLANK);
1681 break;
1682 case CLASS_CNTRL:
1683 EMIT(NFA_CLASS_CNTRL);
1684 break;
1685 case CLASS_DIGIT:
1686 EMIT(NFA_CLASS_DIGIT);
1687 break;
1688 case CLASS_GRAPH:
1689 EMIT(NFA_CLASS_GRAPH);
1690 break;
1691 case CLASS_LOWER:
1692 EMIT(NFA_CLASS_LOWER);
1693 break;
1694 case CLASS_PRINT:
1695 EMIT(NFA_CLASS_PRINT);
1696 break;
1697 case CLASS_PUNCT:
1698 EMIT(NFA_CLASS_PUNCT);
1699 break;
1700 case CLASS_SPACE:
1701 EMIT(NFA_CLASS_SPACE);
1702 break;
1703 case CLASS_UPPER:
1704 EMIT(NFA_CLASS_UPPER);
1705 break;
1706 case CLASS_XDIGIT:
1707 EMIT(NFA_CLASS_XDIGIT);
1708 break;
1709 case CLASS_TAB:
1710 EMIT(NFA_CLASS_TAB);
1711 break;
1712 case CLASS_RETURN:
1713 EMIT(NFA_CLASS_RETURN);
1714 break;
1715 case CLASS_BACKSPACE:
1716 EMIT(NFA_CLASS_BACKSPACE);
1717 break;
1718 case CLASS_ESCAPE:
1719 EMIT(NFA_CLASS_ESCAPE);
1720 break;
1721 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001722 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001723 continue;
1724 }
1725 /* Try equivalence class [=a=] and the like */
1726 if (equiclass != 0)
1727 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001728 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001729 if (result == FAIL)
1730 {
1731 /* should never happen */
1732 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1733 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 continue;
1735 }
1736 /* Try collating class like [. .] */
1737 if (collclass != 0)
1738 {
1739 startc = collclass; /* allow [.a.]-x as a range */
1740 /* Will emit the proper atom at the end of the
1741 * while loop. */
1742 }
1743 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001744 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1745 * start character. */
1746 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 {
1748 emit_range = TRUE;
1749 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001750 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001751 continue; /* reading the end of the range */
1752 }
1753
1754 /* Now handle simple and escaped characters.
1755 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1756 * accepts "\t", "\e", etc., but only when the 'l' flag in
1757 * 'cpoptions' is not included.
1758 * Posix doesn't recognize backslash at all.
1759 */
1760 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001761 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001762 && regparse + 1 <= endp
1763 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001764 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 && vim_strchr(REGEXP_ABBR, regparse[1])
1766 != NULL)
1767 )
1768 )
1769 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001770 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001772 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 startc = reg_string ? NL : NFA_NEWL;
1774 else
1775 if (*regparse == 'd'
1776 || *regparse == 'o'
1777 || *regparse == 'x'
1778 || *regparse == 'u'
1779 || *regparse == 'U'
1780 )
1781 {
1782 /* TODO(RE) This needs more testing */
1783 startc = coll_get_char();
1784 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001785 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 }
1787 else
1788 {
1789 /* \r,\t,\e,\b */
1790 startc = backslash_trans(*regparse);
1791 }
1792 }
1793
1794 /* Normal printable char */
1795 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001796 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797
1798 /* Previous char was '-', so this char is end of range. */
1799 if (emit_range)
1800 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001801 endc = startc;
1802 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001804 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001805
1806 if (endc > startc + 2)
1807 {
1808 /* Emit a range instead of the sequence of
1809 * individual characters. */
1810 if (startc == 0)
1811 /* \x00 is translated to \x0a, start at \x01. */
1812 EMIT(1);
1813 else
1814 --post_ptr; /* remove NFA_CONCAT */
1815 EMIT(endc);
1816 EMIT(NFA_RANGE);
1817 EMIT(NFA_CONCAT);
1818 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001819 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820 || (*mb_char2len)(endc) > 1))
1821 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001822 /* Emit the characters in the range.
1823 * "startc" was already emitted, so skip it.
1824 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 for (c = startc + 1; c <= endc; c++)
1826 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001827 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001828 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830 }
1831 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832 {
1833#ifdef EBCDIC
1834 int alpha_only = FALSE;
1835
1836 /* for alphabetical range skip the gaps
1837 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1838 if (isalpha(startc) && isalpha(endc))
1839 alpha_only = TRUE;
1840#endif
1841 /* Emit the range. "startc" was already emitted, so
1842 * skip it. */
1843 for (c = startc + 1; c <= endc; c++)
1844#ifdef EBCDIC
1845 if (!alpha_only || isalpha(startc))
1846#endif
1847 {
1848 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001849 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001850 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001852 emit_range = FALSE;
1853 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001854 }
1855 else
1856 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001857 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859 * Normally, simply emit startc. But if we get char
1860 * code=0 from a collating char, then replace it with
1861 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001863 * the backtracking engine. */
1864 if (startc == NFA_NEWL)
1865 {
1866 /* Line break can't be matched as part of the
1867 * collection, add an OR below. But not for negated
1868 * range. */
1869 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001870 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001871 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001873 {
1874 if (got_coll_char == TRUE && startc == 0)
1875 EMIT(0x0a);
1876 else
1877 EMIT(startc);
1878 EMIT(NFA_CONCAT);
1879 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 }
1881
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001882 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001883 } /* while (p < endp) */
1884
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001885 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886 if (*regparse == '-') /* if last, '-' is just a char */
1887 {
1888 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001889 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001891
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892 /* skip the trailing ] */
1893 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001894 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001895
1896 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001898 EMIT(NFA_END_NEG_COLL);
1899 else
1900 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001901
1902 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001903 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001904 {
1905 EMIT(reg_string ? NL : NFA_NEWL);
1906 EMIT(NFA_OR);
1907 }
1908
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001910 } /* if exists closing ] */
1911
1912 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001914 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001916 default:
1917 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001918 int plen;
1919
1920nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001921 /* plen is length of current char with composing chars */
1922 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001923 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001924 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001926 int i = 0;
1927
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001928 /* A base character plus composing characters, or just one
1929 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001930 * This requires creating a separate atom as if enclosing
1931 * the characters in (), where NFA_COMPOSING is the ( and
1932 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 * building the postfix form, not the NFA itself;
1934 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001935 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001936 for (;;)
1937 {
1938 EMIT(c);
1939 if (i > 0)
1940 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001941 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001942 break;
1943 c = utf_ptr2char(old_regparse + i);
1944 }
1945 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946 regparse = old_regparse + plen;
1947 }
1948 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001949 {
1950 c = no_Magic(c);
1951 EMIT(c);
1952 }
1953 return OK;
1954 }
1955 }
1956
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001957 return OK;
1958}
1959
1960/*
1961 * Parse something followed by possible [*+=].
1962 *
1963 * A piece is an atom, possibly followed by a multi, an indication of how many
1964 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1965 * characters: "", "a", "aa", etc.
1966 *
1967 * piece ::= atom
1968 * or atom multi
1969 */
1970 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001971nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972{
1973 int i;
1974 int op;
1975 int ret;
1976 long minval, maxval;
1977 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001978 parse_state_T old_state;
1979 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001980 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001981 int old_post_pos;
1982 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983 int quest;
1984
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001985 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1986 * next. */
1987 save_parse_state(&old_state);
1988
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001990 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001991
1992 ret = nfa_regatom();
1993 if (ret == FAIL)
1994 return FAIL; /* cascaded error */
1995
1996 op = peekchr();
1997 if (re_multi_type(op) == NOT_MULTI)
1998 return OK;
1999
2000 skipchr();
2001 switch (op)
2002 {
2003 case Magic('*'):
2004 EMIT(NFA_STAR);
2005 break;
2006
2007 case Magic('+'):
2008 /*
2009 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2010 * first and only submatch would be "aaa". But the backtracking
2011 * engine interprets the plus as "try matching one more time", and
2012 * a* matches a second time at the end of the input, the empty
2013 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002014 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002015 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002016 * In order to be consistent with the old engine, we replace
2017 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002019 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002020 curchr = -1;
2021 if (nfa_regatom() == FAIL)
2022 return FAIL;
2023 EMIT(NFA_STAR);
2024 EMIT(NFA_CONCAT);
2025 skipchr(); /* skip the \+ */
2026 break;
2027
2028 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002029 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002031 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002032 switch(op)
2033 {
2034 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002035 /* \@= */
2036 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002038 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002039 /* \@! */
2040 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002041 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002043 op = no_Magic(getchr());
2044 if (op == '=')
2045 /* \@<= */
2046 i = NFA_PREV_ATOM_JUST_BEFORE;
2047 else if (op == '!')
2048 /* \@<! */
2049 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2050 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002051 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002052 /* \@> */
2053 i = NFA_PREV_ATOM_LIKE_PATTERN;
2054 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002055 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002056 if (i == 0)
2057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002058 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002059 return FAIL;
2060 }
2061 EMIT(i);
2062 if (i == NFA_PREV_ATOM_JUST_BEFORE
2063 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2064 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 break;
2066
2067 case Magic('?'):
2068 case Magic('='):
2069 EMIT(NFA_QUEST);
2070 break;
2071
2072 case Magic('{'):
2073 /* a{2,5} will expand to 'aaa?a?a?'
2074 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2075 * version of '?'
2076 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2077 * parenthesis have the same id
2078 */
2079
2080 greedy = TRUE;
2081 c2 = peekchr();
2082 if (c2 == '-' || c2 == Magic('-'))
2083 {
2084 skipchr();
2085 greedy = FALSE;
2086 }
2087 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002089
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2091 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002092 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002093 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002094 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002095 /* \{}, \{0,} */
2096 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002097 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002098 /* \{-}, \{-0,} */
2099 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100 break;
2101 }
2102
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002103 /* Special case: x{0} or x{-0} */
2104 if (maxval == 0)
2105 {
2106 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002107 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002108 /* NFA_EMPTY is 0-length and works everywhere */
2109 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002110 return OK;
2111 }
2112
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002113 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002114 * maximum is much larger than the minimum and when the maximum is
2115 * large. Bail out if we can use the other engine. */
2116 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002117 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002118 return FAIL;
2119
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002120 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002121 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002122 /* Save parse state after the repeated atom and the \{} */
2123 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002124
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002125 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2126 for (i = 0; i < maxval; i++)
2127 {
2128 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002129 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002130 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002131 if (nfa_regatom() == FAIL)
2132 return FAIL;
2133 /* after "minval" times, atoms are optional */
2134 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002135 {
2136 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002137 {
2138 if (greedy)
2139 EMIT(NFA_STAR);
2140 else
2141 EMIT(NFA_STAR_NONGREEDY);
2142 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002143 else
2144 EMIT(quest);
2145 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002146 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002148 if (i + 1 > minval && maxval == MAX_LIMIT)
2149 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 }
2151
2152 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002153 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002154 curchr = -1;
2155
2156 break;
2157
2158
2159 default:
2160 break;
2161 } /* end switch */
2162
2163 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002165 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166
2167 return OK;
2168}
2169
2170/*
2171 * Parse one or more pieces, concatenated. It matches a match for the
2172 * first piece, followed by a match for the second piece, etc. Example:
2173 * "f[0-9]b", first matches "f", then a digit and then "b".
2174 *
2175 * concat ::= piece
2176 * or piece piece
2177 * or piece piece piece
2178 * etc.
2179 */
2180 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002181nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182{
2183 int cont = TRUE;
2184 int first = TRUE;
2185
2186 while (cont)
2187 {
2188 switch (peekchr())
2189 {
2190 case NUL:
2191 case Magic('|'):
2192 case Magic('&'):
2193 case Magic(')'):
2194 cont = FALSE;
2195 break;
2196
2197 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002199 skipchr_keepstart();
2200 break;
2201 case Magic('c'):
2202 regflags |= RF_ICASE;
2203 skipchr_keepstart();
2204 break;
2205 case Magic('C'):
2206 regflags |= RF_NOICASE;
2207 skipchr_keepstart();
2208 break;
2209 case Magic('v'):
2210 reg_magic = MAGIC_ALL;
2211 skipchr_keepstart();
2212 curchr = -1;
2213 break;
2214 case Magic('m'):
2215 reg_magic = MAGIC_ON;
2216 skipchr_keepstart();
2217 curchr = -1;
2218 break;
2219 case Magic('M'):
2220 reg_magic = MAGIC_OFF;
2221 skipchr_keepstart();
2222 curchr = -1;
2223 break;
2224 case Magic('V'):
2225 reg_magic = MAGIC_NONE;
2226 skipchr_keepstart();
2227 curchr = -1;
2228 break;
2229
2230 default:
2231 if (nfa_regpiece() == FAIL)
2232 return FAIL;
2233 if (first == FALSE)
2234 EMIT(NFA_CONCAT);
2235 else
2236 first = FALSE;
2237 break;
2238 }
2239 }
2240
2241 return OK;
2242}
2243
2244/*
2245 * Parse a branch, one or more concats, separated by "\&". It matches the
2246 * last concat, but only if all the preceding concats also match at the same
2247 * position. Examples:
2248 * "foobeep\&..." matches "foo" in "foobeep".
2249 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2250 *
2251 * branch ::= concat
2252 * or concat \& concat
2253 * or concat \& concat \& concat
2254 * etc.
2255 */
2256 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002257nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002258{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002259 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002260
Bram Moolenaar16299b52013-05-30 18:45:23 +02002261 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002262
2263 /* First branch, possibly the only one */
2264 if (nfa_regconcat() == FAIL)
2265 return FAIL;
2266
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002267 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002268 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002269 {
2270 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002271 /* if concat is empty do emit a node */
2272 if (old_post_pos == (int)(post_ptr - post_start))
2273 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274 EMIT(NFA_NOPEN);
2275 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002276 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277 if (nfa_regconcat() == FAIL)
2278 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002279 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002280 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002281 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283 }
2284
Bram Moolenaar699c1202013-09-25 16:41:54 +02002285 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002286 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002287 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288
2289 return OK;
2290}
2291
2292/*
2293 * Parse a pattern, one or more branches, separated by "\|". It matches
2294 * anything that matches one of the branches. Example: "foo\|beep" matches
2295 * "foo" and matches "beep". If more than one branch matches, the first one
2296 * is used.
2297 *
2298 * pattern ::= branch
2299 * or branch \| branch
2300 * or branch \| branch \| branch
2301 * etc.
2302 */
2303 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002304nfa_reg(
2305 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002306{
2307 int parno = 0;
2308
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 if (paren == REG_PAREN)
2310 {
2311 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002312 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002313 parno = regnpar++;
2314 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002315#ifdef FEAT_SYN_HL
2316 else if (paren == REG_ZPAREN)
2317 {
2318 /* Make a ZOPEN node. */
2319 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002320 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002321 parno = regnzpar++;
2322 }
2323#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324
2325 if (nfa_regbranch() == FAIL)
2326 return FAIL; /* cascaded error */
2327
2328 while (peekchr() == Magic('|'))
2329 {
2330 skipchr();
2331 if (nfa_regbranch() == FAIL)
2332 return FAIL; /* cascaded error */
2333 EMIT(NFA_OR);
2334 }
2335
2336 /* Check for proper termination. */
2337 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2338 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 if (paren == REG_NPAREN)
2340 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2341 else
2342 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2343 }
2344 else if (paren == REG_NOPAREN && peekchr() != NUL)
2345 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346 if (peekchr() == Magic(')'))
2347 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2348 else
2349 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2350 }
2351 /*
2352 * Here we set the flag allowing back references to this set of
2353 * parentheses.
2354 */
2355 if (paren == REG_PAREN)
2356 {
2357 had_endbrace[parno] = TRUE; /* have seen the close paren */
2358 EMIT(NFA_MOPEN + parno);
2359 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002360#ifdef FEAT_SYN_HL
2361 else if (paren == REG_ZPAREN)
2362 EMIT(NFA_ZOPEN + parno);
2363#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364
2365 return OK;
2366}
2367
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002368#ifdef DEBUG
2369static char_u code[50];
2370
2371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002372nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002373{
2374 int addnl = FALSE;
2375
2376 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2377 {
2378 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002379 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002380 }
2381
2382 STRCPY(code, "");
2383 switch (c)
2384 {
2385 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2386 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2387 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2388 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2389 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2390 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2391
Bram Moolenaar5714b802013-05-28 22:03:20 +02002392 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2393 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2394 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2395 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2396 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2397 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2398 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2399 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2400 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002401#ifdef FEAT_SYN_HL
2402 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2403 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2404 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2405 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2406 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2407 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2408 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2409 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2410 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2411#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002412 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002414 case NFA_PREV_ATOM_NO_WIDTH:
2415 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002416 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2417 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002418 case NFA_PREV_ATOM_JUST_BEFORE:
2419 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2420 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2421 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002422 case NFA_PREV_ATOM_LIKE_PATTERN:
2423 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2424
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002425 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2426 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002427 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002428 case NFA_START_INVISIBLE_FIRST:
2429 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002430 case NFA_START_INVISIBLE_NEG:
2431 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002432 case NFA_START_INVISIBLE_NEG_FIRST:
2433 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002434 case NFA_START_INVISIBLE_BEFORE:
2435 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002436 case NFA_START_INVISIBLE_BEFORE_FIRST:
2437 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002438 case NFA_START_INVISIBLE_BEFORE_NEG:
2439 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002440 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2441 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002442 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002444 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002445 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002447 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2448 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002449 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002450
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002451 case NFA_MOPEN:
2452 case NFA_MOPEN1:
2453 case NFA_MOPEN2:
2454 case NFA_MOPEN3:
2455 case NFA_MOPEN4:
2456 case NFA_MOPEN5:
2457 case NFA_MOPEN6:
2458 case NFA_MOPEN7:
2459 case NFA_MOPEN8:
2460 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461 STRCPY(code, "NFA_MOPEN(x)");
2462 code[10] = c - NFA_MOPEN + '0';
2463 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002464 case NFA_MCLOSE:
2465 case NFA_MCLOSE1:
2466 case NFA_MCLOSE2:
2467 case NFA_MCLOSE3:
2468 case NFA_MCLOSE4:
2469 case NFA_MCLOSE5:
2470 case NFA_MCLOSE6:
2471 case NFA_MCLOSE7:
2472 case NFA_MCLOSE8:
2473 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002474 STRCPY(code, "NFA_MCLOSE(x)");
2475 code[11] = c - NFA_MCLOSE + '0';
2476 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002477#ifdef FEAT_SYN_HL
2478 case NFA_ZOPEN:
2479 case NFA_ZOPEN1:
2480 case NFA_ZOPEN2:
2481 case NFA_ZOPEN3:
2482 case NFA_ZOPEN4:
2483 case NFA_ZOPEN5:
2484 case NFA_ZOPEN6:
2485 case NFA_ZOPEN7:
2486 case NFA_ZOPEN8:
2487 case NFA_ZOPEN9:
2488 STRCPY(code, "NFA_ZOPEN(x)");
2489 code[10] = c - NFA_ZOPEN + '0';
2490 break;
2491 case NFA_ZCLOSE:
2492 case NFA_ZCLOSE1:
2493 case NFA_ZCLOSE2:
2494 case NFA_ZCLOSE3:
2495 case NFA_ZCLOSE4:
2496 case NFA_ZCLOSE5:
2497 case NFA_ZCLOSE6:
2498 case NFA_ZCLOSE7:
2499 case NFA_ZCLOSE8:
2500 case NFA_ZCLOSE9:
2501 STRCPY(code, "NFA_ZCLOSE(x)");
2502 code[11] = c - NFA_ZCLOSE + '0';
2503 break;
2504#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002505 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2506 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2507 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2508 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002509 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2510 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002511 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2512 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2513 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2514 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2515 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2516 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2517 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2518 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2519 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2520 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2521 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2522 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2523 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2524 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002525 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002526
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002527 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002528 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2529 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2530 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002531 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002532 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002533
2534 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2535 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2536 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2537 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2538 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2539 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2540 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2541
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2543 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2544 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2545 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2546 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2547 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2548 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2549 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2550 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2551 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2552 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2553 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2554 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2555 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2556 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2557 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2558
2559 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2560 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2561 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2562 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2563 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2564 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2565 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2566 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2567 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2568 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2569 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2570 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2571 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2572 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2573 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2574 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2575 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2576 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2577 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2578 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2579 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2580 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2581 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2582 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2583 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2584 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2585 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002586 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2587 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2588 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2589 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002590
2591 default:
2592 STRCPY(code, "CHAR(x)");
2593 code[5] = c;
2594 }
2595
2596 if (addnl == TRUE)
2597 STRCAT(code, " + NEWLINE ");
2598
2599}
2600
2601#ifdef ENABLE_LOG
2602static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002603static char_u e_log_open_failed[] = N_("Could not open temporary log file for writing, displaying on stderr... ");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002604
2605/*
2606 * Print the postfix notation of the current regexp.
2607 */
2608 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002609nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002610{
2611 int *p;
2612 FILE *f;
2613
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002614 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002615 if (f != NULL)
2616 {
2617 fprintf(f, "\n-------------------------\n");
2618 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002619 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002620 else if (retval == OK)
2621 fprintf(f, ">>> NFA engine succeeded !\n");
2622 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002623 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002624 {
2625 nfa_set_code(*p);
2626 fprintf(f, "%s, ", code);
2627 }
2628 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002629 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002630 fprintf(f, "%d ", *p);
2631 fprintf(f, "\n\n");
2632 fclose(f);
2633 }
2634}
2635
2636/*
2637 * Print the NFA starting with a root node "state".
2638 */
2639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002640nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002642 garray_T indent;
2643
2644 ga_init2(&indent, 1, 64);
2645 ga_append(&indent, '\0');
2646 nfa_print_state2(debugf, state, &indent);
2647 ga_clear(&indent);
2648}
2649
2650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002651nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002652{
2653 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654
2655 if (state == NULL)
2656 return;
2657
2658 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002659
2660 /* Output indent */
2661 p = (char_u *)indent->ga_data;
2662 if (indent->ga_len >= 3)
2663 {
2664 int last = indent->ga_len - 3;
2665 char_u save[2];
2666
2667 STRNCPY(save, &p[last], 2);
2668 STRNCPY(&p[last], "+-", 2);
2669 fprintf(debugf, " %s", p);
2670 STRNCPY(&p[last], save, 2);
2671 }
2672 else
2673 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674
2675 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002676 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002677 code,
2678 state->c,
2679 abs(state->id),
2680 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002681 if (state->id < 0)
2682 return;
2683
2684 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002685
2686 /* grow indent for state->out */
2687 indent->ga_len -= 1;
2688 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002689 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002690 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002691 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002692 ga_append(indent, '\0');
2693
2694 nfa_print_state2(debugf, state->out, indent);
2695
2696 /* replace last part of indent for state->out1 */
2697 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002698 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002699 ga_append(indent, '\0');
2700
2701 nfa_print_state2(debugf, state->out1, indent);
2702
2703 /* shrink indent */
2704 indent->ga_len -= 3;
2705 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002706}
2707
2708/*
2709 * Print the NFA state machine.
2710 */
2711 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002712nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002713{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002714 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715
2716 if (debugf != NULL)
2717 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002718 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002719
Bram Moolenaar473de612013-06-08 18:19:48 +02002720 if (prog->reganch)
2721 fprintf(debugf, "reganch: %d\n", prog->reganch);
2722 if (prog->regstart != NUL)
2723 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2724 prog->regstart, prog->regstart);
2725 if (prog->match_text != NULL)
2726 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002727
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728 fclose(debugf);
2729 }
2730}
2731#endif /* ENABLE_LOG */
2732#endif /* DEBUG */
2733
2734/*
2735 * Parse r.e. @expr and convert it into postfix form.
2736 * Return the postfix string on success, NULL otherwise.
2737 */
2738 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002739re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002740{
2741 if (nfa_reg(REG_NOPAREN) == FAIL)
2742 return NULL;
2743 EMIT(NFA_MOPEN);
2744 return post_start;
2745}
2746
2747/* NB. Some of the code below is inspired by Russ's. */
2748
2749/*
2750 * Represents an NFA state plus zero or one or two arrows exiting.
2751 * if c == MATCH, no arrows out; matching state.
2752 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2753 * If c < 256, labeled arrow with character c to out.
2754 */
2755
2756static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2757
2758/*
2759 * Allocate and initialize nfa_state_T.
2760 */
2761 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002762alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002763{
2764 nfa_state_T *s;
2765
2766 if (istate >= nstate)
2767 return NULL;
2768
2769 s = &state_ptr[istate++];
2770
2771 s->c = c;
2772 s->out = out;
2773 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002774 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002775
2776 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002777 s->lastlist[0] = 0;
2778 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002779
2780 return s;
2781}
2782
2783/*
2784 * A partially built NFA without the matching state filled in.
2785 * Frag_T.start points at the start state.
2786 * Frag_T.out is a list of places that need to be set to the
2787 * next state for this fragment.
2788 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002789
2790/* Since the out pointers in the list are always
2791 * uninitialized, we use the pointers themselves
2792 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002793typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002794union Ptrlist
2795{
2796 Ptrlist *next;
2797 nfa_state_T *s;
2798};
2799
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800struct Frag
2801{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002802 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002803 Ptrlist *out;
2804};
2805typedef struct Frag Frag_T;
2806
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002807/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002808 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002809 */
2810 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002811frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002813 Frag_T n;
2814
2815 n.start = start;
2816 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817 return n;
2818}
2819
2820/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821 * Create singleton list containing just outp.
2822 */
2823 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002824list1(
2825 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826{
2827 Ptrlist *l;
2828
2829 l = (Ptrlist *)outp;
2830 l->next = NULL;
2831 return l;
2832}
2833
2834/*
2835 * Patch the list of states at out to point to start.
2836 */
2837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002838patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839{
2840 Ptrlist *next;
2841
2842 for (; l; l = next)
2843 {
2844 next = l->next;
2845 l->s = s;
2846 }
2847}
2848
2849
2850/*
2851 * Join the two lists l1 and l2, returning the combination.
2852 */
2853 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002854append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855{
2856 Ptrlist *oldl1;
2857
2858 oldl1 = l1;
2859 while (l1->next)
2860 l1 = l1->next;
2861 l1->next = l2;
2862 return oldl1;
2863}
2864
2865/*
2866 * Stack used for transforming postfix form into NFA.
2867 */
2868static Frag_T empty;
2869
2870 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002871st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002872{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002873#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874 FILE *df;
2875 int *p2;
2876
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002877 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002878 if (df)
2879 {
2880 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002881# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002883# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002885# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002886 for (p2 = postfix; p2 < end; p2++)
2887 {
2888 nfa_set_code(*p2);
2889 fprintf(df, "%s, ", code);
2890 }
2891 nfa_set_code(*p);
2892 fprintf(df, "\nCurrent position is: ");
2893 for (p2 = postfix; p2 <= p; p2 ++)
2894 {
2895 nfa_set_code(*p2);
2896 fprintf(df, "%s, ", code);
2897 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002898# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899 for (p2 = postfix; p2 < end; p2++)
2900 {
2901 fprintf(df, "%d, ", *p2);
2902 }
2903 fprintf(df, "\nCurrent position is: ");
2904 for (p2 = postfix; p2 <= p; p2 ++)
2905 {
2906 fprintf(df, "%d, ", *p2);
2907 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002908# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002909 fprintf(df, "\n--------------------------\n");
2910 fclose(df);
2911 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002912#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002913 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914}
2915
2916/*
2917 * Push an item onto the stack.
2918 */
2919 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002920st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921{
2922 Frag_T *stackp = *p;
2923
2924 if (stackp >= stack_end)
2925 return;
2926 *stackp = s;
2927 *p = *p + 1;
2928}
2929
2930/*
2931 * Pop an item from the stack.
2932 */
2933 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002934st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002935{
2936 Frag_T *stackp;
2937
2938 *p = *p - 1;
2939 stackp = *p;
2940 if (stackp < stack)
2941 return empty;
2942 return **p;
2943}
2944
2945/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002946 * Estimate the maximum byte length of anything matching "state".
2947 * When unknown or unlimited return -1.
2948 */
2949 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002950nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002951{
2952 int l, r;
2953 nfa_state_T *state = startstate;
2954 int len = 0;
2955
2956 /* detect looping in a NFA_SPLIT */
2957 if (depth > 4)
2958 return -1;
2959
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002960 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002961 {
2962 switch (state->c)
2963 {
2964 case NFA_END_INVISIBLE:
2965 case NFA_END_INVISIBLE_NEG:
2966 /* the end, return what we have */
2967 return len;
2968
2969 case NFA_SPLIT:
2970 /* two alternatives, use the maximum */
2971 l = nfa_max_width(state->out, depth + 1);
2972 r = nfa_max_width(state->out1, depth + 1);
2973 if (l < 0 || r < 0)
2974 return -1;
2975 return len + (l > r ? l : r);
2976
2977 case NFA_ANY:
2978 case NFA_START_COLL:
2979 case NFA_START_NEG_COLL:
2980 /* matches some character, including composing chars */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002981 if (enc_utf8)
2982 len += MB_MAXBYTES;
2983 else if (has_mbyte)
2984 len += 2;
2985 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002986 ++len;
2987 if (state->c != NFA_ANY)
2988 {
2989 /* skip over the characters */
2990 state = state->out1->out;
2991 continue;
2992 }
2993 break;
2994
2995 case NFA_DIGIT:
2996 case NFA_WHITE:
2997 case NFA_HEX:
2998 case NFA_OCTAL:
2999 /* ascii */
3000 ++len;
3001 break;
3002
3003 case NFA_IDENT:
3004 case NFA_SIDENT:
3005 case NFA_KWORD:
3006 case NFA_SKWORD:
3007 case NFA_FNAME:
3008 case NFA_SFNAME:
3009 case NFA_PRINT:
3010 case NFA_SPRINT:
3011 case NFA_NWHITE:
3012 case NFA_NDIGIT:
3013 case NFA_NHEX:
3014 case NFA_NOCTAL:
3015 case NFA_WORD:
3016 case NFA_NWORD:
3017 case NFA_HEAD:
3018 case NFA_NHEAD:
3019 case NFA_ALPHA:
3020 case NFA_NALPHA:
3021 case NFA_LOWER:
3022 case NFA_NLOWER:
3023 case NFA_UPPER:
3024 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003025 case NFA_LOWER_IC:
3026 case NFA_NLOWER_IC:
3027 case NFA_UPPER_IC:
3028 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003029 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003030 /* possibly non-ascii */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003031 if (has_mbyte)
3032 len += 3;
3033 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003034 ++len;
3035 break;
3036
3037 case NFA_START_INVISIBLE:
3038 case NFA_START_INVISIBLE_NEG:
3039 case NFA_START_INVISIBLE_BEFORE:
3040 case NFA_START_INVISIBLE_BEFORE_NEG:
3041 /* zero-width, out1 points to the END state */
3042 state = state->out1->out;
3043 continue;
3044
3045 case NFA_BACKREF1:
3046 case NFA_BACKREF2:
3047 case NFA_BACKREF3:
3048 case NFA_BACKREF4:
3049 case NFA_BACKREF5:
3050 case NFA_BACKREF6:
3051 case NFA_BACKREF7:
3052 case NFA_BACKREF8:
3053 case NFA_BACKREF9:
3054#ifdef FEAT_SYN_HL
3055 case NFA_ZREF1:
3056 case NFA_ZREF2:
3057 case NFA_ZREF3:
3058 case NFA_ZREF4:
3059 case NFA_ZREF5:
3060 case NFA_ZREF6:
3061 case NFA_ZREF7:
3062 case NFA_ZREF8:
3063 case NFA_ZREF9:
3064#endif
3065 case NFA_NEWL:
3066 case NFA_SKIP:
3067 /* unknown width */
3068 return -1;
3069
3070 case NFA_BOL:
3071 case NFA_EOL:
3072 case NFA_BOF:
3073 case NFA_EOF:
3074 case NFA_BOW:
3075 case NFA_EOW:
3076 case NFA_MOPEN:
3077 case NFA_MOPEN1:
3078 case NFA_MOPEN2:
3079 case NFA_MOPEN3:
3080 case NFA_MOPEN4:
3081 case NFA_MOPEN5:
3082 case NFA_MOPEN6:
3083 case NFA_MOPEN7:
3084 case NFA_MOPEN8:
3085 case NFA_MOPEN9:
3086#ifdef FEAT_SYN_HL
3087 case NFA_ZOPEN:
3088 case NFA_ZOPEN1:
3089 case NFA_ZOPEN2:
3090 case NFA_ZOPEN3:
3091 case NFA_ZOPEN4:
3092 case NFA_ZOPEN5:
3093 case NFA_ZOPEN6:
3094 case NFA_ZOPEN7:
3095 case NFA_ZOPEN8:
3096 case NFA_ZOPEN9:
3097 case NFA_ZCLOSE:
3098 case NFA_ZCLOSE1:
3099 case NFA_ZCLOSE2:
3100 case NFA_ZCLOSE3:
3101 case NFA_ZCLOSE4:
3102 case NFA_ZCLOSE5:
3103 case NFA_ZCLOSE6:
3104 case NFA_ZCLOSE7:
3105 case NFA_ZCLOSE8:
3106 case NFA_ZCLOSE9:
3107#endif
3108 case NFA_MCLOSE:
3109 case NFA_MCLOSE1:
3110 case NFA_MCLOSE2:
3111 case NFA_MCLOSE3:
3112 case NFA_MCLOSE4:
3113 case NFA_MCLOSE5:
3114 case NFA_MCLOSE6:
3115 case NFA_MCLOSE7:
3116 case NFA_MCLOSE8:
3117 case NFA_MCLOSE9:
3118 case NFA_NOPEN:
3119 case NFA_NCLOSE:
3120
3121 case NFA_LNUM_GT:
3122 case NFA_LNUM_LT:
3123 case NFA_COL_GT:
3124 case NFA_COL_LT:
3125 case NFA_VCOL_GT:
3126 case NFA_VCOL_LT:
3127 case NFA_MARK_GT:
3128 case NFA_MARK_LT:
3129 case NFA_VISUAL:
3130 case NFA_LNUM:
3131 case NFA_CURSOR:
3132 case NFA_COL:
3133 case NFA_VCOL:
3134 case NFA_MARK:
3135
3136 case NFA_ZSTART:
3137 case NFA_ZEND:
3138 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003139 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003140 case NFA_START_PATTERN:
3141 case NFA_END_PATTERN:
3142 case NFA_COMPOSING:
3143 case NFA_END_COMPOSING:
3144 /* zero-width */
3145 break;
3146
3147 default:
3148 if (state->c < 0)
3149 /* don't know what this is */
3150 return -1;
3151 /* normal character */
3152 len += MB_CHAR2LEN(state->c);
3153 break;
3154 }
3155
3156 /* normal way to continue */
3157 state = state->out;
3158 }
3159
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003160 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003161 return -1;
3162}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003163
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003164/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003165 * Convert a postfix form into its equivalent NFA.
3166 * Return the NFA start state on success, NULL otherwise.
3167 */
3168 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003169post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003170{
3171 int *p;
3172 int mopen;
3173 int mclose;
3174 Frag_T *stack = NULL;
3175 Frag_T *stackp = NULL;
3176 Frag_T *stack_end = NULL;
3177 Frag_T e1;
3178 Frag_T e2;
3179 Frag_T e;
3180 nfa_state_T *s;
3181 nfa_state_T *s1;
3182 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003183 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003184
3185 if (postfix == NULL)
3186 return NULL;
3187
Bram Moolenaar053bb602013-05-20 13:55:21 +02003188#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189#define POP() st_pop(&stackp, stack); \
3190 if (stackp < stack) \
3191 { \
3192 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003193 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003194 return NULL; \
3195 }
3196
3197 if (nfa_calc_size == FALSE)
3198 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003199 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003200 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003201 if (stack == NULL)
3202 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003204 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003205 }
3206
3207 for (p = postfix; p < end; ++p)
3208 {
3209 switch (*p)
3210 {
3211 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003212 /* Concatenation.
3213 * Pay attention: this operator does not exist in the r.e. itself
3214 * (it is implicit, really). It is added when r.e. is translated
3215 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 if (nfa_calc_size == TRUE)
3217 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003218 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219 break;
3220 }
3221 e2 = POP();
3222 e1 = POP();
3223 patch(e1.out, e2.start);
3224 PUSH(frag(e1.start, e2.out));
3225 break;
3226
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003227 case NFA_OR:
3228 /* Alternation */
3229 if (nfa_calc_size == TRUE)
3230 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003231 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 break;
3233 }
3234 e2 = POP();
3235 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003236 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003238 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 PUSH(frag(s, append(e1.out, e2.out)));
3240 break;
3241
3242 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003243 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 if (nfa_calc_size == TRUE)
3245 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003246 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 break;
3248 }
3249 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003250 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003252 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253 patch(e.out, s);
3254 PUSH(frag(s, list1(&s->out1)));
3255 break;
3256
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003257 case NFA_STAR_NONGREEDY:
3258 /* Zero or more, prefer zero */
3259 if (nfa_calc_size == TRUE)
3260 {
3261 nstate++;
3262 break;
3263 }
3264 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003265 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003266 if (s == NULL)
3267 goto theend;
3268 patch(e.out, s);
3269 PUSH(frag(s, list1(&s->out)));
3270 break;
3271
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272 case NFA_QUEST:
3273 /* one or zero atoms=> greedy match */
3274 if (nfa_calc_size == TRUE)
3275 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003276 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003277 break;
3278 }
3279 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003280 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003282 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 PUSH(frag(s, append(e.out, list1(&s->out1))));
3284 break;
3285
3286 case NFA_QUEST_NONGREEDY:
3287 /* zero or one atoms => non-greedy match */
3288 if (nfa_calc_size == TRUE)
3289 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003290 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 break;
3292 }
3293 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003294 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003296 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 PUSH(frag(s, append(e.out, list1(&s->out))));
3298 break;
3299
Bram Moolenaar417bad22013-06-07 14:08:30 +02003300 case NFA_END_COLL:
3301 case NFA_END_NEG_COLL:
3302 /* On the stack is the sequence starting with NFA_START_COLL or
3303 * NFA_START_NEG_COLL and all possible characters. Patch it to
3304 * add the output to the start. */
3305 if (nfa_calc_size == TRUE)
3306 {
3307 nstate++;
3308 break;
3309 }
3310 e = POP();
3311 s = alloc_state(NFA_END_COLL, NULL, NULL);
3312 if (s == NULL)
3313 goto theend;
3314 patch(e.out, s);
3315 e.start->out1 = s;
3316 PUSH(frag(e.start, list1(&s->out)));
3317 break;
3318
3319 case NFA_RANGE:
3320 /* Before this are two characters, the low and high end of a
3321 * range. Turn them into two states with MIN and MAX. */
3322 if (nfa_calc_size == TRUE)
3323 {
3324 /* nstate += 0; */
3325 break;
3326 }
3327 e2 = POP();
3328 e1 = POP();
3329 e2.start->val = e2.start->c;
3330 e2.start->c = NFA_RANGE_MAX;
3331 e1.start->val = e1.start->c;
3332 e1.start->c = NFA_RANGE_MIN;
3333 patch(e1.out, e2.start);
3334 PUSH(frag(e1.start, e2.out));
3335 break;
3336
Bram Moolenaar699c1202013-09-25 16:41:54 +02003337 case NFA_EMPTY:
3338 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339 if (nfa_calc_size == TRUE)
3340 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003341 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342 break;
3343 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003344 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003345 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003346 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003347 PUSH(frag(s, list1(&s->out)));
3348 break;
3349
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003350 case NFA_OPT_CHARS:
3351 {
3352 int n;
3353
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003354 /* \%[abc] implemented as:
3355 * NFA_SPLIT
3356 * +-CHAR(a)
3357 * | +-NFA_SPLIT
3358 * | +-CHAR(b)
3359 * | | +-NFA_SPLIT
3360 * | | +-CHAR(c)
3361 * | | | +-next
3362 * | | +- next
3363 * | +- next
3364 * +- next
3365 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003366 n = *++p; /* get number of characters */
3367 if (nfa_calc_size == TRUE)
3368 {
3369 nstate += n;
3370 break;
3371 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003372 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003373 e1.out = NULL; /* stores list with out1's */
3374 s1 = NULL; /* previous NFA_SPLIT to connect to */
3375 while (n-- > 0)
3376 {
3377 e = POP(); /* get character */
3378 s = alloc_state(NFA_SPLIT, e.start, NULL);
3379 if (s == NULL)
3380 goto theend;
3381 if (e1.out == NULL)
3382 e1 = e;
3383 patch(e.out, s1);
3384 append(e1.out, list1(&s->out1));
3385 s1 = s;
3386 }
3387 PUSH(frag(s, e1.out));
3388 break;
3389 }
3390
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003392 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003393 case NFA_PREV_ATOM_JUST_BEFORE:
3394 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003395 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003396 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003397 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3398 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003399 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003400 int start_state;
3401 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003402 int n = 0;
3403 nfa_state_T *zend;
3404 nfa_state_T *skip;
3405
Bram Moolenaardecd9542013-06-07 16:31:50 +02003406 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003407 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003408 case NFA_PREV_ATOM_NO_WIDTH:
3409 start_state = NFA_START_INVISIBLE;
3410 end_state = NFA_END_INVISIBLE;
3411 break;
3412 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3413 start_state = NFA_START_INVISIBLE_NEG;
3414 end_state = NFA_END_INVISIBLE_NEG;
3415 break;
3416 case NFA_PREV_ATOM_JUST_BEFORE:
3417 start_state = NFA_START_INVISIBLE_BEFORE;
3418 end_state = NFA_END_INVISIBLE;
3419 break;
3420 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3421 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3422 end_state = NFA_END_INVISIBLE_NEG;
3423 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003424 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003425 start_state = NFA_START_PATTERN;
3426 end_state = NFA_END_PATTERN;
3427 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003428 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003429
3430 if (before)
3431 n = *++p; /* get the count */
3432
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003433 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003434 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003435 * The \@<= operator: match for the preceding atom.
3436 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003437 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003438 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003439
3440 if (nfa_calc_size == TRUE)
3441 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003442 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003443 break;
3444 }
3445 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003446 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003447 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003448 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003449
Bram Moolenaar87953742013-06-05 18:52:40 +02003450 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003451 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003452 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003453 if (pattern)
3454 {
3455 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3456 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003457 if (skip == NULL)
3458 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003459 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003460 if (zend == NULL)
3461 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003462 s1->out= skip;
3463 patch(e.out, zend);
3464 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003465 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003466 else
3467 {
3468 patch(e.out, s1);
3469 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003470 if (before)
3471 {
3472 if (n <= 0)
3473 /* See if we can guess the maximum width, it avoids a
3474 * lot of pointless tries. */
3475 n = nfa_max_width(e.start, 0);
3476 s->val = n; /* store the count */
3477 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003478 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003479 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003480 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003482 case NFA_COMPOSING: /* char with composing char */
3483#if 0
3484 /* TODO */
3485 if (regflags & RF_ICOMBINE)
3486 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003487 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003488 }
3489#endif
3490 /* FALLTHROUGH */
3491
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003492 case NFA_MOPEN: /* \( \) Submatch */
3493 case NFA_MOPEN1:
3494 case NFA_MOPEN2:
3495 case NFA_MOPEN3:
3496 case NFA_MOPEN4:
3497 case NFA_MOPEN5:
3498 case NFA_MOPEN6:
3499 case NFA_MOPEN7:
3500 case NFA_MOPEN8:
3501 case NFA_MOPEN9:
3502#ifdef FEAT_SYN_HL
3503 case NFA_ZOPEN: /* \z( \) Submatch */
3504 case NFA_ZOPEN1:
3505 case NFA_ZOPEN2:
3506 case NFA_ZOPEN3:
3507 case NFA_ZOPEN4:
3508 case NFA_ZOPEN5:
3509 case NFA_ZOPEN6:
3510 case NFA_ZOPEN7:
3511 case NFA_ZOPEN8:
3512 case NFA_ZOPEN9:
3513#endif
3514 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003515 if (nfa_calc_size == TRUE)
3516 {
3517 nstate += 2;
3518 break;
3519 }
3520
3521 mopen = *p;
3522 switch (*p)
3523 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003524 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3525#ifdef FEAT_SYN_HL
3526 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3527 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3528 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3529 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3530 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3531 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3532 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3533 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3534 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3535 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3536#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003537 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003538 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003539 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 mclose = *p + NSUBEXP;
3541 break;
3542 }
3543
3544 /* Allow "NFA_MOPEN" as a valid postfix representation for
3545 * the empty regexp "". In this case, the NFA will be
3546 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3547 * empty groups of parenthesis, and empty mbyte chars */
3548 if (stackp == stack)
3549 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003550 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003551 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003552 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003553 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003555 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003556 patch(list1(&s->out), s1);
3557 PUSH(frag(s, list1(&s1->out)));
3558 break;
3559 }
3560
3561 /* At least one node was emitted before NFA_MOPEN, so
3562 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3563 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003564 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003566 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003567
Bram Moolenaar525666f2013-06-02 16:40:55 +02003568 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003569 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003570 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 patch(e.out, s1);
3572
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003573 if (mopen == NFA_COMPOSING)
3574 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003575 patch(list1(&s->out1), s1);
3576
3577 PUSH(frag(s, list1(&s1->out)));
3578 break;
3579
Bram Moolenaar5714b802013-05-28 22:03:20 +02003580 case NFA_BACKREF1:
3581 case NFA_BACKREF2:
3582 case NFA_BACKREF3:
3583 case NFA_BACKREF4:
3584 case NFA_BACKREF5:
3585 case NFA_BACKREF6:
3586 case NFA_BACKREF7:
3587 case NFA_BACKREF8:
3588 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003589#ifdef FEAT_SYN_HL
3590 case NFA_ZREF1:
3591 case NFA_ZREF2:
3592 case NFA_ZREF3:
3593 case NFA_ZREF4:
3594 case NFA_ZREF5:
3595 case NFA_ZREF6:
3596 case NFA_ZREF7:
3597 case NFA_ZREF8:
3598 case NFA_ZREF9:
3599#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003600 if (nfa_calc_size == TRUE)
3601 {
3602 nstate += 2;
3603 break;
3604 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003605 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003606 if (s == NULL)
3607 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003608 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003609 if (s1 == NULL)
3610 goto theend;
3611 patch(list1(&s->out), s1);
3612 PUSH(frag(s, list1(&s1->out)));
3613 break;
3614
Bram Moolenaar423532e2013-05-29 21:14:42 +02003615 case NFA_LNUM:
3616 case NFA_LNUM_GT:
3617 case NFA_LNUM_LT:
3618 case NFA_VCOL:
3619 case NFA_VCOL_GT:
3620 case NFA_VCOL_LT:
3621 case NFA_COL:
3622 case NFA_COL_GT:
3623 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003624 case NFA_MARK:
3625 case NFA_MARK_GT:
3626 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003627 {
3628 int n = *++p; /* lnum, col or mark name */
3629
Bram Moolenaar423532e2013-05-29 21:14:42 +02003630 if (nfa_calc_size == TRUE)
3631 {
3632 nstate += 1;
3633 break;
3634 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003635 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003636 if (s == NULL)
3637 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003638 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003639 PUSH(frag(s, list1(&s->out)));
3640 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003641 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003642
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 case NFA_ZSTART:
3644 case NFA_ZEND:
3645 default:
3646 /* Operands */
3647 if (nfa_calc_size == TRUE)
3648 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003649 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003650 break;
3651 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003652 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003653 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003654 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 PUSH(frag(s, list1(&s->out)));
3656 break;
3657
3658 } /* switch(*p) */
3659
3660 } /* for(p = postfix; *p; ++p) */
3661
3662 if (nfa_calc_size == TRUE)
3663 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003664 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003665 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003666 }
3667
3668 e = POP();
3669 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003670 {
3671 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003673 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003674
3675 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003676 {
3677 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003679 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003681 matchstate = &state_ptr[istate++]; /* the match state */
3682 matchstate->c = NFA_MATCH;
3683 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003684 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685
3686 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003687 ret = e.start;
3688
3689theend:
3690 vim_free(stack);
3691 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692
3693#undef POP1
3694#undef PUSH1
3695#undef POP2
3696#undef PUSH2
3697#undef POP
3698#undef PUSH
3699}
3700
Bram Moolenaara2947e22013-06-11 22:44:09 +02003701/*
3702 * After building the NFA program, inspect it to add optimization hints.
3703 */
3704 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003705nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003706{
3707 int i;
3708 int c;
3709
3710 for (i = 0; i < prog->nstate; ++i)
3711 {
3712 c = prog->state[i].c;
3713 if (c == NFA_START_INVISIBLE
3714 || c == NFA_START_INVISIBLE_NEG
3715 || c == NFA_START_INVISIBLE_BEFORE
3716 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3717 {
3718 int directly;
3719
3720 /* Do it directly when what follows is possibly the end of the
3721 * match. */
3722 if (match_follows(prog->state[i].out1->out, 0))
3723 directly = TRUE;
3724 else
3725 {
3726 int ch_invisible = failure_chance(prog->state[i].out, 0);
3727 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3728
3729 /* Postpone when the invisible match is expensive or has a
3730 * lower chance of failing. */
3731 if (c == NFA_START_INVISIBLE_BEFORE
3732 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3733 {
3734 /* "before" matches are very expensive when
3735 * unbounded, always prefer what follows then,
3736 * unless what follows will always match.
3737 * Otherwise strongly prefer what follows. */
3738 if (prog->state[i].val <= 0 && ch_follows > 0)
3739 directly = FALSE;
3740 else
3741 directly = ch_follows * 10 < ch_invisible;
3742 }
3743 else
3744 {
3745 /* normal invisible, first do the one with the
3746 * highest failure chance */
3747 directly = ch_follows < ch_invisible;
3748 }
3749 }
3750 if (directly)
3751 /* switch to the _FIRST state */
3752 ++prog->state[i].c;
3753 }
3754 }
3755}
3756
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757/****************************************************************
3758 * NFA execution code.
3759 ****************************************************************/
3760
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003761typedef struct
3762{
3763 int in_use; /* number of subexpr with useful info */
3764
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003765 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003766 union
3767 {
3768 struct multipos
3769 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003770 linenr_T start_lnum;
3771 linenr_T end_lnum;
3772 colnr_T start_col;
3773 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003774 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003775 struct linepos
3776 {
3777 char_u *start;
3778 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003779 } line[NSUBEXP];
3780 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003781} regsub_T;
3782
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003783typedef struct
3784{
3785 regsub_T norm; /* \( .. \) matches */
3786#ifdef FEAT_SYN_HL
3787 regsub_T synt; /* \z( .. \) matches */
3788#endif
3789} regsubs_T;
3790
Bram Moolenaara2d95102013-06-04 14:23:05 +02003791/* nfa_pim_T stores a Postponed Invisible Match. */
3792typedef struct nfa_pim_S nfa_pim_T;
3793struct nfa_pim_S
3794{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003795 int result; /* NFA_PIM_*, see below */
3796 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003797 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003798 union
3799 {
3800 lpos_T pos;
3801 char_u *ptr;
3802 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003803};
3804
3805/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003806#define NFA_PIM_UNUSED 0 /* pim not used */
3807#define NFA_PIM_TODO 1 /* pim not done yet */
3808#define NFA_PIM_MATCH 2 /* pim executed, matches */
3809#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003810
3811
Bram Moolenaar963fee22013-05-26 21:47:28 +02003812/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003813typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003814{
3815 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003817 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3818 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003819 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003820} nfa_thread_T;
3821
Bram Moolenaar963fee22013-05-26 21:47:28 +02003822/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003823typedef struct
3824{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003825 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003826 int n; /* nr of states currently in "t" */
3827 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003828 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003829 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003830} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831
Bram Moolenaar5714b802013-05-28 22:03:20 +02003832#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003833static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003834
3835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003836log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003837{
3838 log_subexpr(&subs->norm);
3839# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003840 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003841 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003842# endif
3843}
3844
Bram Moolenaar5714b802013-05-28 22:03:20 +02003845 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003846log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003847{
3848 int j;
3849
3850 for (j = 0; j < sub->in_use; j++)
3851 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003852 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003853 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003854 sub->list.multi[j].start_col,
3855 (int)sub->list.multi[j].start_lnum,
3856 sub->list.multi[j].end_col,
3857 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003858 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003859 {
3860 char *s = (char *)sub->list.line[j].start;
3861 char *e = (char *)sub->list.line[j].end;
3862
Bram Moolenaar87953742013-06-05 18:52:40 +02003863 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003864 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003865 s == NULL ? "NULL" : s,
3866 e == NULL ? "NULL" : e);
3867 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003868}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003869
3870 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003871pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003872{
3873 static char buf[30];
3874
3875 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3876 buf[0] = NUL;
3877 else
3878 {
3879 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003880 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003881 }
3882 return buf;
3883}
3884
Bram Moolenaar5714b802013-05-28 22:03:20 +02003885#endif
3886
Bram Moolenaar963fee22013-05-26 21:47:28 +02003887/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003888static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003889#ifdef FEAT_RELTIME
3890static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003891static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003892static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003893#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003894
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003895static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003896static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003897
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003898/*
3899 * Copy postponed invisible match info from "from" to "to".
3900 */
3901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003902copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003903{
3904 to->result = from->result;
3905 to->state = from->state;
3906 copy_sub(&to->subs.norm, &from->subs.norm);
3907#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003908 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003909 copy_sub(&to->subs.synt, &from->subs.synt);
3910#endif
3911 to->end = from->end;
3912}
3913
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003915clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003916{
3917 if (REG_MULTI)
3918 /* Use 0xff to set lnum to -1 */
3919 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003920 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003921 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003922 vim_memset(sub->list.line, 0,
3923 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003924 sub->in_use = 0;
3925}
3926
3927/*
3928 * Copy the submatches from "from" to "to".
3929 */
3930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003931copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003932{
3933 to->in_use = from->in_use;
3934 if (from->in_use > 0)
3935 {
3936 /* Copy the match start and end positions. */
3937 if (REG_MULTI)
3938 mch_memmove(&to->list.multi[0],
3939 &from->list.multi[0],
3940 sizeof(struct multipos) * from->in_use);
3941 else
3942 mch_memmove(&to->list.line[0],
3943 &from->list.line[0],
3944 sizeof(struct linepos) * from->in_use);
3945 }
3946}
3947
3948/*
3949 * Like copy_sub() but exclude the main match.
3950 */
3951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003952copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003953{
3954 if (to->in_use < from->in_use)
3955 to->in_use = from->in_use;
3956 if (from->in_use > 1)
3957 {
3958 /* Copy the match start and end positions. */
3959 if (REG_MULTI)
3960 mch_memmove(&to->list.multi[1],
3961 &from->list.multi[1],
3962 sizeof(struct multipos) * (from->in_use - 1));
3963 else
3964 mch_memmove(&to->list.line[1],
3965 &from->list.line[1],
3966 sizeof(struct linepos) * (from->in_use - 1));
3967 }
3968}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003969
Bram Moolenaar428e9872013-05-30 17:05:39 +02003970/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003971 * Like copy_sub() but only do the end of the main match if \ze is present.
3972 */
3973 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003974copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003975{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003976 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003977 {
3978 if (REG_MULTI)
3979 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003980 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003981 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003982 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3983 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003984 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003985 }
3986 else
3987 {
3988 if (from->list.line[0].end != NULL)
3989 to->list.line[0].end = from->list.line[0].end;
3990 }
3991 }
3992}
3993
3994/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003995 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003996 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003997 */
3998 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003999sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004000{
4001 int i;
4002 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004003 linenr_T s1;
4004 linenr_T s2;
4005 char_u *sp1;
4006 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004007
4008 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4009 if (REG_MULTI)
4010 {
4011 for (i = 0; i < todo; ++i)
4012 {
4013 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004014 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004015 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004016 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004017 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004018 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004019 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004020 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004021 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004022 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004023 if (s1 != -1 && sub1->list.multi[i].start_col
4024 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004025 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004026
Bram Moolenaar0270f382018-07-17 05:43:58 +02004027 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004028 {
4029 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004030 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004031 else
4032 s1 = -1;
4033 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004034 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004035 else
4036 s2 = -1;
4037 if (s1 != s2)
4038 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004039 if (s1 != -1 && sub1->list.multi[i].end_col
4040 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004041 return FALSE;
4042 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004043 }
4044 }
4045 else
4046 {
4047 for (i = 0; i < todo; ++i)
4048 {
4049 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004050 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004051 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004052 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004053 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004054 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004055 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004056 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004057 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004058 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004059 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004060 {
4061 if (i < sub1->in_use)
4062 sp1 = sub1->list.line[i].end;
4063 else
4064 sp1 = NULL;
4065 if (i < sub2->in_use)
4066 sp2 = sub2->list.line[i].end;
4067 else
4068 sp2 = NULL;
4069 if (sp1 != sp2)
4070 return FALSE;
4071 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004072 }
4073 }
4074
4075 return TRUE;
4076}
4077
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004078#ifdef ENABLE_LOG
4079 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004080report_state(char *action,
4081 regsub_T *sub,
4082 nfa_state_T *state,
4083 int lid,
4084 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004085{
4086 int col;
4087
4088 if (sub->in_use <= 0)
4089 col = -1;
4090 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004091 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004092 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004093 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004094 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004095 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4096 action, abs(state->id), lid, state->c, code, col,
4097 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004098}
4099#endif
4100
Bram Moolenaar43e02982013-06-07 17:31:29 +02004101/*
4102 * Return TRUE if the same state is already in list "l" with the same
4103 * positions as "subs".
4104 */
4105 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004106has_state_with_pos(
4107 nfa_list_T *l, /* runtime state list */
4108 nfa_state_T *state, /* state to update */
4109 regsubs_T *subs, /* pointers to subexpressions */
4110 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004111{
4112 nfa_thread_T *thread;
4113 int i;
4114
4115 for (i = 0; i < l->n; ++i)
4116 {
4117 thread = &l->t[i];
4118 if (thread->state->id == state->id
4119 && sub_equal(&thread->subs.norm, &subs->norm)
4120#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004121 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004122 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004123#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004124 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004125 return TRUE;
4126 }
4127 return FALSE;
4128}
4129
4130/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004131 * Return TRUE if "one" and "two" are equal. That includes when both are not
4132 * set.
4133 */
4134 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004135pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004136{
4137 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4138 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4139
4140 if (one_unused)
4141 /* one is unused: equal when two is also unused */
4142 return two_unused;
4143 if (two_unused)
4144 /* one is used and two is not: not equal */
4145 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004146 /* compare the state id */
4147 if (one->state->id != two->state->id)
4148 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004149 /* compare the position */
4150 if (REG_MULTI)
4151 return one->end.pos.lnum == two->end.pos.lnum
4152 && one->end.pos.col == two->end.pos.col;
4153 return one->end.ptr == two->end.ptr;
4154}
4155
4156/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004157 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4158 */
4159 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004160match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004161{
4162 nfa_state_T *state = startstate;
4163
4164 /* avoid too much recursion */
4165 if (depth > 10)
4166 return FALSE;
4167
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004168 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004169 {
4170 switch (state->c)
4171 {
4172 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004173 case NFA_MCLOSE:
4174 case NFA_END_INVISIBLE:
4175 case NFA_END_INVISIBLE_NEG:
4176 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004177 return TRUE;
4178
4179 case NFA_SPLIT:
4180 return match_follows(state->out, depth + 1)
4181 || match_follows(state->out1, depth + 1);
4182
4183 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004184 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004185 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004186 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004187 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004188 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004189 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004190 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004191 case NFA_COMPOSING:
4192 /* skip ahead to next state */
4193 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004194 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004195
4196 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004197 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004198 case NFA_IDENT:
4199 case NFA_SIDENT:
4200 case NFA_KWORD:
4201 case NFA_SKWORD:
4202 case NFA_FNAME:
4203 case NFA_SFNAME:
4204 case NFA_PRINT:
4205 case NFA_SPRINT:
4206 case NFA_WHITE:
4207 case NFA_NWHITE:
4208 case NFA_DIGIT:
4209 case NFA_NDIGIT:
4210 case NFA_HEX:
4211 case NFA_NHEX:
4212 case NFA_OCTAL:
4213 case NFA_NOCTAL:
4214 case NFA_WORD:
4215 case NFA_NWORD:
4216 case NFA_HEAD:
4217 case NFA_NHEAD:
4218 case NFA_ALPHA:
4219 case NFA_NALPHA:
4220 case NFA_LOWER:
4221 case NFA_NLOWER:
4222 case NFA_UPPER:
4223 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004224 case NFA_LOWER_IC:
4225 case NFA_NLOWER_IC:
4226 case NFA_UPPER_IC:
4227 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004228 case NFA_START_COLL:
4229 case NFA_START_NEG_COLL:
4230 case NFA_NEWL:
4231 /* state will advance input */
4232 return FALSE;
4233
4234 default:
4235 if (state->c > 0)
4236 /* state will advance input */
4237 return FALSE;
4238
4239 /* Others: zero-width or possibly zero-width, might still find
4240 * a match at the same position, keep looking. */
4241 break;
4242 }
4243 state = state->out;
4244 }
4245 return FALSE;
4246}
4247
4248
4249/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004250 * Return TRUE if "state" is already in list "l".
4251 */
4252 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004253state_in_list(
4254 nfa_list_T *l, /* runtime state list */
4255 nfa_state_T *state, /* state to update */
4256 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004257{
4258 if (state->lastlist[nfa_ll_index] == l->id)
4259 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004260 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004261 return TRUE;
4262 }
4263 return FALSE;
4264}
4265
Bram Moolenaar16b35782016-09-09 20:29:50 +02004266/* Offset used for "off" by addstate_here(). */
4267#define ADDSTATE_HERE_OFFSET 10
4268
Bram Moolenaard05bf562013-06-30 23:24:08 +02004269/*
4270 * Add "state" and possibly what follows to state list ".".
4271 * Returns "subs_arg", possibly copied into temp_subs.
4272 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004273 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004274addstate(
4275 nfa_list_T *l, /* runtime state list */
4276 nfa_state_T *state, /* state to update */
4277 regsubs_T *subs_arg, /* pointers to subexpressions */
4278 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004279 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004280{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004281 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004282 int off = off_arg;
4283 int add_here = FALSE;
4284 int listindex = 0;
4285 int k;
4286 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004287 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004288 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004289 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004290 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004291 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004292 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004293 regsubs_T *subs = subs_arg;
4294 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004295#ifdef ENABLE_LOG
4296 int did_print = FALSE;
4297#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298
Bram Moolenaar16b35782016-09-09 20:29:50 +02004299 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4300 {
4301 add_here = TRUE;
4302 off = 0;
4303 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4304 }
4305
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004306 switch (state->c)
4307 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308 case NFA_NCLOSE:
4309 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004310 case NFA_MCLOSE1:
4311 case NFA_MCLOSE2:
4312 case NFA_MCLOSE3:
4313 case NFA_MCLOSE4:
4314 case NFA_MCLOSE5:
4315 case NFA_MCLOSE6:
4316 case NFA_MCLOSE7:
4317 case NFA_MCLOSE8:
4318 case NFA_MCLOSE9:
4319#ifdef FEAT_SYN_HL
4320 case NFA_ZCLOSE:
4321 case NFA_ZCLOSE1:
4322 case NFA_ZCLOSE2:
4323 case NFA_ZCLOSE3:
4324 case NFA_ZCLOSE4:
4325 case NFA_ZCLOSE5:
4326 case NFA_ZCLOSE6:
4327 case NFA_ZCLOSE7:
4328 case NFA_ZCLOSE8:
4329 case NFA_ZCLOSE9:
4330#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004331 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004332 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004333 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004334 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004335 /* These nodes are not added themselves but their "out" and/or
4336 * "out1" may be added below. */
4337 break;
4338
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004339 case NFA_BOL:
4340 case NFA_BOF:
4341 /* "^" won't match past end-of-line, don't bother trying.
4342 * Except when at the end of the line, or when we are going to the
4343 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004344 if (rex.input > rex.line
4345 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004346 && (nfa_endp == NULL
4347 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004348 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004349 goto skip_add;
4350 /* FALLTHROUGH */
4351
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004352 case NFA_MOPEN1:
4353 case NFA_MOPEN2:
4354 case NFA_MOPEN3:
4355 case NFA_MOPEN4:
4356 case NFA_MOPEN5:
4357 case NFA_MOPEN6:
4358 case NFA_MOPEN7:
4359 case NFA_MOPEN8:
4360 case NFA_MOPEN9:
4361#ifdef FEAT_SYN_HL
4362 case NFA_ZOPEN:
4363 case NFA_ZOPEN1:
4364 case NFA_ZOPEN2:
4365 case NFA_ZOPEN3:
4366 case NFA_ZOPEN4:
4367 case NFA_ZOPEN5:
4368 case NFA_ZOPEN6:
4369 case NFA_ZOPEN7:
4370 case NFA_ZOPEN8:
4371 case NFA_ZOPEN9:
4372#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004373 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004374 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004375 /* These nodes need to be added so that we can bail out when it
4376 * was added to this list before at the same position to avoid an
4377 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004378
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004379 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004380 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004381 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004382 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004383 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004384 * when there is a PIM. For NFA_MATCH check the position,
4385 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004386 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004387 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004388 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004389 /* When called from addstate_here() do insert before
4390 * existing states. */
4391 if (add_here)
4392 {
4393 for (k = 0; k < l->n && k < listindex; ++k)
4394 if (l->t[k].state->id == state->id)
4395 {
4396 found = TRUE;
4397 break;
4398 }
4399 }
4400 if (!add_here || found)
4401 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004402skip_add:
4403#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004404 nfa_set_code(state->c);
4405 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4406 abs(state->id), l->id, state->c, code,
4407 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004408#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004409 return subs;
4410 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004411 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004412
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004413 /* Do not add the state again when it exists with the same
4414 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004415 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004416 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004417 }
4418
Bram Moolenaara0169122013-06-26 18:16:58 +02004419 /* When there are backreferences or PIMs the number of states may
4420 * be (a lot) bigger than anticipated. */
4421 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004422 {
4423 int newlen = l->len * 3 / 2 + 50;
4424
Bram Moolenaard05bf562013-06-30 23:24:08 +02004425 if (subs != &temp_subs)
4426 {
4427 /* "subs" may point into the current array, need to make a
4428 * copy before it becomes invalid. */
4429 copy_sub(&temp_subs.norm, &subs->norm);
4430#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004431 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004432 copy_sub(&temp_subs.synt, &subs->synt);
4433#endif
4434 subs = &temp_subs;
4435 }
4436
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004437 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004438 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4439 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004440 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004441
4442 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004443 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004444 thread = &l->t[l->n++];
4445 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004446 if (pim == NULL)
4447 thread->pim.result = NFA_PIM_UNUSED;
4448 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004449 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004450 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004451 l->has_pim = TRUE;
4452 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004453 copy_sub(&thread->subs.norm, &subs->norm);
4454#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004455 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004456 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004457#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004458#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004459 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004460 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004461#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004462 }
4463
4464#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004465 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004466 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004467#endif
4468 switch (state->c)
4469 {
4470 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004471 break;
4472
4473 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004474 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004475 subs = addstate(l, state->out, subs, pim, off_arg);
4476 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004477 break;
4478
Bram Moolenaar699c1202013-09-25 16:41:54 +02004479 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004480 case NFA_NOPEN:
4481 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004482 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004483 break;
4484
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004485 case NFA_MOPEN:
4486 case NFA_MOPEN1:
4487 case NFA_MOPEN2:
4488 case NFA_MOPEN3:
4489 case NFA_MOPEN4:
4490 case NFA_MOPEN5:
4491 case NFA_MOPEN6:
4492 case NFA_MOPEN7:
4493 case NFA_MOPEN8:
4494 case NFA_MOPEN9:
4495#ifdef FEAT_SYN_HL
4496 case NFA_ZOPEN:
4497 case NFA_ZOPEN1:
4498 case NFA_ZOPEN2:
4499 case NFA_ZOPEN3:
4500 case NFA_ZOPEN4:
4501 case NFA_ZOPEN5:
4502 case NFA_ZOPEN6:
4503 case NFA_ZOPEN7:
4504 case NFA_ZOPEN8:
4505 case NFA_ZOPEN9:
4506#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004507 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004508 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004509 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004510 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004511 sub = &subs->norm;
4512 }
4513#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004514 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004515 {
4516 subidx = state->c - NFA_ZOPEN;
4517 sub = &subs->synt;
4518 }
4519#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004520 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004521 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004522 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004523 sub = &subs->norm;
4524 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004526 /* avoid compiler warnings */
4527 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004528 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004529
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004530 /* Set the position (with "off" added) in the subexpression. Save
4531 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004532 if (REG_MULTI)
4533 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004534 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004535 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004536 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004537 save_in_use = -1;
4538 }
4539 else
4540 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004541 save_in_use = sub->in_use;
4542 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004543 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004544 sub->list.multi[i].start_lnum = -1;
4545 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004546 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004547 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004548 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004549 if (off == -1)
4550 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004551 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004552 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004553 }
4554 else
4555 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004556 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004557 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004558 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004559 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004560 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561 }
4562 else
4563 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004564 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004565 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004566 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004567 save_in_use = -1;
4568 }
4569 else
4570 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004571 save_in_use = sub->in_use;
4572 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004573 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004574 sub->list.line[i].start = NULL;
4575 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004576 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004577 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004578 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004579 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 }
4581
Bram Moolenaar16b35782016-09-09 20:29:50 +02004582 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004583 /* "subs" may have changed, need to set "sub" again */
4584#ifdef FEAT_SYN_HL
4585 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4586 sub = &subs->synt;
4587 else
4588#endif
4589 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004591 if (save_in_use == -1)
4592 {
4593 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004594 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004595 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004596 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004597 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004598 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004599 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 break;
4601
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004602 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004603 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004604 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004605 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004606 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004607 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004608 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 break;
4610 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004611 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004612 case NFA_MCLOSE1:
4613 case NFA_MCLOSE2:
4614 case NFA_MCLOSE3:
4615 case NFA_MCLOSE4:
4616 case NFA_MCLOSE5:
4617 case NFA_MCLOSE6:
4618 case NFA_MCLOSE7:
4619 case NFA_MCLOSE8:
4620 case NFA_MCLOSE9:
4621#ifdef FEAT_SYN_HL
4622 case NFA_ZCLOSE:
4623 case NFA_ZCLOSE1:
4624 case NFA_ZCLOSE2:
4625 case NFA_ZCLOSE3:
4626 case NFA_ZCLOSE4:
4627 case NFA_ZCLOSE5:
4628 case NFA_ZCLOSE6:
4629 case NFA_ZCLOSE7:
4630 case NFA_ZCLOSE8:
4631 case NFA_ZCLOSE9:
4632#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004633 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004634 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004635 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004637 sub = &subs->norm;
4638 }
4639#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004640 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004641 {
4642 subidx = state->c - NFA_ZCLOSE;
4643 sub = &subs->synt;
4644 }
4645#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004646 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004647 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004648 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004649 sub = &subs->norm;
4650 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004652 /* We don't fill in gaps here, there must have been an MOPEN that
4653 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004654 save_in_use = sub->in_use;
4655 if (sub->in_use <= subidx)
4656 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004657 if (REG_MULTI)
4658 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004659 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004660 if (off == -1)
4661 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004662 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004663 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004664 }
4665 else
4666 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004667 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004668 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004669 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004670 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004671 /* avoid compiler warnings */
4672 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004673 }
4674 else
4675 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004676 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004677 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004678 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004679 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680 }
4681
Bram Moolenaar16b35782016-09-09 20:29:50 +02004682 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004683 /* "subs" may have changed, need to set "sub" again */
4684#ifdef FEAT_SYN_HL
4685 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4686 sub = &subs->synt;
4687 else
4688#endif
4689 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004690
4691 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004692 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004693 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004694 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004695 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004696 break;
4697 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004698 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699}
4700
4701/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004702 * Like addstate(), but the new state(s) are put at position "*ip".
4703 * Used for zero-width matches, next state to use is the added one.
4704 * This makes sure the order of states to be tried does not change, which
4705 * matters for alternatives.
4706 */
4707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004708addstate_here(
4709 nfa_list_T *l, /* runtime state list */
4710 nfa_state_T *state, /* state to update */
4711 regsubs_T *subs, /* pointers to subexpressions */
4712 nfa_pim_T *pim, /* postponed look-behind match */
4713 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004714{
4715 int tlen = l->n;
4716 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004717 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004718
Bram Moolenaar16b35782016-09-09 20:29:50 +02004719 /* First add the state(s) at the end, so that we know how many there are.
4720 * Pass the listidx as offset (avoids adding another argument to
4721 * addstate(). */
4722 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004723
Bram Moolenaar4b417062013-05-25 20:19:50 +02004724 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004725 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004726 return;
4727
4728 /* re-order to put the new state at the current position */
4729 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004730 if (count == 0)
4731 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004732 if (count == 1)
4733 {
4734 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004735 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004736 }
4737 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004738 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004739 if (l->n + count - 1 >= l->len)
4740 {
4741 /* not enough space to move the new states, reallocate the list
4742 * and move the states to the right position */
4743 nfa_thread_T *newl;
4744
4745 l->len = l->len * 3 / 2 + 50;
4746 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4747 if (newl == NULL)
4748 return;
4749 mch_memmove(&(newl[0]),
4750 &(l->t[0]),
4751 sizeof(nfa_thread_T) * listidx);
4752 mch_memmove(&(newl[listidx]),
4753 &(l->t[l->n - count]),
4754 sizeof(nfa_thread_T) * count);
4755 mch_memmove(&(newl[listidx + count]),
4756 &(l->t[listidx + 1]),
4757 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4758 vim_free(l->t);
4759 l->t = newl;
4760 }
4761 else
4762 {
4763 /* make space for new states, then move them from the
4764 * end to the current position */
4765 mch_memmove(&(l->t[listidx + count]),
4766 &(l->t[listidx + 1]),
4767 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4768 mch_memmove(&(l->t[listidx]),
4769 &(l->t[l->n - 1]),
4770 sizeof(nfa_thread_T) * count);
4771 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004772 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004773 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004774 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004775}
4776
4777/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 * Check character class "class" against current character c.
4779 */
4780 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004781check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004782{
4783 switch (class)
4784 {
4785 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004786 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004787 return OK;
4788 break;
4789 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004790 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004791 return OK;
4792 break;
4793 case NFA_CLASS_BLANK:
4794 if (c == ' ' || c == '\t')
4795 return OK;
4796 break;
4797 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004798 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004799 return OK;
4800 break;
4801 case NFA_CLASS_DIGIT:
4802 if (VIM_ISDIGIT(c))
4803 return OK;
4804 break;
4805 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004806 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004807 return OK;
4808 break;
4809 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004810 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004811 return OK;
4812 break;
4813 case NFA_CLASS_PRINT:
4814 if (vim_isprintc(c))
4815 return OK;
4816 break;
4817 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004818 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004819 return OK;
4820 break;
4821 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004822 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004823 return OK;
4824 break;
4825 case NFA_CLASS_UPPER:
4826 if (MB_ISUPPER(c))
4827 return OK;
4828 break;
4829 case NFA_CLASS_XDIGIT:
4830 if (vim_isxdigit(c))
4831 return OK;
4832 break;
4833 case NFA_CLASS_TAB:
4834 if (c == '\t')
4835 return OK;
4836 break;
4837 case NFA_CLASS_RETURN:
4838 if (c == '\r')
4839 return OK;
4840 break;
4841 case NFA_CLASS_BACKSPACE:
4842 if (c == '\b')
4843 return OK;
4844 break;
4845 case NFA_CLASS_ESCAPE:
4846 if (c == '\033')
4847 return OK;
4848 break;
4849
4850 default:
4851 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004853 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004854 }
4855 return FAIL;
4856}
4857
Bram Moolenaar5714b802013-05-28 22:03:20 +02004858/*
4859 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004860 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004861 */
4862 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004863match_backref(
4864 regsub_T *sub, /* pointers to subexpressions */
4865 int subidx,
4866 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004867{
4868 int len;
4869
4870 if (sub->in_use <= subidx)
4871 {
4872retempty:
4873 /* backref was not set, match an empty string */
4874 *bytelen = 0;
4875 return TRUE;
4876 }
4877
4878 if (REG_MULTI)
4879 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004880 if (sub->list.multi[subidx].start_lnum < 0
4881 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004882 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004883 if (sub->list.multi[subidx].start_lnum == rex.lnum
4884 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004885 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004886 len = sub->list.multi[subidx].end_col
4887 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004888 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4889 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004890 {
4891 *bytelen = len;
4892 return TRUE;
4893 }
4894 }
4895 else
4896 {
4897 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004898 sub->list.multi[subidx].start_lnum,
4899 sub->list.multi[subidx].start_col,
4900 sub->list.multi[subidx].end_lnum,
4901 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004902 bytelen) == RA_MATCH)
4903 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004904 }
4905 }
4906 else
4907 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004908 if (sub->list.line[subidx].start == NULL
4909 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004910 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004911 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004912 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004913 {
4914 *bytelen = len;
4915 return TRUE;
4916 }
4917 }
4918 return FALSE;
4919}
4920
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004921#ifdef FEAT_SYN_HL
4922
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004923/*
4924 * Check for a match with \z subexpression "subidx".
4925 * Return TRUE if it matches.
4926 */
4927 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004928match_zref(
4929 int subidx,
4930 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004931{
4932 int len;
4933
4934 cleanup_zsubexpr();
4935 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4936 {
4937 /* backref was not set, match an empty string */
4938 *bytelen = 0;
4939 return TRUE;
4940 }
4941
4942 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004943 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004944 {
4945 *bytelen = len;
4946 return TRUE;
4947 }
4948 return FALSE;
4949}
4950#endif
4951
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004952/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004953 * Save list IDs for all NFA states of "prog" into "list".
4954 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004955 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004956 */
4957 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004958nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004959{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004960 int i;
4961 nfa_state_T *p;
4962
4963 /* Order in the list is reverse, it's a bit faster that way. */
4964 p = &prog->state[0];
4965 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004966 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004967 list[i] = p->lastlist[1];
4968 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004969 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004970 }
4971}
4972
4973/*
4974 * Restore list IDs from "list" to all NFA states.
4975 */
4976 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004977nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004978{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004979 int i;
4980 nfa_state_T *p;
4981
4982 p = &prog->state[0];
4983 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004984 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004985 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004986 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004987 }
4988}
4989
Bram Moolenaar423532e2013-05-29 21:14:42 +02004990 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004991nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02004992{
4993 if (op == 1) return pos > val;
4994 if (op == 2) return pos < val;
4995 return val == pos;
4996}
4997
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004998static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004999
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005000/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005001 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005002 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5003 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005004 */
5005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005006recursive_regmatch(
5007 nfa_state_T *state,
5008 nfa_pim_T *pim,
5009 nfa_regprog_T *prog,
5010 regsubs_T *submatch,
5011 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005012 int **listids,
5013 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005014{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005015 int save_reginput_col = (int)(rex.input - rex.line);
5016 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005017 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005018 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005019 save_se_T *save_nfa_endp = nfa_endp;
5020 save_se_T endpos;
5021 save_se_T *endposp = NULL;
5022 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005023 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005024
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005025 if (pim != NULL)
5026 {
5027 /* start at the position where the postponed match was */
5028 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005029 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005030 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005031 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005032 }
5033
Bram Moolenaardecd9542013-06-07 16:31:50 +02005034 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005035 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5036 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5037 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005038 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005039 /* The recursive match must end at the current position. When "pim" is
5040 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005041 endposp = &endpos;
5042 if (REG_MULTI)
5043 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005044 if (pim == NULL)
5045 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005046 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5047 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005048 }
5049 else
5050 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005051 }
5052 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005053 {
5054 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005055 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005056 else
5057 endpos.se_u.ptr = pim->end.ptr;
5058 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005059
5060 /* Go back the specified number of bytes, or as far as the
5061 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005062 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005063 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005064 if (state->val <= 0)
5065 {
5066 if (REG_MULTI)
5067 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005068 rex.line = reg_getline(--rex.lnum);
5069 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005070 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005071 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005072 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005073 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005074 }
5075 else
5076 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005077 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005078 {
5079 /* Not enough bytes in this line, go to end of
5080 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005081 rex.line = reg_getline(--rex.lnum);
5082 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005083 {
5084 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005085 rex.line = reg_getline(++rex.lnum);
5086 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005087 }
5088 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005089 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005090 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005091 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005093 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005094 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005095 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005096 }
5097 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005098 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005099 }
5100 }
5101
Bram Moolenaarf46da702013-06-02 22:37:42 +02005102#ifdef ENABLE_LOG
5103 if (log_fd != stderr)
5104 fclose(log_fd);
5105 log_fd = NULL;
5106#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005107 /* Have to clear the lastlist field of the NFA nodes, so that
5108 * nfa_regmatch() and addstate() can run properly after recursion. */
5109 if (nfa_ll_index == 1)
5110 {
5111 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5112 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005113 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005114 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005115 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005116 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005117 if (*listids == NULL)
5118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005119 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005120 return 0;
5121 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005122 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005123 }
5124 nfa_save_listids(prog, *listids);
5125 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005126 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005127 }
5128 else
5129 {
5130 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005131 * entry. Make sure rex.nfa_listid is different from a previous
5132 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005133 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005134 if (rex.nfa_listid <= rex.nfa_alt_listid)
5135 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005136 }
5137
5138 /* Call nfa_regmatch() to check if the current concat matches at this
5139 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005140 nfa_endp = endposp;
5141 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005142
5143 if (need_restore)
5144 nfa_restore_listids(prog, *listids);
5145 else
5146 {
5147 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005148 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005149 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005150
5151 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005152 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005153 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005154 rex.line = reg_getline(rex.lnum);
5155 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005156 if (result != NFA_TOO_EXPENSIVE)
5157 {
5158 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005159 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005160 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005161 nfa_endp = save_nfa_endp;
5162
5163#ifdef ENABLE_LOG
5164 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5165 if (log_fd != NULL)
5166 {
5167 fprintf(log_fd, "****************************\n");
5168 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5169 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5170 fprintf(log_fd, "****************************\n");
5171 }
5172 else
5173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005174 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005175 log_fd = stderr;
5176 }
5177#endif
5178
5179 return result;
5180}
5181
Bram Moolenaara2d95102013-06-04 14:23:05 +02005182/*
5183 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005184 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005185 * NFA_ANY: 1
5186 * specific character: 99
5187 */
5188 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005189failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005190{
5191 int c = state->c;
5192 int l, r;
5193
5194 /* detect looping */
5195 if (depth > 4)
5196 return 1;
5197
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005198 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005199 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005200 case NFA_SPLIT:
5201 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5202 /* avoid recursive stuff */
5203 return 1;
5204 /* two alternatives, use the lowest failure chance */
5205 l = failure_chance(state->out, depth + 1);
5206 r = failure_chance(state->out1, depth + 1);
5207 return l < r ? l : r;
5208
5209 case NFA_ANY:
5210 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005211 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005212
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005213 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005214 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005215 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005216 /* empty match works always */
5217 return 0;
5218
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005219 case NFA_START_INVISIBLE:
5220 case NFA_START_INVISIBLE_FIRST:
5221 case NFA_START_INVISIBLE_NEG:
5222 case NFA_START_INVISIBLE_NEG_FIRST:
5223 case NFA_START_INVISIBLE_BEFORE:
5224 case NFA_START_INVISIBLE_BEFORE_FIRST:
5225 case NFA_START_INVISIBLE_BEFORE_NEG:
5226 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5227 case NFA_START_PATTERN:
5228 /* recursive regmatch is expensive, use low failure chance */
5229 return 5;
5230
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005231 case NFA_BOL:
5232 case NFA_EOL:
5233 case NFA_BOF:
5234 case NFA_EOF:
5235 case NFA_NEWL:
5236 return 99;
5237
5238 case NFA_BOW:
5239 case NFA_EOW:
5240 return 90;
5241
5242 case NFA_MOPEN:
5243 case NFA_MOPEN1:
5244 case NFA_MOPEN2:
5245 case NFA_MOPEN3:
5246 case NFA_MOPEN4:
5247 case NFA_MOPEN5:
5248 case NFA_MOPEN6:
5249 case NFA_MOPEN7:
5250 case NFA_MOPEN8:
5251 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005252#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005253 case NFA_ZOPEN:
5254 case NFA_ZOPEN1:
5255 case NFA_ZOPEN2:
5256 case NFA_ZOPEN3:
5257 case NFA_ZOPEN4:
5258 case NFA_ZOPEN5:
5259 case NFA_ZOPEN6:
5260 case NFA_ZOPEN7:
5261 case NFA_ZOPEN8:
5262 case NFA_ZOPEN9:
5263 case NFA_ZCLOSE:
5264 case NFA_ZCLOSE1:
5265 case NFA_ZCLOSE2:
5266 case NFA_ZCLOSE3:
5267 case NFA_ZCLOSE4:
5268 case NFA_ZCLOSE5:
5269 case NFA_ZCLOSE6:
5270 case NFA_ZCLOSE7:
5271 case NFA_ZCLOSE8:
5272 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005273#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005274 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005275 case NFA_MCLOSE1:
5276 case NFA_MCLOSE2:
5277 case NFA_MCLOSE3:
5278 case NFA_MCLOSE4:
5279 case NFA_MCLOSE5:
5280 case NFA_MCLOSE6:
5281 case NFA_MCLOSE7:
5282 case NFA_MCLOSE8:
5283 case NFA_MCLOSE9:
5284 case NFA_NCLOSE:
5285 return failure_chance(state->out, depth + 1);
5286
5287 case NFA_BACKREF1:
5288 case NFA_BACKREF2:
5289 case NFA_BACKREF3:
5290 case NFA_BACKREF4:
5291 case NFA_BACKREF5:
5292 case NFA_BACKREF6:
5293 case NFA_BACKREF7:
5294 case NFA_BACKREF8:
5295 case NFA_BACKREF9:
5296#ifdef FEAT_SYN_HL
5297 case NFA_ZREF1:
5298 case NFA_ZREF2:
5299 case NFA_ZREF3:
5300 case NFA_ZREF4:
5301 case NFA_ZREF5:
5302 case NFA_ZREF6:
5303 case NFA_ZREF7:
5304 case NFA_ZREF8:
5305 case NFA_ZREF9:
5306#endif
5307 /* backreferences don't match in many places */
5308 return 94;
5309
5310 case NFA_LNUM_GT:
5311 case NFA_LNUM_LT:
5312 case NFA_COL_GT:
5313 case NFA_COL_LT:
5314 case NFA_VCOL_GT:
5315 case NFA_VCOL_LT:
5316 case NFA_MARK_GT:
5317 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005318 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005319 /* before/after positions don't match very often */
5320 return 85;
5321
5322 case NFA_LNUM:
5323 return 90;
5324
5325 case NFA_CURSOR:
5326 case NFA_COL:
5327 case NFA_VCOL:
5328 case NFA_MARK:
5329 /* specific positions rarely match */
5330 return 98;
5331
5332 case NFA_COMPOSING:
5333 return 95;
5334
5335 default:
5336 if (c > 0)
5337 /* character match fails often */
5338 return 95;
5339 }
5340
5341 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005342 return 50;
5343}
5344
Bram Moolenaarf46da702013-06-02 22:37:42 +02005345/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005346 * Skip until the char "c" we know a match must start with.
5347 */
5348 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005349skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005350{
5351 char_u *s;
5352
5353 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005354 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005355 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005356 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005357 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005358 if (s == NULL)
5359 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005360 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005361 return OK;
5362}
5363
5364/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005365 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005366 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005367 * Returns zero for no match, 1 for a match.
5368 */
5369 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005370find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005371{
5372 colnr_T col = startcol;
5373 int c1, c2;
5374 int len1, len2;
5375 int match;
5376
5377 for (;;)
5378 {
5379 match = TRUE;
5380 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5381 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5382 {
5383 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005384 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005385 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005386 {
5387 match = FALSE;
5388 break;
5389 }
5390 len2 += MB_CHAR2LEN(c2);
5391 }
5392 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005393 /* check that no composing char follows */
5394 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005395 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005396 {
5397 cleanup_subexpr();
5398 if (REG_MULTI)
5399 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005400 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005401 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005402 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005403 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005404 }
5405 else
5406 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005407 rex.reg_startp[0] = rex.line + col;
5408 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005409 }
5410 return 1L;
5411 }
5412
5413 /* Try finding regstart after the current match. */
5414 col += MB_CHAR2LEN(regstart); /* skip regstart */
5415 if (skip_to_start(regstart, &col) == FAIL)
5416 break;
5417 }
5418 return 0L;
5419}
5420
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005421#ifdef FEAT_RELTIME
5422 static int
5423nfa_did_time_out()
5424{
5425 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5426 {
5427 if (nfa_timed_out != NULL)
5428 *nfa_timed_out = TRUE;
5429 return TRUE;
5430 }
5431 return FALSE;
5432}
5433#endif
5434
Bram Moolenaar473de612013-06-08 18:19:48 +02005435/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005436 * Main matching routine.
5437 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005438 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005439 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005440 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005441 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005442 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005443 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 * Note: Caller must ensure that: start != NULL.
5445 */
5446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005447nfa_regmatch(
5448 nfa_regprog_T *prog,
5449 nfa_state_T *start,
5450 regsubs_T *submatch,
5451 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005452{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005453 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005454 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005456 int go_to_nextline = FALSE;
5457 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005458 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005459 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005460 nfa_list_T *thislist;
5461 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005463 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005464 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005465 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005466 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005467 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005468 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005469#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005470 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005471#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005472
Bram Moolenaar41f12052013-08-25 17:01:42 +02005473 /* Some patterns may take a long time to match, especially when using
5474 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5475 fast_breakcheck();
5476 if (got_int)
5477 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005478#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005479 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005480 return FALSE;
5481#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005482
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005483#ifdef NFA_REGEXP_DEBUG_LOG
5484 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5485 if (debug == NULL)
5486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005487 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005488 return FALSE;
5489 }
5490#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005491 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005492
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005493 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005494 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005495
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005496 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005497 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005498 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005499 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005500 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005501 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005502
5503#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005504 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005505 if (log_fd != NULL)
5506 {
5507 fprintf(log_fd, "**********************************\n");
5508 nfa_set_code(start->c);
5509 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5510 abs(start->id), code);
5511 fprintf(log_fd, "**********************************\n");
5512 }
5513 else
5514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005515 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005516 log_fd = stderr;
5517 }
5518#endif
5519
5520 thislist = &list[0];
5521 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005522 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523 nextlist = &list[1];
5524 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005525 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005527 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005529 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005530
5531 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5532 * it's the first MOPEN. */
5533 if (toplevel)
5534 {
5535 if (REG_MULTI)
5536 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005537 m->norm.list.multi[0].start_lnum = rex.lnum;
5538 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005539 }
5540 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005541 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005542 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005543 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005544 }
5545 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005546 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005547
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005548#define ADD_STATE_IF_MATCH(state) \
5549 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005550 add_state = state->out; \
5551 add_off = clen; \
5552 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553
5554 /*
5555 * Run for each character.
5556 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005557 for (;;)
5558 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005559 int curc;
5560 int clen;
5561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562 if (has_mbyte)
5563 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005564 curc = (*mb_ptr2char)(rex.input);
5565 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005566 }
5567 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005568 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005569 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005570 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005571 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005572 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005573 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005574 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005575 go_to_nextline = FALSE;
5576 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577
5578 /* swap lists */
5579 thislist = &list[flag];
5580 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005581 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005582 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005583 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005584 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005585 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005586# ifdef FEAT_EVAL
5587 || nfa_fail_for_testing
5588# endif
5589 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005590 {
5591 /* too many states, retry with old engine */
5592 nfa_match = NFA_TOO_EXPENSIVE;
5593 goto theend;
5594 }
5595
Bram Moolenaar0270f382018-07-17 05:43:58 +02005596 thislist->id = rex.nfa_listid;
5597 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598
5599#ifdef ENABLE_LOG
5600 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005601 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005602 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005603 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005604 {
5605 int i;
5606
5607 for (i = 0; i < thislist->n; i++)
5608 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610 fprintf(log_fd, "\n");
5611#endif
5612
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005613#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614 fprintf(debug, "\n-------------------\n");
5615#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005616 /*
5617 * If the state lists are empty we can stop.
5618 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005619 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005620 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005621
5622 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005623 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005625 /* If the list gets very long there probably is something wrong.
5626 * At least allow interrupting with CTRL-C. */
5627 fast_breakcheck();
5628 if (got_int)
5629 break;
5630#ifdef FEAT_RELTIME
5631 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5632 {
5633 nfa_time_count = 0;
5634 if (nfa_did_time_out())
5635 break;
5636 }
5637#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005638 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005639
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005640#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641 nfa_set_code(t->state->c);
5642 fprintf(debug, "%s, ", code);
5643#endif
5644#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005645 {
5646 int col;
5647
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005648 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005649 col = -1;
5650 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005651 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005652 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005653 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005654 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005655 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005656 abs(t->state->id), (int)t->state->c, code, col,
5657 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005658 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005659#endif
5660
5661 /*
5662 * Handle the possible codes of the current state.
5663 * The most important is NFA_MATCH.
5664 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005665 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005666 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005667 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005668 switch (t->state->c)
5669 {
5670 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005671 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005672 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005673 * rex.reg_icombine is not set, that is not really a match. */
5674 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005675 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005676
Bram Moolenaar963fee22013-05-26 21:47:28 +02005677 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005678 copy_sub(&submatch->norm, &t->subs.norm);
5679#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005680 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005681 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005682#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005683#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005684 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005685#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005686 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005687 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005688 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005689 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005690 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005691 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005692 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005693 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005694
5695 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005696 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005697 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005698 /*
5699 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005700 * NFA_START_INVISIBLE_BEFORE node.
5701 * They surround a zero-width group, used with "\@=", "\&",
5702 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703 * If we got here, it means that the current "invisible" group
5704 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005705 * nfa_regmatch(). For a look-behind match only when it ends
5706 * in the position in "nfa_endp".
5707 * Submatches are stored in *m, and used in the parent call.
5708 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005709#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005710 if (nfa_endp != NULL)
5711 {
5712 if (REG_MULTI)
5713 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005714 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005715 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005716 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005717 nfa_endp->se_u.pos.col);
5718 else
5719 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005720 (int)(rex.input - rex.line),
5721 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005722 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005723#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005724 /* If "nfa_endp" is set it's only a match if it ends at
5725 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005726 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005727 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5728 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005729 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005730 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005731 break;
5732
5733 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005734 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005735 {
5736 copy_sub(&m->norm, &t->subs.norm);
5737#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005738 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005739 copy_sub(&m->synt, &t->subs.synt);
5740#endif
5741 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005742#ifdef ENABLE_LOG
5743 fprintf(log_fd, "Match found:\n");
5744 log_subsexpr(m);
5745#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005746 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005747 /* See comment above at "goto nextchar". */
5748 if (nextlist->n == 0)
5749 clen = 0;
5750 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005751
5752 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005753 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005754 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005755 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005756 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005757 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005758 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005759 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005760 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005761#ifdef ENABLE_LOG
5762 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5763 failure_chance(t->state->out, 0),
5764 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005765#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005766 /* Do it directly if there already is a PIM or when
5767 * nfa_postprocess() detected it will work better. */
5768 if (t->pim.result != NFA_PIM_UNUSED
5769 || t->state->c == NFA_START_INVISIBLE_FIRST
5770 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5771 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5772 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005773 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005774 int in_use = m->norm.in_use;
5775
Bram Moolenaar699c1202013-09-25 16:41:54 +02005776 /* Copy submatch info for the recursive call, opposite
5777 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005778 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005779#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005780 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005781 copy_sub_off(&m->synt, &t->subs.synt);
5782#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005783
Bram Moolenaara2d95102013-06-04 14:23:05 +02005784 /*
5785 * First try matching the invisible match, then what
5786 * follows.
5787 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005788 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005789 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005790 if (result == NFA_TOO_EXPENSIVE)
5791 {
5792 nfa_match = result;
5793 goto theend;
5794 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005795
Bram Moolenaardecd9542013-06-07 16:31:50 +02005796 /* for \@! and \@<! it is a match when the result is
5797 * FALSE */
5798 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005799 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5800 || t->state->c
5801 == NFA_START_INVISIBLE_BEFORE_NEG
5802 || t->state->c
5803 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005804 {
5805 /* Copy submatch info from the recursive call */
5806 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005807#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005808 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005809 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005810#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005811 /* If the pattern has \ze and it matched in the
5812 * sub pattern, use it. */
5813 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005814
Bram Moolenaara2d95102013-06-04 14:23:05 +02005815 /* t->state->out1 is the corresponding
5816 * END_INVISIBLE node; Add its out to the current
5817 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005818 add_here = TRUE;
5819 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005820 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005821 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005822 }
5823 else
5824 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005825 nfa_pim_T pim;
5826
Bram Moolenaara2d95102013-06-04 14:23:05 +02005827 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005828 * First try matching what follows. Only if a match
5829 * is found verify the invisible match matches. Add a
5830 * nfa_pim_T to the following states, it contains info
5831 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005832 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005833 pim.state = t->state;
5834 pim.result = NFA_PIM_TODO;
5835 pim.subs.norm.in_use = 0;
5836#ifdef FEAT_SYN_HL
5837 pim.subs.synt.in_use = 0;
5838#endif
5839 if (REG_MULTI)
5840 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005841 pim.end.pos.col = (int)(rex.input - rex.line);
5842 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005843 }
5844 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005845 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005846
5847 /* t->state->out1 is the corresponding END_INVISIBLE
5848 * node; Add its out to the current list (zero-width
5849 * match). */
5850 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005851 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005852 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005853 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005854 break;
5855
Bram Moolenaar87953742013-06-05 18:52:40 +02005856 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005857 {
5858 nfa_state_T *skip = NULL;
5859#ifdef ENABLE_LOG
5860 int skip_lid = 0;
5861#endif
5862
5863 /* There is no point in trying to match the pattern if the
5864 * output state is not going to be added to the list. */
5865 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5866 {
5867 skip = t->state->out1->out;
5868#ifdef ENABLE_LOG
5869 skip_lid = nextlist->id;
5870#endif
5871 }
5872 else if (state_in_list(nextlist,
5873 t->state->out1->out->out, &t->subs))
5874 {
5875 skip = t->state->out1->out->out;
5876#ifdef ENABLE_LOG
5877 skip_lid = nextlist->id;
5878#endif
5879 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005880 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005881 t->state->out1->out->out, &t->subs))
5882 {
5883 skip = t->state->out1->out->out;
5884#ifdef ENABLE_LOG
5885 skip_lid = thislist->id;
5886#endif
5887 }
5888 if (skip != NULL)
5889 {
5890#ifdef ENABLE_LOG
5891 nfa_set_code(skip->c);
5892 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5893 abs(skip->id), skip_lid, skip->c, code);
5894#endif
5895 break;
5896 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005897 /* Copy submatch info to the recursive call, opposite of what
5898 * happens afterwards. */
5899 copy_sub_off(&m->norm, &t->subs.norm);
5900#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005901 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005902 copy_sub_off(&m->synt, &t->subs.synt);
5903#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005904
Bram Moolenaar87953742013-06-05 18:52:40 +02005905 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005906 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005907 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005908 if (result == NFA_TOO_EXPENSIVE)
5909 {
5910 nfa_match = result;
5911 goto theend;
5912 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005913 if (result)
5914 {
5915 int bytelen;
5916
5917#ifdef ENABLE_LOG
5918 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5919 log_subsexpr(m);
5920#endif
5921 /* Copy submatch info from the recursive call */
5922 copy_sub_off(&t->subs.norm, &m->norm);
5923#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005924 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005925 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005926#endif
5927 /* Now we need to skip over the matched text and then
5928 * continue with what follows. */
5929 if (REG_MULTI)
5930 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005931 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02005932 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02005933 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005934 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02005935
5936#ifdef ENABLE_LOG
5937 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5938#endif
5939 if (bytelen == 0)
5940 {
5941 /* empty match, output of corresponding
5942 * NFA_END_PATTERN/NFA_SKIP to be used at current
5943 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005944 add_here = TRUE;
5945 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005946 }
5947 else if (bytelen <= clen)
5948 {
5949 /* match current character, output of corresponding
5950 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005951 add_state = t->state->out1->out->out;
5952 add_off = clen;
5953 }
5954 else
5955 {
5956 /* skip over the matched characters, set character
5957 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005958 add_state = t->state->out1->out;
5959 add_off = bytelen;
5960 add_count = bytelen - clen;
5961 }
5962 }
5963 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005964 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005965
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005966 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02005967 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005968 {
5969 add_here = TRUE;
5970 add_state = t->state->out;
5971 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005972 break;
5973
5974 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005975 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005976 {
5977 add_here = TRUE;
5978 add_state = t->state->out;
5979 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005980 break;
5981
5982 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005983 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005984
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005985 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005986 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005987 else if (has_mbyte)
5988 {
5989 int this_class;
5990
5991 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005992 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005993 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005994 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005995 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005996 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005997 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005998 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005999 || (rex.input > rex.line
6000 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006001 result = FALSE;
6002 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006003 {
6004 add_here = TRUE;
6005 add_state = t->state->out;
6006 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006007 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006008
6009 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006010 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006011 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006012 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006013 else if (has_mbyte)
6014 {
6015 int this_class, prev_class;
6016
6017 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006018 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006019 prev_class = reg_prev_class();
6020 if (this_class == prev_class
6021 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006022 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006023 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006024 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6025 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006026 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006027 result = FALSE;
6028 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006029 {
6030 add_here = TRUE;
6031 add_state = t->state->out;
6032 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006033 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006034
Bram Moolenaar4b780632013-05-31 22:14:52 +02006035 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006036 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006037 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006038 {
6039 add_here = TRUE;
6040 add_state = t->state->out;
6041 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006042 break;
6043
6044 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006045 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006046 {
6047 add_here = TRUE;
6048 add_state = t->state->out;
6049 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006050 break;
6051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006052 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006053 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006054 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006055 int len = 0;
6056 nfa_state_T *end;
6057 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006058 int cchars[MAX_MCO];
6059 int ccount = 0;
6060 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006061
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006062 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006063 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006064 if (utf_iscomposing(sta->c))
6065 {
6066 /* Only match composing character(s), ignore base
6067 * character. Used for ".{composing}" and "{composing}"
6068 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006069 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006070 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006071 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006072 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006073 /* If \Z was present, then ignore composing characters.
6074 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006075 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006076 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006077 else
6078 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006079 while (sta->c != NFA_END_COMPOSING)
6080 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006081 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006082
6083 /* Check base character matches first, unless ignored. */
6084 else if (len > 0 || mc == sta->c)
6085 {
6086 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006087 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006088 len += mb_char2len(mc);
6089 sta = sta->out;
6090 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006091
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006092 /* We don't care about the order of composing characters.
6093 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006094 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006095 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006096 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006097 cchars[ccount++] = mc;
6098 len += mb_char2len(mc);
6099 if (ccount == MAX_MCO)
6100 break;
6101 }
6102
6103 /* Check that each composing char in the pattern matches a
6104 * composing char in the text. We do not check if all
6105 * composing chars are matched. */
6106 result = OK;
6107 while (sta->c != NFA_END_COMPOSING)
6108 {
6109 for (j = 0; j < ccount; ++j)
6110 if (cchars[j] == sta->c)
6111 break;
6112 if (j == ccount)
6113 {
6114 result = FAIL;
6115 break;
6116 }
6117 sta = sta->out;
6118 }
6119 }
6120 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006121 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006122
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006123 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006124 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006126 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006127
6128 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006129 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006130 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006131 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006132 go_to_nextline = TRUE;
6133 /* Pass -1 for the offset, which means taking the position
6134 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006135 add_state = t->state->out;
6136 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006137 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006138 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006139 {
6140 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006141 add_state = t->state->out;
6142 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006143 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006144 break;
6145
Bram Moolenaar417bad22013-06-07 14:08:30 +02006146 case NFA_START_COLL:
6147 case NFA_START_NEG_COLL:
6148 {
6149 /* What follows is a list of characters, until NFA_END_COLL.
6150 * One of them must match or none of them must match. */
6151 nfa_state_T *state;
6152 int result_if_matched;
6153 int c1, c2;
6154
6155 /* Never match EOL. If it's part of the collection it is added
6156 * as a separate state with an OR. */
6157 if (curc == NUL)
6158 break;
6159
6160 state = t->state->out;
6161 result_if_matched = (t->state->c == NFA_START_COLL);
6162 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006163 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006164 if (state->c == NFA_END_COLL)
6165 {
6166 result = !result_if_matched;
6167 break;
6168 }
6169 if (state->c == NFA_RANGE_MIN)
6170 {
6171 c1 = state->val;
6172 state = state->out; /* advance to NFA_RANGE_MAX */
6173 c2 = state->val;
6174#ifdef ENABLE_LOG
6175 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6176 curc, c1, c2);
6177#endif
6178 if (curc >= c1 && curc <= c2)
6179 {
6180 result = result_if_matched;
6181 break;
6182 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006183 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006184 {
6185 int curc_low = MB_TOLOWER(curc);
6186 int done = FALSE;
6187
6188 for ( ; c1 <= c2; ++c1)
6189 if (MB_TOLOWER(c1) == curc_low)
6190 {
6191 result = result_if_matched;
6192 done = TRUE;
6193 break;
6194 }
6195 if (done)
6196 break;
6197 }
6198 }
6199 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006200 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006201 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006202 == MB_TOLOWER(state->c))))
6203 {
6204 result = result_if_matched;
6205 break;
6206 }
6207 state = state->out;
6208 }
6209 if (result)
6210 {
6211 /* next state is in out of the NFA_END_COLL, out1 of
6212 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006213 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006214 add_off = clen;
6215 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006216 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006217 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006218
6219 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006220 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006221 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006222 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006223 add_state = t->state->out;
6224 add_off = clen;
6225 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006226 break;
6227
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006228 case NFA_ANY_COMPOSING:
6229 /* On a composing character skip over it. Otherwise do
6230 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006231 if (enc_utf8 && utf_iscomposing(curc))
6232 {
6233 add_off = clen;
6234 }
6235 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006236 {
6237 add_here = TRUE;
6238 add_off = 0;
6239 }
6240 add_state = t->state->out;
6241 break;
6242
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006243 /*
6244 * Character classes like \a for alpha, \d for digit etc.
6245 */
6246 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006247 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006248 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006249 break;
6250
6251 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006252 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006253 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006254 break;
6255
6256 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006257 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006258 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006259 break;
6260
6261 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006262 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006263 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006264 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006265 break;
6266
6267 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006268 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006269 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006270 break;
6271
6272 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006273 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006274 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006275 break;
6276
6277 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006278 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006279 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006280 break;
6281
6282 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006283 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006284 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006285 break;
6286
6287 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006288 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006289 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006290 break;
6291
6292 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006293 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006294 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006295 break;
6296
6297 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006298 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006299 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006300 break;
6301
6302 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006303 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006304 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006305 break;
6306
6307 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006308 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006309 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006310 break;
6311
6312 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006313 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006314 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006315 break;
6316
6317 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006318 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006319 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006320 break;
6321
6322 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006323 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006324 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006325 break;
6326
6327 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006328 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006329 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006330 break;
6331
6332 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006333 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006334 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006335 break;
6336
6337 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006338 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006339 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006340 break;
6341
6342 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006343 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006344 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006345 break;
6346
6347 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006348 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006349 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006350 break;
6351
6352 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006353 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006354 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006355 break;
6356
6357 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006358 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006359 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006360 break;
6361
6362 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006363 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006364 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006365 break;
6366
6367 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006368 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006369 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006370 break;
6371
6372 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006373 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006374 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006375 break;
6376
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006377 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006378 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006379 ADD_STATE_IF_MATCH(t->state);
6380 break;
6381
6382 case NFA_NLOWER_IC: /* [^a-z] */
6383 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006384 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006385 ADD_STATE_IF_MATCH(t->state);
6386 break;
6387
6388 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006389 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006390 ADD_STATE_IF_MATCH(t->state);
6391 break;
6392
6393 case NFA_NUPPER_IC: /* ^[A-Z] */
6394 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006395 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006396 ADD_STATE_IF_MATCH(t->state);
6397 break;
6398
Bram Moolenaar5714b802013-05-28 22:03:20 +02006399 case NFA_BACKREF1:
6400 case NFA_BACKREF2:
6401 case NFA_BACKREF3:
6402 case NFA_BACKREF4:
6403 case NFA_BACKREF5:
6404 case NFA_BACKREF6:
6405 case NFA_BACKREF7:
6406 case NFA_BACKREF8:
6407 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006408#ifdef FEAT_SYN_HL
6409 case NFA_ZREF1:
6410 case NFA_ZREF2:
6411 case NFA_ZREF3:
6412 case NFA_ZREF4:
6413 case NFA_ZREF5:
6414 case NFA_ZREF6:
6415 case NFA_ZREF7:
6416 case NFA_ZREF8:
6417 case NFA_ZREF9:
6418#endif
6419 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006420 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006421 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006422 int bytelen;
6423
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006424 if (t->state->c <= NFA_BACKREF9)
6425 {
6426 subidx = t->state->c - NFA_BACKREF1 + 1;
6427 result = match_backref(&t->subs.norm, subidx, &bytelen);
6428 }
6429#ifdef FEAT_SYN_HL
6430 else
6431 {
6432 subidx = t->state->c - NFA_ZREF1 + 1;
6433 result = match_zref(subidx, &bytelen);
6434 }
6435#endif
6436
Bram Moolenaar5714b802013-05-28 22:03:20 +02006437 if (result)
6438 {
6439 if (bytelen == 0)
6440 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006441 /* empty match always works, output of NFA_SKIP to be
6442 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006443 add_here = TRUE;
6444 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006445 }
6446 else if (bytelen <= clen)
6447 {
6448 /* match current character, jump ahead to out of
6449 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006450 add_state = t->state->out->out;
6451 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006452 }
6453 else
6454 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006455 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006456 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006457 add_state = t->state->out;
6458 add_off = bytelen;
6459 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006460 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006461 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006462 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006463 }
6464 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006465 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006466 if (t->count - clen <= 0)
6467 {
6468 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006469 add_state = t->state->out;
6470 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006471 }
6472 else
6473 {
6474 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006475 add_state = t->state;
6476 add_off = 0;
6477 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006478 }
6479 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006480
Bram Moolenaar423532e2013-05-29 21:14:42 +02006481 case NFA_LNUM:
6482 case NFA_LNUM_GT:
6483 case NFA_LNUM_LT:
6484 result = (REG_MULTI &&
6485 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006486 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006487 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006488 {
6489 add_here = TRUE;
6490 add_state = t->state->out;
6491 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006492 break;
6493
6494 case NFA_COL:
6495 case NFA_COL_GT:
6496 case NFA_COL_LT:
6497 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006498 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006499 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006500 {
6501 add_here = TRUE;
6502 add_state = t->state->out;
6503 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006504 break;
6505
6506 case NFA_VCOL:
6507 case NFA_VCOL_GT:
6508 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006509 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006510 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006511 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006512 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006513
6514 /* Bail out quickly when there can't be a match, avoid the
6515 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006516 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006517 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006518 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006519 result = FALSE;
6520 if (op == 1 && col - 1 > t->state->val && col > 100)
6521 {
6522 int ts = wp->w_buffer->b_p_ts;
6523
6524 /* Guess that a character won't use more columns than
6525 * 'tabstop', with a minimum of 4. */
6526 if (ts < 4)
6527 ts = 4;
6528 result = col > t->state->val * ts;
6529 }
6530 if (!result)
6531 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006532 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006533 if (result)
6534 {
6535 add_here = TRUE;
6536 add_state = t->state->out;
6537 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006538 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006539 break;
6540
Bram Moolenaar044aa292013-06-04 21:27:38 +02006541 case NFA_MARK:
6542 case NFA_MARK_GT:
6543 case NFA_MARK_LT:
6544 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006545 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006546
6547 /* Compare the mark position to the match position. */
6548 result = (pos != NULL /* mark doesn't exist */
6549 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006550 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6551 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006552 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006553 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006554 ? t->state->c == NFA_MARK_GT
6555 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006556 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006557 ? t->state->c == NFA_MARK_GT
6558 : t->state->c == NFA_MARK_LT)));
6559 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006560 {
6561 add_here = TRUE;
6562 add_state = t->state->out;
6563 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006564 break;
6565 }
6566
Bram Moolenaar423532e2013-05-29 21:14:42 +02006567 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006568 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006569 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006570 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006571 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006572 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006573 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006574 {
6575 add_here = TRUE;
6576 add_state = t->state->out;
6577 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006578 break;
6579
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006580 case NFA_VISUAL:
6581 result = reg_match_visual();
6582 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006583 {
6584 add_here = TRUE;
6585 add_state = t->state->out;
6586 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006587 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006588
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006589 case NFA_MOPEN1:
6590 case NFA_MOPEN2:
6591 case NFA_MOPEN3:
6592 case NFA_MOPEN4:
6593 case NFA_MOPEN5:
6594 case NFA_MOPEN6:
6595 case NFA_MOPEN7:
6596 case NFA_MOPEN8:
6597 case NFA_MOPEN9:
6598#ifdef FEAT_SYN_HL
6599 case NFA_ZOPEN:
6600 case NFA_ZOPEN1:
6601 case NFA_ZOPEN2:
6602 case NFA_ZOPEN3:
6603 case NFA_ZOPEN4:
6604 case NFA_ZOPEN5:
6605 case NFA_ZOPEN6:
6606 case NFA_ZOPEN7:
6607 case NFA_ZOPEN8:
6608 case NFA_ZOPEN9:
6609#endif
6610 case NFA_NOPEN:
6611 case NFA_ZSTART:
6612 /* These states are only added to be able to bail out when
6613 * they are added again, nothing is to be done. */
6614 break;
6615
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006616 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006617 {
6618 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006619
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006620#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006621 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006622 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006623#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006624 result = (c == curc);
6625
Bram Moolenaar6100d022016-10-02 16:51:57 +02006626 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006627 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006628 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006629 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006630 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006631 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006632 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006633 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006634 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006635
6636 } /* switch (t->state->c) */
6637
6638 if (add_state != NULL)
6639 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006640 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006641 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006642
6643 if (t->pim.result == NFA_PIM_UNUSED)
6644 pim = NULL;
6645 else
6646 pim = &t->pim;
6647
6648 /* Handle the postponed invisible match if the match might end
6649 * without advancing and before the end of the line. */
6650 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006651 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006652 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006653 {
6654#ifdef ENABLE_LOG
6655 fprintf(log_fd, "\n");
6656 fprintf(log_fd, "==================================\n");
6657 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6658 fprintf(log_fd, "\n");
6659#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006660 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006661 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006662 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006663 /* for \@! and \@<! it is a match when the result is
6664 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006665 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006666 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6667 || pim->state->c
6668 == NFA_START_INVISIBLE_BEFORE_NEG
6669 || pim->state->c
6670 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006671 {
6672 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006673 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006674#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006675 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006676 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006677#endif
6678 }
6679 }
6680 else
6681 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006682 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006683#ifdef ENABLE_LOG
6684 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006685 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006686 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6687 fprintf(log_fd, "\n");
6688#endif
6689 }
6690
Bram Moolenaardecd9542013-06-07 16:31:50 +02006691 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006692 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006693 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6694 || pim->state->c
6695 == NFA_START_INVISIBLE_BEFORE_NEG
6696 || pim->state->c
6697 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006698 {
6699 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006700 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006701#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006702 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006703 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006704#endif
6705 }
6706 else
6707 /* look-behind match failed, don't add the state */
6708 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006709
6710 /* Postponed invisible match was handled, don't add it to
6711 * following states. */
6712 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006713 }
6714
Bram Moolenaara951e352013-10-06 15:46:11 +02006715 /* If "pim" points into l->t it will become invalid when
6716 * adding the state causes the list to be reallocated. Make a
6717 * local copy to avoid that. */
6718 if (pim == &t->pim)
6719 {
6720 copy_pim(&pim_copy, pim);
6721 pim = &pim_copy;
6722 }
6723
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006724 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006725 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006726 else
6727 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006728 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006729 if (add_count > 0)
6730 nextlist->t[nextlist->n - 1].count = add_count;
6731 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006732 }
6733
6734 } /* for (thislist = thislist; thislist->state; thislist++) */
6735
Bram Moolenaare23febd2013-05-26 18:40:14 +02006736 /* Look for the start of a match in the current position by adding the
6737 * start state to the list of states.
6738 * The first found match is the leftmost one, thus the order of states
6739 * matters!
6740 * Do not add the start state in recursive calls of nfa_regmatch(),
6741 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006742 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006743 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006744 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006745 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006746 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006747 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006748 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006749 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006750 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006751 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006752 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6753 || (rex.lnum == nfa_endp->se_u.pos.lnum
6754 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006755 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006756 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006757 {
6758#ifdef ENABLE_LOG
6759 fprintf(log_fd, "(---) STARTSTATE\n");
6760#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006761 /* Inline optimized code for addstate() if we know the state is
6762 * the first MOPEN. */
6763 if (toplevel)
6764 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006765 int add = TRUE;
6766 int c;
6767
6768 if (prog->regstart != NUL && clen != 0)
6769 {
6770 if (nextlist->n == 0)
6771 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006772 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006773
6774 /* Nextlist is empty, we can skip ahead to the
6775 * character that must appear at the start. */
6776 if (skip_to_start(prog->regstart, &col) == FAIL)
6777 break;
6778#ifdef ENABLE_LOG
6779 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006780 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006781#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006782 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006783 }
6784 else
6785 {
6786 /* Checking if the required start character matches is
6787 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006788 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006789 if (c != prog->regstart && (!rex.reg_ic
6790 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006791 {
6792#ifdef ENABLE_LOG
6793 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6794#endif
6795 add = FALSE;
6796 }
6797 }
6798 }
6799
6800 if (add)
6801 {
6802 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006803 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006804 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006805 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006806 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006807 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006808 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006809 }
6810 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006811 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006812 }
6813
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006814#ifdef ENABLE_LOG
6815 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006816 {
6817 int i;
6818
6819 for (i = 0; i < thislist->n; i++)
6820 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6821 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006822 fprintf(log_fd, "\n");
6823#endif
6824
6825nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006826 /* Advance to the next character, or advance to the next line, or
6827 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006828 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006829 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006830 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006831 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006832 reg_nextline();
6833 else
6834 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006835
6836 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006837 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006838 if (got_int)
6839 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006840#ifdef FEAT_RELTIME
6841 /* Check for timeout once in a twenty times to avoid overhead. */
6842 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6843 {
6844 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006845 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006846 break;
6847 }
6848#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006849 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006850
6851#ifdef ENABLE_LOG
6852 if (log_fd != stderr)
6853 fclose(log_fd);
6854 log_fd = NULL;
6855#endif
6856
6857theend:
6858 /* Free memory */
6859 vim_free(list[0].t);
6860 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006861 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006862#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006863#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006864 fclose(debug);
6865#endif
6866
Bram Moolenaar963fee22013-05-26 21:47:28 +02006867 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006868}
6869
6870/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006871 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006872 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006873 */
6874 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006875nfa_regtry(
6876 nfa_regprog_T *prog,
6877 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006878 proftime_T *tm UNUSED, /* timeout limit or NULL */
6879 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006880{
6881 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006882 regsubs_T subs, m;
6883 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006884 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006885#ifdef ENABLE_LOG
6886 FILE *f;
6887#endif
6888
Bram Moolenaar0270f382018-07-17 05:43:58 +02006889 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006890#ifdef FEAT_RELTIME
6891 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006892 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006893 nfa_time_count = 0;
6894#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006895
6896#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006897 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006898 if (f != NULL)
6899 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006900 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006901#ifdef DEBUG
6902 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6903#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006904 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006905 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006906 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006907 fprintf(f, "\n\n");
6908 fclose(f);
6909 }
6910 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006911 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006912#endif
6913
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006914 clear_sub(&subs.norm);
6915 clear_sub(&m.norm);
6916#ifdef FEAT_SYN_HL
6917 clear_sub(&subs.synt);
6918 clear_sub(&m.synt);
6919#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006920
Bram Moolenaarfda37292014-11-05 14:27:36 +01006921 result = nfa_regmatch(prog, start, &subs, &m);
6922 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006923 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006924 else if (result == NFA_TOO_EXPENSIVE)
6925 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006926
6927 cleanup_subexpr();
6928 if (REG_MULTI)
6929 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006930 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006931 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006932 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6933 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006934
Bram Moolenaar6100d022016-10-02 16:51:57 +02006935 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6936 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006937 }
6938
Bram Moolenaar6100d022016-10-02 16:51:57 +02006939 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006940 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006941 rex.reg_startpos[0].lnum = 0;
6942 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006943 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006944 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006945 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006946 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006947 rex.reg_endpos[0].lnum = rex.lnum;
6948 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006949 }
6950 else
6951 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006952 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953 }
6954 else
6955 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006956 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006957 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006958 rex.reg_startp[i] = subs.norm.list.line[i].start;
6959 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006960 }
6961
Bram Moolenaar6100d022016-10-02 16:51:57 +02006962 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006963 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006964 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006965 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006966 }
6967
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006968#ifdef FEAT_SYN_HL
6969 /* Package any found \z(...\) matches for export. Default is none. */
6970 unref_extmatch(re_extmatch_out);
6971 re_extmatch_out = NULL;
6972
6973 if (prog->reghasz == REX_SET)
6974 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006975 cleanup_zsubexpr();
6976 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01006977 /* Loop over \z1, \z2, etc. There is no \z0. */
6978 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006979 {
6980 if (REG_MULTI)
6981 {
6982 struct multipos *mpos = &subs.synt.list.multi[i];
6983
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006984 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006985 if (mpos->start_lnum >= 0
6986 && mpos->start_lnum == mpos->end_lnum
6987 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006988 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006989 vim_strnsave(reg_getline(mpos->start_lnum)
6990 + mpos->start_col,
6991 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006992 }
6993 else
6994 {
6995 struct linepos *lpos = &subs.synt.list.line[i];
6996
6997 if (lpos->start != NULL && lpos->end != NULL)
6998 re_extmatch_out->matches[i] =
6999 vim_strnsave(lpos->start,
7000 (int)(lpos->end - lpos->start));
7001 }
7002 }
7003 }
7004#endif
7005
Bram Moolenaar0270f382018-07-17 05:43:58 +02007006 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007007}
7008
7009/*
7010 * Match a regexp against a string ("line" points to the string) or multiple
7011 * lines ("line" is NULL, use reg_getline()).
7012 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007013 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 */
7015 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007016nfa_regexec_both(
7017 char_u *line,
7018 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007019 proftime_T *tm, /* timeout limit or NULL */
7020 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007021{
7022 nfa_regprog_T *prog;
7023 long retval = 0L;
7024 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007025 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007026
7027 if (REG_MULTI)
7028 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007029 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007031 rex.reg_startpos = rex.reg_mmatch->startpos;
7032 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033 }
7034 else
7035 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007036 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7037 rex.reg_startp = rex.reg_match->startp;
7038 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007039 }
7040
7041 /* Be paranoid... */
7042 if (prog == NULL || line == NULL)
7043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007044 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045 goto theend;
7046 }
7047
Bram Moolenaar6100d022016-10-02 16:51:57 +02007048 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007049 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007050 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007051 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007052 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007053
Bram Moolenaar6100d022016-10-02 16:51:57 +02007054 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007055 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007056 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007057
Bram Moolenaar0270f382018-07-17 05:43:58 +02007058 rex.line = line;
7059 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007060
Bram Moolenaar0270f382018-07-17 05:43:58 +02007061 rex.nfa_has_zend = prog->has_zend;
7062 rex.nfa_has_backref = prog->has_backref;
7063 rex.nfa_nsubexpr = prog->nsubexp;
7064 rex.nfa_listid = 1;
7065 rex.nfa_alt_listid = 2;
7066#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007067 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007068#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007069
Bram Moolenaard89616e2013-06-06 18:46:06 +02007070 if (prog->reganch && col > 0)
7071 return 0L;
7072
Bram Moolenaar0270f382018-07-17 05:43:58 +02007073 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007074#ifdef FEAT_SYN_HL
7075 /* Clear the external match subpointers if necessary. */
7076 if (prog->reghasz == REX_SET)
7077 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007078 rex.nfa_has_zsubexpr = TRUE;
7079 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007080 }
7081 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007082 {
7083 rex.nfa_has_zsubexpr = FALSE;
7084 rex.need_clear_zsubexpr = FALSE;
7085 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007086#endif
7087
Bram Moolenaard89616e2013-06-06 18:46:06 +02007088 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007089 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007090 /* Skip ahead until a character we know the match must start with.
7091 * When there is none there is no match. */
7092 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007093 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007094
Bram Moolenaar473de612013-06-08 18:19:48 +02007095 /* If match_text is set it contains the full text that must match.
7096 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007097 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007098 return find_match_text(col, prog->regstart, prog->match_text);
7099 }
7100
Bram Moolenaard89616e2013-06-06 18:46:06 +02007101 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007102 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007103 goto theend;
7104
Bram Moolenaar0270f382018-07-17 05:43:58 +02007105 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7106 // it's accidentally used during execution.
7107 nstate = 0;
7108 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007109 {
7110 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007111 prog->state[i].lastlist[0] = 0;
7112 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113 }
7114
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007115 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007116
Bram Moolenaar0270f382018-07-17 05:43:58 +02007117#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007118 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007119#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007120
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121theend:
7122 return retval;
7123}
7124
7125/*
7126 * Compile a regular expression into internal code for the NFA matcher.
7127 * Returns the program in allocated space. Returns NULL for an error.
7128 */
7129 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007130nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007131{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007132 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007133 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007134 int *postfix;
7135
7136 if (expr == NULL)
7137 return NULL;
7138
Bram Moolenaar0270f382018-07-17 05:43:58 +02007139#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007141#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007142 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007143
7144 init_class_tab();
7145
7146 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7147 return NULL;
7148
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007150 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151 postfix = re2post();
7152 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007153 {
7154 /* TODO: only give this error for debugging? */
7155 if (post_ptr >= post_end)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007156 siemsg("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007157 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007158 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007159
7160 /*
7161 * In order to build the NFA, we parse the input regexp twice:
7162 * 1. first pass to count size (so we can allocate space)
7163 * 2. second to emit code
7164 */
7165#ifdef ENABLE_LOG
7166 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007167 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168
7169 if (f != NULL)
7170 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007171 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007172 fclose(f);
7173 }
7174 }
7175#endif
7176
7177 /*
7178 * PASS 1
7179 * Count number of NFA states in "nstate". Do not build the NFA.
7180 */
7181 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007182
Bram Moolenaar16619a22013-06-11 18:42:36 +02007183 /* allocate the regprog with space for the compiled regexp */
7184 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007185 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7186 if (prog == NULL)
7187 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007188 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007189 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007190
7191 /*
7192 * PASS 2
7193 * Build the NFA
7194 */
7195 prog->start = post2nfa(postfix, post_ptr, FALSE);
7196 if (prog->start == NULL)
7197 goto fail;
7198
7199 prog->regflags = regflags;
7200 prog->engine = &nfa_regengine;
7201 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007202 prog->has_zend = rex.nfa_has_zend;
7203 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007204 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007205
Bram Moolenaara2947e22013-06-11 22:44:09 +02007206 nfa_postprocess(prog);
7207
Bram Moolenaard89616e2013-06-06 18:46:06 +02007208 prog->reganch = nfa_get_reganch(prog->start, 0);
7209 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007210 prog->match_text = nfa_get_match_text(prog->start);
7211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007212#ifdef ENABLE_LOG
7213 nfa_postfix_dump(expr, OK);
7214 nfa_dump(prog);
7215#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007216#ifdef FEAT_SYN_HL
7217 /* Remember whether this pattern has any \z specials in it. */
7218 prog->reghasz = re_has_z;
7219#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007220 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007221#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007222 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007223#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007224
7225out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007226 VIM_CLEAR(post_start);
7227 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228 state_ptr = NULL;
7229 return (regprog_T *)prog;
7230
7231fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007232 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007233#ifdef ENABLE_LOG
7234 nfa_postfix_dump(expr, FAIL);
7235#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007236#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007237 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007238#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007239 goto out;
7240}
7241
Bram Moolenaar473de612013-06-08 18:19:48 +02007242/*
7243 * Free a compiled regexp program, returned by nfa_regcomp().
7244 */
7245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007246nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007247{
7248 if (prog != NULL)
7249 {
7250 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007251 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007252 vim_free(prog);
7253 }
7254}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007255
7256/*
7257 * Match a regexp against a string.
7258 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7259 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007260 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007261 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007262 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007263 */
7264 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007265nfa_regexec_nl(
7266 regmatch_T *rmp,
7267 char_u *line, /* string to match against */
7268 colnr_T col, /* column to start looking for match */
7269 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007270{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007271 rex.reg_match = rmp;
7272 rex.reg_mmatch = NULL;
7273 rex.reg_maxline = 0;
7274 rex.reg_line_lbr = line_lbr;
7275 rex.reg_buf = curbuf;
7276 rex.reg_win = NULL;
7277 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007278 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007279 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007280 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007281}
7282
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007283
7284/*
7285 * Match a regexp against multiple lines.
7286 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7287 * Uses curbuf for line count and 'iskeyword'.
7288 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007289 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007290 * match otherwise.
7291 *
7292 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7293 *
7294 * ! Also NOTE : match may actually be in another line. e.g.:
7295 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7296 *
7297 * +-------------------------+
7298 * |a |
7299 * |b |
7300 * |c |
7301 * | |
7302 * +-------------------------+
7303 *
7304 * then nfa_regexec_multi() returns 3. while the original
7305 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7306 *
7307 * FIXME if this behavior is not compatible.
7308 */
7309 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007310nfa_regexec_multi(
7311 regmmatch_T *rmp,
7312 win_T *win, /* window in which to search or NULL */
7313 buf_T *buf, /* buffer in which to search */
7314 linenr_T lnum, /* nr of line to start looking for match */
7315 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007316 proftime_T *tm, /* timeout limit or NULL */
7317 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007318{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007319 rex.reg_match = NULL;
7320 rex.reg_mmatch = rmp;
7321 rex.reg_buf = buf;
7322 rex.reg_win = win;
7323 rex.reg_firstlnum = lnum;
7324 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7325 rex.reg_line_lbr = FALSE;
7326 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007327 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007328 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007329
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007330 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007331}
7332
7333#ifdef DEBUG
7334# undef ENABLE_LOG
7335#endif