blob: 9633791bcf4f8c3003e4d9797ced0b02ea58b4ef [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
Bram Moolenaar221cd9f2019-01-31 15:34:40 +0100229 NFA_CLASS_ESCAPE,
230 NFA_CLASS_IDENT,
231 NFA_CLASS_KEYWORD,
232 NFA_CLASS_FNAME
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233};
234
235/* Keep in sync with classchars. */
236static int nfa_classcodes[] = {
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
241 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
242 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
243 NFA_UPPER, NFA_NUPPER
244};
245
Bram Moolenaar174a8482013-11-28 14:20:17 +0100246static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaara5483442019-02-17 20:17:02 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %d");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar0270f382018-07-17 05:43:58 +0200250// Variables only used in nfa_regcomp() and descendants.
251static int nfa_re_flags; // re_flags passed to nfa_regcomp()
252static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253static int *post_end;
254static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200255static int nstate; // Number of states in the NFA.
256static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
262static int nfa_ll_index = 0;
263
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100264static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100265static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100267static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100269static int match_follows(nfa_state_T *startstate, int depth);
270static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271
272/* helper functions used when doing re2post() ... regatom() parsing */
273#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275 return FAIL; \
276 *post_ptr++ = c; \
277 } while (0)
278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279/*
280 * Initialize internal variables before NFA compilation.
281 * Return OK on success, FAIL otherwise.
282 */
283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100284nfa_regcomp_start(
285 char_u *expr,
286 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200288 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200289 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290
291 nstate = 0;
292 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200293 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200294 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200296 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200297 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200298 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200299
300 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200301 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200302
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303 post_start = (int *)lalloc(postfix_size, TRUE);
304 if (post_start == NULL)
305 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200307 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200308 rex.nfa_has_zend = FALSE;
309 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200311 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312 regcomp_start(expr, re_flags);
313
314 return OK;
315}
316
317/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200318 * Figure out if the NFA state list starts with an anchor, must match at start
319 * of the line.
320 */
321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200323{
324 nfa_state_T *p = start;
325
326 if (depth > 4)
327 return 0;
328
329 while (p != NULL)
330 {
331 switch (p->c)
332 {
333 case NFA_BOL:
334 case NFA_BOF:
335 return 1; /* yes! */
336
337 case NFA_ZSTART:
338 case NFA_ZEND:
339 case NFA_CURSOR:
340 case NFA_VISUAL:
341
342 case NFA_MOPEN:
343 case NFA_MOPEN1:
344 case NFA_MOPEN2:
345 case NFA_MOPEN3:
346 case NFA_MOPEN4:
347 case NFA_MOPEN5:
348 case NFA_MOPEN6:
349 case NFA_MOPEN7:
350 case NFA_MOPEN8:
351 case NFA_MOPEN9:
352 case NFA_NOPEN:
353#ifdef FEAT_SYN_HL
354 case NFA_ZOPEN:
355 case NFA_ZOPEN1:
356 case NFA_ZOPEN2:
357 case NFA_ZOPEN3:
358 case NFA_ZOPEN4:
359 case NFA_ZOPEN5:
360 case NFA_ZOPEN6:
361 case NFA_ZOPEN7:
362 case NFA_ZOPEN8:
363 case NFA_ZOPEN9:
364#endif
365 p = p->out;
366 break;
367
368 case NFA_SPLIT:
369 return nfa_get_reganch(p->out, depth + 1)
370 && nfa_get_reganch(p->out1, depth + 1);
371
372 default:
373 return 0; /* noooo */
374 }
375 }
376 return 0;
377}
378
379/*
380 * Figure out if the NFA state list starts with a character which must match
381 * at start of the match.
382 */
383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100384nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200385{
386 nfa_state_T *p = start;
387
388 if (depth > 4)
389 return 0;
390
391 while (p != NULL)
392 {
393 switch (p->c)
394 {
395 /* all kinds of zero-width matches */
396 case NFA_BOL:
397 case NFA_BOF:
398 case NFA_BOW:
399 case NFA_EOW:
400 case NFA_ZSTART:
401 case NFA_ZEND:
402 case NFA_CURSOR:
403 case NFA_VISUAL:
404 case NFA_LNUM:
405 case NFA_LNUM_GT:
406 case NFA_LNUM_LT:
407 case NFA_COL:
408 case NFA_COL_GT:
409 case NFA_COL_LT:
410 case NFA_VCOL:
411 case NFA_VCOL_GT:
412 case NFA_VCOL_LT:
413 case NFA_MARK:
414 case NFA_MARK_GT:
415 case NFA_MARK_LT:
416
417 case NFA_MOPEN:
418 case NFA_MOPEN1:
419 case NFA_MOPEN2:
420 case NFA_MOPEN3:
421 case NFA_MOPEN4:
422 case NFA_MOPEN5:
423 case NFA_MOPEN6:
424 case NFA_MOPEN7:
425 case NFA_MOPEN8:
426 case NFA_MOPEN9:
427 case NFA_NOPEN:
428#ifdef FEAT_SYN_HL
429 case NFA_ZOPEN:
430 case NFA_ZOPEN1:
431 case NFA_ZOPEN2:
432 case NFA_ZOPEN3:
433 case NFA_ZOPEN4:
434 case NFA_ZOPEN5:
435 case NFA_ZOPEN6:
436 case NFA_ZOPEN7:
437 case NFA_ZOPEN8:
438 case NFA_ZOPEN9:
439#endif
440 p = p->out;
441 break;
442
443 case NFA_SPLIT:
444 {
445 int c1 = nfa_get_regstart(p->out, depth + 1);
446 int c2 = nfa_get_regstart(p->out1, depth + 1);
447
448 if (c1 == c2)
449 return c1; /* yes! */
450 return 0;
451 }
452
453 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200454 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200455 return p->c; /* yes! */
456 return 0;
457 }
458 }
459 return 0;
460}
461
462/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200463 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200464 * else. If so return a string in allocated memory with what must match after
465 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200466 */
467 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100468nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200469{
470 nfa_state_T *p = start;
471 int len = 0;
472 char_u *ret;
473 char_u *s;
474
475 if (p->c != NFA_MOPEN)
476 return NULL; /* just in case */
477 p = p->out;
478 while (p->c > 0)
479 {
480 len += MB_CHAR2LEN(p->c);
481 p = p->out;
482 }
483 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
484 return NULL;
485
486 ret = alloc(len);
487 if (ret != NULL)
488 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200489 p = start->out->out; /* skip first char, it goes into regstart */
490 s = ret;
491 while (p->c > 0)
492 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200493 if (has_mbyte)
494 s += (*mb_char2bytes)(p->c, s);
495 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 *s++ = p->c;
497 p = p->out;
498 }
499 *s = NUL;
500 }
501 return ret;
502}
503
504/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200505 * Allocate more space for post_start. Called when
506 * running above the estimated number of states.
507 */
508 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100509realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200510{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200511 int nstate_max = (int)(post_end - post_start);
Bram 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 Moolenaara5483442019-02-17 20:17:02 +01001788 startc = (reg_string || emit_range
1789 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001790 else
1791 if (*regparse == 'd'
1792 || *regparse == 'o'
1793 || *regparse == 'x'
1794 || *regparse == 'u'
1795 || *regparse == 'U'
1796 )
1797 {
1798 /* TODO(RE) This needs more testing */
1799 startc = coll_get_char();
1800 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001801 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001802 }
1803 else
1804 {
1805 /* \r,\t,\e,\b */
1806 startc = backslash_trans(*regparse);
1807 }
1808 }
1809
1810 /* Normal printable char */
1811 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001812 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001813
1814 /* Previous char was '-', so this char is end of range. */
1815 if (emit_range)
1816 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001817 endc = startc;
1818 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001819 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001820 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001821
1822 if (endc > startc + 2)
1823 {
1824 /* Emit a range instead of the sequence of
1825 * individual characters. */
1826 if (startc == 0)
1827 /* \x00 is translated to \x0a, start at \x01. */
1828 EMIT(1);
1829 else
1830 --post_ptr; /* remove NFA_CONCAT */
1831 EMIT(endc);
1832 EMIT(NFA_RANGE);
1833 EMIT(NFA_CONCAT);
1834 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001835 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836 || (*mb_char2len)(endc) > 1))
1837 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001838 /* Emit the characters in the range.
1839 * "startc" was already emitted, so skip it.
1840 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001841 for (c = startc + 1; c <= endc; c++)
1842 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001843 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001844 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001845 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001846 }
1847 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 {
1849#ifdef EBCDIC
1850 int alpha_only = FALSE;
1851
1852 /* for alphabetical range skip the gaps
1853 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1854 if (isalpha(startc) && isalpha(endc))
1855 alpha_only = TRUE;
1856#endif
1857 /* Emit the range. "startc" was already emitted, so
1858 * skip it. */
1859 for (c = startc + 1; c <= endc; c++)
1860#ifdef EBCDIC
1861 if (!alpha_only || isalpha(startc))
1862#endif
1863 {
1864 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001865 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001867 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001868 emit_range = FALSE;
1869 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870 }
1871 else
1872 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001873 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001875 * Normally, simply emit startc. But if we get char
1876 * code=0 from a collating char, then replace it with
1877 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001879 * the backtracking engine. */
1880 if (startc == NFA_NEWL)
1881 {
1882 /* Line break can't be matched as part of the
1883 * collection, add an OR below. But not for negated
1884 * range. */
1885 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001886 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001887 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001888 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001889 {
1890 if (got_coll_char == TRUE && startc == 0)
1891 EMIT(0x0a);
1892 else
1893 EMIT(startc);
1894 EMIT(NFA_CONCAT);
1895 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 }
1897
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001898 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 } /* while (p < endp) */
1900
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001901 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 if (*regparse == '-') /* if last, '-' is just a char */
1903 {
1904 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001905 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 /* skip the trailing ] */
1909 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001910 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001911
1912 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001914 EMIT(NFA_END_NEG_COLL);
1915 else
1916 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001917
1918 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001919 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001920 {
1921 EMIT(reg_string ? NL : NFA_NEWL);
1922 EMIT(NFA_OR);
1923 }
1924
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001926 } /* if exists closing ] */
1927
1928 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001930 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001932 default:
1933 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 int plen;
1935
1936nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001937 /* plen is length of current char with composing chars */
1938 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001939 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001940 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001942 int i = 0;
1943
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001944 /* A base character plus composing characters, or just one
1945 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001946 * This requires creating a separate atom as if enclosing
1947 * the characters in (), where NFA_COMPOSING is the ( and
1948 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001949 * building the postfix form, not the NFA itself;
1950 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001951 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001952 for (;;)
1953 {
1954 EMIT(c);
1955 if (i > 0)
1956 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001957 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001958 break;
1959 c = utf_ptr2char(old_regparse + i);
1960 }
1961 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 regparse = old_regparse + plen;
1963 }
1964 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001965 {
1966 c = no_Magic(c);
1967 EMIT(c);
1968 }
1969 return OK;
1970 }
1971 }
1972
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001973 return OK;
1974}
1975
1976/*
1977 * Parse something followed by possible [*+=].
1978 *
1979 * A piece is an atom, possibly followed by a multi, an indication of how many
1980 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1981 * characters: "", "a", "aa", etc.
1982 *
1983 * piece ::= atom
1984 * or atom multi
1985 */
1986 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001987nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001988{
1989 int i;
1990 int op;
1991 int ret;
1992 long minval, maxval;
1993 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001994 parse_state_T old_state;
1995 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001996 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001997 int old_post_pos;
1998 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 int quest;
2000
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002001 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2002 * next. */
2003 save_parse_state(&old_state);
2004
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002006 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007
2008 ret = nfa_regatom();
2009 if (ret == FAIL)
2010 return FAIL; /* cascaded error */
2011
2012 op = peekchr();
2013 if (re_multi_type(op) == NOT_MULTI)
2014 return OK;
2015
2016 skipchr();
2017 switch (op)
2018 {
2019 case Magic('*'):
2020 EMIT(NFA_STAR);
2021 break;
2022
2023 case Magic('+'):
2024 /*
2025 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2026 * first and only submatch would be "aaa". But the backtracking
2027 * engine interprets the plus as "try matching one more time", and
2028 * a* matches a second time at the end of the input, the empty
2029 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002030 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002031 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002032 * In order to be consistent with the old engine, we replace
2033 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002035 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002036 curchr = -1;
2037 if (nfa_regatom() == FAIL)
2038 return FAIL;
2039 EMIT(NFA_STAR);
2040 EMIT(NFA_CONCAT);
2041 skipchr(); /* skip the \+ */
2042 break;
2043
2044 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002045 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002047 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002048 switch(op)
2049 {
2050 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002051 /* \@= */
2052 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002054 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002055 /* \@! */
2056 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002057 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002059 op = no_Magic(getchr());
2060 if (op == '=')
2061 /* \@<= */
2062 i = NFA_PREV_ATOM_JUST_BEFORE;
2063 else if (op == '!')
2064 /* \@<! */
2065 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2066 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002068 /* \@> */
2069 i = NFA_PREV_ATOM_LIKE_PATTERN;
2070 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002071 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002072 if (i == 0)
2073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002074 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002075 return FAIL;
2076 }
2077 EMIT(i);
2078 if (i == NFA_PREV_ATOM_JUST_BEFORE
2079 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2080 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081 break;
2082
2083 case Magic('?'):
2084 case Magic('='):
2085 EMIT(NFA_QUEST);
2086 break;
2087
2088 case Magic('{'):
2089 /* a{2,5} will expand to 'aaa?a?a?'
2090 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2091 * version of '?'
2092 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2093 * parenthesis have the same id
2094 */
2095
2096 greedy = TRUE;
2097 c2 = peekchr();
2098 if (c2 == '-' || c2 == Magic('-'))
2099 {
2100 skipchr();
2101 greedy = FALSE;
2102 }
2103 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002105
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2107 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002108 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002109 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002110 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002111 /* \{}, \{0,} */
2112 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002113 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002114 /* \{-}, \{-0,} */
2115 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002116 break;
2117 }
2118
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002119 /* Special case: x{0} or x{-0} */
2120 if (maxval == 0)
2121 {
2122 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002123 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002124 /* NFA_EMPTY is 0-length and works everywhere */
2125 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126 return OK;
2127 }
2128
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002129 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002130 * maximum is much larger than the minimum and when the maximum is
2131 * large. Bail out if we can use the other engine. */
2132 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002133 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002134 return FAIL;
2135
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002136 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002137 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002138 /* Save parse state after the repeated atom and the \{} */
2139 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002141 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2142 for (i = 0; i < maxval; i++)
2143 {
2144 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002145 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002146 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147 if (nfa_regatom() == FAIL)
2148 return FAIL;
2149 /* after "minval" times, atoms are optional */
2150 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002151 {
2152 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002153 {
2154 if (greedy)
2155 EMIT(NFA_STAR);
2156 else
2157 EMIT(NFA_STAR_NONGREEDY);
2158 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002159 else
2160 EMIT(quest);
2161 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002162 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002164 if (i + 1 > minval && maxval == MAX_LIMIT)
2165 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 }
2167
2168 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002169 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 curchr = -1;
2171
2172 break;
2173
2174
2175 default:
2176 break;
2177 } /* end switch */
2178
2179 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002181 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182
2183 return OK;
2184}
2185
2186/*
2187 * Parse one or more pieces, concatenated. It matches a match for the
2188 * first piece, followed by a match for the second piece, etc. Example:
2189 * "f[0-9]b", first matches "f", then a digit and then "b".
2190 *
2191 * concat ::= piece
2192 * or piece piece
2193 * or piece piece piece
2194 * etc.
2195 */
2196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002197nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198{
2199 int cont = TRUE;
2200 int first = TRUE;
2201
2202 while (cont)
2203 {
2204 switch (peekchr())
2205 {
2206 case NUL:
2207 case Magic('|'):
2208 case Magic('&'):
2209 case Magic(')'):
2210 cont = FALSE;
2211 break;
2212
2213 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002215 skipchr_keepstart();
2216 break;
2217 case Magic('c'):
2218 regflags |= RF_ICASE;
2219 skipchr_keepstart();
2220 break;
2221 case Magic('C'):
2222 regflags |= RF_NOICASE;
2223 skipchr_keepstart();
2224 break;
2225 case Magic('v'):
2226 reg_magic = MAGIC_ALL;
2227 skipchr_keepstart();
2228 curchr = -1;
2229 break;
2230 case Magic('m'):
2231 reg_magic = MAGIC_ON;
2232 skipchr_keepstart();
2233 curchr = -1;
2234 break;
2235 case Magic('M'):
2236 reg_magic = MAGIC_OFF;
2237 skipchr_keepstart();
2238 curchr = -1;
2239 break;
2240 case Magic('V'):
2241 reg_magic = MAGIC_NONE;
2242 skipchr_keepstart();
2243 curchr = -1;
2244 break;
2245
2246 default:
2247 if (nfa_regpiece() == FAIL)
2248 return FAIL;
2249 if (first == FALSE)
2250 EMIT(NFA_CONCAT);
2251 else
2252 first = FALSE;
2253 break;
2254 }
2255 }
2256
2257 return OK;
2258}
2259
2260/*
2261 * Parse a branch, one or more concats, separated by "\&". It matches the
2262 * last concat, but only if all the preceding concats also match at the same
2263 * position. Examples:
2264 * "foobeep\&..." matches "foo" in "foobeep".
2265 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2266 *
2267 * branch ::= concat
2268 * or concat \& concat
2269 * or concat \& concat \& concat
2270 * etc.
2271 */
2272 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002273nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002275 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276
Bram Moolenaar16299b52013-05-30 18:45:23 +02002277 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278
2279 /* First branch, possibly the only one */
2280 if (nfa_regconcat() == FAIL)
2281 return FAIL;
2282
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002284 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 {
2286 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002287 /* if concat is empty do emit a node */
2288 if (old_post_pos == (int)(post_ptr - post_start))
2289 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002290 EMIT(NFA_NOPEN);
2291 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002292 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293 if (nfa_regconcat() == FAIL)
2294 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002295 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002296 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002297 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002298 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002299 }
2300
Bram Moolenaar699c1202013-09-25 16:41:54 +02002301 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002302 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002303 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304
2305 return OK;
2306}
2307
2308/*
2309 * Parse a pattern, one or more branches, separated by "\|". It matches
2310 * anything that matches one of the branches. Example: "foo\|beep" matches
2311 * "foo" and matches "beep". If more than one branch matches, the first one
2312 * is used.
2313 *
2314 * pattern ::= branch
2315 * or branch \| branch
2316 * or branch \| branch \| branch
2317 * etc.
2318 */
2319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002320nfa_reg(
2321 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322{
2323 int parno = 0;
2324
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 if (paren == REG_PAREN)
2326 {
2327 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 parno = regnpar++;
2330 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002331#ifdef FEAT_SYN_HL
2332 else if (paren == REG_ZPAREN)
2333 {
2334 /* Make a ZOPEN node. */
2335 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002336 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002337 parno = regnzpar++;
2338 }
2339#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340
2341 if (nfa_regbranch() == FAIL)
2342 return FAIL; /* cascaded error */
2343
2344 while (peekchr() == Magic('|'))
2345 {
2346 skipchr();
2347 if (nfa_regbranch() == FAIL)
2348 return FAIL; /* cascaded error */
2349 EMIT(NFA_OR);
2350 }
2351
2352 /* Check for proper termination. */
2353 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2354 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 if (paren == REG_NPAREN)
2356 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2357 else
2358 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2359 }
2360 else if (paren == REG_NOPAREN && peekchr() != NUL)
2361 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362 if (peekchr() == Magic(')'))
2363 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2364 else
2365 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2366 }
2367 /*
2368 * Here we set the flag allowing back references to this set of
2369 * parentheses.
2370 */
2371 if (paren == REG_PAREN)
2372 {
2373 had_endbrace[parno] = TRUE; /* have seen the close paren */
2374 EMIT(NFA_MOPEN + parno);
2375 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002376#ifdef FEAT_SYN_HL
2377 else if (paren == REG_ZPAREN)
2378 EMIT(NFA_ZOPEN + parno);
2379#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002380
2381 return OK;
2382}
2383
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384#ifdef DEBUG
2385static char_u code[50];
2386
2387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002388nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389{
2390 int addnl = FALSE;
2391
2392 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2393 {
2394 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002395 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396 }
2397
2398 STRCPY(code, "");
2399 switch (c)
2400 {
2401 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2402 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2403 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2404 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2405 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2406 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2407
Bram Moolenaar5714b802013-05-28 22:03:20 +02002408 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2409 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2410 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2411 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2412 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2413 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2414 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2415 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2416 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002417#ifdef FEAT_SYN_HL
2418 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2419 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2420 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2421 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2422 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2423 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2424 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2425 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2426 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2427#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002428 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2429
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 case NFA_PREV_ATOM_NO_WIDTH:
2431 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002432 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2433 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002434 case NFA_PREV_ATOM_JUST_BEFORE:
2435 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2436 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2437 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002438 case NFA_PREV_ATOM_LIKE_PATTERN:
2439 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2440
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002441 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2442 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002444 case NFA_START_INVISIBLE_FIRST:
2445 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002446 case NFA_START_INVISIBLE_NEG:
2447 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002448 case NFA_START_INVISIBLE_NEG_FIRST:
2449 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002450 case NFA_START_INVISIBLE_BEFORE:
2451 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002452 case NFA_START_INVISIBLE_BEFORE_FIRST:
2453 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002454 case NFA_START_INVISIBLE_BEFORE_NEG:
2455 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002456 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2457 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002458 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002460 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002461 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2464 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002465 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002467 case NFA_MOPEN:
2468 case NFA_MOPEN1:
2469 case NFA_MOPEN2:
2470 case NFA_MOPEN3:
2471 case NFA_MOPEN4:
2472 case NFA_MOPEN5:
2473 case NFA_MOPEN6:
2474 case NFA_MOPEN7:
2475 case NFA_MOPEN8:
2476 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002477 STRCPY(code, "NFA_MOPEN(x)");
2478 code[10] = c - NFA_MOPEN + '0';
2479 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002480 case NFA_MCLOSE:
2481 case NFA_MCLOSE1:
2482 case NFA_MCLOSE2:
2483 case NFA_MCLOSE3:
2484 case NFA_MCLOSE4:
2485 case NFA_MCLOSE5:
2486 case NFA_MCLOSE6:
2487 case NFA_MCLOSE7:
2488 case NFA_MCLOSE8:
2489 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002490 STRCPY(code, "NFA_MCLOSE(x)");
2491 code[11] = c - NFA_MCLOSE + '0';
2492 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002493#ifdef FEAT_SYN_HL
2494 case NFA_ZOPEN:
2495 case NFA_ZOPEN1:
2496 case NFA_ZOPEN2:
2497 case NFA_ZOPEN3:
2498 case NFA_ZOPEN4:
2499 case NFA_ZOPEN5:
2500 case NFA_ZOPEN6:
2501 case NFA_ZOPEN7:
2502 case NFA_ZOPEN8:
2503 case NFA_ZOPEN9:
2504 STRCPY(code, "NFA_ZOPEN(x)");
2505 code[10] = c - NFA_ZOPEN + '0';
2506 break;
2507 case NFA_ZCLOSE:
2508 case NFA_ZCLOSE1:
2509 case NFA_ZCLOSE2:
2510 case NFA_ZCLOSE3:
2511 case NFA_ZCLOSE4:
2512 case NFA_ZCLOSE5:
2513 case NFA_ZCLOSE6:
2514 case NFA_ZCLOSE7:
2515 case NFA_ZCLOSE8:
2516 case NFA_ZCLOSE9:
2517 STRCPY(code, "NFA_ZCLOSE(x)");
2518 code[11] = c - NFA_ZCLOSE + '0';
2519 break;
2520#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2522 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2523 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2524 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002525 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2526 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002527 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2528 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2529 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2530 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2531 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2532 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2533 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2534 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2535 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2536 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2537 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2538 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2539 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2540 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002541 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002542
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002544 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2545 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2546 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002547 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002549
2550 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2551 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2552 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2553 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2554 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2555 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2556 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2557
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2559 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2560 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2561 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2562 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2563 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2564 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2565 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2566 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2567 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2568 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2569 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2570 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2571 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2572 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2573 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002574 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2575 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2576 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002577
2578 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2579 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2580 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2581 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2582 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2583 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2584 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2585 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2586 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2587 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2588 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2589 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2590 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2591 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2592 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2593 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2594 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2595 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2596 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2597 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2598 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2599 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2600 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2601 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2602 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2603 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2604 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002605 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2606 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2607 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2608 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002609
2610 default:
2611 STRCPY(code, "CHAR(x)");
2612 code[5] = c;
2613 }
2614
2615 if (addnl == TRUE)
2616 STRCAT(code, " + NEWLINE ");
2617
2618}
2619
2620#ifdef ENABLE_LOG
2621static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002622static 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 +02002623
2624/*
2625 * Print the postfix notation of the current regexp.
2626 */
2627 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002628nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002629{
2630 int *p;
2631 FILE *f;
2632
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002633 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002634 if (f != NULL)
2635 {
2636 fprintf(f, "\n-------------------------\n");
2637 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002638 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002639 else if (retval == OK)
2640 fprintf(f, ">>> NFA engine succeeded !\n");
2641 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002642 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 {
2644 nfa_set_code(*p);
2645 fprintf(f, "%s, ", code);
2646 }
2647 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002648 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649 fprintf(f, "%d ", *p);
2650 fprintf(f, "\n\n");
2651 fclose(f);
2652 }
2653}
2654
2655/*
2656 * Print the NFA starting with a root node "state".
2657 */
2658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002659nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002660{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002661 garray_T indent;
2662
2663 ga_init2(&indent, 1, 64);
2664 ga_append(&indent, '\0');
2665 nfa_print_state2(debugf, state, &indent);
2666 ga_clear(&indent);
2667}
2668
2669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002670nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002671{
2672 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673
2674 if (state == NULL)
2675 return;
2676
2677 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002678
2679 /* Output indent */
2680 p = (char_u *)indent->ga_data;
2681 if (indent->ga_len >= 3)
2682 {
2683 int last = indent->ga_len - 3;
2684 char_u save[2];
2685
2686 STRNCPY(save, &p[last], 2);
2687 STRNCPY(&p[last], "+-", 2);
2688 fprintf(debugf, " %s", p);
2689 STRNCPY(&p[last], save, 2);
2690 }
2691 else
2692 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002693
2694 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002695 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002696 code,
2697 state->c,
2698 abs(state->id),
2699 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002700 if (state->id < 0)
2701 return;
2702
2703 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002704
2705 /* grow indent for state->out */
2706 indent->ga_len -= 1;
2707 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002708 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002709 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002710 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002711 ga_append(indent, '\0');
2712
2713 nfa_print_state2(debugf, state->out, indent);
2714
2715 /* replace last part of indent for state->out1 */
2716 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002717 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002718 ga_append(indent, '\0');
2719
2720 nfa_print_state2(debugf, state->out1, indent);
2721
2722 /* shrink indent */
2723 indent->ga_len -= 3;
2724 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725}
2726
2727/*
2728 * Print the NFA state machine.
2729 */
2730 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002731nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002733 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002734
2735 if (debugf != NULL)
2736 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002737 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002738
Bram Moolenaar473de612013-06-08 18:19:48 +02002739 if (prog->reganch)
2740 fprintf(debugf, "reganch: %d\n", prog->reganch);
2741 if (prog->regstart != NUL)
2742 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2743 prog->regstart, prog->regstart);
2744 if (prog->match_text != NULL)
2745 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002746
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002747 fclose(debugf);
2748 }
2749}
2750#endif /* ENABLE_LOG */
2751#endif /* DEBUG */
2752
2753/*
2754 * Parse r.e. @expr and convert it into postfix form.
2755 * Return the postfix string on success, NULL otherwise.
2756 */
2757 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002758re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002759{
2760 if (nfa_reg(REG_NOPAREN) == FAIL)
2761 return NULL;
2762 EMIT(NFA_MOPEN);
2763 return post_start;
2764}
2765
2766/* NB. Some of the code below is inspired by Russ's. */
2767
2768/*
2769 * Represents an NFA state plus zero or one or two arrows exiting.
2770 * if c == MATCH, no arrows out; matching state.
2771 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2772 * If c < 256, labeled arrow with character c to out.
2773 */
2774
2775static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2776
2777/*
2778 * Allocate and initialize nfa_state_T.
2779 */
2780 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002781alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002782{
2783 nfa_state_T *s;
2784
2785 if (istate >= nstate)
2786 return NULL;
2787
2788 s = &state_ptr[istate++];
2789
2790 s->c = c;
2791 s->out = out;
2792 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002793 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002794
2795 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002796 s->lastlist[0] = 0;
2797 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798
2799 return s;
2800}
2801
2802/*
2803 * A partially built NFA without the matching state filled in.
2804 * Frag_T.start points at the start state.
2805 * Frag_T.out is a list of places that need to be set to the
2806 * next state for this fragment.
2807 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002808
2809/* Since the out pointers in the list are always
2810 * uninitialized, we use the pointers themselves
2811 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002813union Ptrlist
2814{
2815 Ptrlist *next;
2816 nfa_state_T *s;
2817};
2818
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002819struct Frag
2820{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002821 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 Ptrlist *out;
2823};
2824typedef struct Frag Frag_T;
2825
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002827 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 */
2829 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002830frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002832 Frag_T n;
2833
2834 n.start = start;
2835 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836 return n;
2837}
2838
2839/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840 * Create singleton list containing just outp.
2841 */
2842 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002843list1(
2844 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845{
2846 Ptrlist *l;
2847
2848 l = (Ptrlist *)outp;
2849 l->next = NULL;
2850 return l;
2851}
2852
2853/*
2854 * Patch the list of states at out to point to start.
2855 */
2856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002857patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002858{
2859 Ptrlist *next;
2860
2861 for (; l; l = next)
2862 {
2863 next = l->next;
2864 l->s = s;
2865 }
2866}
2867
2868
2869/*
2870 * Join the two lists l1 and l2, returning the combination.
2871 */
2872 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002873append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874{
2875 Ptrlist *oldl1;
2876
2877 oldl1 = l1;
2878 while (l1->next)
2879 l1 = l1->next;
2880 l1->next = l2;
2881 return oldl1;
2882}
2883
2884/*
2885 * Stack used for transforming postfix form into NFA.
2886 */
2887static Frag_T empty;
2888
2889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002890st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002892#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 FILE *df;
2894 int *p2;
2895
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002896 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897 if (df)
2898 {
2899 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002900# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002902# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002904# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905 for (p2 = postfix; p2 < end; p2++)
2906 {
2907 nfa_set_code(*p2);
2908 fprintf(df, "%s, ", code);
2909 }
2910 nfa_set_code(*p);
2911 fprintf(df, "\nCurrent position is: ");
2912 for (p2 = postfix; p2 <= p; p2 ++)
2913 {
2914 nfa_set_code(*p2);
2915 fprintf(df, "%s, ", code);
2916 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002917# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918 for (p2 = postfix; p2 < end; p2++)
2919 {
2920 fprintf(df, "%d, ", *p2);
2921 }
2922 fprintf(df, "\nCurrent position is: ");
2923 for (p2 = postfix; p2 <= p; p2 ++)
2924 {
2925 fprintf(df, "%d, ", *p2);
2926 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002927# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002928 fprintf(df, "\n--------------------------\n");
2929 fclose(df);
2930 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002931#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002932 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933}
2934
2935/*
2936 * Push an item onto the stack.
2937 */
2938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002939st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940{
2941 Frag_T *stackp = *p;
2942
2943 if (stackp >= stack_end)
2944 return;
2945 *stackp = s;
2946 *p = *p + 1;
2947}
2948
2949/*
2950 * Pop an item from the stack.
2951 */
2952 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002953st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002954{
2955 Frag_T *stackp;
2956
2957 *p = *p - 1;
2958 stackp = *p;
2959 if (stackp < stack)
2960 return empty;
2961 return **p;
2962}
2963
2964/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002965 * Estimate the maximum byte length of anything matching "state".
2966 * When unknown or unlimited return -1.
2967 */
2968 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002969nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002970{
2971 int l, r;
2972 nfa_state_T *state = startstate;
2973 int len = 0;
2974
2975 /* detect looping in a NFA_SPLIT */
2976 if (depth > 4)
2977 return -1;
2978
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002979 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002980 {
2981 switch (state->c)
2982 {
2983 case NFA_END_INVISIBLE:
2984 case NFA_END_INVISIBLE_NEG:
2985 /* the end, return what we have */
2986 return len;
2987
2988 case NFA_SPLIT:
2989 /* two alternatives, use the maximum */
2990 l = nfa_max_width(state->out, depth + 1);
2991 r = nfa_max_width(state->out1, depth + 1);
2992 if (l < 0 || r < 0)
2993 return -1;
2994 return len + (l > r ? l : r);
2995
2996 case NFA_ANY:
2997 case NFA_START_COLL:
2998 case NFA_START_NEG_COLL:
2999 /* matches some character, including composing chars */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003000 if (enc_utf8)
3001 len += MB_MAXBYTES;
3002 else if (has_mbyte)
3003 len += 2;
3004 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003005 ++len;
3006 if (state->c != NFA_ANY)
3007 {
3008 /* skip over the characters */
3009 state = state->out1->out;
3010 continue;
3011 }
3012 break;
3013
3014 case NFA_DIGIT:
3015 case NFA_WHITE:
3016 case NFA_HEX:
3017 case NFA_OCTAL:
3018 /* ascii */
3019 ++len;
3020 break;
3021
3022 case NFA_IDENT:
3023 case NFA_SIDENT:
3024 case NFA_KWORD:
3025 case NFA_SKWORD:
3026 case NFA_FNAME:
3027 case NFA_SFNAME:
3028 case NFA_PRINT:
3029 case NFA_SPRINT:
3030 case NFA_NWHITE:
3031 case NFA_NDIGIT:
3032 case NFA_NHEX:
3033 case NFA_NOCTAL:
3034 case NFA_WORD:
3035 case NFA_NWORD:
3036 case NFA_HEAD:
3037 case NFA_NHEAD:
3038 case NFA_ALPHA:
3039 case NFA_NALPHA:
3040 case NFA_LOWER:
3041 case NFA_NLOWER:
3042 case NFA_UPPER:
3043 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003044 case NFA_LOWER_IC:
3045 case NFA_NLOWER_IC:
3046 case NFA_UPPER_IC:
3047 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003048 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003049 /* possibly non-ascii */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003050 if (has_mbyte)
3051 len += 3;
3052 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003053 ++len;
3054 break;
3055
3056 case NFA_START_INVISIBLE:
3057 case NFA_START_INVISIBLE_NEG:
3058 case NFA_START_INVISIBLE_BEFORE:
3059 case NFA_START_INVISIBLE_BEFORE_NEG:
3060 /* zero-width, out1 points to the END state */
3061 state = state->out1->out;
3062 continue;
3063
3064 case NFA_BACKREF1:
3065 case NFA_BACKREF2:
3066 case NFA_BACKREF3:
3067 case NFA_BACKREF4:
3068 case NFA_BACKREF5:
3069 case NFA_BACKREF6:
3070 case NFA_BACKREF7:
3071 case NFA_BACKREF8:
3072 case NFA_BACKREF9:
3073#ifdef FEAT_SYN_HL
3074 case NFA_ZREF1:
3075 case NFA_ZREF2:
3076 case NFA_ZREF3:
3077 case NFA_ZREF4:
3078 case NFA_ZREF5:
3079 case NFA_ZREF6:
3080 case NFA_ZREF7:
3081 case NFA_ZREF8:
3082 case NFA_ZREF9:
3083#endif
3084 case NFA_NEWL:
3085 case NFA_SKIP:
3086 /* unknown width */
3087 return -1;
3088
3089 case NFA_BOL:
3090 case NFA_EOL:
3091 case NFA_BOF:
3092 case NFA_EOF:
3093 case NFA_BOW:
3094 case NFA_EOW:
3095 case NFA_MOPEN:
3096 case NFA_MOPEN1:
3097 case NFA_MOPEN2:
3098 case NFA_MOPEN3:
3099 case NFA_MOPEN4:
3100 case NFA_MOPEN5:
3101 case NFA_MOPEN6:
3102 case NFA_MOPEN7:
3103 case NFA_MOPEN8:
3104 case NFA_MOPEN9:
3105#ifdef FEAT_SYN_HL
3106 case NFA_ZOPEN:
3107 case NFA_ZOPEN1:
3108 case NFA_ZOPEN2:
3109 case NFA_ZOPEN3:
3110 case NFA_ZOPEN4:
3111 case NFA_ZOPEN5:
3112 case NFA_ZOPEN6:
3113 case NFA_ZOPEN7:
3114 case NFA_ZOPEN8:
3115 case NFA_ZOPEN9:
3116 case NFA_ZCLOSE:
3117 case NFA_ZCLOSE1:
3118 case NFA_ZCLOSE2:
3119 case NFA_ZCLOSE3:
3120 case NFA_ZCLOSE4:
3121 case NFA_ZCLOSE5:
3122 case NFA_ZCLOSE6:
3123 case NFA_ZCLOSE7:
3124 case NFA_ZCLOSE8:
3125 case NFA_ZCLOSE9:
3126#endif
3127 case NFA_MCLOSE:
3128 case NFA_MCLOSE1:
3129 case NFA_MCLOSE2:
3130 case NFA_MCLOSE3:
3131 case NFA_MCLOSE4:
3132 case NFA_MCLOSE5:
3133 case NFA_MCLOSE6:
3134 case NFA_MCLOSE7:
3135 case NFA_MCLOSE8:
3136 case NFA_MCLOSE9:
3137 case NFA_NOPEN:
3138 case NFA_NCLOSE:
3139
3140 case NFA_LNUM_GT:
3141 case NFA_LNUM_LT:
3142 case NFA_COL_GT:
3143 case NFA_COL_LT:
3144 case NFA_VCOL_GT:
3145 case NFA_VCOL_LT:
3146 case NFA_MARK_GT:
3147 case NFA_MARK_LT:
3148 case NFA_VISUAL:
3149 case NFA_LNUM:
3150 case NFA_CURSOR:
3151 case NFA_COL:
3152 case NFA_VCOL:
3153 case NFA_MARK:
3154
3155 case NFA_ZSTART:
3156 case NFA_ZEND:
3157 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003158 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003159 case NFA_START_PATTERN:
3160 case NFA_END_PATTERN:
3161 case NFA_COMPOSING:
3162 case NFA_END_COMPOSING:
3163 /* zero-width */
3164 break;
3165
3166 default:
3167 if (state->c < 0)
3168 /* don't know what this is */
3169 return -1;
3170 /* normal character */
3171 len += MB_CHAR2LEN(state->c);
3172 break;
3173 }
3174
3175 /* normal way to continue */
3176 state = state->out;
3177 }
3178
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003179 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003180 return -1;
3181}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003182
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003183/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003184 * Convert a postfix form into its equivalent NFA.
3185 * Return the NFA start state on success, NULL otherwise.
3186 */
3187 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003188post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189{
3190 int *p;
3191 int mopen;
3192 int mclose;
3193 Frag_T *stack = NULL;
3194 Frag_T *stackp = NULL;
3195 Frag_T *stack_end = NULL;
3196 Frag_T e1;
3197 Frag_T e2;
3198 Frag_T e;
3199 nfa_state_T *s;
3200 nfa_state_T *s1;
3201 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003202 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003203
3204 if (postfix == NULL)
3205 return NULL;
3206
Bram Moolenaar053bb602013-05-20 13:55:21 +02003207#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003208#define POP() st_pop(&stackp, stack); \
3209 if (stackp < stack) \
3210 { \
3211 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003212 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 return NULL; \
3214 }
3215
3216 if (nfa_calc_size == FALSE)
3217 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003218 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003219 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003220 if (stack == NULL)
3221 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003222 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003223 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003224 }
3225
3226 for (p = postfix; p < end; ++p)
3227 {
3228 switch (*p)
3229 {
3230 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003231 /* Concatenation.
3232 * Pay attention: this operator does not exist in the r.e. itself
3233 * (it is implicit, really). It is added when r.e. is translated
3234 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003235 if (nfa_calc_size == TRUE)
3236 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003237 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003238 break;
3239 }
3240 e2 = POP();
3241 e1 = POP();
3242 patch(e1.out, e2.start);
3243 PUSH(frag(e1.start, e2.out));
3244 break;
3245
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246 case NFA_OR:
3247 /* Alternation */
3248 if (nfa_calc_size == TRUE)
3249 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003250 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 break;
3252 }
3253 e2 = POP();
3254 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003255 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003257 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258 PUSH(frag(s, append(e1.out, e2.out)));
3259 break;
3260
3261 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003262 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 if (nfa_calc_size == TRUE)
3264 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003265 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 break;
3267 }
3268 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003269 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003271 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272 patch(e.out, s);
3273 PUSH(frag(s, list1(&s->out1)));
3274 break;
3275
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003276 case NFA_STAR_NONGREEDY:
3277 /* Zero or more, prefer zero */
3278 if (nfa_calc_size == TRUE)
3279 {
3280 nstate++;
3281 break;
3282 }
3283 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003284 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003285 if (s == NULL)
3286 goto theend;
3287 patch(e.out, s);
3288 PUSH(frag(s, list1(&s->out)));
3289 break;
3290
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 case NFA_QUEST:
3292 /* one or zero atoms=> greedy match */
3293 if (nfa_calc_size == TRUE)
3294 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003295 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 break;
3297 }
3298 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003299 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003301 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 PUSH(frag(s, append(e.out, list1(&s->out1))));
3303 break;
3304
3305 case NFA_QUEST_NONGREEDY:
3306 /* zero or one atoms => non-greedy match */
3307 if (nfa_calc_size == TRUE)
3308 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003309 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 break;
3311 }
3312 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003313 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003314 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003315 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 PUSH(frag(s, append(e.out, list1(&s->out))));
3317 break;
3318
Bram Moolenaar417bad22013-06-07 14:08:30 +02003319 case NFA_END_COLL:
3320 case NFA_END_NEG_COLL:
3321 /* On the stack is the sequence starting with NFA_START_COLL or
3322 * NFA_START_NEG_COLL and all possible characters. Patch it to
3323 * add the output to the start. */
3324 if (nfa_calc_size == TRUE)
3325 {
3326 nstate++;
3327 break;
3328 }
3329 e = POP();
3330 s = alloc_state(NFA_END_COLL, NULL, NULL);
3331 if (s == NULL)
3332 goto theend;
3333 patch(e.out, s);
3334 e.start->out1 = s;
3335 PUSH(frag(e.start, list1(&s->out)));
3336 break;
3337
3338 case NFA_RANGE:
3339 /* Before this are two characters, the low and high end of a
3340 * range. Turn them into two states with MIN and MAX. */
3341 if (nfa_calc_size == TRUE)
3342 {
3343 /* nstate += 0; */
3344 break;
3345 }
3346 e2 = POP();
3347 e1 = POP();
3348 e2.start->val = e2.start->c;
3349 e2.start->c = NFA_RANGE_MAX;
3350 e1.start->val = e1.start->c;
3351 e1.start->c = NFA_RANGE_MIN;
3352 patch(e1.out, e2.start);
3353 PUSH(frag(e1.start, e2.out));
3354 break;
3355
Bram Moolenaar699c1202013-09-25 16:41:54 +02003356 case NFA_EMPTY:
3357 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 if (nfa_calc_size == TRUE)
3359 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003360 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361 break;
3362 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003363 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003365 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 PUSH(frag(s, list1(&s->out)));
3367 break;
3368
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003369 case NFA_OPT_CHARS:
3370 {
3371 int n;
3372
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003373 /* \%[abc] implemented as:
3374 * NFA_SPLIT
3375 * +-CHAR(a)
3376 * | +-NFA_SPLIT
3377 * | +-CHAR(b)
3378 * | | +-NFA_SPLIT
3379 * | | +-CHAR(c)
3380 * | | | +-next
3381 * | | +- next
3382 * | +- next
3383 * +- next
3384 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003385 n = *++p; /* get number of characters */
3386 if (nfa_calc_size == TRUE)
3387 {
3388 nstate += n;
3389 break;
3390 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003391 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003392 e1.out = NULL; /* stores list with out1's */
3393 s1 = NULL; /* previous NFA_SPLIT to connect to */
3394 while (n-- > 0)
3395 {
3396 e = POP(); /* get character */
3397 s = alloc_state(NFA_SPLIT, e.start, NULL);
3398 if (s == NULL)
3399 goto theend;
3400 if (e1.out == NULL)
3401 e1 = e;
3402 patch(e.out, s1);
3403 append(e1.out, list1(&s->out1));
3404 s1 = s;
3405 }
3406 PUSH(frag(s, e1.out));
3407 break;
3408 }
3409
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003411 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003412 case NFA_PREV_ATOM_JUST_BEFORE:
3413 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003414 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003415 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003416 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3417 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003418 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003419 int start_state;
3420 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003421 int n = 0;
3422 nfa_state_T *zend;
3423 nfa_state_T *skip;
3424
Bram Moolenaardecd9542013-06-07 16:31:50 +02003425 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003426 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003427 case NFA_PREV_ATOM_NO_WIDTH:
3428 start_state = NFA_START_INVISIBLE;
3429 end_state = NFA_END_INVISIBLE;
3430 break;
3431 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3432 start_state = NFA_START_INVISIBLE_NEG;
3433 end_state = NFA_END_INVISIBLE_NEG;
3434 break;
3435 case NFA_PREV_ATOM_JUST_BEFORE:
3436 start_state = NFA_START_INVISIBLE_BEFORE;
3437 end_state = NFA_END_INVISIBLE;
3438 break;
3439 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3440 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3441 end_state = NFA_END_INVISIBLE_NEG;
3442 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003443 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003444 start_state = NFA_START_PATTERN;
3445 end_state = NFA_END_PATTERN;
3446 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003447 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003448
3449 if (before)
3450 n = *++p; /* get the count */
3451
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003452 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003453 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003454 * The \@<= operator: match for the preceding atom.
3455 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003457 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458
3459 if (nfa_calc_size == TRUE)
3460 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003461 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462 break;
3463 }
3464 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003465 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003467 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003468
Bram Moolenaar87953742013-06-05 18:52:40 +02003469 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003470 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003471 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 if (pattern)
3473 {
3474 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3475 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003476 if (skip == NULL)
3477 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003478 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003479 if (zend == NULL)
3480 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003481 s1->out= skip;
3482 patch(e.out, zend);
3483 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003484 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003485 else
3486 {
3487 patch(e.out, s1);
3488 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003489 if (before)
3490 {
3491 if (n <= 0)
3492 /* See if we can guess the maximum width, it avoids a
3493 * lot of pointless tries. */
3494 n = nfa_max_width(e.start, 0);
3495 s->val = n; /* store the count */
3496 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003497 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003499 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003500
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003501 case NFA_COMPOSING: /* char with composing char */
3502#if 0
3503 /* TODO */
3504 if (regflags & RF_ICOMBINE)
3505 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003506 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003507 }
3508#endif
3509 /* FALLTHROUGH */
3510
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003511 case NFA_MOPEN: /* \( \) Submatch */
3512 case NFA_MOPEN1:
3513 case NFA_MOPEN2:
3514 case NFA_MOPEN3:
3515 case NFA_MOPEN4:
3516 case NFA_MOPEN5:
3517 case NFA_MOPEN6:
3518 case NFA_MOPEN7:
3519 case NFA_MOPEN8:
3520 case NFA_MOPEN9:
3521#ifdef FEAT_SYN_HL
3522 case NFA_ZOPEN: /* \z( \) Submatch */
3523 case NFA_ZOPEN1:
3524 case NFA_ZOPEN2:
3525 case NFA_ZOPEN3:
3526 case NFA_ZOPEN4:
3527 case NFA_ZOPEN5:
3528 case NFA_ZOPEN6:
3529 case NFA_ZOPEN7:
3530 case NFA_ZOPEN8:
3531 case NFA_ZOPEN9:
3532#endif
3533 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003534 if (nfa_calc_size == TRUE)
3535 {
3536 nstate += 2;
3537 break;
3538 }
3539
3540 mopen = *p;
3541 switch (*p)
3542 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003543 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3544#ifdef FEAT_SYN_HL
3545 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3546 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3547 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3548 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3549 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3550 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3551 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3552 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3553 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3554 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3555#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003556 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003557 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003558 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003559 mclose = *p + NSUBEXP;
3560 break;
3561 }
3562
3563 /* Allow "NFA_MOPEN" as a valid postfix representation for
3564 * the empty regexp "". In this case, the NFA will be
3565 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3566 * empty groups of parenthesis, and empty mbyte chars */
3567 if (stackp == stack)
3568 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003569 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003571 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003572 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003573 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003574 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003575 patch(list1(&s->out), s1);
3576 PUSH(frag(s, list1(&s1->out)));
3577 break;
3578 }
3579
3580 /* At least one node was emitted before NFA_MOPEN, so
3581 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3582 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003583 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003584 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003585 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586
Bram Moolenaar525666f2013-06-02 16:40:55 +02003587 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003589 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003590 patch(e.out, s1);
3591
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003592 if (mopen == NFA_COMPOSING)
3593 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 patch(list1(&s->out1), s1);
3595
3596 PUSH(frag(s, list1(&s1->out)));
3597 break;
3598
Bram Moolenaar5714b802013-05-28 22:03:20 +02003599 case NFA_BACKREF1:
3600 case NFA_BACKREF2:
3601 case NFA_BACKREF3:
3602 case NFA_BACKREF4:
3603 case NFA_BACKREF5:
3604 case NFA_BACKREF6:
3605 case NFA_BACKREF7:
3606 case NFA_BACKREF8:
3607 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003608#ifdef FEAT_SYN_HL
3609 case NFA_ZREF1:
3610 case NFA_ZREF2:
3611 case NFA_ZREF3:
3612 case NFA_ZREF4:
3613 case NFA_ZREF5:
3614 case NFA_ZREF6:
3615 case NFA_ZREF7:
3616 case NFA_ZREF8:
3617 case NFA_ZREF9:
3618#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003619 if (nfa_calc_size == TRUE)
3620 {
3621 nstate += 2;
3622 break;
3623 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003624 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003625 if (s == NULL)
3626 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003627 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003628 if (s1 == NULL)
3629 goto theend;
3630 patch(list1(&s->out), s1);
3631 PUSH(frag(s, list1(&s1->out)));
3632 break;
3633
Bram Moolenaar423532e2013-05-29 21:14:42 +02003634 case NFA_LNUM:
3635 case NFA_LNUM_GT:
3636 case NFA_LNUM_LT:
3637 case NFA_VCOL:
3638 case NFA_VCOL_GT:
3639 case NFA_VCOL_LT:
3640 case NFA_COL:
3641 case NFA_COL_GT:
3642 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003643 case NFA_MARK:
3644 case NFA_MARK_GT:
3645 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003646 {
3647 int n = *++p; /* lnum, col or mark name */
3648
Bram Moolenaar423532e2013-05-29 21:14:42 +02003649 if (nfa_calc_size == TRUE)
3650 {
3651 nstate += 1;
3652 break;
3653 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003654 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003655 if (s == NULL)
3656 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003657 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003658 PUSH(frag(s, list1(&s->out)));
3659 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003660 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003661
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003662 case NFA_ZSTART:
3663 case NFA_ZEND:
3664 default:
3665 /* Operands */
3666 if (nfa_calc_size == TRUE)
3667 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003668 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 break;
3670 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003671 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003673 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003674 PUSH(frag(s, list1(&s->out)));
3675 break;
3676
3677 } /* switch(*p) */
3678
3679 } /* for(p = postfix; *p; ++p) */
3680
3681 if (nfa_calc_size == TRUE)
3682 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003683 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003684 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685 }
3686
3687 e = POP();
3688 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003689 {
3690 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003691 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 +02003692 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003693
3694 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003695 {
3696 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003698 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 matchstate = &state_ptr[istate++]; /* the match state */
3701 matchstate->c = NFA_MATCH;
3702 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003703 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704
3705 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003706 ret = e.start;
3707
3708theend:
3709 vim_free(stack);
3710 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003711
3712#undef POP1
3713#undef PUSH1
3714#undef POP2
3715#undef PUSH2
3716#undef POP
3717#undef PUSH
3718}
3719
Bram Moolenaara2947e22013-06-11 22:44:09 +02003720/*
3721 * After building the NFA program, inspect it to add optimization hints.
3722 */
3723 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003724nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003725{
3726 int i;
3727 int c;
3728
3729 for (i = 0; i < prog->nstate; ++i)
3730 {
3731 c = prog->state[i].c;
3732 if (c == NFA_START_INVISIBLE
3733 || c == NFA_START_INVISIBLE_NEG
3734 || c == NFA_START_INVISIBLE_BEFORE
3735 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3736 {
3737 int directly;
3738
3739 /* Do it directly when what follows is possibly the end of the
3740 * match. */
3741 if (match_follows(prog->state[i].out1->out, 0))
3742 directly = TRUE;
3743 else
3744 {
3745 int ch_invisible = failure_chance(prog->state[i].out, 0);
3746 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3747
3748 /* Postpone when the invisible match is expensive or has a
3749 * lower chance of failing. */
3750 if (c == NFA_START_INVISIBLE_BEFORE
3751 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3752 {
3753 /* "before" matches are very expensive when
3754 * unbounded, always prefer what follows then,
3755 * unless what follows will always match.
3756 * Otherwise strongly prefer what follows. */
3757 if (prog->state[i].val <= 0 && ch_follows > 0)
3758 directly = FALSE;
3759 else
3760 directly = ch_follows * 10 < ch_invisible;
3761 }
3762 else
3763 {
3764 /* normal invisible, first do the one with the
3765 * highest failure chance */
3766 directly = ch_follows < ch_invisible;
3767 }
3768 }
3769 if (directly)
3770 /* switch to the _FIRST state */
3771 ++prog->state[i].c;
3772 }
3773 }
3774}
3775
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003776/****************************************************************
3777 * NFA execution code.
3778 ****************************************************************/
3779
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003780typedef struct
3781{
3782 int in_use; /* number of subexpr with useful info */
3783
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003784 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003785 union
3786 {
3787 struct multipos
3788 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003789 linenr_T start_lnum;
3790 linenr_T end_lnum;
3791 colnr_T start_col;
3792 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003793 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003794 struct linepos
3795 {
3796 char_u *start;
3797 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003798 } line[NSUBEXP];
3799 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003800} regsub_T;
3801
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003802typedef struct
3803{
3804 regsub_T norm; /* \( .. \) matches */
3805#ifdef FEAT_SYN_HL
3806 regsub_T synt; /* \z( .. \) matches */
3807#endif
3808} regsubs_T;
3809
Bram Moolenaara2d95102013-06-04 14:23:05 +02003810/* nfa_pim_T stores a Postponed Invisible Match. */
3811typedef struct nfa_pim_S nfa_pim_T;
3812struct nfa_pim_S
3813{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003814 int result; /* NFA_PIM_*, see below */
3815 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003816 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003817 union
3818 {
3819 lpos_T pos;
3820 char_u *ptr;
3821 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003822};
3823
3824/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003825#define NFA_PIM_UNUSED 0 /* pim not used */
3826#define NFA_PIM_TODO 1 /* pim not done yet */
3827#define NFA_PIM_MATCH 2 /* pim executed, matches */
3828#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003829
3830
Bram Moolenaar963fee22013-05-26 21:47:28 +02003831/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003832typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003833{
3834 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003835 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003836 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3837 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003838 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003839} nfa_thread_T;
3840
Bram Moolenaar963fee22013-05-26 21:47:28 +02003841/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003842typedef struct
3843{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003844 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003845 int n; /* nr of states currently in "t" */
3846 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003847 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003848 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003849} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003850
Bram Moolenaar5714b802013-05-28 22:03:20 +02003851#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003852static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003853
3854 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003855log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003856{
3857 log_subexpr(&subs->norm);
3858# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003859 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003860 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003861# endif
3862}
3863
Bram Moolenaar5714b802013-05-28 22:03:20 +02003864 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003865log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003866{
3867 int j;
3868
3869 for (j = 0; j < sub->in_use; j++)
3870 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003871 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003872 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003873 sub->list.multi[j].start_col,
3874 (int)sub->list.multi[j].start_lnum,
3875 sub->list.multi[j].end_col,
3876 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003877 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003878 {
3879 char *s = (char *)sub->list.line[j].start;
3880 char *e = (char *)sub->list.line[j].end;
3881
Bram Moolenaar87953742013-06-05 18:52:40 +02003882 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003883 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003884 s == NULL ? "NULL" : s,
3885 e == NULL ? "NULL" : e);
3886 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003887}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003888
3889 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003890pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003891{
3892 static char buf[30];
3893
3894 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3895 buf[0] = NUL;
3896 else
3897 {
3898 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003899 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003900 }
3901 return buf;
3902}
3903
Bram Moolenaar5714b802013-05-28 22:03:20 +02003904#endif
3905
Bram Moolenaar963fee22013-05-26 21:47:28 +02003906/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003907static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003908#ifdef FEAT_RELTIME
3909static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003910static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003911static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003912#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003913
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003914static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003915static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003916
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003917/*
3918 * Copy postponed invisible match info from "from" to "to".
3919 */
3920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003921copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003922{
3923 to->result = from->result;
3924 to->state = from->state;
3925 copy_sub(&to->subs.norm, &from->subs.norm);
3926#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003927 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003928 copy_sub(&to->subs.synt, &from->subs.synt);
3929#endif
3930 to->end = from->end;
3931}
3932
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003934clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003935{
3936 if (REG_MULTI)
3937 /* Use 0xff to set lnum to -1 */
3938 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003939 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003940 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003941 vim_memset(sub->list.line, 0,
3942 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003943 sub->in_use = 0;
3944}
3945
3946/*
3947 * Copy the submatches from "from" to "to".
3948 */
3949 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003950copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003951{
3952 to->in_use = from->in_use;
3953 if (from->in_use > 0)
3954 {
3955 /* Copy the match start and end positions. */
3956 if (REG_MULTI)
3957 mch_memmove(&to->list.multi[0],
3958 &from->list.multi[0],
3959 sizeof(struct multipos) * from->in_use);
3960 else
3961 mch_memmove(&to->list.line[0],
3962 &from->list.line[0],
3963 sizeof(struct linepos) * from->in_use);
3964 }
3965}
3966
3967/*
3968 * Like copy_sub() but exclude the main match.
3969 */
3970 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003971copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003972{
3973 if (to->in_use < from->in_use)
3974 to->in_use = from->in_use;
3975 if (from->in_use > 1)
3976 {
3977 /* Copy the match start and end positions. */
3978 if (REG_MULTI)
3979 mch_memmove(&to->list.multi[1],
3980 &from->list.multi[1],
3981 sizeof(struct multipos) * (from->in_use - 1));
3982 else
3983 mch_memmove(&to->list.line[1],
3984 &from->list.line[1],
3985 sizeof(struct linepos) * (from->in_use - 1));
3986 }
3987}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003988
Bram Moolenaar428e9872013-05-30 17:05:39 +02003989/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003990 * Like copy_sub() but only do the end of the main match if \ze is present.
3991 */
3992 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003993copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003994{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003995 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003996 {
3997 if (REG_MULTI)
3998 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003999 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004000 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004001 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4002 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004003 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004004 }
4005 else
4006 {
4007 if (from->list.line[0].end != NULL)
4008 to->list.line[0].end = from->list.line[0].end;
4009 }
4010 }
4011}
4012
4013/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004014 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004015 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004016 */
4017 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004018sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004019{
4020 int i;
4021 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004022 linenr_T s1;
4023 linenr_T s2;
4024 char_u *sp1;
4025 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004026
4027 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4028 if (REG_MULTI)
4029 {
4030 for (i = 0; i < todo; ++i)
4031 {
4032 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004033 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004035 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004036 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004037 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004038 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004039 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004040 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004041 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004042 if (s1 != -1 && sub1->list.multi[i].start_col
4043 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004044 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004045
Bram Moolenaar0270f382018-07-17 05:43:58 +02004046 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004047 {
4048 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004049 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004050 else
4051 s1 = -1;
4052 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004053 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004054 else
4055 s2 = -1;
4056 if (s1 != s2)
4057 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004058 if (s1 != -1 && sub1->list.multi[i].end_col
4059 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004060 return FALSE;
4061 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004062 }
4063 }
4064 else
4065 {
4066 for (i = 0; i < todo; ++i)
4067 {
4068 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004069 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004070 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004071 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004072 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004073 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004075 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004076 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004077 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004078 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004079 {
4080 if (i < sub1->in_use)
4081 sp1 = sub1->list.line[i].end;
4082 else
4083 sp1 = NULL;
4084 if (i < sub2->in_use)
4085 sp2 = sub2->list.line[i].end;
4086 else
4087 sp2 = NULL;
4088 if (sp1 != sp2)
4089 return FALSE;
4090 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004091 }
4092 }
4093
4094 return TRUE;
4095}
4096
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004097#ifdef ENABLE_LOG
4098 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004099report_state(char *action,
4100 regsub_T *sub,
4101 nfa_state_T *state,
4102 int lid,
4103 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004104{
4105 int col;
4106
4107 if (sub->in_use <= 0)
4108 col = -1;
4109 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004110 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004111 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004112 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004113 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004114 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4115 action, abs(state->id), lid, state->c, code, col,
4116 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004117}
4118#endif
4119
Bram Moolenaar43e02982013-06-07 17:31:29 +02004120/*
4121 * Return TRUE if the same state is already in list "l" with the same
4122 * positions as "subs".
4123 */
4124 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004125has_state_with_pos(
4126 nfa_list_T *l, /* runtime state list */
4127 nfa_state_T *state, /* state to update */
4128 regsubs_T *subs, /* pointers to subexpressions */
4129 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004130{
4131 nfa_thread_T *thread;
4132 int i;
4133
4134 for (i = 0; i < l->n; ++i)
4135 {
4136 thread = &l->t[i];
4137 if (thread->state->id == state->id
4138 && sub_equal(&thread->subs.norm, &subs->norm)
4139#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004140 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004141 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004142#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004143 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004144 return TRUE;
4145 }
4146 return FALSE;
4147}
4148
4149/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004150 * Return TRUE if "one" and "two" are equal. That includes when both are not
4151 * set.
4152 */
4153 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004154pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004155{
4156 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4157 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4158
4159 if (one_unused)
4160 /* one is unused: equal when two is also unused */
4161 return two_unused;
4162 if (two_unused)
4163 /* one is used and two is not: not equal */
4164 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004165 /* compare the state id */
4166 if (one->state->id != two->state->id)
4167 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004168 /* compare the position */
4169 if (REG_MULTI)
4170 return one->end.pos.lnum == two->end.pos.lnum
4171 && one->end.pos.col == two->end.pos.col;
4172 return one->end.ptr == two->end.ptr;
4173}
4174
4175/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004176 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4177 */
4178 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004179match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004180{
4181 nfa_state_T *state = startstate;
4182
4183 /* avoid too much recursion */
4184 if (depth > 10)
4185 return FALSE;
4186
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004187 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004188 {
4189 switch (state->c)
4190 {
4191 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004192 case NFA_MCLOSE:
4193 case NFA_END_INVISIBLE:
4194 case NFA_END_INVISIBLE_NEG:
4195 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004196 return TRUE;
4197
4198 case NFA_SPLIT:
4199 return match_follows(state->out, depth + 1)
4200 || match_follows(state->out1, depth + 1);
4201
4202 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004203 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004204 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004205 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004206 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004207 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004208 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004209 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004210 case NFA_COMPOSING:
4211 /* skip ahead to next state */
4212 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004213 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004214
4215 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004216 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004217 case NFA_IDENT:
4218 case NFA_SIDENT:
4219 case NFA_KWORD:
4220 case NFA_SKWORD:
4221 case NFA_FNAME:
4222 case NFA_SFNAME:
4223 case NFA_PRINT:
4224 case NFA_SPRINT:
4225 case NFA_WHITE:
4226 case NFA_NWHITE:
4227 case NFA_DIGIT:
4228 case NFA_NDIGIT:
4229 case NFA_HEX:
4230 case NFA_NHEX:
4231 case NFA_OCTAL:
4232 case NFA_NOCTAL:
4233 case NFA_WORD:
4234 case NFA_NWORD:
4235 case NFA_HEAD:
4236 case NFA_NHEAD:
4237 case NFA_ALPHA:
4238 case NFA_NALPHA:
4239 case NFA_LOWER:
4240 case NFA_NLOWER:
4241 case NFA_UPPER:
4242 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004243 case NFA_LOWER_IC:
4244 case NFA_NLOWER_IC:
4245 case NFA_UPPER_IC:
4246 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004247 case NFA_START_COLL:
4248 case NFA_START_NEG_COLL:
4249 case NFA_NEWL:
4250 /* state will advance input */
4251 return FALSE;
4252
4253 default:
4254 if (state->c > 0)
4255 /* state will advance input */
4256 return FALSE;
4257
4258 /* Others: zero-width or possibly zero-width, might still find
4259 * a match at the same position, keep looking. */
4260 break;
4261 }
4262 state = state->out;
4263 }
4264 return FALSE;
4265}
4266
4267
4268/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004269 * Return TRUE if "state" is already in list "l".
4270 */
4271 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004272state_in_list(
4273 nfa_list_T *l, /* runtime state list */
4274 nfa_state_T *state, /* state to update */
4275 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004276{
4277 if (state->lastlist[nfa_ll_index] == l->id)
4278 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004279 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004280 return TRUE;
4281 }
4282 return FALSE;
4283}
4284
Bram Moolenaar16b35782016-09-09 20:29:50 +02004285/* Offset used for "off" by addstate_here(). */
4286#define ADDSTATE_HERE_OFFSET 10
4287
Bram Moolenaard05bf562013-06-30 23:24:08 +02004288/*
4289 * Add "state" and possibly what follows to state list ".".
4290 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004291 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004292 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004293 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004294addstate(
4295 nfa_list_T *l, /* runtime state list */
4296 nfa_state_T *state, /* state to update */
4297 regsubs_T *subs_arg, /* pointers to subexpressions */
4298 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004299 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004300{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004301 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004302 int off = off_arg;
4303 int add_here = FALSE;
4304 int listindex = 0;
4305 int k;
4306 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004307 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004308 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004309 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004310 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004311 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004312 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004313 regsubs_T *subs = subs_arg;
4314 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004315#ifdef ENABLE_LOG
4316 int did_print = FALSE;
4317#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004318 static int depth = 0;
4319
4320 // This function is called recursively. When the depth is too much we run
4321 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004322 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004323 {
4324 --depth;
4325 return NULL;
4326 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004327
Bram Moolenaar16b35782016-09-09 20:29:50 +02004328 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4329 {
4330 add_here = TRUE;
4331 off = 0;
4332 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4333 }
4334
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004335 switch (state->c)
4336 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004337 case NFA_NCLOSE:
4338 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004339 case NFA_MCLOSE1:
4340 case NFA_MCLOSE2:
4341 case NFA_MCLOSE3:
4342 case NFA_MCLOSE4:
4343 case NFA_MCLOSE5:
4344 case NFA_MCLOSE6:
4345 case NFA_MCLOSE7:
4346 case NFA_MCLOSE8:
4347 case NFA_MCLOSE9:
4348#ifdef FEAT_SYN_HL
4349 case NFA_ZCLOSE:
4350 case NFA_ZCLOSE1:
4351 case NFA_ZCLOSE2:
4352 case NFA_ZCLOSE3:
4353 case NFA_ZCLOSE4:
4354 case NFA_ZCLOSE5:
4355 case NFA_ZCLOSE6:
4356 case NFA_ZCLOSE7:
4357 case NFA_ZCLOSE8:
4358 case NFA_ZCLOSE9:
4359#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004360 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004361 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004362 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004363 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004364 /* These nodes are not added themselves but their "out" and/or
4365 * "out1" may be added below. */
4366 break;
4367
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004368 case NFA_BOL:
4369 case NFA_BOF:
4370 /* "^" won't match past end-of-line, don't bother trying.
4371 * Except when at the end of the line, or when we are going to the
4372 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004373 if (rex.input > rex.line
4374 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004375 && (nfa_endp == NULL
4376 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004377 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004378 goto skip_add;
4379 /* FALLTHROUGH */
4380
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004381 case NFA_MOPEN1:
4382 case NFA_MOPEN2:
4383 case NFA_MOPEN3:
4384 case NFA_MOPEN4:
4385 case NFA_MOPEN5:
4386 case NFA_MOPEN6:
4387 case NFA_MOPEN7:
4388 case NFA_MOPEN8:
4389 case NFA_MOPEN9:
4390#ifdef FEAT_SYN_HL
4391 case NFA_ZOPEN:
4392 case NFA_ZOPEN1:
4393 case NFA_ZOPEN2:
4394 case NFA_ZOPEN3:
4395 case NFA_ZOPEN4:
4396 case NFA_ZOPEN5:
4397 case NFA_ZOPEN6:
4398 case NFA_ZOPEN7:
4399 case NFA_ZOPEN8:
4400 case NFA_ZOPEN9:
4401#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004402 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004403 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004404 /* These nodes need to be added so that we can bail out when it
4405 * was added to this list before at the same position to avoid an
4406 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004407
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004409 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004410 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004411 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004412 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004413 * when there is a PIM. For NFA_MATCH check the position,
4414 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004415 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004416 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004417 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004418 /* When called from addstate_here() do insert before
4419 * existing states. */
4420 if (add_here)
4421 {
4422 for (k = 0; k < l->n && k < listindex; ++k)
4423 if (l->t[k].state->id == state->id)
4424 {
4425 found = TRUE;
4426 break;
4427 }
4428 }
4429 if (!add_here || found)
4430 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004431skip_add:
4432#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004433 nfa_set_code(state->c);
4434 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4435 abs(state->id), l->id, state->c, code,
4436 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004437#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004438 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004439 return subs;
4440 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004441 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004442
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004443 /* Do not add the state again when it exists with the same
4444 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004445 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004446 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004447 }
4448
Bram Moolenaar688b3982019-02-13 21:47:36 +01004449 // When there are backreferences or PIMs the number of states may
4450 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004451 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004452 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004453 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004454 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004455 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004456
Bram Moolenaar688b3982019-02-13 21:47:36 +01004457 if ((long)(newsize >> 10) >= p_mmp)
4458 {
4459 emsg(_(e_maxmempat));
4460 --depth;
4461 return NULL;
4462 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004463 if (subs != &temp_subs)
4464 {
4465 /* "subs" may point into the current array, need to make a
4466 * copy before it becomes invalid. */
4467 copy_sub(&temp_subs.norm, &subs->norm);
4468#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004469 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004470 copy_sub(&temp_subs.synt, &subs->synt);
4471#endif
4472 subs = &temp_subs;
4473 }
4474
Bram Moolenaar688b3982019-02-13 21:47:36 +01004475 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004476 if (newt == NULL)
4477 {
4478 // out of memory
4479 --depth;
4480 return NULL;
4481 }
4482 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004483 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004484 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004485
4486 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004487 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004488 thread = &l->t[l->n++];
4489 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004490 if (pim == NULL)
4491 thread->pim.result = NFA_PIM_UNUSED;
4492 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004493 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004494 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004495 l->has_pim = TRUE;
4496 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004497 copy_sub(&thread->subs.norm, &subs->norm);
4498#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004499 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004500 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004501#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004502#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004503 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004504 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004505#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004506 }
4507
4508#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004509 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004510 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004511#endif
4512 switch (state->c)
4513 {
4514 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004515 break;
4516
4517 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004518 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004519 subs = addstate(l, state->out, subs, pim, off_arg);
4520 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004521 break;
4522
Bram Moolenaar699c1202013-09-25 16:41:54 +02004523 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 case NFA_NOPEN:
4525 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004526 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004527 break;
4528
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004529 case NFA_MOPEN:
4530 case NFA_MOPEN1:
4531 case NFA_MOPEN2:
4532 case NFA_MOPEN3:
4533 case NFA_MOPEN4:
4534 case NFA_MOPEN5:
4535 case NFA_MOPEN6:
4536 case NFA_MOPEN7:
4537 case NFA_MOPEN8:
4538 case NFA_MOPEN9:
4539#ifdef FEAT_SYN_HL
4540 case NFA_ZOPEN:
4541 case NFA_ZOPEN1:
4542 case NFA_ZOPEN2:
4543 case NFA_ZOPEN3:
4544 case NFA_ZOPEN4:
4545 case NFA_ZOPEN5:
4546 case NFA_ZOPEN6:
4547 case NFA_ZOPEN7:
4548 case NFA_ZOPEN8:
4549 case NFA_ZOPEN9:
4550#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004551 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004552 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004553 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004555 sub = &subs->norm;
4556 }
4557#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004558 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004559 {
4560 subidx = state->c - NFA_ZOPEN;
4561 sub = &subs->synt;
4562 }
4563#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004564 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004565 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004566 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004567 sub = &subs->norm;
4568 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004569
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004570 /* avoid compiler warnings */
4571 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004572 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004573
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004574 /* Set the position (with "off" added) in the subexpression. Save
4575 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004576 if (REG_MULTI)
4577 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004578 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004579 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004580 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004581 save_in_use = -1;
4582 }
4583 else
4584 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004585 save_in_use = sub->in_use;
4586 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004587 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004588 sub->list.multi[i].start_lnum = -1;
4589 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004590 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004591 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004592 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004593 if (off == -1)
4594 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004595 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004596 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004597 }
4598 else
4599 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004600 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004601 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004602 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004603 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004604 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004605 }
4606 else
4607 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004608 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004609 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004610 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004611 save_in_use = -1;
4612 }
4613 else
4614 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004615 save_in_use = sub->in_use;
4616 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004617 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004618 sub->list.line[i].start = NULL;
4619 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004620 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004621 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004622 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004623 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004624 }
4625
Bram Moolenaar16b35782016-09-09 20:29:50 +02004626 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004627 if (subs == NULL)
4628 break;
4629 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004630#ifdef FEAT_SYN_HL
4631 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4632 sub = &subs->synt;
4633 else
4634#endif
4635 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004637 if (save_in_use == -1)
4638 {
4639 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004640 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004641 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004642 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004644 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004645 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004646 break;
4647
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004648 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004649 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004650 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004651 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004652 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004653 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004654 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004655 break;
4656 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004657 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004658 case NFA_MCLOSE1:
4659 case NFA_MCLOSE2:
4660 case NFA_MCLOSE3:
4661 case NFA_MCLOSE4:
4662 case NFA_MCLOSE5:
4663 case NFA_MCLOSE6:
4664 case NFA_MCLOSE7:
4665 case NFA_MCLOSE8:
4666 case NFA_MCLOSE9:
4667#ifdef FEAT_SYN_HL
4668 case NFA_ZCLOSE:
4669 case NFA_ZCLOSE1:
4670 case NFA_ZCLOSE2:
4671 case NFA_ZCLOSE3:
4672 case NFA_ZCLOSE4:
4673 case NFA_ZCLOSE5:
4674 case NFA_ZCLOSE6:
4675 case NFA_ZCLOSE7:
4676 case NFA_ZCLOSE8:
4677 case NFA_ZCLOSE9:
4678#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004681 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004683 sub = &subs->norm;
4684 }
4685#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004686 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004687 {
4688 subidx = state->c - NFA_ZCLOSE;
4689 sub = &subs->synt;
4690 }
4691#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004692 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004693 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004694 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004695 sub = &subs->norm;
4696 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004697
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004698 /* We don't fill in gaps here, there must have been an MOPEN that
4699 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004700 save_in_use = sub->in_use;
4701 if (sub->in_use <= subidx)
4702 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004703 if (REG_MULTI)
4704 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004705 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004706 if (off == -1)
4707 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004708 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004709 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004710 }
4711 else
4712 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004713 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004714 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004715 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004716 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004717 /* avoid compiler warnings */
4718 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719 }
4720 else
4721 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004722 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004723 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004724 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004725 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004726 }
4727
Bram Moolenaar16b35782016-09-09 20:29:50 +02004728 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004729 if (subs == NULL)
4730 break;
Bram Moolenaarebefd992013-08-14 14:18:40 +02004731 /* "subs" may have changed, need to set "sub" again */
4732#ifdef FEAT_SYN_HL
4733 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4734 sub = &subs->synt;
4735 else
4736#endif
4737 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004738
4739 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004740 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004741 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004742 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004743 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004744 break;
4745 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004746 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004747 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004748}
4749
4750/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004751 * Like addstate(), but the new state(s) are put at position "*ip".
4752 * Used for zero-width matches, next state to use is the added one.
4753 * This makes sure the order of states to be tried does not change, which
4754 * matters for alternatives.
4755 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004756 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004757addstate_here(
4758 nfa_list_T *l, /* runtime state list */
4759 nfa_state_T *state, /* state to update */
4760 regsubs_T *subs, /* pointers to subexpressions */
4761 nfa_pim_T *pim, /* postponed look-behind match */
4762 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004763{
4764 int tlen = l->n;
4765 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004766 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004767 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004768
Bram Moolenaar16b35782016-09-09 20:29:50 +02004769 /* First add the state(s) at the end, so that we know how many there are.
4770 * Pass the listidx as offset (avoids adding another argument to
4771 * addstate(). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004772 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4773 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004774 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004775
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004776 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004777 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004778 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004779
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004780 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004781 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004782 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004783 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004784 if (count == 1)
4785 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004786 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004787 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004788 }
4789 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004790 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004791 if (l->n + count - 1 >= l->len)
4792 {
4793 /* not enough space to move the new states, reallocate the list
4794 * and move the states to the right position */
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004795 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004796 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004797 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004798
Bram Moolenaar688b3982019-02-13 21:47:36 +01004799 if ((long)(newsize >> 10) >= p_mmp)
4800 {
4801 emsg(_(e_maxmempat));
4802 return NULL;
4803 }
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01004804 newl = (nfa_thread_T *)alloc((int)newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004805 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004806 return NULL;
4807 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004808 mch_memmove(&(newl[0]),
4809 &(l->t[0]),
4810 sizeof(nfa_thread_T) * listidx);
4811 mch_memmove(&(newl[listidx]),
4812 &(l->t[l->n - count]),
4813 sizeof(nfa_thread_T) * count);
4814 mch_memmove(&(newl[listidx + count]),
4815 &(l->t[listidx + 1]),
4816 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4817 vim_free(l->t);
4818 l->t = newl;
4819 }
4820 else
4821 {
4822 /* make space for new states, then move them from the
4823 * end to the current position */
4824 mch_memmove(&(l->t[listidx + count]),
4825 &(l->t[listidx + 1]),
4826 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4827 mch_memmove(&(l->t[listidx]),
4828 &(l->t[l->n - 1]),
4829 sizeof(nfa_thread_T) * count);
4830 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004831 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004832 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004833 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004834
4835 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004836}
4837
4838/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004839 * Check character class "class" against current character c.
4840 */
4841 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004842check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004843{
4844 switch (class)
4845 {
4846 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004847 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004848 return OK;
4849 break;
4850 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004851 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004852 return OK;
4853 break;
4854 case NFA_CLASS_BLANK:
4855 if (c == ' ' || c == '\t')
4856 return OK;
4857 break;
4858 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004859 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004860 return OK;
4861 break;
4862 case NFA_CLASS_DIGIT:
4863 if (VIM_ISDIGIT(c))
4864 return OK;
4865 break;
4866 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004867 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004868 return OK;
4869 break;
4870 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004871 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 return OK;
4873 break;
4874 case NFA_CLASS_PRINT:
4875 if (vim_isprintc(c))
4876 return OK;
4877 break;
4878 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004879 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880 return OK;
4881 break;
4882 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004883 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884 return OK;
4885 break;
4886 case NFA_CLASS_UPPER:
4887 if (MB_ISUPPER(c))
4888 return OK;
4889 break;
4890 case NFA_CLASS_XDIGIT:
4891 if (vim_isxdigit(c))
4892 return OK;
4893 break;
4894 case NFA_CLASS_TAB:
4895 if (c == '\t')
4896 return OK;
4897 break;
4898 case NFA_CLASS_RETURN:
4899 if (c == '\r')
4900 return OK;
4901 break;
4902 case NFA_CLASS_BACKSPACE:
4903 if (c == '\b')
4904 return OK;
4905 break;
4906 case NFA_CLASS_ESCAPE:
4907 if (c == '\033')
4908 return OK;
4909 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004910 case NFA_CLASS_IDENT:
4911 if (vim_isIDc(c))
4912 return OK;
4913 break;
4914 case NFA_CLASS_KEYWORD:
4915 if (reg_iswordc(c))
4916 return OK;
4917 break;
4918 case NFA_CLASS_FNAME:
4919 if (vim_isfilec(c))
4920 return OK;
4921 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922
4923 default:
4924 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004925 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004926 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004927 }
4928 return FAIL;
4929}
4930
Bram Moolenaar5714b802013-05-28 22:03:20 +02004931/*
4932 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004933 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004934 */
4935 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004936match_backref(
4937 regsub_T *sub, /* pointers to subexpressions */
4938 int subidx,
4939 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004940{
4941 int len;
4942
4943 if (sub->in_use <= subidx)
4944 {
4945retempty:
4946 /* backref was not set, match an empty string */
4947 *bytelen = 0;
4948 return TRUE;
4949 }
4950
4951 if (REG_MULTI)
4952 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004953 if (sub->list.multi[subidx].start_lnum < 0
4954 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004955 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004956 if (sub->list.multi[subidx].start_lnum == rex.lnum
4957 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004958 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004959 len = sub->list.multi[subidx].end_col
4960 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004961 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4962 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004963 {
4964 *bytelen = len;
4965 return TRUE;
4966 }
4967 }
4968 else
4969 {
4970 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004971 sub->list.multi[subidx].start_lnum,
4972 sub->list.multi[subidx].start_col,
4973 sub->list.multi[subidx].end_lnum,
4974 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004975 bytelen) == RA_MATCH)
4976 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004977 }
4978 }
4979 else
4980 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004981 if (sub->list.line[subidx].start == NULL
4982 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004983 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004984 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004985 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004986 {
4987 *bytelen = len;
4988 return TRUE;
4989 }
4990 }
4991 return FALSE;
4992}
4993
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004994#ifdef FEAT_SYN_HL
4995
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004996/*
4997 * Check for a match with \z subexpression "subidx".
4998 * Return TRUE if it matches.
4999 */
5000 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005001match_zref(
5002 int subidx,
5003 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005004{
5005 int len;
5006
5007 cleanup_zsubexpr();
5008 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5009 {
5010 /* backref was not set, match an empty string */
5011 *bytelen = 0;
5012 return TRUE;
5013 }
5014
5015 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005016 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005017 {
5018 *bytelen = len;
5019 return TRUE;
5020 }
5021 return FALSE;
5022}
5023#endif
5024
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005025/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005026 * Save list IDs for all NFA states of "prog" into "list".
5027 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005028 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029 */
5030 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005031nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005033 int i;
5034 nfa_state_T *p;
5035
5036 /* Order in the list is reverse, it's a bit faster that way. */
5037 p = &prog->state[0];
5038 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005039 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005040 list[i] = p->lastlist[1];
5041 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005042 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 }
5044}
5045
5046/*
5047 * Restore list IDs from "list" to all NFA states.
5048 */
5049 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005050nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005052 int i;
5053 nfa_state_T *p;
5054
5055 p = &prog->state[0];
5056 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005057 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005058 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005059 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005060 }
5061}
5062
Bram Moolenaar423532e2013-05-29 21:14:42 +02005063 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005064nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005065{
5066 if (op == 1) return pos > val;
5067 if (op == 2) return pos < val;
5068 return val == pos;
5069}
5070
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005071static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005072
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005073/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005074 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005075 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5076 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005077 */
5078 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005079recursive_regmatch(
5080 nfa_state_T *state,
5081 nfa_pim_T *pim,
5082 nfa_regprog_T *prog,
5083 regsubs_T *submatch,
5084 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005085 int **listids,
5086 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005087{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005088 int save_reginput_col = (int)(rex.input - rex.line);
5089 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005090 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005091 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092 save_se_T *save_nfa_endp = nfa_endp;
5093 save_se_T endpos;
5094 save_se_T *endposp = NULL;
5095 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005096 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005098 if (pim != NULL)
5099 {
5100 /* start at the position where the postponed match was */
5101 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005102 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005103 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005104 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005105 }
5106
Bram Moolenaardecd9542013-06-07 16:31:50 +02005107 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005108 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5109 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5110 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005111 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005112 /* The recursive match must end at the current position. When "pim" is
5113 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114 endposp = &endpos;
5115 if (REG_MULTI)
5116 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005117 if (pim == NULL)
5118 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005119 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5120 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005121 }
5122 else
5123 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005124 }
5125 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005126 {
5127 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005128 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005129 else
5130 endpos.se_u.ptr = pim->end.ptr;
5131 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005132
5133 /* Go back the specified number of bytes, or as far as the
5134 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005135 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005136 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005137 if (state->val <= 0)
5138 {
5139 if (REG_MULTI)
5140 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005141 rex.line = reg_getline(--rex.lnum);
5142 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005143 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005144 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005145 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005146 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005147 }
5148 else
5149 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005150 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151 {
5152 /* Not enough bytes in this line, go to end of
5153 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005154 rex.line = reg_getline(--rex.lnum);
5155 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005156 {
5157 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005158 rex.line = reg_getline(++rex.lnum);
5159 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005160 }
5161 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005162 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005163 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005164 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005165 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005166 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005167 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005168 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005169 }
5170 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005171 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005172 }
5173 }
5174
Bram Moolenaarf46da702013-06-02 22:37:42 +02005175#ifdef ENABLE_LOG
5176 if (log_fd != stderr)
5177 fclose(log_fd);
5178 log_fd = NULL;
5179#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005180 /* Have to clear the lastlist field of the NFA nodes, so that
5181 * nfa_regmatch() and addstate() can run properly after recursion. */
5182 if (nfa_ll_index == 1)
5183 {
5184 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5185 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005186 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005187 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005188 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005189 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005190 if (*listids == NULL)
5191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005192 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005193 return 0;
5194 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005195 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005196 }
5197 nfa_save_listids(prog, *listids);
5198 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005199 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005200 }
5201 else
5202 {
5203 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005204 * entry. Make sure rex.nfa_listid is different from a previous
5205 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005206 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005207 if (rex.nfa_listid <= rex.nfa_alt_listid)
5208 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005209 }
5210
5211 /* Call nfa_regmatch() to check if the current concat matches at this
5212 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005213 nfa_endp = endposp;
5214 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005215
5216 if (need_restore)
5217 nfa_restore_listids(prog, *listids);
5218 else
5219 {
5220 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005221 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005222 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005223
5224 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005225 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005226 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005227 rex.line = reg_getline(rex.lnum);
5228 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005229 if (result != NFA_TOO_EXPENSIVE)
5230 {
5231 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005232 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005233 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005234 nfa_endp = save_nfa_endp;
5235
5236#ifdef ENABLE_LOG
5237 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5238 if (log_fd != NULL)
5239 {
5240 fprintf(log_fd, "****************************\n");
5241 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5242 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5243 fprintf(log_fd, "****************************\n");
5244 }
5245 else
5246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005247 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005248 log_fd = stderr;
5249 }
5250#endif
5251
5252 return result;
5253}
5254
Bram Moolenaara2d95102013-06-04 14:23:05 +02005255/*
5256 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005257 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005258 * NFA_ANY: 1
5259 * specific character: 99
5260 */
5261 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005262failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005263{
5264 int c = state->c;
5265 int l, r;
5266
5267 /* detect looping */
5268 if (depth > 4)
5269 return 1;
5270
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005271 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005272 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005273 case NFA_SPLIT:
5274 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5275 /* avoid recursive stuff */
5276 return 1;
5277 /* two alternatives, use the lowest failure chance */
5278 l = failure_chance(state->out, depth + 1);
5279 r = failure_chance(state->out1, depth + 1);
5280 return l < r ? l : r;
5281
5282 case NFA_ANY:
5283 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005284 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005285
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005286 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005287 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005288 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005289 /* empty match works always */
5290 return 0;
5291
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005292 case NFA_START_INVISIBLE:
5293 case NFA_START_INVISIBLE_FIRST:
5294 case NFA_START_INVISIBLE_NEG:
5295 case NFA_START_INVISIBLE_NEG_FIRST:
5296 case NFA_START_INVISIBLE_BEFORE:
5297 case NFA_START_INVISIBLE_BEFORE_FIRST:
5298 case NFA_START_INVISIBLE_BEFORE_NEG:
5299 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5300 case NFA_START_PATTERN:
5301 /* recursive regmatch is expensive, use low failure chance */
5302 return 5;
5303
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005304 case NFA_BOL:
5305 case NFA_EOL:
5306 case NFA_BOF:
5307 case NFA_EOF:
5308 case NFA_NEWL:
5309 return 99;
5310
5311 case NFA_BOW:
5312 case NFA_EOW:
5313 return 90;
5314
5315 case NFA_MOPEN:
5316 case NFA_MOPEN1:
5317 case NFA_MOPEN2:
5318 case NFA_MOPEN3:
5319 case NFA_MOPEN4:
5320 case NFA_MOPEN5:
5321 case NFA_MOPEN6:
5322 case NFA_MOPEN7:
5323 case NFA_MOPEN8:
5324 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005325#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005326 case NFA_ZOPEN:
5327 case NFA_ZOPEN1:
5328 case NFA_ZOPEN2:
5329 case NFA_ZOPEN3:
5330 case NFA_ZOPEN4:
5331 case NFA_ZOPEN5:
5332 case NFA_ZOPEN6:
5333 case NFA_ZOPEN7:
5334 case NFA_ZOPEN8:
5335 case NFA_ZOPEN9:
5336 case NFA_ZCLOSE:
5337 case NFA_ZCLOSE1:
5338 case NFA_ZCLOSE2:
5339 case NFA_ZCLOSE3:
5340 case NFA_ZCLOSE4:
5341 case NFA_ZCLOSE5:
5342 case NFA_ZCLOSE6:
5343 case NFA_ZCLOSE7:
5344 case NFA_ZCLOSE8:
5345 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005346#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005347 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005348 case NFA_MCLOSE1:
5349 case NFA_MCLOSE2:
5350 case NFA_MCLOSE3:
5351 case NFA_MCLOSE4:
5352 case NFA_MCLOSE5:
5353 case NFA_MCLOSE6:
5354 case NFA_MCLOSE7:
5355 case NFA_MCLOSE8:
5356 case NFA_MCLOSE9:
5357 case NFA_NCLOSE:
5358 return failure_chance(state->out, depth + 1);
5359
5360 case NFA_BACKREF1:
5361 case NFA_BACKREF2:
5362 case NFA_BACKREF3:
5363 case NFA_BACKREF4:
5364 case NFA_BACKREF5:
5365 case NFA_BACKREF6:
5366 case NFA_BACKREF7:
5367 case NFA_BACKREF8:
5368 case NFA_BACKREF9:
5369#ifdef FEAT_SYN_HL
5370 case NFA_ZREF1:
5371 case NFA_ZREF2:
5372 case NFA_ZREF3:
5373 case NFA_ZREF4:
5374 case NFA_ZREF5:
5375 case NFA_ZREF6:
5376 case NFA_ZREF7:
5377 case NFA_ZREF8:
5378 case NFA_ZREF9:
5379#endif
5380 /* backreferences don't match in many places */
5381 return 94;
5382
5383 case NFA_LNUM_GT:
5384 case NFA_LNUM_LT:
5385 case NFA_COL_GT:
5386 case NFA_COL_LT:
5387 case NFA_VCOL_GT:
5388 case NFA_VCOL_LT:
5389 case NFA_MARK_GT:
5390 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005391 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005392 /* before/after positions don't match very often */
5393 return 85;
5394
5395 case NFA_LNUM:
5396 return 90;
5397
5398 case NFA_CURSOR:
5399 case NFA_COL:
5400 case NFA_VCOL:
5401 case NFA_MARK:
5402 /* specific positions rarely match */
5403 return 98;
5404
5405 case NFA_COMPOSING:
5406 return 95;
5407
5408 default:
5409 if (c > 0)
5410 /* character match fails often */
5411 return 95;
5412 }
5413
5414 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005415 return 50;
5416}
5417
Bram Moolenaarf46da702013-06-02 22:37:42 +02005418/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005419 * Skip until the char "c" we know a match must start with.
5420 */
5421 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005422skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005423{
5424 char_u *s;
5425
5426 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005427 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005428 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005429 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005430 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005431 if (s == NULL)
5432 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005433 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005434 return OK;
5435}
5436
5437/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005438 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005439 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005440 * Returns zero for no match, 1 for a match.
5441 */
5442 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005443find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005444{
5445 colnr_T col = startcol;
5446 int c1, c2;
5447 int len1, len2;
5448 int match;
5449
5450 for (;;)
5451 {
5452 match = TRUE;
5453 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5454 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5455 {
5456 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005457 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005458 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005459 {
5460 match = FALSE;
5461 break;
5462 }
5463 len2 += MB_CHAR2LEN(c2);
5464 }
5465 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005466 /* check that no composing char follows */
5467 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005468 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005469 {
5470 cleanup_subexpr();
5471 if (REG_MULTI)
5472 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005473 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005474 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005475 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005476 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005477 }
5478 else
5479 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005480 rex.reg_startp[0] = rex.line + col;
5481 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005482 }
5483 return 1L;
5484 }
5485
5486 /* Try finding regstart after the current match. */
5487 col += MB_CHAR2LEN(regstart); /* skip regstart */
5488 if (skip_to_start(regstart, &col) == FAIL)
5489 break;
5490 }
5491 return 0L;
5492}
5493
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005494#ifdef FEAT_RELTIME
5495 static int
5496nfa_did_time_out()
5497{
5498 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5499 {
5500 if (nfa_timed_out != NULL)
5501 *nfa_timed_out = TRUE;
5502 return TRUE;
5503 }
5504 return FALSE;
5505}
5506#endif
5507
Bram Moolenaar473de612013-06-08 18:19:48 +02005508/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005509 * Main matching routine.
5510 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005511 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005513 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005514 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005516 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005517 * Note: Caller must ensure that: start != NULL.
5518 */
5519 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005520nfa_regmatch(
5521 nfa_regprog_T *prog,
5522 nfa_state_T *start,
5523 regsubs_T *submatch,
5524 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005525{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005527 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005529 int go_to_nextline = FALSE;
5530 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005531 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005532 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005533 nfa_list_T *thislist;
5534 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005535 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005536 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005537 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005538 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005539 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005540 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005541 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005542 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005543#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005544 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005545#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005546
Bram Moolenaar41f12052013-08-25 17:01:42 +02005547 /* Some patterns may take a long time to match, especially when using
5548 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5549 fast_breakcheck();
5550 if (got_int)
5551 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005552#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005553 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005554 return FALSE;
5555#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005556
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005557#ifdef NFA_REGEXP_DEBUG_LOG
5558 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5559 if (debug == NULL)
5560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005561 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005562 return FALSE;
5563 }
5564#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005565 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005566
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005567 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005568 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005569
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005570 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005571 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005572 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005573 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005574 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005575 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005576
5577#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005578 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005579 if (log_fd != NULL)
5580 {
5581 fprintf(log_fd, "**********************************\n");
5582 nfa_set_code(start->c);
5583 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5584 abs(start->id), code);
5585 fprintf(log_fd, "**********************************\n");
5586 }
5587 else
5588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005589 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005590 log_fd = stderr;
5591 }
5592#endif
5593
5594 thislist = &list[0];
5595 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005596 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597 nextlist = &list[1];
5598 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005599 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005600#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005601 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005602#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005603 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005604
5605 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5606 * it's the first MOPEN. */
5607 if (toplevel)
5608 {
5609 if (REG_MULTI)
5610 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005611 m->norm.list.multi[0].start_lnum = rex.lnum;
5612 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005613 }
5614 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005615 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005616 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005617 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005618 }
5619 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005620 r = addstate(thislist, start, m, NULL, 0);
5621 if (r == NULL)
5622 {
5623 nfa_match = NFA_TOO_EXPENSIVE;
5624 goto theend;
5625 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005626
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005627#define ADD_STATE_IF_MATCH(state) \
5628 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005629 add_state = state->out; \
5630 add_off = clen; \
5631 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005632
5633 /*
5634 * Run for each character.
5635 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005636 for (;;)
5637 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005638 int curc;
5639 int clen;
5640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641 if (has_mbyte)
5642 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005643 curc = (*mb_ptr2char)(rex.input);
5644 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005645 }
5646 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005647 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005648 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005651 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005652 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005653 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005654 go_to_nextline = FALSE;
5655 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005656
5657 /* swap lists */
5658 thislist = &list[flag];
5659 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005660 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005661 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005662 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005663 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005664 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005665# ifdef FEAT_EVAL
5666 || nfa_fail_for_testing
5667# endif
5668 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005669 {
5670 /* too many states, retry with old engine */
5671 nfa_match = NFA_TOO_EXPENSIVE;
5672 goto theend;
5673 }
5674
Bram Moolenaar0270f382018-07-17 05:43:58 +02005675 thislist->id = rex.nfa_listid;
5676 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005677
5678#ifdef ENABLE_LOG
5679 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005680 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005681 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005682 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005683 {
5684 int i;
5685
5686 for (i = 0; i < thislist->n; i++)
5687 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5688 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005689 fprintf(log_fd, "\n");
5690#endif
5691
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005692#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005693 fprintf(debug, "\n-------------------\n");
5694#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005695 /*
5696 * If the state lists are empty we can stop.
5697 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005698 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005699 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005700
5701 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005702 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005704 /* If the list gets very long there probably is something wrong.
5705 * At least allow interrupting with CTRL-C. */
5706 fast_breakcheck();
5707 if (got_int)
5708 break;
5709#ifdef FEAT_RELTIME
5710 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5711 {
5712 nfa_time_count = 0;
5713 if (nfa_did_time_out())
5714 break;
5715 }
5716#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005717 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005718
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005719#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005720 nfa_set_code(t->state->c);
5721 fprintf(debug, "%s, ", code);
5722#endif
5723#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005724 {
5725 int col;
5726
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005727 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005728 col = -1;
5729 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005730 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005731 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005732 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005733 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005734 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005735 abs(t->state->id), (int)t->state->c, code, col,
5736 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005737 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005738#endif
5739
5740 /*
5741 * Handle the possible codes of the current state.
5742 * The most important is NFA_MATCH.
5743 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005744 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005745 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005746 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005747 switch (t->state->c)
5748 {
5749 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005750 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005751 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005752 * rex.reg_icombine is not set, that is not really a match. */
5753 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005754 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005755
Bram Moolenaar963fee22013-05-26 21:47:28 +02005756 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005757 copy_sub(&submatch->norm, &t->subs.norm);
5758#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005759 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005760 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005761#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005762#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005763 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005764#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005765 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005766 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005767 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005768 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005769 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005770 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005771 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005772 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005773
5774 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005775 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005776 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005777 /*
5778 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005779 * NFA_START_INVISIBLE_BEFORE node.
5780 * They surround a zero-width group, used with "\@=", "\&",
5781 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005782 * If we got here, it means that the current "invisible" group
5783 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005784 * nfa_regmatch(). For a look-behind match only when it ends
5785 * in the position in "nfa_endp".
5786 * Submatches are stored in *m, and used in the parent call.
5787 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005788#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005789 if (nfa_endp != NULL)
5790 {
5791 if (REG_MULTI)
5792 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005793 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005794 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005795 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005796 nfa_endp->se_u.pos.col);
5797 else
5798 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005799 (int)(rex.input - rex.line),
5800 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005801 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005802#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005803 /* If "nfa_endp" is set it's only a match if it ends at
5804 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005805 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005806 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5807 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005808 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005809 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005810 break;
5811
5812 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005813 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005814 {
5815 copy_sub(&m->norm, &t->subs.norm);
5816#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005817 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005818 copy_sub(&m->synt, &t->subs.synt);
5819#endif
5820 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005821#ifdef ENABLE_LOG
5822 fprintf(log_fd, "Match found:\n");
5823 log_subsexpr(m);
5824#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005825 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005826 /* See comment above at "goto nextchar". */
5827 if (nextlist->n == 0)
5828 clen = 0;
5829 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005830
5831 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005832 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005833 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005834 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005835 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005836 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005837 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005838 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005839 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005840#ifdef ENABLE_LOG
5841 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5842 failure_chance(t->state->out, 0),
5843 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005844#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005845 /* Do it directly if there already is a PIM or when
5846 * nfa_postprocess() detected it will work better. */
5847 if (t->pim.result != NFA_PIM_UNUSED
5848 || t->state->c == NFA_START_INVISIBLE_FIRST
5849 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5850 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5851 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005852 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005853 int in_use = m->norm.in_use;
5854
Bram Moolenaar699c1202013-09-25 16:41:54 +02005855 /* Copy submatch info for the recursive call, opposite
5856 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005857 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005858#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005859 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005860 copy_sub_off(&m->synt, &t->subs.synt);
5861#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005862
Bram Moolenaara2d95102013-06-04 14:23:05 +02005863 /*
5864 * First try matching the invisible match, then what
5865 * follows.
5866 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005867 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005868 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005869 if (result == NFA_TOO_EXPENSIVE)
5870 {
5871 nfa_match = result;
5872 goto theend;
5873 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005874
Bram Moolenaardecd9542013-06-07 16:31:50 +02005875 /* for \@! and \@<! it is a match when the result is
5876 * FALSE */
5877 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005878 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5879 || t->state->c
5880 == NFA_START_INVISIBLE_BEFORE_NEG
5881 || t->state->c
5882 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005883 {
5884 /* Copy submatch info from the recursive call */
5885 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005886#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005887 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005888 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005889#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005890 /* If the pattern has \ze and it matched in the
5891 * sub pattern, use it. */
5892 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005893
Bram Moolenaara2d95102013-06-04 14:23:05 +02005894 /* t->state->out1 is the corresponding
5895 * END_INVISIBLE node; Add its out to the current
5896 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005897 add_here = TRUE;
5898 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005899 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005900 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005901 }
5902 else
5903 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005904 nfa_pim_T pim;
5905
Bram Moolenaara2d95102013-06-04 14:23:05 +02005906 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005907 * First try matching what follows. Only if a match
5908 * is found verify the invisible match matches. Add a
5909 * nfa_pim_T to the following states, it contains info
5910 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005911 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005912 pim.state = t->state;
5913 pim.result = NFA_PIM_TODO;
5914 pim.subs.norm.in_use = 0;
5915#ifdef FEAT_SYN_HL
5916 pim.subs.synt.in_use = 0;
5917#endif
5918 if (REG_MULTI)
5919 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005920 pim.end.pos.col = (int)(rex.input - rex.line);
5921 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005922 }
5923 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005924 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005925
5926 /* t->state->out1 is the corresponding END_INVISIBLE
5927 * node; Add its out to the current list (zero-width
5928 * match). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005929 if (addstate_here(thislist, t->state->out1->out,
5930 &t->subs, &pim, &listidx) == NULL)
5931 {
5932 nfa_match = NFA_TOO_EXPENSIVE;
5933 goto theend;
5934 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005935 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005936 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005937 break;
5938
Bram Moolenaar87953742013-06-05 18:52:40 +02005939 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005940 {
5941 nfa_state_T *skip = NULL;
5942#ifdef ENABLE_LOG
5943 int skip_lid = 0;
5944#endif
5945
5946 /* There is no point in trying to match the pattern if the
5947 * output state is not going to be added to the list. */
5948 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5949 {
5950 skip = t->state->out1->out;
5951#ifdef ENABLE_LOG
5952 skip_lid = nextlist->id;
5953#endif
5954 }
5955 else if (state_in_list(nextlist,
5956 t->state->out1->out->out, &t->subs))
5957 {
5958 skip = t->state->out1->out->out;
5959#ifdef ENABLE_LOG
5960 skip_lid = nextlist->id;
5961#endif
5962 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005963 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005964 t->state->out1->out->out, &t->subs))
5965 {
5966 skip = t->state->out1->out->out;
5967#ifdef ENABLE_LOG
5968 skip_lid = thislist->id;
5969#endif
5970 }
5971 if (skip != NULL)
5972 {
5973#ifdef ENABLE_LOG
5974 nfa_set_code(skip->c);
5975 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5976 abs(skip->id), skip_lid, skip->c, code);
5977#endif
5978 break;
5979 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005980 /* Copy submatch info to the recursive call, opposite of what
5981 * happens afterwards. */
5982 copy_sub_off(&m->norm, &t->subs.norm);
5983#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005984 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005985 copy_sub_off(&m->synt, &t->subs.synt);
5986#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005987
Bram Moolenaar87953742013-06-05 18:52:40 +02005988 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005989 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005990 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005991 if (result == NFA_TOO_EXPENSIVE)
5992 {
5993 nfa_match = result;
5994 goto theend;
5995 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005996 if (result)
5997 {
5998 int bytelen;
5999
6000#ifdef ENABLE_LOG
6001 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6002 log_subsexpr(m);
6003#endif
6004 /* Copy submatch info from the recursive call */
6005 copy_sub_off(&t->subs.norm, &m->norm);
6006#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006007 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006008 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006009#endif
6010 /* Now we need to skip over the matched text and then
6011 * continue with what follows. */
6012 if (REG_MULTI)
6013 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006014 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006015 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006016 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006017 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006018
6019#ifdef ENABLE_LOG
6020 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6021#endif
6022 if (bytelen == 0)
6023 {
6024 /* empty match, output of corresponding
6025 * NFA_END_PATTERN/NFA_SKIP to be used at current
6026 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006027 add_here = TRUE;
6028 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006029 }
6030 else if (bytelen <= clen)
6031 {
6032 /* match current character, output of corresponding
6033 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006034 add_state = t->state->out1->out->out;
6035 add_off = clen;
6036 }
6037 else
6038 {
6039 /* skip over the matched characters, set character
6040 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006041 add_state = t->state->out1->out;
6042 add_off = bytelen;
6043 add_count = bytelen - clen;
6044 }
6045 }
6046 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006047 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006048
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006049 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006050 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006051 {
6052 add_here = TRUE;
6053 add_state = t->state->out;
6054 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006055 break;
6056
6057 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006058 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006059 {
6060 add_here = TRUE;
6061 add_state = t->state->out;
6062 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006063 break;
6064
6065 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006066 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006068 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006069 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006070 else if (has_mbyte)
6071 {
6072 int this_class;
6073
6074 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006075 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006076 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006077 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006079 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006080 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006081 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006082 || (rex.input > rex.line
6083 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006084 result = FALSE;
6085 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006086 {
6087 add_here = TRUE;
6088 add_state = t->state->out;
6089 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006091
6092 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006093 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006094 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006095 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006096 else if (has_mbyte)
6097 {
6098 int this_class, prev_class;
6099
6100 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006101 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 prev_class = reg_prev_class();
6103 if (this_class == prev_class
6104 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006105 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006106 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006107 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6108 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006109 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006110 result = FALSE;
6111 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006112 {
6113 add_here = TRUE;
6114 add_state = t->state->out;
6115 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006116 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117
Bram Moolenaar4b780632013-05-31 22:14:52 +02006118 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006119 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006120 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006121 {
6122 add_here = TRUE;
6123 add_state = t->state->out;
6124 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006125 break;
6126
6127 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006128 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006129 {
6130 add_here = TRUE;
6131 add_state = t->state->out;
6132 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006133 break;
6134
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006135 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006136 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006137 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006138 int len = 0;
6139 nfa_state_T *end;
6140 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006141 int cchars[MAX_MCO];
6142 int ccount = 0;
6143 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006146 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006147 if (utf_iscomposing(sta->c))
6148 {
6149 /* Only match composing character(s), ignore base
6150 * character. Used for ".{composing}" and "{composing}"
6151 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006152 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006153 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006154 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006155 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006156 /* If \Z was present, then ignore composing characters.
6157 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006158 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006159 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006160 else
6161 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006162 while (sta->c != NFA_END_COMPOSING)
6163 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006164 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006165
6166 /* Check base character matches first, unless ignored. */
6167 else if (len > 0 || mc == sta->c)
6168 {
6169 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006170 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006171 len += mb_char2len(mc);
6172 sta = sta->out;
6173 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006174
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006175 /* We don't care about the order of composing characters.
6176 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006177 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006178 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006179 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006180 cchars[ccount++] = mc;
6181 len += mb_char2len(mc);
6182 if (ccount == MAX_MCO)
6183 break;
6184 }
6185
6186 /* Check that each composing char in the pattern matches a
6187 * composing char in the text. We do not check if all
6188 * composing chars are matched. */
6189 result = OK;
6190 while (sta->c != NFA_END_COMPOSING)
6191 {
6192 for (j = 0; j < ccount; ++j)
6193 if (cchars[j] == sta->c)
6194 break;
6195 if (j == ccount)
6196 {
6197 result = FAIL;
6198 break;
6199 }
6200 sta = sta->out;
6201 }
6202 }
6203 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006204 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006205
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006206 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006207 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006208 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006209 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006210
6211 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006212 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006213 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006214 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006215 go_to_nextline = TRUE;
6216 /* Pass -1 for the offset, which means taking the position
6217 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006218 add_state = t->state->out;
6219 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006220 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006221 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006222 {
6223 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006224 add_state = t->state->out;
6225 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006226 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006227 break;
6228
Bram Moolenaar417bad22013-06-07 14:08:30 +02006229 case NFA_START_COLL:
6230 case NFA_START_NEG_COLL:
6231 {
6232 /* What follows is a list of characters, until NFA_END_COLL.
6233 * One of them must match or none of them must match. */
6234 nfa_state_T *state;
6235 int result_if_matched;
6236 int c1, c2;
6237
6238 /* Never match EOL. If it's part of the collection it is added
6239 * as a separate state with an OR. */
6240 if (curc == NUL)
6241 break;
6242
6243 state = t->state->out;
6244 result_if_matched = (t->state->c == NFA_START_COLL);
6245 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006246 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006247 if (state->c == NFA_END_COLL)
6248 {
6249 result = !result_if_matched;
6250 break;
6251 }
6252 if (state->c == NFA_RANGE_MIN)
6253 {
6254 c1 = state->val;
6255 state = state->out; /* advance to NFA_RANGE_MAX */
6256 c2 = state->val;
6257#ifdef ENABLE_LOG
6258 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6259 curc, c1, c2);
6260#endif
6261 if (curc >= c1 && curc <= c2)
6262 {
6263 result = result_if_matched;
6264 break;
6265 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006266 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006267 {
6268 int curc_low = MB_TOLOWER(curc);
6269 int done = FALSE;
6270
6271 for ( ; c1 <= c2; ++c1)
6272 if (MB_TOLOWER(c1) == curc_low)
6273 {
6274 result = result_if_matched;
6275 done = TRUE;
6276 break;
6277 }
6278 if (done)
6279 break;
6280 }
6281 }
6282 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006283 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006284 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006285 == MB_TOLOWER(state->c))))
6286 {
6287 result = result_if_matched;
6288 break;
6289 }
6290 state = state->out;
6291 }
6292 if (result)
6293 {
6294 /* next state is in out of the NFA_END_COLL, out1 of
6295 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006296 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006297 add_off = clen;
6298 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006299 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006300 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006301
6302 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006303 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006304 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006305 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006306 add_state = t->state->out;
6307 add_off = clen;
6308 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006309 break;
6310
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006311 case NFA_ANY_COMPOSING:
6312 /* On a composing character skip over it. Otherwise do
6313 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006314 if (enc_utf8 && utf_iscomposing(curc))
6315 {
6316 add_off = clen;
6317 }
6318 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006319 {
6320 add_here = TRUE;
6321 add_off = 0;
6322 }
6323 add_state = t->state->out;
6324 break;
6325
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006326 /*
6327 * Character classes like \a for alpha, \d for digit etc.
6328 */
6329 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006330 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006331 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006332 break;
6333
6334 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006335 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006336 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006337 break;
6338
6339 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006340 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006341 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006342 break;
6343
6344 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006345 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006346 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006347 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006348 break;
6349
6350 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006351 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006352 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006353 break;
6354
6355 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006356 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006357 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006358 break;
6359
6360 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006361 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006362 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006363 break;
6364
6365 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006366 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006367 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006368 break;
6369
6370 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006371 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006372 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006373 break;
6374
6375 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006376 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006377 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006378 break;
6379
6380 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006381 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006382 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006383 break;
6384
6385 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006386 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006387 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006388 break;
6389
6390 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006391 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006392 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006393 break;
6394
6395 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006396 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006397 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006398 break;
6399
6400 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006401 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006402 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006403 break;
6404
6405 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006406 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006407 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408 break;
6409
6410 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006411 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006412 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006413 break;
6414
6415 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006416 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006417 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006418 break;
6419
6420 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006421 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006422 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006423 break;
6424
6425 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006426 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006427 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006428 break;
6429
6430 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006431 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006432 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006433 break;
6434
6435 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006436 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006437 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006438 break;
6439
6440 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006441 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006442 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006443 break;
6444
6445 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006446 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006447 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006448 break;
6449
6450 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006451 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006452 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006453 break;
6454
6455 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006456 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006457 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006458 break;
6459
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006460 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006461 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006462 ADD_STATE_IF_MATCH(t->state);
6463 break;
6464
6465 case NFA_NLOWER_IC: /* [^a-z] */
6466 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006467 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006468 ADD_STATE_IF_MATCH(t->state);
6469 break;
6470
6471 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006472 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006473 ADD_STATE_IF_MATCH(t->state);
6474 break;
6475
6476 case NFA_NUPPER_IC: /* ^[A-Z] */
6477 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006478 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006479 ADD_STATE_IF_MATCH(t->state);
6480 break;
6481
Bram Moolenaar5714b802013-05-28 22:03:20 +02006482 case NFA_BACKREF1:
6483 case NFA_BACKREF2:
6484 case NFA_BACKREF3:
6485 case NFA_BACKREF4:
6486 case NFA_BACKREF5:
6487 case NFA_BACKREF6:
6488 case NFA_BACKREF7:
6489 case NFA_BACKREF8:
6490 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006491#ifdef FEAT_SYN_HL
6492 case NFA_ZREF1:
6493 case NFA_ZREF2:
6494 case NFA_ZREF3:
6495 case NFA_ZREF4:
6496 case NFA_ZREF5:
6497 case NFA_ZREF6:
6498 case NFA_ZREF7:
6499 case NFA_ZREF8:
6500 case NFA_ZREF9:
6501#endif
6502 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006503 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006504 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006505 int bytelen;
6506
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006507 if (t->state->c <= NFA_BACKREF9)
6508 {
6509 subidx = t->state->c - NFA_BACKREF1 + 1;
6510 result = match_backref(&t->subs.norm, subidx, &bytelen);
6511 }
6512#ifdef FEAT_SYN_HL
6513 else
6514 {
6515 subidx = t->state->c - NFA_ZREF1 + 1;
6516 result = match_zref(subidx, &bytelen);
6517 }
6518#endif
6519
Bram Moolenaar5714b802013-05-28 22:03:20 +02006520 if (result)
6521 {
6522 if (bytelen == 0)
6523 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006524 /* empty match always works, output of NFA_SKIP to be
6525 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006526 add_here = TRUE;
6527 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006528 }
6529 else if (bytelen <= clen)
6530 {
6531 /* match current character, jump ahead to out of
6532 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006533 add_state = t->state->out->out;
6534 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006535 }
6536 else
6537 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006538 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006539 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006540 add_state = t->state->out;
6541 add_off = bytelen;
6542 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006543 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006544 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006545 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006546 }
6547 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006548 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006549 if (t->count - clen <= 0)
6550 {
6551 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006552 add_state = t->state->out;
6553 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006554 }
6555 else
6556 {
6557 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006558 add_state = t->state;
6559 add_off = 0;
6560 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006561 }
6562 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006563
Bram Moolenaar423532e2013-05-29 21:14:42 +02006564 case NFA_LNUM:
6565 case NFA_LNUM_GT:
6566 case NFA_LNUM_LT:
6567 result = (REG_MULTI &&
6568 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006569 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006570 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006571 {
6572 add_here = TRUE;
6573 add_state = t->state->out;
6574 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006575 break;
6576
6577 case NFA_COL:
6578 case NFA_COL_GT:
6579 case NFA_COL_LT:
6580 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006581 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006582 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006583 {
6584 add_here = TRUE;
6585 add_state = t->state->out;
6586 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006587 break;
6588
6589 case NFA_VCOL:
6590 case NFA_VCOL_GT:
6591 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006592 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006593 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006594 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006595 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006596
6597 /* Bail out quickly when there can't be a match, avoid the
6598 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006599 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006600 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006601 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006602 result = FALSE;
6603 if (op == 1 && col - 1 > t->state->val && col > 100)
6604 {
6605 int ts = wp->w_buffer->b_p_ts;
6606
6607 /* Guess that a character won't use more columns than
6608 * 'tabstop', with a minimum of 4. */
6609 if (ts < 4)
6610 ts = 4;
6611 result = col > t->state->val * ts;
6612 }
6613 if (!result)
6614 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006615 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006616 if (result)
6617 {
6618 add_here = TRUE;
6619 add_state = t->state->out;
6620 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006621 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006622 break;
6623
Bram Moolenaar044aa292013-06-04 21:27:38 +02006624 case NFA_MARK:
6625 case NFA_MARK_GT:
6626 case NFA_MARK_LT:
6627 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006628 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006629
6630 /* Compare the mark position to the match position. */
6631 result = (pos != NULL /* mark doesn't exist */
6632 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006633 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6634 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006635 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006636 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006637 ? t->state->c == NFA_MARK_GT
6638 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006639 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006640 ? t->state->c == NFA_MARK_GT
6641 : t->state->c == NFA_MARK_LT)));
6642 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006643 {
6644 add_here = TRUE;
6645 add_state = t->state->out;
6646 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006647 break;
6648 }
6649
Bram Moolenaar423532e2013-05-29 21:14:42 +02006650 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006651 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006652 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006653 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006654 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006655 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006656 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006657 {
6658 add_here = TRUE;
6659 add_state = t->state->out;
6660 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006661 break;
6662
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006663 case NFA_VISUAL:
6664 result = reg_match_visual();
6665 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006666 {
6667 add_here = TRUE;
6668 add_state = t->state->out;
6669 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006670 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006671
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006672 case NFA_MOPEN1:
6673 case NFA_MOPEN2:
6674 case NFA_MOPEN3:
6675 case NFA_MOPEN4:
6676 case NFA_MOPEN5:
6677 case NFA_MOPEN6:
6678 case NFA_MOPEN7:
6679 case NFA_MOPEN8:
6680 case NFA_MOPEN9:
6681#ifdef FEAT_SYN_HL
6682 case NFA_ZOPEN:
6683 case NFA_ZOPEN1:
6684 case NFA_ZOPEN2:
6685 case NFA_ZOPEN3:
6686 case NFA_ZOPEN4:
6687 case NFA_ZOPEN5:
6688 case NFA_ZOPEN6:
6689 case NFA_ZOPEN7:
6690 case NFA_ZOPEN8:
6691 case NFA_ZOPEN9:
6692#endif
6693 case NFA_NOPEN:
6694 case NFA_ZSTART:
6695 /* These states are only added to be able to bail out when
6696 * they are added again, nothing is to be done. */
6697 break;
6698
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006699 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006700 {
6701 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006702
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006703#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006704 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006705 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006706#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006707 result = (c == curc);
6708
Bram Moolenaar6100d022016-10-02 16:51:57 +02006709 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006710 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006711 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006712 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006713 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006714 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006715 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006716 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006717 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006718
6719 } /* switch (t->state->c) */
6720
6721 if (add_state != NULL)
6722 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006723 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006724 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006725
6726 if (t->pim.result == NFA_PIM_UNUSED)
6727 pim = NULL;
6728 else
6729 pim = &t->pim;
6730
6731 /* Handle the postponed invisible match if the match might end
6732 * without advancing and before the end of the line. */
6733 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006734 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006735 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006736 {
6737#ifdef ENABLE_LOG
6738 fprintf(log_fd, "\n");
6739 fprintf(log_fd, "==================================\n");
6740 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6741 fprintf(log_fd, "\n");
6742#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006743 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006744 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006745 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006746 /* for \@! and \@<! it is a match when the result is
6747 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006748 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006749 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6750 || pim->state->c
6751 == NFA_START_INVISIBLE_BEFORE_NEG
6752 || pim->state->c
6753 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006754 {
6755 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006756 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006758 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006759 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006760#endif
6761 }
6762 }
6763 else
6764 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006765 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006766#ifdef ENABLE_LOG
6767 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006768 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006769 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6770 fprintf(log_fd, "\n");
6771#endif
6772 }
6773
Bram Moolenaardecd9542013-06-07 16:31:50 +02006774 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006775 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006776 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6777 || pim->state->c
6778 == NFA_START_INVISIBLE_BEFORE_NEG
6779 || pim->state->c
6780 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006781 {
6782 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006783 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006784#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006785 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006786 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006787#endif
6788 }
6789 else
6790 /* look-behind match failed, don't add the state */
6791 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006792
6793 /* Postponed invisible match was handled, don't add it to
6794 * following states. */
6795 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006796 }
6797
Bram Moolenaara951e352013-10-06 15:46:11 +02006798 /* If "pim" points into l->t it will become invalid when
6799 * adding the state causes the list to be reallocated. Make a
6800 * local copy to avoid that. */
6801 if (pim == &t->pim)
6802 {
6803 copy_pim(&pim_copy, pim);
6804 pim = &pim_copy;
6805 }
6806
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006807 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006808 r = addstate_here(thislist, add_state, &t->subs,
6809 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006810 else
6811 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006812 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006813 if (add_count > 0)
6814 nextlist->t[nextlist->n - 1].count = add_count;
6815 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006816 if (r == NULL)
6817 {
6818 nfa_match = NFA_TOO_EXPENSIVE;
6819 goto theend;
6820 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006821 }
6822
6823 } /* for (thislist = thislist; thislist->state; thislist++) */
6824
Bram Moolenaare23febd2013-05-26 18:40:14 +02006825 /* Look for the start of a match in the current position by adding the
6826 * start state to the list of states.
6827 * The first found match is the leftmost one, thus the order of states
6828 * matters!
6829 * Do not add the start state in recursive calls of nfa_regmatch(),
6830 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006831 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006832 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006833 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006834 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006835 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006836 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006837 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006838 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006839 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006840 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006841 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6842 || (rex.lnum == nfa_endp->se_u.pos.lnum
6843 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006844 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006845 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006846 {
6847#ifdef ENABLE_LOG
6848 fprintf(log_fd, "(---) STARTSTATE\n");
6849#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006850 /* Inline optimized code for addstate() if we know the state is
6851 * the first MOPEN. */
6852 if (toplevel)
6853 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006854 int add = TRUE;
6855 int c;
6856
6857 if (prog->regstart != NUL && clen != 0)
6858 {
6859 if (nextlist->n == 0)
6860 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006861 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006862
6863 /* Nextlist is empty, we can skip ahead to the
6864 * character that must appear at the start. */
6865 if (skip_to_start(prog->regstart, &col) == FAIL)
6866 break;
6867#ifdef ENABLE_LOG
6868 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006869 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006870#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006871 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006872 }
6873 else
6874 {
6875 /* Checking if the required start character matches is
6876 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006877 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006878 if (c != prog->regstart && (!rex.reg_ic
6879 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006880 {
6881#ifdef ENABLE_LOG
6882 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6883#endif
6884 add = FALSE;
6885 }
6886 }
6887 }
6888
6889 if (add)
6890 {
6891 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006892 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006893 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006894 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006895 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006896 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6897 {
6898 nfa_match = NFA_TOO_EXPENSIVE;
6899 goto theend;
6900 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006901 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006902 }
6903 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006904 {
6905 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6906 {
6907 nfa_match = NFA_TOO_EXPENSIVE;
6908 goto theend;
6909 }
6910 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006911 }
6912
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006913#ifdef ENABLE_LOG
6914 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006915 {
6916 int i;
6917
6918 for (i = 0; i < thislist->n; i++)
6919 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6920 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006921 fprintf(log_fd, "\n");
6922#endif
6923
6924nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006925 /* Advance to the next character, or advance to the next line, or
6926 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006927 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006928 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006929 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006930 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006931 reg_nextline();
6932 else
6933 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006934
6935 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006936 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006937 if (got_int)
6938 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006939#ifdef FEAT_RELTIME
6940 /* Check for timeout once in a twenty times to avoid overhead. */
6941 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6942 {
6943 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006944 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006945 break;
6946 }
6947#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006948 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006949
6950#ifdef ENABLE_LOG
6951 if (log_fd != stderr)
6952 fclose(log_fd);
6953 log_fd = NULL;
6954#endif
6955
6956theend:
6957 /* Free memory */
6958 vim_free(list[0].t);
6959 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006960 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006961#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006962#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006963 fclose(debug);
6964#endif
6965
Bram Moolenaar963fee22013-05-26 21:47:28 +02006966 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006967}
6968
6969/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006970 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006971 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972 */
6973 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006974nfa_regtry(
6975 nfa_regprog_T *prog,
6976 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006977 proftime_T *tm UNUSED, /* timeout limit or NULL */
6978 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006979{
6980 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006981 regsubs_T subs, m;
6982 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006983 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006984#ifdef ENABLE_LOG
6985 FILE *f;
6986#endif
6987
Bram Moolenaar0270f382018-07-17 05:43:58 +02006988 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006989#ifdef FEAT_RELTIME
6990 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006991 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006992 nfa_time_count = 0;
6993#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006994
6995#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006996 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997 if (f != NULL)
6998 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006999 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000#ifdef DEBUG
7001 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7002#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007003 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007004 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007005 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007006 fprintf(f, "\n\n");
7007 fclose(f);
7008 }
7009 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007010 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007011#endif
7012
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007013 clear_sub(&subs.norm);
7014 clear_sub(&m.norm);
7015#ifdef FEAT_SYN_HL
7016 clear_sub(&subs.synt);
7017 clear_sub(&m.synt);
7018#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007019
Bram Moolenaarfda37292014-11-05 14:27:36 +01007020 result = nfa_regmatch(prog, start, &subs, &m);
7021 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007023 else if (result == NFA_TOO_EXPENSIVE)
7024 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007025
7026 cleanup_subexpr();
7027 if (REG_MULTI)
7028 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007029 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007031 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7032 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007033
Bram Moolenaar6100d022016-10-02 16:51:57 +02007034 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7035 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007036 }
7037
Bram Moolenaar6100d022016-10-02 16:51:57 +02007038 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007039 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007040 rex.reg_startpos[0].lnum = 0;
7041 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007043 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007044 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007045 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007046 rex.reg_endpos[0].lnum = rex.lnum;
7047 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048 }
7049 else
7050 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007051 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007052 }
7053 else
7054 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007055 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007056 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007057 rex.reg_startp[i] = subs.norm.list.line[i].start;
7058 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007059 }
7060
Bram Moolenaar6100d022016-10-02 16:51:57 +02007061 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007062 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007063 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007064 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007065 }
7066
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007067#ifdef FEAT_SYN_HL
7068 /* Package any found \z(...\) matches for export. Default is none. */
7069 unref_extmatch(re_extmatch_out);
7070 re_extmatch_out = NULL;
7071
7072 if (prog->reghasz == REX_SET)
7073 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007074 cleanup_zsubexpr();
7075 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007076 /* Loop over \z1, \z2, etc. There is no \z0. */
7077 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007078 {
7079 if (REG_MULTI)
7080 {
7081 struct multipos *mpos = &subs.synt.list.multi[i];
7082
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007083 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007084 if (mpos->start_lnum >= 0
7085 && mpos->start_lnum == mpos->end_lnum
7086 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007087 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007088 vim_strnsave(reg_getline(mpos->start_lnum)
7089 + mpos->start_col,
7090 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007091 }
7092 else
7093 {
7094 struct linepos *lpos = &subs.synt.list.line[i];
7095
7096 if (lpos->start != NULL && lpos->end != NULL)
7097 re_extmatch_out->matches[i] =
7098 vim_strnsave(lpos->start,
7099 (int)(lpos->end - lpos->start));
7100 }
7101 }
7102 }
7103#endif
7104
Bram Moolenaar0270f382018-07-17 05:43:58 +02007105 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007106}
7107
7108/*
7109 * Match a regexp against a string ("line" points to the string) or multiple
7110 * lines ("line" is NULL, use reg_getline()).
7111 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007112 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113 */
7114 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007115nfa_regexec_both(
7116 char_u *line,
7117 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007118 proftime_T *tm, /* timeout limit or NULL */
7119 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007120{
7121 nfa_regprog_T *prog;
7122 long retval = 0L;
7123 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007124 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007125
7126 if (REG_MULTI)
7127 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007128 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007129 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007130 rex.reg_startpos = rex.reg_mmatch->startpos;
7131 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132 }
7133 else
7134 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007135 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7136 rex.reg_startp = rex.reg_match->startp;
7137 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007138 }
7139
7140 /* Be paranoid... */
7141 if (prog == NULL || line == NULL)
7142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007143 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007144 goto theend;
7145 }
7146
Bram Moolenaar6100d022016-10-02 16:51:57 +02007147 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007148 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007149 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007150 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007151 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007152
Bram Moolenaar6100d022016-10-02 16:51:57 +02007153 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007155 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007156
Bram Moolenaar0270f382018-07-17 05:43:58 +02007157 rex.line = line;
7158 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007159
Bram Moolenaar0270f382018-07-17 05:43:58 +02007160 rex.nfa_has_zend = prog->has_zend;
7161 rex.nfa_has_backref = prog->has_backref;
7162 rex.nfa_nsubexpr = prog->nsubexp;
7163 rex.nfa_listid = 1;
7164 rex.nfa_alt_listid = 2;
7165#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007166 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007167#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168
Bram Moolenaard89616e2013-06-06 18:46:06 +02007169 if (prog->reganch && col > 0)
7170 return 0L;
7171
Bram Moolenaar0270f382018-07-17 05:43:58 +02007172 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007173#ifdef FEAT_SYN_HL
7174 /* Clear the external match subpointers if necessary. */
7175 if (prog->reghasz == REX_SET)
7176 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007177 rex.nfa_has_zsubexpr = TRUE;
7178 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007179 }
7180 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007181 {
7182 rex.nfa_has_zsubexpr = FALSE;
7183 rex.need_clear_zsubexpr = FALSE;
7184 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007185#endif
7186
Bram Moolenaard89616e2013-06-06 18:46:06 +02007187 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007188 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007189 /* Skip ahead until a character we know the match must start with.
7190 * When there is none there is no match. */
7191 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007192 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007193
Bram Moolenaar473de612013-06-08 18:19:48 +02007194 /* If match_text is set it contains the full text that must match.
7195 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007196 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007197 return find_match_text(col, prog->regstart, prog->match_text);
7198 }
7199
Bram Moolenaard89616e2013-06-06 18:46:06 +02007200 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007201 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007202 goto theend;
7203
Bram Moolenaar0270f382018-07-17 05:43:58 +02007204 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7205 // it's accidentally used during execution.
7206 nstate = 0;
7207 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007208 {
7209 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007210 prog->state[i].lastlist[0] = 0;
7211 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007212 }
7213
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007214 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007215
Bram Moolenaar0270f382018-07-17 05:43:58 +02007216#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007217 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007218#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007220theend:
7221 return retval;
7222}
7223
7224/*
7225 * Compile a regular expression into internal code for the NFA matcher.
7226 * Returns the program in allocated space. Returns NULL for an error.
7227 */
7228 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007229nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007230{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007231 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007232 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007233 int *postfix;
7234
7235 if (expr == NULL)
7236 return NULL;
7237
Bram Moolenaar0270f382018-07-17 05:43:58 +02007238#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007239 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007240#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007241 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007242
7243 init_class_tab();
7244
7245 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7246 return NULL;
7247
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007248 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007249 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007250 postfix = re2post();
7251 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007252 {
7253 /* TODO: only give this error for debugging? */
7254 if (post_ptr >= post_end)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007255 siemsg("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007256 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007258
7259 /*
7260 * In order to build the NFA, we parse the input regexp twice:
7261 * 1. first pass to count size (so we can allocate space)
7262 * 2. second to emit code
7263 */
7264#ifdef ENABLE_LOG
7265 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007266 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007267
7268 if (f != NULL)
7269 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007270 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007271 fclose(f);
7272 }
7273 }
7274#endif
7275
7276 /*
7277 * PASS 1
7278 * Count number of NFA states in "nstate". Do not build the NFA.
7279 */
7280 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007281
Bram Moolenaar16619a22013-06-11 18:42:36 +02007282 /* allocate the regprog with space for the compiled regexp */
7283 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007284 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7285 if (prog == NULL)
7286 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007287 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007288 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007289
7290 /*
7291 * PASS 2
7292 * Build the NFA
7293 */
7294 prog->start = post2nfa(postfix, post_ptr, FALSE);
7295 if (prog->start == NULL)
7296 goto fail;
7297
7298 prog->regflags = regflags;
7299 prog->engine = &nfa_regengine;
7300 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007301 prog->has_zend = rex.nfa_has_zend;
7302 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007303 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007304
Bram Moolenaara2947e22013-06-11 22:44:09 +02007305 nfa_postprocess(prog);
7306
Bram Moolenaard89616e2013-06-06 18:46:06 +02007307 prog->reganch = nfa_get_reganch(prog->start, 0);
7308 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007309 prog->match_text = nfa_get_match_text(prog->start);
7310
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007311#ifdef ENABLE_LOG
7312 nfa_postfix_dump(expr, OK);
7313 nfa_dump(prog);
7314#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007315#ifdef FEAT_SYN_HL
7316 /* Remember whether this pattern has any \z specials in it. */
7317 prog->reghasz = re_has_z;
7318#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007319 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007320#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007321 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007322#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007323
7324out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007325 VIM_CLEAR(post_start);
7326 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007327 state_ptr = NULL;
7328 return (regprog_T *)prog;
7329
7330fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007331 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007332#ifdef ENABLE_LOG
7333 nfa_postfix_dump(expr, FAIL);
7334#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007335#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007336 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007337#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007338 goto out;
7339}
7340
Bram Moolenaar473de612013-06-08 18:19:48 +02007341/*
7342 * Free a compiled regexp program, returned by nfa_regcomp().
7343 */
7344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007345nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007346{
7347 if (prog != NULL)
7348 {
7349 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007350 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007351 vim_free(prog);
7352 }
7353}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007354
7355/*
7356 * Match a regexp against a string.
7357 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7358 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007359 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007360 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007361 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007362 */
7363 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007364nfa_regexec_nl(
7365 regmatch_T *rmp,
7366 char_u *line, /* string to match against */
7367 colnr_T col, /* column to start looking for match */
7368 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007369{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007370 rex.reg_match = rmp;
7371 rex.reg_mmatch = NULL;
7372 rex.reg_maxline = 0;
7373 rex.reg_line_lbr = line_lbr;
7374 rex.reg_buf = curbuf;
7375 rex.reg_win = NULL;
7376 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007377 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007378 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007379 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007380}
7381
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007382
7383/*
7384 * Match a regexp against multiple lines.
7385 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7386 * Uses curbuf for line count and 'iskeyword'.
7387 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007388 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007389 * match otherwise.
7390 *
7391 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7392 *
7393 * ! Also NOTE : match may actually be in another line. e.g.:
7394 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7395 *
7396 * +-------------------------+
7397 * |a |
7398 * |b |
7399 * |c |
7400 * | |
7401 * +-------------------------+
7402 *
7403 * then nfa_regexec_multi() returns 3. while the original
7404 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7405 *
7406 * FIXME if this behavior is not compatible.
7407 */
7408 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007409nfa_regexec_multi(
7410 regmmatch_T *rmp,
7411 win_T *win, /* window in which to search or NULL */
7412 buf_T *buf, /* buffer in which to search */
7413 linenr_T lnum, /* nr of line to start looking for match */
7414 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007415 proftime_T *tm, /* timeout limit or NULL */
7416 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007417{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007418 rex.reg_match = NULL;
7419 rex.reg_mmatch = rmp;
7420 rex.reg_buf = buf;
7421 rex.reg_win = win;
7422 rex.reg_firstlnum = lnum;
7423 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7424 rex.reg_line_lbr = FALSE;
7425 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007426 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007427 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007428
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007429 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007430}
7431
7432#ifdef DEBUG
7433# undef ENABLE_LOG
7434#endif