blob: d72430faddac76b324ba06c549745cf767bed0c4 [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,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100286static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100288static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100290static int match_follows(nfa_state_T *startstate, int depth);
291static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292
293/* helper functions used when doing re2post() ... regatom() parsing */
294#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200295 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200296 return FAIL; \
297 *post_ptr++ = c; \
298 } while (0)
299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200300/*
301 * Initialize internal variables before NFA compilation.
302 * Return OK on success, FAIL otherwise.
303 */
304 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305nfa_regcomp_start(
306 char_u *expr,
307 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200309 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200310 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311
312 nstate = 0;
313 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200314 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200315 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200317 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200318 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200319 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200320
321 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200322 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200323
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324 post_start = (int *)lalloc(postfix_size, TRUE);
325 if (post_start == NULL)
326 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200328 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200329 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200330 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200331
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200332 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200333 regcomp_start(expr, re_flags);
334
335 return OK;
336}
337
338/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200339 * Figure out if the NFA state list starts with an anchor, must match at start
340 * of the line.
341 */
342 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100343nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200344{
345 nfa_state_T *p = start;
346
347 if (depth > 4)
348 return 0;
349
350 while (p != NULL)
351 {
352 switch (p->c)
353 {
354 case NFA_BOL:
355 case NFA_BOF:
356 return 1; /* yes! */
357
358 case NFA_ZSTART:
359 case NFA_ZEND:
360 case NFA_CURSOR:
361 case NFA_VISUAL:
362
363 case NFA_MOPEN:
364 case NFA_MOPEN1:
365 case NFA_MOPEN2:
366 case NFA_MOPEN3:
367 case NFA_MOPEN4:
368 case NFA_MOPEN5:
369 case NFA_MOPEN6:
370 case NFA_MOPEN7:
371 case NFA_MOPEN8:
372 case NFA_MOPEN9:
373 case NFA_NOPEN:
374#ifdef FEAT_SYN_HL
375 case NFA_ZOPEN:
376 case NFA_ZOPEN1:
377 case NFA_ZOPEN2:
378 case NFA_ZOPEN3:
379 case NFA_ZOPEN4:
380 case NFA_ZOPEN5:
381 case NFA_ZOPEN6:
382 case NFA_ZOPEN7:
383 case NFA_ZOPEN8:
384 case NFA_ZOPEN9:
385#endif
386 p = p->out;
387 break;
388
389 case NFA_SPLIT:
390 return nfa_get_reganch(p->out, depth + 1)
391 && nfa_get_reganch(p->out1, depth + 1);
392
393 default:
394 return 0; /* noooo */
395 }
396 }
397 return 0;
398}
399
400/*
401 * Figure out if the NFA state list starts with a character which must match
402 * at start of the match.
403 */
404 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100405nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200406{
407 nfa_state_T *p = start;
408
409 if (depth > 4)
410 return 0;
411
412 while (p != NULL)
413 {
414 switch (p->c)
415 {
416 /* all kinds of zero-width matches */
417 case NFA_BOL:
418 case NFA_BOF:
419 case NFA_BOW:
420 case NFA_EOW:
421 case NFA_ZSTART:
422 case NFA_ZEND:
423 case NFA_CURSOR:
424 case NFA_VISUAL:
425 case NFA_LNUM:
426 case NFA_LNUM_GT:
427 case NFA_LNUM_LT:
428 case NFA_COL:
429 case NFA_COL_GT:
430 case NFA_COL_LT:
431 case NFA_VCOL:
432 case NFA_VCOL_GT:
433 case NFA_VCOL_LT:
434 case NFA_MARK:
435 case NFA_MARK_GT:
436 case NFA_MARK_LT:
437
438 case NFA_MOPEN:
439 case NFA_MOPEN1:
440 case NFA_MOPEN2:
441 case NFA_MOPEN3:
442 case NFA_MOPEN4:
443 case NFA_MOPEN5:
444 case NFA_MOPEN6:
445 case NFA_MOPEN7:
446 case NFA_MOPEN8:
447 case NFA_MOPEN9:
448 case NFA_NOPEN:
449#ifdef FEAT_SYN_HL
450 case NFA_ZOPEN:
451 case NFA_ZOPEN1:
452 case NFA_ZOPEN2:
453 case NFA_ZOPEN3:
454 case NFA_ZOPEN4:
455 case NFA_ZOPEN5:
456 case NFA_ZOPEN6:
457 case NFA_ZOPEN7:
458 case NFA_ZOPEN8:
459 case NFA_ZOPEN9:
460#endif
461 p = p->out;
462 break;
463
464 case NFA_SPLIT:
465 {
466 int c1 = nfa_get_regstart(p->out, depth + 1);
467 int c2 = nfa_get_regstart(p->out1, depth + 1);
468
469 if (c1 == c2)
470 return c1; /* yes! */
471 return 0;
472 }
473
474 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200475 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200476 return p->c; /* yes! */
477 return 0;
478 }
479 }
480 return 0;
481}
482
483/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200484 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200485 * else. If so return a string in allocated memory with what must match after
486 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200487 */
488 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100489nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200490{
491 nfa_state_T *p = start;
492 int len = 0;
493 char_u *ret;
494 char_u *s;
495
496 if (p->c != NFA_MOPEN)
497 return NULL; /* just in case */
498 p = p->out;
499 while (p->c > 0)
500 {
501 len += MB_CHAR2LEN(p->c);
502 p = p->out;
503 }
504 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
505 return NULL;
506
507 ret = alloc(len);
508 if (ret != NULL)
509 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200510 p = start->out->out; /* skip first char, it goes into regstart */
511 s = ret;
512 while (p->c > 0)
513 {
514#ifdef FEAT_MBYTE
515 if (has_mbyte)
516 s += (*mb_char2bytes)(p->c, s);
517 else
518#endif
519 *s++ = p->c;
520 p = p->out;
521 }
522 *s = NUL;
523 }
524 return ret;
525}
526
527/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200528 * Allocate more space for post_start. Called when
529 * running above the estimated number of states.
530 */
531 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100532realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200533{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200534 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200535 int new_max = nstate_max + 1000;
536 int *new_start;
537 int *old_start;
538
539 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
540 if (new_start == NULL)
541 return FAIL;
542 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200543 old_start = post_start;
544 post_start = new_start;
545 post_ptr = new_start + (post_ptr - old_start);
546 post_end = post_start + new_max;
547 vim_free(old_start);
548 return OK;
549}
550
551/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200552 * Search between "start" and "end" and try to recognize a
553 * character class in expanded form. For example [0-9].
554 * On success, return the id the character class to be emitted.
555 * On failure, return 0 (=FAIL)
556 * Start points to the first char of the range, while end should point
557 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200558 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
559 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200560 */
561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100562nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200563{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200564# define CLASS_not 0x80
565# define CLASS_af 0x40
566# define CLASS_AF 0x20
567# define CLASS_az 0x10
568# define CLASS_AZ 0x08
569# define CLASS_o7 0x04
570# define CLASS_o9 0x02
571# define CLASS_underscore 0x01
572
573 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200574 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200575 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200576
577 if (extra_newl == TRUE)
578 newl = TRUE;
579
580 if (*end != ']')
581 return FAIL;
582 p = start;
583 if (*p == '^')
584 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200585 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200586 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200587 }
588
589 while (p < end)
590 {
591 if (p + 2 < end && *(p + 1) == '-')
592 {
593 switch (*p)
594 {
595 case '0':
596 if (*(p + 2) == '9')
597 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200598 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200599 break;
600 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200601 if (*(p + 2) == '7')
602 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200603 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 break;
605 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200606 return FAIL;
607
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200608 case 'a':
609 if (*(p + 2) == 'z')
610 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200611 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200612 break;
613 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200614 if (*(p + 2) == 'f')
615 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200616 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 break;
618 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200619 return FAIL;
620
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200621 case 'A':
622 if (*(p + 2) == 'Z')
623 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200624 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200625 break;
626 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200627 if (*(p + 2) == 'F')
628 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200629 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200630 break;
631 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200632 return FAIL;
633
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 default:
635 return FAIL;
636 }
637 p += 3;
638 }
639 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
640 {
641 newl = TRUE;
642 p += 2;
643 }
644 else if (*p == '_')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 p ++;
648 }
649 else if (*p == '\n')
650 {
651 newl = TRUE;
652 p ++;
653 }
654 else
655 return FAIL;
656 } /* while (p < end) */
657
658 if (p != end)
659 return FAIL;
660
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200661 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200662 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200663
664 switch (config)
665 {
666 case CLASS_o9:
667 return extra_newl + NFA_DIGIT;
668 case CLASS_not | CLASS_o9:
669 return extra_newl + NFA_NDIGIT;
670 case CLASS_af | CLASS_AF | CLASS_o9:
671 return extra_newl + NFA_HEX;
672 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
673 return extra_newl + NFA_NHEX;
674 case CLASS_o7:
675 return extra_newl + NFA_OCTAL;
676 case CLASS_not | CLASS_o7:
677 return extra_newl + NFA_NOCTAL;
678 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
679 return extra_newl + NFA_WORD;
680 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
681 return extra_newl + NFA_NWORD;
682 case CLASS_az | CLASS_AZ | CLASS_underscore:
683 return extra_newl + NFA_HEAD;
684 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
685 return extra_newl + NFA_NHEAD;
686 case CLASS_az | CLASS_AZ:
687 return extra_newl + NFA_ALPHA;
688 case CLASS_not | CLASS_az | CLASS_AZ:
689 return extra_newl + NFA_NALPHA;
690 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200691 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200692 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200693 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200694 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200695 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200696 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200697 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200699 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200700}
701
702/*
703 * Produce the bytes for equivalence class "c".
704 * Currently only handles latin1, latin9 and utf-8.
705 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
706 * equivalent to 'a OR b OR c'
707 *
708 * NOTE! When changing this function, also update reg_equi_class()
709 */
710 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100711nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200712{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200713#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
714#ifdef FEAT_MBYTE
715# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
716#else
717# define EMITMBC(c)
718#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200719
720#ifdef FEAT_MBYTE
721 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
722 || STRCMP(p_enc, "iso-8859-15") == 0)
723#endif
724 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200725#ifdef EBCDIC
726# define A_circumflex 0x62
727# define A_diaeresis 0x63
728# define A_grave 0x64
729# define A_acute 0x65
730# define A_virguilla 0x66
731# define A_ring 0x67
732# define C_cedilla 0x68
733# define E_acute 0x71
734# define E_circumflex 0x72
735# define E_diaeresis 0x73
736# define E_grave 0x74
737# define I_acute 0x75
738# define I_circumflex 0x76
739# define I_diaeresis 0x77
740# define I_grave 0x78
741# define N_virguilla 0x69
742# define O_circumflex 0xeb
743# define O_diaeresis 0xec
744# define O_grave 0xed
745# define O_acute 0xee
746# define O_virguilla 0xef
747# define O_slash 0x80
748# define U_circumflex 0xfb
749# define U_diaeresis 0xfc
750# define U_grave 0xfd
751# define U_acute 0xfe
752# define Y_acute 0xba
753# define a_grave 0x42
754# define a_acute 0x43
755# define a_circumflex 0x44
756# define a_virguilla 0x45
757# define a_diaeresis 0x46
758# define a_ring 0x47
759# define c_cedilla 0x48
760# define e_grave 0x51
761# define e_acute 0x52
762# define e_circumflex 0x53
763# define e_diaeresis 0x54
764# define i_grave 0x55
765# define i_acute 0x56
766# define i_circumflex 0x57
767# define i_diaeresis 0x58
768# define n_virguilla 0x49
769# define o_grave 0xcb
770# define o_acute 0xcc
771# define o_circumflex 0xcd
772# define o_virguilla 0xce
773# define o_diaeresis 0xcf
774# define o_slash 0x70
775# define u_grave 0xdb
776# define u_acute 0xdc
777# define u_circumflex 0xdd
778# define u_diaeresis 0xde
779# define y_acute 0x8d
780# define y_diaeresis 0xdf
781#else
782# define A_grave 0xc0
783# define A_acute 0xc1
784# define A_circumflex 0xc2
785# define A_virguilla 0xc3
786# define A_diaeresis 0xc4
787# define A_ring 0xc5
788# define C_cedilla 0xc7
789# define E_grave 0xc8
790# define E_acute 0xc9
791# define E_circumflex 0xca
792# define E_diaeresis 0xcb
793# define I_grave 0xcc
794# define I_acute 0xcd
795# define I_circumflex 0xce
796# define I_diaeresis 0xcf
797# define N_virguilla 0xd1
798# define O_grave 0xd2
799# define O_acute 0xd3
800# define O_circumflex 0xd4
801# define O_virguilla 0xd5
802# define O_diaeresis 0xd6
803# define O_slash 0xd8
804# define U_grave 0xd9
805# define U_acute 0xda
806# define U_circumflex 0xdb
807# define U_diaeresis 0xdc
808# define Y_acute 0xdd
809# define a_grave 0xe0
810# define a_acute 0xe1
811# define a_circumflex 0xe2
812# define a_virguilla 0xe3
813# define a_diaeresis 0xe4
814# define a_ring 0xe5
815# define c_cedilla 0xe7
816# define e_grave 0xe8
817# define e_acute 0xe9
818# define e_circumflex 0xea
819# define e_diaeresis 0xeb
820# define i_grave 0xec
821# define i_acute 0xed
822# define i_circumflex 0xee
823# define i_diaeresis 0xef
824# define n_virguilla 0xf1
825# define o_grave 0xf2
826# define o_acute 0xf3
827# define o_circumflex 0xf4
828# define o_virguilla 0xf5
829# define o_diaeresis 0xf6
830# define o_slash 0xf8
831# define u_grave 0xf9
832# define u_acute 0xfa
833# define u_circumflex 0xfb
834# define u_diaeresis 0xfc
835# define y_acute 0xfd
836# define y_diaeresis 0xff
837#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200838 switch (c)
839 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200840 case 'A': case A_grave: case A_acute: case A_circumflex:
841 case A_virguilla: case A_diaeresis: case A_ring:
842 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
843 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
844 CASEMBC(0x1ea2)
845 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
846 EMIT2(A_circumflex); EMIT2(A_virguilla);
847 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200848 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
849 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
850 EMITMBC(0x1ea2)
851 return OK;
852
853 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
854 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200855 return OK;
856
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200857 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
858 CASEMBC(0x10a) CASEMBC(0x10c)
859 EMIT2('C'); EMIT2(C_cedilla);
860 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200861 EMITMBC(0x10a) EMITMBC(0x10c)
862 return OK;
863
864 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200865 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200866 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
867 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 return OK;
869
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'E': case E_grave: case E_acute: case E_circumflex:
871 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
872 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
873 CASEMBC(0x1eba) CASEMBC(0x1ebc)
874 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
875 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200876 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
877 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
878 EMITMBC(0x1ebc)
879 return OK;
880
881 case 'F': CASEMBC(0x1e1e)
882 EMIT2('F'); EMITMBC(0x1e1e)
883 return OK;
884
885 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200886 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
887 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200888 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
889 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
890 EMITMBC(0x1f4) EMITMBC(0x1e20)
891 return OK;
892
893 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200894 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200895 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
896 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200897 return OK;
898
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200899 case 'I': case I_grave: case I_acute: case I_circumflex:
900 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
901 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
902 CASEMBC(0x1cf) CASEMBC(0x1ec8)
903 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
904 EMIT2(I_circumflex); EMIT2(I_diaeresis);
905 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200906 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
907 EMITMBC(0x1cf) EMITMBC(0x1ec8)
908 return OK;
909
910 case 'J': CASEMBC(0x134)
911 EMIT2('J'); EMITMBC(0x134)
912 return OK;
913
914 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200915 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
917 EMITMBC(0x1e34)
918 return OK;
919
920 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200921 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200922 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
923 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
924 return OK;
925
926 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
927 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200928 return OK;
929
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200930 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
931 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
932 EMIT2('N'); EMIT2(N_virguilla);
933 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200934 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200935 return OK;
936
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200937 case 'O': case O_grave: case O_acute: case O_circumflex:
938 case O_virguilla: case O_diaeresis: case O_slash:
939 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
940 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
941 CASEMBC(0x1ec) CASEMBC(0x1ece)
942 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
943 EMIT2(O_circumflex); EMIT2(O_virguilla);
944 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200945 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
946 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
947 EMITMBC(0x1ec) EMITMBC(0x1ece)
948 return OK;
949
950 case 'P': case 0x1e54: case 0x1e56:
951 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
952 return OK;
953
954 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200955 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200956 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
957 EMITMBC(0x1e58) EMITMBC(0x1e5e)
958 return OK;
959
960 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200961 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200962 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
963 EMITMBC(0x160) EMITMBC(0x1e60)
964 return OK;
965
966 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200967 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200968 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
969 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200970 return OK;
971
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200972 case 'U': case U_grave: case U_acute: case U_diaeresis:
973 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
974 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
975 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
976 CASEMBC(0x1ee6)
977 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
978 EMIT2(U_diaeresis); EMIT2(U_circumflex);
979 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200980 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
981 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
982 EMITMBC(0x1ee6)
983 return OK;
984
985 case 'V': CASEMBC(0x1e7c)
986 EMIT2('V'); EMITMBC(0x1e7c)
987 return OK;
988
989 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200990 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200991 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
992 EMITMBC(0x1e84) EMITMBC(0x1e86)
993 return OK;
994
995 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
996 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200997 return OK;
998
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200999 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1000 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1001 CASEMBC(0x1ef8)
1002 EMIT2('Y'); EMIT2(Y_acute);
1003 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001004 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1005 EMITMBC(0x1ef8)
1006 return OK;
1007
1008 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001009 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1011 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001012 return OK;
1013
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001014 case 'a': case a_grave: case a_acute: case a_circumflex:
1015 case a_virguilla: case a_diaeresis: case a_ring:
1016 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1017 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1018 CASEMBC(0x1ea3)
1019 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1020 EMIT2(a_circumflex); EMIT2(a_virguilla);
1021 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001022 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1023 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1024 EMITMBC(0x1ea3)
1025 return OK;
1026
1027 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1028 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001029 return OK;
1030
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001031 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1032 CASEMBC(0x10b) CASEMBC(0x10d)
1033 EMIT2('c'); EMIT2(c_cedilla);
1034 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001035 EMITMBC(0x10b) EMITMBC(0x10d)
1036 return OK;
1037
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001038 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001040 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1041 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'e': case e_grave: case e_acute: case e_circumflex:
1045 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1046 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1047 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1048 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1049 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1050 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001051 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1052 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1053 return OK;
1054
1055 case 'f': CASEMBC(0x1e1f)
1056 EMIT2('f'); EMITMBC(0x1e1f)
1057 return OK;
1058
1059 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001060 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1061 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001062 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1063 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1064 EMITMBC(0x1f5) EMITMBC(0x1e21)
1065 return OK;
1066
1067 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001068 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001069 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1070 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001071 return OK;
1072
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001073 case 'i': case i_grave: case i_acute: case i_circumflex:
1074 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1075 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1076 CASEMBC(0x1ec9)
1077 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1078 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1079 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001080 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1081 EMITMBC(0x1ec9)
1082 return OK;
1083
1084 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1085 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1086 return OK;
1087
1088 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001089 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001090 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1091 EMITMBC(0x1e35)
1092 return OK;
1093
1094 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001095 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001096 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1097 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1098 return OK;
1099
1100 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1101 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001102 return OK;
1103
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001104 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1105 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1106 CASEMBC(0x1e49)
1107 EMIT2('n'); EMIT2(n_virguilla);
1108 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001109 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1110 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001111 return OK;
1112
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001113 case 'o': case o_grave: case o_acute: case o_circumflex:
1114 case o_virguilla: case o_diaeresis: case o_slash:
1115 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1116 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1117 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1118 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1119 EMIT2(o_circumflex); EMIT2(o_virguilla);
1120 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001121 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1122 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1123 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1124 return OK;
1125
1126 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1127 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1128 return OK;
1129
1130 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001131 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001132 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1133 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1134 return OK;
1135
1136 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001137 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001138 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1139 EMITMBC(0x161) EMITMBC(0x1e61)
1140 return OK;
1141
1142 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001143 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001144 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1145 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001146 return OK;
1147
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001148 case 'u': case u_grave: case u_acute: case u_circumflex:
1149 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1150 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1151 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1152 CASEMBC(0x1ee7)
1153 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1154 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1155 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001156 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1157 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1158 EMITMBC(0x1ee7)
1159 return OK;
1160
1161 case 'v': CASEMBC(0x1e7d)
1162 EMIT2('v'); EMITMBC(0x1e7d)
1163 return OK;
1164
1165 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001166 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001167 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1168 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1169 return OK;
1170
1171 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1172 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001173 return OK;
1174
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001175 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1176 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1177 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1178 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1179 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001180 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1181 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001182 return OK;
1183
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001184 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001185 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001186 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1187 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1188 return OK;
1189
1190 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001191 }
1192 }
1193
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001194 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001195 return OK;
1196#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001197#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001198}
1199
1200/*
1201 * Code to parse regular expression.
1202 *
1203 * We try to reuse parsing functions in regexp.c to
1204 * minimize surprise and keep the syntax consistent.
1205 */
1206
1207/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 * Parse the lowest level.
1209 *
1210 * An atom can be one of a long list of items. Many atoms match one character
1211 * in the text. It is often an ordinary character or a character class.
1212 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1213 * is only for syntax highlighting.
1214 *
1215 * atom ::= ordinary-atom
1216 * or \( pattern \)
1217 * or \%( pattern \)
1218 * or \z( pattern \)
1219 */
1220 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001221nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222{
1223 int c;
1224 int charclass;
1225 int equiclass;
1226 int collclass;
1227 int got_coll_char;
1228 char_u *p;
1229 char_u *endp;
1230#ifdef FEAT_MBYTE
1231 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001232#endif
1233 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 int emit_range;
1235 int negated;
1236 int result;
1237 int startc = -1;
1238 int endc = -1;
1239 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001240 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001241
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001242 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001243 switch (c)
1244 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001245 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001246 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001247
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001248 case Magic('^'):
1249 EMIT(NFA_BOL);
1250 break;
1251
1252 case Magic('$'):
1253 EMIT(NFA_EOL);
1254#if defined(FEAT_SYN_HL) || defined(PROTO)
1255 had_eol = TRUE;
1256#endif
1257 break;
1258
1259 case Magic('<'):
1260 EMIT(NFA_BOW);
1261 break;
1262
1263 case Magic('>'):
1264 EMIT(NFA_EOW);
1265 break;
1266
1267 case Magic('_'):
1268 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001269 if (c == NUL)
1270 EMSG_RET_FAIL(_(e_nul_found));
1271
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 if (c == '^') /* "\_^" is start-of-line */
1273 {
1274 EMIT(NFA_BOL);
1275 break;
1276 }
1277 if (c == '$') /* "\_$" is end-of-line */
1278 {
1279 EMIT(NFA_EOL);
1280#if defined(FEAT_SYN_HL) || defined(PROTO)
1281 had_eol = TRUE;
1282#endif
1283 break;
1284 }
1285
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001286 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001287
1288 /* "\_[" is collection plus newline */
1289 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001290 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001291
1292 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001293 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001294
1295 /*
1296 * Character classes.
1297 */
1298 case Magic('.'):
1299 case Magic('i'):
1300 case Magic('I'):
1301 case Magic('k'):
1302 case Magic('K'):
1303 case Magic('f'):
1304 case Magic('F'):
1305 case Magic('p'):
1306 case Magic('P'):
1307 case Magic('s'):
1308 case Magic('S'):
1309 case Magic('d'):
1310 case Magic('D'):
1311 case Magic('x'):
1312 case Magic('X'):
1313 case Magic('o'):
1314 case Magic('O'):
1315 case Magic('w'):
1316 case Magic('W'):
1317 case Magic('h'):
1318 case Magic('H'):
1319 case Magic('a'):
1320 case Magic('A'):
1321 case Magic('l'):
1322 case Magic('L'):
1323 case Magic('u'):
1324 case Magic('U'):
1325 p = vim_strchr(classchars, no_Magic(c));
1326 if (p == NULL)
1327 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001328 if (extra == NFA_ADD_NL)
1329 {
1330 EMSGN(_(e_ill_char_class), c);
1331 rc_did_emsg = TRUE;
1332 return FAIL;
1333 }
Bram Moolenaarde330112017-01-08 20:50:52 +01001334 IEMSGN("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001335 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 }
1337#ifdef FEAT_MBYTE
1338 /* When '.' is followed by a composing char ignore the dot, so that
1339 * the composing char is matched here. */
1340 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1341 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001342 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 c = getchr();
1344 goto nfa_do_multibyte;
1345 }
1346#endif
1347 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001348 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 {
1350 EMIT(NFA_NEWL);
1351 EMIT(NFA_OR);
1352 regflags |= RF_HASNL;
1353 }
1354 break;
1355
1356 case Magic('n'):
1357 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001358 /* In a string "\n" matches a newline character. */
1359 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001360 else
1361 {
1362 /* In buffer text "\n" matches the end of a line. */
1363 EMIT(NFA_NEWL);
1364 regflags |= RF_HASNL;
1365 }
1366 break;
1367
1368 case Magic('('):
1369 if (nfa_reg(REG_PAREN) == FAIL)
1370 return FAIL; /* cascaded error */
1371 break;
1372
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001373 case Magic('|'):
1374 case Magic('&'):
1375 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001376 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 return FAIL;
1378
1379 case Magic('='):
1380 case Magic('?'):
1381 case Magic('+'):
1382 case Magic('@'):
1383 case Magic('*'):
1384 case Magic('{'):
1385 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001386 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001387 return FAIL;
1388
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001389 case Magic('~'):
1390 {
1391 char_u *lp;
1392
1393 /* Previous substitute pattern.
1394 * Generated as "\%(pattern\)". */
1395 if (reg_prev_sub == NULL)
1396 {
1397 EMSG(_(e_nopresub));
1398 return FAIL;
1399 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001400 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001401 {
1402 EMIT(PTR2CHAR(lp));
1403 if (lp != reg_prev_sub)
1404 EMIT(NFA_CONCAT);
1405 }
1406 EMIT(NFA_NOPEN);
1407 break;
1408 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001409
Bram Moolenaar428e9872013-05-30 17:05:39 +02001410 case Magic('1'):
1411 case Magic('2'):
1412 case Magic('3'):
1413 case Magic('4'):
1414 case Magic('5'):
1415 case Magic('6'):
1416 case Magic('7'):
1417 case Magic('8'):
1418 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001419 {
1420 int refnum = no_Magic(c) - '1';
1421
1422 if (!seen_endbrace(refnum + 1))
1423 return FAIL;
1424 EMIT(NFA_BACKREF1 + refnum);
1425 nfa_has_backref = TRUE;
1426 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001427 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001428
1429 case Magic('z'):
1430 c = no_Magic(getchr());
1431 switch (c)
1432 {
1433 case 's':
1434 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001435 if (re_mult_next("\\zs") == FAIL)
1436 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 break;
1438 case 'e':
1439 EMIT(NFA_ZEND);
1440 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001441 if (re_mult_next("\\ze") == FAIL)
1442 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001443 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001444#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 case '1':
1446 case '2':
1447 case '3':
1448 case '4':
1449 case '5':
1450 case '6':
1451 case '7':
1452 case '8':
1453 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001454 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001455 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001456 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001457 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1458 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001459 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001460 re_has_z = REX_USE;
1461 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001463 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001464 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001465 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001466 if (nfa_reg(REG_ZPAREN) == FAIL)
1467 return FAIL; /* cascaded error */
1468 re_has_z = REX_SET;
1469 break;
1470#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001471 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001472 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001473 no_Magic(c));
1474 return FAIL;
1475 }
1476 break;
1477
1478 case Magic('%'):
1479 c = no_Magic(getchr());
1480 switch (c)
1481 {
1482 /* () without a back reference */
1483 case '(':
1484 if (nfa_reg(REG_NPAREN) == FAIL)
1485 return FAIL;
1486 EMIT(NFA_NOPEN);
1487 break;
1488
1489 case 'd': /* %d123 decimal */
1490 case 'o': /* %o123 octal */
1491 case 'x': /* %xab hex 2 */
1492 case 'u': /* %uabcd hex 4 */
1493 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001494 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001495 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496
Bram Moolenaar47196582013-05-25 22:04:23 +02001497 switch (c)
1498 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001499 case 'd': nr = getdecchrs(); break;
1500 case 'o': nr = getoctchrs(); break;
1501 case 'x': nr = gethexchrs(2); break;
1502 case 'u': nr = gethexchrs(4); break;
1503 case 'U': nr = gethexchrs(8); break;
1504 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001505 }
1506
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001507 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001508 EMSG2_RET_FAIL(
1509 _("E678: Invalid character after %s%%[dxouU]"),
1510 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001511 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001512 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001513 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001514 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001515 break;
1516
1517 /* Catch \%^ and \%$ regardless of where they appear in the
1518 * pattern -- regardless of whether or not it makes sense. */
1519 case '^':
1520 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 break;
1522
1523 case '$':
1524 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 break;
1526
1527 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001528 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001529 break;
1530
1531 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001532 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 break;
1534
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001535 case 'C':
1536 EMIT(NFA_ANY_COMPOSING);
1537 break;
1538
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001540 {
1541 int n;
1542
1543 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001544 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001545 {
1546 if (c == NUL)
1547 EMSG2_RET_FAIL(_(e_missing_sb),
1548 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001549 /* recursive call! */
1550 if (nfa_regatom() == FAIL)
1551 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001552 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001553 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001554 if (n == 0)
1555 EMSG2_RET_FAIL(_(e_empty_sb),
1556 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001557 EMIT(NFA_OPT_CHARS);
1558 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001559
1560 /* Emit as "\%(\%[abc]\)" to be able to handle
1561 * "\%[abc]*" which would cause the empty string to be
1562 * matched an unlimited number of times. NFA_NOPEN is
1563 * added only once at a position, while NFA_SPLIT is
1564 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001565 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001566 * a lot. */
1567 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001568 break;
1569 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001570
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001571 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001572 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001573 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001574 int cmp = c;
1575
1576 if (c == '<' || c == '>')
1577 c = getchr();
1578 while (VIM_ISDIGIT(c))
1579 {
1580 n = n * 10 + (c - '0');
1581 c = getchr();
1582 }
1583 if (c == 'l' || c == 'c' || c == 'v')
1584 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001585 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001586 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001587 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001588 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001589 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001590 if (save_prev_at_start)
1591 at_start = TRUE;
1592 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001593 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001594 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001595 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001596 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001597 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001598 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001599 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001600 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001601#if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
1602 if (n > INT_MAX)
1603 {
1604 EMSG(_("E951: \\% value too large"));
1605 return FAIL;
1606 }
1607#endif
1608 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001609 break;
1610 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001611 else if (c == '\'' && n == 0)
1612 {
1613 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001614 EMIT(cmp == '<' ? NFA_MARK_LT :
1615 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001616 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001617 break;
1618 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001619 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001620 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1621 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001622 return FAIL;
1623 }
1624 break;
1625
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001627collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001629 * [abc] uses NFA_START_COLL - NFA_END_COLL
1630 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1631 * Each character is produced as a regular state, using
1632 * NFA_CONCAT to bind them together.
1633 * Besides normal characters there can be:
1634 * - character classes NFA_CLASS_*
1635 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001636 */
1637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001638 p = regparse;
1639 endp = skip_anyof(p);
1640 if (*endp == ']')
1641 {
1642 /*
1643 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001644 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 * and perform the necessary substitutions in the NFA.
1646 */
1647 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001648 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001649 if (result != FAIL)
1650 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001651 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001652 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001653 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001654 EMIT(NFA_NEWL);
1655 EMIT(NFA_OR);
1656 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001657 else
1658 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001660 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 return OK;
1662 }
1663 /*
1664 * Failed to recognize a character class. Use the simple
1665 * version that turns [abc] into 'a' OR 'b' OR 'c'
1666 */
1667 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 if (*regparse == '^') /* negated range */
1670 {
1671 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001672 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001673 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001675 else
1676 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 if (*regparse == '-')
1678 {
1679 startc = '-';
1680 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001681 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001682 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001683 }
1684 /* Emit the OR branches for each character in the [] */
1685 emit_range = FALSE;
1686 while (regparse < endp)
1687 {
1688 oldstartc = startc;
1689 startc = -1;
1690 got_coll_char = FALSE;
1691 if (*regparse == '[')
1692 {
1693 /* Check for [: :], [= =], [. .] */
1694 equiclass = collclass = 0;
1695 charclass = get_char_class(&regparse);
1696 if (charclass == CLASS_NONE)
1697 {
1698 equiclass = get_equi_class(&regparse);
1699 if (equiclass == 0)
1700 collclass = get_coll_element(&regparse);
1701 }
1702
1703 /* Character class like [:alpha:] */
1704 if (charclass != CLASS_NONE)
1705 {
1706 switch (charclass)
1707 {
1708 case CLASS_ALNUM:
1709 EMIT(NFA_CLASS_ALNUM);
1710 break;
1711 case CLASS_ALPHA:
1712 EMIT(NFA_CLASS_ALPHA);
1713 break;
1714 case CLASS_BLANK:
1715 EMIT(NFA_CLASS_BLANK);
1716 break;
1717 case CLASS_CNTRL:
1718 EMIT(NFA_CLASS_CNTRL);
1719 break;
1720 case CLASS_DIGIT:
1721 EMIT(NFA_CLASS_DIGIT);
1722 break;
1723 case CLASS_GRAPH:
1724 EMIT(NFA_CLASS_GRAPH);
1725 break;
1726 case CLASS_LOWER:
1727 EMIT(NFA_CLASS_LOWER);
1728 break;
1729 case CLASS_PRINT:
1730 EMIT(NFA_CLASS_PRINT);
1731 break;
1732 case CLASS_PUNCT:
1733 EMIT(NFA_CLASS_PUNCT);
1734 break;
1735 case CLASS_SPACE:
1736 EMIT(NFA_CLASS_SPACE);
1737 break;
1738 case CLASS_UPPER:
1739 EMIT(NFA_CLASS_UPPER);
1740 break;
1741 case CLASS_XDIGIT:
1742 EMIT(NFA_CLASS_XDIGIT);
1743 break;
1744 case CLASS_TAB:
1745 EMIT(NFA_CLASS_TAB);
1746 break;
1747 case CLASS_RETURN:
1748 EMIT(NFA_CLASS_RETURN);
1749 break;
1750 case CLASS_BACKSPACE:
1751 EMIT(NFA_CLASS_BACKSPACE);
1752 break;
1753 case CLASS_ESCAPE:
1754 EMIT(NFA_CLASS_ESCAPE);
1755 break;
1756 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001757 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001758 continue;
1759 }
1760 /* Try equivalence class [=a=] and the like */
1761 if (equiclass != 0)
1762 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001763 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001764 if (result == FAIL)
1765 {
1766 /* should never happen */
1767 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1768 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 continue;
1770 }
1771 /* Try collating class like [. .] */
1772 if (collclass != 0)
1773 {
1774 startc = collclass; /* allow [.a.]-x as a range */
1775 /* Will emit the proper atom at the end of the
1776 * while loop. */
1777 }
1778 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001779 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1780 * start character. */
1781 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001782 {
1783 emit_range = TRUE;
1784 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001785 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 continue; /* reading the end of the range */
1787 }
1788
1789 /* Now handle simple and escaped characters.
1790 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1791 * accepts "\t", "\e", etc., but only when the 'l' flag in
1792 * 'cpoptions' is not included.
1793 * Posix doesn't recognize backslash at all.
1794 */
1795 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001796 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 && regparse + 1 <= endp
1798 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001799 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 && vim_strchr(REGEXP_ABBR, regparse[1])
1801 != NULL)
1802 )
1803 )
1804 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001805 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001807 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001808 startc = reg_string ? NL : NFA_NEWL;
1809 else
1810 if (*regparse == 'd'
1811 || *regparse == 'o'
1812 || *regparse == 'x'
1813 || *regparse == 'u'
1814 || *regparse == 'U'
1815 )
1816 {
1817 /* TODO(RE) This needs more testing */
1818 startc = coll_get_char();
1819 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001820 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 }
1822 else
1823 {
1824 /* \r,\t,\e,\b */
1825 startc = backslash_trans(*regparse);
1826 }
1827 }
1828
1829 /* Normal printable char */
1830 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001831 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832
1833 /* Previous char was '-', so this char is end of range. */
1834 if (emit_range)
1835 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001836 endc = startc;
1837 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001839 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001840
1841 if (endc > startc + 2)
1842 {
1843 /* Emit a range instead of the sequence of
1844 * individual characters. */
1845 if (startc == 0)
1846 /* \x00 is translated to \x0a, start at \x01. */
1847 EMIT(1);
1848 else
1849 --post_ptr; /* remove NFA_CONCAT */
1850 EMIT(endc);
1851 EMIT(NFA_RANGE);
1852 EMIT(NFA_CONCAT);
1853 }
1854 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001856 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001857 || (*mb_char2len)(endc) > 1))
1858 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001859 /* Emit the characters in the range.
1860 * "startc" was already emitted, so skip it.
1861 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 for (c = startc + 1; c <= endc; c++)
1863 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001864 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001865 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001866 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001867 }
1868 else
1869#endif
1870 {
1871#ifdef EBCDIC
1872 int alpha_only = FALSE;
1873
1874 /* for alphabetical range skip the gaps
1875 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1876 if (isalpha(startc) && isalpha(endc))
1877 alpha_only = TRUE;
1878#endif
1879 /* Emit the range. "startc" was already emitted, so
1880 * skip it. */
1881 for (c = startc + 1; c <= endc; c++)
1882#ifdef EBCDIC
1883 if (!alpha_only || isalpha(startc))
1884#endif
1885 {
1886 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001887 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001888 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001890 emit_range = FALSE;
1891 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892 }
1893 else
1894 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001895 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 * Normally, simply emit startc. But if we get char
1898 * code=0 from a collating char, then replace it with
1899 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001901 * the backtracking engine. */
1902 if (startc == NFA_NEWL)
1903 {
1904 /* Line break can't be matched as part of the
1905 * collection, add an OR below. But not for negated
1906 * range. */
1907 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001908 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001909 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001911 {
1912 if (got_coll_char == TRUE && startc == 0)
1913 EMIT(0x0a);
1914 else
1915 EMIT(startc);
1916 EMIT(NFA_CONCAT);
1917 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001918 }
1919
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001920 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001921 } /* while (p < endp) */
1922
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001923 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001924 if (*regparse == '-') /* if last, '-' is just a char */
1925 {
1926 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001927 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 /* skip the trailing ] */
1931 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001932 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001933
1934 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001936 EMIT(NFA_END_NEG_COLL);
1937 else
1938 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001939
1940 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001941 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001942 {
1943 EMIT(reg_string ? NL : NFA_NEWL);
1944 EMIT(NFA_OR);
1945 }
1946
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001948 } /* if exists closing ] */
1949
1950 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001952 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954 default:
1955 {
1956#ifdef FEAT_MBYTE
1957 int plen;
1958
1959nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001960 /* plen is length of current char with composing chars */
1961 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001962 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001963 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001965 int i = 0;
1966
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001967 /* A base character plus composing characters, or just one
1968 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001969 * This requires creating a separate atom as if enclosing
1970 * the characters in (), where NFA_COMPOSING is the ( and
1971 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972 * building the postfix form, not the NFA itself;
1973 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001974 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001975 for (;;)
1976 {
1977 EMIT(c);
1978 if (i > 0)
1979 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001980 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001981 break;
1982 c = utf_ptr2char(old_regparse + i);
1983 }
1984 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985 regparse = old_regparse + plen;
1986 }
1987 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001988#endif
1989 {
1990 c = no_Magic(c);
1991 EMIT(c);
1992 }
1993 return OK;
1994 }
1995 }
1996
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001997 return OK;
1998}
1999
2000/*
2001 * Parse something followed by possible [*+=].
2002 *
2003 * A piece is an atom, possibly followed by a multi, an indication of how many
2004 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2005 * characters: "", "a", "aa", etc.
2006 *
2007 * piece ::= atom
2008 * or atom multi
2009 */
2010 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002011nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002012{
2013 int i;
2014 int op;
2015 int ret;
2016 long minval, maxval;
2017 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002018 parse_state_T old_state;
2019 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002020 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002021 int old_post_pos;
2022 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 int quest;
2024
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002025 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2026 * next. */
2027 save_parse_state(&old_state);
2028
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002030 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002031
2032 ret = nfa_regatom();
2033 if (ret == FAIL)
2034 return FAIL; /* cascaded error */
2035
2036 op = peekchr();
2037 if (re_multi_type(op) == NOT_MULTI)
2038 return OK;
2039
2040 skipchr();
2041 switch (op)
2042 {
2043 case Magic('*'):
2044 EMIT(NFA_STAR);
2045 break;
2046
2047 case Magic('+'):
2048 /*
2049 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2050 * first and only submatch would be "aaa". But the backtracking
2051 * engine interprets the plus as "try matching one more time", and
2052 * a* matches a second time at the end of the input, the empty
2053 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002054 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002055 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002056 * In order to be consistent with the old engine, we replace
2057 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002059 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002060 curchr = -1;
2061 if (nfa_regatom() == FAIL)
2062 return FAIL;
2063 EMIT(NFA_STAR);
2064 EMIT(NFA_CONCAT);
2065 skipchr(); /* skip the \+ */
2066 break;
2067
2068 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002069 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002071 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 switch(op)
2073 {
2074 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002075 /* \@= */
2076 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002078 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002079 /* \@! */
2080 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002081 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002083 op = no_Magic(getchr());
2084 if (op == '=')
2085 /* \@<= */
2086 i = NFA_PREV_ATOM_JUST_BEFORE;
2087 else if (op == '!')
2088 /* \@<! */
2089 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2090 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002091 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002092 /* \@> */
2093 i = NFA_PREV_ATOM_LIKE_PATTERN;
2094 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002095 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002096 if (i == 0)
2097 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002098 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2099 return FAIL;
2100 }
2101 EMIT(i);
2102 if (i == NFA_PREV_ATOM_JUST_BEFORE
2103 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2104 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105 break;
2106
2107 case Magic('?'):
2108 case Magic('='):
2109 EMIT(NFA_QUEST);
2110 break;
2111
2112 case Magic('{'):
2113 /* a{2,5} will expand to 'aaa?a?a?'
2114 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2115 * version of '?'
2116 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2117 * parenthesis have the same id
2118 */
2119
2120 greedy = TRUE;
2121 c2 = peekchr();
2122 if (c2 == '-' || c2 == Magic('-'))
2123 {
2124 skipchr();
2125 greedy = FALSE;
2126 }
2127 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002129
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002130 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2131 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002132 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002133 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002134 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002135 /* \{}, \{0,} */
2136 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002137 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002138 /* \{-}, \{-0,} */
2139 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140 break;
2141 }
2142
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143 /* Special case: x{0} or x{-0} */
2144 if (maxval == 0)
2145 {
2146 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002147 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002148 /* NFA_EMPTY is 0-length and works everywhere */
2149 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 return OK;
2151 }
2152
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002153 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002154 * maximum is much larger than the minimum and when the maximum is
2155 * large. Bail out if we can use the other engine. */
2156 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002157 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002158 return FAIL;
2159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002160 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002161 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002162 /* Save parse state after the repeated atom and the \{} */
2163 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2166 for (i = 0; i < maxval; i++)
2167 {
2168 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002169 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002170 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002171 if (nfa_regatom() == FAIL)
2172 return FAIL;
2173 /* after "minval" times, atoms are optional */
2174 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002175 {
2176 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002177 {
2178 if (greedy)
2179 EMIT(NFA_STAR);
2180 else
2181 EMIT(NFA_STAR_NONGREEDY);
2182 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002183 else
2184 EMIT(quest);
2185 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002186 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002188 if (i + 1 > minval && maxval == MAX_LIMIT)
2189 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002190 }
2191
2192 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002193 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194 curchr = -1;
2195
2196 break;
2197
2198
2199 default:
2200 break;
2201 } /* end switch */
2202
2203 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002204 /* Can't have a multi follow a multi. */
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002205 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002206
2207 return OK;
2208}
2209
2210/*
2211 * Parse one or more pieces, concatenated. It matches a match for the
2212 * first piece, followed by a match for the second piece, etc. Example:
2213 * "f[0-9]b", first matches "f", then a digit and then "b".
2214 *
2215 * concat ::= piece
2216 * or piece piece
2217 * or piece piece piece
2218 * etc.
2219 */
2220 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002221nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222{
2223 int cont = TRUE;
2224 int first = TRUE;
2225
2226 while (cont)
2227 {
2228 switch (peekchr())
2229 {
2230 case NUL:
2231 case Magic('|'):
2232 case Magic('&'):
2233 case Magic(')'):
2234 cont = FALSE;
2235 break;
2236
2237 case Magic('Z'):
2238#ifdef FEAT_MBYTE
2239 regflags |= RF_ICOMBINE;
2240#endif
2241 skipchr_keepstart();
2242 break;
2243 case Magic('c'):
2244 regflags |= RF_ICASE;
2245 skipchr_keepstart();
2246 break;
2247 case Magic('C'):
2248 regflags |= RF_NOICASE;
2249 skipchr_keepstart();
2250 break;
2251 case Magic('v'):
2252 reg_magic = MAGIC_ALL;
2253 skipchr_keepstart();
2254 curchr = -1;
2255 break;
2256 case Magic('m'):
2257 reg_magic = MAGIC_ON;
2258 skipchr_keepstart();
2259 curchr = -1;
2260 break;
2261 case Magic('M'):
2262 reg_magic = MAGIC_OFF;
2263 skipchr_keepstart();
2264 curchr = -1;
2265 break;
2266 case Magic('V'):
2267 reg_magic = MAGIC_NONE;
2268 skipchr_keepstart();
2269 curchr = -1;
2270 break;
2271
2272 default:
2273 if (nfa_regpiece() == FAIL)
2274 return FAIL;
2275 if (first == FALSE)
2276 EMIT(NFA_CONCAT);
2277 else
2278 first = FALSE;
2279 break;
2280 }
2281 }
2282
2283 return OK;
2284}
2285
2286/*
2287 * Parse a branch, one or more concats, separated by "\&". It matches the
2288 * last concat, but only if all the preceding concats also match at the same
2289 * position. Examples:
2290 * "foobeep\&..." matches "foo" in "foobeep".
2291 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2292 *
2293 * branch ::= concat
2294 * or concat \& concat
2295 * or concat \& concat \& concat
2296 * etc.
2297 */
2298 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002299nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002301 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302
Bram Moolenaar16299b52013-05-30 18:45:23 +02002303 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304
2305 /* First branch, possibly the only one */
2306 if (nfa_regconcat() == FAIL)
2307 return FAIL;
2308
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002310 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002311 {
2312 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002313 /* if concat is empty do emit a node */
2314 if (old_post_pos == (int)(post_ptr - post_start))
2315 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002316 EMIT(NFA_NOPEN);
2317 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002318 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002319 if (nfa_regconcat() == FAIL)
2320 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002321 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002322 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002323 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002324 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 }
2326
Bram Moolenaar699c1202013-09-25 16:41:54 +02002327 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002328 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002329 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330
2331 return OK;
2332}
2333
2334/*
2335 * Parse a pattern, one or more branches, separated by "\|". It matches
2336 * anything that matches one of the branches. Example: "foo\|beep" matches
2337 * "foo" and matches "beep". If more than one branch matches, the first one
2338 * is used.
2339 *
2340 * pattern ::= branch
2341 * or branch \| branch
2342 * or branch \| branch \| branch
2343 * etc.
2344 */
2345 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002346nfa_reg(
2347 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002348{
2349 int parno = 0;
2350
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002351 if (paren == REG_PAREN)
2352 {
2353 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 parno = regnpar++;
2356 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002357#ifdef FEAT_SYN_HL
2358 else if (paren == REG_ZPAREN)
2359 {
2360 /* Make a ZOPEN node. */
2361 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002362 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002363 parno = regnzpar++;
2364 }
2365#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366
2367 if (nfa_regbranch() == FAIL)
2368 return FAIL; /* cascaded error */
2369
2370 while (peekchr() == Magic('|'))
2371 {
2372 skipchr();
2373 if (nfa_regbranch() == FAIL)
2374 return FAIL; /* cascaded error */
2375 EMIT(NFA_OR);
2376 }
2377
2378 /* Check for proper termination. */
2379 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2380 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 if (paren == REG_NPAREN)
2382 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2383 else
2384 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2385 }
2386 else if (paren == REG_NOPAREN && peekchr() != NUL)
2387 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002388 if (peekchr() == Magic(')'))
2389 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2390 else
2391 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2392 }
2393 /*
2394 * Here we set the flag allowing back references to this set of
2395 * parentheses.
2396 */
2397 if (paren == REG_PAREN)
2398 {
2399 had_endbrace[parno] = TRUE; /* have seen the close paren */
2400 EMIT(NFA_MOPEN + parno);
2401 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002402#ifdef FEAT_SYN_HL
2403 else if (paren == REG_ZPAREN)
2404 EMIT(NFA_ZOPEN + parno);
2405#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002406
2407 return OK;
2408}
2409
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002410#ifdef DEBUG
2411static char_u code[50];
2412
2413 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002414nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002415{
2416 int addnl = FALSE;
2417
2418 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2419 {
2420 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002421 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002422 }
2423
2424 STRCPY(code, "");
2425 switch (c)
2426 {
2427 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2428 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2429 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2430 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2431 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2432 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2433
Bram Moolenaar5714b802013-05-28 22:03:20 +02002434 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2435 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2436 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2437 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2438 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2439 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2440 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2441 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2442 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002443#ifdef FEAT_SYN_HL
2444 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2445 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2446 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2447 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2448 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2449 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2450 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2451 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2452 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2453#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002454 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002456 case NFA_PREV_ATOM_NO_WIDTH:
2457 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002458 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2459 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002460 case NFA_PREV_ATOM_JUST_BEFORE:
2461 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2462 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2463 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002464 case NFA_PREV_ATOM_LIKE_PATTERN:
2465 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2466
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002467 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2468 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002470 case NFA_START_INVISIBLE_FIRST:
2471 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002472 case NFA_START_INVISIBLE_NEG:
2473 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002474 case NFA_START_INVISIBLE_NEG_FIRST:
2475 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002476 case NFA_START_INVISIBLE_BEFORE:
2477 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002478 case NFA_START_INVISIBLE_BEFORE_FIRST:
2479 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002480 case NFA_START_INVISIBLE_BEFORE_NEG:
2481 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002482 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2483 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002484 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002485 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002486 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002487 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002488
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002489 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2490 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002491 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002493 case NFA_MOPEN:
2494 case NFA_MOPEN1:
2495 case NFA_MOPEN2:
2496 case NFA_MOPEN3:
2497 case NFA_MOPEN4:
2498 case NFA_MOPEN5:
2499 case NFA_MOPEN6:
2500 case NFA_MOPEN7:
2501 case NFA_MOPEN8:
2502 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503 STRCPY(code, "NFA_MOPEN(x)");
2504 code[10] = c - NFA_MOPEN + '0';
2505 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002506 case NFA_MCLOSE:
2507 case NFA_MCLOSE1:
2508 case NFA_MCLOSE2:
2509 case NFA_MCLOSE3:
2510 case NFA_MCLOSE4:
2511 case NFA_MCLOSE5:
2512 case NFA_MCLOSE6:
2513 case NFA_MCLOSE7:
2514 case NFA_MCLOSE8:
2515 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002516 STRCPY(code, "NFA_MCLOSE(x)");
2517 code[11] = c - NFA_MCLOSE + '0';
2518 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002519#ifdef FEAT_SYN_HL
2520 case NFA_ZOPEN:
2521 case NFA_ZOPEN1:
2522 case NFA_ZOPEN2:
2523 case NFA_ZOPEN3:
2524 case NFA_ZOPEN4:
2525 case NFA_ZOPEN5:
2526 case NFA_ZOPEN6:
2527 case NFA_ZOPEN7:
2528 case NFA_ZOPEN8:
2529 case NFA_ZOPEN9:
2530 STRCPY(code, "NFA_ZOPEN(x)");
2531 code[10] = c - NFA_ZOPEN + '0';
2532 break;
2533 case NFA_ZCLOSE:
2534 case NFA_ZCLOSE1:
2535 case NFA_ZCLOSE2:
2536 case NFA_ZCLOSE3:
2537 case NFA_ZCLOSE4:
2538 case NFA_ZCLOSE5:
2539 case NFA_ZCLOSE6:
2540 case NFA_ZCLOSE7:
2541 case NFA_ZCLOSE8:
2542 case NFA_ZCLOSE9:
2543 STRCPY(code, "NFA_ZCLOSE(x)");
2544 code[11] = c - NFA_ZCLOSE + '0';
2545 break;
2546#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2548 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2549 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2550 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002551 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2552 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002553 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2554 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2555 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2556 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2557 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2558 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2559 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2560 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2561 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2562 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2563 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2564 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2565 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2566 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002567 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002568
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002569 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002570 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2571 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2572 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002573 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002574 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002575
2576 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2577 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2578 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2579 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2580 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2581 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2582 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002584 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2585 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2586 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2587 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2588 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2589 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2590 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2591 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2592 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2593 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2594 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2595 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2596 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2597 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2598 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2599 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2600
2601 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2602 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2603 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2604 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2605 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2606 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2607 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2608 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2609 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2610 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2611 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2612 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2613 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2614 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2615 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2616 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2617 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2618 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2619 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2620 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2621 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2622 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2623 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2624 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2625 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2626 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2627 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002628 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2629 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2630 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2631 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002632
2633 default:
2634 STRCPY(code, "CHAR(x)");
2635 code[5] = c;
2636 }
2637
2638 if (addnl == TRUE)
2639 STRCAT(code, " + NEWLINE ");
2640
2641}
2642
2643#ifdef ENABLE_LOG
2644static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002645static 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 +02002646
2647/*
2648 * Print the postfix notation of the current regexp.
2649 */
2650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002651nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652{
2653 int *p;
2654 FILE *f;
2655
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002656 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002657 if (f != NULL)
2658 {
2659 fprintf(f, "\n-------------------------\n");
2660 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002661 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662 else if (retval == OK)
2663 fprintf(f, ">>> NFA engine succeeded !\n");
2664 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002665 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002666 {
2667 nfa_set_code(*p);
2668 fprintf(f, "%s, ", code);
2669 }
2670 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002671 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672 fprintf(f, "%d ", *p);
2673 fprintf(f, "\n\n");
2674 fclose(f);
2675 }
2676}
2677
2678/*
2679 * Print the NFA starting with a root node "state".
2680 */
2681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002682nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002683{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002684 garray_T indent;
2685
2686 ga_init2(&indent, 1, 64);
2687 ga_append(&indent, '\0');
2688 nfa_print_state2(debugf, state, &indent);
2689 ga_clear(&indent);
2690}
2691
2692 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002693nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002694{
2695 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696
2697 if (state == NULL)
2698 return;
2699
2700 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002701
2702 /* Output indent */
2703 p = (char_u *)indent->ga_data;
2704 if (indent->ga_len >= 3)
2705 {
2706 int last = indent->ga_len - 3;
2707 char_u save[2];
2708
2709 STRNCPY(save, &p[last], 2);
2710 STRNCPY(&p[last], "+-", 2);
2711 fprintf(debugf, " %s", p);
2712 STRNCPY(&p[last], save, 2);
2713 }
2714 else
2715 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002716
2717 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002718 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002719 code,
2720 state->c,
2721 abs(state->id),
2722 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002723 if (state->id < 0)
2724 return;
2725
2726 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002727
2728 /* grow indent for state->out */
2729 indent->ga_len -= 1;
2730 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002731 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002732 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002733 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002734 ga_append(indent, '\0');
2735
2736 nfa_print_state2(debugf, state->out, indent);
2737
2738 /* replace last part of indent for state->out1 */
2739 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002740 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002741 ga_append(indent, '\0');
2742
2743 nfa_print_state2(debugf, state->out1, indent);
2744
2745 /* shrink indent */
2746 indent->ga_len -= 3;
2747 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748}
2749
2750/*
2751 * Print the NFA state machine.
2752 */
2753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002754nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002755{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002756 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002757
2758 if (debugf != NULL)
2759 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002760 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002761
Bram Moolenaar473de612013-06-08 18:19:48 +02002762 if (prog->reganch)
2763 fprintf(debugf, "reganch: %d\n", prog->reganch);
2764 if (prog->regstart != NUL)
2765 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2766 prog->regstart, prog->regstart);
2767 if (prog->match_text != NULL)
2768 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002769
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002770 fclose(debugf);
2771 }
2772}
2773#endif /* ENABLE_LOG */
2774#endif /* DEBUG */
2775
2776/*
2777 * Parse r.e. @expr and convert it into postfix form.
2778 * Return the postfix string on success, NULL otherwise.
2779 */
2780 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002781re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002782{
2783 if (nfa_reg(REG_NOPAREN) == FAIL)
2784 return NULL;
2785 EMIT(NFA_MOPEN);
2786 return post_start;
2787}
2788
2789/* NB. Some of the code below is inspired by Russ's. */
2790
2791/*
2792 * Represents an NFA state plus zero or one or two arrows exiting.
2793 * if c == MATCH, no arrows out; matching state.
2794 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2795 * If c < 256, labeled arrow with character c to out.
2796 */
2797
2798static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2799
2800/*
2801 * Allocate and initialize nfa_state_T.
2802 */
2803 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002804alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002805{
2806 nfa_state_T *s;
2807
2808 if (istate >= nstate)
2809 return NULL;
2810
2811 s = &state_ptr[istate++];
2812
2813 s->c = c;
2814 s->out = out;
2815 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002816 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002817
2818 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002819 s->lastlist[0] = 0;
2820 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821
2822 return s;
2823}
2824
2825/*
2826 * A partially built NFA without the matching state filled in.
2827 * Frag_T.start points at the start state.
2828 * Frag_T.out is a list of places that need to be set to the
2829 * next state for this fragment.
2830 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002831
2832/* Since the out pointers in the list are always
2833 * uninitialized, we use the pointers themselves
2834 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002836union Ptrlist
2837{
2838 Ptrlist *next;
2839 nfa_state_T *s;
2840};
2841
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842struct Frag
2843{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002844 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845 Ptrlist *out;
2846};
2847typedef struct Frag Frag_T;
2848
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002850 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851 */
2852 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002853frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002855 Frag_T n;
2856
2857 n.start = start;
2858 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002859 return n;
2860}
2861
2862/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002863 * Create singleton list containing just outp.
2864 */
2865 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002866list1(
2867 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002868{
2869 Ptrlist *l;
2870
2871 l = (Ptrlist *)outp;
2872 l->next = NULL;
2873 return l;
2874}
2875
2876/*
2877 * Patch the list of states at out to point to start.
2878 */
2879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002880patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002881{
2882 Ptrlist *next;
2883
2884 for (; l; l = next)
2885 {
2886 next = l->next;
2887 l->s = s;
2888 }
2889}
2890
2891
2892/*
2893 * Join the two lists l1 and l2, returning the combination.
2894 */
2895 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002896append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897{
2898 Ptrlist *oldl1;
2899
2900 oldl1 = l1;
2901 while (l1->next)
2902 l1 = l1->next;
2903 l1->next = l2;
2904 return oldl1;
2905}
2906
2907/*
2908 * Stack used for transforming postfix form into NFA.
2909 */
2910static Frag_T empty;
2911
2912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002913st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002915#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916 FILE *df;
2917 int *p2;
2918
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002919 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002920 if (df)
2921 {
2922 fprintf(df, "Error popping the stack!\n");
2923#ifdef DEBUG
2924 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2925#endif
2926 fprintf(df, "Postfix form is: ");
2927#ifdef DEBUG
2928 for (p2 = postfix; p2 < end; p2++)
2929 {
2930 nfa_set_code(*p2);
2931 fprintf(df, "%s, ", code);
2932 }
2933 nfa_set_code(*p);
2934 fprintf(df, "\nCurrent position is: ");
2935 for (p2 = postfix; p2 <= p; p2 ++)
2936 {
2937 nfa_set_code(*p2);
2938 fprintf(df, "%s, ", code);
2939 }
2940#else
2941 for (p2 = postfix; p2 < end; p2++)
2942 {
2943 fprintf(df, "%d, ", *p2);
2944 }
2945 fprintf(df, "\nCurrent position is: ");
2946 for (p2 = postfix; p2 <= p; p2 ++)
2947 {
2948 fprintf(df, "%d, ", *p2);
2949 }
2950#endif
2951 fprintf(df, "\n--------------------------\n");
2952 fclose(df);
2953 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002954#endif
Bram Moolenaar5efa0102018-06-22 21:42:30 +02002955 EMSG(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956}
2957
2958/*
2959 * Push an item onto the stack.
2960 */
2961 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002962st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002963{
2964 Frag_T *stackp = *p;
2965
2966 if (stackp >= stack_end)
2967 return;
2968 *stackp = s;
2969 *p = *p + 1;
2970}
2971
2972/*
2973 * Pop an item from the stack.
2974 */
2975 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002976st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002977{
2978 Frag_T *stackp;
2979
2980 *p = *p - 1;
2981 stackp = *p;
2982 if (stackp < stack)
2983 return empty;
2984 return **p;
2985}
2986
2987/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002988 * Estimate the maximum byte length of anything matching "state".
2989 * When unknown or unlimited return -1.
2990 */
2991 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002992nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002993{
2994 int l, r;
2995 nfa_state_T *state = startstate;
2996 int len = 0;
2997
2998 /* detect looping in a NFA_SPLIT */
2999 if (depth > 4)
3000 return -1;
3001
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003002 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003003 {
3004 switch (state->c)
3005 {
3006 case NFA_END_INVISIBLE:
3007 case NFA_END_INVISIBLE_NEG:
3008 /* the end, return what we have */
3009 return len;
3010
3011 case NFA_SPLIT:
3012 /* two alternatives, use the maximum */
3013 l = nfa_max_width(state->out, depth + 1);
3014 r = nfa_max_width(state->out1, depth + 1);
3015 if (l < 0 || r < 0)
3016 return -1;
3017 return len + (l > r ? l : r);
3018
3019 case NFA_ANY:
3020 case NFA_START_COLL:
3021 case NFA_START_NEG_COLL:
3022 /* matches some character, including composing chars */
3023#ifdef FEAT_MBYTE
3024 if (enc_utf8)
3025 len += MB_MAXBYTES;
3026 else if (has_mbyte)
3027 len += 2;
3028 else
3029#endif
3030 ++len;
3031 if (state->c != NFA_ANY)
3032 {
3033 /* skip over the characters */
3034 state = state->out1->out;
3035 continue;
3036 }
3037 break;
3038
3039 case NFA_DIGIT:
3040 case NFA_WHITE:
3041 case NFA_HEX:
3042 case NFA_OCTAL:
3043 /* ascii */
3044 ++len;
3045 break;
3046
3047 case NFA_IDENT:
3048 case NFA_SIDENT:
3049 case NFA_KWORD:
3050 case NFA_SKWORD:
3051 case NFA_FNAME:
3052 case NFA_SFNAME:
3053 case NFA_PRINT:
3054 case NFA_SPRINT:
3055 case NFA_NWHITE:
3056 case NFA_NDIGIT:
3057 case NFA_NHEX:
3058 case NFA_NOCTAL:
3059 case NFA_WORD:
3060 case NFA_NWORD:
3061 case NFA_HEAD:
3062 case NFA_NHEAD:
3063 case NFA_ALPHA:
3064 case NFA_NALPHA:
3065 case NFA_LOWER:
3066 case NFA_NLOWER:
3067 case NFA_UPPER:
3068 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003069 case NFA_LOWER_IC:
3070 case NFA_NLOWER_IC:
3071 case NFA_UPPER_IC:
3072 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003073 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003074 /* possibly non-ascii */
3075#ifdef FEAT_MBYTE
3076 if (has_mbyte)
3077 len += 3;
3078 else
3079#endif
3080 ++len;
3081 break;
3082
3083 case NFA_START_INVISIBLE:
3084 case NFA_START_INVISIBLE_NEG:
3085 case NFA_START_INVISIBLE_BEFORE:
3086 case NFA_START_INVISIBLE_BEFORE_NEG:
3087 /* zero-width, out1 points to the END state */
3088 state = state->out1->out;
3089 continue;
3090
3091 case NFA_BACKREF1:
3092 case NFA_BACKREF2:
3093 case NFA_BACKREF3:
3094 case NFA_BACKREF4:
3095 case NFA_BACKREF5:
3096 case NFA_BACKREF6:
3097 case NFA_BACKREF7:
3098 case NFA_BACKREF8:
3099 case NFA_BACKREF9:
3100#ifdef FEAT_SYN_HL
3101 case NFA_ZREF1:
3102 case NFA_ZREF2:
3103 case NFA_ZREF3:
3104 case NFA_ZREF4:
3105 case NFA_ZREF5:
3106 case NFA_ZREF6:
3107 case NFA_ZREF7:
3108 case NFA_ZREF8:
3109 case NFA_ZREF9:
3110#endif
3111 case NFA_NEWL:
3112 case NFA_SKIP:
3113 /* unknown width */
3114 return -1;
3115
3116 case NFA_BOL:
3117 case NFA_EOL:
3118 case NFA_BOF:
3119 case NFA_EOF:
3120 case NFA_BOW:
3121 case NFA_EOW:
3122 case NFA_MOPEN:
3123 case NFA_MOPEN1:
3124 case NFA_MOPEN2:
3125 case NFA_MOPEN3:
3126 case NFA_MOPEN4:
3127 case NFA_MOPEN5:
3128 case NFA_MOPEN6:
3129 case NFA_MOPEN7:
3130 case NFA_MOPEN8:
3131 case NFA_MOPEN9:
3132#ifdef FEAT_SYN_HL
3133 case NFA_ZOPEN:
3134 case NFA_ZOPEN1:
3135 case NFA_ZOPEN2:
3136 case NFA_ZOPEN3:
3137 case NFA_ZOPEN4:
3138 case NFA_ZOPEN5:
3139 case NFA_ZOPEN6:
3140 case NFA_ZOPEN7:
3141 case NFA_ZOPEN8:
3142 case NFA_ZOPEN9:
3143 case NFA_ZCLOSE:
3144 case NFA_ZCLOSE1:
3145 case NFA_ZCLOSE2:
3146 case NFA_ZCLOSE3:
3147 case NFA_ZCLOSE4:
3148 case NFA_ZCLOSE5:
3149 case NFA_ZCLOSE6:
3150 case NFA_ZCLOSE7:
3151 case NFA_ZCLOSE8:
3152 case NFA_ZCLOSE9:
3153#endif
3154 case NFA_MCLOSE:
3155 case NFA_MCLOSE1:
3156 case NFA_MCLOSE2:
3157 case NFA_MCLOSE3:
3158 case NFA_MCLOSE4:
3159 case NFA_MCLOSE5:
3160 case NFA_MCLOSE6:
3161 case NFA_MCLOSE7:
3162 case NFA_MCLOSE8:
3163 case NFA_MCLOSE9:
3164 case NFA_NOPEN:
3165 case NFA_NCLOSE:
3166
3167 case NFA_LNUM_GT:
3168 case NFA_LNUM_LT:
3169 case NFA_COL_GT:
3170 case NFA_COL_LT:
3171 case NFA_VCOL_GT:
3172 case NFA_VCOL_LT:
3173 case NFA_MARK_GT:
3174 case NFA_MARK_LT:
3175 case NFA_VISUAL:
3176 case NFA_LNUM:
3177 case NFA_CURSOR:
3178 case NFA_COL:
3179 case NFA_VCOL:
3180 case NFA_MARK:
3181
3182 case NFA_ZSTART:
3183 case NFA_ZEND:
3184 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003185 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003186 case NFA_START_PATTERN:
3187 case NFA_END_PATTERN:
3188 case NFA_COMPOSING:
3189 case NFA_END_COMPOSING:
3190 /* zero-width */
3191 break;
3192
3193 default:
3194 if (state->c < 0)
3195 /* don't know what this is */
3196 return -1;
3197 /* normal character */
3198 len += MB_CHAR2LEN(state->c);
3199 break;
3200 }
3201
3202 /* normal way to continue */
3203 state = state->out;
3204 }
3205
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003206 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003207 return -1;
3208}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003209
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003210/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 * Convert a postfix form into its equivalent NFA.
3212 * Return the NFA start state on success, NULL otherwise.
3213 */
3214 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003215post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003216{
3217 int *p;
3218 int mopen;
3219 int mclose;
3220 Frag_T *stack = NULL;
3221 Frag_T *stackp = NULL;
3222 Frag_T *stack_end = NULL;
3223 Frag_T e1;
3224 Frag_T e2;
3225 Frag_T e;
3226 nfa_state_T *s;
3227 nfa_state_T *s1;
3228 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003229 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230
3231 if (postfix == NULL)
3232 return NULL;
3233
Bram Moolenaar053bb602013-05-20 13:55:21 +02003234#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003235#define POP() st_pop(&stackp, stack); \
3236 if (stackp < stack) \
3237 { \
3238 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003239 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 return NULL; \
3241 }
3242
3243 if (nfa_calc_size == FALSE)
3244 {
3245 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003246 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003248 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 }
3250
3251 for (p = postfix; p < end; ++p)
3252 {
3253 switch (*p)
3254 {
3255 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003256 /* Concatenation.
3257 * Pay attention: this operator does not exist in the r.e. itself
3258 * (it is implicit, really). It is added when r.e. is translated
3259 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003260 if (nfa_calc_size == TRUE)
3261 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003262 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 break;
3264 }
3265 e2 = POP();
3266 e1 = POP();
3267 patch(e1.out, e2.start);
3268 PUSH(frag(e1.start, e2.out));
3269 break;
3270
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 case NFA_OR:
3272 /* Alternation */
3273 if (nfa_calc_size == TRUE)
3274 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003275 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 break;
3277 }
3278 e2 = POP();
3279 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003280 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003282 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 PUSH(frag(s, append(e1.out, e2.out)));
3284 break;
3285
3286 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003287 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 if (nfa_calc_size == TRUE)
3289 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003290 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 break;
3292 }
3293 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003294 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003296 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 patch(e.out, s);
3298 PUSH(frag(s, list1(&s->out1)));
3299 break;
3300
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003301 case NFA_STAR_NONGREEDY:
3302 /* Zero or more, prefer zero */
3303 if (nfa_calc_size == TRUE)
3304 {
3305 nstate++;
3306 break;
3307 }
3308 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003309 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003310 if (s == NULL)
3311 goto theend;
3312 patch(e.out, s);
3313 PUSH(frag(s, list1(&s->out)));
3314 break;
3315
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 case NFA_QUEST:
3317 /* one or zero atoms=> greedy match */
3318 if (nfa_calc_size == TRUE)
3319 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003320 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003321 break;
3322 }
3323 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003324 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003326 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327 PUSH(frag(s, append(e.out, list1(&s->out1))));
3328 break;
3329
3330 case NFA_QUEST_NONGREEDY:
3331 /* zero or one atoms => non-greedy match */
3332 if (nfa_calc_size == TRUE)
3333 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003334 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003335 break;
3336 }
3337 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003338 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003339 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003340 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003341 PUSH(frag(s, append(e.out, list1(&s->out))));
3342 break;
3343
Bram Moolenaar417bad22013-06-07 14:08:30 +02003344 case NFA_END_COLL:
3345 case NFA_END_NEG_COLL:
3346 /* On the stack is the sequence starting with NFA_START_COLL or
3347 * NFA_START_NEG_COLL and all possible characters. Patch it to
3348 * add the output to the start. */
3349 if (nfa_calc_size == TRUE)
3350 {
3351 nstate++;
3352 break;
3353 }
3354 e = POP();
3355 s = alloc_state(NFA_END_COLL, NULL, NULL);
3356 if (s == NULL)
3357 goto theend;
3358 patch(e.out, s);
3359 e.start->out1 = s;
3360 PUSH(frag(e.start, list1(&s->out)));
3361 break;
3362
3363 case NFA_RANGE:
3364 /* Before this are two characters, the low and high end of a
3365 * range. Turn them into two states with MIN and MAX. */
3366 if (nfa_calc_size == TRUE)
3367 {
3368 /* nstate += 0; */
3369 break;
3370 }
3371 e2 = POP();
3372 e1 = POP();
3373 e2.start->val = e2.start->c;
3374 e2.start->c = NFA_RANGE_MAX;
3375 e1.start->val = e1.start->c;
3376 e1.start->c = NFA_RANGE_MIN;
3377 patch(e1.out, e2.start);
3378 PUSH(frag(e1.start, e2.out));
3379 break;
3380
Bram Moolenaar699c1202013-09-25 16:41:54 +02003381 case NFA_EMPTY:
3382 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003383 if (nfa_calc_size == TRUE)
3384 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003385 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386 break;
3387 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003388 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003389 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003390 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391 PUSH(frag(s, list1(&s->out)));
3392 break;
3393
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003394 case NFA_OPT_CHARS:
3395 {
3396 int n;
3397
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003398 /* \%[abc] implemented as:
3399 * NFA_SPLIT
3400 * +-CHAR(a)
3401 * | +-NFA_SPLIT
3402 * | +-CHAR(b)
3403 * | | +-NFA_SPLIT
3404 * | | +-CHAR(c)
3405 * | | | +-next
3406 * | | +- next
3407 * | +- next
3408 * +- next
3409 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003410 n = *++p; /* get number of characters */
3411 if (nfa_calc_size == TRUE)
3412 {
3413 nstate += n;
3414 break;
3415 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003416 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003417 e1.out = NULL; /* stores list with out1's */
3418 s1 = NULL; /* previous NFA_SPLIT to connect to */
3419 while (n-- > 0)
3420 {
3421 e = POP(); /* get character */
3422 s = alloc_state(NFA_SPLIT, e.start, NULL);
3423 if (s == NULL)
3424 goto theend;
3425 if (e1.out == NULL)
3426 e1 = e;
3427 patch(e.out, s1);
3428 append(e1.out, list1(&s->out1));
3429 s1 = s;
3430 }
3431 PUSH(frag(s, e1.out));
3432 break;
3433 }
3434
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003435 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003436 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003437 case NFA_PREV_ATOM_JUST_BEFORE:
3438 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003439 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003440 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003441 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3442 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003443 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003444 int start_state;
3445 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003446 int n = 0;
3447 nfa_state_T *zend;
3448 nfa_state_T *skip;
3449
Bram Moolenaardecd9542013-06-07 16:31:50 +02003450 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003451 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003452 case NFA_PREV_ATOM_NO_WIDTH:
3453 start_state = NFA_START_INVISIBLE;
3454 end_state = NFA_END_INVISIBLE;
3455 break;
3456 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3457 start_state = NFA_START_INVISIBLE_NEG;
3458 end_state = NFA_END_INVISIBLE_NEG;
3459 break;
3460 case NFA_PREV_ATOM_JUST_BEFORE:
3461 start_state = NFA_START_INVISIBLE_BEFORE;
3462 end_state = NFA_END_INVISIBLE;
3463 break;
3464 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3465 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3466 end_state = NFA_END_INVISIBLE_NEG;
3467 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003468 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003469 start_state = NFA_START_PATTERN;
3470 end_state = NFA_END_PATTERN;
3471 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003473
3474 if (before)
3475 n = *++p; /* get the count */
3476
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003477 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003478 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003479 * The \@<= operator: match for the preceding atom.
3480 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003482 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003483
3484 if (nfa_calc_size == TRUE)
3485 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003486 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 break;
3488 }
3489 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003490 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003491 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003492 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493
Bram Moolenaar87953742013-06-05 18:52:40 +02003494 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003495 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003496 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003497 if (pattern)
3498 {
3499 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3500 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003501 if (skip == NULL)
3502 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003503 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003504 if (zend == NULL)
3505 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003506 s1->out= skip;
3507 patch(e.out, zend);
3508 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003509 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003510 else
3511 {
3512 patch(e.out, s1);
3513 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003514 if (before)
3515 {
3516 if (n <= 0)
3517 /* See if we can guess the maximum width, it avoids a
3518 * lot of pointless tries. */
3519 n = nfa_max_width(e.start, 0);
3520 s->val = n; /* store the count */
3521 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003522 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003523 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003524 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003525
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003526#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003527 case NFA_COMPOSING: /* char with composing char */
3528#if 0
3529 /* TODO */
3530 if (regflags & RF_ICOMBINE)
3531 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003532 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003533 }
3534#endif
3535 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003536#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003537
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003538 case NFA_MOPEN: /* \( \) Submatch */
3539 case NFA_MOPEN1:
3540 case NFA_MOPEN2:
3541 case NFA_MOPEN3:
3542 case NFA_MOPEN4:
3543 case NFA_MOPEN5:
3544 case NFA_MOPEN6:
3545 case NFA_MOPEN7:
3546 case NFA_MOPEN8:
3547 case NFA_MOPEN9:
3548#ifdef FEAT_SYN_HL
3549 case NFA_ZOPEN: /* \z( \) Submatch */
3550 case NFA_ZOPEN1:
3551 case NFA_ZOPEN2:
3552 case NFA_ZOPEN3:
3553 case NFA_ZOPEN4:
3554 case NFA_ZOPEN5:
3555 case NFA_ZOPEN6:
3556 case NFA_ZOPEN7:
3557 case NFA_ZOPEN8:
3558 case NFA_ZOPEN9:
3559#endif
3560 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003561 if (nfa_calc_size == TRUE)
3562 {
3563 nstate += 2;
3564 break;
3565 }
3566
3567 mopen = *p;
3568 switch (*p)
3569 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003570 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3571#ifdef FEAT_SYN_HL
3572 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3573 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3574 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3575 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3576 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3577 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3578 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3579 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3580 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3581 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3582#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003583#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003584 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003585#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003587 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 mclose = *p + NSUBEXP;
3589 break;
3590 }
3591
3592 /* Allow "NFA_MOPEN" as a valid postfix representation for
3593 * the empty regexp "". In this case, the NFA will be
3594 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3595 * empty groups of parenthesis, and empty mbyte chars */
3596 if (stackp == stack)
3597 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003598 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003600 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003601 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003602 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003603 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003604 patch(list1(&s->out), s1);
3605 PUSH(frag(s, list1(&s1->out)));
3606 break;
3607 }
3608
3609 /* At least one node was emitted before NFA_MOPEN, so
3610 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3611 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003612 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003614 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615
Bram Moolenaar525666f2013-06-02 16:40:55 +02003616 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003618 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 patch(e.out, s1);
3620
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003621#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003622 if (mopen == NFA_COMPOSING)
3623 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003625#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626
3627 PUSH(frag(s, list1(&s1->out)));
3628 break;
3629
Bram Moolenaar5714b802013-05-28 22:03:20 +02003630 case NFA_BACKREF1:
3631 case NFA_BACKREF2:
3632 case NFA_BACKREF3:
3633 case NFA_BACKREF4:
3634 case NFA_BACKREF5:
3635 case NFA_BACKREF6:
3636 case NFA_BACKREF7:
3637 case NFA_BACKREF8:
3638 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003639#ifdef FEAT_SYN_HL
3640 case NFA_ZREF1:
3641 case NFA_ZREF2:
3642 case NFA_ZREF3:
3643 case NFA_ZREF4:
3644 case NFA_ZREF5:
3645 case NFA_ZREF6:
3646 case NFA_ZREF7:
3647 case NFA_ZREF8:
3648 case NFA_ZREF9:
3649#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003650 if (nfa_calc_size == TRUE)
3651 {
3652 nstate += 2;
3653 break;
3654 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003655 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003656 if (s == NULL)
3657 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003658 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003659 if (s1 == NULL)
3660 goto theend;
3661 patch(list1(&s->out), s1);
3662 PUSH(frag(s, list1(&s1->out)));
3663 break;
3664
Bram Moolenaar423532e2013-05-29 21:14:42 +02003665 case NFA_LNUM:
3666 case NFA_LNUM_GT:
3667 case NFA_LNUM_LT:
3668 case NFA_VCOL:
3669 case NFA_VCOL_GT:
3670 case NFA_VCOL_LT:
3671 case NFA_COL:
3672 case NFA_COL_GT:
3673 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003674 case NFA_MARK:
3675 case NFA_MARK_GT:
3676 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003677 {
3678 int n = *++p; /* lnum, col or mark name */
3679
Bram Moolenaar423532e2013-05-29 21:14:42 +02003680 if (nfa_calc_size == TRUE)
3681 {
3682 nstate += 1;
3683 break;
3684 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003685 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003686 if (s == NULL)
3687 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003688 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003689 PUSH(frag(s, list1(&s->out)));
3690 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003691 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003692
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003693 case NFA_ZSTART:
3694 case NFA_ZEND:
3695 default:
3696 /* Operands */
3697 if (nfa_calc_size == TRUE)
3698 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003699 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 break;
3701 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003702 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003704 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 PUSH(frag(s, list1(&s->out)));
3706 break;
3707
3708 } /* switch(*p) */
3709
3710 } /* for(p = postfix; *p; ++p) */
3711
3712 if (nfa_calc_size == TRUE)
3713 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003714 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003715 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003716 }
3717
3718 e = POP();
3719 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003720 {
3721 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 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 +02003723 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003724
3725 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003726 {
3727 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003728 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003729 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003730
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003731 matchstate = &state_ptr[istate++]; /* the match state */
3732 matchstate->c = NFA_MATCH;
3733 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003734 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735
3736 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003737 ret = e.start;
3738
3739theend:
3740 vim_free(stack);
3741 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003742
3743#undef POP1
3744#undef PUSH1
3745#undef POP2
3746#undef PUSH2
3747#undef POP
3748#undef PUSH
3749}
3750
Bram Moolenaara2947e22013-06-11 22:44:09 +02003751/*
3752 * After building the NFA program, inspect it to add optimization hints.
3753 */
3754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003755nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003756{
3757 int i;
3758 int c;
3759
3760 for (i = 0; i < prog->nstate; ++i)
3761 {
3762 c = prog->state[i].c;
3763 if (c == NFA_START_INVISIBLE
3764 || c == NFA_START_INVISIBLE_NEG
3765 || c == NFA_START_INVISIBLE_BEFORE
3766 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3767 {
3768 int directly;
3769
3770 /* Do it directly when what follows is possibly the end of the
3771 * match. */
3772 if (match_follows(prog->state[i].out1->out, 0))
3773 directly = TRUE;
3774 else
3775 {
3776 int ch_invisible = failure_chance(prog->state[i].out, 0);
3777 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3778
3779 /* Postpone when the invisible match is expensive or has a
3780 * lower chance of failing. */
3781 if (c == NFA_START_INVISIBLE_BEFORE
3782 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3783 {
3784 /* "before" matches are very expensive when
3785 * unbounded, always prefer what follows then,
3786 * unless what follows will always match.
3787 * Otherwise strongly prefer what follows. */
3788 if (prog->state[i].val <= 0 && ch_follows > 0)
3789 directly = FALSE;
3790 else
3791 directly = ch_follows * 10 < ch_invisible;
3792 }
3793 else
3794 {
3795 /* normal invisible, first do the one with the
3796 * highest failure chance */
3797 directly = ch_follows < ch_invisible;
3798 }
3799 }
3800 if (directly)
3801 /* switch to the _FIRST state */
3802 ++prog->state[i].c;
3803 }
3804 }
3805}
3806
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003807/****************************************************************
3808 * NFA execution code.
3809 ****************************************************************/
3810
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003811typedef struct
3812{
3813 int in_use; /* number of subexpr with useful info */
3814
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003815 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003816 union
3817 {
3818 struct multipos
3819 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003820 linenr_T start_lnum;
3821 linenr_T end_lnum;
3822 colnr_T start_col;
3823 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003824 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003825 struct linepos
3826 {
3827 char_u *start;
3828 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003829 } line[NSUBEXP];
3830 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003831} regsub_T;
3832
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003833typedef struct
3834{
3835 regsub_T norm; /* \( .. \) matches */
3836#ifdef FEAT_SYN_HL
3837 regsub_T synt; /* \z( .. \) matches */
3838#endif
3839} regsubs_T;
3840
Bram Moolenaara2d95102013-06-04 14:23:05 +02003841/* nfa_pim_T stores a Postponed Invisible Match. */
3842typedef struct nfa_pim_S nfa_pim_T;
3843struct nfa_pim_S
3844{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003845 int result; /* NFA_PIM_*, see below */
3846 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003847 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003848 union
3849 {
3850 lpos_T pos;
3851 char_u *ptr;
3852 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003853};
3854
3855/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003856#define NFA_PIM_UNUSED 0 /* pim not used */
3857#define NFA_PIM_TODO 1 /* pim not done yet */
3858#define NFA_PIM_MATCH 2 /* pim executed, matches */
3859#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003860
3861
Bram Moolenaar963fee22013-05-26 21:47:28 +02003862/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003863typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003864{
3865 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003866 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003867 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3868 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003869 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003870} nfa_thread_T;
3871
Bram Moolenaar963fee22013-05-26 21:47:28 +02003872/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003873typedef struct
3874{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003875 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003876 int n; /* nr of states currently in "t" */
3877 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003878 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003879 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003880} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881
Bram Moolenaar5714b802013-05-28 22:03:20 +02003882#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003883static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003884
3885 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003886log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003887{
3888 log_subexpr(&subs->norm);
3889# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003890 if (nfa_has_zsubexpr)
3891 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003892# endif
3893}
3894
Bram Moolenaar5714b802013-05-28 22:03:20 +02003895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003896log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003897{
3898 int j;
3899
3900 for (j = 0; j < sub->in_use; j++)
3901 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003902 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003903 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003904 sub->list.multi[j].start_col,
3905 (int)sub->list.multi[j].start_lnum,
3906 sub->list.multi[j].end_col,
3907 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003908 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003909 {
3910 char *s = (char *)sub->list.line[j].start;
3911 char *e = (char *)sub->list.line[j].end;
3912
Bram Moolenaar87953742013-06-05 18:52:40 +02003913 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003914 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003915 s == NULL ? "NULL" : s,
3916 e == NULL ? "NULL" : e);
3917 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003918}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003919
3920 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003921pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003922{
3923 static char buf[30];
3924
3925 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3926 buf[0] = NUL;
3927 else
3928 {
3929 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3930 : (int)(pim->end.ptr - reginput));
3931 }
3932 return buf;
3933}
3934
Bram Moolenaar5714b802013-05-28 22:03:20 +02003935#endif
3936
Bram Moolenaar963fee22013-05-26 21:47:28 +02003937/* Used during execution: whether a match has been found. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02003938static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003939#ifdef FEAT_RELTIME
3940static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003941static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003942static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003943#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003944
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003945static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003946static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003947
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003948/*
3949 * Copy postponed invisible match info from "from" to "to".
3950 */
3951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003952copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003953{
3954 to->result = from->result;
3955 to->state = from->state;
3956 copy_sub(&to->subs.norm, &from->subs.norm);
3957#ifdef FEAT_SYN_HL
3958 if (nfa_has_zsubexpr)
3959 copy_sub(&to->subs.synt, &from->subs.synt);
3960#endif
3961 to->end = from->end;
3962}
3963
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003964 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003965clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003966{
3967 if (REG_MULTI)
3968 /* Use 0xff to set lnum to -1 */
3969 vim_memset(sub->list.multi, 0xff,
3970 sizeof(struct multipos) * nfa_nsubexpr);
3971 else
3972 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3973 sub->in_use = 0;
3974}
3975
3976/*
3977 * Copy the submatches from "from" to "to".
3978 */
3979 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003980copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003981{
3982 to->in_use = from->in_use;
3983 if (from->in_use > 0)
3984 {
3985 /* Copy the match start and end positions. */
3986 if (REG_MULTI)
3987 mch_memmove(&to->list.multi[0],
3988 &from->list.multi[0],
3989 sizeof(struct multipos) * from->in_use);
3990 else
3991 mch_memmove(&to->list.line[0],
3992 &from->list.line[0],
3993 sizeof(struct linepos) * from->in_use);
3994 }
3995}
3996
3997/*
3998 * Like copy_sub() but exclude the main match.
3999 */
4000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004001copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004002{
4003 if (to->in_use < from->in_use)
4004 to->in_use = from->in_use;
4005 if (from->in_use > 1)
4006 {
4007 /* Copy the match start and end positions. */
4008 if (REG_MULTI)
4009 mch_memmove(&to->list.multi[1],
4010 &from->list.multi[1],
4011 sizeof(struct multipos) * (from->in_use - 1));
4012 else
4013 mch_memmove(&to->list.line[1],
4014 &from->list.line[1],
4015 sizeof(struct linepos) * (from->in_use - 1));
4016 }
4017}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004018
Bram Moolenaar428e9872013-05-30 17:05:39 +02004019/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004020 * Like copy_sub() but only do the end of the main match if \ze is present.
4021 */
4022 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004023copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004024{
4025 if (nfa_has_zend)
4026 {
4027 if (REG_MULTI)
4028 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004029 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004030 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004031 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4032 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004033 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004034 }
4035 else
4036 {
4037 if (from->list.line[0].end != NULL)
4038 to->list.line[0].end = from->list.line[0].end;
4039 }
4040 }
4041}
4042
4043/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004044 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004045 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004046 */
4047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004048sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004049{
4050 int i;
4051 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004052 linenr_T s1;
4053 linenr_T s2;
4054 char_u *sp1;
4055 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004056
4057 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4058 if (REG_MULTI)
4059 {
4060 for (i = 0; i < todo; ++i)
4061 {
4062 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004063 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004064 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004065 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004066 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004067 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004068 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004069 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004070 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004071 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004072 if (s1 != -1 && sub1->list.multi[i].start_col
4073 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004075
4076 if (nfa_has_backref)
4077 {
4078 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004079 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004080 else
4081 s1 = -1;
4082 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004083 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004084 else
4085 s2 = -1;
4086 if (s1 != s2)
4087 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004088 if (s1 != -1 && sub1->list.multi[i].end_col
4089 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004090 return FALSE;
4091 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004092 }
4093 }
4094 else
4095 {
4096 for (i = 0; i < todo; ++i)
4097 {
4098 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004099 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004100 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004101 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004102 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004103 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004104 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004105 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004106 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004107 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004108 if (nfa_has_backref)
4109 {
4110 if (i < sub1->in_use)
4111 sp1 = sub1->list.line[i].end;
4112 else
4113 sp1 = NULL;
4114 if (i < sub2->in_use)
4115 sp2 = sub2->list.line[i].end;
4116 else
4117 sp2 = NULL;
4118 if (sp1 != sp2)
4119 return FALSE;
4120 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004121 }
4122 }
4123
4124 return TRUE;
4125}
4126
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004127#ifdef ENABLE_LOG
4128 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004129report_state(char *action,
4130 regsub_T *sub,
4131 nfa_state_T *state,
4132 int lid,
4133 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004134{
4135 int col;
4136
4137 if (sub->in_use <= 0)
4138 col = -1;
4139 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004140 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004141 else
4142 col = (int)(sub->list.line[0].start - regline);
4143 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004144 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4145 action, abs(state->id), lid, state->c, code, col,
4146 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004147}
4148#endif
4149
Bram Moolenaar43e02982013-06-07 17:31:29 +02004150/*
4151 * Return TRUE if the same state is already in list "l" with the same
4152 * positions as "subs".
4153 */
4154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004155has_state_with_pos(
4156 nfa_list_T *l, /* runtime state list */
4157 nfa_state_T *state, /* state to update */
4158 regsubs_T *subs, /* pointers to subexpressions */
4159 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004160{
4161 nfa_thread_T *thread;
4162 int i;
4163
4164 for (i = 0; i < l->n; ++i)
4165 {
4166 thread = &l->t[i];
4167 if (thread->state->id == state->id
4168 && sub_equal(&thread->subs.norm, &subs->norm)
4169#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004170 && (!nfa_has_zsubexpr
4171 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004172#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004173 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004174 return TRUE;
4175 }
4176 return FALSE;
4177}
4178
4179/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004180 * Return TRUE if "one" and "two" are equal. That includes when both are not
4181 * set.
4182 */
4183 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004184pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004185{
4186 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4187 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4188
4189 if (one_unused)
4190 /* one is unused: equal when two is also unused */
4191 return two_unused;
4192 if (two_unused)
4193 /* one is used and two is not: not equal */
4194 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004195 /* compare the state id */
4196 if (one->state->id != two->state->id)
4197 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004198 /* compare the position */
4199 if (REG_MULTI)
4200 return one->end.pos.lnum == two->end.pos.lnum
4201 && one->end.pos.col == two->end.pos.col;
4202 return one->end.ptr == two->end.ptr;
4203}
4204
4205/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004206 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4207 */
4208 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004209match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004210{
4211 nfa_state_T *state = startstate;
4212
4213 /* avoid too much recursion */
4214 if (depth > 10)
4215 return FALSE;
4216
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004217 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004218 {
4219 switch (state->c)
4220 {
4221 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004222 case NFA_MCLOSE:
4223 case NFA_END_INVISIBLE:
4224 case NFA_END_INVISIBLE_NEG:
4225 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004226 return TRUE;
4227
4228 case NFA_SPLIT:
4229 return match_follows(state->out, depth + 1)
4230 || match_follows(state->out1, depth + 1);
4231
4232 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004233 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004234 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004235 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004236 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004237 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004238 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004239 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004240 case NFA_COMPOSING:
4241 /* skip ahead to next state */
4242 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004243 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004244
4245 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004246 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004247 case NFA_IDENT:
4248 case NFA_SIDENT:
4249 case NFA_KWORD:
4250 case NFA_SKWORD:
4251 case NFA_FNAME:
4252 case NFA_SFNAME:
4253 case NFA_PRINT:
4254 case NFA_SPRINT:
4255 case NFA_WHITE:
4256 case NFA_NWHITE:
4257 case NFA_DIGIT:
4258 case NFA_NDIGIT:
4259 case NFA_HEX:
4260 case NFA_NHEX:
4261 case NFA_OCTAL:
4262 case NFA_NOCTAL:
4263 case NFA_WORD:
4264 case NFA_NWORD:
4265 case NFA_HEAD:
4266 case NFA_NHEAD:
4267 case NFA_ALPHA:
4268 case NFA_NALPHA:
4269 case NFA_LOWER:
4270 case NFA_NLOWER:
4271 case NFA_UPPER:
4272 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004273 case NFA_LOWER_IC:
4274 case NFA_NLOWER_IC:
4275 case NFA_UPPER_IC:
4276 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004277 case NFA_START_COLL:
4278 case NFA_START_NEG_COLL:
4279 case NFA_NEWL:
4280 /* state will advance input */
4281 return FALSE;
4282
4283 default:
4284 if (state->c > 0)
4285 /* state will advance input */
4286 return FALSE;
4287
4288 /* Others: zero-width or possibly zero-width, might still find
4289 * a match at the same position, keep looking. */
4290 break;
4291 }
4292 state = state->out;
4293 }
4294 return FALSE;
4295}
4296
4297
4298/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004299 * Return TRUE if "state" is already in list "l".
4300 */
4301 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004302state_in_list(
4303 nfa_list_T *l, /* runtime state list */
4304 nfa_state_T *state, /* state to update */
4305 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004306{
4307 if (state->lastlist[nfa_ll_index] == l->id)
4308 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004309 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004310 return TRUE;
4311 }
4312 return FALSE;
4313}
4314
Bram Moolenaar16b35782016-09-09 20:29:50 +02004315/* Offset used for "off" by addstate_here(). */
4316#define ADDSTATE_HERE_OFFSET 10
4317
Bram Moolenaard05bf562013-06-30 23:24:08 +02004318/*
4319 * Add "state" and possibly what follows to state list ".".
4320 * Returns "subs_arg", possibly copied into temp_subs.
4321 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004322 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004323addstate(
4324 nfa_list_T *l, /* runtime state list */
4325 nfa_state_T *state, /* state to update */
4326 regsubs_T *subs_arg, /* pointers to subexpressions */
4327 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004328 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004329{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004330 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004331 int off = off_arg;
4332 int add_here = FALSE;
4333 int listindex = 0;
4334 int k;
4335 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004336 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004337 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004338 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004339 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004340 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004341 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004342 regsubs_T *subs = subs_arg;
4343 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004344#ifdef ENABLE_LOG
4345 int did_print = FALSE;
4346#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004347
Bram Moolenaar16b35782016-09-09 20:29:50 +02004348 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4349 {
4350 add_here = TRUE;
4351 off = 0;
4352 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4353 }
4354
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004355 switch (state->c)
4356 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004357 case NFA_NCLOSE:
4358 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004359 case NFA_MCLOSE1:
4360 case NFA_MCLOSE2:
4361 case NFA_MCLOSE3:
4362 case NFA_MCLOSE4:
4363 case NFA_MCLOSE5:
4364 case NFA_MCLOSE6:
4365 case NFA_MCLOSE7:
4366 case NFA_MCLOSE8:
4367 case NFA_MCLOSE9:
4368#ifdef FEAT_SYN_HL
4369 case NFA_ZCLOSE:
4370 case NFA_ZCLOSE1:
4371 case NFA_ZCLOSE2:
4372 case NFA_ZCLOSE3:
4373 case NFA_ZCLOSE4:
4374 case NFA_ZCLOSE5:
4375 case NFA_ZCLOSE6:
4376 case NFA_ZCLOSE7:
4377 case NFA_ZCLOSE8:
4378 case NFA_ZCLOSE9:
4379#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004380 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004381 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004382 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004383 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004384 /* These nodes are not added themselves but their "out" and/or
4385 * "out1" may be added below. */
4386 break;
4387
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004388 case NFA_BOL:
4389 case NFA_BOF:
4390 /* "^" won't match past end-of-line, don't bother trying.
4391 * Except when at the end of the line, or when we are going to the
4392 * next line for a look-behind match. */
4393 if (reginput > regline
4394 && *reginput != NUL
4395 && (nfa_endp == NULL
4396 || !REG_MULTI
4397 || reglnum == nfa_endp->se_u.pos.lnum))
4398 goto skip_add;
4399 /* FALLTHROUGH */
4400
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004401 case NFA_MOPEN1:
4402 case NFA_MOPEN2:
4403 case NFA_MOPEN3:
4404 case NFA_MOPEN4:
4405 case NFA_MOPEN5:
4406 case NFA_MOPEN6:
4407 case NFA_MOPEN7:
4408 case NFA_MOPEN8:
4409 case NFA_MOPEN9:
4410#ifdef FEAT_SYN_HL
4411 case NFA_ZOPEN:
4412 case NFA_ZOPEN1:
4413 case NFA_ZOPEN2:
4414 case NFA_ZOPEN3:
4415 case NFA_ZOPEN4:
4416 case NFA_ZOPEN5:
4417 case NFA_ZOPEN6:
4418 case NFA_ZOPEN7:
4419 case NFA_ZOPEN8:
4420 case NFA_ZOPEN9:
4421#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004422 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004423 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004424 /* These nodes need to be added so that we can bail out when it
4425 * was added to this list before at the same position to avoid an
4426 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004427
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004428 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004429 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004430 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004431 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004432 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004433 * when there is a PIM. For NFA_MATCH check the position,
4434 * lower position is preferred. */
4435 if (!nfa_has_backref && pim == NULL && !l->has_pim
4436 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004437 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004438 /* When called from addstate_here() do insert before
4439 * existing states. */
4440 if (add_here)
4441 {
4442 for (k = 0; k < l->n && k < listindex; ++k)
4443 if (l->t[k].state->id == state->id)
4444 {
4445 found = TRUE;
4446 break;
4447 }
4448 }
4449 if (!add_here || found)
4450 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004451skip_add:
4452#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004453 nfa_set_code(state->c);
4454 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4455 abs(state->id), l->id, state->c, code,
4456 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004457#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004458 return subs;
4459 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004460 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004461
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004462 /* Do not add the state again when it exists with the same
4463 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004464 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004465 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004466 }
4467
Bram Moolenaara0169122013-06-26 18:16:58 +02004468 /* When there are backreferences or PIMs the number of states may
4469 * be (a lot) bigger than anticipated. */
4470 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004471 {
4472 int newlen = l->len * 3 / 2 + 50;
4473
Bram Moolenaard05bf562013-06-30 23:24:08 +02004474 if (subs != &temp_subs)
4475 {
4476 /* "subs" may point into the current array, need to make a
4477 * copy before it becomes invalid. */
4478 copy_sub(&temp_subs.norm, &subs->norm);
4479#ifdef FEAT_SYN_HL
4480 if (nfa_has_zsubexpr)
4481 copy_sub(&temp_subs.synt, &subs->synt);
4482#endif
4483 subs = &temp_subs;
4484 }
4485
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004486 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004487 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4488 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004489 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004490
4491 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004492 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004493 thread = &l->t[l->n++];
4494 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004495 if (pim == NULL)
4496 thread->pim.result = NFA_PIM_UNUSED;
4497 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004498 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004499 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004500 l->has_pim = TRUE;
4501 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004502 copy_sub(&thread->subs.norm, &subs->norm);
4503#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004504 if (nfa_has_zsubexpr)
4505 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004506#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004507#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004508 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004509 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004510#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004511 }
4512
4513#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004514 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004515 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004516#endif
4517 switch (state->c)
4518 {
4519 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004520 break;
4521
4522 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004523 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004524 subs = addstate(l, state->out, subs, pim, off_arg);
4525 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004526 break;
4527
Bram Moolenaar699c1202013-09-25 16:41:54 +02004528 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529 case NFA_NOPEN:
4530 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004531 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004532 break;
4533
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004534 case NFA_MOPEN:
4535 case NFA_MOPEN1:
4536 case NFA_MOPEN2:
4537 case NFA_MOPEN3:
4538 case NFA_MOPEN4:
4539 case NFA_MOPEN5:
4540 case NFA_MOPEN6:
4541 case NFA_MOPEN7:
4542 case NFA_MOPEN8:
4543 case NFA_MOPEN9:
4544#ifdef FEAT_SYN_HL
4545 case NFA_ZOPEN:
4546 case NFA_ZOPEN1:
4547 case NFA_ZOPEN2:
4548 case NFA_ZOPEN3:
4549 case NFA_ZOPEN4:
4550 case NFA_ZOPEN5:
4551 case NFA_ZOPEN6:
4552 case NFA_ZOPEN7:
4553 case NFA_ZOPEN8:
4554 case NFA_ZOPEN9:
4555#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004556 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004558 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004560 sub = &subs->norm;
4561 }
4562#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004563 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564 {
4565 subidx = state->c - NFA_ZOPEN;
4566 sub = &subs->synt;
4567 }
4568#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004569 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004570 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004571 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004572 sub = &subs->norm;
4573 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004575 /* avoid compiler warnings */
4576 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004577 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004578
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004579 /* Set the position (with "off" added) in the subexpression. Save
4580 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004581 if (REG_MULTI)
4582 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004583 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004584 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004585 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004586 save_in_use = -1;
4587 }
4588 else
4589 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004590 save_in_use = sub->in_use;
4591 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004592 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004593 sub->list.multi[i].start_lnum = -1;
4594 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004595 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004596 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004597 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004598 if (off == -1)
4599 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004600 sub->list.multi[subidx].start_lnum = reglnum + 1;
4601 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004602 }
4603 else
4604 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004605 sub->list.multi[subidx].start_lnum = reglnum;
4606 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004607 (colnr_T)(reginput - regline + off);
4608 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004609 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004610 }
4611 else
4612 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004613 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004614 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004615 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004616 save_in_use = -1;
4617 }
4618 else
4619 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004620 save_in_use = sub->in_use;
4621 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004622 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004623 sub->list.line[i].start = NULL;
4624 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004626 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004627 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004628 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629 }
4630
Bram Moolenaar16b35782016-09-09 20:29:50 +02004631 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004632 /* "subs" may have changed, need to set "sub" again */
4633#ifdef FEAT_SYN_HL
4634 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4635 sub = &subs->synt;
4636 else
4637#endif
4638 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004640 if (save_in_use == -1)
4641 {
4642 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004643 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004644 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004645 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004646 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004648 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004649 break;
4650
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004651 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004652 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004653 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004654 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004655 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004656 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004657 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 break;
4659 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004660 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004661 case NFA_MCLOSE1:
4662 case NFA_MCLOSE2:
4663 case NFA_MCLOSE3:
4664 case NFA_MCLOSE4:
4665 case NFA_MCLOSE5:
4666 case NFA_MCLOSE6:
4667 case NFA_MCLOSE7:
4668 case NFA_MCLOSE8:
4669 case NFA_MCLOSE9:
4670#ifdef FEAT_SYN_HL
4671 case NFA_ZCLOSE:
4672 case NFA_ZCLOSE1:
4673 case NFA_ZCLOSE2:
4674 case NFA_ZCLOSE3:
4675 case NFA_ZCLOSE4:
4676 case NFA_ZCLOSE5:
4677 case NFA_ZCLOSE6:
4678 case NFA_ZCLOSE7:
4679 case NFA_ZCLOSE8:
4680 case NFA_ZCLOSE9:
4681#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004682 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004684 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004685 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004686 sub = &subs->norm;
4687 }
4688#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004689 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004690 {
4691 subidx = state->c - NFA_ZCLOSE;
4692 sub = &subs->synt;
4693 }
4694#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004695 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004696 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004697 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004698 sub = &subs->norm;
4699 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004700
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004701 /* We don't fill in gaps here, there must have been an MOPEN that
4702 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004703 save_in_use = sub->in_use;
4704 if (sub->in_use <= subidx)
4705 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004706 if (REG_MULTI)
4707 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004708 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004709 if (off == -1)
4710 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004711 sub->list.multi[subidx].end_lnum = reglnum + 1;
4712 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004713 }
4714 else
4715 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004716 sub->list.multi[subidx].end_lnum = reglnum;
4717 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004718 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004719 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004720 /* avoid compiler warnings */
4721 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004722 }
4723 else
4724 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004725 save_ptr = sub->list.line[subidx].end;
4726 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004727 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004728 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004729 }
4730
Bram Moolenaar16b35782016-09-09 20:29:50 +02004731 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004732 /* "subs" may have changed, need to set "sub" again */
4733#ifdef FEAT_SYN_HL
4734 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4735 sub = &subs->synt;
4736 else
4737#endif
4738 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004739
4740 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004741 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004742 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004743 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004744 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004745 break;
4746 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004747 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004748}
4749
4750/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004751 * Like addstate(), but the new state(s) are put at position "*ip".
4752 * Used for zero-width matches, next state to use is the added one.
4753 * This makes sure the order of states to be tried does not change, which
4754 * matters for alternatives.
4755 */
4756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004757addstate_here(
4758 nfa_list_T *l, /* runtime state list */
4759 nfa_state_T *state, /* state to update */
4760 regsubs_T *subs, /* pointers to subexpressions */
4761 nfa_pim_T *pim, /* postponed look-behind match */
4762 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004763{
4764 int tlen = l->n;
4765 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004766 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004767
Bram Moolenaar16b35782016-09-09 20:29:50 +02004768 /* First add the state(s) at the end, so that we know how many there are.
4769 * Pass the listidx as offset (avoids adding another argument to
4770 * addstate(). */
4771 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004772
Bram Moolenaar4b417062013-05-25 20:19:50 +02004773 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004774 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004775 return;
4776
4777 /* re-order to put the new state at the current position */
4778 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004779 if (count == 0)
4780 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004781 if (count == 1)
4782 {
4783 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004784 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004785 }
4786 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004787 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004788 if (l->n + count - 1 >= l->len)
4789 {
4790 /* not enough space to move the new states, reallocate the list
4791 * and move the states to the right position */
4792 nfa_thread_T *newl;
4793
4794 l->len = l->len * 3 / 2 + 50;
4795 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4796 if (newl == NULL)
4797 return;
4798 mch_memmove(&(newl[0]),
4799 &(l->t[0]),
4800 sizeof(nfa_thread_T) * listidx);
4801 mch_memmove(&(newl[listidx]),
4802 &(l->t[l->n - count]),
4803 sizeof(nfa_thread_T) * count);
4804 mch_memmove(&(newl[listidx + count]),
4805 &(l->t[listidx + 1]),
4806 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4807 vim_free(l->t);
4808 l->t = newl;
4809 }
4810 else
4811 {
4812 /* make space for new states, then move them from the
4813 * end to the current position */
4814 mch_memmove(&(l->t[listidx + count]),
4815 &(l->t[listidx + 1]),
4816 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4817 mch_memmove(&(l->t[listidx]),
4818 &(l->t[l->n - 1]),
4819 sizeof(nfa_thread_T) * count);
4820 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004821 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004822 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004823 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004824}
4825
4826/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004827 * Check character class "class" against current character c.
4828 */
4829 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004830check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004831{
4832 switch (class)
4833 {
4834 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004835 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004836 return OK;
4837 break;
4838 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004839 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004840 return OK;
4841 break;
4842 case NFA_CLASS_BLANK:
4843 if (c == ' ' || c == '\t')
4844 return OK;
4845 break;
4846 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004847 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004848 return OK;
4849 break;
4850 case NFA_CLASS_DIGIT:
4851 if (VIM_ISDIGIT(c))
4852 return OK;
4853 break;
4854 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004855 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004856 return OK;
4857 break;
4858 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004859 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004860 return OK;
4861 break;
4862 case NFA_CLASS_PRINT:
4863 if (vim_isprintc(c))
4864 return OK;
4865 break;
4866 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004867 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004868 return OK;
4869 break;
4870 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004871 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 return OK;
4873 break;
4874 case NFA_CLASS_UPPER:
4875 if (MB_ISUPPER(c))
4876 return OK;
4877 break;
4878 case NFA_CLASS_XDIGIT:
4879 if (vim_isxdigit(c))
4880 return OK;
4881 break;
4882 case NFA_CLASS_TAB:
4883 if (c == '\t')
4884 return OK;
4885 break;
4886 case NFA_CLASS_RETURN:
4887 if (c == '\r')
4888 return OK;
4889 break;
4890 case NFA_CLASS_BACKSPACE:
4891 if (c == '\b')
4892 return OK;
4893 break;
4894 case NFA_CLASS_ESCAPE:
4895 if (c == '\033')
4896 return OK;
4897 break;
4898
4899 default:
4900 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004901 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004902 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004903 }
4904 return FAIL;
4905}
4906
Bram Moolenaar5714b802013-05-28 22:03:20 +02004907/*
4908 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004909 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004910 */
4911 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004912match_backref(
4913 regsub_T *sub, /* pointers to subexpressions */
4914 int subidx,
4915 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004916{
4917 int len;
4918
4919 if (sub->in_use <= subidx)
4920 {
4921retempty:
4922 /* backref was not set, match an empty string */
4923 *bytelen = 0;
4924 return TRUE;
4925 }
4926
4927 if (REG_MULTI)
4928 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004929 if (sub->list.multi[subidx].start_lnum < 0
4930 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004931 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004932 if (sub->list.multi[subidx].start_lnum == reglnum
4933 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004934 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004935 len = sub->list.multi[subidx].end_col
4936 - sub->list.multi[subidx].start_col;
4937 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004938 reginput, &len) == 0)
4939 {
4940 *bytelen = len;
4941 return TRUE;
4942 }
4943 }
4944 else
4945 {
4946 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004947 sub->list.multi[subidx].start_lnum,
4948 sub->list.multi[subidx].start_col,
4949 sub->list.multi[subidx].end_lnum,
4950 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004951 bytelen) == RA_MATCH)
4952 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004953 }
4954 }
4955 else
4956 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004957 if (sub->list.line[subidx].start == NULL
4958 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004959 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004960 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4961 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004962 {
4963 *bytelen = len;
4964 return TRUE;
4965 }
4966 }
4967 return FALSE;
4968}
4969
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004970#ifdef FEAT_SYN_HL
4971
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004972/*
4973 * Check for a match with \z subexpression "subidx".
4974 * Return TRUE if it matches.
4975 */
4976 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004977match_zref(
4978 int subidx,
4979 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004980{
4981 int len;
4982
4983 cleanup_zsubexpr();
4984 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4985 {
4986 /* backref was not set, match an empty string */
4987 *bytelen = 0;
4988 return TRUE;
4989 }
4990
4991 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4992 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4993 {
4994 *bytelen = len;
4995 return TRUE;
4996 }
4997 return FALSE;
4998}
4999#endif
5000
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005001/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005002 * Save list IDs for all NFA states of "prog" into "list".
5003 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005004 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005005 */
5006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005007nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005008{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005009 int i;
5010 nfa_state_T *p;
5011
5012 /* Order in the list is reverse, it's a bit faster that way. */
5013 p = &prog->state[0];
5014 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005015 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005016 list[i] = p->lastlist[1];
5017 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005018 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005019 }
5020}
5021
5022/*
5023 * Restore list IDs from "list" to all NFA states.
5024 */
5025 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005026nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005028 int i;
5029 nfa_state_T *p;
5030
5031 p = &prog->state[0];
5032 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005033 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005034 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005035 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005036 }
5037}
5038
Bram Moolenaar423532e2013-05-29 21:14:42 +02005039 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005040nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005041{
5042 if (op == 1) return pos > val;
5043 if (op == 2) return pos < val;
5044 return val == pos;
5045}
5046
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005047static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005048
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005049/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005050 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005051 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5052 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005053 */
5054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005055recursive_regmatch(
5056 nfa_state_T *state,
5057 nfa_pim_T *pim,
5058 nfa_regprog_T *prog,
5059 regsubs_T *submatch,
5060 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005061 int **listids,
5062 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005063{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005064 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005065 int save_reglnum = reglnum;
5066 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005067 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005068 save_se_T *save_nfa_endp = nfa_endp;
5069 save_se_T endpos;
5070 save_se_T *endposp = NULL;
5071 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005072 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005073
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005074 if (pim != NULL)
5075 {
5076 /* start at the position where the postponed match was */
5077 if (REG_MULTI)
5078 reginput = regline + pim->end.pos.col;
5079 else
5080 reginput = pim->end.ptr;
5081 }
5082
Bram Moolenaardecd9542013-06-07 16:31:50 +02005083 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005084 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5085 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5086 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005087 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005088 /* The recursive match must end at the current position. When "pim" is
5089 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005090 endposp = &endpos;
5091 if (REG_MULTI)
5092 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005093 if (pim == NULL)
5094 {
5095 endpos.se_u.pos.col = (int)(reginput - regline);
5096 endpos.se_u.pos.lnum = reglnum;
5097 }
5098 else
5099 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005100 }
5101 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005102 {
5103 if (pim == NULL)
5104 endpos.se_u.ptr = reginput;
5105 else
5106 endpos.se_u.ptr = pim->end.ptr;
5107 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005108
5109 /* Go back the specified number of bytes, or as far as the
5110 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005111 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005112 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005113 if (state->val <= 0)
5114 {
5115 if (REG_MULTI)
5116 {
5117 regline = reg_getline(--reglnum);
5118 if (regline == NULL)
5119 /* can't go before the first line */
5120 regline = reg_getline(++reglnum);
5121 }
5122 reginput = regline;
5123 }
5124 else
5125 {
5126 if (REG_MULTI && (int)(reginput - regline) < state->val)
5127 {
5128 /* Not enough bytes in this line, go to end of
5129 * previous line. */
5130 regline = reg_getline(--reglnum);
5131 if (regline == NULL)
5132 {
5133 /* can't go before the first line */
5134 regline = reg_getline(++reglnum);
5135 reginput = regline;
5136 }
5137 else
5138 reginput = regline + STRLEN(regline);
5139 }
5140 if ((int)(reginput - regline) >= state->val)
5141 {
5142 reginput -= state->val;
5143#ifdef FEAT_MBYTE
5144 if (has_mbyte)
5145 reginput -= mb_head_off(regline, reginput);
5146#endif
5147 }
5148 else
5149 reginput = regline;
5150 }
5151 }
5152
Bram Moolenaarf46da702013-06-02 22:37:42 +02005153#ifdef ENABLE_LOG
5154 if (log_fd != stderr)
5155 fclose(log_fd);
5156 log_fd = NULL;
5157#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005158 /* Have to clear the lastlist field of the NFA nodes, so that
5159 * nfa_regmatch() and addstate() can run properly after recursion. */
5160 if (nfa_ll_index == 1)
5161 {
5162 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5163 * values and clear them. */
Bram Moolenaar2338c322018-07-08 19:07:19 +02005164 if (*listids == NULL || *listids_len < nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005165 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005166 vim_free(*listids);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005167 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5168 if (*listids == NULL)
5169 {
5170 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5171 return 0;
5172 }
Bram Moolenaar2338c322018-07-08 19:07:19 +02005173 *listids_len = nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005174 }
5175 nfa_save_listids(prog, *listids);
5176 need_restore = TRUE;
5177 /* any value of nfa_listid will do */
5178 }
5179 else
5180 {
5181 /* First recursive nfa_regmatch() call, switch to the second lastlist
5182 * entry. Make sure nfa_listid is different from a previous recursive
5183 * call, because some states may still have this ID. */
5184 ++nfa_ll_index;
5185 if (nfa_listid <= nfa_alt_listid)
5186 nfa_listid = nfa_alt_listid;
5187 }
5188
5189 /* Call nfa_regmatch() to check if the current concat matches at this
5190 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005191 nfa_endp = endposp;
5192 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005193
5194 if (need_restore)
5195 nfa_restore_listids(prog, *listids);
5196 else
5197 {
5198 --nfa_ll_index;
5199 nfa_alt_listid = nfa_listid;
5200 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005201
5202 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005203 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005204 if (REG_MULTI)
5205 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005206 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005207 if (result != NFA_TOO_EXPENSIVE)
5208 {
5209 nfa_match = save_nfa_match;
5210 nfa_listid = save_nfa_listid;
5211 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005212 nfa_endp = save_nfa_endp;
5213
5214#ifdef ENABLE_LOG
5215 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5216 if (log_fd != NULL)
5217 {
5218 fprintf(log_fd, "****************************\n");
5219 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5220 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5221 fprintf(log_fd, "****************************\n");
5222 }
5223 else
5224 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005225 EMSG(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005226 log_fd = stderr;
5227 }
5228#endif
5229
5230 return result;
5231}
5232
Bram Moolenaara2d95102013-06-04 14:23:05 +02005233/*
5234 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005235 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005236 * NFA_ANY: 1
5237 * specific character: 99
5238 */
5239 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005240failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005241{
5242 int c = state->c;
5243 int l, r;
5244
5245 /* detect looping */
5246 if (depth > 4)
5247 return 1;
5248
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005249 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005250 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005251 case NFA_SPLIT:
5252 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5253 /* avoid recursive stuff */
5254 return 1;
5255 /* two alternatives, use the lowest failure chance */
5256 l = failure_chance(state->out, depth + 1);
5257 r = failure_chance(state->out1, depth + 1);
5258 return l < r ? l : r;
5259
5260 case NFA_ANY:
5261 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005262 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005263
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005264 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005265 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005266 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005267 /* empty match works always */
5268 return 0;
5269
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005270 case NFA_START_INVISIBLE:
5271 case NFA_START_INVISIBLE_FIRST:
5272 case NFA_START_INVISIBLE_NEG:
5273 case NFA_START_INVISIBLE_NEG_FIRST:
5274 case NFA_START_INVISIBLE_BEFORE:
5275 case NFA_START_INVISIBLE_BEFORE_FIRST:
5276 case NFA_START_INVISIBLE_BEFORE_NEG:
5277 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5278 case NFA_START_PATTERN:
5279 /* recursive regmatch is expensive, use low failure chance */
5280 return 5;
5281
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005282 case NFA_BOL:
5283 case NFA_EOL:
5284 case NFA_BOF:
5285 case NFA_EOF:
5286 case NFA_NEWL:
5287 return 99;
5288
5289 case NFA_BOW:
5290 case NFA_EOW:
5291 return 90;
5292
5293 case NFA_MOPEN:
5294 case NFA_MOPEN1:
5295 case NFA_MOPEN2:
5296 case NFA_MOPEN3:
5297 case NFA_MOPEN4:
5298 case NFA_MOPEN5:
5299 case NFA_MOPEN6:
5300 case NFA_MOPEN7:
5301 case NFA_MOPEN8:
5302 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005303#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005304 case NFA_ZOPEN:
5305 case NFA_ZOPEN1:
5306 case NFA_ZOPEN2:
5307 case NFA_ZOPEN3:
5308 case NFA_ZOPEN4:
5309 case NFA_ZOPEN5:
5310 case NFA_ZOPEN6:
5311 case NFA_ZOPEN7:
5312 case NFA_ZOPEN8:
5313 case NFA_ZOPEN9:
5314 case NFA_ZCLOSE:
5315 case NFA_ZCLOSE1:
5316 case NFA_ZCLOSE2:
5317 case NFA_ZCLOSE3:
5318 case NFA_ZCLOSE4:
5319 case NFA_ZCLOSE5:
5320 case NFA_ZCLOSE6:
5321 case NFA_ZCLOSE7:
5322 case NFA_ZCLOSE8:
5323 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005324#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005325 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005326 case NFA_MCLOSE1:
5327 case NFA_MCLOSE2:
5328 case NFA_MCLOSE3:
5329 case NFA_MCLOSE4:
5330 case NFA_MCLOSE5:
5331 case NFA_MCLOSE6:
5332 case NFA_MCLOSE7:
5333 case NFA_MCLOSE8:
5334 case NFA_MCLOSE9:
5335 case NFA_NCLOSE:
5336 return failure_chance(state->out, depth + 1);
5337
5338 case NFA_BACKREF1:
5339 case NFA_BACKREF2:
5340 case NFA_BACKREF3:
5341 case NFA_BACKREF4:
5342 case NFA_BACKREF5:
5343 case NFA_BACKREF6:
5344 case NFA_BACKREF7:
5345 case NFA_BACKREF8:
5346 case NFA_BACKREF9:
5347#ifdef FEAT_SYN_HL
5348 case NFA_ZREF1:
5349 case NFA_ZREF2:
5350 case NFA_ZREF3:
5351 case NFA_ZREF4:
5352 case NFA_ZREF5:
5353 case NFA_ZREF6:
5354 case NFA_ZREF7:
5355 case NFA_ZREF8:
5356 case NFA_ZREF9:
5357#endif
5358 /* backreferences don't match in many places */
5359 return 94;
5360
5361 case NFA_LNUM_GT:
5362 case NFA_LNUM_LT:
5363 case NFA_COL_GT:
5364 case NFA_COL_LT:
5365 case NFA_VCOL_GT:
5366 case NFA_VCOL_LT:
5367 case NFA_MARK_GT:
5368 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005369 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005370 /* before/after positions don't match very often */
5371 return 85;
5372
5373 case NFA_LNUM:
5374 return 90;
5375
5376 case NFA_CURSOR:
5377 case NFA_COL:
5378 case NFA_VCOL:
5379 case NFA_MARK:
5380 /* specific positions rarely match */
5381 return 98;
5382
5383 case NFA_COMPOSING:
5384 return 95;
5385
5386 default:
5387 if (c > 0)
5388 /* character match fails often */
5389 return 95;
5390 }
5391
5392 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005393 return 50;
5394}
5395
Bram Moolenaarf46da702013-06-02 22:37:42 +02005396/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005397 * Skip until the char "c" we know a match must start with.
5398 */
5399 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005400skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005401{
5402 char_u *s;
5403
5404 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005405 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005406#ifdef FEAT_MBYTE
5407 && !has_mbyte
5408#endif
5409 )
5410 s = vim_strbyte(regline + *colp, c);
5411 else
5412 s = cstrchr(regline + *colp, c);
5413 if (s == NULL)
5414 return FAIL;
5415 *colp = (int)(s - regline);
5416 return OK;
5417}
5418
5419/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005420 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005421 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005422 * Returns zero for no match, 1 for a match.
5423 */
5424 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005425find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005426{
5427 colnr_T col = startcol;
5428 int c1, c2;
5429 int len1, len2;
5430 int match;
5431
5432 for (;;)
5433 {
5434 match = TRUE;
5435 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5436 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5437 {
5438 c1 = PTR2CHAR(match_text + len1);
5439 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005440 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005441 {
5442 match = FALSE;
5443 break;
5444 }
5445 len2 += MB_CHAR2LEN(c2);
5446 }
5447 if (match
5448#ifdef FEAT_MBYTE
5449 /* check that no composing char follows */
5450 && !(enc_utf8
5451 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5452#endif
5453 )
5454 {
5455 cleanup_subexpr();
5456 if (REG_MULTI)
5457 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005458 rex.reg_startpos[0].lnum = reglnum;
5459 rex.reg_startpos[0].col = col;
5460 rex.reg_endpos[0].lnum = reglnum;
5461 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005462 }
5463 else
5464 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005465 rex.reg_startp[0] = regline + col;
5466 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005467 }
5468 return 1L;
5469 }
5470
5471 /* Try finding regstart after the current match. */
5472 col += MB_CHAR2LEN(regstart); /* skip regstart */
5473 if (skip_to_start(regstart, &col) == FAIL)
5474 break;
5475 }
5476 return 0L;
5477}
5478
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005479#ifdef FEAT_RELTIME
5480 static int
5481nfa_did_time_out()
5482{
5483 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5484 {
5485 if (nfa_timed_out != NULL)
5486 *nfa_timed_out = TRUE;
5487 return TRUE;
5488 }
5489 return FALSE;
5490}
5491#endif
5492
Bram Moolenaar473de612013-06-08 18:19:48 +02005493/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494 * Main matching routine.
5495 *
5496 * Run NFA to determine whether it matches reginput.
5497 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005498 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005499 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005500 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005501 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005502 * Note: Caller must ensure that: start != NULL.
5503 */
5504 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005505nfa_regmatch(
5506 nfa_regprog_T *prog,
5507 nfa_state_T *start,
5508 regsubs_T *submatch,
5509 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005510{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005511 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005512 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005514 int go_to_nextline = FALSE;
5515 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005516 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005517 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005518 nfa_list_T *thislist;
5519 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005521 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005522 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005523 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005524 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005525 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005526 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005527#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005528 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005530
Bram Moolenaar41f12052013-08-25 17:01:42 +02005531 /* Some patterns may take a long time to match, especially when using
5532 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5533 fast_breakcheck();
5534 if (got_int)
5535 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005536#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005537 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005538 return FALSE;
5539#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005540
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005541#ifdef NFA_REGEXP_DEBUG_LOG
5542 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5543 if (debug == NULL)
5544 {
Bram Moolenaar5efa0102018-06-22 21:42:30 +02005545 EMSG2("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005546 return FALSE;
5547 }
5548#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005549 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005551 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005552 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005553
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005554 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005555 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005556 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005557 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005558 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005559 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560
5561#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005562 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563 if (log_fd != NULL)
5564 {
5565 fprintf(log_fd, "**********************************\n");
5566 nfa_set_code(start->c);
5567 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5568 abs(start->id), code);
5569 fprintf(log_fd, "**********************************\n");
5570 }
5571 else
5572 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005573 EMSG(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574 log_fd = stderr;
5575 }
5576#endif
5577
5578 thislist = &list[0];
5579 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005580 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005581 nextlist = &list[1];
5582 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005583 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005584#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005585 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005586#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005587 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005588
5589 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5590 * it's the first MOPEN. */
5591 if (toplevel)
5592 {
5593 if (REG_MULTI)
5594 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005595 m->norm.list.multi[0].start_lnum = reglnum;
5596 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005597 }
5598 else
5599 m->norm.list.line[0].start = reginput;
5600 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005601 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005602 }
5603 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005604 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005605
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005606#define ADD_STATE_IF_MATCH(state) \
5607 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005608 add_state = state->out; \
5609 add_off = clen; \
5610 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005611
5612 /*
5613 * Run for each character.
5614 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005615 for (;;)
5616 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005617 int curc;
5618 int clen;
5619
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005620#ifdef FEAT_MBYTE
5621 if (has_mbyte)
5622 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005623 curc = (*mb_ptr2char)(reginput);
5624 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005625 }
5626 else
5627#endif
5628 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005629 curc = *reginput;
5630 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005631 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005632 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005633 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005634 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005635 go_to_nextline = FALSE;
5636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637
5638 /* swap lists */
5639 thislist = &list[flag];
5640 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005641 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005642 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005643 ++nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005644 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005645 && (nfa_listid >= NFA_MAX_STATES
5646# ifdef FEAT_EVAL
5647 || nfa_fail_for_testing
5648# endif
5649 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005650 {
5651 /* too many states, retry with old engine */
5652 nfa_match = NFA_TOO_EXPENSIVE;
5653 goto theend;
5654 }
5655
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005656 thislist->id = nfa_listid;
5657 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005658
5659#ifdef ENABLE_LOG
5660 fprintf(log_fd, "------------------------------------------\n");
5661 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005662 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005663 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005664 {
5665 int i;
5666
5667 for (i = 0; i < thislist->n; i++)
5668 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5669 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005670 fprintf(log_fd, "\n");
5671#endif
5672
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005673#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674 fprintf(debug, "\n-------------------\n");
5675#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005676 /*
5677 * If the state lists are empty we can stop.
5678 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005679 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005680 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681
5682 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005683 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005684 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005685 /* If the list gets very long there probably is something wrong.
5686 * At least allow interrupting with CTRL-C. */
5687 fast_breakcheck();
5688 if (got_int)
5689 break;
5690#ifdef FEAT_RELTIME
5691 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5692 {
5693 nfa_time_count = 0;
5694 if (nfa_did_time_out())
5695 break;
5696 }
5697#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005698 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005699
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005700#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 nfa_set_code(t->state->c);
5702 fprintf(debug, "%s, ", code);
5703#endif
5704#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005705 {
5706 int col;
5707
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005708 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005709 col = -1;
5710 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005711 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005712 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005713 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005714 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005715 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005716 abs(t->state->id), (int)t->state->c, code, col,
5717 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005718 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005719#endif
5720
5721 /*
5722 * Handle the possible codes of the current state.
5723 * The most important is NFA_MATCH.
5724 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005725 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005726 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005727 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005728 switch (t->state->c)
5729 {
5730 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005731 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005732#ifdef FEAT_MBYTE
5733 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005734 * rex.reg_icombine is not set, that is not really a match. */
5735 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005736 break;
5737#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005738 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005739 copy_sub(&submatch->norm, &t->subs.norm);
5740#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005741 if (nfa_has_zsubexpr)
5742 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005743#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005744#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005745 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005746#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005747 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005748 * states at this position. When the list of states is going
5749 * to be empty quit without advancing, so that "reginput" is
5750 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005751 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005752 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005753 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005754 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005755
5756 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005757 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005758 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005759 /*
5760 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005761 * NFA_START_INVISIBLE_BEFORE node.
5762 * They surround a zero-width group, used with "\@=", "\&",
5763 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005764 * If we got here, it means that the current "invisible" group
5765 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005766 * nfa_regmatch(). For a look-behind match only when it ends
5767 * in the position in "nfa_endp".
5768 * Submatches are stored in *m, and used in the parent call.
5769 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005770#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005771 if (nfa_endp != NULL)
5772 {
5773 if (REG_MULTI)
5774 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5775 (int)reglnum,
5776 (int)nfa_endp->se_u.pos.lnum,
5777 (int)(reginput - regline),
5778 nfa_endp->se_u.pos.col);
5779 else
5780 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5781 (int)(reginput - regline),
5782 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005783 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005784#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005785 /* If "nfa_endp" is set it's only a match if it ends at
5786 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005787 if (nfa_endp != NULL && (REG_MULTI
5788 ? (reglnum != nfa_endp->se_u.pos.lnum
5789 || (int)(reginput - regline)
5790 != nfa_endp->se_u.pos.col)
5791 : reginput != nfa_endp->se_u.ptr))
5792 break;
5793
5794 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005795 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005796 {
5797 copy_sub(&m->norm, &t->subs.norm);
5798#ifdef FEAT_SYN_HL
5799 if (nfa_has_zsubexpr)
5800 copy_sub(&m->synt, &t->subs.synt);
5801#endif
5802 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005803#ifdef ENABLE_LOG
5804 fprintf(log_fd, "Match found:\n");
5805 log_subsexpr(m);
5806#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005807 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005808 /* See comment above at "goto nextchar". */
5809 if (nextlist->n == 0)
5810 clen = 0;
5811 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005812
5813 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005814 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005815 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005816 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005817 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005818 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005819 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005820 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005821 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005822#ifdef ENABLE_LOG
5823 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5824 failure_chance(t->state->out, 0),
5825 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005826#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005827 /* Do it directly if there already is a PIM or when
5828 * nfa_postprocess() detected it will work better. */
5829 if (t->pim.result != NFA_PIM_UNUSED
5830 || t->state->c == NFA_START_INVISIBLE_FIRST
5831 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5832 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5833 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005834 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005835 int in_use = m->norm.in_use;
5836
Bram Moolenaar699c1202013-09-25 16:41:54 +02005837 /* Copy submatch info for the recursive call, opposite
5838 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005839 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005840#ifdef FEAT_SYN_HL
5841 if (nfa_has_zsubexpr)
5842 copy_sub_off(&m->synt, &t->subs.synt);
5843#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005844
Bram Moolenaara2d95102013-06-04 14:23:05 +02005845 /*
5846 * First try matching the invisible match, then what
5847 * follows.
5848 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005849 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005850 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005851 if (result == NFA_TOO_EXPENSIVE)
5852 {
5853 nfa_match = result;
5854 goto theend;
5855 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005856
Bram Moolenaardecd9542013-06-07 16:31:50 +02005857 /* for \@! and \@<! it is a match when the result is
5858 * FALSE */
5859 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005860 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5861 || t->state->c
5862 == NFA_START_INVISIBLE_BEFORE_NEG
5863 || t->state->c
5864 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005865 {
5866 /* Copy submatch info from the recursive call */
5867 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005868#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005869 if (nfa_has_zsubexpr)
5870 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005871#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005872 /* If the pattern has \ze and it matched in the
5873 * sub pattern, use it. */
5874 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005875
Bram Moolenaara2d95102013-06-04 14:23:05 +02005876 /* t->state->out1 is the corresponding
5877 * END_INVISIBLE node; Add its out to the current
5878 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005879 add_here = TRUE;
5880 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005881 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005882 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005883 }
5884 else
5885 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005886 nfa_pim_T pim;
5887
Bram Moolenaara2d95102013-06-04 14:23:05 +02005888 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005889 * First try matching what follows. Only if a match
5890 * is found verify the invisible match matches. Add a
5891 * nfa_pim_T to the following states, it contains info
5892 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005893 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005894 pim.state = t->state;
5895 pim.result = NFA_PIM_TODO;
5896 pim.subs.norm.in_use = 0;
5897#ifdef FEAT_SYN_HL
5898 pim.subs.synt.in_use = 0;
5899#endif
5900 if (REG_MULTI)
5901 {
5902 pim.end.pos.col = (int)(reginput - regline);
5903 pim.end.pos.lnum = reglnum;
5904 }
5905 else
5906 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005907
5908 /* t->state->out1 is the corresponding END_INVISIBLE
5909 * node; Add its out to the current list (zero-width
5910 * match). */
5911 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005912 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005913 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005914 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005915 break;
5916
Bram Moolenaar87953742013-06-05 18:52:40 +02005917 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005918 {
5919 nfa_state_T *skip = NULL;
5920#ifdef ENABLE_LOG
5921 int skip_lid = 0;
5922#endif
5923
5924 /* There is no point in trying to match the pattern if the
5925 * output state is not going to be added to the list. */
5926 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5927 {
5928 skip = t->state->out1->out;
5929#ifdef ENABLE_LOG
5930 skip_lid = nextlist->id;
5931#endif
5932 }
5933 else if (state_in_list(nextlist,
5934 t->state->out1->out->out, &t->subs))
5935 {
5936 skip = t->state->out1->out->out;
5937#ifdef ENABLE_LOG
5938 skip_lid = nextlist->id;
5939#endif
5940 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005941 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005942 t->state->out1->out->out, &t->subs))
5943 {
5944 skip = t->state->out1->out->out;
5945#ifdef ENABLE_LOG
5946 skip_lid = thislist->id;
5947#endif
5948 }
5949 if (skip != NULL)
5950 {
5951#ifdef ENABLE_LOG
5952 nfa_set_code(skip->c);
5953 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5954 abs(skip->id), skip_lid, skip->c, code);
5955#endif
5956 break;
5957 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005958 /* Copy submatch info to the recursive call, opposite of what
5959 * happens afterwards. */
5960 copy_sub_off(&m->norm, &t->subs.norm);
5961#ifdef FEAT_SYN_HL
5962 if (nfa_has_zsubexpr)
5963 copy_sub_off(&m->synt, &t->subs.synt);
5964#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005965
Bram Moolenaar87953742013-06-05 18:52:40 +02005966 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005967 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005968 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005969 if (result == NFA_TOO_EXPENSIVE)
5970 {
5971 nfa_match = result;
5972 goto theend;
5973 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005974 if (result)
5975 {
5976 int bytelen;
5977
5978#ifdef ENABLE_LOG
5979 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5980 log_subsexpr(m);
5981#endif
5982 /* Copy submatch info from the recursive call */
5983 copy_sub_off(&t->subs.norm, &m->norm);
5984#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005985 if (nfa_has_zsubexpr)
5986 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005987#endif
5988 /* Now we need to skip over the matched text and then
5989 * continue with what follows. */
5990 if (REG_MULTI)
5991 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005992 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005993 - (int)(reginput - regline);
5994 else
5995 bytelen = (int)(m->norm.list.line[0].end - reginput);
5996
5997#ifdef ENABLE_LOG
5998 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5999#endif
6000 if (bytelen == 0)
6001 {
6002 /* empty match, output of corresponding
6003 * NFA_END_PATTERN/NFA_SKIP to be used at current
6004 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006005 add_here = TRUE;
6006 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006007 }
6008 else if (bytelen <= clen)
6009 {
6010 /* match current character, output of corresponding
6011 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006012 add_state = t->state->out1->out->out;
6013 add_off = clen;
6014 }
6015 else
6016 {
6017 /* skip over the matched characters, set character
6018 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006019 add_state = t->state->out1->out;
6020 add_off = bytelen;
6021 add_count = bytelen - clen;
6022 }
6023 }
6024 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006025 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006027 case NFA_BOL:
6028 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006029 {
6030 add_here = TRUE;
6031 add_state = t->state->out;
6032 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006033 break;
6034
6035 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006036 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006037 {
6038 add_here = TRUE;
6039 add_state = t->state->out;
6040 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006041 break;
6042
6043 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006044 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006045
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006046 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006047 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006048#ifdef FEAT_MBYTE
6049 else if (has_mbyte)
6050 {
6051 int this_class;
6052
6053 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006054 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006055 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006056 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006057 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006058 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006059 }
6060#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006061 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006062 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006063 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006064 result = FALSE;
6065 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006066 {
6067 add_here = TRUE;
6068 add_state = t->state->out;
6069 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006070 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006071
6072 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006073 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006075 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006076#ifdef FEAT_MBYTE
6077 else if (has_mbyte)
6078 {
6079 int this_class, prev_class;
6080
6081 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006082 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006083 prev_class = reg_prev_class();
6084 if (this_class == prev_class
6085 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006086 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006087 }
6088#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006089 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006090 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006091 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006092 result = FALSE;
6093 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006094 {
6095 add_here = TRUE;
6096 add_state = t->state->out;
6097 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006098 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006099
Bram Moolenaar4b780632013-05-31 22:14:52 +02006100 case NFA_BOF:
6101 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006102 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006103 {
6104 add_here = TRUE;
6105 add_state = t->state->out;
6106 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006107 break;
6108
6109 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006110 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006111 {
6112 add_here = TRUE;
6113 add_state = t->state->out;
6114 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006115 break;
6116
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006117#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006118 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006119 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006120 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006121 int len = 0;
6122 nfa_state_T *end;
6123 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006124 int cchars[MAX_MCO];
6125 int ccount = 0;
6126 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006127
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006128 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006129 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006130 if (utf_iscomposing(sta->c))
6131 {
6132 /* Only match composing character(s), ignore base
6133 * character. Used for ".{composing}" and "{composing}"
6134 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006135 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006136 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006137 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006138 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006139 /* If \Z was present, then ignore composing characters.
6140 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006141 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006142 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006143 else
6144 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006145 while (sta->c != NFA_END_COMPOSING)
6146 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006147 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006148
6149 /* Check base character matches first, unless ignored. */
6150 else if (len > 0 || mc == sta->c)
6151 {
6152 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006153 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006154 len += mb_char2len(mc);
6155 sta = sta->out;
6156 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006157
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006158 /* We don't care about the order of composing characters.
6159 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006160 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006161 {
6162 mc = mb_ptr2char(reginput + len);
6163 cchars[ccount++] = mc;
6164 len += mb_char2len(mc);
6165 if (ccount == MAX_MCO)
6166 break;
6167 }
6168
6169 /* Check that each composing char in the pattern matches a
6170 * composing char in the text. We do not check if all
6171 * composing chars are matched. */
6172 result = OK;
6173 while (sta->c != NFA_END_COMPOSING)
6174 {
6175 for (j = 0; j < ccount; ++j)
6176 if (cchars[j] == sta->c)
6177 break;
6178 if (j == ccount)
6179 {
6180 result = FAIL;
6181 break;
6182 }
6183 sta = sta->out;
6184 }
6185 }
6186 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006187 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006188
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006189 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006190 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006191 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006192 }
6193#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006194
6195 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006196 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6197 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006198 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006199 go_to_nextline = TRUE;
6200 /* Pass -1 for the offset, which means taking the position
6201 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006202 add_state = t->state->out;
6203 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006204 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006205 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006206 {
6207 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006208 add_state = t->state->out;
6209 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006210 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006211 break;
6212
Bram Moolenaar417bad22013-06-07 14:08:30 +02006213 case NFA_START_COLL:
6214 case NFA_START_NEG_COLL:
6215 {
6216 /* What follows is a list of characters, until NFA_END_COLL.
6217 * One of them must match or none of them must match. */
6218 nfa_state_T *state;
6219 int result_if_matched;
6220 int c1, c2;
6221
6222 /* Never match EOL. If it's part of the collection it is added
6223 * as a separate state with an OR. */
6224 if (curc == NUL)
6225 break;
6226
6227 state = t->state->out;
6228 result_if_matched = (t->state->c == NFA_START_COLL);
6229 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006230 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006231 if (state->c == NFA_END_COLL)
6232 {
6233 result = !result_if_matched;
6234 break;
6235 }
6236 if (state->c == NFA_RANGE_MIN)
6237 {
6238 c1 = state->val;
6239 state = state->out; /* advance to NFA_RANGE_MAX */
6240 c2 = state->val;
6241#ifdef ENABLE_LOG
6242 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6243 curc, c1, c2);
6244#endif
6245 if (curc >= c1 && curc <= c2)
6246 {
6247 result = result_if_matched;
6248 break;
6249 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006250 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006251 {
6252 int curc_low = MB_TOLOWER(curc);
6253 int done = FALSE;
6254
6255 for ( ; c1 <= c2; ++c1)
6256 if (MB_TOLOWER(c1) == curc_low)
6257 {
6258 result = result_if_matched;
6259 done = TRUE;
6260 break;
6261 }
6262 if (done)
6263 break;
6264 }
6265 }
6266 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006267 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006268 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006269 == MB_TOLOWER(state->c))))
6270 {
6271 result = result_if_matched;
6272 break;
6273 }
6274 state = state->out;
6275 }
6276 if (result)
6277 {
6278 /* next state is in out of the NFA_END_COLL, out1 of
6279 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006280 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006281 add_off = clen;
6282 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006283 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006284 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006285
6286 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006287 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006288 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006289 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006290 add_state = t->state->out;
6291 add_off = clen;
6292 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006293 break;
6294
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006295 case NFA_ANY_COMPOSING:
6296 /* On a composing character skip over it. Otherwise do
6297 * nothing. Always matches. */
6298#ifdef FEAT_MBYTE
6299 if (enc_utf8 && utf_iscomposing(curc))
6300 {
6301 add_off = clen;
6302 }
6303 else
6304#endif
6305 {
6306 add_here = TRUE;
6307 add_off = 0;
6308 }
6309 add_state = t->state->out;
6310 break;
6311
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006312 /*
6313 * Character classes like \a for alpha, \d for digit etc.
6314 */
6315 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006316 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006317 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318 break;
6319
6320 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006321 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006322 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323 break;
6324
6325 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006326 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006327 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328 break;
6329
6330 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006331 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006332 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006333 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006334 break;
6335
6336 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006337 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006338 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006339 break;
6340
6341 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006342 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006343 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006344 break;
6345
6346 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006347 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006348 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006349 break;
6350
6351 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006352 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006353 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006354 break;
6355
6356 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006357 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006358 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006359 break;
6360
6361 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006362 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006363 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006364 break;
6365
6366 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006367 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006368 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 break;
6370
6371 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006372 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006373 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006374 break;
6375
6376 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006377 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006378 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 break;
6380
6381 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006382 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006383 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 break;
6385
6386 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006387 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006388 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389 break;
6390
6391 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006392 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006393 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394 break;
6395
6396 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006397 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006398 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 break;
6400
6401 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006402 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006403 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006404 break;
6405
6406 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006407 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006408 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006409 break;
6410
6411 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006412 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006413 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 break;
6415
6416 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006417 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006418 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006419 break;
6420
6421 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006422 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006423 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006424 break;
6425
6426 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006427 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006428 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006429 break;
6430
6431 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006432 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006433 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006434 break;
6435
6436 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006437 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006438 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006439 break;
6440
6441 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006442 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006443 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006444 break;
6445
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006446 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006447 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006448 ADD_STATE_IF_MATCH(t->state);
6449 break;
6450
6451 case NFA_NLOWER_IC: /* [^a-z] */
6452 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006453 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006454 ADD_STATE_IF_MATCH(t->state);
6455 break;
6456
6457 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006458 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006459 ADD_STATE_IF_MATCH(t->state);
6460 break;
6461
6462 case NFA_NUPPER_IC: /* ^[A-Z] */
6463 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006464 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006465 ADD_STATE_IF_MATCH(t->state);
6466 break;
6467
Bram Moolenaar5714b802013-05-28 22:03:20 +02006468 case NFA_BACKREF1:
6469 case NFA_BACKREF2:
6470 case NFA_BACKREF3:
6471 case NFA_BACKREF4:
6472 case NFA_BACKREF5:
6473 case NFA_BACKREF6:
6474 case NFA_BACKREF7:
6475 case NFA_BACKREF8:
6476 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006477#ifdef FEAT_SYN_HL
6478 case NFA_ZREF1:
6479 case NFA_ZREF2:
6480 case NFA_ZREF3:
6481 case NFA_ZREF4:
6482 case NFA_ZREF5:
6483 case NFA_ZREF6:
6484 case NFA_ZREF7:
6485 case NFA_ZREF8:
6486 case NFA_ZREF9:
6487#endif
6488 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006489 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006490 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006491 int bytelen;
6492
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006493 if (t->state->c <= NFA_BACKREF9)
6494 {
6495 subidx = t->state->c - NFA_BACKREF1 + 1;
6496 result = match_backref(&t->subs.norm, subidx, &bytelen);
6497 }
6498#ifdef FEAT_SYN_HL
6499 else
6500 {
6501 subidx = t->state->c - NFA_ZREF1 + 1;
6502 result = match_zref(subidx, &bytelen);
6503 }
6504#endif
6505
Bram Moolenaar5714b802013-05-28 22:03:20 +02006506 if (result)
6507 {
6508 if (bytelen == 0)
6509 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006510 /* empty match always works, output of NFA_SKIP to be
6511 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006512 add_here = TRUE;
6513 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006514 }
6515 else if (bytelen <= clen)
6516 {
6517 /* match current character, jump ahead to out of
6518 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006519 add_state = t->state->out->out;
6520 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006521 }
6522 else
6523 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006524 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006525 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006526 add_state = t->state->out;
6527 add_off = bytelen;
6528 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006529 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006530 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006531 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006532 }
6533 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006534 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006535 if (t->count - clen <= 0)
6536 {
6537 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006538 add_state = t->state->out;
6539 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006540 }
6541 else
6542 {
6543 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006544 add_state = t->state;
6545 add_off = 0;
6546 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006547 }
6548 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006549
Bram Moolenaar423532e2013-05-29 21:14:42 +02006550 case NFA_LNUM:
6551 case NFA_LNUM_GT:
6552 case NFA_LNUM_LT:
6553 result = (REG_MULTI &&
6554 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006555 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006556 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006557 {
6558 add_here = TRUE;
6559 add_state = t->state->out;
6560 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006561 break;
6562
6563 case NFA_COL:
6564 case NFA_COL_GT:
6565 case NFA_COL_LT:
6566 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6567 (long_u)(reginput - regline) + 1);
6568 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006569 {
6570 add_here = TRUE;
6571 add_state = t->state->out;
6572 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006573 break;
6574
6575 case NFA_VCOL:
6576 case NFA_VCOL_GT:
6577 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006578 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006579 int op = t->state->c - NFA_VCOL;
6580 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006581 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006582
6583 /* Bail out quickly when there can't be a match, avoid the
6584 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006585 if (op != 1 && col > t->state->val
6586#ifdef FEAT_MBYTE
6587 * (has_mbyte ? MB_MAXBYTES : 1)
6588#endif
6589 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006590 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006591 result = FALSE;
6592 if (op == 1 && col - 1 > t->state->val && col > 100)
6593 {
6594 int ts = wp->w_buffer->b_p_ts;
6595
6596 /* Guess that a character won't use more columns than
6597 * 'tabstop', with a minimum of 4. */
6598 if (ts < 4)
6599 ts = 4;
6600 result = col > t->state->val * ts;
6601 }
6602 if (!result)
6603 result = nfa_re_num_cmp(t->state->val, op,
6604 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006605 if (result)
6606 {
6607 add_here = TRUE;
6608 add_state = t->state->out;
6609 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006610 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006611 break;
6612
Bram Moolenaar044aa292013-06-04 21:27:38 +02006613 case NFA_MARK:
6614 case NFA_MARK_GT:
6615 case NFA_MARK_LT:
6616 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006617 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006618
6619 /* Compare the mark position to the match position. */
6620 result = (pos != NULL /* mark doesn't exist */
6621 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006622 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006623 ? (pos->col == (colnr_T)(reginput - regline)
6624 ? t->state->c == NFA_MARK
6625 : (pos->col < (colnr_T)(reginput - regline)
6626 ? t->state->c == NFA_MARK_GT
6627 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006628 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006629 ? t->state->c == NFA_MARK_GT
6630 : t->state->c == NFA_MARK_LT)));
6631 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006632 {
6633 add_here = TRUE;
6634 add_state = t->state->out;
6635 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006636 break;
6637 }
6638
Bram Moolenaar423532e2013-05-29 21:14:42 +02006639 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006640 result = (rex.reg_win != NULL
6641 && (reglnum + rex.reg_firstlnum
6642 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006643 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006644 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006645 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006646 {
6647 add_here = TRUE;
6648 add_state = t->state->out;
6649 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006650 break;
6651
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006652 case NFA_VISUAL:
6653 result = reg_match_visual();
6654 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006655 {
6656 add_here = TRUE;
6657 add_state = t->state->out;
6658 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006659 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006660
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006661 case NFA_MOPEN1:
6662 case NFA_MOPEN2:
6663 case NFA_MOPEN3:
6664 case NFA_MOPEN4:
6665 case NFA_MOPEN5:
6666 case NFA_MOPEN6:
6667 case NFA_MOPEN7:
6668 case NFA_MOPEN8:
6669 case NFA_MOPEN9:
6670#ifdef FEAT_SYN_HL
6671 case NFA_ZOPEN:
6672 case NFA_ZOPEN1:
6673 case NFA_ZOPEN2:
6674 case NFA_ZOPEN3:
6675 case NFA_ZOPEN4:
6676 case NFA_ZOPEN5:
6677 case NFA_ZOPEN6:
6678 case NFA_ZOPEN7:
6679 case NFA_ZOPEN8:
6680 case NFA_ZOPEN9:
6681#endif
6682 case NFA_NOPEN:
6683 case NFA_ZSTART:
6684 /* These states are only added to be able to bail out when
6685 * they are added again, nothing is to be done. */
6686 break;
6687
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006688 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006689 {
6690 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006691
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006692#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006693 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006694 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006695#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006696 result = (c == curc);
6697
Bram Moolenaar6100d022016-10-02 16:51:57 +02006698 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006699 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006700#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006701 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006702 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006703 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006704 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006705#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006706 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006707 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006708 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006709
6710 } /* switch (t->state->c) */
6711
6712 if (add_state != NULL)
6713 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006714 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006715 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006716
6717 if (t->pim.result == NFA_PIM_UNUSED)
6718 pim = NULL;
6719 else
6720 pim = &t->pim;
6721
6722 /* Handle the postponed invisible match if the match might end
6723 * without advancing and before the end of the line. */
6724 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006725 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006726 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006727 {
6728#ifdef ENABLE_LOG
6729 fprintf(log_fd, "\n");
6730 fprintf(log_fd, "==================================\n");
6731 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6732 fprintf(log_fd, "\n");
6733#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006734 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006735 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006736 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006737 /* for \@! and \@<! it is a match when the result is
6738 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006739 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006740 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6741 || pim->state->c
6742 == NFA_START_INVISIBLE_BEFORE_NEG
6743 || pim->state->c
6744 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006745 {
6746 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006747 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006748#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006749 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006750 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006751#endif
6752 }
6753 }
6754 else
6755 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006756 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757#ifdef ENABLE_LOG
6758 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006759 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006760 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6761 fprintf(log_fd, "\n");
6762#endif
6763 }
6764
Bram Moolenaardecd9542013-06-07 16:31:50 +02006765 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006766 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006767 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6768 || pim->state->c
6769 == NFA_START_INVISIBLE_BEFORE_NEG
6770 || pim->state->c
6771 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772 {
6773 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006774 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006775#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006776 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006777 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006778#endif
6779 }
6780 else
6781 /* look-behind match failed, don't add the state */
6782 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006783
6784 /* Postponed invisible match was handled, don't add it to
6785 * following states. */
6786 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006787 }
6788
Bram Moolenaara951e352013-10-06 15:46:11 +02006789 /* If "pim" points into l->t it will become invalid when
6790 * adding the state causes the list to be reallocated. Make a
6791 * local copy to avoid that. */
6792 if (pim == &t->pim)
6793 {
6794 copy_pim(&pim_copy, pim);
6795 pim = &pim_copy;
6796 }
6797
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006798 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006799 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006800 else
6801 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006802 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006803 if (add_count > 0)
6804 nextlist->t[nextlist->n - 1].count = add_count;
6805 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006806 }
6807
6808 } /* for (thislist = thislist; thislist->state; thislist++) */
6809
Bram Moolenaare23febd2013-05-26 18:40:14 +02006810 /* Look for the start of a match in the current position by adding the
6811 * start state to the list of states.
6812 * The first found match is the leftmost one, thus the order of states
6813 * matters!
6814 * Do not add the start state in recursive calls of nfa_regmatch(),
6815 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006816 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006817 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006818 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006819 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006820 && reglnum == 0
6821 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006822 && (rex.reg_maxcol == 0
6823 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006824 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006825 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006826 ? (reglnum < nfa_endp->se_u.pos.lnum
6827 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006828 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006829 < nfa_endp->se_u.pos.col))
6830 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006831 {
6832#ifdef ENABLE_LOG
6833 fprintf(log_fd, "(---) STARTSTATE\n");
6834#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006835 /* Inline optimized code for addstate() if we know the state is
6836 * the first MOPEN. */
6837 if (toplevel)
6838 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006839 int add = TRUE;
6840 int c;
6841
6842 if (prog->regstart != NUL && clen != 0)
6843 {
6844 if (nextlist->n == 0)
6845 {
6846 colnr_T col = (colnr_T)(reginput - regline) + clen;
6847
6848 /* Nextlist is empty, we can skip ahead to the
6849 * character that must appear at the start. */
6850 if (skip_to_start(prog->regstart, &col) == FAIL)
6851 break;
6852#ifdef ENABLE_LOG
6853 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6854 col - ((colnr_T)(reginput - regline) + clen));
6855#endif
6856 reginput = regline + col - clen;
6857 }
6858 else
6859 {
6860 /* Checking if the required start character matches is
6861 * cheaper than adding a state that won't match. */
6862 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006863 if (c != prog->regstart && (!rex.reg_ic
6864 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006865 {
6866#ifdef ENABLE_LOG
6867 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6868#endif
6869 add = FALSE;
6870 }
6871 }
6872 }
6873
6874 if (add)
6875 {
6876 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006877 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006878 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006879 else
6880 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006881 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006882 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006883 }
6884 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006885 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006886 }
6887
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006888#ifdef ENABLE_LOG
6889 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006890 {
6891 int i;
6892
6893 for (i = 0; i < thislist->n; i++)
6894 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6895 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006896 fprintf(log_fd, "\n");
6897#endif
6898
6899nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006900 /* Advance to the next character, or advance to the next line, or
6901 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006902 if (clen != 0)
6903 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006904 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6905 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006906 reg_nextline();
6907 else
6908 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006909
6910 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006911 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006912 if (got_int)
6913 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006914#ifdef FEAT_RELTIME
6915 /* Check for timeout once in a twenty times to avoid overhead. */
6916 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6917 {
6918 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006919 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006920 break;
6921 }
6922#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006923 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006924
6925#ifdef ENABLE_LOG
6926 if (log_fd != stderr)
6927 fclose(log_fd);
6928 log_fd = NULL;
6929#endif
6930
6931theend:
6932 /* Free memory */
6933 vim_free(list[0].t);
6934 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006935 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006936#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006937#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006938 fclose(debug);
6939#endif
6940
Bram Moolenaar963fee22013-05-26 21:47:28 +02006941 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006942}
6943
6944/*
6945 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006946 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006947 */
6948 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006949nfa_regtry(
6950 nfa_regprog_T *prog,
6951 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006952 proftime_T *tm UNUSED, /* timeout limit or NULL */
6953 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006954{
6955 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006956 regsubs_T subs, m;
6957 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006958 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006959#ifdef ENABLE_LOG
6960 FILE *f;
6961#endif
6962
6963 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006964#ifdef FEAT_RELTIME
6965 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006966 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006967 nfa_time_count = 0;
6968#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969
6970#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006971 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972 if (f != NULL)
6973 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006974 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006975#ifdef DEBUG
6976 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6977#endif
6978 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006979 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006980 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006981 fprintf(f, "\n\n");
6982 fclose(f);
6983 }
6984 else
Bram Moolenaar3c867da2018-06-23 14:34:28 +02006985 EMSG("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006986#endif
6987
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006988 clear_sub(&subs.norm);
6989 clear_sub(&m.norm);
6990#ifdef FEAT_SYN_HL
6991 clear_sub(&subs.synt);
6992 clear_sub(&m.synt);
6993#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006994
Bram Moolenaarfda37292014-11-05 14:27:36 +01006995 result = nfa_regmatch(prog, start, &subs, &m);
6996 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006998 else if (result == NFA_TOO_EXPENSIVE)
6999 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000
7001 cleanup_subexpr();
7002 if (REG_MULTI)
7003 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007004 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007005 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007006 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7007 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007008
Bram Moolenaar6100d022016-10-02 16:51:57 +02007009 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7010 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007011 }
7012
Bram Moolenaar6100d022016-10-02 16:51:57 +02007013 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007015 rex.reg_startpos[0].lnum = 0;
7016 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007018 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007019 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007020 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007021 rex.reg_endpos[0].lnum = reglnum;
7022 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007023 }
7024 else
7025 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007026 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007027 }
7028 else
7029 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007030 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007031 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007032 rex.reg_startp[i] = subs.norm.list.line[i].start;
7033 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007034 }
7035
Bram Moolenaar6100d022016-10-02 16:51:57 +02007036 if (rex.reg_startp[0] == NULL)
7037 rex.reg_startp[0] = regline + col;
7038 if (rex.reg_endp[0] == NULL)
7039 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007040 }
7041
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007042#ifdef FEAT_SYN_HL
7043 /* Package any found \z(...\) matches for export. Default is none. */
7044 unref_extmatch(re_extmatch_out);
7045 re_extmatch_out = NULL;
7046
7047 if (prog->reghasz == REX_SET)
7048 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007049 cleanup_zsubexpr();
7050 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007051 /* Loop over \z1, \z2, etc. There is no \z0. */
7052 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007053 {
7054 if (REG_MULTI)
7055 {
7056 struct multipos *mpos = &subs.synt.list.multi[i];
7057
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007058 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007059 if (mpos->start_lnum >= 0
7060 && mpos->start_lnum == mpos->end_lnum
7061 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007062 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007063 vim_strnsave(reg_getline(mpos->start_lnum)
7064 + mpos->start_col,
7065 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007066 }
7067 else
7068 {
7069 struct linepos *lpos = &subs.synt.list.line[i];
7070
7071 if (lpos->start != NULL && lpos->end != NULL)
7072 re_extmatch_out->matches[i] =
7073 vim_strnsave(lpos->start,
7074 (int)(lpos->end - lpos->start));
7075 }
7076 }
7077 }
7078#endif
7079
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007080 return 1 + reglnum;
7081}
7082
7083/*
7084 * Match a regexp against a string ("line" points to the string) or multiple
7085 * lines ("line" is NULL, use reg_getline()).
7086 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007087 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007088 */
7089 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007090nfa_regexec_both(
7091 char_u *line,
7092 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007093 proftime_T *tm, /* timeout limit or NULL */
7094 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007095{
7096 nfa_regprog_T *prog;
7097 long retval = 0L;
7098 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007099 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007100
7101 if (REG_MULTI)
7102 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007103 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007104 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007105 rex.reg_startpos = rex.reg_mmatch->startpos;
7106 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007107 }
7108 else
7109 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007110 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7111 rex.reg_startp = rex.reg_match->startp;
7112 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113 }
7114
7115 /* Be paranoid... */
7116 if (prog == NULL || line == NULL)
7117 {
7118 EMSG(_(e_null));
7119 goto theend;
7120 }
7121
Bram Moolenaar6100d022016-10-02 16:51:57 +02007122 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007123 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007124 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007125 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007126 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127
7128#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007129 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007130 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007131 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132#endif
7133
7134 regline = line;
7135 reglnum = 0; /* relative to line */
7136
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007137 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007138 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007139 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007140 nfa_listid = 1;
7141 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007142 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007143
Bram Moolenaard89616e2013-06-06 18:46:06 +02007144 if (prog->reganch && col > 0)
7145 return 0L;
7146
Bram Moolenaar473de612013-06-08 18:19:48 +02007147 need_clear_subexpr = TRUE;
7148#ifdef FEAT_SYN_HL
7149 /* Clear the external match subpointers if necessary. */
7150 if (prog->reghasz == REX_SET)
7151 {
7152 nfa_has_zsubexpr = TRUE;
7153 need_clear_zsubexpr = TRUE;
7154 }
7155 else
7156 nfa_has_zsubexpr = FALSE;
7157#endif
7158
Bram Moolenaard89616e2013-06-06 18:46:06 +02007159 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007160 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007161 /* Skip ahead until a character we know the match must start with.
7162 * When there is none there is no match. */
7163 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007164 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007165
Bram Moolenaar473de612013-06-08 18:19:48 +02007166 /* If match_text is set it contains the full text that must match.
7167 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007168 if (prog->match_text != NULL
7169#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007170 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007171#endif
7172 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007173 return find_match_text(col, prog->regstart, prog->match_text);
7174 }
7175
Bram Moolenaard89616e2013-06-06 18:46:06 +02007176 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007177 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007178 goto theend;
7179
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007180 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007181 for (i = 0; i < nstate; ++i)
7182 {
7183 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007184 prog->state[i].lastlist[0] = 0;
7185 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007186 }
7187
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007188 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007189
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007190 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007191
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007192theend:
7193 return retval;
7194}
7195
7196/*
7197 * Compile a regular expression into internal code for the NFA matcher.
7198 * Returns the program in allocated space. Returns NULL for an error.
7199 */
7200 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007201nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007202{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007203 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007204 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007205 int *postfix;
7206
7207 if (expr == NULL)
7208 return NULL;
7209
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007210 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007211 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007212
7213 init_class_tab();
7214
7215 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7216 return NULL;
7217
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007218 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007219 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007220 postfix = re2post();
7221 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007222 {
7223 /* TODO: only give this error for debugging? */
7224 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007225 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007226 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007227 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228
7229 /*
7230 * In order to build the NFA, we parse the input regexp twice:
7231 * 1. first pass to count size (so we can allocate space)
7232 * 2. second to emit code
7233 */
7234#ifdef ENABLE_LOG
7235 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007236 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007237
7238 if (f != NULL)
7239 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007240 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007241 fclose(f);
7242 }
7243 }
7244#endif
7245
7246 /*
7247 * PASS 1
7248 * Count number of NFA states in "nstate". Do not build the NFA.
7249 */
7250 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007251
Bram Moolenaar16619a22013-06-11 18:42:36 +02007252 /* allocate the regprog with space for the compiled regexp */
7253 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007254 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7255 if (prog == NULL)
7256 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007257 state_ptr = prog->state;
7258
7259 /*
7260 * PASS 2
7261 * Build the NFA
7262 */
7263 prog->start = post2nfa(postfix, post_ptr, FALSE);
7264 if (prog->start == NULL)
7265 goto fail;
7266
7267 prog->regflags = regflags;
7268 prog->engine = &nfa_regengine;
7269 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007270 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007271 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007272 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007273
Bram Moolenaara2947e22013-06-11 22:44:09 +02007274 nfa_postprocess(prog);
7275
Bram Moolenaard89616e2013-06-06 18:46:06 +02007276 prog->reganch = nfa_get_reganch(prog->start, 0);
7277 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007278 prog->match_text = nfa_get_match_text(prog->start);
7279
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007280#ifdef ENABLE_LOG
7281 nfa_postfix_dump(expr, OK);
7282 nfa_dump(prog);
7283#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007284#ifdef FEAT_SYN_HL
7285 /* Remember whether this pattern has any \z specials in it. */
7286 prog->reghasz = re_has_z;
7287#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007288 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007289 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007290
7291out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007292 VIM_CLEAR(post_start);
7293 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007294 state_ptr = NULL;
7295 return (regprog_T *)prog;
7296
7297fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007298 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007299#ifdef ENABLE_LOG
7300 nfa_postfix_dump(expr, FAIL);
7301#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007302 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007303 goto out;
7304}
7305
Bram Moolenaar473de612013-06-08 18:19:48 +02007306/*
7307 * Free a compiled regexp program, returned by nfa_regcomp().
7308 */
7309 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007310nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007311{
7312 if (prog != NULL)
7313 {
7314 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007315 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007316 vim_free(prog);
7317 }
7318}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007319
7320/*
7321 * Match a regexp against a string.
7322 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7323 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007324 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007325 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007326 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007327 */
7328 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007329nfa_regexec_nl(
7330 regmatch_T *rmp,
7331 char_u *line, /* string to match against */
7332 colnr_T col, /* column to start looking for match */
7333 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007334{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007335 rex.reg_match = rmp;
7336 rex.reg_mmatch = NULL;
7337 rex.reg_maxline = 0;
7338 rex.reg_line_lbr = line_lbr;
7339 rex.reg_buf = curbuf;
7340 rex.reg_win = NULL;
7341 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007342#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007343 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007344#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007345 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007346 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007347}
7348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007349
7350/*
7351 * Match a regexp against multiple lines.
7352 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7353 * Uses curbuf for line count and 'iskeyword'.
7354 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007355 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007356 * match otherwise.
7357 *
7358 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7359 *
7360 * ! Also NOTE : match may actually be in another line. e.g.:
7361 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7362 *
7363 * +-------------------------+
7364 * |a |
7365 * |b |
7366 * |c |
7367 * | |
7368 * +-------------------------+
7369 *
7370 * then nfa_regexec_multi() returns 3. while the original
7371 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7372 *
7373 * FIXME if this behavior is not compatible.
7374 */
7375 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007376nfa_regexec_multi(
7377 regmmatch_T *rmp,
7378 win_T *win, /* window in which to search or NULL */
7379 buf_T *buf, /* buffer in which to search */
7380 linenr_T lnum, /* nr of line to start looking for match */
7381 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007382 proftime_T *tm, /* timeout limit or NULL */
7383 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007384{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007385 rex.reg_match = NULL;
7386 rex.reg_mmatch = rmp;
7387 rex.reg_buf = buf;
7388 rex.reg_win = win;
7389 rex.reg_firstlnum = lnum;
7390 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7391 rex.reg_line_lbr = FALSE;
7392 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007393#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007394 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007395#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007396 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007397
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007398 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007399}
7400
7401#ifdef DEBUG
7402# undef ENABLE_LOG
7403#endif