blob: 031a6cf207cd532a252704592aee3afd147bce29 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
Bram Moolenaar221cd9f2019-01-31 15:34:40 +0100229 NFA_CLASS_ESCAPE,
230 NFA_CLASS_IDENT,
231 NFA_CLASS_KEYWORD,
232 NFA_CLASS_FNAME
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233};
234
235/* Keep in sync with classchars. */
236static int nfa_classcodes[] = {
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
241 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
242 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
243 NFA_UPPER, NFA_NUPPER
244};
245
Bram Moolenaar174a8482013-11-28 14:20:17 +0100246static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar0270f382018-07-17 05:43:58 +0200250// Variables only used in nfa_regcomp() and descendants.
251static int nfa_re_flags; // re_flags passed to nfa_regcomp()
252static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253static int *post_end;
254static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200255static int nstate; // Number of states in the NFA.
256static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar307aa162013-06-02 16:34:21 +0200258/* If not NULL match must end at this position */
259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200261/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
262static int nfa_ll_index = 0;
263
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100264static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100265static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100267static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100269static int match_follows(nfa_state_T *startstate, int depth);
270static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271
272/* helper functions used when doing re2post() ... regatom() parsing */
273#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275 return FAIL; \
276 *post_ptr++ = c; \
277 } while (0)
278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279/*
280 * Initialize internal variables before NFA compilation.
281 * Return OK on success, FAIL otherwise.
282 */
283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100284nfa_regcomp_start(
285 char_u *expr,
286 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200288 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200289 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290
291 nstate = 0;
292 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200293 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200294 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200296 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200297 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200298 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200299
300 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200301 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200302
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303 post_start = (int *)lalloc(postfix_size, TRUE);
304 if (post_start == NULL)
305 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200307 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200308 rex.nfa_has_zend = FALSE;
309 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200311 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312 regcomp_start(expr, re_flags);
313
314 return OK;
315}
316
317/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200318 * Figure out if the NFA state list starts with an anchor, must match at start
319 * of the line.
320 */
321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200323{
324 nfa_state_T *p = start;
325
326 if (depth > 4)
327 return 0;
328
329 while (p != NULL)
330 {
331 switch (p->c)
332 {
333 case NFA_BOL:
334 case NFA_BOF:
335 return 1; /* yes! */
336
337 case NFA_ZSTART:
338 case NFA_ZEND:
339 case NFA_CURSOR:
340 case NFA_VISUAL:
341
342 case NFA_MOPEN:
343 case NFA_MOPEN1:
344 case NFA_MOPEN2:
345 case NFA_MOPEN3:
346 case NFA_MOPEN4:
347 case NFA_MOPEN5:
348 case NFA_MOPEN6:
349 case NFA_MOPEN7:
350 case NFA_MOPEN8:
351 case NFA_MOPEN9:
352 case NFA_NOPEN:
353#ifdef FEAT_SYN_HL
354 case NFA_ZOPEN:
355 case NFA_ZOPEN1:
356 case NFA_ZOPEN2:
357 case NFA_ZOPEN3:
358 case NFA_ZOPEN4:
359 case NFA_ZOPEN5:
360 case NFA_ZOPEN6:
361 case NFA_ZOPEN7:
362 case NFA_ZOPEN8:
363 case NFA_ZOPEN9:
364#endif
365 p = p->out;
366 break;
367
368 case NFA_SPLIT:
369 return nfa_get_reganch(p->out, depth + 1)
370 && nfa_get_reganch(p->out1, depth + 1);
371
372 default:
373 return 0; /* noooo */
374 }
375 }
376 return 0;
377}
378
379/*
380 * Figure out if the NFA state list starts with a character which must match
381 * at start of the match.
382 */
383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100384nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200385{
386 nfa_state_T *p = start;
387
388 if (depth > 4)
389 return 0;
390
391 while (p != NULL)
392 {
393 switch (p->c)
394 {
395 /* all kinds of zero-width matches */
396 case NFA_BOL:
397 case NFA_BOF:
398 case NFA_BOW:
399 case NFA_EOW:
400 case NFA_ZSTART:
401 case NFA_ZEND:
402 case NFA_CURSOR:
403 case NFA_VISUAL:
404 case NFA_LNUM:
405 case NFA_LNUM_GT:
406 case NFA_LNUM_LT:
407 case NFA_COL:
408 case NFA_COL_GT:
409 case NFA_COL_LT:
410 case NFA_VCOL:
411 case NFA_VCOL_GT:
412 case NFA_VCOL_LT:
413 case NFA_MARK:
414 case NFA_MARK_GT:
415 case NFA_MARK_LT:
416
417 case NFA_MOPEN:
418 case NFA_MOPEN1:
419 case NFA_MOPEN2:
420 case NFA_MOPEN3:
421 case NFA_MOPEN4:
422 case NFA_MOPEN5:
423 case NFA_MOPEN6:
424 case NFA_MOPEN7:
425 case NFA_MOPEN8:
426 case NFA_MOPEN9:
427 case NFA_NOPEN:
428#ifdef FEAT_SYN_HL
429 case NFA_ZOPEN:
430 case NFA_ZOPEN1:
431 case NFA_ZOPEN2:
432 case NFA_ZOPEN3:
433 case NFA_ZOPEN4:
434 case NFA_ZOPEN5:
435 case NFA_ZOPEN6:
436 case NFA_ZOPEN7:
437 case NFA_ZOPEN8:
438 case NFA_ZOPEN9:
439#endif
440 p = p->out;
441 break;
442
443 case NFA_SPLIT:
444 {
445 int c1 = nfa_get_regstart(p->out, depth + 1);
446 int c2 = nfa_get_regstart(p->out1, depth + 1);
447
448 if (c1 == c2)
449 return c1; /* yes! */
450 return 0;
451 }
452
453 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200454 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200455 return p->c; /* yes! */
456 return 0;
457 }
458 }
459 return 0;
460}
461
462/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200463 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200464 * else. If so return a string in allocated memory with what must match after
465 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200466 */
467 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100468nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200469{
470 nfa_state_T *p = start;
471 int len = 0;
472 char_u *ret;
473 char_u *s;
474
475 if (p->c != NFA_MOPEN)
476 return NULL; /* just in case */
477 p = p->out;
478 while (p->c > 0)
479 {
480 len += MB_CHAR2LEN(p->c);
481 p = p->out;
482 }
483 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
484 return NULL;
485
486 ret = alloc(len);
487 if (ret != NULL)
488 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200489 p = start->out->out; /* skip first char, it goes into regstart */
490 s = ret;
491 while (p->c > 0)
492 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200493 if (has_mbyte)
494 s += (*mb_char2bytes)(p->c, s);
495 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 *s++ = p->c;
497 p = p->out;
498 }
499 *s = NUL;
500 }
501 return ret;
502}
503
504/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200505 * Allocate more space for post_start. Called when
506 * running above the estimated number of states.
507 */
508 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100509realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200510{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200511 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200512 int new_max = nstate_max + 1000;
513 int *new_start;
514 int *old_start;
515
516 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
517 if (new_start == NULL)
518 return FAIL;
519 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200520 old_start = post_start;
521 post_start = new_start;
522 post_ptr = new_start + (post_ptr - old_start);
523 post_end = post_start + new_max;
524 vim_free(old_start);
525 return OK;
526}
527
528/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200529 * Search between "start" and "end" and try to recognize a
530 * character class in expanded form. For example [0-9].
531 * On success, return the id the character class to be emitted.
532 * On failure, return 0 (=FAIL)
533 * Start points to the first char of the range, while end should point
534 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200535 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
536 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200537 */
538 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100539nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200541# define CLASS_not 0x80
542# define CLASS_af 0x40
543# define CLASS_AF 0x20
544# define CLASS_az 0x10
545# define CLASS_AZ 0x08
546# define CLASS_o7 0x04
547# define CLASS_o9 0x02
548# define CLASS_underscore 0x01
549
550 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200551 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200552 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200553
554 if (extra_newl == TRUE)
555 newl = TRUE;
556
557 if (*end != ']')
558 return FAIL;
559 p = start;
560 if (*p == '^')
561 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200562 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200563 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200564 }
565
566 while (p < end)
567 {
568 if (p + 2 < end && *(p + 1) == '-')
569 {
570 switch (*p)
571 {
572 case '0':
573 if (*(p + 2) == '9')
574 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200575 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200576 break;
577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200578 if (*(p + 2) == '7')
579 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200580 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200581 break;
582 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200583 return FAIL;
584
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200585 case 'a':
586 if (*(p + 2) == 'z')
587 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200588 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200589 break;
590 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200591 if (*(p + 2) == 'f')
592 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200593 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200594 break;
595 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200596 return FAIL;
597
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598 case 'A':
599 if (*(p + 2) == 'Z')
600 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200601 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200602 break;
603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 if (*(p + 2) == 'F')
605 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200606 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 break;
608 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200609 return FAIL;
610
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 default:
612 return FAIL;
613 }
614 p += 3;
615 }
616 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
617 {
618 newl = TRUE;
619 p += 2;
620 }
621 else if (*p == '_')
622 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200623 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200624 p ++;
625 }
626 else if (*p == '\n')
627 {
628 newl = TRUE;
629 p ++;
630 }
631 else
632 return FAIL;
633 } /* while (p < end) */
634
635 if (p != end)
636 return FAIL;
637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200639 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640
641 switch (config)
642 {
643 case CLASS_o9:
644 return extra_newl + NFA_DIGIT;
645 case CLASS_not | CLASS_o9:
646 return extra_newl + NFA_NDIGIT;
647 case CLASS_af | CLASS_AF | CLASS_o9:
648 return extra_newl + NFA_HEX;
649 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
650 return extra_newl + NFA_NHEX;
651 case CLASS_o7:
652 return extra_newl + NFA_OCTAL;
653 case CLASS_not | CLASS_o7:
654 return extra_newl + NFA_NOCTAL;
655 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
656 return extra_newl + NFA_WORD;
657 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
658 return extra_newl + NFA_NWORD;
659 case CLASS_az | CLASS_AZ | CLASS_underscore:
660 return extra_newl + NFA_HEAD;
661 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
662 return extra_newl + NFA_NHEAD;
663 case CLASS_az | CLASS_AZ:
664 return extra_newl + NFA_ALPHA;
665 case CLASS_not | CLASS_az | CLASS_AZ:
666 return extra_newl + NFA_NALPHA;
667 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200668 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200669 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200670 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200671 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200672 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200673 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200674 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200676 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677}
678
679/*
680 * Produce the bytes for equivalence class "c".
681 * Currently only handles latin1, latin9 and utf-8.
682 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
683 * equivalent to 'a OR b OR c'
684 *
685 * NOTE! When changing this function, also update reg_equi_class()
686 */
687 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100688nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200690#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100691#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200693 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
694 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200696#ifdef EBCDIC
697# define A_circumflex 0x62
698# define A_diaeresis 0x63
699# define A_grave 0x64
700# define A_acute 0x65
701# define A_virguilla 0x66
702# define A_ring 0x67
703# define C_cedilla 0x68
704# define E_acute 0x71
705# define E_circumflex 0x72
706# define E_diaeresis 0x73
707# define E_grave 0x74
708# define I_acute 0x75
709# define I_circumflex 0x76
710# define I_diaeresis 0x77
711# define I_grave 0x78
712# define N_virguilla 0x69
713# define O_circumflex 0xeb
714# define O_diaeresis 0xec
715# define O_grave 0xed
716# define O_acute 0xee
717# define O_virguilla 0xef
718# define O_slash 0x80
719# define U_circumflex 0xfb
720# define U_diaeresis 0xfc
721# define U_grave 0xfd
722# define U_acute 0xfe
723# define Y_acute 0xba
724# define a_grave 0x42
725# define a_acute 0x43
726# define a_circumflex 0x44
727# define a_virguilla 0x45
728# define a_diaeresis 0x46
729# define a_ring 0x47
730# define c_cedilla 0x48
731# define e_grave 0x51
732# define e_acute 0x52
733# define e_circumflex 0x53
734# define e_diaeresis 0x54
735# define i_grave 0x55
736# define i_acute 0x56
737# define i_circumflex 0x57
738# define i_diaeresis 0x58
739# define n_virguilla 0x49
740# define o_grave 0xcb
741# define o_acute 0xcc
742# define o_circumflex 0xcd
743# define o_virguilla 0xce
744# define o_diaeresis 0xcf
745# define o_slash 0x70
746# define u_grave 0xdb
747# define u_acute 0xdc
748# define u_circumflex 0xdd
749# define u_diaeresis 0xde
750# define y_acute 0x8d
751# define y_diaeresis 0xdf
752#else
753# define A_grave 0xc0
754# define A_acute 0xc1
755# define A_circumflex 0xc2
756# define A_virguilla 0xc3
757# define A_diaeresis 0xc4
758# define A_ring 0xc5
759# define C_cedilla 0xc7
760# define E_grave 0xc8
761# define E_acute 0xc9
762# define E_circumflex 0xca
763# define E_diaeresis 0xcb
764# define I_grave 0xcc
765# define I_acute 0xcd
766# define I_circumflex 0xce
767# define I_diaeresis 0xcf
768# define N_virguilla 0xd1
769# define O_grave 0xd2
770# define O_acute 0xd3
771# define O_circumflex 0xd4
772# define O_virguilla 0xd5
773# define O_diaeresis 0xd6
774# define O_slash 0xd8
775# define U_grave 0xd9
776# define U_acute 0xda
777# define U_circumflex 0xdb
778# define U_diaeresis 0xdc
779# define Y_acute 0xdd
780# define a_grave 0xe0
781# define a_acute 0xe1
782# define a_circumflex 0xe2
783# define a_virguilla 0xe3
784# define a_diaeresis 0xe4
785# define a_ring 0xe5
786# define c_cedilla 0xe7
787# define e_grave 0xe8
788# define e_acute 0xe9
789# define e_circumflex 0xea
790# define e_diaeresis 0xeb
791# define i_grave 0xec
792# define i_acute 0xed
793# define i_circumflex 0xee
794# define i_diaeresis 0xef
795# define n_virguilla 0xf1
796# define o_grave 0xf2
797# define o_acute 0xf3
798# define o_circumflex 0xf4
799# define o_virguilla 0xf5
800# define o_diaeresis 0xf6
801# define o_slash 0xf8
802# define u_grave 0xf9
803# define u_acute 0xfa
804# define u_circumflex 0xfb
805# define u_diaeresis 0xfc
806# define y_acute 0xfd
807# define y_diaeresis 0xff
808#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200809 switch (c)
810 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200811 case 'A': case A_grave: case A_acute: case A_circumflex:
812 case A_virguilla: case A_diaeresis: case A_ring:
813 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
814 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
815 CASEMBC(0x1ea2)
816 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
817 EMIT2(A_circumflex); EMIT2(A_virguilla);
818 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200819 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
820 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
821 EMITMBC(0x1ea2)
822 return OK;
823
824 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
825 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200826 return OK;
827
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200828 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
829 CASEMBC(0x10a) CASEMBC(0x10c)
830 EMIT2('C'); EMIT2(C_cedilla);
831 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200832 EMITMBC(0x10a) EMITMBC(0x10c)
833 return OK;
834
835 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200836 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200837 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
838 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200839 return OK;
840
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200841 case 'E': case E_grave: case E_acute: case E_circumflex:
842 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
843 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
844 CASEMBC(0x1eba) CASEMBC(0x1ebc)
845 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
846 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200847 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
848 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
849 EMITMBC(0x1ebc)
850 return OK;
851
852 case 'F': CASEMBC(0x1e1e)
853 EMIT2('F'); EMITMBC(0x1e1e)
854 return OK;
855
856 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200857 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
858 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200859 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
860 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
861 EMITMBC(0x1f4) EMITMBC(0x1e20)
862 return OK;
863
864 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200865 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200866 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
867 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 return OK;
869
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'I': case I_grave: case I_acute: case I_circumflex:
871 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
872 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
873 CASEMBC(0x1cf) CASEMBC(0x1ec8)
874 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
875 EMIT2(I_circumflex); EMIT2(I_diaeresis);
876 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200877 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
878 EMITMBC(0x1cf) EMITMBC(0x1ec8)
879 return OK;
880
881 case 'J': CASEMBC(0x134)
882 EMIT2('J'); EMITMBC(0x134)
883 return OK;
884
885 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200886 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200887 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
888 EMITMBC(0x1e34)
889 return OK;
890
891 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200892 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200893 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
894 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
895 return OK;
896
897 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
898 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200899 return OK;
900
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200901 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
902 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
903 EMIT2('N'); EMIT2(N_virguilla);
904 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200905 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 return OK;
907
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200908 case 'O': case O_grave: case O_acute: case O_circumflex:
909 case O_virguilla: case O_diaeresis: case O_slash:
910 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
911 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
912 CASEMBC(0x1ec) CASEMBC(0x1ece)
913 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
914 EMIT2(O_circumflex); EMIT2(O_virguilla);
915 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
917 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
918 EMITMBC(0x1ec) EMITMBC(0x1ece)
919 return OK;
920
921 case 'P': case 0x1e54: case 0x1e56:
922 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
923 return OK;
924
925 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200926 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200927 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
928 EMITMBC(0x1e58) EMITMBC(0x1e5e)
929 return OK;
930
931 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200932 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200933 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
934 EMITMBC(0x160) EMITMBC(0x1e60)
935 return OK;
936
937 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200938 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200939 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
940 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200941 return OK;
942
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200943 case 'U': case U_grave: case U_acute: case U_diaeresis:
944 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
945 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
946 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
947 CASEMBC(0x1ee6)
948 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
949 EMIT2(U_diaeresis); EMIT2(U_circumflex);
950 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200951 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
952 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
953 EMITMBC(0x1ee6)
954 return OK;
955
956 case 'V': CASEMBC(0x1e7c)
957 EMIT2('V'); EMITMBC(0x1e7c)
958 return OK;
959
960 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200961 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200962 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
963 EMITMBC(0x1e84) EMITMBC(0x1e86)
964 return OK;
965
966 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
967 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200968 return OK;
969
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200970 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
971 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
972 CASEMBC(0x1ef8)
973 EMIT2('Y'); EMIT2(Y_acute);
974 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200975 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
976 EMITMBC(0x1ef8)
977 return OK;
978
979 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200980 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200981 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
982 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200983 return OK;
984
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 case 'a': case a_grave: case a_acute: case a_circumflex:
986 case a_virguilla: case a_diaeresis: case a_ring:
987 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
988 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
989 CASEMBC(0x1ea3)
990 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
991 EMIT2(a_circumflex); EMIT2(a_virguilla);
992 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200993 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
994 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
995 EMITMBC(0x1ea3)
996 return OK;
997
998 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
999 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return OK;
1001
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001002 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1003 CASEMBC(0x10b) CASEMBC(0x10d)
1004 EMIT2('c'); EMIT2(c_cedilla);
1005 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001006 EMITMBC(0x10b) EMITMBC(0x10d)
1007 return OK;
1008
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001009 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001010 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001011 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1012 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001013 return OK;
1014
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001015 case 'e': case e_grave: case e_acute: case e_circumflex:
1016 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1017 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1018 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1019 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1020 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1021 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001022 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1023 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1024 return OK;
1025
1026 case 'f': CASEMBC(0x1e1f)
1027 EMIT2('f'); EMITMBC(0x1e1f)
1028 return OK;
1029
1030 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001031 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1032 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001033 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1034 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1035 EMITMBC(0x1f5) EMITMBC(0x1e21)
1036 return OK;
1037
1038 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001040 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1041 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'i': case i_grave: case i_acute: case i_circumflex:
1045 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1046 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1047 CASEMBC(0x1ec9)
1048 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1049 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1050 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001051 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1052 EMITMBC(0x1ec9)
1053 return OK;
1054
1055 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1056 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1057 return OK;
1058
1059 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001060 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001061 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1062 EMITMBC(0x1e35)
1063 return OK;
1064
1065 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001066 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001067 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1068 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1069 return OK;
1070
1071 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1072 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001073 return OK;
1074
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001075 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1076 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1077 CASEMBC(0x1e49)
1078 EMIT2('n'); EMIT2(n_virguilla);
1079 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001080 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1081 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001082 return OK;
1083
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001084 case 'o': case o_grave: case o_acute: case o_circumflex:
1085 case o_virguilla: case o_diaeresis: case o_slash:
1086 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1087 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1088 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1089 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1090 EMIT2(o_circumflex); EMIT2(o_virguilla);
1091 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1093 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1094 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1095 return OK;
1096
1097 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1098 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1099 return OK;
1100
1101 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001102 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001103 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1104 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1105 return OK;
1106
1107 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001108 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001109 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1110 EMITMBC(0x161) EMITMBC(0x1e61)
1111 return OK;
1112
1113 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001114 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001115 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1116 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001117 return OK;
1118
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 case 'u': case u_grave: case u_acute: case u_circumflex:
1120 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1121 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1122 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1123 CASEMBC(0x1ee7)
1124 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1125 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1126 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001127 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1128 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1129 EMITMBC(0x1ee7)
1130 return OK;
1131
1132 case 'v': CASEMBC(0x1e7d)
1133 EMIT2('v'); EMITMBC(0x1e7d)
1134 return OK;
1135
1136 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001137 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001138 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1139 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1140 return OK;
1141
1142 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1143 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001144 return OK;
1145
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001146 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1147 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1148 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1149 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1150 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001151 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1152 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001153 return OK;
1154
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001155 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001156 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001157 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1158 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1159 return OK;
1160
1161 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001162 }
1163 }
1164
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001165 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166 return OK;
1167#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169}
1170
1171/*
1172 * Code to parse regular expression.
1173 *
1174 * We try to reuse parsing functions in regexp.c to
1175 * minimize surprise and keep the syntax consistent.
1176 */
1177
1178/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001179 * Parse the lowest level.
1180 *
1181 * An atom can be one of a long list of items. Many atoms match one character
1182 * in the text. It is often an ordinary character or a character class.
1183 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1184 * is only for syntax highlighting.
1185 *
1186 * atom ::= ordinary-atom
1187 * or \( pattern \)
1188 * or \%( pattern \)
1189 * or \z( pattern \)
1190 */
1191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001192nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001193{
1194 int c;
1195 int charclass;
1196 int equiclass;
1197 int collclass;
1198 int got_coll_char;
1199 char_u *p;
1200 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001202 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 int emit_range;
1204 int negated;
1205 int result;
1206 int startc = -1;
1207 int endc = -1;
1208 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001209 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001211 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 switch (c)
1213 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001214 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001215 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001216
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001217 case Magic('^'):
1218 EMIT(NFA_BOL);
1219 break;
1220
1221 case Magic('$'):
1222 EMIT(NFA_EOL);
1223#if defined(FEAT_SYN_HL) || defined(PROTO)
1224 had_eol = TRUE;
1225#endif
1226 break;
1227
1228 case Magic('<'):
1229 EMIT(NFA_BOW);
1230 break;
1231
1232 case Magic('>'):
1233 EMIT(NFA_EOW);
1234 break;
1235
1236 case Magic('_'):
1237 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001238 if (c == NUL)
1239 EMSG_RET_FAIL(_(e_nul_found));
1240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241 if (c == '^') /* "\_^" is start-of-line */
1242 {
1243 EMIT(NFA_BOL);
1244 break;
1245 }
1246 if (c == '$') /* "\_$" is end-of-line */
1247 {
1248 EMIT(NFA_EOL);
1249#if defined(FEAT_SYN_HL) || defined(PROTO)
1250 had_eol = TRUE;
1251#endif
1252 break;
1253 }
1254
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001255 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256
1257 /* "\_[" is collection plus newline */
1258 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001259 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260
1261 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001262 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
1264 /*
1265 * Character classes.
1266 */
1267 case Magic('.'):
1268 case Magic('i'):
1269 case Magic('I'):
1270 case Magic('k'):
1271 case Magic('K'):
1272 case Magic('f'):
1273 case Magic('F'):
1274 case Magic('p'):
1275 case Magic('P'):
1276 case Magic('s'):
1277 case Magic('S'):
1278 case Magic('d'):
1279 case Magic('D'):
1280 case Magic('x'):
1281 case Magic('X'):
1282 case Magic('o'):
1283 case Magic('O'):
1284 case Magic('w'):
1285 case Magic('W'):
1286 case Magic('h'):
1287 case Magic('H'):
1288 case Magic('a'):
1289 case Magic('A'):
1290 case Magic('l'):
1291 case Magic('L'):
1292 case Magic('u'):
1293 case Magic('U'):
1294 p = vim_strchr(classchars, no_Magic(c));
1295 if (p == NULL)
1296 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001297 if (extra == NFA_ADD_NL)
1298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001300 rc_did_emsg = TRUE;
1301 return FAIL;
1302 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001303 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001304 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001306
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307 /* When '.' is followed by a composing char ignore the dot, so that
1308 * the composing char is matched here. */
1309 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1310 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001311 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312 c = getchr();
1313 goto nfa_do_multibyte;
1314 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001316 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317 {
1318 EMIT(NFA_NEWL);
1319 EMIT(NFA_OR);
1320 regflags |= RF_HASNL;
1321 }
1322 break;
1323
1324 case Magic('n'):
1325 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001326 /* In a string "\n" matches a newline character. */
1327 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328 else
1329 {
1330 /* In buffer text "\n" matches the end of a line. */
1331 EMIT(NFA_NEWL);
1332 regflags |= RF_HASNL;
1333 }
1334 break;
1335
1336 case Magic('('):
1337 if (nfa_reg(REG_PAREN) == FAIL)
1338 return FAIL; /* cascaded error */
1339 break;
1340
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001341 case Magic('|'):
1342 case Magic('&'):
1343 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001344 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 return FAIL;
1346
1347 case Magic('='):
1348 case Magic('?'):
1349 case Magic('+'):
1350 case Magic('@'):
1351 case Magic('*'):
1352 case Magic('{'):
1353 /* these should follow an atom, not form an atom */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001354 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001355 return FAIL;
1356
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001357 case Magic('~'):
1358 {
1359 char_u *lp;
1360
1361 /* Previous substitute pattern.
1362 * Generated as "\%(pattern\)". */
1363 if (reg_prev_sub == NULL)
1364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001365 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001366 return FAIL;
1367 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001368 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001369 {
1370 EMIT(PTR2CHAR(lp));
1371 if (lp != reg_prev_sub)
1372 EMIT(NFA_CONCAT);
1373 }
1374 EMIT(NFA_NOPEN);
1375 break;
1376 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377
Bram Moolenaar428e9872013-05-30 17:05:39 +02001378 case Magic('1'):
1379 case Magic('2'):
1380 case Magic('3'):
1381 case Magic('4'):
1382 case Magic('5'):
1383 case Magic('6'):
1384 case Magic('7'):
1385 case Magic('8'):
1386 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001387 {
1388 int refnum = no_Magic(c) - '1';
1389
1390 if (!seen_endbrace(refnum + 1))
1391 return FAIL;
1392 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001393 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001394 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001395 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001396
1397 case Magic('z'):
1398 c = no_Magic(getchr());
1399 switch (c)
1400 {
1401 case 's':
1402 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001403 if (re_mult_next("\\zs") == FAIL)
1404 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 break;
1406 case 'e':
1407 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001408 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001409 if (re_mult_next("\\ze") == FAIL)
1410 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001411 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001412#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 case '1':
1414 case '2':
1415 case '3':
1416 case '4':
1417 case '5':
1418 case '6':
1419 case '7':
1420 case '8':
1421 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001422 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001423 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001424 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001425 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar0270f382018-07-17 05:43:58 +02001426 /* No need to set rex.nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001427 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001428 re_has_z = REX_USE;
1429 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001430 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001431 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001432 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001433 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001434 if (nfa_reg(REG_ZPAREN) == FAIL)
1435 return FAIL; /* cascaded error */
1436 re_has_z = REX_SET;
1437 break;
1438#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001440 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441 no_Magic(c));
1442 return FAIL;
1443 }
1444 break;
1445
1446 case Magic('%'):
1447 c = no_Magic(getchr());
1448 switch (c)
1449 {
1450 /* () without a back reference */
1451 case '(':
1452 if (nfa_reg(REG_NPAREN) == FAIL)
1453 return FAIL;
1454 EMIT(NFA_NOPEN);
1455 break;
1456
1457 case 'd': /* %d123 decimal */
1458 case 'o': /* %o123 octal */
1459 case 'x': /* %xab hex 2 */
1460 case 'u': /* %uabcd hex 4 */
1461 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001462 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001463 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001464
Bram Moolenaar47196582013-05-25 22:04:23 +02001465 switch (c)
1466 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001467 case 'd': nr = getdecchrs(); break;
1468 case 'o': nr = getoctchrs(); break;
1469 case 'x': nr = gethexchrs(2); break;
1470 case 'u': nr = gethexchrs(4); break;
1471 case 'U': nr = gethexchrs(8); break;
1472 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001473 }
1474
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001475 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001476 EMSG2_RET_FAIL(
1477 _("E678: Invalid character after %s%%[dxouU]"),
1478 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001479 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001480 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001481 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001482 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001483 break;
1484
1485 /* Catch \%^ and \%$ regardless of where they appear in the
1486 * pattern -- regardless of whether or not it makes sense. */
1487 case '^':
1488 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001489 break;
1490
1491 case '$':
1492 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 break;
1494
1495 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001496 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 break;
1498
1499 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001500 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 break;
1502
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001503 case 'C':
1504 EMIT(NFA_ANY_COMPOSING);
1505 break;
1506
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001507 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001508 {
1509 int n;
1510
1511 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001512 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001513 {
1514 if (c == NUL)
1515 EMSG2_RET_FAIL(_(e_missing_sb),
1516 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001517 /* recursive call! */
1518 if (nfa_regatom() == FAIL)
1519 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001520 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001521 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001522 if (n == 0)
1523 EMSG2_RET_FAIL(_(e_empty_sb),
1524 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001525 EMIT(NFA_OPT_CHARS);
1526 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001527
1528 /* Emit as "\%(\%[abc]\)" to be able to handle
1529 * "\%[abc]*" which would cause the empty string to be
1530 * matched an unlimited number of times. NFA_NOPEN is
1531 * added only once at a position, while NFA_SPLIT is
1532 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001533 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001534 * a lot. */
1535 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001536 break;
1537 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001538
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001540 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001541 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001542 int cmp = c;
1543
1544 if (c == '<' || c == '>')
1545 c = getchr();
1546 while (VIM_ISDIGIT(c))
1547 {
1548 n = n * 10 + (c - '0');
1549 c = getchr();
1550 }
1551 if (c == 'l' || c == 'c' || c == 'v')
1552 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001553 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001554 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001555 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001556 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001557 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001558 if (save_prev_at_start)
1559 at_start = TRUE;
1560 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001561 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001562 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001563 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001564 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001565 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001566 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001567 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001568 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001569#if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
1570 if (n > INT_MAX)
1571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001572 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001573 return FAIL;
1574 }
1575#endif
1576 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001577 break;
1578 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001579 else if (c == '\'' && n == 0)
1580 {
1581 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001582 EMIT(cmp == '<' ? NFA_MARK_LT :
1583 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001584 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001585 break;
1586 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001587 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001588 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001589 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001590 return FAIL;
1591 }
1592 break;
1593
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001594 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001595collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001597 * [abc] uses NFA_START_COLL - NFA_END_COLL
1598 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1599 * Each character is produced as a regular state, using
1600 * NFA_CONCAT to bind them together.
1601 * Besides normal characters there can be:
1602 * - character classes NFA_CLASS_*
1603 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 */
1605
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001606 p = regparse;
1607 endp = skip_anyof(p);
1608 if (*endp == ']')
1609 {
1610 /*
1611 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001612 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001613 * and perform the necessary substitutions in the NFA.
1614 */
1615 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001616 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617 if (result != FAIL)
1618 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001619 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001620 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001621 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001622 EMIT(NFA_NEWL);
1623 EMIT(NFA_OR);
1624 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001625 else
1626 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001627 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001628 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001629 return OK;
1630 }
1631 /*
1632 * Failed to recognize a character class. Use the simple
1633 * version that turns [abc] into 'a' OR 'b' OR 'c'
1634 */
1635 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001636 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 if (*regparse == '^') /* negated range */
1638 {
1639 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001640 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001641 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001643 else
1644 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 if (*regparse == '-')
1646 {
1647 startc = '-';
1648 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001649 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001650 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 }
1652 /* Emit the OR branches for each character in the [] */
1653 emit_range = FALSE;
1654 while (regparse < endp)
1655 {
1656 oldstartc = startc;
1657 startc = -1;
1658 got_coll_char = FALSE;
1659 if (*regparse == '[')
1660 {
1661 /* Check for [: :], [= =], [. .] */
1662 equiclass = collclass = 0;
1663 charclass = get_char_class(&regparse);
1664 if (charclass == CLASS_NONE)
1665 {
1666 equiclass = get_equi_class(&regparse);
1667 if (equiclass == 0)
1668 collclass = get_coll_element(&regparse);
1669 }
1670
1671 /* Character class like [:alpha:] */
1672 if (charclass != CLASS_NONE)
1673 {
1674 switch (charclass)
1675 {
1676 case CLASS_ALNUM:
1677 EMIT(NFA_CLASS_ALNUM);
1678 break;
1679 case CLASS_ALPHA:
1680 EMIT(NFA_CLASS_ALPHA);
1681 break;
1682 case CLASS_BLANK:
1683 EMIT(NFA_CLASS_BLANK);
1684 break;
1685 case CLASS_CNTRL:
1686 EMIT(NFA_CLASS_CNTRL);
1687 break;
1688 case CLASS_DIGIT:
1689 EMIT(NFA_CLASS_DIGIT);
1690 break;
1691 case CLASS_GRAPH:
1692 EMIT(NFA_CLASS_GRAPH);
1693 break;
1694 case CLASS_LOWER:
1695 EMIT(NFA_CLASS_LOWER);
1696 break;
1697 case CLASS_PRINT:
1698 EMIT(NFA_CLASS_PRINT);
1699 break;
1700 case CLASS_PUNCT:
1701 EMIT(NFA_CLASS_PUNCT);
1702 break;
1703 case CLASS_SPACE:
1704 EMIT(NFA_CLASS_SPACE);
1705 break;
1706 case CLASS_UPPER:
1707 EMIT(NFA_CLASS_UPPER);
1708 break;
1709 case CLASS_XDIGIT:
1710 EMIT(NFA_CLASS_XDIGIT);
1711 break;
1712 case CLASS_TAB:
1713 EMIT(NFA_CLASS_TAB);
1714 break;
1715 case CLASS_RETURN:
1716 EMIT(NFA_CLASS_RETURN);
1717 break;
1718 case CLASS_BACKSPACE:
1719 EMIT(NFA_CLASS_BACKSPACE);
1720 break;
1721 case CLASS_ESCAPE:
1722 EMIT(NFA_CLASS_ESCAPE);
1723 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001724 case CLASS_IDENT:
1725 EMIT(NFA_CLASS_IDENT);
1726 break;
1727 case CLASS_KEYWORD:
1728 EMIT(NFA_CLASS_KEYWORD);
1729 break;
1730 case CLASS_FNAME:
1731 EMIT(NFA_CLASS_FNAME);
1732 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001734 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 continue;
1736 }
1737 /* Try equivalence class [=a=] and the like */
1738 if (equiclass != 0)
1739 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001740 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741 if (result == FAIL)
1742 {
1743 /* should never happen */
1744 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1745 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001746 continue;
1747 }
1748 /* Try collating class like [. .] */
1749 if (collclass != 0)
1750 {
1751 startc = collclass; /* allow [.a.]-x as a range */
1752 /* Will emit the proper atom at the end of the
1753 * while loop. */
1754 }
1755 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001756 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1757 * start character. */
1758 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001759 {
1760 emit_range = TRUE;
1761 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001762 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001763 continue; /* reading the end of the range */
1764 }
1765
1766 /* Now handle simple and escaped characters.
1767 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1768 * accepts "\t", "\e", etc., but only when the 'l' flag in
1769 * 'cpoptions' is not included.
1770 * Posix doesn't recognize backslash at all.
1771 */
1772 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001773 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774 && regparse + 1 <= endp
1775 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001776 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 && vim_strchr(REGEXP_ABBR, regparse[1])
1778 != NULL)
1779 )
1780 )
1781 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001782 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001784 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001785 startc = reg_string ? NL : NFA_NEWL;
1786 else
1787 if (*regparse == 'd'
1788 || *regparse == 'o'
1789 || *regparse == 'x'
1790 || *regparse == 'u'
1791 || *regparse == 'U'
1792 )
1793 {
1794 /* TODO(RE) This needs more testing */
1795 startc = coll_get_char();
1796 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001797 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001798 }
1799 else
1800 {
1801 /* \r,\t,\e,\b */
1802 startc = backslash_trans(*regparse);
1803 }
1804 }
1805
1806 /* Normal printable char */
1807 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001808 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809
1810 /* Previous char was '-', so this char is end of range. */
1811 if (emit_range)
1812 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001813 endc = startc;
1814 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001815 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001816 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001817
1818 if (endc > startc + 2)
1819 {
1820 /* Emit a range instead of the sequence of
1821 * individual characters. */
1822 if (startc == 0)
1823 /* \x00 is translated to \x0a, start at \x01. */
1824 EMIT(1);
1825 else
1826 --post_ptr; /* remove NFA_CONCAT */
1827 EMIT(endc);
1828 EMIT(NFA_RANGE);
1829 EMIT(NFA_CONCAT);
1830 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001831 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832 || (*mb_char2len)(endc) > 1))
1833 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001834 /* Emit the characters in the range.
1835 * "startc" was already emitted, so skip it.
1836 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837 for (c = startc + 1; c <= endc; c++)
1838 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001839 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001840 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001841 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001842 }
1843 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 {
1845#ifdef EBCDIC
1846 int alpha_only = FALSE;
1847
1848 /* for alphabetical range skip the gaps
1849 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1850 if (isalpha(startc) && isalpha(endc))
1851 alpha_only = TRUE;
1852#endif
1853 /* Emit the range. "startc" was already emitted, so
1854 * skip it. */
1855 for (c = startc + 1; c <= endc; c++)
1856#ifdef EBCDIC
1857 if (!alpha_only || isalpha(startc))
1858#endif
1859 {
1860 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001861 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001863 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001864 emit_range = FALSE;
1865 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 }
1867 else
1868 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001869 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 * Normally, simply emit startc. But if we get char
1872 * code=0 from a collating char, then replace it with
1873 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001874 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001875 * the backtracking engine. */
1876 if (startc == NFA_NEWL)
1877 {
1878 /* Line break can't be matched as part of the
1879 * collection, add an OR below. But not for negated
1880 * range. */
1881 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001882 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001883 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001884 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001885 {
1886 if (got_coll_char == TRUE && startc == 0)
1887 EMIT(0x0a);
1888 else
1889 EMIT(startc);
1890 EMIT(NFA_CONCAT);
1891 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892 }
1893
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001894 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895 } /* while (p < endp) */
1896
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001897 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 if (*regparse == '-') /* if last, '-' is just a char */
1899 {
1900 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001901 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 /* skip the trailing ] */
1905 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001906 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001907
1908 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001910 EMIT(NFA_END_NEG_COLL);
1911 else
1912 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001913
1914 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001915 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001916 {
1917 EMIT(reg_string ? NL : NFA_NEWL);
1918 EMIT(NFA_OR);
1919 }
1920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001921 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001922 } /* if exists closing ] */
1923
1924 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001926 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 default:
1929 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 int plen;
1931
1932nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001933 /* plen is length of current char with composing chars */
1934 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001935 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001936 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001937 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001938 int i = 0;
1939
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001940 /* A base character plus composing characters, or just one
1941 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001942 * This requires creating a separate atom as if enclosing
1943 * the characters in (), where NFA_COMPOSING is the ( and
1944 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 * building the postfix form, not the NFA itself;
1946 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001947 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001948 for (;;)
1949 {
1950 EMIT(c);
1951 if (i > 0)
1952 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001953 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001954 break;
1955 c = utf_ptr2char(old_regparse + i);
1956 }
1957 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 regparse = old_regparse + plen;
1959 }
1960 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001961 {
1962 c = no_Magic(c);
1963 EMIT(c);
1964 }
1965 return OK;
1966 }
1967 }
1968
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001969 return OK;
1970}
1971
1972/*
1973 * Parse something followed by possible [*+=].
1974 *
1975 * A piece is an atom, possibly followed by a multi, an indication of how many
1976 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1977 * characters: "", "a", "aa", etc.
1978 *
1979 * piece ::= atom
1980 * or atom multi
1981 */
1982 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001983nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001984{
1985 int i;
1986 int op;
1987 int ret;
1988 long minval, maxval;
1989 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001990 parse_state_T old_state;
1991 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001992 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001993 int old_post_pos;
1994 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001995 int quest;
1996
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001997 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1998 * next. */
1999 save_parse_state(&old_state);
2000
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002001 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002002 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003
2004 ret = nfa_regatom();
2005 if (ret == FAIL)
2006 return FAIL; /* cascaded error */
2007
2008 op = peekchr();
2009 if (re_multi_type(op) == NOT_MULTI)
2010 return OK;
2011
2012 skipchr();
2013 switch (op)
2014 {
2015 case Magic('*'):
2016 EMIT(NFA_STAR);
2017 break;
2018
2019 case Magic('+'):
2020 /*
2021 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2022 * first and only submatch would be "aaa". But the backtracking
2023 * engine interprets the plus as "try matching one more time", and
2024 * a* matches a second time at the end of the input, the empty
2025 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002026 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002028 * In order to be consistent with the old engine, we replace
2029 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002031 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002032 curchr = -1;
2033 if (nfa_regatom() == FAIL)
2034 return FAIL;
2035 EMIT(NFA_STAR);
2036 EMIT(NFA_CONCAT);
2037 skipchr(); /* skip the \+ */
2038 break;
2039
2040 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002041 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002043 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 switch(op)
2045 {
2046 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002047 /* \@= */
2048 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002050 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002051 /* \@! */
2052 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002053 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002055 op = no_Magic(getchr());
2056 if (op == '=')
2057 /* \@<= */
2058 i = NFA_PREV_ATOM_JUST_BEFORE;
2059 else if (op == '!')
2060 /* \@<! */
2061 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2062 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002064 /* \@> */
2065 i = NFA_PREV_ATOM_LIKE_PATTERN;
2066 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002068 if (i == 0)
2069 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002070 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002071 return FAIL;
2072 }
2073 EMIT(i);
2074 if (i == NFA_PREV_ATOM_JUST_BEFORE
2075 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2076 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 break;
2078
2079 case Magic('?'):
2080 case Magic('='):
2081 EMIT(NFA_QUEST);
2082 break;
2083
2084 case Magic('{'):
2085 /* a{2,5} will expand to 'aaa?a?a?'
2086 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2087 * version of '?'
2088 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2089 * parenthesis have the same id
2090 */
2091
2092 greedy = TRUE;
2093 c2 = peekchr();
2094 if (c2 == '-' || c2 == Magic('-'))
2095 {
2096 skipchr();
2097 greedy = FALSE;
2098 }
2099 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002101
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2103 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002104 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002106 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002107 /* \{}, \{0,} */
2108 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002109 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002110 /* \{-}, \{-0,} */
2111 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 break;
2113 }
2114
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002115 /* Special case: x{0} or x{-0} */
2116 if (maxval == 0)
2117 {
2118 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002119 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002120 /* NFA_EMPTY is 0-length and works everywhere */
2121 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 return OK;
2123 }
2124
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002125 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002126 * maximum is much larger than the minimum and when the maximum is
2127 * large. Bail out if we can use the other engine. */
2128 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002129 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002130 return FAIL;
2131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002132 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002133 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002134 /* Save parse state after the repeated atom and the \{} */
2135 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002136
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002137 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2138 for (i = 0; i < maxval; i++)
2139 {
2140 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002141 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002142 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143 if (nfa_regatom() == FAIL)
2144 return FAIL;
2145 /* after "minval" times, atoms are optional */
2146 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002147 {
2148 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002149 {
2150 if (greedy)
2151 EMIT(NFA_STAR);
2152 else
2153 EMIT(NFA_STAR_NONGREEDY);
2154 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002155 else
2156 EMIT(quest);
2157 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002158 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002159 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002160 if (i + 1 > minval && maxval == MAX_LIMIT)
2161 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002162 }
2163
2164 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002165 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 curchr = -1;
2167
2168 break;
2169
2170
2171 default:
2172 break;
2173 } /* end switch */
2174
2175 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002176 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002177 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178
2179 return OK;
2180}
2181
2182/*
2183 * Parse one or more pieces, concatenated. It matches a match for the
2184 * first piece, followed by a match for the second piece, etc. Example:
2185 * "f[0-9]b", first matches "f", then a digit and then "b".
2186 *
2187 * concat ::= piece
2188 * or piece piece
2189 * or piece piece piece
2190 * etc.
2191 */
2192 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002193nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194{
2195 int cont = TRUE;
2196 int first = TRUE;
2197
2198 while (cont)
2199 {
2200 switch (peekchr())
2201 {
2202 case NUL:
2203 case Magic('|'):
2204 case Magic('&'):
2205 case Magic(')'):
2206 cont = FALSE;
2207 break;
2208
2209 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002210 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002211 skipchr_keepstart();
2212 break;
2213 case Magic('c'):
2214 regflags |= RF_ICASE;
2215 skipchr_keepstart();
2216 break;
2217 case Magic('C'):
2218 regflags |= RF_NOICASE;
2219 skipchr_keepstart();
2220 break;
2221 case Magic('v'):
2222 reg_magic = MAGIC_ALL;
2223 skipchr_keepstart();
2224 curchr = -1;
2225 break;
2226 case Magic('m'):
2227 reg_magic = MAGIC_ON;
2228 skipchr_keepstart();
2229 curchr = -1;
2230 break;
2231 case Magic('M'):
2232 reg_magic = MAGIC_OFF;
2233 skipchr_keepstart();
2234 curchr = -1;
2235 break;
2236 case Magic('V'):
2237 reg_magic = MAGIC_NONE;
2238 skipchr_keepstart();
2239 curchr = -1;
2240 break;
2241
2242 default:
2243 if (nfa_regpiece() == FAIL)
2244 return FAIL;
2245 if (first == FALSE)
2246 EMIT(NFA_CONCAT);
2247 else
2248 first = FALSE;
2249 break;
2250 }
2251 }
2252
2253 return OK;
2254}
2255
2256/*
2257 * Parse a branch, one or more concats, separated by "\&". It matches the
2258 * last concat, but only if all the preceding concats also match at the same
2259 * position. Examples:
2260 * "foobeep\&..." matches "foo" in "foobeep".
2261 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2262 *
2263 * branch ::= concat
2264 * or concat \& concat
2265 * or concat \& concat \& concat
2266 * etc.
2267 */
2268 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002269nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002270{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002271 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002272
Bram Moolenaar16299b52013-05-30 18:45:23 +02002273 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274
2275 /* First branch, possibly the only one */
2276 if (nfa_regconcat() == FAIL)
2277 return FAIL;
2278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002280 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002281 {
2282 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002283 /* if concat is empty do emit a node */
2284 if (old_post_pos == (int)(post_ptr - post_start))
2285 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002286 EMIT(NFA_NOPEN);
2287 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002288 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 if (nfa_regconcat() == FAIL)
2290 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002291 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002292 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002293 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002294 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 }
2296
Bram Moolenaar699c1202013-09-25 16:41:54 +02002297 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002298 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002299 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300
2301 return OK;
2302}
2303
2304/*
2305 * Parse a pattern, one or more branches, separated by "\|". It matches
2306 * anything that matches one of the branches. Example: "foo\|beep" matches
2307 * "foo" and matches "beep". If more than one branch matches, the first one
2308 * is used.
2309 *
2310 * pattern ::= branch
2311 * or branch \| branch
2312 * or branch \| branch \| branch
2313 * etc.
2314 */
2315 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002316nfa_reg(
2317 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002318{
2319 int parno = 0;
2320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002321 if (paren == REG_PAREN)
2322 {
2323 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 parno = regnpar++;
2326 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002327#ifdef FEAT_SYN_HL
2328 else if (paren == REG_ZPAREN)
2329 {
2330 /* Make a ZOPEN node. */
2331 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002332 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002333 parno = regnzpar++;
2334 }
2335#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002336
2337 if (nfa_regbranch() == FAIL)
2338 return FAIL; /* cascaded error */
2339
2340 while (peekchr() == Magic('|'))
2341 {
2342 skipchr();
2343 if (nfa_regbranch() == FAIL)
2344 return FAIL; /* cascaded error */
2345 EMIT(NFA_OR);
2346 }
2347
2348 /* Check for proper termination. */
2349 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2350 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002351 if (paren == REG_NPAREN)
2352 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2353 else
2354 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2355 }
2356 else if (paren == REG_NOPAREN && peekchr() != NUL)
2357 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 if (peekchr() == Magic(')'))
2359 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2360 else
2361 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2362 }
2363 /*
2364 * Here we set the flag allowing back references to this set of
2365 * parentheses.
2366 */
2367 if (paren == REG_PAREN)
2368 {
2369 had_endbrace[parno] = TRUE; /* have seen the close paren */
2370 EMIT(NFA_MOPEN + parno);
2371 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002372#ifdef FEAT_SYN_HL
2373 else if (paren == REG_ZPAREN)
2374 EMIT(NFA_ZOPEN + parno);
2375#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002376
2377 return OK;
2378}
2379
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002380#ifdef DEBUG
2381static char_u code[50];
2382
2383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002384nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385{
2386 int addnl = FALSE;
2387
2388 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2389 {
2390 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002391 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002392 }
2393
2394 STRCPY(code, "");
2395 switch (c)
2396 {
2397 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2398 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2399 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2400 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2401 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2402 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2403
Bram Moolenaar5714b802013-05-28 22:03:20 +02002404 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2405 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2406 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2407 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2408 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2409 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2410 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2411 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2412 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002413#ifdef FEAT_SYN_HL
2414 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2415 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2416 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2417 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2418 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2419 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2420 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2421 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2422 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2423#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002424 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2425
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002426 case NFA_PREV_ATOM_NO_WIDTH:
2427 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002428 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2429 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002430 case NFA_PREV_ATOM_JUST_BEFORE:
2431 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2432 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2433 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002434 case NFA_PREV_ATOM_LIKE_PATTERN:
2435 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2436
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002437 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2438 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002440 case NFA_START_INVISIBLE_FIRST:
2441 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002442 case NFA_START_INVISIBLE_NEG:
2443 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002444 case NFA_START_INVISIBLE_NEG_FIRST:
2445 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002446 case NFA_START_INVISIBLE_BEFORE:
2447 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002448 case NFA_START_INVISIBLE_BEFORE_FIRST:
2449 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002450 case NFA_START_INVISIBLE_BEFORE_NEG:
2451 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002452 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2453 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002454 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002455 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002456 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002457 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2460 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002461 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002463 case NFA_MOPEN:
2464 case NFA_MOPEN1:
2465 case NFA_MOPEN2:
2466 case NFA_MOPEN3:
2467 case NFA_MOPEN4:
2468 case NFA_MOPEN5:
2469 case NFA_MOPEN6:
2470 case NFA_MOPEN7:
2471 case NFA_MOPEN8:
2472 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002473 STRCPY(code, "NFA_MOPEN(x)");
2474 code[10] = c - NFA_MOPEN + '0';
2475 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002476 case NFA_MCLOSE:
2477 case NFA_MCLOSE1:
2478 case NFA_MCLOSE2:
2479 case NFA_MCLOSE3:
2480 case NFA_MCLOSE4:
2481 case NFA_MCLOSE5:
2482 case NFA_MCLOSE6:
2483 case NFA_MCLOSE7:
2484 case NFA_MCLOSE8:
2485 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 STRCPY(code, "NFA_MCLOSE(x)");
2487 code[11] = c - NFA_MCLOSE + '0';
2488 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002489#ifdef FEAT_SYN_HL
2490 case NFA_ZOPEN:
2491 case NFA_ZOPEN1:
2492 case NFA_ZOPEN2:
2493 case NFA_ZOPEN3:
2494 case NFA_ZOPEN4:
2495 case NFA_ZOPEN5:
2496 case NFA_ZOPEN6:
2497 case NFA_ZOPEN7:
2498 case NFA_ZOPEN8:
2499 case NFA_ZOPEN9:
2500 STRCPY(code, "NFA_ZOPEN(x)");
2501 code[10] = c - NFA_ZOPEN + '0';
2502 break;
2503 case NFA_ZCLOSE:
2504 case NFA_ZCLOSE1:
2505 case NFA_ZCLOSE2:
2506 case NFA_ZCLOSE3:
2507 case NFA_ZCLOSE4:
2508 case NFA_ZCLOSE5:
2509 case NFA_ZCLOSE6:
2510 case NFA_ZCLOSE7:
2511 case NFA_ZCLOSE8:
2512 case NFA_ZCLOSE9:
2513 STRCPY(code, "NFA_ZCLOSE(x)");
2514 code[11] = c - NFA_ZCLOSE + '0';
2515 break;
2516#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002517 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2518 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2519 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2520 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002521 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2522 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002523 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2524 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2525 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2526 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2527 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2528 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2529 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2530 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2531 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2532 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2533 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2534 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2535 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2536 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002537 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002538
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002540 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2541 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2542 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002543 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002545
2546 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2547 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2548 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2549 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2550 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2551 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2552 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2553
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002554 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2555 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2556 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2557 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2558 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2559 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2560 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2561 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2562 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2563 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2564 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2565 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2566 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2567 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2568 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2569 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002570 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2571 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2572 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002573
2574 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2575 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2576 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2577 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2578 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2579 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2580 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2581 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2582 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2583 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2584 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2585 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2586 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2587 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2588 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2589 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2590 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2591 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2592 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2593 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2594 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2595 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2596 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2597 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2598 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2599 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2600 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002601 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2602 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2603 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2604 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002605
2606 default:
2607 STRCPY(code, "CHAR(x)");
2608 code[5] = c;
2609 }
2610
2611 if (addnl == TRUE)
2612 STRCAT(code, " + NEWLINE ");
2613
2614}
2615
2616#ifdef ENABLE_LOG
2617static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002618static 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 +02002619
2620/*
2621 * Print the postfix notation of the current regexp.
2622 */
2623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002624nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002625{
2626 int *p;
2627 FILE *f;
2628
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002629 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002630 if (f != NULL)
2631 {
2632 fprintf(f, "\n-------------------------\n");
2633 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002634 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635 else if (retval == OK)
2636 fprintf(f, ">>> NFA engine succeeded !\n");
2637 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002638 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002639 {
2640 nfa_set_code(*p);
2641 fprintf(f, "%s, ", code);
2642 }
2643 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002644 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 fprintf(f, "%d ", *p);
2646 fprintf(f, "\n\n");
2647 fclose(f);
2648 }
2649}
2650
2651/*
2652 * Print the NFA starting with a root node "state".
2653 */
2654 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002655nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002656{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002657 garray_T indent;
2658
2659 ga_init2(&indent, 1, 64);
2660 ga_append(&indent, '\0');
2661 nfa_print_state2(debugf, state, &indent);
2662 ga_clear(&indent);
2663}
2664
2665 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002666nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002667{
2668 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002669
2670 if (state == NULL)
2671 return;
2672
2673 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002674
2675 /* Output indent */
2676 p = (char_u *)indent->ga_data;
2677 if (indent->ga_len >= 3)
2678 {
2679 int last = indent->ga_len - 3;
2680 char_u save[2];
2681
2682 STRNCPY(save, &p[last], 2);
2683 STRNCPY(&p[last], "+-", 2);
2684 fprintf(debugf, " %s", p);
2685 STRNCPY(&p[last], save, 2);
2686 }
2687 else
2688 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002689
2690 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002691 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002692 code,
2693 state->c,
2694 abs(state->id),
2695 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696 if (state->id < 0)
2697 return;
2698
2699 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002700
2701 /* grow indent for state->out */
2702 indent->ga_len -= 1;
2703 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002704 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002705 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002706 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002707 ga_append(indent, '\0');
2708
2709 nfa_print_state2(debugf, state->out, indent);
2710
2711 /* replace last part of indent for state->out1 */
2712 indent->ga_len -= 3;
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->out1, indent);
2717
2718 /* shrink indent */
2719 indent->ga_len -= 3;
2720 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002721}
2722
2723/*
2724 * Print the NFA state machine.
2725 */
2726 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002727nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002729 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730
2731 if (debugf != NULL)
2732 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002733 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002734
Bram Moolenaar473de612013-06-08 18:19:48 +02002735 if (prog->reganch)
2736 fprintf(debugf, "reganch: %d\n", prog->reganch);
2737 if (prog->regstart != NUL)
2738 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2739 prog->regstart, prog->regstart);
2740 if (prog->match_text != NULL)
2741 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002742
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002743 fclose(debugf);
2744 }
2745}
2746#endif /* ENABLE_LOG */
2747#endif /* DEBUG */
2748
2749/*
2750 * Parse r.e. @expr and convert it into postfix form.
2751 * Return the postfix string on success, NULL otherwise.
2752 */
2753 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002754re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755{
2756 if (nfa_reg(REG_NOPAREN) == FAIL)
2757 return NULL;
2758 EMIT(NFA_MOPEN);
2759 return post_start;
2760}
2761
2762/* NB. Some of the code below is inspired by Russ's. */
2763
2764/*
2765 * Represents an NFA state plus zero or one or two arrows exiting.
2766 * if c == MATCH, no arrows out; matching state.
2767 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2768 * If c < 256, labeled arrow with character c to out.
2769 */
2770
2771static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2772
2773/*
2774 * Allocate and initialize nfa_state_T.
2775 */
2776 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002777alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002778{
2779 nfa_state_T *s;
2780
2781 if (istate >= nstate)
2782 return NULL;
2783
2784 s = &state_ptr[istate++];
2785
2786 s->c = c;
2787 s->out = out;
2788 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002789 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002790
2791 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002792 s->lastlist[0] = 0;
2793 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002794
2795 return s;
2796}
2797
2798/*
2799 * A partially built NFA without the matching state filled in.
2800 * Frag_T.start points at the start state.
2801 * Frag_T.out is a list of places that need to be set to the
2802 * next state for this fragment.
2803 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002804
2805/* Since the out pointers in the list are always
2806 * uninitialized, we use the pointers themselves
2807 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002808typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002809union Ptrlist
2810{
2811 Ptrlist *next;
2812 nfa_state_T *s;
2813};
2814
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002815struct Frag
2816{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002817 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002818 Ptrlist *out;
2819};
2820typedef struct Frag Frag_T;
2821
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002823 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002824 */
2825 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002826frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002828 Frag_T n;
2829
2830 n.start = start;
2831 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002832 return n;
2833}
2834
2835/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836 * Create singleton list containing just outp.
2837 */
2838 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002839list1(
2840 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002841{
2842 Ptrlist *l;
2843
2844 l = (Ptrlist *)outp;
2845 l->next = NULL;
2846 return l;
2847}
2848
2849/*
2850 * Patch the list of states at out to point to start.
2851 */
2852 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002853patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854{
2855 Ptrlist *next;
2856
2857 for (; l; l = next)
2858 {
2859 next = l->next;
2860 l->s = s;
2861 }
2862}
2863
2864
2865/*
2866 * Join the two lists l1 and l2, returning the combination.
2867 */
2868 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002869append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002870{
2871 Ptrlist *oldl1;
2872
2873 oldl1 = l1;
2874 while (l1->next)
2875 l1 = l1->next;
2876 l1->next = l2;
2877 return oldl1;
2878}
2879
2880/*
2881 * Stack used for transforming postfix form into NFA.
2882 */
2883static Frag_T empty;
2884
2885 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002886st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002887{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002888#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889 FILE *df;
2890 int *p2;
2891
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002892 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 if (df)
2894 {
2895 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002896# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002898# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002900# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 for (p2 = postfix; p2 < end; p2++)
2902 {
2903 nfa_set_code(*p2);
2904 fprintf(df, "%s, ", code);
2905 }
2906 nfa_set_code(*p);
2907 fprintf(df, "\nCurrent position is: ");
2908 for (p2 = postfix; p2 <= p; p2 ++)
2909 {
2910 nfa_set_code(*p2);
2911 fprintf(df, "%s, ", code);
2912 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002913# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914 for (p2 = postfix; p2 < end; p2++)
2915 {
2916 fprintf(df, "%d, ", *p2);
2917 }
2918 fprintf(df, "\nCurrent position is: ");
2919 for (p2 = postfix; p2 <= p; p2 ++)
2920 {
2921 fprintf(df, "%d, ", *p2);
2922 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002923# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002924 fprintf(df, "\n--------------------------\n");
2925 fclose(df);
2926 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002927#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002928 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002929}
2930
2931/*
2932 * Push an item onto the stack.
2933 */
2934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002935st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936{
2937 Frag_T *stackp = *p;
2938
2939 if (stackp >= stack_end)
2940 return;
2941 *stackp = s;
2942 *p = *p + 1;
2943}
2944
2945/*
2946 * Pop an item from the stack.
2947 */
2948 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002949st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950{
2951 Frag_T *stackp;
2952
2953 *p = *p - 1;
2954 stackp = *p;
2955 if (stackp < stack)
2956 return empty;
2957 return **p;
2958}
2959
2960/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002961 * Estimate the maximum byte length of anything matching "state".
2962 * When unknown or unlimited return -1.
2963 */
2964 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002965nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002966{
2967 int l, r;
2968 nfa_state_T *state = startstate;
2969 int len = 0;
2970
2971 /* detect looping in a NFA_SPLIT */
2972 if (depth > 4)
2973 return -1;
2974
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002975 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002976 {
2977 switch (state->c)
2978 {
2979 case NFA_END_INVISIBLE:
2980 case NFA_END_INVISIBLE_NEG:
2981 /* the end, return what we have */
2982 return len;
2983
2984 case NFA_SPLIT:
2985 /* two alternatives, use the maximum */
2986 l = nfa_max_width(state->out, depth + 1);
2987 r = nfa_max_width(state->out1, depth + 1);
2988 if (l < 0 || r < 0)
2989 return -1;
2990 return len + (l > r ? l : r);
2991
2992 case NFA_ANY:
2993 case NFA_START_COLL:
2994 case NFA_START_NEG_COLL:
2995 /* matches some character, including composing chars */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002996 if (enc_utf8)
2997 len += MB_MAXBYTES;
2998 else if (has_mbyte)
2999 len += 2;
3000 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003001 ++len;
3002 if (state->c != NFA_ANY)
3003 {
3004 /* skip over the characters */
3005 state = state->out1->out;
3006 continue;
3007 }
3008 break;
3009
3010 case NFA_DIGIT:
3011 case NFA_WHITE:
3012 case NFA_HEX:
3013 case NFA_OCTAL:
3014 /* ascii */
3015 ++len;
3016 break;
3017
3018 case NFA_IDENT:
3019 case NFA_SIDENT:
3020 case NFA_KWORD:
3021 case NFA_SKWORD:
3022 case NFA_FNAME:
3023 case NFA_SFNAME:
3024 case NFA_PRINT:
3025 case NFA_SPRINT:
3026 case NFA_NWHITE:
3027 case NFA_NDIGIT:
3028 case NFA_NHEX:
3029 case NFA_NOCTAL:
3030 case NFA_WORD:
3031 case NFA_NWORD:
3032 case NFA_HEAD:
3033 case NFA_NHEAD:
3034 case NFA_ALPHA:
3035 case NFA_NALPHA:
3036 case NFA_LOWER:
3037 case NFA_NLOWER:
3038 case NFA_UPPER:
3039 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003040 case NFA_LOWER_IC:
3041 case NFA_NLOWER_IC:
3042 case NFA_UPPER_IC:
3043 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003044 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003045 /* possibly non-ascii */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003046 if (has_mbyte)
3047 len += 3;
3048 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003049 ++len;
3050 break;
3051
3052 case NFA_START_INVISIBLE:
3053 case NFA_START_INVISIBLE_NEG:
3054 case NFA_START_INVISIBLE_BEFORE:
3055 case NFA_START_INVISIBLE_BEFORE_NEG:
3056 /* zero-width, out1 points to the END state */
3057 state = state->out1->out;
3058 continue;
3059
3060 case NFA_BACKREF1:
3061 case NFA_BACKREF2:
3062 case NFA_BACKREF3:
3063 case NFA_BACKREF4:
3064 case NFA_BACKREF5:
3065 case NFA_BACKREF6:
3066 case NFA_BACKREF7:
3067 case NFA_BACKREF8:
3068 case NFA_BACKREF9:
3069#ifdef FEAT_SYN_HL
3070 case NFA_ZREF1:
3071 case NFA_ZREF2:
3072 case NFA_ZREF3:
3073 case NFA_ZREF4:
3074 case NFA_ZREF5:
3075 case NFA_ZREF6:
3076 case NFA_ZREF7:
3077 case NFA_ZREF8:
3078 case NFA_ZREF9:
3079#endif
3080 case NFA_NEWL:
3081 case NFA_SKIP:
3082 /* unknown width */
3083 return -1;
3084
3085 case NFA_BOL:
3086 case NFA_EOL:
3087 case NFA_BOF:
3088 case NFA_EOF:
3089 case NFA_BOW:
3090 case NFA_EOW:
3091 case NFA_MOPEN:
3092 case NFA_MOPEN1:
3093 case NFA_MOPEN2:
3094 case NFA_MOPEN3:
3095 case NFA_MOPEN4:
3096 case NFA_MOPEN5:
3097 case NFA_MOPEN6:
3098 case NFA_MOPEN7:
3099 case NFA_MOPEN8:
3100 case NFA_MOPEN9:
3101#ifdef FEAT_SYN_HL
3102 case NFA_ZOPEN:
3103 case NFA_ZOPEN1:
3104 case NFA_ZOPEN2:
3105 case NFA_ZOPEN3:
3106 case NFA_ZOPEN4:
3107 case NFA_ZOPEN5:
3108 case NFA_ZOPEN6:
3109 case NFA_ZOPEN7:
3110 case NFA_ZOPEN8:
3111 case NFA_ZOPEN9:
3112 case NFA_ZCLOSE:
3113 case NFA_ZCLOSE1:
3114 case NFA_ZCLOSE2:
3115 case NFA_ZCLOSE3:
3116 case NFA_ZCLOSE4:
3117 case NFA_ZCLOSE5:
3118 case NFA_ZCLOSE6:
3119 case NFA_ZCLOSE7:
3120 case NFA_ZCLOSE8:
3121 case NFA_ZCLOSE9:
3122#endif
3123 case NFA_MCLOSE:
3124 case NFA_MCLOSE1:
3125 case NFA_MCLOSE2:
3126 case NFA_MCLOSE3:
3127 case NFA_MCLOSE4:
3128 case NFA_MCLOSE5:
3129 case NFA_MCLOSE6:
3130 case NFA_MCLOSE7:
3131 case NFA_MCLOSE8:
3132 case NFA_MCLOSE9:
3133 case NFA_NOPEN:
3134 case NFA_NCLOSE:
3135
3136 case NFA_LNUM_GT:
3137 case NFA_LNUM_LT:
3138 case NFA_COL_GT:
3139 case NFA_COL_LT:
3140 case NFA_VCOL_GT:
3141 case NFA_VCOL_LT:
3142 case NFA_MARK_GT:
3143 case NFA_MARK_LT:
3144 case NFA_VISUAL:
3145 case NFA_LNUM:
3146 case NFA_CURSOR:
3147 case NFA_COL:
3148 case NFA_VCOL:
3149 case NFA_MARK:
3150
3151 case NFA_ZSTART:
3152 case NFA_ZEND:
3153 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003154 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003155 case NFA_START_PATTERN:
3156 case NFA_END_PATTERN:
3157 case NFA_COMPOSING:
3158 case NFA_END_COMPOSING:
3159 /* zero-width */
3160 break;
3161
3162 default:
3163 if (state->c < 0)
3164 /* don't know what this is */
3165 return -1;
3166 /* normal character */
3167 len += MB_CHAR2LEN(state->c);
3168 break;
3169 }
3170
3171 /* normal way to continue */
3172 state = state->out;
3173 }
3174
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003175 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003176 return -1;
3177}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003178
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003179/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 * Convert a postfix form into its equivalent NFA.
3181 * Return the NFA start state on success, NULL otherwise.
3182 */
3183 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003184post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003185{
3186 int *p;
3187 int mopen;
3188 int mclose;
3189 Frag_T *stack = NULL;
3190 Frag_T *stackp = NULL;
3191 Frag_T *stack_end = NULL;
3192 Frag_T e1;
3193 Frag_T e2;
3194 Frag_T e;
3195 nfa_state_T *s;
3196 nfa_state_T *s1;
3197 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003198 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003199
3200 if (postfix == NULL)
3201 return NULL;
3202
Bram Moolenaar053bb602013-05-20 13:55:21 +02003203#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204#define POP() st_pop(&stackp, stack); \
3205 if (stackp < stack) \
3206 { \
3207 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003208 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 return NULL; \
3210 }
3211
3212 if (nfa_calc_size == FALSE)
3213 {
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003214 // Allocate space for the stack. Max states on the stack: "nstate'.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003215 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003216 if (stack == NULL)
3217 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003219 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 }
3221
3222 for (p = postfix; p < end; ++p)
3223 {
3224 switch (*p)
3225 {
3226 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003227 /* Concatenation.
3228 * Pay attention: this operator does not exist in the r.e. itself
3229 * (it is implicit, really). It is added when r.e. is translated
3230 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003231 if (nfa_calc_size == TRUE)
3232 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003233 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 break;
3235 }
3236 e2 = POP();
3237 e1 = POP();
3238 patch(e1.out, e2.start);
3239 PUSH(frag(e1.start, e2.out));
3240 break;
3241
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003242 case NFA_OR:
3243 /* Alternation */
3244 if (nfa_calc_size == TRUE)
3245 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003246 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 break;
3248 }
3249 e2 = POP();
3250 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003251 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003253 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 PUSH(frag(s, append(e1.out, e2.out)));
3255 break;
3256
3257 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003258 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 if (nfa_calc_size == TRUE)
3260 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003261 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262 break;
3263 }
3264 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003265 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003267 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 patch(e.out, s);
3269 PUSH(frag(s, list1(&s->out1)));
3270 break;
3271
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003272 case NFA_STAR_NONGREEDY:
3273 /* Zero or more, prefer zero */
3274 if (nfa_calc_size == TRUE)
3275 {
3276 nstate++;
3277 break;
3278 }
3279 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003280 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003281 if (s == NULL)
3282 goto theend;
3283 patch(e.out, s);
3284 PUSH(frag(s, list1(&s->out)));
3285 break;
3286
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003287 case NFA_QUEST:
3288 /* one or zero atoms=> greedy match */
3289 if (nfa_calc_size == TRUE)
3290 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003291 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 break;
3293 }
3294 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003295 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003297 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 PUSH(frag(s, append(e.out, list1(&s->out1))));
3299 break;
3300
3301 case NFA_QUEST_NONGREEDY:
3302 /* zero or one atoms => non-greedy match */
3303 if (nfa_calc_size == TRUE)
3304 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003305 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 break;
3307 }
3308 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003309 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003311 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 PUSH(frag(s, append(e.out, list1(&s->out))));
3313 break;
3314
Bram Moolenaar417bad22013-06-07 14:08:30 +02003315 case NFA_END_COLL:
3316 case NFA_END_NEG_COLL:
3317 /* On the stack is the sequence starting with NFA_START_COLL or
3318 * NFA_START_NEG_COLL and all possible characters. Patch it to
3319 * add the output to the start. */
3320 if (nfa_calc_size == TRUE)
3321 {
3322 nstate++;
3323 break;
3324 }
3325 e = POP();
3326 s = alloc_state(NFA_END_COLL, NULL, NULL);
3327 if (s == NULL)
3328 goto theend;
3329 patch(e.out, s);
3330 e.start->out1 = s;
3331 PUSH(frag(e.start, list1(&s->out)));
3332 break;
3333
3334 case NFA_RANGE:
3335 /* Before this are two characters, the low and high end of a
3336 * range. Turn them into two states with MIN and MAX. */
3337 if (nfa_calc_size == TRUE)
3338 {
3339 /* nstate += 0; */
3340 break;
3341 }
3342 e2 = POP();
3343 e1 = POP();
3344 e2.start->val = e2.start->c;
3345 e2.start->c = NFA_RANGE_MAX;
3346 e1.start->val = e1.start->c;
3347 e1.start->c = NFA_RANGE_MIN;
3348 patch(e1.out, e2.start);
3349 PUSH(frag(e1.start, e2.out));
3350 break;
3351
Bram Moolenaar699c1202013-09-25 16:41:54 +02003352 case NFA_EMPTY:
3353 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 if (nfa_calc_size == TRUE)
3355 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003356 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 break;
3358 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003359 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003361 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 PUSH(frag(s, list1(&s->out)));
3363 break;
3364
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003365 case NFA_OPT_CHARS:
3366 {
3367 int n;
3368
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003369 /* \%[abc] implemented as:
3370 * NFA_SPLIT
3371 * +-CHAR(a)
3372 * | +-NFA_SPLIT
3373 * | +-CHAR(b)
3374 * | | +-NFA_SPLIT
3375 * | | +-CHAR(c)
3376 * | | | +-next
3377 * | | +- next
3378 * | +- next
3379 * +- next
3380 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003381 n = *++p; /* get number of characters */
3382 if (nfa_calc_size == TRUE)
3383 {
3384 nstate += n;
3385 break;
3386 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003387 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003388 e1.out = NULL; /* stores list with out1's */
3389 s1 = NULL; /* previous NFA_SPLIT to connect to */
3390 while (n-- > 0)
3391 {
3392 e = POP(); /* get character */
3393 s = alloc_state(NFA_SPLIT, e.start, NULL);
3394 if (s == NULL)
3395 goto theend;
3396 if (e1.out == NULL)
3397 e1 = e;
3398 patch(e.out, s1);
3399 append(e1.out, list1(&s->out1));
3400 s1 = s;
3401 }
3402 PUSH(frag(s, e1.out));
3403 break;
3404 }
3405
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003407 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003408 case NFA_PREV_ATOM_JUST_BEFORE:
3409 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003410 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003411 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003412 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3413 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003414 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003415 int start_state;
3416 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003417 int n = 0;
3418 nfa_state_T *zend;
3419 nfa_state_T *skip;
3420
Bram Moolenaardecd9542013-06-07 16:31:50 +02003421 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003422 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003423 case NFA_PREV_ATOM_NO_WIDTH:
3424 start_state = NFA_START_INVISIBLE;
3425 end_state = NFA_END_INVISIBLE;
3426 break;
3427 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3428 start_state = NFA_START_INVISIBLE_NEG;
3429 end_state = NFA_END_INVISIBLE_NEG;
3430 break;
3431 case NFA_PREV_ATOM_JUST_BEFORE:
3432 start_state = NFA_START_INVISIBLE_BEFORE;
3433 end_state = NFA_END_INVISIBLE;
3434 break;
3435 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3436 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3437 end_state = NFA_END_INVISIBLE_NEG;
3438 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003439 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003440 start_state = NFA_START_PATTERN;
3441 end_state = NFA_END_PATTERN;
3442 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003443 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003444
3445 if (before)
3446 n = *++p; /* get the count */
3447
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003448 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003449 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003450 * The \@<= operator: match for the preceding atom.
3451 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003452 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003453 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003454
3455 if (nfa_calc_size == TRUE)
3456 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003457 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003458 break;
3459 }
3460 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003461 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003463 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464
Bram Moolenaar87953742013-06-05 18:52:40 +02003465 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003467 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003468 if (pattern)
3469 {
3470 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3471 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003472 if (skip == NULL)
3473 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003474 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003475 if (zend == NULL)
3476 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003477 s1->out= skip;
3478 patch(e.out, zend);
3479 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003480 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003481 else
3482 {
3483 patch(e.out, s1);
3484 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003485 if (before)
3486 {
3487 if (n <= 0)
3488 /* See if we can guess the maximum width, it avoids a
3489 * lot of pointless tries. */
3490 n = nfa_max_width(e.start, 0);
3491 s->val = n; /* store the count */
3492 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003493 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003495 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003497 case NFA_COMPOSING: /* char with composing char */
3498#if 0
3499 /* TODO */
3500 if (regflags & RF_ICOMBINE)
3501 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003502 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003503 }
3504#endif
3505 /* FALLTHROUGH */
3506
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003507 case NFA_MOPEN: /* \( \) Submatch */
3508 case NFA_MOPEN1:
3509 case NFA_MOPEN2:
3510 case NFA_MOPEN3:
3511 case NFA_MOPEN4:
3512 case NFA_MOPEN5:
3513 case NFA_MOPEN6:
3514 case NFA_MOPEN7:
3515 case NFA_MOPEN8:
3516 case NFA_MOPEN9:
3517#ifdef FEAT_SYN_HL
3518 case NFA_ZOPEN: /* \z( \) Submatch */
3519 case NFA_ZOPEN1:
3520 case NFA_ZOPEN2:
3521 case NFA_ZOPEN3:
3522 case NFA_ZOPEN4:
3523 case NFA_ZOPEN5:
3524 case NFA_ZOPEN6:
3525 case NFA_ZOPEN7:
3526 case NFA_ZOPEN8:
3527 case NFA_ZOPEN9:
3528#endif
3529 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530 if (nfa_calc_size == TRUE)
3531 {
3532 nstate += 2;
3533 break;
3534 }
3535
3536 mopen = *p;
3537 switch (*p)
3538 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003539 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3540#ifdef FEAT_SYN_HL
3541 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3542 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3543 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3544 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3545 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3546 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3547 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3548 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3549 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3550 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3551#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003552 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003553 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003554 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003555 mclose = *p + NSUBEXP;
3556 break;
3557 }
3558
3559 /* Allow "NFA_MOPEN" as a valid postfix representation for
3560 * the empty regexp "". In this case, the NFA will be
3561 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3562 * empty groups of parenthesis, and empty mbyte chars */
3563 if (stackp == stack)
3564 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003565 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003566 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003567 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003568 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003569 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003570 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 patch(list1(&s->out), s1);
3572 PUSH(frag(s, list1(&s1->out)));
3573 break;
3574 }
3575
3576 /* At least one node was emitted before NFA_MOPEN, so
3577 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3578 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003579 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003580 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003581 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003582
Bram Moolenaar525666f2013-06-02 16:40:55 +02003583 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003584 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003585 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 patch(e.out, s1);
3587
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003588 if (mopen == NFA_COMPOSING)
3589 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003590 patch(list1(&s->out1), s1);
3591
3592 PUSH(frag(s, list1(&s1->out)));
3593 break;
3594
Bram Moolenaar5714b802013-05-28 22:03:20 +02003595 case NFA_BACKREF1:
3596 case NFA_BACKREF2:
3597 case NFA_BACKREF3:
3598 case NFA_BACKREF4:
3599 case NFA_BACKREF5:
3600 case NFA_BACKREF6:
3601 case NFA_BACKREF7:
3602 case NFA_BACKREF8:
3603 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003604#ifdef FEAT_SYN_HL
3605 case NFA_ZREF1:
3606 case NFA_ZREF2:
3607 case NFA_ZREF3:
3608 case NFA_ZREF4:
3609 case NFA_ZREF5:
3610 case NFA_ZREF6:
3611 case NFA_ZREF7:
3612 case NFA_ZREF8:
3613 case NFA_ZREF9:
3614#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003615 if (nfa_calc_size == TRUE)
3616 {
3617 nstate += 2;
3618 break;
3619 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003620 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003621 if (s == NULL)
3622 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003623 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003624 if (s1 == NULL)
3625 goto theend;
3626 patch(list1(&s->out), s1);
3627 PUSH(frag(s, list1(&s1->out)));
3628 break;
3629
Bram Moolenaar423532e2013-05-29 21:14:42 +02003630 case NFA_LNUM:
3631 case NFA_LNUM_GT:
3632 case NFA_LNUM_LT:
3633 case NFA_VCOL:
3634 case NFA_VCOL_GT:
3635 case NFA_VCOL_LT:
3636 case NFA_COL:
3637 case NFA_COL_GT:
3638 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003639 case NFA_MARK:
3640 case NFA_MARK_GT:
3641 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003642 {
3643 int n = *++p; /* lnum, col or mark name */
3644
Bram Moolenaar423532e2013-05-29 21:14:42 +02003645 if (nfa_calc_size == TRUE)
3646 {
3647 nstate += 1;
3648 break;
3649 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003650 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003651 if (s == NULL)
3652 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003653 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003654 PUSH(frag(s, list1(&s->out)));
3655 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003656 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003657
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003658 case NFA_ZSTART:
3659 case NFA_ZEND:
3660 default:
3661 /* Operands */
3662 if (nfa_calc_size == TRUE)
3663 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003664 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003665 break;
3666 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003667 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003668 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003669 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003670 PUSH(frag(s, list1(&s->out)));
3671 break;
3672
3673 } /* switch(*p) */
3674
3675 } /* for(p = postfix; *p; ++p) */
3676
3677 if (nfa_calc_size == TRUE)
3678 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003679 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003680 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003681 }
3682
3683 e = POP();
3684 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003685 {
3686 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003687 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 +02003688 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689
3690 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003691 {
3692 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003693 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003694 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696 matchstate = &state_ptr[istate++]; /* the match state */
3697 matchstate->c = NFA_MATCH;
3698 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003699 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700
3701 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003702 ret = e.start;
3703
3704theend:
3705 vim_free(stack);
3706 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003707
3708#undef POP1
3709#undef PUSH1
3710#undef POP2
3711#undef PUSH2
3712#undef POP
3713#undef PUSH
3714}
3715
Bram Moolenaara2947e22013-06-11 22:44:09 +02003716/*
3717 * After building the NFA program, inspect it to add optimization hints.
3718 */
3719 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003720nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003721{
3722 int i;
3723 int c;
3724
3725 for (i = 0; i < prog->nstate; ++i)
3726 {
3727 c = prog->state[i].c;
3728 if (c == NFA_START_INVISIBLE
3729 || c == NFA_START_INVISIBLE_NEG
3730 || c == NFA_START_INVISIBLE_BEFORE
3731 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3732 {
3733 int directly;
3734
3735 /* Do it directly when what follows is possibly the end of the
3736 * match. */
3737 if (match_follows(prog->state[i].out1->out, 0))
3738 directly = TRUE;
3739 else
3740 {
3741 int ch_invisible = failure_chance(prog->state[i].out, 0);
3742 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3743
3744 /* Postpone when the invisible match is expensive or has a
3745 * lower chance of failing. */
3746 if (c == NFA_START_INVISIBLE_BEFORE
3747 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3748 {
3749 /* "before" matches are very expensive when
3750 * unbounded, always prefer what follows then,
3751 * unless what follows will always match.
3752 * Otherwise strongly prefer what follows. */
3753 if (prog->state[i].val <= 0 && ch_follows > 0)
3754 directly = FALSE;
3755 else
3756 directly = ch_follows * 10 < ch_invisible;
3757 }
3758 else
3759 {
3760 /* normal invisible, first do the one with the
3761 * highest failure chance */
3762 directly = ch_follows < ch_invisible;
3763 }
3764 }
3765 if (directly)
3766 /* switch to the _FIRST state */
3767 ++prog->state[i].c;
3768 }
3769 }
3770}
3771
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003772/****************************************************************
3773 * NFA execution code.
3774 ****************************************************************/
3775
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003776typedef struct
3777{
3778 int in_use; /* number of subexpr with useful info */
3779
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003780 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003781 union
3782 {
3783 struct multipos
3784 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003785 linenr_T start_lnum;
3786 linenr_T end_lnum;
3787 colnr_T start_col;
3788 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003789 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003790 struct linepos
3791 {
3792 char_u *start;
3793 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003794 } line[NSUBEXP];
3795 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003796} regsub_T;
3797
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003798typedef struct
3799{
3800 regsub_T norm; /* \( .. \) matches */
3801#ifdef FEAT_SYN_HL
3802 regsub_T synt; /* \z( .. \) matches */
3803#endif
3804} regsubs_T;
3805
Bram Moolenaara2d95102013-06-04 14:23:05 +02003806/* nfa_pim_T stores a Postponed Invisible Match. */
3807typedef struct nfa_pim_S nfa_pim_T;
3808struct nfa_pim_S
3809{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003810 int result; /* NFA_PIM_*, see below */
3811 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003812 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003813 union
3814 {
3815 lpos_T pos;
3816 char_u *ptr;
3817 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003818};
3819
3820/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003821#define NFA_PIM_UNUSED 0 /* pim not used */
3822#define NFA_PIM_TODO 1 /* pim not done yet */
3823#define NFA_PIM_MATCH 2 /* pim executed, matches */
3824#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003825
3826
Bram Moolenaar963fee22013-05-26 21:47:28 +02003827/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003828typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003829{
3830 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003831 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003832 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3833 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003834 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003835} nfa_thread_T;
3836
Bram Moolenaar963fee22013-05-26 21:47:28 +02003837/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003838typedef struct
3839{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003840 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003841 int n; /* nr of states currently in "t" */
3842 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003843 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003844 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003845} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846
Bram Moolenaar5714b802013-05-28 22:03:20 +02003847#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003848static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003849
3850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003851log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003852{
3853 log_subexpr(&subs->norm);
3854# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003855 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003856 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003857# endif
3858}
3859
Bram Moolenaar5714b802013-05-28 22:03:20 +02003860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003861log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003862{
3863 int j;
3864
3865 for (j = 0; j < sub->in_use; j++)
3866 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003867 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003868 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003869 sub->list.multi[j].start_col,
3870 (int)sub->list.multi[j].start_lnum,
3871 sub->list.multi[j].end_col,
3872 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003873 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003874 {
3875 char *s = (char *)sub->list.line[j].start;
3876 char *e = (char *)sub->list.line[j].end;
3877
Bram Moolenaar87953742013-06-05 18:52:40 +02003878 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003879 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003880 s == NULL ? "NULL" : s,
3881 e == NULL ? "NULL" : e);
3882 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003883}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003884
3885 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003886pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003887{
3888 static char buf[30];
3889
3890 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3891 buf[0] = NUL;
3892 else
3893 {
3894 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003895 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003896 }
3897 return buf;
3898}
3899
Bram Moolenaar5714b802013-05-28 22:03:20 +02003900#endif
3901
Bram Moolenaar963fee22013-05-26 21:47:28 +02003902/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003903static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003904#ifdef FEAT_RELTIME
3905static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003906static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003907static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003908#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003909
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003910static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003911static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003912
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003913/*
3914 * Copy postponed invisible match info from "from" to "to".
3915 */
3916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003917copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003918{
3919 to->result = from->result;
3920 to->state = from->state;
3921 copy_sub(&to->subs.norm, &from->subs.norm);
3922#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003923 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003924 copy_sub(&to->subs.synt, &from->subs.synt);
3925#endif
3926 to->end = from->end;
3927}
3928
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003930clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003931{
3932 if (REG_MULTI)
3933 /* Use 0xff to set lnum to -1 */
3934 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003935 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003936 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003937 vim_memset(sub->list.line, 0,
3938 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003939 sub->in_use = 0;
3940}
3941
3942/*
3943 * Copy the submatches from "from" to "to".
3944 */
3945 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003946copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003947{
3948 to->in_use = from->in_use;
3949 if (from->in_use > 0)
3950 {
3951 /* Copy the match start and end positions. */
3952 if (REG_MULTI)
3953 mch_memmove(&to->list.multi[0],
3954 &from->list.multi[0],
3955 sizeof(struct multipos) * from->in_use);
3956 else
3957 mch_memmove(&to->list.line[0],
3958 &from->list.line[0],
3959 sizeof(struct linepos) * from->in_use);
3960 }
3961}
3962
3963/*
3964 * Like copy_sub() but exclude the main match.
3965 */
3966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003967copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003968{
3969 if (to->in_use < from->in_use)
3970 to->in_use = from->in_use;
3971 if (from->in_use > 1)
3972 {
3973 /* Copy the match start and end positions. */
3974 if (REG_MULTI)
3975 mch_memmove(&to->list.multi[1],
3976 &from->list.multi[1],
3977 sizeof(struct multipos) * (from->in_use - 1));
3978 else
3979 mch_memmove(&to->list.line[1],
3980 &from->list.line[1],
3981 sizeof(struct linepos) * (from->in_use - 1));
3982 }
3983}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003984
Bram Moolenaar428e9872013-05-30 17:05:39 +02003985/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003986 * Like copy_sub() but only do the end of the main match if \ze is present.
3987 */
3988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003989copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003990{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003991 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003992 {
3993 if (REG_MULTI)
3994 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003995 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003996 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003997 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3998 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003999 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004000 }
4001 else
4002 {
4003 if (from->list.line[0].end != NULL)
4004 to->list.line[0].end = from->list.line[0].end;
4005 }
4006 }
4007}
4008
4009/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004010 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004011 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004012 */
4013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004014sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004015{
4016 int i;
4017 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004018 linenr_T s1;
4019 linenr_T s2;
4020 char_u *sp1;
4021 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004022
4023 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4024 if (REG_MULTI)
4025 {
4026 for (i = 0; i < todo; ++i)
4027 {
4028 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004029 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004030 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004031 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004032 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004033 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004034 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004035 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004036 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004038 if (s1 != -1 && sub1->list.multi[i].start_col
4039 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004040 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004041
Bram Moolenaar0270f382018-07-17 05:43:58 +02004042 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004043 {
4044 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004045 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004046 else
4047 s1 = -1;
4048 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004049 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004050 else
4051 s2 = -1;
4052 if (s1 != s2)
4053 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004054 if (s1 != -1 && sub1->list.multi[i].end_col
4055 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004056 return FALSE;
4057 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004058 }
4059 }
4060 else
4061 {
4062 for (i = 0; i < todo; ++i)
4063 {
4064 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004065 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004066 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004067 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004068 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004069 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004070 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004071 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004072 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004073 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004074 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004075 {
4076 if (i < sub1->in_use)
4077 sp1 = sub1->list.line[i].end;
4078 else
4079 sp1 = NULL;
4080 if (i < sub2->in_use)
4081 sp2 = sub2->list.line[i].end;
4082 else
4083 sp2 = NULL;
4084 if (sp1 != sp2)
4085 return FALSE;
4086 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004087 }
4088 }
4089
4090 return TRUE;
4091}
4092
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004093#ifdef ENABLE_LOG
4094 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004095report_state(char *action,
4096 regsub_T *sub,
4097 nfa_state_T *state,
4098 int lid,
4099 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004100{
4101 int col;
4102
4103 if (sub->in_use <= 0)
4104 col = -1;
4105 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004106 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004107 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004108 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004109 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004110 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4111 action, abs(state->id), lid, state->c, code, col,
4112 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004113}
4114#endif
4115
Bram Moolenaar43e02982013-06-07 17:31:29 +02004116/*
4117 * Return TRUE if the same state is already in list "l" with the same
4118 * positions as "subs".
4119 */
4120 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004121has_state_with_pos(
4122 nfa_list_T *l, /* runtime state list */
4123 nfa_state_T *state, /* state to update */
4124 regsubs_T *subs, /* pointers to subexpressions */
4125 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004126{
4127 nfa_thread_T *thread;
4128 int i;
4129
4130 for (i = 0; i < l->n; ++i)
4131 {
4132 thread = &l->t[i];
4133 if (thread->state->id == state->id
4134 && sub_equal(&thread->subs.norm, &subs->norm)
4135#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004136 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004137 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004138#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004139 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004140 return TRUE;
4141 }
4142 return FALSE;
4143}
4144
4145/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004146 * Return TRUE if "one" and "two" are equal. That includes when both are not
4147 * set.
4148 */
4149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004150pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004151{
4152 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4153 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4154
4155 if (one_unused)
4156 /* one is unused: equal when two is also unused */
4157 return two_unused;
4158 if (two_unused)
4159 /* one is used and two is not: not equal */
4160 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004161 /* compare the state id */
4162 if (one->state->id != two->state->id)
4163 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004164 /* compare the position */
4165 if (REG_MULTI)
4166 return one->end.pos.lnum == two->end.pos.lnum
4167 && one->end.pos.col == two->end.pos.col;
4168 return one->end.ptr == two->end.ptr;
4169}
4170
4171/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004172 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4173 */
4174 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004175match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004176{
4177 nfa_state_T *state = startstate;
4178
4179 /* avoid too much recursion */
4180 if (depth > 10)
4181 return FALSE;
4182
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004183 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004184 {
4185 switch (state->c)
4186 {
4187 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004188 case NFA_MCLOSE:
4189 case NFA_END_INVISIBLE:
4190 case NFA_END_INVISIBLE_NEG:
4191 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004192 return TRUE;
4193
4194 case NFA_SPLIT:
4195 return match_follows(state->out, depth + 1)
4196 || match_follows(state->out1, depth + 1);
4197
4198 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004199 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004200 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004201 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004202 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004203 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004204 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004205 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004206 case NFA_COMPOSING:
4207 /* skip ahead to next state */
4208 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004209 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004210
4211 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004212 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004213 case NFA_IDENT:
4214 case NFA_SIDENT:
4215 case NFA_KWORD:
4216 case NFA_SKWORD:
4217 case NFA_FNAME:
4218 case NFA_SFNAME:
4219 case NFA_PRINT:
4220 case NFA_SPRINT:
4221 case NFA_WHITE:
4222 case NFA_NWHITE:
4223 case NFA_DIGIT:
4224 case NFA_NDIGIT:
4225 case NFA_HEX:
4226 case NFA_NHEX:
4227 case NFA_OCTAL:
4228 case NFA_NOCTAL:
4229 case NFA_WORD:
4230 case NFA_NWORD:
4231 case NFA_HEAD:
4232 case NFA_NHEAD:
4233 case NFA_ALPHA:
4234 case NFA_NALPHA:
4235 case NFA_LOWER:
4236 case NFA_NLOWER:
4237 case NFA_UPPER:
4238 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004239 case NFA_LOWER_IC:
4240 case NFA_NLOWER_IC:
4241 case NFA_UPPER_IC:
4242 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004243 case NFA_START_COLL:
4244 case NFA_START_NEG_COLL:
4245 case NFA_NEWL:
4246 /* state will advance input */
4247 return FALSE;
4248
4249 default:
4250 if (state->c > 0)
4251 /* state will advance input */
4252 return FALSE;
4253
4254 /* Others: zero-width or possibly zero-width, might still find
4255 * a match at the same position, keep looking. */
4256 break;
4257 }
4258 state = state->out;
4259 }
4260 return FALSE;
4261}
4262
4263
4264/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004265 * Return TRUE if "state" is already in list "l".
4266 */
4267 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004268state_in_list(
4269 nfa_list_T *l, /* runtime state list */
4270 nfa_state_T *state, /* state to update */
4271 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004272{
4273 if (state->lastlist[nfa_ll_index] == l->id)
4274 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004275 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004276 return TRUE;
4277 }
4278 return FALSE;
4279}
4280
Bram Moolenaar16b35782016-09-09 20:29:50 +02004281/* Offset used for "off" by addstate_here(). */
4282#define ADDSTATE_HERE_OFFSET 10
4283
Bram Moolenaard05bf562013-06-30 23:24:08 +02004284/*
4285 * Add "state" and possibly what follows to state list ".".
4286 * Returns "subs_arg", possibly copied into temp_subs.
4287 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004288 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004289addstate(
4290 nfa_list_T *l, /* runtime state list */
4291 nfa_state_T *state, /* state to update */
4292 regsubs_T *subs_arg, /* pointers to subexpressions */
4293 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004294 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004296 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004297 int off = off_arg;
4298 int add_here = FALSE;
4299 int listindex = 0;
4300 int k;
4301 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004302 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004303 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004304 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004305 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004306 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004307 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004308 regsubs_T *subs = subs_arg;
4309 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004310#ifdef ENABLE_LOG
4311 int did_print = FALSE;
4312#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004313
Bram Moolenaar16b35782016-09-09 20:29:50 +02004314 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4315 {
4316 add_here = TRUE;
4317 off = 0;
4318 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4319 }
4320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 switch (state->c)
4322 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004323 case NFA_NCLOSE:
4324 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004325 case NFA_MCLOSE1:
4326 case NFA_MCLOSE2:
4327 case NFA_MCLOSE3:
4328 case NFA_MCLOSE4:
4329 case NFA_MCLOSE5:
4330 case NFA_MCLOSE6:
4331 case NFA_MCLOSE7:
4332 case NFA_MCLOSE8:
4333 case NFA_MCLOSE9:
4334#ifdef FEAT_SYN_HL
4335 case NFA_ZCLOSE:
4336 case NFA_ZCLOSE1:
4337 case NFA_ZCLOSE2:
4338 case NFA_ZCLOSE3:
4339 case NFA_ZCLOSE4:
4340 case NFA_ZCLOSE5:
4341 case NFA_ZCLOSE6:
4342 case NFA_ZCLOSE7:
4343 case NFA_ZCLOSE8:
4344 case NFA_ZCLOSE9:
4345#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004346 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004347 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004348 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004349 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004350 /* These nodes are not added themselves but their "out" and/or
4351 * "out1" may be added below. */
4352 break;
4353
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004354 case NFA_BOL:
4355 case NFA_BOF:
4356 /* "^" won't match past end-of-line, don't bother trying.
4357 * Except when at the end of the line, or when we are going to the
4358 * next line for a look-behind match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004359 if (rex.input > rex.line
4360 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004361 && (nfa_endp == NULL
4362 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004363 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004364 goto skip_add;
4365 /* FALLTHROUGH */
4366
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004367 case NFA_MOPEN1:
4368 case NFA_MOPEN2:
4369 case NFA_MOPEN3:
4370 case NFA_MOPEN4:
4371 case NFA_MOPEN5:
4372 case NFA_MOPEN6:
4373 case NFA_MOPEN7:
4374 case NFA_MOPEN8:
4375 case NFA_MOPEN9:
4376#ifdef FEAT_SYN_HL
4377 case NFA_ZOPEN:
4378 case NFA_ZOPEN1:
4379 case NFA_ZOPEN2:
4380 case NFA_ZOPEN3:
4381 case NFA_ZOPEN4:
4382 case NFA_ZOPEN5:
4383 case NFA_ZOPEN6:
4384 case NFA_ZOPEN7:
4385 case NFA_ZOPEN8:
4386 case NFA_ZOPEN9:
4387#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004388 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004389 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004390 /* These nodes need to be added so that we can bail out when it
4391 * was added to this list before at the same position to avoid an
4392 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004393
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004394 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004395 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004397 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004398 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004399 * when there is a PIM. For NFA_MATCH check the position,
4400 * lower position is preferred. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02004401 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004402 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004403 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004404 /* When called from addstate_here() do insert before
4405 * existing states. */
4406 if (add_here)
4407 {
4408 for (k = 0; k < l->n && k < listindex; ++k)
4409 if (l->t[k].state->id == state->id)
4410 {
4411 found = TRUE;
4412 break;
4413 }
4414 }
4415 if (!add_here || found)
4416 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004417skip_add:
4418#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004419 nfa_set_code(state->c);
4420 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4421 abs(state->id), l->id, state->c, code,
4422 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004423#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004424 return subs;
4425 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004426 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004427
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004428 /* Do not add the state again when it exists with the same
4429 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004430 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004431 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004432 }
4433
Bram Moolenaara0169122013-06-26 18:16:58 +02004434 /* When there are backreferences or PIMs the number of states may
4435 * be (a lot) bigger than anticipated. */
4436 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004437 {
4438 int newlen = l->len * 3 / 2 + 50;
4439
Bram Moolenaard05bf562013-06-30 23:24:08 +02004440 if (subs != &temp_subs)
4441 {
4442 /* "subs" may point into the current array, need to make a
4443 * copy before it becomes invalid. */
4444 copy_sub(&temp_subs.norm, &subs->norm);
4445#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004446 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004447 copy_sub(&temp_subs.synt, &subs->synt);
4448#endif
4449 subs = &temp_subs;
4450 }
4451
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004452 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004453 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4454 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004455 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004456
4457 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004458 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004459 thread = &l->t[l->n++];
4460 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004461 if (pim == NULL)
4462 thread->pim.result = NFA_PIM_UNUSED;
4463 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004464 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004465 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004466 l->has_pim = TRUE;
4467 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004468 copy_sub(&thread->subs.norm, &subs->norm);
4469#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004470 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004471 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004472#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004473#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004474 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004475 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004476#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004477 }
4478
4479#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004480 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004481 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004482#endif
4483 switch (state->c)
4484 {
4485 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004486 break;
4487
4488 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004489 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004490 subs = addstate(l, state->out, subs, pim, off_arg);
4491 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004492 break;
4493
Bram Moolenaar699c1202013-09-25 16:41:54 +02004494 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004495 case NFA_NOPEN:
4496 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004497 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004498 break;
4499
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004500 case NFA_MOPEN:
4501 case NFA_MOPEN1:
4502 case NFA_MOPEN2:
4503 case NFA_MOPEN3:
4504 case NFA_MOPEN4:
4505 case NFA_MOPEN5:
4506 case NFA_MOPEN6:
4507 case NFA_MOPEN7:
4508 case NFA_MOPEN8:
4509 case NFA_MOPEN9:
4510#ifdef FEAT_SYN_HL
4511 case NFA_ZOPEN:
4512 case NFA_ZOPEN1:
4513 case NFA_ZOPEN2:
4514 case NFA_ZOPEN3:
4515 case NFA_ZOPEN4:
4516 case NFA_ZOPEN5:
4517 case NFA_ZOPEN6:
4518 case NFA_ZOPEN7:
4519 case NFA_ZOPEN8:
4520 case NFA_ZOPEN9:
4521#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004522 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004523 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004524 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004526 sub = &subs->norm;
4527 }
4528#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004529 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004530 {
4531 subidx = state->c - NFA_ZOPEN;
4532 sub = &subs->synt;
4533 }
4534#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004535 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004536 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004537 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004538 sub = &subs->norm;
4539 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004541 /* avoid compiler warnings */
4542 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004543 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004544
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004545 /* Set the position (with "off" added) in the subexpression. Save
4546 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004547 if (REG_MULTI)
4548 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004549 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004550 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004551 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004552 save_in_use = -1;
4553 }
4554 else
4555 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004556 save_in_use = sub->in_use;
4557 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004558 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004559 sub->list.multi[i].start_lnum = -1;
4560 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004561 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004562 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004563 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004564 if (off == -1)
4565 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004566 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004567 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004568 }
4569 else
4570 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004571 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004572 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004573 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004574 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004575 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004576 }
4577 else
4578 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004579 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004580 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004581 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004582 save_in_use = -1;
4583 }
4584 else
4585 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004586 save_in_use = sub->in_use;
4587 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004588 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004589 sub->list.line[i].start = NULL;
4590 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004591 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004592 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004593 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004594 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004595 }
4596
Bram Moolenaar16b35782016-09-09 20:29:50 +02004597 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004598 /* "subs" may have changed, need to set "sub" again */
4599#ifdef FEAT_SYN_HL
4600 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4601 sub = &subs->synt;
4602 else
4603#endif
4604 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004605
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004606 if (save_in_use == -1)
4607 {
4608 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004609 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004610 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004611 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004612 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004613 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004614 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004615 break;
4616
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004617 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004618 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004619 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004620 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004621 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004622 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004623 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004624 break;
4625 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004626 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004627 case NFA_MCLOSE1:
4628 case NFA_MCLOSE2:
4629 case NFA_MCLOSE3:
4630 case NFA_MCLOSE4:
4631 case NFA_MCLOSE5:
4632 case NFA_MCLOSE6:
4633 case NFA_MCLOSE7:
4634 case NFA_MCLOSE8:
4635 case NFA_MCLOSE9:
4636#ifdef FEAT_SYN_HL
4637 case NFA_ZCLOSE:
4638 case NFA_ZCLOSE1:
4639 case NFA_ZCLOSE2:
4640 case NFA_ZCLOSE3:
4641 case NFA_ZCLOSE4:
4642 case NFA_ZCLOSE5:
4643 case NFA_ZCLOSE6:
4644 case NFA_ZCLOSE7:
4645 case NFA_ZCLOSE8:
4646 case NFA_ZCLOSE9:
4647#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004648 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004649 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004650 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004652 sub = &subs->norm;
4653 }
4654#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004655 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004656 {
4657 subidx = state->c - NFA_ZCLOSE;
4658 sub = &subs->synt;
4659 }
4660#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004661 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004662 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004663 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004664 sub = &subs->norm;
4665 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004666
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004667 /* We don't fill in gaps here, there must have been an MOPEN that
4668 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004669 save_in_use = sub->in_use;
4670 if (sub->in_use <= subidx)
4671 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004672 if (REG_MULTI)
4673 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004674 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004675 if (off == -1)
4676 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004677 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004678 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004679 }
4680 else
4681 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004682 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004683 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004684 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004685 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004686 /* avoid compiler warnings */
4687 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 }
4689 else
4690 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004691 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004692 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004693 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004694 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 }
4696
Bram Moolenaar16b35782016-09-09 20:29:50 +02004697 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004698 /* "subs" may have changed, need to set "sub" again */
4699#ifdef FEAT_SYN_HL
4700 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4701 sub = &subs->synt;
4702 else
4703#endif
4704 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004705
4706 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004707 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004708 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004709 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004710 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004711 break;
4712 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004713 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714}
4715
4716/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004717 * Like addstate(), but the new state(s) are put at position "*ip".
4718 * Used for zero-width matches, next state to use is the added one.
4719 * This makes sure the order of states to be tried does not change, which
4720 * matters for alternatives.
4721 */
4722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004723addstate_here(
4724 nfa_list_T *l, /* runtime state list */
4725 nfa_state_T *state, /* state to update */
4726 regsubs_T *subs, /* pointers to subexpressions */
4727 nfa_pim_T *pim, /* postponed look-behind match */
4728 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004729{
4730 int tlen = l->n;
4731 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004732 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004733
Bram Moolenaar16b35782016-09-09 20:29:50 +02004734 /* First add the state(s) at the end, so that we know how many there are.
4735 * Pass the listidx as offset (avoids adding another argument to
4736 * addstate(). */
4737 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004738
Bram Moolenaar4b417062013-05-25 20:19:50 +02004739 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004740 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004741 return;
4742
4743 /* re-order to put the new state at the current position */
4744 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004745 if (count == 0)
4746 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004747 if (count == 1)
4748 {
4749 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004750 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004751 }
4752 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004753 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004754 if (l->n + count - 1 >= l->len)
4755 {
4756 /* not enough space to move the new states, reallocate the list
4757 * and move the states to the right position */
4758 nfa_thread_T *newl;
4759
4760 l->len = l->len * 3 / 2 + 50;
4761 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4762 if (newl == NULL)
4763 return;
4764 mch_memmove(&(newl[0]),
4765 &(l->t[0]),
4766 sizeof(nfa_thread_T) * listidx);
4767 mch_memmove(&(newl[listidx]),
4768 &(l->t[l->n - count]),
4769 sizeof(nfa_thread_T) * count);
4770 mch_memmove(&(newl[listidx + count]),
4771 &(l->t[listidx + 1]),
4772 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4773 vim_free(l->t);
4774 l->t = newl;
4775 }
4776 else
4777 {
4778 /* make space for new states, then move them from the
4779 * end to the current position */
4780 mch_memmove(&(l->t[listidx + count]),
4781 &(l->t[listidx + 1]),
4782 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4783 mch_memmove(&(l->t[listidx]),
4784 &(l->t[l->n - 1]),
4785 sizeof(nfa_thread_T) * count);
4786 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004787 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004788 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004789 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004790}
4791
4792/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004793 * Check character class "class" against current character c.
4794 */
4795 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004796check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004797{
4798 switch (class)
4799 {
4800 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004801 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004802 return OK;
4803 break;
4804 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004805 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004806 return OK;
4807 break;
4808 case NFA_CLASS_BLANK:
4809 if (c == ' ' || c == '\t')
4810 return OK;
4811 break;
4812 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004813 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004814 return OK;
4815 break;
4816 case NFA_CLASS_DIGIT:
4817 if (VIM_ISDIGIT(c))
4818 return OK;
4819 break;
4820 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004821 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004822 return OK;
4823 break;
4824 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004825 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004826 return OK;
4827 break;
4828 case NFA_CLASS_PRINT:
4829 if (vim_isprintc(c))
4830 return OK;
4831 break;
4832 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004833 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004834 return OK;
4835 break;
4836 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004837 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004838 return OK;
4839 break;
4840 case NFA_CLASS_UPPER:
4841 if (MB_ISUPPER(c))
4842 return OK;
4843 break;
4844 case NFA_CLASS_XDIGIT:
4845 if (vim_isxdigit(c))
4846 return OK;
4847 break;
4848 case NFA_CLASS_TAB:
4849 if (c == '\t')
4850 return OK;
4851 break;
4852 case NFA_CLASS_RETURN:
4853 if (c == '\r')
4854 return OK;
4855 break;
4856 case NFA_CLASS_BACKSPACE:
4857 if (c == '\b')
4858 return OK;
4859 break;
4860 case NFA_CLASS_ESCAPE:
4861 if (c == '\033')
4862 return OK;
4863 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004864 case NFA_CLASS_IDENT:
4865 if (vim_isIDc(c))
4866 return OK;
4867 break;
4868 case NFA_CLASS_KEYWORD:
4869 if (reg_iswordc(c))
4870 return OK;
4871 break;
4872 case NFA_CLASS_FNAME:
4873 if (vim_isfilec(c))
4874 return OK;
4875 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876
4877 default:
4878 /* should not be here :P */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004879 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004880 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004881 }
4882 return FAIL;
4883}
4884
Bram Moolenaar5714b802013-05-28 22:03:20 +02004885/*
4886 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004887 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004888 */
4889 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004890match_backref(
4891 regsub_T *sub, /* pointers to subexpressions */
4892 int subidx,
4893 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004894{
4895 int len;
4896
4897 if (sub->in_use <= subidx)
4898 {
4899retempty:
4900 /* backref was not set, match an empty string */
4901 *bytelen = 0;
4902 return TRUE;
4903 }
4904
4905 if (REG_MULTI)
4906 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004907 if (sub->list.multi[subidx].start_lnum < 0
4908 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004909 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004910 if (sub->list.multi[subidx].start_lnum == rex.lnum
4911 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004912 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004913 len = sub->list.multi[subidx].end_col
4914 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004915 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4916 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004917 {
4918 *bytelen = len;
4919 return TRUE;
4920 }
4921 }
4922 else
4923 {
4924 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004925 sub->list.multi[subidx].start_lnum,
4926 sub->list.multi[subidx].start_col,
4927 sub->list.multi[subidx].end_lnum,
4928 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004929 bytelen) == RA_MATCH)
4930 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004931 }
4932 }
4933 else
4934 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004935 if (sub->list.line[subidx].start == NULL
4936 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004937 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004938 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004939 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004940 {
4941 *bytelen = len;
4942 return TRUE;
4943 }
4944 }
4945 return FALSE;
4946}
4947
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004948#ifdef FEAT_SYN_HL
4949
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004950/*
4951 * Check for a match with \z subexpression "subidx".
4952 * Return TRUE if it matches.
4953 */
4954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004955match_zref(
4956 int subidx,
4957 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004958{
4959 int len;
4960
4961 cleanup_zsubexpr();
4962 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4963 {
4964 /* backref was not set, match an empty string */
4965 *bytelen = 0;
4966 return TRUE;
4967 }
4968
4969 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004970 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004971 {
4972 *bytelen = len;
4973 return TRUE;
4974 }
4975 return FALSE;
4976}
4977#endif
4978
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004979/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004980 * Save list IDs for all NFA states of "prog" into "list".
4981 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004982 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004983 */
4984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004985nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004986{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004987 int i;
4988 nfa_state_T *p;
4989
4990 /* Order in the list is reverse, it's a bit faster that way. */
4991 p = &prog->state[0];
4992 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004993 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004994 list[i] = p->lastlist[1];
4995 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004996 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004997 }
4998}
4999
5000/*
5001 * Restore list IDs from "list" to all NFA states.
5002 */
5003 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005004nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005005{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005006 int i;
5007 nfa_state_T *p;
5008
5009 p = &prog->state[0];
5010 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005011 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005012 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005013 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005014 }
5015}
5016
Bram Moolenaar423532e2013-05-29 21:14:42 +02005017 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005018nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005019{
5020 if (op == 1) return pos > val;
5021 if (op == 2) return pos < val;
5022 return val == pos;
5023}
5024
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005025static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005028 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005029 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5030 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005031 */
5032 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005033recursive_regmatch(
5034 nfa_state_T *state,
5035 nfa_pim_T *pim,
5036 nfa_regprog_T *prog,
5037 regsubs_T *submatch,
5038 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005039 int **listids,
5040 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005041{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005042 int save_reginput_col = (int)(rex.input - rex.line);
5043 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005044 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005045 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005046 save_se_T *save_nfa_endp = nfa_endp;
5047 save_se_T endpos;
5048 save_se_T *endposp = NULL;
5049 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005050 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005051
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005052 if (pim != NULL)
5053 {
5054 /* start at the position where the postponed match was */
5055 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005056 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005057 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005058 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005059 }
5060
Bram Moolenaardecd9542013-06-07 16:31:50 +02005061 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005062 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5063 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5064 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005065 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005066 /* The recursive match must end at the current position. When "pim" is
5067 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005068 endposp = &endpos;
5069 if (REG_MULTI)
5070 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005071 if (pim == NULL)
5072 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005073 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5074 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005075 }
5076 else
5077 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005078 }
5079 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005080 {
5081 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005082 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005083 else
5084 endpos.se_u.ptr = pim->end.ptr;
5085 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005086
5087 /* Go back the specified number of bytes, or as far as the
5088 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005089 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005090 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005091 if (state->val <= 0)
5092 {
5093 if (REG_MULTI)
5094 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005095 rex.line = reg_getline(--rex.lnum);
5096 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005098 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005099 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005100 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005101 }
5102 else
5103 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005104 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005105 {
5106 /* Not enough bytes in this line, go to end of
5107 * previous line. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005108 rex.line = reg_getline(--rex.lnum);
5109 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005110 {
5111 /* can't go before the first line */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005112 rex.line = reg_getline(++rex.lnum);
5113 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114 }
5115 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005116 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005117 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005118 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005119 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005120 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005121 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005122 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005123 }
5124 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005125 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005126 }
5127 }
5128
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129#ifdef ENABLE_LOG
5130 if (log_fd != stderr)
5131 fclose(log_fd);
5132 log_fd = NULL;
5133#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005134 /* Have to clear the lastlist field of the NFA nodes, so that
5135 * nfa_regmatch() and addstate() can run properly after recursion. */
5136 if (nfa_ll_index == 1)
5137 {
5138 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5139 * values and clear them. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005140 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005141 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005142 vim_free(*listids);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005143 *listids = (int *)lalloc(sizeof(int) * prog->nstate, TRUE);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005144 if (*listids == NULL)
5145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005146 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005147 return 0;
5148 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005149 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005150 }
5151 nfa_save_listids(prog, *listids);
5152 need_restore = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005153 /* any value of rex.nfa_listid will do */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005154 }
5155 else
5156 {
5157 /* First recursive nfa_regmatch() call, switch to the second lastlist
Bram Moolenaar0270f382018-07-17 05:43:58 +02005158 * entry. Make sure rex.nfa_listid is different from a previous
5159 * recursive call, because some states may still have this ID. */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005160 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005161 if (rex.nfa_listid <= rex.nfa_alt_listid)
5162 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005163 }
5164
5165 /* Call nfa_regmatch() to check if the current concat matches at this
5166 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005167 nfa_endp = endposp;
5168 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005169
5170 if (need_restore)
5171 nfa_restore_listids(prog, *listids);
5172 else
5173 {
5174 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005175 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005176 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005177
5178 /* restore position in input text */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005179 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005180 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005181 rex.line = reg_getline(rex.lnum);
5182 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005183 if (result != NFA_TOO_EXPENSIVE)
5184 {
5185 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005186 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005187 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005188 nfa_endp = save_nfa_endp;
5189
5190#ifdef ENABLE_LOG
5191 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5192 if (log_fd != NULL)
5193 {
5194 fprintf(log_fd, "****************************\n");
5195 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5196 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5197 fprintf(log_fd, "****************************\n");
5198 }
5199 else
5200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005201 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005202 log_fd = stderr;
5203 }
5204#endif
5205
5206 return result;
5207}
5208
Bram Moolenaara2d95102013-06-04 14:23:05 +02005209/*
5210 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005211 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005212 * NFA_ANY: 1
5213 * specific character: 99
5214 */
5215 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005216failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005217{
5218 int c = state->c;
5219 int l, r;
5220
5221 /* detect looping */
5222 if (depth > 4)
5223 return 1;
5224
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005225 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005226 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005227 case NFA_SPLIT:
5228 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5229 /* avoid recursive stuff */
5230 return 1;
5231 /* two alternatives, use the lowest failure chance */
5232 l = failure_chance(state->out, depth + 1);
5233 r = failure_chance(state->out1, depth + 1);
5234 return l < r ? l : r;
5235
5236 case NFA_ANY:
5237 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005238 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005239
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005240 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005241 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005242 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005243 /* empty match works always */
5244 return 0;
5245
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005246 case NFA_START_INVISIBLE:
5247 case NFA_START_INVISIBLE_FIRST:
5248 case NFA_START_INVISIBLE_NEG:
5249 case NFA_START_INVISIBLE_NEG_FIRST:
5250 case NFA_START_INVISIBLE_BEFORE:
5251 case NFA_START_INVISIBLE_BEFORE_FIRST:
5252 case NFA_START_INVISIBLE_BEFORE_NEG:
5253 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5254 case NFA_START_PATTERN:
5255 /* recursive regmatch is expensive, use low failure chance */
5256 return 5;
5257
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005258 case NFA_BOL:
5259 case NFA_EOL:
5260 case NFA_BOF:
5261 case NFA_EOF:
5262 case NFA_NEWL:
5263 return 99;
5264
5265 case NFA_BOW:
5266 case NFA_EOW:
5267 return 90;
5268
5269 case NFA_MOPEN:
5270 case NFA_MOPEN1:
5271 case NFA_MOPEN2:
5272 case NFA_MOPEN3:
5273 case NFA_MOPEN4:
5274 case NFA_MOPEN5:
5275 case NFA_MOPEN6:
5276 case NFA_MOPEN7:
5277 case NFA_MOPEN8:
5278 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005279#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005280 case NFA_ZOPEN:
5281 case NFA_ZOPEN1:
5282 case NFA_ZOPEN2:
5283 case NFA_ZOPEN3:
5284 case NFA_ZOPEN4:
5285 case NFA_ZOPEN5:
5286 case NFA_ZOPEN6:
5287 case NFA_ZOPEN7:
5288 case NFA_ZOPEN8:
5289 case NFA_ZOPEN9:
5290 case NFA_ZCLOSE:
5291 case NFA_ZCLOSE1:
5292 case NFA_ZCLOSE2:
5293 case NFA_ZCLOSE3:
5294 case NFA_ZCLOSE4:
5295 case NFA_ZCLOSE5:
5296 case NFA_ZCLOSE6:
5297 case NFA_ZCLOSE7:
5298 case NFA_ZCLOSE8:
5299 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005300#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005301 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005302 case NFA_MCLOSE1:
5303 case NFA_MCLOSE2:
5304 case NFA_MCLOSE3:
5305 case NFA_MCLOSE4:
5306 case NFA_MCLOSE5:
5307 case NFA_MCLOSE6:
5308 case NFA_MCLOSE7:
5309 case NFA_MCLOSE8:
5310 case NFA_MCLOSE9:
5311 case NFA_NCLOSE:
5312 return failure_chance(state->out, depth + 1);
5313
5314 case NFA_BACKREF1:
5315 case NFA_BACKREF2:
5316 case NFA_BACKREF3:
5317 case NFA_BACKREF4:
5318 case NFA_BACKREF5:
5319 case NFA_BACKREF6:
5320 case NFA_BACKREF7:
5321 case NFA_BACKREF8:
5322 case NFA_BACKREF9:
5323#ifdef FEAT_SYN_HL
5324 case NFA_ZREF1:
5325 case NFA_ZREF2:
5326 case NFA_ZREF3:
5327 case NFA_ZREF4:
5328 case NFA_ZREF5:
5329 case NFA_ZREF6:
5330 case NFA_ZREF7:
5331 case NFA_ZREF8:
5332 case NFA_ZREF9:
5333#endif
5334 /* backreferences don't match in many places */
5335 return 94;
5336
5337 case NFA_LNUM_GT:
5338 case NFA_LNUM_LT:
5339 case NFA_COL_GT:
5340 case NFA_COL_LT:
5341 case NFA_VCOL_GT:
5342 case NFA_VCOL_LT:
5343 case NFA_MARK_GT:
5344 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005345 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005346 /* before/after positions don't match very often */
5347 return 85;
5348
5349 case NFA_LNUM:
5350 return 90;
5351
5352 case NFA_CURSOR:
5353 case NFA_COL:
5354 case NFA_VCOL:
5355 case NFA_MARK:
5356 /* specific positions rarely match */
5357 return 98;
5358
5359 case NFA_COMPOSING:
5360 return 95;
5361
5362 default:
5363 if (c > 0)
5364 /* character match fails often */
5365 return 95;
5366 }
5367
5368 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005369 return 50;
5370}
5371
Bram Moolenaarf46da702013-06-02 22:37:42 +02005372/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005373 * Skip until the char "c" we know a match must start with.
5374 */
5375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005376skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005377{
5378 char_u *s;
5379
5380 /* Used often, do some work to avoid call overhead. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005381 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005382 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005383 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005384 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005385 if (s == NULL)
5386 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005387 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005388 return OK;
5389}
5390
5391/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005392 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005393 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005394 * Returns zero for no match, 1 for a match.
5395 */
5396 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005397find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005398{
5399 colnr_T col = startcol;
5400 int c1, c2;
5401 int len1, len2;
5402 int match;
5403
5404 for (;;)
5405 {
5406 match = TRUE;
5407 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5408 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5409 {
5410 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005411 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005412 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005413 {
5414 match = FALSE;
5415 break;
5416 }
5417 len2 += MB_CHAR2LEN(c2);
5418 }
5419 if (match
Bram Moolenaar473de612013-06-08 18:19:48 +02005420 /* check that no composing char follows */
5421 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005422 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005423 {
5424 cleanup_subexpr();
5425 if (REG_MULTI)
5426 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005427 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005428 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005429 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005430 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005431 }
5432 else
5433 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005434 rex.reg_startp[0] = rex.line + col;
5435 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005436 }
5437 return 1L;
5438 }
5439
5440 /* Try finding regstart after the current match. */
5441 col += MB_CHAR2LEN(regstart); /* skip regstart */
5442 if (skip_to_start(regstart, &col) == FAIL)
5443 break;
5444 }
5445 return 0L;
5446}
5447
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005448#ifdef FEAT_RELTIME
5449 static int
5450nfa_did_time_out()
5451{
5452 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5453 {
5454 if (nfa_timed_out != NULL)
5455 *nfa_timed_out = TRUE;
5456 return TRUE;
5457 }
5458 return FALSE;
5459}
5460#endif
5461
Bram Moolenaar473de612013-06-08 18:19:48 +02005462/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005463 * Main matching routine.
5464 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005465 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005466 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005467 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005468 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005469 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005470 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005471 * Note: Caller must ensure that: start != NULL.
5472 */
5473 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005474nfa_regmatch(
5475 nfa_regprog_T *prog,
5476 nfa_state_T *start,
5477 regsubs_T *submatch,
5478 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005479{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005480 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005481 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005482 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005483 int go_to_nextline = FALSE;
5484 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005485 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005486 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005487 nfa_list_T *thislist;
5488 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005489 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005490 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005491 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005492 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005493 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005494 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005495 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005496#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005497 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005498#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005499
Bram Moolenaar41f12052013-08-25 17:01:42 +02005500 /* Some patterns may take a long time to match, especially when using
5501 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5502 fast_breakcheck();
5503 if (got_int)
5504 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005505#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005506 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005507 return FALSE;
5508#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005509
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005510#ifdef NFA_REGEXP_DEBUG_LOG
5511 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5512 if (debug == NULL)
5513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005514 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005515 return FALSE;
5516 }
5517#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005518 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005519
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005520 /* Allocate memory for the lists of nodes. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02005521 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005522
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005523 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005524 list[0].len = prog->nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005525 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005526 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005527 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529
5530#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005531 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532 if (log_fd != NULL)
5533 {
5534 fprintf(log_fd, "**********************************\n");
5535 nfa_set_code(start->c);
5536 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5537 abs(start->id), code);
5538 fprintf(log_fd, "**********************************\n");
5539 }
5540 else
5541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005542 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543 log_fd = stderr;
5544 }
5545#endif
5546
5547 thislist = &list[0];
5548 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005549 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550 nextlist = &list[1];
5551 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005552 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005554 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005555#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005556 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005557
5558 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5559 * it's the first MOPEN. */
5560 if (toplevel)
5561 {
5562 if (REG_MULTI)
5563 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005564 m->norm.list.multi[0].start_lnum = rex.lnum;
5565 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005566 }
5567 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005568 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005569 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005570 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005571 }
5572 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005573 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005575#define ADD_STATE_IF_MATCH(state) \
5576 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005577 add_state = state->out; \
5578 add_off = clen; \
5579 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005580
5581 /*
5582 * Run for each character.
5583 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005584 for (;;)
5585 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005586 int curc;
5587 int clen;
5588
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005589 if (has_mbyte)
5590 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005591 curc = (*mb_ptr2char)(rex.input);
5592 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005593 }
5594 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005595 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005596 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005597 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005599 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005600 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005601 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005602 go_to_nextline = FALSE;
5603 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005604
5605 /* swap lists */
5606 thislist = &list[flag];
5607 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005608 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005609 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005610 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005611 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005612 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005613# ifdef FEAT_EVAL
5614 || nfa_fail_for_testing
5615# endif
5616 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005617 {
5618 /* too many states, retry with old engine */
5619 nfa_match = NFA_TOO_EXPENSIVE;
5620 goto theend;
5621 }
5622
Bram Moolenaar0270f382018-07-17 05:43:58 +02005623 thislist->id = rex.nfa_listid;
5624 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005625
5626#ifdef ENABLE_LOG
5627 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005628 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005629 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005630 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005631 {
5632 int i;
5633
5634 for (i = 0; i < thislist->n; i++)
5635 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637 fprintf(log_fd, "\n");
5638#endif
5639
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005640#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005641 fprintf(debug, "\n-------------------\n");
5642#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005643 /*
5644 * If the state lists are empty we can stop.
5645 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005646 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005647 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648
5649 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005650 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005651 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005652 /* If the list gets very long there probably is something wrong.
5653 * At least allow interrupting with CTRL-C. */
5654 fast_breakcheck();
5655 if (got_int)
5656 break;
5657#ifdef FEAT_RELTIME
5658 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5659 {
5660 nfa_time_count = 0;
5661 if (nfa_did_time_out())
5662 break;
5663 }
5664#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005665 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005666
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005667#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005668 nfa_set_code(t->state->c);
5669 fprintf(debug, "%s, ", code);
5670#endif
5671#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005672 {
5673 int col;
5674
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005675 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005676 col = -1;
5677 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005678 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005679 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005680 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005681 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005682 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005683 abs(t->state->id), (int)t->state->c, code, col,
5684 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005685 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005686#endif
5687
5688 /*
5689 * Handle the possible codes of the current state.
5690 * The most important is NFA_MATCH.
5691 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005692 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005693 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005694 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005695 switch (t->state->c)
5696 {
5697 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005698 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005699 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005700 * rex.reg_icombine is not set, that is not really a match. */
5701 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005702 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005703
Bram Moolenaar963fee22013-05-26 21:47:28 +02005704 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005705 copy_sub(&submatch->norm, &t->subs.norm);
5706#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005707 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005708 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005709#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005711 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005712#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005713 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005714 * states at this position. When the list of states is going
Bram Moolenaar0270f382018-07-17 05:43:58 +02005715 * to be empty quit without advancing, so that "rex.input" is
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005716 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005717 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005718 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005719 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005720 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721
5722 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005723 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005724 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005725 /*
5726 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005727 * NFA_START_INVISIBLE_BEFORE node.
5728 * They surround a zero-width group, used with "\@=", "\&",
5729 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005730 * If we got here, it means that the current "invisible" group
5731 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005732 * nfa_regmatch(). For a look-behind match only when it ends
5733 * in the position in "nfa_endp".
5734 * Submatches are stored in *m, and used in the parent call.
5735 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005736#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005737 if (nfa_endp != NULL)
5738 {
5739 if (REG_MULTI)
5740 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005741 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005742 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005743 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005744 nfa_endp->se_u.pos.col);
5745 else
5746 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005747 (int)(rex.input - rex.line),
5748 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005749 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005750#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005751 /* If "nfa_endp" is set it's only a match if it ends at
5752 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005753 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005754 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5755 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005756 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005757 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005758 break;
5759
5760 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005761 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005762 {
5763 copy_sub(&m->norm, &t->subs.norm);
5764#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005765 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005766 copy_sub(&m->synt, &t->subs.synt);
5767#endif
5768 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005769#ifdef ENABLE_LOG
5770 fprintf(log_fd, "Match found:\n");
5771 log_subsexpr(m);
5772#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005773 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005774 /* See comment above at "goto nextchar". */
5775 if (nextlist->n == 0)
5776 clen = 0;
5777 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005778
5779 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005780 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005781 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005782 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005783 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005784 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005785 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005786 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005787 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005788#ifdef ENABLE_LOG
5789 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5790 failure_chance(t->state->out, 0),
5791 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005792#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005793 /* Do it directly if there already is a PIM or when
5794 * nfa_postprocess() detected it will work better. */
5795 if (t->pim.result != NFA_PIM_UNUSED
5796 || t->state->c == NFA_START_INVISIBLE_FIRST
5797 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5798 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5799 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005800 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005801 int in_use = m->norm.in_use;
5802
Bram Moolenaar699c1202013-09-25 16:41:54 +02005803 /* Copy submatch info for the recursive call, opposite
5804 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005805 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005806#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005807 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005808 copy_sub_off(&m->synt, &t->subs.synt);
5809#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005810
Bram Moolenaara2d95102013-06-04 14:23:05 +02005811 /*
5812 * First try matching the invisible match, then what
5813 * follows.
5814 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005815 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005816 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005817 if (result == NFA_TOO_EXPENSIVE)
5818 {
5819 nfa_match = result;
5820 goto theend;
5821 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005822
Bram Moolenaardecd9542013-06-07 16:31:50 +02005823 /* for \@! and \@<! it is a match when the result is
5824 * FALSE */
5825 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005826 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5827 || t->state->c
5828 == NFA_START_INVISIBLE_BEFORE_NEG
5829 || t->state->c
5830 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005831 {
5832 /* Copy submatch info from the recursive call */
5833 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005834#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005835 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005836 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005837#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005838 /* If the pattern has \ze and it matched in the
5839 * sub pattern, use it. */
5840 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005841
Bram Moolenaara2d95102013-06-04 14:23:05 +02005842 /* t->state->out1 is the corresponding
5843 * END_INVISIBLE node; Add its out to the current
5844 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005845 add_here = TRUE;
5846 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005847 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005848 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005849 }
5850 else
5851 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005852 nfa_pim_T pim;
5853
Bram Moolenaara2d95102013-06-04 14:23:05 +02005854 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005855 * First try matching what follows. Only if a match
5856 * is found verify the invisible match matches. Add a
5857 * nfa_pim_T to the following states, it contains info
5858 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005859 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005860 pim.state = t->state;
5861 pim.result = NFA_PIM_TODO;
5862 pim.subs.norm.in_use = 0;
5863#ifdef FEAT_SYN_HL
5864 pim.subs.synt.in_use = 0;
5865#endif
5866 if (REG_MULTI)
5867 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005868 pim.end.pos.col = (int)(rex.input - rex.line);
5869 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005870 }
5871 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005872 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005873
5874 /* t->state->out1 is the corresponding END_INVISIBLE
5875 * node; Add its out to the current list (zero-width
5876 * match). */
5877 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005878 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005879 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005880 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005881 break;
5882
Bram Moolenaar87953742013-06-05 18:52:40 +02005883 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005884 {
5885 nfa_state_T *skip = NULL;
5886#ifdef ENABLE_LOG
5887 int skip_lid = 0;
5888#endif
5889
5890 /* There is no point in trying to match the pattern if the
5891 * output state is not going to be added to the list. */
5892 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5893 {
5894 skip = t->state->out1->out;
5895#ifdef ENABLE_LOG
5896 skip_lid = nextlist->id;
5897#endif
5898 }
5899 else if (state_in_list(nextlist,
5900 t->state->out1->out->out, &t->subs))
5901 {
5902 skip = t->state->out1->out->out;
5903#ifdef ENABLE_LOG
5904 skip_lid = nextlist->id;
5905#endif
5906 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005907 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005908 t->state->out1->out->out, &t->subs))
5909 {
5910 skip = t->state->out1->out->out;
5911#ifdef ENABLE_LOG
5912 skip_lid = thislist->id;
5913#endif
5914 }
5915 if (skip != NULL)
5916 {
5917#ifdef ENABLE_LOG
5918 nfa_set_code(skip->c);
5919 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5920 abs(skip->id), skip_lid, skip->c, code);
5921#endif
5922 break;
5923 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005924 /* Copy submatch info to the recursive call, opposite of what
5925 * happens afterwards. */
5926 copy_sub_off(&m->norm, &t->subs.norm);
5927#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005928 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005929 copy_sub_off(&m->synt, &t->subs.synt);
5930#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005931
Bram Moolenaar87953742013-06-05 18:52:40 +02005932 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005933 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005934 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005935 if (result == NFA_TOO_EXPENSIVE)
5936 {
5937 nfa_match = result;
5938 goto theend;
5939 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005940 if (result)
5941 {
5942 int bytelen;
5943
5944#ifdef ENABLE_LOG
5945 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5946 log_subsexpr(m);
5947#endif
5948 /* Copy submatch info from the recursive call */
5949 copy_sub_off(&t->subs.norm, &m->norm);
5950#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005951 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005952 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005953#endif
5954 /* Now we need to skip over the matched text and then
5955 * continue with what follows. */
5956 if (REG_MULTI)
5957 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005958 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02005959 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02005960 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005961 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02005962
5963#ifdef ENABLE_LOG
5964 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5965#endif
5966 if (bytelen == 0)
5967 {
5968 /* empty match, output of corresponding
5969 * NFA_END_PATTERN/NFA_SKIP to be used at current
5970 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005971 add_here = TRUE;
5972 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005973 }
5974 else if (bytelen <= clen)
5975 {
5976 /* match current character, output of corresponding
5977 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005978 add_state = t->state->out1->out->out;
5979 add_off = clen;
5980 }
5981 else
5982 {
5983 /* skip over the matched characters, set character
5984 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005985 add_state = t->state->out1->out;
5986 add_off = bytelen;
5987 add_count = bytelen - clen;
5988 }
5989 }
5990 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005991 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005992
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005993 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02005994 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005995 {
5996 add_here = TRUE;
5997 add_state = t->state->out;
5998 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005999 break;
6000
6001 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006002 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006003 {
6004 add_here = TRUE;
6005 add_state = t->state->out;
6006 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006007 break;
6008
6009 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006010 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006011
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006012 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006013 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006014 else if (has_mbyte)
6015 {
6016 int this_class;
6017
6018 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006019 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006020 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006021 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006022 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006023 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006024 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006025 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006026 || (rex.input > rex.line
6027 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006028 result = FALSE;
6029 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006030 {
6031 add_here = TRUE;
6032 add_state = t->state->out;
6033 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006034 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006035
6036 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006037 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006038 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006039 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006040 else if (has_mbyte)
6041 {
6042 int this_class, prev_class;
6043
6044 /* Get class of current and previous char (if it exists). */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006045 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006046 prev_class = reg_prev_class();
6047 if (this_class == prev_class
6048 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006049 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006050 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006051 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6052 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006053 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006054 result = FALSE;
6055 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006056 {
6057 add_here = TRUE;
6058 add_state = t->state->out;
6059 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006060 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006061
Bram Moolenaar4b780632013-05-31 22:14:52 +02006062 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006063 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006064 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006065 {
6066 add_here = TRUE;
6067 add_state = t->state->out;
6068 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006069 break;
6070
6071 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006072 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006073 {
6074 add_here = TRUE;
6075 add_state = t->state->out;
6076 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006077 break;
6078
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006079 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006080 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006081 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006082 int len = 0;
6083 nfa_state_T *end;
6084 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006085 int cchars[MAX_MCO];
6086 int ccount = 0;
6087 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006088
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006089 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006090 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006091 if (utf_iscomposing(sta->c))
6092 {
6093 /* Only match composing character(s), ignore base
6094 * character. Used for ".{composing}" and "{composing}"
6095 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006096 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006097 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006098 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006099 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006100 /* If \Z was present, then ignore composing characters.
6101 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006102 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006103 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006104 else
6105 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006106 while (sta->c != NFA_END_COMPOSING)
6107 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006108 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006109
6110 /* Check base character matches first, unless ignored. */
6111 else if (len > 0 || mc == sta->c)
6112 {
6113 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006114 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006115 len += mb_char2len(mc);
6116 sta = sta->out;
6117 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006118
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006119 /* We don't care about the order of composing characters.
6120 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006121 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006122 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006123 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006124 cchars[ccount++] = mc;
6125 len += mb_char2len(mc);
6126 if (ccount == MAX_MCO)
6127 break;
6128 }
6129
6130 /* Check that each composing char in the pattern matches a
6131 * composing char in the text. We do not check if all
6132 * composing chars are matched. */
6133 result = OK;
6134 while (sta->c != NFA_END_COMPOSING)
6135 {
6136 for (j = 0; j < ccount; ++j)
6137 if (cchars[j] == sta->c)
6138 break;
6139 if (j == ccount)
6140 {
6141 result = FAIL;
6142 break;
6143 }
6144 sta = sta->out;
6145 }
6146 }
6147 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006148 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006149
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006150 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006151 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006152 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006153 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006154
6155 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006156 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006157 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006158 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006159 go_to_nextline = TRUE;
6160 /* Pass -1 for the offset, which means taking the position
6161 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006162 add_state = t->state->out;
6163 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006164 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006165 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006166 {
6167 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006168 add_state = t->state->out;
6169 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006170 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171 break;
6172
Bram Moolenaar417bad22013-06-07 14:08:30 +02006173 case NFA_START_COLL:
6174 case NFA_START_NEG_COLL:
6175 {
6176 /* What follows is a list of characters, until NFA_END_COLL.
6177 * One of them must match or none of them must match. */
6178 nfa_state_T *state;
6179 int result_if_matched;
6180 int c1, c2;
6181
6182 /* Never match EOL. If it's part of the collection it is added
6183 * as a separate state with an OR. */
6184 if (curc == NUL)
6185 break;
6186
6187 state = t->state->out;
6188 result_if_matched = (t->state->c == NFA_START_COLL);
6189 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006190 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006191 if (state->c == NFA_END_COLL)
6192 {
6193 result = !result_if_matched;
6194 break;
6195 }
6196 if (state->c == NFA_RANGE_MIN)
6197 {
6198 c1 = state->val;
6199 state = state->out; /* advance to NFA_RANGE_MAX */
6200 c2 = state->val;
6201#ifdef ENABLE_LOG
6202 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6203 curc, c1, c2);
6204#endif
6205 if (curc >= c1 && curc <= c2)
6206 {
6207 result = result_if_matched;
6208 break;
6209 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006210 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006211 {
6212 int curc_low = MB_TOLOWER(curc);
6213 int done = FALSE;
6214
6215 for ( ; c1 <= c2; ++c1)
6216 if (MB_TOLOWER(c1) == curc_low)
6217 {
6218 result = result_if_matched;
6219 done = TRUE;
6220 break;
6221 }
6222 if (done)
6223 break;
6224 }
6225 }
6226 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006227 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006228 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006229 == MB_TOLOWER(state->c))))
6230 {
6231 result = result_if_matched;
6232 break;
6233 }
6234 state = state->out;
6235 }
6236 if (result)
6237 {
6238 /* next state is in out of the NFA_END_COLL, out1 of
6239 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006240 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006241 add_off = clen;
6242 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006243 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006244 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006245
6246 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006247 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006248 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006249 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006250 add_state = t->state->out;
6251 add_off = clen;
6252 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006253 break;
6254
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006255 case NFA_ANY_COMPOSING:
6256 /* On a composing character skip over it. Otherwise do
6257 * nothing. Always matches. */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006258 if (enc_utf8 && utf_iscomposing(curc))
6259 {
6260 add_off = clen;
6261 }
6262 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006263 {
6264 add_here = TRUE;
6265 add_off = 0;
6266 }
6267 add_state = t->state->out;
6268 break;
6269
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006270 /*
6271 * Character classes like \a for alpha, \d for digit etc.
6272 */
6273 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006274 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006275 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006276 break;
6277
6278 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006279 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006280 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006281 break;
6282
6283 case NFA_KWORD: /* \k */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006284 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006285 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006286 break;
6287
6288 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006289 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006290 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006291 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006292 break;
6293
6294 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006295 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006296 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006297 break;
6298
6299 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006300 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006301 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006302 break;
6303
6304 case NFA_PRINT: /* \p */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006305 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006306 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307 break;
6308
6309 case NFA_SPRINT: /* \P */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006310 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006311 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006312 break;
6313
6314 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006315 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006316 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006317 break;
6318
6319 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006320 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006321 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006322 break;
6323
6324 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006325 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006326 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006327 break;
6328
6329 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006330 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006331 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006332 break;
6333
6334 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006335 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006336 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006337 break;
6338
6339 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006340 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006341 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006342 break;
6343
6344 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006345 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006346 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006347 break;
6348
6349 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006350 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006351 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006352 break;
6353
6354 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006355 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006356 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006357 break;
6358
6359 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006360 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006361 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006362 break;
6363
6364 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006365 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006366 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006367 break;
6368
6369 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006370 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006371 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006372 break;
6373
6374 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006375 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006376 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006377 break;
6378
6379 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006380 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006381 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006382 break;
6383
6384 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006385 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006386 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006387 break;
6388
6389 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006390 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006391 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006392 break;
6393
6394 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006395 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006396 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006397 break;
6398
6399 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006400 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006401 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006402 break;
6403
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006404 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006405 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006406 ADD_STATE_IF_MATCH(t->state);
6407 break;
6408
6409 case NFA_NLOWER_IC: /* [^a-z] */
6410 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006411 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006412 ADD_STATE_IF_MATCH(t->state);
6413 break;
6414
6415 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006416 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006417 ADD_STATE_IF_MATCH(t->state);
6418 break;
6419
6420 case NFA_NUPPER_IC: /* ^[A-Z] */
6421 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006422 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006423 ADD_STATE_IF_MATCH(t->state);
6424 break;
6425
Bram Moolenaar5714b802013-05-28 22:03:20 +02006426 case NFA_BACKREF1:
6427 case NFA_BACKREF2:
6428 case NFA_BACKREF3:
6429 case NFA_BACKREF4:
6430 case NFA_BACKREF5:
6431 case NFA_BACKREF6:
6432 case NFA_BACKREF7:
6433 case NFA_BACKREF8:
6434 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006435#ifdef FEAT_SYN_HL
6436 case NFA_ZREF1:
6437 case NFA_ZREF2:
6438 case NFA_ZREF3:
6439 case NFA_ZREF4:
6440 case NFA_ZREF5:
6441 case NFA_ZREF6:
6442 case NFA_ZREF7:
6443 case NFA_ZREF8:
6444 case NFA_ZREF9:
6445#endif
6446 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006447 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006448 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006449 int bytelen;
6450
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006451 if (t->state->c <= NFA_BACKREF9)
6452 {
6453 subidx = t->state->c - NFA_BACKREF1 + 1;
6454 result = match_backref(&t->subs.norm, subidx, &bytelen);
6455 }
6456#ifdef FEAT_SYN_HL
6457 else
6458 {
6459 subidx = t->state->c - NFA_ZREF1 + 1;
6460 result = match_zref(subidx, &bytelen);
6461 }
6462#endif
6463
Bram Moolenaar5714b802013-05-28 22:03:20 +02006464 if (result)
6465 {
6466 if (bytelen == 0)
6467 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006468 /* empty match always works, output of NFA_SKIP to be
6469 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006470 add_here = TRUE;
6471 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006472 }
6473 else if (bytelen <= clen)
6474 {
6475 /* match current character, jump ahead to out of
6476 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006477 add_state = t->state->out->out;
6478 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006479 }
6480 else
6481 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006482 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006483 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006484 add_state = t->state->out;
6485 add_off = bytelen;
6486 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006487 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006488 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006489 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006490 }
6491 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006492 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006493 if (t->count - clen <= 0)
6494 {
6495 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006496 add_state = t->state->out;
6497 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006498 }
6499 else
6500 {
6501 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006502 add_state = t->state;
6503 add_off = 0;
6504 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006505 }
6506 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006507
Bram Moolenaar423532e2013-05-29 21:14:42 +02006508 case NFA_LNUM:
6509 case NFA_LNUM_GT:
6510 case NFA_LNUM_LT:
6511 result = (REG_MULTI &&
6512 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006513 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006514 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006515 {
6516 add_here = TRUE;
6517 add_state = t->state->out;
6518 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006519 break;
6520
6521 case NFA_COL:
6522 case NFA_COL_GT:
6523 case NFA_COL_LT:
6524 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006525 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006526 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006527 {
6528 add_here = TRUE;
6529 add_state = t->state->out;
6530 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006531 break;
6532
6533 case NFA_VCOL:
6534 case NFA_VCOL_GT:
6535 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006536 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006537 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006538 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006539 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006540
6541 /* Bail out quickly when there can't be a match, avoid the
6542 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006543 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006544 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006545 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006546 result = FALSE;
6547 if (op == 1 && col - 1 > t->state->val && col > 100)
6548 {
6549 int ts = wp->w_buffer->b_p_ts;
6550
6551 /* Guess that a character won't use more columns than
6552 * 'tabstop', with a minimum of 4. */
6553 if (ts < 4)
6554 ts = 4;
6555 result = col > t->state->val * ts;
6556 }
6557 if (!result)
6558 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006559 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006560 if (result)
6561 {
6562 add_here = TRUE;
6563 add_state = t->state->out;
6564 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006565 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006566 break;
6567
Bram Moolenaar044aa292013-06-04 21:27:38 +02006568 case NFA_MARK:
6569 case NFA_MARK_GT:
6570 case NFA_MARK_LT:
6571 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006572 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006573
6574 /* Compare the mark position to the match position. */
6575 result = (pos != NULL /* mark doesn't exist */
6576 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006577 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6578 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006579 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006580 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006581 ? t->state->c == NFA_MARK_GT
6582 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006583 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006584 ? t->state->c == NFA_MARK_GT
6585 : t->state->c == NFA_MARK_LT)));
6586 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006587 {
6588 add_here = TRUE;
6589 add_state = t->state->out;
6590 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006591 break;
6592 }
6593
Bram Moolenaar423532e2013-05-29 21:14:42 +02006594 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006595 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006596 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006597 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006598 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006599 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006600 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006601 {
6602 add_here = TRUE;
6603 add_state = t->state->out;
6604 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006605 break;
6606
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006607 case NFA_VISUAL:
6608 result = reg_match_visual();
6609 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006610 {
6611 add_here = TRUE;
6612 add_state = t->state->out;
6613 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006614 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006615
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006616 case NFA_MOPEN1:
6617 case NFA_MOPEN2:
6618 case NFA_MOPEN3:
6619 case NFA_MOPEN4:
6620 case NFA_MOPEN5:
6621 case NFA_MOPEN6:
6622 case NFA_MOPEN7:
6623 case NFA_MOPEN8:
6624 case NFA_MOPEN9:
6625#ifdef FEAT_SYN_HL
6626 case NFA_ZOPEN:
6627 case NFA_ZOPEN1:
6628 case NFA_ZOPEN2:
6629 case NFA_ZOPEN3:
6630 case NFA_ZOPEN4:
6631 case NFA_ZOPEN5:
6632 case NFA_ZOPEN6:
6633 case NFA_ZOPEN7:
6634 case NFA_ZOPEN8:
6635 case NFA_ZOPEN9:
6636#endif
6637 case NFA_NOPEN:
6638 case NFA_ZSTART:
6639 /* These states are only added to be able to bail out when
6640 * they are added again, nothing is to be done. */
6641 break;
6642
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006643 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006644 {
6645 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006646
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006647#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006648 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006649 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006650#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006651 result = (c == curc);
6652
Bram Moolenaar6100d022016-10-02 16:51:57 +02006653 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006654 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006655 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006656 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006657 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006658 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006659 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006660 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006661 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006662
6663 } /* switch (t->state->c) */
6664
6665 if (add_state != NULL)
6666 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006667 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006668 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006669
6670 if (t->pim.result == NFA_PIM_UNUSED)
6671 pim = NULL;
6672 else
6673 pim = &t->pim;
6674
6675 /* Handle the postponed invisible match if the match might end
6676 * without advancing and before the end of the line. */
6677 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006678 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006679 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006680 {
6681#ifdef ENABLE_LOG
6682 fprintf(log_fd, "\n");
6683 fprintf(log_fd, "==================================\n");
6684 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6685 fprintf(log_fd, "\n");
6686#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006687 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006688 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006689 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006690 /* for \@! and \@<! it is a match when the result is
6691 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006692 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006693 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6694 || pim->state->c
6695 == NFA_START_INVISIBLE_BEFORE_NEG
6696 || pim->state->c
6697 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006698 {
6699 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006700 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006701#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006702 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006703 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006704#endif
6705 }
6706 }
6707 else
6708 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006709 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006710#ifdef ENABLE_LOG
6711 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006712 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006713 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6714 fprintf(log_fd, "\n");
6715#endif
6716 }
6717
Bram Moolenaardecd9542013-06-07 16:31:50 +02006718 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006719 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006720 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6721 || pim->state->c
6722 == NFA_START_INVISIBLE_BEFORE_NEG
6723 || pim->state->c
6724 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006725 {
6726 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006727 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006728#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006729 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006730 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006731#endif
6732 }
6733 else
6734 /* look-behind match failed, don't add the state */
6735 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006736
6737 /* Postponed invisible match was handled, don't add it to
6738 * following states. */
6739 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006740 }
6741
Bram Moolenaara951e352013-10-06 15:46:11 +02006742 /* If "pim" points into l->t it will become invalid when
6743 * adding the state causes the list to be reallocated. Make a
6744 * local copy to avoid that. */
6745 if (pim == &t->pim)
6746 {
6747 copy_pim(&pim_copy, pim);
6748 pim = &pim_copy;
6749 }
6750
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006751 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006752 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006753 else
6754 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006755 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006756 if (add_count > 0)
6757 nextlist->t[nextlist->n - 1].count = add_count;
6758 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006759 }
6760
6761 } /* for (thislist = thislist; thislist->state; thislist++) */
6762
Bram Moolenaare23febd2013-05-26 18:40:14 +02006763 /* Look for the start of a match in the current position by adding the
6764 * start state to the list of states.
6765 * The first found match is the leftmost one, thus the order of states
6766 * matters!
6767 * Do not add the start state in recursive calls of nfa_regmatch(),
6768 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006769 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006770 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006771 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006772 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006773 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006774 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006775 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006776 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006777 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006778 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006779 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6780 || (rex.lnum == nfa_endp->se_u.pos.lnum
6781 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006782 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006783 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006784 {
6785#ifdef ENABLE_LOG
6786 fprintf(log_fd, "(---) STARTSTATE\n");
6787#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006788 /* Inline optimized code for addstate() if we know the state is
6789 * the first MOPEN. */
6790 if (toplevel)
6791 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006792 int add = TRUE;
6793 int c;
6794
6795 if (prog->regstart != NUL && clen != 0)
6796 {
6797 if (nextlist->n == 0)
6798 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006799 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006800
6801 /* Nextlist is empty, we can skip ahead to the
6802 * character that must appear at the start. */
6803 if (skip_to_start(prog->regstart, &col) == FAIL)
6804 break;
6805#ifdef ENABLE_LOG
6806 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006807 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006808#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006809 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006810 }
6811 else
6812 {
6813 /* Checking if the required start character matches is
6814 * cheaper than adding a state that won't match. */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006815 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006816 if (c != prog->regstart && (!rex.reg_ic
6817 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006818 {
6819#ifdef ENABLE_LOG
6820 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6821#endif
6822 add = FALSE;
6823 }
6824 }
6825 }
6826
6827 if (add)
6828 {
6829 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006830 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006831 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006832 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006833 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006834 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006835 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006836 }
6837 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006838 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006839 }
6840
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006841#ifdef ENABLE_LOG
6842 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006843 {
6844 int i;
6845
6846 for (i = 0; i < thislist->n; i++)
6847 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6848 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006849 fprintf(log_fd, "\n");
6850#endif
6851
6852nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006853 /* Advance to the next character, or advance to the next line, or
6854 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006855 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006856 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006857 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006858 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006859 reg_nextline();
6860 else
6861 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006862
6863 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006864 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006865 if (got_int)
6866 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006867#ifdef FEAT_RELTIME
6868 /* Check for timeout once in a twenty times to avoid overhead. */
6869 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6870 {
6871 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006872 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006873 break;
6874 }
6875#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006876 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006877
6878#ifdef ENABLE_LOG
6879 if (log_fd != stderr)
6880 fclose(log_fd);
6881 log_fd = NULL;
6882#endif
6883
6884theend:
6885 /* Free memory */
6886 vim_free(list[0].t);
6887 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006888 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006889#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006890#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006891 fclose(debug);
6892#endif
6893
Bram Moolenaar963fee22013-05-26 21:47:28 +02006894 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006895}
6896
6897/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006898 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006899 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006900 */
6901 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006902nfa_regtry(
6903 nfa_regprog_T *prog,
6904 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006905 proftime_T *tm UNUSED, /* timeout limit or NULL */
6906 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006907{
6908 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006909 regsubs_T subs, m;
6910 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006911 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006912#ifdef ENABLE_LOG
6913 FILE *f;
6914#endif
6915
Bram Moolenaar0270f382018-07-17 05:43:58 +02006916 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006917#ifdef FEAT_RELTIME
6918 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006919 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006920 nfa_time_count = 0;
6921#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006922
6923#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006924 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006925 if (f != NULL)
6926 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006927 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006928#ifdef DEBUG
6929 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6930#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006931 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006932 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006933 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006934 fprintf(f, "\n\n");
6935 fclose(f);
6936 }
6937 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006938 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006939#endif
6940
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006941 clear_sub(&subs.norm);
6942 clear_sub(&m.norm);
6943#ifdef FEAT_SYN_HL
6944 clear_sub(&subs.synt);
6945 clear_sub(&m.synt);
6946#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006947
Bram Moolenaarfda37292014-11-05 14:27:36 +01006948 result = nfa_regmatch(prog, start, &subs, &m);
6949 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006950 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006951 else if (result == NFA_TOO_EXPENSIVE)
6952 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953
6954 cleanup_subexpr();
6955 if (REG_MULTI)
6956 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006957 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006958 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006959 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
6960 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006961
Bram Moolenaar6100d022016-10-02 16:51:57 +02006962 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
6963 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006964 }
6965
Bram Moolenaar6100d022016-10-02 16:51:57 +02006966 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006967 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006968 rex.reg_startpos[0].lnum = 0;
6969 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006970 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006971 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006973 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006974 rex.reg_endpos[0].lnum = rex.lnum;
6975 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006976 }
6977 else
6978 /* Use line number of "\ze". */
Bram Moolenaar0270f382018-07-17 05:43:58 +02006979 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006980 }
6981 else
6982 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006983 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006984 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006985 rex.reg_startp[i] = subs.norm.list.line[i].start;
6986 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006987 }
6988
Bram Moolenaar6100d022016-10-02 16:51:57 +02006989 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006990 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006991 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006992 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006993 }
6994
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006995#ifdef FEAT_SYN_HL
6996 /* Package any found \z(...\) matches for export. Default is none. */
6997 unref_extmatch(re_extmatch_out);
6998 re_extmatch_out = NULL;
6999
7000 if (prog->reghasz == REX_SET)
7001 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007002 cleanup_zsubexpr();
7003 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007004 /* Loop over \z1, \z2, etc. There is no \z0. */
7005 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007006 {
7007 if (REG_MULTI)
7008 {
7009 struct multipos *mpos = &subs.synt.list.multi[i];
7010
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007011 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007012 if (mpos->start_lnum >= 0
7013 && mpos->start_lnum == mpos->end_lnum
7014 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007015 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007016 vim_strnsave(reg_getline(mpos->start_lnum)
7017 + mpos->start_col,
7018 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007019 }
7020 else
7021 {
7022 struct linepos *lpos = &subs.synt.list.line[i];
7023
7024 if (lpos->start != NULL && lpos->end != NULL)
7025 re_extmatch_out->matches[i] =
7026 vim_strnsave(lpos->start,
7027 (int)(lpos->end - lpos->start));
7028 }
7029 }
7030 }
7031#endif
7032
Bram Moolenaar0270f382018-07-17 05:43:58 +02007033 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007034}
7035
7036/*
7037 * Match a regexp against a string ("line" points to the string) or multiple
7038 * lines ("line" is NULL, use reg_getline()).
7039 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007040 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007041 */
7042 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007043nfa_regexec_both(
7044 char_u *line,
7045 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007046 proftime_T *tm, /* timeout limit or NULL */
7047 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048{
7049 nfa_regprog_T *prog;
7050 long retval = 0L;
7051 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007052 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007053
7054 if (REG_MULTI)
7055 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007056 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007057 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007058 rex.reg_startpos = rex.reg_mmatch->startpos;
7059 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007060 }
7061 else
7062 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007063 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7064 rex.reg_startp = rex.reg_match->startp;
7065 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007066 }
7067
7068 /* Be paranoid... */
7069 if (prog == NULL || line == NULL)
7070 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007071 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007072 goto theend;
7073 }
7074
Bram Moolenaar6100d022016-10-02 16:51:57 +02007075 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007076 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007077 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007078 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007079 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007080
Bram Moolenaar6100d022016-10-02 16:51:57 +02007081 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007082 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007083 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007084
Bram Moolenaar0270f382018-07-17 05:43:58 +02007085 rex.line = line;
7086 rex.lnum = 0; /* relative to line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007087
Bram Moolenaar0270f382018-07-17 05:43:58 +02007088 rex.nfa_has_zend = prog->has_zend;
7089 rex.nfa_has_backref = prog->has_backref;
7090 rex.nfa_nsubexpr = prog->nsubexp;
7091 rex.nfa_listid = 1;
7092 rex.nfa_alt_listid = 2;
7093#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007094 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007095#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007096
Bram Moolenaard89616e2013-06-06 18:46:06 +02007097 if (prog->reganch && col > 0)
7098 return 0L;
7099
Bram Moolenaar0270f382018-07-17 05:43:58 +02007100 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007101#ifdef FEAT_SYN_HL
7102 /* Clear the external match subpointers if necessary. */
7103 if (prog->reghasz == REX_SET)
7104 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007105 rex.nfa_has_zsubexpr = TRUE;
7106 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007107 }
7108 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007109 {
7110 rex.nfa_has_zsubexpr = FALSE;
7111 rex.need_clear_zsubexpr = FALSE;
7112 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007113#endif
7114
Bram Moolenaard89616e2013-06-06 18:46:06 +02007115 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007116 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007117 /* Skip ahead until a character we know the match must start with.
7118 * When there is none there is no match. */
7119 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007120 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007121
Bram Moolenaar473de612013-06-08 18:19:48 +02007122 /* If match_text is set it contains the full text that must match.
7123 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01007124 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007125 return find_match_text(col, prog->regstart, prog->match_text);
7126 }
7127
Bram Moolenaard89616e2013-06-06 18:46:06 +02007128 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007129 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007130 goto theend;
7131
Bram Moolenaar0270f382018-07-17 05:43:58 +02007132 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7133 // it's accidentally used during execution.
7134 nstate = 0;
7135 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007136 {
7137 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007138 prog->state[i].lastlist[0] = 0;
7139 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140 }
7141
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007142 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007143
Bram Moolenaar0270f382018-07-17 05:43:58 +02007144#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007145 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007146#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007147
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007148theend:
7149 return retval;
7150}
7151
7152/*
7153 * Compile a regular expression into internal code for the NFA matcher.
7154 * Returns the program in allocated space. Returns NULL for an error.
7155 */
7156 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007157nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007159 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007160 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007161 int *postfix;
7162
7163 if (expr == NULL)
7164 return NULL;
7165
Bram Moolenaar0270f382018-07-17 05:43:58 +02007166#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007167 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007168#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007169 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170
7171 init_class_tab();
7172
7173 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7174 return NULL;
7175
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007176 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007177 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007178 postfix = re2post();
7179 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007180 {
7181 /* TODO: only give this error for debugging? */
7182 if (post_ptr >= post_end)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007183 siemsg("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007184 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007185 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007186
7187 /*
7188 * In order to build the NFA, we parse the input regexp twice:
7189 * 1. first pass to count size (so we can allocate space)
7190 * 2. second to emit code
7191 */
7192#ifdef ENABLE_LOG
7193 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007194 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007195
7196 if (f != NULL)
7197 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007198 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007199 fclose(f);
7200 }
7201 }
7202#endif
7203
7204 /*
7205 * PASS 1
7206 * Count number of NFA states in "nstate". Do not build the NFA.
7207 */
7208 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007209
Bram Moolenaar16619a22013-06-11 18:42:36 +02007210 /* allocate the regprog with space for the compiled regexp */
7211 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007212 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7213 if (prog == NULL)
7214 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007215 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007216 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007217
7218 /*
7219 * PASS 2
7220 * Build the NFA
7221 */
7222 prog->start = post2nfa(postfix, post_ptr, FALSE);
7223 if (prog->start == NULL)
7224 goto fail;
7225
7226 prog->regflags = regflags;
7227 prog->engine = &nfa_regengine;
7228 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007229 prog->has_zend = rex.nfa_has_zend;
7230 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007231 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007232
Bram Moolenaara2947e22013-06-11 22:44:09 +02007233 nfa_postprocess(prog);
7234
Bram Moolenaard89616e2013-06-06 18:46:06 +02007235 prog->reganch = nfa_get_reganch(prog->start, 0);
7236 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007237 prog->match_text = nfa_get_match_text(prog->start);
7238
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007239#ifdef ENABLE_LOG
7240 nfa_postfix_dump(expr, OK);
7241 nfa_dump(prog);
7242#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007243#ifdef FEAT_SYN_HL
7244 /* Remember whether this pattern has any \z specials in it. */
7245 prog->reghasz = re_has_z;
7246#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007247 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007248#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007249 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007250#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007251
7252out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007253 VIM_CLEAR(post_start);
7254 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007255 state_ptr = NULL;
7256 return (regprog_T *)prog;
7257
7258fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007259 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007260#ifdef ENABLE_LOG
7261 nfa_postfix_dump(expr, FAIL);
7262#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007263#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007264 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007265#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007266 goto out;
7267}
7268
Bram Moolenaar473de612013-06-08 18:19:48 +02007269/*
7270 * Free a compiled regexp program, returned by nfa_regcomp().
7271 */
7272 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007273nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007274{
7275 if (prog != NULL)
7276 {
7277 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007278 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007279 vim_free(prog);
7280 }
7281}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007282
7283/*
7284 * Match a regexp against a string.
7285 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7286 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007287 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007288 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007289 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007290 */
7291 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007292nfa_regexec_nl(
7293 regmatch_T *rmp,
7294 char_u *line, /* string to match against */
7295 colnr_T col, /* column to start looking for match */
7296 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007297{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007298 rex.reg_match = rmp;
7299 rex.reg_mmatch = NULL;
7300 rex.reg_maxline = 0;
7301 rex.reg_line_lbr = line_lbr;
7302 rex.reg_buf = curbuf;
7303 rex.reg_win = NULL;
7304 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007305 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007306 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007307 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007308}
7309
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007310
7311/*
7312 * Match a regexp against multiple lines.
7313 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7314 * Uses curbuf for line count and 'iskeyword'.
7315 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007316 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007317 * match otherwise.
7318 *
7319 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7320 *
7321 * ! Also NOTE : match may actually be in another line. e.g.:
7322 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7323 *
7324 * +-------------------------+
7325 * |a |
7326 * |b |
7327 * |c |
7328 * | |
7329 * +-------------------------+
7330 *
7331 * then nfa_regexec_multi() returns 3. while the original
7332 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7333 *
7334 * FIXME if this behavior is not compatible.
7335 */
7336 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007337nfa_regexec_multi(
7338 regmmatch_T *rmp,
7339 win_T *win, /* window in which to search or NULL */
7340 buf_T *buf, /* buffer in which to search */
7341 linenr_T lnum, /* nr of line to start looking for match */
7342 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007343 proftime_T *tm, /* timeout limit or NULL */
7344 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007345{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007346 rex.reg_match = NULL;
7347 rex.reg_mmatch = rmp;
7348 rex.reg_buf = buf;
7349 rex.reg_win = win;
7350 rex.reg_firstlnum = lnum;
7351 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7352 rex.reg_line_lbr = FALSE;
7353 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007354 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007355 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007356
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007357 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007358}
7359
7360#ifdef DEBUG
7361# undef ENABLE_LOG
7362#endif