blob: 2a16fff94a2d60aac905c6d1f8fb5ba43e058817 [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 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004452 int newlen = l->len * 3 / 2 + 50;
4453 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004454
Bram Moolenaard05bf562013-06-30 23:24:08 +02004455 if (subs != &temp_subs)
4456 {
4457 /* "subs" may point into the current array, need to make a
4458 * copy before it becomes invalid. */
4459 copy_sub(&temp_subs.norm, &subs->norm);
4460#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004461 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004462 copy_sub(&temp_subs.synt, &subs->synt);
4463#endif
4464 subs = &temp_subs;
4465 }
4466
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004467 newt = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4468 if (newt == NULL)
4469 {
4470 // out of memory
4471 --depth;
4472 return NULL;
4473 }
4474 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004475 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004476 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004477
4478 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004479 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004480 thread = &l->t[l->n++];
4481 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004482 if (pim == NULL)
4483 thread->pim.result = NFA_PIM_UNUSED;
4484 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004485 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004486 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004487 l->has_pim = TRUE;
4488 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004489 copy_sub(&thread->subs.norm, &subs->norm);
4490#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004491 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004492 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004493#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004494#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004495 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004496 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004497#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498 }
4499
4500#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004501 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004502 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503#endif
4504 switch (state->c)
4505 {
4506 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004507 break;
4508
4509 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004510 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004511 subs = addstate(l, state->out, subs, pim, off_arg);
4512 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004513 break;
4514
Bram Moolenaar699c1202013-09-25 16:41:54 +02004515 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004516 case NFA_NOPEN:
4517 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004518 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 break;
4520
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004521 case NFA_MOPEN:
4522 case NFA_MOPEN1:
4523 case NFA_MOPEN2:
4524 case NFA_MOPEN3:
4525 case NFA_MOPEN4:
4526 case NFA_MOPEN5:
4527 case NFA_MOPEN6:
4528 case NFA_MOPEN7:
4529 case NFA_MOPEN8:
4530 case NFA_MOPEN9:
4531#ifdef FEAT_SYN_HL
4532 case NFA_ZOPEN:
4533 case NFA_ZOPEN1:
4534 case NFA_ZOPEN2:
4535 case NFA_ZOPEN3:
4536 case NFA_ZOPEN4:
4537 case NFA_ZOPEN5:
4538 case NFA_ZOPEN6:
4539 case NFA_ZOPEN7:
4540 case NFA_ZOPEN8:
4541 case NFA_ZOPEN9:
4542#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004543 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004545 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004546 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004547 sub = &subs->norm;
4548 }
4549#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004550 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004551 {
4552 subidx = state->c - NFA_ZOPEN;
4553 sub = &subs->synt;
4554 }
4555#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004556 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004557 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004558 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004559 sub = &subs->norm;
4560 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004562 /* avoid compiler warnings */
4563 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004564 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004565
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004566 /* Set the position (with "off" added) in the subexpression. Save
4567 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004568 if (REG_MULTI)
4569 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004570 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004571 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004572 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004573 save_in_use = -1;
4574 }
4575 else
4576 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004577 save_in_use = sub->in_use;
4578 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004579 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004580 sub->list.multi[i].start_lnum = -1;
4581 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004582 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004583 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004584 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004585 if (off == -1)
4586 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004587 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004588 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004589 }
4590 else
4591 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004592 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004593 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004594 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004595 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004596 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004597 }
4598 else
4599 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004600 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004601 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004602 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004603 save_in_use = -1;
4604 }
4605 else
4606 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004607 save_in_use = sub->in_use;
4608 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004609 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004610 sub->list.line[i].start = NULL;
4611 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004612 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004613 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004614 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004615 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004616 }
4617
Bram Moolenaar16b35782016-09-09 20:29:50 +02004618 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004619 if (subs == NULL)
4620 break;
4621 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004622#ifdef FEAT_SYN_HL
4623 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4624 sub = &subs->synt;
4625 else
4626#endif
4627 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004628
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004629 if (save_in_use == -1)
4630 {
4631 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004632 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004633 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004634 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004635 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004637 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 break;
4639
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004640 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004641 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004642 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004643 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004644 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004645 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004646 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647 break;
4648 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004649 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004650 case NFA_MCLOSE1:
4651 case NFA_MCLOSE2:
4652 case NFA_MCLOSE3:
4653 case NFA_MCLOSE4:
4654 case NFA_MCLOSE5:
4655 case NFA_MCLOSE6:
4656 case NFA_MCLOSE7:
4657 case NFA_MCLOSE8:
4658 case NFA_MCLOSE9:
4659#ifdef FEAT_SYN_HL
4660 case NFA_ZCLOSE:
4661 case NFA_ZCLOSE1:
4662 case NFA_ZCLOSE2:
4663 case NFA_ZCLOSE3:
4664 case NFA_ZCLOSE4:
4665 case NFA_ZCLOSE5:
4666 case NFA_ZCLOSE6:
4667 case NFA_ZCLOSE7:
4668 case NFA_ZCLOSE8:
4669 case NFA_ZCLOSE9:
4670#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004671 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004672 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004673 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004674 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004675 sub = &subs->norm;
4676 }
4677#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004678 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004679 {
4680 subidx = state->c - NFA_ZCLOSE;
4681 sub = &subs->synt;
4682 }
4683#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004684 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004685 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004686 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004687 sub = &subs->norm;
4688 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004689
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004690 /* We don't fill in gaps here, there must have been an MOPEN that
4691 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004692 save_in_use = sub->in_use;
4693 if (sub->in_use <= subidx)
4694 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 if (REG_MULTI)
4696 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004697 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004698 if (off == -1)
4699 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004700 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004701 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004702 }
4703 else
4704 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004705 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004706 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004707 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004708 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004709 /* avoid compiler warnings */
4710 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 }
4712 else
4713 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004714 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004715 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004716 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004717 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004718 }
4719
Bram Moolenaar16b35782016-09-09 20:29:50 +02004720 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004721 if (subs == NULL)
4722 break;
Bram Moolenaarebefd992013-08-14 14:18:40 +02004723 /* "subs" may have changed, need to set "sub" again */
4724#ifdef FEAT_SYN_HL
4725 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4726 sub = &subs->synt;
4727 else
4728#endif
4729 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730
4731 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004732 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004734 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004735 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 break;
4737 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004738 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004739 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004740}
4741
4742/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004743 * Like addstate(), but the new state(s) are put at position "*ip".
4744 * Used for zero-width matches, next state to use is the added one.
4745 * This makes sure the order of states to be tried does not change, which
4746 * matters for alternatives.
4747 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004748 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004749addstate_here(
4750 nfa_list_T *l, /* runtime state list */
4751 nfa_state_T *state, /* state to update */
4752 regsubs_T *subs, /* pointers to subexpressions */
4753 nfa_pim_T *pim, /* postponed look-behind match */
4754 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004755{
4756 int tlen = l->n;
4757 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004758 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004759 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004760
Bram Moolenaar16b35782016-09-09 20:29:50 +02004761 /* First add the state(s) at the end, so that we know how many there are.
4762 * Pass the listidx as offset (avoids adding another argument to
4763 * addstate(). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004764 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4765 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004766 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004767
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004768 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004769 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004770 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004771
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004772 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004773 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004774 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004775 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004776 if (count == 1)
4777 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004778 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004779 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004780 }
4781 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004782 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004783 if (l->n + count - 1 >= l->len)
4784 {
4785 /* not enough space to move the new states, reallocate the list
4786 * and move the states to the right position */
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004787 int newlen = l->len * 3 / 2 + 50;
4788 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004789
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004790 newl = (nfa_thread_T *)alloc(newlen * sizeof(nfa_thread_T));
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004791 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004792 return NULL;
4793 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004794 mch_memmove(&(newl[0]),
4795 &(l->t[0]),
4796 sizeof(nfa_thread_T) * listidx);
4797 mch_memmove(&(newl[listidx]),
4798 &(l->t[l->n - count]),
4799 sizeof(nfa_thread_T) * count);
4800 mch_memmove(&(newl[listidx + count]),
4801 &(l->t[listidx + 1]),
4802 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4803 vim_free(l->t);
4804 l->t = newl;
4805 }
4806 else
4807 {
4808 /* make space for new states, then move them from the
4809 * end to the current position */
4810 mch_memmove(&(l->t[listidx + count]),
4811 &(l->t[listidx + 1]),
4812 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4813 mch_memmove(&(l->t[listidx]),
4814 &(l->t[l->n - 1]),
4815 sizeof(nfa_thread_T) * count);
4816 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004817 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004818 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004819 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004820
4821 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004822}
4823
4824/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004825 * Check character class "class" against current character c.
4826 */
4827 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004828check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004829{
4830 switch (class)
4831 {
4832 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004833 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004834 return OK;
4835 break;
4836 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004837 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004838 return OK;
4839 break;
4840 case NFA_CLASS_BLANK:
4841 if (c == ' ' || c == '\t')
4842 return OK;
4843 break;
4844 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004845 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004846 return OK;
4847 break;
4848 case NFA_CLASS_DIGIT:
4849 if (VIM_ISDIGIT(c))
4850 return OK;
4851 break;
4852 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004853 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004854 return OK;
4855 break;
4856 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004857 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858 return OK;
4859 break;
4860 case NFA_CLASS_PRINT:
4861 if (vim_isprintc(c))
4862 return OK;
4863 break;
4864 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004865 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004866 return OK;
4867 break;
4868 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004869 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870 return OK;
4871 break;
4872 case NFA_CLASS_UPPER:
4873 if (MB_ISUPPER(c))
4874 return OK;
4875 break;
4876 case NFA_CLASS_XDIGIT:
4877 if (vim_isxdigit(c))
4878 return OK;
4879 break;
4880 case NFA_CLASS_TAB:
4881 if (c == '\t')
4882 return OK;
4883 break;
4884 case NFA_CLASS_RETURN:
4885 if (c == '\r')
4886 return OK;
4887 break;
4888 case NFA_CLASS_BACKSPACE:
4889 if (c == '\b')
4890 return OK;
4891 break;
4892 case NFA_CLASS_ESCAPE:
4893 if (c == '\033')
4894 return OK;
4895 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004896 case NFA_CLASS_IDENT:
4897 if (vim_isIDc(c))
4898 return OK;
4899 break;
4900 case NFA_CLASS_KEYWORD:
4901 if (reg_iswordc(c))
4902 return OK;
4903 break;
4904 case NFA_CLASS_FNAME:
4905 if (vim_isfilec(c))
4906 return OK;
4907 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908
4909 default:
4910 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004911 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004912 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004913 }
4914 return FAIL;
4915}
4916
Bram Moolenaar5714b802013-05-28 22:03:20 +02004917/*
4918 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004919 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004920 */
4921 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004922match_backref(
4923 regsub_T *sub, /* pointers to subexpressions */
4924 int subidx,
4925 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004926{
4927 int len;
4928
4929 if (sub->in_use <= subidx)
4930 {
4931retempty:
4932 /* backref was not set, match an empty string */
4933 *bytelen = 0;
4934 return TRUE;
4935 }
4936
4937 if (REG_MULTI)
4938 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004939 if (sub->list.multi[subidx].start_lnum < 0
4940 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004941 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004942 if (sub->list.multi[subidx].start_lnum == rex.lnum
4943 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004944 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004945 len = sub->list.multi[subidx].end_col
4946 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004947 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4948 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004949 {
4950 *bytelen = len;
4951 return TRUE;
4952 }
4953 }
4954 else
4955 {
4956 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004957 sub->list.multi[subidx].start_lnum,
4958 sub->list.multi[subidx].start_col,
4959 sub->list.multi[subidx].end_lnum,
4960 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004961 bytelen) == RA_MATCH)
4962 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004963 }
4964 }
4965 else
4966 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004967 if (sub->list.line[subidx].start == NULL
4968 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004969 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004970 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004971 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004972 {
4973 *bytelen = len;
4974 return TRUE;
4975 }
4976 }
4977 return FALSE;
4978}
4979
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004980#ifdef FEAT_SYN_HL
4981
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004982/*
4983 * Check for a match with \z subexpression "subidx".
4984 * Return TRUE if it matches.
4985 */
4986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004987match_zref(
4988 int subidx,
4989 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004990{
4991 int len;
4992
4993 cleanup_zsubexpr();
4994 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4995 {
4996 /* backref was not set, match an empty string */
4997 *bytelen = 0;
4998 return TRUE;
4999 }
5000
5001 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005002 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005003 {
5004 *bytelen = len;
5005 return TRUE;
5006 }
5007 return FALSE;
5008}
5009#endif
5010
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005011/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005012 * Save list IDs for all NFA states of "prog" into "list".
5013 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005014 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015 */
5016 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005017nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005018{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005019 int i;
5020 nfa_state_T *p;
5021
5022 /* Order in the list is reverse, it's a bit faster that way. */
5023 p = &prog->state[0];
5024 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005025 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005026 list[i] = p->lastlist[1];
5027 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005028 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029 }
5030}
5031
5032/*
5033 * Restore list IDs from "list" to all NFA states.
5034 */
5035 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005036nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005037{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005038 int i;
5039 nfa_state_T *p;
5040
5041 p = &prog->state[0];
5042 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005044 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005045 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005046 }
5047}
5048
Bram Moolenaar423532e2013-05-29 21:14:42 +02005049 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005050nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005051{
5052 if (op == 1) return pos > val;
5053 if (op == 2) return pos < val;
5054 return val == pos;
5055}
5056
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005057static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005058
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005059/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005060 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005061 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5062 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005063 */
5064 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005065recursive_regmatch(
5066 nfa_state_T *state,
5067 nfa_pim_T *pim,
5068 nfa_regprog_T *prog,
5069 regsubs_T *submatch,
5070 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005071 int **listids,
5072 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005073{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005074 int save_reginput_col = (int)(rex.input - rex.line);
5075 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005076 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005077 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005078 save_se_T *save_nfa_endp = nfa_endp;
5079 save_se_T endpos;
5080 save_se_T *endposp = NULL;
5081 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005082 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005083
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005084 if (pim != NULL)
5085 {
5086 /* start at the position where the postponed match was */
5087 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005088 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005089 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005090 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005091 }
5092
Bram Moolenaardecd9542013-06-07 16:31:50 +02005093 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005094 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5095 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5096 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005098 /* The recursive match must end at the current position. When "pim" is
5099 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005100 endposp = &endpos;
5101 if (REG_MULTI)
5102 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005103 if (pim == NULL)
5104 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005105 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5106 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005107 }
5108 else
5109 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005110 }
5111 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005112 {
5113 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005114 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005115 else
5116 endpos.se_u.ptr = pim->end.ptr;
5117 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005118
5119 /* Go back the specified number of bytes, or as far as the
5120 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005121 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005122 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005123 if (state->val <= 0)
5124 {
5125 if (REG_MULTI)
5126 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005127 rex.line = reg_getline(--rex.lnum);
5128 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005130 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005131 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005132 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005133 }
5134 else
5135 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005136 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005137 {
5138 /* Not enough bytes in this line, go to end of
5139 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005140 rex.line = reg_getline(--rex.lnum);
5141 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005142 {
5143 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005144 rex.line = reg_getline(++rex.lnum);
5145 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005146 }
5147 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005148 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005149 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005150 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005152 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005153 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005154 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005155 }
5156 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005157 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005158 }
5159 }
5160
Bram Moolenaarf46da702013-06-02 22:37:42 +02005161#ifdef ENABLE_LOG
5162 if (log_fd != stderr)
5163 fclose(log_fd);
5164 log_fd = NULL;
5165#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005166 /* Have to clear the lastlist field of the NFA nodes, so that
5167 * nfa_regmatch() and addstate() can run properly after recursion. */
5168 if (nfa_ll_index == 1)
5169 {
5170 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5171 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005172 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005173 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005174 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005175 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005176 if (*listids == NULL)
5177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005178 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005179 return 0;
5180 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005181 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005182 }
5183 nfa_save_listids(prog, *listids);
5184 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005185 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005186 }
5187 else
5188 {
5189 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005190 * entry. Make sure rex.nfa_listid is different from a previous
5191 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005192 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005193 if (rex.nfa_listid <= rex.nfa_alt_listid)
5194 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005195 }
5196
5197 /* Call nfa_regmatch() to check if the current concat matches at this
5198 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005199 nfa_endp = endposp;
5200 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005201
5202 if (need_restore)
5203 nfa_restore_listids(prog, *listids);
5204 else
5205 {
5206 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005207 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005208 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005209
5210 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005211 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005212 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005213 rex.line = reg_getline(rex.lnum);
5214 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005215 if (result != NFA_TOO_EXPENSIVE)
5216 {
5217 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005218 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005219 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005220 nfa_endp = save_nfa_endp;
5221
5222#ifdef ENABLE_LOG
5223 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5224 if (log_fd != NULL)
5225 {
5226 fprintf(log_fd, "****************************\n");
5227 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5228 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5229 fprintf(log_fd, "****************************\n");
5230 }
5231 else
5232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005233 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005234 log_fd = stderr;
5235 }
5236#endif
5237
5238 return result;
5239}
5240
Bram Moolenaara2d95102013-06-04 14:23:05 +02005241/*
5242 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005243 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005244 * NFA_ANY: 1
5245 * specific character: 99
5246 */
5247 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005248failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005249{
5250 int c = state->c;
5251 int l, r;
5252
5253 /* detect looping */
5254 if (depth > 4)
5255 return 1;
5256
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005257 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005258 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005259 case NFA_SPLIT:
5260 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5261 /* avoid recursive stuff */
5262 return 1;
5263 /* two alternatives, use the lowest failure chance */
5264 l = failure_chance(state->out, depth + 1);
5265 r = failure_chance(state->out1, depth + 1);
5266 return l < r ? l : r;
5267
5268 case NFA_ANY:
5269 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005270 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005271
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005272 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005273 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005274 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005275 /* empty match works always */
5276 return 0;
5277
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005278 case NFA_START_INVISIBLE:
5279 case NFA_START_INVISIBLE_FIRST:
5280 case NFA_START_INVISIBLE_NEG:
5281 case NFA_START_INVISIBLE_NEG_FIRST:
5282 case NFA_START_INVISIBLE_BEFORE:
5283 case NFA_START_INVISIBLE_BEFORE_FIRST:
5284 case NFA_START_INVISIBLE_BEFORE_NEG:
5285 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5286 case NFA_START_PATTERN:
5287 /* recursive regmatch is expensive, use low failure chance */
5288 return 5;
5289
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005290 case NFA_BOL:
5291 case NFA_EOL:
5292 case NFA_BOF:
5293 case NFA_EOF:
5294 case NFA_NEWL:
5295 return 99;
5296
5297 case NFA_BOW:
5298 case NFA_EOW:
5299 return 90;
5300
5301 case NFA_MOPEN:
5302 case NFA_MOPEN1:
5303 case NFA_MOPEN2:
5304 case NFA_MOPEN3:
5305 case NFA_MOPEN4:
5306 case NFA_MOPEN5:
5307 case NFA_MOPEN6:
5308 case NFA_MOPEN7:
5309 case NFA_MOPEN8:
5310 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005311#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005312 case NFA_ZOPEN:
5313 case NFA_ZOPEN1:
5314 case NFA_ZOPEN2:
5315 case NFA_ZOPEN3:
5316 case NFA_ZOPEN4:
5317 case NFA_ZOPEN5:
5318 case NFA_ZOPEN6:
5319 case NFA_ZOPEN7:
5320 case NFA_ZOPEN8:
5321 case NFA_ZOPEN9:
5322 case NFA_ZCLOSE:
5323 case NFA_ZCLOSE1:
5324 case NFA_ZCLOSE2:
5325 case NFA_ZCLOSE3:
5326 case NFA_ZCLOSE4:
5327 case NFA_ZCLOSE5:
5328 case NFA_ZCLOSE6:
5329 case NFA_ZCLOSE7:
5330 case NFA_ZCLOSE8:
5331 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005332#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005333 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005334 case NFA_MCLOSE1:
5335 case NFA_MCLOSE2:
5336 case NFA_MCLOSE3:
5337 case NFA_MCLOSE4:
5338 case NFA_MCLOSE5:
5339 case NFA_MCLOSE6:
5340 case NFA_MCLOSE7:
5341 case NFA_MCLOSE8:
5342 case NFA_MCLOSE9:
5343 case NFA_NCLOSE:
5344 return failure_chance(state->out, depth + 1);
5345
5346 case NFA_BACKREF1:
5347 case NFA_BACKREF2:
5348 case NFA_BACKREF3:
5349 case NFA_BACKREF4:
5350 case NFA_BACKREF5:
5351 case NFA_BACKREF6:
5352 case NFA_BACKREF7:
5353 case NFA_BACKREF8:
5354 case NFA_BACKREF9:
5355#ifdef FEAT_SYN_HL
5356 case NFA_ZREF1:
5357 case NFA_ZREF2:
5358 case NFA_ZREF3:
5359 case NFA_ZREF4:
5360 case NFA_ZREF5:
5361 case NFA_ZREF6:
5362 case NFA_ZREF7:
5363 case NFA_ZREF8:
5364 case NFA_ZREF9:
5365#endif
5366 /* backreferences don't match in many places */
5367 return 94;
5368
5369 case NFA_LNUM_GT:
5370 case NFA_LNUM_LT:
5371 case NFA_COL_GT:
5372 case NFA_COL_LT:
5373 case NFA_VCOL_GT:
5374 case NFA_VCOL_LT:
5375 case NFA_MARK_GT:
5376 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005377 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005378 /* before/after positions don't match very often */
5379 return 85;
5380
5381 case NFA_LNUM:
5382 return 90;
5383
5384 case NFA_CURSOR:
5385 case NFA_COL:
5386 case NFA_VCOL:
5387 case NFA_MARK:
5388 /* specific positions rarely match */
5389 return 98;
5390
5391 case NFA_COMPOSING:
5392 return 95;
5393
5394 default:
5395 if (c > 0)
5396 /* character match fails often */
5397 return 95;
5398 }
5399
5400 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005401 return 50;
5402}
5403
Bram Moolenaarf46da702013-06-02 22:37:42 +02005404/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005405 * Skip until the char "c" we know a match must start with.
5406 */
5407 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005408skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005409{
5410 char_u *s;
5411
5412 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005413 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005414 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005415 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005416 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005417 if (s == NULL)
5418 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005419 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005420 return OK;
5421}
5422
5423/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005424 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005425 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005426 * Returns zero for no match, 1 for a match.
5427 */
5428 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005429find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005430{
5431 colnr_T col = startcol;
5432 int c1, c2;
5433 int len1, len2;
5434 int match;
5435
5436 for (;;)
5437 {
5438 match = TRUE;
5439 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5440 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5441 {
5442 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005443 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005444 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005445 {
5446 match = FALSE;
5447 break;
5448 }
5449 len2 += MB_CHAR2LEN(c2);
5450 }
5451 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005452 /* check that no composing char follows */
5453 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005454 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005455 {
5456 cleanup_subexpr();
5457 if (REG_MULTI)
5458 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005459 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005460 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005461 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005462 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005463 }
5464 else
5465 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005466 rex.reg_startp[0] = rex.line + col;
5467 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005468 }
5469 return 1L;
5470 }
5471
5472 /* Try finding regstart after the current match. */
5473 col += MB_CHAR2LEN(regstart); /* skip regstart */
5474 if (skip_to_start(regstart, &col) == FAIL)
5475 break;
5476 }
5477 return 0L;
5478}
5479
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005480#ifdef FEAT_RELTIME
5481 static int
5482nfa_did_time_out()
5483{
5484 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5485 {
5486 if (nfa_timed_out != NULL)
5487 *nfa_timed_out = TRUE;
5488 return TRUE;
5489 }
5490 return FALSE;
5491}
5492#endif
5493
Bram Moolenaar473de612013-06-08 18:19:48 +02005494/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005495 * Main matching routine.
5496 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005497 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005498 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005499 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005500 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005501 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005502 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503 * Note: Caller must ensure that: start != NULL.
5504 */
5505 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005506nfa_regmatch(
5507 nfa_regprog_T *prog,
5508 nfa_state_T *start,
5509 regsubs_T *submatch,
5510 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005511{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005513 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005514 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005515 int go_to_nextline = FALSE;
5516 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005517 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005518 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005519 nfa_list_T *thislist;
5520 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005521 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005522 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005523 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005524 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005525 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005526 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005527 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005528 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005529#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005530 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005532
Bram Moolenaar41f12052013-08-25 17:01:42 +02005533 /* Some patterns may take a long time to match, especially when using
5534 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5535 fast_breakcheck();
5536 if (got_int)
5537 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005538#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005539 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005540 return FALSE;
5541#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005542
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005543#ifdef NFA_REGEXP_DEBUG_LOG
5544 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5545 if (debug == NULL)
5546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005547 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005548 return FALSE;
5549 }
5550#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005551 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005553 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005554 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005555
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005556 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005557 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005558 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005559 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005560 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005561 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562
5563#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005564 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565 if (log_fd != NULL)
5566 {
5567 fprintf(log_fd, "**********************************\n");
5568 nfa_set_code(start->c);
5569 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5570 abs(start->id), code);
5571 fprintf(log_fd, "**********************************\n");
5572 }
5573 else
5574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005575 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005576 log_fd = stderr;
5577 }
5578#endif
5579
5580 thislist = &list[0];
5581 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005582 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005583 nextlist = &list[1];
5584 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005585 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005586#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005587 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005588#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005589 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005590
5591 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5592 * it's the first MOPEN. */
5593 if (toplevel)
5594 {
5595 if (REG_MULTI)
5596 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005597 m->norm.list.multi[0].start_lnum = rex.lnum;
5598 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005599 }
5600 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005601 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005602 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005603 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005604 }
5605 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005606 r = addstate(thislist, start, m, NULL, 0);
5607 if (r == NULL)
5608 {
5609 nfa_match = NFA_TOO_EXPENSIVE;
5610 goto theend;
5611 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005612
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005613#define ADD_STATE_IF_MATCH(state) \
5614 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005615 add_state = state->out; \
5616 add_off = clen; \
5617 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005618
5619 /*
5620 * Run for each character.
5621 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005622 for (;;)
5623 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005624 int curc;
5625 int clen;
5626
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005627 if (has_mbyte)
5628 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005629 curc = (*mb_ptr2char)(rex.input);
5630 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005631 }
5632 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005633 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005634 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005635 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005636 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005637 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005638 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005639 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005640 go_to_nextline = FALSE;
5641 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005642
5643 /* swap lists */
5644 thislist = &list[flag];
5645 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005646 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005647 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005648 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005649 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005650 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005651# ifdef FEAT_EVAL
5652 || nfa_fail_for_testing
5653# endif
5654 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005655 {
5656 /* too many states, retry with old engine */
5657 nfa_match = NFA_TOO_EXPENSIVE;
5658 goto theend;
5659 }
5660
Bram Moolenaar0270f382018-07-17 05:43:58 +02005661 thislist->id = rex.nfa_listid;
5662 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005663
5664#ifdef ENABLE_LOG
5665 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005666 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005667 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005668 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005669 {
5670 int i;
5671
5672 for (i = 0; i < thislist->n; i++)
5673 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5674 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675 fprintf(log_fd, "\n");
5676#endif
5677
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005678#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005679 fprintf(debug, "\n-------------------\n");
5680#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005681 /*
5682 * If the state lists are empty we can stop.
5683 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005684 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005685 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005686
5687 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005688 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005689 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005690 /* If the list gets very long there probably is something wrong.
5691 * At least allow interrupting with CTRL-C. */
5692 fast_breakcheck();
5693 if (got_int)
5694 break;
5695#ifdef FEAT_RELTIME
5696 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5697 {
5698 nfa_time_count = 0;
5699 if (nfa_did_time_out())
5700 break;
5701 }
5702#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005703 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005704
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005705#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005706 nfa_set_code(t->state->c);
5707 fprintf(debug, "%s, ", code);
5708#endif
5709#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005710 {
5711 int col;
5712
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005713 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005714 col = -1;
5715 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005716 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005717 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005718 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005719 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005720 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005721 abs(t->state->id), (int)t->state->c, code, col,
5722 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005723 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005724#endif
5725
5726 /*
5727 * Handle the possible codes of the current state.
5728 * The most important is NFA_MATCH.
5729 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005730 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005731 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005732 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005733 switch (t->state->c)
5734 {
5735 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005736 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005737 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005738 * rex.reg_icombine is not set, that is not really a match. */
5739 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005740 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005741
Bram Moolenaar963fee22013-05-26 21:47:28 +02005742 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005743 copy_sub(&submatch->norm, &t->subs.norm);
5744#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005745 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005746 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005747#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005748#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005749 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005750#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005751 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005752 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005753 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005754 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005755 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005756 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005757 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005758 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005759
5760 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005761 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005762 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005763 /*
5764 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005765 * NFA_START_INVISIBLE_BEFORE node.
5766 * They surround a zero-width group, used with "\@=", "\&",
5767 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005768 * If we got here, it means that the current "invisible" group
5769 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005770 * nfa_regmatch(). For a look-behind match only when it ends
5771 * in the position in "nfa_endp".
5772 * Submatches are stored in *m, and used in the parent call.
5773 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005774#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005775 if (nfa_endp != NULL)
5776 {
5777 if (REG_MULTI)
5778 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005779 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005780 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005781 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005782 nfa_endp->se_u.pos.col);
5783 else
5784 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005785 (int)(rex.input - rex.line),
5786 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005787 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005788#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005789 /* If "nfa_endp" is set it's only a match if it ends at
5790 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005791 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005792 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5793 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005794 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005795 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005796 break;
5797
5798 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005799 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005800 {
5801 copy_sub(&m->norm, &t->subs.norm);
5802#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005803 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005804 copy_sub(&m->synt, &t->subs.synt);
5805#endif
5806 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005807#ifdef ENABLE_LOG
5808 fprintf(log_fd, "Match found:\n");
5809 log_subsexpr(m);
5810#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005811 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005812 /* See comment above at "goto nextchar". */
5813 if (nextlist->n == 0)
5814 clen = 0;
5815 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005816
5817 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005818 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005819 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005820 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005821 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005822 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005823 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005824 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005825 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005826#ifdef ENABLE_LOG
5827 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5828 failure_chance(t->state->out, 0),
5829 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005830#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005831 /* Do it directly if there already is a PIM or when
5832 * nfa_postprocess() detected it will work better. */
5833 if (t->pim.result != NFA_PIM_UNUSED
5834 || t->state->c == NFA_START_INVISIBLE_FIRST
5835 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5836 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5837 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005838 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005839 int in_use = m->norm.in_use;
5840
Bram Moolenaar699c1202013-09-25 16:41:54 +02005841 /* Copy submatch info for the recursive call, opposite
5842 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005843 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005844#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005845 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005846 copy_sub_off(&m->synt, &t->subs.synt);
5847#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005848
Bram Moolenaara2d95102013-06-04 14:23:05 +02005849 /*
5850 * First try matching the invisible match, then what
5851 * follows.
5852 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005853 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005854 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005855 if (result == NFA_TOO_EXPENSIVE)
5856 {
5857 nfa_match = result;
5858 goto theend;
5859 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005860
Bram Moolenaardecd9542013-06-07 16:31:50 +02005861 /* for \@! and \@<! it is a match when the result is
5862 * FALSE */
5863 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005864 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5865 || t->state->c
5866 == NFA_START_INVISIBLE_BEFORE_NEG
5867 || t->state->c
5868 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005869 {
5870 /* Copy submatch info from the recursive call */
5871 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005872#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005873 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005874 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005875#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005876 /* If the pattern has \ze and it matched in the
5877 * sub pattern, use it. */
5878 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005879
Bram Moolenaara2d95102013-06-04 14:23:05 +02005880 /* t->state->out1 is the corresponding
5881 * END_INVISIBLE node; Add its out to the current
5882 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005883 add_here = TRUE;
5884 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005885 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005886 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005887 }
5888 else
5889 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005890 nfa_pim_T pim;
5891
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005893 * First try matching what follows. Only if a match
5894 * is found verify the invisible match matches. Add a
5895 * nfa_pim_T to the following states, it contains info
5896 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005897 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005898 pim.state = t->state;
5899 pim.result = NFA_PIM_TODO;
5900 pim.subs.norm.in_use = 0;
5901#ifdef FEAT_SYN_HL
5902 pim.subs.synt.in_use = 0;
5903#endif
5904 if (REG_MULTI)
5905 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005906 pim.end.pos.col = (int)(rex.input - rex.line);
5907 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005908 }
5909 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005910 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005911
5912 /* t->state->out1 is the corresponding END_INVISIBLE
5913 * node; Add its out to the current list (zero-width
5914 * match). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005915 if (addstate_here(thislist, t->state->out1->out,
5916 &t->subs, &pim, &listidx) == NULL)
5917 {
5918 nfa_match = NFA_TOO_EXPENSIVE;
5919 goto theend;
5920 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005921 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005922 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005923 break;
5924
Bram Moolenaar87953742013-06-05 18:52:40 +02005925 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005926 {
5927 nfa_state_T *skip = NULL;
5928#ifdef ENABLE_LOG
5929 int skip_lid = 0;
5930#endif
5931
5932 /* There is no point in trying to match the pattern if the
5933 * output state is not going to be added to the list. */
5934 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5935 {
5936 skip = t->state->out1->out;
5937#ifdef ENABLE_LOG
5938 skip_lid = nextlist->id;
5939#endif
5940 }
5941 else if (state_in_list(nextlist,
5942 t->state->out1->out->out, &t->subs))
5943 {
5944 skip = t->state->out1->out->out;
5945#ifdef ENABLE_LOG
5946 skip_lid = nextlist->id;
5947#endif
5948 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005949 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005950 t->state->out1->out->out, &t->subs))
5951 {
5952 skip = t->state->out1->out->out;
5953#ifdef ENABLE_LOG
5954 skip_lid = thislist->id;
5955#endif
5956 }
5957 if (skip != NULL)
5958 {
5959#ifdef ENABLE_LOG
5960 nfa_set_code(skip->c);
5961 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5962 abs(skip->id), skip_lid, skip->c, code);
5963#endif
5964 break;
5965 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005966 /* Copy submatch info to the recursive call, opposite of what
5967 * happens afterwards. */
5968 copy_sub_off(&m->norm, &t->subs.norm);
5969#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005970 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005971 copy_sub_off(&m->synt, &t->subs.synt);
5972#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005973
Bram Moolenaar87953742013-06-05 18:52:40 +02005974 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005975 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005976 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005977 if (result == NFA_TOO_EXPENSIVE)
5978 {
5979 nfa_match = result;
5980 goto theend;
5981 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005982 if (result)
5983 {
5984 int bytelen;
5985
5986#ifdef ENABLE_LOG
5987 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5988 log_subsexpr(m);
5989#endif
5990 /* Copy submatch info from the recursive call */
5991 copy_sub_off(&t->subs.norm, &m->norm);
5992#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005993 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005994 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005995#endif
5996 /* Now we need to skip over the matched text and then
5997 * continue with what follows. */
5998 if (REG_MULTI)
5999 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006000 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006001 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006002 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006003 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006004
6005#ifdef ENABLE_LOG
6006 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6007#endif
6008 if (bytelen == 0)
6009 {
6010 /* empty match, output of corresponding
6011 * NFA_END_PATTERN/NFA_SKIP to be used at current
6012 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006013 add_here = TRUE;
6014 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006015 }
6016 else if (bytelen <= clen)
6017 {
6018 /* match current character, output of corresponding
6019 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006020 add_state = t->state->out1->out->out;
6021 add_off = clen;
6022 }
6023 else
6024 {
6025 /* skip over the matched characters, set character
6026 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006027 add_state = t->state->out1->out;
6028 add_off = bytelen;
6029 add_count = bytelen - clen;
6030 }
6031 }
6032 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006033 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006034
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006035 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006036 if (rex.input == rex.line)
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_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006044 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006045 {
6046 add_here = TRUE;
6047 add_state = t->state->out;
6048 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006049 break;
6050
6051 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006052 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006053
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006054 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006055 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006056 else if (has_mbyte)
6057 {
6058 int this_class;
6059
6060 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006061 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006062 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006063 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006064 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006065 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006066 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006067 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006068 || (rex.input > rex.line
6069 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006070 result = FALSE;
6071 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006072 {
6073 add_here = TRUE;
6074 add_state = t->state->out;
6075 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006076 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006077
6078 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006079 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006080 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006081 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006082 else if (has_mbyte)
6083 {
6084 int this_class, prev_class;
6085
6086 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006087 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006088 prev_class = reg_prev_class();
6089 if (this_class == prev_class
6090 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006091 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006092 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006093 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6094 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006095 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006096 result = FALSE;
6097 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006098 {
6099 add_here = TRUE;
6100 add_state = t->state->out;
6101 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006103
Bram Moolenaar4b780632013-05-31 22:14:52 +02006104 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006105 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006106 && (!REG_MULTI || rex.reg_firstlnum == 1))
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
6113 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006114 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006115 {
6116 add_here = TRUE;
6117 add_state = t->state->out;
6118 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006119 break;
6120
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006121 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006122 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006123 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006124 int len = 0;
6125 nfa_state_T *end;
6126 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006127 int cchars[MAX_MCO];
6128 int ccount = 0;
6129 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006130
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006131 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006132 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006133 if (utf_iscomposing(sta->c))
6134 {
6135 /* Only match composing character(s), ignore base
6136 * character. Used for ".{composing}" and "{composing}"
6137 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006138 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006139 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006140 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006141 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006142 /* If \Z was present, then ignore composing characters.
6143 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006144 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006145 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006146 else
6147 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006148 while (sta->c != NFA_END_COMPOSING)
6149 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006150 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006151
6152 /* Check base character matches first, unless ignored. */
6153 else if (len > 0 || mc == sta->c)
6154 {
6155 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006156 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006157 len += mb_char2len(mc);
6158 sta = sta->out;
6159 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006160
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006161 /* We don't care about the order of composing characters.
6162 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006163 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006164 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006165 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006166 cchars[ccount++] = mc;
6167 len += mb_char2len(mc);
6168 if (ccount == MAX_MCO)
6169 break;
6170 }
6171
6172 /* Check that each composing char in the pattern matches a
6173 * composing char in the text. We do not check if all
6174 * composing chars are matched. */
6175 result = OK;
6176 while (sta->c != NFA_END_COMPOSING)
6177 {
6178 for (j = 0; j < ccount; ++j)
6179 if (cchars[j] == sta->c)
6180 break;
6181 if (j == ccount)
6182 {
6183 result = FAIL;
6184 break;
6185 }
6186 sta = sta->out;
6187 }
6188 }
6189 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006190 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006191
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006192 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006193 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006194 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006195 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006196
6197 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006198 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006199 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006200 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006201 go_to_nextline = TRUE;
6202 /* Pass -1 for the offset, which means taking the position
6203 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006204 add_state = t->state->out;
6205 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006206 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006207 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006208 {
6209 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006210 add_state = t->state->out;
6211 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006212 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006213 break;
6214
Bram Moolenaar417bad22013-06-07 14:08:30 +02006215 case NFA_START_COLL:
6216 case NFA_START_NEG_COLL:
6217 {
6218 /* What follows is a list of characters, until NFA_END_COLL.
6219 * One of them must match or none of them must match. */
6220 nfa_state_T *state;
6221 int result_if_matched;
6222 int c1, c2;
6223
6224 /* Never match EOL. If it's part of the collection it is added
6225 * as a separate state with an OR. */
6226 if (curc == NUL)
6227 break;
6228
6229 state = t->state->out;
6230 result_if_matched = (t->state->c == NFA_START_COLL);
6231 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006232 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006233 if (state->c == NFA_END_COLL)
6234 {
6235 result = !result_if_matched;
6236 break;
6237 }
6238 if (state->c == NFA_RANGE_MIN)
6239 {
6240 c1 = state->val;
6241 state = state->out; /* advance to NFA_RANGE_MAX */
6242 c2 = state->val;
6243#ifdef ENABLE_LOG
6244 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6245 curc, c1, c2);
6246#endif
6247 if (curc >= c1 && curc <= c2)
6248 {
6249 result = result_if_matched;
6250 break;
6251 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006252 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006253 {
6254 int curc_low = MB_TOLOWER(curc);
6255 int done = FALSE;
6256
6257 for ( ; c1 <= c2; ++c1)
6258 if (MB_TOLOWER(c1) == curc_low)
6259 {
6260 result = result_if_matched;
6261 done = TRUE;
6262 break;
6263 }
6264 if (done)
6265 break;
6266 }
6267 }
6268 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006269 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006270 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006271 == MB_TOLOWER(state->c))))
6272 {
6273 result = result_if_matched;
6274 break;
6275 }
6276 state = state->out;
6277 }
6278 if (result)
6279 {
6280 /* next state is in out of the NFA_END_COLL, out1 of
6281 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006282 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006283 add_off = clen;
6284 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006285 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006286 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006287
6288 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006289 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006290 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006291 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006292 add_state = t->state->out;
6293 add_off = clen;
6294 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006295 break;
6296
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006297 case NFA_ANY_COMPOSING:
6298 /* On a composing character skip over it. Otherwise do
6299 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006300 if (enc_utf8 && utf_iscomposing(curc))
6301 {
6302 add_off = clen;
6303 }
6304 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006305 {
6306 add_here = TRUE;
6307 add_off = 0;
6308 }
6309 add_state = t->state->out;
6310 break;
6311
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006312 /*
6313 * Character classes like \a for alpha, \d for digit etc.
6314 */
6315 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006316 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006317 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318 break;
6319
6320 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006321 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006322 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323 break;
6324
6325 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006326 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006327 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328 break;
6329
6330 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006331 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006332 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006333 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006334 break;
6335
6336 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006337 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006338 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006339 break;
6340
6341 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006342 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006343 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006344 break;
6345
6346 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006347 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006348 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006349 break;
6350
6351 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006352 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006353 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006354 break;
6355
6356 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006357 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006358 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006359 break;
6360
6361 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006362 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006363 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006364 break;
6365
6366 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006367 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006368 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 break;
6370
6371 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006372 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006373 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006374 break;
6375
6376 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006377 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006378 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 break;
6380
6381 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006382 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006383 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 break;
6385
6386 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006387 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006388 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389 break;
6390
6391 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006392 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006393 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394 break;
6395
6396 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006397 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006398 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 break;
6400
6401 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006402 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006403 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006404 break;
6405
6406 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006407 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006408 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006409 break;
6410
6411 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006412 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006413 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 break;
6415
6416 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006417 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006418 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006419 break;
6420
6421 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006422 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006423 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006424 break;
6425
6426 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006427 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006428 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006429 break;
6430
6431 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006432 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006433 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006434 break;
6435
6436 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006437 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006438 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006439 break;
6440
6441 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006442 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006443 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006444 break;
6445
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006446 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006447 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006448 ADD_STATE_IF_MATCH(t->state);
6449 break;
6450
6451 case NFA_NLOWER_IC: /* [^a-z] */
6452 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006453 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006454 ADD_STATE_IF_MATCH(t->state);
6455 break;
6456
6457 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006458 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006459 ADD_STATE_IF_MATCH(t->state);
6460 break;
6461
6462 case NFA_NUPPER_IC: /* ^[A-Z] */
6463 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006464 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006465 ADD_STATE_IF_MATCH(t->state);
6466 break;
6467
Bram Moolenaar5714b802013-05-28 22:03:20 +02006468 case NFA_BACKREF1:
6469 case NFA_BACKREF2:
6470 case NFA_BACKREF3:
6471 case NFA_BACKREF4:
6472 case NFA_BACKREF5:
6473 case NFA_BACKREF6:
6474 case NFA_BACKREF7:
6475 case NFA_BACKREF8:
6476 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006477#ifdef FEAT_SYN_HL
6478 case NFA_ZREF1:
6479 case NFA_ZREF2:
6480 case NFA_ZREF3:
6481 case NFA_ZREF4:
6482 case NFA_ZREF5:
6483 case NFA_ZREF6:
6484 case NFA_ZREF7:
6485 case NFA_ZREF8:
6486 case NFA_ZREF9:
6487#endif
6488 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006489 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006490 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006491 int bytelen;
6492
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006493 if (t->state->c <= NFA_BACKREF9)
6494 {
6495 subidx = t->state->c - NFA_BACKREF1 + 1;
6496 result = match_backref(&t->subs.norm, subidx, &bytelen);
6497 }
6498#ifdef FEAT_SYN_HL
6499 else
6500 {
6501 subidx = t->state->c - NFA_ZREF1 + 1;
6502 result = match_zref(subidx, &bytelen);
6503 }
6504#endif
6505
Bram Moolenaar5714b802013-05-28 22:03:20 +02006506 if (result)
6507 {
6508 if (bytelen == 0)
6509 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006510 /* empty match always works, output of NFA_SKIP to be
6511 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006512 add_here = TRUE;
6513 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006514 }
6515 else if (bytelen <= clen)
6516 {
6517 /* match current character, jump ahead to out of
6518 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006519 add_state = t->state->out->out;
6520 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006521 }
6522 else
6523 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006524 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006525 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006526 add_state = t->state->out;
6527 add_off = bytelen;
6528 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006529 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006530 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006531 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006532 }
6533 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006534 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006535 if (t->count - clen <= 0)
6536 {
6537 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006538 add_state = t->state->out;
6539 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006540 }
6541 else
6542 {
6543 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006544 add_state = t->state;
6545 add_off = 0;
6546 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006547 }
6548 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006549
Bram Moolenaar423532e2013-05-29 21:14:42 +02006550 case NFA_LNUM:
6551 case NFA_LNUM_GT:
6552 case NFA_LNUM_LT:
6553 result = (REG_MULTI &&
6554 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006555 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006556 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006557 {
6558 add_here = TRUE;
6559 add_state = t->state->out;
6560 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006561 break;
6562
6563 case NFA_COL:
6564 case NFA_COL_GT:
6565 case NFA_COL_LT:
6566 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006567 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006568 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006569 {
6570 add_here = TRUE;
6571 add_state = t->state->out;
6572 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006573 break;
6574
6575 case NFA_VCOL:
6576 case NFA_VCOL_GT:
6577 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006578 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006579 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006580 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006581 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006582
6583 /* Bail out quickly when there can't be a match, avoid the
6584 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006585 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006586 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006587 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006588 result = FALSE;
6589 if (op == 1 && col - 1 > t->state->val && col > 100)
6590 {
6591 int ts = wp->w_buffer->b_p_ts;
6592
6593 /* Guess that a character won't use more columns than
6594 * 'tabstop', with a minimum of 4. */
6595 if (ts < 4)
6596 ts = 4;
6597 result = col > t->state->val * ts;
6598 }
6599 if (!result)
6600 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006601 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006602 if (result)
6603 {
6604 add_here = TRUE;
6605 add_state = t->state->out;
6606 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006607 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006608 break;
6609
Bram Moolenaar044aa292013-06-04 21:27:38 +02006610 case NFA_MARK:
6611 case NFA_MARK_GT:
6612 case NFA_MARK_LT:
6613 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006614 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006615
6616 /* Compare the mark position to the match position. */
6617 result = (pos != NULL /* mark doesn't exist */
6618 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006619 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6620 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006621 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006622 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006623 ? t->state->c == NFA_MARK_GT
6624 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006625 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006626 ? t->state->c == NFA_MARK_GT
6627 : t->state->c == NFA_MARK_LT)));
6628 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006629 {
6630 add_here = TRUE;
6631 add_state = t->state->out;
6632 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006633 break;
6634 }
6635
Bram Moolenaar423532e2013-05-29 21:14:42 +02006636 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006637 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006638 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006639 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006640 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006641 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006642 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006643 {
6644 add_here = TRUE;
6645 add_state = t->state->out;
6646 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006647 break;
6648
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006649 case NFA_VISUAL:
6650 result = reg_match_visual();
6651 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006652 {
6653 add_here = TRUE;
6654 add_state = t->state->out;
6655 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006656 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006657
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006658 case NFA_MOPEN1:
6659 case NFA_MOPEN2:
6660 case NFA_MOPEN3:
6661 case NFA_MOPEN4:
6662 case NFA_MOPEN5:
6663 case NFA_MOPEN6:
6664 case NFA_MOPEN7:
6665 case NFA_MOPEN8:
6666 case NFA_MOPEN9:
6667#ifdef FEAT_SYN_HL
6668 case NFA_ZOPEN:
6669 case NFA_ZOPEN1:
6670 case NFA_ZOPEN2:
6671 case NFA_ZOPEN3:
6672 case NFA_ZOPEN4:
6673 case NFA_ZOPEN5:
6674 case NFA_ZOPEN6:
6675 case NFA_ZOPEN7:
6676 case NFA_ZOPEN8:
6677 case NFA_ZOPEN9:
6678#endif
6679 case NFA_NOPEN:
6680 case NFA_ZSTART:
6681 /* These states are only added to be able to bail out when
6682 * they are added again, nothing is to be done. */
6683 break;
6684
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006685 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006686 {
6687 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006688
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006689#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006690 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006691 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006692#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006693 result = (c == curc);
6694
Bram Moolenaar6100d022016-10-02 16:51:57 +02006695 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006696 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006697 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006698 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006699 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006700 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006701 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006702 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006703 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006704
6705 } /* switch (t->state->c) */
6706
6707 if (add_state != NULL)
6708 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006709 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006710 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006711
6712 if (t->pim.result == NFA_PIM_UNUSED)
6713 pim = NULL;
6714 else
6715 pim = &t->pim;
6716
6717 /* Handle the postponed invisible match if the match might end
6718 * without advancing and before the end of the line. */
6719 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006720 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006721 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006722 {
6723#ifdef ENABLE_LOG
6724 fprintf(log_fd, "\n");
6725 fprintf(log_fd, "==================================\n");
6726 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6727 fprintf(log_fd, "\n");
6728#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006729 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006730 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006731 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006732 /* for \@! and \@<! it is a match when the result is
6733 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006734 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006735 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6736 || pim->state->c
6737 == NFA_START_INVISIBLE_BEFORE_NEG
6738 || pim->state->c
6739 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006740 {
6741 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006742 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006743#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006744 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006745 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006746#endif
6747 }
6748 }
6749 else
6750 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006751 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006752#ifdef ENABLE_LOG
6753 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006754 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006755 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6756 fprintf(log_fd, "\n");
6757#endif
6758 }
6759
Bram Moolenaardecd9542013-06-07 16:31:50 +02006760 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006761 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006762 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6763 || pim->state->c
6764 == NFA_START_INVISIBLE_BEFORE_NEG
6765 || pim->state->c
6766 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006767 {
6768 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006769 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006770#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006771 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006772 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006773#endif
6774 }
6775 else
6776 /* look-behind match failed, don't add the state */
6777 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006778
6779 /* Postponed invisible match was handled, don't add it to
6780 * following states. */
6781 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006782 }
6783
Bram Moolenaara951e352013-10-06 15:46:11 +02006784 /* If "pim" points into l->t it will become invalid when
6785 * adding the state causes the list to be reallocated. Make a
6786 * local copy to avoid that. */
6787 if (pim == &t->pim)
6788 {
6789 copy_pim(&pim_copy, pim);
6790 pim = &pim_copy;
6791 }
6792
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006793 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006794 r = addstate_here(thislist, add_state, &t->subs,
6795 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006796 else
6797 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006798 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006799 if (add_count > 0)
6800 nextlist->t[nextlist->n - 1].count = add_count;
6801 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006802 if (r == NULL)
6803 {
6804 nfa_match = NFA_TOO_EXPENSIVE;
6805 goto theend;
6806 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006807 }
6808
6809 } /* for (thislist = thislist; thislist->state; thislist++) */
6810
Bram Moolenaare23febd2013-05-26 18:40:14 +02006811 /* Look for the start of a match in the current position by adding the
6812 * start state to the list of states.
6813 * The first found match is the leftmost one, thus the order of states
6814 * matters!
6815 * Do not add the start state in recursive calls of nfa_regmatch(),
6816 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006817 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006818 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006819 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006820 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006821 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006822 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006823 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006824 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006825 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006826 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006827 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6828 || (rex.lnum == nfa_endp->se_u.pos.lnum
6829 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006830 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006831 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006832 {
6833#ifdef ENABLE_LOG
6834 fprintf(log_fd, "(---) STARTSTATE\n");
6835#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006836 /* Inline optimized code for addstate() if we know the state is
6837 * the first MOPEN. */
6838 if (toplevel)
6839 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006840 int add = TRUE;
6841 int c;
6842
6843 if (prog->regstart != NUL && clen != 0)
6844 {
6845 if (nextlist->n == 0)
6846 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006847 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006848
6849 /* Nextlist is empty, we can skip ahead to the
6850 * character that must appear at the start. */
6851 if (skip_to_start(prog->regstart, &col) == FAIL)
6852 break;
6853#ifdef ENABLE_LOG
6854 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006855 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006856#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006857 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006858 }
6859 else
6860 {
6861 /* Checking if the required start character matches is
6862 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006863 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006864 if (c != prog->regstart && (!rex.reg_ic
6865 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006866 {
6867#ifdef ENABLE_LOG
6868 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6869#endif
6870 add = FALSE;
6871 }
6872 }
6873 }
6874
6875 if (add)
6876 {
6877 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006878 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006879 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006880 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006881 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006882 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6883 {
6884 nfa_match = NFA_TOO_EXPENSIVE;
6885 goto theend;
6886 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006887 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006888 }
6889 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006890 {
6891 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6892 {
6893 nfa_match = NFA_TOO_EXPENSIVE;
6894 goto theend;
6895 }
6896 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006897 }
6898
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006899#ifdef ENABLE_LOG
6900 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006901 {
6902 int i;
6903
6904 for (i = 0; i < thislist->n; i++)
6905 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6906 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006907 fprintf(log_fd, "\n");
6908#endif
6909
6910nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006911 /* Advance to the next character, or advance to the next line, or
6912 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006913 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006914 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006915 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006916 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006917 reg_nextline();
6918 else
6919 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006920
6921 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006922 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006923 if (got_int)
6924 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006925#ifdef FEAT_RELTIME
6926 /* Check for timeout once in a twenty times to avoid overhead. */
6927 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6928 {
6929 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006930 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006931 break;
6932 }
6933#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006934 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006935
6936#ifdef ENABLE_LOG
6937 if (log_fd != stderr)
6938 fclose(log_fd);
6939 log_fd = NULL;
6940#endif
6941
6942theend:
6943 /* Free memory */
6944 vim_free(list[0].t);
6945 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006946 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006947#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006948#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006949 fclose(debug);
6950#endif
6951
Bram Moolenaar963fee22013-05-26 21:47:28 +02006952 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953}
6954
6955/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006956 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006957 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006958 */
6959 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006960nfa_regtry(
6961 nfa_regprog_T *prog,
6962 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006963 proftime_T *tm UNUSED, /* timeout limit or NULL */
6964 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006965{
6966 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006967 regsubs_T subs, m;
6968 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006969 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006970#ifdef ENABLE_LOG
6971 FILE *f;
6972#endif
6973
Bram Moolenaar0270f382018-07-17 05:43:58 +02006974 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006975#ifdef FEAT_RELTIME
6976 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006977 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006978 nfa_time_count = 0;
6979#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006980
6981#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006982 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006983 if (f != NULL)
6984 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006985 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006986#ifdef DEBUG
6987 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6988#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006989 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006990 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006991 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992 fprintf(f, "\n\n");
6993 fclose(f);
6994 }
6995 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006996 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997#endif
6998
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006999 clear_sub(&subs.norm);
7000 clear_sub(&m.norm);
7001#ifdef FEAT_SYN_HL
7002 clear_sub(&subs.synt);
7003 clear_sub(&m.synt);
7004#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007005
Bram Moolenaarfda37292014-11-05 14:27:36 +01007006 result = nfa_regmatch(prog, start, &subs, &m);
7007 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007008 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007009 else if (result == NFA_TOO_EXPENSIVE)
7010 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007011
7012 cleanup_subexpr();
7013 if (REG_MULTI)
7014 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007015 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007016 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007017 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7018 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007019
Bram Moolenaar6100d022016-10-02 16:51:57 +02007020 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7021 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022 }
7023
Bram Moolenaar6100d022016-10-02 16:51:57 +02007024 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007025 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007026 rex.reg_startpos[0].lnum = 0;
7027 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007029 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007031 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007032 rex.reg_endpos[0].lnum = rex.lnum;
7033 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007034 }
7035 else
7036 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007037 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007038 }
7039 else
7040 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007041 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007043 rex.reg_startp[i] = subs.norm.list.line[i].start;
7044 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045 }
7046
Bram Moolenaar6100d022016-10-02 16:51:57 +02007047 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007048 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007049 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007050 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007051 }
7052
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007053#ifdef FEAT_SYN_HL
7054 /* Package any found \z(...\) matches for export. Default is none. */
7055 unref_extmatch(re_extmatch_out);
7056 re_extmatch_out = NULL;
7057
7058 if (prog->reghasz == REX_SET)
7059 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007060 cleanup_zsubexpr();
7061 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007062 /* Loop over \z1, \z2, etc. There is no \z0. */
7063 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007064 {
7065 if (REG_MULTI)
7066 {
7067 struct multipos *mpos = &subs.synt.list.multi[i];
7068
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007069 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007070 if (mpos->start_lnum >= 0
7071 && mpos->start_lnum == mpos->end_lnum
7072 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007073 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007074 vim_strnsave(reg_getline(mpos->start_lnum)
7075 + mpos->start_col,
7076 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007077 }
7078 else
7079 {
7080 struct linepos *lpos = &subs.synt.list.line[i];
7081
7082 if (lpos->start != NULL && lpos->end != NULL)
7083 re_extmatch_out->matches[i] =
7084 vim_strnsave(lpos->start,
7085 (int)(lpos->end - lpos->start));
7086 }
7087 }
7088 }
7089#endif
7090
Bram Moolenaar0270f382018-07-17 05:43:58 +02007091 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007092}
7093
7094/*
7095 * Match a regexp against a string ("line" points to the string) or multiple
7096 * lines ("line" is NULL, use reg_getline()).
7097 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007098 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007099 */
7100 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007101nfa_regexec_both(
7102 char_u *line,
7103 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007104 proftime_T *tm, /* timeout limit or NULL */
7105 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007106{
7107 nfa_regprog_T *prog;
7108 long retval = 0L;
7109 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007110 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007111
7112 if (REG_MULTI)
7113 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007114 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007115 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007116 rex.reg_startpos = rex.reg_mmatch->startpos;
7117 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007118 }
7119 else
7120 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007121 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7122 rex.reg_startp = rex.reg_match->startp;
7123 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007124 }
7125
7126 /* Be paranoid... */
7127 if (prog == NULL || line == NULL)
7128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007129 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007130 goto theend;
7131 }
7132
Bram Moolenaar6100d022016-10-02 16:51:57 +02007133 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007134 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007135 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007136 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007137 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007138
Bram Moolenaar6100d022016-10-02 16:51:57 +02007139 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007141 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007142
Bram Moolenaar0270f382018-07-17 05:43:58 +02007143 rex.line = line;
7144 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007145
Bram Moolenaar0270f382018-07-17 05:43:58 +02007146 rex.nfa_has_zend = prog->has_zend;
7147 rex.nfa_has_backref = prog->has_backref;
7148 rex.nfa_nsubexpr = prog->nsubexp;
7149 rex.nfa_listid = 1;
7150 rex.nfa_alt_listid = 2;
7151#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007152 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007153#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154
Bram Moolenaard89616e2013-06-06 18:46:06 +02007155 if (prog->reganch && col > 0)
7156 return 0L;
7157
Bram Moolenaar0270f382018-07-17 05:43:58 +02007158 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007159#ifdef FEAT_SYN_HL
7160 /* Clear the external match subpointers if necessary. */
7161 if (prog->reghasz == REX_SET)
7162 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007163 rex.nfa_has_zsubexpr = TRUE;
7164 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007165 }
7166 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007167 {
7168 rex.nfa_has_zsubexpr = FALSE;
7169 rex.need_clear_zsubexpr = FALSE;
7170 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007171#endif
7172
Bram Moolenaard89616e2013-06-06 18:46:06 +02007173 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007174 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007175 /* Skip ahead until a character we know the match must start with.
7176 * When there is none there is no match. */
7177 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007178 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007179
Bram Moolenaar473de612013-06-08 18:19:48 +02007180 /* If match_text is set it contains the full text that must match.
7181 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007182 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007183 return find_match_text(col, prog->regstart, prog->match_text);
7184 }
7185
Bram Moolenaard89616e2013-06-06 18:46:06 +02007186 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007187 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007188 goto theend;
7189
Bram Moolenaar0270f382018-07-17 05:43:58 +02007190 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7191 // it's accidentally used during execution.
7192 nstate = 0;
7193 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007194 {
7195 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007196 prog->state[i].lastlist[0] = 0;
7197 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007198 }
7199
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007200 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007201
Bram Moolenaar0270f382018-07-17 05:43:58 +02007202#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007203 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007204#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007205
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007206theend:
7207 return retval;
7208}
7209
7210/*
7211 * Compile a regular expression into internal code for the NFA matcher.
7212 * Returns the program in allocated space. Returns NULL for an error.
7213 */
7214 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007215nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007216{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007217 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007218 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219 int *postfix;
7220
7221 if (expr == NULL)
7222 return NULL;
7223
Bram Moolenaar0270f382018-07-17 05:43:58 +02007224#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007225 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007226#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007227 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228
7229 init_class_tab();
7230
7231 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7232 return NULL;
7233
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007234 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007235 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007236 postfix = re2post();
7237 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007238 {
7239 /* TODO: only give this error for debugging? */
7240 if (post_ptr >= post_end)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007241 siemsg("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007242 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007243 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007244
7245 /*
7246 * In order to build the NFA, we parse the input regexp twice:
7247 * 1. first pass to count size (so we can allocate space)
7248 * 2. second to emit code
7249 */
7250#ifdef ENABLE_LOG
7251 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007252 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007253
7254 if (f != NULL)
7255 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007256 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007257 fclose(f);
7258 }
7259 }
7260#endif
7261
7262 /*
7263 * PASS 1
7264 * Count number of NFA states in "nstate". Do not build the NFA.
7265 */
7266 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007267
Bram Moolenaar16619a22013-06-11 18:42:36 +02007268 /* allocate the regprog with space for the compiled regexp */
7269 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007270 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7271 if (prog == NULL)
7272 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007273 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007274 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007275
7276 /*
7277 * PASS 2
7278 * Build the NFA
7279 */
7280 prog->start = post2nfa(postfix, post_ptr, FALSE);
7281 if (prog->start == NULL)
7282 goto fail;
7283
7284 prog->regflags = regflags;
7285 prog->engine = &nfa_regengine;
7286 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007287 prog->has_zend = rex.nfa_has_zend;
7288 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007289 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007290
Bram Moolenaara2947e22013-06-11 22:44:09 +02007291 nfa_postprocess(prog);
7292
Bram Moolenaard89616e2013-06-06 18:46:06 +02007293 prog->reganch = nfa_get_reganch(prog->start, 0);
7294 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007295 prog->match_text = nfa_get_match_text(prog->start);
7296
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007297#ifdef ENABLE_LOG
7298 nfa_postfix_dump(expr, OK);
7299 nfa_dump(prog);
7300#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007301#ifdef FEAT_SYN_HL
7302 /* Remember whether this pattern has any \z specials in it. */
7303 prog->reghasz = re_has_z;
7304#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007305 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007306#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007307 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007308#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007309
7310out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007311 VIM_CLEAR(post_start);
7312 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007313 state_ptr = NULL;
7314 return (regprog_T *)prog;
7315
7316fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007317 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007318#ifdef ENABLE_LOG
7319 nfa_postfix_dump(expr, FAIL);
7320#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007321#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007322 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007323#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007324 goto out;
7325}
7326
Bram Moolenaar473de612013-06-08 18:19:48 +02007327/*
7328 * Free a compiled regexp program, returned by nfa_regcomp().
7329 */
7330 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007331nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007332{
7333 if (prog != NULL)
7334 {
7335 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007336 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007337 vim_free(prog);
7338 }
7339}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007340
7341/*
7342 * Match a regexp against a string.
7343 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7344 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007345 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007346 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007347 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007348 */
7349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007350nfa_regexec_nl(
7351 regmatch_T *rmp,
7352 char_u *line, /* string to match against */
7353 colnr_T col, /* column to start looking for match */
7354 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007355{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007356 rex.reg_match = rmp;
7357 rex.reg_mmatch = NULL;
7358 rex.reg_maxline = 0;
7359 rex.reg_line_lbr = line_lbr;
7360 rex.reg_buf = curbuf;
7361 rex.reg_win = NULL;
7362 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007363 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007364 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007365 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007366}
7367
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007368
7369/*
7370 * Match a regexp against multiple lines.
7371 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7372 * Uses curbuf for line count and 'iskeyword'.
7373 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007374 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007375 * match otherwise.
7376 *
7377 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7378 *
7379 * ! Also NOTE : match may actually be in another line. e.g.:
7380 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7381 *
7382 * +-------------------------+
7383 * |a |
7384 * |b |
7385 * |c |
7386 * | |
7387 * +-------------------------+
7388 *
7389 * then nfa_regexec_multi() returns 3. while the original
7390 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7391 *
7392 * FIXME if this behavior is not compatible.
7393 */
7394 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007395nfa_regexec_multi(
7396 regmmatch_T *rmp,
7397 win_T *win, /* window in which to search or NULL */
7398 buf_T *buf, /* buffer in which to search */
7399 linenr_T lnum, /* nr of line to start looking for match */
7400 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007401 proftime_T *tm, /* timeout limit or NULL */
7402 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007403{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007404 rex.reg_match = NULL;
7405 rex.reg_mmatch = rmp;
7406 rex.reg_buf = buf;
7407 rex.reg_win = win;
7408 rex.reg_firstlnum = lnum;
7409 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7410 rex.reg_line_lbr = FALSE;
7411 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007412 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007413 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007414
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007415 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007416}
7417
7418#ifdef DEBUG
7419# undef ENABLE_LOG
7420#endif