blob: 5e4fadd028bdcae30bc109a5c6d9f0315e291183 [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!
Bram Moolenaar52797ba2021-12-16 14:45:13 +000021 * To disable all of this when compiling Vim for debugging, undefine DEBUG in
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020022 * 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 Moolenaar63d9e732019-12-05 21:10:38 +010032// Added to NFA_ANY - NFA_NUPPER_IC to include a NL.
Bram Moolenaar1cfad522013-08-14 12:06:49 +020033#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010039 NFA_EMPTY, // matches 0-length
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar63d9e732019-12-05 21:10:38 +010041 NFA_START_COLL, // [abc] start
42 NFA_END_COLL, // [abc] end
43 NFA_START_NEG_COLL, // [^abc] start
44 NFA_END_NEG_COLL, // [^abc] end (postfix only)
45 NFA_RANGE, // range of the two previous items
46 // (postfix only)
47 NFA_RANGE_MIN, // low end of a range
48 NFA_RANGE_MAX, // high end of a range
Bram Moolenaar417bad22013-06-07 14:08:30 +020049
Bram Moolenaar63d9e732019-12-05 21:10:38 +010050 NFA_CONCAT, // concatenate two previous items (postfix
51 // only)
52 NFA_OR, // \| (postfix only)
53 NFA_STAR, // greedy * (postfix only)
54 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
Bram Moolenaar63d9e732019-12-05 21:10:38 +010058 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
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020064 NFA_NEWL,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010065 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 \%( ... \)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020069 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 Moolenaar63d9e732019-12-05 21:10:38 +010081 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
84 NFA_ANY_COMPOSING, // \%C: Any composing characters.
85 NFA_OPT_CHARS, // \%[abc]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
Bram Moolenaar63d9e732019-12-05 21:10:38 +010087 // 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 \@>
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020093
Bram Moolenaar63d9e732019-12-05 21:10:38 +010094 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
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100104 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
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200113#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100114 NFA_SKIP, // Skip characters
Bram Moolenaar5714b802013-05-28 22:03:20 +0200115
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
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100162 // NFA_FIRST_NL
163 NFA_ANY, // Match any one character.
164 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
190 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]
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200194
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
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100198 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
208 NFA_MARK, // Match mark
209 NFA_MARK_GT, // Match > mark
210 NFA_MARK_LT, // Match < mark
211 NFA_VISUAL, // Match Visual area
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100213 // Character classes [:alnum:] etc
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
Bram Moolenaar221cd9f2019-01-31 15:34:40 +0100229 NFA_CLASS_ESCAPE,
230 NFA_CLASS_IDENT,
231 NFA_CLASS_KEYWORD,
232 NFA_CLASS_FNAME
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233};
234
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100235// Keep in sync with classchars.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236static int nfa_classcodes[] = {
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
241 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
242 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
243 NFA_UPPER, NFA_NUPPER
244};
245
Bram Moolenaar0270f382018-07-17 05:43:58 +0200246// Variables only used in nfa_regcomp() and descendants.
247static int nfa_re_flags; // re_flags passed to nfa_regcomp()
248static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249static int *post_end;
250static int *post_ptr;
Bram Moolenaar66c50c52021-01-02 17:43:49 +0100251
252// Set when the pattern should use the NFA engine.
253// E.g. [[:upper:]] only allows 8bit characters for BT engine,
254// while NFA engine handles multibyte characters correctly.
255static int wants_nfa;
256
Bram Moolenaar0270f382018-07-17 05:43:58 +0200257static int nstate; // Number of states in the NFA.
258static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100260// If not NULL match must end at this position
Bram Moolenaar307aa162013-06-02 16:34:21 +0200261static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100263// 0 for first call to nfa_regmatch(), 1 for recursive call.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200264static int nfa_ll_index = 0;
265
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100266static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100267static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100269static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200270#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100271static int match_follows(nfa_state_T *startstate, int depth);
272static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100274// helper functions used when doing re2post() ... regatom() parsing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200276 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200277 return FAIL; \
278 *post_ptr++ = c; \
279 } while (0)
280
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200281/*
282 * Initialize internal variables before NFA compilation.
283 * Return OK on success, FAIL otherwise.
284 */
285 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100286nfa_regcomp_start(
287 char_u *expr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100288 int re_flags) // see vim_regcomp()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200290 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200291 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200292
293 nstate = 0;
294 istate = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100295 // A reasonable estimation for maximum size
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200296 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100298 // Some items blow up in size, such as [A-z]. Add more space for that.
299 // When it is still not enough realloc_post_list() will be used.
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200300 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200301
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100302 // Size for postfix representation of expr.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200303 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200304
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200305 post_start = alloc(postfix_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 if (post_start == NULL)
307 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200308 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200309 post_end = post_start + nstate_max;
Bram Moolenaar66c50c52021-01-02 17:43:49 +0100310 wants_nfa = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200311 rex.nfa_has_zend = FALSE;
312 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100314 // shared with BT engine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315 regcomp_start(expr, re_flags);
316
317 return OK;
318}
319
320/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200321 * Figure out if the NFA state list starts with an anchor, must match at start
322 * of the line.
323 */
324 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200326{
327 nfa_state_T *p = start;
328
329 if (depth > 4)
330 return 0;
331
332 while (p != NULL)
333 {
334 switch (p->c)
335 {
336 case NFA_BOL:
337 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100338 return 1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200339
340 case NFA_ZSTART:
341 case NFA_ZEND:
342 case NFA_CURSOR:
343 case NFA_VISUAL:
344
345 case NFA_MOPEN:
346 case NFA_MOPEN1:
347 case NFA_MOPEN2:
348 case NFA_MOPEN3:
349 case NFA_MOPEN4:
350 case NFA_MOPEN5:
351 case NFA_MOPEN6:
352 case NFA_MOPEN7:
353 case NFA_MOPEN8:
354 case NFA_MOPEN9:
355 case NFA_NOPEN:
356#ifdef FEAT_SYN_HL
357 case NFA_ZOPEN:
358 case NFA_ZOPEN1:
359 case NFA_ZOPEN2:
360 case NFA_ZOPEN3:
361 case NFA_ZOPEN4:
362 case NFA_ZOPEN5:
363 case NFA_ZOPEN6:
364 case NFA_ZOPEN7:
365 case NFA_ZOPEN8:
366 case NFA_ZOPEN9:
367#endif
368 p = p->out;
369 break;
370
371 case NFA_SPLIT:
372 return nfa_get_reganch(p->out, depth + 1)
373 && nfa_get_reganch(p->out1, depth + 1);
374
375 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100376 return 0; // noooo
Bram Moolenaard89616e2013-06-06 18:46:06 +0200377 }
378 }
379 return 0;
380}
381
382/*
383 * Figure out if the NFA state list starts with a character which must match
384 * at start of the match.
385 */
386 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100387nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200388{
389 nfa_state_T *p = start;
390
391 if (depth > 4)
392 return 0;
393
394 while (p != NULL)
395 {
396 switch (p->c)
397 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100398 // all kinds of zero-width matches
Bram Moolenaard89616e2013-06-06 18:46:06 +0200399 case NFA_BOL:
400 case NFA_BOF:
401 case NFA_BOW:
402 case NFA_EOW:
403 case NFA_ZSTART:
404 case NFA_ZEND:
405 case NFA_CURSOR:
406 case NFA_VISUAL:
407 case NFA_LNUM:
408 case NFA_LNUM_GT:
409 case NFA_LNUM_LT:
410 case NFA_COL:
411 case NFA_COL_GT:
412 case NFA_COL_LT:
413 case NFA_VCOL:
414 case NFA_VCOL_GT:
415 case NFA_VCOL_LT:
416 case NFA_MARK:
417 case NFA_MARK_GT:
418 case NFA_MARK_LT:
419
420 case NFA_MOPEN:
421 case NFA_MOPEN1:
422 case NFA_MOPEN2:
423 case NFA_MOPEN3:
424 case NFA_MOPEN4:
425 case NFA_MOPEN5:
426 case NFA_MOPEN6:
427 case NFA_MOPEN7:
428 case NFA_MOPEN8:
429 case NFA_MOPEN9:
430 case NFA_NOPEN:
431#ifdef FEAT_SYN_HL
432 case NFA_ZOPEN:
433 case NFA_ZOPEN1:
434 case NFA_ZOPEN2:
435 case NFA_ZOPEN3:
436 case NFA_ZOPEN4:
437 case NFA_ZOPEN5:
438 case NFA_ZOPEN6:
439 case NFA_ZOPEN7:
440 case NFA_ZOPEN8:
441 case NFA_ZOPEN9:
442#endif
443 p = p->out;
444 break;
445
446 case NFA_SPLIT:
447 {
448 int c1 = nfa_get_regstart(p->out, depth + 1);
449 int c2 = nfa_get_regstart(p->out1, depth + 1);
450
451 if (c1 == c2)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100452 return c1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200453 return 0;
454 }
455
456 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200457 if (p->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100458 return p->c; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200459 return 0;
460 }
461 }
462 return 0;
463}
464
465/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200466 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200467 * else. If so return a string in allocated memory with what must match after
468 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200469 */
470 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100471nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200472{
473 nfa_state_T *p = start;
474 int len = 0;
475 char_u *ret;
476 char_u *s;
477
478 if (p->c != NFA_MOPEN)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100479 return NULL; // just in case
Bram Moolenaar473de612013-06-08 18:19:48 +0200480 p = p->out;
481 while (p->c > 0)
482 {
483 len += MB_CHAR2LEN(p->c);
484 p = p->out;
485 }
486 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
487 return NULL;
488
489 ret = alloc(len);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000490 if (ret == NULL)
491 return NULL;
492
493 p = start->out->out; // skip first char, it goes into regstart
494 s = ret;
495 while (p->c > 0)
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000497 if (has_mbyte)
498 s += (*mb_char2bytes)(p->c, s);
499 else
500 *s++ = p->c;
501 p = p->out;
Bram Moolenaar473de612013-06-08 18:19:48 +0200502 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +0000503 *s = NUL;
Bram Moolenaar473de612013-06-08 18:19:48 +0200504 return ret;
505}
506
507/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200508 * Allocate more space for post_start. Called when
509 * running above the estimated number of states.
510 */
511 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100512realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200513{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200514 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100515 int new_max;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200516 int *new_start;
517 int *old_start;
518
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100519 // For weird patterns the number of states can be very high. Increasing by
520 // 50% seems a reasonable compromise between memory use and speed.
521 new_max = nstate_max * 3 / 2;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200522 new_start = ALLOC_MULT(int, new_max);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200523 if (new_start == NULL)
524 return FAIL;
525 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200526 old_start = post_start;
527 post_start = new_start;
528 post_ptr = new_start + (post_ptr - old_start);
529 post_end = post_start + new_max;
530 vim_free(old_start);
531 return OK;
532}
533
534/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200535 * Search between "start" and "end" and try to recognize a
536 * character class in expanded form. For example [0-9].
537 * On success, return the id the character class to be emitted.
538 * On failure, return 0 (=FAIL)
539 * Start points to the first char of the range, while end should point
540 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200541 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
542 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200543 */
544 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100545nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200546{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200547# define CLASS_not 0x80
548# define CLASS_af 0x40
549# define CLASS_AF 0x20
550# define CLASS_az 0x10
551# define CLASS_AZ 0x08
552# define CLASS_o7 0x04
553# define CLASS_o9 0x02
554# define CLASS_underscore 0x01
555
556 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200557 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200558 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200559
560 if (extra_newl == TRUE)
561 newl = TRUE;
562
563 if (*end != ']')
564 return FAIL;
565 p = start;
566 if (*p == '^')
567 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200568 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200569 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200570 }
571
572 while (p < end)
573 {
574 if (p + 2 < end && *(p + 1) == '-')
575 {
576 switch (*p)
577 {
578 case '0':
579 if (*(p + 2) == '9')
580 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200581 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 break;
583 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200584 if (*(p + 2) == '7')
585 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200586 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200587 break;
588 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200589 return FAIL;
590
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200591 case 'a':
592 if (*(p + 2) == 'z')
593 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200594 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200595 break;
596 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 if (*(p + 2) == 'f')
598 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200599 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200600 break;
601 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200602 return FAIL;
603
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 case 'A':
605 if (*(p + 2) == 'Z')
606 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200607 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200608 break;
609 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 if (*(p + 2) == 'F')
611 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200612 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200613 break;
614 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200615 return FAIL;
616
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 default:
618 return FAIL;
619 }
620 p += 3;
621 }
622 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
623 {
624 newl = TRUE;
625 p += 2;
626 }
627 else if (*p == '_')
628 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200629 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200630 p ++;
631 }
632 else if (*p == '\n')
633 {
634 newl = TRUE;
635 p ++;
636 }
637 else
638 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100639 } // while (p < end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200640
641 if (p != end)
642 return FAIL;
643
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200645 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646
647 switch (config)
648 {
649 case CLASS_o9:
650 return extra_newl + NFA_DIGIT;
651 case CLASS_not | CLASS_o9:
652 return extra_newl + NFA_NDIGIT;
653 case CLASS_af | CLASS_AF | CLASS_o9:
654 return extra_newl + NFA_HEX;
655 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
656 return extra_newl + NFA_NHEX;
657 case CLASS_o7:
658 return extra_newl + NFA_OCTAL;
659 case CLASS_not | CLASS_o7:
660 return extra_newl + NFA_NOCTAL;
661 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
662 return extra_newl + NFA_WORD;
663 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
664 return extra_newl + NFA_NWORD;
665 case CLASS_az | CLASS_AZ | CLASS_underscore:
666 return extra_newl + NFA_HEAD;
667 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
668 return extra_newl + NFA_NHEAD;
669 case CLASS_az | CLASS_AZ:
670 return extra_newl + NFA_ALPHA;
671 case CLASS_not | CLASS_az | CLASS_AZ:
672 return extra_newl + NFA_NALPHA;
673 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200674 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200675 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200676 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200677 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200678 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200679 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200680 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200681 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200682 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200683}
684
685/*
686 * Produce the bytes for equivalence class "c".
687 * Currently only handles latin1, latin9 and utf-8.
688 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
689 * equivalent to 'a OR b OR c'
690 *
691 * NOTE! When changing this function, also update reg_equi_class()
692 */
693 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100694nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200696#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200697
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
699 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200700 {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000701#define A_grave 0xc0
702#define A_acute 0xc1
703#define A_circumflex 0xc2
704#define A_virguilla 0xc3
705#define A_diaeresis 0xc4
706#define A_ring 0xc5
707#define C_cedilla 0xc7
708#define E_grave 0xc8
709#define E_acute 0xc9
710#define E_circumflex 0xca
711#define E_diaeresis 0xcb
712#define I_grave 0xcc
713#define I_acute 0xcd
714#define I_circumflex 0xce
715#define I_diaeresis 0xcf
716#define N_virguilla 0xd1
717#define O_grave 0xd2
718#define O_acute 0xd3
719#define O_circumflex 0xd4
720#define O_virguilla 0xd5
721#define O_diaeresis 0xd6
722#define O_slash 0xd8
723#define U_grave 0xd9
724#define U_acute 0xda
725#define U_circumflex 0xdb
726#define U_diaeresis 0xdc
727#define Y_acute 0xdd
728#define a_grave 0xe0
729#define a_acute 0xe1
730#define a_circumflex 0xe2
731#define a_virguilla 0xe3
732#define a_diaeresis 0xe4
733#define a_ring 0xe5
734#define c_cedilla 0xe7
735#define e_grave 0xe8
736#define e_acute 0xe9
737#define e_circumflex 0xea
738#define e_diaeresis 0xeb
739#define i_grave 0xec
740#define i_acute 0xed
741#define i_circumflex 0xee
742#define i_diaeresis 0xef
743#define n_virguilla 0xf1
744#define o_grave 0xf2
745#define o_acute 0xf3
746#define o_circumflex 0xf4
747#define o_virguilla 0xf5
748#define o_diaeresis 0xf6
749#define o_slash 0xf8
750#define u_grave 0xf9
751#define u_acute 0xfa
752#define u_circumflex 0xfb
753#define u_diaeresis 0xfc
754#define y_acute 0xfd
755#define y_diaeresis 0xff
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200756 switch (c)
757 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200758 case 'A': case A_grave: case A_acute: case A_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200759 case A_virguilla: case A_diaeresis: case A_ring:
760 case 0x100: case 0x102: case 0x104: case 0x1cd:
761 case 0x1de: case 0x1e0: case 0x1fa: case 0x200:
762 case 0x202: case 0x226: case 0x23a: case 0x1e00:
763 case 0x1ea0: case 0x1ea2: case 0x1ea4: case 0x1ea6:
764 case 0x1ea8: case 0x1eaa: case 0x1eac: case 0x1eae:
765 case 0x1eb0: case 0x1eb2: case 0x1eb4: case 0x1eb6:
766 EMIT2('A') EMIT2(A_grave) EMIT2(A_acute)
767 EMIT2(A_circumflex) EMIT2(A_virguilla)
768 EMIT2(A_diaeresis) EMIT2(A_ring)
769 EMIT2(0x100) EMIT2(0x102) EMIT2(0x104)
770 EMIT2(0x1cd) EMIT2(0x1de) EMIT2(0x1e0)
771 EMIT2(0x1fa) EMIT2(0x200) EMIT2(0x202)
772 EMIT2(0x226) EMIT2(0x23a) EMIT2(0x1e00)
773 EMIT2(0x1ea0) EMIT2(0x1ea2) EMIT2(0x1ea4)
774 EMIT2(0x1ea6) EMIT2(0x1ea8) EMIT2(0x1eaa)
775 EMIT2(0x1eac) EMIT2(0x1eae) EMIT2(0x1eb0)
776 EMIT2(0x1eb2) EMIT2(0x1eb6) EMIT2(0x1eb4)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200777 return OK;
778
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200779 case 'B': case 0x181: case 0x243: case 0x1e02:
780 case 0x1e04: case 0x1e06:
781 EMIT2('B')
782 EMIT2(0x181) EMIT2(0x243) EMIT2(0x1e02)
783 EMIT2(0x1e04) EMIT2(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200784 return OK;
785
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200786 case 'C': case C_cedilla: case 0x106: case 0x108:
787 case 0x10a: case 0x10c: case 0x187: case 0x23b:
788 case 0x1e08: case 0xa792:
789 EMIT2('C') EMIT2(C_cedilla)
790 EMIT2(0x106) EMIT2(0x108) EMIT2(0x10a)
791 EMIT2(0x10c) EMIT2(0x187) EMIT2(0x23b)
792 EMIT2(0x1e08) EMIT2(0xa792)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200793 return OK;
794
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200795 case 'D': case 0x10e: case 0x110: case 0x18a:
796 case 0x1e0a: case 0x1e0c: case 0x1e0e: case 0x1e10:
797 case 0x1e12:
798 EMIT2('D') EMIT2(0x10e) EMIT2(0x110) EMIT2(0x18a)
799 EMIT2(0x1e0a) EMIT2(0x1e0c) EMIT2(0x1e0e)
800 EMIT2(0x1e10) EMIT2(0x1e12)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200801 return OK;
802
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200803 case 'E': case E_grave: case E_acute: case E_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200804 case E_diaeresis: case 0x112: case 0x114: case 0x116:
805 case 0x118: case 0x11a: case 0x204: case 0x206:
806 case 0x228: case 0x246: case 0x1e14: case 0x1e16:
807 case 0x1e18: case 0x1e1a: case 0x1e1c: case 0x1eb8:
808 case 0x1eba: case 0x1ebc: case 0x1ebe: case 0x1ec0:
809 case 0x1ec2: case 0x1ec4: case 0x1ec6:
810 EMIT2('E') EMIT2(E_grave) EMIT2(E_acute)
811 EMIT2(E_circumflex) EMIT2(E_diaeresis)
812 EMIT2(0x112) EMIT2(0x114) EMIT2(0x116)
813 EMIT2(0x118) EMIT2(0x11a) EMIT2(0x204)
814 EMIT2(0x206) EMIT2(0x228) EMIT2(0x246)
815 EMIT2(0x1e14) EMIT2(0x1e16) EMIT2(0x1e18)
816 EMIT2(0x1e1a) EMIT2(0x1e1c) EMIT2(0x1eb8)
817 EMIT2(0x1eba) EMIT2(0x1ebc) EMIT2(0x1ebe)
818 EMIT2(0x1ec0) EMIT2(0x1ec2) EMIT2(0x1ec4)
819 EMIT2(0x1ec6)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200820 return OK;
821
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200822 case 'F': case 0x191: case 0x1e1e: case 0xa798:
823 EMIT2('F') EMIT2(0x191) EMIT2(0x1e1e) EMIT2(0xa798)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200824 return OK;
825
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200826 case 'G': case 0x11c: case 0x11e: case 0x120:
827 case 0x122: case 0x193: case 0x1e4: case 0x1e6:
828 case 0x1f4: case 0x1e20: case 0xa7a0:
829 EMIT2('G') EMIT2(0x11c) EMIT2(0x11e) EMIT2(0x120)
830 EMIT2(0x122) EMIT2(0x193) EMIT2(0x1e4)
831 EMIT2(0x1e6) EMIT2(0x1f4) EMIT2(0x1e20)
832 EMIT2(0xa7a0)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200833 return OK;
834
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200835 case 'H': case 0x124: case 0x126: case 0x21e:
836 case 0x1e22: case 0x1e24: case 0x1e26: case 0x1e28:
837 case 0x1e2a: case 0x2c67:
838 EMIT2('H') EMIT2(0x124) EMIT2(0x126) EMIT2(0x21e)
839 EMIT2(0x1e22) EMIT2(0x1e24) EMIT2(0x1e26)
840 EMIT2(0x1e28) EMIT2(0x1e2a) EMIT2(0x2c67)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200841 return OK;
842
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200843 case 'I': case I_grave: case I_acute: case I_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200844 case I_diaeresis: case 0x128: case 0x12a: case 0x12c:
845 case 0x12e: case 0x130: case 0x197: case 0x1cf:
846 case 0x208: case 0x20a: case 0x1e2c: case 0x1e2e:
847 case 0x1ec8: case 0x1eca:
848 EMIT2('I') EMIT2(I_grave) EMIT2(I_acute)
849 EMIT2(I_circumflex) EMIT2(I_diaeresis)
850 EMIT2(0x128) EMIT2(0x12a) EMIT2(0x12c)
851 EMIT2(0x12e) EMIT2(0x130) EMIT2(0x197)
852 EMIT2(0x1cf) EMIT2(0x208) EMIT2(0x20a)
853 EMIT2(0x1e2c) EMIT2(0x1e2e) EMIT2(0x1ec8)
854 EMIT2(0x1eca)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200855 return OK;
856
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200857 case 'J': case 0x134: case 0x248:
858 EMIT2('J') EMIT2(0x134) EMIT2(0x248)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200859 return OK;
860
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200861 case 'K': case 0x136: case 0x198: case 0x1e8: case 0x1e30:
862 case 0x1e32: case 0x1e34: case 0x2c69: case 0xa740:
863 EMIT2('K') EMIT2(0x136) EMIT2(0x198) EMIT2(0x1e8)
864 EMIT2(0x1e30) EMIT2(0x1e32) EMIT2(0x1e34)
865 EMIT2(0x2c69) EMIT2(0xa740)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200866 return OK;
867
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200868 case 'L': case 0x139: case 0x13b: case 0x13d:
869 case 0x13f: case 0x141: case 0x23d: case 0x1e36:
870 case 0x1e38: case 0x1e3a: case 0x1e3c: case 0x2c60:
871 EMIT2('L') EMIT2(0x139) EMIT2(0x13b)
872 EMIT2(0x13d) EMIT2(0x13f) EMIT2(0x141)
873 EMIT2(0x23d) EMIT2(0x1e36) EMIT2(0x1e38)
874 EMIT2(0x1e3a) EMIT2(0x1e3c) EMIT2(0x2c60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200875 return OK;
876
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200877 case 'M': case 0x1e3e: case 0x1e40: case 0x1e42:
878 EMIT2('M') EMIT2(0x1e3e) EMIT2(0x1e40)
879 EMIT2(0x1e42)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200880 return OK;
881
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200882 case 'N': case N_virguilla:
883 case 0x143: case 0x145: case 0x147: case 0x1f8:
884 case 0x1e44: case 0x1e46: case 0x1e48: case 0x1e4a:
885 case 0xa7a4:
886 EMIT2('N') EMIT2(N_virguilla)
887 EMIT2(0x143) EMIT2(0x145) EMIT2(0x147)
888 EMIT2(0x1f8) EMIT2(0x1e44) EMIT2(0x1e46)
889 EMIT2(0x1e48) EMIT2(0x1e4a) EMIT2(0xa7a4)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200890 return OK;
891
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200892 case 'O': case O_grave: case O_acute: case O_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200893 case O_virguilla: case O_diaeresis: case O_slash:
894 case 0x14c: case 0x14e: case 0x150: case 0x19f:
895 case 0x1a0: case 0x1d1: case 0x1ea: case 0x1ec:
896 case 0x1fe: case 0x20c: case 0x20e: case 0x22a:
897 case 0x22c: case 0x22e: case 0x230: case 0x1e4c:
898 case 0x1e4e: case 0x1e50: case 0x1e52: case 0x1ecc:
899 case 0x1ece: case 0x1ed0: case 0x1ed2: case 0x1ed4:
900 case 0x1ed6: case 0x1ed8: case 0x1eda: case 0x1edc:
901 case 0x1ede: case 0x1ee0: case 0x1ee2:
902 EMIT2('O') EMIT2(O_grave) EMIT2(O_acute)
903 EMIT2(O_circumflex) EMIT2(O_virguilla)
904 EMIT2(O_diaeresis) EMIT2(O_slash)
905 EMIT2(0x14c) EMIT2(0x14e) EMIT2(0x150)
906 EMIT2(0x19f) EMIT2(0x1a0) EMIT2(0x1d1)
907 EMIT2(0x1ea) EMIT2(0x1ec) EMIT2(0x1fe)
908 EMIT2(0x20c) EMIT2(0x20e) EMIT2(0x22a)
909 EMIT2(0x22c) EMIT2(0x22e) EMIT2(0x230)
910 EMIT2(0x1e4c) EMIT2(0x1e4e) EMIT2(0x1e50)
911 EMIT2(0x1e52) EMIT2(0x1ecc) EMIT2(0x1ece)
912 EMIT2(0x1ed0) EMIT2(0x1ed2) EMIT2(0x1ed4)
913 EMIT2(0x1ed6) EMIT2(0x1ed8) EMIT2(0x1eda)
914 EMIT2(0x1edc) EMIT2(0x1ede) EMIT2(0x1ee0)
915 EMIT2(0x1ee2)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 return OK;
917
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200918 case 'P': case 0x1a4: case 0x1e54: case 0x1e56: case 0x2c63:
919 EMIT2('P') EMIT2(0x1a4) EMIT2(0x1e54) EMIT2(0x1e56)
920 EMIT2(0x2c63)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200921 return OK;
922
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200923 case 'Q': case 0x24a:
924 EMIT2('Q') EMIT2(0x24a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200925 return OK;
926
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200927 case 'R': case 0x154: case 0x156: case 0x158: case 0x210:
928 case 0x212: case 0x24c: case 0x1e58: case 0x1e5a:
929 case 0x1e5c: case 0x1e5e: case 0x2c64: case 0xa7a6:
930 EMIT2('R') EMIT2(0x154) EMIT2(0x156) EMIT2(0x158)
931 EMIT2(0x210) EMIT2(0x212) EMIT2(0x24c) EMIT2(0x1e58)
932 EMIT2(0x1e5a) EMIT2(0x1e5c) EMIT2(0x1e5e) EMIT2(0x2c64)
933 EMIT2(0xa7a6)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200934 return OK;
935
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200936 case 'S': case 0x15a: case 0x15c: case 0x15e: case 0x160:
937 case 0x218: case 0x1e60: case 0x1e62: case 0x1e64:
938 case 0x1e66: case 0x1e68: case 0x2c7e: case 0xa7a8:
939 EMIT2('S') EMIT2(0x15a) EMIT2(0x15c) EMIT2(0x15e)
940 EMIT2(0x160) EMIT2(0x218) EMIT2(0x1e60) EMIT2(0x1e62)
941 EMIT2(0x1e64) EMIT2(0x1e66) EMIT2(0x1e68) EMIT2(0x2c7e)
942 EMIT2(0xa7a8)
943 return OK;
944
945 case 'T': case 0x162: case 0x164: case 0x166: case 0x1ac:
946 case 0x1ae: case 0x21a: case 0x23e: case 0x1e6a: case 0x1e6c:
947 case 0x1e6e: case 0x1e70:
948 EMIT2('T') EMIT2(0x162) EMIT2(0x164) EMIT2(0x166)
949 EMIT2(0x1ac) EMIT2(0x1ae) EMIT2(0x23e) EMIT2(0x21a)
950 EMIT2(0x1e6a) EMIT2(0x1e6c) EMIT2(0x1e6e) EMIT2(0x1e70)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200951 return OK;
952
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200953 case 'U': case U_grave: case U_acute: case U_diaeresis:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200954 case U_circumflex: case 0x168: case 0x16a: case 0x16c:
955 case 0x16e: case 0x170: case 0x172: case 0x1af:
956 case 0x1d3: case 0x1d5: case 0x1d7: case 0x1d9:
957 case 0x1db: case 0x214: case 0x216: case 0x244:
958 case 0x1e72: case 0x1e74: case 0x1e76: case 0x1e78:
959 case 0x1e7a: case 0x1ee4: case 0x1ee6: case 0x1ee8:
960 case 0x1eea: case 0x1eec: case 0x1eee: case 0x1ef0:
961 EMIT2('U') EMIT2(U_grave) EMIT2(U_acute)
962 EMIT2(U_diaeresis) EMIT2(U_circumflex)
963 EMIT2(0x168) EMIT2(0x16a)
964 EMIT2(0x16c) EMIT2(0x16e) EMIT2(0x170)
965 EMIT2(0x172) EMIT2(0x1af) EMIT2(0x1d3)
966 EMIT2(0x1d5) EMIT2(0x1d7) EMIT2(0x1d9)
967 EMIT2(0x1db) EMIT2(0x214) EMIT2(0x216)
968 EMIT2(0x244) EMIT2(0x1e72) EMIT2(0x1e74)
969 EMIT2(0x1e76) EMIT2(0x1e78) EMIT2(0x1e7a)
970 EMIT2(0x1ee4) EMIT2(0x1ee6) EMIT2(0x1ee8)
971 EMIT2(0x1eea) EMIT2(0x1eec) EMIT2(0x1eee)
972 EMIT2(0x1ef0)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200973 return OK;
974
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200975 case 'V': case 0x1b2: case 0x1e7c: case 0x1e7e:
976 EMIT2('V') EMIT2(0x1b2) EMIT2(0x1e7c) EMIT2(0x1e7e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200977 return OK;
978
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200979 case 'W': case 0x174: case 0x1e80: case 0x1e82: case 0x1e84:
980 case 0x1e86: case 0x1e88:
981 EMIT2('W') EMIT2(0x174) EMIT2(0x1e80) EMIT2(0x1e82)
982 EMIT2(0x1e84) EMIT2(0x1e86) EMIT2(0x1e88)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200983 return OK;
984
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200985 case 'X': case 0x1e8a: case 0x1e8c:
986 EMIT2('X') EMIT2(0x1e8a) EMIT2(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200987 return OK;
988
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200989 case 'Y': case Y_acute: case 0x176: case 0x178:
990 case 0x1b3: case 0x232: case 0x24e: case 0x1e8e:
991 case 0x1ef2: case 0x1ef4: case 0x1ef6: case 0x1ef8:
992 EMIT2('Y') EMIT2(Y_acute)
993 EMIT2(0x176) EMIT2(0x178) EMIT2(0x1b3)
994 EMIT2(0x232) EMIT2(0x24e) EMIT2(0x1e8e)
995 EMIT2(0x1ef2) EMIT2(0x1ef4) EMIT2(0x1ef6)
996 EMIT2(0x1ef8)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200997 return OK;
998
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200999 case 'Z': case 0x179: case 0x17b: case 0x17d:
1000 case 0x1b5: case 0x1e90: case 0x1e92: case 0x1e94:
1001 case 0x2c6b:
1002 EMIT2('Z') EMIT2(0x179) EMIT2(0x17b) EMIT2(0x17d)
1003 EMIT2(0x1b5) EMIT2(0x1e90) EMIT2(0x1e92)
1004 EMIT2(0x1e94) EMIT2(0x2c6b)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001005 return OK;
1006
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001007 case 'a': case a_grave: case a_acute: case a_circumflex:
1008 case a_virguilla: case a_diaeresis: case a_ring:
1009 case 0x101: case 0x103: case 0x105: case 0x1ce:
1010 case 0x1df: case 0x1e1: case 0x1fb: case 0x201:
1011 case 0x203: case 0x227: case 0x1d8f: case 0x1e01:
1012 case 0x1e9a: case 0x1ea1: case 0x1ea3: case 0x1ea5:
1013 case 0x1ea7: case 0x1ea9: case 0x1eab: case 0x1ead:
1014 case 0x1eaf: case 0x1eb1: case 0x1eb3: case 0x1eb5:
1015 case 0x1eb7: case 0x2c65:
1016 EMIT2('a') EMIT2(a_grave) EMIT2(a_acute)
1017 EMIT2(a_circumflex) EMIT2(a_virguilla)
1018 EMIT2(a_diaeresis) EMIT2(a_ring)
1019 EMIT2(0x101) EMIT2(0x103) EMIT2(0x105)
1020 EMIT2(0x1ce) EMIT2(0x1df) EMIT2(0x1e1)
1021 EMIT2(0x1fb) EMIT2(0x201) EMIT2(0x203)
1022 EMIT2(0x227) EMIT2(0x1d8f) EMIT2(0x1e01)
1023 EMIT2(0x1e9a) EMIT2(0x1ea1) EMIT2(0x1ea3)
1024 EMIT2(0x1ea5) EMIT2(0x1ea7) EMIT2(0x1ea9)
1025 EMIT2(0x1eab) EMIT2(0x1ead) EMIT2(0x1eaf)
1026 EMIT2(0x1eb1) EMIT2(0x1eb3) EMIT2(0x1eb5)
1027 EMIT2(0x1eb7) EMIT2(0x2c65)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001028 return OK;
1029
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001030 case 'b': case 0x180: case 0x253: case 0x1d6c: case 0x1d80:
1031 case 0x1e03: case 0x1e05: case 0x1e07:
1032 EMIT2('b') EMIT2(0x180) EMIT2(0x253) EMIT2(0x1d6c)
1033 EMIT2(0x1d80) EMIT2(0x1e03) EMIT2(0x1e05) EMIT2(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001034 return OK;
1035
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001036 case 'c': case c_cedilla: case 0x107: case 0x109: case 0x10b:
1037 case 0x10d: case 0x188: case 0x23c: case 0x1e09: case 0xa793:
1038 case 0xa794:
1039 EMIT2('c') EMIT2(c_cedilla)
1040 EMIT2(0x107) EMIT2(0x109) EMIT2(0x10b)
1041 EMIT2(0x10d) EMIT2(0x188) EMIT2(0x23c)
1042 EMIT2(0x1e09) EMIT2(0xa793) EMIT2(0xa794)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001043 return OK;
1044
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001045 case 'd': case 0x10f: case 0x111: case 0x257: case 0x1d6d:
1046 case 0x1d81: case 0x1d91: case 0x1e0b: case 0x1e0d: case 0x1e0f:
1047 case 0x1e11: case 0x1e13:
1048 EMIT2('d') EMIT2(0x10f) EMIT2(0x111)
1049 EMIT2(0x257) EMIT2(0x1d6d) EMIT2(0x1d81)
1050 EMIT2(0x1d91) EMIT2(0x1e0b) EMIT2(0x1e0d)
1051 EMIT2(0x1e0f) EMIT2(0x1e11) EMIT2(0x1e13)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001052 return OK;
1053
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001054 case 'e': case e_grave: case e_acute: case e_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001055 case e_diaeresis: case 0x113: case 0x115: case 0x117:
1056 case 0x119: case 0x11b: case 0x205: case 0x207:
1057 case 0x229: case 0x247: case 0x1d92: case 0x1e15:
1058 case 0x1e17: case 0x1e19: case 0x1e1b: case 0x1e1d:
1059 case 0x1eb9: case 0x1ebb: case 0x1ebd: case 0x1ebf:
1060 case 0x1ec1: case 0x1ec3: case 0x1ec5: case 0x1ec7:
1061 EMIT2('e') EMIT2(e_grave) EMIT2(e_acute)
1062 EMIT2(e_circumflex) EMIT2(e_diaeresis)
1063 EMIT2(0x113) EMIT2(0x115)
1064 EMIT2(0x117) EMIT2(0x119) EMIT2(0x11b)
1065 EMIT2(0x205) EMIT2(0x207) EMIT2(0x229)
1066 EMIT2(0x247) EMIT2(0x1d92) EMIT2(0x1e15)
1067 EMIT2(0x1e17) EMIT2(0x1e19) EMIT2(0x1e1b)
1068 EMIT2(0x1e1d) EMIT2(0x1eb9) EMIT2(0x1ebb)
1069 EMIT2(0x1ebd) EMIT2(0x1ebf) EMIT2(0x1ec1)
1070 EMIT2(0x1ec3) EMIT2(0x1ec5) EMIT2(0x1ec7)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001071 return OK;
1072
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001073 case 'f': case 0x192: case 0x1d6e: case 0x1d82:
1074 case 0x1e1f: case 0xa799:
1075 EMIT2('f') EMIT2(0x192) EMIT2(0x1d6e) EMIT2(0x1d82)
1076 EMIT2(0x1e1f) EMIT2(0xa799)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001077 return OK;
1078
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001079 case 'g': case 0x11d: case 0x11f: case 0x121: case 0x123:
1080 case 0x1e5: case 0x1e7: case 0x1f5: case 0x260: case 0x1d83:
1081 case 0x1e21: case 0xa7a1:
1082 EMIT2('g') EMIT2(0x11d) EMIT2(0x11f) EMIT2(0x121)
1083 EMIT2(0x123) EMIT2(0x1e5) EMIT2(0x1e7)
1084 EMIT2(0x1f5) EMIT2(0x260) EMIT2(0x1d83)
1085 EMIT2(0x1e21) EMIT2(0xa7a1)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001086 return OK;
1087
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001088 case 'h': case 0x125: case 0x127: case 0x21f: case 0x1e23:
1089 case 0x1e25: case 0x1e27: case 0x1e29: case 0x1e2b:
1090 case 0x1e96: case 0x2c68: case 0xa795:
1091 EMIT2('h') EMIT2(0x125) EMIT2(0x127) EMIT2(0x21f)
1092 EMIT2(0x1e23) EMIT2(0x1e25) EMIT2(0x1e27)
1093 EMIT2(0x1e29) EMIT2(0x1e2b) EMIT2(0x1e96)
1094 EMIT2(0x2c68) EMIT2(0xa795)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001095 return OK;
1096
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001097 case 'i': case i_grave: case i_acute: case i_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001098 case i_diaeresis: case 0x129: case 0x12b: case 0x12d:
1099 case 0x12f: case 0x1d0: case 0x209: case 0x20b:
1100 case 0x268: case 0x1d96: case 0x1e2d: case 0x1e2f:
1101 case 0x1ec9: case 0x1ecb:
1102 EMIT2('i') EMIT2(i_grave) EMIT2(i_acute)
1103 EMIT2(i_circumflex) EMIT2(i_diaeresis)
1104 EMIT2(0x129) EMIT2(0x12b) EMIT2(0x12d)
1105 EMIT2(0x12f) EMIT2(0x1d0) EMIT2(0x209)
1106 EMIT2(0x20b) EMIT2(0x268) EMIT2(0x1d96)
1107 EMIT2(0x1e2d) EMIT2(0x1e2f) EMIT2(0x1ec9)
1108 EMIT2(0x1ecb) EMIT2(0x1ecb)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001109 return OK;
1110
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001111 case 'j': case 0x135: case 0x1f0: case 0x249:
1112 EMIT2('j') EMIT2(0x135) EMIT2(0x1f0) EMIT2(0x249)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001113 return OK;
1114
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001115 case 'k': case 0x137: case 0x199: case 0x1e9: case 0x1d84:
1116 case 0x1e31: case 0x1e33: case 0x1e35: case 0x2c6a: case 0xa741:
1117 EMIT2('k') EMIT2(0x137) EMIT2(0x199) EMIT2(0x1e9)
1118 EMIT2(0x1d84) EMIT2(0x1e31) EMIT2(0x1e33)
1119 EMIT2(0x1e35) EMIT2(0x2c6a) EMIT2(0xa741)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 return OK;
1121
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001122 case 'l': case 0x13a: case 0x13c: case 0x13e: case 0x140:
1123 case 0x142: case 0x19a: case 0x1e37: case 0x1e39: case 0x1e3b:
1124 case 0x1e3d: case 0x2c61:
1125 EMIT2('l') EMIT2(0x13a) EMIT2(0x13c)
1126 EMIT2(0x13e) EMIT2(0x140) EMIT2(0x142)
1127 EMIT2(0x19a) EMIT2(0x1e37) EMIT2(0x1e39)
1128 EMIT2(0x1e3b) EMIT2(0x1e3d) EMIT2(0x2c61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001129 return OK;
1130
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001131 case 'm': case 0x1d6f: case 0x1e3f: case 0x1e41: case 0x1e43:
1132 EMIT2('m') EMIT2(0x1d6f) EMIT2(0x1e3f)
1133 EMIT2(0x1e41) EMIT2(0x1e43)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001134 return OK;
1135
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001136 case 'n': case n_virguilla: case 0x144: case 0x146: case 0x148:
1137 case 0x149: case 0x1f9: case 0x1d70: case 0x1d87: case 0x1e45:
1138 case 0x1e47: case 0x1e49: case 0x1e4b: case 0xa7a5:
1139 EMIT2('n') EMIT2(n_virguilla)
1140 EMIT2(0x144) EMIT2(0x146) EMIT2(0x148)
1141 EMIT2(0x149) EMIT2(0x1f9) EMIT2(0x1d70)
1142 EMIT2(0x1d87) EMIT2(0x1e45) EMIT2(0x1e47)
1143 EMIT2(0x1e49) EMIT2(0x1e4b) EMIT2(0xa7a5)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001144 return OK;
1145
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001146 case 'o': case o_grave: case o_acute: case o_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001147 case o_virguilla: case o_diaeresis: case o_slash:
1148 case 0x14d: case 0x14f: case 0x151: case 0x1a1:
1149 case 0x1d2: case 0x1eb: case 0x1ed: case 0x1ff:
1150 case 0x20d: case 0x20f: case 0x22b: case 0x22d:
1151 case 0x22f: case 0x231: case 0x275: case 0x1e4d:
1152 case 0x1e4f: case 0x1e51: case 0x1e53: case 0x1ecd:
1153 case 0x1ecf: case 0x1ed1: case 0x1ed3: case 0x1ed5:
1154 case 0x1ed7: case 0x1ed9: case 0x1edb: case 0x1edd:
1155 case 0x1edf: case 0x1ee1: case 0x1ee3:
1156 EMIT2('o') EMIT2(o_grave) EMIT2(o_acute)
1157 EMIT2(o_circumflex) EMIT2(o_virguilla)
1158 EMIT2(o_diaeresis) EMIT2(o_slash)
1159 EMIT2(0x14d) EMIT2(0x14f) EMIT2(0x151)
1160 EMIT2(0x1a1) EMIT2(0x1d2) EMIT2(0x1eb)
1161 EMIT2(0x1ed) EMIT2(0x1ff) EMIT2(0x20d)
1162 EMIT2(0x20f) EMIT2(0x22b) EMIT2(0x22d)
1163 EMIT2(0x22f) EMIT2(0x231) EMIT2(0x275)
1164 EMIT2(0x1e4d) EMIT2(0x1e4f) EMIT2(0x1e51)
1165 EMIT2(0x1e53) EMIT2(0x1ecd) EMIT2(0x1ecf)
1166 EMIT2(0x1ed1) EMIT2(0x1ed3) EMIT2(0x1ed5)
1167 EMIT2(0x1ed7) EMIT2(0x1ed9) EMIT2(0x1edb)
1168 EMIT2(0x1edd) EMIT2(0x1edf) EMIT2(0x1ee1)
1169 EMIT2(0x1ee3)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001170 return OK;
1171
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001172 case 'p': case 0x1a5: case 0x1d71: case 0x1d7d: case 0x1d88:
1173 case 0x1e55: case 0x1e57:
1174 EMIT2('p') EMIT2(0x1a5) EMIT2(0x1d71) EMIT2(0x1d7d)
1175 EMIT2(0x1d88) EMIT2(0x1e55) EMIT2(0x1e57)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001176 return OK;
1177
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001178 case 'q': case 0x24b: case 0x2a0:
1179 EMIT2('q') EMIT2(0x24b) EMIT2(0x2a0)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001180 return OK;
1181
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001182 case 'r': case 0x155: case 0x157: case 0x159: case 0x211:
1183 case 0x213: case 0x24d: case 0x27d: case 0x1d72: case 0x1d73:
1184 case 0x1d89: case 0x1e59: case 0x1e5b: case 0x1e5d: case 0x1e5f:
1185 case 0xa7a7:
1186 EMIT2('r') EMIT2(0x155) EMIT2(0x157) EMIT2(0x159)
1187 EMIT2(0x211) EMIT2(0x213) EMIT2(0x24d) EMIT2(0x27d)
1188 EMIT2(0x1d72) EMIT2(0x1d73) EMIT2(0x1d89) EMIT2(0x1e59)
1189 EMIT2(0x1e5b) EMIT2(0x1e5d) EMIT2(0x1e5f) EMIT2(0xa7a7)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001190 return OK;
1191
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001192 case 's': case 0x15b: case 0x15d: case 0x15f: case 0x161:
1193 case 0x219: case 0x23f: case 0x1d74: case 0x1d8a: case 0x1e61:
1194 case 0x1e63: case 0x1e65: case 0x1e67: case 0x1e69: case 0xa7a9:
1195 EMIT2('s') EMIT2(0x15b) EMIT2(0x15d) EMIT2(0x15f)
1196 EMIT2(0x161) EMIT2(0x219) EMIT2(0x23f) EMIT2(0x1d74)
1197 EMIT2(0x1d8a) EMIT2(0x1e61) EMIT2(0x1e63) EMIT2(0x1e65)
1198 EMIT2(0x1e67) EMIT2(0x1e69) EMIT2(0xa7a9)
1199 return OK;
1200
1201 case 't': case 0x163: case 0x165: case 0x167: case 0x1ab:
1202 case 0x1ad: case 0x21b: case 0x288: case 0x1d75: case 0x1e6b:
1203 case 0x1e6d: case 0x1e6f: case 0x1e71: case 0x1e97: case 0x2c66:
1204 EMIT2('t') EMIT2(0x163) EMIT2(0x165) EMIT2(0x167)
1205 EMIT2(0x1ab) EMIT2(0x1ad) EMIT2(0x21b) EMIT2(0x288)
1206 EMIT2(0x1d75) EMIT2(0x1e6b) EMIT2(0x1e6d) EMIT2(0x1e6f)
1207 EMIT2(0x1e71) EMIT2(0x1e97) EMIT2(0x2c66)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001208 return OK;
1209
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001210 case 'u': case u_grave: case u_acute: case u_circumflex:
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001211 case u_diaeresis: case 0x169: case 0x16b: case 0x16d:
1212 case 0x16f: case 0x171: case 0x173: case 0x1b0: case 0x1d4:
1213 case 0x1d6: case 0x1d8: case 0x1da: case 0x1dc: case 0x215:
1214 case 0x217: case 0x289: case 0x1d7e: case 0x1d99: case 0x1e73:
1215 case 0x1e75: case 0x1e77: case 0x1e79: case 0x1e7b:
1216 case 0x1ee5: case 0x1ee7: case 0x1ee9: case 0x1eeb:
1217 case 0x1eed: case 0x1eef: case 0x1ef1:
1218 EMIT2('u') EMIT2(u_grave) EMIT2(u_acute)
1219 EMIT2(u_circumflex) EMIT2(u_diaeresis)
1220 EMIT2(0x169) EMIT2(0x16b)
1221 EMIT2(0x16d) EMIT2(0x16f) EMIT2(0x171)
1222 EMIT2(0x173) EMIT2(0x1d6) EMIT2(0x1d8)
1223 EMIT2(0x215) EMIT2(0x217) EMIT2(0x1b0)
1224 EMIT2(0x1d4) EMIT2(0x1da) EMIT2(0x1dc)
1225 EMIT2(0x289) EMIT2(0x1e73) EMIT2(0x1d7e)
1226 EMIT2(0x1d99) EMIT2(0x1e75) EMIT2(0x1e77)
1227 EMIT2(0x1e79) EMIT2(0x1e7b) EMIT2(0x1ee5)
1228 EMIT2(0x1ee7) EMIT2(0x1ee9) EMIT2(0x1eeb)
1229 EMIT2(0x1eed) EMIT2(0x1eef) EMIT2(0x1ef1)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001230 return OK;
1231
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001232 case 'v': case 0x28b: case 0x1d8c: case 0x1e7d: case 0x1e7f:
1233 EMIT2('v') EMIT2(0x28b) EMIT2(0x1d8c) EMIT2(0x1e7d)
1234 EMIT2(0x1e7f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001235 return OK;
1236
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001237 case 'w': case 0x175: case 0x1e81: case 0x1e83: case 0x1e85:
1238 case 0x1e87: case 0x1e89: case 0x1e98:
1239 EMIT2('w') EMIT2(0x175) EMIT2(0x1e81) EMIT2(0x1e83)
1240 EMIT2(0x1e85) EMIT2(0x1e87) EMIT2(0x1e89) EMIT2(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001241 return OK;
1242
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001243 case 'x': case 0x1e8b: case 0x1e8d:
1244 EMIT2('x') EMIT2(0x1e8b) EMIT2(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245 return OK;
1246
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001247 case 'y': case y_acute: case y_diaeresis: case 0x177:
1248 case 0x1b4: case 0x233: case 0x24f: case 0x1e8f:
1249 case 0x1e99: case 0x1ef3: case 0x1ef5: case 0x1ef7:
1250 case 0x1ef9:
1251 EMIT2('y') EMIT2(y_acute) EMIT2(y_diaeresis)
1252 EMIT2(0x177) EMIT2(0x1b4) EMIT2(0x233) EMIT2(0x24f)
1253 EMIT2(0x1e8f) EMIT2(0x1e99) EMIT2(0x1ef3)
1254 EMIT2(0x1ef5) EMIT2(0x1ef7) EMIT2(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001255 return OK;
1256
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001257 case 'z': case 0x17a: case 0x17c: case 0x17e: case 0x1b6:
1258 case 0x1d76: case 0x1d8e: case 0x1e91: case 0x1e93:
1259 case 0x1e95: case 0x2c6c:
1260 EMIT2('z') EMIT2(0x17a) EMIT2(0x17c) EMIT2(0x17e)
1261 EMIT2(0x1b6) EMIT2(0x1d76) EMIT2(0x1d8e) EMIT2(0x1e91)
1262 EMIT2(0x1e93) EMIT2(0x1e95) EMIT2(0x2c6c)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001263 return OK;
1264
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001265 // default: character itself
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266 }
1267 }
1268
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001269 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001270 return OK;
1271#undef EMIT2
1272}
1273
1274/*
1275 * Code to parse regular expression.
1276 *
1277 * We try to reuse parsing functions in regexp.c to
1278 * minimize surprise and keep the syntax consistent.
1279 */
1280
1281/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 * Parse the lowest level.
1283 *
1284 * An atom can be one of a long list of items. Many atoms match one character
1285 * in the text. It is often an ordinary character or a character class.
1286 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1287 * is only for syntax highlighting.
1288 *
1289 * atom ::= ordinary-atom
1290 * or \( pattern \)
1291 * or \%( pattern \)
1292 * or \z( pattern \)
1293 */
1294 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001295nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001296{
1297 int c;
1298 int charclass;
1299 int equiclass;
1300 int collclass;
1301 int got_coll_char;
1302 char_u *p;
1303 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001304 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001306 int emit_range;
1307 int negated;
1308 int result;
1309 int startc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001310 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001311
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001313 switch (c)
1314 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001315 case NUL:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001316 EMSG_RET_FAIL(_(e_nfa_regexp_end_encountered_prematurely));
Bram Moolenaar47196582013-05-25 22:04:23 +02001317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 case Magic('^'):
1319 EMIT(NFA_BOL);
1320 break;
1321
1322 case Magic('$'):
1323 EMIT(NFA_EOL);
1324#if defined(FEAT_SYN_HL) || defined(PROTO)
1325 had_eol = TRUE;
1326#endif
1327 break;
1328
1329 case Magic('<'):
1330 EMIT(NFA_BOW);
1331 break;
1332
1333 case Magic('>'):
1334 EMIT(NFA_EOW);
1335 break;
1336
1337 case Magic('_'):
1338 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001339 if (c == NUL)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001340 EMSG_RET_FAIL(_(e_nfa_regexp_end_encountered_prematurely));
Bram Moolenaar174a8482013-11-28 14:20:17 +01001341
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001342 if (c == '^') // "\_^" is start-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 {
1344 EMIT(NFA_BOL);
1345 break;
1346 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001347 if (c == '$') // "\_$" is end-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 {
1349 EMIT(NFA_EOL);
1350#if defined(FEAT_SYN_HL) || defined(PROTO)
1351 had_eol = TRUE;
1352#endif
1353 break;
1354 }
1355
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001356 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001358 // "\_[" is collection plus newline
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001360 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001361
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001362 // "\_x" is character class plus newline
1363 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364
1365 /*
1366 * Character classes.
1367 */
1368 case Magic('.'):
1369 case Magic('i'):
1370 case Magic('I'):
1371 case Magic('k'):
1372 case Magic('K'):
1373 case Magic('f'):
1374 case Magic('F'):
1375 case Magic('p'):
1376 case Magic('P'):
1377 case Magic('s'):
1378 case Magic('S'):
1379 case Magic('d'):
1380 case Magic('D'):
1381 case Magic('x'):
1382 case Magic('X'):
1383 case Magic('o'):
1384 case Magic('O'):
1385 case Magic('w'):
1386 case Magic('W'):
1387 case Magic('h'):
1388 case Magic('H'):
1389 case Magic('a'):
1390 case Magic('A'):
1391 case Magic('l'):
1392 case Magic('L'):
1393 case Magic('u'):
1394 case Magic('U'):
1395 p = vim_strchr(classchars, no_Magic(c));
1396 if (p == NULL)
1397 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001398 if (extra == NFA_ADD_NL)
1399 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001400 semsg(_(e_nfa_regexp_invalid_character_class_nr), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001401 rc_did_emsg = TRUE;
1402 return FAIL;
1403 }
Bram Moolenaar097c5372023-05-24 21:02:24 +01001404 siemsg("Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001405 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001407
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001408 // When '.' is followed by a composing char ignore the dot, so that
1409 // the composing char is matched here.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001410 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1411 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001412 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001413 c = getchr();
1414 goto nfa_do_multibyte;
1415 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001417 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001418 {
1419 EMIT(NFA_NEWL);
1420 EMIT(NFA_OR);
1421 regflags |= RF_HASNL;
1422 }
1423 break;
1424
1425 case Magic('n'):
1426 if (reg_string)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001427 // In a string "\n" matches a newline character.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001428 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001429 else
1430 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001431 // In buffer text "\n" matches the end of a line.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001432 EMIT(NFA_NEWL);
1433 regflags |= RF_HASNL;
1434 }
1435 break;
1436
1437 case Magic('('):
1438 if (nfa_reg(REG_PAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001439 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440 break;
1441
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 case Magic('|'):
1443 case Magic('&'):
1444 case Magic(')'):
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001445 semsg(_(e_nfa_regexp_misplaced_chr), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001446 return FAIL;
1447
1448 case Magic('='):
1449 case Magic('?'):
1450 case Magic('+'):
1451 case Magic('@'):
1452 case Magic('*'):
1453 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001454 // these should follow an atom, not form an atom
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001455 semsg(_(e_nfa_regexp_misplaced_chr), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001456 return FAIL;
1457
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001458 case Magic('~'):
1459 {
1460 char_u *lp;
1461
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001462 // Previous substitute pattern.
1463 // Generated as "\%(pattern\)".
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001464 if (reg_prev_sub == NULL)
1465 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001466 emsg(_(e_no_previous_substitute_regular_expression));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001467 return FAIL;
1468 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001469 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001470 {
1471 EMIT(PTR2CHAR(lp));
1472 if (lp != reg_prev_sub)
1473 EMIT(NFA_CONCAT);
1474 }
1475 EMIT(NFA_NOPEN);
1476 break;
1477 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001478
Bram Moolenaar428e9872013-05-30 17:05:39 +02001479 case Magic('1'):
1480 case Magic('2'):
1481 case Magic('3'):
1482 case Magic('4'):
1483 case Magic('5'):
1484 case Magic('6'):
1485 case Magic('7'):
1486 case Magic('8'):
1487 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001488 {
1489 int refnum = no_Magic(c) - '1';
1490
1491 if (!seen_endbrace(refnum + 1))
1492 return FAIL;
1493 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001494 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001495 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001496 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497
1498 case Magic('z'):
1499 c = no_Magic(getchr());
1500 switch (c)
1501 {
1502 case 's':
1503 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001504 if (re_mult_next("\\zs") == FAIL)
1505 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 break;
1507 case 'e':
1508 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001509 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001510 if (re_mult_next("\\ze") == FAIL)
1511 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001512 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001513#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 case '1':
1515 case '2':
1516 case '3':
1517 case '4':
1518 case '5':
1519 case '6':
1520 case '7':
1521 case '8':
1522 case '9':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001523 // \z1...\z9
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001524 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001525 EMSG_RET_FAIL(_(e_z1_z9_not_allowed_here));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001526 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001527 // No need to set rex.nfa_has_backref, the sub-matches don't
1528 // change when \z1 .. \z9 matches or not.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001529 re_has_z = REX_USE;
1530 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001531 case '(':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001532 // \z(
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001533 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001534 EMSG_RET_FAIL(_(e_z_not_allowed_here));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001535 if (nfa_reg(REG_ZPAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001536 return FAIL; // cascaded error
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001537 re_has_z = REX_SET;
1538 break;
1539#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 default:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001541 semsg(_(e_nfa_regexp_unknown_operator_z_chr), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 return FAIL;
1543 }
1544 break;
1545
1546 case Magic('%'):
1547 c = no_Magic(getchr());
1548 switch (c)
1549 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001550 // () without a back reference
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 case '(':
1552 if (nfa_reg(REG_NPAREN) == FAIL)
1553 return FAIL;
1554 EMIT(NFA_NOPEN);
1555 break;
1556
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001557 case 'd': // %d123 decimal
1558 case 'o': // %o123 octal
1559 case 'x': // %xab hex 2
1560 case 'u': // %uabcd hex 4
1561 case 'U': // %U1234abcd hex 8
Bram Moolenaar47196582013-05-25 22:04:23 +02001562 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001563 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001564
Bram Moolenaar47196582013-05-25 22:04:23 +02001565 switch (c)
1566 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001567 case 'd': nr = getdecchrs(); break;
1568 case 'o': nr = getoctchrs(); break;
1569 case 'x': nr = gethexchrs(2); break;
1570 case 'u': nr = gethexchrs(4); break;
1571 case 'U': nr = gethexchrs(8); break;
1572 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001573 }
1574
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001575 if (nr < 0 || nr > INT_MAX)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001576 EMSG2_RET_FAIL(_(e_invalid_character_after_str_2),
1577 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001578 // A NUL is stored in the text as NL
1579 // TODO: what if a composing character follows?
Bram Moolenaar595cad22013-09-22 13:57:24 +02001580 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001581 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001582 break;
1583
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001584 // Catch \%^ and \%$ regardless of where they appear in the
1585 // pattern -- regardless of whether or not it makes sense.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001586 case '^':
1587 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001588 break;
1589
1590 case '$':
1591 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001592 break;
1593
1594 case '#':
Christian Brabandt360da402022-05-18 15:04:02 +01001595 if (regparse[0] == '=' && regparse[1] >= 48
1596 && regparse[1] <= 50)
1597 {
1598 // misplaced \%#=1
1599 semsg(_(e_atom_engine_must_be_at_start_of_pattern),
1600 regparse[1]);
1601 return FAIL;
1602 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001603 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 break;
1605
1606 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001607 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001608 break;
1609
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001610 case 'C':
1611 EMIT(NFA_ANY_COMPOSING);
1612 break;
1613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001614 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001615 {
1616 int n;
1617
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001618 // \%[abc]
Bram Moolenaard7986252013-06-17 21:33:41 +02001619 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001620 {
1621 if (c == NUL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001622 EMSG2_RET_FAIL(_(e_missing_sb_after_str),
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001623 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001624 // recursive call!
Bram Moolenaard7986252013-06-17 21:33:41 +02001625 if (nfa_regatom() == FAIL)
1626 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001627 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001628 getchr(); // get the ]
Bram Moolenaar2976c022013-06-05 21:30:37 +02001629 if (n == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001630 EMSG2_RET_FAIL(_(e_empty_str_brackets),
Bram Moolenaar2976c022013-06-05 21:30:37 +02001631 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001632 EMIT(NFA_OPT_CHARS);
1633 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001634
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001635 // Emit as "\%(\%[abc]\)" to be able to handle
1636 // "\%[abc]*" which would cause the empty string to be
1637 // matched an unlimited number of times. NFA_NOPEN is
1638 // added only once at a position, while NFA_SPLIT is
1639 // added multiple times. This is more efficient than
1640 // not allowing NFA_SPLIT multiple times, it is used
1641 // a lot.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001642 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001643 break;
1644 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001645
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001646 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001647 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001648 long_u n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001649 int cmp = c;
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001650 int cur = FALSE;
Bram Moolenaar72bb10d2022-04-05 14:00:32 +01001651 int got_digit = FALSE;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001652
1653 if (c == '<' || c == '>')
1654 c = getchr();
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001655 if (no_Magic(c) == '.')
1656 {
1657 cur = TRUE;
1658 c = getchr();
1659 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001660 while (VIM_ISDIGIT(c))
1661 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001662 long_u tmp;
1663
1664 if (cur)
Bram Moolenaarb10ff5c2022-03-19 11:31:38 +00001665 {
Bram Moolenaar91ff3d42022-04-04 18:32:32 +01001666 semsg(_(e_regexp_number_after_dot_pos_search_chr),
Bram Moolenaarb10ff5c2022-03-19 11:31:38 +00001667 no_Magic(c));
1668 return FAIL;
1669 }
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001670 tmp = n * 10 + (c - '0');
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001671
1672 if (tmp < n)
1673 {
1674 // overflow.
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001675 emsg(_(e_percent_value_too_large));
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001676 return FAIL;
1677 }
1678 n = tmp;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001679 c = getchr();
Bram Moolenaar72bb10d2022-04-05 14:00:32 +01001680 got_digit = TRUE;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001681 }
1682 if (c == 'l' || c == 'c' || c == 'v')
1683 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001684 long_u limit = INT_MAX;
Bram Moolenaar9403a212019-02-13 18:35:06 +01001685
Bram Moolenaar72bb10d2022-04-05 14:00:32 +01001686 if (!cur && !got_digit)
Bram Moolenaar91ff3d42022-04-04 18:32:32 +01001687 {
1688 semsg(_(e_nfa_regexp_missing_value_in_chr),
1689 no_Magic(c));
1690 return FAIL;
1691 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001692 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001693 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001694 if (cur)
1695 n = curwin->w_cursor.lnum;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001696 // \%{n}l \%{n}<l \%{n}>l
Bram Moolenaar423532e2013-05-29 21:14:42 +02001697 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001698 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001699 if (save_prev_at_start)
1700 at_start = TRUE;
1701 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001702 else if (c == 'c')
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001703 {
1704 if (cur)
1705 {
1706 n = curwin->w_cursor.col;
1707 n++;
1708 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001709 // \%{n}c \%{n}<c \%{n}>c
Bram Moolenaar423532e2013-05-29 21:14:42 +02001710 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001711 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001712 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001713 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001714 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001715 if (cur)
1716 {
1717 colnr_T vcol = 0;
1718
1719 getvvcol(curwin, &curwin->w_cursor,
1720 NULL, NULL, &vcol);
1721 n = ++vcol;
1722 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001723 // \%{n}v \%{n}<v \%{n}>v
Bram Moolenaar423532e2013-05-29 21:14:42 +02001724 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001725 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001726 limit = INT_MAX / MB_MAXBYTES;
1727 }
1728 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001729 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001730 emsg(_(e_percent_value_too_large));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001731 return FAIL;
1732 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001733 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001734 break;
1735 }
Julio B46fa3c72024-03-28 10:23:37 +01001736 else if (no_Magic(c) == '\'' && n == 0)
Bram Moolenaar044aa292013-06-04 21:27:38 +02001737 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001738 // \%'m \%<'m \%>'m
Bram Moolenaar044aa292013-06-04 21:27:38 +02001739 EMIT(cmp == '<' ? NFA_MARK_LT :
1740 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001741 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001742 break;
1743 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001744 }
Bram Moolenaarc96311b2022-11-25 21:13:47 +00001745 semsg(_(e_nfa_regexp_unknown_operator_percent_chr),
1746 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 return FAIL;
1748 }
1749 break;
1750
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001751 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001752collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001753 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001754 * [abc] uses NFA_START_COLL - NFA_END_COLL
1755 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1756 * Each character is produced as a regular state, using
1757 * NFA_CONCAT to bind them together.
1758 * Besides normal characters there can be:
1759 * - character classes NFA_CLASS_*
1760 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001761 */
1762
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001763 p = regparse;
1764 endp = skip_anyof(p);
1765 if (*endp == ']')
1766 {
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01001767 int plen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 /*
1769 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001770 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771 * and perform the necessary substitutions in the NFA.
1772 */
1773 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001774 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001775 if (result != FAIL)
1776 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001777 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001779 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 EMIT(NFA_NEWL);
1781 EMIT(NFA_OR);
1782 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001783 else
1784 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001785 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001786 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001787 return OK;
1788 }
1789 /*
1790 * Failed to recognize a character class. Use the simple
1791 * version that turns [abc] into 'a' OR 'b' OR 'c'
1792 */
=?UTF-8?q?Dundar=20G=C3=B6c?=09f688c2021-07-08 18:05:00 +02001793 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 negated = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001795 if (*regparse == '^') // negated range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001796 {
1797 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001798 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001799 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001801 else
1802 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 if (*regparse == '-')
1804 {
1805 startc = '-';
1806 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001807 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001808 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001810 // Emit the OR branches for each character in the []
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811 emit_range = FALSE;
1812 while (regparse < endp)
1813 {
=?UTF-8?q?Dundar=20G=C3=B6c?=09f688c2021-07-08 18:05:00 +02001814 int oldstartc = startc;
1815
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 startc = -1;
1817 got_coll_char = FALSE;
1818 if (*regparse == '[')
1819 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001820 // Check for [: :], [= =], [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 equiclass = collclass = 0;
1822 charclass = get_char_class(&regparse);
1823 if (charclass == CLASS_NONE)
1824 {
1825 equiclass = get_equi_class(&regparse);
1826 if (equiclass == 0)
1827 collclass = get_coll_element(&regparse);
1828 }
1829
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001830 // Character class like [:alpha:]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831 if (charclass != CLASS_NONE)
1832 {
1833 switch (charclass)
1834 {
1835 case CLASS_ALNUM:
1836 EMIT(NFA_CLASS_ALNUM);
1837 break;
1838 case CLASS_ALPHA:
1839 EMIT(NFA_CLASS_ALPHA);
1840 break;
1841 case CLASS_BLANK:
1842 EMIT(NFA_CLASS_BLANK);
1843 break;
1844 case CLASS_CNTRL:
1845 EMIT(NFA_CLASS_CNTRL);
1846 break;
1847 case CLASS_DIGIT:
1848 EMIT(NFA_CLASS_DIGIT);
1849 break;
1850 case CLASS_GRAPH:
1851 EMIT(NFA_CLASS_GRAPH);
1852 break;
1853 case CLASS_LOWER:
Bram Moolenaar66c50c52021-01-02 17:43:49 +01001854 wants_nfa = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 EMIT(NFA_CLASS_LOWER);
1856 break;
1857 case CLASS_PRINT:
1858 EMIT(NFA_CLASS_PRINT);
1859 break;
1860 case CLASS_PUNCT:
1861 EMIT(NFA_CLASS_PUNCT);
1862 break;
1863 case CLASS_SPACE:
1864 EMIT(NFA_CLASS_SPACE);
1865 break;
1866 case CLASS_UPPER:
Bram Moolenaar66c50c52021-01-02 17:43:49 +01001867 wants_nfa = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 EMIT(NFA_CLASS_UPPER);
1869 break;
1870 case CLASS_XDIGIT:
1871 EMIT(NFA_CLASS_XDIGIT);
1872 break;
1873 case CLASS_TAB:
1874 EMIT(NFA_CLASS_TAB);
1875 break;
1876 case CLASS_RETURN:
1877 EMIT(NFA_CLASS_RETURN);
1878 break;
1879 case CLASS_BACKSPACE:
1880 EMIT(NFA_CLASS_BACKSPACE);
1881 break;
1882 case CLASS_ESCAPE:
1883 EMIT(NFA_CLASS_ESCAPE);
1884 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001885 case CLASS_IDENT:
1886 EMIT(NFA_CLASS_IDENT);
1887 break;
1888 case CLASS_KEYWORD:
1889 EMIT(NFA_CLASS_KEYWORD);
1890 break;
1891 case CLASS_FNAME:
1892 EMIT(NFA_CLASS_FNAME);
1893 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001895 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 continue;
1897 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001898 // Try equivalence class [=a=] and the like
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 if (equiclass != 0)
1900 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001901 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 if (result == FAIL)
1903 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001904 // should never happen
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001905 EMSG_RET_FAIL(_(e_error_building_nfa_with_equivalence_class));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 continue;
1908 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001909 // Try collating class like [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 if (collclass != 0)
1911 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001912 startc = collclass; // allow [.a.]-x as a range
1913 // Will emit the proper atom at the end of the
1914 // while loop.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 }
1916 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001917 // Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1918 // start character.
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001919 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001920 {
1921 emit_range = TRUE;
1922 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001923 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001924 continue; // reading the end of the range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 }
1926
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001927 // Now handle simple and escaped characters.
1928 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1929 // accepts "\t", "\e", etc., but only when the 'l' flag in
1930 // 'cpoptions' is not included.
1931 // Posix doesn't recognize backslash at all.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001932 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001933 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 && regparse + 1 <= endp
1935 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001936 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001937 && vim_strchr(REGEXP_ABBR, regparse[1])
1938 != NULL)
1939 )
1940 )
1941 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001942 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001944 if (*regparse == 'n')
Bram Moolenaara5483442019-02-17 20:17:02 +01001945 startc = (reg_string || emit_range
1946 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001947 else if (*regparse == 'd'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 || *regparse == 'o'
1949 || *regparse == 'x'
1950 || *regparse == 'u'
1951 || *regparse == 'U'
1952 )
1953 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001954 // TODO(RE) This needs more testing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001955 startc = coll_get_char();
1956 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001957 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 }
1959 else
1960 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001961 // \r,\t,\e,\b
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 startc = backslash_trans(*regparse);
1963 }
1964 }
1965
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001966 // Normal printable char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001968 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001969
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001970 // Previous char was '-', so this char is end of range.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001971 if (emit_range)
1972 {
=?UTF-8?q?Dundar=20G=C3=B6c?=09f688c2021-07-08 18:05:00 +02001973 int endc = startc;
1974
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001975 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976 if (startc > endc)
Bram Moolenaar677658a2022-01-05 16:09:06 +00001977 EMSG_RET_FAIL(_(e_reverse_range_in_character_class));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001978
1979 if (endc > startc + 2)
1980 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001981 // Emit a range instead of the sequence of
1982 // individual characters.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001983 if (startc == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001984 // \x00 is translated to \x0a, start at \x01.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001985 EMIT(1);
1986 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001987 --post_ptr; // remove NFA_CONCAT
Bram Moolenaar417bad22013-06-07 14:08:30 +02001988 EMIT(endc);
1989 EMIT(NFA_RANGE);
1990 EMIT(NFA_CONCAT);
1991 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001992 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001993 || (*mb_char2len)(endc) > 1))
1994 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001995 // Emit the characters in the range.
1996 // "startc" was already emitted, so skip it.
1997 //
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001998 for (c = startc + 1; c <= endc; c++)
1999 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002000 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02002001 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003 }
2004 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002005 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002006 // Emit the range. "startc" was already emitted, so
2007 // skip it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 for (c = startc + 1; c <= endc; c++)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002009 {
2010 EMIT(c);
2011 EMIT(NFA_CONCAT);
2012 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002013 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02002014 emit_range = FALSE;
2015 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016 }
2017 else
2018 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002019 // This char (startc) is not part of a range. Just
2020 // emit it.
2021 // Normally, simply emit startc. But if we get char
2022 // code=0 from a collating char, then replace it with
2023 // 0x0a.
2024 // This is needed to completely mimic the behaviour of
2025 // the backtracking engine.
Bram Moolenaar417bad22013-06-07 14:08:30 +02002026 if (startc == NFA_NEWL)
2027 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002028 // Line break can't be matched as part of the
2029 // collection, add an OR below. But not for negated
2030 // range.
Bram Moolenaar417bad22013-06-07 14:08:30 +02002031 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002032 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002033 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02002035 {
2036 if (got_coll_char == TRUE && startc == 0)
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01002037 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02002038 EMIT(0x0a);
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01002039 EMIT(NFA_CONCAT);
2040 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02002041 else
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01002042 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02002043 EMIT(startc);
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01002044 if (!(enc_utf8 && (utf_ptr2len(regparse) != (plen = utfc_ptr2len(regparse)))))
2045 {
2046 EMIT(NFA_CONCAT);
2047 }
2048 }
Christian Brabandtca22fc32023-08-20 20:34:22 +02002049 }
Christian Brabandtca22fc32023-08-20 20:34:22 +02002050 }
Christian Brabandtbe07caa2023-08-20 22:26:15 +02002051
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01002052 if (enc_utf8 && (utf_ptr2len(regparse) != (plen = utfc_ptr2len(regparse))))
2053 {
2054 int i = utf_ptr2len(regparse);
2055
2056 c = utf_ptr2char(regparse + i);
2057
2058 // Add composing characters
2059 for (;;)
2060 {
2061 if (c == 0)
2062 // \x00 is translated to \x0a, start at \x01.
2063 EMIT(1);
2064 else
2065 EMIT(c);
2066 EMIT(NFA_CONCAT);
2067 if ((i += utf_char2len(c)) >= plen)
2068 break;
2069 c = utf_ptr2char(regparse + i);
2070 }
2071 EMIT(NFA_COMPOSING);
2072 EMIT(NFA_CONCAT);
2073 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002074 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002075 } // while (p < endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002076
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002077 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002078 if (*regparse == '-') // if last, '-' is just a char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002079 {
2080 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02002081 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002084 // skip the trailing ]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002086 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02002087
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002088 // Mark end of the collection.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002089 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02002090 EMIT(NFA_END_NEG_COLL);
2091 else
2092 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02002093
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002094 // \_[] also matches \n but it's not negated
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002095 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02002096 {
2097 EMIT(reg_string ? NL : NFA_NEWL);
2098 EMIT(NFA_OR);
2099 }
2100
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002101 return OK;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002102 } // if exists closing ]
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002103
2104 if (reg_strict)
Bram Moolenaar677658a2022-01-05 16:09:06 +00002105 EMSG_RET_FAIL(_(e_missing_rsb_after_str_lsb));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002106 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002107
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002108 default:
2109 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002110 int plen;
2111
2112nfa_do_multibyte:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002113 // plen is length of current char with composing chars
Bram Moolenaar47196582013-05-25 22:04:23 +02002114 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02002115 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02002116 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002117 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02002118 int i = 0;
2119
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002120 // A base character plus composing characters, or just one
2121 // or more composing characters.
2122 // This requires creating a separate atom as if enclosing
2123 // the characters in (), where NFA_COMPOSING is the ( and
2124 // NFA_END_COMPOSING is the ). Note that right now we are
2125 // building the postfix form, not the NFA itself;
2126 // a composing char could be: a, b, c, NFA_COMPOSING
2127 // where 'b' and 'c' are chars with codes > 256.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002128 for (;;)
2129 {
2130 EMIT(c);
2131 if (i > 0)
2132 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002133 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002134 break;
2135 c = utf_ptr2char(old_regparse + i);
2136 }
2137 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002138 regparse = old_regparse + plen;
2139 }
2140 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002141 {
2142 c = no_Magic(c);
2143 EMIT(c);
2144 }
2145 return OK;
2146 }
2147 }
2148
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002149 return OK;
2150}
2151
2152/*
2153 * Parse something followed by possible [*+=].
2154 *
2155 * A piece is an atom, possibly followed by a multi, an indication of how many
2156 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2157 * characters: "", "a", "aa", etc.
2158 *
2159 * piece ::= atom
2160 * or atom multi
2161 */
2162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002163nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164{
2165 int i;
2166 int op;
2167 int ret;
2168 long minval, maxval;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002169 int greedy = TRUE; // Braces are prefixed with '-' ?
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002170 parse_state_T old_state;
2171 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002172 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002173 int old_post_pos;
2174 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002175 int quest;
2176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002177 // Save the current parse state, so that we can use it if <atom>{m,n} is
2178 // next.
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002179 save_parse_state(&old_state);
2180
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002181 // store current pos in the postfix form, for \{m,n} involving 0s
Bram Moolenaar16299b52013-05-30 18:45:23 +02002182 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002183
2184 ret = nfa_regatom();
2185 if (ret == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002186 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187
2188 op = peekchr();
2189 if (re_multi_type(op) == NOT_MULTI)
2190 return OK;
2191
2192 skipchr();
2193 switch (op)
2194 {
2195 case Magic('*'):
2196 EMIT(NFA_STAR);
2197 break;
2198
2199 case Magic('+'):
2200 /*
2201 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2202 * first and only submatch would be "aaa". But the backtracking
2203 * engine interprets the plus as "try matching one more time", and
2204 * a* matches a second time at the end of the input, the empty
2205 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002206 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002208 * In order to be consistent with the old engine, we replace
2209 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002210 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002211 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002212 curchr = -1;
2213 if (nfa_regatom() == FAIL)
2214 return FAIL;
2215 EMIT(NFA_STAR);
2216 EMIT(NFA_CONCAT);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002217 skipchr(); // skip the \+
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 break;
2219
2220 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002221 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002223 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 switch(op)
2225 {
2226 case '=':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002227 // \@=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002228 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002230 case '!':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002231 // \@!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002232 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002233 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002235 op = no_Magic(getchr());
2236 if (op == '=')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002237 // \@<=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002238 i = NFA_PREV_ATOM_JUST_BEFORE;
2239 else if (op == '!')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002240 // \@<!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002241 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2242 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002243 case '>':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002244 // \@>
Bram Moolenaar87953742013-06-05 18:52:40 +02002245 i = NFA_PREV_ATOM_LIKE_PATTERN;
2246 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002247 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002248 if (i == 0)
2249 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002250 semsg(_(e_nfa_regexp_unknown_operator_at_chr), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002251 return FAIL;
2252 }
2253 EMIT(i);
2254 if (i == NFA_PREV_ATOM_JUST_BEFORE
2255 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2256 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 break;
2258
2259 case Magic('?'):
2260 case Magic('='):
2261 EMIT(NFA_QUEST);
2262 break;
2263
2264 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002265 // a{2,5} will expand to 'aaa?a?a?'
2266 // a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2267 // version of '?'
2268 // \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2269 // parenthesis have the same id
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002270
2271 greedy = TRUE;
2272 c2 = peekchr();
2273 if (c2 == '-' || c2 == Magic('-'))
2274 {
2275 skipchr();
2276 greedy = FALSE;
2277 }
2278 if (!read_limits(&minval, &maxval))
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002279 EMSG_RET_FAIL(_(e_nfa_regexp_error_reading_repetition_limits));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002280
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002281 // <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2282 // <atom>*
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002283 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002284 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002285 if (greedy) // { { (match the braces)
2286 // \{}, \{0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002287 EMIT(NFA_STAR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002288 else // { { (match the braces)
2289 // \{-}, \{-0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002290 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002291 break;
2292 }
2293
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002294 // Special case: x{0} or x{-0}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002295 if (maxval == 0)
2296 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002297 // Ignore result of previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002298 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002299 // NFA_EMPTY is 0-length and works everywhere
Bram Moolenaar699c1202013-09-25 16:41:54 +02002300 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002301 return OK;
2302 }
2303
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002304 // The engine is very inefficient (uses too many states) when the
2305 // maximum is much larger than the minimum and when the maximum is
Bram Moolenaar66c50c52021-01-02 17:43:49 +01002306 // large. However, when maxval is MAX_LIMIT, it is okay, as this
2307 // will emit NFA_STAR.
2308 // Bail out if we can use the other engine, but only, when the
2309 // pattern does not need the NFA engine like (e.g. [[:upper:]]\{2,\}
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002310 // does not work with characters > 8 bit with the BT engine)
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002311 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaar66c50c52021-01-02 17:43:49 +01002312 && (maxval > 500 || maxval > minval + 200)
2313 && (maxval != MAX_LIMIT && minval < 200)
2314 && !wants_nfa)
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002315 return FAIL;
2316
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002317 // Ignore previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002318 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002319 // Save parse state after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002320 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002321
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2323 for (i = 0; i < maxval; i++)
2324 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002325 // Goto beginning of the repeated atom
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002326 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002327 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328 if (nfa_regatom() == FAIL)
2329 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002330 // after "minval" times, atoms are optional
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002332 {
2333 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002334 {
2335 if (greedy)
2336 EMIT(NFA_STAR);
2337 else
2338 EMIT(NFA_STAR_NONGREEDY);
2339 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002340 else
2341 EMIT(quest);
2342 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002343 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002344 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002345 if (i + 1 > minval && maxval == MAX_LIMIT)
2346 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002347 }
2348
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002349 // Go to just after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002350 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002351 curchr = -1;
2352
2353 break;
2354
2355
2356 default:
2357 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002358 } // end switch
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002359
2360 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002361 // Can't have a multi follow a multi.
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002362 EMSG_RET_FAIL(_(e_nfa_regexp_cant_have_multi_follow_multi));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002363
2364 return OK;
2365}
2366
2367/*
2368 * Parse one or more pieces, concatenated. It matches a match for the
2369 * first piece, followed by a match for the second piece, etc. Example:
2370 * "f[0-9]b", first matches "f", then a digit and then "b".
2371 *
2372 * concat ::= piece
2373 * or piece piece
2374 * or piece piece piece
2375 * etc.
2376 */
2377 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002378nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379{
2380 int cont = TRUE;
2381 int first = TRUE;
2382
2383 while (cont)
2384 {
2385 switch (peekchr())
2386 {
2387 case NUL:
2388 case Magic('|'):
2389 case Magic('&'):
2390 case Magic(')'):
2391 cont = FALSE;
2392 break;
2393
2394 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002395 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396 skipchr_keepstart();
2397 break;
2398 case Magic('c'):
2399 regflags |= RF_ICASE;
2400 skipchr_keepstart();
2401 break;
2402 case Magic('C'):
2403 regflags |= RF_NOICASE;
2404 skipchr_keepstart();
2405 break;
2406 case Magic('v'):
2407 reg_magic = MAGIC_ALL;
2408 skipchr_keepstart();
2409 curchr = -1;
2410 break;
2411 case Magic('m'):
2412 reg_magic = MAGIC_ON;
2413 skipchr_keepstart();
2414 curchr = -1;
2415 break;
2416 case Magic('M'):
2417 reg_magic = MAGIC_OFF;
2418 skipchr_keepstart();
2419 curchr = -1;
2420 break;
2421 case Magic('V'):
2422 reg_magic = MAGIC_NONE;
2423 skipchr_keepstart();
2424 curchr = -1;
2425 break;
2426
2427 default:
2428 if (nfa_regpiece() == FAIL)
2429 return FAIL;
2430 if (first == FALSE)
2431 EMIT(NFA_CONCAT);
2432 else
2433 first = FALSE;
2434 break;
2435 }
2436 }
2437
2438 return OK;
2439}
2440
2441/*
2442 * Parse a branch, one or more concats, separated by "\&". It matches the
2443 * last concat, but only if all the preceding concats also match at the same
2444 * position. Examples:
2445 * "foobeep\&..." matches "foo" in "foobeep".
2446 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2447 *
2448 * branch ::= concat
2449 * or concat \& concat
2450 * or concat \& concat \& concat
2451 * etc.
2452 */
2453 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002454nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002455{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002456 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002457
Bram Moolenaar16299b52013-05-30 18:45:23 +02002458 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002460 // First branch, possibly the only one
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002461 if (nfa_regconcat() == FAIL)
2462 return FAIL;
2463
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002464 // Try next concats
Bram Moolenaar890dd052017-12-16 19:59:37 +01002465 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466 {
2467 skipchr();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002468 // if concat is empty do emit a node
Bram Moolenaar890dd052017-12-16 19:59:37 +01002469 if (old_post_pos == (int)(post_ptr - post_start))
2470 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 EMIT(NFA_NOPEN);
2472 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002473 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002474 if (nfa_regconcat() == FAIL)
2475 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002476 // if concat is empty do emit a node
Bram Moolenaar16299b52013-05-30 18:45:23 +02002477 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002478 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002480 }
2481
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002482 // if a branch is empty, emit one node for it
Bram Moolenaar16299b52013-05-30 18:45:23 +02002483 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002484 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002485
2486 return OK;
2487}
2488
2489/*
2490 * Parse a pattern, one or more branches, separated by "\|". It matches
2491 * anything that matches one of the branches. Example: "foo\|beep" matches
2492 * "foo" and matches "beep". If more than one branch matches, the first one
2493 * is used.
2494 *
2495 * pattern ::= branch
2496 * or branch \| branch
2497 * or branch \| branch \| branch
2498 * etc.
2499 */
2500 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002501nfa_reg(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002502 int paren) // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503{
2504 int parno = 0;
2505
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506 if (paren == REG_PAREN)
2507 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002508 if (regnpar >= NSUBEXP) // Too many `('
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002509 EMSG_RET_FAIL(_(e_nfa_regexp_too_many_parens));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002510 parno = regnpar++;
2511 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002512#ifdef FEAT_SYN_HL
2513 else if (paren == REG_ZPAREN)
2514 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002515 // Make a ZOPEN node.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002516 if (regnzpar >= NSUBEXP)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002517 EMSG_RET_FAIL(_(e_nfa_regexp_too_many_z));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002518 parno = regnzpar++;
2519 }
2520#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521
2522 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002523 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002524
2525 while (peekchr() == Magic('|'))
2526 {
2527 skipchr();
2528 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002529 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530 EMIT(NFA_OR);
2531 }
2532
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002533 // Check for proper termination.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002534 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2535 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002536 if (paren == REG_NPAREN)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002537 EMSG2_RET_FAIL(_(e_unmatched_str_percent_open),
2538 reg_magic == MAGIC_ALL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 else
Bram Moolenaard8e44472021-07-21 22:20:33 +02002540 EMSG2_RET_FAIL(_(e_unmatched_str_open), reg_magic == MAGIC_ALL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541 }
2542 else if (paren == REG_NOPAREN && peekchr() != NUL)
2543 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002544 if (peekchr() == Magic(')'))
Bram Moolenaard8e44472021-07-21 22:20:33 +02002545 EMSG2_RET_FAIL(_(e_unmatched_str_close), reg_magic == MAGIC_ALL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002546 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002547 EMSG_RET_FAIL(_(e_nfa_regexp_proper_termination_error));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 }
2549 /*
2550 * Here we set the flag allowing back references to this set of
2551 * parentheses.
2552 */
2553 if (paren == REG_PAREN)
2554 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002555 had_endbrace[parno] = TRUE; // have seen the close paren
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556 EMIT(NFA_MOPEN + parno);
2557 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002558#ifdef FEAT_SYN_HL
2559 else if (paren == REG_ZPAREN)
2560 EMIT(NFA_ZOPEN + parno);
2561#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562
2563 return OK;
2564}
2565
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002566#ifdef DEBUG
2567static char_u code[50];
2568
2569 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002570nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002571{
2572 int addnl = FALSE;
2573
2574 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2575 {
2576 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002577 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002578 }
2579
2580 STRCPY(code, "");
2581 switch (c)
2582 {
2583 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2584 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2585 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2586 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2587 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2588 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2589
Bram Moolenaar5714b802013-05-28 22:03:20 +02002590 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2591 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2592 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2593 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2594 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2595 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2596 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2597 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2598 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002599#ifdef FEAT_SYN_HL
2600 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2601 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2602 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2603 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2604 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2605 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2606 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2607 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2608 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2609#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002610 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2611
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002612 case NFA_PREV_ATOM_NO_WIDTH:
2613 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002614 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2615 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002616 case NFA_PREV_ATOM_JUST_BEFORE:
2617 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2618 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2619 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002620 case NFA_PREV_ATOM_LIKE_PATTERN:
2621 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2622
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002623 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2624 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002625 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002626 case NFA_START_INVISIBLE_FIRST:
2627 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002628 case NFA_START_INVISIBLE_NEG:
2629 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002630 case NFA_START_INVISIBLE_NEG_FIRST:
2631 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002632 case NFA_START_INVISIBLE_BEFORE:
2633 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002634 case NFA_START_INVISIBLE_BEFORE_FIRST:
2635 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002636 case NFA_START_INVISIBLE_BEFORE_NEG:
2637 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002638 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2639 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002640 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002642 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002643 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002644
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002645 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2646 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002647 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002649 case NFA_MOPEN:
2650 case NFA_MOPEN1:
2651 case NFA_MOPEN2:
2652 case NFA_MOPEN3:
2653 case NFA_MOPEN4:
2654 case NFA_MOPEN5:
2655 case NFA_MOPEN6:
2656 case NFA_MOPEN7:
2657 case NFA_MOPEN8:
2658 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002659 STRCPY(code, "NFA_MOPEN(x)");
2660 code[10] = c - NFA_MOPEN + '0';
2661 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002662 case NFA_MCLOSE:
2663 case NFA_MCLOSE1:
2664 case NFA_MCLOSE2:
2665 case NFA_MCLOSE3:
2666 case NFA_MCLOSE4:
2667 case NFA_MCLOSE5:
2668 case NFA_MCLOSE6:
2669 case NFA_MCLOSE7:
2670 case NFA_MCLOSE8:
2671 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002672 STRCPY(code, "NFA_MCLOSE(x)");
2673 code[11] = c - NFA_MCLOSE + '0';
2674 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002675#ifdef FEAT_SYN_HL
2676 case NFA_ZOPEN:
2677 case NFA_ZOPEN1:
2678 case NFA_ZOPEN2:
2679 case NFA_ZOPEN3:
2680 case NFA_ZOPEN4:
2681 case NFA_ZOPEN5:
2682 case NFA_ZOPEN6:
2683 case NFA_ZOPEN7:
2684 case NFA_ZOPEN8:
2685 case NFA_ZOPEN9:
2686 STRCPY(code, "NFA_ZOPEN(x)");
2687 code[10] = c - NFA_ZOPEN + '0';
2688 break;
2689 case NFA_ZCLOSE:
2690 case NFA_ZCLOSE1:
2691 case NFA_ZCLOSE2:
2692 case NFA_ZCLOSE3:
2693 case NFA_ZCLOSE4:
2694 case NFA_ZCLOSE5:
2695 case NFA_ZCLOSE6:
2696 case NFA_ZCLOSE7:
2697 case NFA_ZCLOSE8:
2698 case NFA_ZCLOSE9:
2699 STRCPY(code, "NFA_ZCLOSE(x)");
2700 code[11] = c - NFA_ZCLOSE + '0';
2701 break;
2702#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002703 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2704 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2705 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2706 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002707 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2708 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002709 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2710 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2711 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2712 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2713 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2714 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2715 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2716 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2717 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2718 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2719 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2720 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2721 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2722 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002723 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002724
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002726 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2727 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2728 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002729 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002731
2732 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2733 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2734 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2735 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2736 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2737 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2738 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2739
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002740 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2741 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2742 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2743 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2744 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2745 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2746 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2747 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2748 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2749 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2750 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2751 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2752 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2753 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2754 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2755 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002756 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2757 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2758 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002759
2760 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2761 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2762 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2763 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2764 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2765 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2766 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2767 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2768 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2769 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2770 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2771 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2772 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2773 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2774 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2775 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2776 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2777 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2778 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2779 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2780 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2781 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2782 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2783 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2784 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2785 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2786 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002787 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2788 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2789 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2790 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791
2792 default:
2793 STRCPY(code, "CHAR(x)");
2794 code[5] = c;
2795 }
2796
2797 if (addnl == TRUE)
2798 STRCAT(code, " + NEWLINE ");
2799
2800}
2801
2802#ifdef ENABLE_LOG
2803static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002804static 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 +02002805
2806/*
2807 * Print the postfix notation of the current regexp.
2808 */
2809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002810nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811{
2812 int *p;
2813 FILE *f;
2814
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002815 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002816 if (f == NULL)
2817 return;
2818
2819 fprintf(f, "\n-------------------------\n");
2820 if (retval == FAIL)
2821 fprintf(f, ">>> NFA engine failed... \n");
2822 else if (retval == OK)
2823 fprintf(f, ">>> NFA engine succeeded !\n");
2824 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
2825 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002827 nfa_set_code(*p);
2828 fprintf(f, "%s, ", code);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002829 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002830 fprintf(f, "\"\nPostfix notation (int): ");
2831 for (p = post_start; *p && p < post_ptr; p++)
2832 fprintf(f, "%d ", *p);
2833 fprintf(f, "\n\n");
2834 fclose(f);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835}
2836
2837/*
2838 * Print the NFA starting with a root node "state".
2839 */
2840 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002841nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002842{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002843 garray_T indent;
2844
2845 ga_init2(&indent, 1, 64);
2846 ga_append(&indent, '\0');
2847 nfa_print_state2(debugf, state, &indent);
2848 ga_clear(&indent);
2849}
2850
2851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002852nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002853{
2854 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855
2856 if (state == NULL)
2857 return;
2858
2859 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002860
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002861 // Output indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002862 p = (char_u *)indent->ga_data;
2863 if (indent->ga_len >= 3)
2864 {
2865 int last = indent->ga_len - 3;
2866 char_u save[2];
2867
2868 STRNCPY(save, &p[last], 2);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002869 memcpy(&p[last], "+-", 2);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002870 fprintf(debugf, " %s", p);
2871 STRNCPY(&p[last], save, 2);
2872 }
2873 else
2874 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875
2876 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002877 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002878 code,
2879 state->c,
2880 abs(state->id),
2881 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 if (state->id < 0)
2883 return;
2884
2885 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002886
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002887 // grow indent for state->out
Bram Moolenaar152e7892013-05-25 12:28:11 +02002888 indent->ga_len -= 1;
2889 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002890 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002891 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002892 ga_concat(indent, (char_u *)" ");
Yegappan Lakshmananbc404bf2021-12-19 19:19:31 +00002893 ga_append(indent, NUL);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002894
2895 nfa_print_state2(debugf, state->out, indent);
2896
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002897 // replace last part of indent for state->out1
Bram Moolenaar152e7892013-05-25 12:28:11 +02002898 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002899 ga_concat(indent, (char_u *)" ");
Yegappan Lakshmananbc404bf2021-12-19 19:19:31 +00002900 ga_append(indent, NUL);
Bram Moolenaar152e7892013-05-25 12:28:11 +02002901
2902 nfa_print_state2(debugf, state->out1, indent);
2903
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002904 // shrink indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002905 indent->ga_len -= 3;
Yegappan Lakshmananbc404bf2021-12-19 19:19:31 +00002906 ga_append(indent, NUL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002907}
2908
2909/*
2910 * Print the NFA state machine.
2911 */
2912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002913nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002915 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002917 if (debugf == NULL)
2918 return;
Bram Moolenaard89616e2013-06-06 18:46:06 +02002919
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002920 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002921
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002922 if (prog->reganch)
2923 fprintf(debugf, "reganch: %d\n", prog->reganch);
2924 if (prog->regstart != NUL)
2925 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2926 prog->regstart, prog->regstart);
2927 if (prog->match_text != NULL)
2928 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
2929
2930 fclose(debugf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002932#endif // ENABLE_LOG
2933#endif // DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934
2935/*
2936 * Parse r.e. @expr and convert it into postfix form.
2937 * Return the postfix string on success, NULL otherwise.
2938 */
2939 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002940re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002941{
2942 if (nfa_reg(REG_NOPAREN) == FAIL)
2943 return NULL;
2944 EMIT(NFA_MOPEN);
2945 return post_start;
2946}
2947
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002948// NB. Some of the code below is inspired by Russ's.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002949
2950/*
2951 * Represents an NFA state plus zero or one or two arrows exiting.
2952 * if c == MATCH, no arrows out; matching state.
2953 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2954 * If c < 256, labeled arrow with character c to out.
2955 */
2956
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002957static nfa_state_T *state_ptr; // points to nfa_prog->state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002958
2959/*
2960 * Allocate and initialize nfa_state_T.
2961 */
2962 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002963alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002964{
2965 nfa_state_T *s;
2966
2967 if (istate >= nstate)
2968 return NULL;
2969
2970 s = &state_ptr[istate++];
2971
2972 s->c = c;
2973 s->out = out;
2974 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002975 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976
2977 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002978 s->lastlist[0] = 0;
2979 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002980
2981 return s;
2982}
2983
2984/*
2985 * A partially built NFA without the matching state filled in.
2986 * Frag_T.start points at the start state.
2987 * Frag_T.out is a list of places that need to be set to the
2988 * next state for this fragment.
2989 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002990
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002991// Since the out pointers in the list are always
2992// uninitialized, we use the pointers themselves
2993// as storage for the Ptrlists.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002994typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002995union Ptrlist
2996{
2997 Ptrlist *next;
2998 nfa_state_T *s;
2999};
3000
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003001struct Frag
3002{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02003003 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003004 Ptrlist *out;
3005};
3006typedef struct Frag Frag_T;
3007
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003008/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02003009 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003010 */
3011 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003012frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003013{
Bram Moolenaar053bb602013-05-20 13:55:21 +02003014 Frag_T n;
3015
3016 n.start = start;
3017 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003018 return n;
3019}
3020
3021/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003022 * Create singleton list containing just outp.
3023 */
3024 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01003025list1(
3026 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003027{
3028 Ptrlist *l;
3029
3030 l = (Ptrlist *)outp;
3031 l->next = NULL;
3032 return l;
3033}
3034
3035/*
3036 * Patch the list of states at out to point to start.
3037 */
3038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003039patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003040{
3041 Ptrlist *next;
3042
3043 for (; l; l = next)
3044 {
3045 next = l->next;
3046 l->s = s;
3047 }
3048}
3049
3050
3051/*
3052 * Join the two lists l1 and l2, returning the combination.
3053 */
3054 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01003055append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003056{
3057 Ptrlist *oldl1;
3058
3059 oldl1 = l1;
3060 while (l1->next)
3061 l1 = l1->next;
3062 l1->next = l2;
3063 return oldl1;
3064}
3065
3066/*
3067 * Stack used for transforming postfix form into NFA.
3068 */
3069static Frag_T empty;
3070
3071 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003072st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003073{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003074#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003075 FILE *df;
3076 int *p2;
3077
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003078 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003079 if (df)
3080 {
3081 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02003082# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003083 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02003084# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003085 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02003086# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003087 for (p2 = postfix; p2 < end; p2++)
3088 {
3089 nfa_set_code(*p2);
3090 fprintf(df, "%s, ", code);
3091 }
3092 nfa_set_code(*p);
3093 fprintf(df, "\nCurrent position is: ");
3094 for (p2 = postfix; p2 <= p; p2 ++)
3095 {
3096 nfa_set_code(*p2);
3097 fprintf(df, "%s, ", code);
3098 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02003099# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003100 for (p2 = postfix; p2 < end; p2++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003101 fprintf(df, "%d, ", *p2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003102 fprintf(df, "\nCurrent position is: ");
3103 for (p2 = postfix; p2 <= p; p2 ++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003104 fprintf(df, "%d, ", *p2);
Bram Moolenaar0270f382018-07-17 05:43:58 +02003105# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003106 fprintf(df, "\n--------------------------\n");
3107 fclose(df);
3108 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003109#endif
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003110 emsg(_(e_nfa_regexp_could_not_pop_stack));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003111}
3112
3113/*
3114 * Push an item onto the stack.
3115 */
3116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003117st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118{
3119 Frag_T *stackp = *p;
3120
3121 if (stackp >= stack_end)
3122 return;
3123 *stackp = s;
3124 *p = *p + 1;
3125}
3126
3127/*
3128 * Pop an item from the stack.
3129 */
3130 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003131st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003132{
3133 Frag_T *stackp;
3134
3135 *p = *p - 1;
3136 stackp = *p;
3137 if (stackp < stack)
3138 return empty;
3139 return **p;
3140}
3141
3142/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003143 * Estimate the maximum byte length of anything matching "state".
3144 * When unknown or unlimited return -1.
3145 */
3146 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003147nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003148{
3149 int l, r;
3150 nfa_state_T *state = startstate;
3151 int len = 0;
3152
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003153 // detect looping in a NFA_SPLIT
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003154 if (depth > 4)
3155 return -1;
3156
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003157 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003158 {
3159 switch (state->c)
3160 {
3161 case NFA_END_INVISIBLE:
3162 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003163 // the end, return what we have
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003164 return len;
3165
3166 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003167 // two alternatives, use the maximum
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003168 l = nfa_max_width(state->out, depth + 1);
3169 r = nfa_max_width(state->out1, depth + 1);
3170 if (l < 0 || r < 0)
3171 return -1;
3172 return len + (l > r ? l : r);
3173
3174 case NFA_ANY:
3175 case NFA_START_COLL:
3176 case NFA_START_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003177 // matches some character, including composing chars
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003178 if (enc_utf8)
3179 len += MB_MAXBYTES;
3180 else if (has_mbyte)
3181 len += 2;
3182 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003183 ++len;
3184 if (state->c != NFA_ANY)
3185 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003186 // skip over the characters
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003187 state = state->out1->out;
3188 continue;
3189 }
3190 break;
3191
3192 case NFA_DIGIT:
3193 case NFA_WHITE:
3194 case NFA_HEX:
3195 case NFA_OCTAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003196 // ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003197 ++len;
3198 break;
3199
3200 case NFA_IDENT:
3201 case NFA_SIDENT:
3202 case NFA_KWORD:
3203 case NFA_SKWORD:
3204 case NFA_FNAME:
3205 case NFA_SFNAME:
3206 case NFA_PRINT:
3207 case NFA_SPRINT:
3208 case NFA_NWHITE:
3209 case NFA_NDIGIT:
3210 case NFA_NHEX:
3211 case NFA_NOCTAL:
3212 case NFA_WORD:
3213 case NFA_NWORD:
3214 case NFA_HEAD:
3215 case NFA_NHEAD:
3216 case NFA_ALPHA:
3217 case NFA_NALPHA:
3218 case NFA_LOWER:
3219 case NFA_NLOWER:
3220 case NFA_UPPER:
3221 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003222 case NFA_LOWER_IC:
3223 case NFA_NLOWER_IC:
3224 case NFA_UPPER_IC:
3225 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003226 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003227 // possibly non-ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003228 if (has_mbyte)
3229 len += 3;
3230 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003231 ++len;
3232 break;
3233
3234 case NFA_START_INVISIBLE:
3235 case NFA_START_INVISIBLE_NEG:
3236 case NFA_START_INVISIBLE_BEFORE:
3237 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003238 // zero-width, out1 points to the END state
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003239 state = state->out1->out;
3240 continue;
3241
3242 case NFA_BACKREF1:
3243 case NFA_BACKREF2:
3244 case NFA_BACKREF3:
3245 case NFA_BACKREF4:
3246 case NFA_BACKREF5:
3247 case NFA_BACKREF6:
3248 case NFA_BACKREF7:
3249 case NFA_BACKREF8:
3250 case NFA_BACKREF9:
3251#ifdef FEAT_SYN_HL
3252 case NFA_ZREF1:
3253 case NFA_ZREF2:
3254 case NFA_ZREF3:
3255 case NFA_ZREF4:
3256 case NFA_ZREF5:
3257 case NFA_ZREF6:
3258 case NFA_ZREF7:
3259 case NFA_ZREF8:
3260 case NFA_ZREF9:
3261#endif
3262 case NFA_NEWL:
3263 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003264 // unknown width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003265 return -1;
3266
3267 case NFA_BOL:
3268 case NFA_EOL:
3269 case NFA_BOF:
3270 case NFA_EOF:
3271 case NFA_BOW:
3272 case NFA_EOW:
3273 case NFA_MOPEN:
3274 case NFA_MOPEN1:
3275 case NFA_MOPEN2:
3276 case NFA_MOPEN3:
3277 case NFA_MOPEN4:
3278 case NFA_MOPEN5:
3279 case NFA_MOPEN6:
3280 case NFA_MOPEN7:
3281 case NFA_MOPEN8:
3282 case NFA_MOPEN9:
3283#ifdef FEAT_SYN_HL
3284 case NFA_ZOPEN:
3285 case NFA_ZOPEN1:
3286 case NFA_ZOPEN2:
3287 case NFA_ZOPEN3:
3288 case NFA_ZOPEN4:
3289 case NFA_ZOPEN5:
3290 case NFA_ZOPEN6:
3291 case NFA_ZOPEN7:
3292 case NFA_ZOPEN8:
3293 case NFA_ZOPEN9:
3294 case NFA_ZCLOSE:
3295 case NFA_ZCLOSE1:
3296 case NFA_ZCLOSE2:
3297 case NFA_ZCLOSE3:
3298 case NFA_ZCLOSE4:
3299 case NFA_ZCLOSE5:
3300 case NFA_ZCLOSE6:
3301 case NFA_ZCLOSE7:
3302 case NFA_ZCLOSE8:
3303 case NFA_ZCLOSE9:
3304#endif
3305 case NFA_MCLOSE:
3306 case NFA_MCLOSE1:
3307 case NFA_MCLOSE2:
3308 case NFA_MCLOSE3:
3309 case NFA_MCLOSE4:
3310 case NFA_MCLOSE5:
3311 case NFA_MCLOSE6:
3312 case NFA_MCLOSE7:
3313 case NFA_MCLOSE8:
3314 case NFA_MCLOSE9:
3315 case NFA_NOPEN:
3316 case NFA_NCLOSE:
3317
3318 case NFA_LNUM_GT:
3319 case NFA_LNUM_LT:
3320 case NFA_COL_GT:
3321 case NFA_COL_LT:
3322 case NFA_VCOL_GT:
3323 case NFA_VCOL_LT:
3324 case NFA_MARK_GT:
3325 case NFA_MARK_LT:
3326 case NFA_VISUAL:
3327 case NFA_LNUM:
3328 case NFA_CURSOR:
3329 case NFA_COL:
3330 case NFA_VCOL:
3331 case NFA_MARK:
3332
3333 case NFA_ZSTART:
3334 case NFA_ZEND:
3335 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003336 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003337 case NFA_START_PATTERN:
3338 case NFA_END_PATTERN:
3339 case NFA_COMPOSING:
3340 case NFA_END_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003341 // zero-width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003342 break;
3343
3344 default:
3345 if (state->c < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003346 // don't know what this is
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003347 return -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003348 // normal character
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003349 len += MB_CHAR2LEN(state->c);
3350 break;
3351 }
3352
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003353 // normal way to continue
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003354 state = state->out;
3355 }
3356
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003357 // unrecognized, "cannot happen"
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003358 return -1;
3359}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003360
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003361/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 * Convert a postfix form into its equivalent NFA.
3363 * Return the NFA start state on success, NULL otherwise.
3364 */
3365 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003366post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003367{
3368 int *p;
3369 int mopen;
3370 int mclose;
3371 Frag_T *stack = NULL;
3372 Frag_T *stackp = NULL;
3373 Frag_T *stack_end = NULL;
3374 Frag_T e1;
3375 Frag_T e2;
3376 Frag_T e;
3377 nfa_state_T *s;
3378 nfa_state_T *s1;
3379 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003380 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003381
3382 if (postfix == NULL)
3383 return NULL;
3384
Bram Moolenaar053bb602013-05-20 13:55:21 +02003385#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386#define POP() st_pop(&stackp, stack); \
3387 if (stackp < stack) \
3388 { \
3389 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003390 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391 return NULL; \
3392 }
3393
3394 if (nfa_calc_size == FALSE)
3395 {
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003396 // Allocate space for the stack. Max states on the stack: "nstate".
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003397 stack = ALLOC_MULT(Frag_T, nstate + 1);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003398 if (stack == NULL)
3399 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003401 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402 }
3403
3404 for (p = postfix; p < end; ++p)
3405 {
3406 switch (*p)
3407 {
3408 case NFA_CONCAT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003409 // Concatenation.
3410 // Pay attention: this operator does not exist in the r.e. itself
3411 // (it is implicit, really). It is added when r.e. is translated
3412 // to postfix form in re2post().
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003413 if (nfa_calc_size == TRUE)
3414 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003415 // nstate += 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003416 break;
3417 }
3418 e2 = POP();
3419 e1 = POP();
3420 patch(e1.out, e2.start);
3421 PUSH(frag(e1.start, e2.out));
3422 break;
3423
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 case NFA_OR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003425 // Alternation
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003426 if (nfa_calc_size == TRUE)
3427 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003428 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 break;
3430 }
3431 e2 = POP();
3432 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003433 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003435 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003436 PUSH(frag(s, append(e1.out, e2.out)));
3437 break;
3438
3439 case NFA_STAR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003440 // Zero or more, prefer more
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003441 if (nfa_calc_size == TRUE)
3442 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003443 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 break;
3445 }
3446 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003447 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003448 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003449 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003450 patch(e.out, s);
3451 PUSH(frag(s, list1(&s->out1)));
3452 break;
3453
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003454 case NFA_STAR_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003455 // Zero or more, prefer zero
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003456 if (nfa_calc_size == TRUE)
3457 {
3458 nstate++;
3459 break;
3460 }
3461 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003462 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003463 if (s == NULL)
3464 goto theend;
3465 patch(e.out, s);
3466 PUSH(frag(s, list1(&s->out)));
3467 break;
3468
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 case NFA_QUEST:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003470 // one or zero atoms=> greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471 if (nfa_calc_size == TRUE)
3472 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003473 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 break;
3475 }
3476 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003477 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003478 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003479 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003480 PUSH(frag(s, append(e.out, list1(&s->out1))));
3481 break;
3482
3483 case NFA_QUEST_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003484 // zero or one atoms => non-greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 if (nfa_calc_size == TRUE)
3486 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003487 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003488 break;
3489 }
3490 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003491 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003492 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003493 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003494 PUSH(frag(s, append(e.out, list1(&s->out))));
3495 break;
3496
Bram Moolenaar417bad22013-06-07 14:08:30 +02003497 case NFA_END_COLL:
3498 case NFA_END_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003499 // On the stack is the sequence starting with NFA_START_COLL or
3500 // NFA_START_NEG_COLL and all possible characters. Patch it to
3501 // add the output to the start.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003502 if (nfa_calc_size == TRUE)
3503 {
3504 nstate++;
3505 break;
3506 }
3507 e = POP();
3508 s = alloc_state(NFA_END_COLL, NULL, NULL);
3509 if (s == NULL)
3510 goto theend;
3511 patch(e.out, s);
3512 e.start->out1 = s;
3513 PUSH(frag(e.start, list1(&s->out)));
3514 break;
3515
3516 case NFA_RANGE:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003517 // Before this are two characters, the low and high end of a
3518 // range. Turn them into two states with MIN and MAX.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003519 if (nfa_calc_size == TRUE)
3520 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003521 // nstate += 0;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003522 break;
3523 }
3524 e2 = POP();
3525 e1 = POP();
3526 e2.start->val = e2.start->c;
3527 e2.start->c = NFA_RANGE_MAX;
3528 e1.start->val = e1.start->c;
3529 e1.start->c = NFA_RANGE_MIN;
3530 patch(e1.out, e2.start);
3531 PUSH(frag(e1.start, e2.out));
3532 break;
3533
Bram Moolenaar699c1202013-09-25 16:41:54 +02003534 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003535 // 0-length, used in a repetition with max/min count of 0
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003536 if (nfa_calc_size == TRUE)
3537 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003538 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003539 break;
3540 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003541 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003543 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003544 PUSH(frag(s, list1(&s->out)));
3545 break;
3546
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003547 case NFA_OPT_CHARS:
3548 {
3549 int n;
3550
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003551 // \%[abc] implemented as:
3552 // NFA_SPLIT
3553 // +-CHAR(a)
3554 // | +-NFA_SPLIT
3555 // | +-CHAR(b)
3556 // | | +-NFA_SPLIT
3557 // | | +-CHAR(c)
3558 // | | | +-next
3559 // | | +- next
3560 // | +- next
3561 // +- next
3562 n = *++p; // get number of characters
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003563 if (nfa_calc_size == TRUE)
3564 {
3565 nstate += n;
3566 break;
3567 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003568 s = NULL; // avoid compiler warning
3569 e1.out = NULL; // stores list with out1's
3570 s1 = NULL; // previous NFA_SPLIT to connect to
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003571 while (n-- > 0)
3572 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003573 e = POP(); // get character
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003574 s = alloc_state(NFA_SPLIT, e.start, NULL);
3575 if (s == NULL)
3576 goto theend;
3577 if (e1.out == NULL)
3578 e1 = e;
3579 patch(e.out, s1);
3580 append(e1.out, list1(&s->out1));
3581 s1 = s;
3582 }
3583 PUSH(frag(s, e1.out));
3584 break;
3585 }
3586
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003587 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003588 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003589 case NFA_PREV_ATOM_JUST_BEFORE:
3590 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003591 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003592 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003593 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3594 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003595 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003596 int start_state;
3597 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003598 int n = 0;
3599 nfa_state_T *zend;
3600 nfa_state_T *skip;
3601
Bram Moolenaardecd9542013-06-07 16:31:50 +02003602 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003603 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003604 case NFA_PREV_ATOM_NO_WIDTH:
3605 start_state = NFA_START_INVISIBLE;
3606 end_state = NFA_END_INVISIBLE;
3607 break;
3608 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3609 start_state = NFA_START_INVISIBLE_NEG;
3610 end_state = NFA_END_INVISIBLE_NEG;
3611 break;
3612 case NFA_PREV_ATOM_JUST_BEFORE:
3613 start_state = NFA_START_INVISIBLE_BEFORE;
3614 end_state = NFA_END_INVISIBLE;
3615 break;
3616 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3617 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3618 end_state = NFA_END_INVISIBLE_NEG;
3619 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003620 default: // NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003621 start_state = NFA_START_PATTERN;
3622 end_state = NFA_END_PATTERN;
3623 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003624 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003625
3626 if (before)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003627 n = *++p; // get the count
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003628
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003629 // The \@= operator: match the preceding atom with zero width.
3630 // The \@! operator: no match for the preceding atom.
3631 // The \@<= operator: match for the preceding atom.
3632 // The \@<! operator: no match for the preceding atom.
3633 // Surrounds the preceding atom with START_INVISIBLE and
3634 // END_INVISIBLE, similarly to MOPEN.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635
3636 if (nfa_calc_size == TRUE)
3637 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003638 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 break;
3640 }
3641 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003642 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003644 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003645
Bram Moolenaar87953742013-06-05 18:52:40 +02003646 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003647 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003648 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003649 if (pattern)
3650 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003651 // NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02003652 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003653 if (skip == NULL)
3654 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003655 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003656 if (zend == NULL)
3657 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003658 s1->out= skip;
3659 patch(e.out, zend);
3660 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003661 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003662 else
3663 {
3664 patch(e.out, s1);
3665 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003666 if (before)
3667 {
3668 if (n <= 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003669 // See if we can guess the maximum width, it avoids a
3670 // lot of pointless tries.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003671 n = nfa_max_width(e.start, 0);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003672 s->val = n; // store the count
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003673 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003674 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003676 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003677
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003678 case NFA_COMPOSING: // char with composing char
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003679#if 0
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003680 // TODO
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003681 if (regflags & RF_ICOMBINE)
3682 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003683 // use the base character only
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003684 }
3685#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003686 // FALLTHROUGH
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003687
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003688 case NFA_MOPEN: // \( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003689 case NFA_MOPEN1:
3690 case NFA_MOPEN2:
3691 case NFA_MOPEN3:
3692 case NFA_MOPEN4:
3693 case NFA_MOPEN5:
3694 case NFA_MOPEN6:
3695 case NFA_MOPEN7:
3696 case NFA_MOPEN8:
3697 case NFA_MOPEN9:
3698#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003699 case NFA_ZOPEN: // \z( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003700 case NFA_ZOPEN1:
3701 case NFA_ZOPEN2:
3702 case NFA_ZOPEN3:
3703 case NFA_ZOPEN4:
3704 case NFA_ZOPEN5:
3705 case NFA_ZOPEN6:
3706 case NFA_ZOPEN7:
3707 case NFA_ZOPEN8:
3708 case NFA_ZOPEN9:
3709#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003710 case NFA_NOPEN: // \%( \) "Invisible Submatch"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003711 if (nfa_calc_size == TRUE)
3712 {
3713 nstate += 2;
3714 break;
3715 }
3716
3717 mopen = *p;
3718 switch (*p)
3719 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003720 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3721#ifdef FEAT_SYN_HL
3722 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3723 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3724 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3725 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3726 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3727 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3728 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3729 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3730 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3731 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3732#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003733 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003734 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003735 // NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 mclose = *p + NSUBEXP;
3737 break;
3738 }
3739
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003740 // Allow "NFA_MOPEN" as a valid postfix representation for
3741 // the empty regexp "". In this case, the NFA will be
3742 // NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3743 // empty groups of parenthesis, and empty mbyte chars
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003744 if (stackp == stack)
3745 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003746 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003748 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003749 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003751 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752 patch(list1(&s->out), s1);
3753 PUSH(frag(s, list1(&s1->out)));
3754 break;
3755 }
3756
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003757 // At least one node was emitted before NFA_MOPEN, so
3758 // at least one node will be between NFA_MOPEN and NFA_MCLOSE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 e = POP();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003760 s = alloc_state(mopen, e.start, NULL); // `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003761 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003762 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003763
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003764 s1 = alloc_state(mclose, NULL, NULL); // `)'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003765 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003766 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767 patch(e.out, s1);
3768
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003769 if (mopen == NFA_COMPOSING)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003770 // COMPOSING->out1 = END_COMPOSING
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003771 patch(list1(&s->out1), s1);
3772
3773 PUSH(frag(s, list1(&s1->out)));
3774 break;
3775
Bram Moolenaar5714b802013-05-28 22:03:20 +02003776 case NFA_BACKREF1:
3777 case NFA_BACKREF2:
3778 case NFA_BACKREF3:
3779 case NFA_BACKREF4:
3780 case NFA_BACKREF5:
3781 case NFA_BACKREF6:
3782 case NFA_BACKREF7:
3783 case NFA_BACKREF8:
3784 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003785#ifdef FEAT_SYN_HL
3786 case NFA_ZREF1:
3787 case NFA_ZREF2:
3788 case NFA_ZREF3:
3789 case NFA_ZREF4:
3790 case NFA_ZREF5:
3791 case NFA_ZREF6:
3792 case NFA_ZREF7:
3793 case NFA_ZREF8:
3794 case NFA_ZREF9:
3795#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003796 if (nfa_calc_size == TRUE)
3797 {
3798 nstate += 2;
3799 break;
3800 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003801 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003802 if (s == NULL)
3803 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003804 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003805 if (s1 == NULL)
3806 goto theend;
3807 patch(list1(&s->out), s1);
3808 PUSH(frag(s, list1(&s1->out)));
3809 break;
3810
Bram Moolenaar423532e2013-05-29 21:14:42 +02003811 case NFA_LNUM:
3812 case NFA_LNUM_GT:
3813 case NFA_LNUM_LT:
3814 case NFA_VCOL:
3815 case NFA_VCOL_GT:
3816 case NFA_VCOL_LT:
3817 case NFA_COL:
3818 case NFA_COL_GT:
3819 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003820 case NFA_MARK:
3821 case NFA_MARK_GT:
3822 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003823 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003824 int n = *++p; // lnum, col or mark name
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003825
Bram Moolenaar423532e2013-05-29 21:14:42 +02003826 if (nfa_calc_size == TRUE)
3827 {
3828 nstate += 1;
3829 break;
3830 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003831 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003832 if (s == NULL)
3833 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003834 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003835 PUSH(frag(s, list1(&s->out)));
3836 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003837 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003838
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003839 case NFA_ZSTART:
3840 case NFA_ZEND:
3841 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003842 // Operands
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003843 if (nfa_calc_size == TRUE)
3844 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003845 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846 break;
3847 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003848 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003849 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003850 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003851 PUSH(frag(s, list1(&s->out)));
3852 break;
3853
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003854 } // switch(*p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003855
Bram Moolenaarc9471b12023-05-09 15:00:00 +01003856 } // for (p = postfix; *p; ++p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003857
3858 if (nfa_calc_size == TRUE)
3859 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003860 nstate++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003861 goto theend; // Return value when counting size is ignored anyway
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003862 }
3863
3864 e = POP();
3865 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003866 {
3867 vim_free(stack);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003868 EMSG_RET_NULL(_(e_nfa_regexp_while_converting_from_postfix_to_nfa_too_many_stats_left_on_stack));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003869 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003870
3871 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003872 {
3873 vim_free(stack);
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003874 EMSG_RET_NULL(_(e_nfa_regexp_not_enough_space_to_store_whole_nfa));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003875 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003876
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003877 matchstate = &state_ptr[istate++]; // the match state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003878 matchstate->c = NFA_MATCH;
3879 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003880 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881
3882 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003883 ret = e.start;
3884
3885theend:
3886 vim_free(stack);
3887 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003888
3889#undef POP1
3890#undef PUSH1
3891#undef POP2
3892#undef PUSH2
3893#undef POP
3894#undef PUSH
3895}
3896
Bram Moolenaara2947e22013-06-11 22:44:09 +02003897/*
3898 * After building the NFA program, inspect it to add optimization hints.
3899 */
3900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003901nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003902{
3903 int i;
3904 int c;
3905
3906 for (i = 0; i < prog->nstate; ++i)
3907 {
3908 c = prog->state[i].c;
3909 if (c == NFA_START_INVISIBLE
3910 || c == NFA_START_INVISIBLE_NEG
3911 || c == NFA_START_INVISIBLE_BEFORE
3912 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3913 {
3914 int directly;
3915
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003916 // Do it directly when what follows is possibly the end of the
3917 // match.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003918 if (match_follows(prog->state[i].out1->out, 0))
3919 directly = TRUE;
3920 else
3921 {
3922 int ch_invisible = failure_chance(prog->state[i].out, 0);
3923 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3924
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003925 // Postpone when the invisible match is expensive or has a
3926 // lower chance of failing.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003927 if (c == NFA_START_INVISIBLE_BEFORE
3928 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3929 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003930 // "before" matches are very expensive when
3931 // unbounded, always prefer what follows then,
3932 // unless what follows will always match.
3933 // Otherwise strongly prefer what follows.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003934 if (prog->state[i].val <= 0 && ch_follows > 0)
3935 directly = FALSE;
3936 else
3937 directly = ch_follows * 10 < ch_invisible;
3938 }
3939 else
3940 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003941 // normal invisible, first do the one with the
3942 // highest failure chance
Bram Moolenaara2947e22013-06-11 22:44:09 +02003943 directly = ch_follows < ch_invisible;
3944 }
3945 }
3946 if (directly)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003947 // switch to the _FIRST state
Bram Moolenaara2947e22013-06-11 22:44:09 +02003948 ++prog->state[i].c;
3949 }
3950 }
3951}
3952
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003953/////////////////////////////////////////////////////////////////
3954// NFA execution code.
3955/////////////////////////////////////////////////////////////////
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003956
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003957typedef struct
3958{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003959 int in_use; // number of subexpr with useful info
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003960
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003961 // When REG_MULTI is TRUE list.multi is used, otherwise list.line.
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003962 union
3963 {
3964 struct multipos
3965 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003966 linenr_T start_lnum;
3967 linenr_T end_lnum;
3968 colnr_T start_col;
3969 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003970 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003971 struct linepos
3972 {
3973 char_u *start;
3974 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003975 } line[NSUBEXP];
3976 } list;
Bram Moolenaar79336e12022-12-11 14:18:31 +00003977 colnr_T orig_start_col; // list.multi[0].start_col without \zs
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003978} regsub_T;
3979
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003980typedef struct
3981{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003982 regsub_T norm; // \( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003983#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003984 regsub_T synt; // \z( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003985#endif
3986} regsubs_T;
3987
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003988// nfa_pim_T stores a Postponed Invisible Match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02003989typedef struct nfa_pim_S nfa_pim_T;
3990struct nfa_pim_S
3991{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003992 int result; // NFA_PIM_*, see below
3993 nfa_state_T *state; // the invisible match start state
3994 regsubs_T subs; // submatch info, only party used
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003995 union
3996 {
3997 lpos_T pos;
3998 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003999 } end; // where the match must end
Bram Moolenaara2d95102013-06-04 14:23:05 +02004000};
4001
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004002// Values for done in nfa_pim_T.
4003#define NFA_PIM_UNUSED 0 // pim not used
4004#define NFA_PIM_TODO 1 // pim not done yet
4005#define NFA_PIM_MATCH 2 // pim executed, matches
4006#define NFA_PIM_NOMATCH 3 // pim executed, no match
Bram Moolenaara2d95102013-06-04 14:23:05 +02004007
4008
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004009// nfa_thread_T contains execution information of a NFA state
Bram Moolenaar4b417062013-05-25 20:19:50 +02004010typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004011{
4012 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004013 int count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004014 nfa_pim_T pim; // if pim.result != NFA_PIM_UNUSED: postponed
4015 // invisible match
4016 regsubs_T subs; // submatch info, only party used
Bram Moolenaar4b417062013-05-25 20:19:50 +02004017} nfa_thread_T;
4018
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004019// nfa_list_T contains the alternative NFA execution states.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004020typedef struct
4021{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004022 nfa_thread_T *t; // allocated array of states
4023 int n; // nr of states currently in "t"
4024 int len; // max nr of states in "t"
4025 int id; // ID of the list
4026 int has_pim; // TRUE when any state has a PIM
Bram Moolenaar4b417062013-05-25 20:19:50 +02004027} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004028
Bram Moolenaar5714b802013-05-28 22:03:20 +02004029#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004030static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004031
4032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004033log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004034{
4035 log_subexpr(&subs->norm);
4036# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004037 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02004038 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004039# endif
4040}
4041
Bram Moolenaar5714b802013-05-28 22:03:20 +02004042 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004043log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004044{
4045 int j;
4046
4047 for (j = 0; j < sub->in_use; j++)
4048 if (REG_MULTI)
Bram Moolenaarc96311b2022-11-25 21:13:47 +00004049 fprintf(log_fd,
4050 "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02004051 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004052 sub->list.multi[j].start_col,
4053 (int)sub->list.multi[j].start_lnum,
4054 sub->list.multi[j].end_col,
4055 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02004056 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02004057 {
4058 char *s = (char *)sub->list.line[j].start;
4059 char *e = (char *)sub->list.line[j].end;
4060
Bram Moolenaar87953742013-06-05 18:52:40 +02004061 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02004062 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02004063 s == NULL ? "NULL" : s,
4064 e == NULL ? "NULL" : e);
4065 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004066}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004067
4068 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01004069pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004070{
4071 static char buf[30];
4072
4073 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
4074 buf[0] = NUL;
4075 else
4076 {
4077 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02004078 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004079 }
4080 return buf;
4081}
4082
Bram Moolenaar5714b802013-05-28 22:03:20 +02004083#endif
4084
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004085// Used during execution: whether a match has been found.
Bram Moolenaar2338c322018-07-08 19:07:19 +02004086static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01004087#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004088static int *nfa_timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01004089#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02004090
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004091static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004092static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004093
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004094/*
4095 * Copy postponed invisible match info from "from" to "to".
4096 */
4097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004098copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004099{
4100 to->result = from->result;
4101 to->state = from->state;
4102 copy_sub(&to->subs.norm, &from->subs.norm);
4103#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004104 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004105 copy_sub(&to->subs.synt, &from->subs.synt);
4106#endif
4107 to->end = from->end;
4108}
4109
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004111clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004112{
4113 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004114 // Use 0xff to set lnum to -1
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004115 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02004116 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004117 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004118 vim_memset(sub->list.line, 0,
4119 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004120 sub->in_use = 0;
4121}
4122
4123/*
4124 * Copy the submatches from "from" to "to".
4125 */
4126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004127copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004128{
4129 to->in_use = from->in_use;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004130 if (from->in_use <= 0)
4131 return;
4132
4133 // Copy the match start and end positions.
4134 if (REG_MULTI)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004135 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004136 mch_memmove(&to->list.multi[0],
4137 &from->list.multi[0],
4138 sizeof(struct multipos) * from->in_use);
4139 to->orig_start_col = from->orig_start_col;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004140 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004141 else
4142 mch_memmove(&to->list.line[0],
4143 &from->list.line[0],
4144 sizeof(struct linepos) * from->in_use);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004145}
4146
4147/*
4148 * Like copy_sub() but exclude the main match.
4149 */
4150 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004151copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004152{
4153 if (to->in_use < from->in_use)
4154 to->in_use = from->in_use;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004155 if (from->in_use <= 1)
4156 return;
4157
4158 // Copy the match start and end positions.
4159 if (REG_MULTI)
4160 mch_memmove(&to->list.multi[1],
4161 &from->list.multi[1],
4162 sizeof(struct multipos) * (from->in_use - 1));
4163 else
4164 mch_memmove(&to->list.line[1],
4165 &from->list.line[1],
4166 sizeof(struct linepos) * (from->in_use - 1));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004167}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004168
Bram Moolenaar428e9872013-05-30 17:05:39 +02004169/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004170 * Like copy_sub() but only do the end of the main match if \ze is present.
4171 */
4172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004173copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004174{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004175 if (!rex.nfa_has_zend)
4176 return;
4177
4178 if (REG_MULTI)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004179 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004180 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004181 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004182 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4183 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaarf2118842013-09-25 18:16:38 +02004184 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004185 }
4186 else
4187 {
4188 if (from->list.line[0].end != NULL)
4189 to->list.line[0].end = from->list.line[0].end;
Bram Moolenaarf2118842013-09-25 18:16:38 +02004190 }
4191}
4192
4193/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004194 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004195 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004196 */
4197 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004198sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004199{
4200 int i;
4201 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004202 linenr_T s1;
4203 linenr_T s2;
4204 char_u *sp1;
4205 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004206
4207 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4208 if (REG_MULTI)
4209 {
4210 for (i = 0; i < todo; ++i)
4211 {
4212 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004213 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004214 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004215 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004216 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004217 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004218 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004219 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004220 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004221 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004222 if (s1 != -1 && sub1->list.multi[i].start_col
4223 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004224 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004225
Bram Moolenaar0270f382018-07-17 05:43:58 +02004226 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004227 {
4228 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004229 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004230 else
4231 s1 = -1;
4232 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004233 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004234 else
4235 s2 = -1;
4236 if (s1 != s2)
4237 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004238 if (s1 != -1 && sub1->list.multi[i].end_col
4239 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004240 return FALSE;
4241 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004242 }
4243 }
4244 else
4245 {
4246 for (i = 0; i < todo; ++i)
4247 {
4248 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004249 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004250 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004251 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004252 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004253 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004254 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004255 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004256 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004257 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004258 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004259 {
4260 if (i < sub1->in_use)
4261 sp1 = sub1->list.line[i].end;
4262 else
4263 sp1 = NULL;
4264 if (i < sub2->in_use)
4265 sp2 = sub2->list.line[i].end;
4266 else
4267 sp2 = NULL;
4268 if (sp1 != sp2)
4269 return FALSE;
4270 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004271 }
4272 }
4273
4274 return TRUE;
4275}
4276
Bram Moolenaar616592e2022-06-17 15:17:10 +01004277#ifdef FEAT_RELTIME
4278/*
4279 * Check if we are past the time limit, if there is one.
4280 */
4281 static int
4282nfa_did_time_out(void)
4283{
4284 if (*timeout_flag)
4285 {
4286 if (nfa_timed_out != NULL)
4287 {
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00004288# ifdef FEAT_EVAL
Bram Moolenaar616592e2022-06-17 15:17:10 +01004289 if (!*nfa_timed_out)
4290 ch_log(NULL, "NFA regexp timed out");
Bram Moolenaar509ce032022-06-20 11:23:01 +01004291# endif
Bram Moolenaar616592e2022-06-17 15:17:10 +01004292 *nfa_timed_out = TRUE;
4293 }
4294 return TRUE;
4295 }
4296 return FALSE;
4297}
4298#endif
4299
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004300#ifdef ENABLE_LOG
4301 static void
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004302open_debug_log(int result)
4303{
4304 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4305 if (log_fd == NULL)
4306 {
4307 emsg(_(e_log_open_failed));
4308 log_fd = stderr;
4309 }
4310
4311 fprintf(log_fd, "****************************\n");
4312 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4313 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : result == MAYBE
4314 ? "MAYBE" : "FALSE");
4315 fprintf(log_fd, "****************************\n");
4316}
4317
4318 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004319report_state(char *action,
4320 regsub_T *sub,
4321 nfa_state_T *state,
4322 int lid,
4323 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004324{
4325 int col;
4326
4327 if (sub->in_use <= 0)
4328 col = -1;
4329 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004330 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004331 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004332 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004333 nfa_set_code(state->c);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004334 if (log_fd == NULL)
4335 open_debug_log(MAYBE);
4336
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004337 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4338 action, abs(state->id), lid, state->c, code, col,
4339 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004340}
4341#endif
4342
Bram Moolenaar43e02982013-06-07 17:31:29 +02004343/*
4344 * Return TRUE if the same state is already in list "l" with the same
4345 * positions as "subs".
4346 */
4347 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004348has_state_with_pos(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004349 nfa_list_T *l, // runtime state list
4350 nfa_state_T *state, // state to update
4351 regsubs_T *subs, // pointers to subexpressions
4352 nfa_pim_T *pim) // postponed match or NULL
Bram Moolenaar43e02982013-06-07 17:31:29 +02004353{
4354 nfa_thread_T *thread;
4355 int i;
4356
4357 for (i = 0; i < l->n; ++i)
4358 {
4359 thread = &l->t[i];
4360 if (thread->state->id == state->id
4361 && sub_equal(&thread->subs.norm, &subs->norm)
4362#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004363 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004364 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004365#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004366 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004367 return TRUE;
4368 }
4369 return FALSE;
4370}
4371
4372/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004373 * Return TRUE if "one" and "two" are equal. That includes when both are not
4374 * set.
4375 */
4376 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004377pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004378{
4379 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4380 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4381
4382 if (one_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004383 // one is unused: equal when two is also unused
Bram Moolenaar69b52452013-07-17 21:10:51 +02004384 return two_unused;
4385 if (two_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004386 // one is used and two is not: not equal
Bram Moolenaar69b52452013-07-17 21:10:51 +02004387 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004388 // compare the state id
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004389 if (one->state->id != two->state->id)
4390 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004391 // compare the position
Bram Moolenaar69b52452013-07-17 21:10:51 +02004392 if (REG_MULTI)
4393 return one->end.pos.lnum == two->end.pos.lnum
4394 && one->end.pos.col == two->end.pos.col;
4395 return one->end.ptr == two->end.ptr;
4396}
4397
4398/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004399 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4400 */
4401 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004402match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004403{
4404 nfa_state_T *state = startstate;
4405
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004406 // avoid too much recursion
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004407 if (depth > 10)
4408 return FALSE;
4409
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004410 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004411 {
4412 switch (state->c)
4413 {
4414 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004415 case NFA_MCLOSE:
4416 case NFA_END_INVISIBLE:
4417 case NFA_END_INVISIBLE_NEG:
4418 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004419 return TRUE;
4420
4421 case NFA_SPLIT:
4422 return match_follows(state->out, depth + 1)
4423 || match_follows(state->out1, depth + 1);
4424
4425 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004426 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004427 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004428 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004429 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004430 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004431 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004432 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004433 case NFA_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004434 // skip ahead to next state
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004435 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004436 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004437
4438 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004439 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004440 case NFA_IDENT:
4441 case NFA_SIDENT:
4442 case NFA_KWORD:
4443 case NFA_SKWORD:
4444 case NFA_FNAME:
4445 case NFA_SFNAME:
4446 case NFA_PRINT:
4447 case NFA_SPRINT:
4448 case NFA_WHITE:
4449 case NFA_NWHITE:
4450 case NFA_DIGIT:
4451 case NFA_NDIGIT:
4452 case NFA_HEX:
4453 case NFA_NHEX:
4454 case NFA_OCTAL:
4455 case NFA_NOCTAL:
4456 case NFA_WORD:
4457 case NFA_NWORD:
4458 case NFA_HEAD:
4459 case NFA_NHEAD:
4460 case NFA_ALPHA:
4461 case NFA_NALPHA:
4462 case NFA_LOWER:
4463 case NFA_NLOWER:
4464 case NFA_UPPER:
4465 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004466 case NFA_LOWER_IC:
4467 case NFA_NLOWER_IC:
4468 case NFA_UPPER_IC:
4469 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004470 case NFA_START_COLL:
4471 case NFA_START_NEG_COLL:
4472 case NFA_NEWL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004473 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004474 return FALSE;
4475
4476 default:
4477 if (state->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004478 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004479 return FALSE;
4480
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004481 // Others: zero-width or possibly zero-width, might still find
4482 // a match at the same position, keep looking.
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004483 break;
4484 }
4485 state = state->out;
4486 }
4487 return FALSE;
4488}
4489
4490
4491/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004492 * Return TRUE if "state" is already in list "l".
4493 */
4494 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004495state_in_list(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004496 nfa_list_T *l, // runtime state list
4497 nfa_state_T *state, // state to update
4498 regsubs_T *subs) // pointers to subexpressions
Bram Moolenaar43e02982013-06-07 17:31:29 +02004499{
4500 if (state->lastlist[nfa_ll_index] == l->id)
4501 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004502 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004503 return TRUE;
4504 }
4505 return FALSE;
4506}
4507
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004508// Offset used for "off" by addstate_here().
Bram Moolenaar16b35782016-09-09 20:29:50 +02004509#define ADDSTATE_HERE_OFFSET 10
4510
Bram Moolenaard05bf562013-06-30 23:24:08 +02004511/*
4512 * Add "state" and possibly what follows to state list ".".
4513 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar616592e2022-06-17 15:17:10 +01004514 * Returns NULL when recursiveness is too deep or timed out.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004515 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004516 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004517addstate(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004518 nfa_list_T *l, // runtime state list
4519 nfa_state_T *state, // state to update
4520 regsubs_T *subs_arg, // pointers to subexpressions
4521 nfa_pim_T *pim, // postponed look-behind match
4522 int off_arg) // byte offset, when -1 go to next line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004523{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004524 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004525 int off = off_arg;
4526 int add_here = FALSE;
4527 int listindex = 0;
4528 int k;
4529 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004530 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004531 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004532 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004533 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004534 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004535 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004536 regsubs_T *subs = subs_arg;
4537 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004538#ifdef ENABLE_LOG
4539 int did_print = FALSE;
4540#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004541 static int depth = 0;
4542
Bram Moolenaar616592e2022-06-17 15:17:10 +01004543#ifdef FEAT_RELTIME
4544 if (nfa_did_time_out())
4545 return NULL;
4546#endif
4547
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004548 // This function is called recursively. When the depth is too much we run
4549 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004550 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004551 {
4552 --depth;
4553 return NULL;
4554 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555
Bram Moolenaar16b35782016-09-09 20:29:50 +02004556 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4557 {
4558 add_here = TRUE;
4559 off = 0;
4560 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4561 }
4562
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 switch (state->c)
4564 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004565 case NFA_NCLOSE:
4566 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004567 case NFA_MCLOSE1:
4568 case NFA_MCLOSE2:
4569 case NFA_MCLOSE3:
4570 case NFA_MCLOSE4:
4571 case NFA_MCLOSE5:
4572 case NFA_MCLOSE6:
4573 case NFA_MCLOSE7:
4574 case NFA_MCLOSE8:
4575 case NFA_MCLOSE9:
4576#ifdef FEAT_SYN_HL
4577 case NFA_ZCLOSE:
4578 case NFA_ZCLOSE1:
4579 case NFA_ZCLOSE2:
4580 case NFA_ZCLOSE3:
4581 case NFA_ZCLOSE4:
4582 case NFA_ZCLOSE5:
4583 case NFA_ZCLOSE6:
4584 case NFA_ZCLOSE7:
4585 case NFA_ZCLOSE8:
4586 case NFA_ZCLOSE9:
4587#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004588 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004589 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004590 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004591 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004592 // These nodes are not added themselves but their "out" and/or
4593 // "out1" may be added below.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004594 break;
4595
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004596 case NFA_BOL:
4597 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004598 // "^" won't match past end-of-line, don't bother trying.
4599 // Except when at the end of the line, or when we are going to the
4600 // next line for a look-behind match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004601 if (rex.input > rex.line
4602 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004603 && (nfa_endp == NULL
4604 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004605 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004606 goto skip_add;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004607 // FALLTHROUGH
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004608
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004609 case NFA_MOPEN1:
4610 case NFA_MOPEN2:
4611 case NFA_MOPEN3:
4612 case NFA_MOPEN4:
4613 case NFA_MOPEN5:
4614 case NFA_MOPEN6:
4615 case NFA_MOPEN7:
4616 case NFA_MOPEN8:
4617 case NFA_MOPEN9:
4618#ifdef FEAT_SYN_HL
4619 case NFA_ZOPEN:
4620 case NFA_ZOPEN1:
4621 case NFA_ZOPEN2:
4622 case NFA_ZOPEN3:
4623 case NFA_ZOPEN4:
4624 case NFA_ZOPEN5:
4625 case NFA_ZOPEN6:
4626 case NFA_ZOPEN7:
4627 case NFA_ZOPEN8:
4628 case NFA_ZOPEN9:
4629#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004630 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004631 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004632 // These nodes need to be added so that we can bail out when it
4633 // was added to this list before at the same position to avoid an
4634 // endless loop for "\(\)*"
Bram Moolenaar307aa162013-06-02 16:34:21 +02004635
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004637 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004639 // This state is already in the list, don't add it again,
4640 // unless it is an MOPEN that is used for a backreference or
4641 // when there is a PIM. For NFA_MATCH check the position,
4642 // lower position is preferred.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004643 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004644 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004646 // When called from addstate_here() do insert before
4647 // existing states.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004648 if (add_here)
4649 {
4650 for (k = 0; k < l->n && k < listindex; ++k)
4651 if (l->t[k].state->id == state->id)
4652 {
4653 found = TRUE;
4654 break;
4655 }
4656 }
4657 if (!add_here || found)
4658 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004659skip_add:
4660#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004661 nfa_set_code(state->c);
4662 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4663 abs(state->id), l->id, state->c, code,
4664 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004665#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004666 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004667 return subs;
4668 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004669 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004670
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004671 // Do not add the state again when it exists with the same
4672 // positions.
Bram Moolenaar69b52452013-07-17 21:10:51 +02004673 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004674 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004675 }
4676
Bram Moolenaar688b3982019-02-13 21:47:36 +01004677 // When there are backreferences or PIMs the number of states may
4678 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004679 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004680 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004681 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004682 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004683 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004684
Bram Moolenaar688b3982019-02-13 21:47:36 +01004685 if ((long)(newsize >> 10) >= p_mmp)
4686 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004687 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar688b3982019-02-13 21:47:36 +01004688 --depth;
4689 return NULL;
4690 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004691 if (subs != &temp_subs)
4692 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004693 // "subs" may point into the current array, need to make a
4694 // copy before it becomes invalid.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004695 copy_sub(&temp_subs.norm, &subs->norm);
4696#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004697 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004698 copy_sub(&temp_subs.synt, &subs->synt);
4699#endif
4700 subs = &temp_subs;
4701 }
4702
Bram Moolenaar688b3982019-02-13 21:47:36 +01004703 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004704 if (newt == NULL)
4705 {
4706 // out of memory
4707 --depth;
4708 return NULL;
4709 }
4710 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004711 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004713
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004714 // add the state to the list
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004715 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004716 thread = &l->t[l->n++];
4717 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004718 if (pim == NULL)
4719 thread->pim.result = NFA_PIM_UNUSED;
4720 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004721 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004722 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004723 l->has_pim = TRUE;
4724 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004725 copy_sub(&thread->subs.norm, &subs->norm);
4726#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004727 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004728 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004729#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004730#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004731 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004732 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004733#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004734 }
4735
4736#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004737 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004738 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004739#endif
4740 switch (state->c)
4741 {
4742 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743 break;
4744
4745 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004746 // order matters here
Bram Moolenaar16b35782016-09-09 20:29:50 +02004747 subs = addstate(l, state->out, subs, pim, off_arg);
4748 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004749 break;
4750
Bram Moolenaar699c1202013-09-25 16:41:54 +02004751 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752 case NFA_NOPEN:
4753 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004754 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004755 break;
4756
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004757 case NFA_MOPEN:
4758 case NFA_MOPEN1:
4759 case NFA_MOPEN2:
4760 case NFA_MOPEN3:
4761 case NFA_MOPEN4:
4762 case NFA_MOPEN5:
4763 case NFA_MOPEN6:
4764 case NFA_MOPEN7:
4765 case NFA_MOPEN8:
4766 case NFA_MOPEN9:
4767#ifdef FEAT_SYN_HL
4768 case NFA_ZOPEN:
4769 case NFA_ZOPEN1:
4770 case NFA_ZOPEN2:
4771 case NFA_ZOPEN3:
4772 case NFA_ZOPEN4:
4773 case NFA_ZOPEN5:
4774 case NFA_ZOPEN6:
4775 case NFA_ZOPEN7:
4776 case NFA_ZOPEN8:
4777 case NFA_ZOPEN9:
4778#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004779 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004780 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004781 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004782 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004783 sub = &subs->norm;
4784 }
4785#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004786 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004787 {
4788 subidx = state->c - NFA_ZOPEN;
4789 sub = &subs->synt;
4790 }
4791#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004792 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004793 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004794 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004795 sub = &subs->norm;
4796 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004797
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004798 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004799 save_ptr = NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004800 CLEAR_FIELD(save_multipos);
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004801
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004802 // Set the position (with "off" added) in the subexpression. Save
4803 // and restore it when it was in use. Otherwise fill any gap.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004804 if (REG_MULTI)
4805 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004806 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004807 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004808 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004809 save_in_use = -1;
4810 }
4811 else
4812 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004813 save_in_use = sub->in_use;
4814 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004815 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004816 sub->list.multi[i].start_lnum = -1;
4817 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004818 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004819 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004820 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004821 if (off == -1)
4822 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004823 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004824 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004825 }
4826 else
4827 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004828 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004829 sub->list.multi[subidx].start_col =
Bram Moolenaarc96311b2022-11-25 21:13:47 +00004830 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004831 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004832 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004833 }
4834 else
4835 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004836 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004837 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004838 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004839 save_in_use = -1;
4840 }
4841 else
4842 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004843 save_in_use = sub->in_use;
4844 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004845 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004846 sub->list.line[i].start = NULL;
4847 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004848 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004849 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004850 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004851 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004852 }
4853
Bram Moolenaar16b35782016-09-09 20:29:50 +02004854 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004855 if (subs == NULL)
4856 break;
4857 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004858#ifdef FEAT_SYN_HL
4859 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4860 sub = &subs->synt;
4861 else
4862#endif
4863 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004865 if (save_in_use == -1)
4866 {
4867 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004868 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004869 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004870 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004871 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004873 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004874 break;
4875
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004876 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004877 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004878 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004879 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004881 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004882 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004883 break;
4884 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004885 // FALLTHROUGH
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004886 case NFA_MCLOSE1:
4887 case NFA_MCLOSE2:
4888 case NFA_MCLOSE3:
4889 case NFA_MCLOSE4:
4890 case NFA_MCLOSE5:
4891 case NFA_MCLOSE6:
4892 case NFA_MCLOSE7:
4893 case NFA_MCLOSE8:
4894 case NFA_MCLOSE9:
4895#ifdef FEAT_SYN_HL
4896 case NFA_ZCLOSE:
4897 case NFA_ZCLOSE1:
4898 case NFA_ZCLOSE2:
4899 case NFA_ZCLOSE3:
4900 case NFA_ZCLOSE4:
4901 case NFA_ZCLOSE5:
4902 case NFA_ZCLOSE6:
4903 case NFA_ZCLOSE7:
4904 case NFA_ZCLOSE8:
4905 case NFA_ZCLOSE9:
4906#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004907 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004909 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004910 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004911 sub = &subs->norm;
4912 }
4913#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004914 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004915 {
4916 subidx = state->c - NFA_ZCLOSE;
4917 sub = &subs->synt;
4918 }
4919#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004920 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004921 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004922 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004923 sub = &subs->norm;
4924 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004925
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004926 // We don't fill in gaps here, there must have been an MOPEN that
4927 // has done that.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004928 save_in_use = sub->in_use;
4929 if (sub->in_use <= subidx)
4930 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004931 if (REG_MULTI)
4932 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004933 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004934 if (off == -1)
4935 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004936 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004937 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004938 }
4939 else
4940 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004941 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004942 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004943 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004944 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004945 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004946 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004947 }
4948 else
4949 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004950 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004951 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004952 // avoid compiler warnings
Bram Moolenaara80faa82020-04-12 19:37:17 +02004953 CLEAR_FIELD(save_multipos);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004954 }
4955
Bram Moolenaar16b35782016-09-09 20:29:50 +02004956 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004957 if (subs == NULL)
4958 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004959 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004960#ifdef FEAT_SYN_HL
4961 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4962 sub = &subs->synt;
4963 else
4964#endif
4965 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004966
4967 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004968 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004969 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004970 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004971 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004972 break;
4973 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004974 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004975 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004976}
4977
4978/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004979 * Like addstate(), but the new state(s) are put at position "*ip".
4980 * Used for zero-width matches, next state to use is the added one.
4981 * This makes sure the order of states to be tried does not change, which
4982 * matters for alternatives.
4983 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004984 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004985addstate_here(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004986 nfa_list_T *l, // runtime state list
4987 nfa_state_T *state, // state to update
4988 regsubs_T *subs, // pointers to subexpressions
4989 nfa_pim_T *pim, // postponed look-behind match
Bram Moolenaar05540972016-01-30 20:31:25 +01004990 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004991{
4992 int tlen = l->n;
4993 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004994 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004995 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004996
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004997 // First add the state(s) at the end, so that we know how many there are.
4998 // Pass the listidx as offset (avoids adding another argument to
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004999 // addstate()).
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005000 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
5001 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01005002 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005003
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005004 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02005005 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005006 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005007
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005008 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02005009 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02005010 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005011 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02005012 if (count == 1)
5013 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005014 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02005015 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02005016 }
5017 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02005018 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02005019 if (l->n + count - 1 >= l->len)
5020 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005021 // not enough space to move the new states, reallocate the list
5022 // and move the states to the right position
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01005023 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01005024 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01005025 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02005026
Bram Moolenaar688b3982019-02-13 21:47:36 +01005027 if ((long)(newsize >> 10) >= p_mmp)
5028 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005029 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar688b3982019-02-13 21:47:36 +01005030 return NULL;
5031 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005032 newl = alloc(newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02005033 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01005034 return NULL;
5035 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02005036 mch_memmove(&(newl[0]),
5037 &(l->t[0]),
5038 sizeof(nfa_thread_T) * listidx);
5039 mch_memmove(&(newl[listidx]),
5040 &(l->t[l->n - count]),
5041 sizeof(nfa_thread_T) * count);
5042 mch_memmove(&(newl[listidx + count]),
5043 &(l->t[listidx + 1]),
5044 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
5045 vim_free(l->t);
5046 l->t = newl;
5047 }
5048 else
5049 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005050 // make space for new states, then move them from the
5051 // end to the current position
Bram Moolenaar55480dc2013-06-30 13:17:24 +02005052 mch_memmove(&(l->t[listidx + count]),
5053 &(l->t[listidx + 1]),
5054 sizeof(nfa_thread_T) * (l->n - listidx - 1));
5055 mch_memmove(&(l->t[listidx]),
5056 &(l->t[l->n - 1]),
5057 sizeof(nfa_thread_T) * count);
5058 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02005059 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02005060 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005061 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005062
5063 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005064}
5065
5066/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005067 * Check character class "class" against current character c.
5068 */
5069 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005070check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005071{
5072 switch (class)
5073 {
5074 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02005075 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005076 return OK;
5077 break;
5078 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02005079 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005080 return OK;
5081 break;
5082 case NFA_CLASS_BLANK:
5083 if (c == ' ' || c == '\t')
5084 return OK;
5085 break;
5086 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02005087 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088 return OK;
5089 break;
5090 case NFA_CLASS_DIGIT:
5091 if (VIM_ISDIGIT(c))
5092 return OK;
5093 break;
5094 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02005095 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005096 return OK;
5097 break;
5098 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02005099 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005100 return OK;
5101 break;
5102 case NFA_CLASS_PRINT:
5103 if (vim_isprintc(c))
5104 return OK;
5105 break;
5106 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02005107 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005108 return OK;
5109 break;
5110 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005111 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005112 return OK;
5113 break;
5114 case NFA_CLASS_UPPER:
5115 if (MB_ISUPPER(c))
5116 return OK;
5117 break;
5118 case NFA_CLASS_XDIGIT:
5119 if (vim_isxdigit(c))
5120 return OK;
5121 break;
5122 case NFA_CLASS_TAB:
5123 if (c == '\t')
5124 return OK;
5125 break;
5126 case NFA_CLASS_RETURN:
5127 if (c == '\r')
5128 return OK;
5129 break;
5130 case NFA_CLASS_BACKSPACE:
5131 if (c == '\b')
5132 return OK;
5133 break;
5134 case NFA_CLASS_ESCAPE:
5135 if (c == '\033')
5136 return OK;
5137 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01005138 case NFA_CLASS_IDENT:
5139 if (vim_isIDc(c))
5140 return OK;
5141 break;
5142 case NFA_CLASS_KEYWORD:
5143 if (reg_iswordc(c))
5144 return OK;
5145 break;
5146 case NFA_CLASS_FNAME:
5147 if (vim_isfilec(c))
5148 return OK;
5149 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005150
5151 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005152 // should not be here :P
Christian Brabandtb64cec22024-03-31 17:52:56 +02005153 siemsg(_(e_nfa_regexp_invalid_character_class_nr), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02005154 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005155 }
5156 return FAIL;
5157}
5158
Bram Moolenaar5714b802013-05-28 22:03:20 +02005159/*
5160 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005161 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02005162 */
5163 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005164match_backref(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005165 regsub_T *sub, // pointers to subexpressions
Bram Moolenaar05540972016-01-30 20:31:25 +01005166 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005167 int *bytelen) // out: length of match in bytes
Bram Moolenaar5714b802013-05-28 22:03:20 +02005168{
5169 int len;
5170
5171 if (sub->in_use <= subidx)
5172 {
5173retempty:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005174 // backref was not set, match an empty string
Bram Moolenaar5714b802013-05-28 22:03:20 +02005175 *bytelen = 0;
5176 return TRUE;
5177 }
5178
5179 if (REG_MULTI)
5180 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005181 if (sub->list.multi[subidx].start_lnum < 0
5182 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005183 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005184 if (sub->list.multi[subidx].start_lnum == rex.lnum
5185 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005186 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005187 len = sub->list.multi[subidx].end_col
5188 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005189 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
5190 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02005191 {
5192 *bytelen = len;
5193 return TRUE;
5194 }
5195 }
5196 else
5197 {
5198 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005199 sub->list.multi[subidx].start_lnum,
5200 sub->list.multi[subidx].start_col,
5201 sub->list.multi[subidx].end_lnum,
5202 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02005203 bytelen) == RA_MATCH)
5204 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005205 }
5206 }
5207 else
5208 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005209 if (sub->list.line[subidx].start == NULL
5210 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005211 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005212 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005213 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005214 {
5215 *bytelen = len;
5216 return TRUE;
5217 }
5218 }
5219 return FALSE;
5220}
5221
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005222#ifdef FEAT_SYN_HL
5223
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005224/*
5225 * Check for a match with \z subexpression "subidx".
5226 * Return TRUE if it matches.
5227 */
5228 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005229match_zref(
5230 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005231 int *bytelen) // out: length of match in bytes
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005232{
5233 int len;
5234
5235 cleanup_zsubexpr();
5236 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5237 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005238 // backref was not set, match an empty string
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005239 *bytelen = 0;
5240 return TRUE;
5241 }
5242
5243 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005244 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005245 {
5246 *bytelen = len;
5247 return TRUE;
5248 }
5249 return FALSE;
5250}
5251#endif
5252
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005253/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005254 * Save list IDs for all NFA states of "prog" into "list".
5255 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005256 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005257 */
5258 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005259nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005260{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005261 int i;
5262 nfa_state_T *p;
5263
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005264 // Order in the list is reverse, it's a bit faster that way.
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005265 p = &prog->state[0];
5266 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005267 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005268 list[i] = p->lastlist[1];
5269 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005270 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005271 }
5272}
5273
5274/*
5275 * Restore list IDs from "list" to all NFA states.
5276 */
5277 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005278nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005279{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005280 int i;
5281 nfa_state_T *p;
5282
5283 p = &prog->state[0];
5284 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005285 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005286 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005287 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005288 }
5289}
5290
Bram Moolenaar423532e2013-05-29 21:14:42 +02005291 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005292nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005293{
5294 if (op == 1) return pos > val;
5295 if (op == 2) return pos < val;
5296 return val == pos;
5297}
5298
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005299static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005300
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005301/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005302 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005303 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5304 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005305 */
5306 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005307recursive_regmatch(
5308 nfa_state_T *state,
5309 nfa_pim_T *pim,
5310 nfa_regprog_T *prog,
5311 regsubs_T *submatch,
5312 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005313 int **listids,
5314 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005315{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005316 int save_reginput_col = (int)(rex.input - rex.line);
5317 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005318 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005319 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005320 save_se_T *save_nfa_endp = nfa_endp;
5321 save_se_T endpos;
5322 save_se_T *endposp = NULL;
5323 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005324 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005325
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005326 if (pim != NULL)
5327 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005328 // start at the position where the postponed match was
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005329 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005330 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005331 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005332 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005333 }
5334
Bram Moolenaardecd9542013-06-07 16:31:50 +02005335 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005336 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5337 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5338 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005339 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005340 // The recursive match must end at the current position. When "pim" is
5341 // not NULL it specifies the current position.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005342 endposp = &endpos;
5343 if (REG_MULTI)
5344 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005345 if (pim == NULL)
5346 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005347 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5348 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005349 }
5350 else
5351 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005352 }
5353 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005354 {
5355 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005356 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005357 else
5358 endpos.se_u.ptr = pim->end.ptr;
5359 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005360
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005361 // Go back the specified number of bytes, or as far as the
5362 // start of the previous line, to try matching "\@<=" or
5363 // not matching "\@<!". This is very inefficient, limit the number of
5364 // bytes if possible.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005365 if (state->val <= 0)
5366 {
5367 if (REG_MULTI)
5368 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005369 rex.line = reg_getline(--rex.lnum);
5370 if (rex.line == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005371 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005372 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005373 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005374 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005375 }
5376 else
5377 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005378 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005379 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005380 // Not enough bytes in this line, go to end of
5381 // previous line.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005382 rex.line = reg_getline(--rex.lnum);
5383 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005384 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005385 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005386 rex.line = reg_getline(++rex.lnum);
5387 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005388 }
5389 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005390 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005391 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005392 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005393 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005394 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005395 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005396 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005397 }
5398 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005399 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005400 }
5401 }
5402
Bram Moolenaarf46da702013-06-02 22:37:42 +02005403#ifdef ENABLE_LOG
5404 if (log_fd != stderr)
5405 fclose(log_fd);
5406 log_fd = NULL;
5407#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005408 // Have to clear the lastlist field of the NFA nodes, so that
5409 // nfa_regmatch() and addstate() can run properly after recursion.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005410 if (nfa_ll_index == 1)
5411 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005412 // Already calling nfa_regmatch() recursively. Save the lastlist[1]
5413 // values and clear them.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005414 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005415 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005416 vim_free(*listids);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005417 *listids = ALLOC_MULT(int, prog->nstate);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005418 if (*listids == NULL)
5419 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005420 emsg(_(e_nfa_regexp_could_not_allocate_memory_for_branch_traversal));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005421 return 0;
5422 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005423 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005424 }
5425 nfa_save_listids(prog, *listids);
5426 need_restore = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005427 // any value of rex.nfa_listid will do
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005428 }
5429 else
5430 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005431 // First recursive nfa_regmatch() call, switch to the second lastlist
5432 // entry. Make sure rex.nfa_listid is different from a previous
5433 // recursive call, because some states may still have this ID.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005434 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005435 if (rex.nfa_listid <= rex.nfa_alt_listid)
5436 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005437 }
5438
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005439 // Call nfa_regmatch() to check if the current concat matches at this
5440 // position. The concat ends with the node NFA_END_INVISIBLE
Bram Moolenaarf46da702013-06-02 22:37:42 +02005441 nfa_endp = endposp;
5442 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005443
5444 if (need_restore)
5445 nfa_restore_listids(prog, *listids);
5446 else
5447 {
5448 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005449 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005450 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005451
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005452 // restore position in input text
Bram Moolenaar0270f382018-07-17 05:43:58 +02005453 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005454 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005455 rex.line = reg_getline(rex.lnum);
5456 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005457 if (result != NFA_TOO_EXPENSIVE)
5458 {
5459 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005460 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005461 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005462 nfa_endp = save_nfa_endp;
5463
5464#ifdef ENABLE_LOG
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00005465 open_debug_log(result);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005466#endif
5467
5468 return result;
5469}
5470
Bram Moolenaara2d95102013-06-04 14:23:05 +02005471/*
5472 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005473 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005474 * NFA_ANY: 1
5475 * specific character: 99
5476 */
5477 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005478failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005479{
5480 int c = state->c;
5481 int l, r;
5482
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005483 // detect looping
Bram Moolenaara2d95102013-06-04 14:23:05 +02005484 if (depth > 4)
5485 return 1;
5486
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005487 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005488 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005489 case NFA_SPLIT:
5490 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005491 // avoid recursive stuff
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005492 return 1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005493 // two alternatives, use the lowest failure chance
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005494 l = failure_chance(state->out, depth + 1);
5495 r = failure_chance(state->out1, depth + 1);
5496 return l < r ? l : r;
5497
5498 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005499 // matches anything, unlikely to fail
Bram Moolenaara2d95102013-06-04 14:23:05 +02005500 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005501
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005502 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005503 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005504 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005505 // empty match works always
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005506 return 0;
5507
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005508 case NFA_START_INVISIBLE:
5509 case NFA_START_INVISIBLE_FIRST:
5510 case NFA_START_INVISIBLE_NEG:
5511 case NFA_START_INVISIBLE_NEG_FIRST:
5512 case NFA_START_INVISIBLE_BEFORE:
5513 case NFA_START_INVISIBLE_BEFORE_FIRST:
5514 case NFA_START_INVISIBLE_BEFORE_NEG:
5515 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5516 case NFA_START_PATTERN:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005517 // recursive regmatch is expensive, use low failure chance
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005518 return 5;
5519
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005520 case NFA_BOL:
5521 case NFA_EOL:
5522 case NFA_BOF:
5523 case NFA_EOF:
5524 case NFA_NEWL:
5525 return 99;
5526
5527 case NFA_BOW:
5528 case NFA_EOW:
5529 return 90;
5530
5531 case NFA_MOPEN:
5532 case NFA_MOPEN1:
5533 case NFA_MOPEN2:
5534 case NFA_MOPEN3:
5535 case NFA_MOPEN4:
5536 case NFA_MOPEN5:
5537 case NFA_MOPEN6:
5538 case NFA_MOPEN7:
5539 case NFA_MOPEN8:
5540 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005541#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005542 case NFA_ZOPEN:
5543 case NFA_ZOPEN1:
5544 case NFA_ZOPEN2:
5545 case NFA_ZOPEN3:
5546 case NFA_ZOPEN4:
5547 case NFA_ZOPEN5:
5548 case NFA_ZOPEN6:
5549 case NFA_ZOPEN7:
5550 case NFA_ZOPEN8:
5551 case NFA_ZOPEN9:
5552 case NFA_ZCLOSE:
5553 case NFA_ZCLOSE1:
5554 case NFA_ZCLOSE2:
5555 case NFA_ZCLOSE3:
5556 case NFA_ZCLOSE4:
5557 case NFA_ZCLOSE5:
5558 case NFA_ZCLOSE6:
5559 case NFA_ZCLOSE7:
5560 case NFA_ZCLOSE8:
5561 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005562#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005563 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005564 case NFA_MCLOSE1:
5565 case NFA_MCLOSE2:
5566 case NFA_MCLOSE3:
5567 case NFA_MCLOSE4:
5568 case NFA_MCLOSE5:
5569 case NFA_MCLOSE6:
5570 case NFA_MCLOSE7:
5571 case NFA_MCLOSE8:
5572 case NFA_MCLOSE9:
5573 case NFA_NCLOSE:
5574 return failure_chance(state->out, depth + 1);
5575
5576 case NFA_BACKREF1:
5577 case NFA_BACKREF2:
5578 case NFA_BACKREF3:
5579 case NFA_BACKREF4:
5580 case NFA_BACKREF5:
5581 case NFA_BACKREF6:
5582 case NFA_BACKREF7:
5583 case NFA_BACKREF8:
5584 case NFA_BACKREF9:
5585#ifdef FEAT_SYN_HL
5586 case NFA_ZREF1:
5587 case NFA_ZREF2:
5588 case NFA_ZREF3:
5589 case NFA_ZREF4:
5590 case NFA_ZREF5:
5591 case NFA_ZREF6:
5592 case NFA_ZREF7:
5593 case NFA_ZREF8:
5594 case NFA_ZREF9:
5595#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005596 // backreferences don't match in many places
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005597 return 94;
5598
5599 case NFA_LNUM_GT:
5600 case NFA_LNUM_LT:
5601 case NFA_COL_GT:
5602 case NFA_COL_LT:
5603 case NFA_VCOL_GT:
5604 case NFA_VCOL_LT:
5605 case NFA_MARK_GT:
5606 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005607 case NFA_VISUAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005608 // before/after positions don't match very often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005609 return 85;
5610
5611 case NFA_LNUM:
5612 return 90;
5613
5614 case NFA_CURSOR:
5615 case NFA_COL:
5616 case NFA_VCOL:
5617 case NFA_MARK:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005618 // specific positions rarely match
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005619 return 98;
5620
5621 case NFA_COMPOSING:
5622 return 95;
5623
5624 default:
5625 if (c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005626 // character match fails often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005627 return 95;
5628 }
5629
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005630 // something else, includes character classes
Bram Moolenaara2d95102013-06-04 14:23:05 +02005631 return 50;
5632}
5633
Bram Moolenaarf46da702013-06-02 22:37:42 +02005634/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005635 * Skip until the char "c" we know a match must start with.
5636 */
5637 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005638skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005639{
5640 char_u *s;
5641
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005642 // Used often, do some work to avoid call overhead.
Bram Moolenaara12a1612019-01-24 16:39:02 +01005643 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005644 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005645 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005646 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005647 if (s == NULL)
5648 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005649 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005650 return OK;
5651}
5652
5653/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005654 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005655 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005656 * Returns zero for no match, 1 for a match.
5657 */
5658 static long
Bram Moolenaar79336e12022-12-11 14:18:31 +00005659find_match_text(colnr_T *startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005660{
Bram Moolenaar79336e12022-12-11 14:18:31 +00005661 colnr_T col = *startcol;
Bram Moolenaar473de612013-06-08 18:19:48 +02005662 int c1, c2;
5663 int len1, len2;
5664 int match;
5665
5666 for (;;)
5667 {
5668 match = TRUE;
Christian Brabandtc97f4d62024-04-10 16:18:15 +02005669 len2 = MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005670 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5671 {
5672 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005673 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar59de4172020-06-09 19:34:54 +02005674 if (c1 != c2 && (!rex.reg_ic || MB_CASEFOLD(c1) != MB_CASEFOLD(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005675 {
5676 match = FALSE;
5677 break;
5678 }
Bram Moolenaar65b60562021-09-07 19:26:53 +02005679 len2 += enc_utf8 ? utf_ptr2len(rex.line + col + len2)
5680 : MB_CHAR2LEN(c2);
Bram Moolenaar473de612013-06-08 18:19:48 +02005681 }
5682 if (match
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005683 // check that no composing char follows
Bram Moolenaar473de612013-06-08 18:19:48 +02005684 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005685 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005686 {
5687 cleanup_subexpr();
5688 if (REG_MULTI)
5689 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005690 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005691 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005692 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005693 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005694 }
5695 else
5696 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005697 rex.reg_startp[0] = rex.line + col;
5698 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005699 }
Bram Moolenaar79336e12022-12-11 14:18:31 +00005700 *startcol = col;
Bram Moolenaar473de612013-06-08 18:19:48 +02005701 return 1L;
5702 }
5703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005704 // Try finding regstart after the current match.
5705 col += MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005706 if (skip_to_start(regstart, &col) == FAIL)
5707 break;
5708 }
Bram Moolenaar79336e12022-12-11 14:18:31 +00005709
5710 *startcol = col;
Bram Moolenaar473de612013-06-08 18:19:48 +02005711 return 0L;
5712}
5713
5714/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005715 * Main matching routine.
5716 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005717 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005718 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005719 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005720 *
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005721 * Return TRUE if there is a match, FALSE if there is no match,
5722 * NFA_TOO_EXPENSIVE if we end up with too many states.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005723 * When there is a match "submatch" contains the positions.
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005724 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005725 * Note: Caller must ensure that: start != NULL.
5726 */
5727 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005728nfa_regmatch(
5729 nfa_regprog_T *prog,
5730 nfa_state_T *start,
5731 regsubs_T *submatch,
5732 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005733{
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005734 int result = FALSE;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005735 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005736 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005737 int go_to_nextline = FALSE;
5738 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005739 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005740 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005741 nfa_list_T *thislist;
5742 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005743 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005744 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005745 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005746 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005747 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005748 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005749 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005750 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005751#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005752 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005753#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005754
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005755 // Some patterns may take a long time to match, especially when using
5756 // recursive_regmatch(). Allow interrupting them with CTRL-C.
Bram Moolenaar41f12052013-08-25 17:01:42 +02005757 fast_breakcheck();
5758 if (got_int)
5759 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005760#ifdef FEAT_RELTIME
Paul Ollis65745772022-06-05 16:55:54 +01005761 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005762 return FALSE;
5763#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005764
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005765#ifdef NFA_REGEXP_DEBUG_LOG
5766 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5767 if (debug == NULL)
5768 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005769 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005770 return FALSE;
5771 }
5772#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005773 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005774
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005775 // Allocate memory for the lists of nodes.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005776 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005777
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005778 list[0].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005779 list[0].len = prog->nstate + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005780 list[1].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005781 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005782 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005783 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005784
5785#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005786 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00005787 if (log_fd == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005789 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005790 log_fd = stderr;
5791 }
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00005792 fprintf(log_fd, "**********************************\n");
5793 nfa_set_code(start->c);
5794 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5795 abs(start->id), code);
5796 fprintf(log_fd, "**********************************\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005797#endif
5798
5799 thislist = &list[0];
5800 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005801 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005802 nextlist = &list[1];
5803 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005804 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005805#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005806 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005807#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005808 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005809
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005810 // Inline optimized code for addstate(thislist, start, m, 0) if we know
5811 // it's the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005812 if (toplevel)
5813 {
5814 if (REG_MULTI)
5815 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005816 m->norm.list.multi[0].start_lnum = rex.lnum;
5817 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar79336e12022-12-11 14:18:31 +00005818 m->norm.orig_start_col = m->norm.list.multi[0].start_col;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005819 }
5820 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005821 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005822 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005823 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005824 }
5825 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005826 r = addstate(thislist, start, m, NULL, 0);
5827 if (r == NULL)
5828 {
5829 nfa_match = NFA_TOO_EXPENSIVE;
5830 goto theend;
5831 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005832
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00005833#define ADD_STATE_IF_MATCH(state) \
5834 if (result) \
5835 { \
5836 add_state = state->out; \
5837 add_off = clen; \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005838 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005839
5840 /*
5841 * Run for each character.
5842 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005843 for (;;)
5844 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005845 int curc;
5846 int clen;
5847
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005848 if (has_mbyte)
5849 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005850 curc = (*mb_ptr2char)(rex.input);
5851 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005852 }
5853 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005854 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005855 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005856 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005857 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005858 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005859 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005860 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005861 go_to_nextline = FALSE;
5862 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005863
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005864 // swap lists
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005865 thislist = &list[flag];
5866 nextlist = &list[flag ^= 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005867 nextlist->n = 0; // clear nextlist
Bram Moolenaar196ed142013-07-21 18:59:24 +02005868 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005869 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005870 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005871 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005872# ifdef FEAT_EVAL
5873 || nfa_fail_for_testing
5874# endif
5875 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005876 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005877 // too many states, retry with old engine
Bram Moolenaarfda37292014-11-05 14:27:36 +01005878 nfa_match = NFA_TOO_EXPENSIVE;
5879 goto theend;
5880 }
5881
Bram Moolenaar0270f382018-07-17 05:43:58 +02005882 thislist->id = rex.nfa_listid;
5883 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005884
5885#ifdef ENABLE_LOG
5886 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005887 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005888 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005889 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005890 {
5891 int i;
5892
5893 for (i = 0; i < thislist->n; i++)
5894 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5895 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005896 fprintf(log_fd, "\n");
5897#endif
5898
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005899#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005900 fprintf(debug, "\n-------------------\n");
5901#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005902 /*
5903 * If the state lists are empty we can stop.
5904 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005905 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005906 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005907
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005908 // compute nextlist
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005909 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005910 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005911 // If the list gets very long there probably is something wrong.
5912 // At least allow interrupting with CTRL-C.
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005913 fast_breakcheck();
5914 if (got_int)
5915 break;
5916#ifdef FEAT_RELTIME
Paul Ollis65745772022-06-05 16:55:54 +01005917 if (nfa_did_time_out())
Bram Moolenaar305abc62022-05-28 11:08:40 +01005918 break;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005919#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005920 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005921
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005922#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005923 nfa_set_code(t->state->c);
5924 fprintf(debug, "%s, ", code);
5925#endif
5926#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005927 {
5928 int col;
5929
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005930 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005931 col = -1;
5932 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005933 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005934 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005935 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005936 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005937 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005938 abs(t->state->id), (int)t->state->c, code, col,
5939 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005940 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005941#endif
5942
5943 /*
5944 * Handle the possible codes of the current state.
5945 * The most important is NFA_MATCH.
5946 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005947 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005948 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005949 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005950 switch (t->state->c)
5951 {
5952 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005953 {
Bram Moolenaaref2dff52020-12-21 14:54:32 +01005954 // If the match is not at the start of the line, ends before a
5955 // composing characters and rex.reg_icombine is not set, that
5956 // is not really a match.
5957 if (enc_utf8 && !rex.reg_icombine
5958 && rex.input != rex.line && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005959 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005960
Bram Moolenaar963fee22013-05-26 21:47:28 +02005961 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005962 copy_sub(&submatch->norm, &t->subs.norm);
5963#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005964 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005965 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005966#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005967#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005968 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005969#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005970 // Found the left-most longest match, do not look at any other
5971 // states at this position. When the list of states is going
5972 // to be empty quit without advancing, so that "rex.input" is
5973 // correct.
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005974 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005975 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005976 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005977 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005978
5979 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005980 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005981 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005982 /*
5983 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005984 * NFA_START_INVISIBLE_BEFORE node.
5985 * They surround a zero-width group, used with "\@=", "\&",
5986 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005987 * If we got here, it means that the current "invisible" group
5988 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005989 * nfa_regmatch(). For a look-behind match only when it ends
5990 * in the position in "nfa_endp".
5991 * Submatches are stored in *m, and used in the parent call.
5992 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005993#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005994 if (nfa_endp != NULL)
5995 {
5996 if (REG_MULTI)
5997 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005998 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005999 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006000 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02006001 nfa_endp->se_u.pos.col);
6002 else
6003 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006004 (int)(rex.input - rex.line),
6005 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006006 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02006007#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006008 // If "nfa_endp" is set it's only a match if it ends at
6009 // "nfa_endp"
Bram Moolenaarf46da702013-06-02 22:37:42 +02006010 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006011 ? (rex.lnum != nfa_endp->se_u.pos.lnum
6012 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02006013 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006014 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02006015 break;
6016
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006017 // do not set submatches for \@!
Bram Moolenaardecd9542013-06-07 16:31:50 +02006018 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02006019 {
6020 copy_sub(&m->norm, &t->subs.norm);
6021#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006022 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02006023 copy_sub(&m->synt, &t->subs.synt);
6024#endif
6025 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006026#ifdef ENABLE_LOG
6027 fprintf(log_fd, "Match found:\n");
6028 log_subsexpr(m);
6029#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02006030 nfa_match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006031 // See comment above at "goto nextchar".
Bram Moolenaar78c93e42013-09-05 16:05:36 +02006032 if (nextlist->n == 0)
6033 clen = 0;
6034 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006035
6036 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02006037 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006038 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02006039 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02006040 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02006041 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006042 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02006043 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006044 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02006045#ifdef ENABLE_LOG
6046 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
6047 failure_chance(t->state->out, 0),
6048 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02006049#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006050 // Do it directly if there already is a PIM or when
6051 // nfa_postprocess() detected it will work better.
Bram Moolenaara2947e22013-06-11 22:44:09 +02006052 if (t->pim.result != NFA_PIM_UNUSED
6053 || t->state->c == NFA_START_INVISIBLE_FIRST
6054 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
6055 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
6056 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006057 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02006058 int in_use = m->norm.in_use;
6059
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006060 // Copy submatch info for the recursive call, opposite
6061 // of what happens on success below.
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02006062 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02006063#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006064 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02006065 copy_sub_off(&m->synt, &t->subs.synt);
6066#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02006067
Bram Moolenaara2d95102013-06-04 14:23:05 +02006068 /*
6069 * First try matching the invisible match, then what
6070 * follows.
6071 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006072 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006073 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006074 if (result == NFA_TOO_EXPENSIVE)
6075 {
6076 nfa_match = result;
6077 goto theend;
6078 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006079
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006080 // for \@! and \@<! it is a match when the result is
6081 // FALSE
Bram Moolenaardecd9542013-06-07 16:31:50 +02006082 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006083 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
6084 || t->state->c
6085 == NFA_START_INVISIBLE_BEFORE_NEG
6086 || t->state->c
6087 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006088 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006089 // Copy submatch info from the recursive call
Bram Moolenaara2d95102013-06-04 14:23:05 +02006090 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006091#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006092 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006093 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006094#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006095 // If the pattern has \ze and it matched in the
6096 // sub pattern, use it.
Bram Moolenaarf2118842013-09-25 18:16:38 +02006097 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02006098
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006099 // t->state->out1 is the corresponding
6100 // END_INVISIBLE node; Add its out to the current
6101 // list (zero-width match).
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006102 add_here = TRUE;
6103 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006104 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02006105 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006106 }
6107 else
6108 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006109 nfa_pim_T pim;
6110
Bram Moolenaara2d95102013-06-04 14:23:05 +02006111 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006112 * First try matching what follows. Only if a match
6113 * is found verify the invisible match matches. Add a
6114 * nfa_pim_T to the following states, it contains info
6115 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02006116 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006117 pim.state = t->state;
6118 pim.result = NFA_PIM_TODO;
6119 pim.subs.norm.in_use = 0;
6120#ifdef FEAT_SYN_HL
6121 pim.subs.synt.in_use = 0;
6122#endif
6123 if (REG_MULTI)
6124 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006125 pim.end.pos.col = (int)(rex.input - rex.line);
6126 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006127 }
6128 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006129 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006130
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006131 // t->state->out1 is the corresponding END_INVISIBLE
6132 // node; Add its out to the current list (zero-width
6133 // match).
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006134 if (addstate_here(thislist, t->state->out1->out,
6135 &t->subs, &pim, &listidx) == NULL)
6136 {
6137 nfa_match = NFA_TOO_EXPENSIVE;
6138 goto theend;
6139 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006140 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006141 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006142 break;
6143
Bram Moolenaar87953742013-06-05 18:52:40 +02006144 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02006145 {
6146 nfa_state_T *skip = NULL;
6147#ifdef ENABLE_LOG
6148 int skip_lid = 0;
6149#endif
6150
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006151 // There is no point in trying to match the pattern if the
6152 // output state is not going to be added to the list.
Bram Moolenaar43e02982013-06-07 17:31:29 +02006153 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
6154 {
6155 skip = t->state->out1->out;
6156#ifdef ENABLE_LOG
6157 skip_lid = nextlist->id;
6158#endif
6159 }
6160 else if (state_in_list(nextlist,
6161 t->state->out1->out->out, &t->subs))
6162 {
6163 skip = t->state->out1->out->out;
6164#ifdef ENABLE_LOG
6165 skip_lid = nextlist->id;
6166#endif
6167 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02006168 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02006169 t->state->out1->out->out, &t->subs))
6170 {
6171 skip = t->state->out1->out->out;
6172#ifdef ENABLE_LOG
6173 skip_lid = thislist->id;
6174#endif
6175 }
6176 if (skip != NULL)
6177 {
6178#ifdef ENABLE_LOG
6179 nfa_set_code(skip->c);
6180 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
6181 abs(skip->id), skip_lid, skip->c, code);
6182#endif
6183 break;
6184 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006185 // Copy submatch info to the recursive call, opposite of what
6186 // happens afterwards.
Bram Moolenaar699c1202013-09-25 16:41:54 +02006187 copy_sub_off(&m->norm, &t->subs.norm);
6188#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006189 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02006190 copy_sub_off(&m->synt, &t->subs.synt);
6191#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02006192
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006193 // First try matching the pattern.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006194 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006195 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006196 if (result == NFA_TOO_EXPENSIVE)
6197 {
6198 nfa_match = result;
6199 goto theend;
6200 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006201 if (result)
6202 {
6203 int bytelen;
6204
6205#ifdef ENABLE_LOG
6206 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6207 log_subsexpr(m);
6208#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006209 // Copy submatch info from the recursive call
Bram Moolenaar87953742013-06-05 18:52:40 +02006210 copy_sub_off(&t->subs.norm, &m->norm);
6211#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006212 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006213 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006214#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006215 // Now we need to skip over the matched text and then
6216 // continue with what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02006217 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006218 // TODO: multi-line match
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006219 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006220 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006221 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006222 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006223
6224#ifdef ENABLE_LOG
6225 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6226#endif
6227 if (bytelen == 0)
6228 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006229 // empty match, output of corresponding
6230 // NFA_END_PATTERN/NFA_SKIP to be used at current
6231 // position
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006232 add_here = TRUE;
6233 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006234 }
6235 else if (bytelen <= clen)
6236 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006237 // match current character, output of corresponding
6238 // NFA_END_PATTERN to be used at next position.
Bram Moolenaar87953742013-06-05 18:52:40 +02006239 add_state = t->state->out1->out->out;
6240 add_off = clen;
6241 }
6242 else
6243 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006244 // skip over the matched characters, set character
6245 // count in NFA_SKIP
Bram Moolenaar87953742013-06-05 18:52:40 +02006246 add_state = t->state->out1->out;
6247 add_off = bytelen;
6248 add_count = bytelen - clen;
6249 }
6250 }
6251 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006252 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006253
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006254 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006255 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006256 {
6257 add_here = TRUE;
6258 add_state = t->state->out;
6259 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006260 break;
6261
6262 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006263 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006264 {
6265 add_here = TRUE;
6266 add_state = t->state->out;
6267 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006268 break;
6269
6270 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006271 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006272
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006273 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006274 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006275 else if (has_mbyte)
6276 {
6277 int this_class;
6278
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006279 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006280 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006281 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006282 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006283 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006284 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006285 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006286 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006287 || (rex.input > rex.line
Bram Moolenaarc96311b2022-11-25 21:13:47 +00006288 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006289 result = FALSE;
6290 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006291 {
6292 add_here = TRUE;
6293 add_state = t->state->out;
6294 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006295 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006296
6297 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006298 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006299 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006300 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006301 else if (has_mbyte)
6302 {
6303 int this_class, prev_class;
6304
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006305 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006306 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307 prev_class = reg_prev_class();
6308 if (this_class == prev_class
6309 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006310 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006311 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006312 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6313 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006314 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006315 result = FALSE;
6316 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006317 {
6318 add_here = TRUE;
6319 add_state = t->state->out;
6320 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006321 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006322
Bram Moolenaar4b780632013-05-31 22:14:52 +02006323 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006324 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006325 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006326 {
6327 add_here = TRUE;
6328 add_state = t->state->out;
6329 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006330 break;
6331
6332 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006333 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006334 {
6335 add_here = TRUE;
6336 add_state = t->state->out;
6337 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006338 break;
6339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006340 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006341 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006342 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006343 int len = 0;
6344 nfa_state_T *end;
6345 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006346 int cchars[MAX_MCO];
6347 int ccount = 0;
6348 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006349
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006350 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006351 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006352 if (utf_iscomposing(sta->c))
6353 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006354 // Only match composing character(s), ignore base
6355 // character. Used for ".{composing}" and "{composing}"
6356 // (no preceding character).
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006357 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006358 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006359 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006360 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006361 // If \Z was present, then ignore composing characters.
6362 // When ignoring the base character this always matches.
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006363 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006364 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006365 else
6366 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006367 while (sta->c != NFA_END_COMPOSING)
6368 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006370
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006371 // Check base character matches first, unless ignored.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006372 else if (len > 0 || mc == sta->c)
6373 {
6374 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006375 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006376 len += mb_char2len(mc);
6377 sta = sta->out;
6378 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006380 // We don't care about the order of composing characters.
6381 // Get them into cchars[] first.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006382 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006383 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006384 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006385 cchars[ccount++] = mc;
6386 len += mb_char2len(mc);
6387 if (ccount == MAX_MCO)
6388 break;
6389 }
6390
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006391 // Check that each composing char in the pattern matches a
6392 // composing char in the text. We do not check if all
6393 // composing chars are matched.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006394 result = OK;
6395 while (sta->c != NFA_END_COMPOSING)
6396 {
6397 for (j = 0; j < ccount; ++j)
6398 if (cchars[j] == sta->c)
6399 break;
6400 if (j == ccount)
6401 {
6402 result = FAIL;
6403 break;
6404 }
6405 sta = sta->out;
6406 }
6407 }
6408 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006409 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006410
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006411 end = t->state->out1; // NFA_END_COMPOSING
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006412 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006413 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006414 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006415
6416 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006417 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaarc96311b2022-11-25 21:13:47 +00006418 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006419 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006420 go_to_nextline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006421 // Pass -1 for the offset, which means taking the position
6422 // at the start of the next line.
Bram Moolenaara2d95102013-06-04 14:23:05 +02006423 add_state = t->state->out;
6424 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006426 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006427 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006428 // match \n as if it is an ordinary character
Bram Moolenaara2d95102013-06-04 14:23:05 +02006429 add_state = t->state->out;
6430 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006431 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006432 break;
6433
Bram Moolenaar417bad22013-06-07 14:08:30 +02006434 case NFA_START_COLL:
6435 case NFA_START_NEG_COLL:
6436 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006437 // What follows is a list of characters, until NFA_END_COLL.
6438 // One of them must match or none of them must match.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006439 nfa_state_T *state;
6440 int result_if_matched;
6441 int c1, c2;
6442
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006443 // Never match EOL. If it's part of the collection it is added
6444 // as a separate state with an OR.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006445 if (curc == NUL)
6446 break;
6447
6448 state = t->state->out;
6449 result_if_matched = (t->state->c == NFA_START_COLL);
6450 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006451 {
Christian Brabandtd2cc51f2024-01-04 22:54:08 +01006452 if (state->c == NFA_COMPOSING)
6453 {
6454 int mc = curc;
6455 int len = 0;
6456 nfa_state_T *end;
6457 nfa_state_T *sta;
6458 int cchars[MAX_MCO];
6459 int ccount = 0;
6460 int j;
6461
6462 sta = t->state->out->out;
6463 len = 0;
6464 if (utf_iscomposing(sta->c))
6465 {
6466 // Only match composing character(s), ignore base
6467 // character. Used for ".{composing}" and "{composing}"
6468 // (no preceding character).
6469 len += mb_char2len(mc);
6470 }
6471 if (rex.reg_icombine && len == 0)
6472 {
6473 // If \Z was present, then ignore composing characters.
6474 // When ignoring the base character this always matches.
6475 if (sta->c != curc)
6476 result = FAIL;
6477 else
6478 result = OK;
6479 while (sta->c != NFA_END_COMPOSING)
6480 sta = sta->out;
6481 }
6482 // Check base character matches first, unless ignored.
6483 else if (len > 0 || mc == sta->c)
6484// if (len > 0 || mc == sta->c)
6485 {
6486 if (len == 0)
6487 {
6488 len += mb_char2len(mc);
6489 sta = sta->out;
6490 }
6491
6492 // We don't care about the order of composing characters.
6493 // Get them into cchars[] first.
6494 while (len < clen)
6495 {
6496 mc = mb_ptr2char(rex.input + len);
6497 cchars[ccount++] = mc;
6498 len += mb_char2len(mc);
6499 if (ccount == MAX_MCO)
6500 break;
6501 }
6502
6503 // Check that each composing char in the pattern matches a
6504 // composing char in the text. We do not check if all
6505 // composing chars are matched.
6506 result = OK;
6507 while (sta->c != NFA_END_COMPOSING)
6508 {
6509 for (j = 0; j < ccount; ++j)
6510 if (cchars[j] == sta->c)
6511 break;
6512 if (j == ccount)
6513 {
6514 result = FAIL;
6515 break;
6516 }
6517 sta = sta->out;
6518 }
6519 }
6520 else
6521 result = FAIL;
6522
6523 if (t->state->out->out1->c == NFA_END_COMPOSING)
6524 {
6525 end = t->state->out->out1;
6526 ADD_STATE_IF_MATCH(end);
6527 }
6528 break;
6529 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02006530 if (state->c == NFA_END_COLL)
6531 {
6532 result = !result_if_matched;
6533 break;
6534 }
6535 if (state->c == NFA_RANGE_MIN)
6536 {
6537 c1 = state->val;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006538 state = state->out; // advance to NFA_RANGE_MAX
Bram Moolenaar417bad22013-06-07 14:08:30 +02006539 c2 = state->val;
6540#ifdef ENABLE_LOG
6541 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6542 curc, c1, c2);
6543#endif
6544 if (curc >= c1 && curc <= c2)
6545 {
6546 result = result_if_matched;
6547 break;
6548 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006549 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006550 {
Bram Moolenaar59de4172020-06-09 19:34:54 +02006551 int curc_low = MB_CASEFOLD(curc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02006552 int done = FALSE;
6553
6554 for ( ; c1 <= c2; ++c1)
Bram Moolenaar59de4172020-06-09 19:34:54 +02006555 if (MB_CASEFOLD(c1) == curc_low)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006556 {
6557 result = result_if_matched;
6558 done = TRUE;
6559 break;
6560 }
6561 if (done)
6562 break;
6563 }
6564 }
6565 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006566 : (curc == state->c
Bram Moolenaar59de4172020-06-09 19:34:54 +02006567 || (rex.reg_ic && MB_CASEFOLD(curc)
6568 == MB_CASEFOLD(state->c))))
Bram Moolenaar417bad22013-06-07 14:08:30 +02006569 {
6570 result = result_if_matched;
6571 break;
6572 }
6573 state = state->out;
6574 }
6575 if (result)
6576 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006577 // next state is in out of the NFA_END_COLL, out1 of
6578 // START points to the END state
Bram Moolenaar417bad22013-06-07 14:08:30 +02006579 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006580 add_off = clen;
6581 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006582 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006583 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006584
6585 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006586 // Any char except '\0', (end of input) does not match.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006587 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006588 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006589 add_state = t->state->out;
6590 add_off = clen;
6591 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006592 break;
6593
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006594 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006595 // On a composing character skip over it. Otherwise do
6596 // nothing. Always matches.
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006597 if (enc_utf8 && utf_iscomposing(curc))
6598 {
6599 add_off = clen;
6600 }
6601 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006602 {
6603 add_here = TRUE;
6604 add_off = 0;
6605 }
6606 add_state = t->state->out;
6607 break;
6608
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006609 /*
6610 * Character classes like \a for alpha, \d for digit etc.
6611 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006612 case NFA_IDENT: // \i
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006613 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006614 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006615 break;
6616
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006617 case NFA_SIDENT: // \I
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006618 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006619 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006620 break;
6621
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006622 case NFA_KWORD: // \k
Bram Moolenaar0270f382018-07-17 05:43:58 +02006623 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006624 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006625 break;
6626
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006627 case NFA_SKWORD: // \K
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006628 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006629 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006630 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006631 break;
6632
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006633 case NFA_FNAME: // \f
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006634 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006635 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006636 break;
6637
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006638 case NFA_SFNAME: // \F
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006639 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006640 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006641 break;
6642
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006643 case NFA_PRINT: // \p
Bram Moolenaar0270f382018-07-17 05:43:58 +02006644 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006645 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006646 break;
6647
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006648 case NFA_SPRINT: // \P
Bram Moolenaar0270f382018-07-17 05:43:58 +02006649 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006650 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006651 break;
6652
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006653 case NFA_WHITE: // \s
Bram Moolenaar1c465442017-03-12 20:10:05 +01006654 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006655 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006656 break;
6657
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006658 case NFA_NWHITE: // \S
Bram Moolenaar1c465442017-03-12 20:10:05 +01006659 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006660 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006661 break;
6662
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006663 case NFA_DIGIT: // \d
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006664 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006665 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006666 break;
6667
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006668 case NFA_NDIGIT: // \D
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006669 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006670 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006671 break;
6672
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006673 case NFA_HEX: // \x
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006674 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006675 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006676 break;
6677
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006678 case NFA_NHEX: // \X
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006679 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006680 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006681 break;
6682
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006683 case NFA_OCTAL: // \o
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006684 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006685 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006686 break;
6687
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006688 case NFA_NOCTAL: // \O
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006689 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006690 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006691 break;
6692
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006693 case NFA_WORD: // \w
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006694 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006695 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006696 break;
6697
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006698 case NFA_NWORD: // \W
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006699 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006700 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006701 break;
6702
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006703 case NFA_HEAD: // \h
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006704 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006705 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006706 break;
6707
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006708 case NFA_NHEAD: // \H
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006709 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006710 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006711 break;
6712
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006713 case NFA_ALPHA: // \a
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006714 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006715 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006716 break;
6717
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006718 case NFA_NALPHA: // \A
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006719 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006720 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006721 break;
6722
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006723 case NFA_LOWER: // \l
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006724 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006725 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006726 break;
6727
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006728 case NFA_NLOWER: // \L
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006729 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006730 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006731 break;
6732
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006733 case NFA_UPPER: // \u
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006734 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006735 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006736 break;
6737
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006738 case NFA_NUPPER: // \U
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006739 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006740 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006741 break;
6742
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006743 case NFA_LOWER_IC: // [a-z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006744 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006745 ADD_STATE_IF_MATCH(t->state);
6746 break;
6747
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006748 case NFA_NLOWER_IC: // [^a-z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006749 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006750 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006751 ADD_STATE_IF_MATCH(t->state);
6752 break;
6753
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006754 case NFA_UPPER_IC: // [A-Z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006755 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006756 ADD_STATE_IF_MATCH(t->state);
6757 break;
6758
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006759 case NFA_NUPPER_IC: // ^[A-Z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006760 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006761 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006762 ADD_STATE_IF_MATCH(t->state);
6763 break;
6764
Bram Moolenaar5714b802013-05-28 22:03:20 +02006765 case NFA_BACKREF1:
6766 case NFA_BACKREF2:
6767 case NFA_BACKREF3:
6768 case NFA_BACKREF4:
6769 case NFA_BACKREF5:
6770 case NFA_BACKREF6:
6771 case NFA_BACKREF7:
6772 case NFA_BACKREF8:
6773 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006774#ifdef FEAT_SYN_HL
6775 case NFA_ZREF1:
6776 case NFA_ZREF2:
6777 case NFA_ZREF3:
6778 case NFA_ZREF4:
6779 case NFA_ZREF5:
6780 case NFA_ZREF6:
6781 case NFA_ZREF7:
6782 case NFA_ZREF8:
6783 case NFA_ZREF9:
6784#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006785 // \1 .. \9 \z1 .. \z9
Bram Moolenaar5714b802013-05-28 22:03:20 +02006786 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006787 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006788 int bytelen;
6789
Bram Moolenaar1f761382023-03-25 11:31:32 +00006790#ifdef FEAT_SYN_HL
6791 if (t->state->c >= NFA_BACKREF1 && t->state->c <= NFA_BACKREF9)
6792#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006793 {
6794 subidx = t->state->c - NFA_BACKREF1 + 1;
6795 result = match_backref(&t->subs.norm, subidx, &bytelen);
6796 }
6797#ifdef FEAT_SYN_HL
6798 else
6799 {
6800 subidx = t->state->c - NFA_ZREF1 + 1;
6801 result = match_zref(subidx, &bytelen);
6802 }
6803#endif
6804
Bram Moolenaar5714b802013-05-28 22:03:20 +02006805 if (result)
6806 {
6807 if (bytelen == 0)
6808 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006809 // empty match always works, output of NFA_SKIP to be
6810 // used next
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006811 add_here = TRUE;
6812 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006813 }
6814 else if (bytelen <= clen)
6815 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006816 // match current character, jump ahead to out of
6817 // NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006818 add_state = t->state->out->out;
6819 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006820 }
6821 else
6822 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006823 // skip over the matched characters, set character
6824 // count in NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006825 add_state = t->state->out;
6826 add_off = bytelen;
6827 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006828 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006829 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006830 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006831 }
6832 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006833 // character of previous matching \1 .. \9 or \@>
Bram Moolenaar5714b802013-05-28 22:03:20 +02006834 if (t->count - clen <= 0)
6835 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006836 // end of match, go to what follows
Bram Moolenaara2d95102013-06-04 14:23:05 +02006837 add_state = t->state->out;
6838 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006839 }
6840 else
6841 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006842 // add state again with decremented count
Bram Moolenaara2d95102013-06-04 14:23:05 +02006843 add_state = t->state;
6844 add_off = 0;
6845 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006846 }
6847 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006848
Bram Moolenaar423532e2013-05-29 21:14:42 +02006849 case NFA_LNUM:
6850 case NFA_LNUM_GT:
6851 case NFA_LNUM_LT:
6852 result = (REG_MULTI &&
6853 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006854 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006855 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006856 {
6857 add_here = TRUE;
6858 add_state = t->state->out;
6859 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006860 break;
6861
6862 case NFA_COL:
6863 case NFA_COL_GT:
6864 case NFA_COL_LT:
6865 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006866 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006867 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006868 {
6869 add_here = TRUE;
6870 add_state = t->state->out;
6871 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006872 break;
6873
6874 case NFA_VCOL:
6875 case NFA_VCOL_GT:
6876 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006877 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006878 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006879 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006880 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006881
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006882 // Bail out quickly when there can't be a match, avoid the
6883 // overhead of win_linetabsize() on long lines.
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006884 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006885 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006886 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006887 result = FALSE;
6888 if (op == 1 && col - 1 > t->state->val && col > 100)
6889 {
6890 int ts = wp->w_buffer->b_p_ts;
6891
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006892 // Guess that a character won't use more columns than
6893 // 'tabstop', with a minimum of 4.
Bram Moolenaaref795d12015-01-18 16:46:32 +01006894 if (ts < 4)
6895 ts = 4;
6896 result = col > t->state->val * ts;
6897 }
6898 if (!result)
Bram Moolenaar13ed4942022-08-19 13:59:25 +01006899 {
Bram Moolenaar753aead2022-09-08 12:17:06 +01006900 linenr_T lnum = REG_MULTI
6901 ? rex.reg_firstlnum + rex.lnum : 1;
6902 long_u vcol;
Bram Moolenaar13ed4942022-08-19 13:59:25 +01006903
Bram Moolenaar753aead2022-09-08 12:17:06 +01006904 if (REG_MULTI && (lnum <= 0
6905 || lnum > wp->w_buffer->b_ml.ml_line_count))
6906 lnum = 1;
Bram Moolenaar88456cd2022-11-18 22:14:09 +00006907 vcol = (long_u)win_linetabsize(wp, lnum, rex.line, col);
Bram Moolenaar13ed4942022-08-19 13:59:25 +01006908 result = nfa_re_num_cmp(t->state->val, op, vcol + 1);
6909 }
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006910 if (result)
6911 {
6912 add_here = TRUE;
6913 add_state = t->state->out;
6914 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006915 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006916 break;
6917
Bram Moolenaar044aa292013-06-04 21:27:38 +02006918 case NFA_MARK:
6919 case NFA_MARK_GT:
6920 case NFA_MARK_LT:
6921 {
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01006922 pos_T *pos;
6923 size_t col = REG_MULTI ? rex.input - rex.line : 0;
6924
6925 pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006926
Bram Moolenaar64066b92021-11-17 18:22:56 +00006927 // Line may have been freed, get it again.
6928 if (REG_MULTI)
6929 {
6930 rex.line = reg_getline(rex.lnum);
6931 rex.input = rex.line + col;
6932 }
6933
Bram Moolenaar872bee52021-05-24 22:56:15 +02006934 // Compare the mark position to the match position, if the mark
6935 // exists and mark is set in reg_buf.
6936 if (pos != NULL && pos->lnum > 0)
6937 {
6938 colnr_T pos_col = pos->lnum == rex.lnum + rex.reg_firstlnum
6939 && pos->col == MAXCOL
6940 ? (colnr_T)STRLEN(reg_getline(
6941 pos->lnum - rex.reg_firstlnum))
6942 : pos->col;
6943
6944 result = (pos->lnum == rex.lnum + rex.reg_firstlnum
6945 ? (pos_col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006946 ? t->state->c == NFA_MARK
Bram Moolenaar872bee52021-05-24 22:56:15 +02006947 : (pos_col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006948 ? t->state->c == NFA_MARK_GT
6949 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006950 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006951 ? t->state->c == NFA_MARK_GT
Bram Moolenaar872bee52021-05-24 22:56:15 +02006952 : t->state->c == NFA_MARK_LT));
6953 if (result)
6954 {
6955 add_here = TRUE;
6956 add_state = t->state->out;
6957 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006958 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006959 break;
6960 }
6961
Bram Moolenaar423532e2013-05-29 21:14:42 +02006962 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006963 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006964 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006965 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006966 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006967 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006968 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006969 {
6970 add_here = TRUE;
6971 add_state = t->state->out;
6972 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006973 break;
6974
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006975 case NFA_VISUAL:
6976 result = reg_match_visual();
6977 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006978 {
6979 add_here = TRUE;
6980 add_state = t->state->out;
6981 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006982 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006983
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006984 case NFA_MOPEN1:
6985 case NFA_MOPEN2:
6986 case NFA_MOPEN3:
6987 case NFA_MOPEN4:
6988 case NFA_MOPEN5:
6989 case NFA_MOPEN6:
6990 case NFA_MOPEN7:
6991 case NFA_MOPEN8:
6992 case NFA_MOPEN9:
6993#ifdef FEAT_SYN_HL
6994 case NFA_ZOPEN:
6995 case NFA_ZOPEN1:
6996 case NFA_ZOPEN2:
6997 case NFA_ZOPEN3:
6998 case NFA_ZOPEN4:
6999 case NFA_ZOPEN5:
7000 case NFA_ZOPEN6:
7001 case NFA_ZOPEN7:
7002 case NFA_ZOPEN8:
7003 case NFA_ZOPEN9:
7004#endif
7005 case NFA_NOPEN:
7006 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007007 // These states are only added to be able to bail out when
7008 // they are added again, nothing is to be done.
Bram Moolenaar398d53d2013-08-01 15:45:52 +02007009 break;
7010
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007011 default: // regular character
Bram Moolenaarc4912e52013-05-26 19:19:52 +02007012 {
7013 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02007014
Bram Moolenaar398d53d2013-08-01 15:45:52 +02007015#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02007016 if (c < 0)
Bram Moolenaar097c5372023-05-24 21:02:24 +01007017 siemsg("Negative state char: %ld", (long)c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02007018#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02007019 result = (c == curc);
7020
Bram Moolenaar6100d022016-10-02 16:51:57 +02007021 if (!result && rex.reg_ic)
Bram Moolenaar59de4172020-06-09 19:34:54 +02007022 result = MB_CASEFOLD(c) == MB_CASEFOLD(curc);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007023 // If rex.reg_icombine is not set only skip over the character
7024 // itself. When it is set skip over composing characters.
Bram Moolenaar6100d022016-10-02 16:51:57 +02007025 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007026 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02007027 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02007029 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02007030
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007031 } // switch (t->state->c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02007032
7033 if (add_state != NULL)
7034 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007035 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02007036 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007037
7038 if (t->pim.result == NFA_PIM_UNUSED)
7039 pim = NULL;
7040 else
7041 pim = &t->pim;
7042
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007043 // Handle the postponed invisible match if the match might end
7044 // without advancing and before the end of the line.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007045 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02007046 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007047 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02007048 {
7049#ifdef ENABLE_LOG
7050 fprintf(log_fd, "\n");
7051 fprintf(log_fd, "==================================\n");
7052 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
7053 fprintf(log_fd, "\n");
7054#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007055 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02007056 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007057 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007058 // for \@! and \@<! it is a match when the result is
7059 // FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007060 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02007061 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
7062 || pim->state->c
7063 == NFA_START_INVISIBLE_BEFORE_NEG
7064 || pim->state->c
7065 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02007066 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007067 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007068 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02007069#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02007070 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007071 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02007072#endif
7073 }
7074 }
7075 else
7076 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007077 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02007078#ifdef ENABLE_LOG
7079 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007080 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02007081 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
7082 fprintf(log_fd, "\n");
7083#endif
7084 }
7085
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007086 // for \@! and \@<! it is a match when result is FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007087 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02007088 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
7089 || pim->state->c
7090 == NFA_START_INVISIBLE_BEFORE_NEG
7091 || pim->state->c
7092 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02007093 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007094 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007095 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02007096#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02007097 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007098 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02007099#endif
7100 }
7101 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007102 // look-behind match failed, don't add the state
Bram Moolenaara2d95102013-06-04 14:23:05 +02007103 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007104
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007105 // Postponed invisible match was handled, don't add it to
7106 // following states.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02007107 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02007108 }
7109
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007110 // If "pim" points into l->t it will become invalid when
7111 // adding the state causes the list to be reallocated. Make a
7112 // local copy to avoid that.
Bram Moolenaara951e352013-10-06 15:46:11 +02007113 if (pim == &t->pim)
7114 {
7115 copy_pim(&pim_copy, pim);
7116 pim = &pim_copy;
7117 }
7118
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02007119 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01007120 r = addstate_here(thislist, add_state, &t->subs,
7121 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02007122 else
7123 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01007124 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02007125 if (add_count > 0)
7126 nextlist->t[nextlist->n - 1].count = add_count;
7127 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01007128 if (r == NULL)
7129 {
7130 nfa_match = NFA_TOO_EXPENSIVE;
7131 goto theend;
7132 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007133 }
7134
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007135 } // for (thislist = thislist; thislist->state; thislist++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007136
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007137 // Look for the start of a match in the current position by adding the
7138 // start state to the list of states.
7139 // The first found match is the leftmost one, thus the order of states
7140 // matters!
7141 // Do not add the start state in recursive calls of nfa_regmatch(),
7142 // because recursive calls should only start in the first position.
7143 // Unless "nfa_endp" is not NULL, then we match the end position.
7144 // Also don't start a match past the first line.
Bram Moolenaar61602c52013-06-01 19:54:43 +02007145 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02007146 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02007147 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02007148 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02007149 && (rex.reg_maxcol == 0
Bram Moolenaarc96311b2022-11-25 21:13:47 +00007150 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02007151 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02007152 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02007153 ? (rex.lnum < nfa_endp->se_u.pos.lnum
7154 || (rex.lnum == nfa_endp->se_u.pos.lnum
7155 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02007156 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02007157 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158 {
7159#ifdef ENABLE_LOG
7160 fprintf(log_fd, "(---) STARTSTATE\n");
7161#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007162 // Inline optimized code for addstate() if we know the state is
7163 // the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02007164 if (toplevel)
7165 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007166 int add = TRUE;
7167 int c;
7168
7169 if (prog->regstart != NUL && clen != 0)
7170 {
7171 if (nextlist->n == 0)
7172 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007173 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007174
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007175 // Nextlist is empty, we can skip ahead to the
7176 // character that must appear at the start.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007177 if (skip_to_start(prog->regstart, &col) == FAIL)
7178 break;
7179#ifdef ENABLE_LOG
7180 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02007181 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007182#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007183 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007184 }
7185 else
7186 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007187 // Checking if the required start character matches is
7188 // cheaper than adding a state that won't match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02007189 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02007190 if (c != prog->regstart && (!rex.reg_ic
Bram Moolenaar59de4172020-06-09 19:34:54 +02007191 || MB_CASEFOLD(c) != MB_CASEFOLD(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007192 {
7193#ifdef ENABLE_LOG
7194 fprintf(log_fd, " Skipping start state, regstart does not match\n");
7195#endif
7196 add = FALSE;
7197 }
7198 }
7199 }
7200
7201 if (add)
7202 {
7203 if (REG_MULTI)
Bram Moolenaar79336e12022-12-11 14:18:31 +00007204 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007205 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02007206 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar79336e12022-12-11 14:18:31 +00007207 m->norm.orig_start_col =
7208 m->norm.list.multi[0].start_col;
7209 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007210 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007211 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01007212 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
7213 {
7214 nfa_match = NFA_TOO_EXPENSIVE;
7215 goto theend;
7216 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007217 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02007218 }
7219 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01007220 {
7221 if (addstate(nextlist, start, m, NULL, clen) == NULL)
7222 {
7223 nfa_match = NFA_TOO_EXPENSIVE;
7224 goto theend;
7225 }
7226 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007227 }
7228
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007229#ifdef ENABLE_LOG
7230 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02007231 {
7232 int i;
7233
7234 for (i = 0; i < thislist->n; i++)
7235 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
7236 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007237 fprintf(log_fd, "\n");
7238#endif
7239
7240nextchar:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007241 // Advance to the next character, or advance to the next line, or
7242 // finish.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02007243 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007244 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02007245 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02007246 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02007247 reg_nextline();
7248 else
7249 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01007250
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007251 // Allow interrupting with CTRL-C.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007252 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01007253 if (got_int)
7254 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007255#ifdef FEAT_RELTIME
Paul Ollis65745772022-06-05 16:55:54 +01007256 if (nfa_did_time_out())
Bram Moolenaar305abc62022-05-28 11:08:40 +01007257 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007258#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02007259 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007260
7261#ifdef ENABLE_LOG
7262 if (log_fd != stderr)
7263 fclose(log_fd);
7264 log_fd = NULL;
7265#endif
7266
7267theend:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007268 // Free memory
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007269 vim_free(list[0].t);
7270 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02007271 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02007272#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007273#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007274 fclose(debug);
7275#endif
7276
Bram Moolenaar963fee22013-05-26 21:47:28 +02007277 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007278}
7279
7280/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02007281 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01007282 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007283 */
7284 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007285nfa_regtry(
7286 nfa_regprog_T *prog,
7287 colnr_T col,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007288 int *timed_out UNUSED) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007289{
7290 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007291 regsubs_T subs, m;
7292 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007293 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007294#ifdef ENABLE_LOG
7295 FILE *f;
7296#endif
7297
Bram Moolenaar0270f382018-07-17 05:43:58 +02007298 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007299#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007300 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007301#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007302
7303#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007304 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007305 if (f != NULL)
7306 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007307 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaar097c5372023-05-24 21:02:24 +01007308# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007309 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar097c5372023-05-24 21:02:24 +01007310# endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007311 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007312 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007313 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007314 fprintf(f, "\n\n");
7315 fclose(f);
7316 }
7317 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007318 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007319#endif
7320
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007321 clear_sub(&subs.norm);
7322 clear_sub(&m.norm);
7323#ifdef FEAT_SYN_HL
7324 clear_sub(&subs.synt);
7325 clear_sub(&m.synt);
7326#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007327
Bram Moolenaarfda37292014-11-05 14:27:36 +01007328 result = nfa_regmatch(prog, start, &subs, &m);
7329 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007330 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007331 else if (result == NFA_TOO_EXPENSIVE)
7332 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007333
7334 cleanup_subexpr();
7335 if (REG_MULTI)
7336 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007337 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007338 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007339 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7340 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007341
Bram Moolenaar6100d022016-10-02 16:51:57 +02007342 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7343 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007344 }
Bram Moolenaar79336e12022-12-11 14:18:31 +00007345 if (rex.reg_mmatch != NULL)
7346 rex.reg_mmatch->rmm_matchcol = subs.norm.orig_start_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007347
Bram Moolenaar6100d022016-10-02 16:51:57 +02007348 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007349 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007350 rex.reg_startpos[0].lnum = 0;
7351 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007352 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007353 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007354 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007355 // pattern has a \ze but it didn't match, use current end
Bram Moolenaar0270f382018-07-17 05:43:58 +02007356 rex.reg_endpos[0].lnum = rex.lnum;
7357 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007358 }
7359 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007360 // Use line number of "\ze".
Bram Moolenaar0270f382018-07-17 05:43:58 +02007361 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007362 }
7363 else
7364 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007365 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007366 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007367 rex.reg_startp[i] = subs.norm.list.line[i].start;
7368 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007369 }
7370
Bram Moolenaar6100d022016-10-02 16:51:57 +02007371 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007372 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007373 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007374 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007375 }
7376
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007377#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007378 // Package any found \z(...\) matches for export. Default is none.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007379 unref_extmatch(re_extmatch_out);
7380 re_extmatch_out = NULL;
7381
7382 if (prog->reghasz == REX_SET)
7383 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007384 cleanup_zsubexpr();
7385 re_extmatch_out = make_extmatch();
Bram Moolenaar7c77b342019-12-22 19:40:40 +01007386 if (re_extmatch_out == NULL)
7387 return 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007388 // Loop over \z1, \z2, etc. There is no \z0.
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007389 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007390 {
7391 if (REG_MULTI)
7392 {
7393 struct multipos *mpos = &subs.synt.list.multi[i];
7394
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007395 // Only accept single line matches that are valid.
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007396 if (mpos->start_lnum >= 0
7397 && mpos->start_lnum == mpos->end_lnum
7398 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007399 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007400 vim_strnsave(reg_getline(mpos->start_lnum)
7401 + mpos->start_col,
7402 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007403 }
7404 else
7405 {
7406 struct linepos *lpos = &subs.synt.list.line[i];
7407
7408 if (lpos->start != NULL && lpos->end != NULL)
7409 re_extmatch_out->matches[i] =
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007410 vim_strnsave(lpos->start, lpos->end - lpos->start);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007411 }
7412 }
7413 }
7414#endif
7415
Bram Moolenaar0270f382018-07-17 05:43:58 +02007416 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007417}
7418
7419/*
7420 * Match a regexp against a string ("line" points to the string) or multiple
Bram Moolenaardf365142021-05-03 20:01:45 +02007421 * lines (if "line" is NULL, use reg_getline()).
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007422 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007423 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007424 */
7425 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007426nfa_regexec_both(
7427 char_u *line,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007428 colnr_T startcol, // column to start looking for match
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007429 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007430{
7431 nfa_regprog_T *prog;
7432 long retval = 0L;
7433 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007434 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007435
7436 if (REG_MULTI)
7437 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007438 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007439 line = reg_getline((linenr_T)0); // relative to the cursor
Bram Moolenaar6100d022016-10-02 16:51:57 +02007440 rex.reg_startpos = rex.reg_mmatch->startpos;
7441 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007442 }
7443 else
7444 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007445 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7446 rex.reg_startp = rex.reg_match->startp;
7447 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007448 }
7449
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007450 // Be paranoid...
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007451 if (prog == NULL || line == NULL)
7452 {
RestorerZ68ebcee2023-05-31 17:12:14 +01007453 iemsg(e_null_argument);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007454 goto theend;
7455 }
7456
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007457 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007458 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007459 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007460 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007461 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007462
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007463 // If pattern contains "\Z" overrule value of rex.reg_icombine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007464 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007465 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007466
Bram Moolenaar0270f382018-07-17 05:43:58 +02007467 rex.line = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007468 rex.lnum = 0; // relative to line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007469
Bram Moolenaar0270f382018-07-17 05:43:58 +02007470 rex.nfa_has_zend = prog->has_zend;
7471 rex.nfa_has_backref = prog->has_backref;
7472 rex.nfa_nsubexpr = prog->nsubexp;
7473 rex.nfa_listid = 1;
7474 rex.nfa_alt_listid = 2;
7475#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007476 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007477#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007478
Bram Moolenaard89616e2013-06-06 18:46:06 +02007479 if (prog->reganch && col > 0)
7480 return 0L;
7481
Bram Moolenaar0270f382018-07-17 05:43:58 +02007482 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007483#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007484 // Clear the external match subpointers if necessary.
Bram Moolenaar473de612013-06-08 18:19:48 +02007485 if (prog->reghasz == REX_SET)
7486 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007487 rex.nfa_has_zsubexpr = TRUE;
7488 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007489 }
7490 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007491 {
7492 rex.nfa_has_zsubexpr = FALSE;
7493 rex.need_clear_zsubexpr = FALSE;
7494 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007495#endif
7496
Bram Moolenaard89616e2013-06-06 18:46:06 +02007497 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007498 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007499 // Skip ahead until a character we know the match must start with.
7500 // When there is none there is no match.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007501 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007502 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007503
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007504 // If match_text is set it contains the full text that must match.
7505 // Nothing else to try. Doesn't handle combining chars well.
Christian Brabandtc97f4d62024-04-10 16:18:15 +02007506 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar01105b32022-11-26 11:47:10 +00007507 {
Bram Moolenaar79336e12022-12-11 14:18:31 +00007508 retval = find_match_text(&col, prog->regstart, prog->match_text);
Bram Moolenaar01105b32022-11-26 11:47:10 +00007509 if (REG_MULTI)
7510 rex.reg_mmatch->rmm_matchcol = col;
7511 else
7512 rex.reg_match->rm_matchcol = col;
7513 return retval;
7514 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007515 }
7516
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007517 // If the start column is past the maximum column: no need to try.
Bram Moolenaar6100d022016-10-02 16:51:57 +02007518 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007519 goto theend;
7520
Bram Moolenaar0270f382018-07-17 05:43:58 +02007521 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7522 // it's accidentally used during execution.
7523 nstate = 0;
7524 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007525 {
7526 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007527 prog->state[i].lastlist[0] = 0;
7528 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007529 }
7530
Paul Ollis65745772022-06-05 16:55:54 +01007531 retval = nfa_regtry(prog, col, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007532
Bram Moolenaar0270f382018-07-17 05:43:58 +02007533#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007534 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007535#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007536
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007537theend:
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007538 if (retval > 0)
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007539 {
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007540 // Make sure the end is never before the start. Can happen when \zs and
7541 // \ze are used.
7542 if (REG_MULTI)
7543 {
7544 lpos_T *start = &rex.reg_mmatch->startpos[0];
7545 lpos_T *end = &rex.reg_mmatch->endpos[0];
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007546
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007547 if (end->lnum < start->lnum
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007548 || (end->lnum == start->lnum && end->col < start->col))
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007549 rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
7550 }
7551 else
7552 {
7553 if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
7554 rex.reg_match->endp[0] = rex.reg_match->startp[0];
Bram Moolenaar01105b32022-11-26 11:47:10 +00007555
7556 // startpos[0] may be set by "\zs", also return the column where
7557 // the whole pattern matched.
7558 rex.reg_match->rm_matchcol = col;
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007559 }
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007560 }
7561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007562 return retval;
7563}
7564
7565/*
7566 * Compile a regular expression into internal code for the NFA matcher.
7567 * Returns the program in allocated space. Returns NULL for an error.
7568 */
7569 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007570nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007571{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007572 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007573 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007574 int *postfix;
7575
7576 if (expr == NULL)
7577 return NULL;
7578
Bram Moolenaar0270f382018-07-17 05:43:58 +02007579#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007580 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007581#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007582 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007583
7584 init_class_tab();
7585
7586 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7587 return NULL;
7588
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007589 // Build postfix form of the regexp. Needed to build the NFA
7590 // (and count its size).
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007591 postfix = re2post();
7592 if (postfix == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007593 goto fail; // Cascaded (syntax?) error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007594
7595 /*
7596 * In order to build the NFA, we parse the input regexp twice:
7597 * 1. first pass to count size (so we can allocate space)
7598 * 2. second to emit code
7599 */
7600#ifdef ENABLE_LOG
7601 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007602 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007603
7604 if (f != NULL)
7605 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007606 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007607 fclose(f);
7608 }
7609 }
7610#endif
7611
7612 /*
7613 * PASS 1
7614 * Count number of NFA states in "nstate". Do not build the NFA.
7615 */
7616 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007617
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007618 // allocate the regprog with space for the compiled regexp
zeertzjq1b438a82023-02-01 13:11:15 +00007619 prog_size = offsetof(nfa_regprog_T, state) + sizeof(nfa_state_T) * nstate;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007620 prog = alloc(prog_size);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007621 if (prog == NULL)
7622 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007623 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007624 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007625
7626 /*
7627 * PASS 2
7628 * Build the NFA
7629 */
7630 prog->start = post2nfa(postfix, post_ptr, FALSE);
7631 if (prog->start == NULL)
7632 goto fail;
7633
7634 prog->regflags = regflags;
7635 prog->engine = &nfa_regengine;
7636 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007637 prog->has_zend = rex.nfa_has_zend;
7638 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007639 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007640
Bram Moolenaara2947e22013-06-11 22:44:09 +02007641 nfa_postprocess(prog);
7642
Bram Moolenaard89616e2013-06-06 18:46:06 +02007643 prog->reganch = nfa_get_reganch(prog->start, 0);
7644 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007645 prog->match_text = nfa_get_match_text(prog->start);
7646
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007647#ifdef ENABLE_LOG
7648 nfa_postfix_dump(expr, OK);
7649 nfa_dump(prog);
7650#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007651#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007652 // Remember whether this pattern has any \z specials in it.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007653 prog->reghasz = re_has_z;
7654#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007655 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007656#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007657 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007658#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007659
7660out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007661 VIM_CLEAR(post_start);
7662 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007663 state_ptr = NULL;
7664 return (regprog_T *)prog;
7665
7666fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007667 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007668#ifdef ENABLE_LOG
7669 nfa_postfix_dump(expr, FAIL);
7670#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007671#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007672 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007673#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007674 goto out;
7675}
7676
Bram Moolenaar473de612013-06-08 18:19:48 +02007677/*
7678 * Free a compiled regexp program, returned by nfa_regcomp().
7679 */
7680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007681nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007682{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007683 if (prog == NULL)
7684 return;
7685
7686 vim_free(((nfa_regprog_T *)prog)->match_text);
7687 vim_free(((nfa_regprog_T *)prog)->pattern);
7688 vim_free(prog);
Bram Moolenaar473de612013-06-08 18:19:48 +02007689}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007690
7691/*
7692 * Match a regexp against a string.
7693 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7694 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007695 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007696 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007697 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007698 */
7699 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007700nfa_regexec_nl(
7701 regmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007702 char_u *line, // string to match against
7703 colnr_T col, // column to start looking for match
Bram Moolenaar05540972016-01-30 20:31:25 +01007704 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007705{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007706 rex.reg_match = rmp;
7707 rex.reg_mmatch = NULL;
7708 rex.reg_maxline = 0;
7709 rex.reg_line_lbr = line_lbr;
7710 rex.reg_buf = curbuf;
7711 rex.reg_win = NULL;
7712 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007713 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007714 rex.reg_maxcol = 0;
Paul Ollis65745772022-06-05 16:55:54 +01007715 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007716}
7717
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007718
7719/*
7720 * Match a regexp against multiple lines.
7721 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7722 * Uses curbuf for line count and 'iskeyword'.
7723 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007724 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007725 * match otherwise.
7726 *
7727 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7728 *
7729 * ! Also NOTE : match may actually be in another line. e.g.:
7730 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7731 *
7732 * +-------------------------+
7733 * |a |
7734 * |b |
7735 * |c |
7736 * | |
7737 * +-------------------------+
7738 *
7739 * then nfa_regexec_multi() returns 3. while the original
7740 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7741 *
7742 * FIXME if this behavior is not compatible.
7743 */
7744 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007745nfa_regexec_multi(
7746 regmmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007747 win_T *win, // window in which to search or NULL
7748 buf_T *buf, // buffer in which to search
7749 linenr_T lnum, // nr of line to start looking for match
7750 colnr_T col, // column to start looking for match
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007751 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007752{
Bram Moolenaarf4140482020-02-15 23:06:45 +01007753 init_regexec_multi(rmp, win, buf, lnum);
Paul Ollis65745772022-06-05 16:55:54 +01007754 return nfa_regexec_both(NULL, col, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007755}
7756
7757#ifdef DEBUG
7758# undef ENABLE_LOG
7759#endif