blob: 15547bbdf9c1f453585091256e87681a99ef6ca2 [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 {
490#ifdef FEAT_MBYTE
491 if (has_mbyte)
492 s += (*mb_char2bytes)(p->c, s);
493 else
494#endif
495 *s++ = p->c;
496 p = p->out;
497 }
498 *s = NUL;
499 }
500 return ret;
501}
502
503/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200504 * Allocate more space for post_start. Called when
505 * running above the estimated number of states.
506 */
507 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100508realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200509{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200510 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200511 int new_max = nstate_max + 1000;
512 int *new_start;
513 int *old_start;
514
515 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
516 if (new_start == NULL)
517 return FAIL;
518 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200519 old_start = post_start;
520 post_start = new_start;
521 post_ptr = new_start + (post_ptr - old_start);
522 post_end = post_start + new_max;
523 vim_free(old_start);
524 return OK;
525}
526
527/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200528 * Search between "start" and "end" and try to recognize a
529 * character class in expanded form. For example [0-9].
530 * On success, return the id the character class to be emitted.
531 * On failure, return 0 (=FAIL)
532 * Start points to the first char of the range, while end should point
533 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200534 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
535 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200536 */
537 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100538nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200539{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200540# define CLASS_not 0x80
541# define CLASS_af 0x40
542# define CLASS_AF 0x20
543# define CLASS_az 0x10
544# define CLASS_AZ 0x08
545# define CLASS_o7 0x04
546# define CLASS_o9 0x02
547# define CLASS_underscore 0x01
548
549 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200550 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200551 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200552
553 if (extra_newl == TRUE)
554 newl = TRUE;
555
556 if (*end != ']')
557 return FAIL;
558 p = start;
559 if (*p == '^')
560 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200561 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200562 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200563 }
564
565 while (p < end)
566 {
567 if (p + 2 < end && *(p + 1) == '-')
568 {
569 switch (*p)
570 {
571 case '0':
572 if (*(p + 2) == '9')
573 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200574 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200575 break;
576 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200577 if (*(p + 2) == '7')
578 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200579 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200580 break;
581 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200582 return FAIL;
583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200584 case 'a':
585 if (*(p + 2) == 'z')
586 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200587 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200588 break;
589 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 if (*(p + 2) == 'f')
591 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200592 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593 break;
594 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200595 return FAIL;
596
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 case 'A':
598 if (*(p + 2) == 'Z')
599 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200600 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200601 break;
602 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200603 if (*(p + 2) == 'F')
604 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200605 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200606 break;
607 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200608 return FAIL;
609
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 default:
611 return FAIL;
612 }
613 p += 3;
614 }
615 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
616 {
617 newl = TRUE;
618 p += 2;
619 }
620 else if (*p == '_')
621 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200622 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200623 p ++;
624 }
625 else if (*p == '\n')
626 {
627 newl = TRUE;
628 p ++;
629 }
630 else
631 return FAIL;
632 } /* while (p < end) */
633
634 if (p != end)
635 return FAIL;
636
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200637 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200638 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200639
640 switch (config)
641 {
642 case CLASS_o9:
643 return extra_newl + NFA_DIGIT;
644 case CLASS_not | CLASS_o9:
645 return extra_newl + NFA_NDIGIT;
646 case CLASS_af | CLASS_AF | CLASS_o9:
647 return extra_newl + NFA_HEX;
648 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
649 return extra_newl + NFA_NHEX;
650 case CLASS_o7:
651 return extra_newl + NFA_OCTAL;
652 case CLASS_not | CLASS_o7:
653 return extra_newl + NFA_NOCTAL;
654 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
655 return extra_newl + NFA_WORD;
656 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
657 return extra_newl + NFA_NWORD;
658 case CLASS_az | CLASS_AZ | CLASS_underscore:
659 return extra_newl + NFA_HEAD;
660 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
661 return extra_newl + NFA_NHEAD;
662 case CLASS_az | CLASS_AZ:
663 return extra_newl + NFA_ALPHA;
664 case CLASS_not | CLASS_az | CLASS_AZ:
665 return extra_newl + NFA_NALPHA;
666 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200667 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200668 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200669 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200670 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200671 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200672 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200673 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200674 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200676}
677
678/*
679 * Produce the bytes for equivalence class "c".
680 * Currently only handles latin1, latin9 and utf-8.
681 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
682 * equivalent to 'a OR b OR c'
683 *
684 * NOTE! When changing this function, also update reg_equi_class()
685 */
686 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100687nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200688{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200689#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
690#ifdef FEAT_MBYTE
691# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
692#else
693# define EMITMBC(c)
694#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695
696#ifdef FEAT_MBYTE
697 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
698 || STRCMP(p_enc, "iso-8859-15") == 0)
699#endif
700 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200701#ifdef EBCDIC
702# define A_circumflex 0x62
703# define A_diaeresis 0x63
704# define A_grave 0x64
705# define A_acute 0x65
706# define A_virguilla 0x66
707# define A_ring 0x67
708# define C_cedilla 0x68
709# define E_acute 0x71
710# define E_circumflex 0x72
711# define E_diaeresis 0x73
712# define E_grave 0x74
713# define I_acute 0x75
714# define I_circumflex 0x76
715# define I_diaeresis 0x77
716# define I_grave 0x78
717# define N_virguilla 0x69
718# define O_circumflex 0xeb
719# define O_diaeresis 0xec
720# define O_grave 0xed
721# define O_acute 0xee
722# define O_virguilla 0xef
723# define O_slash 0x80
724# define U_circumflex 0xfb
725# define U_diaeresis 0xfc
726# define U_grave 0xfd
727# define U_acute 0xfe
728# define Y_acute 0xba
729# define a_grave 0x42
730# define a_acute 0x43
731# define a_circumflex 0x44
732# define a_virguilla 0x45
733# define a_diaeresis 0x46
734# define a_ring 0x47
735# define c_cedilla 0x48
736# define e_grave 0x51
737# define e_acute 0x52
738# define e_circumflex 0x53
739# define e_diaeresis 0x54
740# define i_grave 0x55
741# define i_acute 0x56
742# define i_circumflex 0x57
743# define i_diaeresis 0x58
744# define n_virguilla 0x49
745# define o_grave 0xcb
746# define o_acute 0xcc
747# define o_circumflex 0xcd
748# define o_virguilla 0xce
749# define o_diaeresis 0xcf
750# define o_slash 0x70
751# define u_grave 0xdb
752# define u_acute 0xdc
753# define u_circumflex 0xdd
754# define u_diaeresis 0xde
755# define y_acute 0x8d
756# define y_diaeresis 0xdf
757#else
758# define A_grave 0xc0
759# define A_acute 0xc1
760# define A_circumflex 0xc2
761# define A_virguilla 0xc3
762# define A_diaeresis 0xc4
763# define A_ring 0xc5
764# define C_cedilla 0xc7
765# define E_grave 0xc8
766# define E_acute 0xc9
767# define E_circumflex 0xca
768# define E_diaeresis 0xcb
769# define I_grave 0xcc
770# define I_acute 0xcd
771# define I_circumflex 0xce
772# define I_diaeresis 0xcf
773# define N_virguilla 0xd1
774# define O_grave 0xd2
775# define O_acute 0xd3
776# define O_circumflex 0xd4
777# define O_virguilla 0xd5
778# define O_diaeresis 0xd6
779# define O_slash 0xd8
780# define U_grave 0xd9
781# define U_acute 0xda
782# define U_circumflex 0xdb
783# define U_diaeresis 0xdc
784# define Y_acute 0xdd
785# define a_grave 0xe0
786# define a_acute 0xe1
787# define a_circumflex 0xe2
788# define a_virguilla 0xe3
789# define a_diaeresis 0xe4
790# define a_ring 0xe5
791# define c_cedilla 0xe7
792# define e_grave 0xe8
793# define e_acute 0xe9
794# define e_circumflex 0xea
795# define e_diaeresis 0xeb
796# define i_grave 0xec
797# define i_acute 0xed
798# define i_circumflex 0xee
799# define i_diaeresis 0xef
800# define n_virguilla 0xf1
801# define o_grave 0xf2
802# define o_acute 0xf3
803# define o_circumflex 0xf4
804# define o_virguilla 0xf5
805# define o_diaeresis 0xf6
806# define o_slash 0xf8
807# define u_grave 0xf9
808# define u_acute 0xfa
809# define u_circumflex 0xfb
810# define u_diaeresis 0xfc
811# define y_acute 0xfd
812# define y_diaeresis 0xff
813#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 switch (c)
815 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200816 case 'A': case A_grave: case A_acute: case A_circumflex:
817 case A_virguilla: case A_diaeresis: case A_ring:
818 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
819 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
820 CASEMBC(0x1ea2)
821 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
822 EMIT2(A_circumflex); EMIT2(A_virguilla);
823 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200824 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
825 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
826 EMITMBC(0x1ea2)
827 return OK;
828
829 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
830 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200831 return OK;
832
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200833 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
834 CASEMBC(0x10a) CASEMBC(0x10c)
835 EMIT2('C'); EMIT2(C_cedilla);
836 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200837 EMITMBC(0x10a) EMITMBC(0x10c)
838 return OK;
839
840 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200841 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200842 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
843 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844 return OK;
845
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200846 case 'E': case E_grave: case E_acute: case E_circumflex:
847 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
848 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
849 CASEMBC(0x1eba) CASEMBC(0x1ebc)
850 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
851 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200852 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
853 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
854 EMITMBC(0x1ebc)
855 return OK;
856
857 case 'F': CASEMBC(0x1e1e)
858 EMIT2('F'); EMITMBC(0x1e1e)
859 return OK;
860
861 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200862 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
863 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200864 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
865 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
866 EMITMBC(0x1f4) EMITMBC(0x1e20)
867 return OK;
868
869 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200871 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
872 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200873 return OK;
874
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200875 case 'I': case I_grave: case I_acute: case I_circumflex:
876 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
877 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
878 CASEMBC(0x1cf) CASEMBC(0x1ec8)
879 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
880 EMIT2(I_circumflex); EMIT2(I_diaeresis);
881 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200882 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
883 EMITMBC(0x1cf) EMITMBC(0x1ec8)
884 return OK;
885
886 case 'J': CASEMBC(0x134)
887 EMIT2('J'); EMITMBC(0x134)
888 return OK;
889
890 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200891 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200892 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
893 EMITMBC(0x1e34)
894 return OK;
895
896 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200897 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200898 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
899 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
900 return OK;
901
902 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
903 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200904 return OK;
905
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200906 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
907 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
908 EMIT2('N'); EMIT2(N_virguilla);
909 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200910 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200911 return OK;
912
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200913 case 'O': case O_grave: case O_acute: case O_circumflex:
914 case O_virguilla: case O_diaeresis: case O_slash:
915 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
916 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
917 CASEMBC(0x1ec) CASEMBC(0x1ece)
918 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
919 EMIT2(O_circumflex); EMIT2(O_virguilla);
920 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200921 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
922 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
923 EMITMBC(0x1ec) EMITMBC(0x1ece)
924 return OK;
925
926 case 'P': case 0x1e54: case 0x1e56:
927 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
928 return OK;
929
930 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200931 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200932 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
933 EMITMBC(0x1e58) EMITMBC(0x1e5e)
934 return OK;
935
936 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200937 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200938 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
939 EMITMBC(0x160) EMITMBC(0x1e60)
940 return OK;
941
942 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200943 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200944 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
945 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200946 return OK;
947
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200948 case 'U': case U_grave: case U_acute: case U_diaeresis:
949 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
950 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
951 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
952 CASEMBC(0x1ee6)
953 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
954 EMIT2(U_diaeresis); EMIT2(U_circumflex);
955 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200956 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
957 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
958 EMITMBC(0x1ee6)
959 return OK;
960
961 case 'V': CASEMBC(0x1e7c)
962 EMIT2('V'); EMITMBC(0x1e7c)
963 return OK;
964
965 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200966 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200967 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
968 EMITMBC(0x1e84) EMITMBC(0x1e86)
969 return OK;
970
971 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
972 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200973 return OK;
974
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200975 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
976 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
977 CASEMBC(0x1ef8)
978 EMIT2('Y'); EMIT2(Y_acute);
979 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200980 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
981 EMITMBC(0x1ef8)
982 return OK;
983
984 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200986 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
987 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200988 return OK;
989
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200990 case 'a': case a_grave: case a_acute: case a_circumflex:
991 case a_virguilla: case a_diaeresis: case a_ring:
992 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
993 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
994 CASEMBC(0x1ea3)
995 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
996 EMIT2(a_circumflex); EMIT2(a_virguilla);
997 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200998 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
999 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1000 EMITMBC(0x1ea3)
1001 return OK;
1002
1003 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1004 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001005 return OK;
1006
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001007 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1008 CASEMBC(0x10b) CASEMBC(0x10d)
1009 EMIT2('c'); EMIT2(c_cedilla);
1010 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001011 EMITMBC(0x10b) EMITMBC(0x10d)
1012 return OK;
1013
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001014 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001015 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001016 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1017 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001018 return OK;
1019
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001020 case 'e': case e_grave: case e_acute: case e_circumflex:
1021 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1022 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1023 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1024 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1025 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1026 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001027 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1028 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1029 return OK;
1030
1031 case 'f': CASEMBC(0x1e1f)
1032 EMIT2('f'); EMITMBC(0x1e1f)
1033 return OK;
1034
1035 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001036 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1037 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001038 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1039 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1040 EMITMBC(0x1f5) EMITMBC(0x1e21)
1041 return OK;
1042
1043 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001045 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1046 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001047 return OK;
1048
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001049 case 'i': case i_grave: case i_acute: case i_circumflex:
1050 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1051 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1052 CASEMBC(0x1ec9)
1053 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1054 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1055 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001056 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1057 EMITMBC(0x1ec9)
1058 return OK;
1059
1060 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1061 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1062 return OK;
1063
1064 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001065 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001066 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1067 EMITMBC(0x1e35)
1068 return OK;
1069
1070 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001071 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001072 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1073 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1074 return OK;
1075
1076 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1077 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001078 return OK;
1079
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001080 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1081 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1082 CASEMBC(0x1e49)
1083 EMIT2('n'); EMIT2(n_virguilla);
1084 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001085 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1086 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001087 return OK;
1088
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001089 case 'o': case o_grave: case o_acute: case o_circumflex:
1090 case o_virguilla: case o_diaeresis: case o_slash:
1091 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1092 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1093 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1094 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1095 EMIT2(o_circumflex); EMIT2(o_virguilla);
1096 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001097 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1098 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1099 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1100 return OK;
1101
1102 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1103 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1104 return OK;
1105
1106 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001107 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001108 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1109 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1110 return OK;
1111
1112 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001113 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001114 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1115 EMITMBC(0x161) EMITMBC(0x1e61)
1116 return OK;
1117
1118 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1121 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001122 return OK;
1123
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001124 case 'u': case u_grave: case u_acute: case u_circumflex:
1125 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1126 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1127 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1128 CASEMBC(0x1ee7)
1129 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1130 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1131 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001132 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1133 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1134 EMITMBC(0x1ee7)
1135 return OK;
1136
1137 case 'v': CASEMBC(0x1e7d)
1138 EMIT2('v'); EMITMBC(0x1e7d)
1139 return OK;
1140
1141 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001142 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001143 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1144 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1145 return OK;
1146
1147 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1148 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001149 return OK;
1150
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001151 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1152 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1153 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1154 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1155 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001156 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1157 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001158 return OK;
1159
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001160 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001161 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1163 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1164 return OK;
1165
1166 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001167 }
1168 }
1169
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001170 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001171 return OK;
1172#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001173#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001174}
1175
1176/*
1177 * Code to parse regular expression.
1178 *
1179 * We try to reuse parsing functions in regexp.c to
1180 * minimize surprise and keep the syntax consistent.
1181 */
1182
1183/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001184 * Parse the lowest level.
1185 *
1186 * An atom can be one of a long list of items. Many atoms match one character
1187 * in the text. It is often an ordinary character or a character class.
1188 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1189 * is only for syntax highlighting.
1190 *
1191 * atom ::= ordinary-atom
1192 * or \( pattern \)
1193 * or \%( pattern \)
1194 * or \z( pattern \)
1195 */
1196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001197nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001198{
1199 int c;
1200 int charclass;
1201 int equiclass;
1202 int collclass;
1203 int got_coll_char;
1204 char_u *p;
1205 char_u *endp;
1206#ifdef FEAT_MBYTE
1207 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208#endif
1209 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210 int emit_range;
1211 int negated;
1212 int result;
1213 int startc = -1;
1214 int endc = -1;
1215 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001216 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001218 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219 switch (c)
1220 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001221 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001222 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001223
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001224 case Magic('^'):
1225 EMIT(NFA_BOL);
1226 break;
1227
1228 case Magic('$'):
1229 EMIT(NFA_EOL);
1230#if defined(FEAT_SYN_HL) || defined(PROTO)
1231 had_eol = TRUE;
1232#endif
1233 break;
1234
1235 case Magic('<'):
1236 EMIT(NFA_BOW);
1237 break;
1238
1239 case Magic('>'):
1240 EMIT(NFA_EOW);
1241 break;
1242
1243 case Magic('_'):
1244 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001245 if (c == NUL)
1246 EMSG_RET_FAIL(_(e_nul_found));
1247
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001248 if (c == '^') /* "\_^" is start-of-line */
1249 {
1250 EMIT(NFA_BOL);
1251 break;
1252 }
1253 if (c == '$') /* "\_$" is end-of-line */
1254 {
1255 EMIT(NFA_EOL);
1256#if defined(FEAT_SYN_HL) || defined(PROTO)
1257 had_eol = TRUE;
1258#endif
1259 break;
1260 }
1261
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001262 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
1264 /* "\_[" is collection plus newline */
1265 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001266 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267
1268 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001269 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001270
1271 /*
1272 * Character classes.
1273 */
1274 case Magic('.'):
1275 case Magic('i'):
1276 case Magic('I'):
1277 case Magic('k'):
1278 case Magic('K'):
1279 case Magic('f'):
1280 case Magic('F'):
1281 case Magic('p'):
1282 case Magic('P'):
1283 case Magic('s'):
1284 case Magic('S'):
1285 case Magic('d'):
1286 case Magic('D'):
1287 case Magic('x'):
1288 case Magic('X'):
1289 case Magic('o'):
1290 case Magic('O'):
1291 case Magic('w'):
1292 case Magic('W'):
1293 case Magic('h'):
1294 case Magic('H'):
1295 case Magic('a'):
1296 case Magic('A'):
1297 case Magic('l'):
1298 case Magic('L'):
1299 case Magic('u'):
1300 case Magic('U'):
1301 p = vim_strchr(classchars, no_Magic(c));
1302 if (p == NULL)
1303 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001304 if (extra == NFA_ADD_NL)
1305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001306 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001307 rc_did_emsg = TRUE;
1308 return FAIL;
1309 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 siemsg("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001311 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312 }
1313#ifdef FEAT_MBYTE
1314 /* When '.' is followed by a composing char ignore the dot, so that
1315 * the composing char is matched here. */
1316 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1317 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001318 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 c = getchr();
1320 goto nfa_do_multibyte;
1321 }
1322#endif
1323 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001324 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001325 {
1326 EMIT(NFA_NEWL);
1327 EMIT(NFA_OR);
1328 regflags |= RF_HASNL;
1329 }
1330 break;
1331
1332 case Magic('n'):
1333 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001334 /* In a string "\n" matches a newline character. */
1335 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 else
1337 {
1338 /* In buffer text "\n" matches the end of a line. */
1339 EMIT(NFA_NEWL);
1340 regflags |= RF_HASNL;
1341 }
1342 break;
1343
1344 case Magic('('):
1345 if (nfa_reg(REG_PAREN) == FAIL)
1346 return FAIL; /* cascaded error */
1347 break;
1348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 case Magic('|'):
1350 case Magic('&'):
1351 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353 return FAIL;
1354
1355 case Magic('='):
1356 case Magic('?'):
1357 case Magic('+'):
1358 case Magic('@'):
1359 case Magic('*'):
1360 case Magic('{'):
1361 /* these should follow an atom, not form an atom */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001363 return FAIL;
1364
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001365 case Magic('~'):
1366 {
1367 char_u *lp;
1368
1369 /* Previous substitute pattern.
1370 * Generated as "\%(pattern\)". */
1371 if (reg_prev_sub == NULL)
1372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001373 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001374 return FAIL;
1375 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001376 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001377 {
1378 EMIT(PTR2CHAR(lp));
1379 if (lp != reg_prev_sub)
1380 EMIT(NFA_CONCAT);
1381 }
1382 EMIT(NFA_NOPEN);
1383 break;
1384 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001385
Bram Moolenaar428e9872013-05-30 17:05:39 +02001386 case Magic('1'):
1387 case Magic('2'):
1388 case Magic('3'):
1389 case Magic('4'):
1390 case Magic('5'):
1391 case Magic('6'):
1392 case Magic('7'):
1393 case Magic('8'):
1394 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001395 {
1396 int refnum = no_Magic(c) - '1';
1397
1398 if (!seen_endbrace(refnum + 1))
1399 return FAIL;
1400 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001401 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001402 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001403 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001404
1405 case Magic('z'):
1406 c = no_Magic(getchr());
1407 switch (c)
1408 {
1409 case 's':
1410 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001411 if (re_mult_next("\\zs") == FAIL)
1412 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 break;
1414 case 'e':
1415 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001416 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001417 if (re_mult_next("\\ze") == FAIL)
1418 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001419 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001420#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001421 case '1':
1422 case '2':
1423 case '3':
1424 case '4':
1425 case '5':
1426 case '6':
1427 case '7':
1428 case '8':
1429 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001430 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001431 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001432 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001433 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar0270f382018-07-17 05:43:58 +02001434 /* No need to set rex.nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001435 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001436 re_has_z = REX_USE;
1437 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001438 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001439 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001440 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001441 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001442 if (nfa_reg(REG_ZPAREN) == FAIL)
1443 return FAIL; /* cascaded error */
1444 re_has_z = REX_SET;
1445 break;
1446#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001447 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001448 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001449 no_Magic(c));
1450 return FAIL;
1451 }
1452 break;
1453
1454 case Magic('%'):
1455 c = no_Magic(getchr());
1456 switch (c)
1457 {
1458 /* () without a back reference */
1459 case '(':
1460 if (nfa_reg(REG_NPAREN) == FAIL)
1461 return FAIL;
1462 EMIT(NFA_NOPEN);
1463 break;
1464
1465 case 'd': /* %d123 decimal */
1466 case 'o': /* %o123 octal */
1467 case 'x': /* %xab hex 2 */
1468 case 'u': /* %uabcd hex 4 */
1469 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001470 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001471 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001472
Bram Moolenaar47196582013-05-25 22:04:23 +02001473 switch (c)
1474 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001475 case 'd': nr = getdecchrs(); break;
1476 case 'o': nr = getoctchrs(); break;
1477 case 'x': nr = gethexchrs(2); break;
1478 case 'u': nr = gethexchrs(4); break;
1479 case 'U': nr = gethexchrs(8); break;
1480 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001481 }
1482
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001483 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001484 EMSG2_RET_FAIL(
1485 _("E678: Invalid character after %s%%[dxouU]"),
1486 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001487 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001488 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001489 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001490 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001491 break;
1492
1493 /* Catch \%^ and \%$ regardless of where they appear in the
1494 * pattern -- regardless of whether or not it makes sense. */
1495 case '^':
1496 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 break;
1498
1499 case '$':
1500 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 break;
1502
1503 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001504 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001505 break;
1506
1507 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001508 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 break;
1510
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001511 case 'C':
1512 EMIT(NFA_ANY_COMPOSING);
1513 break;
1514
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001515 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001516 {
1517 int n;
1518
1519 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001520 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001521 {
1522 if (c == NUL)
1523 EMSG2_RET_FAIL(_(e_missing_sb),
1524 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001525 /* recursive call! */
1526 if (nfa_regatom() == FAIL)
1527 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001528 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001529 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001530 if (n == 0)
1531 EMSG2_RET_FAIL(_(e_empty_sb),
1532 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001533 EMIT(NFA_OPT_CHARS);
1534 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001535
1536 /* Emit as "\%(\%[abc]\)" to be able to handle
1537 * "\%[abc]*" which would cause the empty string to be
1538 * matched an unlimited number of times. NFA_NOPEN is
1539 * added only once at a position, while NFA_SPLIT is
1540 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001541 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001542 * a lot. */
1543 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001544 break;
1545 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001546
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001547 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001548 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001549 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001550 int cmp = c;
1551
1552 if (c == '<' || c == '>')
1553 c = getchr();
1554 while (VIM_ISDIGIT(c))
1555 {
1556 n = n * 10 + (c - '0');
1557 c = getchr();
1558 }
1559 if (c == 'l' || c == 'c' || c == 'v')
1560 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001561 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001562 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001563 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001564 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001565 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001566 if (save_prev_at_start)
1567 at_start = TRUE;
1568 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001569 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001570 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001571 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001572 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001573 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001574 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001575 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001576 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001577#if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
1578 if (n > INT_MAX)
1579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001580 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001581 return FAIL;
1582 }
1583#endif
1584 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001585 break;
1586 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001587 else if (c == '\'' && n == 0)
1588 {
1589 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001590 EMIT(cmp == '<' ? NFA_MARK_LT :
1591 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001592 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001593 break;
1594 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001595 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001596 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001597 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001598 return FAIL;
1599 }
1600 break;
1601
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001603collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001605 * [abc] uses NFA_START_COLL - NFA_END_COLL
1606 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1607 * Each character is produced as a regular state, using
1608 * NFA_CONCAT to bind them together.
1609 * Besides normal characters there can be:
1610 * - character classes NFA_CLASS_*
1611 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 */
1613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001614 p = regparse;
1615 endp = skip_anyof(p);
1616 if (*endp == ']')
1617 {
1618 /*
1619 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001620 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001621 * and perform the necessary substitutions in the NFA.
1622 */
1623 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001624 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001625 if (result != FAIL)
1626 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001627 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001629 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001630 EMIT(NFA_NEWL);
1631 EMIT(NFA_OR);
1632 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001633 else
1634 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001636 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 return OK;
1638 }
1639 /*
1640 * Failed to recognize a character class. Use the simple
1641 * version that turns [abc] into 'a' OR 'b' OR 'c'
1642 */
1643 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 if (*regparse == '^') /* negated range */
1646 {
1647 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001648 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001649 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001650 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001651 else
1652 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 if (*regparse == '-')
1654 {
1655 startc = '-';
1656 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001657 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001658 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 }
1660 /* Emit the OR branches for each character in the [] */
1661 emit_range = FALSE;
1662 while (regparse < endp)
1663 {
1664 oldstartc = startc;
1665 startc = -1;
1666 got_coll_char = FALSE;
1667 if (*regparse == '[')
1668 {
1669 /* Check for [: :], [= =], [. .] */
1670 equiclass = collclass = 0;
1671 charclass = get_char_class(&regparse);
1672 if (charclass == CLASS_NONE)
1673 {
1674 equiclass = get_equi_class(&regparse);
1675 if (equiclass == 0)
1676 collclass = get_coll_element(&regparse);
1677 }
1678
1679 /* Character class like [:alpha:] */
1680 if (charclass != CLASS_NONE)
1681 {
1682 switch (charclass)
1683 {
1684 case CLASS_ALNUM:
1685 EMIT(NFA_CLASS_ALNUM);
1686 break;
1687 case CLASS_ALPHA:
1688 EMIT(NFA_CLASS_ALPHA);
1689 break;
1690 case CLASS_BLANK:
1691 EMIT(NFA_CLASS_BLANK);
1692 break;
1693 case CLASS_CNTRL:
1694 EMIT(NFA_CLASS_CNTRL);
1695 break;
1696 case CLASS_DIGIT:
1697 EMIT(NFA_CLASS_DIGIT);
1698 break;
1699 case CLASS_GRAPH:
1700 EMIT(NFA_CLASS_GRAPH);
1701 break;
1702 case CLASS_LOWER:
1703 EMIT(NFA_CLASS_LOWER);
1704 break;
1705 case CLASS_PRINT:
1706 EMIT(NFA_CLASS_PRINT);
1707 break;
1708 case CLASS_PUNCT:
1709 EMIT(NFA_CLASS_PUNCT);
1710 break;
1711 case CLASS_SPACE:
1712 EMIT(NFA_CLASS_SPACE);
1713 break;
1714 case CLASS_UPPER:
1715 EMIT(NFA_CLASS_UPPER);
1716 break;
1717 case CLASS_XDIGIT:
1718 EMIT(NFA_CLASS_XDIGIT);
1719 break;
1720 case CLASS_TAB:
1721 EMIT(NFA_CLASS_TAB);
1722 break;
1723 case CLASS_RETURN:
1724 EMIT(NFA_CLASS_RETURN);
1725 break;
1726 case CLASS_BACKSPACE:
1727 EMIT(NFA_CLASS_BACKSPACE);
1728 break;
1729 case CLASS_ESCAPE:
1730 EMIT(NFA_CLASS_ESCAPE);
1731 break;
1732 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001733 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001734 continue;
1735 }
1736 /* Try equivalence class [=a=] and the like */
1737 if (equiclass != 0)
1738 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001739 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 if (result == FAIL)
1741 {
1742 /* should never happen */
1743 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1744 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001745 continue;
1746 }
1747 /* Try collating class like [. .] */
1748 if (collclass != 0)
1749 {
1750 startc = collclass; /* allow [.a.]-x as a range */
1751 /* Will emit the proper atom at the end of the
1752 * while loop. */
1753 }
1754 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001755 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1756 * start character. */
1757 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001758 {
1759 emit_range = TRUE;
1760 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001761 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001762 continue; /* reading the end of the range */
1763 }
1764
1765 /* Now handle simple and escaped characters.
1766 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1767 * accepts "\t", "\e", etc., but only when the 'l' flag in
1768 * 'cpoptions' is not included.
1769 * Posix doesn't recognize backslash at all.
1770 */
1771 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001772 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 && regparse + 1 <= endp
1774 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001775 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001776 && vim_strchr(REGEXP_ABBR, regparse[1])
1777 != NULL)
1778 )
1779 )
1780 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001781 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001782
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001783 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 startc = reg_string ? NL : NFA_NEWL;
1785 else
1786 if (*regparse == 'd'
1787 || *regparse == 'o'
1788 || *regparse == 'x'
1789 || *regparse == 'u'
1790 || *regparse == 'U'
1791 )
1792 {
1793 /* TODO(RE) This needs more testing */
1794 startc = coll_get_char();
1795 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001796 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 }
1798 else
1799 {
1800 /* \r,\t,\e,\b */
1801 startc = backslash_trans(*regparse);
1802 }
1803 }
1804
1805 /* Normal printable char */
1806 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001807 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001808
1809 /* Previous char was '-', so this char is end of range. */
1810 if (emit_range)
1811 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001812 endc = startc;
1813 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001814 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001815 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001816
1817 if (endc > startc + 2)
1818 {
1819 /* Emit a range instead of the sequence of
1820 * individual characters. */
1821 if (startc == 0)
1822 /* \x00 is translated to \x0a, start at \x01. */
1823 EMIT(1);
1824 else
1825 --post_ptr; /* remove NFA_CONCAT */
1826 EMIT(endc);
1827 EMIT(NFA_RANGE);
1828 EMIT(NFA_CONCAT);
1829 }
1830 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001832 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001833 || (*mb_char2len)(endc) > 1))
1834 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001835 /* Emit the characters in the range.
1836 * "startc" was already emitted, so skip it.
1837 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 for (c = startc + 1; c <= endc; c++)
1839 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001840 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001841 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001842 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001843 }
1844 else
1845#endif
1846 {
1847#ifdef EBCDIC
1848 int alpha_only = FALSE;
1849
1850 /* for alphabetical range skip the gaps
1851 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1852 if (isalpha(startc) && isalpha(endc))
1853 alpha_only = TRUE;
1854#endif
1855 /* Emit the range. "startc" was already emitted, so
1856 * skip it. */
1857 for (c = startc + 1; c <= endc; c++)
1858#ifdef EBCDIC
1859 if (!alpha_only || isalpha(startc))
1860#endif
1861 {
1862 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001863 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001864 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001865 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001866 emit_range = FALSE;
1867 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 }
1869 else
1870 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001871 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001873 * Normally, simply emit startc. But if we get char
1874 * code=0 from a collating char, then replace it with
1875 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001876 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001877 * the backtracking engine. */
1878 if (startc == NFA_NEWL)
1879 {
1880 /* Line break can't be matched as part of the
1881 * collection, add an OR below. But not for negated
1882 * range. */
1883 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001884 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001885 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001887 {
1888 if (got_coll_char == TRUE && startc == 0)
1889 EMIT(0x0a);
1890 else
1891 EMIT(startc);
1892 EMIT(NFA_CONCAT);
1893 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894 }
1895
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001896 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 } /* while (p < endp) */
1898
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001899 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 if (*regparse == '-') /* if last, '-' is just a char */
1901 {
1902 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001903 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 /* skip the trailing ] */
1907 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001908 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001909
1910 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001912 EMIT(NFA_END_NEG_COLL);
1913 else
1914 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001915
1916 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001917 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001918 {
1919 EMIT(reg_string ? NL : NFA_NEWL);
1920 EMIT(NFA_OR);
1921 }
1922
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001924 } /* if exists closing ] */
1925
1926 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001928 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 default:
1931 {
1932#ifdef FEAT_MBYTE
1933 int plen;
1934
1935nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001936 /* plen is length of current char with composing chars */
1937 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001938 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001939 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001941 int i = 0;
1942
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001943 /* A base character plus composing characters, or just one
1944 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001945 * This requires creating a separate atom as if enclosing
1946 * the characters in (), where NFA_COMPOSING is the ( and
1947 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 * building the postfix form, not the NFA itself;
1949 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001950 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001951 for (;;)
1952 {
1953 EMIT(c);
1954 if (i > 0)
1955 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001956 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001957 break;
1958 c = utf_ptr2char(old_regparse + i);
1959 }
1960 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001961 regparse = old_regparse + plen;
1962 }
1963 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964#endif
1965 {
1966 c = no_Magic(c);
1967 EMIT(c);
1968 }
1969 return OK;
1970 }
1971 }
1972
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001973 return OK;
1974}
1975
1976/*
1977 * Parse something followed by possible [*+=].
1978 *
1979 * A piece is an atom, possibly followed by a multi, an indication of how many
1980 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1981 * characters: "", "a", "aa", etc.
1982 *
1983 * piece ::= atom
1984 * or atom multi
1985 */
1986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001987nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001988{
1989 int i;
1990 int op;
1991 int ret;
1992 long minval, maxval;
1993 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001994 parse_state_T old_state;
1995 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001996 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001997 int old_post_pos;
1998 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 int quest;
2000
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002001 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2002 * next. */
2003 save_parse_state(&old_state);
2004
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002006 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007
2008 ret = nfa_regatom();
2009 if (ret == FAIL)
2010 return FAIL; /* cascaded error */
2011
2012 op = peekchr();
2013 if (re_multi_type(op) == NOT_MULTI)
2014 return OK;
2015
2016 skipchr();
2017 switch (op)
2018 {
2019 case Magic('*'):
2020 EMIT(NFA_STAR);
2021 break;
2022
2023 case Magic('+'):
2024 /*
2025 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2026 * first and only submatch would be "aaa". But the backtracking
2027 * engine interprets the plus as "try matching one more time", and
2028 * a* matches a second time at the end of the input, the empty
2029 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002030 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002031 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002032 * In order to be consistent with the old engine, we replace
2033 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002035 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002036 curchr = -1;
2037 if (nfa_regatom() == FAIL)
2038 return FAIL;
2039 EMIT(NFA_STAR);
2040 EMIT(NFA_CONCAT);
2041 skipchr(); /* skip the \+ */
2042 break;
2043
2044 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002045 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002047 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002048 switch(op)
2049 {
2050 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002051 /* \@= */
2052 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002054 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002055 /* \@! */
2056 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002057 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002059 op = no_Magic(getchr());
2060 if (op == '=')
2061 /* \@<= */
2062 i = NFA_PREV_ATOM_JUST_BEFORE;
2063 else if (op == '!')
2064 /* \@<! */
2065 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2066 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002068 /* \@> */
2069 i = NFA_PREV_ATOM_LIKE_PATTERN;
2070 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002071 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002072 if (i == 0)
2073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002074 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002075 return FAIL;
2076 }
2077 EMIT(i);
2078 if (i == NFA_PREV_ATOM_JUST_BEFORE
2079 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2080 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081 break;
2082
2083 case Magic('?'):
2084 case Magic('='):
2085 EMIT(NFA_QUEST);
2086 break;
2087
2088 case Magic('{'):
2089 /* a{2,5} will expand to 'aaa?a?a?'
2090 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2091 * version of '?'
2092 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2093 * parenthesis have the same id
2094 */
2095
2096 greedy = TRUE;
2097 c2 = peekchr();
2098 if (c2 == '-' || c2 == Magic('-'))
2099 {
2100 skipchr();
2101 greedy = FALSE;
2102 }
2103 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002105
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2107 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002108 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002109 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002110 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002111 /* \{}, \{0,} */
2112 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002113 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002114 /* \{-}, \{-0,} */
2115 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002116 break;
2117 }
2118
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002119 /* Special case: x{0} or x{-0} */
2120 if (maxval == 0)
2121 {
2122 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002123 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002124 /* NFA_EMPTY is 0-length and works everywhere */
2125 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126 return OK;
2127 }
2128
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002129 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002130 * maximum is much larger than the minimum and when the maximum is
2131 * large. Bail out if we can use the other engine. */
2132 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002133 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002134 return FAIL;
2135
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002136 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002137 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002138 /* Save parse state after the repeated atom and the \{} */
2139 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002141 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2142 for (i = 0; i < maxval; i++)
2143 {
2144 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002145 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002146 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147 if (nfa_regatom() == FAIL)
2148 return FAIL;
2149 /* after "minval" times, atoms are optional */
2150 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002151 {
2152 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002153 {
2154 if (greedy)
2155 EMIT(NFA_STAR);
2156 else
2157 EMIT(NFA_STAR_NONGREEDY);
2158 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002159 else
2160 EMIT(quest);
2161 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002162 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002164 if (i + 1 > minval && maxval == MAX_LIMIT)
2165 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 }
2167
2168 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002169 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 curchr = -1;
2171
2172 break;
2173
2174
2175 default:
2176 break;
2177 } /* end switch */
2178
2179 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002181 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182
2183 return OK;
2184}
2185
2186/*
2187 * Parse one or more pieces, concatenated. It matches a match for the
2188 * first piece, followed by a match for the second piece, etc. Example:
2189 * "f[0-9]b", first matches "f", then a digit and then "b".
2190 *
2191 * concat ::= piece
2192 * or piece piece
2193 * or piece piece piece
2194 * etc.
2195 */
2196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002197nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198{
2199 int cont = TRUE;
2200 int first = TRUE;
2201
2202 while (cont)
2203 {
2204 switch (peekchr())
2205 {
2206 case NUL:
2207 case Magic('|'):
2208 case Magic('&'):
2209 case Magic(')'):
2210 cont = FALSE;
2211 break;
2212
2213 case Magic('Z'):
2214#ifdef FEAT_MBYTE
2215 regflags |= RF_ICOMBINE;
2216#endif
2217 skipchr_keepstart();
2218 break;
2219 case Magic('c'):
2220 regflags |= RF_ICASE;
2221 skipchr_keepstart();
2222 break;
2223 case Magic('C'):
2224 regflags |= RF_NOICASE;
2225 skipchr_keepstart();
2226 break;
2227 case Magic('v'):
2228 reg_magic = MAGIC_ALL;
2229 skipchr_keepstart();
2230 curchr = -1;
2231 break;
2232 case Magic('m'):
2233 reg_magic = MAGIC_ON;
2234 skipchr_keepstart();
2235 curchr = -1;
2236 break;
2237 case Magic('M'):
2238 reg_magic = MAGIC_OFF;
2239 skipchr_keepstart();
2240 curchr = -1;
2241 break;
2242 case Magic('V'):
2243 reg_magic = MAGIC_NONE;
2244 skipchr_keepstart();
2245 curchr = -1;
2246 break;
2247
2248 default:
2249 if (nfa_regpiece() == FAIL)
2250 return FAIL;
2251 if (first == FALSE)
2252 EMIT(NFA_CONCAT);
2253 else
2254 first = FALSE;
2255 break;
2256 }
2257 }
2258
2259 return OK;
2260}
2261
2262/*
2263 * Parse a branch, one or more concats, separated by "\&". It matches the
2264 * last concat, but only if all the preceding concats also match at the same
2265 * position. Examples:
2266 * "foobeep\&..." matches "foo" in "foobeep".
2267 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2268 *
2269 * branch ::= concat
2270 * or concat \& concat
2271 * or concat \& concat \& concat
2272 * etc.
2273 */
2274 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002275nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002277 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278
Bram Moolenaar16299b52013-05-30 18:45:23 +02002279 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002280
2281 /* First branch, possibly the only one */
2282 if (nfa_regconcat() == FAIL)
2283 return FAIL;
2284
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002286 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287 {
2288 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002289 /* if concat is empty do emit a node */
2290 if (old_post_pos == (int)(post_ptr - post_start))
2291 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002292 EMIT(NFA_NOPEN);
2293 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002294 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 if (nfa_regconcat() == FAIL)
2296 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002297 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002298 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002299 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 }
2302
Bram Moolenaar699c1202013-09-25 16:41:54 +02002303 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002304 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002305 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002306
2307 return OK;
2308}
2309
2310/*
2311 * Parse a pattern, one or more branches, separated by "\|". It matches
2312 * anything that matches one of the branches. Example: "foo\|beep" matches
2313 * "foo" and matches "beep". If more than one branch matches, the first one
2314 * is used.
2315 *
2316 * pattern ::= branch
2317 * or branch \| branch
2318 * or branch \| branch \| branch
2319 * etc.
2320 */
2321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002322nfa_reg(
2323 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324{
2325 int parno = 0;
2326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002327 if (paren == REG_PAREN)
2328 {
2329 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 parno = regnpar++;
2332 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002333#ifdef FEAT_SYN_HL
2334 else if (paren == REG_ZPAREN)
2335 {
2336 /* Make a ZOPEN node. */
2337 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002338 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002339 parno = regnzpar++;
2340 }
2341#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002342
2343 if (nfa_regbranch() == FAIL)
2344 return FAIL; /* cascaded error */
2345
2346 while (peekchr() == Magic('|'))
2347 {
2348 skipchr();
2349 if (nfa_regbranch() == FAIL)
2350 return FAIL; /* cascaded error */
2351 EMIT(NFA_OR);
2352 }
2353
2354 /* Check for proper termination. */
2355 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2356 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357 if (paren == REG_NPAREN)
2358 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2359 else
2360 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2361 }
2362 else if (paren == REG_NOPAREN && peekchr() != NUL)
2363 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364 if (peekchr() == Magic(')'))
2365 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2366 else
2367 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2368 }
2369 /*
2370 * Here we set the flag allowing back references to this set of
2371 * parentheses.
2372 */
2373 if (paren == REG_PAREN)
2374 {
2375 had_endbrace[parno] = TRUE; /* have seen the close paren */
2376 EMIT(NFA_MOPEN + parno);
2377 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002378#ifdef FEAT_SYN_HL
2379 else if (paren == REG_ZPAREN)
2380 EMIT(NFA_ZOPEN + parno);
2381#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002382
2383 return OK;
2384}
2385
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002386#ifdef DEBUG
2387static char_u code[50];
2388
2389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002390nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002391{
2392 int addnl = FALSE;
2393
2394 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2395 {
2396 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002397 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398 }
2399
2400 STRCPY(code, "");
2401 switch (c)
2402 {
2403 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2404 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2405 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2406 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2407 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2408 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2409
Bram Moolenaar5714b802013-05-28 22:03:20 +02002410 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2411 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2412 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2413 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2414 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2415 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2416 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2417 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2418 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002419#ifdef FEAT_SYN_HL
2420 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2421 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2422 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2423 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2424 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2425 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2426 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2427 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2428 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2429#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002430 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2431
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432 case NFA_PREV_ATOM_NO_WIDTH:
2433 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002434 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2435 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002436 case NFA_PREV_ATOM_JUST_BEFORE:
2437 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2438 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2439 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002440 case NFA_PREV_ATOM_LIKE_PATTERN:
2441 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2442
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002443 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2444 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002446 case NFA_START_INVISIBLE_FIRST:
2447 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002448 case NFA_START_INVISIBLE_NEG:
2449 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002450 case NFA_START_INVISIBLE_NEG_FIRST:
2451 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002452 case NFA_START_INVISIBLE_BEFORE:
2453 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002454 case NFA_START_INVISIBLE_BEFORE_FIRST:
2455 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002456 case NFA_START_INVISIBLE_BEFORE_NEG:
2457 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002458 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2459 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002460 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002462 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002463 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2466 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002467 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002468
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002469 case NFA_MOPEN:
2470 case NFA_MOPEN1:
2471 case NFA_MOPEN2:
2472 case NFA_MOPEN3:
2473 case NFA_MOPEN4:
2474 case NFA_MOPEN5:
2475 case NFA_MOPEN6:
2476 case NFA_MOPEN7:
2477 case NFA_MOPEN8:
2478 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 STRCPY(code, "NFA_MOPEN(x)");
2480 code[10] = c - NFA_MOPEN + '0';
2481 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002482 case NFA_MCLOSE:
2483 case NFA_MCLOSE1:
2484 case NFA_MCLOSE2:
2485 case NFA_MCLOSE3:
2486 case NFA_MCLOSE4:
2487 case NFA_MCLOSE5:
2488 case NFA_MCLOSE6:
2489 case NFA_MCLOSE7:
2490 case NFA_MCLOSE8:
2491 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492 STRCPY(code, "NFA_MCLOSE(x)");
2493 code[11] = c - NFA_MCLOSE + '0';
2494 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002495#ifdef FEAT_SYN_HL
2496 case NFA_ZOPEN:
2497 case NFA_ZOPEN1:
2498 case NFA_ZOPEN2:
2499 case NFA_ZOPEN3:
2500 case NFA_ZOPEN4:
2501 case NFA_ZOPEN5:
2502 case NFA_ZOPEN6:
2503 case NFA_ZOPEN7:
2504 case NFA_ZOPEN8:
2505 case NFA_ZOPEN9:
2506 STRCPY(code, "NFA_ZOPEN(x)");
2507 code[10] = c - NFA_ZOPEN + '0';
2508 break;
2509 case NFA_ZCLOSE:
2510 case NFA_ZCLOSE1:
2511 case NFA_ZCLOSE2:
2512 case NFA_ZCLOSE3:
2513 case NFA_ZCLOSE4:
2514 case NFA_ZCLOSE5:
2515 case NFA_ZCLOSE6:
2516 case NFA_ZCLOSE7:
2517 case NFA_ZCLOSE8:
2518 case NFA_ZCLOSE9:
2519 STRCPY(code, "NFA_ZCLOSE(x)");
2520 code[11] = c - NFA_ZCLOSE + '0';
2521 break;
2522#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002523 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2524 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2525 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2526 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002527 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2528 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002529 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2530 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2531 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2532 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2533 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2534 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2535 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2536 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2537 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2538 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2539 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2540 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2541 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2542 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002543 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002544
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002545 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002546 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2547 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2548 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002549 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002551
2552 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2553 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2554 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2555 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2556 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2557 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2558 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2559
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2561 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2562 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2563 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2564 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2565 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2566 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2567 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2568 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2569 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2570 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2571 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2572 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2573 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2574 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2575 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2576
2577 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2578 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2579 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2580 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2581 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2582 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2583 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2584 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2585 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2586 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2587 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2588 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2589 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2590 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2591 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2592 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2593 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2594 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2595 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2596 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2597 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2598 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2599 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2600 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2601 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2602 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2603 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002604 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2605 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2606 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2607 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608
2609 default:
2610 STRCPY(code, "CHAR(x)");
2611 code[5] = c;
2612 }
2613
2614 if (addnl == TRUE)
2615 STRCAT(code, " + NEWLINE ");
2616
2617}
2618
2619#ifdef ENABLE_LOG
2620static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002621static 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 +02002622
2623/*
2624 * Print the postfix notation of the current regexp.
2625 */
2626 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002627nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628{
2629 int *p;
2630 FILE *f;
2631
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002632 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633 if (f != NULL)
2634 {
2635 fprintf(f, "\n-------------------------\n");
2636 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002637 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638 else if (retval == OK)
2639 fprintf(f, ">>> NFA engine succeeded !\n");
2640 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002641 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002642 {
2643 nfa_set_code(*p);
2644 fprintf(f, "%s, ", code);
2645 }
2646 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002647 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 fprintf(f, "%d ", *p);
2649 fprintf(f, "\n\n");
2650 fclose(f);
2651 }
2652}
2653
2654/*
2655 * Print the NFA starting with a root node "state".
2656 */
2657 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002658nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002660 garray_T indent;
2661
2662 ga_init2(&indent, 1, 64);
2663 ga_append(&indent, '\0');
2664 nfa_print_state2(debugf, state, &indent);
2665 ga_clear(&indent);
2666}
2667
2668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002669nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002670{
2671 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672
2673 if (state == NULL)
2674 return;
2675
2676 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002677
2678 /* Output indent */
2679 p = (char_u *)indent->ga_data;
2680 if (indent->ga_len >= 3)
2681 {
2682 int last = indent->ga_len - 3;
2683 char_u save[2];
2684
2685 STRNCPY(save, &p[last], 2);
2686 STRNCPY(&p[last], "+-", 2);
2687 fprintf(debugf, " %s", p);
2688 STRNCPY(&p[last], save, 2);
2689 }
2690 else
2691 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002692
2693 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002694 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002695 code,
2696 state->c,
2697 abs(state->id),
2698 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699 if (state->id < 0)
2700 return;
2701
2702 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002703
2704 /* grow indent for state->out */
2705 indent->ga_len -= 1;
2706 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002707 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002708 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002709 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002710 ga_append(indent, '\0');
2711
2712 nfa_print_state2(debugf, state->out, indent);
2713
2714 /* replace last part of indent for state->out1 */
2715 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002716 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002717 ga_append(indent, '\0');
2718
2719 nfa_print_state2(debugf, state->out1, indent);
2720
2721 /* shrink indent */
2722 indent->ga_len -= 3;
2723 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002724}
2725
2726/*
2727 * Print the NFA state machine.
2728 */
2729 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002730nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002732 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002733
2734 if (debugf != NULL)
2735 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002736 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002737
Bram Moolenaar473de612013-06-08 18:19:48 +02002738 if (prog->reganch)
2739 fprintf(debugf, "reganch: %d\n", prog->reganch);
2740 if (prog->regstart != NUL)
2741 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2742 prog->regstart, prog->regstart);
2743 if (prog->match_text != NULL)
2744 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002745
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002746 fclose(debugf);
2747 }
2748}
2749#endif /* ENABLE_LOG */
2750#endif /* DEBUG */
2751
2752/*
2753 * Parse r.e. @expr and convert it into postfix form.
2754 * Return the postfix string on success, NULL otherwise.
2755 */
2756 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002757re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002758{
2759 if (nfa_reg(REG_NOPAREN) == FAIL)
2760 return NULL;
2761 EMIT(NFA_MOPEN);
2762 return post_start;
2763}
2764
2765/* NB. Some of the code below is inspired by Russ's. */
2766
2767/*
2768 * Represents an NFA state plus zero or one or two arrows exiting.
2769 * if c == MATCH, no arrows out; matching state.
2770 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2771 * If c < 256, labeled arrow with character c to out.
2772 */
2773
2774static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2775
2776/*
2777 * Allocate and initialize nfa_state_T.
2778 */
2779 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002780alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002781{
2782 nfa_state_T *s;
2783
2784 if (istate >= nstate)
2785 return NULL;
2786
2787 s = &state_ptr[istate++];
2788
2789 s->c = c;
2790 s->out = out;
2791 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002792 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002793
2794 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002795 s->lastlist[0] = 0;
2796 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002797
2798 return s;
2799}
2800
2801/*
2802 * A partially built NFA without the matching state filled in.
2803 * Frag_T.start points at the start state.
2804 * Frag_T.out is a list of places that need to be set to the
2805 * next state for this fragment.
2806 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002807
2808/* Since the out pointers in the list are always
2809 * uninitialized, we use the pointers themselves
2810 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002812union Ptrlist
2813{
2814 Ptrlist *next;
2815 nfa_state_T *s;
2816};
2817
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002818struct Frag
2819{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002820 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821 Ptrlist *out;
2822};
2823typedef struct Frag Frag_T;
2824
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002825/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002826 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827 */
2828 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002829frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002830{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002831 Frag_T n;
2832
2833 n.start = start;
2834 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835 return n;
2836}
2837
2838/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839 * Create singleton list containing just outp.
2840 */
2841 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002842list1(
2843 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002844{
2845 Ptrlist *l;
2846
2847 l = (Ptrlist *)outp;
2848 l->next = NULL;
2849 return l;
2850}
2851
2852/*
2853 * Patch the list of states at out to point to start.
2854 */
2855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002856patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002857{
2858 Ptrlist *next;
2859
2860 for (; l; l = next)
2861 {
2862 next = l->next;
2863 l->s = s;
2864 }
2865}
2866
2867
2868/*
2869 * Join the two lists l1 and l2, returning the combination.
2870 */
2871 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002872append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873{
2874 Ptrlist *oldl1;
2875
2876 oldl1 = l1;
2877 while (l1->next)
2878 l1 = l1->next;
2879 l1->next = l2;
2880 return oldl1;
2881}
2882
2883/*
2884 * Stack used for transforming postfix form into NFA.
2885 */
2886static Frag_T empty;
2887
2888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002889st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002890{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002891#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002892 FILE *df;
2893 int *p2;
2894
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002895 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 if (df)
2897 {
2898 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002899# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002901# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002903# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904 for (p2 = postfix; p2 < end; p2++)
2905 {
2906 nfa_set_code(*p2);
2907 fprintf(df, "%s, ", code);
2908 }
2909 nfa_set_code(*p);
2910 fprintf(df, "\nCurrent position is: ");
2911 for (p2 = postfix; p2 <= p; p2 ++)
2912 {
2913 nfa_set_code(*p2);
2914 fprintf(df, "%s, ", code);
2915 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002916# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 for (p2 = postfix; p2 < end; p2++)
2918 {
2919 fprintf(df, "%d, ", *p2);
2920 }
2921 fprintf(df, "\nCurrent position is: ");
2922 for (p2 = postfix; p2 <= p; p2 ++)
2923 {
2924 fprintf(df, "%d, ", *p2);
2925 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002926# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002927 fprintf(df, "\n--------------------------\n");
2928 fclose(df);
2929 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002930#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002931 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932}
2933
2934/*
2935 * Push an item onto the stack.
2936 */
2937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002938st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002939{
2940 Frag_T *stackp = *p;
2941
2942 if (stackp >= stack_end)
2943 return;
2944 *stackp = s;
2945 *p = *p + 1;
2946}
2947
2948/*
2949 * Pop an item from the stack.
2950 */
2951 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002952st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953{
2954 Frag_T *stackp;
2955
2956 *p = *p - 1;
2957 stackp = *p;
2958 if (stackp < stack)
2959 return empty;
2960 return **p;
2961}
2962
2963/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002964 * Estimate the maximum byte length of anything matching "state".
2965 * When unknown or unlimited return -1.
2966 */
2967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002968nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002969{
2970 int l, r;
2971 nfa_state_T *state = startstate;
2972 int len = 0;
2973
2974 /* detect looping in a NFA_SPLIT */
2975 if (depth > 4)
2976 return -1;
2977
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002978 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002979 {
2980 switch (state->c)
2981 {
2982 case NFA_END_INVISIBLE:
2983 case NFA_END_INVISIBLE_NEG:
2984 /* the end, return what we have */
2985 return len;
2986
2987 case NFA_SPLIT:
2988 /* two alternatives, use the maximum */
2989 l = nfa_max_width(state->out, depth + 1);
2990 r = nfa_max_width(state->out1, depth + 1);
2991 if (l < 0 || r < 0)
2992 return -1;
2993 return len + (l > r ? l : r);
2994
2995 case NFA_ANY:
2996 case NFA_START_COLL:
2997 case NFA_START_NEG_COLL:
2998 /* matches some character, including composing chars */
2999#ifdef FEAT_MBYTE
3000 if (enc_utf8)
3001 len += MB_MAXBYTES;
3002 else if (has_mbyte)
3003 len += 2;
3004 else
3005#endif
3006 ++len;
3007 if (state->c != NFA_ANY)
3008 {
3009 /* skip over the characters */
3010 state = state->out1->out;
3011 continue;
3012 }
3013 break;
3014
3015 case NFA_DIGIT:
3016 case NFA_WHITE:
3017 case NFA_HEX:
3018 case NFA_OCTAL:
3019 /* ascii */
3020 ++len;
3021 break;
3022
3023 case NFA_IDENT:
3024 case NFA_SIDENT:
3025 case NFA_KWORD:
3026 case NFA_SKWORD:
3027 case NFA_FNAME:
3028 case NFA_SFNAME:
3029 case NFA_PRINT:
3030 case NFA_SPRINT:
3031 case NFA_NWHITE:
3032 case NFA_NDIGIT:
3033 case NFA_NHEX:
3034 case NFA_NOCTAL:
3035 case NFA_WORD:
3036 case NFA_NWORD:
3037 case NFA_HEAD:
3038 case NFA_NHEAD:
3039 case NFA_ALPHA:
3040 case NFA_NALPHA:
3041 case NFA_LOWER:
3042 case NFA_NLOWER:
3043 case NFA_UPPER:
3044 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003045 case NFA_LOWER_IC:
3046 case NFA_NLOWER_IC:
3047 case NFA_UPPER_IC:
3048 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003049 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003050 /* possibly non-ascii */
3051#ifdef FEAT_MBYTE
3052 if (has_mbyte)
3053 len += 3;
3054 else
3055#endif
3056 ++len;
3057 break;
3058
3059 case NFA_START_INVISIBLE:
3060 case NFA_START_INVISIBLE_NEG:
3061 case NFA_START_INVISIBLE_BEFORE:
3062 case NFA_START_INVISIBLE_BEFORE_NEG:
3063 /* zero-width, out1 points to the END state */
3064 state = state->out1->out;
3065 continue;
3066
3067 case NFA_BACKREF1:
3068 case NFA_BACKREF2:
3069 case NFA_BACKREF3:
3070 case NFA_BACKREF4:
3071 case NFA_BACKREF5:
3072 case NFA_BACKREF6:
3073 case NFA_BACKREF7:
3074 case NFA_BACKREF8:
3075 case NFA_BACKREF9:
3076#ifdef FEAT_SYN_HL
3077 case NFA_ZREF1:
3078 case NFA_ZREF2:
3079 case NFA_ZREF3:
3080 case NFA_ZREF4:
3081 case NFA_ZREF5:
3082 case NFA_ZREF6:
3083 case NFA_ZREF7:
3084 case NFA_ZREF8:
3085 case NFA_ZREF9:
3086#endif
3087 case NFA_NEWL:
3088 case NFA_SKIP:
3089 /* unknown width */
3090 return -1;
3091
3092 case NFA_BOL:
3093 case NFA_EOL:
3094 case NFA_BOF:
3095 case NFA_EOF:
3096 case NFA_BOW:
3097 case NFA_EOW:
3098 case NFA_MOPEN:
3099 case NFA_MOPEN1:
3100 case NFA_MOPEN2:
3101 case NFA_MOPEN3:
3102 case NFA_MOPEN4:
3103 case NFA_MOPEN5:
3104 case NFA_MOPEN6:
3105 case NFA_MOPEN7:
3106 case NFA_MOPEN8:
3107 case NFA_MOPEN9:
3108#ifdef FEAT_SYN_HL
3109 case NFA_ZOPEN:
3110 case NFA_ZOPEN1:
3111 case NFA_ZOPEN2:
3112 case NFA_ZOPEN3:
3113 case NFA_ZOPEN4:
3114 case NFA_ZOPEN5:
3115 case NFA_ZOPEN6:
3116 case NFA_ZOPEN7:
3117 case NFA_ZOPEN8:
3118 case NFA_ZOPEN9:
3119 case NFA_ZCLOSE:
3120 case NFA_ZCLOSE1:
3121 case NFA_ZCLOSE2:
3122 case NFA_ZCLOSE3:
3123 case NFA_ZCLOSE4:
3124 case NFA_ZCLOSE5:
3125 case NFA_ZCLOSE6:
3126 case NFA_ZCLOSE7:
3127 case NFA_ZCLOSE8:
3128 case NFA_ZCLOSE9:
3129#endif
3130 case NFA_MCLOSE:
3131 case NFA_MCLOSE1:
3132 case NFA_MCLOSE2:
3133 case NFA_MCLOSE3:
3134 case NFA_MCLOSE4:
3135 case NFA_MCLOSE5:
3136 case NFA_MCLOSE6:
3137 case NFA_MCLOSE7:
3138 case NFA_MCLOSE8:
3139 case NFA_MCLOSE9:
3140 case NFA_NOPEN:
3141 case NFA_NCLOSE:
3142
3143 case NFA_LNUM_GT:
3144 case NFA_LNUM_LT:
3145 case NFA_COL_GT:
3146 case NFA_COL_LT:
3147 case NFA_VCOL_GT:
3148 case NFA_VCOL_LT:
3149 case NFA_MARK_GT:
3150 case NFA_MARK_LT:
3151 case NFA_VISUAL:
3152 case NFA_LNUM:
3153 case NFA_CURSOR:
3154 case NFA_COL:
3155 case NFA_VCOL:
3156 case NFA_MARK:
3157
3158 case NFA_ZSTART:
3159 case NFA_ZEND:
3160 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003161 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003162 case NFA_START_PATTERN:
3163 case NFA_END_PATTERN:
3164 case NFA_COMPOSING:
3165 case NFA_END_COMPOSING:
3166 /* zero-width */
3167 break;
3168
3169 default:
3170 if (state->c < 0)
3171 /* don't know what this is */
3172 return -1;
3173 /* normal character */
3174 len += MB_CHAR2LEN(state->c);
3175 break;
3176 }
3177
3178 /* normal way to continue */
3179 state = state->out;
3180 }
3181
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003182 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003183 return -1;
3184}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003185
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003186/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003187 * Convert a postfix form into its equivalent NFA.
3188 * Return the NFA start state on success, NULL otherwise.
3189 */
3190 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003191post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003192{
3193 int *p;
3194 int mopen;
3195 int mclose;
3196 Frag_T *stack = NULL;
3197 Frag_T *stackp = NULL;
3198 Frag_T *stack_end = NULL;
3199 Frag_T e1;
3200 Frag_T e2;
3201 Frag_T e;
3202 nfa_state_T *s;
3203 nfa_state_T *s1;
3204 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003205 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206
3207 if (postfix == NULL)
3208 return NULL;
3209
Bram Moolenaar053bb602013-05-20 13:55:21 +02003210#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211#define POP() st_pop(&stackp, stack); \
3212 if (stackp < stack) \
3213 { \
3214 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003215 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 return NULL; \
3217 }
3218
3219 if (nfa_calc_size == FALSE)
3220 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003221 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003222 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003223 if (stack == NULL)
3224 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003226 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003227 }
3228
3229 for (p = postfix; p < end; ++p)
3230 {
3231 switch (*p)
3232 {
3233 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003234 /* Concatenation.
3235 * Pay attention: this operator does not exist in the r.e. itself
3236 * (it is implicit, really). It is added when r.e. is translated
3237 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003238 if (nfa_calc_size == TRUE)
3239 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003240 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241 break;
3242 }
3243 e2 = POP();
3244 e1 = POP();
3245 patch(e1.out, e2.start);
3246 PUSH(frag(e1.start, e2.out));
3247 break;
3248
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 case NFA_OR:
3250 /* Alternation */
3251 if (nfa_calc_size == TRUE)
3252 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003253 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 break;
3255 }
3256 e2 = POP();
3257 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003258 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003260 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 PUSH(frag(s, append(e1.out, e2.out)));
3262 break;
3263
3264 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003265 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 if (nfa_calc_size == TRUE)
3267 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003268 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 break;
3270 }
3271 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003272 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003274 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 patch(e.out, s);
3276 PUSH(frag(s, list1(&s->out1)));
3277 break;
3278
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003279 case NFA_STAR_NONGREEDY:
3280 /* Zero or more, prefer zero */
3281 if (nfa_calc_size == TRUE)
3282 {
3283 nstate++;
3284 break;
3285 }
3286 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003287 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003288 if (s == NULL)
3289 goto theend;
3290 patch(e.out, s);
3291 PUSH(frag(s, list1(&s->out)));
3292 break;
3293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 case NFA_QUEST:
3295 /* one or zero atoms=> greedy match */
3296 if (nfa_calc_size == TRUE)
3297 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003298 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300 }
3301 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003302 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003303 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003304 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 PUSH(frag(s, append(e.out, list1(&s->out1))));
3306 break;
3307
3308 case NFA_QUEST_NONGREEDY:
3309 /* zero or one atoms => non-greedy match */
3310 if (nfa_calc_size == TRUE)
3311 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003312 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 break;
3314 }
3315 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003316 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003317 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003318 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 PUSH(frag(s, append(e.out, list1(&s->out))));
3320 break;
3321
Bram Moolenaar417bad22013-06-07 14:08:30 +02003322 case NFA_END_COLL:
3323 case NFA_END_NEG_COLL:
3324 /* On the stack is the sequence starting with NFA_START_COLL or
3325 * NFA_START_NEG_COLL and all possible characters. Patch it to
3326 * add the output to the start. */
3327 if (nfa_calc_size == TRUE)
3328 {
3329 nstate++;
3330 break;
3331 }
3332 e = POP();
3333 s = alloc_state(NFA_END_COLL, NULL, NULL);
3334 if (s == NULL)
3335 goto theend;
3336 patch(e.out, s);
3337 e.start->out1 = s;
3338 PUSH(frag(e.start, list1(&s->out)));
3339 break;
3340
3341 case NFA_RANGE:
3342 /* Before this are two characters, the low and high end of a
3343 * range. Turn them into two states with MIN and MAX. */
3344 if (nfa_calc_size == TRUE)
3345 {
3346 /* nstate += 0; */
3347 break;
3348 }
3349 e2 = POP();
3350 e1 = POP();
3351 e2.start->val = e2.start->c;
3352 e2.start->c = NFA_RANGE_MAX;
3353 e1.start->val = e1.start->c;
3354 e1.start->c = NFA_RANGE_MIN;
3355 patch(e1.out, e2.start);
3356 PUSH(frag(e1.start, e2.out));
3357 break;
3358
Bram Moolenaar699c1202013-09-25 16:41:54 +02003359 case NFA_EMPTY:
3360 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361 if (nfa_calc_size == TRUE)
3362 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003363 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 break;
3365 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003366 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003368 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 PUSH(frag(s, list1(&s->out)));
3370 break;
3371
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003372 case NFA_OPT_CHARS:
3373 {
3374 int n;
3375
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003376 /* \%[abc] implemented as:
3377 * NFA_SPLIT
3378 * +-CHAR(a)
3379 * | +-NFA_SPLIT
3380 * | +-CHAR(b)
3381 * | | +-NFA_SPLIT
3382 * | | +-CHAR(c)
3383 * | | | +-next
3384 * | | +- next
3385 * | +- next
3386 * +- next
3387 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003388 n = *++p; /* get number of characters */
3389 if (nfa_calc_size == TRUE)
3390 {
3391 nstate += n;
3392 break;
3393 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003394 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003395 e1.out = NULL; /* stores list with out1's */
3396 s1 = NULL; /* previous NFA_SPLIT to connect to */
3397 while (n-- > 0)
3398 {
3399 e = POP(); /* get character */
3400 s = alloc_state(NFA_SPLIT, e.start, NULL);
3401 if (s == NULL)
3402 goto theend;
3403 if (e1.out == NULL)
3404 e1 = e;
3405 patch(e.out, s1);
3406 append(e1.out, list1(&s->out1));
3407 s1 = s;
3408 }
3409 PUSH(frag(s, e1.out));
3410 break;
3411 }
3412
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003414 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003415 case NFA_PREV_ATOM_JUST_BEFORE:
3416 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003417 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003418 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003419 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3420 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003421 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003422 int start_state;
3423 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003424 int n = 0;
3425 nfa_state_T *zend;
3426 nfa_state_T *skip;
3427
Bram Moolenaardecd9542013-06-07 16:31:50 +02003428 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003429 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003430 case NFA_PREV_ATOM_NO_WIDTH:
3431 start_state = NFA_START_INVISIBLE;
3432 end_state = NFA_END_INVISIBLE;
3433 break;
3434 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3435 start_state = NFA_START_INVISIBLE_NEG;
3436 end_state = NFA_END_INVISIBLE_NEG;
3437 break;
3438 case NFA_PREV_ATOM_JUST_BEFORE:
3439 start_state = NFA_START_INVISIBLE_BEFORE;
3440 end_state = NFA_END_INVISIBLE;
3441 break;
3442 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3443 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3444 end_state = NFA_END_INVISIBLE_NEG;
3445 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003446 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003447 start_state = NFA_START_PATTERN;
3448 end_state = NFA_END_PATTERN;
3449 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003450 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003451
3452 if (before)
3453 n = *++p; /* get the count */
3454
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003455 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003456 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003457 * The \@<= operator: match for the preceding atom.
3458 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003460 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461
3462 if (nfa_calc_size == TRUE)
3463 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003464 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 break;
3466 }
3467 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003468 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003470 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003473 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003474 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003475 if (pattern)
3476 {
3477 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3478 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003479 if (skip == NULL)
3480 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003481 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003482 if (zend == NULL)
3483 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003484 s1->out= skip;
3485 patch(e.out, zend);
3486 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003487 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003488 else
3489 {
3490 patch(e.out, s1);
3491 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003492 if (before)
3493 {
3494 if (n <= 0)
3495 /* See if we can guess the maximum width, it avoids a
3496 * lot of pointless tries. */
3497 n = nfa_max_width(e.start, 0);
3498 s->val = n; /* store the count */
3499 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003500 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003502 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003504#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003505 case NFA_COMPOSING: /* char with composing char */
3506#if 0
3507 /* TODO */
3508 if (regflags & RF_ICOMBINE)
3509 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003510 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003511 }
3512#endif
3513 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003514#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003515
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003516 case NFA_MOPEN: /* \( \) Submatch */
3517 case NFA_MOPEN1:
3518 case NFA_MOPEN2:
3519 case NFA_MOPEN3:
3520 case NFA_MOPEN4:
3521 case NFA_MOPEN5:
3522 case NFA_MOPEN6:
3523 case NFA_MOPEN7:
3524 case NFA_MOPEN8:
3525 case NFA_MOPEN9:
3526#ifdef FEAT_SYN_HL
3527 case NFA_ZOPEN: /* \z( \) Submatch */
3528 case NFA_ZOPEN1:
3529 case NFA_ZOPEN2:
3530 case NFA_ZOPEN3:
3531 case NFA_ZOPEN4:
3532 case NFA_ZOPEN5:
3533 case NFA_ZOPEN6:
3534 case NFA_ZOPEN7:
3535 case NFA_ZOPEN8:
3536 case NFA_ZOPEN9:
3537#endif
3538 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003539 if (nfa_calc_size == TRUE)
3540 {
3541 nstate += 2;
3542 break;
3543 }
3544
3545 mopen = *p;
3546 switch (*p)
3547 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003548 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3549#ifdef FEAT_SYN_HL
3550 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3551 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3552 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3553 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3554 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3555 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3556 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3557 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3558 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3559 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3560#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003561#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003562 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003563#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003564 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003565 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003566 mclose = *p + NSUBEXP;
3567 break;
3568 }
3569
3570 /* Allow "NFA_MOPEN" as a valid postfix representation for
3571 * the empty regexp "". In this case, the NFA will be
3572 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3573 * empty groups of parenthesis, and empty mbyte chars */
3574 if (stackp == stack)
3575 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003576 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003578 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003579 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003581 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003582 patch(list1(&s->out), s1);
3583 PUSH(frag(s, list1(&s1->out)));
3584 break;
3585 }
3586
3587 /* At least one node was emitted before NFA_MOPEN, so
3588 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3589 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003590 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003591 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003592 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003593
Bram Moolenaar525666f2013-06-02 16:40:55 +02003594 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003596 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 patch(e.out, s1);
3598
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003599#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003600 if (mopen == NFA_COMPOSING)
3601 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003603#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003604
3605 PUSH(frag(s, list1(&s1->out)));
3606 break;
3607
Bram Moolenaar5714b802013-05-28 22:03:20 +02003608 case NFA_BACKREF1:
3609 case NFA_BACKREF2:
3610 case NFA_BACKREF3:
3611 case NFA_BACKREF4:
3612 case NFA_BACKREF5:
3613 case NFA_BACKREF6:
3614 case NFA_BACKREF7:
3615 case NFA_BACKREF8:
3616 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003617#ifdef FEAT_SYN_HL
3618 case NFA_ZREF1:
3619 case NFA_ZREF2:
3620 case NFA_ZREF3:
3621 case NFA_ZREF4:
3622 case NFA_ZREF5:
3623 case NFA_ZREF6:
3624 case NFA_ZREF7:
3625 case NFA_ZREF8:
3626 case NFA_ZREF9:
3627#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003628 if (nfa_calc_size == TRUE)
3629 {
3630 nstate += 2;
3631 break;
3632 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003633 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003634 if (s == NULL)
3635 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003636 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003637 if (s1 == NULL)
3638 goto theend;
3639 patch(list1(&s->out), s1);
3640 PUSH(frag(s, list1(&s1->out)));
3641 break;
3642
Bram Moolenaar423532e2013-05-29 21:14:42 +02003643 case NFA_LNUM:
3644 case NFA_LNUM_GT:
3645 case NFA_LNUM_LT:
3646 case NFA_VCOL:
3647 case NFA_VCOL_GT:
3648 case NFA_VCOL_LT:
3649 case NFA_COL:
3650 case NFA_COL_GT:
3651 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003652 case NFA_MARK:
3653 case NFA_MARK_GT:
3654 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003655 {
3656 int n = *++p; /* lnum, col or mark name */
3657
Bram Moolenaar423532e2013-05-29 21:14:42 +02003658 if (nfa_calc_size == TRUE)
3659 {
3660 nstate += 1;
3661 break;
3662 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003663 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003664 if (s == NULL)
3665 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003666 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003667 PUSH(frag(s, list1(&s->out)));
3668 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003669 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003670
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 case NFA_ZSTART:
3672 case NFA_ZEND:
3673 default:
3674 /* Operands */
3675 if (nfa_calc_size == TRUE)
3676 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003677 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 break;
3679 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003680 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003681 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003682 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003683 PUSH(frag(s, list1(&s->out)));
3684 break;
3685
3686 } /* switch(*p) */
3687
3688 } /* for(p = postfix; *p; ++p) */
3689
3690 if (nfa_calc_size == TRUE)
3691 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003692 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003693 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694 }
3695
3696 e = POP();
3697 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003698 {
3699 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 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 +02003701 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003702
3703 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003704 {
3705 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003706 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003707 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003708
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709 matchstate = &state_ptr[istate++]; /* the match state */
3710 matchstate->c = NFA_MATCH;
3711 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003712 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003713
3714 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003715 ret = e.start;
3716
3717theend:
3718 vim_free(stack);
3719 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720
3721#undef POP1
3722#undef PUSH1
3723#undef POP2
3724#undef PUSH2
3725#undef POP
3726#undef PUSH
3727}
3728
Bram Moolenaara2947e22013-06-11 22:44:09 +02003729/*
3730 * After building the NFA program, inspect it to add optimization hints.
3731 */
3732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003733nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003734{
3735 int i;
3736 int c;
3737
3738 for (i = 0; i < prog->nstate; ++i)
3739 {
3740 c = prog->state[i].c;
3741 if (c == NFA_START_INVISIBLE
3742 || c == NFA_START_INVISIBLE_NEG
3743 || c == NFA_START_INVISIBLE_BEFORE
3744 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3745 {
3746 int directly;
3747
3748 /* Do it directly when what follows is possibly the end of the
3749 * match. */
3750 if (match_follows(prog->state[i].out1->out, 0))
3751 directly = TRUE;
3752 else
3753 {
3754 int ch_invisible = failure_chance(prog->state[i].out, 0);
3755 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3756
3757 /* Postpone when the invisible match is expensive or has a
3758 * lower chance of failing. */
3759 if (c == NFA_START_INVISIBLE_BEFORE
3760 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3761 {
3762 /* "before" matches are very expensive when
3763 * unbounded, always prefer what follows then,
3764 * unless what follows will always match.
3765 * Otherwise strongly prefer what follows. */
3766 if (prog->state[i].val <= 0 && ch_follows > 0)
3767 directly = FALSE;
3768 else
3769 directly = ch_follows * 10 < ch_invisible;
3770 }
3771 else
3772 {
3773 /* normal invisible, first do the one with the
3774 * highest failure chance */
3775 directly = ch_follows < ch_invisible;
3776 }
3777 }
3778 if (directly)
3779 /* switch to the _FIRST state */
3780 ++prog->state[i].c;
3781 }
3782 }
3783}
3784
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003785/****************************************************************
3786 * NFA execution code.
3787 ****************************************************************/
3788
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003789typedef struct
3790{
3791 int in_use; /* number of subexpr with useful info */
3792
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003793 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003794 union
3795 {
3796 struct multipos
3797 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003798 linenr_T start_lnum;
3799 linenr_T end_lnum;
3800 colnr_T start_col;
3801 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003802 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003803 struct linepos
3804 {
3805 char_u *start;
3806 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003807 } line[NSUBEXP];
3808 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003809} regsub_T;
3810
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003811typedef struct
3812{
3813 regsub_T norm; /* \( .. \) matches */
3814#ifdef FEAT_SYN_HL
3815 regsub_T synt; /* \z( .. \) matches */
3816#endif
3817} regsubs_T;
3818
Bram Moolenaara2d95102013-06-04 14:23:05 +02003819/* nfa_pim_T stores a Postponed Invisible Match. */
3820typedef struct nfa_pim_S nfa_pim_T;
3821struct nfa_pim_S
3822{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003823 int result; /* NFA_PIM_*, see below */
3824 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003825 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003826 union
3827 {
3828 lpos_T pos;
3829 char_u *ptr;
3830 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003831};
3832
3833/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003834#define NFA_PIM_UNUSED 0 /* pim not used */
3835#define NFA_PIM_TODO 1 /* pim not done yet */
3836#define NFA_PIM_MATCH 2 /* pim executed, matches */
3837#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003838
3839
Bram Moolenaar963fee22013-05-26 21:47:28 +02003840/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003841typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003842{
3843 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003844 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003845 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3846 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003847 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003848} nfa_thread_T;
3849
Bram Moolenaar963fee22013-05-26 21:47:28 +02003850/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851typedef struct
3852{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003853 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003854 int n; /* nr of states currently in "t" */
3855 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003856 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003857 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003858} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003859
Bram Moolenaar5714b802013-05-28 22:03:20 +02003860#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003861static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003862
3863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003864log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003865{
3866 log_subexpr(&subs->norm);
3867# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003868 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003869 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003870# endif
3871}
3872
Bram Moolenaar5714b802013-05-28 22:03:20 +02003873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003874log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003875{
3876 int j;
3877
3878 for (j = 0; j < sub->in_use; j++)
3879 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003880 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003881 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003882 sub->list.multi[j].start_col,
3883 (int)sub->list.multi[j].start_lnum,
3884 sub->list.multi[j].end_col,
3885 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003886 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003887 {
3888 char *s = (char *)sub->list.line[j].start;
3889 char *e = (char *)sub->list.line[j].end;
3890
Bram Moolenaar87953742013-06-05 18:52:40 +02003891 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003892 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003893 s == NULL ? "NULL" : s,
3894 e == NULL ? "NULL" : e);
3895 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003896}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003897
3898 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003899pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003900{
3901 static char buf[30];
3902
3903 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3904 buf[0] = NUL;
3905 else
3906 {
3907 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003908 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003909 }
3910 return buf;
3911}
3912
Bram Moolenaar5714b802013-05-28 22:03:20 +02003913#endif
3914
Bram Moolenaar963fee22013-05-26 21:47:28 +02003915/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003916static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003917#ifdef FEAT_RELTIME
3918static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003919static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003920static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003921#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003922
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003923static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003924static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003925
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003926/*
3927 * Copy postponed invisible match info from "from" to "to".
3928 */
3929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003930copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003931{
3932 to->result = from->result;
3933 to->state = from->state;
3934 copy_sub(&to->subs.norm, &from->subs.norm);
3935#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003936 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003937 copy_sub(&to->subs.synt, &from->subs.synt);
3938#endif
3939 to->end = from->end;
3940}
3941
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003943clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003944{
3945 if (REG_MULTI)
3946 /* Use 0xff to set lnum to -1 */
3947 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003948 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003949 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003950 vim_memset(sub->list.line, 0,
3951 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003952 sub->in_use = 0;
3953}
3954
3955/*
3956 * Copy the submatches from "from" to "to".
3957 */
3958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003959copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003960{
3961 to->in_use = from->in_use;
3962 if (from->in_use > 0)
3963 {
3964 /* Copy the match start and end positions. */
3965 if (REG_MULTI)
3966 mch_memmove(&to->list.multi[0],
3967 &from->list.multi[0],
3968 sizeof(struct multipos) * from->in_use);
3969 else
3970 mch_memmove(&to->list.line[0],
3971 &from->list.line[0],
3972 sizeof(struct linepos) * from->in_use);
3973 }
3974}
3975
3976/*
3977 * Like copy_sub() but exclude the main match.
3978 */
3979 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003980copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003981{
3982 if (to->in_use < from->in_use)
3983 to->in_use = from->in_use;
3984 if (from->in_use > 1)
3985 {
3986 /* Copy the match start and end positions. */
3987 if (REG_MULTI)
3988 mch_memmove(&to->list.multi[1],
3989 &from->list.multi[1],
3990 sizeof(struct multipos) * (from->in_use - 1));
3991 else
3992 mch_memmove(&to->list.line[1],
3993 &from->list.line[1],
3994 sizeof(struct linepos) * (from->in_use - 1));
3995 }
3996}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003997
Bram Moolenaar428e9872013-05-30 17:05:39 +02003998/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003999 * Like copy_sub() but only do the end of the main match if \ze is present.
4000 */
4001 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004002copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004003{
Bram Moolenaar0270f382018-07-17 05:43:58 +02004004 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004005 {
4006 if (REG_MULTI)
4007 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004008 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004009 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004010 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4011 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004012 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004013 }
4014 else
4015 {
4016 if (from->list.line[0].end != NULL)
4017 to->list.line[0].end = from->list.line[0].end;
4018 }
4019 }
4020}
4021
4022/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004023 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004024 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004025 */
4026 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004027sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004028{
4029 int i;
4030 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004031 linenr_T s1;
4032 linenr_T s2;
4033 char_u *sp1;
4034 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004035
4036 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4037 if (REG_MULTI)
4038 {
4039 for (i = 0; i < todo; ++i)
4040 {
4041 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004042 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004043 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004044 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004045 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004046 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004047 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004048 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004049 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004050 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004051 if (s1 != -1 && sub1->list.multi[i].start_col
4052 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004053 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004054
Bram Moolenaar0270f382018-07-17 05:43:58 +02004055 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004056 {
4057 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004058 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004059 else
4060 s1 = -1;
4061 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004062 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004063 else
4064 s2 = -1;
4065 if (s1 != s2)
4066 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004067 if (s1 != -1 && sub1->list.multi[i].end_col
4068 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004069 return FALSE;
4070 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004071 }
4072 }
4073 else
4074 {
4075 for (i = 0; i < todo; ++i)
4076 {
4077 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004078 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004079 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004080 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004081 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004082 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004083 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004084 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004085 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004086 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004087 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004088 {
4089 if (i < sub1->in_use)
4090 sp1 = sub1->list.line[i].end;
4091 else
4092 sp1 = NULL;
4093 if (i < sub2->in_use)
4094 sp2 = sub2->list.line[i].end;
4095 else
4096 sp2 = NULL;
4097 if (sp1 != sp2)
4098 return FALSE;
4099 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004100 }
4101 }
4102
4103 return TRUE;
4104}
4105
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004106#ifdef ENABLE_LOG
4107 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004108report_state(char *action,
4109 regsub_T *sub,
4110 nfa_state_T *state,
4111 int lid,
4112 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004113{
4114 int col;
4115
4116 if (sub->in_use <= 0)
4117 col = -1;
4118 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004119 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004120 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004121 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004122 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004123 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4124 action, abs(state->id), lid, state->c, code, col,
4125 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004126}
4127#endif
4128
Bram Moolenaar43e02982013-06-07 17:31:29 +02004129/*
4130 * Return TRUE if the same state is already in list "l" with the same
4131 * positions as "subs".
4132 */
4133 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004134has_state_with_pos(
4135 nfa_list_T *l, /* runtime state list */
4136 nfa_state_T *state, /* state to update */
4137 regsubs_T *subs, /* pointers to subexpressions */
4138 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004139{
4140 nfa_thread_T *thread;
4141 int i;
4142
4143 for (i = 0; i < l->n; ++i)
4144 {
4145 thread = &l->t[i];
4146 if (thread->state->id == state->id
4147 && sub_equal(&thread->subs.norm, &subs->norm)
4148#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004149 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004150 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004151#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004152 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004153 return TRUE;
4154 }
4155 return FALSE;
4156}
4157
4158/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004159 * Return TRUE if "one" and "two" are equal. That includes when both are not
4160 * set.
4161 */
4162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004163pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004164{
4165 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4166 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4167
4168 if (one_unused)
4169 /* one is unused: equal when two is also unused */
4170 return two_unused;
4171 if (two_unused)
4172 /* one is used and two is not: not equal */
4173 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004174 /* compare the state id */
4175 if (one->state->id != two->state->id)
4176 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004177 /* compare the position */
4178 if (REG_MULTI)
4179 return one->end.pos.lnum == two->end.pos.lnum
4180 && one->end.pos.col == two->end.pos.col;
4181 return one->end.ptr == two->end.ptr;
4182}
4183
4184/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004185 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4186 */
4187 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004188match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004189{
4190 nfa_state_T *state = startstate;
4191
4192 /* avoid too much recursion */
4193 if (depth > 10)
4194 return FALSE;
4195
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004196 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004197 {
4198 switch (state->c)
4199 {
4200 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004201 case NFA_MCLOSE:
4202 case NFA_END_INVISIBLE:
4203 case NFA_END_INVISIBLE_NEG:
4204 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004205 return TRUE;
4206
4207 case NFA_SPLIT:
4208 return match_follows(state->out, depth + 1)
4209 || match_follows(state->out1, depth + 1);
4210
4211 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004212 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004213 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004214 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004215 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004216 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004217 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004218 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004219 case NFA_COMPOSING:
4220 /* skip ahead to next state */
4221 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004222 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004223
4224 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004225 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004226 case NFA_IDENT:
4227 case NFA_SIDENT:
4228 case NFA_KWORD:
4229 case NFA_SKWORD:
4230 case NFA_FNAME:
4231 case NFA_SFNAME:
4232 case NFA_PRINT:
4233 case NFA_SPRINT:
4234 case NFA_WHITE:
4235 case NFA_NWHITE:
4236 case NFA_DIGIT:
4237 case NFA_NDIGIT:
4238 case NFA_HEX:
4239 case NFA_NHEX:
4240 case NFA_OCTAL:
4241 case NFA_NOCTAL:
4242 case NFA_WORD:
4243 case NFA_NWORD:
4244 case NFA_HEAD:
4245 case NFA_NHEAD:
4246 case NFA_ALPHA:
4247 case NFA_NALPHA:
4248 case NFA_LOWER:
4249 case NFA_NLOWER:
4250 case NFA_UPPER:
4251 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004252 case NFA_LOWER_IC:
4253 case NFA_NLOWER_IC:
4254 case NFA_UPPER_IC:
4255 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004256 case NFA_START_COLL:
4257 case NFA_START_NEG_COLL:
4258 case NFA_NEWL:
4259 /* state will advance input */
4260 return FALSE;
4261
4262 default:
4263 if (state->c > 0)
4264 /* state will advance input */
4265 return FALSE;
4266
4267 /* Others: zero-width or possibly zero-width, might still find
4268 * a match at the same position, keep looking. */
4269 break;
4270 }
4271 state = state->out;
4272 }
4273 return FALSE;
4274}
4275
4276
4277/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004278 * Return TRUE if "state" is already in list "l".
4279 */
4280 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004281state_in_list(
4282 nfa_list_T *l, /* runtime state list */
4283 nfa_state_T *state, /* state to update */
4284 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004285{
4286 if (state->lastlist[nfa_ll_index] == l->id)
4287 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004288 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004289 return TRUE;
4290 }
4291 return FALSE;
4292}
4293
Bram Moolenaar16b35782016-09-09 20:29:50 +02004294/* Offset used for "off" by addstate_here(). */
4295#define ADDSTATE_HERE_OFFSET 10
4296
Bram Moolenaard05bf562013-06-30 23:24:08 +02004297/*
4298 * Add "state" and possibly what follows to state list ".".
4299 * Returns "subs_arg", possibly copied into temp_subs.
4300 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004301 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004302addstate(
4303 nfa_list_T *l, /* runtime state list */
4304 nfa_state_T *state, /* state to update */
4305 regsubs_T *subs_arg, /* pointers to subexpressions */
4306 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004307 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004309 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004310 int off = off_arg;
4311 int add_here = FALSE;
4312 int listindex = 0;
4313 int k;
4314 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004315 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004316 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004317 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004318 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004319 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004320 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004321 regsubs_T *subs = subs_arg;
4322 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004323#ifdef ENABLE_LOG
4324 int did_print = FALSE;
4325#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326
Bram Moolenaar16b35782016-09-09 20:29:50 +02004327 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4328 {
4329 add_here = TRUE;
4330 off = 0;
4331 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4332 }
4333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334 switch (state->c)
4335 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 case NFA_NCLOSE:
4337 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004338 case NFA_MCLOSE1:
4339 case NFA_MCLOSE2:
4340 case NFA_MCLOSE3:
4341 case NFA_MCLOSE4:
4342 case NFA_MCLOSE5:
4343 case NFA_MCLOSE6:
4344 case NFA_MCLOSE7:
4345 case NFA_MCLOSE8:
4346 case NFA_MCLOSE9:
4347#ifdef FEAT_SYN_HL
4348 case NFA_ZCLOSE:
4349 case NFA_ZCLOSE1:
4350 case NFA_ZCLOSE2:
4351 case NFA_ZCLOSE3:
4352 case NFA_ZCLOSE4:
4353 case NFA_ZCLOSE5:
4354 case NFA_ZCLOSE6:
4355 case NFA_ZCLOSE7:
4356 case NFA_ZCLOSE8:
4357 case NFA_ZCLOSE9:
4358#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004359 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004360 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004361 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004362 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004363 /* These nodes are not added themselves but their "out" and/or
4364 * "out1" may be added below. */
4365 break;
4366
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004367 case NFA_BOL:
4368 case NFA_BOF:
4369 /* "^" won't match past end-of-line, don't bother trying.
4370 * Except when at the end of the line, or when we are going to the
4371 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004372 if (rex.input > rex.line
4373 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004374 && (nfa_endp == NULL
4375 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004376 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004377 goto skip_add;
4378 /* FALLTHROUGH */
4379
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004380 case NFA_MOPEN1:
4381 case NFA_MOPEN2:
4382 case NFA_MOPEN3:
4383 case NFA_MOPEN4:
4384 case NFA_MOPEN5:
4385 case NFA_MOPEN6:
4386 case NFA_MOPEN7:
4387 case NFA_MOPEN8:
4388 case NFA_MOPEN9:
4389#ifdef FEAT_SYN_HL
4390 case NFA_ZOPEN:
4391 case NFA_ZOPEN1:
4392 case NFA_ZOPEN2:
4393 case NFA_ZOPEN3:
4394 case NFA_ZOPEN4:
4395 case NFA_ZOPEN5:
4396 case NFA_ZOPEN6:
4397 case NFA_ZOPEN7:
4398 case NFA_ZOPEN8:
4399 case NFA_ZOPEN9:
4400#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004401 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004402 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004403 /* These nodes need to be added so that we can bail out when it
4404 * was added to this list before at the same position to avoid an
4405 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004406
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004407 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004408 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004409 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004410 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004411 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004412 * when there is a PIM. For NFA_MATCH check the position,
4413 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004414 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004415 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004416 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004417 /* When called from addstate_here() do insert before
4418 * existing states. */
4419 if (add_here)
4420 {
4421 for (k = 0; k < l->n && k < listindex; ++k)
4422 if (l->t[k].state->id == state->id)
4423 {
4424 found = TRUE;
4425 break;
4426 }
4427 }
4428 if (!add_here || found)
4429 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004430skip_add:
4431#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004432 nfa_set_code(state->c);
4433 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4434 abs(state->id), l->id, state->c, code,
4435 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004436#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004437 return subs;
4438 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004439 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004440
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004441 /* Do not add the state again when it exists with the same
4442 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004443 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004444 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004445 }
4446
Bram Moolenaara0169122013-06-26 18:16:58 +02004447 /* When there are backreferences or PIMs the number of states may
4448 * be (a lot) bigger than anticipated. */
4449 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004450 {
4451 int newlen = l->len * 3 / 2 + 50;
4452
Bram Moolenaard05bf562013-06-30 23:24:08 +02004453 if (subs != &temp_subs)
4454 {
4455 /* "subs" may point into the current array, need to make a
4456 * copy before it becomes invalid. */
4457 copy_sub(&temp_subs.norm, &subs->norm);
4458#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004459 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004460 copy_sub(&temp_subs.synt, &subs->synt);
4461#endif
4462 subs = &temp_subs;
4463 }
4464
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004465 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004466 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4467 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004468 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004469
4470 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004471 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004472 thread = &l->t[l->n++];
4473 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004474 if (pim == NULL)
4475 thread->pim.result = NFA_PIM_UNUSED;
4476 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004477 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004478 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004479 l->has_pim = TRUE;
4480 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004481 copy_sub(&thread->subs.norm, &subs->norm);
4482#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004483 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004484 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004485#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004486#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004487 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004488 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004489#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004490 }
4491
4492#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004493 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004494 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004495#endif
4496 switch (state->c)
4497 {
4498 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004499 break;
4500
4501 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004502 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004503 subs = addstate(l, state->out, subs, pim, off_arg);
4504 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004505 break;
4506
Bram Moolenaar699c1202013-09-25 16:41:54 +02004507 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004508 case NFA_NOPEN:
4509 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004510 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004511 break;
4512
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004513 case NFA_MOPEN:
4514 case NFA_MOPEN1:
4515 case NFA_MOPEN2:
4516 case NFA_MOPEN3:
4517 case NFA_MOPEN4:
4518 case NFA_MOPEN5:
4519 case NFA_MOPEN6:
4520 case NFA_MOPEN7:
4521 case NFA_MOPEN8:
4522 case NFA_MOPEN9:
4523#ifdef FEAT_SYN_HL
4524 case NFA_ZOPEN:
4525 case NFA_ZOPEN1:
4526 case NFA_ZOPEN2:
4527 case NFA_ZOPEN3:
4528 case NFA_ZOPEN4:
4529 case NFA_ZOPEN5:
4530 case NFA_ZOPEN6:
4531 case NFA_ZOPEN7:
4532 case NFA_ZOPEN8:
4533 case NFA_ZOPEN9:
4534#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004535 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004536 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004537 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004538 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004539 sub = &subs->norm;
4540 }
4541#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004542 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004543 {
4544 subidx = state->c - NFA_ZOPEN;
4545 sub = &subs->synt;
4546 }
4547#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004548 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004549 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004550 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004551 sub = &subs->norm;
4552 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004554 /* avoid compiler warnings */
4555 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004556 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004557
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004558 /* Set the position (with "off" added) in the subexpression. Save
4559 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 if (REG_MULTI)
4561 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004562 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004563 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004564 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004565 save_in_use = -1;
4566 }
4567 else
4568 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004569 save_in_use = sub->in_use;
4570 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004571 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004572 sub->list.multi[i].start_lnum = -1;
4573 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004574 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004575 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004576 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004577 if (off == -1)
4578 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004579 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004580 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004581 }
4582 else
4583 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004584 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004585 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004586 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004587 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004588 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589 }
4590 else
4591 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004592 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004593 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004594 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004595 save_in_use = -1;
4596 }
4597 else
4598 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004599 save_in_use = sub->in_use;
4600 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004601 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004602 sub->list.line[i].start = NULL;
4603 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004604 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004605 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004606 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004607 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004608 }
4609
Bram Moolenaar16b35782016-09-09 20:29:50 +02004610 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004611 /* "subs" may have changed, need to set "sub" again */
4612#ifdef FEAT_SYN_HL
4613 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4614 sub = &subs->synt;
4615 else
4616#endif
4617 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004619 if (save_in_use == -1)
4620 {
4621 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004622 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004623 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004624 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004626 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004627 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004628 break;
4629
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004630 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004631 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004632 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004633 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004634 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004635 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004636 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004637 break;
4638 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004639 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004640 case NFA_MCLOSE1:
4641 case NFA_MCLOSE2:
4642 case NFA_MCLOSE3:
4643 case NFA_MCLOSE4:
4644 case NFA_MCLOSE5:
4645 case NFA_MCLOSE6:
4646 case NFA_MCLOSE7:
4647 case NFA_MCLOSE8:
4648 case NFA_MCLOSE9:
4649#ifdef FEAT_SYN_HL
4650 case NFA_ZCLOSE:
4651 case NFA_ZCLOSE1:
4652 case NFA_ZCLOSE2:
4653 case NFA_ZCLOSE3:
4654 case NFA_ZCLOSE4:
4655 case NFA_ZCLOSE5:
4656 case NFA_ZCLOSE6:
4657 case NFA_ZCLOSE7:
4658 case NFA_ZCLOSE8:
4659 case NFA_ZCLOSE9:
4660#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004661 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004662 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004663 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004664 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004665 sub = &subs->norm;
4666 }
4667#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004668 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004669 {
4670 subidx = state->c - NFA_ZCLOSE;
4671 sub = &subs->synt;
4672 }
4673#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004674 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004675 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004676 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004677 sub = &subs->norm;
4678 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004680 /* We don't fill in gaps here, there must have been an MOPEN that
4681 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004682 save_in_use = sub->in_use;
4683 if (sub->in_use <= subidx)
4684 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 if (REG_MULTI)
4686 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004687 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004688 if (off == -1)
4689 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004690 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004691 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004692 }
4693 else
4694 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004695 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004696 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004697 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004698 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004699 /* avoid compiler warnings */
4700 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701 }
4702 else
4703 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004704 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004705 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004706 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004707 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 }
4709
Bram Moolenaar16b35782016-09-09 20:29:50 +02004710 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004711 /* "subs" may have changed, need to set "sub" again */
4712#ifdef FEAT_SYN_HL
4713 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4714 sub = &subs->synt;
4715 else
4716#endif
4717 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004718
4719 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004720 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004721 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004722 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004723 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 break;
4725 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004726 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004727}
4728
4729/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004730 * Like addstate(), but the new state(s) are put at position "*ip".
4731 * Used for zero-width matches, next state to use is the added one.
4732 * This makes sure the order of states to be tried does not change, which
4733 * matters for alternatives.
4734 */
4735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004736addstate_here(
4737 nfa_list_T *l, /* runtime state list */
4738 nfa_state_T *state, /* state to update */
4739 regsubs_T *subs, /* pointers to subexpressions */
4740 nfa_pim_T *pim, /* postponed look-behind match */
4741 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004742{
4743 int tlen = l->n;
4744 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004745 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004746
Bram Moolenaar16b35782016-09-09 20:29:50 +02004747 /* First add the state(s) at the end, so that we know how many there are.
4748 * Pass the listidx as offset (avoids adding another argument to
4749 * addstate(). */
4750 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004751
Bram Moolenaar4b417062013-05-25 20:19:50 +02004752 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004753 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004754 return;
4755
4756 /* re-order to put the new state at the current position */
4757 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004758 if (count == 0)
4759 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004760 if (count == 1)
4761 {
4762 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004763 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004764 }
4765 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004766 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004767 if (l->n + count - 1 >= l->len)
4768 {
4769 /* not enough space to move the new states, reallocate the list
4770 * and move the states to the right position */
4771 nfa_thread_T *newl;
4772
4773 l->len = l->len * 3 / 2 + 50;
4774 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4775 if (newl == NULL)
4776 return;
4777 mch_memmove(&(newl[0]),
4778 &(l->t[0]),
4779 sizeof(nfa_thread_T) * listidx);
4780 mch_memmove(&(newl[listidx]),
4781 &(l->t[l->n - count]),
4782 sizeof(nfa_thread_T) * count);
4783 mch_memmove(&(newl[listidx + count]),
4784 &(l->t[listidx + 1]),
4785 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4786 vim_free(l->t);
4787 l->t = newl;
4788 }
4789 else
4790 {
4791 /* make space for new states, then move them from the
4792 * end to the current position */
4793 mch_memmove(&(l->t[listidx + count]),
4794 &(l->t[listidx + 1]),
4795 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4796 mch_memmove(&(l->t[listidx]),
4797 &(l->t[l->n - 1]),
4798 sizeof(nfa_thread_T) * count);
4799 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004800 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004801 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004802 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004803}
4804
4805/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004806 * Check character class "class" against current character c.
4807 */
4808 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004809check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004810{
4811 switch (class)
4812 {
4813 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004814 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004815 return OK;
4816 break;
4817 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004818 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004819 return OK;
4820 break;
4821 case NFA_CLASS_BLANK:
4822 if (c == ' ' || c == '\t')
4823 return OK;
4824 break;
4825 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004826 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004827 return OK;
4828 break;
4829 case NFA_CLASS_DIGIT:
4830 if (VIM_ISDIGIT(c))
4831 return OK;
4832 break;
4833 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004834 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004835 return OK;
4836 break;
4837 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004838 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004839 return OK;
4840 break;
4841 case NFA_CLASS_PRINT:
4842 if (vim_isprintc(c))
4843 return OK;
4844 break;
4845 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004846 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004847 return OK;
4848 break;
4849 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004850 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004851 return OK;
4852 break;
4853 case NFA_CLASS_UPPER:
4854 if (MB_ISUPPER(c))
4855 return OK;
4856 break;
4857 case NFA_CLASS_XDIGIT:
4858 if (vim_isxdigit(c))
4859 return OK;
4860 break;
4861 case NFA_CLASS_TAB:
4862 if (c == '\t')
4863 return OK;
4864 break;
4865 case NFA_CLASS_RETURN:
4866 if (c == '\r')
4867 return OK;
4868 break;
4869 case NFA_CLASS_BACKSPACE:
4870 if (c == '\b')
4871 return OK;
4872 break;
4873 case NFA_CLASS_ESCAPE:
4874 if (c == '\033')
4875 return OK;
4876 break;
4877
4878 default:
4879 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004881 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004882 }
4883 return FAIL;
4884}
4885
Bram Moolenaar5714b802013-05-28 22:03:20 +02004886/*
4887 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004888 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004889 */
4890 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004891match_backref(
4892 regsub_T *sub, /* pointers to subexpressions */
4893 int subidx,
4894 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004895{
4896 int len;
4897
4898 if (sub->in_use <= subidx)
4899 {
4900retempty:
4901 /* backref was not set, match an empty string */
4902 *bytelen = 0;
4903 return TRUE;
4904 }
4905
4906 if (REG_MULTI)
4907 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004908 if (sub->list.multi[subidx].start_lnum < 0
4909 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004910 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004911 if (sub->list.multi[subidx].start_lnum == rex.lnum
4912 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004913 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004914 len = sub->list.multi[subidx].end_col
4915 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004916 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4917 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004918 {
4919 *bytelen = len;
4920 return TRUE;
4921 }
4922 }
4923 else
4924 {
4925 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004926 sub->list.multi[subidx].start_lnum,
4927 sub->list.multi[subidx].start_col,
4928 sub->list.multi[subidx].end_lnum,
4929 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004930 bytelen) == RA_MATCH)
4931 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004932 }
4933 }
4934 else
4935 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004936 if (sub->list.line[subidx].start == NULL
4937 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004938 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004939 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004940 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004941 {
4942 *bytelen = len;
4943 return TRUE;
4944 }
4945 }
4946 return FALSE;
4947}
4948
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004949#ifdef FEAT_SYN_HL
4950
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004951/*
4952 * Check for a match with \z subexpression "subidx".
4953 * Return TRUE if it matches.
4954 */
4955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004956match_zref(
4957 int subidx,
4958 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004959{
4960 int len;
4961
4962 cleanup_zsubexpr();
4963 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4964 {
4965 /* backref was not set, match an empty string */
4966 *bytelen = 0;
4967 return TRUE;
4968 }
4969
4970 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004971 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004972 {
4973 *bytelen = len;
4974 return TRUE;
4975 }
4976 return FALSE;
4977}
4978#endif
4979
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004980/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004981 * Save list IDs for all NFA states of "prog" into "list".
4982 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004983 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004984 */
4985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004986nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004987{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004988 int i;
4989 nfa_state_T *p;
4990
4991 /* Order in the list is reverse, it's a bit faster that way. */
4992 p = &prog->state[0];
4993 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004994 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004995 list[i] = p->lastlist[1];
4996 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004997 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004998 }
4999}
5000
5001/*
5002 * Restore list IDs from "list" to all NFA states.
5003 */
5004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005005nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005006{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005007 int i;
5008 nfa_state_T *p;
5009
5010 p = &prog->state[0];
5011 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005012 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005013 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005014 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015 }
5016}
5017
Bram Moolenaar423532e2013-05-29 21:14:42 +02005018 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005019nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005020{
5021 if (op == 1) return pos > val;
5022 if (op == 2) return pos < val;
5023 return val == pos;
5024}
5025
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005026static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005027
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005028/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005029 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005030 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5031 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005032 */
5033 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005034recursive_regmatch(
5035 nfa_state_T *state,
5036 nfa_pim_T *pim,
5037 nfa_regprog_T *prog,
5038 regsubs_T *submatch,
5039 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005040 int **listids,
5041 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005042{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005043 int save_reginput_col = (int)(rex.input - rex.line);
5044 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005045 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005046 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005047 save_se_T *save_nfa_endp = nfa_endp;
5048 save_se_T endpos;
5049 save_se_T *endposp = NULL;
5050 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005051 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005052
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005053 if (pim != NULL)
5054 {
5055 /* start at the position where the postponed match was */
5056 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005057 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005058 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005059 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005060 }
5061
Bram Moolenaardecd9542013-06-07 16:31:50 +02005062 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005063 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5064 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5065 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005066 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005067 /* The recursive match must end at the current position. When "pim" is
5068 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005069 endposp = &endpos;
5070 if (REG_MULTI)
5071 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005072 if (pim == NULL)
5073 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005074 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5075 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005076 }
5077 else
5078 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005079 }
5080 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005081 {
5082 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005083 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005084 else
5085 endpos.se_u.ptr = pim->end.ptr;
5086 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005087
5088 /* Go back the specified number of bytes, or as far as the
5089 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005090 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005091 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092 if (state->val <= 0)
5093 {
5094 if (REG_MULTI)
5095 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005096 rex.line = reg_getline(--rex.lnum);
5097 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005098 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005099 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005100 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005101 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005102 }
5103 else
5104 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005105 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005106 {
5107 /* Not enough bytes in this line, go to end of
5108 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005109 rex.line = reg_getline(--rex.lnum);
5110 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005111 {
5112 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005113 rex.line = reg_getline(++rex.lnum);
5114 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005115 }
5116 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005117 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005118 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005119 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005120 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005121 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005122#ifdef FEAT_MBYTE
5123 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005124 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005125#endif
5126 }
5127 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005128 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129 }
5130 }
5131
Bram Moolenaarf46da702013-06-02 22:37:42 +02005132#ifdef ENABLE_LOG
5133 if (log_fd != stderr)
5134 fclose(log_fd);
5135 log_fd = NULL;
5136#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005137 /* Have to clear the lastlist field of the NFA nodes, so that
5138 * nfa_regmatch() and addstate() can run properly after recursion. */
5139 if (nfa_ll_index == 1)
5140 {
5141 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5142 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005143 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005144 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005145 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005146 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005147 if (*listids == NULL)
5148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005149 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005150 return 0;
5151 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005152 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005153 }
5154 nfa_save_listids(prog, *listids);
5155 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005156 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005157 }
5158 else
5159 {
5160 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005161 * entry. Make sure rex.nfa_listid is different from a previous
5162 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005163 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005164 if (rex.nfa_listid <= rex.nfa_alt_listid)
5165 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005166 }
5167
5168 /* Call nfa_regmatch() to check if the current concat matches at this
5169 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005170 nfa_endp = endposp;
5171 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005172
5173 if (need_restore)
5174 nfa_restore_listids(prog, *listids);
5175 else
5176 {
5177 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005178 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005179 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005180
5181 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005182 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005183 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005184 rex.line = reg_getline(rex.lnum);
5185 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005186 if (result != NFA_TOO_EXPENSIVE)
5187 {
5188 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005189 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005190 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005191 nfa_endp = save_nfa_endp;
5192
5193#ifdef ENABLE_LOG
5194 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5195 if (log_fd != NULL)
5196 {
5197 fprintf(log_fd, "****************************\n");
5198 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5199 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5200 fprintf(log_fd, "****************************\n");
5201 }
5202 else
5203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005204 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005205 log_fd = stderr;
5206 }
5207#endif
5208
5209 return result;
5210}
5211
Bram Moolenaara2d95102013-06-04 14:23:05 +02005212/*
5213 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005214 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005215 * NFA_ANY: 1
5216 * specific character: 99
5217 */
5218 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005219failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005220{
5221 int c = state->c;
5222 int l, r;
5223
5224 /* detect looping */
5225 if (depth > 4)
5226 return 1;
5227
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005228 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005229 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005230 case NFA_SPLIT:
5231 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5232 /* avoid recursive stuff */
5233 return 1;
5234 /* two alternatives, use the lowest failure chance */
5235 l = failure_chance(state->out, depth + 1);
5236 r = failure_chance(state->out1, depth + 1);
5237 return l < r ? l : r;
5238
5239 case NFA_ANY:
5240 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005241 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005242
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005243 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005244 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005245 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005246 /* empty match works always */
5247 return 0;
5248
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005249 case NFA_START_INVISIBLE:
5250 case NFA_START_INVISIBLE_FIRST:
5251 case NFA_START_INVISIBLE_NEG:
5252 case NFA_START_INVISIBLE_NEG_FIRST:
5253 case NFA_START_INVISIBLE_BEFORE:
5254 case NFA_START_INVISIBLE_BEFORE_FIRST:
5255 case NFA_START_INVISIBLE_BEFORE_NEG:
5256 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5257 case NFA_START_PATTERN:
5258 /* recursive regmatch is expensive, use low failure chance */
5259 return 5;
5260
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005261 case NFA_BOL:
5262 case NFA_EOL:
5263 case NFA_BOF:
5264 case NFA_EOF:
5265 case NFA_NEWL:
5266 return 99;
5267
5268 case NFA_BOW:
5269 case NFA_EOW:
5270 return 90;
5271
5272 case NFA_MOPEN:
5273 case NFA_MOPEN1:
5274 case NFA_MOPEN2:
5275 case NFA_MOPEN3:
5276 case NFA_MOPEN4:
5277 case NFA_MOPEN5:
5278 case NFA_MOPEN6:
5279 case NFA_MOPEN7:
5280 case NFA_MOPEN8:
5281 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005282#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005283 case NFA_ZOPEN:
5284 case NFA_ZOPEN1:
5285 case NFA_ZOPEN2:
5286 case NFA_ZOPEN3:
5287 case NFA_ZOPEN4:
5288 case NFA_ZOPEN5:
5289 case NFA_ZOPEN6:
5290 case NFA_ZOPEN7:
5291 case NFA_ZOPEN8:
5292 case NFA_ZOPEN9:
5293 case NFA_ZCLOSE:
5294 case NFA_ZCLOSE1:
5295 case NFA_ZCLOSE2:
5296 case NFA_ZCLOSE3:
5297 case NFA_ZCLOSE4:
5298 case NFA_ZCLOSE5:
5299 case NFA_ZCLOSE6:
5300 case NFA_ZCLOSE7:
5301 case NFA_ZCLOSE8:
5302 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005303#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005304 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005305 case NFA_MCLOSE1:
5306 case NFA_MCLOSE2:
5307 case NFA_MCLOSE3:
5308 case NFA_MCLOSE4:
5309 case NFA_MCLOSE5:
5310 case NFA_MCLOSE6:
5311 case NFA_MCLOSE7:
5312 case NFA_MCLOSE8:
5313 case NFA_MCLOSE9:
5314 case NFA_NCLOSE:
5315 return failure_chance(state->out, depth + 1);
5316
5317 case NFA_BACKREF1:
5318 case NFA_BACKREF2:
5319 case NFA_BACKREF3:
5320 case NFA_BACKREF4:
5321 case NFA_BACKREF5:
5322 case NFA_BACKREF6:
5323 case NFA_BACKREF7:
5324 case NFA_BACKREF8:
5325 case NFA_BACKREF9:
5326#ifdef FEAT_SYN_HL
5327 case NFA_ZREF1:
5328 case NFA_ZREF2:
5329 case NFA_ZREF3:
5330 case NFA_ZREF4:
5331 case NFA_ZREF5:
5332 case NFA_ZREF6:
5333 case NFA_ZREF7:
5334 case NFA_ZREF8:
5335 case NFA_ZREF9:
5336#endif
5337 /* backreferences don't match in many places */
5338 return 94;
5339
5340 case NFA_LNUM_GT:
5341 case NFA_LNUM_LT:
5342 case NFA_COL_GT:
5343 case NFA_COL_LT:
5344 case NFA_VCOL_GT:
5345 case NFA_VCOL_LT:
5346 case NFA_MARK_GT:
5347 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005348 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005349 /* before/after positions don't match very often */
5350 return 85;
5351
5352 case NFA_LNUM:
5353 return 90;
5354
5355 case NFA_CURSOR:
5356 case NFA_COL:
5357 case NFA_VCOL:
5358 case NFA_MARK:
5359 /* specific positions rarely match */
5360 return 98;
5361
5362 case NFA_COMPOSING:
5363 return 95;
5364
5365 default:
5366 if (c > 0)
5367 /* character match fails often */
5368 return 95;
5369 }
5370
5371 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005372 return 50;
5373}
5374
Bram Moolenaarf46da702013-06-02 22:37:42 +02005375/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005376 * Skip until the char "c" we know a match must start with.
5377 */
5378 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005379skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005380{
5381 char_u *s;
5382
5383 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005384 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005385#ifdef FEAT_MBYTE
5386 && !has_mbyte
5387#endif
5388 )
Bram Moolenaar0270f382018-07-17 05:43:58 +02005389 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005390 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005391 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005392 if (s == NULL)
5393 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005394 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005395 return OK;
5396}
5397
5398/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005399 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005400 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005401 * Returns zero for no match, 1 for a match.
5402 */
5403 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005404find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005405{
5406 colnr_T col = startcol;
5407 int c1, c2;
5408 int len1, len2;
5409 int match;
5410
5411 for (;;)
5412 {
5413 match = TRUE;
5414 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5415 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5416 {
5417 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005418 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005419 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005420 {
5421 match = FALSE;
5422 break;
5423 }
5424 len2 += MB_CHAR2LEN(c2);
5425 }
5426 if (match
5427#ifdef FEAT_MBYTE
5428 /* check that no composing char follows */
5429 && !(enc_utf8
Bram Moolenaar0270f382018-07-17 05:43:58 +02005430 && utf_iscomposing(PTR2CHAR(rex.line + col + len2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005431#endif
5432 )
5433 {
5434 cleanup_subexpr();
5435 if (REG_MULTI)
5436 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005437 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005438 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005439 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005440 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005441 }
5442 else
5443 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005444 rex.reg_startp[0] = rex.line + col;
5445 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005446 }
5447 return 1L;
5448 }
5449
5450 /* Try finding regstart after the current match. */
5451 col += MB_CHAR2LEN(regstart); /* skip regstart */
5452 if (skip_to_start(regstart, &col) == FAIL)
5453 break;
5454 }
5455 return 0L;
5456}
5457
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005458#ifdef FEAT_RELTIME
5459 static int
5460nfa_did_time_out()
5461{
5462 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5463 {
5464 if (nfa_timed_out != NULL)
5465 *nfa_timed_out = TRUE;
5466 return TRUE;
5467 }
5468 return FALSE;
5469}
5470#endif
5471
Bram Moolenaar473de612013-06-08 18:19:48 +02005472/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005473 * Main matching routine.
5474 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005475 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005477 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005478 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005479 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005480 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005481 * Note: Caller must ensure that: start != NULL.
5482 */
5483 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005484nfa_regmatch(
5485 nfa_regprog_T *prog,
5486 nfa_state_T *start,
5487 regsubs_T *submatch,
5488 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005489{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005490 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005491 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005492 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005493 int go_to_nextline = FALSE;
5494 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005495 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005496 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005497 nfa_list_T *thislist;
5498 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005499 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005500 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005501 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005502 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005503 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005504 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005505 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005506#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005507 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005508#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005509
Bram Moolenaar41f12052013-08-25 17:01:42 +02005510 /* Some patterns may take a long time to match, especially when using
5511 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5512 fast_breakcheck();
5513 if (got_int)
5514 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005515#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005516 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005517 return FALSE;
5518#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005519
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005520#ifdef NFA_REGEXP_DEBUG_LOG
5521 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5522 if (debug == NULL)
5523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005524 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005525 return FALSE;
5526 }
5527#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005528 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005530 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005531 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005532
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005533 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005534 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005535 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005536 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005537 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005539
5540#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005541 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005542 if (log_fd != NULL)
5543 {
5544 fprintf(log_fd, "**********************************\n");
5545 nfa_set_code(start->c);
5546 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5547 abs(start->id), code);
5548 fprintf(log_fd, "**********************************\n");
5549 }
5550 else
5551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553 log_fd = stderr;
5554 }
5555#endif
5556
5557 thislist = &list[0];
5558 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005559 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560 nextlist = &list[1];
5561 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005562 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005564 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005566 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005567
5568 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5569 * it's the first MOPEN. */
5570 if (toplevel)
5571 {
5572 if (REG_MULTI)
5573 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005574 m->norm.list.multi[0].start_lnum = rex.lnum;
5575 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005576 }
5577 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005578 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005579 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005580 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005581 }
5582 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005583 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005584
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005585#define ADD_STATE_IF_MATCH(state) \
5586 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005587 add_state = state->out; \
5588 add_off = clen; \
5589 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005590
5591 /*
5592 * Run for each character.
5593 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005594 for (;;)
5595 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005596 int curc;
5597 int clen;
5598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599#ifdef FEAT_MBYTE
5600 if (has_mbyte)
5601 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005602 curc = (*mb_ptr2char)(rex.input);
5603 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604 }
5605 else
5606#endif
5607 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005608 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005609 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005611 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005612 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005613 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005614 go_to_nextline = FALSE;
5615 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005616
5617 /* swap lists */
5618 thislist = &list[flag];
5619 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005620 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005621 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005622 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005623 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005624 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005625# ifdef FEAT_EVAL
5626 || nfa_fail_for_testing
5627# endif
5628 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005629 {
5630 /* too many states, retry with old engine */
5631 nfa_match = NFA_TOO_EXPENSIVE;
5632 goto theend;
5633 }
5634
Bram Moolenaar0270f382018-07-17 05:43:58 +02005635 thislist->id = rex.nfa_listid;
5636 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637
5638#ifdef ENABLE_LOG
5639 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005640 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005641 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005642 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005643 {
5644 int i;
5645
5646 for (i = 0; i < thislist->n; i++)
5647 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5648 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005649 fprintf(log_fd, "\n");
5650#endif
5651
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005652#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653 fprintf(debug, "\n-------------------\n");
5654#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005655 /*
5656 * If the state lists are empty we can stop.
5657 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005658 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005659 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005660
5661 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005662 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005663 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005664 /* If the list gets very long there probably is something wrong.
5665 * At least allow interrupting with CTRL-C. */
5666 fast_breakcheck();
5667 if (got_int)
5668 break;
5669#ifdef FEAT_RELTIME
5670 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5671 {
5672 nfa_time_count = 0;
5673 if (nfa_did_time_out())
5674 break;
5675 }
5676#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005677 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005678
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005679#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680 nfa_set_code(t->state->c);
5681 fprintf(debug, "%s, ", code);
5682#endif
5683#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005684 {
5685 int col;
5686
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005687 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005688 col = -1;
5689 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005690 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005691 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005692 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005693 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005694 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005695 abs(t->state->id), (int)t->state->c, code, col,
5696 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005697 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698#endif
5699
5700 /*
5701 * Handle the possible codes of the current state.
5702 * The most important is NFA_MATCH.
5703 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005704 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005705 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005706 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005707 switch (t->state->c)
5708 {
5709 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005710 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005711#ifdef FEAT_MBYTE
5712 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005713 * rex.reg_icombine is not set, that is not really a match. */
5714 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005715 break;
5716#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005717 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005718 copy_sub(&submatch->norm, &t->subs.norm);
5719#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005720 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005721 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005722#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005723#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005724 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005725#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005726 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005727 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005728 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005729 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005730 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005731 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005732 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005733 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005734
5735 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005736 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005737 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005738 /*
5739 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005740 * NFA_START_INVISIBLE_BEFORE node.
5741 * They surround a zero-width group, used with "\@=", "\&",
5742 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005743 * If we got here, it means that the current "invisible" group
5744 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005745 * nfa_regmatch(). For a look-behind match only when it ends
5746 * in the position in "nfa_endp".
5747 * Submatches are stored in *m, and used in the parent call.
5748 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005749#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005750 if (nfa_endp != NULL)
5751 {
5752 if (REG_MULTI)
5753 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005754 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005755 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005756 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005757 nfa_endp->se_u.pos.col);
5758 else
5759 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005760 (int)(rex.input - rex.line),
5761 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005762 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005763#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005764 /* If "nfa_endp" is set it's only a match if it ends at
5765 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005766 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005767 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5768 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005769 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005770 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005771 break;
5772
5773 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005774 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005775 {
5776 copy_sub(&m->norm, &t->subs.norm);
5777#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005778 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005779 copy_sub(&m->synt, &t->subs.synt);
5780#endif
5781 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005782#ifdef ENABLE_LOG
5783 fprintf(log_fd, "Match found:\n");
5784 log_subsexpr(m);
5785#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005786 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005787 /* See comment above at "goto nextchar". */
5788 if (nextlist->n == 0)
5789 clen = 0;
5790 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005791
5792 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005793 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005794 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005795 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005796 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005797 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005798 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005799 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005800 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005801#ifdef ENABLE_LOG
5802 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5803 failure_chance(t->state->out, 0),
5804 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005805#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005806 /* Do it directly if there already is a PIM or when
5807 * nfa_postprocess() detected it will work better. */
5808 if (t->pim.result != NFA_PIM_UNUSED
5809 || t->state->c == NFA_START_INVISIBLE_FIRST
5810 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5811 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5812 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005813 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005814 int in_use = m->norm.in_use;
5815
Bram Moolenaar699c1202013-09-25 16:41:54 +02005816 /* Copy submatch info for the recursive call, opposite
5817 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005818 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005819#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005820 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005821 copy_sub_off(&m->synt, &t->subs.synt);
5822#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005823
Bram Moolenaara2d95102013-06-04 14:23:05 +02005824 /*
5825 * First try matching the invisible match, then what
5826 * follows.
5827 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005828 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005829 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005830 if (result == NFA_TOO_EXPENSIVE)
5831 {
5832 nfa_match = result;
5833 goto theend;
5834 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005835
Bram Moolenaardecd9542013-06-07 16:31:50 +02005836 /* for \@! and \@<! it is a match when the result is
5837 * FALSE */
5838 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005839 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5840 || t->state->c
5841 == NFA_START_INVISIBLE_BEFORE_NEG
5842 || t->state->c
5843 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005844 {
5845 /* Copy submatch info from the recursive call */
5846 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005847#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005848 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005849 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005850#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005851 /* If the pattern has \ze and it matched in the
5852 * sub pattern, use it. */
5853 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005854
Bram Moolenaara2d95102013-06-04 14:23:05 +02005855 /* t->state->out1 is the corresponding
5856 * END_INVISIBLE node; Add its out to the current
5857 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005858 add_here = TRUE;
5859 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005860 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005861 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005862 }
5863 else
5864 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005865 nfa_pim_T pim;
5866
Bram Moolenaara2d95102013-06-04 14:23:05 +02005867 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005868 * First try matching what follows. Only if a match
5869 * is found verify the invisible match matches. Add a
5870 * nfa_pim_T to the following states, it contains info
5871 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005872 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005873 pim.state = t->state;
5874 pim.result = NFA_PIM_TODO;
5875 pim.subs.norm.in_use = 0;
5876#ifdef FEAT_SYN_HL
5877 pim.subs.synt.in_use = 0;
5878#endif
5879 if (REG_MULTI)
5880 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005881 pim.end.pos.col = (int)(rex.input - rex.line);
5882 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005883 }
5884 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005885 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005886
5887 /* t->state->out1 is the corresponding END_INVISIBLE
5888 * node; Add its out to the current list (zero-width
5889 * match). */
5890 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005891 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005893 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005894 break;
5895
Bram Moolenaar87953742013-06-05 18:52:40 +02005896 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005897 {
5898 nfa_state_T *skip = NULL;
5899#ifdef ENABLE_LOG
5900 int skip_lid = 0;
5901#endif
5902
5903 /* There is no point in trying to match the pattern if the
5904 * output state is not going to be added to the list. */
5905 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5906 {
5907 skip = t->state->out1->out;
5908#ifdef ENABLE_LOG
5909 skip_lid = nextlist->id;
5910#endif
5911 }
5912 else if (state_in_list(nextlist,
5913 t->state->out1->out->out, &t->subs))
5914 {
5915 skip = t->state->out1->out->out;
5916#ifdef ENABLE_LOG
5917 skip_lid = nextlist->id;
5918#endif
5919 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005920 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005921 t->state->out1->out->out, &t->subs))
5922 {
5923 skip = t->state->out1->out->out;
5924#ifdef ENABLE_LOG
5925 skip_lid = thislist->id;
5926#endif
5927 }
5928 if (skip != NULL)
5929 {
5930#ifdef ENABLE_LOG
5931 nfa_set_code(skip->c);
5932 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5933 abs(skip->id), skip_lid, skip->c, code);
5934#endif
5935 break;
5936 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005937 /* Copy submatch info to the recursive call, opposite of what
5938 * happens afterwards. */
5939 copy_sub_off(&m->norm, &t->subs.norm);
5940#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005941 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005942 copy_sub_off(&m->synt, &t->subs.synt);
5943#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005944
Bram Moolenaar87953742013-06-05 18:52:40 +02005945 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005946 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005947 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005948 if (result == NFA_TOO_EXPENSIVE)
5949 {
5950 nfa_match = result;
5951 goto theend;
5952 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005953 if (result)
5954 {
5955 int bytelen;
5956
5957#ifdef ENABLE_LOG
5958 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5959 log_subsexpr(m);
5960#endif
5961 /* Copy submatch info from the recursive call */
5962 copy_sub_off(&t->subs.norm, &m->norm);
5963#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005964 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005965 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005966#endif
5967 /* Now we need to skip over the matched text and then
5968 * continue with what follows. */
5969 if (REG_MULTI)
5970 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005971 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02005972 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02005973 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005974 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02005975
5976#ifdef ENABLE_LOG
5977 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5978#endif
5979 if (bytelen == 0)
5980 {
5981 /* empty match, output of corresponding
5982 * NFA_END_PATTERN/NFA_SKIP to be used at current
5983 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005984 add_here = TRUE;
5985 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005986 }
5987 else if (bytelen <= clen)
5988 {
5989 /* match current character, output of corresponding
5990 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005991 add_state = t->state->out1->out->out;
5992 add_off = clen;
5993 }
5994 else
5995 {
5996 /* skip over the matched characters, set character
5997 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005998 add_state = t->state->out1->out;
5999 add_off = bytelen;
6000 add_count = bytelen - clen;
6001 }
6002 }
6003 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006004 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006005
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006006 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006007 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006008 {
6009 add_here = TRUE;
6010 add_state = t->state->out;
6011 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006012 break;
6013
6014 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006015 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006016 {
6017 add_here = TRUE;
6018 add_state = t->state->out;
6019 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006020 break;
6021
6022 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006023 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006024
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006025 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006026 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006027#ifdef FEAT_MBYTE
6028 else if (has_mbyte)
6029 {
6030 int this_class;
6031
6032 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006033 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006034 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006035 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006036 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006037 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006038 }
6039#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006040 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006041 || (rex.input > rex.line
6042 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006043 result = FALSE;
6044 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006045 {
6046 add_here = TRUE;
6047 add_state = t->state->out;
6048 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006049 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006050
6051 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006052 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006053 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006054 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006055#ifdef FEAT_MBYTE
6056 else if (has_mbyte)
6057 {
6058 int this_class, prev_class;
6059
6060 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006061 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006062 prev_class = reg_prev_class();
6063 if (this_class == prev_class
6064 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006065 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006066 }
6067#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006068 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6069 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006070 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006071 result = FALSE;
6072 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006073 {
6074 add_here = TRUE;
6075 add_state = t->state->out;
6076 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006077 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078
Bram Moolenaar4b780632013-05-31 22:14:52 +02006079 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006080 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006081 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006082 {
6083 add_here = TRUE;
6084 add_state = t->state->out;
6085 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006086 break;
6087
6088 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006089 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006090 {
6091 add_here = TRUE;
6092 add_state = t->state->out;
6093 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006094 break;
6095
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006096#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006098 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006099 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006100 int len = 0;
6101 nfa_state_T *end;
6102 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006103 int cchars[MAX_MCO];
6104 int ccount = 0;
6105 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006106
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006107 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006108 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006109 if (utf_iscomposing(sta->c))
6110 {
6111 /* Only match composing character(s), ignore base
6112 * character. Used for ".{composing}" and "{composing}"
6113 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006114 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006115 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006116 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006118 /* If \Z was present, then ignore composing characters.
6119 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006120 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006121 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006122 else
6123 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006124 while (sta->c != NFA_END_COMPOSING)
6125 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006127
6128 /* Check base character matches first, unless ignored. */
6129 else if (len > 0 || mc == sta->c)
6130 {
6131 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006132 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006133 len += mb_char2len(mc);
6134 sta = sta->out;
6135 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006136
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006137 /* We don't care about the order of composing characters.
6138 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006139 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006140 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006141 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006142 cchars[ccount++] = mc;
6143 len += mb_char2len(mc);
6144 if (ccount == MAX_MCO)
6145 break;
6146 }
6147
6148 /* Check that each composing char in the pattern matches a
6149 * composing char in the text. We do not check if all
6150 * composing chars are matched. */
6151 result = OK;
6152 while (sta->c != NFA_END_COMPOSING)
6153 {
6154 for (j = 0; j < ccount; ++j)
6155 if (cchars[j] == sta->c)
6156 break;
6157 if (j == ccount)
6158 {
6159 result = FAIL;
6160 break;
6161 }
6162 sta = sta->out;
6163 }
6164 }
6165 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006166 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006167
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006168 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006169 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006170 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006171 }
6172#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006173
6174 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006175 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006176 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006177 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006178 go_to_nextline = TRUE;
6179 /* Pass -1 for the offset, which means taking the position
6180 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006181 add_state = t->state->out;
6182 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006183 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006184 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006185 {
6186 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006187 add_state = t->state->out;
6188 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006189 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006190 break;
6191
Bram Moolenaar417bad22013-06-07 14:08:30 +02006192 case NFA_START_COLL:
6193 case NFA_START_NEG_COLL:
6194 {
6195 /* What follows is a list of characters, until NFA_END_COLL.
6196 * One of them must match or none of them must match. */
6197 nfa_state_T *state;
6198 int result_if_matched;
6199 int c1, c2;
6200
6201 /* Never match EOL. If it's part of the collection it is added
6202 * as a separate state with an OR. */
6203 if (curc == NUL)
6204 break;
6205
6206 state = t->state->out;
6207 result_if_matched = (t->state->c == NFA_START_COLL);
6208 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006209 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006210 if (state->c == NFA_END_COLL)
6211 {
6212 result = !result_if_matched;
6213 break;
6214 }
6215 if (state->c == NFA_RANGE_MIN)
6216 {
6217 c1 = state->val;
6218 state = state->out; /* advance to NFA_RANGE_MAX */
6219 c2 = state->val;
6220#ifdef ENABLE_LOG
6221 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6222 curc, c1, c2);
6223#endif
6224 if (curc >= c1 && curc <= c2)
6225 {
6226 result = result_if_matched;
6227 break;
6228 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006229 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006230 {
6231 int curc_low = MB_TOLOWER(curc);
6232 int done = FALSE;
6233
6234 for ( ; c1 <= c2; ++c1)
6235 if (MB_TOLOWER(c1) == curc_low)
6236 {
6237 result = result_if_matched;
6238 done = TRUE;
6239 break;
6240 }
6241 if (done)
6242 break;
6243 }
6244 }
6245 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006246 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006247 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006248 == MB_TOLOWER(state->c))))
6249 {
6250 result = result_if_matched;
6251 break;
6252 }
6253 state = state->out;
6254 }
6255 if (result)
6256 {
6257 /* next state is in out of the NFA_END_COLL, out1 of
6258 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006259 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006260 add_off = clen;
6261 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006262 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006263 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006264
6265 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006266 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006267 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006268 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006269 add_state = t->state->out;
6270 add_off = clen;
6271 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006272 break;
6273
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006274 case NFA_ANY_COMPOSING:
6275 /* On a composing character skip over it. Otherwise do
6276 * nothing. Always matches. */
6277#ifdef FEAT_MBYTE
6278 if (enc_utf8 && utf_iscomposing(curc))
6279 {
6280 add_off = clen;
6281 }
6282 else
6283#endif
6284 {
6285 add_here = TRUE;
6286 add_off = 0;
6287 }
6288 add_state = t->state->out;
6289 break;
6290
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006291 /*
6292 * Character classes like \a for alpha, \d for digit etc.
6293 */
6294 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006295 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006296 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006297 break;
6298
6299 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006300 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006301 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006302 break;
6303
6304 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006305 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006306 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307 break;
6308
6309 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006310 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006311 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006312 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006313 break;
6314
6315 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006316 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006317 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318 break;
6319
6320 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006321 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006322 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323 break;
6324
6325 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006326 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006327 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328 break;
6329
6330 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006331 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006332 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006333 break;
6334
6335 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006336 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006337 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 break;
6339
6340 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006341 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006342 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006343 break;
6344
6345 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006346 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006347 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006348 break;
6349
6350 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006351 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006352 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006353 break;
6354
6355 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006356 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006357 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006358 break;
6359
6360 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006361 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006362 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006363 break;
6364
6365 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006366 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006367 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006368 break;
6369
6370 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006371 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006372 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006373 break;
6374
6375 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006376 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006377 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006378 break;
6379
6380 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006381 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006382 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006383 break;
6384
6385 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006386 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006387 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006388 break;
6389
6390 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006391 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006392 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006393 break;
6394
6395 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006396 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006397 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006398 break;
6399
6400 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006401 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006402 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006403 break;
6404
6405 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006406 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006407 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408 break;
6409
6410 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006411 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006412 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006413 break;
6414
6415 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006416 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006417 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006418 break;
6419
6420 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006421 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006422 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006423 break;
6424
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006425 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006426 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006427 ADD_STATE_IF_MATCH(t->state);
6428 break;
6429
6430 case NFA_NLOWER_IC: /* [^a-z] */
6431 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006432 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006433 ADD_STATE_IF_MATCH(t->state);
6434 break;
6435
6436 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006437 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006438 ADD_STATE_IF_MATCH(t->state);
6439 break;
6440
6441 case NFA_NUPPER_IC: /* ^[A-Z] */
6442 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006443 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006444 ADD_STATE_IF_MATCH(t->state);
6445 break;
6446
Bram Moolenaar5714b802013-05-28 22:03:20 +02006447 case NFA_BACKREF1:
6448 case NFA_BACKREF2:
6449 case NFA_BACKREF3:
6450 case NFA_BACKREF4:
6451 case NFA_BACKREF5:
6452 case NFA_BACKREF6:
6453 case NFA_BACKREF7:
6454 case NFA_BACKREF8:
6455 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006456#ifdef FEAT_SYN_HL
6457 case NFA_ZREF1:
6458 case NFA_ZREF2:
6459 case NFA_ZREF3:
6460 case NFA_ZREF4:
6461 case NFA_ZREF5:
6462 case NFA_ZREF6:
6463 case NFA_ZREF7:
6464 case NFA_ZREF8:
6465 case NFA_ZREF9:
6466#endif
6467 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006468 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006469 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006470 int bytelen;
6471
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006472 if (t->state->c <= NFA_BACKREF9)
6473 {
6474 subidx = t->state->c - NFA_BACKREF1 + 1;
6475 result = match_backref(&t->subs.norm, subidx, &bytelen);
6476 }
6477#ifdef FEAT_SYN_HL
6478 else
6479 {
6480 subidx = t->state->c - NFA_ZREF1 + 1;
6481 result = match_zref(subidx, &bytelen);
6482 }
6483#endif
6484
Bram Moolenaar5714b802013-05-28 22:03:20 +02006485 if (result)
6486 {
6487 if (bytelen == 0)
6488 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006489 /* empty match always works, output of NFA_SKIP to be
6490 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006491 add_here = TRUE;
6492 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006493 }
6494 else if (bytelen <= clen)
6495 {
6496 /* match current character, jump ahead to out of
6497 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006498 add_state = t->state->out->out;
6499 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006500 }
6501 else
6502 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006503 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006504 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006505 add_state = t->state->out;
6506 add_off = bytelen;
6507 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006508 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006509 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006510 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006511 }
6512 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006513 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006514 if (t->count - clen <= 0)
6515 {
6516 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006517 add_state = t->state->out;
6518 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006519 }
6520 else
6521 {
6522 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006523 add_state = t->state;
6524 add_off = 0;
6525 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006526 }
6527 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006528
Bram Moolenaar423532e2013-05-29 21:14:42 +02006529 case NFA_LNUM:
6530 case NFA_LNUM_GT:
6531 case NFA_LNUM_LT:
6532 result = (REG_MULTI &&
6533 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006534 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006535 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006536 {
6537 add_here = TRUE;
6538 add_state = t->state->out;
6539 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006540 break;
6541
6542 case NFA_COL:
6543 case NFA_COL_GT:
6544 case NFA_COL_LT:
6545 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006546 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006547 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006548 {
6549 add_here = TRUE;
6550 add_state = t->state->out;
6551 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006552 break;
6553
6554 case NFA_VCOL:
6555 case NFA_VCOL_GT:
6556 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006557 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006558 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006559 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006560 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006561
6562 /* Bail out quickly when there can't be a match, avoid the
6563 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006564 if (op != 1 && col > t->state->val
6565#ifdef FEAT_MBYTE
6566 * (has_mbyte ? MB_MAXBYTES : 1)
6567#endif
6568 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006569 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006570 result = FALSE;
6571 if (op == 1 && col - 1 > t->state->val && col > 100)
6572 {
6573 int ts = wp->w_buffer->b_p_ts;
6574
6575 /* Guess that a character won't use more columns than
6576 * 'tabstop', with a minimum of 4. */
6577 if (ts < 4)
6578 ts = 4;
6579 result = col > t->state->val * ts;
6580 }
6581 if (!result)
6582 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006583 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006584 if (result)
6585 {
6586 add_here = TRUE;
6587 add_state = t->state->out;
6588 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006589 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006590 break;
6591
Bram Moolenaar044aa292013-06-04 21:27:38 +02006592 case NFA_MARK:
6593 case NFA_MARK_GT:
6594 case NFA_MARK_LT:
6595 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006596 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006597
6598 /* Compare the mark position to the match position. */
6599 result = (pos != NULL /* mark doesn't exist */
6600 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006601 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6602 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006603 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006604 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006605 ? t->state->c == NFA_MARK_GT
6606 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006607 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006608 ? t->state->c == NFA_MARK_GT
6609 : t->state->c == NFA_MARK_LT)));
6610 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006611 {
6612 add_here = TRUE;
6613 add_state = t->state->out;
6614 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006615 break;
6616 }
6617
Bram Moolenaar423532e2013-05-29 21:14:42 +02006618 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006619 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006620 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006621 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006622 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006623 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006624 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006625 {
6626 add_here = TRUE;
6627 add_state = t->state->out;
6628 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006629 break;
6630
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006631 case NFA_VISUAL:
6632 result = reg_match_visual();
6633 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006634 {
6635 add_here = TRUE;
6636 add_state = t->state->out;
6637 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006638 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006639
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006640 case NFA_MOPEN1:
6641 case NFA_MOPEN2:
6642 case NFA_MOPEN3:
6643 case NFA_MOPEN4:
6644 case NFA_MOPEN5:
6645 case NFA_MOPEN6:
6646 case NFA_MOPEN7:
6647 case NFA_MOPEN8:
6648 case NFA_MOPEN9:
6649#ifdef FEAT_SYN_HL
6650 case NFA_ZOPEN:
6651 case NFA_ZOPEN1:
6652 case NFA_ZOPEN2:
6653 case NFA_ZOPEN3:
6654 case NFA_ZOPEN4:
6655 case NFA_ZOPEN5:
6656 case NFA_ZOPEN6:
6657 case NFA_ZOPEN7:
6658 case NFA_ZOPEN8:
6659 case NFA_ZOPEN9:
6660#endif
6661 case NFA_NOPEN:
6662 case NFA_ZSTART:
6663 /* These states are only added to be able to bail out when
6664 * they are added again, nothing is to be done. */
6665 break;
6666
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006667 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006668 {
6669 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006670
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006671#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006672 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006673 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006674#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006675 result = (c == curc);
6676
Bram Moolenaar6100d022016-10-02 16:51:57 +02006677 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006678 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006679#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006680 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006681 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006682 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006683 clen = utf_ptr2len(rex.input);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006684#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006685 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006686 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006687 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006688
6689 } /* switch (t->state->c) */
6690
6691 if (add_state != NULL)
6692 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006693 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006694 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006695
6696 if (t->pim.result == NFA_PIM_UNUSED)
6697 pim = NULL;
6698 else
6699 pim = &t->pim;
6700
6701 /* Handle the postponed invisible match if the match might end
6702 * without advancing and before the end of the line. */
6703 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006704 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006705 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006706 {
6707#ifdef ENABLE_LOG
6708 fprintf(log_fd, "\n");
6709 fprintf(log_fd, "==================================\n");
6710 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6711 fprintf(log_fd, "\n");
6712#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006713 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006714 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006715 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006716 /* for \@! and \@<! it is a match when the result is
6717 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006718 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006719 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6720 || pim->state->c
6721 == NFA_START_INVISIBLE_BEFORE_NEG
6722 || pim->state->c
6723 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006724 {
6725 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006726 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006727#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006728 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006729 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006730#endif
6731 }
6732 }
6733 else
6734 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006735 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006736#ifdef ENABLE_LOG
6737 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006738 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006739 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6740 fprintf(log_fd, "\n");
6741#endif
6742 }
6743
Bram Moolenaardecd9542013-06-07 16:31:50 +02006744 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006745 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006746 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6747 || pim->state->c
6748 == NFA_START_INVISIBLE_BEFORE_NEG
6749 || pim->state->c
6750 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006751 {
6752 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006753 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006754#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006755 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006756 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757#endif
6758 }
6759 else
6760 /* look-behind match failed, don't add the state */
6761 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006762
6763 /* Postponed invisible match was handled, don't add it to
6764 * following states. */
6765 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006766 }
6767
Bram Moolenaara951e352013-10-06 15:46:11 +02006768 /* If "pim" points into l->t it will become invalid when
6769 * adding the state causes the list to be reallocated. Make a
6770 * local copy to avoid that. */
6771 if (pim == &t->pim)
6772 {
6773 copy_pim(&pim_copy, pim);
6774 pim = &pim_copy;
6775 }
6776
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006777 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006778 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006779 else
6780 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006781 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006782 if (add_count > 0)
6783 nextlist->t[nextlist->n - 1].count = add_count;
6784 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006785 }
6786
6787 } /* for (thislist = thislist; thislist->state; thislist++) */
6788
Bram Moolenaare23febd2013-05-26 18:40:14 +02006789 /* Look for the start of a match in the current position by adding the
6790 * start state to the list of states.
6791 * The first found match is the leftmost one, thus the order of states
6792 * matters!
6793 * Do not add the start state in recursive calls of nfa_regmatch(),
6794 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006795 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006796 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006797 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006798 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006799 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006800 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006801 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006802 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006803 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006804 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006805 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6806 || (rex.lnum == nfa_endp->se_u.pos.lnum
6807 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006808 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006809 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006810 {
6811#ifdef ENABLE_LOG
6812 fprintf(log_fd, "(---) STARTSTATE\n");
6813#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006814 /* Inline optimized code for addstate() if we know the state is
6815 * the first MOPEN. */
6816 if (toplevel)
6817 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006818 int add = TRUE;
6819 int c;
6820
6821 if (prog->regstart != NUL && clen != 0)
6822 {
6823 if (nextlist->n == 0)
6824 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006825 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006826
6827 /* Nextlist is empty, we can skip ahead to the
6828 * character that must appear at the start. */
6829 if (skip_to_start(prog->regstart, &col) == FAIL)
6830 break;
6831#ifdef ENABLE_LOG
6832 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006833 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006834#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006835 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006836 }
6837 else
6838 {
6839 /* Checking if the required start character matches is
6840 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006841 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006842 if (c != prog->regstart && (!rex.reg_ic
6843 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006844 {
6845#ifdef ENABLE_LOG
6846 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6847#endif
6848 add = FALSE;
6849 }
6850 }
6851 }
6852
6853 if (add)
6854 {
6855 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006856 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006857 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006858 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006859 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006860 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006861 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006862 }
6863 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006864 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006865 }
6866
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006867#ifdef ENABLE_LOG
6868 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006869 {
6870 int i;
6871
6872 for (i = 0; i < thislist->n; i++)
6873 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6874 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006875 fprintf(log_fd, "\n");
6876#endif
6877
6878nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006879 /* Advance to the next character, or advance to the next line, or
6880 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006881 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006882 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006883 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006884 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006885 reg_nextline();
6886 else
6887 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006888
6889 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006890 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006891 if (got_int)
6892 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006893#ifdef FEAT_RELTIME
6894 /* Check for timeout once in a twenty times to avoid overhead. */
6895 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6896 {
6897 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006898 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006899 break;
6900 }
6901#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006902 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006903
6904#ifdef ENABLE_LOG
6905 if (log_fd != stderr)
6906 fclose(log_fd);
6907 log_fd = NULL;
6908#endif
6909
6910theend:
6911 /* Free memory */
6912 vim_free(list[0].t);
6913 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006914 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006915#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006916#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006917 fclose(debug);
6918#endif
6919
Bram Moolenaar963fee22013-05-26 21:47:28 +02006920 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006921}
6922
6923/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006924 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006925 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006926 */
6927 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006928nfa_regtry(
6929 nfa_regprog_T *prog,
6930 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006931 proftime_T *tm UNUSED, /* timeout limit or NULL */
6932 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006933{
6934 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006935 regsubs_T subs, m;
6936 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006937 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006938#ifdef ENABLE_LOG
6939 FILE *f;
6940#endif
6941
Bram Moolenaar0270f382018-07-17 05:43:58 +02006942 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006943#ifdef FEAT_RELTIME
6944 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006945 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006946 nfa_time_count = 0;
6947#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006948
6949#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006950 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006951 if (f != NULL)
6952 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006953 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006954#ifdef DEBUG
6955 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6956#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006957 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006958 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006959 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006960 fprintf(f, "\n\n");
6961 fclose(f);
6962 }
6963 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006964 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006965#endif
6966
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006967 clear_sub(&subs.norm);
6968 clear_sub(&m.norm);
6969#ifdef FEAT_SYN_HL
6970 clear_sub(&subs.synt);
6971 clear_sub(&m.synt);
6972#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006973
Bram Moolenaarfda37292014-11-05 14:27:36 +01006974 result = nfa_regmatch(prog, start, &subs, &m);
6975 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006976 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006977 else if (result == NFA_TOO_EXPENSIVE)
6978 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006979
6980 cleanup_subexpr();
6981 if (REG_MULTI)
6982 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006983 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006984 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006985 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6986 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006987
Bram Moolenaar6100d022016-10-02 16:51:57 +02006988 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6989 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006990 }
6991
Bram Moolenaar6100d022016-10-02 16:51:57 +02006992 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006993 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006994 rex.reg_startpos[0].lnum = 0;
6995 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006996 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006997 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006998 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006999 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007000 rex.reg_endpos[0].lnum = rex.lnum;
7001 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007002 }
7003 else
7004 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007005 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007006 }
7007 else
7008 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007009 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007010 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007011 rex.reg_startp[i] = subs.norm.list.line[i].start;
7012 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007013 }
7014
Bram Moolenaar6100d022016-10-02 16:51:57 +02007015 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007016 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007017 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007018 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007019 }
7020
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007021#ifdef FEAT_SYN_HL
7022 /* Package any found \z(...\) matches for export. Default is none. */
7023 unref_extmatch(re_extmatch_out);
7024 re_extmatch_out = NULL;
7025
7026 if (prog->reghasz == REX_SET)
7027 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007028 cleanup_zsubexpr();
7029 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007030 /* Loop over \z1, \z2, etc. There is no \z0. */
7031 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007032 {
7033 if (REG_MULTI)
7034 {
7035 struct multipos *mpos = &subs.synt.list.multi[i];
7036
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007037 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007038 if (mpos->start_lnum >= 0
7039 && mpos->start_lnum == mpos->end_lnum
7040 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007041 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007042 vim_strnsave(reg_getline(mpos->start_lnum)
7043 + mpos->start_col,
7044 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007045 }
7046 else
7047 {
7048 struct linepos *lpos = &subs.synt.list.line[i];
7049
7050 if (lpos->start != NULL && lpos->end != NULL)
7051 re_extmatch_out->matches[i] =
7052 vim_strnsave(lpos->start,
7053 (int)(lpos->end - lpos->start));
7054 }
7055 }
7056 }
7057#endif
7058
Bram Moolenaar0270f382018-07-17 05:43:58 +02007059 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007060}
7061
7062/*
7063 * Match a regexp against a string ("line" points to the string) or multiple
7064 * lines ("line" is NULL, use reg_getline()).
7065 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007066 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007067 */
7068 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007069nfa_regexec_both(
7070 char_u *line,
7071 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007072 proftime_T *tm, /* timeout limit or NULL */
7073 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007074{
7075 nfa_regprog_T *prog;
7076 long retval = 0L;
7077 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007078 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007079
7080 if (REG_MULTI)
7081 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007082 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007083 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007084 rex.reg_startpos = rex.reg_mmatch->startpos;
7085 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007086 }
7087 else
7088 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007089 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7090 rex.reg_startp = rex.reg_match->startp;
7091 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007092 }
7093
7094 /* Be paranoid... */
7095 if (prog == NULL || line == NULL)
7096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007097 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007098 goto theend;
7099 }
7100
Bram Moolenaar6100d022016-10-02 16:51:57 +02007101 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007102 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007103 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007104 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007105 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007106
7107#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007108 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007109 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007110 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007111#endif
7112
Bram Moolenaar0270f382018-07-17 05:43:58 +02007113 rex.line = line;
7114 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007115
Bram Moolenaar0270f382018-07-17 05:43:58 +02007116 rex.nfa_has_zend = prog->has_zend;
7117 rex.nfa_has_backref = prog->has_backref;
7118 rex.nfa_nsubexpr = prog->nsubexp;
7119 rex.nfa_listid = 1;
7120 rex.nfa_alt_listid = 2;
7121#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007122 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007123#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007124
Bram Moolenaard89616e2013-06-06 18:46:06 +02007125 if (prog->reganch && col > 0)
7126 return 0L;
7127
Bram Moolenaar0270f382018-07-17 05:43:58 +02007128 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007129#ifdef FEAT_SYN_HL
7130 /* Clear the external match subpointers if necessary. */
7131 if (prog->reghasz == REX_SET)
7132 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007133 rex.nfa_has_zsubexpr = TRUE;
7134 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007135 }
7136 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007137 {
7138 rex.nfa_has_zsubexpr = FALSE;
7139 rex.need_clear_zsubexpr = FALSE;
7140 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007141#endif
7142
Bram Moolenaard89616e2013-06-06 18:46:06 +02007143 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007144 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007145 /* Skip ahead until a character we know the match must start with.
7146 * When there is none there is no match. */
7147 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007148 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007149
Bram Moolenaar473de612013-06-08 18:19:48 +02007150 /* If match_text is set it contains the full text that must match.
7151 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007152 if (prog->match_text != NULL
7153#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007154 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007155#endif
7156 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007157 return find_match_text(col, prog->regstart, prog->match_text);
7158 }
7159
Bram Moolenaard89616e2013-06-06 18:46:06 +02007160 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007161 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007162 goto theend;
7163
Bram Moolenaar0270f382018-07-17 05:43:58 +02007164 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7165 // it's accidentally used during execution.
7166 nstate = 0;
7167 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168 {
7169 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007170 prog->state[i].lastlist[0] = 0;
7171 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007172 }
7173
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007174 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007175
Bram Moolenaar0270f382018-07-17 05:43:58 +02007176#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007177 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007178#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007179
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007180theend:
7181 return retval;
7182}
7183
7184/*
7185 * Compile a regular expression into internal code for the NFA matcher.
7186 * Returns the program in allocated space. Returns NULL for an error.
7187 */
7188 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007189nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007190{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007191 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007192 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007193 int *postfix;
7194
7195 if (expr == NULL)
7196 return NULL;
7197
Bram Moolenaar0270f382018-07-17 05:43:58 +02007198#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007199 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007200#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007201 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007202
7203 init_class_tab();
7204
7205 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7206 return NULL;
7207
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007209 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007210 postfix = re2post();
7211 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007212 {
7213 /* TODO: only give this error for debugging? */
7214 if (post_ptr >= post_end)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007215 siemsg("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007216 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007217 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007218
7219 /*
7220 * In order to build the NFA, we parse the input regexp twice:
7221 * 1. first pass to count size (so we can allocate space)
7222 * 2. second to emit code
7223 */
7224#ifdef ENABLE_LOG
7225 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007226 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007227
7228 if (f != NULL)
7229 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007230 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007231 fclose(f);
7232 }
7233 }
7234#endif
7235
7236 /*
7237 * PASS 1
7238 * Count number of NFA states in "nstate". Do not build the NFA.
7239 */
7240 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007241
Bram Moolenaar16619a22013-06-11 18:42:36 +02007242 /* allocate the regprog with space for the compiled regexp */
7243 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007244 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7245 if (prog == NULL)
7246 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007247 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007248 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007249
7250 /*
7251 * PASS 2
7252 * Build the NFA
7253 */
7254 prog->start = post2nfa(postfix, post_ptr, FALSE);
7255 if (prog->start == NULL)
7256 goto fail;
7257
7258 prog->regflags = regflags;
7259 prog->engine = &nfa_regengine;
7260 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007261 prog->has_zend = rex.nfa_has_zend;
7262 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007263 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007264
Bram Moolenaara2947e22013-06-11 22:44:09 +02007265 nfa_postprocess(prog);
7266
Bram Moolenaard89616e2013-06-06 18:46:06 +02007267 prog->reganch = nfa_get_reganch(prog->start, 0);
7268 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007269 prog->match_text = nfa_get_match_text(prog->start);
7270
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007271#ifdef ENABLE_LOG
7272 nfa_postfix_dump(expr, OK);
7273 nfa_dump(prog);
7274#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007275#ifdef FEAT_SYN_HL
7276 /* Remember whether this pattern has any \z specials in it. */
7277 prog->reghasz = re_has_z;
7278#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007279 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007280#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007281 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007282#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007283
7284out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007285 VIM_CLEAR(post_start);
7286 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007287 state_ptr = NULL;
7288 return (regprog_T *)prog;
7289
7290fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007291 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007292#ifdef ENABLE_LOG
7293 nfa_postfix_dump(expr, FAIL);
7294#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007295#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007296 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007297#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007298 goto out;
7299}
7300
Bram Moolenaar473de612013-06-08 18:19:48 +02007301/*
7302 * Free a compiled regexp program, returned by nfa_regcomp().
7303 */
7304 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007305nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007306{
7307 if (prog != NULL)
7308 {
7309 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007310 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007311 vim_free(prog);
7312 }
7313}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007314
7315/*
7316 * Match a regexp against a string.
7317 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7318 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007319 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007320 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007321 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007322 */
7323 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007324nfa_regexec_nl(
7325 regmatch_T *rmp,
7326 char_u *line, /* string to match against */
7327 colnr_T col, /* column to start looking for match */
7328 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007329{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007330 rex.reg_match = rmp;
7331 rex.reg_mmatch = NULL;
7332 rex.reg_maxline = 0;
7333 rex.reg_line_lbr = line_lbr;
7334 rex.reg_buf = curbuf;
7335 rex.reg_win = NULL;
7336 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007337#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007338 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007339#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007340 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007341 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007342}
7343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007344
7345/*
7346 * Match a regexp against multiple lines.
7347 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7348 * Uses curbuf for line count and 'iskeyword'.
7349 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007350 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007351 * match otherwise.
7352 *
7353 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7354 *
7355 * ! Also NOTE : match may actually be in another line. e.g.:
7356 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7357 *
7358 * +-------------------------+
7359 * |a |
7360 * |b |
7361 * |c |
7362 * | |
7363 * +-------------------------+
7364 *
7365 * then nfa_regexec_multi() returns 3. while the original
7366 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7367 *
7368 * FIXME if this behavior is not compatible.
7369 */
7370 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007371nfa_regexec_multi(
7372 regmmatch_T *rmp,
7373 win_T *win, /* window in which to search or NULL */
7374 buf_T *buf, /* buffer in which to search */
7375 linenr_T lnum, /* nr of line to start looking for match */
7376 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007377 proftime_T *tm, /* timeout limit or NULL */
7378 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007379{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007380 rex.reg_match = NULL;
7381 rex.reg_mmatch = rmp;
7382 rex.reg_buf = buf;
7383 rex.reg_win = win;
7384 rex.reg_firstlnum = lnum;
7385 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7386 rex.reg_line_lbr = FALSE;
7387 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007388#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007389 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007390#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007391 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007392
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007393 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007394}
7395
7396#ifdef DEBUG
7397# undef ENABLE_LOG
7398#endif