blob: 1e106359806556c58de4e68ae5b8ebe33d6bff2a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
Bram Moolenaar221cd9f2019-01-31 15:34:40 +0100229 NFA_CLASS_ESCAPE,
230 NFA_CLASS_IDENT,
231 NFA_CLASS_KEYWORD,
232 NFA_CLASS_FNAME
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233};
234
235/* Keep in sync with classchars. */
236static int nfa_classcodes[] = {
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
241 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
242 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
243 NFA_UPPER, NFA_NUPPER
244};
245
Bram Moolenaar174a8482013-11-28 14:20:17 +0100246static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaara5483442019-02-17 20:17:02 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %d");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar0270f382018-07-17 05:43:58 +0200250// Variables only used in nfa_regcomp() and descendants.
251static int nfa_re_flags; // re_flags passed to nfa_regcomp()
252static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253static int *post_end;
254static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200255static int nstate; // Number of states in the NFA.
256static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
262static int nfa_ll_index = 0;
263
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100264static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100265static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100267static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100269static int match_follows(nfa_state_T *startstate, int depth);
270static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271
272/* helper functions used when doing re2post() ... regatom() parsing */
273#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275 return FAIL; \
276 *post_ptr++ = c; \
277 } while (0)
278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279/*
280 * Initialize internal variables before NFA compilation.
281 * Return OK on success, FAIL otherwise.
282 */
283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100284nfa_regcomp_start(
285 char_u *expr,
286 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200288 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200289 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290
291 nstate = 0;
292 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200293 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200294 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200296 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200297 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200298 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200299
300 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200301 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200302
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303 post_start = (int *)lalloc(postfix_size, TRUE);
304 if (post_start == NULL)
305 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200307 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200308 rex.nfa_has_zend = FALSE;
309 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200311 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312 regcomp_start(expr, re_flags);
313
314 return OK;
315}
316
317/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200318 * Figure out if the NFA state list starts with an anchor, must match at start
319 * of the line.
320 */
321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200323{
324 nfa_state_T *p = start;
325
326 if (depth > 4)
327 return 0;
328
329 while (p != NULL)
330 {
331 switch (p->c)
332 {
333 case NFA_BOL:
334 case NFA_BOF:
335 return 1; /* yes! */
336
337 case NFA_ZSTART:
338 case NFA_ZEND:
339 case NFA_CURSOR:
340 case NFA_VISUAL:
341
342 case NFA_MOPEN:
343 case NFA_MOPEN1:
344 case NFA_MOPEN2:
345 case NFA_MOPEN3:
346 case NFA_MOPEN4:
347 case NFA_MOPEN5:
348 case NFA_MOPEN6:
349 case NFA_MOPEN7:
350 case NFA_MOPEN8:
351 case NFA_MOPEN9:
352 case NFA_NOPEN:
353#ifdef FEAT_SYN_HL
354 case NFA_ZOPEN:
355 case NFA_ZOPEN1:
356 case NFA_ZOPEN2:
357 case NFA_ZOPEN3:
358 case NFA_ZOPEN4:
359 case NFA_ZOPEN5:
360 case NFA_ZOPEN6:
361 case NFA_ZOPEN7:
362 case NFA_ZOPEN8:
363 case NFA_ZOPEN9:
364#endif
365 p = p->out;
366 break;
367
368 case NFA_SPLIT:
369 return nfa_get_reganch(p->out, depth + 1)
370 && nfa_get_reganch(p->out1, depth + 1);
371
372 default:
373 return 0; /* noooo */
374 }
375 }
376 return 0;
377}
378
379/*
380 * Figure out if the NFA state list starts with a character which must match
381 * at start of the match.
382 */
383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100384nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200385{
386 nfa_state_T *p = start;
387
388 if (depth > 4)
389 return 0;
390
391 while (p != NULL)
392 {
393 switch (p->c)
394 {
395 /* all kinds of zero-width matches */
396 case NFA_BOL:
397 case NFA_BOF:
398 case NFA_BOW:
399 case NFA_EOW:
400 case NFA_ZSTART:
401 case NFA_ZEND:
402 case NFA_CURSOR:
403 case NFA_VISUAL:
404 case NFA_LNUM:
405 case NFA_LNUM_GT:
406 case NFA_LNUM_LT:
407 case NFA_COL:
408 case NFA_COL_GT:
409 case NFA_COL_LT:
410 case NFA_VCOL:
411 case NFA_VCOL_GT:
412 case NFA_VCOL_LT:
413 case NFA_MARK:
414 case NFA_MARK_GT:
415 case NFA_MARK_LT:
416
417 case NFA_MOPEN:
418 case NFA_MOPEN1:
419 case NFA_MOPEN2:
420 case NFA_MOPEN3:
421 case NFA_MOPEN4:
422 case NFA_MOPEN5:
423 case NFA_MOPEN6:
424 case NFA_MOPEN7:
425 case NFA_MOPEN8:
426 case NFA_MOPEN9:
427 case NFA_NOPEN:
428#ifdef FEAT_SYN_HL
429 case NFA_ZOPEN:
430 case NFA_ZOPEN1:
431 case NFA_ZOPEN2:
432 case NFA_ZOPEN3:
433 case NFA_ZOPEN4:
434 case NFA_ZOPEN5:
435 case NFA_ZOPEN6:
436 case NFA_ZOPEN7:
437 case NFA_ZOPEN8:
438 case NFA_ZOPEN9:
439#endif
440 p = p->out;
441 break;
442
443 case NFA_SPLIT:
444 {
445 int c1 = nfa_get_regstart(p->out, depth + 1);
446 int c2 = nfa_get_regstart(p->out1, depth + 1);
447
448 if (c1 == c2)
449 return c1; /* yes! */
450 return 0;
451 }
452
453 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200454 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200455 return p->c; /* yes! */
456 return 0;
457 }
458 }
459 return 0;
460}
461
462/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200463 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200464 * else. If so return a string in allocated memory with what must match after
465 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200466 */
467 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100468nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200469{
470 nfa_state_T *p = start;
471 int len = 0;
472 char_u *ret;
473 char_u *s;
474
475 if (p->c != NFA_MOPEN)
476 return NULL; /* just in case */
477 p = p->out;
478 while (p->c > 0)
479 {
480 len += MB_CHAR2LEN(p->c);
481 p = p->out;
482 }
483 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
484 return NULL;
485
486 ret = alloc(len);
487 if (ret != NULL)
488 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200489 p = start->out->out; /* skip first char, it goes into regstart */
490 s = ret;
491 while (p->c > 0)
492 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200493 if (has_mbyte)
494 s += (*mb_char2bytes)(p->c, s);
495 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 *s++ = p->c;
497 p = p->out;
498 }
499 *s = NUL;
500 }
501 return ret;
502}
503
504/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200505 * Allocate more space for post_start. Called when
506 * running above the estimated number of states.
507 */
508 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100509realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200510{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200511 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100512 int new_max;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200513 int *new_start;
514 int *old_start;
515
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100516 // For weird patterns the number of states can be very high. Increasing by
517 // 50% seems a reasonable compromise between memory use and speed.
518 new_max = nstate_max * 3 / 2;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200519 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
520 if (new_start == NULL)
521 return FAIL;
522 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200523 old_start = post_start;
524 post_start = new_start;
525 post_ptr = new_start + (post_ptr - old_start);
526 post_end = post_start + new_max;
527 vim_free(old_start);
528 return OK;
529}
530
531/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200532 * Search between "start" and "end" and try to recognize a
533 * character class in expanded form. For example [0-9].
534 * On success, return the id the character class to be emitted.
535 * On failure, return 0 (=FAIL)
536 * Start points to the first char of the range, while end should point
537 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200538 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
539 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540 */
541 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100542nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200543{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200544# define CLASS_not 0x80
545# define CLASS_af 0x40
546# define CLASS_AF 0x20
547# define CLASS_az 0x10
548# define CLASS_AZ 0x08
549# define CLASS_o7 0x04
550# define CLASS_o9 0x02
551# define CLASS_underscore 0x01
552
553 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200554 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200555 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200556
557 if (extra_newl == TRUE)
558 newl = TRUE;
559
560 if (*end != ']')
561 return FAIL;
562 p = start;
563 if (*p == '^')
564 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200565 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200566 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200567 }
568
569 while (p < end)
570 {
571 if (p + 2 < end && *(p + 1) == '-')
572 {
573 switch (*p)
574 {
575 case '0':
576 if (*(p + 2) == '9')
577 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200578 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200579 break;
580 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200581 if (*(p + 2) == '7')
582 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200583 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200584 break;
585 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200586 return FAIL;
587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200588 case 'a':
589 if (*(p + 2) == 'z')
590 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200591 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 break;
593 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200594 if (*(p + 2) == 'f')
595 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200596 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 break;
598 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200599 return FAIL;
600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200601 case 'A':
602 if (*(p + 2) == 'Z')
603 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200604 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200605 break;
606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 if (*(p + 2) == 'F')
608 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200609 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 break;
611 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200612 return FAIL;
613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200614 default:
615 return FAIL;
616 }
617 p += 3;
618 }
619 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
620 {
621 newl = TRUE;
622 p += 2;
623 }
624 else if (*p == '_')
625 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200626 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200627 p ++;
628 }
629 else if (*p == '\n')
630 {
631 newl = TRUE;
632 p ++;
633 }
634 else
635 return FAIL;
636 } /* while (p < end) */
637
638 if (p != end)
639 return FAIL;
640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200642 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643
644 switch (config)
645 {
646 case CLASS_o9:
647 return extra_newl + NFA_DIGIT;
648 case CLASS_not | CLASS_o9:
649 return extra_newl + NFA_NDIGIT;
650 case CLASS_af | CLASS_AF | CLASS_o9:
651 return extra_newl + NFA_HEX;
652 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
653 return extra_newl + NFA_NHEX;
654 case CLASS_o7:
655 return extra_newl + NFA_OCTAL;
656 case CLASS_not | CLASS_o7:
657 return extra_newl + NFA_NOCTAL;
658 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
659 return extra_newl + NFA_WORD;
660 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
661 return extra_newl + NFA_NWORD;
662 case CLASS_az | CLASS_AZ | CLASS_underscore:
663 return extra_newl + NFA_HEAD;
664 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
665 return extra_newl + NFA_NHEAD;
666 case CLASS_az | CLASS_AZ:
667 return extra_newl + NFA_ALPHA;
668 case CLASS_not | CLASS_az | CLASS_AZ:
669 return extra_newl + NFA_NALPHA;
670 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200671 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200672 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200673 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200674 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200675 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200677 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200678 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680}
681
682/*
683 * Produce the bytes for equivalence class "c".
684 * Currently only handles latin1, latin9 and utf-8.
685 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
686 * equivalent to 'a OR b OR c'
687 *
688 * NOTE! When changing this function, also update reg_equi_class()
689 */
690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100691nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200693#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100694#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
697 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200699#ifdef EBCDIC
700# define A_circumflex 0x62
701# define A_diaeresis 0x63
702# define A_grave 0x64
703# define A_acute 0x65
704# define A_virguilla 0x66
705# define A_ring 0x67
706# define C_cedilla 0x68
707# define E_acute 0x71
708# define E_circumflex 0x72
709# define E_diaeresis 0x73
710# define E_grave 0x74
711# define I_acute 0x75
712# define I_circumflex 0x76
713# define I_diaeresis 0x77
714# define I_grave 0x78
715# define N_virguilla 0x69
716# define O_circumflex 0xeb
717# define O_diaeresis 0xec
718# define O_grave 0xed
719# define O_acute 0xee
720# define O_virguilla 0xef
721# define O_slash 0x80
722# define U_circumflex 0xfb
723# define U_diaeresis 0xfc
724# define U_grave 0xfd
725# define U_acute 0xfe
726# define Y_acute 0xba
727# define a_grave 0x42
728# define a_acute 0x43
729# define a_circumflex 0x44
730# define a_virguilla 0x45
731# define a_diaeresis 0x46
732# define a_ring 0x47
733# define c_cedilla 0x48
734# define e_grave 0x51
735# define e_acute 0x52
736# define e_circumflex 0x53
737# define e_diaeresis 0x54
738# define i_grave 0x55
739# define i_acute 0x56
740# define i_circumflex 0x57
741# define i_diaeresis 0x58
742# define n_virguilla 0x49
743# define o_grave 0xcb
744# define o_acute 0xcc
745# define o_circumflex 0xcd
746# define o_virguilla 0xce
747# define o_diaeresis 0xcf
748# define o_slash 0x70
749# define u_grave 0xdb
750# define u_acute 0xdc
751# define u_circumflex 0xdd
752# define u_diaeresis 0xde
753# define y_acute 0x8d
754# define y_diaeresis 0xdf
755#else
756# define A_grave 0xc0
757# define A_acute 0xc1
758# define A_circumflex 0xc2
759# define A_virguilla 0xc3
760# define A_diaeresis 0xc4
761# define A_ring 0xc5
762# define C_cedilla 0xc7
763# define E_grave 0xc8
764# define E_acute 0xc9
765# define E_circumflex 0xca
766# define E_diaeresis 0xcb
767# define I_grave 0xcc
768# define I_acute 0xcd
769# define I_circumflex 0xce
770# define I_diaeresis 0xcf
771# define N_virguilla 0xd1
772# define O_grave 0xd2
773# define O_acute 0xd3
774# define O_circumflex 0xd4
775# define O_virguilla 0xd5
776# define O_diaeresis 0xd6
777# define O_slash 0xd8
778# define U_grave 0xd9
779# define U_acute 0xda
780# define U_circumflex 0xdb
781# define U_diaeresis 0xdc
782# define Y_acute 0xdd
783# define a_grave 0xe0
784# define a_acute 0xe1
785# define a_circumflex 0xe2
786# define a_virguilla 0xe3
787# define a_diaeresis 0xe4
788# define a_ring 0xe5
789# define c_cedilla 0xe7
790# define e_grave 0xe8
791# define e_acute 0xe9
792# define e_circumflex 0xea
793# define e_diaeresis 0xeb
794# define i_grave 0xec
795# define i_acute 0xed
796# define i_circumflex 0xee
797# define i_diaeresis 0xef
798# define n_virguilla 0xf1
799# define o_grave 0xf2
800# define o_acute 0xf3
801# define o_circumflex 0xf4
802# define o_virguilla 0xf5
803# define o_diaeresis 0xf6
804# define o_slash 0xf8
805# define u_grave 0xf9
806# define u_acute 0xfa
807# define u_circumflex 0xfb
808# define u_diaeresis 0xfc
809# define y_acute 0xfd
810# define y_diaeresis 0xff
811#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 switch (c)
813 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200814 case 'A': case A_grave: case A_acute: case A_circumflex:
815 case A_virguilla: case A_diaeresis: case A_ring:
816 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
817 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
818 CASEMBC(0x1ea2)
819 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
820 EMIT2(A_circumflex); EMIT2(A_virguilla);
821 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200822 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
823 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
824 EMITMBC(0x1ea2)
825 return OK;
826
827 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
828 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200829 return OK;
830
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200831 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
832 CASEMBC(0x10a) CASEMBC(0x10c)
833 EMIT2('C'); EMIT2(C_cedilla);
834 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200835 EMITMBC(0x10a) EMITMBC(0x10c)
836 return OK;
837
838 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200839 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200840 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
841 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200842 return OK;
843
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200844 case 'E': case E_grave: case E_acute: case E_circumflex:
845 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
846 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
847 CASEMBC(0x1eba) CASEMBC(0x1ebc)
848 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
849 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200850 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
851 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
852 EMITMBC(0x1ebc)
853 return OK;
854
855 case 'F': CASEMBC(0x1e1e)
856 EMIT2('F'); EMITMBC(0x1e1e)
857 return OK;
858
859 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200860 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
861 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200862 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
863 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
864 EMITMBC(0x1f4) EMITMBC(0x1e20)
865 return OK;
866
867 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200868 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200869 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
870 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200871 return OK;
872
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200873 case 'I': case I_grave: case I_acute: case I_circumflex:
874 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
875 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
876 CASEMBC(0x1cf) CASEMBC(0x1ec8)
877 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
878 EMIT2(I_circumflex); EMIT2(I_diaeresis);
879 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200880 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
881 EMITMBC(0x1cf) EMITMBC(0x1ec8)
882 return OK;
883
884 case 'J': CASEMBC(0x134)
885 EMIT2('J'); EMITMBC(0x134)
886 return OK;
887
888 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200889 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200890 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
891 EMITMBC(0x1e34)
892 return OK;
893
894 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
897 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
898 return OK;
899
900 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
901 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200902 return OK;
903
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200904 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
905 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
906 EMIT2('N'); EMIT2(N_virguilla);
907 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200908 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909 return OK;
910
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200911 case 'O': case O_grave: case O_acute: case O_circumflex:
912 case O_virguilla: case O_diaeresis: case O_slash:
913 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
914 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
915 CASEMBC(0x1ec) CASEMBC(0x1ece)
916 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
917 EMIT2(O_circumflex); EMIT2(O_virguilla);
918 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200919 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
920 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
921 EMITMBC(0x1ec) EMITMBC(0x1ece)
922 return OK;
923
924 case 'P': case 0x1e54: case 0x1e56:
925 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
926 return OK;
927
928 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200930 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
931 EMITMBC(0x1e58) EMITMBC(0x1e5e)
932 return OK;
933
934 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200935 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
937 EMITMBC(0x160) EMITMBC(0x1e60)
938 return OK;
939
940 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200941 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200942 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
943 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200944 return OK;
945
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200946 case 'U': case U_grave: case U_acute: case U_diaeresis:
947 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
948 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
949 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
950 CASEMBC(0x1ee6)
951 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
952 EMIT2(U_diaeresis); EMIT2(U_circumflex);
953 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200954 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
955 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
956 EMITMBC(0x1ee6)
957 return OK;
958
959 case 'V': CASEMBC(0x1e7c)
960 EMIT2('V'); EMITMBC(0x1e7c)
961 return OK;
962
963 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200964 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200965 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
966 EMITMBC(0x1e84) EMITMBC(0x1e86)
967 return OK;
968
969 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
970 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 return OK;
972
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200973 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
974 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
975 CASEMBC(0x1ef8)
976 EMIT2('Y'); EMIT2(Y_acute);
977 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200978 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
979 EMITMBC(0x1ef8)
980 return OK;
981
982 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200983 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200984 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
985 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200986 return OK;
987
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200988 case 'a': case a_grave: case a_acute: case a_circumflex:
989 case a_virguilla: case a_diaeresis: case a_ring:
990 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
991 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
992 CASEMBC(0x1ea3)
993 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
994 EMIT2(a_circumflex); EMIT2(a_virguilla);
995 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200996 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
997 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
998 EMITMBC(0x1ea3)
999 return OK;
1000
1001 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1002 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 return OK;
1004
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001005 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1006 CASEMBC(0x10b) CASEMBC(0x10d)
1007 EMIT2('c'); EMIT2(c_cedilla);
1008 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001009 EMITMBC(0x10b) EMITMBC(0x10d)
1010 return OK;
1011
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001012 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001013 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001014 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1015 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001016 return OK;
1017
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001018 case 'e': case e_grave: case e_acute: case e_circumflex:
1019 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1020 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1021 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1022 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1023 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1024 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001025 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1026 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1027 return OK;
1028
1029 case 'f': CASEMBC(0x1e1f)
1030 EMIT2('f'); EMITMBC(0x1e1f)
1031 return OK;
1032
1033 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001034 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1035 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001036 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1037 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1038 EMITMBC(0x1f5) EMITMBC(0x1e21)
1039 return OK;
1040
1041 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001042 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001043 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1044 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001045 return OK;
1046
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001047 case 'i': case i_grave: case i_acute: case i_circumflex:
1048 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1049 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1050 CASEMBC(0x1ec9)
1051 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1052 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1053 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001054 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1055 EMITMBC(0x1ec9)
1056 return OK;
1057
1058 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1059 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1060 return OK;
1061
1062 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001063 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001064 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1065 EMITMBC(0x1e35)
1066 return OK;
1067
1068 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001070 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1071 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1072 return OK;
1073
1074 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1075 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001076 return OK;
1077
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001078 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1079 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1080 CASEMBC(0x1e49)
1081 EMIT2('n'); EMIT2(n_virguilla);
1082 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001083 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1084 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 return OK;
1086
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001087 case 'o': case o_grave: case o_acute: case o_circumflex:
1088 case o_virguilla: case o_diaeresis: case o_slash:
1089 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1090 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1091 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1092 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1093 EMIT2(o_circumflex); EMIT2(o_virguilla);
1094 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001095 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1096 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1097 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1098 return OK;
1099
1100 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1101 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1102 return OK;
1103
1104 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001105 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001106 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1107 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1108 return OK;
1109
1110 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001111 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001112 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1113 EMITMBC(0x161) EMITMBC(0x1e61)
1114 return OK;
1115
1116 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001117 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001118 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1119 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120 return OK;
1121
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001122 case 'u': case u_grave: case u_acute: case u_circumflex:
1123 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1124 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1125 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1126 CASEMBC(0x1ee7)
1127 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1128 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1129 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001130 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1131 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1132 EMITMBC(0x1ee7)
1133 return OK;
1134
1135 case 'v': CASEMBC(0x1e7d)
1136 EMIT2('v'); EMITMBC(0x1e7d)
1137 return OK;
1138
1139 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001140 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001141 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1142 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1143 return OK;
1144
1145 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1146 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001147 return OK;
1148
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001149 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1150 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1151 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1152 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1153 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001154 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1155 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001156 return OK;
1157
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001158 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001159 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001160 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1161 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1162 return OK;
1163
1164 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001165 }
1166 }
1167
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169 return OK;
1170#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001171#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001172}
1173
1174/*
1175 * Code to parse regular expression.
1176 *
1177 * We try to reuse parsing functions in regexp.c to
1178 * minimize surprise and keep the syntax consistent.
1179 */
1180
1181/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001182 * Parse the lowest level.
1183 *
1184 * An atom can be one of a long list of items. Many atoms match one character
1185 * in the text. It is often an ordinary character or a character class.
1186 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1187 * is only for syntax highlighting.
1188 *
1189 * atom ::= ordinary-atom
1190 * or \( pattern \)
1191 * or \%( pattern \)
1192 * or \z( pattern \)
1193 */
1194 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001195nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001196{
1197 int c;
1198 int charclass;
1199 int equiclass;
1200 int collclass;
1201 int got_coll_char;
1202 char_u *p;
1203 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001204 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 int emit_range;
1207 int negated;
1208 int result;
1209 int startc = -1;
1210 int endc = -1;
1211 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001212 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001215 switch (c)
1216 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001217 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001218 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001220 case Magic('^'):
1221 EMIT(NFA_BOL);
1222 break;
1223
1224 case Magic('$'):
1225 EMIT(NFA_EOL);
1226#if defined(FEAT_SYN_HL) || defined(PROTO)
1227 had_eol = TRUE;
1228#endif
1229 break;
1230
1231 case Magic('<'):
1232 EMIT(NFA_BOW);
1233 break;
1234
1235 case Magic('>'):
1236 EMIT(NFA_EOW);
1237 break;
1238
1239 case Magic('_'):
1240 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001241 if (c == NUL)
1242 EMSG_RET_FAIL(_(e_nul_found));
1243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001244 if (c == '^') /* "\_^" is start-of-line */
1245 {
1246 EMIT(NFA_BOL);
1247 break;
1248 }
1249 if (c == '$') /* "\_$" is end-of-line */
1250 {
1251 EMIT(NFA_EOL);
1252#if defined(FEAT_SYN_HL) || defined(PROTO)
1253 had_eol = TRUE;
1254#endif
1255 break;
1256 }
1257
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001258 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259
1260 /* "\_[" is collection plus newline */
1261 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001262 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
1264 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001265 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266
1267 /*
1268 * Character classes.
1269 */
1270 case Magic('.'):
1271 case Magic('i'):
1272 case Magic('I'):
1273 case Magic('k'):
1274 case Magic('K'):
1275 case Magic('f'):
1276 case Magic('F'):
1277 case Magic('p'):
1278 case Magic('P'):
1279 case Magic('s'):
1280 case Magic('S'):
1281 case Magic('d'):
1282 case Magic('D'):
1283 case Magic('x'):
1284 case Magic('X'):
1285 case Magic('o'):
1286 case Magic('O'):
1287 case Magic('w'):
1288 case Magic('W'):
1289 case Magic('h'):
1290 case Magic('H'):
1291 case Magic('a'):
1292 case Magic('A'):
1293 case Magic('l'):
1294 case Magic('L'):
1295 case Magic('u'):
1296 case Magic('U'):
1297 p = vim_strchr(classchars, no_Magic(c));
1298 if (p == NULL)
1299 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001300 if (extra == NFA_ADD_NL)
1301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001302 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001303 rc_did_emsg = TRUE;
1304 return FAIL;
1305 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001306 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001307 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001309
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001310 /* When '.' is followed by a composing char ignore the dot, so that
1311 * the composing char is matched here. */
1312 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1313 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001314 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 c = getchr();
1316 goto nfa_do_multibyte;
1317 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001319 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 {
1321 EMIT(NFA_NEWL);
1322 EMIT(NFA_OR);
1323 regflags |= RF_HASNL;
1324 }
1325 break;
1326
1327 case Magic('n'):
1328 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001329 /* In a string "\n" matches a newline character. */
1330 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 else
1332 {
1333 /* In buffer text "\n" matches the end of a line. */
1334 EMIT(NFA_NEWL);
1335 regflags |= RF_HASNL;
1336 }
1337 break;
1338
1339 case Magic('('):
1340 if (nfa_reg(REG_PAREN) == FAIL)
1341 return FAIL; /* cascaded error */
1342 break;
1343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 case Magic('|'):
1345 case Magic('&'):
1346 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001347 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 return FAIL;
1349
1350 case Magic('='):
1351 case Magic('?'):
1352 case Magic('+'):
1353 case Magic('@'):
1354 case Magic('*'):
1355 case Magic('{'):
1356 /* these should follow an atom, not form an atom */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 return FAIL;
1359
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001360 case Magic('~'):
1361 {
1362 char_u *lp;
1363
1364 /* Previous substitute pattern.
1365 * Generated as "\%(pattern\)". */
1366 if (reg_prev_sub == NULL)
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001369 return FAIL;
1370 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001371 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001372 {
1373 EMIT(PTR2CHAR(lp));
1374 if (lp != reg_prev_sub)
1375 EMIT(NFA_CONCAT);
1376 }
1377 EMIT(NFA_NOPEN);
1378 break;
1379 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001380
Bram Moolenaar428e9872013-05-30 17:05:39 +02001381 case Magic('1'):
1382 case Magic('2'):
1383 case Magic('3'):
1384 case Magic('4'):
1385 case Magic('5'):
1386 case Magic('6'):
1387 case Magic('7'):
1388 case Magic('8'):
1389 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001390 {
1391 int refnum = no_Magic(c) - '1';
1392
1393 if (!seen_endbrace(refnum + 1))
1394 return FAIL;
1395 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001396 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001397 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001398 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399
1400 case Magic('z'):
1401 c = no_Magic(getchr());
1402 switch (c)
1403 {
1404 case 's':
1405 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001406 if (re_mult_next("\\zs") == FAIL)
1407 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408 break;
1409 case 'e':
1410 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001411 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001412 if (re_mult_next("\\ze") == FAIL)
1413 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001414 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001415#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 case '1':
1417 case '2':
1418 case '3':
1419 case '4':
1420 case '5':
1421 case '6':
1422 case '7':
1423 case '8':
1424 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001425 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001426 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001427 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001428 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar0270f382018-07-17 05:43:58 +02001429 /* No need to set rex.nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001430 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001431 re_has_z = REX_USE;
1432 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001433 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001434 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001435 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001436 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001437 if (nfa_reg(REG_ZPAREN) == FAIL)
1438 return FAIL; /* cascaded error */
1439 re_has_z = REX_SET;
1440 break;
1441#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001443 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 no_Magic(c));
1445 return FAIL;
1446 }
1447 break;
1448
1449 case Magic('%'):
1450 c = no_Magic(getchr());
1451 switch (c)
1452 {
1453 /* () without a back reference */
1454 case '(':
1455 if (nfa_reg(REG_NPAREN) == FAIL)
1456 return FAIL;
1457 EMIT(NFA_NOPEN);
1458 break;
1459
1460 case 'd': /* %d123 decimal */
1461 case 'o': /* %o123 octal */
1462 case 'x': /* %xab hex 2 */
1463 case 'u': /* %uabcd hex 4 */
1464 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001465 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001466 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467
Bram Moolenaar47196582013-05-25 22:04:23 +02001468 switch (c)
1469 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001470 case 'd': nr = getdecchrs(); break;
1471 case 'o': nr = getoctchrs(); break;
1472 case 'x': nr = gethexchrs(2); break;
1473 case 'u': nr = gethexchrs(4); break;
1474 case 'U': nr = gethexchrs(8); break;
1475 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001476 }
1477
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001478 if (nr < 0 || nr > INT_MAX)
Bram Moolenaar47196582013-05-25 22:04:23 +02001479 EMSG2_RET_FAIL(
1480 _("E678: Invalid character after %s%%[dxouU]"),
1481 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001482 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001483 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001484 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001485 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 break;
1487
1488 /* Catch \%^ and \%$ regardless of where they appear in the
1489 * pattern -- regardless of whether or not it makes sense. */
1490 case '^':
1491 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 break;
1493
1494 case '$':
1495 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 break;
1497
1498 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001499 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 break;
1501
1502 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001503 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 break;
1505
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001506 case 'C':
1507 EMIT(NFA_ANY_COMPOSING);
1508 break;
1509
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001511 {
1512 int n;
1513
1514 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001515 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001516 {
1517 if (c == NUL)
1518 EMSG2_RET_FAIL(_(e_missing_sb),
1519 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001520 /* recursive call! */
1521 if (nfa_regatom() == FAIL)
1522 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001523 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001524 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001525 if (n == 0)
1526 EMSG2_RET_FAIL(_(e_empty_sb),
1527 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001528 EMIT(NFA_OPT_CHARS);
1529 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001530
1531 /* Emit as "\%(\%[abc]\)" to be able to handle
1532 * "\%[abc]*" which would cause the empty string to be
1533 * matched an unlimited number of times. NFA_NOPEN is
1534 * added only once at a position, while NFA_SPLIT is
1535 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001536 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001537 * a lot. */
1538 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001539 break;
1540 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001541
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001543 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001544 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001545 int cmp = c;
1546
1547 if (c == '<' || c == '>')
1548 c = getchr();
1549 while (VIM_ISDIGIT(c))
1550 {
1551 n = n * 10 + (c - '0');
1552 c = getchr();
1553 }
1554 if (c == 'l' || c == 'c' || c == 'v')
1555 {
Bram Moolenaar9403a212019-02-13 18:35:06 +01001556 int limit = INT_MAX;
1557
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001559 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001560 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001561 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001562 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001563 if (save_prev_at_start)
1564 at_start = TRUE;
1565 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001566 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001567 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001568 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001569 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001570 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001571 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001572 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001573 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001574 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001575 limit = INT_MAX / MB_MAXBYTES;
1576 }
1577 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001579 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001580 return FAIL;
1581 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001582 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001583 break;
1584 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001585 else if (c == '\'' && n == 0)
1586 {
1587 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001588 EMIT(cmp == '<' ? NFA_MARK_LT :
1589 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001590 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001591 break;
1592 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001593 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001595 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596 return FAIL;
1597 }
1598 break;
1599
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001600 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001601collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001603 * [abc] uses NFA_START_COLL - NFA_END_COLL
1604 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1605 * Each character is produced as a regular state, using
1606 * NFA_CONCAT to bind them together.
1607 * Besides normal characters there can be:
1608 * - character classes NFA_CLASS_*
1609 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 */
1611
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 p = regparse;
1613 endp = skip_anyof(p);
1614 if (*endp == ']')
1615 {
1616 /*
1617 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001618 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 * and perform the necessary substitutions in the NFA.
1620 */
1621 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001622 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 if (result != FAIL)
1624 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001625 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001627 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 EMIT(NFA_NEWL);
1629 EMIT(NFA_OR);
1630 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001631 else
1632 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001633 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001634 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 return OK;
1636 }
1637 /*
1638 * Failed to recognize a character class. Use the simple
1639 * version that turns [abc] into 'a' OR 'b' OR 'c'
1640 */
1641 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001643 if (*regparse == '^') /* negated range */
1644 {
1645 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001646 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001647 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001648 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001649 else
1650 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 if (*regparse == '-')
1652 {
1653 startc = '-';
1654 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001655 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001656 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 }
1658 /* Emit the OR branches for each character in the [] */
1659 emit_range = FALSE;
1660 while (regparse < endp)
1661 {
1662 oldstartc = startc;
1663 startc = -1;
1664 got_coll_char = FALSE;
1665 if (*regparse == '[')
1666 {
1667 /* Check for [: :], [= =], [. .] */
1668 equiclass = collclass = 0;
1669 charclass = get_char_class(&regparse);
1670 if (charclass == CLASS_NONE)
1671 {
1672 equiclass = get_equi_class(&regparse);
1673 if (equiclass == 0)
1674 collclass = get_coll_element(&regparse);
1675 }
1676
1677 /* Character class like [:alpha:] */
1678 if (charclass != CLASS_NONE)
1679 {
1680 switch (charclass)
1681 {
1682 case CLASS_ALNUM:
1683 EMIT(NFA_CLASS_ALNUM);
1684 break;
1685 case CLASS_ALPHA:
1686 EMIT(NFA_CLASS_ALPHA);
1687 break;
1688 case CLASS_BLANK:
1689 EMIT(NFA_CLASS_BLANK);
1690 break;
1691 case CLASS_CNTRL:
1692 EMIT(NFA_CLASS_CNTRL);
1693 break;
1694 case CLASS_DIGIT:
1695 EMIT(NFA_CLASS_DIGIT);
1696 break;
1697 case CLASS_GRAPH:
1698 EMIT(NFA_CLASS_GRAPH);
1699 break;
1700 case CLASS_LOWER:
1701 EMIT(NFA_CLASS_LOWER);
1702 break;
1703 case CLASS_PRINT:
1704 EMIT(NFA_CLASS_PRINT);
1705 break;
1706 case CLASS_PUNCT:
1707 EMIT(NFA_CLASS_PUNCT);
1708 break;
1709 case CLASS_SPACE:
1710 EMIT(NFA_CLASS_SPACE);
1711 break;
1712 case CLASS_UPPER:
1713 EMIT(NFA_CLASS_UPPER);
1714 break;
1715 case CLASS_XDIGIT:
1716 EMIT(NFA_CLASS_XDIGIT);
1717 break;
1718 case CLASS_TAB:
1719 EMIT(NFA_CLASS_TAB);
1720 break;
1721 case CLASS_RETURN:
1722 EMIT(NFA_CLASS_RETURN);
1723 break;
1724 case CLASS_BACKSPACE:
1725 EMIT(NFA_CLASS_BACKSPACE);
1726 break;
1727 case CLASS_ESCAPE:
1728 EMIT(NFA_CLASS_ESCAPE);
1729 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001730 case CLASS_IDENT:
1731 EMIT(NFA_CLASS_IDENT);
1732 break;
1733 case CLASS_KEYWORD:
1734 EMIT(NFA_CLASS_KEYWORD);
1735 break;
1736 case CLASS_FNAME:
1737 EMIT(NFA_CLASS_FNAME);
1738 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001740 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741 continue;
1742 }
1743 /* Try equivalence class [=a=] and the like */
1744 if (equiclass != 0)
1745 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001746 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 if (result == FAIL)
1748 {
1749 /* should never happen */
1750 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001752 continue;
1753 }
1754 /* Try collating class like [. .] */
1755 if (collclass != 0)
1756 {
1757 startc = collclass; /* allow [.a.]-x as a range */
1758 /* Will emit the proper atom at the end of the
1759 * while loop. */
1760 }
1761 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001762 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1763 * start character. */
1764 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 {
1766 emit_range = TRUE;
1767 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001768 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 continue; /* reading the end of the range */
1770 }
1771
1772 /* Now handle simple and escaped characters.
1773 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1774 * accepts "\t", "\e", etc., but only when the 'l' flag in
1775 * 'cpoptions' is not included.
1776 * Posix doesn't recognize backslash at all.
1777 */
1778 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001779 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 && regparse + 1 <= endp
1781 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001782 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 && vim_strchr(REGEXP_ABBR, regparse[1])
1784 != NULL)
1785 )
1786 )
1787 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001788 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001790 if (*regparse == 'n')
Bram Moolenaara5483442019-02-17 20:17:02 +01001791 startc = (reg_string || emit_range
1792 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001793 else
1794 if (*regparse == 'd'
1795 || *regparse == 'o'
1796 || *regparse == 'x'
1797 || *regparse == 'u'
1798 || *regparse == 'U'
1799 )
1800 {
1801 /* TODO(RE) This needs more testing */
1802 startc = coll_get_char();
1803 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001804 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805 }
1806 else
1807 {
1808 /* \r,\t,\e,\b */
1809 startc = backslash_trans(*regparse);
1810 }
1811 }
1812
1813 /* Normal printable char */
1814 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001815 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816
1817 /* Previous char was '-', so this char is end of range. */
1818 if (emit_range)
1819 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001820 endc = startc;
1821 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001822 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001823 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001824
1825 if (endc > startc + 2)
1826 {
1827 /* Emit a range instead of the sequence of
1828 * individual characters. */
1829 if (startc == 0)
1830 /* \x00 is translated to \x0a, start at \x01. */
1831 EMIT(1);
1832 else
1833 --post_ptr; /* remove NFA_CONCAT */
1834 EMIT(endc);
1835 EMIT(NFA_RANGE);
1836 EMIT(NFA_CONCAT);
1837 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001838 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001839 || (*mb_char2len)(endc) > 1))
1840 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001841 /* Emit the characters in the range.
1842 * "startc" was already emitted, so skip it.
1843 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 for (c = startc + 1; c <= endc; c++)
1845 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001846 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001847 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001849 }
1850 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 {
1852#ifdef EBCDIC
1853 int alpha_only = FALSE;
1854
1855 /* for alphabetical range skip the gaps
1856 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1857 if (isalpha(startc) && isalpha(endc))
1858 alpha_only = TRUE;
1859#endif
1860 /* Emit the range. "startc" was already emitted, so
1861 * skip it. */
1862 for (c = startc + 1; c <= endc; c++)
1863#ifdef EBCDIC
1864 if (!alpha_only || isalpha(startc))
1865#endif
1866 {
1867 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001868 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001871 emit_range = FALSE;
1872 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001873 }
1874 else
1875 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001876 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878 * Normally, simply emit startc. But if we get char
1879 * code=0 from a collating char, then replace it with
1880 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001882 * the backtracking engine. */
1883 if (startc == NFA_NEWL)
1884 {
1885 /* Line break can't be matched as part of the
1886 * collection, add an OR below. But not for negated
1887 * range. */
1888 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001889 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001890 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001891 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001892 {
1893 if (got_coll_char == TRUE && startc == 0)
1894 EMIT(0x0a);
1895 else
1896 EMIT(startc);
1897 EMIT(NFA_CONCAT);
1898 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 }
1900
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001901 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 } /* while (p < endp) */
1903
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001904 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905 if (*regparse == '-') /* if last, '-' is just a char */
1906 {
1907 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001908 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911 /* skip the trailing ] */
1912 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001913 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001914
1915 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001916 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001917 EMIT(NFA_END_NEG_COLL);
1918 else
1919 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001920
1921 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001922 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001923 {
1924 EMIT(reg_string ? NL : NFA_NEWL);
1925 EMIT(NFA_OR);
1926 }
1927
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001929 } /* if exists closing ] */
1930
1931 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001932 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001933 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 default:
1936 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001937 int plen;
1938
1939nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001940 /* plen is length of current char with composing chars */
1941 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001942 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001943 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001945 int i = 0;
1946
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001947 /* A base character plus composing characters, or just one
1948 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001949 * This requires creating a separate atom as if enclosing
1950 * the characters in (), where NFA_COMPOSING is the ( and
1951 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 * building the postfix form, not the NFA itself;
1953 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001954 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001955 for (;;)
1956 {
1957 EMIT(c);
1958 if (i > 0)
1959 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001960 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001961 break;
1962 c = utf_ptr2char(old_regparse + i);
1963 }
1964 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001965 regparse = old_regparse + plen;
1966 }
1967 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968 {
1969 c = no_Magic(c);
1970 EMIT(c);
1971 }
1972 return OK;
1973 }
1974 }
1975
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976 return OK;
1977}
1978
1979/*
1980 * Parse something followed by possible [*+=].
1981 *
1982 * A piece is an atom, possibly followed by a multi, an indication of how many
1983 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1984 * characters: "", "a", "aa", etc.
1985 *
1986 * piece ::= atom
1987 * or atom multi
1988 */
1989 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001990nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001991{
1992 int i;
1993 int op;
1994 int ret;
1995 long minval, maxval;
1996 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001997 parse_state_T old_state;
1998 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001999 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002000 int old_post_pos;
2001 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 int quest;
2003
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002004 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2005 * next. */
2006 save_parse_state(&old_state);
2007
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002009 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002010
2011 ret = nfa_regatom();
2012 if (ret == FAIL)
2013 return FAIL; /* cascaded error */
2014
2015 op = peekchr();
2016 if (re_multi_type(op) == NOT_MULTI)
2017 return OK;
2018
2019 skipchr();
2020 switch (op)
2021 {
2022 case Magic('*'):
2023 EMIT(NFA_STAR);
2024 break;
2025
2026 case Magic('+'):
2027 /*
2028 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2029 * first and only submatch would be "aaa". But the backtracking
2030 * engine interprets the plus as "try matching one more time", and
2031 * a* matches a second time at the end of the input, the empty
2032 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002033 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002035 * In order to be consistent with the old engine, we replace
2036 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002038 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002039 curchr = -1;
2040 if (nfa_regatom() == FAIL)
2041 return FAIL;
2042 EMIT(NFA_STAR);
2043 EMIT(NFA_CONCAT);
2044 skipchr(); /* skip the \+ */
2045 break;
2046
2047 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002048 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002050 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002051 switch(op)
2052 {
2053 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002054 /* \@= */
2055 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002057 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002058 /* \@! */
2059 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002060 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002062 op = no_Magic(getchr());
2063 if (op == '=')
2064 /* \@<= */
2065 i = NFA_PREV_ATOM_JUST_BEFORE;
2066 else if (op == '!')
2067 /* \@<! */
2068 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2069 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002071 /* \@> */
2072 i = NFA_PREV_ATOM_LIKE_PATTERN;
2073 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002074 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002075 if (i == 0)
2076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002077 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002078 return FAIL;
2079 }
2080 EMIT(i);
2081 if (i == NFA_PREV_ATOM_JUST_BEFORE
2082 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2083 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002084 break;
2085
2086 case Magic('?'):
2087 case Magic('='):
2088 EMIT(NFA_QUEST);
2089 break;
2090
2091 case Magic('{'):
2092 /* a{2,5} will expand to 'aaa?a?a?'
2093 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2094 * version of '?'
2095 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2096 * parenthesis have the same id
2097 */
2098
2099 greedy = TRUE;
2100 c2 = peekchr();
2101 if (c2 == '-' || c2 == Magic('-'))
2102 {
2103 skipchr();
2104 greedy = FALSE;
2105 }
2106 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002107 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002108
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002109 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2110 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002111 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002113 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002114 /* \{}, \{0,} */
2115 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002116 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002117 /* \{-}, \{-0,} */
2118 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002119 break;
2120 }
2121
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 /* Special case: x{0} or x{-0} */
2123 if (maxval == 0)
2124 {
2125 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002126 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002127 /* NFA_EMPTY is 0-length and works everywhere */
2128 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002129 return OK;
2130 }
2131
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002132 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002133 * maximum is much larger than the minimum and when the maximum is
2134 * large. Bail out if we can use the other engine. */
2135 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002136 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002137 return FAIL;
2138
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002139 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002140 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002141 /* Save parse state after the repeated atom and the \{} */
2142 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002144 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2145 for (i = 0; i < maxval; i++)
2146 {
2147 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002148 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002149 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 if (nfa_regatom() == FAIL)
2151 return FAIL;
2152 /* after "minval" times, atoms are optional */
2153 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002154 {
2155 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002156 {
2157 if (greedy)
2158 EMIT(NFA_STAR);
2159 else
2160 EMIT(NFA_STAR_NONGREEDY);
2161 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002162 else
2163 EMIT(quest);
2164 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002165 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002167 if (i + 1 > minval && maxval == MAX_LIMIT)
2168 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169 }
2170
2171 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002172 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 curchr = -1;
2174
2175 break;
2176
2177
2178 default:
2179 break;
2180 } /* end switch */
2181
2182 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002183 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002184 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002185
2186 return OK;
2187}
2188
2189/*
2190 * Parse one or more pieces, concatenated. It matches a match for the
2191 * first piece, followed by a match for the second piece, etc. Example:
2192 * "f[0-9]b", first matches "f", then a digit and then "b".
2193 *
2194 * concat ::= piece
2195 * or piece piece
2196 * or piece piece piece
2197 * etc.
2198 */
2199 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002200nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201{
2202 int cont = TRUE;
2203 int first = TRUE;
2204
2205 while (cont)
2206 {
2207 switch (peekchr())
2208 {
2209 case NUL:
2210 case Magic('|'):
2211 case Magic('&'):
2212 case Magic(')'):
2213 cont = FALSE;
2214 break;
2215
2216 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 skipchr_keepstart();
2219 break;
2220 case Magic('c'):
2221 regflags |= RF_ICASE;
2222 skipchr_keepstart();
2223 break;
2224 case Magic('C'):
2225 regflags |= RF_NOICASE;
2226 skipchr_keepstart();
2227 break;
2228 case Magic('v'):
2229 reg_magic = MAGIC_ALL;
2230 skipchr_keepstart();
2231 curchr = -1;
2232 break;
2233 case Magic('m'):
2234 reg_magic = MAGIC_ON;
2235 skipchr_keepstart();
2236 curchr = -1;
2237 break;
2238 case Magic('M'):
2239 reg_magic = MAGIC_OFF;
2240 skipchr_keepstart();
2241 curchr = -1;
2242 break;
2243 case Magic('V'):
2244 reg_magic = MAGIC_NONE;
2245 skipchr_keepstart();
2246 curchr = -1;
2247 break;
2248
2249 default:
2250 if (nfa_regpiece() == FAIL)
2251 return FAIL;
2252 if (first == FALSE)
2253 EMIT(NFA_CONCAT);
2254 else
2255 first = FALSE;
2256 break;
2257 }
2258 }
2259
2260 return OK;
2261}
2262
2263/*
2264 * Parse a branch, one or more concats, separated by "\&". It matches the
2265 * last concat, but only if all the preceding concats also match at the same
2266 * position. Examples:
2267 * "foobeep\&..." matches "foo" in "foobeep".
2268 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2269 *
2270 * branch ::= concat
2271 * or concat \& concat
2272 * or concat \& concat \& concat
2273 * etc.
2274 */
2275 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002276nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002277{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002278 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279
Bram Moolenaar16299b52013-05-30 18:45:23 +02002280 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002281
2282 /* First branch, possibly the only one */
2283 if (nfa_regconcat() == FAIL)
2284 return FAIL;
2285
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002287 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002288 {
2289 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002290 /* if concat is empty do emit a node */
2291 if (old_post_pos == (int)(post_ptr - post_start))
2292 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293 EMIT(NFA_NOPEN);
2294 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002295 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296 if (nfa_regconcat() == FAIL)
2297 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002298 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002299 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002300 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302 }
2303
Bram Moolenaar699c1202013-09-25 16:41:54 +02002304 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002305 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002306 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002307
2308 return OK;
2309}
2310
2311/*
2312 * Parse a pattern, one or more branches, separated by "\|". It matches
2313 * anything that matches one of the branches. Example: "foo\|beep" matches
2314 * "foo" and matches "beep". If more than one branch matches, the first one
2315 * is used.
2316 *
2317 * pattern ::= branch
2318 * or branch \| branch
2319 * or branch \| branch \| branch
2320 * etc.
2321 */
2322 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002323nfa_reg(
2324 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325{
2326 int parno = 0;
2327
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328 if (paren == REG_PAREN)
2329 {
2330 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002332 parno = regnpar++;
2333 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002334#ifdef FEAT_SYN_HL
2335 else if (paren == REG_ZPAREN)
2336 {
2337 /* Make a ZOPEN node. */
2338 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002339 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002340 parno = regnzpar++;
2341 }
2342#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002343
2344 if (nfa_regbranch() == FAIL)
2345 return FAIL; /* cascaded error */
2346
2347 while (peekchr() == Magic('|'))
2348 {
2349 skipchr();
2350 if (nfa_regbranch() == FAIL)
2351 return FAIL; /* cascaded error */
2352 EMIT(NFA_OR);
2353 }
2354
2355 /* Check for proper termination. */
2356 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2357 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 if (paren == REG_NPAREN)
2359 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2360 else
2361 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2362 }
2363 else if (paren == REG_NOPAREN && peekchr() != NUL)
2364 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365 if (peekchr() == Magic(')'))
2366 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2367 else
2368 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2369 }
2370 /*
2371 * Here we set the flag allowing back references to this set of
2372 * parentheses.
2373 */
2374 if (paren == REG_PAREN)
2375 {
2376 had_endbrace[parno] = TRUE; /* have seen the close paren */
2377 EMIT(NFA_MOPEN + parno);
2378 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002379#ifdef FEAT_SYN_HL
2380 else if (paren == REG_ZPAREN)
2381 EMIT(NFA_ZOPEN + parno);
2382#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383
2384 return OK;
2385}
2386
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002387#ifdef DEBUG
2388static char_u code[50];
2389
2390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002391nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002392{
2393 int addnl = FALSE;
2394
2395 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2396 {
2397 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002398 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002399 }
2400
2401 STRCPY(code, "");
2402 switch (c)
2403 {
2404 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2405 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2406 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2407 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2408 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2409 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2410
Bram Moolenaar5714b802013-05-28 22:03:20 +02002411 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2412 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2413 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2414 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2415 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2416 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2417 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2418 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2419 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002420#ifdef FEAT_SYN_HL
2421 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2422 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2423 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2424 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2425 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2426 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2427 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2428 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2429 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2430#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002431 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2432
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433 case NFA_PREV_ATOM_NO_WIDTH:
2434 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002435 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2436 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002437 case NFA_PREV_ATOM_JUST_BEFORE:
2438 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2439 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2440 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002441 case NFA_PREV_ATOM_LIKE_PATTERN:
2442 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2443
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002444 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2445 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002447 case NFA_START_INVISIBLE_FIRST:
2448 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002449 case NFA_START_INVISIBLE_NEG:
2450 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002451 case NFA_START_INVISIBLE_NEG_FIRST:
2452 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002453 case NFA_START_INVISIBLE_BEFORE:
2454 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002455 case NFA_START_INVISIBLE_BEFORE_FIRST:
2456 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002457 case NFA_START_INVISIBLE_BEFORE_NEG:
2458 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002459 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2460 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002461 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002463 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002464 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002465
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2467 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002468 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002470 case NFA_MOPEN:
2471 case NFA_MOPEN1:
2472 case NFA_MOPEN2:
2473 case NFA_MOPEN3:
2474 case NFA_MOPEN4:
2475 case NFA_MOPEN5:
2476 case NFA_MOPEN6:
2477 case NFA_MOPEN7:
2478 case NFA_MOPEN8:
2479 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480 STRCPY(code, "NFA_MOPEN(x)");
2481 code[10] = c - NFA_MOPEN + '0';
2482 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002483 case NFA_MCLOSE:
2484 case NFA_MCLOSE1:
2485 case NFA_MCLOSE2:
2486 case NFA_MCLOSE3:
2487 case NFA_MCLOSE4:
2488 case NFA_MCLOSE5:
2489 case NFA_MCLOSE6:
2490 case NFA_MCLOSE7:
2491 case NFA_MCLOSE8:
2492 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002493 STRCPY(code, "NFA_MCLOSE(x)");
2494 code[11] = c - NFA_MCLOSE + '0';
2495 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002496#ifdef FEAT_SYN_HL
2497 case NFA_ZOPEN:
2498 case NFA_ZOPEN1:
2499 case NFA_ZOPEN2:
2500 case NFA_ZOPEN3:
2501 case NFA_ZOPEN4:
2502 case NFA_ZOPEN5:
2503 case NFA_ZOPEN6:
2504 case NFA_ZOPEN7:
2505 case NFA_ZOPEN8:
2506 case NFA_ZOPEN9:
2507 STRCPY(code, "NFA_ZOPEN(x)");
2508 code[10] = c - NFA_ZOPEN + '0';
2509 break;
2510 case NFA_ZCLOSE:
2511 case NFA_ZCLOSE1:
2512 case NFA_ZCLOSE2:
2513 case NFA_ZCLOSE3:
2514 case NFA_ZCLOSE4:
2515 case NFA_ZCLOSE5:
2516 case NFA_ZCLOSE6:
2517 case NFA_ZCLOSE7:
2518 case NFA_ZCLOSE8:
2519 case NFA_ZCLOSE9:
2520 STRCPY(code, "NFA_ZCLOSE(x)");
2521 code[11] = c - NFA_ZCLOSE + '0';
2522 break;
2523#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002524 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2525 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2526 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2527 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002528 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2529 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002530 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2531 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2532 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2533 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2534 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2535 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2536 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2537 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2538 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2539 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2540 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2541 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2542 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2543 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002544 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002545
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002546 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002547 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2548 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2549 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002550 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002551 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002552
2553 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2554 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2555 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2556 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2557 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2558 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2559 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002561 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2562 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2563 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2564 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2565 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2566 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2567 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2568 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2569 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2570 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2571 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2572 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2573 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2574 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2575 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2576 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002577 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2578 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2579 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002580
2581 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2582 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2583 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2584 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2585 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2586 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2587 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2588 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2589 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2590 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2591 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2592 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2593 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2594 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2595 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2596 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2597 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2598 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2599 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2600 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2601 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2602 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2603 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2604 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2605 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2606 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2607 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002608 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2609 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2610 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2611 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002612
2613 default:
2614 STRCPY(code, "CHAR(x)");
2615 code[5] = c;
2616 }
2617
2618 if (addnl == TRUE)
2619 STRCAT(code, " + NEWLINE ");
2620
2621}
2622
2623#ifdef ENABLE_LOG
2624static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002625static 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 +02002626
2627/*
2628 * Print the postfix notation of the current regexp.
2629 */
2630 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002631nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002632{
2633 int *p;
2634 FILE *f;
2635
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002636 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002637 if (f != NULL)
2638 {
2639 fprintf(f, "\n-------------------------\n");
2640 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002641 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002642 else if (retval == OK)
2643 fprintf(f, ">>> NFA engine succeeded !\n");
2644 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002645 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002646 {
2647 nfa_set_code(*p);
2648 fprintf(f, "%s, ", code);
2649 }
2650 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002651 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652 fprintf(f, "%d ", *p);
2653 fprintf(f, "\n\n");
2654 fclose(f);
2655 }
2656}
2657
2658/*
2659 * Print the NFA starting with a root node "state".
2660 */
2661 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002662nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002663{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002664 garray_T indent;
2665
2666 ga_init2(&indent, 1, 64);
2667 ga_append(&indent, '\0');
2668 nfa_print_state2(debugf, state, &indent);
2669 ga_clear(&indent);
2670}
2671
2672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002673nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002674{
2675 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676
2677 if (state == NULL)
2678 return;
2679
2680 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002681
2682 /* Output indent */
2683 p = (char_u *)indent->ga_data;
2684 if (indent->ga_len >= 3)
2685 {
2686 int last = indent->ga_len - 3;
2687 char_u save[2];
2688
2689 STRNCPY(save, &p[last], 2);
2690 STRNCPY(&p[last], "+-", 2);
2691 fprintf(debugf, " %s", p);
2692 STRNCPY(&p[last], save, 2);
2693 }
2694 else
2695 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696
2697 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002698 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002699 code,
2700 state->c,
2701 abs(state->id),
2702 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002703 if (state->id < 0)
2704 return;
2705
2706 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002707
2708 /* grow indent for state->out */
2709 indent->ga_len -= 1;
2710 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002711 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002712 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002713 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002714 ga_append(indent, '\0');
2715
2716 nfa_print_state2(debugf, state->out, indent);
2717
2718 /* replace last part of indent for state->out1 */
2719 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002720 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002721 ga_append(indent, '\0');
2722
2723 nfa_print_state2(debugf, state->out1, indent);
2724
2725 /* shrink indent */
2726 indent->ga_len -= 3;
2727 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728}
2729
2730/*
2731 * Print the NFA state machine.
2732 */
2733 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002734nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002735{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002736 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737
2738 if (debugf != NULL)
2739 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002740 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002741
Bram Moolenaar473de612013-06-08 18:19:48 +02002742 if (prog->reganch)
2743 fprintf(debugf, "reganch: %d\n", prog->reganch);
2744 if (prog->regstart != NUL)
2745 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2746 prog->regstart, prog->regstart);
2747 if (prog->match_text != NULL)
2748 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002749
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002750 fclose(debugf);
2751 }
2752}
2753#endif /* ENABLE_LOG */
2754#endif /* DEBUG */
2755
2756/*
2757 * Parse r.e. @expr and convert it into postfix form.
2758 * Return the postfix string on success, NULL otherwise.
2759 */
2760 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002761re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762{
2763 if (nfa_reg(REG_NOPAREN) == FAIL)
2764 return NULL;
2765 EMIT(NFA_MOPEN);
2766 return post_start;
2767}
2768
2769/* NB. Some of the code below is inspired by Russ's. */
2770
2771/*
2772 * Represents an NFA state plus zero or one or two arrows exiting.
2773 * if c == MATCH, no arrows out; matching state.
2774 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2775 * If c < 256, labeled arrow with character c to out.
2776 */
2777
2778static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2779
2780/*
2781 * Allocate and initialize nfa_state_T.
2782 */
2783 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002784alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002785{
2786 nfa_state_T *s;
2787
2788 if (istate >= nstate)
2789 return NULL;
2790
2791 s = &state_ptr[istate++];
2792
2793 s->c = c;
2794 s->out = out;
2795 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002796 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002797
2798 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002799 s->lastlist[0] = 0;
2800 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002801
2802 return s;
2803}
2804
2805/*
2806 * A partially built NFA without the matching state filled in.
2807 * Frag_T.start points at the start state.
2808 * Frag_T.out is a list of places that need to be set to the
2809 * next state for this fragment.
2810 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002811
2812/* Since the out pointers in the list are always
2813 * uninitialized, we use the pointers themselves
2814 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002815typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002816union Ptrlist
2817{
2818 Ptrlist *next;
2819 nfa_state_T *s;
2820};
2821
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822struct Frag
2823{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002824 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002825 Ptrlist *out;
2826};
2827typedef struct Frag Frag_T;
2828
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002830 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831 */
2832 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002833frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002835 Frag_T n;
2836
2837 n.start = start;
2838 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839 return n;
2840}
2841
2842/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002843 * Create singleton list containing just outp.
2844 */
2845 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002846list1(
2847 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002848{
2849 Ptrlist *l;
2850
2851 l = (Ptrlist *)outp;
2852 l->next = NULL;
2853 return l;
2854}
2855
2856/*
2857 * Patch the list of states at out to point to start.
2858 */
2859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002860patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002861{
2862 Ptrlist *next;
2863
2864 for (; l; l = next)
2865 {
2866 next = l->next;
2867 l->s = s;
2868 }
2869}
2870
2871
2872/*
2873 * Join the two lists l1 and l2, returning the combination.
2874 */
2875 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002876append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877{
2878 Ptrlist *oldl1;
2879
2880 oldl1 = l1;
2881 while (l1->next)
2882 l1 = l1->next;
2883 l1->next = l2;
2884 return oldl1;
2885}
2886
2887/*
2888 * Stack used for transforming postfix form into NFA.
2889 */
2890static Frag_T empty;
2891
2892 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002893st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002894{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002895#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 FILE *df;
2897 int *p2;
2898
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002899 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900 if (df)
2901 {
2902 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002903# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002905# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002907# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002908 for (p2 = postfix; p2 < end; p2++)
2909 {
2910 nfa_set_code(*p2);
2911 fprintf(df, "%s, ", code);
2912 }
2913 nfa_set_code(*p);
2914 fprintf(df, "\nCurrent position is: ");
2915 for (p2 = postfix; p2 <= p; p2 ++)
2916 {
2917 nfa_set_code(*p2);
2918 fprintf(df, "%s, ", code);
2919 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002920# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 for (p2 = postfix; p2 < end; p2++)
2922 {
2923 fprintf(df, "%d, ", *p2);
2924 }
2925 fprintf(df, "\nCurrent position is: ");
2926 for (p2 = postfix; p2 <= p; p2 ++)
2927 {
2928 fprintf(df, "%d, ", *p2);
2929 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002930# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931 fprintf(df, "\n--------------------------\n");
2932 fclose(df);
2933 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002934#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002935 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936}
2937
2938/*
2939 * Push an item onto the stack.
2940 */
2941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002942st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943{
2944 Frag_T *stackp = *p;
2945
2946 if (stackp >= stack_end)
2947 return;
2948 *stackp = s;
2949 *p = *p + 1;
2950}
2951
2952/*
2953 * Pop an item from the stack.
2954 */
2955 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002956st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002957{
2958 Frag_T *stackp;
2959
2960 *p = *p - 1;
2961 stackp = *p;
2962 if (stackp < stack)
2963 return empty;
2964 return **p;
2965}
2966
2967/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002968 * Estimate the maximum byte length of anything matching "state".
2969 * When unknown or unlimited return -1.
2970 */
2971 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002972nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002973{
2974 int l, r;
2975 nfa_state_T *state = startstate;
2976 int len = 0;
2977
2978 /* detect looping in a NFA_SPLIT */
2979 if (depth > 4)
2980 return -1;
2981
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002982 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002983 {
2984 switch (state->c)
2985 {
2986 case NFA_END_INVISIBLE:
2987 case NFA_END_INVISIBLE_NEG:
2988 /* the end, return what we have */
2989 return len;
2990
2991 case NFA_SPLIT:
2992 /* two alternatives, use the maximum */
2993 l = nfa_max_width(state->out, depth + 1);
2994 r = nfa_max_width(state->out1, depth + 1);
2995 if (l < 0 || r < 0)
2996 return -1;
2997 return len + (l > r ? l : r);
2998
2999 case NFA_ANY:
3000 case NFA_START_COLL:
3001 case NFA_START_NEG_COLL:
3002 /* matches some character, including composing chars */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003003 if (enc_utf8)
3004 len += MB_MAXBYTES;
3005 else if (has_mbyte)
3006 len += 2;
3007 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003008 ++len;
3009 if (state->c != NFA_ANY)
3010 {
3011 /* skip over the characters */
3012 state = state->out1->out;
3013 continue;
3014 }
3015 break;
3016
3017 case NFA_DIGIT:
3018 case NFA_WHITE:
3019 case NFA_HEX:
3020 case NFA_OCTAL:
3021 /* ascii */
3022 ++len;
3023 break;
3024
3025 case NFA_IDENT:
3026 case NFA_SIDENT:
3027 case NFA_KWORD:
3028 case NFA_SKWORD:
3029 case NFA_FNAME:
3030 case NFA_SFNAME:
3031 case NFA_PRINT:
3032 case NFA_SPRINT:
3033 case NFA_NWHITE:
3034 case NFA_NDIGIT:
3035 case NFA_NHEX:
3036 case NFA_NOCTAL:
3037 case NFA_WORD:
3038 case NFA_NWORD:
3039 case NFA_HEAD:
3040 case NFA_NHEAD:
3041 case NFA_ALPHA:
3042 case NFA_NALPHA:
3043 case NFA_LOWER:
3044 case NFA_NLOWER:
3045 case NFA_UPPER:
3046 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003047 case NFA_LOWER_IC:
3048 case NFA_NLOWER_IC:
3049 case NFA_UPPER_IC:
3050 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003051 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003052 /* possibly non-ascii */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003053 if (has_mbyte)
3054 len += 3;
3055 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003056 ++len;
3057 break;
3058
3059 case NFA_START_INVISIBLE:
3060 case NFA_START_INVISIBLE_NEG:
3061 case NFA_START_INVISIBLE_BEFORE:
3062 case NFA_START_INVISIBLE_BEFORE_NEG:
3063 /* zero-width, out1 points to the END state */
3064 state = state->out1->out;
3065 continue;
3066
3067 case NFA_BACKREF1:
3068 case NFA_BACKREF2:
3069 case NFA_BACKREF3:
3070 case NFA_BACKREF4:
3071 case NFA_BACKREF5:
3072 case NFA_BACKREF6:
3073 case NFA_BACKREF7:
3074 case NFA_BACKREF8:
3075 case NFA_BACKREF9:
3076#ifdef FEAT_SYN_HL
3077 case NFA_ZREF1:
3078 case NFA_ZREF2:
3079 case NFA_ZREF3:
3080 case NFA_ZREF4:
3081 case NFA_ZREF5:
3082 case NFA_ZREF6:
3083 case NFA_ZREF7:
3084 case NFA_ZREF8:
3085 case NFA_ZREF9:
3086#endif
3087 case NFA_NEWL:
3088 case NFA_SKIP:
3089 /* unknown width */
3090 return -1;
3091
3092 case NFA_BOL:
3093 case NFA_EOL:
3094 case NFA_BOF:
3095 case NFA_EOF:
3096 case NFA_BOW:
3097 case NFA_EOW:
3098 case NFA_MOPEN:
3099 case NFA_MOPEN1:
3100 case NFA_MOPEN2:
3101 case NFA_MOPEN3:
3102 case NFA_MOPEN4:
3103 case NFA_MOPEN5:
3104 case NFA_MOPEN6:
3105 case NFA_MOPEN7:
3106 case NFA_MOPEN8:
3107 case NFA_MOPEN9:
3108#ifdef FEAT_SYN_HL
3109 case NFA_ZOPEN:
3110 case NFA_ZOPEN1:
3111 case NFA_ZOPEN2:
3112 case NFA_ZOPEN3:
3113 case NFA_ZOPEN4:
3114 case NFA_ZOPEN5:
3115 case NFA_ZOPEN6:
3116 case NFA_ZOPEN7:
3117 case NFA_ZOPEN8:
3118 case NFA_ZOPEN9:
3119 case NFA_ZCLOSE:
3120 case NFA_ZCLOSE1:
3121 case NFA_ZCLOSE2:
3122 case NFA_ZCLOSE3:
3123 case NFA_ZCLOSE4:
3124 case NFA_ZCLOSE5:
3125 case NFA_ZCLOSE6:
3126 case NFA_ZCLOSE7:
3127 case NFA_ZCLOSE8:
3128 case NFA_ZCLOSE9:
3129#endif
3130 case NFA_MCLOSE:
3131 case NFA_MCLOSE1:
3132 case NFA_MCLOSE2:
3133 case NFA_MCLOSE3:
3134 case NFA_MCLOSE4:
3135 case NFA_MCLOSE5:
3136 case NFA_MCLOSE6:
3137 case NFA_MCLOSE7:
3138 case NFA_MCLOSE8:
3139 case NFA_MCLOSE9:
3140 case NFA_NOPEN:
3141 case NFA_NCLOSE:
3142
3143 case NFA_LNUM_GT:
3144 case NFA_LNUM_LT:
3145 case NFA_COL_GT:
3146 case NFA_COL_LT:
3147 case NFA_VCOL_GT:
3148 case NFA_VCOL_LT:
3149 case NFA_MARK_GT:
3150 case NFA_MARK_LT:
3151 case NFA_VISUAL:
3152 case NFA_LNUM:
3153 case NFA_CURSOR:
3154 case NFA_COL:
3155 case NFA_VCOL:
3156 case NFA_MARK:
3157
3158 case NFA_ZSTART:
3159 case NFA_ZEND:
3160 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003161 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003162 case NFA_START_PATTERN:
3163 case NFA_END_PATTERN:
3164 case NFA_COMPOSING:
3165 case NFA_END_COMPOSING:
3166 /* zero-width */
3167 break;
3168
3169 default:
3170 if (state->c < 0)
3171 /* don't know what this is */
3172 return -1;
3173 /* normal character */
3174 len += MB_CHAR2LEN(state->c);
3175 break;
3176 }
3177
3178 /* normal way to continue */
3179 state = state->out;
3180 }
3181
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003182 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003183 return -1;
3184}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003185
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003186/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003187 * Convert a postfix form into its equivalent NFA.
3188 * Return the NFA start state on success, NULL otherwise.
3189 */
3190 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003191post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003192{
3193 int *p;
3194 int mopen;
3195 int mclose;
3196 Frag_T *stack = NULL;
3197 Frag_T *stackp = NULL;
3198 Frag_T *stack_end = NULL;
3199 Frag_T e1;
3200 Frag_T e2;
3201 Frag_T e;
3202 nfa_state_T *s;
3203 nfa_state_T *s1;
3204 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003205 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206
3207 if (postfix == NULL)
3208 return NULL;
3209
Bram Moolenaar053bb602013-05-20 13:55:21 +02003210#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211#define POP() st_pop(&stackp, stack); \
3212 if (stackp < stack) \
3213 { \
3214 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003215 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216 return NULL; \
3217 }
3218
3219 if (nfa_calc_size == FALSE)
3220 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003221 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003222 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003223 if (stack == NULL)
3224 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003226 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003227 }
3228
3229 for (p = postfix; p < end; ++p)
3230 {
3231 switch (*p)
3232 {
3233 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003234 /* Concatenation.
3235 * Pay attention: this operator does not exist in the r.e. itself
3236 * (it is implicit, really). It is added when r.e. is translated
3237 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003238 if (nfa_calc_size == TRUE)
3239 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003240 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241 break;
3242 }
3243 e2 = POP();
3244 e1 = POP();
3245 patch(e1.out, e2.start);
3246 PUSH(frag(e1.start, e2.out));
3247 break;
3248
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 case NFA_OR:
3250 /* Alternation */
3251 if (nfa_calc_size == TRUE)
3252 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003253 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 break;
3255 }
3256 e2 = POP();
3257 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003258 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003260 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 PUSH(frag(s, append(e1.out, e2.out)));
3262 break;
3263
3264 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003265 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 if (nfa_calc_size == TRUE)
3267 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003268 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 break;
3270 }
3271 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003272 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003274 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 patch(e.out, s);
3276 PUSH(frag(s, list1(&s->out1)));
3277 break;
3278
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003279 case NFA_STAR_NONGREEDY:
3280 /* Zero or more, prefer zero */
3281 if (nfa_calc_size == TRUE)
3282 {
3283 nstate++;
3284 break;
3285 }
3286 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003287 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003288 if (s == NULL)
3289 goto theend;
3290 patch(e.out, s);
3291 PUSH(frag(s, list1(&s->out)));
3292 break;
3293
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 case NFA_QUEST:
3295 /* one or zero atoms=> greedy match */
3296 if (nfa_calc_size == TRUE)
3297 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003298 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300 }
3301 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003302 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003303 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003304 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 PUSH(frag(s, append(e.out, list1(&s->out1))));
3306 break;
3307
3308 case NFA_QUEST_NONGREEDY:
3309 /* zero or one atoms => non-greedy match */
3310 if (nfa_calc_size == TRUE)
3311 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003312 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 break;
3314 }
3315 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003316 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003317 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003318 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 PUSH(frag(s, append(e.out, list1(&s->out))));
3320 break;
3321
Bram Moolenaar417bad22013-06-07 14:08:30 +02003322 case NFA_END_COLL:
3323 case NFA_END_NEG_COLL:
3324 /* On the stack is the sequence starting with NFA_START_COLL or
3325 * NFA_START_NEG_COLL and all possible characters. Patch it to
3326 * add the output to the start. */
3327 if (nfa_calc_size == TRUE)
3328 {
3329 nstate++;
3330 break;
3331 }
3332 e = POP();
3333 s = alloc_state(NFA_END_COLL, NULL, NULL);
3334 if (s == NULL)
3335 goto theend;
3336 patch(e.out, s);
3337 e.start->out1 = s;
3338 PUSH(frag(e.start, list1(&s->out)));
3339 break;
3340
3341 case NFA_RANGE:
3342 /* Before this are two characters, the low and high end of a
3343 * range. Turn them into two states with MIN and MAX. */
3344 if (nfa_calc_size == TRUE)
3345 {
3346 /* nstate += 0; */
3347 break;
3348 }
3349 e2 = POP();
3350 e1 = POP();
3351 e2.start->val = e2.start->c;
3352 e2.start->c = NFA_RANGE_MAX;
3353 e1.start->val = e1.start->c;
3354 e1.start->c = NFA_RANGE_MIN;
3355 patch(e1.out, e2.start);
3356 PUSH(frag(e1.start, e2.out));
3357 break;
3358
Bram Moolenaar699c1202013-09-25 16:41:54 +02003359 case NFA_EMPTY:
3360 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361 if (nfa_calc_size == TRUE)
3362 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003363 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 break;
3365 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003366 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003368 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 PUSH(frag(s, list1(&s->out)));
3370 break;
3371
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003372 case NFA_OPT_CHARS:
3373 {
3374 int n;
3375
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003376 /* \%[abc] implemented as:
3377 * NFA_SPLIT
3378 * +-CHAR(a)
3379 * | +-NFA_SPLIT
3380 * | +-CHAR(b)
3381 * | | +-NFA_SPLIT
3382 * | | +-CHAR(c)
3383 * | | | +-next
3384 * | | +- next
3385 * | +- next
3386 * +- next
3387 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003388 n = *++p; /* get number of characters */
3389 if (nfa_calc_size == TRUE)
3390 {
3391 nstate += n;
3392 break;
3393 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003394 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003395 e1.out = NULL; /* stores list with out1's */
3396 s1 = NULL; /* previous NFA_SPLIT to connect to */
3397 while (n-- > 0)
3398 {
3399 e = POP(); /* get character */
3400 s = alloc_state(NFA_SPLIT, e.start, NULL);
3401 if (s == NULL)
3402 goto theend;
3403 if (e1.out == NULL)
3404 e1 = e;
3405 patch(e.out, s1);
3406 append(e1.out, list1(&s->out1));
3407 s1 = s;
3408 }
3409 PUSH(frag(s, e1.out));
3410 break;
3411 }
3412
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003414 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003415 case NFA_PREV_ATOM_JUST_BEFORE:
3416 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003417 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003418 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003419 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3420 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003421 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003422 int start_state;
3423 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003424 int n = 0;
3425 nfa_state_T *zend;
3426 nfa_state_T *skip;
3427
Bram Moolenaardecd9542013-06-07 16:31:50 +02003428 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003429 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003430 case NFA_PREV_ATOM_NO_WIDTH:
3431 start_state = NFA_START_INVISIBLE;
3432 end_state = NFA_END_INVISIBLE;
3433 break;
3434 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3435 start_state = NFA_START_INVISIBLE_NEG;
3436 end_state = NFA_END_INVISIBLE_NEG;
3437 break;
3438 case NFA_PREV_ATOM_JUST_BEFORE:
3439 start_state = NFA_START_INVISIBLE_BEFORE;
3440 end_state = NFA_END_INVISIBLE;
3441 break;
3442 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3443 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3444 end_state = NFA_END_INVISIBLE_NEG;
3445 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003446 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003447 start_state = NFA_START_PATTERN;
3448 end_state = NFA_END_PATTERN;
3449 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003450 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003451
3452 if (before)
3453 n = *++p; /* get the count */
3454
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003455 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003456 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003457 * The \@<= operator: match for the preceding atom.
3458 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003459 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003460 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461
3462 if (nfa_calc_size == TRUE)
3463 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003464 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 break;
3466 }
3467 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003468 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003470 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003473 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003474 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003475 if (pattern)
3476 {
3477 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3478 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003479 if (skip == NULL)
3480 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003481 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003482 if (zend == NULL)
3483 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003484 s1->out= skip;
3485 patch(e.out, zend);
3486 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003487 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003488 else
3489 {
3490 patch(e.out, s1);
3491 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003492 if (before)
3493 {
3494 if (n <= 0)
3495 /* See if we can guess the maximum width, it avoids a
3496 * lot of pointless tries. */
3497 n = nfa_max_width(e.start, 0);
3498 s->val = n; /* store the count */
3499 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003500 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003501 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003502 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003504 case NFA_COMPOSING: /* char with composing char */
3505#if 0
3506 /* TODO */
3507 if (regflags & RF_ICOMBINE)
3508 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003509 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003510 }
3511#endif
3512 /* FALLTHROUGH */
3513
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003514 case NFA_MOPEN: /* \( \) Submatch */
3515 case NFA_MOPEN1:
3516 case NFA_MOPEN2:
3517 case NFA_MOPEN3:
3518 case NFA_MOPEN4:
3519 case NFA_MOPEN5:
3520 case NFA_MOPEN6:
3521 case NFA_MOPEN7:
3522 case NFA_MOPEN8:
3523 case NFA_MOPEN9:
3524#ifdef FEAT_SYN_HL
3525 case NFA_ZOPEN: /* \z( \) Submatch */
3526 case NFA_ZOPEN1:
3527 case NFA_ZOPEN2:
3528 case NFA_ZOPEN3:
3529 case NFA_ZOPEN4:
3530 case NFA_ZOPEN5:
3531 case NFA_ZOPEN6:
3532 case NFA_ZOPEN7:
3533 case NFA_ZOPEN8:
3534 case NFA_ZOPEN9:
3535#endif
3536 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003537 if (nfa_calc_size == TRUE)
3538 {
3539 nstate += 2;
3540 break;
3541 }
3542
3543 mopen = *p;
3544 switch (*p)
3545 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003546 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3547#ifdef FEAT_SYN_HL
3548 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3549 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3550 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3551 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3552 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3553 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3554 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3555 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3556 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3557 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3558#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003559 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003560 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003561 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003562 mclose = *p + NSUBEXP;
3563 break;
3564 }
3565
3566 /* Allow "NFA_MOPEN" as a valid postfix representation for
3567 * the empty regexp "". In this case, the NFA will be
3568 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3569 * empty groups of parenthesis, and empty mbyte chars */
3570 if (stackp == stack)
3571 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003572 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003573 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003574 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003575 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003576 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003577 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 patch(list1(&s->out), s1);
3579 PUSH(frag(s, list1(&s1->out)));
3580 break;
3581 }
3582
3583 /* At least one node was emitted before NFA_MOPEN, so
3584 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3585 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003586 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003587 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003588 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589
Bram Moolenaar525666f2013-06-02 16:40:55 +02003590 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003591 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003592 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003593 patch(e.out, s1);
3594
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003595 if (mopen == NFA_COMPOSING)
3596 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 patch(list1(&s->out1), s1);
3598
3599 PUSH(frag(s, list1(&s1->out)));
3600 break;
3601
Bram Moolenaar5714b802013-05-28 22:03:20 +02003602 case NFA_BACKREF1:
3603 case NFA_BACKREF2:
3604 case NFA_BACKREF3:
3605 case NFA_BACKREF4:
3606 case NFA_BACKREF5:
3607 case NFA_BACKREF6:
3608 case NFA_BACKREF7:
3609 case NFA_BACKREF8:
3610 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003611#ifdef FEAT_SYN_HL
3612 case NFA_ZREF1:
3613 case NFA_ZREF2:
3614 case NFA_ZREF3:
3615 case NFA_ZREF4:
3616 case NFA_ZREF5:
3617 case NFA_ZREF6:
3618 case NFA_ZREF7:
3619 case NFA_ZREF8:
3620 case NFA_ZREF9:
3621#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003622 if (nfa_calc_size == TRUE)
3623 {
3624 nstate += 2;
3625 break;
3626 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003627 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003628 if (s == NULL)
3629 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003630 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003631 if (s1 == NULL)
3632 goto theend;
3633 patch(list1(&s->out), s1);
3634 PUSH(frag(s, list1(&s1->out)));
3635 break;
3636
Bram Moolenaar423532e2013-05-29 21:14:42 +02003637 case NFA_LNUM:
3638 case NFA_LNUM_GT:
3639 case NFA_LNUM_LT:
3640 case NFA_VCOL:
3641 case NFA_VCOL_GT:
3642 case NFA_VCOL_LT:
3643 case NFA_COL:
3644 case NFA_COL_GT:
3645 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003646 case NFA_MARK:
3647 case NFA_MARK_GT:
3648 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003649 {
3650 int n = *++p; /* lnum, col or mark name */
3651
Bram Moolenaar423532e2013-05-29 21:14:42 +02003652 if (nfa_calc_size == TRUE)
3653 {
3654 nstate += 1;
3655 break;
3656 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003657 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003658 if (s == NULL)
3659 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003660 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003661 PUSH(frag(s, list1(&s->out)));
3662 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003663 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003664
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665 case NFA_ZSTART:
3666 case NFA_ZEND:
3667 default:
3668 /* Operands */
3669 if (nfa_calc_size == TRUE)
3670 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003671 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 break;
3673 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003674 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003676 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677 PUSH(frag(s, list1(&s->out)));
3678 break;
3679
3680 } /* switch(*p) */
3681
3682 } /* for(p = postfix; *p; ++p) */
3683
3684 if (nfa_calc_size == TRUE)
3685 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003686 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003687 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 }
3689
3690 e = POP();
3691 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003692 {
3693 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694 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 +02003695 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696
3697 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003698 {
3699 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003701 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003702
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 matchstate = &state_ptr[istate++]; /* the match state */
3704 matchstate->c = NFA_MATCH;
3705 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003706 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003707
3708 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003709 ret = e.start;
3710
3711theend:
3712 vim_free(stack);
3713 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003714
3715#undef POP1
3716#undef PUSH1
3717#undef POP2
3718#undef PUSH2
3719#undef POP
3720#undef PUSH
3721}
3722
Bram Moolenaara2947e22013-06-11 22:44:09 +02003723/*
3724 * After building the NFA program, inspect it to add optimization hints.
3725 */
3726 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003727nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003728{
3729 int i;
3730 int c;
3731
3732 for (i = 0; i < prog->nstate; ++i)
3733 {
3734 c = prog->state[i].c;
3735 if (c == NFA_START_INVISIBLE
3736 || c == NFA_START_INVISIBLE_NEG
3737 || c == NFA_START_INVISIBLE_BEFORE
3738 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3739 {
3740 int directly;
3741
3742 /* Do it directly when what follows is possibly the end of the
3743 * match. */
3744 if (match_follows(prog->state[i].out1->out, 0))
3745 directly = TRUE;
3746 else
3747 {
3748 int ch_invisible = failure_chance(prog->state[i].out, 0);
3749 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3750
3751 /* Postpone when the invisible match is expensive or has a
3752 * lower chance of failing. */
3753 if (c == NFA_START_INVISIBLE_BEFORE
3754 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3755 {
3756 /* "before" matches are very expensive when
3757 * unbounded, always prefer what follows then,
3758 * unless what follows will always match.
3759 * Otherwise strongly prefer what follows. */
3760 if (prog->state[i].val <= 0 && ch_follows > 0)
3761 directly = FALSE;
3762 else
3763 directly = ch_follows * 10 < ch_invisible;
3764 }
3765 else
3766 {
3767 /* normal invisible, first do the one with the
3768 * highest failure chance */
3769 directly = ch_follows < ch_invisible;
3770 }
3771 }
3772 if (directly)
3773 /* switch to the _FIRST state */
3774 ++prog->state[i].c;
3775 }
3776 }
3777}
3778
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779/****************************************************************
3780 * NFA execution code.
3781 ****************************************************************/
3782
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003783typedef struct
3784{
3785 int in_use; /* number of subexpr with useful info */
3786
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003787 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003788 union
3789 {
3790 struct multipos
3791 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003792 linenr_T start_lnum;
3793 linenr_T end_lnum;
3794 colnr_T start_col;
3795 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003796 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003797 struct linepos
3798 {
3799 char_u *start;
3800 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003801 } line[NSUBEXP];
3802 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003803} regsub_T;
3804
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003805typedef struct
3806{
3807 regsub_T norm; /* \( .. \) matches */
3808#ifdef FEAT_SYN_HL
3809 regsub_T synt; /* \z( .. \) matches */
3810#endif
3811} regsubs_T;
3812
Bram Moolenaara2d95102013-06-04 14:23:05 +02003813/* nfa_pim_T stores a Postponed Invisible Match. */
3814typedef struct nfa_pim_S nfa_pim_T;
3815struct nfa_pim_S
3816{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003817 int result; /* NFA_PIM_*, see below */
3818 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003819 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003820 union
3821 {
3822 lpos_T pos;
3823 char_u *ptr;
3824 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003825};
3826
3827/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003828#define NFA_PIM_UNUSED 0 /* pim not used */
3829#define NFA_PIM_TODO 1 /* pim not done yet */
3830#define NFA_PIM_MATCH 2 /* pim executed, matches */
3831#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003832
3833
Bram Moolenaar963fee22013-05-26 21:47:28 +02003834/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003835typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836{
3837 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003838 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003839 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3840 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003841 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003842} nfa_thread_T;
3843
Bram Moolenaar963fee22013-05-26 21:47:28 +02003844/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003845typedef struct
3846{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003847 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003848 int n; /* nr of states currently in "t" */
3849 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003850 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003851 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003852} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003853
Bram Moolenaar5714b802013-05-28 22:03:20 +02003854#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003855static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003856
3857 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003858log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003859{
3860 log_subexpr(&subs->norm);
3861# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003862 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003863 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003864# endif
3865}
3866
Bram Moolenaar5714b802013-05-28 22:03:20 +02003867 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003868log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003869{
3870 int j;
3871
3872 for (j = 0; j < sub->in_use; j++)
3873 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003874 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003875 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003876 sub->list.multi[j].start_col,
3877 (int)sub->list.multi[j].start_lnum,
3878 sub->list.multi[j].end_col,
3879 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003880 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003881 {
3882 char *s = (char *)sub->list.line[j].start;
3883 char *e = (char *)sub->list.line[j].end;
3884
Bram Moolenaar87953742013-06-05 18:52:40 +02003885 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003886 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003887 s == NULL ? "NULL" : s,
3888 e == NULL ? "NULL" : e);
3889 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003890}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003891
3892 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003893pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003894{
3895 static char buf[30];
3896
3897 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3898 buf[0] = NUL;
3899 else
3900 {
3901 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003902 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003903 }
3904 return buf;
3905}
3906
Bram Moolenaar5714b802013-05-28 22:03:20 +02003907#endif
3908
Bram Moolenaar963fee22013-05-26 21:47:28 +02003909/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003910static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003911#ifdef FEAT_RELTIME
3912static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003913static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003914static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003915#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003916
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003917static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003918static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003919
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003920/*
3921 * Copy postponed invisible match info from "from" to "to".
3922 */
3923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003924copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003925{
3926 to->result = from->result;
3927 to->state = from->state;
3928 copy_sub(&to->subs.norm, &from->subs.norm);
3929#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003930 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003931 copy_sub(&to->subs.synt, &from->subs.synt);
3932#endif
3933 to->end = from->end;
3934}
3935
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003937clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003938{
3939 if (REG_MULTI)
3940 /* Use 0xff to set lnum to -1 */
3941 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003942 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003943 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003944 vim_memset(sub->list.line, 0,
3945 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003946 sub->in_use = 0;
3947}
3948
3949/*
3950 * Copy the submatches from "from" to "to".
3951 */
3952 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003953copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003954{
3955 to->in_use = from->in_use;
3956 if (from->in_use > 0)
3957 {
3958 /* Copy the match start and end positions. */
3959 if (REG_MULTI)
3960 mch_memmove(&to->list.multi[0],
3961 &from->list.multi[0],
3962 sizeof(struct multipos) * from->in_use);
3963 else
3964 mch_memmove(&to->list.line[0],
3965 &from->list.line[0],
3966 sizeof(struct linepos) * from->in_use);
3967 }
3968}
3969
3970/*
3971 * Like copy_sub() but exclude the main match.
3972 */
3973 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003974copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003975{
3976 if (to->in_use < from->in_use)
3977 to->in_use = from->in_use;
3978 if (from->in_use > 1)
3979 {
3980 /* Copy the match start and end positions. */
3981 if (REG_MULTI)
3982 mch_memmove(&to->list.multi[1],
3983 &from->list.multi[1],
3984 sizeof(struct multipos) * (from->in_use - 1));
3985 else
3986 mch_memmove(&to->list.line[1],
3987 &from->list.line[1],
3988 sizeof(struct linepos) * (from->in_use - 1));
3989 }
3990}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003991
Bram Moolenaar428e9872013-05-30 17:05:39 +02003992/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003993 * Like copy_sub() but only do the end of the main match if \ze is present.
3994 */
3995 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003996copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003997{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003998 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003999 {
4000 if (REG_MULTI)
4001 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004002 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004003 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004004 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4005 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004006 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004007 }
4008 else
4009 {
4010 if (from->list.line[0].end != NULL)
4011 to->list.line[0].end = from->list.line[0].end;
4012 }
4013 }
4014}
4015
4016/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004017 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004018 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004019 */
4020 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004021sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004022{
4023 int i;
4024 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004025 linenr_T s1;
4026 linenr_T s2;
4027 char_u *sp1;
4028 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004029
4030 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4031 if (REG_MULTI)
4032 {
4033 for (i = 0; i < todo; ++i)
4034 {
4035 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004036 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004038 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004039 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004040 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004041 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004042 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004043 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004044 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004045 if (s1 != -1 && sub1->list.multi[i].start_col
4046 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004047 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004048
Bram Moolenaar0270f382018-07-17 05:43:58 +02004049 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004050 {
4051 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004052 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004053 else
4054 s1 = -1;
4055 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004056 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004057 else
4058 s2 = -1;
4059 if (s1 != s2)
4060 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004061 if (s1 != -1 && sub1->list.multi[i].end_col
4062 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004063 return FALSE;
4064 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004065 }
4066 }
4067 else
4068 {
4069 for (i = 0; i < todo; ++i)
4070 {
4071 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004072 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004073 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004075 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004076 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004077 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004078 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004079 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004080 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004081 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004082 {
4083 if (i < sub1->in_use)
4084 sp1 = sub1->list.line[i].end;
4085 else
4086 sp1 = NULL;
4087 if (i < sub2->in_use)
4088 sp2 = sub2->list.line[i].end;
4089 else
4090 sp2 = NULL;
4091 if (sp1 != sp2)
4092 return FALSE;
4093 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004094 }
4095 }
4096
4097 return TRUE;
4098}
4099
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004100#ifdef ENABLE_LOG
4101 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004102report_state(char *action,
4103 regsub_T *sub,
4104 nfa_state_T *state,
4105 int lid,
4106 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004107{
4108 int col;
4109
4110 if (sub->in_use <= 0)
4111 col = -1;
4112 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004113 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004114 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004115 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004116 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004117 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4118 action, abs(state->id), lid, state->c, code, col,
4119 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004120}
4121#endif
4122
Bram Moolenaar43e02982013-06-07 17:31:29 +02004123/*
4124 * Return TRUE if the same state is already in list "l" with the same
4125 * positions as "subs".
4126 */
4127 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004128has_state_with_pos(
4129 nfa_list_T *l, /* runtime state list */
4130 nfa_state_T *state, /* state to update */
4131 regsubs_T *subs, /* pointers to subexpressions */
4132 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004133{
4134 nfa_thread_T *thread;
4135 int i;
4136
4137 for (i = 0; i < l->n; ++i)
4138 {
4139 thread = &l->t[i];
4140 if (thread->state->id == state->id
4141 && sub_equal(&thread->subs.norm, &subs->norm)
4142#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004143 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004144 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004145#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004146 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004147 return TRUE;
4148 }
4149 return FALSE;
4150}
4151
4152/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004153 * Return TRUE if "one" and "two" are equal. That includes when both are not
4154 * set.
4155 */
4156 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004157pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004158{
4159 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4160 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4161
4162 if (one_unused)
4163 /* one is unused: equal when two is also unused */
4164 return two_unused;
4165 if (two_unused)
4166 /* one is used and two is not: not equal */
4167 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004168 /* compare the state id */
4169 if (one->state->id != two->state->id)
4170 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004171 /* compare the position */
4172 if (REG_MULTI)
4173 return one->end.pos.lnum == two->end.pos.lnum
4174 && one->end.pos.col == two->end.pos.col;
4175 return one->end.ptr == two->end.ptr;
4176}
4177
4178/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004179 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4180 */
4181 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004182match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004183{
4184 nfa_state_T *state = startstate;
4185
4186 /* avoid too much recursion */
4187 if (depth > 10)
4188 return FALSE;
4189
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004190 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004191 {
4192 switch (state->c)
4193 {
4194 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004195 case NFA_MCLOSE:
4196 case NFA_END_INVISIBLE:
4197 case NFA_END_INVISIBLE_NEG:
4198 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004199 return TRUE;
4200
4201 case NFA_SPLIT:
4202 return match_follows(state->out, depth + 1)
4203 || match_follows(state->out1, depth + 1);
4204
4205 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004206 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004207 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004208 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004209 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004210 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004211 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004212 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004213 case NFA_COMPOSING:
4214 /* skip ahead to next state */
4215 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004216 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004217
4218 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004219 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004220 case NFA_IDENT:
4221 case NFA_SIDENT:
4222 case NFA_KWORD:
4223 case NFA_SKWORD:
4224 case NFA_FNAME:
4225 case NFA_SFNAME:
4226 case NFA_PRINT:
4227 case NFA_SPRINT:
4228 case NFA_WHITE:
4229 case NFA_NWHITE:
4230 case NFA_DIGIT:
4231 case NFA_NDIGIT:
4232 case NFA_HEX:
4233 case NFA_NHEX:
4234 case NFA_OCTAL:
4235 case NFA_NOCTAL:
4236 case NFA_WORD:
4237 case NFA_NWORD:
4238 case NFA_HEAD:
4239 case NFA_NHEAD:
4240 case NFA_ALPHA:
4241 case NFA_NALPHA:
4242 case NFA_LOWER:
4243 case NFA_NLOWER:
4244 case NFA_UPPER:
4245 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004246 case NFA_LOWER_IC:
4247 case NFA_NLOWER_IC:
4248 case NFA_UPPER_IC:
4249 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004250 case NFA_START_COLL:
4251 case NFA_START_NEG_COLL:
4252 case NFA_NEWL:
4253 /* state will advance input */
4254 return FALSE;
4255
4256 default:
4257 if (state->c > 0)
4258 /* state will advance input */
4259 return FALSE;
4260
4261 /* Others: zero-width or possibly zero-width, might still find
4262 * a match at the same position, keep looking. */
4263 break;
4264 }
4265 state = state->out;
4266 }
4267 return FALSE;
4268}
4269
4270
4271/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004272 * Return TRUE if "state" is already in list "l".
4273 */
4274 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004275state_in_list(
4276 nfa_list_T *l, /* runtime state list */
4277 nfa_state_T *state, /* state to update */
4278 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004279{
4280 if (state->lastlist[nfa_ll_index] == l->id)
4281 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004282 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004283 return TRUE;
4284 }
4285 return FALSE;
4286}
4287
Bram Moolenaar16b35782016-09-09 20:29:50 +02004288/* Offset used for "off" by addstate_here(). */
4289#define ADDSTATE_HERE_OFFSET 10
4290
Bram Moolenaard05bf562013-06-30 23:24:08 +02004291/*
4292 * Add "state" and possibly what follows to state list ".".
4293 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004294 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004295 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004296 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004297addstate(
4298 nfa_list_T *l, /* runtime state list */
4299 nfa_state_T *state, /* state to update */
4300 regsubs_T *subs_arg, /* pointers to subexpressions */
4301 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004302 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004303{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004304 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004305 int off = off_arg;
4306 int add_here = FALSE;
4307 int listindex = 0;
4308 int k;
4309 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004310 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004311 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004312 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004313 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004314 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004315 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004316 regsubs_T *subs = subs_arg;
4317 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004318#ifdef ENABLE_LOG
4319 int did_print = FALSE;
4320#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004321 static int depth = 0;
4322
4323 // This function is called recursively. When the depth is too much we run
4324 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004325 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004326 {
4327 --depth;
4328 return NULL;
4329 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330
Bram Moolenaar16b35782016-09-09 20:29:50 +02004331 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4332 {
4333 add_here = TRUE;
4334 off = 0;
4335 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4336 }
4337
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004338 switch (state->c)
4339 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004340 case NFA_NCLOSE:
4341 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004342 case NFA_MCLOSE1:
4343 case NFA_MCLOSE2:
4344 case NFA_MCLOSE3:
4345 case NFA_MCLOSE4:
4346 case NFA_MCLOSE5:
4347 case NFA_MCLOSE6:
4348 case NFA_MCLOSE7:
4349 case NFA_MCLOSE8:
4350 case NFA_MCLOSE9:
4351#ifdef FEAT_SYN_HL
4352 case NFA_ZCLOSE:
4353 case NFA_ZCLOSE1:
4354 case NFA_ZCLOSE2:
4355 case NFA_ZCLOSE3:
4356 case NFA_ZCLOSE4:
4357 case NFA_ZCLOSE5:
4358 case NFA_ZCLOSE6:
4359 case NFA_ZCLOSE7:
4360 case NFA_ZCLOSE8:
4361 case NFA_ZCLOSE9:
4362#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004363 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004364 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004365 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004366 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004367 /* These nodes are not added themselves but their "out" and/or
4368 * "out1" may be added below. */
4369 break;
4370
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004371 case NFA_BOL:
4372 case NFA_BOF:
4373 /* "^" won't match past end-of-line, don't bother trying.
4374 * Except when at the end of the line, or when we are going to the
4375 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004376 if (rex.input > rex.line
4377 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004378 && (nfa_endp == NULL
4379 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004380 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004381 goto skip_add;
4382 /* FALLTHROUGH */
4383
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004384 case NFA_MOPEN1:
4385 case NFA_MOPEN2:
4386 case NFA_MOPEN3:
4387 case NFA_MOPEN4:
4388 case NFA_MOPEN5:
4389 case NFA_MOPEN6:
4390 case NFA_MOPEN7:
4391 case NFA_MOPEN8:
4392 case NFA_MOPEN9:
4393#ifdef FEAT_SYN_HL
4394 case NFA_ZOPEN:
4395 case NFA_ZOPEN1:
4396 case NFA_ZOPEN2:
4397 case NFA_ZOPEN3:
4398 case NFA_ZOPEN4:
4399 case NFA_ZOPEN5:
4400 case NFA_ZOPEN6:
4401 case NFA_ZOPEN7:
4402 case NFA_ZOPEN8:
4403 case NFA_ZOPEN9:
4404#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004405 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004406 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004407 /* These nodes need to be added so that we can bail out when it
4408 * was added to this list before at the same position to avoid an
4409 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004410
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004411 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004412 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004413 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004414 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004415 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004416 * when there is a PIM. For NFA_MATCH check the position,
4417 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004418 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004419 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004420 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004421 /* When called from addstate_here() do insert before
4422 * existing states. */
4423 if (add_here)
4424 {
4425 for (k = 0; k < l->n && k < listindex; ++k)
4426 if (l->t[k].state->id == state->id)
4427 {
4428 found = TRUE;
4429 break;
4430 }
4431 }
4432 if (!add_here || found)
4433 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004434skip_add:
4435#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004436 nfa_set_code(state->c);
4437 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4438 abs(state->id), l->id, state->c, code,
4439 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004440#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004441 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004442 return subs;
4443 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004444 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004445
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004446 /* Do not add the state again when it exists with the same
4447 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004448 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004449 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004450 }
4451
Bram Moolenaar688b3982019-02-13 21:47:36 +01004452 // When there are backreferences or PIMs the number of states may
4453 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004454 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004455 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004456 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004457 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004458 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004459
Bram Moolenaar688b3982019-02-13 21:47:36 +01004460 if ((long)(newsize >> 10) >= p_mmp)
4461 {
4462 emsg(_(e_maxmempat));
4463 --depth;
4464 return NULL;
4465 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004466 if (subs != &temp_subs)
4467 {
4468 /* "subs" may point into the current array, need to make a
4469 * copy before it becomes invalid. */
4470 copy_sub(&temp_subs.norm, &subs->norm);
4471#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004472 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004473 copy_sub(&temp_subs.synt, &subs->synt);
4474#endif
4475 subs = &temp_subs;
4476 }
4477
Bram Moolenaar688b3982019-02-13 21:47:36 +01004478 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004479 if (newt == NULL)
4480 {
4481 // out of memory
4482 --depth;
4483 return NULL;
4484 }
4485 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004486 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004487 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004488
4489 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004490 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004491 thread = &l->t[l->n++];
4492 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004493 if (pim == NULL)
4494 thread->pim.result = NFA_PIM_UNUSED;
4495 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004496 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004497 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004498 l->has_pim = TRUE;
4499 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004500 copy_sub(&thread->subs.norm, &subs->norm);
4501#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004502 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004503 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004504#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004505#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004506 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004507 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004508#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004509 }
4510
4511#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004512 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004513 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004514#endif
4515 switch (state->c)
4516 {
4517 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004518 break;
4519
4520 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004521 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004522 subs = addstate(l, state->out, subs, pim, off_arg);
4523 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004524 break;
4525
Bram Moolenaar699c1202013-09-25 16:41:54 +02004526 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004527 case NFA_NOPEN:
4528 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004529 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004530 break;
4531
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004532 case NFA_MOPEN:
4533 case NFA_MOPEN1:
4534 case NFA_MOPEN2:
4535 case NFA_MOPEN3:
4536 case NFA_MOPEN4:
4537 case NFA_MOPEN5:
4538 case NFA_MOPEN6:
4539 case NFA_MOPEN7:
4540 case NFA_MOPEN8:
4541 case NFA_MOPEN9:
4542#ifdef FEAT_SYN_HL
4543 case NFA_ZOPEN:
4544 case NFA_ZOPEN1:
4545 case NFA_ZOPEN2:
4546 case NFA_ZOPEN3:
4547 case NFA_ZOPEN4:
4548 case NFA_ZOPEN5:
4549 case NFA_ZOPEN6:
4550 case NFA_ZOPEN7:
4551 case NFA_ZOPEN8:
4552 case NFA_ZOPEN9:
4553#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004556 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004558 sub = &subs->norm;
4559 }
4560#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004561 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004562 {
4563 subidx = state->c - NFA_ZOPEN;
4564 sub = &subs->synt;
4565 }
4566#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004567 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004568 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004569 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004570 sub = &subs->norm;
4571 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004572
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004573 /* avoid compiler warnings */
4574 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004575 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004576
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004577 /* Set the position (with "off" added) in the subexpression. Save
4578 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004579 if (REG_MULTI)
4580 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004581 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004582 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004583 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004584 save_in_use = -1;
4585 }
4586 else
4587 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004588 save_in_use = sub->in_use;
4589 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004590 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004591 sub->list.multi[i].start_lnum = -1;
4592 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004593 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004594 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004595 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004596 if (off == -1)
4597 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004598 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004599 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004600 }
4601 else
4602 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004603 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004604 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004605 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004606 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004607 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004608 }
4609 else
4610 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004611 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004612 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004613 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004614 save_in_use = -1;
4615 }
4616 else
4617 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004618 save_in_use = sub->in_use;
4619 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004620 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004621 sub->list.line[i].start = NULL;
4622 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004623 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004624 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004626 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004627 }
4628
Bram Moolenaar16b35782016-09-09 20:29:50 +02004629 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004630 if (subs == NULL)
4631 break;
4632 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004633#ifdef FEAT_SYN_HL
4634 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4635 sub = &subs->synt;
4636 else
4637#endif
4638 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004640 if (save_in_use == -1)
4641 {
4642 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004643 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004644 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004645 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004646 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004648 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004649 break;
4650
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004651 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004652 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004653 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004654 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004655 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004656 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004657 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 break;
4659 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004660 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004661 case NFA_MCLOSE1:
4662 case NFA_MCLOSE2:
4663 case NFA_MCLOSE3:
4664 case NFA_MCLOSE4:
4665 case NFA_MCLOSE5:
4666 case NFA_MCLOSE6:
4667 case NFA_MCLOSE7:
4668 case NFA_MCLOSE8:
4669 case NFA_MCLOSE9:
4670#ifdef FEAT_SYN_HL
4671 case NFA_ZCLOSE:
4672 case NFA_ZCLOSE1:
4673 case NFA_ZCLOSE2:
4674 case NFA_ZCLOSE3:
4675 case NFA_ZCLOSE4:
4676 case NFA_ZCLOSE5:
4677 case NFA_ZCLOSE6:
4678 case NFA_ZCLOSE7:
4679 case NFA_ZCLOSE8:
4680 case NFA_ZCLOSE9:
4681#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004684 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004686 sub = &subs->norm;
4687 }
4688#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004689 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004690 {
4691 subidx = state->c - NFA_ZCLOSE;
4692 sub = &subs->synt;
4693 }
4694#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004695 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004696 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004697 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004698 sub = &subs->norm;
4699 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004700
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004701 /* We don't fill in gaps here, there must have been an MOPEN that
4702 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004703 save_in_use = sub->in_use;
4704 if (sub->in_use <= subidx)
4705 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004706 if (REG_MULTI)
4707 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004708 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004709 if (off == -1)
4710 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004711 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004712 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004713 }
4714 else
4715 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004716 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004717 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004718 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004719 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004720 /* avoid compiler warnings */
4721 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004722 }
4723 else
4724 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004725 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004726 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004727 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004728 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004729 }
4730
Bram Moolenaar16b35782016-09-09 20:29:50 +02004731 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004732 if (subs == NULL)
4733 break;
Bram Moolenaarebefd992013-08-14 14:18:40 +02004734 /* "subs" may have changed, need to set "sub" again */
4735#ifdef FEAT_SYN_HL
4736 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4737 sub = &subs->synt;
4738 else
4739#endif
4740 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004741
4742 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004743 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004744 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004745 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004746 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004747 break;
4748 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004749 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004750 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004751}
4752
4753/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004754 * Like addstate(), but the new state(s) are put at position "*ip".
4755 * Used for zero-width matches, next state to use is the added one.
4756 * This makes sure the order of states to be tried does not change, which
4757 * matters for alternatives.
4758 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004759 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004760addstate_here(
4761 nfa_list_T *l, /* runtime state list */
4762 nfa_state_T *state, /* state to update */
4763 regsubs_T *subs, /* pointers to subexpressions */
4764 nfa_pim_T *pim, /* postponed look-behind match */
4765 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004766{
4767 int tlen = l->n;
4768 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004769 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004770 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004771
Bram Moolenaar16b35782016-09-09 20:29:50 +02004772 /* First add the state(s) at the end, so that we know how many there are.
4773 * Pass the listidx as offset (avoids adding another argument to
4774 * addstate(). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004775 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4776 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004777 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004778
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004779 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004780 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004781 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004782
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004783 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004784 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004785 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004786 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004787 if (count == 1)
4788 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004789 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004790 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004791 }
4792 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004793 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004794 if (l->n + count - 1 >= l->len)
4795 {
4796 /* not enough space to move the new states, reallocate the list
4797 * and move the states to the right position */
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004798 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004799 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004800 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004801
Bram Moolenaar688b3982019-02-13 21:47:36 +01004802 if ((long)(newsize >> 10) >= p_mmp)
4803 {
4804 emsg(_(e_maxmempat));
4805 return NULL;
4806 }
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01004807 newl = (nfa_thread_T *)alloc((int)newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004808 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004809 return NULL;
4810 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004811 mch_memmove(&(newl[0]),
4812 &(l->t[0]),
4813 sizeof(nfa_thread_T) * listidx);
4814 mch_memmove(&(newl[listidx]),
4815 &(l->t[l->n - count]),
4816 sizeof(nfa_thread_T) * count);
4817 mch_memmove(&(newl[listidx + count]),
4818 &(l->t[listidx + 1]),
4819 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4820 vim_free(l->t);
4821 l->t = newl;
4822 }
4823 else
4824 {
4825 /* make space for new states, then move them from the
4826 * end to the current position */
4827 mch_memmove(&(l->t[listidx + count]),
4828 &(l->t[listidx + 1]),
4829 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4830 mch_memmove(&(l->t[listidx]),
4831 &(l->t[l->n - 1]),
4832 sizeof(nfa_thread_T) * count);
4833 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004834 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004835 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004836 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004837
4838 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004839}
4840
4841/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004842 * Check character class "class" against current character c.
4843 */
4844 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004845check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004846{
4847 switch (class)
4848 {
4849 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004850 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004851 return OK;
4852 break;
4853 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004854 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004855 return OK;
4856 break;
4857 case NFA_CLASS_BLANK:
4858 if (c == ' ' || c == '\t')
4859 return OK;
4860 break;
4861 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004862 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863 return OK;
4864 break;
4865 case NFA_CLASS_DIGIT:
4866 if (VIM_ISDIGIT(c))
4867 return OK;
4868 break;
4869 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004870 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004871 return OK;
4872 break;
4873 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004874 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004875 return OK;
4876 break;
4877 case NFA_CLASS_PRINT:
4878 if (vim_isprintc(c))
4879 return OK;
4880 break;
4881 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004882 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004883 return OK;
4884 break;
4885 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004886 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004887 return OK;
4888 break;
4889 case NFA_CLASS_UPPER:
4890 if (MB_ISUPPER(c))
4891 return OK;
4892 break;
4893 case NFA_CLASS_XDIGIT:
4894 if (vim_isxdigit(c))
4895 return OK;
4896 break;
4897 case NFA_CLASS_TAB:
4898 if (c == '\t')
4899 return OK;
4900 break;
4901 case NFA_CLASS_RETURN:
4902 if (c == '\r')
4903 return OK;
4904 break;
4905 case NFA_CLASS_BACKSPACE:
4906 if (c == '\b')
4907 return OK;
4908 break;
4909 case NFA_CLASS_ESCAPE:
4910 if (c == '\033')
4911 return OK;
4912 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004913 case NFA_CLASS_IDENT:
4914 if (vim_isIDc(c))
4915 return OK;
4916 break;
4917 case NFA_CLASS_KEYWORD:
4918 if (reg_iswordc(c))
4919 return OK;
4920 break;
4921 case NFA_CLASS_FNAME:
4922 if (vim_isfilec(c))
4923 return OK;
4924 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925
4926 default:
4927 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004928 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004929 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930 }
4931 return FAIL;
4932}
4933
Bram Moolenaar5714b802013-05-28 22:03:20 +02004934/*
4935 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004936 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004937 */
4938 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004939match_backref(
4940 regsub_T *sub, /* pointers to subexpressions */
4941 int subidx,
4942 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004943{
4944 int len;
4945
4946 if (sub->in_use <= subidx)
4947 {
4948retempty:
4949 /* backref was not set, match an empty string */
4950 *bytelen = 0;
4951 return TRUE;
4952 }
4953
4954 if (REG_MULTI)
4955 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004956 if (sub->list.multi[subidx].start_lnum < 0
4957 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004958 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004959 if (sub->list.multi[subidx].start_lnum == rex.lnum
4960 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004961 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004962 len = sub->list.multi[subidx].end_col
4963 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004964 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4965 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004966 {
4967 *bytelen = len;
4968 return TRUE;
4969 }
4970 }
4971 else
4972 {
4973 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004974 sub->list.multi[subidx].start_lnum,
4975 sub->list.multi[subidx].start_col,
4976 sub->list.multi[subidx].end_lnum,
4977 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004978 bytelen) == RA_MATCH)
4979 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004980 }
4981 }
4982 else
4983 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004984 if (sub->list.line[subidx].start == NULL
4985 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004986 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004987 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004988 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004989 {
4990 *bytelen = len;
4991 return TRUE;
4992 }
4993 }
4994 return FALSE;
4995}
4996
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004997#ifdef FEAT_SYN_HL
4998
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004999/*
5000 * Check for a match with \z subexpression "subidx".
5001 * Return TRUE if it matches.
5002 */
5003 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005004match_zref(
5005 int subidx,
5006 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005007{
5008 int len;
5009
5010 cleanup_zsubexpr();
5011 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5012 {
5013 /* backref was not set, match an empty string */
5014 *bytelen = 0;
5015 return TRUE;
5016 }
5017
5018 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005019 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005020 {
5021 *bytelen = len;
5022 return TRUE;
5023 }
5024 return FALSE;
5025}
5026#endif
5027
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005028/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005029 * Save list IDs for all NFA states of "prog" into "list".
5030 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005031 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005032 */
5033 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005034nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005035{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005036 int i;
5037 nfa_state_T *p;
5038
5039 /* Order in the list is reverse, it's a bit faster that way. */
5040 p = &prog->state[0];
5041 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005042 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005043 list[i] = p->lastlist[1];
5044 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005045 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005046 }
5047}
5048
5049/*
5050 * Restore list IDs from "list" to all NFA states.
5051 */
5052 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005053nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005054{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005055 int i;
5056 nfa_state_T *p;
5057
5058 p = &prog->state[0];
5059 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005060 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005061 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005062 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005063 }
5064}
5065
Bram Moolenaar423532e2013-05-29 21:14:42 +02005066 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005067nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005068{
5069 if (op == 1) return pos > val;
5070 if (op == 2) return pos < val;
5071 return val == pos;
5072}
5073
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005074static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005075
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005076/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005077 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005078 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5079 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005080 */
5081 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005082recursive_regmatch(
5083 nfa_state_T *state,
5084 nfa_pim_T *pim,
5085 nfa_regprog_T *prog,
5086 regsubs_T *submatch,
5087 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005088 int **listids,
5089 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005090{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005091 int save_reginput_col = (int)(rex.input - rex.line);
5092 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005093 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005094 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005095 save_se_T *save_nfa_endp = nfa_endp;
5096 save_se_T endpos;
5097 save_se_T *endposp = NULL;
5098 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005099 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005100
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005101 if (pim != NULL)
5102 {
5103 /* start at the position where the postponed match was */
5104 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005105 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005106 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005107 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005108 }
5109
Bram Moolenaardecd9542013-06-07 16:31:50 +02005110 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005111 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5112 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5113 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005115 /* The recursive match must end at the current position. When "pim" is
5116 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005117 endposp = &endpos;
5118 if (REG_MULTI)
5119 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005120 if (pim == NULL)
5121 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005122 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5123 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005124 }
5125 else
5126 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005127 }
5128 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005129 {
5130 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005131 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005132 else
5133 endpos.se_u.ptr = pim->end.ptr;
5134 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005135
5136 /* Go back the specified number of bytes, or as far as the
5137 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005138 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005139 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005140 if (state->val <= 0)
5141 {
5142 if (REG_MULTI)
5143 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005144 rex.line = reg_getline(--rex.lnum);
5145 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005146 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005147 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005148 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005149 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005150 }
5151 else
5152 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005153 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005154 {
5155 /* Not enough bytes in this line, go to end of
5156 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005157 rex.line = reg_getline(--rex.lnum);
5158 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005159 {
5160 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005161 rex.line = reg_getline(++rex.lnum);
5162 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005163 }
5164 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005165 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005166 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005167 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005168 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005169 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005170 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005171 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005172 }
5173 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005174 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005175 }
5176 }
5177
Bram Moolenaarf46da702013-06-02 22:37:42 +02005178#ifdef ENABLE_LOG
5179 if (log_fd != stderr)
5180 fclose(log_fd);
5181 log_fd = NULL;
5182#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005183 /* Have to clear the lastlist field of the NFA nodes, so that
5184 * nfa_regmatch() and addstate() can run properly after recursion. */
5185 if (nfa_ll_index == 1)
5186 {
5187 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5188 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005189 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005190 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005191 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005192 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005193 if (*listids == NULL)
5194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005195 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005196 return 0;
5197 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005198 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005199 }
5200 nfa_save_listids(prog, *listids);
5201 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005202 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005203 }
5204 else
5205 {
5206 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005207 * entry. Make sure rex.nfa_listid is different from a previous
5208 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005209 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005210 if (rex.nfa_listid <= rex.nfa_alt_listid)
5211 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005212 }
5213
5214 /* Call nfa_regmatch() to check if the current concat matches at this
5215 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005216 nfa_endp = endposp;
5217 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005218
5219 if (need_restore)
5220 nfa_restore_listids(prog, *listids);
5221 else
5222 {
5223 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005224 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005225 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005226
5227 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005228 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005229 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005230 rex.line = reg_getline(rex.lnum);
5231 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005232 if (result != NFA_TOO_EXPENSIVE)
5233 {
5234 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005235 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005236 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005237 nfa_endp = save_nfa_endp;
5238
5239#ifdef ENABLE_LOG
5240 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5241 if (log_fd != NULL)
5242 {
5243 fprintf(log_fd, "****************************\n");
5244 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5245 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5246 fprintf(log_fd, "****************************\n");
5247 }
5248 else
5249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005250 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005251 log_fd = stderr;
5252 }
5253#endif
5254
5255 return result;
5256}
5257
Bram Moolenaara2d95102013-06-04 14:23:05 +02005258/*
5259 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005260 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005261 * NFA_ANY: 1
5262 * specific character: 99
5263 */
5264 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005265failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005266{
5267 int c = state->c;
5268 int l, r;
5269
5270 /* detect looping */
5271 if (depth > 4)
5272 return 1;
5273
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005274 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005275 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005276 case NFA_SPLIT:
5277 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5278 /* avoid recursive stuff */
5279 return 1;
5280 /* two alternatives, use the lowest failure chance */
5281 l = failure_chance(state->out, depth + 1);
5282 r = failure_chance(state->out1, depth + 1);
5283 return l < r ? l : r;
5284
5285 case NFA_ANY:
5286 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005287 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005288
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005289 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005290 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005291 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005292 /* empty match works always */
5293 return 0;
5294
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005295 case NFA_START_INVISIBLE:
5296 case NFA_START_INVISIBLE_FIRST:
5297 case NFA_START_INVISIBLE_NEG:
5298 case NFA_START_INVISIBLE_NEG_FIRST:
5299 case NFA_START_INVISIBLE_BEFORE:
5300 case NFA_START_INVISIBLE_BEFORE_FIRST:
5301 case NFA_START_INVISIBLE_BEFORE_NEG:
5302 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5303 case NFA_START_PATTERN:
5304 /* recursive regmatch is expensive, use low failure chance */
5305 return 5;
5306
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005307 case NFA_BOL:
5308 case NFA_EOL:
5309 case NFA_BOF:
5310 case NFA_EOF:
5311 case NFA_NEWL:
5312 return 99;
5313
5314 case NFA_BOW:
5315 case NFA_EOW:
5316 return 90;
5317
5318 case NFA_MOPEN:
5319 case NFA_MOPEN1:
5320 case NFA_MOPEN2:
5321 case NFA_MOPEN3:
5322 case NFA_MOPEN4:
5323 case NFA_MOPEN5:
5324 case NFA_MOPEN6:
5325 case NFA_MOPEN7:
5326 case NFA_MOPEN8:
5327 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005328#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005329 case NFA_ZOPEN:
5330 case NFA_ZOPEN1:
5331 case NFA_ZOPEN2:
5332 case NFA_ZOPEN3:
5333 case NFA_ZOPEN4:
5334 case NFA_ZOPEN5:
5335 case NFA_ZOPEN6:
5336 case NFA_ZOPEN7:
5337 case NFA_ZOPEN8:
5338 case NFA_ZOPEN9:
5339 case NFA_ZCLOSE:
5340 case NFA_ZCLOSE1:
5341 case NFA_ZCLOSE2:
5342 case NFA_ZCLOSE3:
5343 case NFA_ZCLOSE4:
5344 case NFA_ZCLOSE5:
5345 case NFA_ZCLOSE6:
5346 case NFA_ZCLOSE7:
5347 case NFA_ZCLOSE8:
5348 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005349#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005350 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005351 case NFA_MCLOSE1:
5352 case NFA_MCLOSE2:
5353 case NFA_MCLOSE3:
5354 case NFA_MCLOSE4:
5355 case NFA_MCLOSE5:
5356 case NFA_MCLOSE6:
5357 case NFA_MCLOSE7:
5358 case NFA_MCLOSE8:
5359 case NFA_MCLOSE9:
5360 case NFA_NCLOSE:
5361 return failure_chance(state->out, depth + 1);
5362
5363 case NFA_BACKREF1:
5364 case NFA_BACKREF2:
5365 case NFA_BACKREF3:
5366 case NFA_BACKREF4:
5367 case NFA_BACKREF5:
5368 case NFA_BACKREF6:
5369 case NFA_BACKREF7:
5370 case NFA_BACKREF8:
5371 case NFA_BACKREF9:
5372#ifdef FEAT_SYN_HL
5373 case NFA_ZREF1:
5374 case NFA_ZREF2:
5375 case NFA_ZREF3:
5376 case NFA_ZREF4:
5377 case NFA_ZREF5:
5378 case NFA_ZREF6:
5379 case NFA_ZREF7:
5380 case NFA_ZREF8:
5381 case NFA_ZREF9:
5382#endif
5383 /* backreferences don't match in many places */
5384 return 94;
5385
5386 case NFA_LNUM_GT:
5387 case NFA_LNUM_LT:
5388 case NFA_COL_GT:
5389 case NFA_COL_LT:
5390 case NFA_VCOL_GT:
5391 case NFA_VCOL_LT:
5392 case NFA_MARK_GT:
5393 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005394 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005395 /* before/after positions don't match very often */
5396 return 85;
5397
5398 case NFA_LNUM:
5399 return 90;
5400
5401 case NFA_CURSOR:
5402 case NFA_COL:
5403 case NFA_VCOL:
5404 case NFA_MARK:
5405 /* specific positions rarely match */
5406 return 98;
5407
5408 case NFA_COMPOSING:
5409 return 95;
5410
5411 default:
5412 if (c > 0)
5413 /* character match fails often */
5414 return 95;
5415 }
5416
5417 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005418 return 50;
5419}
5420
Bram Moolenaarf46da702013-06-02 22:37:42 +02005421/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005422 * Skip until the char "c" we know a match must start with.
5423 */
5424 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005425skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005426{
5427 char_u *s;
5428
5429 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005430 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005431 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005432 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005433 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005434 if (s == NULL)
5435 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005436 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005437 return OK;
5438}
5439
5440/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005441 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005442 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005443 * Returns zero for no match, 1 for a match.
5444 */
5445 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005446find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005447{
5448 colnr_T col = startcol;
5449 int c1, c2;
5450 int len1, len2;
5451 int match;
5452
5453 for (;;)
5454 {
5455 match = TRUE;
5456 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5457 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5458 {
5459 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005460 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005461 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005462 {
5463 match = FALSE;
5464 break;
5465 }
5466 len2 += MB_CHAR2LEN(c2);
5467 }
5468 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005469 /* check that no composing char follows */
5470 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005471 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005472 {
5473 cleanup_subexpr();
5474 if (REG_MULTI)
5475 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005476 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005477 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005478 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005479 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005480 }
5481 else
5482 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005483 rex.reg_startp[0] = rex.line + col;
5484 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005485 }
5486 return 1L;
5487 }
5488
5489 /* Try finding regstart after the current match. */
5490 col += MB_CHAR2LEN(regstart); /* skip regstart */
5491 if (skip_to_start(regstart, &col) == FAIL)
5492 break;
5493 }
5494 return 0L;
5495}
5496
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005497#ifdef FEAT_RELTIME
5498 static int
5499nfa_did_time_out()
5500{
5501 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5502 {
5503 if (nfa_timed_out != NULL)
5504 *nfa_timed_out = TRUE;
5505 return TRUE;
5506 }
5507 return FALSE;
5508}
5509#endif
5510
Bram Moolenaar473de612013-06-08 18:19:48 +02005511/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 * Main matching routine.
5513 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005514 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005516 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005517 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005519 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 * Note: Caller must ensure that: start != NULL.
5521 */
5522 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005523nfa_regmatch(
5524 nfa_regprog_T *prog,
5525 nfa_state_T *start,
5526 regsubs_T *submatch,
5527 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005530 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005532 int go_to_nextline = FALSE;
5533 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005534 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005535 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005536 nfa_list_T *thislist;
5537 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005539 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005540 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005541 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005542 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005543 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005544 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005545 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005546#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005547 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005548#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005549
Bram Moolenaar41f12052013-08-25 17:01:42 +02005550 /* Some patterns may take a long time to match, especially when using
5551 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5552 fast_breakcheck();
5553 if (got_int)
5554 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005555#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005556 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005557 return FALSE;
5558#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005559
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005560#ifdef NFA_REGEXP_DEBUG_LOG
5561 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5562 if (debug == NULL)
5563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005564 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005565 return FALSE;
5566 }
5567#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005568 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005569
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005570 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005571 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005572
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005573 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005574 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005575 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005576 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005577 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005579
5580#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005581 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582 if (log_fd != NULL)
5583 {
5584 fprintf(log_fd, "**********************************\n");
5585 nfa_set_code(start->c);
5586 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5587 abs(start->id), code);
5588 fprintf(log_fd, "**********************************\n");
5589 }
5590 else
5591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005592 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005593 log_fd = stderr;
5594 }
5595#endif
5596
5597 thislist = &list[0];
5598 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005599 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005600 nextlist = &list[1];
5601 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005602 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005603#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005604 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005605#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005606 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005607
5608 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5609 * it's the first MOPEN. */
5610 if (toplevel)
5611 {
5612 if (REG_MULTI)
5613 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005614 m->norm.list.multi[0].start_lnum = rex.lnum;
5615 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005616 }
5617 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005618 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005619 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005620 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005621 }
5622 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005623 r = addstate(thislist, start, m, NULL, 0);
5624 if (r == NULL)
5625 {
5626 nfa_match = NFA_TOO_EXPENSIVE;
5627 goto theend;
5628 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005629
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005630#define ADD_STATE_IF_MATCH(state) \
5631 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005632 add_state = state->out; \
5633 add_off = clen; \
5634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005635
5636 /*
5637 * Run for each character.
5638 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005639 for (;;)
5640 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005641 int curc;
5642 int clen;
5643
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644 if (has_mbyte)
5645 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005646 curc = (*mb_ptr2char)(rex.input);
5647 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648 }
5649 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005651 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005652 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005654 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005655 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005656 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005657 go_to_nextline = FALSE;
5658 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005659
5660 /* swap lists */
5661 thislist = &list[flag];
5662 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005663 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005664 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005665 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005666 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005667 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005668# ifdef FEAT_EVAL
5669 || nfa_fail_for_testing
5670# endif
5671 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005672 {
5673 /* too many states, retry with old engine */
5674 nfa_match = NFA_TOO_EXPENSIVE;
5675 goto theend;
5676 }
5677
Bram Moolenaar0270f382018-07-17 05:43:58 +02005678 thislist->id = rex.nfa_listid;
5679 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680
5681#ifdef ENABLE_LOG
5682 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005683 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005684 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005685 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005686 {
5687 int i;
5688
5689 for (i = 0; i < thislist->n; i++)
5690 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5691 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005692 fprintf(log_fd, "\n");
5693#endif
5694
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005695#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696 fprintf(debug, "\n-------------------\n");
5697#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005698 /*
5699 * If the state lists are empty we can stop.
5700 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005701 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005702 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703
5704 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005705 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005706 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005707 /* If the list gets very long there probably is something wrong.
5708 * At least allow interrupting with CTRL-C. */
5709 fast_breakcheck();
5710 if (got_int)
5711 break;
5712#ifdef FEAT_RELTIME
5713 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5714 {
5715 nfa_time_count = 0;
5716 if (nfa_did_time_out())
5717 break;
5718 }
5719#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005720 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005722#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005723 nfa_set_code(t->state->c);
5724 fprintf(debug, "%s, ", code);
5725#endif
5726#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005727 {
5728 int col;
5729
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005730 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005731 col = -1;
5732 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005733 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005734 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005735 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005736 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005737 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005738 abs(t->state->id), (int)t->state->c, code, col,
5739 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005740 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741#endif
5742
5743 /*
5744 * Handle the possible codes of the current state.
5745 * The most important is NFA_MATCH.
5746 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005747 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005748 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005749 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005750 switch (t->state->c)
5751 {
5752 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005753 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005754 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005755 * rex.reg_icombine is not set, that is not really a match. */
5756 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005757 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005758
Bram Moolenaar963fee22013-05-26 21:47:28 +02005759 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005760 copy_sub(&submatch->norm, &t->subs.norm);
5761#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005762 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005763 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005764#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005765#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005766 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005767#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005768 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005769 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005770 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005771 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005772 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005773 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005774 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005775 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005776
5777 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005778 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005779 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005780 /*
5781 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005782 * NFA_START_INVISIBLE_BEFORE node.
5783 * They surround a zero-width group, used with "\@=", "\&",
5784 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005785 * If we got here, it means that the current "invisible" group
5786 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005787 * nfa_regmatch(). For a look-behind match only when it ends
5788 * in the position in "nfa_endp".
5789 * Submatches are stored in *m, and used in the parent call.
5790 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005791#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005792 if (nfa_endp != NULL)
5793 {
5794 if (REG_MULTI)
5795 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005796 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005797 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005798 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005799 nfa_endp->se_u.pos.col);
5800 else
5801 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005802 (int)(rex.input - rex.line),
5803 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005804 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005805#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005806 /* If "nfa_endp" is set it's only a match if it ends at
5807 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005808 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005809 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5810 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005811 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005812 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005813 break;
5814
5815 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005816 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005817 {
5818 copy_sub(&m->norm, &t->subs.norm);
5819#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005820 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005821 copy_sub(&m->synt, &t->subs.synt);
5822#endif
5823 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005824#ifdef ENABLE_LOG
5825 fprintf(log_fd, "Match found:\n");
5826 log_subsexpr(m);
5827#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005828 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005829 /* See comment above at "goto nextchar". */
5830 if (nextlist->n == 0)
5831 clen = 0;
5832 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005833
5834 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005835 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005836 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005837 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005838 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005839 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005840 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005841 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005842 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005843#ifdef ENABLE_LOG
5844 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5845 failure_chance(t->state->out, 0),
5846 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005847#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005848 /* Do it directly if there already is a PIM or when
5849 * nfa_postprocess() detected it will work better. */
5850 if (t->pim.result != NFA_PIM_UNUSED
5851 || t->state->c == NFA_START_INVISIBLE_FIRST
5852 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5853 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5854 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005855 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005856 int in_use = m->norm.in_use;
5857
Bram Moolenaar699c1202013-09-25 16:41:54 +02005858 /* Copy submatch info for the recursive call, opposite
5859 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005860 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005861#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005862 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005863 copy_sub_off(&m->synt, &t->subs.synt);
5864#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005865
Bram Moolenaara2d95102013-06-04 14:23:05 +02005866 /*
5867 * First try matching the invisible match, then what
5868 * follows.
5869 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005870 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005871 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005872 if (result == NFA_TOO_EXPENSIVE)
5873 {
5874 nfa_match = result;
5875 goto theend;
5876 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005877
Bram Moolenaardecd9542013-06-07 16:31:50 +02005878 /* for \@! and \@<! it is a match when the result is
5879 * FALSE */
5880 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005881 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5882 || t->state->c
5883 == NFA_START_INVISIBLE_BEFORE_NEG
5884 || t->state->c
5885 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005886 {
5887 /* Copy submatch info from the recursive call */
5888 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005889#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005890 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005891 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005892#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005893 /* If the pattern has \ze and it matched in the
5894 * sub pattern, use it. */
5895 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005896
Bram Moolenaara2d95102013-06-04 14:23:05 +02005897 /* t->state->out1 is the corresponding
5898 * END_INVISIBLE node; Add its out to the current
5899 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005900 add_here = TRUE;
5901 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005902 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005903 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005904 }
5905 else
5906 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005907 nfa_pim_T pim;
5908
Bram Moolenaara2d95102013-06-04 14:23:05 +02005909 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005910 * First try matching what follows. Only if a match
5911 * is found verify the invisible match matches. Add a
5912 * nfa_pim_T to the following states, it contains info
5913 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005914 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005915 pim.state = t->state;
5916 pim.result = NFA_PIM_TODO;
5917 pim.subs.norm.in_use = 0;
5918#ifdef FEAT_SYN_HL
5919 pim.subs.synt.in_use = 0;
5920#endif
5921 if (REG_MULTI)
5922 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005923 pim.end.pos.col = (int)(rex.input - rex.line);
5924 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005925 }
5926 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005927 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005928
5929 /* t->state->out1 is the corresponding END_INVISIBLE
5930 * node; Add its out to the current list (zero-width
5931 * match). */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005932 if (addstate_here(thislist, t->state->out1->out,
5933 &t->subs, &pim, &listidx) == NULL)
5934 {
5935 nfa_match = NFA_TOO_EXPENSIVE;
5936 goto theend;
5937 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005938 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005939 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005940 break;
5941
Bram Moolenaar87953742013-06-05 18:52:40 +02005942 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005943 {
5944 nfa_state_T *skip = NULL;
5945#ifdef ENABLE_LOG
5946 int skip_lid = 0;
5947#endif
5948
5949 /* There is no point in trying to match the pattern if the
5950 * output state is not going to be added to the list. */
5951 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5952 {
5953 skip = t->state->out1->out;
5954#ifdef ENABLE_LOG
5955 skip_lid = nextlist->id;
5956#endif
5957 }
5958 else if (state_in_list(nextlist,
5959 t->state->out1->out->out, &t->subs))
5960 {
5961 skip = t->state->out1->out->out;
5962#ifdef ENABLE_LOG
5963 skip_lid = nextlist->id;
5964#endif
5965 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005966 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005967 t->state->out1->out->out, &t->subs))
5968 {
5969 skip = t->state->out1->out->out;
5970#ifdef ENABLE_LOG
5971 skip_lid = thislist->id;
5972#endif
5973 }
5974 if (skip != NULL)
5975 {
5976#ifdef ENABLE_LOG
5977 nfa_set_code(skip->c);
5978 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5979 abs(skip->id), skip_lid, skip->c, code);
5980#endif
5981 break;
5982 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005983 /* Copy submatch info to the recursive call, opposite of what
5984 * happens afterwards. */
5985 copy_sub_off(&m->norm, &t->subs.norm);
5986#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005987 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005988 copy_sub_off(&m->synt, &t->subs.synt);
5989#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005990
Bram Moolenaar87953742013-06-05 18:52:40 +02005991 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005992 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005993 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005994 if (result == NFA_TOO_EXPENSIVE)
5995 {
5996 nfa_match = result;
5997 goto theend;
5998 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005999 if (result)
6000 {
6001 int bytelen;
6002
6003#ifdef ENABLE_LOG
6004 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6005 log_subsexpr(m);
6006#endif
6007 /* Copy submatch info from the recursive call */
6008 copy_sub_off(&t->subs.norm, &m->norm);
6009#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006010 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006011 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006012#endif
6013 /* Now we need to skip over the matched text and then
6014 * continue with what follows. */
6015 if (REG_MULTI)
6016 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006017 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006018 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006019 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006020 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006021
6022#ifdef ENABLE_LOG
6023 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6024#endif
6025 if (bytelen == 0)
6026 {
6027 /* empty match, output of corresponding
6028 * NFA_END_PATTERN/NFA_SKIP to be used at current
6029 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006030 add_here = TRUE;
6031 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006032 }
6033 else if (bytelen <= clen)
6034 {
6035 /* match current character, output of corresponding
6036 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006037 add_state = t->state->out1->out->out;
6038 add_off = clen;
6039 }
6040 else
6041 {
6042 /* skip over the matched characters, set character
6043 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006044 add_state = t->state->out1->out;
6045 add_off = bytelen;
6046 add_count = bytelen - clen;
6047 }
6048 }
6049 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006050 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006052 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006053 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006054 {
6055 add_here = TRUE;
6056 add_state = t->state->out;
6057 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006058 break;
6059
6060 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006061 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006062 {
6063 add_here = TRUE;
6064 add_state = t->state->out;
6065 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006066 break;
6067
6068 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006069 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006070
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006071 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006072 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006073 else if (has_mbyte)
6074 {
6075 int this_class;
6076
6077 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006078 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006079 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006080 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006081 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006082 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006083 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006084 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006085 || (rex.input > rex.line
6086 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006087 result = FALSE;
6088 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006089 {
6090 add_here = TRUE;
6091 add_state = t->state->out;
6092 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006094
6095 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006096 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006097 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006098 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006099 else if (has_mbyte)
6100 {
6101 int this_class, prev_class;
6102
6103 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006104 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006105 prev_class = reg_prev_class();
6106 if (this_class == prev_class
6107 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006108 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006109 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006110 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6111 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006112 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006113 result = FALSE;
6114 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006115 {
6116 add_here = TRUE;
6117 add_state = t->state->out;
6118 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006119 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006120
Bram Moolenaar4b780632013-05-31 22:14:52 +02006121 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006122 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006123 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006124 {
6125 add_here = TRUE;
6126 add_state = t->state->out;
6127 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006128 break;
6129
6130 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006131 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006132 {
6133 add_here = TRUE;
6134 add_state = t->state->out;
6135 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006136 break;
6137
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006138 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006139 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006140 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006141 int len = 0;
6142 nfa_state_T *end;
6143 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006144 int cchars[MAX_MCO];
6145 int ccount = 0;
6146 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006147
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006148 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006149 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006150 if (utf_iscomposing(sta->c))
6151 {
6152 /* Only match composing character(s), ignore base
6153 * character. Used for ".{composing}" and "{composing}"
6154 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006155 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006156 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006157 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006158 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006159 /* If \Z was present, then ignore composing characters.
6160 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006161 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006162 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006163 else
6164 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006165 while (sta->c != NFA_END_COMPOSING)
6166 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006167 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006168
6169 /* Check base character matches first, unless ignored. */
6170 else if (len > 0 || mc == sta->c)
6171 {
6172 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006173 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006174 len += mb_char2len(mc);
6175 sta = sta->out;
6176 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006177
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006178 /* We don't care about the order of composing characters.
6179 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006180 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006181 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006182 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006183 cchars[ccount++] = mc;
6184 len += mb_char2len(mc);
6185 if (ccount == MAX_MCO)
6186 break;
6187 }
6188
6189 /* Check that each composing char in the pattern matches a
6190 * composing char in the text. We do not check if all
6191 * composing chars are matched. */
6192 result = OK;
6193 while (sta->c != NFA_END_COMPOSING)
6194 {
6195 for (j = 0; j < ccount; ++j)
6196 if (cchars[j] == sta->c)
6197 break;
6198 if (j == ccount)
6199 {
6200 result = FAIL;
6201 break;
6202 }
6203 sta = sta->out;
6204 }
6205 }
6206 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006207 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006208
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006209 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006210 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006211 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006212 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006213
6214 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006215 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006216 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006217 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006218 go_to_nextline = TRUE;
6219 /* Pass -1 for the offset, which means taking the position
6220 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006221 add_state = t->state->out;
6222 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006223 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006224 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006225 {
6226 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006227 add_state = t->state->out;
6228 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006229 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006230 break;
6231
Bram Moolenaar417bad22013-06-07 14:08:30 +02006232 case NFA_START_COLL:
6233 case NFA_START_NEG_COLL:
6234 {
6235 /* What follows is a list of characters, until NFA_END_COLL.
6236 * One of them must match or none of them must match. */
6237 nfa_state_T *state;
6238 int result_if_matched;
6239 int c1, c2;
6240
6241 /* Never match EOL. If it's part of the collection it is added
6242 * as a separate state with an OR. */
6243 if (curc == NUL)
6244 break;
6245
6246 state = t->state->out;
6247 result_if_matched = (t->state->c == NFA_START_COLL);
6248 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006249 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006250 if (state->c == NFA_END_COLL)
6251 {
6252 result = !result_if_matched;
6253 break;
6254 }
6255 if (state->c == NFA_RANGE_MIN)
6256 {
6257 c1 = state->val;
6258 state = state->out; /* advance to NFA_RANGE_MAX */
6259 c2 = state->val;
6260#ifdef ENABLE_LOG
6261 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6262 curc, c1, c2);
6263#endif
6264 if (curc >= c1 && curc <= c2)
6265 {
6266 result = result_if_matched;
6267 break;
6268 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006269 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006270 {
6271 int curc_low = MB_TOLOWER(curc);
6272 int done = FALSE;
6273
6274 for ( ; c1 <= c2; ++c1)
6275 if (MB_TOLOWER(c1) == curc_low)
6276 {
6277 result = result_if_matched;
6278 done = TRUE;
6279 break;
6280 }
6281 if (done)
6282 break;
6283 }
6284 }
6285 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006286 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006287 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006288 == MB_TOLOWER(state->c))))
6289 {
6290 result = result_if_matched;
6291 break;
6292 }
6293 state = state->out;
6294 }
6295 if (result)
6296 {
6297 /* next state is in out of the NFA_END_COLL, out1 of
6298 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006299 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006300 add_off = clen;
6301 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006302 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006303 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006304
6305 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006306 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006307 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006308 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006309 add_state = t->state->out;
6310 add_off = clen;
6311 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006312 break;
6313
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006314 case NFA_ANY_COMPOSING:
6315 /* On a composing character skip over it. Otherwise do
6316 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006317 if (enc_utf8 && utf_iscomposing(curc))
6318 {
6319 add_off = clen;
6320 }
6321 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006322 {
6323 add_here = TRUE;
6324 add_off = 0;
6325 }
6326 add_state = t->state->out;
6327 break;
6328
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329 /*
6330 * Character classes like \a for alpha, \d for digit etc.
6331 */
6332 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006333 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006334 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006335 break;
6336
6337 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006338 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006339 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006340 break;
6341
6342 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006343 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006344 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006345 break;
6346
6347 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006348 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006349 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006350 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006351 break;
6352
6353 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006354 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006355 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006356 break;
6357
6358 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006359 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006360 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006361 break;
6362
6363 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006364 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006365 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 break;
6367
6368 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006369 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006370 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006371 break;
6372
6373 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006374 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006375 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006376 break;
6377
6378 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006379 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006380 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 break;
6382
6383 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006384 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006385 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 break;
6387
6388 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006389 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006390 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391 break;
6392
6393 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006394 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006395 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396 break;
6397
6398 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006399 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006400 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 break;
6402
6403 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006404 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006405 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006406 break;
6407
6408 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006409 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006410 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006411 break;
6412
6413 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006415 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 break;
6417
6418 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006420 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421 break;
6422
6423 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006425 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006426 break;
6427
6428 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006430 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431 break;
6432
6433 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006435 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006436 break;
6437
6438 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006439 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006440 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006441 break;
6442
6443 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006444 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006445 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006446 break;
6447
6448 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006449 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006450 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006451 break;
6452
6453 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006454 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006455 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006456 break;
6457
6458 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006459 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006460 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006461 break;
6462
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006463 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006464 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006465 ADD_STATE_IF_MATCH(t->state);
6466 break;
6467
6468 case NFA_NLOWER_IC: /* [^a-z] */
6469 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006470 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006471 ADD_STATE_IF_MATCH(t->state);
6472 break;
6473
6474 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006475 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006476 ADD_STATE_IF_MATCH(t->state);
6477 break;
6478
6479 case NFA_NUPPER_IC: /* ^[A-Z] */
6480 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006481 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006482 ADD_STATE_IF_MATCH(t->state);
6483 break;
6484
Bram Moolenaar5714b802013-05-28 22:03:20 +02006485 case NFA_BACKREF1:
6486 case NFA_BACKREF2:
6487 case NFA_BACKREF3:
6488 case NFA_BACKREF4:
6489 case NFA_BACKREF5:
6490 case NFA_BACKREF6:
6491 case NFA_BACKREF7:
6492 case NFA_BACKREF8:
6493 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006494#ifdef FEAT_SYN_HL
6495 case NFA_ZREF1:
6496 case NFA_ZREF2:
6497 case NFA_ZREF3:
6498 case NFA_ZREF4:
6499 case NFA_ZREF5:
6500 case NFA_ZREF6:
6501 case NFA_ZREF7:
6502 case NFA_ZREF8:
6503 case NFA_ZREF9:
6504#endif
6505 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006506 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006507 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006508 int bytelen;
6509
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006510 if (t->state->c <= NFA_BACKREF9)
6511 {
6512 subidx = t->state->c - NFA_BACKREF1 + 1;
6513 result = match_backref(&t->subs.norm, subidx, &bytelen);
6514 }
6515#ifdef FEAT_SYN_HL
6516 else
6517 {
6518 subidx = t->state->c - NFA_ZREF1 + 1;
6519 result = match_zref(subidx, &bytelen);
6520 }
6521#endif
6522
Bram Moolenaar5714b802013-05-28 22:03:20 +02006523 if (result)
6524 {
6525 if (bytelen == 0)
6526 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006527 /* empty match always works, output of NFA_SKIP to be
6528 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006529 add_here = TRUE;
6530 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006531 }
6532 else if (bytelen <= clen)
6533 {
6534 /* match current character, jump ahead to out of
6535 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006536 add_state = t->state->out->out;
6537 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006538 }
6539 else
6540 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006541 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006542 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006543 add_state = t->state->out;
6544 add_off = bytelen;
6545 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006546 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006547 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006548 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006549 }
6550 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006551 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006552 if (t->count - clen <= 0)
6553 {
6554 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006555 add_state = t->state->out;
6556 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006557 }
6558 else
6559 {
6560 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006561 add_state = t->state;
6562 add_off = 0;
6563 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006564 }
6565 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006566
Bram Moolenaar423532e2013-05-29 21:14:42 +02006567 case NFA_LNUM:
6568 case NFA_LNUM_GT:
6569 case NFA_LNUM_LT:
6570 result = (REG_MULTI &&
6571 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006572 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006573 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006574 {
6575 add_here = TRUE;
6576 add_state = t->state->out;
6577 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006578 break;
6579
6580 case NFA_COL:
6581 case NFA_COL_GT:
6582 case NFA_COL_LT:
6583 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006584 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006585 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006586 {
6587 add_here = TRUE;
6588 add_state = t->state->out;
6589 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006590 break;
6591
6592 case NFA_VCOL:
6593 case NFA_VCOL_GT:
6594 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006595 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006596 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006597 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006598 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006599
6600 /* Bail out quickly when there can't be a match, avoid the
6601 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006602 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006603 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006604 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006605 result = FALSE;
6606 if (op == 1 && col - 1 > t->state->val && col > 100)
6607 {
6608 int ts = wp->w_buffer->b_p_ts;
6609
6610 /* Guess that a character won't use more columns than
6611 * 'tabstop', with a minimum of 4. */
6612 if (ts < 4)
6613 ts = 4;
6614 result = col > t->state->val * ts;
6615 }
6616 if (!result)
6617 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006618 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006619 if (result)
6620 {
6621 add_here = TRUE;
6622 add_state = t->state->out;
6623 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006624 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006625 break;
6626
Bram Moolenaar044aa292013-06-04 21:27:38 +02006627 case NFA_MARK:
6628 case NFA_MARK_GT:
6629 case NFA_MARK_LT:
6630 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006631 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006632
6633 /* Compare the mark position to the match position. */
6634 result = (pos != NULL /* mark doesn't exist */
6635 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006636 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6637 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006638 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006639 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006640 ? t->state->c == NFA_MARK_GT
6641 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006642 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006643 ? t->state->c == NFA_MARK_GT
6644 : t->state->c == NFA_MARK_LT)));
6645 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006646 {
6647 add_here = TRUE;
6648 add_state = t->state->out;
6649 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006650 break;
6651 }
6652
Bram Moolenaar423532e2013-05-29 21:14:42 +02006653 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006654 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006655 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006656 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006657 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006658 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006659 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006660 {
6661 add_here = TRUE;
6662 add_state = t->state->out;
6663 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006664 break;
6665
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006666 case NFA_VISUAL:
6667 result = reg_match_visual();
6668 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006669 {
6670 add_here = TRUE;
6671 add_state = t->state->out;
6672 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006673 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006674
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006675 case NFA_MOPEN1:
6676 case NFA_MOPEN2:
6677 case NFA_MOPEN3:
6678 case NFA_MOPEN4:
6679 case NFA_MOPEN5:
6680 case NFA_MOPEN6:
6681 case NFA_MOPEN7:
6682 case NFA_MOPEN8:
6683 case NFA_MOPEN9:
6684#ifdef FEAT_SYN_HL
6685 case NFA_ZOPEN:
6686 case NFA_ZOPEN1:
6687 case NFA_ZOPEN2:
6688 case NFA_ZOPEN3:
6689 case NFA_ZOPEN4:
6690 case NFA_ZOPEN5:
6691 case NFA_ZOPEN6:
6692 case NFA_ZOPEN7:
6693 case NFA_ZOPEN8:
6694 case NFA_ZOPEN9:
6695#endif
6696 case NFA_NOPEN:
6697 case NFA_ZSTART:
6698 /* These states are only added to be able to bail out when
6699 * they are added again, nothing is to be done. */
6700 break;
6701
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006702 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006703 {
6704 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006705
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006706#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006707 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006708 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006709#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006710 result = (c == curc);
6711
Bram Moolenaar6100d022016-10-02 16:51:57 +02006712 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006713 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006714 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006715 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006716 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006717 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006718 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006719 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006720 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006721
6722 } /* switch (t->state->c) */
6723
6724 if (add_state != NULL)
6725 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006726 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006727 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006728
6729 if (t->pim.result == NFA_PIM_UNUSED)
6730 pim = NULL;
6731 else
6732 pim = &t->pim;
6733
6734 /* Handle the postponed invisible match if the match might end
6735 * without advancing and before the end of the line. */
6736 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006737 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006738 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006739 {
6740#ifdef ENABLE_LOG
6741 fprintf(log_fd, "\n");
6742 fprintf(log_fd, "==================================\n");
6743 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6744 fprintf(log_fd, "\n");
6745#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006746 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006747 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006748 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006749 /* for \@! and \@<! it is a match when the result is
6750 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006751 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006752 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6753 || pim->state->c
6754 == NFA_START_INVISIBLE_BEFORE_NEG
6755 || pim->state->c
6756 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757 {
6758 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006759 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006760#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006761 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006762 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006763#endif
6764 }
6765 }
6766 else
6767 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006768 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006769#ifdef ENABLE_LOG
6770 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006771 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6773 fprintf(log_fd, "\n");
6774#endif
6775 }
6776
Bram Moolenaardecd9542013-06-07 16:31:50 +02006777 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006778 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006779 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6780 || pim->state->c
6781 == NFA_START_INVISIBLE_BEFORE_NEG
6782 || pim->state->c
6783 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006784 {
6785 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006786 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006787#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006788 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006789 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006790#endif
6791 }
6792 else
6793 /* look-behind match failed, don't add the state */
6794 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006795
6796 /* Postponed invisible match was handled, don't add it to
6797 * following states. */
6798 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006799 }
6800
Bram Moolenaara951e352013-10-06 15:46:11 +02006801 /* If "pim" points into l->t it will become invalid when
6802 * adding the state causes the list to be reallocated. Make a
6803 * local copy to avoid that. */
6804 if (pim == &t->pim)
6805 {
6806 copy_pim(&pim_copy, pim);
6807 pim = &pim_copy;
6808 }
6809
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006810 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006811 r = addstate_here(thislist, add_state, &t->subs,
6812 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006813 else
6814 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006815 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006816 if (add_count > 0)
6817 nextlist->t[nextlist->n - 1].count = add_count;
6818 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006819 if (r == NULL)
6820 {
6821 nfa_match = NFA_TOO_EXPENSIVE;
6822 goto theend;
6823 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006824 }
6825
6826 } /* for (thislist = thislist; thislist->state; thislist++) */
6827
Bram Moolenaare23febd2013-05-26 18:40:14 +02006828 /* Look for the start of a match in the current position by adding the
6829 * start state to the list of states.
6830 * The first found match is the leftmost one, thus the order of states
6831 * matters!
6832 * Do not add the start state in recursive calls of nfa_regmatch(),
6833 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006834 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006835 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006836 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006837 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006838 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006839 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006840 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006841 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006842 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006843 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006844 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6845 || (rex.lnum == nfa_endp->se_u.pos.lnum
6846 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006847 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006848 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006849 {
6850#ifdef ENABLE_LOG
6851 fprintf(log_fd, "(---) STARTSTATE\n");
6852#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006853 /* Inline optimized code for addstate() if we know the state is
6854 * the first MOPEN. */
6855 if (toplevel)
6856 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006857 int add = TRUE;
6858 int c;
6859
6860 if (prog->regstart != NUL && clen != 0)
6861 {
6862 if (nextlist->n == 0)
6863 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006864 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006865
6866 /* Nextlist is empty, we can skip ahead to the
6867 * character that must appear at the start. */
6868 if (skip_to_start(prog->regstart, &col) == FAIL)
6869 break;
6870#ifdef ENABLE_LOG
6871 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006872 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006873#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006874 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006875 }
6876 else
6877 {
6878 /* Checking if the required start character matches is
6879 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006880 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006881 if (c != prog->regstart && (!rex.reg_ic
6882 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006883 {
6884#ifdef ENABLE_LOG
6885 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6886#endif
6887 add = FALSE;
6888 }
6889 }
6890 }
6891
6892 if (add)
6893 {
6894 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006895 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006896 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006897 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006898 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006899 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6900 {
6901 nfa_match = NFA_TOO_EXPENSIVE;
6902 goto theend;
6903 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006904 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006905 }
6906 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006907 {
6908 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6909 {
6910 nfa_match = NFA_TOO_EXPENSIVE;
6911 goto theend;
6912 }
6913 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006914 }
6915
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006916#ifdef ENABLE_LOG
6917 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006918 {
6919 int i;
6920
6921 for (i = 0; i < thislist->n; i++)
6922 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6923 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006924 fprintf(log_fd, "\n");
6925#endif
6926
6927nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006928 /* Advance to the next character, or advance to the next line, or
6929 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006930 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006931 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006932 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006933 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006934 reg_nextline();
6935 else
6936 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006937
6938 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006939 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006940 if (got_int)
6941 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006942#ifdef FEAT_RELTIME
6943 /* Check for timeout once in a twenty times to avoid overhead. */
6944 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6945 {
6946 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006947 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006948 break;
6949 }
6950#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006951 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006952
6953#ifdef ENABLE_LOG
6954 if (log_fd != stderr)
6955 fclose(log_fd);
6956 log_fd = NULL;
6957#endif
6958
6959theend:
6960 /* Free memory */
6961 vim_free(list[0].t);
6962 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006963 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006964#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006965#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006966 fclose(debug);
6967#endif
6968
Bram Moolenaar963fee22013-05-26 21:47:28 +02006969 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006970}
6971
6972/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006973 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006974 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006975 */
6976 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006977nfa_regtry(
6978 nfa_regprog_T *prog,
6979 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006980 proftime_T *tm UNUSED, /* timeout limit or NULL */
6981 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006982{
6983 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006984 regsubs_T subs, m;
6985 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006986 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006987#ifdef ENABLE_LOG
6988 FILE *f;
6989#endif
6990
Bram Moolenaar0270f382018-07-17 05:43:58 +02006991 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006992#ifdef FEAT_RELTIME
6993 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006994 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006995 nfa_time_count = 0;
6996#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997
6998#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006999 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000 if (f != NULL)
7001 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007002 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007003#ifdef DEBUG
7004 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7005#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007006 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007007 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007008 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007009 fprintf(f, "\n\n");
7010 fclose(f);
7011 }
7012 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007013 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014#endif
7015
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007016 clear_sub(&subs.norm);
7017 clear_sub(&m.norm);
7018#ifdef FEAT_SYN_HL
7019 clear_sub(&subs.synt);
7020 clear_sub(&m.synt);
7021#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022
Bram Moolenaarfda37292014-11-05 14:27:36 +01007023 result = nfa_regmatch(prog, start, &subs, &m);
7024 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007025 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007026 else if (result == NFA_TOO_EXPENSIVE)
7027 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028
7029 cleanup_subexpr();
7030 if (REG_MULTI)
7031 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007032 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007034 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7035 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007036
Bram Moolenaar6100d022016-10-02 16:51:57 +02007037 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7038 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007039 }
7040
Bram Moolenaar6100d022016-10-02 16:51:57 +02007041 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007043 rex.reg_startpos[0].lnum = 0;
7044 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007046 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007047 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007048 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007049 rex.reg_endpos[0].lnum = rex.lnum;
7050 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007051 }
7052 else
7053 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02007054 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007055 }
7056 else
7057 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007058 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007059 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007060 rex.reg_startp[i] = subs.norm.list.line[i].start;
7061 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007062 }
7063
Bram Moolenaar6100d022016-10-02 16:51:57 +02007064 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007065 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007066 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007067 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007068 }
7069
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007070#ifdef FEAT_SYN_HL
7071 /* Package any found \z(...\) matches for export. Default is none. */
7072 unref_extmatch(re_extmatch_out);
7073 re_extmatch_out = NULL;
7074
7075 if (prog->reghasz == REX_SET)
7076 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007077 cleanup_zsubexpr();
7078 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007079 /* Loop over \z1, \z2, etc. There is no \z0. */
7080 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007081 {
7082 if (REG_MULTI)
7083 {
7084 struct multipos *mpos = &subs.synt.list.multi[i];
7085
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007086 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007087 if (mpos->start_lnum >= 0
7088 && mpos->start_lnum == mpos->end_lnum
7089 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007090 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007091 vim_strnsave(reg_getline(mpos->start_lnum)
7092 + mpos->start_col,
7093 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007094 }
7095 else
7096 {
7097 struct linepos *lpos = &subs.synt.list.line[i];
7098
7099 if (lpos->start != NULL && lpos->end != NULL)
7100 re_extmatch_out->matches[i] =
7101 vim_strnsave(lpos->start,
7102 (int)(lpos->end - lpos->start));
7103 }
7104 }
7105 }
7106#endif
7107
Bram Moolenaar0270f382018-07-17 05:43:58 +02007108 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007109}
7110
7111/*
7112 * Match a regexp against a string ("line" points to the string) or multiple
7113 * lines ("line" is NULL, use reg_getline()).
7114 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007115 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007116 */
7117 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007118nfa_regexec_both(
7119 char_u *line,
7120 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007121 proftime_T *tm, /* timeout limit or NULL */
7122 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007123{
7124 nfa_regprog_T *prog;
7125 long retval = 0L;
7126 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007127 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007128
7129 if (REG_MULTI)
7130 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007131 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007133 rex.reg_startpos = rex.reg_mmatch->startpos;
7134 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007135 }
7136 else
7137 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007138 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7139 rex.reg_startp = rex.reg_match->startp;
7140 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007141 }
7142
7143 /* Be paranoid... */
7144 if (prog == NULL || line == NULL)
7145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007146 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007147 goto theend;
7148 }
7149
Bram Moolenaar6100d022016-10-02 16:51:57 +02007150 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007152 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007153 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007154 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007155
Bram Moolenaar6100d022016-10-02 16:51:57 +02007156 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007157 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007158 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007159
Bram Moolenaar0270f382018-07-17 05:43:58 +02007160 rex.line = line;
7161 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007162
Bram Moolenaar0270f382018-07-17 05:43:58 +02007163 rex.nfa_has_zend = prog->has_zend;
7164 rex.nfa_has_backref = prog->has_backref;
7165 rex.nfa_nsubexpr = prog->nsubexp;
7166 rex.nfa_listid = 1;
7167 rex.nfa_alt_listid = 2;
7168#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007169 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007170#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007171
Bram Moolenaard89616e2013-06-06 18:46:06 +02007172 if (prog->reganch && col > 0)
7173 return 0L;
7174
Bram Moolenaar0270f382018-07-17 05:43:58 +02007175 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007176#ifdef FEAT_SYN_HL
7177 /* Clear the external match subpointers if necessary. */
7178 if (prog->reghasz == REX_SET)
7179 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007180 rex.nfa_has_zsubexpr = TRUE;
7181 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007182 }
7183 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007184 {
7185 rex.nfa_has_zsubexpr = FALSE;
7186 rex.need_clear_zsubexpr = FALSE;
7187 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007188#endif
7189
Bram Moolenaard89616e2013-06-06 18:46:06 +02007190 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007191 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007192 /* Skip ahead until a character we know the match must start with.
7193 * When there is none there is no match. */
7194 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007195 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007196
Bram Moolenaar473de612013-06-08 18:19:48 +02007197 /* If match_text is set it contains the full text that must match.
7198 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007199 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007200 return find_match_text(col, prog->regstart, prog->match_text);
7201 }
7202
Bram Moolenaard89616e2013-06-06 18:46:06 +02007203 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007204 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007205 goto theend;
7206
Bram Moolenaar0270f382018-07-17 05:43:58 +02007207 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7208 // it's accidentally used during execution.
7209 nstate = 0;
7210 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007211 {
7212 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007213 prog->state[i].lastlist[0] = 0;
7214 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007215 }
7216
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007217 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007218
Bram Moolenaar0270f382018-07-17 05:43:58 +02007219#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007220 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007221#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007222
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007223theend:
7224 return retval;
7225}
7226
7227/*
7228 * Compile a regular expression into internal code for the NFA matcher.
7229 * Returns the program in allocated space. Returns NULL for an error.
7230 */
7231 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007232nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007233{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007234 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007235 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007236 int *postfix;
7237
7238 if (expr == NULL)
7239 return NULL;
7240
Bram Moolenaar0270f382018-07-17 05:43:58 +02007241#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007242 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007243#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007244 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007245
7246 init_class_tab();
7247
7248 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7249 return NULL;
7250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007251 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007252 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007253 postfix = re2post();
7254 if (postfix == NULL)
7255 goto fail; /* Cascaded (syntax?) error */
7256
7257 /*
7258 * In order to build the NFA, we parse the input regexp twice:
7259 * 1. first pass to count size (so we can allocate space)
7260 * 2. second to emit code
7261 */
7262#ifdef ENABLE_LOG
7263 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007264 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007265
7266 if (f != NULL)
7267 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007268 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007269 fclose(f);
7270 }
7271 }
7272#endif
7273
7274 /*
7275 * PASS 1
7276 * Count number of NFA states in "nstate". Do not build the NFA.
7277 */
7278 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007279
Bram Moolenaar16619a22013-06-11 18:42:36 +02007280 /* allocate the regprog with space for the compiled regexp */
7281 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007282 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7283 if (prog == NULL)
7284 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007285 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007286 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007287
7288 /*
7289 * PASS 2
7290 * Build the NFA
7291 */
7292 prog->start = post2nfa(postfix, post_ptr, FALSE);
7293 if (prog->start == NULL)
7294 goto fail;
7295
7296 prog->regflags = regflags;
7297 prog->engine = &nfa_regengine;
7298 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007299 prog->has_zend = rex.nfa_has_zend;
7300 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007301 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007302
Bram Moolenaara2947e22013-06-11 22:44:09 +02007303 nfa_postprocess(prog);
7304
Bram Moolenaard89616e2013-06-06 18:46:06 +02007305 prog->reganch = nfa_get_reganch(prog->start, 0);
7306 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007307 prog->match_text = nfa_get_match_text(prog->start);
7308
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007309#ifdef ENABLE_LOG
7310 nfa_postfix_dump(expr, OK);
7311 nfa_dump(prog);
7312#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007313#ifdef FEAT_SYN_HL
7314 /* Remember whether this pattern has any \z specials in it. */
7315 prog->reghasz = re_has_z;
7316#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007317 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007318#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007319 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007320#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007321
7322out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007323 VIM_CLEAR(post_start);
7324 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007325 state_ptr = NULL;
7326 return (regprog_T *)prog;
7327
7328fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007329 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007330#ifdef ENABLE_LOG
7331 nfa_postfix_dump(expr, FAIL);
7332#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007333#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007334 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007335#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007336 goto out;
7337}
7338
Bram Moolenaar473de612013-06-08 18:19:48 +02007339/*
7340 * Free a compiled regexp program, returned by nfa_regcomp().
7341 */
7342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007343nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007344{
7345 if (prog != NULL)
7346 {
7347 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007348 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007349 vim_free(prog);
7350 }
7351}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007352
7353/*
7354 * Match a regexp against a string.
7355 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7356 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007357 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007358 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007359 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007360 */
7361 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007362nfa_regexec_nl(
7363 regmatch_T *rmp,
7364 char_u *line, /* string to match against */
7365 colnr_T col, /* column to start looking for match */
7366 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007367{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007368 rex.reg_match = rmp;
7369 rex.reg_mmatch = NULL;
7370 rex.reg_maxline = 0;
7371 rex.reg_line_lbr = line_lbr;
7372 rex.reg_buf = curbuf;
7373 rex.reg_win = NULL;
7374 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007375 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007376 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007377 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007378}
7379
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007380
7381/*
7382 * Match a regexp against multiple lines.
7383 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7384 * Uses curbuf for line count and 'iskeyword'.
7385 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007386 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007387 * match otherwise.
7388 *
7389 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7390 *
7391 * ! Also NOTE : match may actually be in another line. e.g.:
7392 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7393 *
7394 * +-------------------------+
7395 * |a |
7396 * |b |
7397 * |c |
7398 * | |
7399 * +-------------------------+
7400 *
7401 * then nfa_regexec_multi() returns 3. while the original
7402 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7403 *
7404 * FIXME if this behavior is not compatible.
7405 */
7406 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007407nfa_regexec_multi(
7408 regmmatch_T *rmp,
7409 win_T *win, /* window in which to search or NULL */
7410 buf_T *buf, /* buffer in which to search */
7411 linenr_T lnum, /* nr of line to start looking for match */
7412 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007413 proftime_T *tm, /* timeout limit or NULL */
7414 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007415{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007416 rex.reg_match = NULL;
7417 rex.reg_mmatch = rmp;
7418 rex.reg_buf = buf;
7419 rex.reg_win = win;
7420 rex.reg_firstlnum = lnum;
7421 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7422 rex.reg_line_lbr = FALSE;
7423 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007424 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007425 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007426
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007427 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007428}
7429
7430#ifdef DEBUG
7431# undef ENABLE_LOG
7432#endif