blob: 3e2ef93fa1dbd3556171b28e2a9bafc90407bde5 [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 Moolenaar174a8482013-11-28 14:20:17 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
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 Moolenaar16299b52013-05-30 18:45:23 +0200512 int new_max = nstate_max + 1000;
513 int *new_start;
514 int *old_start;
515
516 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
517 if (new_start == NULL)
518 return FAIL;
519 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200520 old_start = post_start;
521 post_start = new_start;
522 post_ptr = new_start + (post_ptr - old_start);
523 post_end = post_start + new_max;
524 vim_free(old_start);
525 return OK;
526}
527
528/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200529 * Search between "start" and "end" and try to recognize a
530 * character class in expanded form. For example [0-9].
531 * On success, return the id the character class to be emitted.
532 * On failure, return 0 (=FAIL)
533 * Start points to the first char of the range, while end should point
534 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200535 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
536 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200537 */
538 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100539nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200541# define CLASS_not 0x80
542# define CLASS_af 0x40
543# define CLASS_AF 0x20
544# define CLASS_az 0x10
545# define CLASS_AZ 0x08
546# define CLASS_o7 0x04
547# define CLASS_o9 0x02
548# define CLASS_underscore 0x01
549
550 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200551 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200552 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200553
554 if (extra_newl == TRUE)
555 newl = TRUE;
556
557 if (*end != ']')
558 return FAIL;
559 p = start;
560 if (*p == '^')
561 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200562 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200563 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200564 }
565
566 while (p < end)
567 {
568 if (p + 2 < end && *(p + 1) == '-')
569 {
570 switch (*p)
571 {
572 case '0':
573 if (*(p + 2) == '9')
574 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200575 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200576 break;
577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200578 if (*(p + 2) == '7')
579 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200580 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200581 break;
582 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200583 return FAIL;
584
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200585 case 'a':
586 if (*(p + 2) == 'z')
587 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200588 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200589 break;
590 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200591 if (*(p + 2) == 'f')
592 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200593 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200594 break;
595 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200596 return FAIL;
597
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598 case 'A':
599 if (*(p + 2) == 'Z')
600 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200601 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200602 break;
603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 if (*(p + 2) == 'F')
605 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200606 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 break;
608 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200609 return FAIL;
610
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 default:
612 return FAIL;
613 }
614 p += 3;
615 }
616 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
617 {
618 newl = TRUE;
619 p += 2;
620 }
621 else if (*p == '_')
622 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200623 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200624 p ++;
625 }
626 else if (*p == '\n')
627 {
628 newl = TRUE;
629 p ++;
630 }
631 else
632 return FAIL;
633 } /* while (p < end) */
634
635 if (p != end)
636 return FAIL;
637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200639 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640
641 switch (config)
642 {
643 case CLASS_o9:
644 return extra_newl + NFA_DIGIT;
645 case CLASS_not | CLASS_o9:
646 return extra_newl + NFA_NDIGIT;
647 case CLASS_af | CLASS_AF | CLASS_o9:
648 return extra_newl + NFA_HEX;
649 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
650 return extra_newl + NFA_NHEX;
651 case CLASS_o7:
652 return extra_newl + NFA_OCTAL;
653 case CLASS_not | CLASS_o7:
654 return extra_newl + NFA_NOCTAL;
655 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
656 return extra_newl + NFA_WORD;
657 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
658 return extra_newl + NFA_NWORD;
659 case CLASS_az | CLASS_AZ | CLASS_underscore:
660 return extra_newl + NFA_HEAD;
661 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
662 return extra_newl + NFA_NHEAD;
663 case CLASS_az | CLASS_AZ:
664 return extra_newl + NFA_ALPHA;
665 case CLASS_not | CLASS_az | CLASS_AZ:
666 return extra_newl + NFA_NALPHA;
667 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200668 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200669 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200670 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200671 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200672 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200673 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200674 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200676 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677}
678
679/*
680 * Produce the bytes for equivalence class "c".
681 * Currently only handles latin1, latin9 and utf-8.
682 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
683 * equivalent to 'a OR b OR c'
684 *
685 * NOTE! When changing this function, also update reg_equi_class()
686 */
687 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100688nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200690#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100691#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200693 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
694 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200696#ifdef EBCDIC
697# define A_circumflex 0x62
698# define A_diaeresis 0x63
699# define A_grave 0x64
700# define A_acute 0x65
701# define A_virguilla 0x66
702# define A_ring 0x67
703# define C_cedilla 0x68
704# define E_acute 0x71
705# define E_circumflex 0x72
706# define E_diaeresis 0x73
707# define E_grave 0x74
708# define I_acute 0x75
709# define I_circumflex 0x76
710# define I_diaeresis 0x77
711# define I_grave 0x78
712# define N_virguilla 0x69
713# define O_circumflex 0xeb
714# define O_diaeresis 0xec
715# define O_grave 0xed
716# define O_acute 0xee
717# define O_virguilla 0xef
718# define O_slash 0x80
719# define U_circumflex 0xfb
720# define U_diaeresis 0xfc
721# define U_grave 0xfd
722# define U_acute 0xfe
723# define Y_acute 0xba
724# define a_grave 0x42
725# define a_acute 0x43
726# define a_circumflex 0x44
727# define a_virguilla 0x45
728# define a_diaeresis 0x46
729# define a_ring 0x47
730# define c_cedilla 0x48
731# define e_grave 0x51
732# define e_acute 0x52
733# define e_circumflex 0x53
734# define e_diaeresis 0x54
735# define i_grave 0x55
736# define i_acute 0x56
737# define i_circumflex 0x57
738# define i_diaeresis 0x58
739# define n_virguilla 0x49
740# define o_grave 0xcb
741# define o_acute 0xcc
742# define o_circumflex 0xcd
743# define o_virguilla 0xce
744# define o_diaeresis 0xcf
745# define o_slash 0x70
746# define u_grave 0xdb
747# define u_acute 0xdc
748# define u_circumflex 0xdd
749# define u_diaeresis 0xde
750# define y_acute 0x8d
751# define y_diaeresis 0xdf
752#else
753# define A_grave 0xc0
754# define A_acute 0xc1
755# define A_circumflex 0xc2
756# define A_virguilla 0xc3
757# define A_diaeresis 0xc4
758# define A_ring 0xc5
759# define C_cedilla 0xc7
760# define E_grave 0xc8
761# define E_acute 0xc9
762# define E_circumflex 0xca
763# define E_diaeresis 0xcb
764# define I_grave 0xcc
765# define I_acute 0xcd
766# define I_circumflex 0xce
767# define I_diaeresis 0xcf
768# define N_virguilla 0xd1
769# define O_grave 0xd2
770# define O_acute 0xd3
771# define O_circumflex 0xd4
772# define O_virguilla 0xd5
773# define O_diaeresis 0xd6
774# define O_slash 0xd8
775# define U_grave 0xd9
776# define U_acute 0xda
777# define U_circumflex 0xdb
778# define U_diaeresis 0xdc
779# define Y_acute 0xdd
780# define a_grave 0xe0
781# define a_acute 0xe1
782# define a_circumflex 0xe2
783# define a_virguilla 0xe3
784# define a_diaeresis 0xe4
785# define a_ring 0xe5
786# define c_cedilla 0xe7
787# define e_grave 0xe8
788# define e_acute 0xe9
789# define e_circumflex 0xea
790# define e_diaeresis 0xeb
791# define i_grave 0xec
792# define i_acute 0xed
793# define i_circumflex 0xee
794# define i_diaeresis 0xef
795# define n_virguilla 0xf1
796# define o_grave 0xf2
797# define o_acute 0xf3
798# define o_circumflex 0xf4
799# define o_virguilla 0xf5
800# define o_diaeresis 0xf6
801# define o_slash 0xf8
802# define u_grave 0xf9
803# define u_acute 0xfa
804# define u_circumflex 0xfb
805# define u_diaeresis 0xfc
806# define y_acute 0xfd
807# define y_diaeresis 0xff
808#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200809 switch (c)
810 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200811 case 'A': case A_grave: case A_acute: case A_circumflex:
812 case A_virguilla: case A_diaeresis: case A_ring:
813 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
814 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
815 CASEMBC(0x1ea2)
816 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
817 EMIT2(A_circumflex); EMIT2(A_virguilla);
818 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200819 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
820 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
821 EMITMBC(0x1ea2)
822 return OK;
823
824 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
825 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200826 return OK;
827
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200828 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
829 CASEMBC(0x10a) CASEMBC(0x10c)
830 EMIT2('C'); EMIT2(C_cedilla);
831 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200832 EMITMBC(0x10a) EMITMBC(0x10c)
833 return OK;
834
835 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200836 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200837 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
838 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200839 return OK;
840
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200841 case 'E': case E_grave: case E_acute: case E_circumflex:
842 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
843 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
844 CASEMBC(0x1eba) CASEMBC(0x1ebc)
845 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
846 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200847 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
848 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
849 EMITMBC(0x1ebc)
850 return OK;
851
852 case 'F': CASEMBC(0x1e1e)
853 EMIT2('F'); EMITMBC(0x1e1e)
854 return OK;
855
856 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200857 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
858 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200859 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
860 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
861 EMITMBC(0x1f4) EMITMBC(0x1e20)
862 return OK;
863
864 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200865 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200866 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
867 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 return OK;
869
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'I': case I_grave: case I_acute: case I_circumflex:
871 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
872 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
873 CASEMBC(0x1cf) CASEMBC(0x1ec8)
874 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
875 EMIT2(I_circumflex); EMIT2(I_diaeresis);
876 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200877 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
878 EMITMBC(0x1cf) EMITMBC(0x1ec8)
879 return OK;
880
881 case 'J': CASEMBC(0x134)
882 EMIT2('J'); EMITMBC(0x134)
883 return OK;
884
885 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200886 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200887 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
888 EMITMBC(0x1e34)
889 return OK;
890
891 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200892 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200893 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
894 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
895 return OK;
896
897 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
898 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200899 return OK;
900
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200901 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
902 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
903 EMIT2('N'); EMIT2(N_virguilla);
904 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200905 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 return OK;
907
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200908 case 'O': case O_grave: case O_acute: case O_circumflex:
909 case O_virguilla: case O_diaeresis: case O_slash:
910 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
911 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
912 CASEMBC(0x1ec) CASEMBC(0x1ece)
913 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
914 EMIT2(O_circumflex); EMIT2(O_virguilla);
915 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
917 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
918 EMITMBC(0x1ec) EMITMBC(0x1ece)
919 return OK;
920
921 case 'P': case 0x1e54: case 0x1e56:
922 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
923 return OK;
924
925 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200926 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200927 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
928 EMITMBC(0x1e58) EMITMBC(0x1e5e)
929 return OK;
930
931 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200932 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200933 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
934 EMITMBC(0x160) EMITMBC(0x1e60)
935 return OK;
936
937 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200938 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200939 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
940 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200941 return OK;
942
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200943 case 'U': case U_grave: case U_acute: case U_diaeresis:
944 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
945 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
946 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
947 CASEMBC(0x1ee6)
948 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
949 EMIT2(U_diaeresis); EMIT2(U_circumflex);
950 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200951 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
952 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
953 EMITMBC(0x1ee6)
954 return OK;
955
956 case 'V': CASEMBC(0x1e7c)
957 EMIT2('V'); EMITMBC(0x1e7c)
958 return OK;
959
960 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200961 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200962 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
963 EMITMBC(0x1e84) EMITMBC(0x1e86)
964 return OK;
965
966 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
967 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200968 return OK;
969
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200970 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
971 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
972 CASEMBC(0x1ef8)
973 EMIT2('Y'); EMIT2(Y_acute);
974 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200975 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
976 EMITMBC(0x1ef8)
977 return OK;
978
979 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200980 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200981 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
982 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200983 return OK;
984
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 case 'a': case a_grave: case a_acute: case a_circumflex:
986 case a_virguilla: case a_diaeresis: case a_ring:
987 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
988 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
989 CASEMBC(0x1ea3)
990 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
991 EMIT2(a_circumflex); EMIT2(a_virguilla);
992 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200993 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
994 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
995 EMITMBC(0x1ea3)
996 return OK;
997
998 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
999 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return OK;
1001
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001002 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1003 CASEMBC(0x10b) CASEMBC(0x10d)
1004 EMIT2('c'); EMIT2(c_cedilla);
1005 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001006 EMITMBC(0x10b) EMITMBC(0x10d)
1007 return OK;
1008
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001009 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001010 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001011 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1012 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001013 return OK;
1014
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001015 case 'e': case e_grave: case e_acute: case e_circumflex:
1016 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1017 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1018 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1019 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1020 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1021 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001022 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1023 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1024 return OK;
1025
1026 case 'f': CASEMBC(0x1e1f)
1027 EMIT2('f'); EMITMBC(0x1e1f)
1028 return OK;
1029
1030 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001031 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1032 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001033 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1034 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1035 EMITMBC(0x1f5) EMITMBC(0x1e21)
1036 return OK;
1037
1038 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001040 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1041 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'i': case i_grave: case i_acute: case i_circumflex:
1045 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1046 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1047 CASEMBC(0x1ec9)
1048 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1049 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1050 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001051 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1052 EMITMBC(0x1ec9)
1053 return OK;
1054
1055 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1056 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1057 return OK;
1058
1059 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001060 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001061 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1062 EMITMBC(0x1e35)
1063 return OK;
1064
1065 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001066 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001067 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1068 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1069 return OK;
1070
1071 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1072 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001073 return OK;
1074
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001075 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1076 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1077 CASEMBC(0x1e49)
1078 EMIT2('n'); EMIT2(n_virguilla);
1079 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001080 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1081 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001082 return OK;
1083
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001084 case 'o': case o_grave: case o_acute: case o_circumflex:
1085 case o_virguilla: case o_diaeresis: case o_slash:
1086 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1087 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1088 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1089 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1090 EMIT2(o_circumflex); EMIT2(o_virguilla);
1091 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1093 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1094 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1095 return OK;
1096
1097 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1098 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1099 return OK;
1100
1101 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001102 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001103 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1104 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1105 return OK;
1106
1107 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001108 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001109 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1110 EMITMBC(0x161) EMITMBC(0x1e61)
1111 return OK;
1112
1113 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001114 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001115 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1116 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001117 return OK;
1118
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 case 'u': case u_grave: case u_acute: case u_circumflex:
1120 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1121 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1122 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1123 CASEMBC(0x1ee7)
1124 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1125 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1126 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001127 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1128 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1129 EMITMBC(0x1ee7)
1130 return OK;
1131
1132 case 'v': CASEMBC(0x1e7d)
1133 EMIT2('v'); EMITMBC(0x1e7d)
1134 return OK;
1135
1136 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001137 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001138 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1139 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1140 return OK;
1141
1142 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1143 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001144 return OK;
1145
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001146 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1147 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1148 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1149 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1150 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001151 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1152 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001153 return OK;
1154
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001155 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001156 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001157 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1158 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1159 return OK;
1160
1161 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 }
1163 }
1164
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001165 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166 return OK;
1167#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169}
1170
1171/*
1172 * Code to parse regular expression.
1173 *
1174 * We try to reuse parsing functions in regexp.c to
1175 * minimize surprise and keep the syntax consistent.
1176 */
1177
1178/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001179 * Parse the lowest level.
1180 *
1181 * An atom can be one of a long list of items. Many atoms match one character
1182 * in the text. It is often an ordinary character or a character class.
1183 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1184 * is only for syntax highlighting.
1185 *
1186 * atom ::= ordinary-atom
1187 * or \( pattern \)
1188 * or \%( pattern \)
1189 * or \z( pattern \)
1190 */
1191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001192nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001193{
1194 int c;
1195 int charclass;
1196 int equiclass;
1197 int collclass;
1198 int got_coll_char;
1199 char_u *p;
1200 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001202 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 int emit_range;
1204 int negated;
1205 int result;
1206 int startc = -1;
1207 int endc = -1;
1208 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001209 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 switch (c)
1213 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001214 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001215 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001216
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217 case Magic('^'):
1218 EMIT(NFA_BOL);
1219 break;
1220
1221 case Magic('$'):
1222 EMIT(NFA_EOL);
1223#if defined(FEAT_SYN_HL) || defined(PROTO)
1224 had_eol = TRUE;
1225#endif
1226 break;
1227
1228 case Magic('<'):
1229 EMIT(NFA_BOW);
1230 break;
1231
1232 case Magic('>'):
1233 EMIT(NFA_EOW);
1234 break;
1235
1236 case Magic('_'):
1237 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001238 if (c == NUL)
1239 EMSG_RET_FAIL(_(e_nul_found));
1240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 if (c == '^') /* "\_^" is start-of-line */
1242 {
1243 EMIT(NFA_BOL);
1244 break;
1245 }
1246 if (c == '$') /* "\_$" is end-of-line */
1247 {
1248 EMIT(NFA_EOL);
1249#if defined(FEAT_SYN_HL) || defined(PROTO)
1250 had_eol = TRUE;
1251#endif
1252 break;
1253 }
1254
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001255 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256
1257 /* "\_[" is collection plus newline */
1258 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001259 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260
1261 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001262 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
1264 /*
1265 * Character classes.
1266 */
1267 case Magic('.'):
1268 case Magic('i'):
1269 case Magic('I'):
1270 case Magic('k'):
1271 case Magic('K'):
1272 case Magic('f'):
1273 case Magic('F'):
1274 case Magic('p'):
1275 case Magic('P'):
1276 case Magic('s'):
1277 case Magic('S'):
1278 case Magic('d'):
1279 case Magic('D'):
1280 case Magic('x'):
1281 case Magic('X'):
1282 case Magic('o'):
1283 case Magic('O'):
1284 case Magic('w'):
1285 case Magic('W'):
1286 case Magic('h'):
1287 case Magic('H'):
1288 case Magic('a'):
1289 case Magic('A'):
1290 case Magic('l'):
1291 case Magic('L'):
1292 case Magic('u'):
1293 case Magic('U'):
1294 p = vim_strchr(classchars, no_Magic(c));
1295 if (p == NULL)
1296 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001297 if (extra == NFA_ADD_NL)
1298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001300 rc_did_emsg = TRUE;
1301 return FAIL;
1302 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001303 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001304 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001306
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307 /* When '.' is followed by a composing char ignore the dot, so that
1308 * the composing char is matched here. */
1309 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1310 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001311 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312 c = getchr();
1313 goto nfa_do_multibyte;
1314 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001316 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317 {
1318 EMIT(NFA_NEWL);
1319 EMIT(NFA_OR);
1320 regflags |= RF_HASNL;
1321 }
1322 break;
1323
1324 case Magic('n'):
1325 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001326 /* In a string "\n" matches a newline character. */
1327 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328 else
1329 {
1330 /* In buffer text "\n" matches the end of a line. */
1331 EMIT(NFA_NEWL);
1332 regflags |= RF_HASNL;
1333 }
1334 break;
1335
1336 case Magic('('):
1337 if (nfa_reg(REG_PAREN) == FAIL)
1338 return FAIL; /* cascaded error */
1339 break;
1340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001341 case Magic('|'):
1342 case Magic('&'):
1343 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001344 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 return FAIL;
1346
1347 case Magic('='):
1348 case Magic('?'):
1349 case Magic('+'):
1350 case Magic('@'):
1351 case Magic('*'):
1352 case Magic('{'):
1353 /* these should follow an atom, not form an atom */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001354 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355 return FAIL;
1356
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001357 case Magic('~'):
1358 {
1359 char_u *lp;
1360
1361 /* Previous substitute pattern.
1362 * Generated as "\%(pattern\)". */
1363 if (reg_prev_sub == NULL)
1364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001365 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001366 return FAIL;
1367 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001368 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001369 {
1370 EMIT(PTR2CHAR(lp));
1371 if (lp != reg_prev_sub)
1372 EMIT(NFA_CONCAT);
1373 }
1374 EMIT(NFA_NOPEN);
1375 break;
1376 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377
Bram Moolenaar428e9872013-05-30 17:05:39 +02001378 case Magic('1'):
1379 case Magic('2'):
1380 case Magic('3'):
1381 case Magic('4'):
1382 case Magic('5'):
1383 case Magic('6'):
1384 case Magic('7'):
1385 case Magic('8'):
1386 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001387 {
1388 int refnum = no_Magic(c) - '1';
1389
1390 if (!seen_endbrace(refnum + 1))
1391 return FAIL;
1392 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001393 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001394 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001395 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001396
1397 case Magic('z'):
1398 c = no_Magic(getchr());
1399 switch (c)
1400 {
1401 case 's':
1402 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001403 if (re_mult_next("\\zs") == FAIL)
1404 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 break;
1406 case 'e':
1407 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001408 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001409 if (re_mult_next("\\ze") == FAIL)
1410 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001411 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001412#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 case '1':
1414 case '2':
1415 case '3':
1416 case '4':
1417 case '5':
1418 case '6':
1419 case '7':
1420 case '8':
1421 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001422 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001423 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001424 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001425 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar0270f382018-07-17 05:43:58 +02001426 /* No need to set rex.nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001427 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001428 re_has_z = REX_USE;
1429 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001430 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001431 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001432 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001433 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001434 if (nfa_reg(REG_ZPAREN) == FAIL)
1435 return FAIL; /* cascaded error */
1436 re_has_z = REX_SET;
1437 break;
1438#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001440 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441 no_Magic(c));
1442 return FAIL;
1443 }
1444 break;
1445
1446 case Magic('%'):
1447 c = no_Magic(getchr());
1448 switch (c)
1449 {
1450 /* () without a back reference */
1451 case '(':
1452 if (nfa_reg(REG_NPAREN) == FAIL)
1453 return FAIL;
1454 EMIT(NFA_NOPEN);
1455 break;
1456
1457 case 'd': /* %d123 decimal */
1458 case 'o': /* %o123 octal */
1459 case 'x': /* %xab hex 2 */
1460 case 'u': /* %uabcd hex 4 */
1461 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001462 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001463 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464
Bram Moolenaar47196582013-05-25 22:04:23 +02001465 switch (c)
1466 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001467 case 'd': nr = getdecchrs(); break;
1468 case 'o': nr = getoctchrs(); break;
1469 case 'x': nr = gethexchrs(2); break;
1470 case 'u': nr = gethexchrs(4); break;
1471 case 'U': nr = gethexchrs(8); break;
1472 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001473 }
1474
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001475 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001476 EMSG2_RET_FAIL(
1477 _("E678: Invalid character after %s%%[dxouU]"),
1478 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001479 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001480 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001481 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001482 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 break;
1484
1485 /* Catch \%^ and \%$ regardless of where they appear in the
1486 * pattern -- regardless of whether or not it makes sense. */
1487 case '^':
1488 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001489 break;
1490
1491 case '$':
1492 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 break;
1494
1495 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001496 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 break;
1498
1499 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001500 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 break;
1502
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001503 case 'C':
1504 EMIT(NFA_ANY_COMPOSING);
1505 break;
1506
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001507 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001508 {
1509 int n;
1510
1511 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001512 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001513 {
1514 if (c == NUL)
1515 EMSG2_RET_FAIL(_(e_missing_sb),
1516 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001517 /* recursive call! */
1518 if (nfa_regatom() == FAIL)
1519 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001520 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001521 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001522 if (n == 0)
1523 EMSG2_RET_FAIL(_(e_empty_sb),
1524 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001525 EMIT(NFA_OPT_CHARS);
1526 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001527
1528 /* Emit as "\%(\%[abc]\)" to be able to handle
1529 * "\%[abc]*" which would cause the empty string to be
1530 * matched an unlimited number of times. NFA_NOPEN is
1531 * added only once at a position, while NFA_SPLIT is
1532 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001533 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001534 * a lot. */
1535 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001536 break;
1537 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001538
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001540 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001541 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001542 int cmp = c;
1543
1544 if (c == '<' || c == '>')
1545 c = getchr();
1546 while (VIM_ISDIGIT(c))
1547 {
1548 n = n * 10 + (c - '0');
1549 c = getchr();
1550 }
1551 if (c == 'l' || c == 'c' || c == 'v')
1552 {
Bram Moolenaar9403a212019-02-13 18:35:06 +01001553 int limit = INT_MAX;
1554
Bram Moolenaar423532e2013-05-29 21:14:42 +02001555 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001556 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001557 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001559 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001560 if (save_prev_at_start)
1561 at_start = TRUE;
1562 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001563 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001564 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001565 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001566 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001567 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001568 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001569 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001570 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001571 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001572 limit = INT_MAX / MB_MAXBYTES;
1573 }
1574 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001576 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001577 return FAIL;
1578 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001579 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001580 break;
1581 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001582 else if (c == '\'' && n == 0)
1583 {
1584 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001585 EMIT(cmp == '<' ? NFA_MARK_LT :
1586 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001587 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001588 break;
1589 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001590 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001591 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001592 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 return FAIL;
1594 }
1595 break;
1596
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001597 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001598collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001600 * [abc] uses NFA_START_COLL - NFA_END_COLL
1601 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1602 * Each character is produced as a regular state, using
1603 * NFA_CONCAT to bind them together.
1604 * Besides normal characters there can be:
1605 * - character classes NFA_CLASS_*
1606 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001607 */
1608
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001609 p = regparse;
1610 endp = skip_anyof(p);
1611 if (*endp == ']')
1612 {
1613 /*
1614 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001615 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001616 * and perform the necessary substitutions in the NFA.
1617 */
1618 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001619 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001620 if (result != FAIL)
1621 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001622 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001624 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001625 EMIT(NFA_NEWL);
1626 EMIT(NFA_OR);
1627 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001628 else
1629 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001630 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001631 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001632 return OK;
1633 }
1634 /*
1635 * Failed to recognize a character class. Use the simple
1636 * version that turns [abc] into 'a' OR 'b' OR 'c'
1637 */
1638 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001640 if (*regparse == '^') /* negated range */
1641 {
1642 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001643 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001644 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001646 else
1647 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001648 if (*regparse == '-')
1649 {
1650 startc = '-';
1651 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001652 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001653 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001654 }
1655 /* Emit the OR branches for each character in the [] */
1656 emit_range = FALSE;
1657 while (regparse < endp)
1658 {
1659 oldstartc = startc;
1660 startc = -1;
1661 got_coll_char = FALSE;
1662 if (*regparse == '[')
1663 {
1664 /* Check for [: :], [= =], [. .] */
1665 equiclass = collclass = 0;
1666 charclass = get_char_class(&regparse);
1667 if (charclass == CLASS_NONE)
1668 {
1669 equiclass = get_equi_class(&regparse);
1670 if (equiclass == 0)
1671 collclass = get_coll_element(&regparse);
1672 }
1673
1674 /* Character class like [:alpha:] */
1675 if (charclass != CLASS_NONE)
1676 {
1677 switch (charclass)
1678 {
1679 case CLASS_ALNUM:
1680 EMIT(NFA_CLASS_ALNUM);
1681 break;
1682 case CLASS_ALPHA:
1683 EMIT(NFA_CLASS_ALPHA);
1684 break;
1685 case CLASS_BLANK:
1686 EMIT(NFA_CLASS_BLANK);
1687 break;
1688 case CLASS_CNTRL:
1689 EMIT(NFA_CLASS_CNTRL);
1690 break;
1691 case CLASS_DIGIT:
1692 EMIT(NFA_CLASS_DIGIT);
1693 break;
1694 case CLASS_GRAPH:
1695 EMIT(NFA_CLASS_GRAPH);
1696 break;
1697 case CLASS_LOWER:
1698 EMIT(NFA_CLASS_LOWER);
1699 break;
1700 case CLASS_PRINT:
1701 EMIT(NFA_CLASS_PRINT);
1702 break;
1703 case CLASS_PUNCT:
1704 EMIT(NFA_CLASS_PUNCT);
1705 break;
1706 case CLASS_SPACE:
1707 EMIT(NFA_CLASS_SPACE);
1708 break;
1709 case CLASS_UPPER:
1710 EMIT(NFA_CLASS_UPPER);
1711 break;
1712 case CLASS_XDIGIT:
1713 EMIT(NFA_CLASS_XDIGIT);
1714 break;
1715 case CLASS_TAB:
1716 EMIT(NFA_CLASS_TAB);
1717 break;
1718 case CLASS_RETURN:
1719 EMIT(NFA_CLASS_RETURN);
1720 break;
1721 case CLASS_BACKSPACE:
1722 EMIT(NFA_CLASS_BACKSPACE);
1723 break;
1724 case CLASS_ESCAPE:
1725 EMIT(NFA_CLASS_ESCAPE);
1726 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001727 case CLASS_IDENT:
1728 EMIT(NFA_CLASS_IDENT);
1729 break;
1730 case CLASS_KEYWORD:
1731 EMIT(NFA_CLASS_KEYWORD);
1732 break;
1733 case CLASS_FNAME:
1734 EMIT(NFA_CLASS_FNAME);
1735 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001736 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001737 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 continue;
1739 }
1740 /* Try equivalence class [=a=] and the like */
1741 if (equiclass != 0)
1742 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001743 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 if (result == FAIL)
1745 {
1746 /* should never happen */
1747 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001749 continue;
1750 }
1751 /* Try collating class like [. .] */
1752 if (collclass != 0)
1753 {
1754 startc = collclass; /* allow [.a.]-x as a range */
1755 /* Will emit the proper atom at the end of the
1756 * while loop. */
1757 }
1758 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001759 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1760 * start character. */
1761 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001762 {
1763 emit_range = TRUE;
1764 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001765 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 continue; /* reading the end of the range */
1767 }
1768
1769 /* Now handle simple and escaped characters.
1770 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1771 * accepts "\t", "\e", etc., but only when the 'l' flag in
1772 * 'cpoptions' is not included.
1773 * Posix doesn't recognize backslash at all.
1774 */
1775 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001776 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 && regparse + 1 <= endp
1778 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001779 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 && vim_strchr(REGEXP_ABBR, regparse[1])
1781 != NULL)
1782 )
1783 )
1784 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001785 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001787 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 startc = reg_string ? NL : NFA_NEWL;
1789 else
1790 if (*regparse == 'd'
1791 || *regparse == 'o'
1792 || *regparse == 'x'
1793 || *regparse == 'u'
1794 || *regparse == 'U'
1795 )
1796 {
1797 /* TODO(RE) This needs more testing */
1798 startc = coll_get_char();
1799 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001800 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 }
1802 else
1803 {
1804 /* \r,\t,\e,\b */
1805 startc = backslash_trans(*regparse);
1806 }
1807 }
1808
1809 /* Normal printable char */
1810 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001811 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812
1813 /* Previous char was '-', so this char is end of range. */
1814 if (emit_range)
1815 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001816 endc = startc;
1817 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001819 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001820
1821 if (endc > startc + 2)
1822 {
1823 /* Emit a range instead of the sequence of
1824 * individual characters. */
1825 if (startc == 0)
1826 /* \x00 is translated to \x0a, start at \x01. */
1827 EMIT(1);
1828 else
1829 --post_ptr; /* remove NFA_CONCAT */
1830 EMIT(endc);
1831 EMIT(NFA_RANGE);
1832 EMIT(NFA_CONCAT);
1833 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001834 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001835 || (*mb_char2len)(endc) > 1))
1836 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001837 /* Emit the characters in the range.
1838 * "startc" was already emitted, so skip it.
1839 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001840 for (c = startc + 1; c <= endc; c++)
1841 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001842 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001843 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001845 }
1846 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847 {
1848#ifdef EBCDIC
1849 int alpha_only = FALSE;
1850
1851 /* for alphabetical range skip the gaps
1852 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1853 if (isalpha(startc) && isalpha(endc))
1854 alpha_only = TRUE;
1855#endif
1856 /* Emit the range. "startc" was already emitted, so
1857 * skip it. */
1858 for (c = startc + 1; c <= endc; c++)
1859#ifdef EBCDIC
1860 if (!alpha_only || isalpha(startc))
1861#endif
1862 {
1863 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001864 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001865 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001867 emit_range = FALSE;
1868 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 }
1870 else
1871 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001872 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001873 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 * Normally, simply emit startc. But if we get char
1875 * code=0 from a collating char, then replace it with
1876 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001878 * the backtracking engine. */
1879 if (startc == NFA_NEWL)
1880 {
1881 /* Line break can't be matched as part of the
1882 * collection, add an OR below. But not for negated
1883 * range. */
1884 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001885 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001886 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001888 {
1889 if (got_coll_char == TRUE && startc == 0)
1890 EMIT(0x0a);
1891 else
1892 EMIT(startc);
1893 EMIT(NFA_CONCAT);
1894 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 }
1896
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001897 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 } /* while (p < endp) */
1899
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001900 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901 if (*regparse == '-') /* if last, '-' is just a char */
1902 {
1903 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001904 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 /* skip the trailing ] */
1908 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001909 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001910
1911 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001913 EMIT(NFA_END_NEG_COLL);
1914 else
1915 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001916
1917 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001918 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001919 {
1920 EMIT(reg_string ? NL : NFA_NEWL);
1921 EMIT(NFA_OR);
1922 }
1923
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001924 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001925 } /* if exists closing ] */
1926
1927 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001929 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 default:
1932 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 int plen;
1934
1935nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001936 /* plen is length of current char with composing chars */
1937 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001938 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001939 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001941 int i = 0;
1942
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001943 /* A base character plus composing characters, or just one
1944 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001945 * This requires creating a separate atom as if enclosing
1946 * the characters in (), where NFA_COMPOSING is the ( and
1947 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 * building the postfix form, not the NFA itself;
1949 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001950 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001951 for (;;)
1952 {
1953 EMIT(c);
1954 if (i > 0)
1955 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001956 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001957 break;
1958 c = utf_ptr2char(old_regparse + i);
1959 }
1960 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001961 regparse = old_regparse + plen;
1962 }
1963 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 {
1965 c = no_Magic(c);
1966 EMIT(c);
1967 }
1968 return OK;
1969 }
1970 }
1971
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972 return OK;
1973}
1974
1975/*
1976 * Parse something followed by possible [*+=].
1977 *
1978 * A piece is an atom, possibly followed by a multi, an indication of how many
1979 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1980 * characters: "", "a", "aa", etc.
1981 *
1982 * piece ::= atom
1983 * or atom multi
1984 */
1985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001986nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987{
1988 int i;
1989 int op;
1990 int ret;
1991 long minval, maxval;
1992 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001993 parse_state_T old_state;
1994 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001995 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001996 int old_post_pos;
1997 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001998 int quest;
1999
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002000 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2001 * next. */
2002 save_parse_state(&old_state);
2003
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002004 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002005 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002006
2007 ret = nfa_regatom();
2008 if (ret == FAIL)
2009 return FAIL; /* cascaded error */
2010
2011 op = peekchr();
2012 if (re_multi_type(op) == NOT_MULTI)
2013 return OK;
2014
2015 skipchr();
2016 switch (op)
2017 {
2018 case Magic('*'):
2019 EMIT(NFA_STAR);
2020 break;
2021
2022 case Magic('+'):
2023 /*
2024 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2025 * first and only submatch would be "aaa". But the backtracking
2026 * engine interprets the plus as "try matching one more time", and
2027 * a* matches a second time at the end of the input, the empty
2028 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002029 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002031 * In order to be consistent with the old engine, we replace
2032 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002034 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035 curchr = -1;
2036 if (nfa_regatom() == FAIL)
2037 return FAIL;
2038 EMIT(NFA_STAR);
2039 EMIT(NFA_CONCAT);
2040 skipchr(); /* skip the \+ */
2041 break;
2042
2043 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002044 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002045 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002046 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047 switch(op)
2048 {
2049 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002050 /* \@= */
2051 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002052 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002053 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002054 /* \@! */
2055 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002056 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002057 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002058 op = no_Magic(getchr());
2059 if (op == '=')
2060 /* \@<= */
2061 i = NFA_PREV_ATOM_JUST_BEFORE;
2062 else if (op == '!')
2063 /* \@<! */
2064 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2065 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002066 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002067 /* \@> */
2068 i = NFA_PREV_ATOM_LIKE_PATTERN;
2069 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002071 if (i == 0)
2072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002074 return FAIL;
2075 }
2076 EMIT(i);
2077 if (i == NFA_PREV_ATOM_JUST_BEFORE
2078 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2079 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002080 break;
2081
2082 case Magic('?'):
2083 case Magic('='):
2084 EMIT(NFA_QUEST);
2085 break;
2086
2087 case Magic('{'):
2088 /* a{2,5} will expand to 'aaa?a?a?'
2089 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2090 * version of '?'
2091 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2092 * parenthesis have the same id
2093 */
2094
2095 greedy = TRUE;
2096 c2 = peekchr();
2097 if (c2 == '-' || c2 == Magic('-'))
2098 {
2099 skipchr();
2100 greedy = FALSE;
2101 }
2102 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002103 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002104
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2106 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002107 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002109 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002110 /* \{}, \{0,} */
2111 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002112 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002113 /* \{-}, \{-0,} */
2114 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002115 break;
2116 }
2117
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118 /* Special case: x{0} or x{-0} */
2119 if (maxval == 0)
2120 {
2121 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002122 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002123 /* NFA_EMPTY is 0-length and works everywhere */
2124 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002125 return OK;
2126 }
2127
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002128 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002129 * maximum is much larger than the minimum and when the maximum is
2130 * large. Bail out if we can use the other engine. */
2131 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002132 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002133 return FAIL;
2134
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002135 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002136 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002137 /* Save parse state after the repeated atom and the \{} */
2138 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002139
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2141 for (i = 0; i < maxval; i++)
2142 {
2143 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002144 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002145 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002146 if (nfa_regatom() == FAIL)
2147 return FAIL;
2148 /* after "minval" times, atoms are optional */
2149 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002150 {
2151 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002152 {
2153 if (greedy)
2154 EMIT(NFA_STAR);
2155 else
2156 EMIT(NFA_STAR_NONGREEDY);
2157 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002158 else
2159 EMIT(quest);
2160 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002161 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002162 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002163 if (i + 1 > minval && maxval == MAX_LIMIT)
2164 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 }
2166
2167 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002168 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169 curchr = -1;
2170
2171 break;
2172
2173
2174 default:
2175 break;
2176 } /* end switch */
2177
2178 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002180 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181
2182 return OK;
2183}
2184
2185/*
2186 * Parse one or more pieces, concatenated. It matches a match for the
2187 * first piece, followed by a match for the second piece, etc. Example:
2188 * "f[0-9]b", first matches "f", then a digit and then "b".
2189 *
2190 * concat ::= piece
2191 * or piece piece
2192 * or piece piece piece
2193 * etc.
2194 */
2195 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002196nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002197{
2198 int cont = TRUE;
2199 int first = TRUE;
2200
2201 while (cont)
2202 {
2203 switch (peekchr())
2204 {
2205 case NUL:
2206 case Magic('|'):
2207 case Magic('&'):
2208 case Magic(')'):
2209 cont = FALSE;
2210 break;
2211
2212 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002213 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 skipchr_keepstart();
2215 break;
2216 case Magic('c'):
2217 regflags |= RF_ICASE;
2218 skipchr_keepstart();
2219 break;
2220 case Magic('C'):
2221 regflags |= RF_NOICASE;
2222 skipchr_keepstart();
2223 break;
2224 case Magic('v'):
2225 reg_magic = MAGIC_ALL;
2226 skipchr_keepstart();
2227 curchr = -1;
2228 break;
2229 case Magic('m'):
2230 reg_magic = MAGIC_ON;
2231 skipchr_keepstart();
2232 curchr = -1;
2233 break;
2234 case Magic('M'):
2235 reg_magic = MAGIC_OFF;
2236 skipchr_keepstart();
2237 curchr = -1;
2238 break;
2239 case Magic('V'):
2240 reg_magic = MAGIC_NONE;
2241 skipchr_keepstart();
2242 curchr = -1;
2243 break;
2244
2245 default:
2246 if (nfa_regpiece() == FAIL)
2247 return FAIL;
2248 if (first == FALSE)
2249 EMIT(NFA_CONCAT);
2250 else
2251 first = FALSE;
2252 break;
2253 }
2254 }
2255
2256 return OK;
2257}
2258
2259/*
2260 * Parse a branch, one or more concats, separated by "\&". It matches the
2261 * last concat, but only if all the preceding concats also match at the same
2262 * position. Examples:
2263 * "foobeep\&..." matches "foo" in "foobeep".
2264 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2265 *
2266 * branch ::= concat
2267 * or concat \& concat
2268 * or concat \& concat \& concat
2269 * etc.
2270 */
2271 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002272nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002273{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002274 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002275
Bram Moolenaar16299b52013-05-30 18:45:23 +02002276 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277
2278 /* First branch, possibly the only one */
2279 if (nfa_regconcat() == FAIL)
2280 return FAIL;
2281
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002283 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002284 {
2285 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002286 /* if concat is empty do emit a node */
2287 if (old_post_pos == (int)(post_ptr - post_start))
2288 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 EMIT(NFA_NOPEN);
2290 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002291 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002292 if (nfa_regconcat() == FAIL)
2293 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002294 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002295 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002296 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002297 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002298 }
2299
Bram Moolenaar699c1202013-09-25 16:41:54 +02002300 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002301 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002302 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002303
2304 return OK;
2305}
2306
2307/*
2308 * Parse a pattern, one or more branches, separated by "\|". It matches
2309 * anything that matches one of the branches. Example: "foo\|beep" matches
2310 * "foo" and matches "beep". If more than one branch matches, the first one
2311 * is used.
2312 *
2313 * pattern ::= branch
2314 * or branch \| branch
2315 * or branch \| branch \| branch
2316 * etc.
2317 */
2318 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002319nfa_reg(
2320 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002321{
2322 int parno = 0;
2323
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324 if (paren == REG_PAREN)
2325 {
2326 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002327 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328 parno = regnpar++;
2329 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002330#ifdef FEAT_SYN_HL
2331 else if (paren == REG_ZPAREN)
2332 {
2333 /* Make a ZOPEN node. */
2334 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002335 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002336 parno = regnzpar++;
2337 }
2338#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339
2340 if (nfa_regbranch() == FAIL)
2341 return FAIL; /* cascaded error */
2342
2343 while (peekchr() == Magic('|'))
2344 {
2345 skipchr();
2346 if (nfa_regbranch() == FAIL)
2347 return FAIL; /* cascaded error */
2348 EMIT(NFA_OR);
2349 }
2350
2351 /* Check for proper termination. */
2352 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2353 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354 if (paren == REG_NPAREN)
2355 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2356 else
2357 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2358 }
2359 else if (paren == REG_NOPAREN && peekchr() != NUL)
2360 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002361 if (peekchr() == Magic(')'))
2362 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2363 else
2364 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2365 }
2366 /*
2367 * Here we set the flag allowing back references to this set of
2368 * parentheses.
2369 */
2370 if (paren == REG_PAREN)
2371 {
2372 had_endbrace[parno] = TRUE; /* have seen the close paren */
2373 EMIT(NFA_MOPEN + parno);
2374 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002375#ifdef FEAT_SYN_HL
2376 else if (paren == REG_ZPAREN)
2377 EMIT(NFA_ZOPEN + parno);
2378#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379
2380 return OK;
2381}
2382
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383#ifdef DEBUG
2384static char_u code[50];
2385
2386 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002387nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002388{
2389 int addnl = FALSE;
2390
2391 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2392 {
2393 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002394 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395 }
2396
2397 STRCPY(code, "");
2398 switch (c)
2399 {
2400 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2401 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2402 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2403 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2404 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2405 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2406
Bram Moolenaar5714b802013-05-28 22:03:20 +02002407 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2408 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2409 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2410 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2411 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2412 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2413 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2414 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2415 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002416#ifdef FEAT_SYN_HL
2417 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2418 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2419 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2420 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2421 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2422 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2423 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2424 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2425 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2426#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002427 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2428
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002429 case NFA_PREV_ATOM_NO_WIDTH:
2430 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002431 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2432 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002433 case NFA_PREV_ATOM_JUST_BEFORE:
2434 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2435 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2436 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002437 case NFA_PREV_ATOM_LIKE_PATTERN:
2438 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2439
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002440 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2441 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002442 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002443 case NFA_START_INVISIBLE_FIRST:
2444 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002445 case NFA_START_INVISIBLE_NEG:
2446 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002447 case NFA_START_INVISIBLE_NEG_FIRST:
2448 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002449 case NFA_START_INVISIBLE_BEFORE:
2450 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002451 case NFA_START_INVISIBLE_BEFORE_FIRST:
2452 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002453 case NFA_START_INVISIBLE_BEFORE_NEG:
2454 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002455 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2456 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002457 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002459 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002460 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2463 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002464 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002466 case NFA_MOPEN:
2467 case NFA_MOPEN1:
2468 case NFA_MOPEN2:
2469 case NFA_MOPEN3:
2470 case NFA_MOPEN4:
2471 case NFA_MOPEN5:
2472 case NFA_MOPEN6:
2473 case NFA_MOPEN7:
2474 case NFA_MOPEN8:
2475 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002476 STRCPY(code, "NFA_MOPEN(x)");
2477 code[10] = c - NFA_MOPEN + '0';
2478 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002479 case NFA_MCLOSE:
2480 case NFA_MCLOSE1:
2481 case NFA_MCLOSE2:
2482 case NFA_MCLOSE3:
2483 case NFA_MCLOSE4:
2484 case NFA_MCLOSE5:
2485 case NFA_MCLOSE6:
2486 case NFA_MCLOSE7:
2487 case NFA_MCLOSE8:
2488 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002489 STRCPY(code, "NFA_MCLOSE(x)");
2490 code[11] = c - NFA_MCLOSE + '0';
2491 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002492#ifdef FEAT_SYN_HL
2493 case NFA_ZOPEN:
2494 case NFA_ZOPEN1:
2495 case NFA_ZOPEN2:
2496 case NFA_ZOPEN3:
2497 case NFA_ZOPEN4:
2498 case NFA_ZOPEN5:
2499 case NFA_ZOPEN6:
2500 case NFA_ZOPEN7:
2501 case NFA_ZOPEN8:
2502 case NFA_ZOPEN9:
2503 STRCPY(code, "NFA_ZOPEN(x)");
2504 code[10] = c - NFA_ZOPEN + '0';
2505 break;
2506 case NFA_ZCLOSE:
2507 case NFA_ZCLOSE1:
2508 case NFA_ZCLOSE2:
2509 case NFA_ZCLOSE3:
2510 case NFA_ZCLOSE4:
2511 case NFA_ZCLOSE5:
2512 case NFA_ZCLOSE6:
2513 case NFA_ZCLOSE7:
2514 case NFA_ZCLOSE8:
2515 case NFA_ZCLOSE9:
2516 STRCPY(code, "NFA_ZCLOSE(x)");
2517 code[11] = c - NFA_ZCLOSE + '0';
2518 break;
2519#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002520 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2521 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2522 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2523 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002524 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2525 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002526 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2527 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2528 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2529 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2530 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2531 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2532 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2533 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2534 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2535 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2536 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2537 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2538 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2539 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002540 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002541
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002542 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002543 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2544 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2545 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002546 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002548
2549 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2550 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2551 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2552 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2553 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2554 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2555 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2556
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2558 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2559 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2560 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2561 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2562 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2563 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2564 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2565 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2566 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2567 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2568 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2569 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2570 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2571 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2572 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002573 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2574 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2575 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002576
2577 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2578 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2579 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2580 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2581 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2582 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2583 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2584 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2585 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2586 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2587 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2588 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2589 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2590 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2591 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2592 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2593 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2594 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2595 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2596 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2597 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2598 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2599 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2600 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2601 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2602 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2603 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002604 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2605 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2606 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2607 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608
2609 default:
2610 STRCPY(code, "CHAR(x)");
2611 code[5] = c;
2612 }
2613
2614 if (addnl == TRUE)
2615 STRCAT(code, " + NEWLINE ");
2616
2617}
2618
2619#ifdef ENABLE_LOG
2620static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002621static char_u e_log_open_failed[] = N_("Could not open temporary log file for writing, displaying on stderr... ");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002622
2623/*
2624 * Print the postfix notation of the current regexp.
2625 */
2626 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002627nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628{
2629 int *p;
2630 FILE *f;
2631
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002632 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633 if (f != NULL)
2634 {
2635 fprintf(f, "\n-------------------------\n");
2636 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002637 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638 else if (retval == OK)
2639 fprintf(f, ">>> NFA engine succeeded !\n");
2640 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002641 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002642 {
2643 nfa_set_code(*p);
2644 fprintf(f, "%s, ", code);
2645 }
2646 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002647 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 fprintf(f, "%d ", *p);
2649 fprintf(f, "\n\n");
2650 fclose(f);
2651 }
2652}
2653
2654/*
2655 * Print the NFA starting with a root node "state".
2656 */
2657 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002658nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002660 garray_T indent;
2661
2662 ga_init2(&indent, 1, 64);
2663 ga_append(&indent, '\0');
2664 nfa_print_state2(debugf, state, &indent);
2665 ga_clear(&indent);
2666}
2667
2668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002669nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002670{
2671 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672
2673 if (state == NULL)
2674 return;
2675
2676 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002677
2678 /* Output indent */
2679 p = (char_u *)indent->ga_data;
2680 if (indent->ga_len >= 3)
2681 {
2682 int last = indent->ga_len - 3;
2683 char_u save[2];
2684
2685 STRNCPY(save, &p[last], 2);
2686 STRNCPY(&p[last], "+-", 2);
2687 fprintf(debugf, " %s", p);
2688 STRNCPY(&p[last], save, 2);
2689 }
2690 else
2691 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002692
2693 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002694 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002695 code,
2696 state->c,
2697 abs(state->id),
2698 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699 if (state->id < 0)
2700 return;
2701
2702 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002703
2704 /* grow indent for state->out */
2705 indent->ga_len -= 1;
2706 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002707 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002708 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002709 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002710 ga_append(indent, '\0');
2711
2712 nfa_print_state2(debugf, state->out, indent);
2713
2714 /* replace last part of indent for state->out1 */
2715 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002716 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002717 ga_append(indent, '\0');
2718
2719 nfa_print_state2(debugf, state->out1, indent);
2720
2721 /* shrink indent */
2722 indent->ga_len -= 3;
2723 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002724}
2725
2726/*
2727 * Print the NFA state machine.
2728 */
2729 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002730nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002731{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002732 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002733
2734 if (debugf != NULL)
2735 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002736 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002737
Bram Moolenaar473de612013-06-08 18:19:48 +02002738 if (prog->reganch)
2739 fprintf(debugf, "reganch: %d\n", prog->reganch);
2740 if (prog->regstart != NUL)
2741 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2742 prog->regstart, prog->regstart);
2743 if (prog->match_text != NULL)
2744 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002745
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002746 fclose(debugf);
2747 }
2748}
2749#endif /* ENABLE_LOG */
2750#endif /* DEBUG */
2751
2752/*
2753 * Parse r.e. @expr and convert it into postfix form.
2754 * Return the postfix string on success, NULL otherwise.
2755 */
2756 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002757re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002758{
2759 if (nfa_reg(REG_NOPAREN) == FAIL)
2760 return NULL;
2761 EMIT(NFA_MOPEN);
2762 return post_start;
2763}
2764
2765/* NB. Some of the code below is inspired by Russ's. */
2766
2767/*
2768 * Represents an NFA state plus zero or one or two arrows exiting.
2769 * if c == MATCH, no arrows out; matching state.
2770 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2771 * If c < 256, labeled arrow with character c to out.
2772 */
2773
2774static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2775
2776/*
2777 * Allocate and initialize nfa_state_T.
2778 */
2779 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002780alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002781{
2782 nfa_state_T *s;
2783
2784 if (istate >= nstate)
2785 return NULL;
2786
2787 s = &state_ptr[istate++];
2788
2789 s->c = c;
2790 s->out = out;
2791 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002792 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002793
2794 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002795 s->lastlist[0] = 0;
2796 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002797
2798 return s;
2799}
2800
2801/*
2802 * A partially built NFA without the matching state filled in.
2803 * Frag_T.start points at the start state.
2804 * Frag_T.out is a list of places that need to be set to the
2805 * next state for this fragment.
2806 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002807
2808/* Since the out pointers in the list are always
2809 * uninitialized, we use the pointers themselves
2810 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002812union Ptrlist
2813{
2814 Ptrlist *next;
2815 nfa_state_T *s;
2816};
2817
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002818struct Frag
2819{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002820 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821 Ptrlist *out;
2822};
2823typedef struct Frag Frag_T;
2824
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002825/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002826 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827 */
2828 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002829frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002830{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002831 Frag_T n;
2832
2833 n.start = start;
2834 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835 return n;
2836}
2837
2838/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839 * Create singleton list containing just outp.
2840 */
2841 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002842list1(
2843 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002844{
2845 Ptrlist *l;
2846
2847 l = (Ptrlist *)outp;
2848 l->next = NULL;
2849 return l;
2850}
2851
2852/*
2853 * Patch the list of states at out to point to start.
2854 */
2855 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002856patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002857{
2858 Ptrlist *next;
2859
2860 for (; l; l = next)
2861 {
2862 next = l->next;
2863 l->s = s;
2864 }
2865}
2866
2867
2868/*
2869 * Join the two lists l1 and l2, returning the combination.
2870 */
2871 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002872append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002873{
2874 Ptrlist *oldl1;
2875
2876 oldl1 = l1;
2877 while (l1->next)
2878 l1 = l1->next;
2879 l1->next = l2;
2880 return oldl1;
2881}
2882
2883/*
2884 * Stack used for transforming postfix form into NFA.
2885 */
2886static Frag_T empty;
2887
2888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002889st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002890{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002891#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002892 FILE *df;
2893 int *p2;
2894
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002895 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 if (df)
2897 {
2898 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002899# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002901# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002903# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904 for (p2 = postfix; p2 < end; p2++)
2905 {
2906 nfa_set_code(*p2);
2907 fprintf(df, "%s, ", code);
2908 }
2909 nfa_set_code(*p);
2910 fprintf(df, "\nCurrent position is: ");
2911 for (p2 = postfix; p2 <= p; p2 ++)
2912 {
2913 nfa_set_code(*p2);
2914 fprintf(df, "%s, ", code);
2915 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002916# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 for (p2 = postfix; p2 < end; p2++)
2918 {
2919 fprintf(df, "%d, ", *p2);
2920 }
2921 fprintf(df, "\nCurrent position is: ");
2922 for (p2 = postfix; p2 <= p; p2 ++)
2923 {
2924 fprintf(df, "%d, ", *p2);
2925 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002926# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002927 fprintf(df, "\n--------------------------\n");
2928 fclose(df);
2929 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002930#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002931 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932}
2933
2934/*
2935 * Push an item onto the stack.
2936 */
2937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002938st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002939{
2940 Frag_T *stackp = *p;
2941
2942 if (stackp >= stack_end)
2943 return;
2944 *stackp = s;
2945 *p = *p + 1;
2946}
2947
2948/*
2949 * Pop an item from the stack.
2950 */
2951 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002952st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953{
2954 Frag_T *stackp;
2955
2956 *p = *p - 1;
2957 stackp = *p;
2958 if (stackp < stack)
2959 return empty;
2960 return **p;
2961}
2962
2963/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002964 * Estimate the maximum byte length of anything matching "state".
2965 * When unknown or unlimited return -1.
2966 */
2967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002968nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002969{
2970 int l, r;
2971 nfa_state_T *state = startstate;
2972 int len = 0;
2973
2974 /* detect looping in a NFA_SPLIT */
2975 if (depth > 4)
2976 return -1;
2977
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002978 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002979 {
2980 switch (state->c)
2981 {
2982 case NFA_END_INVISIBLE:
2983 case NFA_END_INVISIBLE_NEG:
2984 /* the end, return what we have */
2985 return len;
2986
2987 case NFA_SPLIT:
2988 /* two alternatives, use the maximum */
2989 l = nfa_max_width(state->out, depth + 1);
2990 r = nfa_max_width(state->out1, depth + 1);
2991 if (l < 0 || r < 0)
2992 return -1;
2993 return len + (l > r ? l : r);
2994
2995 case NFA_ANY:
2996 case NFA_START_COLL:
2997 case NFA_START_NEG_COLL:
2998 /* matches some character, including composing chars */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002999 if (enc_utf8)
3000 len += MB_MAXBYTES;
3001 else if (has_mbyte)
3002 len += 2;
3003 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003004 ++len;
3005 if (state->c != NFA_ANY)
3006 {
3007 /* skip over the characters */
3008 state = state->out1->out;
3009 continue;
3010 }
3011 break;
3012
3013 case NFA_DIGIT:
3014 case NFA_WHITE:
3015 case NFA_HEX:
3016 case NFA_OCTAL:
3017 /* ascii */
3018 ++len;
3019 break;
3020
3021 case NFA_IDENT:
3022 case NFA_SIDENT:
3023 case NFA_KWORD:
3024 case NFA_SKWORD:
3025 case NFA_FNAME:
3026 case NFA_SFNAME:
3027 case NFA_PRINT:
3028 case NFA_SPRINT:
3029 case NFA_NWHITE:
3030 case NFA_NDIGIT:
3031 case NFA_NHEX:
3032 case NFA_NOCTAL:
3033 case NFA_WORD:
3034 case NFA_NWORD:
3035 case NFA_HEAD:
3036 case NFA_NHEAD:
3037 case NFA_ALPHA:
3038 case NFA_NALPHA:
3039 case NFA_LOWER:
3040 case NFA_NLOWER:
3041 case NFA_UPPER:
3042 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003043 case NFA_LOWER_IC:
3044 case NFA_NLOWER_IC:
3045 case NFA_UPPER_IC:
3046 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003047 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003048 /* possibly non-ascii */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003049 if (has_mbyte)
3050 len += 3;
3051 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003052 ++len;
3053 break;
3054
3055 case NFA_START_INVISIBLE:
3056 case NFA_START_INVISIBLE_NEG:
3057 case NFA_START_INVISIBLE_BEFORE:
3058 case NFA_START_INVISIBLE_BEFORE_NEG:
3059 /* zero-width, out1 points to the END state */
3060 state = state->out1->out;
3061 continue;
3062
3063 case NFA_BACKREF1:
3064 case NFA_BACKREF2:
3065 case NFA_BACKREF3:
3066 case NFA_BACKREF4:
3067 case NFA_BACKREF5:
3068 case NFA_BACKREF6:
3069 case NFA_BACKREF7:
3070 case NFA_BACKREF8:
3071 case NFA_BACKREF9:
3072#ifdef FEAT_SYN_HL
3073 case NFA_ZREF1:
3074 case NFA_ZREF2:
3075 case NFA_ZREF3:
3076 case NFA_ZREF4:
3077 case NFA_ZREF5:
3078 case NFA_ZREF6:
3079 case NFA_ZREF7:
3080 case NFA_ZREF8:
3081 case NFA_ZREF9:
3082#endif
3083 case NFA_NEWL:
3084 case NFA_SKIP:
3085 /* unknown width */
3086 return -1;
3087
3088 case NFA_BOL:
3089 case NFA_EOL:
3090 case NFA_BOF:
3091 case NFA_EOF:
3092 case NFA_BOW:
3093 case NFA_EOW:
3094 case NFA_MOPEN:
3095 case NFA_MOPEN1:
3096 case NFA_MOPEN2:
3097 case NFA_MOPEN3:
3098 case NFA_MOPEN4:
3099 case NFA_MOPEN5:
3100 case NFA_MOPEN6:
3101 case NFA_MOPEN7:
3102 case NFA_MOPEN8:
3103 case NFA_MOPEN9:
3104#ifdef FEAT_SYN_HL
3105 case NFA_ZOPEN:
3106 case NFA_ZOPEN1:
3107 case NFA_ZOPEN2:
3108 case NFA_ZOPEN3:
3109 case NFA_ZOPEN4:
3110 case NFA_ZOPEN5:
3111 case NFA_ZOPEN6:
3112 case NFA_ZOPEN7:
3113 case NFA_ZOPEN8:
3114 case NFA_ZOPEN9:
3115 case NFA_ZCLOSE:
3116 case NFA_ZCLOSE1:
3117 case NFA_ZCLOSE2:
3118 case NFA_ZCLOSE3:
3119 case NFA_ZCLOSE4:
3120 case NFA_ZCLOSE5:
3121 case NFA_ZCLOSE6:
3122 case NFA_ZCLOSE7:
3123 case NFA_ZCLOSE8:
3124 case NFA_ZCLOSE9:
3125#endif
3126 case NFA_MCLOSE:
3127 case NFA_MCLOSE1:
3128 case NFA_MCLOSE2:
3129 case NFA_MCLOSE3:
3130 case NFA_MCLOSE4:
3131 case NFA_MCLOSE5:
3132 case NFA_MCLOSE6:
3133 case NFA_MCLOSE7:
3134 case NFA_MCLOSE8:
3135 case NFA_MCLOSE9:
3136 case NFA_NOPEN:
3137 case NFA_NCLOSE:
3138
3139 case NFA_LNUM_GT:
3140 case NFA_LNUM_LT:
3141 case NFA_COL_GT:
3142 case NFA_COL_LT:
3143 case NFA_VCOL_GT:
3144 case NFA_VCOL_LT:
3145 case NFA_MARK_GT:
3146 case NFA_MARK_LT:
3147 case NFA_VISUAL:
3148 case NFA_LNUM:
3149 case NFA_CURSOR:
3150 case NFA_COL:
3151 case NFA_VCOL:
3152 case NFA_MARK:
3153
3154 case NFA_ZSTART:
3155 case NFA_ZEND:
3156 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003157 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003158 case NFA_START_PATTERN:
3159 case NFA_END_PATTERN:
3160 case NFA_COMPOSING:
3161 case NFA_END_COMPOSING:
3162 /* zero-width */
3163 break;
3164
3165 default:
3166 if (state->c < 0)
3167 /* don't know what this is */
3168 return -1;
3169 /* normal character */
3170 len += MB_CHAR2LEN(state->c);
3171 break;
3172 }
3173
3174 /* normal way to continue */
3175 state = state->out;
3176 }
3177
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003178 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003179 return -1;
3180}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003181
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003182/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003183 * Convert a postfix form into its equivalent NFA.
3184 * Return the NFA start state on success, NULL otherwise.
3185 */
3186 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003187post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003188{
3189 int *p;
3190 int mopen;
3191 int mclose;
3192 Frag_T *stack = NULL;
3193 Frag_T *stackp = NULL;
3194 Frag_T *stack_end = NULL;
3195 Frag_T e1;
3196 Frag_T e2;
3197 Frag_T e;
3198 nfa_state_T *s;
3199 nfa_state_T *s1;
3200 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003201 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202
3203 if (postfix == NULL)
3204 return NULL;
3205
Bram Moolenaar053bb602013-05-20 13:55:21 +02003206#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207#define POP() st_pop(&stackp, stack); \
3208 if (stackp < stack) \
3209 { \
3210 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003211 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003212 return NULL; \
3213 }
3214
3215 if (nfa_calc_size == FALSE)
3216 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003217 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003218 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003219 if (stack == NULL)
3220 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003222 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223 }
3224
3225 for (p = postfix; p < end; ++p)
3226 {
3227 switch (*p)
3228 {
3229 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003230 /* Concatenation.
3231 * Pay attention: this operator does not exist in the r.e. itself
3232 * (it is implicit, really). It is added when r.e. is translated
3233 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 if (nfa_calc_size == TRUE)
3235 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003236 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237 break;
3238 }
3239 e2 = POP();
3240 e1 = POP();
3241 patch(e1.out, e2.start);
3242 PUSH(frag(e1.start, e2.out));
3243 break;
3244
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245 case NFA_OR:
3246 /* Alternation */
3247 if (nfa_calc_size == TRUE)
3248 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003249 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003250 break;
3251 }
3252 e2 = POP();
3253 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003254 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003255 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003256 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003257 PUSH(frag(s, append(e1.out, e2.out)));
3258 break;
3259
3260 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003261 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262 if (nfa_calc_size == TRUE)
3263 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003264 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003265 break;
3266 }
3267 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003268 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003270 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 patch(e.out, s);
3272 PUSH(frag(s, list1(&s->out1)));
3273 break;
3274
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003275 case NFA_STAR_NONGREEDY:
3276 /* Zero or more, prefer zero */
3277 if (nfa_calc_size == TRUE)
3278 {
3279 nstate++;
3280 break;
3281 }
3282 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003283 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003284 if (s == NULL)
3285 goto theend;
3286 patch(e.out, s);
3287 PUSH(frag(s, list1(&s->out)));
3288 break;
3289
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003290 case NFA_QUEST:
3291 /* one or zero atoms=> greedy match */
3292 if (nfa_calc_size == TRUE)
3293 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003294 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 break;
3296 }
3297 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003298 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003300 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 PUSH(frag(s, append(e.out, list1(&s->out1))));
3302 break;
3303
3304 case NFA_QUEST_NONGREEDY:
3305 /* zero or one atoms => non-greedy match */
3306 if (nfa_calc_size == TRUE)
3307 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003308 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 break;
3310 }
3311 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003312 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003314 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003315 PUSH(frag(s, append(e.out, list1(&s->out))));
3316 break;
3317
Bram Moolenaar417bad22013-06-07 14:08:30 +02003318 case NFA_END_COLL:
3319 case NFA_END_NEG_COLL:
3320 /* On the stack is the sequence starting with NFA_START_COLL or
3321 * NFA_START_NEG_COLL and all possible characters. Patch it to
3322 * add the output to the start. */
3323 if (nfa_calc_size == TRUE)
3324 {
3325 nstate++;
3326 break;
3327 }
3328 e = POP();
3329 s = alloc_state(NFA_END_COLL, NULL, NULL);
3330 if (s == NULL)
3331 goto theend;
3332 patch(e.out, s);
3333 e.start->out1 = s;
3334 PUSH(frag(e.start, list1(&s->out)));
3335 break;
3336
3337 case NFA_RANGE:
3338 /* Before this are two characters, the low and high end of a
3339 * range. Turn them into two states with MIN and MAX. */
3340 if (nfa_calc_size == TRUE)
3341 {
3342 /* nstate += 0; */
3343 break;
3344 }
3345 e2 = POP();
3346 e1 = POP();
3347 e2.start->val = e2.start->c;
3348 e2.start->c = NFA_RANGE_MAX;
3349 e1.start->val = e1.start->c;
3350 e1.start->c = NFA_RANGE_MIN;
3351 patch(e1.out, e2.start);
3352 PUSH(frag(e1.start, e2.out));
3353 break;
3354
Bram Moolenaar699c1202013-09-25 16:41:54 +02003355 case NFA_EMPTY:
3356 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 if (nfa_calc_size == TRUE)
3358 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003359 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 break;
3361 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003362 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003364 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003365 PUSH(frag(s, list1(&s->out)));
3366 break;
3367
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003368 case NFA_OPT_CHARS:
3369 {
3370 int n;
3371
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003372 /* \%[abc] implemented as:
3373 * NFA_SPLIT
3374 * +-CHAR(a)
3375 * | +-NFA_SPLIT
3376 * | +-CHAR(b)
3377 * | | +-NFA_SPLIT
3378 * | | +-CHAR(c)
3379 * | | | +-next
3380 * | | +- next
3381 * | +- next
3382 * +- next
3383 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003384 n = *++p; /* get number of characters */
3385 if (nfa_calc_size == TRUE)
3386 {
3387 nstate += n;
3388 break;
3389 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003390 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003391 e1.out = NULL; /* stores list with out1's */
3392 s1 = NULL; /* previous NFA_SPLIT to connect to */
3393 while (n-- > 0)
3394 {
3395 e = POP(); /* get character */
3396 s = alloc_state(NFA_SPLIT, e.start, NULL);
3397 if (s == NULL)
3398 goto theend;
3399 if (e1.out == NULL)
3400 e1 = e;
3401 patch(e.out, s1);
3402 append(e1.out, list1(&s->out1));
3403 s1 = s;
3404 }
3405 PUSH(frag(s, e1.out));
3406 break;
3407 }
3408
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003409 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003410 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003411 case NFA_PREV_ATOM_JUST_BEFORE:
3412 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003413 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003414 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003415 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3416 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003417 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003418 int start_state;
3419 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003420 int n = 0;
3421 nfa_state_T *zend;
3422 nfa_state_T *skip;
3423
Bram Moolenaardecd9542013-06-07 16:31:50 +02003424 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003425 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003426 case NFA_PREV_ATOM_NO_WIDTH:
3427 start_state = NFA_START_INVISIBLE;
3428 end_state = NFA_END_INVISIBLE;
3429 break;
3430 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3431 start_state = NFA_START_INVISIBLE_NEG;
3432 end_state = NFA_END_INVISIBLE_NEG;
3433 break;
3434 case NFA_PREV_ATOM_JUST_BEFORE:
3435 start_state = NFA_START_INVISIBLE_BEFORE;
3436 end_state = NFA_END_INVISIBLE;
3437 break;
3438 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3439 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3440 end_state = NFA_END_INVISIBLE_NEG;
3441 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003442 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003443 start_state = NFA_START_PATTERN;
3444 end_state = NFA_END_PATTERN;
3445 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003446 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003447
3448 if (before)
3449 n = *++p; /* get the count */
3450
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003451 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003452 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003453 * The \@<= operator: match for the preceding atom.
3454 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003456 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457
3458 if (nfa_calc_size == TRUE)
3459 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003460 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 break;
3462 }
3463 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003464 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003466 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003467
Bram Moolenaar87953742013-06-05 18:52:40 +02003468 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003470 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003471 if (pattern)
3472 {
3473 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3474 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003475 if (skip == NULL)
3476 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003477 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003478 if (zend == NULL)
3479 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003480 s1->out= skip;
3481 patch(e.out, zend);
3482 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003483 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003484 else
3485 {
3486 patch(e.out, s1);
3487 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003488 if (before)
3489 {
3490 if (n <= 0)
3491 /* See if we can guess the maximum width, it avoids a
3492 * lot of pointless tries. */
3493 n = nfa_max_width(e.start, 0);
3494 s->val = n; /* store the count */
3495 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003496 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003497 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003498 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003499
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003500 case NFA_COMPOSING: /* char with composing char */
3501#if 0
3502 /* TODO */
3503 if (regflags & RF_ICOMBINE)
3504 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003505 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003506 }
3507#endif
3508 /* FALLTHROUGH */
3509
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003510 case NFA_MOPEN: /* \( \) Submatch */
3511 case NFA_MOPEN1:
3512 case NFA_MOPEN2:
3513 case NFA_MOPEN3:
3514 case NFA_MOPEN4:
3515 case NFA_MOPEN5:
3516 case NFA_MOPEN6:
3517 case NFA_MOPEN7:
3518 case NFA_MOPEN8:
3519 case NFA_MOPEN9:
3520#ifdef FEAT_SYN_HL
3521 case NFA_ZOPEN: /* \z( \) Submatch */
3522 case NFA_ZOPEN1:
3523 case NFA_ZOPEN2:
3524 case NFA_ZOPEN3:
3525 case NFA_ZOPEN4:
3526 case NFA_ZOPEN5:
3527 case NFA_ZOPEN6:
3528 case NFA_ZOPEN7:
3529 case NFA_ZOPEN8:
3530 case NFA_ZOPEN9:
3531#endif
3532 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003533 if (nfa_calc_size == TRUE)
3534 {
3535 nstate += 2;
3536 break;
3537 }
3538
3539 mopen = *p;
3540 switch (*p)
3541 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003542 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3543#ifdef FEAT_SYN_HL
3544 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3545 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3546 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3547 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3548 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3549 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3550 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3551 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3552 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3553 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3554#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003555 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003556 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003557 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003558 mclose = *p + NSUBEXP;
3559 break;
3560 }
3561
3562 /* Allow "NFA_MOPEN" as a valid postfix representation for
3563 * the empty regexp "". In this case, the NFA will be
3564 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3565 * empty groups of parenthesis, and empty mbyte chars */
3566 if (stackp == stack)
3567 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003568 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003569 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003570 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003571 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003572 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003573 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003574 patch(list1(&s->out), s1);
3575 PUSH(frag(s, list1(&s1->out)));
3576 break;
3577 }
3578
3579 /* At least one node was emitted before NFA_MOPEN, so
3580 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3581 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003582 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003584 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003585
Bram Moolenaar525666f2013-06-02 16:40:55 +02003586 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003587 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003588 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589 patch(e.out, s1);
3590
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003591 if (mopen == NFA_COMPOSING)
3592 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003593 patch(list1(&s->out1), s1);
3594
3595 PUSH(frag(s, list1(&s1->out)));
3596 break;
3597
Bram Moolenaar5714b802013-05-28 22:03:20 +02003598 case NFA_BACKREF1:
3599 case NFA_BACKREF2:
3600 case NFA_BACKREF3:
3601 case NFA_BACKREF4:
3602 case NFA_BACKREF5:
3603 case NFA_BACKREF6:
3604 case NFA_BACKREF7:
3605 case NFA_BACKREF8:
3606 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003607#ifdef FEAT_SYN_HL
3608 case NFA_ZREF1:
3609 case NFA_ZREF2:
3610 case NFA_ZREF3:
3611 case NFA_ZREF4:
3612 case NFA_ZREF5:
3613 case NFA_ZREF6:
3614 case NFA_ZREF7:
3615 case NFA_ZREF8:
3616 case NFA_ZREF9:
3617#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003618 if (nfa_calc_size == TRUE)
3619 {
3620 nstate += 2;
3621 break;
3622 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003623 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003624 if (s == NULL)
3625 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003626 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003627 if (s1 == NULL)
3628 goto theend;
3629 patch(list1(&s->out), s1);
3630 PUSH(frag(s, list1(&s1->out)));
3631 break;
3632
Bram Moolenaar423532e2013-05-29 21:14:42 +02003633 case NFA_LNUM:
3634 case NFA_LNUM_GT:
3635 case NFA_LNUM_LT:
3636 case NFA_VCOL:
3637 case NFA_VCOL_GT:
3638 case NFA_VCOL_LT:
3639 case NFA_COL:
3640 case NFA_COL_GT:
3641 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003642 case NFA_MARK:
3643 case NFA_MARK_GT:
3644 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003645 {
3646 int n = *++p; /* lnum, col or mark name */
3647
Bram Moolenaar423532e2013-05-29 21:14:42 +02003648 if (nfa_calc_size == TRUE)
3649 {
3650 nstate += 1;
3651 break;
3652 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003653 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003654 if (s == NULL)
3655 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003656 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003657 PUSH(frag(s, list1(&s->out)));
3658 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003659 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003660
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003661 case NFA_ZSTART:
3662 case NFA_ZEND:
3663 default:
3664 /* Operands */
3665 if (nfa_calc_size == TRUE)
3666 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003667 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 break;
3669 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003670 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003671 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003672 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003673 PUSH(frag(s, list1(&s->out)));
3674 break;
3675
3676 } /* switch(*p) */
3677
3678 } /* for(p = postfix; *p; ++p) */
3679
3680 if (nfa_calc_size == TRUE)
3681 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003682 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003683 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003684 }
3685
3686 e = POP();
3687 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003688 {
3689 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003690 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 +02003691 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692
3693 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003694 {
3695 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003697 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699 matchstate = &state_ptr[istate++]; /* the match state */
3700 matchstate->c = NFA_MATCH;
3701 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003702 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703
3704 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003705 ret = e.start;
3706
3707theend:
3708 vim_free(stack);
3709 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003710
3711#undef POP1
3712#undef PUSH1
3713#undef POP2
3714#undef PUSH2
3715#undef POP
3716#undef PUSH
3717}
3718
Bram Moolenaara2947e22013-06-11 22:44:09 +02003719/*
3720 * After building the NFA program, inspect it to add optimization hints.
3721 */
3722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003723nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003724{
3725 int i;
3726 int c;
3727
3728 for (i = 0; i < prog->nstate; ++i)
3729 {
3730 c = prog->state[i].c;
3731 if (c == NFA_START_INVISIBLE
3732 || c == NFA_START_INVISIBLE_NEG
3733 || c == NFA_START_INVISIBLE_BEFORE
3734 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3735 {
3736 int directly;
3737
3738 /* Do it directly when what follows is possibly the end of the
3739 * match. */
3740 if (match_follows(prog->state[i].out1->out, 0))
3741 directly = TRUE;
3742 else
3743 {
3744 int ch_invisible = failure_chance(prog->state[i].out, 0);
3745 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3746
3747 /* Postpone when the invisible match is expensive or has a
3748 * lower chance of failing. */
3749 if (c == NFA_START_INVISIBLE_BEFORE
3750 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3751 {
3752 /* "before" matches are very expensive when
3753 * unbounded, always prefer what follows then,
3754 * unless what follows will always match.
3755 * Otherwise strongly prefer what follows. */
3756 if (prog->state[i].val <= 0 && ch_follows > 0)
3757 directly = FALSE;
3758 else
3759 directly = ch_follows * 10 < ch_invisible;
3760 }
3761 else
3762 {
3763 /* normal invisible, first do the one with the
3764 * highest failure chance */
3765 directly = ch_follows < ch_invisible;
3766 }
3767 }
3768 if (directly)
3769 /* switch to the _FIRST state */
3770 ++prog->state[i].c;
3771 }
3772 }
3773}
3774
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003775/****************************************************************
3776 * NFA execution code.
3777 ****************************************************************/
3778
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003779typedef struct
3780{
3781 int in_use; /* number of subexpr with useful info */
3782
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003783 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003784 union
3785 {
3786 struct multipos
3787 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003788 linenr_T start_lnum;
3789 linenr_T end_lnum;
3790 colnr_T start_col;
3791 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003792 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003793 struct linepos
3794 {
3795 char_u *start;
3796 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003797 } line[NSUBEXP];
3798 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003799} regsub_T;
3800
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003801typedef struct
3802{
3803 regsub_T norm; /* \( .. \) matches */
3804#ifdef FEAT_SYN_HL
3805 regsub_T synt; /* \z( .. \) matches */
3806#endif
3807} regsubs_T;
3808
Bram Moolenaara2d95102013-06-04 14:23:05 +02003809/* nfa_pim_T stores a Postponed Invisible Match. */
3810typedef struct nfa_pim_S nfa_pim_T;
3811struct nfa_pim_S
3812{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003813 int result; /* NFA_PIM_*, see below */
3814 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003815 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003816 union
3817 {
3818 lpos_T pos;
3819 char_u *ptr;
3820 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003821};
3822
3823/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003824#define NFA_PIM_UNUSED 0 /* pim not used */
3825#define NFA_PIM_TODO 1 /* pim not done yet */
3826#define NFA_PIM_MATCH 2 /* pim executed, matches */
3827#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003828
3829
Bram Moolenaar963fee22013-05-26 21:47:28 +02003830/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003831typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003832{
3833 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003834 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003835 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3836 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003837 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003838} nfa_thread_T;
3839
Bram Moolenaar963fee22013-05-26 21:47:28 +02003840/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003841typedef struct
3842{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003843 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003844 int n; /* nr of states currently in "t" */
3845 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003846 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003847 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003848} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849
Bram Moolenaar5714b802013-05-28 22:03:20 +02003850#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003851static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003852
3853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003854log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003855{
3856 log_subexpr(&subs->norm);
3857# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003858 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003859 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003860# endif
3861}
3862
Bram Moolenaar5714b802013-05-28 22:03:20 +02003863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003864log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003865{
3866 int j;
3867
3868 for (j = 0; j < sub->in_use; j++)
3869 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003870 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003871 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003872 sub->list.multi[j].start_col,
3873 (int)sub->list.multi[j].start_lnum,
3874 sub->list.multi[j].end_col,
3875 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003876 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003877 {
3878 char *s = (char *)sub->list.line[j].start;
3879 char *e = (char *)sub->list.line[j].end;
3880
Bram Moolenaar87953742013-06-05 18:52:40 +02003881 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003882 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003883 s == NULL ? "NULL" : s,
3884 e == NULL ? "NULL" : e);
3885 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003886}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003887
3888 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003889pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003890{
3891 static char buf[30];
3892
3893 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3894 buf[0] = NUL;
3895 else
3896 {
3897 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003898 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003899 }
3900 return buf;
3901}
3902
Bram Moolenaar5714b802013-05-28 22:03:20 +02003903#endif
3904
Bram Moolenaar963fee22013-05-26 21:47:28 +02003905/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003906static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003907#ifdef FEAT_RELTIME
3908static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003909static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003910static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003911#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003912
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003913static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003914static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003915
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003916/*
3917 * Copy postponed invisible match info from "from" to "to".
3918 */
3919 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003920copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003921{
3922 to->result = from->result;
3923 to->state = from->state;
3924 copy_sub(&to->subs.norm, &from->subs.norm);
3925#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003926 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003927 copy_sub(&to->subs.synt, &from->subs.synt);
3928#endif
3929 to->end = from->end;
3930}
3931
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003933clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003934{
3935 if (REG_MULTI)
3936 /* Use 0xff to set lnum to -1 */
3937 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003938 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003939 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003940 vim_memset(sub->list.line, 0,
3941 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003942 sub->in_use = 0;
3943}
3944
3945/*
3946 * Copy the submatches from "from" to "to".
3947 */
3948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003949copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003950{
3951 to->in_use = from->in_use;
3952 if (from->in_use > 0)
3953 {
3954 /* Copy the match start and end positions. */
3955 if (REG_MULTI)
3956 mch_memmove(&to->list.multi[0],
3957 &from->list.multi[0],
3958 sizeof(struct multipos) * from->in_use);
3959 else
3960 mch_memmove(&to->list.line[0],
3961 &from->list.line[0],
3962 sizeof(struct linepos) * from->in_use);
3963 }
3964}
3965
3966/*
3967 * Like copy_sub() but exclude the main match.
3968 */
3969 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003970copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003971{
3972 if (to->in_use < from->in_use)
3973 to->in_use = from->in_use;
3974 if (from->in_use > 1)
3975 {
3976 /* Copy the match start and end positions. */
3977 if (REG_MULTI)
3978 mch_memmove(&to->list.multi[1],
3979 &from->list.multi[1],
3980 sizeof(struct multipos) * (from->in_use - 1));
3981 else
3982 mch_memmove(&to->list.line[1],
3983 &from->list.line[1],
3984 sizeof(struct linepos) * (from->in_use - 1));
3985 }
3986}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003987
Bram Moolenaar428e9872013-05-30 17:05:39 +02003988/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003989 * Like copy_sub() but only do the end of the main match if \ze is present.
3990 */
3991 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003992copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003993{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003994 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003995 {
3996 if (REG_MULTI)
3997 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003998 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003999 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004000 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4001 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004002 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004003 }
4004 else
4005 {
4006 if (from->list.line[0].end != NULL)
4007 to->list.line[0].end = from->list.line[0].end;
4008 }
4009 }
4010}
4011
4012/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004013 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004014 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004015 */
4016 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004017sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004018{
4019 int i;
4020 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004021 linenr_T s1;
4022 linenr_T s2;
4023 char_u *sp1;
4024 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004025
4026 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4027 if (REG_MULTI)
4028 {
4029 for (i = 0; i < todo; ++i)
4030 {
4031 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004032 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004034 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004035 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004036 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004038 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004039 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004040 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004041 if (s1 != -1 && sub1->list.multi[i].start_col
4042 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004043 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004044
Bram Moolenaar0270f382018-07-17 05:43:58 +02004045 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004046 {
4047 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004048 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004049 else
4050 s1 = -1;
4051 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004052 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004053 else
4054 s2 = -1;
4055 if (s1 != s2)
4056 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004057 if (s1 != -1 && sub1->list.multi[i].end_col
4058 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004059 return FALSE;
4060 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004061 }
4062 }
4063 else
4064 {
4065 for (i = 0; i < todo; ++i)
4066 {
4067 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004068 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004069 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004070 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004071 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004072 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004073 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004075 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004076 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004077 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004078 {
4079 if (i < sub1->in_use)
4080 sp1 = sub1->list.line[i].end;
4081 else
4082 sp1 = NULL;
4083 if (i < sub2->in_use)
4084 sp2 = sub2->list.line[i].end;
4085 else
4086 sp2 = NULL;
4087 if (sp1 != sp2)
4088 return FALSE;
4089 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004090 }
4091 }
4092
4093 return TRUE;
4094}
4095
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004096#ifdef ENABLE_LOG
4097 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004098report_state(char *action,
4099 regsub_T *sub,
4100 nfa_state_T *state,
4101 int lid,
4102 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004103{
4104 int col;
4105
4106 if (sub->in_use <= 0)
4107 col = -1;
4108 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004109 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004110 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004111 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004112 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004113 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4114 action, abs(state->id), lid, state->c, code, col,
4115 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004116}
4117#endif
4118
Bram Moolenaar43e02982013-06-07 17:31:29 +02004119/*
4120 * Return TRUE if the same state is already in list "l" with the same
4121 * positions as "subs".
4122 */
4123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004124has_state_with_pos(
4125 nfa_list_T *l, /* runtime state list */
4126 nfa_state_T *state, /* state to update */
4127 regsubs_T *subs, /* pointers to subexpressions */
4128 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004129{
4130 nfa_thread_T *thread;
4131 int i;
4132
4133 for (i = 0; i < l->n; ++i)
4134 {
4135 thread = &l->t[i];
4136 if (thread->state->id == state->id
4137 && sub_equal(&thread->subs.norm, &subs->norm)
4138#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004139 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004140 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004141#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004142 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004143 return TRUE;
4144 }
4145 return FALSE;
4146}
4147
4148/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004149 * Return TRUE if "one" and "two" are equal. That includes when both are not
4150 * set.
4151 */
4152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004153pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004154{
4155 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4156 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4157
4158 if (one_unused)
4159 /* one is unused: equal when two is also unused */
4160 return two_unused;
4161 if (two_unused)
4162 /* one is used and two is not: not equal */
4163 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004164 /* compare the state id */
4165 if (one->state->id != two->state->id)
4166 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004167 /* compare the position */
4168 if (REG_MULTI)
4169 return one->end.pos.lnum == two->end.pos.lnum
4170 && one->end.pos.col == two->end.pos.col;
4171 return one->end.ptr == two->end.ptr;
4172}
4173
4174/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004175 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4176 */
4177 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004178match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004179{
4180 nfa_state_T *state = startstate;
4181
4182 /* avoid too much recursion */
4183 if (depth > 10)
4184 return FALSE;
4185
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004186 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004187 {
4188 switch (state->c)
4189 {
4190 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004191 case NFA_MCLOSE:
4192 case NFA_END_INVISIBLE:
4193 case NFA_END_INVISIBLE_NEG:
4194 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004195 return TRUE;
4196
4197 case NFA_SPLIT:
4198 return match_follows(state->out, depth + 1)
4199 || match_follows(state->out1, depth + 1);
4200
4201 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004202 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004203 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004204 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004205 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004206 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004207 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004208 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004209 case NFA_COMPOSING:
4210 /* skip ahead to next state */
4211 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004212 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004213
4214 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004215 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004216 case NFA_IDENT:
4217 case NFA_SIDENT:
4218 case NFA_KWORD:
4219 case NFA_SKWORD:
4220 case NFA_FNAME:
4221 case NFA_SFNAME:
4222 case NFA_PRINT:
4223 case NFA_SPRINT:
4224 case NFA_WHITE:
4225 case NFA_NWHITE:
4226 case NFA_DIGIT:
4227 case NFA_NDIGIT:
4228 case NFA_HEX:
4229 case NFA_NHEX:
4230 case NFA_OCTAL:
4231 case NFA_NOCTAL:
4232 case NFA_WORD:
4233 case NFA_NWORD:
4234 case NFA_HEAD:
4235 case NFA_NHEAD:
4236 case NFA_ALPHA:
4237 case NFA_NALPHA:
4238 case NFA_LOWER:
4239 case NFA_NLOWER:
4240 case NFA_UPPER:
4241 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004242 case NFA_LOWER_IC:
4243 case NFA_NLOWER_IC:
4244 case NFA_UPPER_IC:
4245 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004246 case NFA_START_COLL:
4247 case NFA_START_NEG_COLL:
4248 case NFA_NEWL:
4249 /* state will advance input */
4250 return FALSE;
4251
4252 default:
4253 if (state->c > 0)
4254 /* state will advance input */
4255 return FALSE;
4256
4257 /* Others: zero-width or possibly zero-width, might still find
4258 * a match at the same position, keep looking. */
4259 break;
4260 }
4261 state = state->out;
4262 }
4263 return FALSE;
4264}
4265
4266
4267/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004268 * Return TRUE if "state" is already in list "l".
4269 */
4270 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004271state_in_list(
4272 nfa_list_T *l, /* runtime state list */
4273 nfa_state_T *state, /* state to update */
4274 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004275{
4276 if (state->lastlist[nfa_ll_index] == l->id)
4277 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004278 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004279 return TRUE;
4280 }
4281 return FALSE;
4282}
4283
Bram Moolenaar16b35782016-09-09 20:29:50 +02004284/* Offset used for "off" by addstate_here(). */
4285#define ADDSTATE_HERE_OFFSET 10
4286
Bram Moolenaard05bf562013-06-30 23:24:08 +02004287/*
4288 * Add "state" and possibly what follows to state list ".".
4289 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004290 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004291 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004292 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004293addstate(
4294 nfa_list_T *l, /* runtime state list */
4295 nfa_state_T *state, /* state to update */
4296 regsubs_T *subs_arg, /* pointers to subexpressions */
4297 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004298 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004300 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004301 int off = off_arg;
4302 int add_here = FALSE;
4303 int listindex = 0;
4304 int k;
4305 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004306 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004307 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004308 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004309 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004310 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004311 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004312 regsubs_T *subs = subs_arg;
4313 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004314#ifdef ENABLE_LOG
4315 int did_print = FALSE;
4316#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004317 static int depth = 0;
4318
4319 // This function is called recursively. When the depth is too much we run
4320 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004321 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004322 {
4323 --depth;
4324 return NULL;
4325 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326
Bram Moolenaar16b35782016-09-09 20:29:50 +02004327 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4328 {
4329 add_here = TRUE;
4330 off = 0;
4331 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4332 }
4333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334 switch (state->c)
4335 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 case NFA_NCLOSE:
4337 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004338 case NFA_MCLOSE1:
4339 case NFA_MCLOSE2:
4340 case NFA_MCLOSE3:
4341 case NFA_MCLOSE4:
4342 case NFA_MCLOSE5:
4343 case NFA_MCLOSE6:
4344 case NFA_MCLOSE7:
4345 case NFA_MCLOSE8:
4346 case NFA_MCLOSE9:
4347#ifdef FEAT_SYN_HL
4348 case NFA_ZCLOSE:
4349 case NFA_ZCLOSE1:
4350 case NFA_ZCLOSE2:
4351 case NFA_ZCLOSE3:
4352 case NFA_ZCLOSE4:
4353 case NFA_ZCLOSE5:
4354 case NFA_ZCLOSE6:
4355 case NFA_ZCLOSE7:
4356 case NFA_ZCLOSE8:
4357 case NFA_ZCLOSE9:
4358#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004359 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004360 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004361 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004362 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004363 /* These nodes are not added themselves but their "out" and/or
4364 * "out1" may be added below. */
4365 break;
4366
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004367 case NFA_BOL:
4368 case NFA_BOF:
4369 /* "^" won't match past end-of-line, don't bother trying.
4370 * Except when at the end of the line, or when we are going to the
4371 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004372 if (rex.input > rex.line
4373 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004374 && (nfa_endp == NULL
4375 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004376 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004377 goto skip_add;
4378 /* FALLTHROUGH */
4379
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004380 case NFA_MOPEN1:
4381 case NFA_MOPEN2:
4382 case NFA_MOPEN3:
4383 case NFA_MOPEN4:
4384 case NFA_MOPEN5:
4385 case NFA_MOPEN6:
4386 case NFA_MOPEN7:
4387 case NFA_MOPEN8:
4388 case NFA_MOPEN9:
4389#ifdef FEAT_SYN_HL
4390 case NFA_ZOPEN:
4391 case NFA_ZOPEN1:
4392 case NFA_ZOPEN2:
4393 case NFA_ZOPEN3:
4394 case NFA_ZOPEN4:
4395 case NFA_ZOPEN5:
4396 case NFA_ZOPEN6:
4397 case NFA_ZOPEN7:
4398 case NFA_ZOPEN8:
4399 case NFA_ZOPEN9:
4400#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004401 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004402 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004403 /* These nodes need to be added so that we can bail out when it
4404 * was added to this list before at the same position to avoid an
4405 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004406
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004407 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004408 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004409 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004410 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004411 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004412 * when there is a PIM. For NFA_MATCH check the position,
4413 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004414 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004415 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004416 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004417 /* When called from addstate_here() do insert before
4418 * existing states. */
4419 if (add_here)
4420 {
4421 for (k = 0; k < l->n && k < listindex; ++k)
4422 if (l->t[k].state->id == state->id)
4423 {
4424 found = TRUE;
4425 break;
4426 }
4427 }
4428 if (!add_here || found)
4429 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004430skip_add:
4431#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004432 nfa_set_code(state->c);
4433 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4434 abs(state->id), l->id, state->c, code,
4435 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004436#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004437 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004438 return subs;
4439 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004440 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004441
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004442 /* Do not add the state again when it exists with the same
4443 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004444 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004445 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004446 }
4447
Bram Moolenaara0169122013-06-26 18:16:58 +02004448 /* When there are backreferences or PIMs the number of states may
4449 * be (a lot) bigger than anticipated. */
4450 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004451 {
4452 int newlen = l->len * 3 / 2 + 50;
4453
Bram Moolenaard05bf562013-06-30 23:24:08 +02004454 if (subs != &temp_subs)
4455 {
4456 /* "subs" may point into the current array, need to make a
4457 * copy before it becomes invalid. */
4458 copy_sub(&temp_subs.norm, &subs->norm);
4459#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004460 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004461 copy_sub(&temp_subs.synt, &subs->synt);
4462#endif
4463 subs = &temp_subs;
4464 }
4465
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004466 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004467 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4468 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004470
4471 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004472 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004473 thread = &l->t[l->n++];
4474 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004475 if (pim == NULL)
4476 thread->pim.result = NFA_PIM_UNUSED;
4477 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004478 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004479 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004480 l->has_pim = TRUE;
4481 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004482 copy_sub(&thread->subs.norm, &subs->norm);
4483#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004484 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004485 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004486#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004487#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004488 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004489 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004490#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004491 }
4492
4493#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004494 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004495 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004496#endif
4497 switch (state->c)
4498 {
4499 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004500 break;
4501
4502 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004503 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004504 subs = addstate(l, state->out, subs, pim, off_arg);
4505 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004506 break;
4507
Bram Moolenaar699c1202013-09-25 16:41:54 +02004508 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509 case NFA_NOPEN:
4510 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004511 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004512 break;
4513
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004514 case NFA_MOPEN:
4515 case NFA_MOPEN1:
4516 case NFA_MOPEN2:
4517 case NFA_MOPEN3:
4518 case NFA_MOPEN4:
4519 case NFA_MOPEN5:
4520 case NFA_MOPEN6:
4521 case NFA_MOPEN7:
4522 case NFA_MOPEN8:
4523 case NFA_MOPEN9:
4524#ifdef FEAT_SYN_HL
4525 case NFA_ZOPEN:
4526 case NFA_ZOPEN1:
4527 case NFA_ZOPEN2:
4528 case NFA_ZOPEN3:
4529 case NFA_ZOPEN4:
4530 case NFA_ZOPEN5:
4531 case NFA_ZOPEN6:
4532 case NFA_ZOPEN7:
4533 case NFA_ZOPEN8:
4534 case NFA_ZOPEN9:
4535#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004536 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004537 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004538 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004540 sub = &subs->norm;
4541 }
4542#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004543 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004544 {
4545 subidx = state->c - NFA_ZOPEN;
4546 sub = &subs->synt;
4547 }
4548#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004549 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004550 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004551 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004552 sub = &subs->norm;
4553 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004555 /* avoid compiler warnings */
4556 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004557 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004558
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004559 /* Set the position (with "off" added) in the subexpression. Save
4560 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561 if (REG_MULTI)
4562 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004563 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004564 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004565 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004566 save_in_use = -1;
4567 }
4568 else
4569 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004570 save_in_use = sub->in_use;
4571 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004572 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004573 sub->list.multi[i].start_lnum = -1;
4574 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004575 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004576 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004577 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004578 if (off == -1)
4579 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004580 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004581 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004582 }
4583 else
4584 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004585 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004586 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004587 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004588 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004589 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004590 }
4591 else
4592 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004593 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004594 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004595 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004596 save_in_use = -1;
4597 }
4598 else
4599 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004600 save_in_use = sub->in_use;
4601 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004602 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004603 sub->list.line[i].start = NULL;
4604 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004605 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004606 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004607 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004608 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 }
4610
Bram Moolenaar16b35782016-09-09 20:29:50 +02004611 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004612 if (subs == NULL)
4613 break;
4614 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004615#ifdef FEAT_SYN_HL
4616 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4617 sub = &subs->synt;
4618 else
4619#endif
4620 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004621
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004622 if (save_in_use == -1)
4623 {
4624 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004625 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004626 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004627 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004628 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004630 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004631 break;
4632
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004633 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004634 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004635 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004636 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004637 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004638 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004639 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640 break;
4641 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004642 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004643 case NFA_MCLOSE1:
4644 case NFA_MCLOSE2:
4645 case NFA_MCLOSE3:
4646 case NFA_MCLOSE4:
4647 case NFA_MCLOSE5:
4648 case NFA_MCLOSE6:
4649 case NFA_MCLOSE7:
4650 case NFA_MCLOSE8:
4651 case NFA_MCLOSE9:
4652#ifdef FEAT_SYN_HL
4653 case NFA_ZCLOSE:
4654 case NFA_ZCLOSE1:
4655 case NFA_ZCLOSE2:
4656 case NFA_ZCLOSE3:
4657 case NFA_ZCLOSE4:
4658 case NFA_ZCLOSE5:
4659 case NFA_ZCLOSE6:
4660 case NFA_ZCLOSE7:
4661 case NFA_ZCLOSE8:
4662 case NFA_ZCLOSE9:
4663#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004664 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004665 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004666 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004667 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004668 sub = &subs->norm;
4669 }
4670#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004671 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004672 {
4673 subidx = state->c - NFA_ZCLOSE;
4674 sub = &subs->synt;
4675 }
4676#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004677 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004678 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004679 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004680 sub = &subs->norm;
4681 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004683 /* We don't fill in gaps here, there must have been an MOPEN that
4684 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004685 save_in_use = sub->in_use;
4686 if (sub->in_use <= subidx)
4687 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 if (REG_MULTI)
4689 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004690 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004691 if (off == -1)
4692 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004693 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004694 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004695 }
4696 else
4697 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004698 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004699 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004700 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004701 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004702 /* avoid compiler warnings */
4703 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004704 }
4705 else
4706 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004707 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004708 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004709 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004710 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 }
4712
Bram Moolenaar16b35782016-09-09 20:29:50 +02004713 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004714 if (subs == NULL)
4715 break;
Bram Moolenaarebefd992013-08-14 14:18:40 +02004716 /* "subs" may have changed, need to set "sub" again */
4717#ifdef FEAT_SYN_HL
4718 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4719 sub = &subs->synt;
4720 else
4721#endif
4722 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004723
4724 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004725 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004726 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004727 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004728 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004729 break;
4730 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004731 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004732 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733}
4734
4735/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004736 * Like addstate(), but the new state(s) are put at position "*ip".
4737 * Used for zero-width matches, next state to use is the added one.
4738 * This makes sure the order of states to be tried does not change, which
4739 * matters for alternatives.
4740 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004741 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004742addstate_here(
4743 nfa_list_T *l, /* runtime state list */
4744 nfa_state_T *state, /* state to update */
4745 regsubs_T *subs, /* pointers to subexpressions */
4746 nfa_pim_T *pim, /* postponed look-behind match */
4747 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004748{
4749 int tlen = l->n;
4750 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004751 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004752 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004753
Bram Moolenaar16b35782016-09-09 20:29:50 +02004754 /* First add the state(s) at the end, so that we know how many there are.
4755 * Pass the listidx as offset (avoids adding another argument to
4756 * addstate(). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004757 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4758 if (r == NULL)
4759 return r;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004760
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004761 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004762 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004763 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004764
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004765 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004766 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004767 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004768 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004769 if (count == 1)
4770 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004771 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004772 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004773 }
4774 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004775 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004776 if (l->n + count - 1 >= l->len)
4777 {
4778 /* not enough space to move the new states, reallocate the list
4779 * and move the states to the right position */
4780 nfa_thread_T *newl;
4781
4782 l->len = l->len * 3 / 2 + 50;
4783 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4784 if (newl == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004785 return r;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004786 mch_memmove(&(newl[0]),
4787 &(l->t[0]),
4788 sizeof(nfa_thread_T) * listidx);
4789 mch_memmove(&(newl[listidx]),
4790 &(l->t[l->n - count]),
4791 sizeof(nfa_thread_T) * count);
4792 mch_memmove(&(newl[listidx + count]),
4793 &(l->t[listidx + 1]),
4794 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4795 vim_free(l->t);
4796 l->t = newl;
4797 }
4798 else
4799 {
4800 /* make space for new states, then move them from the
4801 * end to the current position */
4802 mch_memmove(&(l->t[listidx + count]),
4803 &(l->t[listidx + 1]),
4804 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4805 mch_memmove(&(l->t[listidx]),
4806 &(l->t[l->n - 1]),
4807 sizeof(nfa_thread_T) * count);
4808 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004809 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004810 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004811 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004812
4813 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004814}
4815
4816/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004817 * Check character class "class" against current character c.
4818 */
4819 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004820check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004821{
4822 switch (class)
4823 {
4824 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004825 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826 return OK;
4827 break;
4828 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004829 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004830 return OK;
4831 break;
4832 case NFA_CLASS_BLANK:
4833 if (c == ' ' || c == '\t')
4834 return OK;
4835 break;
4836 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004837 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004838 return OK;
4839 break;
4840 case NFA_CLASS_DIGIT:
4841 if (VIM_ISDIGIT(c))
4842 return OK;
4843 break;
4844 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004845 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004846 return OK;
4847 break;
4848 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004849 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004850 return OK;
4851 break;
4852 case NFA_CLASS_PRINT:
4853 if (vim_isprintc(c))
4854 return OK;
4855 break;
4856 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004857 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858 return OK;
4859 break;
4860 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004861 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004862 return OK;
4863 break;
4864 case NFA_CLASS_UPPER:
4865 if (MB_ISUPPER(c))
4866 return OK;
4867 break;
4868 case NFA_CLASS_XDIGIT:
4869 if (vim_isxdigit(c))
4870 return OK;
4871 break;
4872 case NFA_CLASS_TAB:
4873 if (c == '\t')
4874 return OK;
4875 break;
4876 case NFA_CLASS_RETURN:
4877 if (c == '\r')
4878 return OK;
4879 break;
4880 case NFA_CLASS_BACKSPACE:
4881 if (c == '\b')
4882 return OK;
4883 break;
4884 case NFA_CLASS_ESCAPE:
4885 if (c == '\033')
4886 return OK;
4887 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004888 case NFA_CLASS_IDENT:
4889 if (vim_isIDc(c))
4890 return OK;
4891 break;
4892 case NFA_CLASS_KEYWORD:
4893 if (reg_iswordc(c))
4894 return OK;
4895 break;
4896 case NFA_CLASS_FNAME:
4897 if (vim_isfilec(c))
4898 return OK;
4899 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004900
4901 default:
4902 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004903 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004904 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004905 }
4906 return FAIL;
4907}
4908
Bram Moolenaar5714b802013-05-28 22:03:20 +02004909/*
4910 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004911 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004912 */
4913 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004914match_backref(
4915 regsub_T *sub, /* pointers to subexpressions */
4916 int subidx,
4917 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004918{
4919 int len;
4920
4921 if (sub->in_use <= subidx)
4922 {
4923retempty:
4924 /* backref was not set, match an empty string */
4925 *bytelen = 0;
4926 return TRUE;
4927 }
4928
4929 if (REG_MULTI)
4930 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004931 if (sub->list.multi[subidx].start_lnum < 0
4932 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004933 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004934 if (sub->list.multi[subidx].start_lnum == rex.lnum
4935 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004936 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004937 len = sub->list.multi[subidx].end_col
4938 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004939 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4940 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004941 {
4942 *bytelen = len;
4943 return TRUE;
4944 }
4945 }
4946 else
4947 {
4948 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004949 sub->list.multi[subidx].start_lnum,
4950 sub->list.multi[subidx].start_col,
4951 sub->list.multi[subidx].end_lnum,
4952 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004953 bytelen) == RA_MATCH)
4954 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004955 }
4956 }
4957 else
4958 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004959 if (sub->list.line[subidx].start == NULL
4960 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004961 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004962 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004963 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004964 {
4965 *bytelen = len;
4966 return TRUE;
4967 }
4968 }
4969 return FALSE;
4970}
4971
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004972#ifdef FEAT_SYN_HL
4973
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004974/*
4975 * Check for a match with \z subexpression "subidx".
4976 * Return TRUE if it matches.
4977 */
4978 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004979match_zref(
4980 int subidx,
4981 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004982{
4983 int len;
4984
4985 cleanup_zsubexpr();
4986 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4987 {
4988 /* backref was not set, match an empty string */
4989 *bytelen = 0;
4990 return TRUE;
4991 }
4992
4993 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004994 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004995 {
4996 *bytelen = len;
4997 return TRUE;
4998 }
4999 return FALSE;
5000}
5001#endif
5002
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005003/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005004 * Save list IDs for all NFA states of "prog" into "list".
5005 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005006 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005007 */
5008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005009nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005010{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005011 int i;
5012 nfa_state_T *p;
5013
5014 /* Order in the list is reverse, it's a bit faster that way. */
5015 p = &prog->state[0];
5016 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005017 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005018 list[i] = p->lastlist[1];
5019 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005020 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005021 }
5022}
5023
5024/*
5025 * Restore list IDs from "list" to all NFA states.
5026 */
5027 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005028nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005030 int i;
5031 nfa_state_T *p;
5032
5033 p = &prog->state[0];
5034 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005035 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005036 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005037 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005038 }
5039}
5040
Bram Moolenaar423532e2013-05-29 21:14:42 +02005041 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005042nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005043{
5044 if (op == 1) return pos > val;
5045 if (op == 2) return pos < val;
5046 return val == pos;
5047}
5048
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005049static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005050
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005052 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005053 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5054 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005055 */
5056 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005057recursive_regmatch(
5058 nfa_state_T *state,
5059 nfa_pim_T *pim,
5060 nfa_regprog_T *prog,
5061 regsubs_T *submatch,
5062 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005063 int **listids,
5064 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005065{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005066 int save_reginput_col = (int)(rex.input - rex.line);
5067 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005068 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005069 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005070 save_se_T *save_nfa_endp = nfa_endp;
5071 save_se_T endpos;
5072 save_se_T *endposp = NULL;
5073 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005074 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005075
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005076 if (pim != NULL)
5077 {
5078 /* start at the position where the postponed match was */
5079 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005080 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005081 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005082 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005083 }
5084
Bram Moolenaardecd9542013-06-07 16:31:50 +02005085 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005086 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5087 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5088 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005089 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005090 /* The recursive match must end at the current position. When "pim" is
5091 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092 endposp = &endpos;
5093 if (REG_MULTI)
5094 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005095 if (pim == NULL)
5096 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005097 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5098 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005099 }
5100 else
5101 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005102 }
5103 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005104 {
5105 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005106 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005107 else
5108 endpos.se_u.ptr = pim->end.ptr;
5109 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005110
5111 /* Go back the specified number of bytes, or as far as the
5112 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005113 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005114 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005115 if (state->val <= 0)
5116 {
5117 if (REG_MULTI)
5118 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005119 rex.line = reg_getline(--rex.lnum);
5120 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005121 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005122 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005123 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005124 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005125 }
5126 else
5127 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005128 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129 {
5130 /* Not enough bytes in this line, go to end of
5131 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005132 rex.line = reg_getline(--rex.lnum);
5133 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005134 {
5135 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005136 rex.line = reg_getline(++rex.lnum);
5137 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005138 }
5139 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005140 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005141 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005142 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005143 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005144 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005145 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005146 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005147 }
5148 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005149 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005150 }
5151 }
5152
Bram Moolenaarf46da702013-06-02 22:37:42 +02005153#ifdef ENABLE_LOG
5154 if (log_fd != stderr)
5155 fclose(log_fd);
5156 log_fd = NULL;
5157#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005158 /* Have to clear the lastlist field of the NFA nodes, so that
5159 * nfa_regmatch() and addstate() can run properly after recursion. */
5160 if (nfa_ll_index == 1)
5161 {
5162 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5163 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005164 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005165 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005166 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005167 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005168 if (*listids == NULL)
5169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005170 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005171 return 0;
5172 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005173 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005174 }
5175 nfa_save_listids(prog, *listids);
5176 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005177 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005178 }
5179 else
5180 {
5181 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005182 * entry. Make sure rex.nfa_listid is different from a previous
5183 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005184 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005185 if (rex.nfa_listid <= rex.nfa_alt_listid)
5186 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005187 }
5188
5189 /* Call nfa_regmatch() to check if the current concat matches at this
5190 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005191 nfa_endp = endposp;
5192 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005193
5194 if (need_restore)
5195 nfa_restore_listids(prog, *listids);
5196 else
5197 {
5198 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005199 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005200 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005201
5202 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005203 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005204 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005205 rex.line = reg_getline(rex.lnum);
5206 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005207 if (result != NFA_TOO_EXPENSIVE)
5208 {
5209 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005210 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005211 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005212 nfa_endp = save_nfa_endp;
5213
5214#ifdef ENABLE_LOG
5215 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5216 if (log_fd != NULL)
5217 {
5218 fprintf(log_fd, "****************************\n");
5219 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5220 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5221 fprintf(log_fd, "****************************\n");
5222 }
5223 else
5224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005225 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005226 log_fd = stderr;
5227 }
5228#endif
5229
5230 return result;
5231}
5232
Bram Moolenaara2d95102013-06-04 14:23:05 +02005233/*
5234 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005235 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005236 * NFA_ANY: 1
5237 * specific character: 99
5238 */
5239 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005240failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005241{
5242 int c = state->c;
5243 int l, r;
5244
5245 /* detect looping */
5246 if (depth > 4)
5247 return 1;
5248
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005249 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005250 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005251 case NFA_SPLIT:
5252 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5253 /* avoid recursive stuff */
5254 return 1;
5255 /* two alternatives, use the lowest failure chance */
5256 l = failure_chance(state->out, depth + 1);
5257 r = failure_chance(state->out1, depth + 1);
5258 return l < r ? l : r;
5259
5260 case NFA_ANY:
5261 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005262 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005263
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005264 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005265 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005266 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005267 /* empty match works always */
5268 return 0;
5269
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005270 case NFA_START_INVISIBLE:
5271 case NFA_START_INVISIBLE_FIRST:
5272 case NFA_START_INVISIBLE_NEG:
5273 case NFA_START_INVISIBLE_NEG_FIRST:
5274 case NFA_START_INVISIBLE_BEFORE:
5275 case NFA_START_INVISIBLE_BEFORE_FIRST:
5276 case NFA_START_INVISIBLE_BEFORE_NEG:
5277 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5278 case NFA_START_PATTERN:
5279 /* recursive regmatch is expensive, use low failure chance */
5280 return 5;
5281
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005282 case NFA_BOL:
5283 case NFA_EOL:
5284 case NFA_BOF:
5285 case NFA_EOF:
5286 case NFA_NEWL:
5287 return 99;
5288
5289 case NFA_BOW:
5290 case NFA_EOW:
5291 return 90;
5292
5293 case NFA_MOPEN:
5294 case NFA_MOPEN1:
5295 case NFA_MOPEN2:
5296 case NFA_MOPEN3:
5297 case NFA_MOPEN4:
5298 case NFA_MOPEN5:
5299 case NFA_MOPEN6:
5300 case NFA_MOPEN7:
5301 case NFA_MOPEN8:
5302 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005303#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005304 case NFA_ZOPEN:
5305 case NFA_ZOPEN1:
5306 case NFA_ZOPEN2:
5307 case NFA_ZOPEN3:
5308 case NFA_ZOPEN4:
5309 case NFA_ZOPEN5:
5310 case NFA_ZOPEN6:
5311 case NFA_ZOPEN7:
5312 case NFA_ZOPEN8:
5313 case NFA_ZOPEN9:
5314 case NFA_ZCLOSE:
5315 case NFA_ZCLOSE1:
5316 case NFA_ZCLOSE2:
5317 case NFA_ZCLOSE3:
5318 case NFA_ZCLOSE4:
5319 case NFA_ZCLOSE5:
5320 case NFA_ZCLOSE6:
5321 case NFA_ZCLOSE7:
5322 case NFA_ZCLOSE8:
5323 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005324#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005325 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005326 case NFA_MCLOSE1:
5327 case NFA_MCLOSE2:
5328 case NFA_MCLOSE3:
5329 case NFA_MCLOSE4:
5330 case NFA_MCLOSE5:
5331 case NFA_MCLOSE6:
5332 case NFA_MCLOSE7:
5333 case NFA_MCLOSE8:
5334 case NFA_MCLOSE9:
5335 case NFA_NCLOSE:
5336 return failure_chance(state->out, depth + 1);
5337
5338 case NFA_BACKREF1:
5339 case NFA_BACKREF2:
5340 case NFA_BACKREF3:
5341 case NFA_BACKREF4:
5342 case NFA_BACKREF5:
5343 case NFA_BACKREF6:
5344 case NFA_BACKREF7:
5345 case NFA_BACKREF8:
5346 case NFA_BACKREF9:
5347#ifdef FEAT_SYN_HL
5348 case NFA_ZREF1:
5349 case NFA_ZREF2:
5350 case NFA_ZREF3:
5351 case NFA_ZREF4:
5352 case NFA_ZREF5:
5353 case NFA_ZREF6:
5354 case NFA_ZREF7:
5355 case NFA_ZREF8:
5356 case NFA_ZREF9:
5357#endif
5358 /* backreferences don't match in many places */
5359 return 94;
5360
5361 case NFA_LNUM_GT:
5362 case NFA_LNUM_LT:
5363 case NFA_COL_GT:
5364 case NFA_COL_LT:
5365 case NFA_VCOL_GT:
5366 case NFA_VCOL_LT:
5367 case NFA_MARK_GT:
5368 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005369 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005370 /* before/after positions don't match very often */
5371 return 85;
5372
5373 case NFA_LNUM:
5374 return 90;
5375
5376 case NFA_CURSOR:
5377 case NFA_COL:
5378 case NFA_VCOL:
5379 case NFA_MARK:
5380 /* specific positions rarely match */
5381 return 98;
5382
5383 case NFA_COMPOSING:
5384 return 95;
5385
5386 default:
5387 if (c > 0)
5388 /* character match fails often */
5389 return 95;
5390 }
5391
5392 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005393 return 50;
5394}
5395
Bram Moolenaarf46da702013-06-02 22:37:42 +02005396/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005397 * Skip until the char "c" we know a match must start with.
5398 */
5399 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005400skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005401{
5402 char_u *s;
5403
5404 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005405 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005406 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005407 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005408 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005409 if (s == NULL)
5410 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005411 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005412 return OK;
5413}
5414
5415/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005416 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005417 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005418 * Returns zero for no match, 1 for a match.
5419 */
5420 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005421find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005422{
5423 colnr_T col = startcol;
5424 int c1, c2;
5425 int len1, len2;
5426 int match;
5427
5428 for (;;)
5429 {
5430 match = TRUE;
5431 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5432 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5433 {
5434 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005435 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005436 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005437 {
5438 match = FALSE;
5439 break;
5440 }
5441 len2 += MB_CHAR2LEN(c2);
5442 }
5443 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005444 /* check that no composing char follows */
5445 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005446 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005447 {
5448 cleanup_subexpr();
5449 if (REG_MULTI)
5450 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005451 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005452 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005453 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005454 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005455 }
5456 else
5457 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005458 rex.reg_startp[0] = rex.line + col;
5459 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005460 }
5461 return 1L;
5462 }
5463
5464 /* Try finding regstart after the current match. */
5465 col += MB_CHAR2LEN(regstart); /* skip regstart */
5466 if (skip_to_start(regstart, &col) == FAIL)
5467 break;
5468 }
5469 return 0L;
5470}
5471
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005472#ifdef FEAT_RELTIME
5473 static int
5474nfa_did_time_out()
5475{
5476 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5477 {
5478 if (nfa_timed_out != NULL)
5479 *nfa_timed_out = TRUE;
5480 return TRUE;
5481 }
5482 return FALSE;
5483}
5484#endif
5485
Bram Moolenaar473de612013-06-08 18:19:48 +02005486/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005487 * Main matching routine.
5488 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005489 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005490 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005491 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005492 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005493 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005494 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005495 * Note: Caller must ensure that: start != NULL.
5496 */
5497 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005498nfa_regmatch(
5499 nfa_regprog_T *prog,
5500 nfa_state_T *start,
5501 regsubs_T *submatch,
5502 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005504 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005505 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005506 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005507 int go_to_nextline = FALSE;
5508 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005509 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005510 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005511 nfa_list_T *thislist;
5512 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005514 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005515 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005516 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005517 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005518 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005519 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005520 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005521#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005522 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005524
Bram Moolenaar41f12052013-08-25 17:01:42 +02005525 /* Some patterns may take a long time to match, especially when using
5526 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5527 fast_breakcheck();
5528 if (got_int)
5529 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005530#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005531 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005532 return FALSE;
5533#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005534
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005535#ifdef NFA_REGEXP_DEBUG_LOG
5536 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5537 if (debug == NULL)
5538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005539 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005540 return FALSE;
5541 }
5542#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005543 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005545 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005546 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005547
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005548 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005549 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005550 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005551 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005552 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005554
5555#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005556 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557 if (log_fd != NULL)
5558 {
5559 fprintf(log_fd, "**********************************\n");
5560 nfa_set_code(start->c);
5561 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5562 abs(start->id), code);
5563 fprintf(log_fd, "**********************************\n");
5564 }
5565 else
5566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005567 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005568 log_fd = stderr;
5569 }
5570#endif
5571
5572 thislist = &list[0];
5573 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005574 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005575 nextlist = &list[1];
5576 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005577 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005579 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005580#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005581 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005582
5583 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5584 * it's the first MOPEN. */
5585 if (toplevel)
5586 {
5587 if (REG_MULTI)
5588 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005589 m->norm.list.multi[0].start_lnum = rex.lnum;
5590 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005591 }
5592 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005593 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005594 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005595 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005596 }
5597 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005598 r = addstate(thislist, start, m, NULL, 0);
5599 if (r == NULL)
5600 {
5601 nfa_match = NFA_TOO_EXPENSIVE;
5602 goto theend;
5603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005605#define ADD_STATE_IF_MATCH(state) \
5606 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005607 add_state = state->out; \
5608 add_off = clen; \
5609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610
5611 /*
5612 * Run for each character.
5613 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005614 for (;;)
5615 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005616 int curc;
5617 int clen;
5618
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005619 if (has_mbyte)
5620 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005621 curc = (*mb_ptr2char)(rex.input);
5622 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005623 }
5624 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005625 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005626 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005627 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005628 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005629 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005630 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005631 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005632 go_to_nextline = FALSE;
5633 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005634
5635 /* swap lists */
5636 thislist = &list[flag];
5637 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005638 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005639 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005640 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005641 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005642 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005643# ifdef FEAT_EVAL
5644 || nfa_fail_for_testing
5645# endif
5646 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005647 {
5648 /* too many states, retry with old engine */
5649 nfa_match = NFA_TOO_EXPENSIVE;
5650 goto theend;
5651 }
5652
Bram Moolenaar0270f382018-07-17 05:43:58 +02005653 thislist->id = rex.nfa_listid;
5654 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005655
5656#ifdef ENABLE_LOG
5657 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005658 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005659 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005660 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005661 {
5662 int i;
5663
5664 for (i = 0; i < thislist->n; i++)
5665 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5666 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005667 fprintf(log_fd, "\n");
5668#endif
5669
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005670#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671 fprintf(debug, "\n-------------------\n");
5672#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005673 /*
5674 * If the state lists are empty we can stop.
5675 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005676 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005677 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005678
5679 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005680 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005682 /* If the list gets very long there probably is something wrong.
5683 * At least allow interrupting with CTRL-C. */
5684 fast_breakcheck();
5685 if (got_int)
5686 break;
5687#ifdef FEAT_RELTIME
5688 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5689 {
5690 nfa_time_count = 0;
5691 if (nfa_did_time_out())
5692 break;
5693 }
5694#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005695 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005697#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698 nfa_set_code(t->state->c);
5699 fprintf(debug, "%s, ", code);
5700#endif
5701#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005702 {
5703 int col;
5704
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005705 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005706 col = -1;
5707 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005708 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005709 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005710 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005711 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005712 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005713 abs(t->state->id), (int)t->state->c, code, col,
5714 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005715 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716#endif
5717
5718 /*
5719 * Handle the possible codes of the current state.
5720 * The most important is NFA_MATCH.
5721 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005722 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005723 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005724 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005725 switch (t->state->c)
5726 {
5727 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005728 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005729 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005730 * rex.reg_icombine is not set, that is not really a match. */
5731 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005732 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005733
Bram Moolenaar963fee22013-05-26 21:47:28 +02005734 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005735 copy_sub(&submatch->norm, &t->subs.norm);
5736#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005737 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005738 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005739#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005740#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005741 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005742#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005743 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005744 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005745 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005746 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005747 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005748 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005749 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005750 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005751
5752 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005753 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005754 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005755 /*
5756 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005757 * NFA_START_INVISIBLE_BEFORE node.
5758 * They surround a zero-width group, used with "\@=", "\&",
5759 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005760 * If we got here, it means that the current "invisible" group
5761 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005762 * nfa_regmatch(). For a look-behind match only when it ends
5763 * in the position in "nfa_endp".
5764 * Submatches are stored in *m, and used in the parent call.
5765 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005766#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005767 if (nfa_endp != NULL)
5768 {
5769 if (REG_MULTI)
5770 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005771 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005772 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005773 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005774 nfa_endp->se_u.pos.col);
5775 else
5776 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005777 (int)(rex.input - rex.line),
5778 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005779 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005780#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005781 /* If "nfa_endp" is set it's only a match if it ends at
5782 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005783 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005784 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5785 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005786 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005787 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005788 break;
5789
5790 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005791 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005792 {
5793 copy_sub(&m->norm, &t->subs.norm);
5794#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005795 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005796 copy_sub(&m->synt, &t->subs.synt);
5797#endif
5798 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005799#ifdef ENABLE_LOG
5800 fprintf(log_fd, "Match found:\n");
5801 log_subsexpr(m);
5802#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005803 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005804 /* See comment above at "goto nextchar". */
5805 if (nextlist->n == 0)
5806 clen = 0;
5807 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005808
5809 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005810 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005811 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005812 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005813 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005814 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005815 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005816 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005817 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005818#ifdef ENABLE_LOG
5819 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5820 failure_chance(t->state->out, 0),
5821 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005822#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005823 /* Do it directly if there already is a PIM or when
5824 * nfa_postprocess() detected it will work better. */
5825 if (t->pim.result != NFA_PIM_UNUSED
5826 || t->state->c == NFA_START_INVISIBLE_FIRST
5827 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5828 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5829 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005830 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005831 int in_use = m->norm.in_use;
5832
Bram Moolenaar699c1202013-09-25 16:41:54 +02005833 /* Copy submatch info for the recursive call, opposite
5834 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005835 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005836#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005837 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005838 copy_sub_off(&m->synt, &t->subs.synt);
5839#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005840
Bram Moolenaara2d95102013-06-04 14:23:05 +02005841 /*
5842 * First try matching the invisible match, then what
5843 * follows.
5844 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005845 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005846 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005847 if (result == NFA_TOO_EXPENSIVE)
5848 {
5849 nfa_match = result;
5850 goto theend;
5851 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005852
Bram Moolenaardecd9542013-06-07 16:31:50 +02005853 /* for \@! and \@<! it is a match when the result is
5854 * FALSE */
5855 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005856 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5857 || t->state->c
5858 == NFA_START_INVISIBLE_BEFORE_NEG
5859 || t->state->c
5860 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005861 {
5862 /* Copy submatch info from the recursive call */
5863 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005864#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005865 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005866 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005867#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005868 /* If the pattern has \ze and it matched in the
5869 * sub pattern, use it. */
5870 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005871
Bram Moolenaara2d95102013-06-04 14:23:05 +02005872 /* t->state->out1 is the corresponding
5873 * END_INVISIBLE node; Add its out to the current
5874 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005875 add_here = TRUE;
5876 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005877 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005878 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005879 }
5880 else
5881 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005882 nfa_pim_T pim;
5883
Bram Moolenaara2d95102013-06-04 14:23:05 +02005884 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005885 * First try matching what follows. Only if a match
5886 * is found verify the invisible match matches. Add a
5887 * nfa_pim_T to the following states, it contains info
5888 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005889 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005890 pim.state = t->state;
5891 pim.result = NFA_PIM_TODO;
5892 pim.subs.norm.in_use = 0;
5893#ifdef FEAT_SYN_HL
5894 pim.subs.synt.in_use = 0;
5895#endif
5896 if (REG_MULTI)
5897 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005898 pim.end.pos.col = (int)(rex.input - rex.line);
5899 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005900 }
5901 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005902 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005903
5904 /* t->state->out1 is the corresponding END_INVISIBLE
5905 * node; Add its out to the current list (zero-width
5906 * match). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005907 if (addstate_here(thislist, t->state->out1->out,
5908 &t->subs, &pim, &listidx) == NULL)
5909 {
5910 nfa_match = NFA_TOO_EXPENSIVE;
5911 goto theend;
5912 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005913 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005914 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005915 break;
5916
Bram Moolenaar87953742013-06-05 18:52:40 +02005917 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005918 {
5919 nfa_state_T *skip = NULL;
5920#ifdef ENABLE_LOG
5921 int skip_lid = 0;
5922#endif
5923
5924 /* There is no point in trying to match the pattern if the
5925 * output state is not going to be added to the list. */
5926 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5927 {
5928 skip = t->state->out1->out;
5929#ifdef ENABLE_LOG
5930 skip_lid = nextlist->id;
5931#endif
5932 }
5933 else if (state_in_list(nextlist,
5934 t->state->out1->out->out, &t->subs))
5935 {
5936 skip = t->state->out1->out->out;
5937#ifdef ENABLE_LOG
5938 skip_lid = nextlist->id;
5939#endif
5940 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005941 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005942 t->state->out1->out->out, &t->subs))
5943 {
5944 skip = t->state->out1->out->out;
5945#ifdef ENABLE_LOG
5946 skip_lid = thislist->id;
5947#endif
5948 }
5949 if (skip != NULL)
5950 {
5951#ifdef ENABLE_LOG
5952 nfa_set_code(skip->c);
5953 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5954 abs(skip->id), skip_lid, skip->c, code);
5955#endif
5956 break;
5957 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005958 /* Copy submatch info to the recursive call, opposite of what
5959 * happens afterwards. */
5960 copy_sub_off(&m->norm, &t->subs.norm);
5961#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005962 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005963 copy_sub_off(&m->synt, &t->subs.synt);
5964#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005965
Bram Moolenaar87953742013-06-05 18:52:40 +02005966 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005967 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005968 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005969 if (result == NFA_TOO_EXPENSIVE)
5970 {
5971 nfa_match = result;
5972 goto theend;
5973 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005974 if (result)
5975 {
5976 int bytelen;
5977
5978#ifdef ENABLE_LOG
5979 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5980 log_subsexpr(m);
5981#endif
5982 /* Copy submatch info from the recursive call */
5983 copy_sub_off(&t->subs.norm, &m->norm);
5984#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005985 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005986 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005987#endif
5988 /* Now we need to skip over the matched text and then
5989 * continue with what follows. */
5990 if (REG_MULTI)
5991 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005992 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02005993 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02005994 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005995 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02005996
5997#ifdef ENABLE_LOG
5998 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5999#endif
6000 if (bytelen == 0)
6001 {
6002 /* empty match, output of corresponding
6003 * NFA_END_PATTERN/NFA_SKIP to be used at current
6004 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006005 add_here = TRUE;
6006 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006007 }
6008 else if (bytelen <= clen)
6009 {
6010 /* match current character, output of corresponding
6011 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006012 add_state = t->state->out1->out->out;
6013 add_off = clen;
6014 }
6015 else
6016 {
6017 /* skip over the matched characters, set character
6018 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006019 add_state = t->state->out1->out;
6020 add_off = bytelen;
6021 add_count = bytelen - clen;
6022 }
6023 }
6024 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006025 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006027 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006028 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006029 {
6030 add_here = TRUE;
6031 add_state = t->state->out;
6032 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006033 break;
6034
6035 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006036 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006037 {
6038 add_here = TRUE;
6039 add_state = t->state->out;
6040 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006041 break;
6042
6043 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006044 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006045
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006046 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006047 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006048 else if (has_mbyte)
6049 {
6050 int this_class;
6051
6052 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006053 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006055 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006056 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006057 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006058 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006059 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006060 || (rex.input > rex.line
6061 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006062 result = FALSE;
6063 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006064 {
6065 add_here = TRUE;
6066 add_state = t->state->out;
6067 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006068 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006069
6070 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006071 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006072 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006073 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074 else if (has_mbyte)
6075 {
6076 int this_class, prev_class;
6077
6078 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006079 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006080 prev_class = reg_prev_class();
6081 if (this_class == prev_class
6082 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006083 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006084 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006085 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6086 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006087 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006088 result = FALSE;
6089 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006090 {
6091 add_here = TRUE;
6092 add_state = t->state->out;
6093 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006094 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006095
Bram Moolenaar4b780632013-05-31 22:14:52 +02006096 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006097 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006098 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006099 {
6100 add_here = TRUE;
6101 add_state = t->state->out;
6102 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006103 break;
6104
6105 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006106 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006107 {
6108 add_here = TRUE;
6109 add_state = t->state->out;
6110 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006111 break;
6112
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006113 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006114 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006115 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006116 int len = 0;
6117 nfa_state_T *end;
6118 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006119 int cchars[MAX_MCO];
6120 int ccount = 0;
6121 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006122
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006123 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006124 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006125 if (utf_iscomposing(sta->c))
6126 {
6127 /* Only match composing character(s), ignore base
6128 * character. Used for ".{composing}" and "{composing}"
6129 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006130 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006131 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006132 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006133 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006134 /* If \Z was present, then ignore composing characters.
6135 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006136 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006137 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006138 else
6139 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006140 while (sta->c != NFA_END_COMPOSING)
6141 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006142 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006143
6144 /* Check base character matches first, unless ignored. */
6145 else if (len > 0 || mc == sta->c)
6146 {
6147 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006148 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006149 len += mb_char2len(mc);
6150 sta = sta->out;
6151 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006152
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006153 /* We don't care about the order of composing characters.
6154 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006155 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006156 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006157 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006158 cchars[ccount++] = mc;
6159 len += mb_char2len(mc);
6160 if (ccount == MAX_MCO)
6161 break;
6162 }
6163
6164 /* Check that each composing char in the pattern matches a
6165 * composing char in the text. We do not check if all
6166 * composing chars are matched. */
6167 result = OK;
6168 while (sta->c != NFA_END_COMPOSING)
6169 {
6170 for (j = 0; j < ccount; ++j)
6171 if (cchars[j] == sta->c)
6172 break;
6173 if (j == ccount)
6174 {
6175 result = FAIL;
6176 break;
6177 }
6178 sta = sta->out;
6179 }
6180 }
6181 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006182 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006183
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006184 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006185 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006186 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006187 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188
6189 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006190 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006191 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006192 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006193 go_to_nextline = TRUE;
6194 /* Pass -1 for the offset, which means taking the position
6195 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006196 add_state = t->state->out;
6197 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006198 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006199 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006200 {
6201 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006202 add_state = t->state->out;
6203 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006204 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006205 break;
6206
Bram Moolenaar417bad22013-06-07 14:08:30 +02006207 case NFA_START_COLL:
6208 case NFA_START_NEG_COLL:
6209 {
6210 /* What follows is a list of characters, until NFA_END_COLL.
6211 * One of them must match or none of them must match. */
6212 nfa_state_T *state;
6213 int result_if_matched;
6214 int c1, c2;
6215
6216 /* Never match EOL. If it's part of the collection it is added
6217 * as a separate state with an OR. */
6218 if (curc == NUL)
6219 break;
6220
6221 state = t->state->out;
6222 result_if_matched = (t->state->c == NFA_START_COLL);
6223 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006224 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006225 if (state->c == NFA_END_COLL)
6226 {
6227 result = !result_if_matched;
6228 break;
6229 }
6230 if (state->c == NFA_RANGE_MIN)
6231 {
6232 c1 = state->val;
6233 state = state->out; /* advance to NFA_RANGE_MAX */
6234 c2 = state->val;
6235#ifdef ENABLE_LOG
6236 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6237 curc, c1, c2);
6238#endif
6239 if (curc >= c1 && curc <= c2)
6240 {
6241 result = result_if_matched;
6242 break;
6243 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006244 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006245 {
6246 int curc_low = MB_TOLOWER(curc);
6247 int done = FALSE;
6248
6249 for ( ; c1 <= c2; ++c1)
6250 if (MB_TOLOWER(c1) == curc_low)
6251 {
6252 result = result_if_matched;
6253 done = TRUE;
6254 break;
6255 }
6256 if (done)
6257 break;
6258 }
6259 }
6260 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006261 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006262 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006263 == MB_TOLOWER(state->c))))
6264 {
6265 result = result_if_matched;
6266 break;
6267 }
6268 state = state->out;
6269 }
6270 if (result)
6271 {
6272 /* next state is in out of the NFA_END_COLL, out1 of
6273 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006274 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006275 add_off = clen;
6276 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006277 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006278 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006279
6280 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006281 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006282 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006283 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006284 add_state = t->state->out;
6285 add_off = clen;
6286 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006287 break;
6288
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006289 case NFA_ANY_COMPOSING:
6290 /* On a composing character skip over it. Otherwise do
6291 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006292 if (enc_utf8 && utf_iscomposing(curc))
6293 {
6294 add_off = clen;
6295 }
6296 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006297 {
6298 add_here = TRUE;
6299 add_off = 0;
6300 }
6301 add_state = t->state->out;
6302 break;
6303
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006304 /*
6305 * Character classes like \a for alpha, \d for digit etc.
6306 */
6307 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006308 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006309 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006310 break;
6311
6312 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006313 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006314 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006315 break;
6316
6317 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006318 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006319 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006320 break;
6321
6322 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006323 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006324 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006325 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006326 break;
6327
6328 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006329 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006330 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006331 break;
6332
6333 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006334 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006335 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006336 break;
6337
6338 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006339 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006340 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006341 break;
6342
6343 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006344 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
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_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006349 result = VIM_ISWHITE(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_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006354 result = curc != NUL && !VIM_ISWHITE(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_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006359 result = ri_digit(curc);
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_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006364 result = curc != NUL && !ri_digit(curc);
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_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006369 result = ri_hex(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_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006374 result = curc != NUL && !ri_hex(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_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006379 result = ri_octal(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_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006384 result = curc != NUL && !ri_octal(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_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006389 result = ri_word(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_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006394 result = curc != NUL && !ri_word(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_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006399 result = ri_head(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_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006404 result = curc != NUL && !ri_head(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_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006409 result = ri_alpha(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_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = curc != NUL && !ri_alpha(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_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = ri_lower(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_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = curc != NUL && !ri_lower(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_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = ri_upper(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_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = curc != NUL && !ri_upper(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
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006438 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006439 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006440 ADD_STATE_IF_MATCH(t->state);
6441 break;
6442
6443 case NFA_NLOWER_IC: /* [^a-z] */
6444 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006445 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006446 ADD_STATE_IF_MATCH(t->state);
6447 break;
6448
6449 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006450 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006451 ADD_STATE_IF_MATCH(t->state);
6452 break;
6453
6454 case NFA_NUPPER_IC: /* ^[A-Z] */
6455 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006456 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006457 ADD_STATE_IF_MATCH(t->state);
6458 break;
6459
Bram Moolenaar5714b802013-05-28 22:03:20 +02006460 case NFA_BACKREF1:
6461 case NFA_BACKREF2:
6462 case NFA_BACKREF3:
6463 case NFA_BACKREF4:
6464 case NFA_BACKREF5:
6465 case NFA_BACKREF6:
6466 case NFA_BACKREF7:
6467 case NFA_BACKREF8:
6468 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006469#ifdef FEAT_SYN_HL
6470 case NFA_ZREF1:
6471 case NFA_ZREF2:
6472 case NFA_ZREF3:
6473 case NFA_ZREF4:
6474 case NFA_ZREF5:
6475 case NFA_ZREF6:
6476 case NFA_ZREF7:
6477 case NFA_ZREF8:
6478 case NFA_ZREF9:
6479#endif
6480 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006481 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006482 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006483 int bytelen;
6484
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006485 if (t->state->c <= NFA_BACKREF9)
6486 {
6487 subidx = t->state->c - NFA_BACKREF1 + 1;
6488 result = match_backref(&t->subs.norm, subidx, &bytelen);
6489 }
6490#ifdef FEAT_SYN_HL
6491 else
6492 {
6493 subidx = t->state->c - NFA_ZREF1 + 1;
6494 result = match_zref(subidx, &bytelen);
6495 }
6496#endif
6497
Bram Moolenaar5714b802013-05-28 22:03:20 +02006498 if (result)
6499 {
6500 if (bytelen == 0)
6501 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006502 /* empty match always works, output of NFA_SKIP to be
6503 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006504 add_here = TRUE;
6505 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006506 }
6507 else if (bytelen <= clen)
6508 {
6509 /* match current character, jump ahead to out of
6510 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006511 add_state = t->state->out->out;
6512 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006513 }
6514 else
6515 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006516 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006517 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006518 add_state = t->state->out;
6519 add_off = bytelen;
6520 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006521 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006522 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006523 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006524 }
6525 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006526 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006527 if (t->count - clen <= 0)
6528 {
6529 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006530 add_state = t->state->out;
6531 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006532 }
6533 else
6534 {
6535 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006536 add_state = t->state;
6537 add_off = 0;
6538 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006539 }
6540 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006541
Bram Moolenaar423532e2013-05-29 21:14:42 +02006542 case NFA_LNUM:
6543 case NFA_LNUM_GT:
6544 case NFA_LNUM_LT:
6545 result = (REG_MULTI &&
6546 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006547 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006548 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006549 {
6550 add_here = TRUE;
6551 add_state = t->state->out;
6552 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006553 break;
6554
6555 case NFA_COL:
6556 case NFA_COL_GT:
6557 case NFA_COL_LT:
6558 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006559 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006560 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006561 {
6562 add_here = TRUE;
6563 add_state = t->state->out;
6564 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006565 break;
6566
6567 case NFA_VCOL:
6568 case NFA_VCOL_GT:
6569 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006570 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006571 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006572 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006573 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006574
6575 /* Bail out quickly when there can't be a match, avoid the
6576 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006577 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006578 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006579 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006580 result = FALSE;
6581 if (op == 1 && col - 1 > t->state->val && col > 100)
6582 {
6583 int ts = wp->w_buffer->b_p_ts;
6584
6585 /* Guess that a character won't use more columns than
6586 * 'tabstop', with a minimum of 4. */
6587 if (ts < 4)
6588 ts = 4;
6589 result = col > t->state->val * ts;
6590 }
6591 if (!result)
6592 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006593 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006594 if (result)
6595 {
6596 add_here = TRUE;
6597 add_state = t->state->out;
6598 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006599 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006600 break;
6601
Bram Moolenaar044aa292013-06-04 21:27:38 +02006602 case NFA_MARK:
6603 case NFA_MARK_GT:
6604 case NFA_MARK_LT:
6605 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006606 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006607
6608 /* Compare the mark position to the match position. */
6609 result = (pos != NULL /* mark doesn't exist */
6610 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006611 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6612 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006613 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006614 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006615 ? t->state->c == NFA_MARK_GT
6616 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006617 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006618 ? t->state->c == NFA_MARK_GT
6619 : t->state->c == NFA_MARK_LT)));
6620 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006621 {
6622 add_here = TRUE;
6623 add_state = t->state->out;
6624 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006625 break;
6626 }
6627
Bram Moolenaar423532e2013-05-29 21:14:42 +02006628 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006629 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006630 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006631 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006632 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006633 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006634 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006635 {
6636 add_here = TRUE;
6637 add_state = t->state->out;
6638 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006639 break;
6640
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006641 case NFA_VISUAL:
6642 result = reg_match_visual();
6643 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006644 {
6645 add_here = TRUE;
6646 add_state = t->state->out;
6647 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006648 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006649
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006650 case NFA_MOPEN1:
6651 case NFA_MOPEN2:
6652 case NFA_MOPEN3:
6653 case NFA_MOPEN4:
6654 case NFA_MOPEN5:
6655 case NFA_MOPEN6:
6656 case NFA_MOPEN7:
6657 case NFA_MOPEN8:
6658 case NFA_MOPEN9:
6659#ifdef FEAT_SYN_HL
6660 case NFA_ZOPEN:
6661 case NFA_ZOPEN1:
6662 case NFA_ZOPEN2:
6663 case NFA_ZOPEN3:
6664 case NFA_ZOPEN4:
6665 case NFA_ZOPEN5:
6666 case NFA_ZOPEN6:
6667 case NFA_ZOPEN7:
6668 case NFA_ZOPEN8:
6669 case NFA_ZOPEN9:
6670#endif
6671 case NFA_NOPEN:
6672 case NFA_ZSTART:
6673 /* These states are only added to be able to bail out when
6674 * they are added again, nothing is to be done. */
6675 break;
6676
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006677 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006678 {
6679 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006680
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006681#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006682 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006683 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006684#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006685 result = (c == curc);
6686
Bram Moolenaar6100d022016-10-02 16:51:57 +02006687 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006688 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006689 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006690 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006691 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006692 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006693 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006694 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006695 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006696
6697 } /* switch (t->state->c) */
6698
6699 if (add_state != NULL)
6700 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006701 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006702 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006703
6704 if (t->pim.result == NFA_PIM_UNUSED)
6705 pim = NULL;
6706 else
6707 pim = &t->pim;
6708
6709 /* Handle the postponed invisible match if the match might end
6710 * without advancing and before the end of the line. */
6711 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006712 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006713 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006714 {
6715#ifdef ENABLE_LOG
6716 fprintf(log_fd, "\n");
6717 fprintf(log_fd, "==================================\n");
6718 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6719 fprintf(log_fd, "\n");
6720#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006721 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006722 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006723 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006724 /* for \@! and \@<! it is a match when the result is
6725 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006726 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006727 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6728 || pim->state->c
6729 == NFA_START_INVISIBLE_BEFORE_NEG
6730 || pim->state->c
6731 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006732 {
6733 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006734 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006735#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006736 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006737 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006738#endif
6739 }
6740 }
6741 else
6742 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006743 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006744#ifdef ENABLE_LOG
6745 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006746 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006747 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6748 fprintf(log_fd, "\n");
6749#endif
6750 }
6751
Bram Moolenaardecd9542013-06-07 16:31:50 +02006752 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006753 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006754 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6755 || pim->state->c
6756 == NFA_START_INVISIBLE_BEFORE_NEG
6757 || pim->state->c
6758 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006759 {
6760 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006761 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006762#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006763 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006764 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006765#endif
6766 }
6767 else
6768 /* look-behind match failed, don't add the state */
6769 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006770
6771 /* Postponed invisible match was handled, don't add it to
6772 * following states. */
6773 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006774 }
6775
Bram Moolenaara951e352013-10-06 15:46:11 +02006776 /* If "pim" points into l->t it will become invalid when
6777 * adding the state causes the list to be reallocated. Make a
6778 * local copy to avoid that. */
6779 if (pim == &t->pim)
6780 {
6781 copy_pim(&pim_copy, pim);
6782 pim = &pim_copy;
6783 }
6784
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006785 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006786 r = addstate_here(thislist, add_state, &t->subs,
6787 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006788 else
6789 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006790 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006791 if (add_count > 0)
6792 nextlist->t[nextlist->n - 1].count = add_count;
6793 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006794 if (r == NULL)
6795 {
6796 nfa_match = NFA_TOO_EXPENSIVE;
6797 goto theend;
6798 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006799 }
6800
6801 } /* for (thislist = thislist; thislist->state; thislist++) */
6802
Bram Moolenaare23febd2013-05-26 18:40:14 +02006803 /* Look for the start of a match in the current position by adding the
6804 * start state to the list of states.
6805 * The first found match is the leftmost one, thus the order of states
6806 * matters!
6807 * Do not add the start state in recursive calls of nfa_regmatch(),
6808 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006809 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006810 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006811 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006812 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006813 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006814 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006815 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006816 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006817 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006818 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006819 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6820 || (rex.lnum == nfa_endp->se_u.pos.lnum
6821 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006822 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006823 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006824 {
6825#ifdef ENABLE_LOG
6826 fprintf(log_fd, "(---) STARTSTATE\n");
6827#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006828 /* Inline optimized code for addstate() if we know the state is
6829 * the first MOPEN. */
6830 if (toplevel)
6831 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006832 int add = TRUE;
6833 int c;
6834
6835 if (prog->regstart != NUL && clen != 0)
6836 {
6837 if (nextlist->n == 0)
6838 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006839 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006840
6841 /* Nextlist is empty, we can skip ahead to the
6842 * character that must appear at the start. */
6843 if (skip_to_start(prog->regstart, &col) == FAIL)
6844 break;
6845#ifdef ENABLE_LOG
6846 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006847 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006848#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006849 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006850 }
6851 else
6852 {
6853 /* Checking if the required start character matches is
6854 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006855 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006856 if (c != prog->regstart && (!rex.reg_ic
6857 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006858 {
6859#ifdef ENABLE_LOG
6860 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6861#endif
6862 add = FALSE;
6863 }
6864 }
6865 }
6866
6867 if (add)
6868 {
6869 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006870 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006871 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006872 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006873 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006874 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6875 {
6876 nfa_match = NFA_TOO_EXPENSIVE;
6877 goto theend;
6878 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006879 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006880 }
6881 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006882 {
6883 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6884 {
6885 nfa_match = NFA_TOO_EXPENSIVE;
6886 goto theend;
6887 }
6888 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006889 }
6890
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006891#ifdef ENABLE_LOG
6892 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006893 {
6894 int i;
6895
6896 for (i = 0; i < thislist->n; i++)
6897 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6898 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006899 fprintf(log_fd, "\n");
6900#endif
6901
6902nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006903 /* Advance to the next character, or advance to the next line, or
6904 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006905 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006906 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006907 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006908 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006909 reg_nextline();
6910 else
6911 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006912
6913 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006914 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006915 if (got_int)
6916 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006917#ifdef FEAT_RELTIME
6918 /* Check for timeout once in a twenty times to avoid overhead. */
6919 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6920 {
6921 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006922 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006923 break;
6924 }
6925#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006926 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006927
6928#ifdef ENABLE_LOG
6929 if (log_fd != stderr)
6930 fclose(log_fd);
6931 log_fd = NULL;
6932#endif
6933
6934theend:
6935 /* Free memory */
6936 vim_free(list[0].t);
6937 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006938 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006939#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006940#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006941 fclose(debug);
6942#endif
6943
Bram Moolenaar963fee22013-05-26 21:47:28 +02006944 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006945}
6946
6947/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006948 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006949 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006950 */
6951 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006952nfa_regtry(
6953 nfa_regprog_T *prog,
6954 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006955 proftime_T *tm UNUSED, /* timeout limit or NULL */
6956 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006957{
6958 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006959 regsubs_T subs, m;
6960 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006961 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006962#ifdef ENABLE_LOG
6963 FILE *f;
6964#endif
6965
Bram Moolenaar0270f382018-07-17 05:43:58 +02006966 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006967#ifdef FEAT_RELTIME
6968 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006969 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006970 nfa_time_count = 0;
6971#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972
6973#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006974 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006975 if (f != NULL)
6976 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006977 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006978#ifdef DEBUG
6979 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6980#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006981 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006982 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006983 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006984 fprintf(f, "\n\n");
6985 fclose(f);
6986 }
6987 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006988 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006989#endif
6990
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006991 clear_sub(&subs.norm);
6992 clear_sub(&m.norm);
6993#ifdef FEAT_SYN_HL
6994 clear_sub(&subs.synt);
6995 clear_sub(&m.synt);
6996#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997
Bram Moolenaarfda37292014-11-05 14:27:36 +01006998 result = nfa_regmatch(prog, start, &subs, &m);
6999 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007001 else if (result == NFA_TOO_EXPENSIVE)
7002 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007003
7004 cleanup_subexpr();
7005 if (REG_MULTI)
7006 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007007 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007008 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007009 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7010 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007011
Bram Moolenaar6100d022016-10-02 16:51:57 +02007012 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7013 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 }
7015
Bram Moolenaar6100d022016-10-02 16:51:57 +02007016 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007018 rex.reg_startpos[0].lnum = 0;
7019 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007021 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007023 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007024 rex.reg_endpos[0].lnum = rex.lnum;
7025 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007026 }
7027 else
7028 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007029 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030 }
7031 else
7032 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007033 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007034 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007035 rex.reg_startp[i] = subs.norm.list.line[i].start;
7036 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007037 }
7038
Bram Moolenaar6100d022016-10-02 16:51:57 +02007039 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007040 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007041 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007042 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007043 }
7044
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007045#ifdef FEAT_SYN_HL
7046 /* Package any found \z(...\) matches for export. Default is none. */
7047 unref_extmatch(re_extmatch_out);
7048 re_extmatch_out = NULL;
7049
7050 if (prog->reghasz == REX_SET)
7051 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007052 cleanup_zsubexpr();
7053 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007054 /* Loop over \z1, \z2, etc. There is no \z0. */
7055 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007056 {
7057 if (REG_MULTI)
7058 {
7059 struct multipos *mpos = &subs.synt.list.multi[i];
7060
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007061 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007062 if (mpos->start_lnum >= 0
7063 && mpos->start_lnum == mpos->end_lnum
7064 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007065 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007066 vim_strnsave(reg_getline(mpos->start_lnum)
7067 + mpos->start_col,
7068 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007069 }
7070 else
7071 {
7072 struct linepos *lpos = &subs.synt.list.line[i];
7073
7074 if (lpos->start != NULL && lpos->end != NULL)
7075 re_extmatch_out->matches[i] =
7076 vim_strnsave(lpos->start,
7077 (int)(lpos->end - lpos->start));
7078 }
7079 }
7080 }
7081#endif
7082
Bram Moolenaar0270f382018-07-17 05:43:58 +02007083 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007084}
7085
7086/*
7087 * Match a regexp against a string ("line" points to the string) or multiple
7088 * lines ("line" is NULL, use reg_getline()).
7089 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007090 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007091 */
7092 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007093nfa_regexec_both(
7094 char_u *line,
7095 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007096 proftime_T *tm, /* timeout limit or NULL */
7097 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007098{
7099 nfa_regprog_T *prog;
7100 long retval = 0L;
7101 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007102 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007103
7104 if (REG_MULTI)
7105 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007106 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007107 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007108 rex.reg_startpos = rex.reg_mmatch->startpos;
7109 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007110 }
7111 else
7112 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007113 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7114 rex.reg_startp = rex.reg_match->startp;
7115 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007116 }
7117
7118 /* Be paranoid... */
7119 if (prog == NULL || line == NULL)
7120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007121 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007122 goto theend;
7123 }
7124
Bram Moolenaar6100d022016-10-02 16:51:57 +02007125 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007126 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007127 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007128 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007129 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007130
Bram Moolenaar6100d022016-10-02 16:51:57 +02007131 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007133 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007134
Bram Moolenaar0270f382018-07-17 05:43:58 +02007135 rex.line = line;
7136 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007137
Bram Moolenaar0270f382018-07-17 05:43:58 +02007138 rex.nfa_has_zend = prog->has_zend;
7139 rex.nfa_has_backref = prog->has_backref;
7140 rex.nfa_nsubexpr = prog->nsubexp;
7141 rex.nfa_listid = 1;
7142 rex.nfa_alt_listid = 2;
7143#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007144 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007145#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007146
Bram Moolenaard89616e2013-06-06 18:46:06 +02007147 if (prog->reganch && col > 0)
7148 return 0L;
7149
Bram Moolenaar0270f382018-07-17 05:43:58 +02007150 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007151#ifdef FEAT_SYN_HL
7152 /* Clear the external match subpointers if necessary. */
7153 if (prog->reghasz == REX_SET)
7154 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007155 rex.nfa_has_zsubexpr = TRUE;
7156 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007157 }
7158 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007159 {
7160 rex.nfa_has_zsubexpr = FALSE;
7161 rex.need_clear_zsubexpr = FALSE;
7162 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007163#endif
7164
Bram Moolenaard89616e2013-06-06 18:46:06 +02007165 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007166 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007167 /* Skip ahead until a character we know the match must start with.
7168 * When there is none there is no match. */
7169 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007170 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007171
Bram Moolenaar473de612013-06-08 18:19:48 +02007172 /* If match_text is set it contains the full text that must match.
7173 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007174 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007175 return find_match_text(col, prog->regstart, prog->match_text);
7176 }
7177
Bram Moolenaard89616e2013-06-06 18:46:06 +02007178 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007179 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007180 goto theend;
7181
Bram Moolenaar0270f382018-07-17 05:43:58 +02007182 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7183 // it's accidentally used during execution.
7184 nstate = 0;
7185 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007186 {
7187 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007188 prog->state[i].lastlist[0] = 0;
7189 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007190 }
7191
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007192 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007193
Bram Moolenaar0270f382018-07-17 05:43:58 +02007194#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007195 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007196#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007197
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007198theend:
7199 return retval;
7200}
7201
7202/*
7203 * Compile a regular expression into internal code for the NFA matcher.
7204 * Returns the program in allocated space. Returns NULL for an error.
7205 */
7206 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007207nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007209 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007210 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007211 int *postfix;
7212
7213 if (expr == NULL)
7214 return NULL;
7215
Bram Moolenaar0270f382018-07-17 05:43:58 +02007216#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007217 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007218#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007219 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007220
7221 init_class_tab();
7222
7223 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7224 return NULL;
7225
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007226 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007227 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228 postfix = re2post();
7229 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007230 {
7231 /* TODO: only give this error for debugging? */
7232 if (post_ptr >= post_end)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007233 siemsg("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007234 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007235 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007236
7237 /*
7238 * In order to build the NFA, we parse the input regexp twice:
7239 * 1. first pass to count size (so we can allocate space)
7240 * 2. second to emit code
7241 */
7242#ifdef ENABLE_LOG
7243 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007244 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007245
7246 if (f != NULL)
7247 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007248 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007249 fclose(f);
7250 }
7251 }
7252#endif
7253
7254 /*
7255 * PASS 1
7256 * Count number of NFA states in "nstate". Do not build the NFA.
7257 */
7258 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007259
Bram Moolenaar16619a22013-06-11 18:42:36 +02007260 /* allocate the regprog with space for the compiled regexp */
7261 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007262 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7263 if (prog == NULL)
7264 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007265 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007266 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007267
7268 /*
7269 * PASS 2
7270 * Build the NFA
7271 */
7272 prog->start = post2nfa(postfix, post_ptr, FALSE);
7273 if (prog->start == NULL)
7274 goto fail;
7275
7276 prog->regflags = regflags;
7277 prog->engine = &nfa_regengine;
7278 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007279 prog->has_zend = rex.nfa_has_zend;
7280 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007281 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007282
Bram Moolenaara2947e22013-06-11 22:44:09 +02007283 nfa_postprocess(prog);
7284
Bram Moolenaard89616e2013-06-06 18:46:06 +02007285 prog->reganch = nfa_get_reganch(prog->start, 0);
7286 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007287 prog->match_text = nfa_get_match_text(prog->start);
7288
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007289#ifdef ENABLE_LOG
7290 nfa_postfix_dump(expr, OK);
7291 nfa_dump(prog);
7292#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007293#ifdef FEAT_SYN_HL
7294 /* Remember whether this pattern has any \z specials in it. */
7295 prog->reghasz = re_has_z;
7296#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007297 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007298#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007299 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007300#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007301
7302out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007303 VIM_CLEAR(post_start);
7304 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007305 state_ptr = NULL;
7306 return (regprog_T *)prog;
7307
7308fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007309 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007310#ifdef ENABLE_LOG
7311 nfa_postfix_dump(expr, FAIL);
7312#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007313#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007314 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007315#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007316 goto out;
7317}
7318
Bram Moolenaar473de612013-06-08 18:19:48 +02007319/*
7320 * Free a compiled regexp program, returned by nfa_regcomp().
7321 */
7322 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007323nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007324{
7325 if (prog != NULL)
7326 {
7327 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007328 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007329 vim_free(prog);
7330 }
7331}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007332
7333/*
7334 * Match a regexp against a string.
7335 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7336 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007337 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007338 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007339 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007340 */
7341 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007342nfa_regexec_nl(
7343 regmatch_T *rmp,
7344 char_u *line, /* string to match against */
7345 colnr_T col, /* column to start looking for match */
7346 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007347{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007348 rex.reg_match = rmp;
7349 rex.reg_mmatch = NULL;
7350 rex.reg_maxline = 0;
7351 rex.reg_line_lbr = line_lbr;
7352 rex.reg_buf = curbuf;
7353 rex.reg_win = NULL;
7354 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007355 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007356 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007357 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007358}
7359
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007360
7361/*
7362 * Match a regexp against multiple lines.
7363 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7364 * Uses curbuf for line count and 'iskeyword'.
7365 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007366 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007367 * match otherwise.
7368 *
7369 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7370 *
7371 * ! Also NOTE : match may actually be in another line. e.g.:
7372 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7373 *
7374 * +-------------------------+
7375 * |a |
7376 * |b |
7377 * |c |
7378 * | |
7379 * +-------------------------+
7380 *
7381 * then nfa_regexec_multi() returns 3. while the original
7382 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7383 *
7384 * FIXME if this behavior is not compatible.
7385 */
7386 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007387nfa_regexec_multi(
7388 regmmatch_T *rmp,
7389 win_T *win, /* window in which to search or NULL */
7390 buf_T *buf, /* buffer in which to search */
7391 linenr_T lnum, /* nr of line to start looking for match */
7392 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007393 proftime_T *tm, /* timeout limit or NULL */
7394 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007395{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007396 rex.reg_match = NULL;
7397 rex.reg_mmatch = rmp;
7398 rex.reg_buf = buf;
7399 rex.reg_win = win;
7400 rex.reg_firstlnum = lnum;
7401 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7402 rex.reg_line_lbr = FALSE;
7403 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007404 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007405 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007406
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007407 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007408}
7409
7410#ifdef DEBUG
7411# undef ENABLE_LOG
7412#endif