blob: af7994c706c8d9fb4330d8b5d924006c3badd46f [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,
Bram Moolenaar221cd9f2019-01-31 15:34:40 +0100229 NFA_CLASS_ESCAPE,
230 NFA_CLASS_IDENT,
231 NFA_CLASS_KEYWORD,
232 NFA_CLASS_FNAME
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233};
234
235/* Keep in sync with classchars. */
236static int nfa_classcodes[] = {
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
241 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
242 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
243 NFA_UPPER, NFA_NUPPER
244};
245
Bram Moolenaar174a8482013-11-28 14:20:17 +0100246static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaara5483442019-02-17 20:17:02 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %d");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar0270f382018-07-17 05:43:58 +0200250// Variables only used in nfa_regcomp() and descendants.
251static int nfa_re_flags; // re_flags passed to nfa_regcomp()
252static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253static int *post_end;
254static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200255static int nstate; // Number of states in the NFA.
256static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
262static int nfa_ll_index = 0;
263
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100264static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100265static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100267static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100269static int match_follows(nfa_state_T *startstate, int depth);
270static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271
272/* helper functions used when doing re2post() ... regatom() parsing */
273#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275 return FAIL; \
276 *post_ptr++ = c; \
277 } while (0)
278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279/*
280 * Initialize internal variables before NFA compilation.
281 * Return OK on success, FAIL otherwise.
282 */
283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100284nfa_regcomp_start(
285 char_u *expr,
286 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200288 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200289 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290
291 nstate = 0;
292 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200293 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200294 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200296 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200297 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200298 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200299
300 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200301 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200302
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303 post_start = (int *)lalloc(postfix_size, TRUE);
304 if (post_start == NULL)
305 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200307 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200308 rex.nfa_has_zend = FALSE;
309 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200311 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312 regcomp_start(expr, re_flags);
313
314 return OK;
315}
316
317/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200318 * Figure out if the NFA state list starts with an anchor, must match at start
319 * of the line.
320 */
321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200323{
324 nfa_state_T *p = start;
325
326 if (depth > 4)
327 return 0;
328
329 while (p != NULL)
330 {
331 switch (p->c)
332 {
333 case NFA_BOL:
334 case NFA_BOF:
335 return 1; /* yes! */
336
337 case NFA_ZSTART:
338 case NFA_ZEND:
339 case NFA_CURSOR:
340 case NFA_VISUAL:
341
342 case NFA_MOPEN:
343 case NFA_MOPEN1:
344 case NFA_MOPEN2:
345 case NFA_MOPEN3:
346 case NFA_MOPEN4:
347 case NFA_MOPEN5:
348 case NFA_MOPEN6:
349 case NFA_MOPEN7:
350 case NFA_MOPEN8:
351 case NFA_MOPEN9:
352 case NFA_NOPEN:
353#ifdef FEAT_SYN_HL
354 case NFA_ZOPEN:
355 case NFA_ZOPEN1:
356 case NFA_ZOPEN2:
357 case NFA_ZOPEN3:
358 case NFA_ZOPEN4:
359 case NFA_ZOPEN5:
360 case NFA_ZOPEN6:
361 case NFA_ZOPEN7:
362 case NFA_ZOPEN8:
363 case NFA_ZOPEN9:
364#endif
365 p = p->out;
366 break;
367
368 case NFA_SPLIT:
369 return nfa_get_reganch(p->out, depth + 1)
370 && nfa_get_reganch(p->out1, depth + 1);
371
372 default:
373 return 0; /* noooo */
374 }
375 }
376 return 0;
377}
378
379/*
380 * Figure out if the NFA state list starts with a character which must match
381 * at start of the match.
382 */
383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100384nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200385{
386 nfa_state_T *p = start;
387
388 if (depth > 4)
389 return 0;
390
391 while (p != NULL)
392 {
393 switch (p->c)
394 {
395 /* all kinds of zero-width matches */
396 case NFA_BOL:
397 case NFA_BOF:
398 case NFA_BOW:
399 case NFA_EOW:
400 case NFA_ZSTART:
401 case NFA_ZEND:
402 case NFA_CURSOR:
403 case NFA_VISUAL:
404 case NFA_LNUM:
405 case NFA_LNUM_GT:
406 case NFA_LNUM_LT:
407 case NFA_COL:
408 case NFA_COL_GT:
409 case NFA_COL_LT:
410 case NFA_VCOL:
411 case NFA_VCOL_GT:
412 case NFA_VCOL_LT:
413 case NFA_MARK:
414 case NFA_MARK_GT:
415 case NFA_MARK_LT:
416
417 case NFA_MOPEN:
418 case NFA_MOPEN1:
419 case NFA_MOPEN2:
420 case NFA_MOPEN3:
421 case NFA_MOPEN4:
422 case NFA_MOPEN5:
423 case NFA_MOPEN6:
424 case NFA_MOPEN7:
425 case NFA_MOPEN8:
426 case NFA_MOPEN9:
427 case NFA_NOPEN:
428#ifdef FEAT_SYN_HL
429 case NFA_ZOPEN:
430 case NFA_ZOPEN1:
431 case NFA_ZOPEN2:
432 case NFA_ZOPEN3:
433 case NFA_ZOPEN4:
434 case NFA_ZOPEN5:
435 case NFA_ZOPEN6:
436 case NFA_ZOPEN7:
437 case NFA_ZOPEN8:
438 case NFA_ZOPEN9:
439#endif
440 p = p->out;
441 break;
442
443 case NFA_SPLIT:
444 {
445 int c1 = nfa_get_regstart(p->out, depth + 1);
446 int c2 = nfa_get_regstart(p->out1, depth + 1);
447
448 if (c1 == c2)
449 return c1; /* yes! */
450 return 0;
451 }
452
453 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200454 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200455 return p->c; /* yes! */
456 return 0;
457 }
458 }
459 return 0;
460}
461
462/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200463 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200464 * else. If so return a string in allocated memory with what must match after
465 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200466 */
467 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100468nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200469{
470 nfa_state_T *p = start;
471 int len = 0;
472 char_u *ret;
473 char_u *s;
474
475 if (p->c != NFA_MOPEN)
476 return NULL; /* just in case */
477 p = p->out;
478 while (p->c > 0)
479 {
480 len += MB_CHAR2LEN(p->c);
481 p = p->out;
482 }
483 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
484 return NULL;
485
486 ret = alloc(len);
487 if (ret != NULL)
488 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200489 p = start->out->out; /* skip first char, it goes into regstart */
490 s = ret;
491 while (p->c > 0)
492 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200493 if (has_mbyte)
494 s += (*mb_char2bytes)(p->c, s);
495 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 *s++ = p->c;
497 p = p->out;
498 }
499 *s = NUL;
500 }
501 return ret;
502}
503
504/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200505 * Allocate more space for post_start. Called when
506 * running above the estimated number of states.
507 */
508 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100509realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200510{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200511 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100512 int new_max;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200513 int *new_start;
514 int *old_start;
515
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100516 // For weird patterns the number of states can be very high. Increasing by
517 // 50% seems a reasonable compromise between memory use and speed.
518 new_max = nstate_max * 3 / 2;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200519 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
520 if (new_start == NULL)
521 return FAIL;
522 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200523 old_start = post_start;
524 post_start = new_start;
525 post_ptr = new_start + (post_ptr - old_start);
526 post_end = post_start + new_max;
527 vim_free(old_start);
528 return OK;
529}
530
531/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200532 * Search between "start" and "end" and try to recognize a
533 * character class in expanded form. For example [0-9].
534 * On success, return the id the character class to be emitted.
535 * On failure, return 0 (=FAIL)
536 * Start points to the first char of the range, while end should point
537 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200538 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
539 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540 */
541 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100542nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200543{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200544# define CLASS_not 0x80
545# define CLASS_af 0x40
546# define CLASS_AF 0x20
547# define CLASS_az 0x10
548# define CLASS_AZ 0x08
549# define CLASS_o7 0x04
550# define CLASS_o9 0x02
551# define CLASS_underscore 0x01
552
553 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200554 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200555 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200556
557 if (extra_newl == TRUE)
558 newl = TRUE;
559
560 if (*end != ']')
561 return FAIL;
562 p = start;
563 if (*p == '^')
564 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200565 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200566 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200567 }
568
569 while (p < end)
570 {
571 if (p + 2 < end && *(p + 1) == '-')
572 {
573 switch (*p)
574 {
575 case '0':
576 if (*(p + 2) == '9')
577 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200578 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200579 break;
580 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200581 if (*(p + 2) == '7')
582 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200583 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200584 break;
585 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200586 return FAIL;
587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200588 case 'a':
589 if (*(p + 2) == 'z')
590 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200591 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 break;
593 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200594 if (*(p + 2) == 'f')
595 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200596 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 break;
598 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200599 return FAIL;
600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200601 case 'A':
602 if (*(p + 2) == 'Z')
603 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200604 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200605 break;
606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 if (*(p + 2) == 'F')
608 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200609 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 break;
611 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200612 return FAIL;
613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200614 default:
615 return FAIL;
616 }
617 p += 3;
618 }
619 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
620 {
621 newl = TRUE;
622 p += 2;
623 }
624 else if (*p == '_')
625 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200626 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200627 p ++;
628 }
629 else if (*p == '\n')
630 {
631 newl = TRUE;
632 p ++;
633 }
634 else
635 return FAIL;
636 } /* while (p < end) */
637
638 if (p != end)
639 return FAIL;
640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200642 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643
644 switch (config)
645 {
646 case CLASS_o9:
647 return extra_newl + NFA_DIGIT;
648 case CLASS_not | CLASS_o9:
649 return extra_newl + NFA_NDIGIT;
650 case CLASS_af | CLASS_AF | CLASS_o9:
651 return extra_newl + NFA_HEX;
652 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
653 return extra_newl + NFA_NHEX;
654 case CLASS_o7:
655 return extra_newl + NFA_OCTAL;
656 case CLASS_not | CLASS_o7:
657 return extra_newl + NFA_NOCTAL;
658 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
659 return extra_newl + NFA_WORD;
660 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
661 return extra_newl + NFA_NWORD;
662 case CLASS_az | CLASS_AZ | CLASS_underscore:
663 return extra_newl + NFA_HEAD;
664 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
665 return extra_newl + NFA_NHEAD;
666 case CLASS_az | CLASS_AZ:
667 return extra_newl + NFA_ALPHA;
668 case CLASS_not | CLASS_az | CLASS_AZ:
669 return extra_newl + NFA_NALPHA;
670 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200671 return extra_newl + NFA_LOWER_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_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200674 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200675 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200677 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200678 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680}
681
682/*
683 * Produce the bytes for equivalence class "c".
684 * Currently only handles latin1, latin9 and utf-8.
685 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
686 * equivalent to 'a OR b OR c'
687 *
688 * NOTE! When changing this function, also update reg_equi_class()
689 */
690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100691nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200693#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100694#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
697 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200699#ifdef EBCDIC
700# define A_circumflex 0x62
701# define A_diaeresis 0x63
702# define A_grave 0x64
703# define A_acute 0x65
704# define A_virguilla 0x66
705# define A_ring 0x67
706# define C_cedilla 0x68
707# define E_acute 0x71
708# define E_circumflex 0x72
709# define E_diaeresis 0x73
710# define E_grave 0x74
711# define I_acute 0x75
712# define I_circumflex 0x76
713# define I_diaeresis 0x77
714# define I_grave 0x78
715# define N_virguilla 0x69
716# define O_circumflex 0xeb
717# define O_diaeresis 0xec
718# define O_grave 0xed
719# define O_acute 0xee
720# define O_virguilla 0xef
721# define O_slash 0x80
722# define U_circumflex 0xfb
723# define U_diaeresis 0xfc
724# define U_grave 0xfd
725# define U_acute 0xfe
726# define Y_acute 0xba
727# define a_grave 0x42
728# define a_acute 0x43
729# define a_circumflex 0x44
730# define a_virguilla 0x45
731# define a_diaeresis 0x46
732# define a_ring 0x47
733# define c_cedilla 0x48
734# define e_grave 0x51
735# define e_acute 0x52
736# define e_circumflex 0x53
737# define e_diaeresis 0x54
738# define i_grave 0x55
739# define i_acute 0x56
740# define i_circumflex 0x57
741# define i_diaeresis 0x58
742# define n_virguilla 0x49
743# define o_grave 0xcb
744# define o_acute 0xcc
745# define o_circumflex 0xcd
746# define o_virguilla 0xce
747# define o_diaeresis 0xcf
748# define o_slash 0x70
749# define u_grave 0xdb
750# define u_acute 0xdc
751# define u_circumflex 0xdd
752# define u_diaeresis 0xde
753# define y_acute 0x8d
754# define y_diaeresis 0xdf
755#else
756# define A_grave 0xc0
757# define A_acute 0xc1
758# define A_circumflex 0xc2
759# define A_virguilla 0xc3
760# define A_diaeresis 0xc4
761# define A_ring 0xc5
762# define C_cedilla 0xc7
763# define E_grave 0xc8
764# define E_acute 0xc9
765# define E_circumflex 0xca
766# define E_diaeresis 0xcb
767# define I_grave 0xcc
768# define I_acute 0xcd
769# define I_circumflex 0xce
770# define I_diaeresis 0xcf
771# define N_virguilla 0xd1
772# define O_grave 0xd2
773# define O_acute 0xd3
774# define O_circumflex 0xd4
775# define O_virguilla 0xd5
776# define O_diaeresis 0xd6
777# define O_slash 0xd8
778# define U_grave 0xd9
779# define U_acute 0xda
780# define U_circumflex 0xdb
781# define U_diaeresis 0xdc
782# define Y_acute 0xdd
783# define a_grave 0xe0
784# define a_acute 0xe1
785# define a_circumflex 0xe2
786# define a_virguilla 0xe3
787# define a_diaeresis 0xe4
788# define a_ring 0xe5
789# define c_cedilla 0xe7
790# define e_grave 0xe8
791# define e_acute 0xe9
792# define e_circumflex 0xea
793# define e_diaeresis 0xeb
794# define i_grave 0xec
795# define i_acute 0xed
796# define i_circumflex 0xee
797# define i_diaeresis 0xef
798# define n_virguilla 0xf1
799# define o_grave 0xf2
800# define o_acute 0xf3
801# define o_circumflex 0xf4
802# define o_virguilla 0xf5
803# define o_diaeresis 0xf6
804# define o_slash 0xf8
805# define u_grave 0xf9
806# define u_acute 0xfa
807# define u_circumflex 0xfb
808# define u_diaeresis 0xfc
809# define y_acute 0xfd
810# define y_diaeresis 0xff
811#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 switch (c)
813 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200814 case 'A': case A_grave: case A_acute: case A_circumflex:
815 case A_virguilla: case A_diaeresis: case A_ring:
816 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
817 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
818 CASEMBC(0x1ea2)
819 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
820 EMIT2(A_circumflex); EMIT2(A_virguilla);
821 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200822 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
823 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
824 EMITMBC(0x1ea2)
825 return OK;
826
827 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
828 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200829 return OK;
830
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200831 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
832 CASEMBC(0x10a) CASEMBC(0x10c)
833 EMIT2('C'); EMIT2(C_cedilla);
834 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200835 EMITMBC(0x10a) EMITMBC(0x10c)
836 return OK;
837
838 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200839 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200840 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
841 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200842 return OK;
843
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200844 case 'E': case E_grave: case E_acute: case E_circumflex:
845 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
846 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
847 CASEMBC(0x1eba) CASEMBC(0x1ebc)
848 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
849 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200850 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
851 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
852 EMITMBC(0x1ebc)
853 return OK;
854
855 case 'F': CASEMBC(0x1e1e)
856 EMIT2('F'); EMITMBC(0x1e1e)
857 return OK;
858
859 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200860 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
861 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200862 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
863 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
864 EMITMBC(0x1f4) EMITMBC(0x1e20)
865 return OK;
866
867 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200868 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200869 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
870 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200871 return OK;
872
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200873 case 'I': case I_grave: case I_acute: case I_circumflex:
874 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
875 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
876 CASEMBC(0x1cf) CASEMBC(0x1ec8)
877 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
878 EMIT2(I_circumflex); EMIT2(I_diaeresis);
879 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200880 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
881 EMITMBC(0x1cf) EMITMBC(0x1ec8)
882 return OK;
883
884 case 'J': CASEMBC(0x134)
885 EMIT2('J'); EMITMBC(0x134)
886 return OK;
887
888 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200889 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200890 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
891 EMITMBC(0x1e34)
892 return OK;
893
894 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
897 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
898 return OK;
899
900 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
901 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200902 return OK;
903
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200904 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
905 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
906 EMIT2('N'); EMIT2(N_virguilla);
907 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200908 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909 return OK;
910
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200911 case 'O': case O_grave: case O_acute: case O_circumflex:
912 case O_virguilla: case O_diaeresis: case O_slash:
913 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
914 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
915 CASEMBC(0x1ec) CASEMBC(0x1ece)
916 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
917 EMIT2(O_circumflex); EMIT2(O_virguilla);
918 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200919 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
920 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
921 EMITMBC(0x1ec) EMITMBC(0x1ece)
922 return OK;
923
924 case 'P': case 0x1e54: case 0x1e56:
925 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
926 return OK;
927
928 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200930 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
931 EMITMBC(0x1e58) EMITMBC(0x1e5e)
932 return OK;
933
934 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200935 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
937 EMITMBC(0x160) EMITMBC(0x1e60)
938 return OK;
939
940 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200941 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200942 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
943 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200944 return OK;
945
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200946 case 'U': case U_grave: case U_acute: case U_diaeresis:
947 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
948 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
949 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
950 CASEMBC(0x1ee6)
951 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
952 EMIT2(U_diaeresis); EMIT2(U_circumflex);
953 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200954 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
955 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
956 EMITMBC(0x1ee6)
957 return OK;
958
959 case 'V': CASEMBC(0x1e7c)
960 EMIT2('V'); EMITMBC(0x1e7c)
961 return OK;
962
963 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200964 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200965 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
966 EMITMBC(0x1e84) EMITMBC(0x1e86)
967 return OK;
968
969 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
970 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 return OK;
972
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200973 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
974 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
975 CASEMBC(0x1ef8)
976 EMIT2('Y'); EMIT2(Y_acute);
977 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200978 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
979 EMITMBC(0x1ef8)
980 return OK;
981
982 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200983 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200984 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
985 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200986 return OK;
987
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200988 case 'a': case a_grave: case a_acute: case a_circumflex:
989 case a_virguilla: case a_diaeresis: case a_ring:
990 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
991 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
992 CASEMBC(0x1ea3)
993 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
994 EMIT2(a_circumflex); EMIT2(a_virguilla);
995 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200996 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
997 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
998 EMITMBC(0x1ea3)
999 return OK;
1000
1001 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1002 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 return OK;
1004
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001005 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1006 CASEMBC(0x10b) CASEMBC(0x10d)
1007 EMIT2('c'); EMIT2(c_cedilla);
1008 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001009 EMITMBC(0x10b) EMITMBC(0x10d)
1010 return OK;
1011
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001012 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001013 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001014 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1015 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001016 return OK;
1017
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001018 case 'e': case e_grave: case e_acute: case e_circumflex:
1019 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1020 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1021 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1022 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1023 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1024 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001025 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1026 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1027 return OK;
1028
1029 case 'f': CASEMBC(0x1e1f)
1030 EMIT2('f'); EMITMBC(0x1e1f)
1031 return OK;
1032
1033 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001034 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1035 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001036 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1037 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1038 EMITMBC(0x1f5) EMITMBC(0x1e21)
1039 return OK;
1040
1041 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001042 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001043 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1044 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001045 return OK;
1046
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001047 case 'i': case i_grave: case i_acute: case i_circumflex:
1048 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1049 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1050 CASEMBC(0x1ec9)
1051 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1052 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1053 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001054 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1055 EMITMBC(0x1ec9)
1056 return OK;
1057
1058 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1059 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1060 return OK;
1061
1062 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001063 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001064 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1065 EMITMBC(0x1e35)
1066 return OK;
1067
1068 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001070 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1071 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1072 return OK;
1073
1074 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1075 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001076 return OK;
1077
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001078 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1079 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1080 CASEMBC(0x1e49)
1081 EMIT2('n'); EMIT2(n_virguilla);
1082 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001083 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1084 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 return OK;
1086
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001087 case 'o': case o_grave: case o_acute: case o_circumflex:
1088 case o_virguilla: case o_diaeresis: case o_slash:
1089 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1090 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1091 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1092 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1093 EMIT2(o_circumflex); EMIT2(o_virguilla);
1094 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001095 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1096 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1097 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1098 return OK;
1099
1100 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1101 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1102 return OK;
1103
1104 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001105 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001106 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1107 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1108 return OK;
1109
1110 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001111 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001112 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1113 EMITMBC(0x161) EMITMBC(0x1e61)
1114 return OK;
1115
1116 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001117 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001118 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1119 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120 return OK;
1121
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001122 case 'u': case u_grave: case u_acute: case u_circumflex:
1123 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1124 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1125 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1126 CASEMBC(0x1ee7)
1127 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1128 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1129 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001130 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1131 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1132 EMITMBC(0x1ee7)
1133 return OK;
1134
1135 case 'v': CASEMBC(0x1e7d)
1136 EMIT2('v'); EMITMBC(0x1e7d)
1137 return OK;
1138
1139 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001140 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001141 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1142 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1143 return OK;
1144
1145 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1146 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001147 return OK;
1148
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001149 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1150 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1151 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1152 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1153 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001154 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1155 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001156 return OK;
1157
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001158 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001159 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001160 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1161 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1162 return OK;
1163
1164 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001165 }
1166 }
1167
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169 return OK;
1170#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001171#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001172}
1173
1174/*
1175 * Code to parse regular expression.
1176 *
1177 * We try to reuse parsing functions in regexp.c to
1178 * minimize surprise and keep the syntax consistent.
1179 */
1180
1181/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001182 * Parse the lowest level.
1183 *
1184 * An atom can be one of a long list of items. Many atoms match one character
1185 * in the text. It is often an ordinary character or a character class.
1186 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1187 * is only for syntax highlighting.
1188 *
1189 * atom ::= ordinary-atom
1190 * or \( pattern \)
1191 * or \%( pattern \)
1192 * or \z( pattern \)
1193 */
1194 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001195nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001196{
1197 int c;
1198 int charclass;
1199 int equiclass;
1200 int collclass;
1201 int got_coll_char;
1202 char_u *p;
1203 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001204 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 int emit_range;
1207 int negated;
1208 int result;
1209 int startc = -1;
1210 int endc = -1;
1211 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001212 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001215 switch (c)
1216 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001217 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001218 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001220 case Magic('^'):
1221 EMIT(NFA_BOL);
1222 break;
1223
1224 case Magic('$'):
1225 EMIT(NFA_EOL);
1226#if defined(FEAT_SYN_HL) || defined(PROTO)
1227 had_eol = TRUE;
1228#endif
1229 break;
1230
1231 case Magic('<'):
1232 EMIT(NFA_BOW);
1233 break;
1234
1235 case Magic('>'):
1236 EMIT(NFA_EOW);
1237 break;
1238
1239 case Magic('_'):
1240 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001241 if (c == NUL)
1242 EMSG_RET_FAIL(_(e_nul_found));
1243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001244 if (c == '^') /* "\_^" is start-of-line */
1245 {
1246 EMIT(NFA_BOL);
1247 break;
1248 }
1249 if (c == '$') /* "\_$" is end-of-line */
1250 {
1251 EMIT(NFA_EOL);
1252#if defined(FEAT_SYN_HL) || defined(PROTO)
1253 had_eol = TRUE;
1254#endif
1255 break;
1256 }
1257
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001258 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259
1260 /* "\_[" is collection plus newline */
1261 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001262 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
1264 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001265 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266
1267 /*
1268 * Character classes.
1269 */
1270 case Magic('.'):
1271 case Magic('i'):
1272 case Magic('I'):
1273 case Magic('k'):
1274 case Magic('K'):
1275 case Magic('f'):
1276 case Magic('F'):
1277 case Magic('p'):
1278 case Magic('P'):
1279 case Magic('s'):
1280 case Magic('S'):
1281 case Magic('d'):
1282 case Magic('D'):
1283 case Magic('x'):
1284 case Magic('X'):
1285 case Magic('o'):
1286 case Magic('O'):
1287 case Magic('w'):
1288 case Magic('W'):
1289 case Magic('h'):
1290 case Magic('H'):
1291 case Magic('a'):
1292 case Magic('A'):
1293 case Magic('l'):
1294 case Magic('L'):
1295 case Magic('u'):
1296 case Magic('U'):
1297 p = vim_strchr(classchars, no_Magic(c));
1298 if (p == NULL)
1299 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001300 if (extra == NFA_ADD_NL)
1301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001302 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001303 rc_did_emsg = TRUE;
1304 return FAIL;
1305 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001306 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001307 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001309
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001310 /* When '.' is followed by a composing char ignore the dot, so that
1311 * the composing char is matched here. */
1312 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1313 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001314 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 c = getchr();
1316 goto nfa_do_multibyte;
1317 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001319 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 {
1321 EMIT(NFA_NEWL);
1322 EMIT(NFA_OR);
1323 regflags |= RF_HASNL;
1324 }
1325 break;
1326
1327 case Magic('n'):
1328 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001329 /* In a string "\n" matches a newline character. */
1330 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 else
1332 {
1333 /* In buffer text "\n" matches the end of a line. */
1334 EMIT(NFA_NEWL);
1335 regflags |= RF_HASNL;
1336 }
1337 break;
1338
1339 case Magic('('):
1340 if (nfa_reg(REG_PAREN) == FAIL)
1341 return FAIL; /* cascaded error */
1342 break;
1343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 case Magic('|'):
1345 case Magic('&'):
1346 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001347 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 return FAIL;
1349
1350 case Magic('='):
1351 case Magic('?'):
1352 case Magic('+'):
1353 case Magic('@'):
1354 case Magic('*'):
1355 case Magic('{'):
1356 /* these should follow an atom, not form an atom */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 return FAIL;
1359
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001360 case Magic('~'):
1361 {
1362 char_u *lp;
1363
1364 /* Previous substitute pattern.
1365 * Generated as "\%(pattern\)". */
1366 if (reg_prev_sub == NULL)
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001369 return FAIL;
1370 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001371 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001372 {
1373 EMIT(PTR2CHAR(lp));
1374 if (lp != reg_prev_sub)
1375 EMIT(NFA_CONCAT);
1376 }
1377 EMIT(NFA_NOPEN);
1378 break;
1379 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001380
Bram Moolenaar428e9872013-05-30 17:05:39 +02001381 case Magic('1'):
1382 case Magic('2'):
1383 case Magic('3'):
1384 case Magic('4'):
1385 case Magic('5'):
1386 case Magic('6'):
1387 case Magic('7'):
1388 case Magic('8'):
1389 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001390 {
1391 int refnum = no_Magic(c) - '1';
1392
1393 if (!seen_endbrace(refnum + 1))
1394 return FAIL;
1395 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001396 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001397 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001398 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399
1400 case Magic('z'):
1401 c = no_Magic(getchr());
1402 switch (c)
1403 {
1404 case 's':
1405 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001406 if (re_mult_next("\\zs") == FAIL)
1407 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408 break;
1409 case 'e':
1410 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001411 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001412 if (re_mult_next("\\ze") == FAIL)
1413 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001414 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001415#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 case '1':
1417 case '2':
1418 case '3':
1419 case '4':
1420 case '5':
1421 case '6':
1422 case '7':
1423 case '8':
1424 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001425 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001426 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001427 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001428 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar0270f382018-07-17 05:43:58 +02001429 /* No need to set rex.nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001430 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001431 re_has_z = REX_USE;
1432 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001433 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001434 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001435 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001436 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001437 if (nfa_reg(REG_ZPAREN) == FAIL)
1438 return FAIL; /* cascaded error */
1439 re_has_z = REX_SET;
1440 break;
1441#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001443 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 no_Magic(c));
1445 return FAIL;
1446 }
1447 break;
1448
1449 case Magic('%'):
1450 c = no_Magic(getchr());
1451 switch (c)
1452 {
1453 /* () without a back reference */
1454 case '(':
1455 if (nfa_reg(REG_NPAREN) == FAIL)
1456 return FAIL;
1457 EMIT(NFA_NOPEN);
1458 break;
1459
1460 case 'd': /* %d123 decimal */
1461 case 'o': /* %o123 octal */
1462 case 'x': /* %xab hex 2 */
1463 case 'u': /* %uabcd hex 4 */
1464 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001465 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001466 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467
Bram Moolenaar47196582013-05-25 22:04:23 +02001468 switch (c)
1469 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001470 case 'd': nr = getdecchrs(); break;
1471 case 'o': nr = getoctchrs(); break;
1472 case 'x': nr = gethexchrs(2); break;
1473 case 'u': nr = gethexchrs(4); break;
1474 case 'U': nr = gethexchrs(8); break;
1475 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001476 }
1477
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001478 if (nr < 0 || nr > INT_MAX)
Bram Moolenaar47196582013-05-25 22:04:23 +02001479 EMSG2_RET_FAIL(
1480 _("E678: Invalid character after %s%%[dxouU]"),
1481 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001482 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001483 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001484 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001485 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 break;
1487
1488 /* Catch \%^ and \%$ regardless of where they appear in the
1489 * pattern -- regardless of whether or not it makes sense. */
1490 case '^':
1491 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 break;
1493
1494 case '$':
1495 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 break;
1497
1498 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001499 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 break;
1501
1502 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001503 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 break;
1505
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001506 case 'C':
1507 EMIT(NFA_ANY_COMPOSING);
1508 break;
1509
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001511 {
1512 int n;
1513
1514 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001515 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001516 {
1517 if (c == NUL)
1518 EMSG2_RET_FAIL(_(e_missing_sb),
1519 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001520 /* recursive call! */
1521 if (nfa_regatom() == FAIL)
1522 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001523 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001524 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001525 if (n == 0)
1526 EMSG2_RET_FAIL(_(e_empty_sb),
1527 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001528 EMIT(NFA_OPT_CHARS);
1529 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001530
1531 /* Emit as "\%(\%[abc]\)" to be able to handle
1532 * "\%[abc]*" which would cause the empty string to be
1533 * matched an unlimited number of times. NFA_NOPEN is
1534 * added only once at a position, while NFA_SPLIT is
1535 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001536 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001537 * a lot. */
1538 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001539 break;
1540 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001541
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001543 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001544 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001545 int cmp = c;
1546
1547 if (c == '<' || c == '>')
1548 c = getchr();
1549 while (VIM_ISDIGIT(c))
1550 {
1551 n = n * 10 + (c - '0');
1552 c = getchr();
1553 }
1554 if (c == 'l' || c == 'c' || c == 'v')
1555 {
Bram Moolenaar9403a212019-02-13 18:35:06 +01001556 int limit = INT_MAX;
1557
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001559 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001560 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001561 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001562 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001563 if (save_prev_at_start)
1564 at_start = TRUE;
1565 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001566 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001567 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001568 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001569 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001570 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001571 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001572 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001573 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001574 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001575 limit = INT_MAX / MB_MAXBYTES;
1576 }
1577 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001579 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001580 return FAIL;
1581 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001582 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001583 break;
1584 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001585 else if (c == '\'' && n == 0)
1586 {
1587 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001588 EMIT(cmp == '<' ? NFA_MARK_LT :
1589 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001590 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001591 break;
1592 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001593 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001595 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596 return FAIL;
1597 }
1598 break;
1599
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001600 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001601collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001603 * [abc] uses NFA_START_COLL - NFA_END_COLL
1604 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1605 * Each character is produced as a regular state, using
1606 * NFA_CONCAT to bind them together.
1607 * Besides normal characters there can be:
1608 * - character classes NFA_CLASS_*
1609 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 */
1611
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 p = regparse;
1613 endp = skip_anyof(p);
1614 if (*endp == ']')
1615 {
1616 /*
1617 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001618 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 * and perform the necessary substitutions in the NFA.
1620 */
1621 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001622 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 if (result != FAIL)
1624 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001625 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001627 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 EMIT(NFA_NEWL);
1629 EMIT(NFA_OR);
1630 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001631 else
1632 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001633 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001634 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 return OK;
1636 }
1637 /*
1638 * Failed to recognize a character class. Use the simple
1639 * version that turns [abc] into 'a' OR 'b' OR 'c'
1640 */
1641 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001643 if (*regparse == '^') /* negated range */
1644 {
1645 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001646 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001647 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001648 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001649 else
1650 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 if (*regparse == '-')
1652 {
1653 startc = '-';
1654 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001655 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001656 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 }
1658 /* Emit the OR branches for each character in the [] */
1659 emit_range = FALSE;
1660 while (regparse < endp)
1661 {
1662 oldstartc = startc;
1663 startc = -1;
1664 got_coll_char = FALSE;
1665 if (*regparse == '[')
1666 {
1667 /* Check for [: :], [= =], [. .] */
1668 equiclass = collclass = 0;
1669 charclass = get_char_class(&regparse);
1670 if (charclass == CLASS_NONE)
1671 {
1672 equiclass = get_equi_class(&regparse);
1673 if (equiclass == 0)
1674 collclass = get_coll_element(&regparse);
1675 }
1676
1677 /* Character class like [:alpha:] */
1678 if (charclass != CLASS_NONE)
1679 {
1680 switch (charclass)
1681 {
1682 case CLASS_ALNUM:
1683 EMIT(NFA_CLASS_ALNUM);
1684 break;
1685 case CLASS_ALPHA:
1686 EMIT(NFA_CLASS_ALPHA);
1687 break;
1688 case CLASS_BLANK:
1689 EMIT(NFA_CLASS_BLANK);
1690 break;
1691 case CLASS_CNTRL:
1692 EMIT(NFA_CLASS_CNTRL);
1693 break;
1694 case CLASS_DIGIT:
1695 EMIT(NFA_CLASS_DIGIT);
1696 break;
1697 case CLASS_GRAPH:
1698 EMIT(NFA_CLASS_GRAPH);
1699 break;
1700 case CLASS_LOWER:
1701 EMIT(NFA_CLASS_LOWER);
1702 break;
1703 case CLASS_PRINT:
1704 EMIT(NFA_CLASS_PRINT);
1705 break;
1706 case CLASS_PUNCT:
1707 EMIT(NFA_CLASS_PUNCT);
1708 break;
1709 case CLASS_SPACE:
1710 EMIT(NFA_CLASS_SPACE);
1711 break;
1712 case CLASS_UPPER:
1713 EMIT(NFA_CLASS_UPPER);
1714 break;
1715 case CLASS_XDIGIT:
1716 EMIT(NFA_CLASS_XDIGIT);
1717 break;
1718 case CLASS_TAB:
1719 EMIT(NFA_CLASS_TAB);
1720 break;
1721 case CLASS_RETURN:
1722 EMIT(NFA_CLASS_RETURN);
1723 break;
1724 case CLASS_BACKSPACE:
1725 EMIT(NFA_CLASS_BACKSPACE);
1726 break;
1727 case CLASS_ESCAPE:
1728 EMIT(NFA_CLASS_ESCAPE);
1729 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001730 case CLASS_IDENT:
1731 EMIT(NFA_CLASS_IDENT);
1732 break;
1733 case CLASS_KEYWORD:
1734 EMIT(NFA_CLASS_KEYWORD);
1735 break;
1736 case CLASS_FNAME:
1737 EMIT(NFA_CLASS_FNAME);
1738 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001740 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741 continue;
1742 }
1743 /* Try equivalence class [=a=] and the like */
1744 if (equiclass != 0)
1745 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001746 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 if (result == FAIL)
1748 {
1749 /* should never happen */
1750 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001752 continue;
1753 }
1754 /* Try collating class like [. .] */
1755 if (collclass != 0)
1756 {
1757 startc = collclass; /* allow [.a.]-x as a range */
1758 /* Will emit the proper atom at the end of the
1759 * while loop. */
1760 }
1761 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001762 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1763 * start character. */
1764 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 {
1766 emit_range = TRUE;
1767 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001768 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 continue; /* reading the end of the range */
1770 }
1771
1772 /* Now handle simple and escaped characters.
1773 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1774 * accepts "\t", "\e", etc., but only when the 'l' flag in
1775 * 'cpoptions' is not included.
1776 * Posix doesn't recognize backslash at all.
1777 */
1778 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001779 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 && regparse + 1 <= endp
1781 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001782 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 && vim_strchr(REGEXP_ABBR, regparse[1])
1784 != NULL)
1785 )
1786 )
1787 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001788 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001790 if (*regparse == 'n')
Bram Moolenaara5483442019-02-17 20:17:02 +01001791 startc = (reg_string || emit_range
1792 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001793 else if (*regparse == 'd'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 || *regparse == 'o'
1795 || *regparse == 'x'
1796 || *regparse == 'u'
1797 || *regparse == 'U'
1798 )
1799 {
1800 /* TODO(RE) This needs more testing */
1801 startc = coll_get_char();
1802 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001803 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001804 }
1805 else
1806 {
1807 /* \r,\t,\e,\b */
1808 startc = backslash_trans(*regparse);
1809 }
1810 }
1811
1812 /* Normal printable char */
1813 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001814 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001815
1816 /* Previous char was '-', so this char is end of range. */
1817 if (emit_range)
1818 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001819 endc = startc;
1820 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001822 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001823
1824 if (endc > startc + 2)
1825 {
1826 /* Emit a range instead of the sequence of
1827 * individual characters. */
1828 if (startc == 0)
1829 /* \x00 is translated to \x0a, start at \x01. */
1830 EMIT(1);
1831 else
1832 --post_ptr; /* remove NFA_CONCAT */
1833 EMIT(endc);
1834 EMIT(NFA_RANGE);
1835 EMIT(NFA_CONCAT);
1836 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001837 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 || (*mb_char2len)(endc) > 1))
1839 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001840 /* Emit the characters in the range.
1841 * "startc" was already emitted, so skip it.
1842 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001843 for (c = startc + 1; c <= endc; c++)
1844 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001845 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001846 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 }
1849 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001850 {
1851#ifdef EBCDIC
1852 int alpha_only = FALSE;
1853
1854 /* for alphabetical range skip the gaps
1855 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1856 if (isalpha(startc) && isalpha(endc))
1857 alpha_only = TRUE;
1858#endif
1859 /* Emit the range. "startc" was already emitted, so
1860 * skip it. */
1861 for (c = startc + 1; c <= endc; c++)
1862#ifdef EBCDIC
1863 if (!alpha_only || isalpha(startc))
1864#endif
1865 {
1866 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001867 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001870 emit_range = FALSE;
1871 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872 }
1873 else
1874 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001875 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001876 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 * Normally, simply emit startc. But if we get char
1878 * code=0 from a collating char, then replace it with
1879 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001881 * the backtracking engine. */
1882 if (startc == NFA_NEWL)
1883 {
1884 /* Line break can't be matched as part of the
1885 * collection, add an OR below. But not for negated
1886 * range. */
1887 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001888 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001889 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001891 {
1892 if (got_coll_char == TRUE && startc == 0)
1893 EMIT(0x0a);
1894 else
1895 EMIT(startc);
1896 EMIT(NFA_CONCAT);
1897 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 }
1899
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001900 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 } /* while (p < endp) */
1902
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001903 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 if (*regparse == '-') /* if last, '-' is just a char */
1905 {
1906 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001907 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 /* skip the trailing ] */
1911 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001912 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001913
1914 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001916 EMIT(NFA_END_NEG_COLL);
1917 else
1918 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001919
1920 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001921 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001922 {
1923 EMIT(reg_string ? NL : NFA_NEWL);
1924 EMIT(NFA_OR);
1925 }
1926
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001928 } /* if exists closing ] */
1929
1930 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001932 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 default:
1935 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 int plen;
1937
1938nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001939 /* plen is length of current char with composing chars */
1940 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001941 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001942 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001944 int i = 0;
1945
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001946 /* A base character plus composing characters, or just one
1947 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001948 * This requires creating a separate atom as if enclosing
1949 * the characters in (), where NFA_COMPOSING is the ( and
1950 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 * building the postfix form, not the NFA itself;
1952 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001953 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001954 for (;;)
1955 {
1956 EMIT(c);
1957 if (i > 0)
1958 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001959 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001960 break;
1961 c = utf_ptr2char(old_regparse + i);
1962 }
1963 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 regparse = old_regparse + plen;
1965 }
1966 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967 {
1968 c = no_Magic(c);
1969 EMIT(c);
1970 }
1971 return OK;
1972 }
1973 }
1974
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001975 return OK;
1976}
1977
1978/*
1979 * Parse something followed by possible [*+=].
1980 *
1981 * A piece is an atom, possibly followed by a multi, an indication of how many
1982 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1983 * characters: "", "a", "aa", etc.
1984 *
1985 * piece ::= atom
1986 * or atom multi
1987 */
1988 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001989nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001990{
1991 int i;
1992 int op;
1993 int ret;
1994 long minval, maxval;
1995 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001996 parse_state_T old_state;
1997 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001998 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001999 int old_post_pos;
2000 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002001 int quest;
2002
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002003 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2004 * next. */
2005 save_parse_state(&old_state);
2006
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002008 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009
2010 ret = nfa_regatom();
2011 if (ret == FAIL)
2012 return FAIL; /* cascaded error */
2013
2014 op = peekchr();
2015 if (re_multi_type(op) == NOT_MULTI)
2016 return OK;
2017
2018 skipchr();
2019 switch (op)
2020 {
2021 case Magic('*'):
2022 EMIT(NFA_STAR);
2023 break;
2024
2025 case Magic('+'):
2026 /*
2027 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2028 * first and only submatch would be "aaa". But the backtracking
2029 * engine interprets the plus as "try matching one more time", and
2030 * a* matches a second time at the end of the input, the empty
2031 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002032 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002034 * In order to be consistent with the old engine, we replace
2035 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002036 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002037 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 curchr = -1;
2039 if (nfa_regatom() == FAIL)
2040 return FAIL;
2041 EMIT(NFA_STAR);
2042 EMIT(NFA_CONCAT);
2043 skipchr(); /* skip the \+ */
2044 break;
2045
2046 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002047 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002048 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002049 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002050 switch(op)
2051 {
2052 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002053 /* \@= */
2054 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002055 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002056 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002057 /* \@! */
2058 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002059 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002060 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002061 op = no_Magic(getchr());
2062 if (op == '=')
2063 /* \@<= */
2064 i = NFA_PREV_ATOM_JUST_BEFORE;
2065 else if (op == '!')
2066 /* \@<! */
2067 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2068 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002070 /* \@> */
2071 i = NFA_PREV_ATOM_LIKE_PATTERN;
2072 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002074 if (i == 0)
2075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002076 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002077 return FAIL;
2078 }
2079 EMIT(i);
2080 if (i == NFA_PREV_ATOM_JUST_BEFORE
2081 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2082 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 break;
2084
2085 case Magic('?'):
2086 case Magic('='):
2087 EMIT(NFA_QUEST);
2088 break;
2089
2090 case Magic('{'):
2091 /* a{2,5} will expand to 'aaa?a?a?'
2092 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2093 * version of '?'
2094 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2095 * parenthesis have the same id
2096 */
2097
2098 greedy = TRUE;
2099 c2 = peekchr();
2100 if (c2 == '-' || c2 == Magic('-'))
2101 {
2102 skipchr();
2103 greedy = FALSE;
2104 }
2105 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002107
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2109 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002110 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002111 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002112 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002113 /* \{}, \{0,} */
2114 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002115 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002116 /* \{-}, \{-0,} */
2117 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118 break;
2119 }
2120
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002121 /* Special case: x{0} or x{-0} */
2122 if (maxval == 0)
2123 {
2124 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002125 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002126 /* NFA_EMPTY is 0-length and works everywhere */
2127 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128 return OK;
2129 }
2130
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002131 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002132 * maximum is much larger than the minimum and when the maximum is
2133 * large. Bail out if we can use the other engine. */
2134 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002135 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002136 return FAIL;
2137
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002138 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002139 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002140 /* Save parse state after the repeated atom and the \{} */
2141 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002142
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2144 for (i = 0; i < maxval; i++)
2145 {
2146 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002147 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002148 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002149 if (nfa_regatom() == FAIL)
2150 return FAIL;
2151 /* after "minval" times, atoms are optional */
2152 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002153 {
2154 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002155 {
2156 if (greedy)
2157 EMIT(NFA_STAR);
2158 else
2159 EMIT(NFA_STAR_NONGREEDY);
2160 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002161 else
2162 EMIT(quest);
2163 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002164 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002166 if (i + 1 > minval && maxval == MAX_LIMIT)
2167 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002168 }
2169
2170 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002171 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002172 curchr = -1;
2173
2174 break;
2175
2176
2177 default:
2178 break;
2179 } /* end switch */
2180
2181 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002183 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002184
2185 return OK;
2186}
2187
2188/*
2189 * Parse one or more pieces, concatenated. It matches a match for the
2190 * first piece, followed by a match for the second piece, etc. Example:
2191 * "f[0-9]b", first matches "f", then a digit and then "b".
2192 *
2193 * concat ::= piece
2194 * or piece piece
2195 * or piece piece piece
2196 * etc.
2197 */
2198 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002199nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002200{
2201 int cont = TRUE;
2202 int first = TRUE;
2203
2204 while (cont)
2205 {
2206 switch (peekchr())
2207 {
2208 case NUL:
2209 case Magic('|'):
2210 case Magic('&'):
2211 case Magic(')'):
2212 cont = FALSE;
2213 break;
2214
2215 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002216 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 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;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002576 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2577 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2578 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002579
2580 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2581 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2582 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2583 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2584 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2585 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2586 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2587 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2588 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2589 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2590 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2591 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2592 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2593 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2594 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2595 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2596 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2597 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2598 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2599 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2600 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2601 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2602 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2603 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2604 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2605 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2606 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002607 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2608 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2609 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2610 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002611
2612 default:
2613 STRCPY(code, "CHAR(x)");
2614 code[5] = c;
2615 }
2616
2617 if (addnl == TRUE)
2618 STRCAT(code, " + NEWLINE ");
2619
2620}
2621
2622#ifdef ENABLE_LOG
2623static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002624static 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 +02002625
2626/*
2627 * Print the postfix notation of the current regexp.
2628 */
2629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002630nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002631{
2632 int *p;
2633 FILE *f;
2634
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002635 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002636 if (f != NULL)
2637 {
2638 fprintf(f, "\n-------------------------\n");
2639 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002640 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641 else if (retval == OK)
2642 fprintf(f, ">>> NFA engine succeeded !\n");
2643 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002644 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 {
2646 nfa_set_code(*p);
2647 fprintf(f, "%s, ", code);
2648 }
2649 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002650 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651 fprintf(f, "%d ", *p);
2652 fprintf(f, "\n\n");
2653 fclose(f);
2654 }
2655}
2656
2657/*
2658 * Print the NFA starting with a root node "state".
2659 */
2660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002661nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002663 garray_T indent;
2664
2665 ga_init2(&indent, 1, 64);
2666 ga_append(&indent, '\0');
2667 nfa_print_state2(debugf, state, &indent);
2668 ga_clear(&indent);
2669}
2670
2671 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002672nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002673{
2674 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002675
2676 if (state == NULL)
2677 return;
2678
2679 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002680
2681 /* Output indent */
2682 p = (char_u *)indent->ga_data;
2683 if (indent->ga_len >= 3)
2684 {
2685 int last = indent->ga_len - 3;
2686 char_u save[2];
2687
2688 STRNCPY(save, &p[last], 2);
2689 STRNCPY(&p[last], "+-", 2);
2690 fprintf(debugf, " %s", p);
2691 STRNCPY(&p[last], save, 2);
2692 }
2693 else
2694 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002695
2696 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002697 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002698 code,
2699 state->c,
2700 abs(state->id),
2701 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002702 if (state->id < 0)
2703 return;
2704
2705 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002706
2707 /* grow indent for state->out */
2708 indent->ga_len -= 1;
2709 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002710 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002711 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002712 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002713 ga_append(indent, '\0');
2714
2715 nfa_print_state2(debugf, state->out, indent);
2716
2717 /* replace last part of indent for state->out1 */
2718 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002719 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002720 ga_append(indent, '\0');
2721
2722 nfa_print_state2(debugf, state->out1, indent);
2723
2724 /* shrink indent */
2725 indent->ga_len -= 3;
2726 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002727}
2728
2729/*
2730 * Print the NFA state machine.
2731 */
2732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002733nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002734{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002735 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002736
2737 if (debugf != NULL)
2738 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002739 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002740
Bram Moolenaar473de612013-06-08 18:19:48 +02002741 if (prog->reganch)
2742 fprintf(debugf, "reganch: %d\n", prog->reganch);
2743 if (prog->regstart != NUL)
2744 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2745 prog->regstart, prog->regstart);
2746 if (prog->match_text != NULL)
2747 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002748
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002749 fclose(debugf);
2750 }
2751}
2752#endif /* ENABLE_LOG */
2753#endif /* DEBUG */
2754
2755/*
2756 * Parse r.e. @expr and convert it into postfix form.
2757 * Return the postfix string on success, NULL otherwise.
2758 */
2759 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002760re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002761{
2762 if (nfa_reg(REG_NOPAREN) == FAIL)
2763 return NULL;
2764 EMIT(NFA_MOPEN);
2765 return post_start;
2766}
2767
2768/* NB. Some of the code below is inspired by Russ's. */
2769
2770/*
2771 * Represents an NFA state plus zero or one or two arrows exiting.
2772 * if c == MATCH, no arrows out; matching state.
2773 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2774 * If c < 256, labeled arrow with character c to out.
2775 */
2776
2777static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2778
2779/*
2780 * Allocate and initialize nfa_state_T.
2781 */
2782 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002783alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784{
2785 nfa_state_T *s;
2786
2787 if (istate >= nstate)
2788 return NULL;
2789
2790 s = &state_ptr[istate++];
2791
2792 s->c = c;
2793 s->out = out;
2794 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002795 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002796
2797 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002798 s->lastlist[0] = 0;
2799 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800
2801 return s;
2802}
2803
2804/*
2805 * A partially built NFA without the matching state filled in.
2806 * Frag_T.start points at the start state.
2807 * Frag_T.out is a list of places that need to be set to the
2808 * next state for this fragment.
2809 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002810
2811/* Since the out pointers in the list are always
2812 * uninitialized, we use the pointers themselves
2813 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002814typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002815union Ptrlist
2816{
2817 Ptrlist *next;
2818 nfa_state_T *s;
2819};
2820
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821struct Frag
2822{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002823 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824 Ptrlist *out;
2825};
2826typedef struct Frag Frag_T;
2827
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002829 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002830 */
2831 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002832frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002833{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002834 Frag_T n;
2835
2836 n.start = start;
2837 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002838 return n;
2839}
2840
2841/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842 * Create singleton list containing just outp.
2843 */
2844 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002845list1(
2846 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002847{
2848 Ptrlist *l;
2849
2850 l = (Ptrlist *)outp;
2851 l->next = NULL;
2852 return l;
2853}
2854
2855/*
2856 * Patch the list of states at out to point to start.
2857 */
2858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002859patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860{
2861 Ptrlist *next;
2862
2863 for (; l; l = next)
2864 {
2865 next = l->next;
2866 l->s = s;
2867 }
2868}
2869
2870
2871/*
2872 * Join the two lists l1 and l2, returning the combination.
2873 */
2874 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002875append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002876{
2877 Ptrlist *oldl1;
2878
2879 oldl1 = l1;
2880 while (l1->next)
2881 l1 = l1->next;
2882 l1->next = l2;
2883 return oldl1;
2884}
2885
2886/*
2887 * Stack used for transforming postfix form into NFA.
2888 */
2889static Frag_T empty;
2890
2891 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002892st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002894#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 FILE *df;
2896 int *p2;
2897
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002898 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899 if (df)
2900 {
2901 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002902# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002904# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002906# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002907 for (p2 = postfix; p2 < end; p2++)
2908 {
2909 nfa_set_code(*p2);
2910 fprintf(df, "%s, ", code);
2911 }
2912 nfa_set_code(*p);
2913 fprintf(df, "\nCurrent position is: ");
2914 for (p2 = postfix; p2 <= p; p2 ++)
2915 {
2916 nfa_set_code(*p2);
2917 fprintf(df, "%s, ", code);
2918 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002919# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002920 for (p2 = postfix; p2 < end; p2++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 fprintf(df, "%d, ", *p2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002922 fprintf(df, "\nCurrent position is: ");
2923 for (p2 = postfix; p2 <= p; p2 ++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002924 fprintf(df, "%d, ", *p2);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002925# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 fprintf(df, "\n--------------------------\n");
2927 fclose(df);
2928 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002929#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002930 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931}
2932
2933/*
2934 * Push an item onto the stack.
2935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938{
2939 Frag_T *stackp = *p;
2940
2941 if (stackp >= stack_end)
2942 return;
2943 *stackp = s;
2944 *p = *p + 1;
2945}
2946
2947/*
2948 * Pop an item from the stack.
2949 */
2950 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002951st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002952{
2953 Frag_T *stackp;
2954
2955 *p = *p - 1;
2956 stackp = *p;
2957 if (stackp < stack)
2958 return empty;
2959 return **p;
2960}
2961
2962/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002963 * Estimate the maximum byte length of anything matching "state".
2964 * When unknown or unlimited return -1.
2965 */
2966 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002967nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002968{
2969 int l, r;
2970 nfa_state_T *state = startstate;
2971 int len = 0;
2972
2973 /* detect looping in a NFA_SPLIT */
2974 if (depth > 4)
2975 return -1;
2976
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002977 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002978 {
2979 switch (state->c)
2980 {
2981 case NFA_END_INVISIBLE:
2982 case NFA_END_INVISIBLE_NEG:
2983 /* the end, return what we have */
2984 return len;
2985
2986 case NFA_SPLIT:
2987 /* two alternatives, use the maximum */
2988 l = nfa_max_width(state->out, depth + 1);
2989 r = nfa_max_width(state->out1, depth + 1);
2990 if (l < 0 || r < 0)
2991 return -1;
2992 return len + (l > r ? l : r);
2993
2994 case NFA_ANY:
2995 case NFA_START_COLL:
2996 case NFA_START_NEG_COLL:
2997 /* matches some character, including composing chars */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002998 if (enc_utf8)
2999 len += MB_MAXBYTES;
3000 else if (has_mbyte)
3001 len += 2;
3002 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003003 ++len;
3004 if (state->c != NFA_ANY)
3005 {
3006 /* skip over the characters */
3007 state = state->out1->out;
3008 continue;
3009 }
3010 break;
3011
3012 case NFA_DIGIT:
3013 case NFA_WHITE:
3014 case NFA_HEX:
3015 case NFA_OCTAL:
3016 /* ascii */
3017 ++len;
3018 break;
3019
3020 case NFA_IDENT:
3021 case NFA_SIDENT:
3022 case NFA_KWORD:
3023 case NFA_SKWORD:
3024 case NFA_FNAME:
3025 case NFA_SFNAME:
3026 case NFA_PRINT:
3027 case NFA_SPRINT:
3028 case NFA_NWHITE:
3029 case NFA_NDIGIT:
3030 case NFA_NHEX:
3031 case NFA_NOCTAL:
3032 case NFA_WORD:
3033 case NFA_NWORD:
3034 case NFA_HEAD:
3035 case NFA_NHEAD:
3036 case NFA_ALPHA:
3037 case NFA_NALPHA:
3038 case NFA_LOWER:
3039 case NFA_NLOWER:
3040 case NFA_UPPER:
3041 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003042 case NFA_LOWER_IC:
3043 case NFA_NLOWER_IC:
3044 case NFA_UPPER_IC:
3045 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003046 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003047 /* possibly non-ascii */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003048 if (has_mbyte)
3049 len += 3;
3050 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003051 ++len;
3052 break;
3053
3054 case NFA_START_INVISIBLE:
3055 case NFA_START_INVISIBLE_NEG:
3056 case NFA_START_INVISIBLE_BEFORE:
3057 case NFA_START_INVISIBLE_BEFORE_NEG:
3058 /* zero-width, out1 points to the END state */
3059 state = state->out1->out;
3060 continue;
3061
3062 case NFA_BACKREF1:
3063 case NFA_BACKREF2:
3064 case NFA_BACKREF3:
3065 case NFA_BACKREF4:
3066 case NFA_BACKREF5:
3067 case NFA_BACKREF6:
3068 case NFA_BACKREF7:
3069 case NFA_BACKREF8:
3070 case NFA_BACKREF9:
3071#ifdef FEAT_SYN_HL
3072 case NFA_ZREF1:
3073 case NFA_ZREF2:
3074 case NFA_ZREF3:
3075 case NFA_ZREF4:
3076 case NFA_ZREF5:
3077 case NFA_ZREF6:
3078 case NFA_ZREF7:
3079 case NFA_ZREF8:
3080 case NFA_ZREF9:
3081#endif
3082 case NFA_NEWL:
3083 case NFA_SKIP:
3084 /* unknown width */
3085 return -1;
3086
3087 case NFA_BOL:
3088 case NFA_EOL:
3089 case NFA_BOF:
3090 case NFA_EOF:
3091 case NFA_BOW:
3092 case NFA_EOW:
3093 case NFA_MOPEN:
3094 case NFA_MOPEN1:
3095 case NFA_MOPEN2:
3096 case NFA_MOPEN3:
3097 case NFA_MOPEN4:
3098 case NFA_MOPEN5:
3099 case NFA_MOPEN6:
3100 case NFA_MOPEN7:
3101 case NFA_MOPEN8:
3102 case NFA_MOPEN9:
3103#ifdef FEAT_SYN_HL
3104 case NFA_ZOPEN:
3105 case NFA_ZOPEN1:
3106 case NFA_ZOPEN2:
3107 case NFA_ZOPEN3:
3108 case NFA_ZOPEN4:
3109 case NFA_ZOPEN5:
3110 case NFA_ZOPEN6:
3111 case NFA_ZOPEN7:
3112 case NFA_ZOPEN8:
3113 case NFA_ZOPEN9:
3114 case NFA_ZCLOSE:
3115 case NFA_ZCLOSE1:
3116 case NFA_ZCLOSE2:
3117 case NFA_ZCLOSE3:
3118 case NFA_ZCLOSE4:
3119 case NFA_ZCLOSE5:
3120 case NFA_ZCLOSE6:
3121 case NFA_ZCLOSE7:
3122 case NFA_ZCLOSE8:
3123 case NFA_ZCLOSE9:
3124#endif
3125 case NFA_MCLOSE:
3126 case NFA_MCLOSE1:
3127 case NFA_MCLOSE2:
3128 case NFA_MCLOSE3:
3129 case NFA_MCLOSE4:
3130 case NFA_MCLOSE5:
3131 case NFA_MCLOSE6:
3132 case NFA_MCLOSE7:
3133 case NFA_MCLOSE8:
3134 case NFA_MCLOSE9:
3135 case NFA_NOPEN:
3136 case NFA_NCLOSE:
3137
3138 case NFA_LNUM_GT:
3139 case NFA_LNUM_LT:
3140 case NFA_COL_GT:
3141 case NFA_COL_LT:
3142 case NFA_VCOL_GT:
3143 case NFA_VCOL_LT:
3144 case NFA_MARK_GT:
3145 case NFA_MARK_LT:
3146 case NFA_VISUAL:
3147 case NFA_LNUM:
3148 case NFA_CURSOR:
3149 case NFA_COL:
3150 case NFA_VCOL:
3151 case NFA_MARK:
3152
3153 case NFA_ZSTART:
3154 case NFA_ZEND:
3155 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003156 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003157 case NFA_START_PATTERN:
3158 case NFA_END_PATTERN:
3159 case NFA_COMPOSING:
3160 case NFA_END_COMPOSING:
3161 /* zero-width */
3162 break;
3163
3164 default:
3165 if (state->c < 0)
3166 /* don't know what this is */
3167 return -1;
3168 /* normal character */
3169 len += MB_CHAR2LEN(state->c);
3170 break;
3171 }
3172
3173 /* normal way to continue */
3174 state = state->out;
3175 }
3176
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003177 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003178 return -1;
3179}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003180
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003181/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003182 * Convert a postfix form into its equivalent NFA.
3183 * Return the NFA start state on success, NULL otherwise.
3184 */
3185 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003186post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003187{
3188 int *p;
3189 int mopen;
3190 int mclose;
3191 Frag_T *stack = NULL;
3192 Frag_T *stackp = NULL;
3193 Frag_T *stack_end = NULL;
3194 Frag_T e1;
3195 Frag_T e2;
3196 Frag_T e;
3197 nfa_state_T *s;
3198 nfa_state_T *s1;
3199 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003200 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003201
3202 if (postfix == NULL)
3203 return NULL;
3204
Bram Moolenaar053bb602013-05-20 13:55:21 +02003205#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206#define POP() st_pop(&stackp, stack); \
3207 if (stackp < stack) \
3208 { \
3209 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003210 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 return NULL; \
3212 }
3213
3214 if (nfa_calc_size == FALSE)
3215 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003216 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003217 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003218 if (stack == NULL)
3219 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003221 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003222 }
3223
3224 for (p = postfix; p < end; ++p)
3225 {
3226 switch (*p)
3227 {
3228 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003229 /* Concatenation.
3230 * Pay attention: this operator does not exist in the r.e. itself
3231 * (it is implicit, really). It is added when r.e. is translated
3232 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003233 if (nfa_calc_size == TRUE)
3234 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003235 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236 break;
3237 }
3238 e2 = POP();
3239 e1 = POP();
3240 patch(e1.out, e2.start);
3241 PUSH(frag(e1.start, e2.out));
3242 break;
3243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 case NFA_OR:
3245 /* Alternation */
3246 if (nfa_calc_size == TRUE)
3247 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003248 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 break;
3250 }
3251 e2 = POP();
3252 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003253 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003255 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 PUSH(frag(s, append(e1.out, e2.out)));
3257 break;
3258
3259 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003260 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 if (nfa_calc_size == TRUE)
3262 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003263 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003264 break;
3265 }
3266 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003267 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003269 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 patch(e.out, s);
3271 PUSH(frag(s, list1(&s->out1)));
3272 break;
3273
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003274 case NFA_STAR_NONGREEDY:
3275 /* Zero or more, prefer zero */
3276 if (nfa_calc_size == TRUE)
3277 {
3278 nstate++;
3279 break;
3280 }
3281 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003282 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003283 if (s == NULL)
3284 goto theend;
3285 patch(e.out, s);
3286 PUSH(frag(s, list1(&s->out)));
3287 break;
3288
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 case NFA_QUEST:
3290 /* one or zero atoms=> greedy match */
3291 if (nfa_calc_size == TRUE)
3292 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003293 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 break;
3295 }
3296 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003297 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003299 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 PUSH(frag(s, append(e.out, list1(&s->out1))));
3301 break;
3302
3303 case NFA_QUEST_NONGREEDY:
3304 /* zero or one atoms => non-greedy match */
3305 if (nfa_calc_size == TRUE)
3306 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003307 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 break;
3309 }
3310 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003311 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003313 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 PUSH(frag(s, append(e.out, list1(&s->out))));
3315 break;
3316
Bram Moolenaar417bad22013-06-07 14:08:30 +02003317 case NFA_END_COLL:
3318 case NFA_END_NEG_COLL:
3319 /* On the stack is the sequence starting with NFA_START_COLL or
3320 * NFA_START_NEG_COLL and all possible characters. Patch it to
3321 * add the output to the start. */
3322 if (nfa_calc_size == TRUE)
3323 {
3324 nstate++;
3325 break;
3326 }
3327 e = POP();
3328 s = alloc_state(NFA_END_COLL, NULL, NULL);
3329 if (s == NULL)
3330 goto theend;
3331 patch(e.out, s);
3332 e.start->out1 = s;
3333 PUSH(frag(e.start, list1(&s->out)));
3334 break;
3335
3336 case NFA_RANGE:
3337 /* Before this are two characters, the low and high end of a
3338 * range. Turn them into two states with MIN and MAX. */
3339 if (nfa_calc_size == TRUE)
3340 {
3341 /* nstate += 0; */
3342 break;
3343 }
3344 e2 = POP();
3345 e1 = POP();
3346 e2.start->val = e2.start->c;
3347 e2.start->c = NFA_RANGE_MAX;
3348 e1.start->val = e1.start->c;
3349 e1.start->c = NFA_RANGE_MIN;
3350 patch(e1.out, e2.start);
3351 PUSH(frag(e1.start, e2.out));
3352 break;
3353
Bram Moolenaar699c1202013-09-25 16:41:54 +02003354 case NFA_EMPTY:
3355 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356 if (nfa_calc_size == TRUE)
3357 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003358 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003359 break;
3360 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003361 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003363 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 PUSH(frag(s, list1(&s->out)));
3365 break;
3366
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003367 case NFA_OPT_CHARS:
3368 {
3369 int n;
3370
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003371 /* \%[abc] implemented as:
3372 * NFA_SPLIT
3373 * +-CHAR(a)
3374 * | +-NFA_SPLIT
3375 * | +-CHAR(b)
3376 * | | +-NFA_SPLIT
3377 * | | +-CHAR(c)
3378 * | | | +-next
3379 * | | +- next
3380 * | +- next
3381 * +- next
3382 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003383 n = *++p; /* get number of characters */
3384 if (nfa_calc_size == TRUE)
3385 {
3386 nstate += n;
3387 break;
3388 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003389 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003390 e1.out = NULL; /* stores list with out1's */
3391 s1 = NULL; /* previous NFA_SPLIT to connect to */
3392 while (n-- > 0)
3393 {
3394 e = POP(); /* get character */
3395 s = alloc_state(NFA_SPLIT, e.start, NULL);
3396 if (s == NULL)
3397 goto theend;
3398 if (e1.out == NULL)
3399 e1 = e;
3400 patch(e.out, s1);
3401 append(e1.out, list1(&s->out1));
3402 s1 = s;
3403 }
3404 PUSH(frag(s, e1.out));
3405 break;
3406 }
3407
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003408 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003409 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003410 case NFA_PREV_ATOM_JUST_BEFORE:
3411 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003412 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003413 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003414 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3415 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003416 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003417 int start_state;
3418 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003419 int n = 0;
3420 nfa_state_T *zend;
3421 nfa_state_T *skip;
3422
Bram Moolenaardecd9542013-06-07 16:31:50 +02003423 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003424 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003425 case NFA_PREV_ATOM_NO_WIDTH:
3426 start_state = NFA_START_INVISIBLE;
3427 end_state = NFA_END_INVISIBLE;
3428 break;
3429 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3430 start_state = NFA_START_INVISIBLE_NEG;
3431 end_state = NFA_END_INVISIBLE_NEG;
3432 break;
3433 case NFA_PREV_ATOM_JUST_BEFORE:
3434 start_state = NFA_START_INVISIBLE_BEFORE;
3435 end_state = NFA_END_INVISIBLE;
3436 break;
3437 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3438 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3439 end_state = NFA_END_INVISIBLE_NEG;
3440 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003441 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003442 start_state = NFA_START_PATTERN;
3443 end_state = NFA_END_PATTERN;
3444 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003445 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003446
3447 if (before)
3448 n = *++p; /* get the count */
3449
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003450 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003451 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003452 * The \@<= operator: match for the preceding atom.
3453 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003454 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003455 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456
3457 if (nfa_calc_size == TRUE)
3458 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003459 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003460 break;
3461 }
3462 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003463 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003465 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466
Bram Moolenaar87953742013-06-05 18:52:40 +02003467 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003468 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003469 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003470 if (pattern)
3471 {
3472 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3473 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003474 if (skip == NULL)
3475 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003476 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003477 if (zend == NULL)
3478 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003479 s1->out= skip;
3480 patch(e.out, zend);
3481 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003482 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003483 else
3484 {
3485 patch(e.out, s1);
3486 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003487 if (before)
3488 {
3489 if (n <= 0)
3490 /* See if we can guess the maximum width, it avoids a
3491 * lot of pointless tries. */
3492 n = nfa_max_width(e.start, 0);
3493 s->val = n; /* store the count */
3494 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003495 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003497 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003499 case NFA_COMPOSING: /* char with composing char */
3500#if 0
3501 /* TODO */
3502 if (regflags & RF_ICOMBINE)
3503 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003504 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003505 }
3506#endif
3507 /* FALLTHROUGH */
3508
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003509 case NFA_MOPEN: /* \( \) Submatch */
3510 case NFA_MOPEN1:
3511 case NFA_MOPEN2:
3512 case NFA_MOPEN3:
3513 case NFA_MOPEN4:
3514 case NFA_MOPEN5:
3515 case NFA_MOPEN6:
3516 case NFA_MOPEN7:
3517 case NFA_MOPEN8:
3518 case NFA_MOPEN9:
3519#ifdef FEAT_SYN_HL
3520 case NFA_ZOPEN: /* \z( \) Submatch */
3521 case NFA_ZOPEN1:
3522 case NFA_ZOPEN2:
3523 case NFA_ZOPEN3:
3524 case NFA_ZOPEN4:
3525 case NFA_ZOPEN5:
3526 case NFA_ZOPEN6:
3527 case NFA_ZOPEN7:
3528 case NFA_ZOPEN8:
3529 case NFA_ZOPEN9:
3530#endif
3531 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532 if (nfa_calc_size == TRUE)
3533 {
3534 nstate += 2;
3535 break;
3536 }
3537
3538 mopen = *p;
3539 switch (*p)
3540 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003541 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3542#ifdef FEAT_SYN_HL
3543 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3544 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3545 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3546 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3547 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3548 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3549 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3550 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3551 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3552 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3553#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003554 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003556 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003557 mclose = *p + NSUBEXP;
3558 break;
3559 }
3560
3561 /* Allow "NFA_MOPEN" as a valid postfix representation for
3562 * the empty regexp "". In this case, the NFA will be
3563 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3564 * empty groups of parenthesis, and empty mbyte chars */
3565 if (stackp == stack)
3566 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003567 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003569 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003570 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003572 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003573 patch(list1(&s->out), s1);
3574 PUSH(frag(s, list1(&s1->out)));
3575 break;
3576 }
3577
3578 /* At least one node was emitted before NFA_MOPEN, so
3579 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3580 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003581 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003582 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003583 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003584
Bram Moolenaar525666f2013-06-02 16:40:55 +02003585 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003587 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 patch(e.out, s1);
3589
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003590 if (mopen == NFA_COMPOSING)
3591 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 patch(list1(&s->out1), s1);
3593
3594 PUSH(frag(s, list1(&s1->out)));
3595 break;
3596
Bram Moolenaar5714b802013-05-28 22:03:20 +02003597 case NFA_BACKREF1:
3598 case NFA_BACKREF2:
3599 case NFA_BACKREF3:
3600 case NFA_BACKREF4:
3601 case NFA_BACKREF5:
3602 case NFA_BACKREF6:
3603 case NFA_BACKREF7:
3604 case NFA_BACKREF8:
3605 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003606#ifdef FEAT_SYN_HL
3607 case NFA_ZREF1:
3608 case NFA_ZREF2:
3609 case NFA_ZREF3:
3610 case NFA_ZREF4:
3611 case NFA_ZREF5:
3612 case NFA_ZREF6:
3613 case NFA_ZREF7:
3614 case NFA_ZREF8:
3615 case NFA_ZREF9:
3616#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003617 if (nfa_calc_size == TRUE)
3618 {
3619 nstate += 2;
3620 break;
3621 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003622 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003623 if (s == NULL)
3624 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003625 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003626 if (s1 == NULL)
3627 goto theend;
3628 patch(list1(&s->out), s1);
3629 PUSH(frag(s, list1(&s1->out)));
3630 break;
3631
Bram Moolenaar423532e2013-05-29 21:14:42 +02003632 case NFA_LNUM:
3633 case NFA_LNUM_GT:
3634 case NFA_LNUM_LT:
3635 case NFA_VCOL:
3636 case NFA_VCOL_GT:
3637 case NFA_VCOL_LT:
3638 case NFA_COL:
3639 case NFA_COL_GT:
3640 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003641 case NFA_MARK:
3642 case NFA_MARK_GT:
3643 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003644 {
3645 int n = *++p; /* lnum, col or mark name */
3646
Bram Moolenaar423532e2013-05-29 21:14:42 +02003647 if (nfa_calc_size == TRUE)
3648 {
3649 nstate += 1;
3650 break;
3651 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003652 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003653 if (s == NULL)
3654 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003655 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003656 PUSH(frag(s, list1(&s->out)));
3657 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003658 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003659
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 case NFA_ZSTART:
3661 case NFA_ZEND:
3662 default:
3663 /* Operands */
3664 if (nfa_calc_size == TRUE)
3665 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003666 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 break;
3668 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003669 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003670 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003671 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 PUSH(frag(s, list1(&s->out)));
3673 break;
3674
3675 } /* switch(*p) */
3676
3677 } /* for(p = postfix; *p; ++p) */
3678
3679 if (nfa_calc_size == TRUE)
3680 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003681 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003682 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003683 }
3684
3685 e = POP();
3686 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003687 {
3688 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689 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 +02003690 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003691
3692 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003693 {
3694 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003696 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698 matchstate = &state_ptr[istate++]; /* the match state */
3699 matchstate->c = NFA_MATCH;
3700 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003701 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003702
3703 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003704 ret = e.start;
3705
3706theend:
3707 vim_free(stack);
3708 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709
3710#undef POP1
3711#undef PUSH1
3712#undef POP2
3713#undef PUSH2
3714#undef POP
3715#undef PUSH
3716}
3717
Bram Moolenaara2947e22013-06-11 22:44:09 +02003718/*
3719 * After building the NFA program, inspect it to add optimization hints.
3720 */
3721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003722nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003723{
3724 int i;
3725 int c;
3726
3727 for (i = 0; i < prog->nstate; ++i)
3728 {
3729 c = prog->state[i].c;
3730 if (c == NFA_START_INVISIBLE
3731 || c == NFA_START_INVISIBLE_NEG
3732 || c == NFA_START_INVISIBLE_BEFORE
3733 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3734 {
3735 int directly;
3736
3737 /* Do it directly when what follows is possibly the end of the
3738 * match. */
3739 if (match_follows(prog->state[i].out1->out, 0))
3740 directly = TRUE;
3741 else
3742 {
3743 int ch_invisible = failure_chance(prog->state[i].out, 0);
3744 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3745
3746 /* Postpone when the invisible match is expensive or has a
3747 * lower chance of failing. */
3748 if (c == NFA_START_INVISIBLE_BEFORE
3749 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3750 {
3751 /* "before" matches are very expensive when
3752 * unbounded, always prefer what follows then,
3753 * unless what follows will always match.
3754 * Otherwise strongly prefer what follows. */
3755 if (prog->state[i].val <= 0 && ch_follows > 0)
3756 directly = FALSE;
3757 else
3758 directly = ch_follows * 10 < ch_invisible;
3759 }
3760 else
3761 {
3762 /* normal invisible, first do the one with the
3763 * highest failure chance */
3764 directly = ch_follows < ch_invisible;
3765 }
3766 }
3767 if (directly)
3768 /* switch to the _FIRST state */
3769 ++prog->state[i].c;
3770 }
3771 }
3772}
3773
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774/****************************************************************
3775 * NFA execution code.
3776 ****************************************************************/
3777
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003778typedef struct
3779{
3780 int in_use; /* number of subexpr with useful info */
3781
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003782 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003783 union
3784 {
3785 struct multipos
3786 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003787 linenr_T start_lnum;
3788 linenr_T end_lnum;
3789 colnr_T start_col;
3790 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003791 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003792 struct linepos
3793 {
3794 char_u *start;
3795 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003796 } line[NSUBEXP];
3797 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003798} regsub_T;
3799
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003800typedef struct
3801{
3802 regsub_T norm; /* \( .. \) matches */
3803#ifdef FEAT_SYN_HL
3804 regsub_T synt; /* \z( .. \) matches */
3805#endif
3806} regsubs_T;
3807
Bram Moolenaara2d95102013-06-04 14:23:05 +02003808/* nfa_pim_T stores a Postponed Invisible Match. */
3809typedef struct nfa_pim_S nfa_pim_T;
3810struct nfa_pim_S
3811{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003812 int result; /* NFA_PIM_*, see below */
3813 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003814 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003815 union
3816 {
3817 lpos_T pos;
3818 char_u *ptr;
3819 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003820};
3821
3822/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003823#define NFA_PIM_UNUSED 0 /* pim not used */
3824#define NFA_PIM_TODO 1 /* pim not done yet */
3825#define NFA_PIM_MATCH 2 /* pim executed, matches */
3826#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003827
3828
Bram Moolenaar963fee22013-05-26 21:47:28 +02003829/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003830typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831{
3832 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003833 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003834 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3835 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003836 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003837} nfa_thread_T;
3838
Bram Moolenaar963fee22013-05-26 21:47:28 +02003839/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003840typedef struct
3841{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003842 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003843 int n; /* nr of states currently in "t" */
3844 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003845 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003846 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003847} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003848
Bram Moolenaar5714b802013-05-28 22:03:20 +02003849#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003850static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003851
3852 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003853log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003854{
3855 log_subexpr(&subs->norm);
3856# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003857 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003858 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003859# endif
3860}
3861
Bram Moolenaar5714b802013-05-28 22:03:20 +02003862 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003863log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003864{
3865 int j;
3866
3867 for (j = 0; j < sub->in_use; j++)
3868 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003869 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003870 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003871 sub->list.multi[j].start_col,
3872 (int)sub->list.multi[j].start_lnum,
3873 sub->list.multi[j].end_col,
3874 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003875 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003876 {
3877 char *s = (char *)sub->list.line[j].start;
3878 char *e = (char *)sub->list.line[j].end;
3879
Bram Moolenaar87953742013-06-05 18:52:40 +02003880 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003881 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003882 s == NULL ? "NULL" : s,
3883 e == NULL ? "NULL" : e);
3884 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003885}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003886
3887 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003888pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003889{
3890 static char buf[30];
3891
3892 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3893 buf[0] = NUL;
3894 else
3895 {
3896 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003897 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003898 }
3899 return buf;
3900}
3901
Bram Moolenaar5714b802013-05-28 22:03:20 +02003902#endif
3903
Bram Moolenaar963fee22013-05-26 21:47:28 +02003904/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003905static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003906#ifdef FEAT_RELTIME
3907static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003908static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003909static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003910#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003911
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003912static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003913static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003914
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003915/*
3916 * Copy postponed invisible match info from "from" to "to".
3917 */
3918 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003919copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003920{
3921 to->result = from->result;
3922 to->state = from->state;
3923 copy_sub(&to->subs.norm, &from->subs.norm);
3924#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003925 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003926 copy_sub(&to->subs.synt, &from->subs.synt);
3927#endif
3928 to->end = from->end;
3929}
3930
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003932clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003933{
3934 if (REG_MULTI)
3935 /* Use 0xff to set lnum to -1 */
3936 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003937 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003938 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003939 vim_memset(sub->list.line, 0,
3940 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003941 sub->in_use = 0;
3942}
3943
3944/*
3945 * Copy the submatches from "from" to "to".
3946 */
3947 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003948copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003949{
3950 to->in_use = from->in_use;
3951 if (from->in_use > 0)
3952 {
3953 /* Copy the match start and end positions. */
3954 if (REG_MULTI)
3955 mch_memmove(&to->list.multi[0],
3956 &from->list.multi[0],
3957 sizeof(struct multipos) * from->in_use);
3958 else
3959 mch_memmove(&to->list.line[0],
3960 &from->list.line[0],
3961 sizeof(struct linepos) * from->in_use);
3962 }
3963}
3964
3965/*
3966 * Like copy_sub() but exclude the main match.
3967 */
3968 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003969copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003970{
3971 if (to->in_use < from->in_use)
3972 to->in_use = from->in_use;
3973 if (from->in_use > 1)
3974 {
3975 /* Copy the match start and end positions. */
3976 if (REG_MULTI)
3977 mch_memmove(&to->list.multi[1],
3978 &from->list.multi[1],
3979 sizeof(struct multipos) * (from->in_use - 1));
3980 else
3981 mch_memmove(&to->list.line[1],
3982 &from->list.line[1],
3983 sizeof(struct linepos) * (from->in_use - 1));
3984 }
3985}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003986
Bram Moolenaar428e9872013-05-30 17:05:39 +02003987/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003988 * Like copy_sub() but only do the end of the main match if \ze is present.
3989 */
3990 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003991copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003992{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003993 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003994 {
3995 if (REG_MULTI)
3996 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003997 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003998 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003999 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4000 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004001 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004002 }
4003 else
4004 {
4005 if (from->list.line[0].end != NULL)
4006 to->list.line[0].end = from->list.line[0].end;
4007 }
4008 }
4009}
4010
4011/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004012 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004013 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004014 */
4015 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004016sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004017{
4018 int i;
4019 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004020 linenr_T s1;
4021 linenr_T s2;
4022 char_u *sp1;
4023 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004024
4025 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4026 if (REG_MULTI)
4027 {
4028 for (i = 0; i < todo; ++i)
4029 {
4030 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004031 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004032 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004033 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004035 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004036 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004037 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004038 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004039 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004040 if (s1 != -1 && sub1->list.multi[i].start_col
4041 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004042 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004043
Bram Moolenaar0270f382018-07-17 05:43:58 +02004044 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004045 {
4046 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004047 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004048 else
4049 s1 = -1;
4050 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004051 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004052 else
4053 s2 = -1;
4054 if (s1 != s2)
4055 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004056 if (s1 != -1 && sub1->list.multi[i].end_col
4057 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004058 return FALSE;
4059 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004060 }
4061 }
4062 else
4063 {
4064 for (i = 0; i < todo; ++i)
4065 {
4066 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004067 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004068 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004069 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004070 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004071 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004072 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004073 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004074 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004075 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004076 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004077 {
4078 if (i < sub1->in_use)
4079 sp1 = sub1->list.line[i].end;
4080 else
4081 sp1 = NULL;
4082 if (i < sub2->in_use)
4083 sp2 = sub2->list.line[i].end;
4084 else
4085 sp2 = NULL;
4086 if (sp1 != sp2)
4087 return FALSE;
4088 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004089 }
4090 }
4091
4092 return TRUE;
4093}
4094
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004095#ifdef ENABLE_LOG
4096 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004097report_state(char *action,
4098 regsub_T *sub,
4099 nfa_state_T *state,
4100 int lid,
4101 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004102{
4103 int col;
4104
4105 if (sub->in_use <= 0)
4106 col = -1;
4107 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004108 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004109 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004110 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004111 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004112 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4113 action, abs(state->id), lid, state->c, code, col,
4114 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004115}
4116#endif
4117
Bram Moolenaar43e02982013-06-07 17:31:29 +02004118/*
4119 * Return TRUE if the same state is already in list "l" with the same
4120 * positions as "subs".
4121 */
4122 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004123has_state_with_pos(
4124 nfa_list_T *l, /* runtime state list */
4125 nfa_state_T *state, /* state to update */
4126 regsubs_T *subs, /* pointers to subexpressions */
4127 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004128{
4129 nfa_thread_T *thread;
4130 int i;
4131
4132 for (i = 0; i < l->n; ++i)
4133 {
4134 thread = &l->t[i];
4135 if (thread->state->id == state->id
4136 && sub_equal(&thread->subs.norm, &subs->norm)
4137#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004138 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004139 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004140#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004141 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004142 return TRUE;
4143 }
4144 return FALSE;
4145}
4146
4147/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004148 * Return TRUE if "one" and "two" are equal. That includes when both are not
4149 * set.
4150 */
4151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004152pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004153{
4154 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4155 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4156
4157 if (one_unused)
4158 /* one is unused: equal when two is also unused */
4159 return two_unused;
4160 if (two_unused)
4161 /* one is used and two is not: not equal */
4162 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004163 /* compare the state id */
4164 if (one->state->id != two->state->id)
4165 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004166 /* compare the position */
4167 if (REG_MULTI)
4168 return one->end.pos.lnum == two->end.pos.lnum
4169 && one->end.pos.col == two->end.pos.col;
4170 return one->end.ptr == two->end.ptr;
4171}
4172
4173/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004174 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4175 */
4176 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004177match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004178{
4179 nfa_state_T *state = startstate;
4180
4181 /* avoid too much recursion */
4182 if (depth > 10)
4183 return FALSE;
4184
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004185 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004186 {
4187 switch (state->c)
4188 {
4189 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004190 case NFA_MCLOSE:
4191 case NFA_END_INVISIBLE:
4192 case NFA_END_INVISIBLE_NEG:
4193 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004194 return TRUE;
4195
4196 case NFA_SPLIT:
4197 return match_follows(state->out, depth + 1)
4198 || match_follows(state->out1, depth + 1);
4199
4200 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004201 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004202 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004203 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004204 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004205 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004206 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004207 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004208 case NFA_COMPOSING:
4209 /* skip ahead to next state */
4210 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004211 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004212
4213 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004214 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004215 case NFA_IDENT:
4216 case NFA_SIDENT:
4217 case NFA_KWORD:
4218 case NFA_SKWORD:
4219 case NFA_FNAME:
4220 case NFA_SFNAME:
4221 case NFA_PRINT:
4222 case NFA_SPRINT:
4223 case NFA_WHITE:
4224 case NFA_NWHITE:
4225 case NFA_DIGIT:
4226 case NFA_NDIGIT:
4227 case NFA_HEX:
4228 case NFA_NHEX:
4229 case NFA_OCTAL:
4230 case NFA_NOCTAL:
4231 case NFA_WORD:
4232 case NFA_NWORD:
4233 case NFA_HEAD:
4234 case NFA_NHEAD:
4235 case NFA_ALPHA:
4236 case NFA_NALPHA:
4237 case NFA_LOWER:
4238 case NFA_NLOWER:
4239 case NFA_UPPER:
4240 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004241 case NFA_LOWER_IC:
4242 case NFA_NLOWER_IC:
4243 case NFA_UPPER_IC:
4244 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004245 case NFA_START_COLL:
4246 case NFA_START_NEG_COLL:
4247 case NFA_NEWL:
4248 /* state will advance input */
4249 return FALSE;
4250
4251 default:
4252 if (state->c > 0)
4253 /* state will advance input */
4254 return FALSE;
4255
4256 /* Others: zero-width or possibly zero-width, might still find
4257 * a match at the same position, keep looking. */
4258 break;
4259 }
4260 state = state->out;
4261 }
4262 return FALSE;
4263}
4264
4265
4266/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004267 * Return TRUE if "state" is already in list "l".
4268 */
4269 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004270state_in_list(
4271 nfa_list_T *l, /* runtime state list */
4272 nfa_state_T *state, /* state to update */
4273 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004274{
4275 if (state->lastlist[nfa_ll_index] == l->id)
4276 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004277 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004278 return TRUE;
4279 }
4280 return FALSE;
4281}
4282
Bram Moolenaar16b35782016-09-09 20:29:50 +02004283/* Offset used for "off" by addstate_here(). */
4284#define ADDSTATE_HERE_OFFSET 10
4285
Bram Moolenaard05bf562013-06-30 23:24:08 +02004286/*
4287 * Add "state" and possibly what follows to state list ".".
4288 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004289 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004290 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004291 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004292addstate(
4293 nfa_list_T *l, /* runtime state list */
4294 nfa_state_T *state, /* state to update */
4295 regsubs_T *subs_arg, /* pointers to subexpressions */
4296 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004297 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004299 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004300 int off = off_arg;
4301 int add_here = FALSE;
4302 int listindex = 0;
4303 int k;
4304 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004305 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004306 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004307 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004308 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004309 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004310 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004311 regsubs_T *subs = subs_arg;
4312 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004313#ifdef ENABLE_LOG
4314 int did_print = FALSE;
4315#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004316 static int depth = 0;
4317
4318 // This function is called recursively. When the depth is too much we run
4319 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004320 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004321 {
4322 --depth;
4323 return NULL;
4324 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004325
Bram Moolenaar16b35782016-09-09 20:29:50 +02004326 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4327 {
4328 add_here = TRUE;
4329 off = 0;
4330 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4331 }
4332
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004333 switch (state->c)
4334 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335 case NFA_NCLOSE:
4336 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004337 case NFA_MCLOSE1:
4338 case NFA_MCLOSE2:
4339 case NFA_MCLOSE3:
4340 case NFA_MCLOSE4:
4341 case NFA_MCLOSE5:
4342 case NFA_MCLOSE6:
4343 case NFA_MCLOSE7:
4344 case NFA_MCLOSE8:
4345 case NFA_MCLOSE9:
4346#ifdef FEAT_SYN_HL
4347 case NFA_ZCLOSE:
4348 case NFA_ZCLOSE1:
4349 case NFA_ZCLOSE2:
4350 case NFA_ZCLOSE3:
4351 case NFA_ZCLOSE4:
4352 case NFA_ZCLOSE5:
4353 case NFA_ZCLOSE6:
4354 case NFA_ZCLOSE7:
4355 case NFA_ZCLOSE8:
4356 case NFA_ZCLOSE9:
4357#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004358 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004359 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004360 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004361 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004362 /* These nodes are not added themselves but their "out" and/or
4363 * "out1" may be added below. */
4364 break;
4365
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004366 case NFA_BOL:
4367 case NFA_BOF:
4368 /* "^" won't match past end-of-line, don't bother trying.
4369 * Except when at the end of the line, or when we are going to the
4370 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004371 if (rex.input > rex.line
4372 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004373 && (nfa_endp == NULL
4374 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004375 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004376 goto skip_add;
4377 /* FALLTHROUGH */
4378
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004379 case NFA_MOPEN1:
4380 case NFA_MOPEN2:
4381 case NFA_MOPEN3:
4382 case NFA_MOPEN4:
4383 case NFA_MOPEN5:
4384 case NFA_MOPEN6:
4385 case NFA_MOPEN7:
4386 case NFA_MOPEN8:
4387 case NFA_MOPEN9:
4388#ifdef FEAT_SYN_HL
4389 case NFA_ZOPEN:
4390 case NFA_ZOPEN1:
4391 case NFA_ZOPEN2:
4392 case NFA_ZOPEN3:
4393 case NFA_ZOPEN4:
4394 case NFA_ZOPEN5:
4395 case NFA_ZOPEN6:
4396 case NFA_ZOPEN7:
4397 case NFA_ZOPEN8:
4398 case NFA_ZOPEN9:
4399#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004400 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004401 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004402 /* These nodes need to be added so that we can bail out when it
4403 * was added to this list before at the same position to avoid an
4404 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004405
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004406 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004407 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004409 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004410 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004411 * when there is a PIM. For NFA_MATCH check the position,
4412 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004413 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004414 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004415 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004416 /* When called from addstate_here() do insert before
4417 * existing states. */
4418 if (add_here)
4419 {
4420 for (k = 0; k < l->n && k < listindex; ++k)
4421 if (l->t[k].state->id == state->id)
4422 {
4423 found = TRUE;
4424 break;
4425 }
4426 }
4427 if (!add_here || found)
4428 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004429skip_add:
4430#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004431 nfa_set_code(state->c);
4432 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4433 abs(state->id), l->id, state->c, code,
4434 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004435#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004436 --depth;
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 Moolenaar688b3982019-02-13 21:47:36 +01004447 // When there are backreferences or PIMs the number of states may
4448 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004449 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004450 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004451 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004452 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004453 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004454
Bram Moolenaar688b3982019-02-13 21:47:36 +01004455 if ((long)(newsize >> 10) >= p_mmp)
4456 {
4457 emsg(_(e_maxmempat));
4458 --depth;
4459 return NULL;
4460 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004461 if (subs != &temp_subs)
4462 {
4463 /* "subs" may point into the current array, need to make a
4464 * copy before it becomes invalid. */
4465 copy_sub(&temp_subs.norm, &subs->norm);
4466#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004467 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004468 copy_sub(&temp_subs.synt, &subs->synt);
4469#endif
4470 subs = &temp_subs;
4471 }
4472
Bram Moolenaar688b3982019-02-13 21:47:36 +01004473 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004474 if (newt == NULL)
4475 {
4476 // out of memory
4477 --depth;
4478 return NULL;
4479 }
4480 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004481 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004482 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004483
4484 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004485 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004486 thread = &l->t[l->n++];
4487 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004488 if (pim == NULL)
4489 thread->pim.result = NFA_PIM_UNUSED;
4490 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004491 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004492 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004493 l->has_pim = TRUE;
4494 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004495 copy_sub(&thread->subs.norm, &subs->norm);
4496#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004497 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004498 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004499#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004500#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004501 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004502 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004503#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004504 }
4505
4506#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004507 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004508 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509#endif
4510 switch (state->c)
4511 {
4512 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004513 break;
4514
4515 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004516 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004517 subs = addstate(l, state->out, subs, pim, off_arg);
4518 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 break;
4520
Bram Moolenaar699c1202013-09-25 16:41:54 +02004521 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004522 case NFA_NOPEN:
4523 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004524 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 break;
4526
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004527 case NFA_MOPEN:
4528 case NFA_MOPEN1:
4529 case NFA_MOPEN2:
4530 case NFA_MOPEN3:
4531 case NFA_MOPEN4:
4532 case NFA_MOPEN5:
4533 case NFA_MOPEN6:
4534 case NFA_MOPEN7:
4535 case NFA_MOPEN8:
4536 case NFA_MOPEN9:
4537#ifdef FEAT_SYN_HL
4538 case NFA_ZOPEN:
4539 case NFA_ZOPEN1:
4540 case NFA_ZOPEN2:
4541 case NFA_ZOPEN3:
4542 case NFA_ZOPEN4:
4543 case NFA_ZOPEN5:
4544 case NFA_ZOPEN6:
4545 case NFA_ZOPEN7:
4546 case NFA_ZOPEN8:
4547 case NFA_ZOPEN9:
4548#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004551 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004552 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004553 sub = &subs->norm;
4554 }
4555#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004556 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004557 {
4558 subidx = state->c - NFA_ZOPEN;
4559 sub = &subs->synt;
4560 }
4561#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004562 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004563 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004564 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004565 sub = &subs->norm;
4566 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004567
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004568 /* avoid compiler warnings */
4569 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004570 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004571
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004572 /* Set the position (with "off" added) in the subexpression. Save
4573 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574 if (REG_MULTI)
4575 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004576 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004577 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004578 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004579 save_in_use = -1;
4580 }
4581 else
4582 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004583 save_in_use = sub->in_use;
4584 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004585 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004586 sub->list.multi[i].start_lnum = -1;
4587 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004588 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004589 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004590 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004591 if (off == -1)
4592 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004593 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004594 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004595 }
4596 else
4597 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004598 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004599 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004600 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004601 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004602 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004603 }
4604 else
4605 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004606 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004607 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004608 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004609 save_in_use = -1;
4610 }
4611 else
4612 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004613 save_in_use = sub->in_use;
4614 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004615 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004616 sub->list.line[i].start = NULL;
4617 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004618 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004619 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004620 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004621 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004622 }
4623
Bram Moolenaar16b35782016-09-09 20:29:50 +02004624 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004625 if (subs == NULL)
4626 break;
4627 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004628#ifdef FEAT_SYN_HL
4629 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4630 sub = &subs->synt;
4631 else
4632#endif
4633 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004634
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004635 if (save_in_use == -1)
4636 {
4637 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004638 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004639 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004640 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004641 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004642 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004643 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004644 break;
4645
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004646 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004647 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004648 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004649 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004651 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004652 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004653 break;
4654 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004655 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004656 case NFA_MCLOSE1:
4657 case NFA_MCLOSE2:
4658 case NFA_MCLOSE3:
4659 case NFA_MCLOSE4:
4660 case NFA_MCLOSE5:
4661 case NFA_MCLOSE6:
4662 case NFA_MCLOSE7:
4663 case NFA_MCLOSE8:
4664 case NFA_MCLOSE9:
4665#ifdef FEAT_SYN_HL
4666 case NFA_ZCLOSE:
4667 case NFA_ZCLOSE1:
4668 case NFA_ZCLOSE2:
4669 case NFA_ZCLOSE3:
4670 case NFA_ZCLOSE4:
4671 case NFA_ZCLOSE5:
4672 case NFA_ZCLOSE6:
4673 case NFA_ZCLOSE7:
4674 case NFA_ZCLOSE8:
4675 case NFA_ZCLOSE9:
4676#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004678 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004679 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004681 sub = &subs->norm;
4682 }
4683#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004684 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004685 {
4686 subidx = state->c - NFA_ZCLOSE;
4687 sub = &subs->synt;
4688 }
4689#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004690 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004691 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004692 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004693 sub = &subs->norm;
4694 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004696 /* We don't fill in gaps here, there must have been an MOPEN that
4697 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004698 save_in_use = sub->in_use;
4699 if (sub->in_use <= subidx)
4700 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701 if (REG_MULTI)
4702 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004703 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004704 if (off == -1)
4705 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004706 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004707 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004708 }
4709 else
4710 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004711 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004712 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004713 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004714 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004715 /* avoid compiler warnings */
4716 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004717 }
4718 else
4719 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004720 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004721 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004722 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004723 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 }
4725
Bram Moolenaar16b35782016-09-09 20:29:50 +02004726 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004727 if (subs == NULL)
4728 break;
Bram Moolenaarebefd992013-08-14 14:18:40 +02004729 /* "subs" may have changed, need to set "sub" again */
4730#ifdef FEAT_SYN_HL
4731 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4732 sub = &subs->synt;
4733 else
4734#endif
4735 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736
4737 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004738 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004739 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004740 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004741 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004742 break;
4743 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004744 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004745 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004746}
4747
4748/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004749 * Like addstate(), but the new state(s) are put at position "*ip".
4750 * Used for zero-width matches, next state to use is the added one.
4751 * This makes sure the order of states to be tried does not change, which
4752 * matters for alternatives.
4753 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004754 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004755addstate_here(
4756 nfa_list_T *l, /* runtime state list */
4757 nfa_state_T *state, /* state to update */
4758 regsubs_T *subs, /* pointers to subexpressions */
4759 nfa_pim_T *pim, /* postponed look-behind match */
4760 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004761{
4762 int tlen = l->n;
4763 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004764 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004765 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004766
Bram Moolenaar16b35782016-09-09 20:29:50 +02004767 /* First add the state(s) at the end, so that we know how many there are.
4768 * Pass the listidx as offset (avoids adding another argument to
4769 * addstate(). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004770 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4771 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004772 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004773
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004774 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004775 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004776 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004777
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004778 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004779 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004780 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004781 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004782 if (count == 1)
4783 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004784 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004785 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004786 }
4787 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004788 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004789 if (l->n + count - 1 >= l->len)
4790 {
4791 /* not enough space to move the new states, reallocate the list
4792 * and move the states to the right position */
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004793 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004794 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004795 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004796
Bram Moolenaar688b3982019-02-13 21:47:36 +01004797 if ((long)(newsize >> 10) >= p_mmp)
4798 {
4799 emsg(_(e_maxmempat));
4800 return NULL;
4801 }
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01004802 newl = (nfa_thread_T *)alloc((int)newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004803 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004804 return NULL;
4805 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004806 mch_memmove(&(newl[0]),
4807 &(l->t[0]),
4808 sizeof(nfa_thread_T) * listidx);
4809 mch_memmove(&(newl[listidx]),
4810 &(l->t[l->n - count]),
4811 sizeof(nfa_thread_T) * count);
4812 mch_memmove(&(newl[listidx + count]),
4813 &(l->t[listidx + 1]),
4814 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4815 vim_free(l->t);
4816 l->t = newl;
4817 }
4818 else
4819 {
4820 /* make space for new states, then move them from the
4821 * end to the current position */
4822 mch_memmove(&(l->t[listidx + count]),
4823 &(l->t[listidx + 1]),
4824 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4825 mch_memmove(&(l->t[listidx]),
4826 &(l->t[l->n - 1]),
4827 sizeof(nfa_thread_T) * count);
4828 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004829 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004830 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004831 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004832
4833 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004834}
4835
4836/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004837 * Check character class "class" against current character c.
4838 */
4839 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004840check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004841{
4842 switch (class)
4843 {
4844 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004845 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004846 return OK;
4847 break;
4848 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004849 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004850 return OK;
4851 break;
4852 case NFA_CLASS_BLANK:
4853 if (c == ' ' || c == '\t')
4854 return OK;
4855 break;
4856 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004857 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858 return OK;
4859 break;
4860 case NFA_CLASS_DIGIT:
4861 if (VIM_ISDIGIT(c))
4862 return OK;
4863 break;
4864 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004865 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 return OK;
4867 break;
4868 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004869 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870 return OK;
4871 break;
4872 case NFA_CLASS_PRINT:
4873 if (vim_isprintc(c))
4874 return OK;
4875 break;
4876 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004877 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004878 return OK;
4879 break;
4880 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004881 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004882 return OK;
4883 break;
4884 case NFA_CLASS_UPPER:
4885 if (MB_ISUPPER(c))
4886 return OK;
4887 break;
4888 case NFA_CLASS_XDIGIT:
4889 if (vim_isxdigit(c))
4890 return OK;
4891 break;
4892 case NFA_CLASS_TAB:
4893 if (c == '\t')
4894 return OK;
4895 break;
4896 case NFA_CLASS_RETURN:
4897 if (c == '\r')
4898 return OK;
4899 break;
4900 case NFA_CLASS_BACKSPACE:
4901 if (c == '\b')
4902 return OK;
4903 break;
4904 case NFA_CLASS_ESCAPE:
4905 if (c == '\033')
4906 return OK;
4907 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004908 case NFA_CLASS_IDENT:
4909 if (vim_isIDc(c))
4910 return OK;
4911 break;
4912 case NFA_CLASS_KEYWORD:
4913 if (reg_iswordc(c))
4914 return OK;
4915 break;
4916 case NFA_CLASS_FNAME:
4917 if (vim_isfilec(c))
4918 return OK;
4919 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920
4921 default:
4922 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004923 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004924 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925 }
4926 return FAIL;
4927}
4928
Bram Moolenaar5714b802013-05-28 22:03:20 +02004929/*
4930 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004931 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004932 */
4933 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004934match_backref(
4935 regsub_T *sub, /* pointers to subexpressions */
4936 int subidx,
4937 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004938{
4939 int len;
4940
4941 if (sub->in_use <= subidx)
4942 {
4943retempty:
4944 /* backref was not set, match an empty string */
4945 *bytelen = 0;
4946 return TRUE;
4947 }
4948
4949 if (REG_MULTI)
4950 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004951 if (sub->list.multi[subidx].start_lnum < 0
4952 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004953 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004954 if (sub->list.multi[subidx].start_lnum == rex.lnum
4955 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004956 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004957 len = sub->list.multi[subidx].end_col
4958 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004959 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4960 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004961 {
4962 *bytelen = len;
4963 return TRUE;
4964 }
4965 }
4966 else
4967 {
4968 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004969 sub->list.multi[subidx].start_lnum,
4970 sub->list.multi[subidx].start_col,
4971 sub->list.multi[subidx].end_lnum,
4972 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004973 bytelen) == RA_MATCH)
4974 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004975 }
4976 }
4977 else
4978 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004979 if (sub->list.line[subidx].start == NULL
4980 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004981 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004982 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004983 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004984 {
4985 *bytelen = len;
4986 return TRUE;
4987 }
4988 }
4989 return FALSE;
4990}
4991
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004992#ifdef FEAT_SYN_HL
4993
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004994/*
4995 * Check for a match with \z subexpression "subidx".
4996 * Return TRUE if it matches.
4997 */
4998 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004999match_zref(
5000 int subidx,
5001 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005002{
5003 int len;
5004
5005 cleanup_zsubexpr();
5006 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5007 {
5008 /* backref was not set, match an empty string */
5009 *bytelen = 0;
5010 return TRUE;
5011 }
5012
5013 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005014 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005015 {
5016 *bytelen = len;
5017 return TRUE;
5018 }
5019 return FALSE;
5020}
5021#endif
5022
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005023/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005024 * Save list IDs for all NFA states of "prog" into "list".
5025 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005026 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027 */
5028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005029nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005031 int i;
5032 nfa_state_T *p;
5033
5034 /* Order in the list is reverse, it's a bit faster that way. */
5035 p = &prog->state[0];
5036 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005037 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005038 list[i] = p->lastlist[1];
5039 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005040 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005041 }
5042}
5043
5044/*
5045 * Restore list IDs from "list" to all NFA states.
5046 */
5047 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005048nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005049{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005050 int i;
5051 nfa_state_T *p;
5052
5053 p = &prog->state[0];
5054 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005056 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005057 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005058 }
5059}
5060
Bram Moolenaar423532e2013-05-29 21:14:42 +02005061 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005062nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005063{
5064 if (op == 1) return pos > val;
5065 if (op == 2) return pos < val;
5066 return val == pos;
5067}
5068
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005069static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005070
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005071/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005072 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005073 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5074 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005075 */
5076 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005077recursive_regmatch(
5078 nfa_state_T *state,
5079 nfa_pim_T *pim,
5080 nfa_regprog_T *prog,
5081 regsubs_T *submatch,
5082 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005083 int **listids,
5084 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005085{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005086 int save_reginput_col = (int)(rex.input - rex.line);
5087 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005088 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005089 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005090 save_se_T *save_nfa_endp = nfa_endp;
5091 save_se_T endpos;
5092 save_se_T *endposp = NULL;
5093 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005094 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005095
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005096 if (pim != NULL)
5097 {
5098 /* start at the position where the postponed match was */
5099 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005100 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005101 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005102 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005103 }
5104
Bram Moolenaardecd9542013-06-07 16:31:50 +02005105 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005106 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5107 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5108 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005109 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005110 /* The recursive match must end at the current position. When "pim" is
5111 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005112 endposp = &endpos;
5113 if (REG_MULTI)
5114 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005115 if (pim == NULL)
5116 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005117 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5118 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005119 }
5120 else
5121 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005122 }
5123 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005124 {
5125 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005126 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005127 else
5128 endpos.se_u.ptr = pim->end.ptr;
5129 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005130
5131 /* Go back the specified number of bytes, or as far as the
5132 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005133 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005134 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005135 if (state->val <= 0)
5136 {
5137 if (REG_MULTI)
5138 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005139 rex.line = reg_getline(--rex.lnum);
5140 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005141 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005142 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005143 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005144 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005145 }
5146 else
5147 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005148 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005149 {
5150 /* Not enough bytes in this line, go to end of
5151 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005152 rex.line = reg_getline(--rex.lnum);
5153 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005154 {
5155 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005156 rex.line = reg_getline(++rex.lnum);
5157 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005158 }
5159 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005160 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005161 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005162 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005163 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005164 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005165 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005166 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005167 }
5168 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005169 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005170 }
5171 }
5172
Bram Moolenaarf46da702013-06-02 22:37:42 +02005173#ifdef ENABLE_LOG
5174 if (log_fd != stderr)
5175 fclose(log_fd);
5176 log_fd = NULL;
5177#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005178 /* Have to clear the lastlist field of the NFA nodes, so that
5179 * nfa_regmatch() and addstate() can run properly after recursion. */
5180 if (nfa_ll_index == 1)
5181 {
5182 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5183 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005184 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005185 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005186 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005187 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005188 if (*listids == NULL)
5189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005190 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005191 return 0;
5192 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005193 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005194 }
5195 nfa_save_listids(prog, *listids);
5196 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005197 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005198 }
5199 else
5200 {
5201 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005202 * entry. Make sure rex.nfa_listid is different from a previous
5203 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005204 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005205 if (rex.nfa_listid <= rex.nfa_alt_listid)
5206 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005207 }
5208
5209 /* Call nfa_regmatch() to check if the current concat matches at this
5210 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005211 nfa_endp = endposp;
5212 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005213
5214 if (need_restore)
5215 nfa_restore_listids(prog, *listids);
5216 else
5217 {
5218 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005219 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005220 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005221
5222 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005223 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005224 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005225 rex.line = reg_getline(rex.lnum);
5226 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005227 if (result != NFA_TOO_EXPENSIVE)
5228 {
5229 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005230 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005231 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005232 nfa_endp = save_nfa_endp;
5233
5234#ifdef ENABLE_LOG
5235 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5236 if (log_fd != NULL)
5237 {
5238 fprintf(log_fd, "****************************\n");
5239 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5240 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5241 fprintf(log_fd, "****************************\n");
5242 }
5243 else
5244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005245 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005246 log_fd = stderr;
5247 }
5248#endif
5249
5250 return result;
5251}
5252
Bram Moolenaara2d95102013-06-04 14:23:05 +02005253/*
5254 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005255 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005256 * NFA_ANY: 1
5257 * specific character: 99
5258 */
5259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005260failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005261{
5262 int c = state->c;
5263 int l, r;
5264
5265 /* detect looping */
5266 if (depth > 4)
5267 return 1;
5268
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005269 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005270 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005271 case NFA_SPLIT:
5272 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5273 /* avoid recursive stuff */
5274 return 1;
5275 /* two alternatives, use the lowest failure chance */
5276 l = failure_chance(state->out, depth + 1);
5277 r = failure_chance(state->out1, depth + 1);
5278 return l < r ? l : r;
5279
5280 case NFA_ANY:
5281 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005282 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005283
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005284 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005285 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005286 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005287 /* empty match works always */
5288 return 0;
5289
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005290 case NFA_START_INVISIBLE:
5291 case NFA_START_INVISIBLE_FIRST:
5292 case NFA_START_INVISIBLE_NEG:
5293 case NFA_START_INVISIBLE_NEG_FIRST:
5294 case NFA_START_INVISIBLE_BEFORE:
5295 case NFA_START_INVISIBLE_BEFORE_FIRST:
5296 case NFA_START_INVISIBLE_BEFORE_NEG:
5297 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5298 case NFA_START_PATTERN:
5299 /* recursive regmatch is expensive, use low failure chance */
5300 return 5;
5301
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005302 case NFA_BOL:
5303 case NFA_EOL:
5304 case NFA_BOF:
5305 case NFA_EOF:
5306 case NFA_NEWL:
5307 return 99;
5308
5309 case NFA_BOW:
5310 case NFA_EOW:
5311 return 90;
5312
5313 case NFA_MOPEN:
5314 case NFA_MOPEN1:
5315 case NFA_MOPEN2:
5316 case NFA_MOPEN3:
5317 case NFA_MOPEN4:
5318 case NFA_MOPEN5:
5319 case NFA_MOPEN6:
5320 case NFA_MOPEN7:
5321 case NFA_MOPEN8:
5322 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005323#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005324 case NFA_ZOPEN:
5325 case NFA_ZOPEN1:
5326 case NFA_ZOPEN2:
5327 case NFA_ZOPEN3:
5328 case NFA_ZOPEN4:
5329 case NFA_ZOPEN5:
5330 case NFA_ZOPEN6:
5331 case NFA_ZOPEN7:
5332 case NFA_ZOPEN8:
5333 case NFA_ZOPEN9:
5334 case NFA_ZCLOSE:
5335 case NFA_ZCLOSE1:
5336 case NFA_ZCLOSE2:
5337 case NFA_ZCLOSE3:
5338 case NFA_ZCLOSE4:
5339 case NFA_ZCLOSE5:
5340 case NFA_ZCLOSE6:
5341 case NFA_ZCLOSE7:
5342 case NFA_ZCLOSE8:
5343 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005344#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005345 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005346 case NFA_MCLOSE1:
5347 case NFA_MCLOSE2:
5348 case NFA_MCLOSE3:
5349 case NFA_MCLOSE4:
5350 case NFA_MCLOSE5:
5351 case NFA_MCLOSE6:
5352 case NFA_MCLOSE7:
5353 case NFA_MCLOSE8:
5354 case NFA_MCLOSE9:
5355 case NFA_NCLOSE:
5356 return failure_chance(state->out, depth + 1);
5357
5358 case NFA_BACKREF1:
5359 case NFA_BACKREF2:
5360 case NFA_BACKREF3:
5361 case NFA_BACKREF4:
5362 case NFA_BACKREF5:
5363 case NFA_BACKREF6:
5364 case NFA_BACKREF7:
5365 case NFA_BACKREF8:
5366 case NFA_BACKREF9:
5367#ifdef FEAT_SYN_HL
5368 case NFA_ZREF1:
5369 case NFA_ZREF2:
5370 case NFA_ZREF3:
5371 case NFA_ZREF4:
5372 case NFA_ZREF5:
5373 case NFA_ZREF6:
5374 case NFA_ZREF7:
5375 case NFA_ZREF8:
5376 case NFA_ZREF9:
5377#endif
5378 /* backreferences don't match in many places */
5379 return 94;
5380
5381 case NFA_LNUM_GT:
5382 case NFA_LNUM_LT:
5383 case NFA_COL_GT:
5384 case NFA_COL_LT:
5385 case NFA_VCOL_GT:
5386 case NFA_VCOL_LT:
5387 case NFA_MARK_GT:
5388 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005389 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005390 /* before/after positions don't match very often */
5391 return 85;
5392
5393 case NFA_LNUM:
5394 return 90;
5395
5396 case NFA_CURSOR:
5397 case NFA_COL:
5398 case NFA_VCOL:
5399 case NFA_MARK:
5400 /* specific positions rarely match */
5401 return 98;
5402
5403 case NFA_COMPOSING:
5404 return 95;
5405
5406 default:
5407 if (c > 0)
5408 /* character match fails often */
5409 return 95;
5410 }
5411
5412 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005413 return 50;
5414}
5415
Bram Moolenaarf46da702013-06-02 22:37:42 +02005416/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005417 * Skip until the char "c" we know a match must start with.
5418 */
5419 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005420skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005421{
5422 char_u *s;
5423
5424 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005425 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005426 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005427 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005428 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005429 if (s == NULL)
5430 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005431 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005432 return OK;
5433}
5434
5435/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005436 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005437 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005438 * Returns zero for no match, 1 for a match.
5439 */
5440 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005441find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005442{
5443 colnr_T col = startcol;
5444 int c1, c2;
5445 int len1, len2;
5446 int match;
5447
5448 for (;;)
5449 {
5450 match = TRUE;
5451 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5452 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5453 {
5454 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005455 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005456 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005457 {
5458 match = FALSE;
5459 break;
5460 }
5461 len2 += MB_CHAR2LEN(c2);
5462 }
5463 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005464 /* check that no composing char follows */
5465 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005466 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005467 {
5468 cleanup_subexpr();
5469 if (REG_MULTI)
5470 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005471 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005472 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005473 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005474 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005475 }
5476 else
5477 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005478 rex.reg_startp[0] = rex.line + col;
5479 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005480 }
5481 return 1L;
5482 }
5483
5484 /* Try finding regstart after the current match. */
5485 col += MB_CHAR2LEN(regstart); /* skip regstart */
5486 if (skip_to_start(regstart, &col) == FAIL)
5487 break;
5488 }
5489 return 0L;
5490}
5491
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005492#ifdef FEAT_RELTIME
5493 static int
5494nfa_did_time_out()
5495{
5496 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5497 {
5498 if (nfa_timed_out != NULL)
5499 *nfa_timed_out = TRUE;
5500 return TRUE;
5501 }
5502 return FALSE;
5503}
5504#endif
5505
Bram Moolenaar473de612013-06-08 18:19:48 +02005506/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005507 * Main matching routine.
5508 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005509 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005510 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005511 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005512 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005514 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515 * Note: Caller must ensure that: start != NULL.
5516 */
5517 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005518nfa_regmatch(
5519 nfa_regprog_T *prog,
5520 nfa_state_T *start,
5521 regsubs_T *submatch,
5522 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005525 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005527 int go_to_nextline = FALSE;
5528 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005529 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005530 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005531 nfa_list_T *thislist;
5532 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005534 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005535 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005536 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005537 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005538 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005539 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005540 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005541#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005542 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005544
Bram Moolenaar41f12052013-08-25 17:01:42 +02005545 /* Some patterns may take a long time to match, especially when using
5546 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5547 fast_breakcheck();
5548 if (got_int)
5549 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005550#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005551 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005552 return FALSE;
5553#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005554
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005555#ifdef NFA_REGEXP_DEBUG_LOG
5556 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5557 if (debug == NULL)
5558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005559 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005560 return FALSE;
5561 }
5562#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005563 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005565 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005566 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005567
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005568 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005569 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005570 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005571 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005572 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005573 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574
5575#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005576 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577 if (log_fd != NULL)
5578 {
5579 fprintf(log_fd, "**********************************\n");
5580 nfa_set_code(start->c);
5581 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5582 abs(start->id), code);
5583 fprintf(log_fd, "**********************************\n");
5584 }
5585 else
5586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005588 log_fd = stderr;
5589 }
5590#endif
5591
5592 thislist = &list[0];
5593 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005594 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005595 nextlist = &list[1];
5596 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005597 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005599 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005600#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005601 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005602
5603 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5604 * it's the first MOPEN. */
5605 if (toplevel)
5606 {
5607 if (REG_MULTI)
5608 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005609 m->norm.list.multi[0].start_lnum = rex.lnum;
5610 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005611 }
5612 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005613 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005614 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005615 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005616 }
5617 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005618 r = addstate(thislist, start, m, NULL, 0);
5619 if (r == NULL)
5620 {
5621 nfa_match = NFA_TOO_EXPENSIVE;
5622 goto theend;
5623 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005625#define ADD_STATE_IF_MATCH(state) \
5626 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005627 add_state = state->out; \
5628 add_off = clen; \
5629 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005630
5631 /*
5632 * Run for each character.
5633 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005634 for (;;)
5635 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005636 int curc;
5637 int clen;
5638
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005639 if (has_mbyte)
5640 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005641 curc = (*mb_ptr2char)(rex.input);
5642 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005643 }
5644 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005645 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005646 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005647 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005650 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005651 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005652 go_to_nextline = FALSE;
5653 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005654
5655 /* swap lists */
5656 thislist = &list[flag];
5657 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005658 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005659 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005660 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005661 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005662 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005663# ifdef FEAT_EVAL
5664 || nfa_fail_for_testing
5665# endif
5666 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005667 {
5668 /* too many states, retry with old engine */
5669 nfa_match = NFA_TOO_EXPENSIVE;
5670 goto theend;
5671 }
5672
Bram Moolenaar0270f382018-07-17 05:43:58 +02005673 thislist->id = rex.nfa_listid;
5674 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675
5676#ifdef ENABLE_LOG
5677 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005678 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005679 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005681 {
5682 int i;
5683
5684 for (i = 0; i < thislist->n; i++)
5685 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5686 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005687 fprintf(log_fd, "\n");
5688#endif
5689
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005690#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005691 fprintf(debug, "\n-------------------\n");
5692#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005693 /*
5694 * If the state lists are empty we can stop.
5695 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005696 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005697 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698
5699 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005700 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005702 /* If the list gets very long there probably is something wrong.
5703 * At least allow interrupting with CTRL-C. */
5704 fast_breakcheck();
5705 if (got_int)
5706 break;
5707#ifdef FEAT_RELTIME
5708 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5709 {
5710 nfa_time_count = 0;
5711 if (nfa_did_time_out())
5712 break;
5713 }
5714#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005715 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005717#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005718 nfa_set_code(t->state->c);
5719 fprintf(debug, "%s, ", code);
5720#endif
5721#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005722 {
5723 int col;
5724
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005725 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005726 col = -1;
5727 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005728 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005729 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005730 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005731 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005732 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005733 abs(t->state->id), (int)t->state->c, code, col,
5734 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005735 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005736#endif
5737
5738 /*
5739 * Handle the possible codes of the current state.
5740 * The most important is NFA_MATCH.
5741 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005742 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005743 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005744 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005745 switch (t->state->c)
5746 {
5747 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005748 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005749 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005750 * rex.reg_icombine is not set, that is not really a match. */
5751 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005752 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005753
Bram Moolenaar963fee22013-05-26 21:47:28 +02005754 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005755 copy_sub(&submatch->norm, &t->subs.norm);
5756#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005757 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005758 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005759#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005760#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005761 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005762#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005763 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005764 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005765 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005766 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005767 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005768 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005769 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005770 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005771
5772 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005773 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005774 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005775 /*
5776 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005777 * NFA_START_INVISIBLE_BEFORE node.
5778 * They surround a zero-width group, used with "\@=", "\&",
5779 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005780 * If we got here, it means that the current "invisible" group
5781 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005782 * nfa_regmatch(). For a look-behind match only when it ends
5783 * in the position in "nfa_endp".
5784 * Submatches are stored in *m, and used in the parent call.
5785 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005786#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005787 if (nfa_endp != NULL)
5788 {
5789 if (REG_MULTI)
5790 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005791 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005792 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005793 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005794 nfa_endp->se_u.pos.col);
5795 else
5796 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005797 (int)(rex.input - rex.line),
5798 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005799 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005800#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005801 /* If "nfa_endp" is set it's only a match if it ends at
5802 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005803 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005804 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5805 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005806 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005807 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005808 break;
5809
5810 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005811 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005812 {
5813 copy_sub(&m->norm, &t->subs.norm);
5814#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005815 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005816 copy_sub(&m->synt, &t->subs.synt);
5817#endif
5818 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005819#ifdef ENABLE_LOG
5820 fprintf(log_fd, "Match found:\n");
5821 log_subsexpr(m);
5822#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005823 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005824 /* See comment above at "goto nextchar". */
5825 if (nextlist->n == 0)
5826 clen = 0;
5827 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005828
5829 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005830 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005831 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005832 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005833 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005834 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005835 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005836 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005837 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005838#ifdef ENABLE_LOG
5839 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5840 failure_chance(t->state->out, 0),
5841 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005842#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005843 /* Do it directly if there already is a PIM or when
5844 * nfa_postprocess() detected it will work better. */
5845 if (t->pim.result != NFA_PIM_UNUSED
5846 || t->state->c == NFA_START_INVISIBLE_FIRST
5847 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5848 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5849 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005850 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005851 int in_use = m->norm.in_use;
5852
Bram Moolenaar699c1202013-09-25 16:41:54 +02005853 /* Copy submatch info for the recursive call, opposite
5854 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005855 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005856#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005857 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005858 copy_sub_off(&m->synt, &t->subs.synt);
5859#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005860
Bram Moolenaara2d95102013-06-04 14:23:05 +02005861 /*
5862 * First try matching the invisible match, then what
5863 * follows.
5864 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005865 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005866 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005867 if (result == NFA_TOO_EXPENSIVE)
5868 {
5869 nfa_match = result;
5870 goto theend;
5871 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005872
Bram Moolenaardecd9542013-06-07 16:31:50 +02005873 /* for \@! and \@<! it is a match when the result is
5874 * FALSE */
5875 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005876 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5877 || t->state->c
5878 == NFA_START_INVISIBLE_BEFORE_NEG
5879 || t->state->c
5880 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005881 {
5882 /* Copy submatch info from the recursive call */
5883 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005884#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005885 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005886 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005887#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005888 /* If the pattern has \ze and it matched in the
5889 * sub pattern, use it. */
5890 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005891
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 /* t->state->out1 is the corresponding
5893 * END_INVISIBLE node; Add its out to the current
5894 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005895 add_here = TRUE;
5896 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005897 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005898 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005899 }
5900 else
5901 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005902 nfa_pim_T pim;
5903
Bram Moolenaara2d95102013-06-04 14:23:05 +02005904 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005905 * First try matching what follows. Only if a match
5906 * is found verify the invisible match matches. Add a
5907 * nfa_pim_T to the following states, it contains info
5908 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005909 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005910 pim.state = t->state;
5911 pim.result = NFA_PIM_TODO;
5912 pim.subs.norm.in_use = 0;
5913#ifdef FEAT_SYN_HL
5914 pim.subs.synt.in_use = 0;
5915#endif
5916 if (REG_MULTI)
5917 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005918 pim.end.pos.col = (int)(rex.input - rex.line);
5919 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005920 }
5921 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005922 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005923
5924 /* t->state->out1 is the corresponding END_INVISIBLE
5925 * node; Add its out to the current list (zero-width
5926 * match). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005927 if (addstate_here(thislist, t->state->out1->out,
5928 &t->subs, &pim, &listidx) == NULL)
5929 {
5930 nfa_match = NFA_TOO_EXPENSIVE;
5931 goto theend;
5932 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005933 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005934 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005935 break;
5936
Bram Moolenaar87953742013-06-05 18:52:40 +02005937 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005938 {
5939 nfa_state_T *skip = NULL;
5940#ifdef ENABLE_LOG
5941 int skip_lid = 0;
5942#endif
5943
5944 /* There is no point in trying to match the pattern if the
5945 * output state is not going to be added to the list. */
5946 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5947 {
5948 skip = t->state->out1->out;
5949#ifdef ENABLE_LOG
5950 skip_lid = nextlist->id;
5951#endif
5952 }
5953 else if (state_in_list(nextlist,
5954 t->state->out1->out->out, &t->subs))
5955 {
5956 skip = t->state->out1->out->out;
5957#ifdef ENABLE_LOG
5958 skip_lid = nextlist->id;
5959#endif
5960 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005961 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005962 t->state->out1->out->out, &t->subs))
5963 {
5964 skip = t->state->out1->out->out;
5965#ifdef ENABLE_LOG
5966 skip_lid = thislist->id;
5967#endif
5968 }
5969 if (skip != NULL)
5970 {
5971#ifdef ENABLE_LOG
5972 nfa_set_code(skip->c);
5973 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5974 abs(skip->id), skip_lid, skip->c, code);
5975#endif
5976 break;
5977 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005978 /* Copy submatch info to the recursive call, opposite of what
5979 * happens afterwards. */
5980 copy_sub_off(&m->norm, &t->subs.norm);
5981#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005982 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005983 copy_sub_off(&m->synt, &t->subs.synt);
5984#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005985
Bram Moolenaar87953742013-06-05 18:52:40 +02005986 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005987 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005988 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005989 if (result == NFA_TOO_EXPENSIVE)
5990 {
5991 nfa_match = result;
5992 goto theend;
5993 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005994 if (result)
5995 {
5996 int bytelen;
5997
5998#ifdef ENABLE_LOG
5999 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6000 log_subsexpr(m);
6001#endif
6002 /* Copy submatch info from the recursive call */
6003 copy_sub_off(&t->subs.norm, &m->norm);
6004#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006005 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006006 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006007#endif
6008 /* Now we need to skip over the matched text and then
6009 * continue with what follows. */
6010 if (REG_MULTI)
6011 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006012 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006013 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006014 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006015 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006016
6017#ifdef ENABLE_LOG
6018 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6019#endif
6020 if (bytelen == 0)
6021 {
6022 /* empty match, output of corresponding
6023 * NFA_END_PATTERN/NFA_SKIP to be used at current
6024 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006025 add_here = TRUE;
6026 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006027 }
6028 else if (bytelen <= clen)
6029 {
6030 /* match current character, output of corresponding
6031 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006032 add_state = t->state->out1->out->out;
6033 add_off = clen;
6034 }
6035 else
6036 {
6037 /* skip over the matched characters, set character
6038 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006039 add_state = t->state->out1->out;
6040 add_off = bytelen;
6041 add_count = bytelen - clen;
6042 }
6043 }
6044 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006045 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006046
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006047 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006048 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006049 {
6050 add_here = TRUE;
6051 add_state = t->state->out;
6052 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006053 break;
6054
6055 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006056 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006057 {
6058 add_here = TRUE;
6059 add_state = t->state->out;
6060 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006061 break;
6062
6063 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006064 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006065
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006066 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006067 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006068 else if (has_mbyte)
6069 {
6070 int this_class;
6071
6072 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006073 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006075 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006076 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006077 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006079 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006080 || (rex.input > rex.line
6081 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006082 result = FALSE;
6083 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006084 {
6085 add_here = TRUE;
6086 add_state = t->state->out;
6087 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006088 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006089
6090 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006091 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006092 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006093 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006094 else if (has_mbyte)
6095 {
6096 int this_class, prev_class;
6097
6098 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006099 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006100 prev_class = reg_prev_class();
6101 if (this_class == prev_class
6102 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006103 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006104 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006105 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6106 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006107 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006108 result = FALSE;
6109 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006110 {
6111 add_here = TRUE;
6112 add_state = t->state->out;
6113 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006114 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115
Bram Moolenaar4b780632013-05-31 22:14:52 +02006116 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006117 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006118 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006119 {
6120 add_here = TRUE;
6121 add_state = t->state->out;
6122 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006123 break;
6124
6125 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006126 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006127 {
6128 add_here = TRUE;
6129 add_state = t->state->out;
6130 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006131 break;
6132
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006133 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006134 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006135 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006136 int len = 0;
6137 nfa_state_T *end;
6138 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006139 int cchars[MAX_MCO];
6140 int ccount = 0;
6141 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006142
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006143 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006144 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006145 if (utf_iscomposing(sta->c))
6146 {
6147 /* Only match composing character(s), ignore base
6148 * character. Used for ".{composing}" and "{composing}"
6149 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006150 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006151 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006152 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006153 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006154 /* If \Z was present, then ignore composing characters.
6155 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006156 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006157 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006158 else
6159 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006160 while (sta->c != NFA_END_COMPOSING)
6161 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006162 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006163
6164 /* Check base character matches first, unless ignored. */
6165 else if (len > 0 || mc == sta->c)
6166 {
6167 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006168 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006169 len += mb_char2len(mc);
6170 sta = sta->out;
6171 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006172
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006173 /* We don't care about the order of composing characters.
6174 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006175 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006176 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006177 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006178 cchars[ccount++] = mc;
6179 len += mb_char2len(mc);
6180 if (ccount == MAX_MCO)
6181 break;
6182 }
6183
6184 /* Check that each composing char in the pattern matches a
6185 * composing char in the text. We do not check if all
6186 * composing chars are matched. */
6187 result = OK;
6188 while (sta->c != NFA_END_COMPOSING)
6189 {
6190 for (j = 0; j < ccount; ++j)
6191 if (cchars[j] == sta->c)
6192 break;
6193 if (j == ccount)
6194 {
6195 result = FAIL;
6196 break;
6197 }
6198 sta = sta->out;
6199 }
6200 }
6201 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006202 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006203
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006204 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006205 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006206 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006207 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006208
6209 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006210 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006211 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006212 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006213 go_to_nextline = TRUE;
6214 /* Pass -1 for the offset, which means taking the position
6215 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006216 add_state = t->state->out;
6217 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006218 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006219 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006220 {
6221 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006222 add_state = t->state->out;
6223 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006224 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006225 break;
6226
Bram Moolenaar417bad22013-06-07 14:08:30 +02006227 case NFA_START_COLL:
6228 case NFA_START_NEG_COLL:
6229 {
6230 /* What follows is a list of characters, until NFA_END_COLL.
6231 * One of them must match or none of them must match. */
6232 nfa_state_T *state;
6233 int result_if_matched;
6234 int c1, c2;
6235
6236 /* Never match EOL. If it's part of the collection it is added
6237 * as a separate state with an OR. */
6238 if (curc == NUL)
6239 break;
6240
6241 state = t->state->out;
6242 result_if_matched = (t->state->c == NFA_START_COLL);
6243 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006244 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006245 if (state->c == NFA_END_COLL)
6246 {
6247 result = !result_if_matched;
6248 break;
6249 }
6250 if (state->c == NFA_RANGE_MIN)
6251 {
6252 c1 = state->val;
6253 state = state->out; /* advance to NFA_RANGE_MAX */
6254 c2 = state->val;
6255#ifdef ENABLE_LOG
6256 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6257 curc, c1, c2);
6258#endif
6259 if (curc >= c1 && curc <= c2)
6260 {
6261 result = result_if_matched;
6262 break;
6263 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006264 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006265 {
6266 int curc_low = MB_TOLOWER(curc);
6267 int done = FALSE;
6268
6269 for ( ; c1 <= c2; ++c1)
6270 if (MB_TOLOWER(c1) == curc_low)
6271 {
6272 result = result_if_matched;
6273 done = TRUE;
6274 break;
6275 }
6276 if (done)
6277 break;
6278 }
6279 }
6280 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006281 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006282 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006283 == MB_TOLOWER(state->c))))
6284 {
6285 result = result_if_matched;
6286 break;
6287 }
6288 state = state->out;
6289 }
6290 if (result)
6291 {
6292 /* next state is in out of the NFA_END_COLL, out1 of
6293 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006294 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006295 add_off = clen;
6296 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006297 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006298 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006299
6300 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006301 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006302 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006303 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006304 add_state = t->state->out;
6305 add_off = clen;
6306 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307 break;
6308
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006309 case NFA_ANY_COMPOSING:
6310 /* On a composing character skip over it. Otherwise do
6311 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006312 if (enc_utf8 && utf_iscomposing(curc))
6313 {
6314 add_off = clen;
6315 }
6316 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006317 {
6318 add_here = TRUE;
6319 add_off = 0;
6320 }
6321 add_state = t->state->out;
6322 break;
6323
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006324 /*
6325 * Character classes like \a for alpha, \d for digit etc.
6326 */
6327 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006328 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006329 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006330 break;
6331
6332 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006333 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006334 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006335 break;
6336
6337 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006338 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006339 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006340 break;
6341
6342 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006343 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006344 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006345 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006346 break;
6347
6348 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006349 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006350 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006351 break;
6352
6353 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006354 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006355 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006356 break;
6357
6358 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006359 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006360 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006361 break;
6362
6363 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006364 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006365 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 break;
6367
6368 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006369 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006370 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006371 break;
6372
6373 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006374 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006375 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006376 break;
6377
6378 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006379 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006380 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 break;
6382
6383 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006384 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006385 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 break;
6387
6388 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006389 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006390 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391 break;
6392
6393 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006394 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006395 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396 break;
6397
6398 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006399 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006400 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 break;
6402
6403 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006404 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006405 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006406 break;
6407
6408 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006409 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006410 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006411 break;
6412
6413 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006415 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 break;
6417
6418 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006420 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421 break;
6422
6423 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006425 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006426 break;
6427
6428 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006430 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431 break;
6432
6433 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006435 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006436 break;
6437
6438 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006439 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006440 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006441 break;
6442
6443 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006444 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006445 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006446 break;
6447
6448 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006449 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006450 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006451 break;
6452
6453 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006454 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006455 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006456 break;
6457
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006458 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006459 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006460 ADD_STATE_IF_MATCH(t->state);
6461 break;
6462
6463 case NFA_NLOWER_IC: /* [^a-z] */
6464 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006465 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006466 ADD_STATE_IF_MATCH(t->state);
6467 break;
6468
6469 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006470 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006471 ADD_STATE_IF_MATCH(t->state);
6472 break;
6473
6474 case NFA_NUPPER_IC: /* ^[A-Z] */
6475 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006476 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006477 ADD_STATE_IF_MATCH(t->state);
6478 break;
6479
Bram Moolenaar5714b802013-05-28 22:03:20 +02006480 case NFA_BACKREF1:
6481 case NFA_BACKREF2:
6482 case NFA_BACKREF3:
6483 case NFA_BACKREF4:
6484 case NFA_BACKREF5:
6485 case NFA_BACKREF6:
6486 case NFA_BACKREF7:
6487 case NFA_BACKREF8:
6488 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006489#ifdef FEAT_SYN_HL
6490 case NFA_ZREF1:
6491 case NFA_ZREF2:
6492 case NFA_ZREF3:
6493 case NFA_ZREF4:
6494 case NFA_ZREF5:
6495 case NFA_ZREF6:
6496 case NFA_ZREF7:
6497 case NFA_ZREF8:
6498 case NFA_ZREF9:
6499#endif
6500 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006501 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006502 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006503 int bytelen;
6504
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006505 if (t->state->c <= NFA_BACKREF9)
6506 {
6507 subidx = t->state->c - NFA_BACKREF1 + 1;
6508 result = match_backref(&t->subs.norm, subidx, &bytelen);
6509 }
6510#ifdef FEAT_SYN_HL
6511 else
6512 {
6513 subidx = t->state->c - NFA_ZREF1 + 1;
6514 result = match_zref(subidx, &bytelen);
6515 }
6516#endif
6517
Bram Moolenaar5714b802013-05-28 22:03:20 +02006518 if (result)
6519 {
6520 if (bytelen == 0)
6521 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006522 /* empty match always works, output of NFA_SKIP to be
6523 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006524 add_here = TRUE;
6525 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006526 }
6527 else if (bytelen <= clen)
6528 {
6529 /* match current character, jump ahead to out of
6530 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006531 add_state = t->state->out->out;
6532 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006533 }
6534 else
6535 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006536 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006537 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006538 add_state = t->state->out;
6539 add_off = bytelen;
6540 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006541 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006542 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006543 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006544 }
6545 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006546 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006547 if (t->count - clen <= 0)
6548 {
6549 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006550 add_state = t->state->out;
6551 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006552 }
6553 else
6554 {
6555 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006556 add_state = t->state;
6557 add_off = 0;
6558 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006559 }
6560 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006561
Bram Moolenaar423532e2013-05-29 21:14:42 +02006562 case NFA_LNUM:
6563 case NFA_LNUM_GT:
6564 case NFA_LNUM_LT:
6565 result = (REG_MULTI &&
6566 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006567 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006568 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006569 {
6570 add_here = TRUE;
6571 add_state = t->state->out;
6572 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006573 break;
6574
6575 case NFA_COL:
6576 case NFA_COL_GT:
6577 case NFA_COL_LT:
6578 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006579 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006580 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006581 {
6582 add_here = TRUE;
6583 add_state = t->state->out;
6584 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006585 break;
6586
6587 case NFA_VCOL:
6588 case NFA_VCOL_GT:
6589 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006590 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006591 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006592 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006593 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006594
6595 /* Bail out quickly when there can't be a match, avoid the
6596 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006597 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006598 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006599 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006600 result = FALSE;
6601 if (op == 1 && col - 1 > t->state->val && col > 100)
6602 {
6603 int ts = wp->w_buffer->b_p_ts;
6604
6605 /* Guess that a character won't use more columns than
6606 * 'tabstop', with a minimum of 4. */
6607 if (ts < 4)
6608 ts = 4;
6609 result = col > t->state->val * ts;
6610 }
6611 if (!result)
6612 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006613 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006614 if (result)
6615 {
6616 add_here = TRUE;
6617 add_state = t->state->out;
6618 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006619 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006620 break;
6621
Bram Moolenaar044aa292013-06-04 21:27:38 +02006622 case NFA_MARK:
6623 case NFA_MARK_GT:
6624 case NFA_MARK_LT:
6625 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006626 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006627
6628 /* Compare the mark position to the match position. */
6629 result = (pos != NULL /* mark doesn't exist */
6630 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006631 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6632 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006633 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006634 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006635 ? t->state->c == NFA_MARK_GT
6636 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006637 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006638 ? t->state->c == NFA_MARK_GT
6639 : t->state->c == NFA_MARK_LT)));
6640 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006641 {
6642 add_here = TRUE;
6643 add_state = t->state->out;
6644 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006645 break;
6646 }
6647
Bram Moolenaar423532e2013-05-29 21:14:42 +02006648 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006649 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006650 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006651 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006652 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006653 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006654 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006655 {
6656 add_here = TRUE;
6657 add_state = t->state->out;
6658 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006659 break;
6660
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006661 case NFA_VISUAL:
6662 result = reg_match_visual();
6663 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006664 {
6665 add_here = TRUE;
6666 add_state = t->state->out;
6667 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006668 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006669
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006670 case NFA_MOPEN1:
6671 case NFA_MOPEN2:
6672 case NFA_MOPEN3:
6673 case NFA_MOPEN4:
6674 case NFA_MOPEN5:
6675 case NFA_MOPEN6:
6676 case NFA_MOPEN7:
6677 case NFA_MOPEN8:
6678 case NFA_MOPEN9:
6679#ifdef FEAT_SYN_HL
6680 case NFA_ZOPEN:
6681 case NFA_ZOPEN1:
6682 case NFA_ZOPEN2:
6683 case NFA_ZOPEN3:
6684 case NFA_ZOPEN4:
6685 case NFA_ZOPEN5:
6686 case NFA_ZOPEN6:
6687 case NFA_ZOPEN7:
6688 case NFA_ZOPEN8:
6689 case NFA_ZOPEN9:
6690#endif
6691 case NFA_NOPEN:
6692 case NFA_ZSTART:
6693 /* These states are only added to be able to bail out when
6694 * they are added again, nothing is to be done. */
6695 break;
6696
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006697 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006698 {
6699 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006700
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006701#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006702 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006703 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006704#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006705 result = (c == curc);
6706
Bram Moolenaar6100d022016-10-02 16:51:57 +02006707 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006708 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006709 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006710 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006711 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006712 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006713 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006714 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006715 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006716
6717 } /* switch (t->state->c) */
6718
6719 if (add_state != NULL)
6720 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006721 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006722 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006723
6724 if (t->pim.result == NFA_PIM_UNUSED)
6725 pim = NULL;
6726 else
6727 pim = &t->pim;
6728
6729 /* Handle the postponed invisible match if the match might end
6730 * without advancing and before the end of the line. */
6731 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006732 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006733 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006734 {
6735#ifdef ENABLE_LOG
6736 fprintf(log_fd, "\n");
6737 fprintf(log_fd, "==================================\n");
6738 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6739 fprintf(log_fd, "\n");
6740#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006741 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006742 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006743 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006744 /* for \@! and \@<! it is a match when the result is
6745 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006746 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006747 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6748 || pim->state->c
6749 == NFA_START_INVISIBLE_BEFORE_NEG
6750 || pim->state->c
6751 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006752 {
6753 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006754 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006755#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006756 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006757 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006758#endif
6759 }
6760 }
6761 else
6762 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006763 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006764#ifdef ENABLE_LOG
6765 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006766 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006767 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6768 fprintf(log_fd, "\n");
6769#endif
6770 }
6771
Bram Moolenaardecd9542013-06-07 16:31:50 +02006772 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006773 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006774 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6775 || pim->state->c
6776 == NFA_START_INVISIBLE_BEFORE_NEG
6777 || pim->state->c
6778 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006779 {
6780 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006781 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006782#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006783 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006784 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006785#endif
6786 }
6787 else
6788 /* look-behind match failed, don't add the state */
6789 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006790
6791 /* Postponed invisible match was handled, don't add it to
6792 * following states. */
6793 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006794 }
6795
Bram Moolenaara951e352013-10-06 15:46:11 +02006796 /* If "pim" points into l->t it will become invalid when
6797 * adding the state causes the list to be reallocated. Make a
6798 * local copy to avoid that. */
6799 if (pim == &t->pim)
6800 {
6801 copy_pim(&pim_copy, pim);
6802 pim = &pim_copy;
6803 }
6804
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006805 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006806 r = addstate_here(thislist, add_state, &t->subs,
6807 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006808 else
6809 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006810 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006811 if (add_count > 0)
6812 nextlist->t[nextlist->n - 1].count = add_count;
6813 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006814 if (r == NULL)
6815 {
6816 nfa_match = NFA_TOO_EXPENSIVE;
6817 goto theend;
6818 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006819 }
6820
6821 } /* for (thislist = thislist; thislist->state; thislist++) */
6822
Bram Moolenaare23febd2013-05-26 18:40:14 +02006823 /* Look for the start of a match in the current position by adding the
6824 * start state to the list of states.
6825 * The first found match is the leftmost one, thus the order of states
6826 * matters!
6827 * Do not add the start state in recursive calls of nfa_regmatch(),
6828 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006829 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006830 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006831 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006832 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006833 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006834 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006835 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006836 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006837 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006838 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006839 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6840 || (rex.lnum == nfa_endp->se_u.pos.lnum
6841 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006842 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006843 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006844 {
6845#ifdef ENABLE_LOG
6846 fprintf(log_fd, "(---) STARTSTATE\n");
6847#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006848 /* Inline optimized code for addstate() if we know the state is
6849 * the first MOPEN. */
6850 if (toplevel)
6851 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006852 int add = TRUE;
6853 int c;
6854
6855 if (prog->regstart != NUL && clen != 0)
6856 {
6857 if (nextlist->n == 0)
6858 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006859 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006860
6861 /* Nextlist is empty, we can skip ahead to the
6862 * character that must appear at the start. */
6863 if (skip_to_start(prog->regstart, &col) == FAIL)
6864 break;
6865#ifdef ENABLE_LOG
6866 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006867 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006868#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006869 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006870 }
6871 else
6872 {
6873 /* Checking if the required start character matches is
6874 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006875 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006876 if (c != prog->regstart && (!rex.reg_ic
6877 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006878 {
6879#ifdef ENABLE_LOG
6880 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6881#endif
6882 add = FALSE;
6883 }
6884 }
6885 }
6886
6887 if (add)
6888 {
6889 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006890 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006891 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006892 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006893 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006894 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6895 {
6896 nfa_match = NFA_TOO_EXPENSIVE;
6897 goto theend;
6898 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006899 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006900 }
6901 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006902 {
6903 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6904 {
6905 nfa_match = NFA_TOO_EXPENSIVE;
6906 goto theend;
6907 }
6908 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006909 }
6910
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006911#ifdef ENABLE_LOG
6912 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006913 {
6914 int i;
6915
6916 for (i = 0; i < thislist->n; i++)
6917 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6918 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006919 fprintf(log_fd, "\n");
6920#endif
6921
6922nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006923 /* Advance to the next character, or advance to the next line, or
6924 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006925 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006926 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006927 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006928 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006929 reg_nextline();
6930 else
6931 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006932
6933 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006934 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006935 if (got_int)
6936 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006937#ifdef FEAT_RELTIME
6938 /* Check for timeout once in a twenty times to avoid overhead. */
6939 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6940 {
6941 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006942 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006943 break;
6944 }
6945#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006946 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006947
6948#ifdef ENABLE_LOG
6949 if (log_fd != stderr)
6950 fclose(log_fd);
6951 log_fd = NULL;
6952#endif
6953
6954theend:
6955 /* Free memory */
6956 vim_free(list[0].t);
6957 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006958 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006959#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006960#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006961 fclose(debug);
6962#endif
6963
Bram Moolenaar963fee22013-05-26 21:47:28 +02006964 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006965}
6966
6967/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006968 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006969 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006970 */
6971 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006972nfa_regtry(
6973 nfa_regprog_T *prog,
6974 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006975 proftime_T *tm UNUSED, /* timeout limit or NULL */
6976 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006977{
6978 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006979 regsubs_T subs, m;
6980 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006981 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006982#ifdef ENABLE_LOG
6983 FILE *f;
6984#endif
6985
Bram Moolenaar0270f382018-07-17 05:43:58 +02006986 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006987#ifdef FEAT_RELTIME
6988 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006989 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006990 nfa_time_count = 0;
6991#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992
6993#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006994 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006995 if (f != NULL)
6996 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006997 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006998#ifdef DEBUG
6999 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7000#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007001 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007002 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007003 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007004 fprintf(f, "\n\n");
7005 fclose(f);
7006 }
7007 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007008 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007009#endif
7010
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007011 clear_sub(&subs.norm);
7012 clear_sub(&m.norm);
7013#ifdef FEAT_SYN_HL
7014 clear_sub(&subs.synt);
7015 clear_sub(&m.synt);
7016#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017
Bram Moolenaarfda37292014-11-05 14:27:36 +01007018 result = nfa_regmatch(prog, start, &subs, &m);
7019 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007021 else if (result == NFA_TOO_EXPENSIVE)
7022 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007023
7024 cleanup_subexpr();
7025 if (REG_MULTI)
7026 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007027 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007029 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7030 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007031
Bram Moolenaar6100d022016-10-02 16:51:57 +02007032 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7033 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007034 }
7035
Bram Moolenaar6100d022016-10-02 16:51:57 +02007036 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007037 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007038 rex.reg_startpos[0].lnum = 0;
7039 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007040 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007041 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007043 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007044 rex.reg_endpos[0].lnum = rex.lnum;
7045 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007046 }
7047 else
7048 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007049 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007050 }
7051 else
7052 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007053 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007054 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007055 rex.reg_startp[i] = subs.norm.list.line[i].start;
7056 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007057 }
7058
Bram Moolenaar6100d022016-10-02 16:51:57 +02007059 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007060 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007061 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007062 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007063 }
7064
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007065#ifdef FEAT_SYN_HL
7066 /* Package any found \z(...\) matches for export. Default is none. */
7067 unref_extmatch(re_extmatch_out);
7068 re_extmatch_out = NULL;
7069
7070 if (prog->reghasz == REX_SET)
7071 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007072 cleanup_zsubexpr();
7073 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007074 /* Loop over \z1, \z2, etc. There is no \z0. */
7075 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007076 {
7077 if (REG_MULTI)
7078 {
7079 struct multipos *mpos = &subs.synt.list.multi[i];
7080
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007081 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007082 if (mpos->start_lnum >= 0
7083 && mpos->start_lnum == mpos->end_lnum
7084 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007085 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007086 vim_strnsave(reg_getline(mpos->start_lnum)
7087 + mpos->start_col,
7088 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007089 }
7090 else
7091 {
7092 struct linepos *lpos = &subs.synt.list.line[i];
7093
7094 if (lpos->start != NULL && lpos->end != NULL)
7095 re_extmatch_out->matches[i] =
7096 vim_strnsave(lpos->start,
7097 (int)(lpos->end - lpos->start));
7098 }
7099 }
7100 }
7101#endif
7102
Bram Moolenaar0270f382018-07-17 05:43:58 +02007103 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007104}
7105
7106/*
7107 * Match a regexp against a string ("line" points to the string) or multiple
7108 * lines ("line" is NULL, use reg_getline()).
7109 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007110 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007111 */
7112 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007113nfa_regexec_both(
7114 char_u *line,
7115 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007116 proftime_T *tm, /* timeout limit or NULL */
7117 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007118{
7119 nfa_regprog_T *prog;
7120 long retval = 0L;
7121 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007122 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007123
7124 if (REG_MULTI)
7125 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007126 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007128 rex.reg_startpos = rex.reg_mmatch->startpos;
7129 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007130 }
7131 else
7132 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007133 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7134 rex.reg_startp = rex.reg_match->startp;
7135 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007136 }
7137
7138 /* Be paranoid... */
7139 if (prog == NULL || line == NULL)
7140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007141 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007142 goto theend;
7143 }
7144
Bram Moolenaar6100d022016-10-02 16:51:57 +02007145 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007146 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007147 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007148 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007149 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007150
Bram Moolenaar6100d022016-10-02 16:51:57 +02007151 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007152 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007153 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154
Bram Moolenaar0270f382018-07-17 05:43:58 +02007155 rex.line = line;
7156 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007157
Bram Moolenaar0270f382018-07-17 05:43:58 +02007158 rex.nfa_has_zend = prog->has_zend;
7159 rex.nfa_has_backref = prog->has_backref;
7160 rex.nfa_nsubexpr = prog->nsubexp;
7161 rex.nfa_listid = 1;
7162 rex.nfa_alt_listid = 2;
7163#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007164 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007165#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007166
Bram Moolenaard89616e2013-06-06 18:46:06 +02007167 if (prog->reganch && col > 0)
7168 return 0L;
7169
Bram Moolenaar0270f382018-07-17 05:43:58 +02007170 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007171#ifdef FEAT_SYN_HL
7172 /* Clear the external match subpointers if necessary. */
7173 if (prog->reghasz == REX_SET)
7174 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007175 rex.nfa_has_zsubexpr = TRUE;
7176 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007177 }
7178 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007179 {
7180 rex.nfa_has_zsubexpr = FALSE;
7181 rex.need_clear_zsubexpr = FALSE;
7182 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007183#endif
7184
Bram Moolenaard89616e2013-06-06 18:46:06 +02007185 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007186 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007187 /* Skip ahead until a character we know the match must start with.
7188 * When there is none there is no match. */
7189 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007190 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007191
Bram Moolenaar473de612013-06-08 18:19:48 +02007192 /* If match_text is set it contains the full text that must match.
7193 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007194 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007195 return find_match_text(col, prog->regstart, prog->match_text);
7196 }
7197
Bram Moolenaard89616e2013-06-06 18:46:06 +02007198 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007199 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007200 goto theend;
7201
Bram Moolenaar0270f382018-07-17 05:43:58 +02007202 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7203 // it's accidentally used during execution.
7204 nstate = 0;
7205 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007206 {
7207 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007208 prog->state[i].lastlist[0] = 0;
7209 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007210 }
7211
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007212 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007213
Bram Moolenaar0270f382018-07-17 05:43:58 +02007214#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007215 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007216#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007217
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007218theend:
7219 return retval;
7220}
7221
7222/*
7223 * Compile a regular expression into internal code for the NFA matcher.
7224 * Returns the program in allocated space. Returns NULL for an error.
7225 */
7226 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007227nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007229 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007230 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007231 int *postfix;
7232
7233 if (expr == NULL)
7234 return NULL;
7235
Bram Moolenaar0270f382018-07-17 05:43:58 +02007236#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007237 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007238#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007239 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007240
7241 init_class_tab();
7242
7243 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7244 return NULL;
7245
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007246 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007247 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007248 postfix = re2post();
7249 if (postfix == NULL)
7250 goto fail; /* Cascaded (syntax?) error */
7251
7252 /*
7253 * In order to build the NFA, we parse the input regexp twice:
7254 * 1. first pass to count size (so we can allocate space)
7255 * 2. second to emit code
7256 */
7257#ifdef ENABLE_LOG
7258 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007259 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007260
7261 if (f != NULL)
7262 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007263 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007264 fclose(f);
7265 }
7266 }
7267#endif
7268
7269 /*
7270 * PASS 1
7271 * Count number of NFA states in "nstate". Do not build the NFA.
7272 */
7273 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007274
Bram Moolenaar16619a22013-06-11 18:42:36 +02007275 /* allocate the regprog with space for the compiled regexp */
7276 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007277 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7278 if (prog == NULL)
7279 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007280 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007281 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007282
7283 /*
7284 * PASS 2
7285 * Build the NFA
7286 */
7287 prog->start = post2nfa(postfix, post_ptr, FALSE);
7288 if (prog->start == NULL)
7289 goto fail;
7290
7291 prog->regflags = regflags;
7292 prog->engine = &nfa_regengine;
7293 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007294 prog->has_zend = rex.nfa_has_zend;
7295 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007296 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007297
Bram Moolenaara2947e22013-06-11 22:44:09 +02007298 nfa_postprocess(prog);
7299
Bram Moolenaard89616e2013-06-06 18:46:06 +02007300 prog->reganch = nfa_get_reganch(prog->start, 0);
7301 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007302 prog->match_text = nfa_get_match_text(prog->start);
7303
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007304#ifdef ENABLE_LOG
7305 nfa_postfix_dump(expr, OK);
7306 nfa_dump(prog);
7307#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007308#ifdef FEAT_SYN_HL
7309 /* Remember whether this pattern has any \z specials in it. */
7310 prog->reghasz = re_has_z;
7311#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007312 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007313#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007314 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007315#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007316
7317out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007318 VIM_CLEAR(post_start);
7319 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007320 state_ptr = NULL;
7321 return (regprog_T *)prog;
7322
7323fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007324 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007325#ifdef ENABLE_LOG
7326 nfa_postfix_dump(expr, FAIL);
7327#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007328#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007329 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007330#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007331 goto out;
7332}
7333
Bram Moolenaar473de612013-06-08 18:19:48 +02007334/*
7335 * Free a compiled regexp program, returned by nfa_regcomp().
7336 */
7337 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007338nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007339{
7340 if (prog != NULL)
7341 {
7342 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007343 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007344 vim_free(prog);
7345 }
7346}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007347
7348/*
7349 * Match a regexp against a string.
7350 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7351 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007352 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007353 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007354 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007355 */
7356 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007357nfa_regexec_nl(
7358 regmatch_T *rmp,
7359 char_u *line, /* string to match against */
7360 colnr_T col, /* column to start looking for match */
7361 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007362{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007363 rex.reg_match = rmp;
7364 rex.reg_mmatch = NULL;
7365 rex.reg_maxline = 0;
7366 rex.reg_line_lbr = line_lbr;
7367 rex.reg_buf = curbuf;
7368 rex.reg_win = NULL;
7369 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007370 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007371 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007372 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007373}
7374
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007375
7376/*
7377 * Match a regexp against multiple lines.
7378 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7379 * Uses curbuf for line count and 'iskeyword'.
7380 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007381 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007382 * match otherwise.
7383 *
7384 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7385 *
7386 * ! Also NOTE : match may actually be in another line. e.g.:
7387 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7388 *
7389 * +-------------------------+
7390 * |a |
7391 * |b |
7392 * |c |
7393 * | |
7394 * +-------------------------+
7395 *
7396 * then nfa_regexec_multi() returns 3. while the original
7397 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7398 *
7399 * FIXME if this behavior is not compatible.
7400 */
7401 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007402nfa_regexec_multi(
7403 regmmatch_T *rmp,
7404 win_T *win, /* window in which to search or NULL */
7405 buf_T *buf, /* buffer in which to search */
7406 linenr_T lnum, /* nr of line to start looking for match */
7407 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007408 proftime_T *tm, /* timeout limit or NULL */
7409 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007410{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007411 rex.reg_match = NULL;
7412 rex.reg_mmatch = rmp;
7413 rex.reg_buf = buf;
7414 rex.reg_win = win;
7415 rex.reg_firstlnum = lnum;
7416 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7417 rex.reg_line_lbr = FALSE;
7418 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007419 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007420 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007421
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007422 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007423}
7424
7425#ifdef DEBUG
7426# undef ENABLE_LOG
7427#endif