blob: da80ee92163148d273eca820029db85be8d043fe [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram 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 Moolenaar174a8482013-11-28 14:20:17 +0100246static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaara5483442019-02-17 20:17:02 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %d");
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +0200249static char_u e_value_too_large[] = N_("E951: \\% value too large");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250
Bram Moolenaar0270f382018-07-17 05:43:58 +0200251// Variables only used in nfa_regcomp() and descendants.
252static int nfa_re_flags; // re_flags passed to nfa_regcomp()
253static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200254static int *post_end;
255static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200256static int nstate; // Number of states in the NFA.
257static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200258
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100259// If not NULL match must end at this position
Bram Moolenaar307aa162013-06-02 16:34:21 +0200260static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200261
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100262// 0 for first call to nfa_regmatch(), 1 for recursive call.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200263static int nfa_ll_index = 0;
264
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100265static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100266static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200267#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100268static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100270static int match_follows(nfa_state_T *startstate, int depth);
271static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100273// helper functions used when doing re2post() ... regatom() parsing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200274#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200275 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200276 return FAIL; \
277 *post_ptr++ = c; \
278 } while (0)
279
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200280/*
281 * Initialize internal variables before NFA compilation.
282 * Return OK on success, FAIL otherwise.
283 */
284 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100285nfa_regcomp_start(
286 char_u *expr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100287 int re_flags) // see vim_regcomp()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200288{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200289 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200290 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200291
292 nstate = 0;
293 istate = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100294 // A reasonable estimation for maximum size
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200295 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200296
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100297 // Some items blow up in size, such as [A-z]. Add more space for that.
298 // When it is still not enough realloc_post_list() will be used.
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200299 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200300
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100301 // Size for postfix representation of expr.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200302 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200303
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200304 post_start = alloc(postfix_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305 if (post_start == NULL)
306 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200308 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200309 rex.nfa_has_zend = FALSE;
310 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100312 // shared with BT engine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313 regcomp_start(expr, re_flags);
314
315 return OK;
316}
317
318/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200319 * Figure out if the NFA state list starts with an anchor, must match at start
320 * of the line.
321 */
322 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100323nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200324{
325 nfa_state_T *p = start;
326
327 if (depth > 4)
328 return 0;
329
330 while (p != NULL)
331 {
332 switch (p->c)
333 {
334 case NFA_BOL:
335 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100336 return 1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200337
338 case NFA_ZSTART:
339 case NFA_ZEND:
340 case NFA_CURSOR:
341 case NFA_VISUAL:
342
343 case NFA_MOPEN:
344 case NFA_MOPEN1:
345 case NFA_MOPEN2:
346 case NFA_MOPEN3:
347 case NFA_MOPEN4:
348 case NFA_MOPEN5:
349 case NFA_MOPEN6:
350 case NFA_MOPEN7:
351 case NFA_MOPEN8:
352 case NFA_MOPEN9:
353 case NFA_NOPEN:
354#ifdef FEAT_SYN_HL
355 case NFA_ZOPEN:
356 case NFA_ZOPEN1:
357 case NFA_ZOPEN2:
358 case NFA_ZOPEN3:
359 case NFA_ZOPEN4:
360 case NFA_ZOPEN5:
361 case NFA_ZOPEN6:
362 case NFA_ZOPEN7:
363 case NFA_ZOPEN8:
364 case NFA_ZOPEN9:
365#endif
366 p = p->out;
367 break;
368
369 case NFA_SPLIT:
370 return nfa_get_reganch(p->out, depth + 1)
371 && nfa_get_reganch(p->out1, depth + 1);
372
373 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100374 return 0; // noooo
Bram Moolenaard89616e2013-06-06 18:46:06 +0200375 }
376 }
377 return 0;
378}
379
380/*
381 * Figure out if the NFA state list starts with a character which must match
382 * at start of the match.
383 */
384 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100385nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200386{
387 nfa_state_T *p = start;
388
389 if (depth > 4)
390 return 0;
391
392 while (p != NULL)
393 {
394 switch (p->c)
395 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100396 // all kinds of zero-width matches
Bram Moolenaard89616e2013-06-06 18:46:06 +0200397 case NFA_BOL:
398 case NFA_BOF:
399 case NFA_BOW:
400 case NFA_EOW:
401 case NFA_ZSTART:
402 case NFA_ZEND:
403 case NFA_CURSOR:
404 case NFA_VISUAL:
405 case NFA_LNUM:
406 case NFA_LNUM_GT:
407 case NFA_LNUM_LT:
408 case NFA_COL:
409 case NFA_COL_GT:
410 case NFA_COL_LT:
411 case NFA_VCOL:
412 case NFA_VCOL_GT:
413 case NFA_VCOL_LT:
414 case NFA_MARK:
415 case NFA_MARK_GT:
416 case NFA_MARK_LT:
417
418 case NFA_MOPEN:
419 case NFA_MOPEN1:
420 case NFA_MOPEN2:
421 case NFA_MOPEN3:
422 case NFA_MOPEN4:
423 case NFA_MOPEN5:
424 case NFA_MOPEN6:
425 case NFA_MOPEN7:
426 case NFA_MOPEN8:
427 case NFA_MOPEN9:
428 case NFA_NOPEN:
429#ifdef FEAT_SYN_HL
430 case NFA_ZOPEN:
431 case NFA_ZOPEN1:
432 case NFA_ZOPEN2:
433 case NFA_ZOPEN3:
434 case NFA_ZOPEN4:
435 case NFA_ZOPEN5:
436 case NFA_ZOPEN6:
437 case NFA_ZOPEN7:
438 case NFA_ZOPEN8:
439 case NFA_ZOPEN9:
440#endif
441 p = p->out;
442 break;
443
444 case NFA_SPLIT:
445 {
446 int c1 = nfa_get_regstart(p->out, depth + 1);
447 int c2 = nfa_get_regstart(p->out1, depth + 1);
448
449 if (c1 == c2)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100450 return c1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200451 return 0;
452 }
453
454 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200455 if (p->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100456 return p->c; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200457 return 0;
458 }
459 }
460 return 0;
461}
462
463/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200464 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200465 * else. If so return a string in allocated memory with what must match after
466 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200467 */
468 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100469nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200470{
471 nfa_state_T *p = start;
472 int len = 0;
473 char_u *ret;
474 char_u *s;
475
476 if (p->c != NFA_MOPEN)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 return NULL; // just in case
Bram Moolenaar473de612013-06-08 18:19:48 +0200478 p = p->out;
479 while (p->c > 0)
480 {
481 len += MB_CHAR2LEN(p->c);
482 p = p->out;
483 }
484 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
485 return NULL;
486
487 ret = alloc(len);
488 if (ret != NULL)
489 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100490 p = start->out->out; // skip first char, it goes into regstart
Bram Moolenaar473de612013-06-08 18:19:48 +0200491 s = ret;
492 while (p->c > 0)
493 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200494 if (has_mbyte)
495 s += (*mb_char2bytes)(p->c, s);
496 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200497 *s++ = p->c;
498 p = p->out;
499 }
500 *s = NUL;
501 }
502 return ret;
503}
504
505/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200506 * Allocate more space for post_start. Called when
507 * running above the estimated number of states.
508 */
509 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100510realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200511{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200512 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100513 int new_max;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200514 int *new_start;
515 int *old_start;
516
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100517 // For weird patterns the number of states can be very high. Increasing by
518 // 50% seems a reasonable compromise between memory use and speed.
519 new_max = nstate_max * 3 / 2;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200520 new_start = ALLOC_MULT(int, new_max);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200521 if (new_start == NULL)
522 return FAIL;
523 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200524 old_start = post_start;
525 post_start = new_start;
526 post_ptr = new_start + (post_ptr - old_start);
527 post_end = post_start + new_max;
528 vim_free(old_start);
529 return OK;
530}
531
532/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200533 * Search between "start" and "end" and try to recognize a
534 * character class in expanded form. For example [0-9].
535 * On success, return the id the character class to be emitted.
536 * On failure, return 0 (=FAIL)
537 * Start points to the first char of the range, while end should point
538 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200539 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
540 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200541 */
542 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100543nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200544{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200545# define CLASS_not 0x80
546# define CLASS_af 0x40
547# define CLASS_AF 0x20
548# define CLASS_az 0x10
549# define CLASS_AZ 0x08
550# define CLASS_o7 0x04
551# define CLASS_o9 0x02
552# define CLASS_underscore 0x01
553
554 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200555 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200556 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200557
558 if (extra_newl == TRUE)
559 newl = TRUE;
560
561 if (*end != ']')
562 return FAIL;
563 p = start;
564 if (*p == '^')
565 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200566 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200567 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200568 }
569
570 while (p < end)
571 {
572 if (p + 2 < end && *(p + 1) == '-')
573 {
574 switch (*p)
575 {
576 case '0':
577 if (*(p + 2) == '9')
578 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200579 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200580 break;
581 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 if (*(p + 2) == '7')
583 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200584 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200585 break;
586 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200587 return FAIL;
588
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200589 case 'a':
590 if (*(p + 2) == 'z')
591 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200592 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593 break;
594 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200595 if (*(p + 2) == 'f')
596 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200597 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200598 break;
599 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200600 return FAIL;
601
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200602 case 'A':
603 if (*(p + 2) == 'Z')
604 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200605 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200606 break;
607 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200608 if (*(p + 2) == 'F')
609 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200610 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611 break;
612 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200613 return FAIL;
614
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200615 default:
616 return FAIL;
617 }
618 p += 3;
619 }
620 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
621 {
622 newl = TRUE;
623 p += 2;
624 }
625 else if (*p == '_')
626 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200627 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200628 p ++;
629 }
630 else if (*p == '\n')
631 {
632 newl = TRUE;
633 p ++;
634 }
635 else
636 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100637 } // while (p < end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638
639 if (p != end)
640 return FAIL;
641
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200643 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200644
645 switch (config)
646 {
647 case CLASS_o9:
648 return extra_newl + NFA_DIGIT;
649 case CLASS_not | CLASS_o9:
650 return extra_newl + NFA_NDIGIT;
651 case CLASS_af | CLASS_AF | CLASS_o9:
652 return extra_newl + NFA_HEX;
653 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
654 return extra_newl + NFA_NHEX;
655 case CLASS_o7:
656 return extra_newl + NFA_OCTAL;
657 case CLASS_not | CLASS_o7:
658 return extra_newl + NFA_NOCTAL;
659 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
660 return extra_newl + NFA_WORD;
661 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
662 return extra_newl + NFA_NWORD;
663 case CLASS_az | CLASS_AZ | CLASS_underscore:
664 return extra_newl + NFA_HEAD;
665 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
666 return extra_newl + NFA_NHEAD;
667 case CLASS_az | CLASS_AZ:
668 return extra_newl + NFA_ALPHA;
669 case CLASS_not | CLASS_az | CLASS_AZ:
670 return extra_newl + NFA_NALPHA;
671 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200672 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200673 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200674 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200675 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200676 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200677 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200678 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200681}
682
683/*
684 * Produce the bytes for equivalence class "c".
685 * Currently only handles latin1, latin9 and utf-8.
686 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
687 * equivalent to 'a OR b OR c'
688 *
689 * NOTE! When changing this function, also update reg_equi_class()
690 */
691 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100692nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200693{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200694#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100695#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200697 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
698 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200699 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200700#ifdef EBCDIC
701# define A_circumflex 0x62
702# define A_diaeresis 0x63
703# define A_grave 0x64
704# define A_acute 0x65
705# define A_virguilla 0x66
706# define A_ring 0x67
707# define C_cedilla 0x68
708# define E_acute 0x71
709# define E_circumflex 0x72
710# define E_diaeresis 0x73
711# define E_grave 0x74
712# define I_acute 0x75
713# define I_circumflex 0x76
714# define I_diaeresis 0x77
715# define I_grave 0x78
716# define N_virguilla 0x69
717# define O_circumflex 0xeb
718# define O_diaeresis 0xec
719# define O_grave 0xed
720# define O_acute 0xee
721# define O_virguilla 0xef
722# define O_slash 0x80
723# define U_circumflex 0xfb
724# define U_diaeresis 0xfc
725# define U_grave 0xfd
726# define U_acute 0xfe
727# define Y_acute 0xba
728# define a_grave 0x42
729# define a_acute 0x43
730# define a_circumflex 0x44
731# define a_virguilla 0x45
732# define a_diaeresis 0x46
733# define a_ring 0x47
734# define c_cedilla 0x48
735# define e_grave 0x51
736# define e_acute 0x52
737# define e_circumflex 0x53
738# define e_diaeresis 0x54
739# define i_grave 0x55
740# define i_acute 0x56
741# define i_circumflex 0x57
742# define i_diaeresis 0x58
743# define n_virguilla 0x49
744# define o_grave 0xcb
745# define o_acute 0xcc
746# define o_circumflex 0xcd
747# define o_virguilla 0xce
748# define o_diaeresis 0xcf
749# define o_slash 0x70
750# define u_grave 0xdb
751# define u_acute 0xdc
752# define u_circumflex 0xdd
753# define u_diaeresis 0xde
754# define y_acute 0x8d
755# define y_diaeresis 0xdf
756#else
757# define A_grave 0xc0
758# define A_acute 0xc1
759# define A_circumflex 0xc2
760# define A_virguilla 0xc3
761# define A_diaeresis 0xc4
762# define A_ring 0xc5
763# define C_cedilla 0xc7
764# define E_grave 0xc8
765# define E_acute 0xc9
766# define E_circumflex 0xca
767# define E_diaeresis 0xcb
768# define I_grave 0xcc
769# define I_acute 0xcd
770# define I_circumflex 0xce
771# define I_diaeresis 0xcf
772# define N_virguilla 0xd1
773# define O_grave 0xd2
774# define O_acute 0xd3
775# define O_circumflex 0xd4
776# define O_virguilla 0xd5
777# define O_diaeresis 0xd6
778# define O_slash 0xd8
779# define U_grave 0xd9
780# define U_acute 0xda
781# define U_circumflex 0xdb
782# define U_diaeresis 0xdc
783# define Y_acute 0xdd
784# define a_grave 0xe0
785# define a_acute 0xe1
786# define a_circumflex 0xe2
787# define a_virguilla 0xe3
788# define a_diaeresis 0xe4
789# define a_ring 0xe5
790# define c_cedilla 0xe7
791# define e_grave 0xe8
792# define e_acute 0xe9
793# define e_circumflex 0xea
794# define e_diaeresis 0xeb
795# define i_grave 0xec
796# define i_acute 0xed
797# define i_circumflex 0xee
798# define i_diaeresis 0xef
799# define n_virguilla 0xf1
800# define o_grave 0xf2
801# define o_acute 0xf3
802# define o_circumflex 0xf4
803# define o_virguilla 0xf5
804# define o_diaeresis 0xf6
805# define o_slash 0xf8
806# define u_grave 0xf9
807# define u_acute 0xfa
808# define u_circumflex 0xfb
809# define u_diaeresis 0xfc
810# define y_acute 0xfd
811# define y_diaeresis 0xff
812#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200813 switch (c)
814 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200815 case 'A': case A_grave: case A_acute: case A_circumflex:
816 case A_virguilla: case A_diaeresis: case A_ring:
817 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
818 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
819 CASEMBC(0x1ea2)
820 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
821 EMIT2(A_circumflex); EMIT2(A_virguilla);
822 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200823 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
824 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
825 EMITMBC(0x1ea2)
826 return OK;
827
828 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
829 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200830 return OK;
831
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200832 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
833 CASEMBC(0x10a) CASEMBC(0x10c)
834 EMIT2('C'); EMIT2(C_cedilla);
835 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200836 EMITMBC(0x10a) EMITMBC(0x10c)
837 return OK;
838
839 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200840 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200841 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
842 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843 return OK;
844
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200845 case 'E': case E_grave: case E_acute: case E_circumflex:
846 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
847 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
848 CASEMBC(0x1eba) CASEMBC(0x1ebc)
849 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
850 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200851 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
852 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
853 EMITMBC(0x1ebc)
854 return OK;
855
856 case 'F': CASEMBC(0x1e1e)
857 EMIT2('F'); EMITMBC(0x1e1e)
858 return OK;
859
860 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200861 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
862 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200863 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
864 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
865 EMITMBC(0x1f4) EMITMBC(0x1e20)
866 return OK;
867
868 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200869 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200870 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
871 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200872 return OK;
873
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200874 case 'I': case I_grave: case I_acute: case I_circumflex:
875 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
876 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
877 CASEMBC(0x1cf) CASEMBC(0x1ec8)
878 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
879 EMIT2(I_circumflex); EMIT2(I_diaeresis);
880 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200881 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
882 EMITMBC(0x1cf) EMITMBC(0x1ec8)
883 return OK;
884
885 case 'J': CASEMBC(0x134)
886 EMIT2('J'); EMITMBC(0x134)
887 return OK;
888
889 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200890 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200891 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
892 EMITMBC(0x1e34)
893 return OK;
894
895 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200896 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200897 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
898 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
899 return OK;
900
901 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
902 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200903 return OK;
904
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200905 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
906 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
907 EMIT2('N'); EMIT2(N_virguilla);
908 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200909 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200910 return OK;
911
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200912 case 'O': case O_grave: case O_acute: case O_circumflex:
913 case O_virguilla: case O_diaeresis: case O_slash:
914 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
915 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
916 CASEMBC(0x1ec) CASEMBC(0x1ece)
917 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
918 EMIT2(O_circumflex); EMIT2(O_virguilla);
919 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200920 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
921 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
922 EMITMBC(0x1ec) EMITMBC(0x1ece)
923 return OK;
924
925 case 'P': case 0x1e54: case 0x1e56:
926 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
927 return OK;
928
929 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200930 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200931 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
932 EMITMBC(0x1e58) EMITMBC(0x1e5e)
933 return OK;
934
935 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200936 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200937 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
938 EMITMBC(0x160) EMITMBC(0x1e60)
939 return OK;
940
941 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200942 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200943 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
944 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200945 return OK;
946
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200947 case 'U': case U_grave: case U_acute: case U_diaeresis:
948 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
949 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
950 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
951 CASEMBC(0x1ee6)
952 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
953 EMIT2(U_diaeresis); EMIT2(U_circumflex);
954 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200955 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
956 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
957 EMITMBC(0x1ee6)
958 return OK;
959
960 case 'V': CASEMBC(0x1e7c)
961 EMIT2('V'); EMITMBC(0x1e7c)
962 return OK;
963
964 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200965 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200966 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
967 EMITMBC(0x1e84) EMITMBC(0x1e86)
968 return OK;
969
970 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
971 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200972 return OK;
973
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200974 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
975 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
976 CASEMBC(0x1ef8)
977 EMIT2('Y'); EMIT2(Y_acute);
978 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200979 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
980 EMITMBC(0x1ef8)
981 return OK;
982
983 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200984 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200985 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
986 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200987 return OK;
988
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200989 case 'a': case a_grave: case a_acute: case a_circumflex:
990 case a_virguilla: case a_diaeresis: case a_ring:
991 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
992 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
993 CASEMBC(0x1ea3)
994 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
995 EMIT2(a_circumflex); EMIT2(a_virguilla);
996 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200997 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
998 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
999 EMITMBC(0x1ea3)
1000 return OK;
1001
1002 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1003 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001004 return OK;
1005
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001006 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1007 CASEMBC(0x10b) CASEMBC(0x10d)
1008 EMIT2('c'); EMIT2(c_cedilla);
1009 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 EMITMBC(0x10b) EMITMBC(0x10d)
1011 return OK;
1012
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001013 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001014 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001015 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1016 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001017 return OK;
1018
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001019 case 'e': case e_grave: case e_acute: case e_circumflex:
1020 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1021 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1022 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1023 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1024 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1025 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001026 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1027 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1028 return OK;
1029
1030 case 'f': CASEMBC(0x1e1f)
1031 EMIT2('f'); EMITMBC(0x1e1f)
1032 return OK;
1033
1034 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001035 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1036 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001037 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1038 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1039 EMITMBC(0x1f5) EMITMBC(0x1e21)
1040 return OK;
1041
1042 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001043 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001044 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1045 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001046 return OK;
1047
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001048 case 'i': case i_grave: case i_acute: case i_circumflex:
1049 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1050 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1051 CASEMBC(0x1ec9)
1052 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1053 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1054 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001055 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1056 EMITMBC(0x1ec9)
1057 return OK;
1058
1059 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1060 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1061 return OK;
1062
1063 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001064 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001065 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1066 EMITMBC(0x1e35)
1067 return OK;
1068
1069 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001070 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001071 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1072 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1073 return OK;
1074
1075 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1076 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 return OK;
1078
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001079 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1080 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1081 CASEMBC(0x1e49)
1082 EMIT2('n'); EMIT2(n_virguilla);
1083 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001084 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1085 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 return OK;
1087
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001088 case 'o': case o_grave: case o_acute: case o_circumflex:
1089 case o_virguilla: case o_diaeresis: case o_slash:
1090 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1091 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1092 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1093 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1094 EMIT2(o_circumflex); EMIT2(o_virguilla);
1095 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001096 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1097 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1098 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1099 return OK;
1100
1101 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1102 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1103 return OK;
1104
1105 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001106 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001107 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1108 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1109 return OK;
1110
1111 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001112 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001113 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1114 EMITMBC(0x161) EMITMBC(0x1e61)
1115 return OK;
1116
1117 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001118 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001119 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1120 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001121 return OK;
1122
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001123 case 'u': case u_grave: case u_acute: case u_circumflex:
1124 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1125 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1126 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1127 CASEMBC(0x1ee7)
1128 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1129 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1130 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001131 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1132 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1133 EMITMBC(0x1ee7)
1134 return OK;
1135
1136 case 'v': CASEMBC(0x1e7d)
1137 EMIT2('v'); EMITMBC(0x1e7d)
1138 return OK;
1139
1140 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001141 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001142 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1143 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1144 return OK;
1145
1146 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1147 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001148 return OK;
1149
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001150 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1151 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1152 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1153 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1154 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001155 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1156 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001157 return OK;
1158
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001159 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001160 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001161 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1162 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1163 return OK;
1164
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001165 // default: character itself
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001166 }
1167 }
1168
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001169 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001170 return OK;
1171#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001172#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001173}
1174
1175/*
1176 * Code to parse regular expression.
1177 *
1178 * We try to reuse parsing functions in regexp.c to
1179 * minimize surprise and keep the syntax consistent.
1180 */
1181
1182/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001183 * Parse the lowest level.
1184 *
1185 * An atom can be one of a long list of items. Many atoms match one character
1186 * in the text. It is often an ordinary character or a character class.
1187 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1188 * is only for syntax highlighting.
1189 *
1190 * atom ::= ordinary-atom
1191 * or \( pattern \)
1192 * or \%( pattern \)
1193 * or \z( pattern \)
1194 */
1195 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001196nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001197{
1198 int c;
1199 int charclass;
1200 int equiclass;
1201 int collclass;
1202 int got_coll_char;
1203 char_u *p;
1204 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001207 int emit_range;
1208 int negated;
1209 int result;
1210 int startc = -1;
1211 int endc = -1;
1212 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001213 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001215 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001216 switch (c)
1217 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001218 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001219 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001220
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 case Magic('^'):
1222 EMIT(NFA_BOL);
1223 break;
1224
1225 case Magic('$'):
1226 EMIT(NFA_EOL);
1227#if defined(FEAT_SYN_HL) || defined(PROTO)
1228 had_eol = TRUE;
1229#endif
1230 break;
1231
1232 case Magic('<'):
1233 EMIT(NFA_BOW);
1234 break;
1235
1236 case Magic('>'):
1237 EMIT(NFA_EOW);
1238 break;
1239
1240 case Magic('_'):
1241 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001242 if (c == NUL)
1243 EMSG_RET_FAIL(_(e_nul_found));
1244
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001245 if (c == '^') // "\_^" is start-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 {
1247 EMIT(NFA_BOL);
1248 break;
1249 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001250 if (c == '$') // "\_$" is end-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001251 {
1252 EMIT(NFA_EOL);
1253#if defined(FEAT_SYN_HL) || defined(PROTO)
1254 had_eol = TRUE;
1255#endif
1256 break;
1257 }
1258
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001259 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001261 // "\_[" is collection plus newline
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001263 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001265 // "\_x" is character class plus newline
1266 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267
1268 /*
1269 * Character classes.
1270 */
1271 case Magic('.'):
1272 case Magic('i'):
1273 case Magic('I'):
1274 case Magic('k'):
1275 case Magic('K'):
1276 case Magic('f'):
1277 case Magic('F'):
1278 case Magic('p'):
1279 case Magic('P'):
1280 case Magic('s'):
1281 case Magic('S'):
1282 case Magic('d'):
1283 case Magic('D'):
1284 case Magic('x'):
1285 case Magic('X'):
1286 case Magic('o'):
1287 case Magic('O'):
1288 case Magic('w'):
1289 case Magic('W'):
1290 case Magic('h'):
1291 case Magic('H'):
1292 case Magic('a'):
1293 case Magic('A'):
1294 case Magic('l'):
1295 case Magic('L'):
1296 case Magic('u'):
1297 case Magic('U'):
1298 p = vim_strchr(classchars, no_Magic(c));
1299 if (p == NULL)
1300 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001301 if (extra == NFA_ADD_NL)
1302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001304 rc_did_emsg = TRUE;
1305 return FAIL;
1306 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001307 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001308 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001309 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001310
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001311 // When '.' is followed by a composing char ignore the dot, so that
1312 // the composing char is matched here.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001313 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1314 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001315 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316 c = getchr();
1317 goto nfa_do_multibyte;
1318 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001320 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 {
1322 EMIT(NFA_NEWL);
1323 EMIT(NFA_OR);
1324 regflags |= RF_HASNL;
1325 }
1326 break;
1327
1328 case Magic('n'):
1329 if (reg_string)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001330 // In a string "\n" matches a newline character.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001331 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 else
1333 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001334 // In buffer text "\n" matches the end of a line.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001335 EMIT(NFA_NEWL);
1336 regflags |= RF_HASNL;
1337 }
1338 break;
1339
1340 case Magic('('):
1341 if (nfa_reg(REG_PAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001342 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001343 break;
1344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 case Magic('|'):
1346 case Magic('&'):
1347 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001348 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 return FAIL;
1350
1351 case Magic('='):
1352 case Magic('?'):
1353 case Magic('+'):
1354 case Magic('@'):
1355 case Magic('*'):
1356 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001357 // these should follow an atom, not form an atom
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 return FAIL;
1360
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001361 case Magic('~'):
1362 {
1363 char_u *lp;
1364
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001365 // Previous substitute pattern.
1366 // Generated as "\%(pattern\)".
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001367 if (reg_prev_sub == NULL)
1368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001369 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001370 return FAIL;
1371 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001372 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001373 {
1374 EMIT(PTR2CHAR(lp));
1375 if (lp != reg_prev_sub)
1376 EMIT(NFA_CONCAT);
1377 }
1378 EMIT(NFA_NOPEN);
1379 break;
1380 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001381
Bram Moolenaar428e9872013-05-30 17:05:39 +02001382 case Magic('1'):
1383 case Magic('2'):
1384 case Magic('3'):
1385 case Magic('4'):
1386 case Magic('5'):
1387 case Magic('6'):
1388 case Magic('7'):
1389 case Magic('8'):
1390 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001391 {
1392 int refnum = no_Magic(c) - '1';
1393
1394 if (!seen_endbrace(refnum + 1))
1395 return FAIL;
1396 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001397 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001398 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001399 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001400
1401 case Magic('z'):
1402 c = no_Magic(getchr());
1403 switch (c)
1404 {
1405 case 's':
1406 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001407 if (re_mult_next("\\zs") == FAIL)
1408 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001409 break;
1410 case 'e':
1411 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001412 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001413 if (re_mult_next("\\ze") == FAIL)
1414 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001415 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001416#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 case '1':
1418 case '2':
1419 case '3':
1420 case '4':
1421 case '5':
1422 case '6':
1423 case '7':
1424 case '8':
1425 case '9':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001426 // \z1...\z9
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001427 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001428 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001429 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001430 // No need to set rex.nfa_has_backref, the sub-matches don't
1431 // change when \z1 .. \z9 matches or not.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001432 re_has_z = REX_USE;
1433 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434 case '(':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001435 // \z(
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001436 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001437 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001438 if (nfa_reg(REG_ZPAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001439 return FAIL; // cascaded error
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001440 re_has_z = REX_SET;
1441 break;
1442#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001443 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001444 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001445 no_Magic(c));
1446 return FAIL;
1447 }
1448 break;
1449
1450 case Magic('%'):
1451 c = no_Magic(getchr());
1452 switch (c)
1453 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001454 // () without a back reference
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001455 case '(':
1456 if (nfa_reg(REG_NPAREN) == FAIL)
1457 return FAIL;
1458 EMIT(NFA_NOPEN);
1459 break;
1460
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001461 case 'd': // %d123 decimal
1462 case 'o': // %o123 octal
1463 case 'x': // %xab hex 2
1464 case 'u': // %uabcd hex 4
1465 case 'U': // %U1234abcd hex 8
Bram Moolenaar47196582013-05-25 22:04:23 +02001466 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001467 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001468
Bram Moolenaar47196582013-05-25 22:04:23 +02001469 switch (c)
1470 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001471 case 'd': nr = getdecchrs(); break;
1472 case 'o': nr = getoctchrs(); break;
1473 case 'x': nr = gethexchrs(2); break;
1474 case 'u': nr = gethexchrs(4); break;
1475 case 'U': nr = gethexchrs(8); break;
1476 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001477 }
1478
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001479 if (nr < 0 || nr > INT_MAX)
Bram Moolenaar47196582013-05-25 22:04:23 +02001480 EMSG2_RET_FAIL(
1481 _("E678: Invalid character after %s%%[dxouU]"),
1482 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001483 // A NUL is stored in the text as NL
1484 // TODO: what if a composing character follows?
Bram Moolenaar595cad22013-09-22 13:57:24 +02001485 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001486 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 break;
1488
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001489 // Catch \%^ and \%$ regardless of where they appear in the
1490 // pattern -- regardless of whether or not it makes sense.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001491 case '^':
1492 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 break;
1494
1495 case '$':
1496 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 break;
1498
1499 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001500 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 break;
1502
1503 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001504 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001505 break;
1506
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001507 case 'C':
1508 EMIT(NFA_ANY_COMPOSING);
1509 break;
1510
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001512 {
1513 int n;
1514
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001515 // \%[abc]
Bram Moolenaard7986252013-06-17 21:33:41 +02001516 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001517 {
1518 if (c == NUL)
1519 EMSG2_RET_FAIL(_(e_missing_sb),
1520 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001521 // recursive call!
Bram Moolenaard7986252013-06-17 21:33:41 +02001522 if (nfa_regatom() == FAIL)
1523 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001524 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001525 getchr(); // get the ]
Bram Moolenaar2976c022013-06-05 21:30:37 +02001526 if (n == 0)
1527 EMSG2_RET_FAIL(_(e_empty_sb),
1528 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001529 EMIT(NFA_OPT_CHARS);
1530 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001531
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001532 // Emit as "\%(\%[abc]\)" to be able to handle
1533 // "\%[abc]*" which would cause the empty string to be
1534 // matched an unlimited number of times. NFA_NOPEN is
1535 // added only once at a position, while NFA_SPLIT is
1536 // added multiple times. This is more efficient than
1537 // not allowing NFA_SPLIT multiple times, it is used
1538 // a lot.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001539 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001540 break;
1541 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001542
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001544 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001545 long_u n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001546 int cmp = c;
1547
1548 if (c == '<' || c == '>')
1549 c = getchr();
1550 while (VIM_ISDIGIT(c))
1551 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001552 long_u tmp = n * 10 + (c - '0');
1553
1554 if (tmp < n)
1555 {
1556 // overflow.
1557 emsg(_(e_value_too_large));
1558 return FAIL;
1559 }
1560 n = tmp;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001561 c = getchr();
1562 }
1563 if (c == 'l' || c == 'c' || c == 'v')
1564 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001565 long_u limit = INT_MAX;
Bram Moolenaar9403a212019-02-13 18:35:06 +01001566
Bram Moolenaar423532e2013-05-29 21:14:42 +02001567 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001568 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001569 // \%{n}l \%{n}<l \%{n}>l
Bram Moolenaar423532e2013-05-29 21:14:42 +02001570 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001571 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001572 if (save_prev_at_start)
1573 at_start = TRUE;
1574 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001575 else if (c == 'c')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001576 // \%{n}c \%{n}<c \%{n}>c
Bram Moolenaar423532e2013-05-29 21:14:42 +02001577 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001578 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001579 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001580 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001581 // \%{n}v \%{n}<v \%{n}>v
Bram Moolenaar423532e2013-05-29 21:14:42 +02001582 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001583 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001584 limit = INT_MAX / MB_MAXBYTES;
1585 }
1586 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001587 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001588 emsg(_(e_value_too_large));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001589 return FAIL;
1590 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001591 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001592 break;
1593 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001594 else if (c == '\'' && n == 0)
1595 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001596 // \%'m \%<'m \%>'m
Bram Moolenaar044aa292013-06-04 21:27:38 +02001597 EMIT(cmp == '<' ? NFA_MARK_LT :
1598 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001599 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001600 break;
1601 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001602 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001603 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001604 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001605 return FAIL;
1606 }
1607 break;
1608
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001609 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001610collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001611 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001612 * [abc] uses NFA_START_COLL - NFA_END_COLL
1613 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1614 * Each character is produced as a regular state, using
1615 * NFA_CONCAT to bind them together.
1616 * Besides normal characters there can be:
1617 * - character classes NFA_CLASS_*
1618 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 */
1620
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001621 p = regparse;
1622 endp = skip_anyof(p);
1623 if (*endp == ']')
1624 {
1625 /*
1626 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001627 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 * and perform the necessary substitutions in the NFA.
1629 */
1630 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001631 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001632 if (result != FAIL)
1633 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001634 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001636 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 EMIT(NFA_NEWL);
1638 EMIT(NFA_OR);
1639 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001640 else
1641 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001643 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 return OK;
1645 }
1646 /*
1647 * Failed to recognize a character class. Use the simple
1648 * version that turns [abc] into 'a' OR 'b' OR 'c'
1649 */
1650 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 negated = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001652 if (*regparse == '^') // negated range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 {
1654 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001655 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001656 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001658 else
1659 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001660 if (*regparse == '-')
1661 {
1662 startc = '-';
1663 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001664 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001665 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001667 // Emit the OR branches for each character in the []
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 emit_range = FALSE;
1669 while (regparse < endp)
1670 {
1671 oldstartc = startc;
1672 startc = -1;
1673 got_coll_char = FALSE;
1674 if (*regparse == '[')
1675 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001676 // Check for [: :], [= =], [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 equiclass = collclass = 0;
1678 charclass = get_char_class(&regparse);
1679 if (charclass == CLASS_NONE)
1680 {
1681 equiclass = get_equi_class(&regparse);
1682 if (equiclass == 0)
1683 collclass = get_coll_element(&regparse);
1684 }
1685
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001686 // Character class like [:alpha:]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001687 if (charclass != CLASS_NONE)
1688 {
1689 switch (charclass)
1690 {
1691 case CLASS_ALNUM:
1692 EMIT(NFA_CLASS_ALNUM);
1693 break;
1694 case CLASS_ALPHA:
1695 EMIT(NFA_CLASS_ALPHA);
1696 break;
1697 case CLASS_BLANK:
1698 EMIT(NFA_CLASS_BLANK);
1699 break;
1700 case CLASS_CNTRL:
1701 EMIT(NFA_CLASS_CNTRL);
1702 break;
1703 case CLASS_DIGIT:
1704 EMIT(NFA_CLASS_DIGIT);
1705 break;
1706 case CLASS_GRAPH:
1707 EMIT(NFA_CLASS_GRAPH);
1708 break;
1709 case CLASS_LOWER:
1710 EMIT(NFA_CLASS_LOWER);
1711 break;
1712 case CLASS_PRINT:
1713 EMIT(NFA_CLASS_PRINT);
1714 break;
1715 case CLASS_PUNCT:
1716 EMIT(NFA_CLASS_PUNCT);
1717 break;
1718 case CLASS_SPACE:
1719 EMIT(NFA_CLASS_SPACE);
1720 break;
1721 case CLASS_UPPER:
1722 EMIT(NFA_CLASS_UPPER);
1723 break;
1724 case CLASS_XDIGIT:
1725 EMIT(NFA_CLASS_XDIGIT);
1726 break;
1727 case CLASS_TAB:
1728 EMIT(NFA_CLASS_TAB);
1729 break;
1730 case CLASS_RETURN:
1731 EMIT(NFA_CLASS_RETURN);
1732 break;
1733 case CLASS_BACKSPACE:
1734 EMIT(NFA_CLASS_BACKSPACE);
1735 break;
1736 case CLASS_ESCAPE:
1737 EMIT(NFA_CLASS_ESCAPE);
1738 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001739 case CLASS_IDENT:
1740 EMIT(NFA_CLASS_IDENT);
1741 break;
1742 case CLASS_KEYWORD:
1743 EMIT(NFA_CLASS_KEYWORD);
1744 break;
1745 case CLASS_FNAME:
1746 EMIT(NFA_CLASS_FNAME);
1747 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001748 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001749 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001750 continue;
1751 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001752 // Try equivalence class [=a=] and the like
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001753 if (equiclass != 0)
1754 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001755 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001756 if (result == FAIL)
1757 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001758 // should never happen
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001759 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1760 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001761 continue;
1762 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001763 // Try collating class like [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001764 if (collclass != 0)
1765 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001766 startc = collclass; // allow [.a.]-x as a range
1767 // Will emit the proper atom at the end of the
1768 // while loop.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 }
1770 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001771 // Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1772 // start character.
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001773 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001774 {
1775 emit_range = TRUE;
1776 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001777 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001778 continue; // reading the end of the range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 }
1780
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001781 // Now handle simple and escaped characters.
1782 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1783 // accepts "\t", "\e", etc., but only when the 'l' flag in
1784 // 'cpoptions' is not included.
1785 // Posix doesn't recognize backslash at all.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001787 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 && regparse + 1 <= endp
1789 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001790 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001791 && vim_strchr(REGEXP_ABBR, regparse[1])
1792 != NULL)
1793 )
1794 )
1795 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001796 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001798 if (*regparse == 'n')
Bram Moolenaara5483442019-02-17 20:17:02 +01001799 startc = (reg_string || emit_range
1800 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001801 else if (*regparse == 'd'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001802 || *regparse == 'o'
1803 || *regparse == 'x'
1804 || *regparse == 'u'
1805 || *regparse == 'U'
1806 )
1807 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001808 // TODO(RE) This needs more testing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809 startc = coll_get_char();
1810 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001811 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812 }
1813 else
1814 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001815 // \r,\t,\e,\b
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 startc = backslash_trans(*regparse);
1817 }
1818 }
1819
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001820 // Normal printable char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001822 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001824 // Previous char was '-', so this char is end of range.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 if (emit_range)
1826 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001827 endc = startc;
1828 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001830 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001831
1832 if (endc > startc + 2)
1833 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001834 // Emit a range instead of the sequence of
1835 // individual characters.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001836 if (startc == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001837 // \x00 is translated to \x0a, start at \x01.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001838 EMIT(1);
1839 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001840 --post_ptr; // remove NFA_CONCAT
Bram Moolenaar417bad22013-06-07 14:08:30 +02001841 EMIT(endc);
1842 EMIT(NFA_RANGE);
1843 EMIT(NFA_CONCAT);
1844 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001845 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001846 || (*mb_char2len)(endc) > 1))
1847 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001848 // Emit the characters in the range.
1849 // "startc" was already emitted, so skip it.
1850 //
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 for (c = startc + 1; c <= endc; c++)
1852 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001853 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001854 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 }
1857 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 {
1859#ifdef EBCDIC
1860 int alpha_only = FALSE;
1861
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001862 // for alphabetical range skip the gaps
1863 // 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001864 if (isalpha(startc) && isalpha(endc))
1865 alpha_only = TRUE;
1866#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001867 // Emit the range. "startc" was already emitted, so
1868 // skip it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001869 for (c = startc + 1; c <= endc; c++)
1870#ifdef EBCDIC
1871 if (!alpha_only || isalpha(startc))
1872#endif
1873 {
1874 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001875 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001876 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001878 emit_range = FALSE;
1879 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 }
1881 else
1882 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001883 // This char (startc) is not part of a range. Just
1884 // emit it.
1885 // Normally, simply emit startc. But if we get char
1886 // code=0 from a collating char, then replace it with
1887 // 0x0a.
1888 // This is needed to completely mimic the behaviour of
1889 // the backtracking engine.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001890 if (startc == NFA_NEWL)
1891 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001892 // Line break can't be matched as part of the
1893 // collection, add an OR below. But not for negated
1894 // range.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001895 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001896 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001897 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001899 {
1900 if (got_coll_char == TRUE && startc == 0)
1901 EMIT(0x0a);
1902 else
1903 EMIT(startc);
1904 EMIT(NFA_CONCAT);
1905 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906 }
1907
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001908 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001909 } // while (p < endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001911 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001912 if (*regparse == '-') // if last, '-' is just a char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001913 {
1914 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001915 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001916 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001917
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001918 // skip the trailing ]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001920 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001921
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001922 // Mark end of the collection.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001924 EMIT(NFA_END_NEG_COLL);
1925 else
1926 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001927
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001928 // \_[] also matches \n but it's not negated
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001929 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001930 {
1931 EMIT(reg_string ? NL : NFA_NEWL);
1932 EMIT(NFA_OR);
1933 }
1934
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 return OK;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001936 } // if exists closing ]
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001937
1938 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001940 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001942 default:
1943 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944 int plen;
1945
1946nfa_do_multibyte:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001947 // plen is length of current char with composing chars
Bram Moolenaar47196582013-05-25 22:04:23 +02001948 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001949 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001950 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001952 int i = 0;
1953
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001954 // A base character plus composing characters, or just one
1955 // or more composing characters.
1956 // This requires creating a separate atom as if enclosing
1957 // the characters in (), where NFA_COMPOSING is the ( and
1958 // NFA_END_COMPOSING is the ). Note that right now we are
1959 // building the postfix form, not the NFA itself;
1960 // a composing char could be: a, b, c, NFA_COMPOSING
1961 // where 'b' and 'c' are chars with codes > 256.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001962 for (;;)
1963 {
1964 EMIT(c);
1965 if (i > 0)
1966 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001967 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001968 break;
1969 c = utf_ptr2char(old_regparse + i);
1970 }
1971 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001972 regparse = old_regparse + plen;
1973 }
1974 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001975 {
1976 c = no_Magic(c);
1977 EMIT(c);
1978 }
1979 return OK;
1980 }
1981 }
1982
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983 return OK;
1984}
1985
1986/*
1987 * Parse something followed by possible [*+=].
1988 *
1989 * A piece is an atom, possibly followed by a multi, an indication of how many
1990 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1991 * characters: "", "a", "aa", etc.
1992 *
1993 * piece ::= atom
1994 * or atom multi
1995 */
1996 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001997nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001998{
1999 int i;
2000 int op;
2001 int ret;
2002 long minval, maxval;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002003 int greedy = TRUE; // Braces are prefixed with '-' ?
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002004 parse_state_T old_state;
2005 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002006 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002007 int old_post_pos;
2008 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009 int quest;
2010
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002011 // Save the current parse state, so that we can use it if <atom>{m,n} is
2012 // next.
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002013 save_parse_state(&old_state);
2014
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002015 // store current pos in the postfix form, for \{m,n} involving 0s
Bram Moolenaar16299b52013-05-30 18:45:23 +02002016 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002017
2018 ret = nfa_regatom();
2019 if (ret == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002020 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002021
2022 op = peekchr();
2023 if (re_multi_type(op) == NOT_MULTI)
2024 return OK;
2025
2026 skipchr();
2027 switch (op)
2028 {
2029 case Magic('*'):
2030 EMIT(NFA_STAR);
2031 break;
2032
2033 case Magic('+'):
2034 /*
2035 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2036 * first and only submatch would be "aaa". But the backtracking
2037 * engine interprets the plus as "try matching one more time", and
2038 * a* matches a second time at the end of the input, the empty
2039 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002040 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002041 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002042 * In order to be consistent with the old engine, we replace
2043 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002045 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 curchr = -1;
2047 if (nfa_regatom() == FAIL)
2048 return FAIL;
2049 EMIT(NFA_STAR);
2050 EMIT(NFA_CONCAT);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002051 skipchr(); // skip the \+
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002052 break;
2053
2054 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002055 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002057 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 switch(op)
2059 {
2060 case '=':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002061 // \@=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002062 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002064 case '!':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002065 // \@!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002066 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002067 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002069 op = no_Magic(getchr());
2070 if (op == '=')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002071 // \@<=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002072 i = NFA_PREV_ATOM_JUST_BEFORE;
2073 else if (op == '!')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002074 // \@<!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002075 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2076 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 case '>':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002078 // \@>
Bram Moolenaar87953742013-06-05 18:52:40 +02002079 i = NFA_PREV_ATOM_LIKE_PATTERN;
2080 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002082 if (i == 0)
2083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002084 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002085 return FAIL;
2086 }
2087 EMIT(i);
2088 if (i == NFA_PREV_ATOM_JUST_BEFORE
2089 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2090 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002091 break;
2092
2093 case Magic('?'):
2094 case Magic('='):
2095 EMIT(NFA_QUEST);
2096 break;
2097
2098 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002099 // a{2,5} will expand to 'aaa?a?a?'
2100 // a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2101 // version of '?'
2102 // \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2103 // parenthesis have the same id
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104
2105 greedy = TRUE;
2106 c2 = peekchr();
2107 if (c2 == '-' || c2 == Magic('-'))
2108 {
2109 skipchr();
2110 greedy = FALSE;
2111 }
2112 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002113 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002114
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002115 // <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2116 // <atom>*
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002117 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002119 if (greedy) // { { (match the braces)
2120 // \{}, \{0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002121 EMIT(NFA_STAR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002122 else // { { (match the braces)
2123 // \{-}, \{-0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002124 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002125 break;
2126 }
2127
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002128 // Special case: x{0} or x{-0}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002129 if (maxval == 0)
2130 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002131 // Ignore result of previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002132 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002133 // NFA_EMPTY is 0-length and works everywhere
Bram Moolenaar699c1202013-09-25 16:41:54 +02002134 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002135 return OK;
2136 }
2137
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002138 // The engine is very inefficient (uses too many states) when the
2139 // maximum is much larger than the minimum and when the maximum is
2140 // large. Bail out if we can use the other engine.
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002141 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002142 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002143 return FAIL;
2144
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002145 // Ignore previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002146 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002147 // Save parse state after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002148 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002149
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2151 for (i = 0; i < maxval; i++)
2152 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002153 // Goto beginning of the repeated atom
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002154 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002155 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002156 if (nfa_regatom() == FAIL)
2157 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002158 // after "minval" times, atoms are optional
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002159 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002160 {
2161 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002162 {
2163 if (greedy)
2164 EMIT(NFA_STAR);
2165 else
2166 EMIT(NFA_STAR_NONGREEDY);
2167 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002168 else
2169 EMIT(quest);
2170 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002171 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002172 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002173 if (i + 1 > minval && maxval == MAX_LIMIT)
2174 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002175 }
2176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002177 // Go to just after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002178 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179 curchr = -1;
2180
2181 break;
2182
2183
2184 default:
2185 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002186 } // end switch
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187
2188 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002189 // Can't have a multi follow a multi.
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002190 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002191
2192 return OK;
2193}
2194
2195/*
2196 * Parse one or more pieces, concatenated. It matches a match for the
2197 * first piece, followed by a match for the second piece, etc. Example:
2198 * "f[0-9]b", first matches "f", then a digit and then "b".
2199 *
2200 * concat ::= piece
2201 * or piece piece
2202 * or piece piece piece
2203 * etc.
2204 */
2205 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002206nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207{
2208 int cont = TRUE;
2209 int first = TRUE;
2210
2211 while (cont)
2212 {
2213 switch (peekchr())
2214 {
2215 case NUL:
2216 case Magic('|'):
2217 case Magic('&'):
2218 case Magic(')'):
2219 cont = FALSE;
2220 break;
2221
2222 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002223 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 skipchr_keepstart();
2225 break;
2226 case Magic('c'):
2227 regflags |= RF_ICASE;
2228 skipchr_keepstart();
2229 break;
2230 case Magic('C'):
2231 regflags |= RF_NOICASE;
2232 skipchr_keepstart();
2233 break;
2234 case Magic('v'):
2235 reg_magic = MAGIC_ALL;
2236 skipchr_keepstart();
2237 curchr = -1;
2238 break;
2239 case Magic('m'):
2240 reg_magic = MAGIC_ON;
2241 skipchr_keepstart();
2242 curchr = -1;
2243 break;
2244 case Magic('M'):
2245 reg_magic = MAGIC_OFF;
2246 skipchr_keepstart();
2247 curchr = -1;
2248 break;
2249 case Magic('V'):
2250 reg_magic = MAGIC_NONE;
2251 skipchr_keepstart();
2252 curchr = -1;
2253 break;
2254
2255 default:
2256 if (nfa_regpiece() == FAIL)
2257 return FAIL;
2258 if (first == FALSE)
2259 EMIT(NFA_CONCAT);
2260 else
2261 first = FALSE;
2262 break;
2263 }
2264 }
2265
2266 return OK;
2267}
2268
2269/*
2270 * Parse a branch, one or more concats, separated by "\&". It matches the
2271 * last concat, but only if all the preceding concats also match at the same
2272 * position. Examples:
2273 * "foobeep\&..." matches "foo" in "foobeep".
2274 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2275 *
2276 * branch ::= concat
2277 * or concat \& concat
2278 * or concat \& concat \& concat
2279 * etc.
2280 */
2281 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002282nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002284 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285
Bram Moolenaar16299b52013-05-30 18:45:23 +02002286 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002288 // First branch, possibly the only one
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002289 if (nfa_regconcat() == FAIL)
2290 return FAIL;
2291
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002292 // Try next concats
Bram Moolenaar890dd052017-12-16 19:59:37 +01002293 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002294 {
2295 skipchr();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002296 // if concat is empty do emit a node
Bram Moolenaar890dd052017-12-16 19:59:37 +01002297 if (old_post_pos == (int)(post_ptr - post_start))
2298 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002299 EMIT(NFA_NOPEN);
2300 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002301 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302 if (nfa_regconcat() == FAIL)
2303 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002304 // if concat is empty do emit a node
Bram Moolenaar16299b52013-05-30 18:45:23 +02002305 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002306 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002307 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002308 }
2309
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002310 // if a branch is empty, emit one node for it
Bram Moolenaar16299b52013-05-30 18:45:23 +02002311 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002312 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002313
2314 return OK;
2315}
2316
2317/*
2318 * Parse a pattern, one or more branches, separated by "\|". It matches
2319 * anything that matches one of the branches. Example: "foo\|beep" matches
2320 * "foo" and matches "beep". If more than one branch matches, the first one
2321 * is used.
2322 *
2323 * pattern ::= branch
2324 * or branch \| branch
2325 * or branch \| branch \| branch
2326 * etc.
2327 */
2328 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002329nfa_reg(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002330 int paren) // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002331{
2332 int parno = 0;
2333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 if (paren == REG_PAREN)
2335 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002336 if (regnpar >= NSUBEXP) // Too many `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002337 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002338 parno = regnpar++;
2339 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002340#ifdef FEAT_SYN_HL
2341 else if (paren == REG_ZPAREN)
2342 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002343 // Make a ZOPEN node.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002344 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002345 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002346 parno = regnzpar++;
2347 }
2348#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349
2350 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002351 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352
2353 while (peekchr() == Magic('|'))
2354 {
2355 skipchr();
2356 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002357 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 EMIT(NFA_OR);
2359 }
2360
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002361 // Check for proper termination.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2363 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364 if (paren == REG_NPAREN)
2365 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2366 else
2367 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2368 }
2369 else if (paren == REG_NOPAREN && peekchr() != NUL)
2370 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371 if (peekchr() == Magic(')'))
2372 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2373 else
2374 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2375 }
2376 /*
2377 * Here we set the flag allowing back references to this set of
2378 * parentheses.
2379 */
2380 if (paren == REG_PAREN)
2381 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002382 had_endbrace[parno] = TRUE; // have seen the close paren
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002383 EMIT(NFA_MOPEN + parno);
2384 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002385#ifdef FEAT_SYN_HL
2386 else if (paren == REG_ZPAREN)
2387 EMIT(NFA_ZOPEN + parno);
2388#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389
2390 return OK;
2391}
2392
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002393#ifdef DEBUG
2394static char_u code[50];
2395
2396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002397nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398{
2399 int addnl = FALSE;
2400
2401 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2402 {
2403 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002404 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002405 }
2406
2407 STRCPY(code, "");
2408 switch (c)
2409 {
2410 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2411 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2412 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2413 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2414 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2415 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2416
Bram Moolenaar5714b802013-05-28 22:03:20 +02002417 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2418 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2419 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2420 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2421 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2422 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2423 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2424 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2425 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002426#ifdef FEAT_SYN_HL
2427 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2428 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2429 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2430 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2431 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2432 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2433 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2434 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2435 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2436#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002437 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2438
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 case NFA_PREV_ATOM_NO_WIDTH:
2440 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002441 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2442 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002443 case NFA_PREV_ATOM_JUST_BEFORE:
2444 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2445 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2446 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002447 case NFA_PREV_ATOM_LIKE_PATTERN:
2448 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2449
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002450 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2451 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002453 case NFA_START_INVISIBLE_FIRST:
2454 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002455 case NFA_START_INVISIBLE_NEG:
2456 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002457 case NFA_START_INVISIBLE_NEG_FIRST:
2458 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002459 case NFA_START_INVISIBLE_BEFORE:
2460 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002461 case NFA_START_INVISIBLE_BEFORE_FIRST:
2462 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002463 case NFA_START_INVISIBLE_BEFORE_NEG:
2464 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002465 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2466 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002467 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002468 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002469 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002470 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002472 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2473 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002474 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002475
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002476 case NFA_MOPEN:
2477 case NFA_MOPEN1:
2478 case NFA_MOPEN2:
2479 case NFA_MOPEN3:
2480 case NFA_MOPEN4:
2481 case NFA_MOPEN5:
2482 case NFA_MOPEN6:
2483 case NFA_MOPEN7:
2484 case NFA_MOPEN8:
2485 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 STRCPY(code, "NFA_MOPEN(x)");
2487 code[10] = c - NFA_MOPEN + '0';
2488 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002489 case NFA_MCLOSE:
2490 case NFA_MCLOSE1:
2491 case NFA_MCLOSE2:
2492 case NFA_MCLOSE3:
2493 case NFA_MCLOSE4:
2494 case NFA_MCLOSE5:
2495 case NFA_MCLOSE6:
2496 case NFA_MCLOSE7:
2497 case NFA_MCLOSE8:
2498 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 STRCPY(code, "NFA_MCLOSE(x)");
2500 code[11] = c - NFA_MCLOSE + '0';
2501 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002502#ifdef FEAT_SYN_HL
2503 case NFA_ZOPEN:
2504 case NFA_ZOPEN1:
2505 case NFA_ZOPEN2:
2506 case NFA_ZOPEN3:
2507 case NFA_ZOPEN4:
2508 case NFA_ZOPEN5:
2509 case NFA_ZOPEN6:
2510 case NFA_ZOPEN7:
2511 case NFA_ZOPEN8:
2512 case NFA_ZOPEN9:
2513 STRCPY(code, "NFA_ZOPEN(x)");
2514 code[10] = c - NFA_ZOPEN + '0';
2515 break;
2516 case NFA_ZCLOSE:
2517 case NFA_ZCLOSE1:
2518 case NFA_ZCLOSE2:
2519 case NFA_ZCLOSE3:
2520 case NFA_ZCLOSE4:
2521 case NFA_ZCLOSE5:
2522 case NFA_ZCLOSE6:
2523 case NFA_ZCLOSE7:
2524 case NFA_ZCLOSE8:
2525 case NFA_ZCLOSE9:
2526 STRCPY(code, "NFA_ZCLOSE(x)");
2527 code[11] = c - NFA_ZCLOSE + '0';
2528 break;
2529#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002530 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2531 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2532 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2533 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002534 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2535 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002536 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2537 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2538 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2539 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2540 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2541 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2542 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2543 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2544 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2545 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2546 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2547 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2548 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2549 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002550 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002551
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002552 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002553 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2554 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2555 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002556 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002557 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002558
2559 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2560 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2561 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2562 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2563 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2564 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2565 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2566
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002567 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2568 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2569 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2570 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2571 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2572 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2573 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2574 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2575 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2576 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2577 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2578 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2579 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2580 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2581 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2582 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002583 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2584 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2585 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586
2587 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2588 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2589 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2590 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2591 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2592 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2593 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2594 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2595 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2596 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2597 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2598 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2599 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2600 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2601 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2602 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2603 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2604 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2605 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2606 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2607 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2608 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2609 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2610 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2611 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2612 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2613 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002614 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2615 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2616 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2617 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618
2619 default:
2620 STRCPY(code, "CHAR(x)");
2621 code[5] = c;
2622 }
2623
2624 if (addnl == TRUE)
2625 STRCAT(code, " + NEWLINE ");
2626
2627}
2628
2629#ifdef ENABLE_LOG
2630static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002631static 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 +02002632
2633/*
2634 * Print the postfix notation of the current regexp.
2635 */
2636 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002637nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638{
2639 int *p;
2640 FILE *f;
2641
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002642 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 if (f != NULL)
2644 {
2645 fprintf(f, "\n-------------------------\n");
2646 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002647 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 else if (retval == OK)
2649 fprintf(f, ">>> NFA engine succeeded !\n");
2650 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002651 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002652 {
2653 nfa_set_code(*p);
2654 fprintf(f, "%s, ", code);
2655 }
2656 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002657 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 fprintf(f, "%d ", *p);
2659 fprintf(f, "\n\n");
2660 fclose(f);
2661 }
2662}
2663
2664/*
2665 * Print the NFA starting with a root node "state".
2666 */
2667 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002668nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002669{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002670 garray_T indent;
2671
2672 ga_init2(&indent, 1, 64);
2673 ga_append(&indent, '\0');
2674 nfa_print_state2(debugf, state, &indent);
2675 ga_clear(&indent);
2676}
2677
2678 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002679nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002680{
2681 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002682
2683 if (state == NULL)
2684 return;
2685
2686 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002687
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002688 // Output indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002689 p = (char_u *)indent->ga_data;
2690 if (indent->ga_len >= 3)
2691 {
2692 int last = indent->ga_len - 3;
2693 char_u save[2];
2694
2695 STRNCPY(save, &p[last], 2);
2696 STRNCPY(&p[last], "+-", 2);
2697 fprintf(debugf, " %s", p);
2698 STRNCPY(&p[last], save, 2);
2699 }
2700 else
2701 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002702
2703 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002704 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002705 code,
2706 state->c,
2707 abs(state->id),
2708 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002709 if (state->id < 0)
2710 return;
2711
2712 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002713
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002714 // grow indent for state->out
Bram Moolenaar152e7892013-05-25 12:28:11 +02002715 indent->ga_len -= 1;
2716 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002717 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002718 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002719 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002720 ga_append(indent, '\0');
2721
2722 nfa_print_state2(debugf, state->out, indent);
2723
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002724 // replace last part of indent for state->out1
Bram Moolenaar152e7892013-05-25 12:28:11 +02002725 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002726 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002727 ga_append(indent, '\0');
2728
2729 nfa_print_state2(debugf, state->out1, indent);
2730
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002731 // shrink indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002732 indent->ga_len -= 3;
2733 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002734}
2735
2736/*
2737 * Print the NFA state machine.
2738 */
2739 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002740nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002741{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002742 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002743
2744 if (debugf != NULL)
2745 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002746 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002747
Bram Moolenaar473de612013-06-08 18:19:48 +02002748 if (prog->reganch)
2749 fprintf(debugf, "reganch: %d\n", prog->reganch);
2750 if (prog->regstart != NUL)
2751 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2752 prog->regstart, prog->regstart);
2753 if (prog->match_text != NULL)
2754 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002755
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756 fclose(debugf);
2757 }
2758}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002759#endif // ENABLE_LOG
2760#endif // DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002761
2762/*
2763 * Parse r.e. @expr and convert it into postfix form.
2764 * Return the postfix string on success, NULL otherwise.
2765 */
2766 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002767re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002768{
2769 if (nfa_reg(REG_NOPAREN) == FAIL)
2770 return NULL;
2771 EMIT(NFA_MOPEN);
2772 return post_start;
2773}
2774
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002775// NB. Some of the code below is inspired by Russ's.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776
2777/*
2778 * Represents an NFA state plus zero or one or two arrows exiting.
2779 * if c == MATCH, no arrows out; matching state.
2780 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2781 * If c < 256, labeled arrow with character c to out.
2782 */
2783
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002784static nfa_state_T *state_ptr; // points to nfa_prog->state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002785
2786/*
2787 * Allocate and initialize nfa_state_T.
2788 */
2789 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002790alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791{
2792 nfa_state_T *s;
2793
2794 if (istate >= nstate)
2795 return NULL;
2796
2797 s = &state_ptr[istate++];
2798
2799 s->c = c;
2800 s->out = out;
2801 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002802 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002803
2804 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002805 s->lastlist[0] = 0;
2806 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002807
2808 return s;
2809}
2810
2811/*
2812 * A partially built NFA without the matching state filled in.
2813 * Frag_T.start points at the start state.
2814 * Frag_T.out is a list of places that need to be set to the
2815 * next state for this fragment.
2816 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002817
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002818// Since the out pointers in the list are always
2819// uninitialized, we use the pointers themselves
2820// as storage for the Ptrlists.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002821typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002822union Ptrlist
2823{
2824 Ptrlist *next;
2825 nfa_state_T *s;
2826};
2827
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828struct Frag
2829{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002830 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831 Ptrlist *out;
2832};
2833typedef struct Frag Frag_T;
2834
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002836 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002837 */
2838 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002839frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002841 Frag_T n;
2842
2843 n.start = start;
2844 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845 return n;
2846}
2847
2848/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849 * Create singleton list containing just outp.
2850 */
2851 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002852list1(
2853 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002854{
2855 Ptrlist *l;
2856
2857 l = (Ptrlist *)outp;
2858 l->next = NULL;
2859 return l;
2860}
2861
2862/*
2863 * Patch the list of states at out to point to start.
2864 */
2865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002866patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867{
2868 Ptrlist *next;
2869
2870 for (; l; l = next)
2871 {
2872 next = l->next;
2873 l->s = s;
2874 }
2875}
2876
2877
2878/*
2879 * Join the two lists l1 and l2, returning the combination.
2880 */
2881 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002882append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002883{
2884 Ptrlist *oldl1;
2885
2886 oldl1 = l1;
2887 while (l1->next)
2888 l1 = l1->next;
2889 l1->next = l2;
2890 return oldl1;
2891}
2892
2893/*
2894 * Stack used for transforming postfix form into NFA.
2895 */
2896static Frag_T empty;
2897
2898 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002899st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002901#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902 FILE *df;
2903 int *p2;
2904
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002905 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906 if (df)
2907 {
2908 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002909# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002910 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002911# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002912 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002913# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002914 for (p2 = postfix; p2 < end; p2++)
2915 {
2916 nfa_set_code(*p2);
2917 fprintf(df, "%s, ", code);
2918 }
2919 nfa_set_code(*p);
2920 fprintf(df, "\nCurrent position is: ");
2921 for (p2 = postfix; p2 <= p; p2 ++)
2922 {
2923 nfa_set_code(*p2);
2924 fprintf(df, "%s, ", code);
2925 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002926# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002927 for (p2 = postfix; p2 < end; p2++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002928 fprintf(df, "%d, ", *p2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002929 fprintf(df, "\nCurrent position is: ");
2930 for (p2 = postfix; p2 <= p; p2 ++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002931 fprintf(df, "%d, ", *p2);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002932# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933 fprintf(df, "\n--------------------------\n");
2934 fclose(df);
2935 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002936#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002937 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002938}
2939
2940/*
2941 * Push an item onto the stack.
2942 */
2943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002944st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945{
2946 Frag_T *stackp = *p;
2947
2948 if (stackp >= stack_end)
2949 return;
2950 *stackp = s;
2951 *p = *p + 1;
2952}
2953
2954/*
2955 * Pop an item from the stack.
2956 */
2957 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002958st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002959{
2960 Frag_T *stackp;
2961
2962 *p = *p - 1;
2963 stackp = *p;
2964 if (stackp < stack)
2965 return empty;
2966 return **p;
2967}
2968
2969/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002970 * Estimate the maximum byte length of anything matching "state".
2971 * When unknown or unlimited return -1.
2972 */
2973 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002974nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002975{
2976 int l, r;
2977 nfa_state_T *state = startstate;
2978 int len = 0;
2979
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002980 // detect looping in a NFA_SPLIT
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002981 if (depth > 4)
2982 return -1;
2983
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002984 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002985 {
2986 switch (state->c)
2987 {
2988 case NFA_END_INVISIBLE:
2989 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002990 // the end, return what we have
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002991 return len;
2992
2993 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002994 // two alternatives, use the maximum
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002995 l = nfa_max_width(state->out, depth + 1);
2996 r = nfa_max_width(state->out1, depth + 1);
2997 if (l < 0 || r < 0)
2998 return -1;
2999 return len + (l > r ? l : r);
3000
3001 case NFA_ANY:
3002 case NFA_START_COLL:
3003 case NFA_START_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003004 // matches some character, including composing chars
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003005 if (enc_utf8)
3006 len += MB_MAXBYTES;
3007 else if (has_mbyte)
3008 len += 2;
3009 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003010 ++len;
3011 if (state->c != NFA_ANY)
3012 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003013 // skip over the characters
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003014 state = state->out1->out;
3015 continue;
3016 }
3017 break;
3018
3019 case NFA_DIGIT:
3020 case NFA_WHITE:
3021 case NFA_HEX:
3022 case NFA_OCTAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003023 // ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003024 ++len;
3025 break;
3026
3027 case NFA_IDENT:
3028 case NFA_SIDENT:
3029 case NFA_KWORD:
3030 case NFA_SKWORD:
3031 case NFA_FNAME:
3032 case NFA_SFNAME:
3033 case NFA_PRINT:
3034 case NFA_SPRINT:
3035 case NFA_NWHITE:
3036 case NFA_NDIGIT:
3037 case NFA_NHEX:
3038 case NFA_NOCTAL:
3039 case NFA_WORD:
3040 case NFA_NWORD:
3041 case NFA_HEAD:
3042 case NFA_NHEAD:
3043 case NFA_ALPHA:
3044 case NFA_NALPHA:
3045 case NFA_LOWER:
3046 case NFA_NLOWER:
3047 case NFA_UPPER:
3048 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003049 case NFA_LOWER_IC:
3050 case NFA_NLOWER_IC:
3051 case NFA_UPPER_IC:
3052 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003053 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003054 // possibly non-ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003055 if (has_mbyte)
3056 len += 3;
3057 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003058 ++len;
3059 break;
3060
3061 case NFA_START_INVISIBLE:
3062 case NFA_START_INVISIBLE_NEG:
3063 case NFA_START_INVISIBLE_BEFORE:
3064 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003065 // zero-width, out1 points to the END state
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003066 state = state->out1->out;
3067 continue;
3068
3069 case NFA_BACKREF1:
3070 case NFA_BACKREF2:
3071 case NFA_BACKREF3:
3072 case NFA_BACKREF4:
3073 case NFA_BACKREF5:
3074 case NFA_BACKREF6:
3075 case NFA_BACKREF7:
3076 case NFA_BACKREF8:
3077 case NFA_BACKREF9:
3078#ifdef FEAT_SYN_HL
3079 case NFA_ZREF1:
3080 case NFA_ZREF2:
3081 case NFA_ZREF3:
3082 case NFA_ZREF4:
3083 case NFA_ZREF5:
3084 case NFA_ZREF6:
3085 case NFA_ZREF7:
3086 case NFA_ZREF8:
3087 case NFA_ZREF9:
3088#endif
3089 case NFA_NEWL:
3090 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003091 // unknown width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003092 return -1;
3093
3094 case NFA_BOL:
3095 case NFA_EOL:
3096 case NFA_BOF:
3097 case NFA_EOF:
3098 case NFA_BOW:
3099 case NFA_EOW:
3100 case NFA_MOPEN:
3101 case NFA_MOPEN1:
3102 case NFA_MOPEN2:
3103 case NFA_MOPEN3:
3104 case NFA_MOPEN4:
3105 case NFA_MOPEN5:
3106 case NFA_MOPEN6:
3107 case NFA_MOPEN7:
3108 case NFA_MOPEN8:
3109 case NFA_MOPEN9:
3110#ifdef FEAT_SYN_HL
3111 case NFA_ZOPEN:
3112 case NFA_ZOPEN1:
3113 case NFA_ZOPEN2:
3114 case NFA_ZOPEN3:
3115 case NFA_ZOPEN4:
3116 case NFA_ZOPEN5:
3117 case NFA_ZOPEN6:
3118 case NFA_ZOPEN7:
3119 case NFA_ZOPEN8:
3120 case NFA_ZOPEN9:
3121 case NFA_ZCLOSE:
3122 case NFA_ZCLOSE1:
3123 case NFA_ZCLOSE2:
3124 case NFA_ZCLOSE3:
3125 case NFA_ZCLOSE4:
3126 case NFA_ZCLOSE5:
3127 case NFA_ZCLOSE6:
3128 case NFA_ZCLOSE7:
3129 case NFA_ZCLOSE8:
3130 case NFA_ZCLOSE9:
3131#endif
3132 case NFA_MCLOSE:
3133 case NFA_MCLOSE1:
3134 case NFA_MCLOSE2:
3135 case NFA_MCLOSE3:
3136 case NFA_MCLOSE4:
3137 case NFA_MCLOSE5:
3138 case NFA_MCLOSE6:
3139 case NFA_MCLOSE7:
3140 case NFA_MCLOSE8:
3141 case NFA_MCLOSE9:
3142 case NFA_NOPEN:
3143 case NFA_NCLOSE:
3144
3145 case NFA_LNUM_GT:
3146 case NFA_LNUM_LT:
3147 case NFA_COL_GT:
3148 case NFA_COL_LT:
3149 case NFA_VCOL_GT:
3150 case NFA_VCOL_LT:
3151 case NFA_MARK_GT:
3152 case NFA_MARK_LT:
3153 case NFA_VISUAL:
3154 case NFA_LNUM:
3155 case NFA_CURSOR:
3156 case NFA_COL:
3157 case NFA_VCOL:
3158 case NFA_MARK:
3159
3160 case NFA_ZSTART:
3161 case NFA_ZEND:
3162 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003163 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003164 case NFA_START_PATTERN:
3165 case NFA_END_PATTERN:
3166 case NFA_COMPOSING:
3167 case NFA_END_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003168 // zero-width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003169 break;
3170
3171 default:
3172 if (state->c < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003173 // don't know what this is
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003174 return -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003175 // normal character
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003176 len += MB_CHAR2LEN(state->c);
3177 break;
3178 }
3179
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003180 // normal way to continue
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003181 state = state->out;
3182 }
3183
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003184 // unrecognized, "cannot happen"
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003185 return -1;
3186}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003187
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003188/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189 * Convert a postfix form into its equivalent NFA.
3190 * Return the NFA start state on success, NULL otherwise.
3191 */
3192 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003193post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003194{
3195 int *p;
3196 int mopen;
3197 int mclose;
3198 Frag_T *stack = NULL;
3199 Frag_T *stackp = NULL;
3200 Frag_T *stack_end = NULL;
3201 Frag_T e1;
3202 Frag_T e2;
3203 Frag_T e;
3204 nfa_state_T *s;
3205 nfa_state_T *s1;
3206 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003207 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003208
3209 if (postfix == NULL)
3210 return NULL;
3211
Bram Moolenaar053bb602013-05-20 13:55:21 +02003212#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213#define POP() st_pop(&stackp, stack); \
3214 if (stackp < stack) \
3215 { \
3216 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003217 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 return NULL; \
3219 }
3220
3221 if (nfa_calc_size == FALSE)
3222 {
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003223 // Allocate space for the stack. Max states on the stack: "nstate".
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003224 stack = ALLOC_MULT(Frag_T, nstate + 1);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003225 if (stack == NULL)
3226 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003227 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003228 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003229 }
3230
3231 for (p = postfix; p < end; ++p)
3232 {
3233 switch (*p)
3234 {
3235 case NFA_CONCAT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003236 // Concatenation.
3237 // Pay attention: this operator does not exist in the r.e. itself
3238 // (it is implicit, really). It is added when r.e. is translated
3239 // to postfix form in re2post().
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 if (nfa_calc_size == TRUE)
3241 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003242 // nstate += 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003243 break;
3244 }
3245 e2 = POP();
3246 e1 = POP();
3247 patch(e1.out, e2.start);
3248 PUSH(frag(e1.start, e2.out));
3249 break;
3250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 case NFA_OR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003252 // Alternation
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253 if (nfa_calc_size == TRUE)
3254 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003255 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 break;
3257 }
3258 e2 = POP();
3259 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003260 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003262 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003263 PUSH(frag(s, append(e1.out, e2.out)));
3264 break;
3265
3266 case NFA_STAR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003267 // Zero or more, prefer more
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 if (nfa_calc_size == TRUE)
3269 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003270 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 break;
3272 }
3273 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003274 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003275 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003276 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003277 patch(e.out, s);
3278 PUSH(frag(s, list1(&s->out1)));
3279 break;
3280
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003281 case NFA_STAR_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003282 // Zero or more, prefer zero
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003283 if (nfa_calc_size == TRUE)
3284 {
3285 nstate++;
3286 break;
3287 }
3288 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003289 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003290 if (s == NULL)
3291 goto theend;
3292 patch(e.out, s);
3293 PUSH(frag(s, list1(&s->out)));
3294 break;
3295
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 case NFA_QUEST:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003297 // one or zero atoms=> greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 if (nfa_calc_size == TRUE)
3299 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003300 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003301 break;
3302 }
3303 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003304 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003306 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 PUSH(frag(s, append(e.out, list1(&s->out1))));
3308 break;
3309
3310 case NFA_QUEST_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003311 // zero or one atoms => non-greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 if (nfa_calc_size == TRUE)
3313 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003314 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003315 break;
3316 }
3317 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003318 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003320 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003321 PUSH(frag(s, append(e.out, list1(&s->out))));
3322 break;
3323
Bram Moolenaar417bad22013-06-07 14:08:30 +02003324 case NFA_END_COLL:
3325 case NFA_END_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003326 // On the stack is the sequence starting with NFA_START_COLL or
3327 // NFA_START_NEG_COLL and all possible characters. Patch it to
3328 // add the output to the start.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003329 if (nfa_calc_size == TRUE)
3330 {
3331 nstate++;
3332 break;
3333 }
3334 e = POP();
3335 s = alloc_state(NFA_END_COLL, NULL, NULL);
3336 if (s == NULL)
3337 goto theend;
3338 patch(e.out, s);
3339 e.start->out1 = s;
3340 PUSH(frag(e.start, list1(&s->out)));
3341 break;
3342
3343 case NFA_RANGE:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003344 // Before this are two characters, the low and high end of a
3345 // range. Turn them into two states with MIN and MAX.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003346 if (nfa_calc_size == TRUE)
3347 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003348 // nstate += 0;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003349 break;
3350 }
3351 e2 = POP();
3352 e1 = POP();
3353 e2.start->val = e2.start->c;
3354 e2.start->c = NFA_RANGE_MAX;
3355 e1.start->val = e1.start->c;
3356 e1.start->c = NFA_RANGE_MIN;
3357 patch(e1.out, e2.start);
3358 PUSH(frag(e1.start, e2.out));
3359 break;
3360
Bram Moolenaar699c1202013-09-25 16:41:54 +02003361 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003362 // 0-length, used in a repetition with max/min count of 0
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 if (nfa_calc_size == TRUE)
3364 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003365 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003366 break;
3367 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003368 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003369 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003370 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 PUSH(frag(s, list1(&s->out)));
3372 break;
3373
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003374 case NFA_OPT_CHARS:
3375 {
3376 int n;
3377
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003378 // \%[abc] implemented as:
3379 // NFA_SPLIT
3380 // +-CHAR(a)
3381 // | +-NFA_SPLIT
3382 // | +-CHAR(b)
3383 // | | +-NFA_SPLIT
3384 // | | +-CHAR(c)
3385 // | | | +-next
3386 // | | +- next
3387 // | +- next
3388 // +- next
3389 n = *++p; // get number of characters
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003390 if (nfa_calc_size == TRUE)
3391 {
3392 nstate += n;
3393 break;
3394 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003395 s = NULL; // avoid compiler warning
3396 e1.out = NULL; // stores list with out1's
3397 s1 = NULL; // previous NFA_SPLIT to connect to
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003398 while (n-- > 0)
3399 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003400 e = POP(); // get character
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003401 s = alloc_state(NFA_SPLIT, e.start, NULL);
3402 if (s == NULL)
3403 goto theend;
3404 if (e1.out == NULL)
3405 e1 = e;
3406 patch(e.out, s1);
3407 append(e1.out, list1(&s->out1));
3408 s1 = s;
3409 }
3410 PUSH(frag(s, e1.out));
3411 break;
3412 }
3413
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003414 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003415 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003416 case NFA_PREV_ATOM_JUST_BEFORE:
3417 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003418 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003419 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003420 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3421 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003422 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003423 int start_state;
3424 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003425 int n = 0;
3426 nfa_state_T *zend;
3427 nfa_state_T *skip;
3428
Bram Moolenaardecd9542013-06-07 16:31:50 +02003429 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003430 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003431 case NFA_PREV_ATOM_NO_WIDTH:
3432 start_state = NFA_START_INVISIBLE;
3433 end_state = NFA_END_INVISIBLE;
3434 break;
3435 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3436 start_state = NFA_START_INVISIBLE_NEG;
3437 end_state = NFA_END_INVISIBLE_NEG;
3438 break;
3439 case NFA_PREV_ATOM_JUST_BEFORE:
3440 start_state = NFA_START_INVISIBLE_BEFORE;
3441 end_state = NFA_END_INVISIBLE;
3442 break;
3443 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3444 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3445 end_state = NFA_END_INVISIBLE_NEG;
3446 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003447 default: // NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003448 start_state = NFA_START_PATTERN;
3449 end_state = NFA_END_PATTERN;
3450 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003451 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003452
3453 if (before)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003454 n = *++p; // get the count
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003455
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003456 // The \@= operator: match the preceding atom with zero width.
3457 // The \@! operator: no match for the preceding atom.
3458 // The \@<= operator: match for the preceding atom.
3459 // The \@<! operator: no match for the preceding atom.
3460 // Surrounds the preceding atom with START_INVISIBLE and
3461 // END_INVISIBLE, similarly to MOPEN.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462
3463 if (nfa_calc_size == TRUE)
3464 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003465 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 break;
3467 }
3468 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003469 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003470 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003471 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003472
Bram Moolenaar87953742013-06-05 18:52:40 +02003473 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003474 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003475 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003476 if (pattern)
3477 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003478 // NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02003479 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003480 if (skip == NULL)
3481 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003482 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003483 if (zend == NULL)
3484 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003485 s1->out= skip;
3486 patch(e.out, zend);
3487 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003488 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003489 else
3490 {
3491 patch(e.out, s1);
3492 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003493 if (before)
3494 {
3495 if (n <= 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003496 // See if we can guess the maximum width, it avoids a
3497 // lot of pointless tries.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003498 n = nfa_max_width(e.start, 0);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003499 s->val = n; // store the count
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003500 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003501 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003503 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003505 case NFA_COMPOSING: // char with composing char
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003506#if 0
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003507 // TODO
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003508 if (regflags & RF_ICOMBINE)
3509 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003510 // use the base character only
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003511 }
3512#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003513 // FALLTHROUGH
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003514
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003515 case NFA_MOPEN: // \( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003516 case NFA_MOPEN1:
3517 case NFA_MOPEN2:
3518 case NFA_MOPEN3:
3519 case NFA_MOPEN4:
3520 case NFA_MOPEN5:
3521 case NFA_MOPEN6:
3522 case NFA_MOPEN7:
3523 case NFA_MOPEN8:
3524 case NFA_MOPEN9:
3525#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003526 case NFA_ZOPEN: // \z( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003527 case NFA_ZOPEN1:
3528 case NFA_ZOPEN2:
3529 case NFA_ZOPEN3:
3530 case NFA_ZOPEN4:
3531 case NFA_ZOPEN5:
3532 case NFA_ZOPEN6:
3533 case NFA_ZOPEN7:
3534 case NFA_ZOPEN8:
3535 case NFA_ZOPEN9:
3536#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003537 case NFA_NOPEN: // \%( \) "Invisible Submatch"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003538 if (nfa_calc_size == TRUE)
3539 {
3540 nstate += 2;
3541 break;
3542 }
3543
3544 mopen = *p;
3545 switch (*p)
3546 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003547 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3548#ifdef FEAT_SYN_HL
3549 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3550 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3551 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3552 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3553 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3554 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3555 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3556 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3557 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3558 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3559#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003560 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003561 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003562 // NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003563 mclose = *p + NSUBEXP;
3564 break;
3565 }
3566
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003567 // Allow "NFA_MOPEN" as a valid postfix representation for
3568 // the empty regexp "". In this case, the NFA will be
3569 // NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3570 // empty groups of parenthesis, and empty mbyte chars
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003571 if (stackp == stack)
3572 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003573 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003574 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003575 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003576 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003578 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003579 patch(list1(&s->out), s1);
3580 PUSH(frag(s, list1(&s1->out)));
3581 break;
3582 }
3583
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003584 // At least one node was emitted before NFA_MOPEN, so
3585 // at least one node will be between NFA_MOPEN and NFA_MCLOSE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 e = POP();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003587 s = alloc_state(mopen, e.start, NULL); // `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003589 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003590
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003591 s1 = alloc_state(mclose, NULL, NULL); // `)'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003593 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 patch(e.out, s1);
3595
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003596 if (mopen == NFA_COMPOSING)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003597 // COMPOSING->out1 = END_COMPOSING
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003598 patch(list1(&s->out1), s1);
3599
3600 PUSH(frag(s, list1(&s1->out)));
3601 break;
3602
Bram Moolenaar5714b802013-05-28 22:03:20 +02003603 case NFA_BACKREF1:
3604 case NFA_BACKREF2:
3605 case NFA_BACKREF3:
3606 case NFA_BACKREF4:
3607 case NFA_BACKREF5:
3608 case NFA_BACKREF6:
3609 case NFA_BACKREF7:
3610 case NFA_BACKREF8:
3611 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003612#ifdef FEAT_SYN_HL
3613 case NFA_ZREF1:
3614 case NFA_ZREF2:
3615 case NFA_ZREF3:
3616 case NFA_ZREF4:
3617 case NFA_ZREF5:
3618 case NFA_ZREF6:
3619 case NFA_ZREF7:
3620 case NFA_ZREF8:
3621 case NFA_ZREF9:
3622#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003623 if (nfa_calc_size == TRUE)
3624 {
3625 nstate += 2;
3626 break;
3627 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003628 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003629 if (s == NULL)
3630 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003631 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003632 if (s1 == NULL)
3633 goto theend;
3634 patch(list1(&s->out), s1);
3635 PUSH(frag(s, list1(&s1->out)));
3636 break;
3637
Bram Moolenaar423532e2013-05-29 21:14:42 +02003638 case NFA_LNUM:
3639 case NFA_LNUM_GT:
3640 case NFA_LNUM_LT:
3641 case NFA_VCOL:
3642 case NFA_VCOL_GT:
3643 case NFA_VCOL_LT:
3644 case NFA_COL:
3645 case NFA_COL_GT:
3646 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003647 case NFA_MARK:
3648 case NFA_MARK_GT:
3649 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003650 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003651 int n = *++p; // lnum, col or mark name
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003652
Bram Moolenaar423532e2013-05-29 21:14:42 +02003653 if (nfa_calc_size == TRUE)
3654 {
3655 nstate += 1;
3656 break;
3657 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003658 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003659 if (s == NULL)
3660 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003661 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003662 PUSH(frag(s, list1(&s->out)));
3663 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003664 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003665
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003666 case NFA_ZSTART:
3667 case NFA_ZEND:
3668 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003669 // Operands
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003670 if (nfa_calc_size == TRUE)
3671 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003672 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003673 break;
3674 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003675 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003677 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003678 PUSH(frag(s, list1(&s->out)));
3679 break;
3680
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003681 } // switch(*p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003682
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003683 } // for(p = postfix; *p; ++p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003684
3685 if (nfa_calc_size == TRUE)
3686 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003687 nstate++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003688 goto theend; // Return value when counting size is ignored anyway
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003689 }
3690
3691 e = POP();
3692 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003693 {
3694 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003695 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003696 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697
3698 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003699 {
3700 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003701 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003702 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003704 matchstate = &state_ptr[istate++]; // the match state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 matchstate->c = NFA_MATCH;
3706 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003707 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003708
3709 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003710 ret = e.start;
3711
3712theend:
3713 vim_free(stack);
3714 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003715
3716#undef POP1
3717#undef PUSH1
3718#undef POP2
3719#undef PUSH2
3720#undef POP
3721#undef PUSH
3722}
3723
Bram Moolenaara2947e22013-06-11 22:44:09 +02003724/*
3725 * After building the NFA program, inspect it to add optimization hints.
3726 */
3727 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003728nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003729{
3730 int i;
3731 int c;
3732
3733 for (i = 0; i < prog->nstate; ++i)
3734 {
3735 c = prog->state[i].c;
3736 if (c == NFA_START_INVISIBLE
3737 || c == NFA_START_INVISIBLE_NEG
3738 || c == NFA_START_INVISIBLE_BEFORE
3739 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3740 {
3741 int directly;
3742
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003743 // Do it directly when what follows is possibly the end of the
3744 // match.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003745 if (match_follows(prog->state[i].out1->out, 0))
3746 directly = TRUE;
3747 else
3748 {
3749 int ch_invisible = failure_chance(prog->state[i].out, 0);
3750 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3751
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003752 // Postpone when the invisible match is expensive or has a
3753 // lower chance of failing.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003754 if (c == NFA_START_INVISIBLE_BEFORE
3755 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3756 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003757 // "before" matches are very expensive when
3758 // unbounded, always prefer what follows then,
3759 // unless what follows will always match.
3760 // Otherwise strongly prefer what follows.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003761 if (prog->state[i].val <= 0 && ch_follows > 0)
3762 directly = FALSE;
3763 else
3764 directly = ch_follows * 10 < ch_invisible;
3765 }
3766 else
3767 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003768 // normal invisible, first do the one with the
3769 // highest failure chance
Bram Moolenaara2947e22013-06-11 22:44:09 +02003770 directly = ch_follows < ch_invisible;
3771 }
3772 }
3773 if (directly)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003774 // switch to the _FIRST state
Bram Moolenaara2947e22013-06-11 22:44:09 +02003775 ++prog->state[i].c;
3776 }
3777 }
3778}
3779
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003780/////////////////////////////////////////////////////////////////
3781// NFA execution code.
3782/////////////////////////////////////////////////////////////////
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003784typedef struct
3785{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003786 int in_use; // number of subexpr with useful info
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003787
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003788 // When REG_MULTI is TRUE list.multi is used, otherwise list.line.
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003789 union
3790 {
3791 struct multipos
3792 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003793 linenr_T start_lnum;
3794 linenr_T end_lnum;
3795 colnr_T start_col;
3796 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003797 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003798 struct linepos
3799 {
3800 char_u *start;
3801 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003802 } line[NSUBEXP];
3803 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003804} regsub_T;
3805
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003806typedef struct
3807{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003808 regsub_T norm; // \( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003809#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003810 regsub_T synt; // \z( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003811#endif
3812} regsubs_T;
3813
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003814// nfa_pim_T stores a Postponed Invisible Match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02003815typedef struct nfa_pim_S nfa_pim_T;
3816struct nfa_pim_S
3817{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003818 int result; // NFA_PIM_*, see below
3819 nfa_state_T *state; // the invisible match start state
3820 regsubs_T subs; // submatch info, only party used
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003821 union
3822 {
3823 lpos_T pos;
3824 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003825 } end; // where the match must end
Bram Moolenaara2d95102013-06-04 14:23:05 +02003826};
3827
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003828// Values for done in nfa_pim_T.
3829#define NFA_PIM_UNUSED 0 // pim not used
3830#define NFA_PIM_TODO 1 // pim not done yet
3831#define NFA_PIM_MATCH 2 // pim executed, matches
3832#define NFA_PIM_NOMATCH 3 // pim executed, no match
Bram Moolenaara2d95102013-06-04 14:23:05 +02003833
3834
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003835// nfa_thread_T contains execution information of a NFA state
Bram Moolenaar4b417062013-05-25 20:19:50 +02003836typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003837{
3838 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003839 int count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003840 nfa_pim_T pim; // if pim.result != NFA_PIM_UNUSED: postponed
3841 // invisible match
3842 regsubs_T subs; // submatch info, only party used
Bram Moolenaar4b417062013-05-25 20:19:50 +02003843} nfa_thread_T;
3844
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003845// nfa_list_T contains the alternative NFA execution states.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003846typedef struct
3847{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003848 nfa_thread_T *t; // allocated array of states
3849 int n; // nr of states currently in "t"
3850 int len; // max nr of states in "t"
3851 int id; // ID of the list
3852 int has_pim; // TRUE when any state has a PIM
Bram Moolenaar4b417062013-05-25 20:19:50 +02003853} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003854
Bram Moolenaar5714b802013-05-28 22:03:20 +02003855#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003856static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003857
3858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003859log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003860{
3861 log_subexpr(&subs->norm);
3862# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003863 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003864 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003865# endif
3866}
3867
Bram Moolenaar5714b802013-05-28 22:03:20 +02003868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003869log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003870{
3871 int j;
3872
3873 for (j = 0; j < sub->in_use; j++)
3874 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003875 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003876 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003877 sub->list.multi[j].start_col,
3878 (int)sub->list.multi[j].start_lnum,
3879 sub->list.multi[j].end_col,
3880 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003881 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003882 {
3883 char *s = (char *)sub->list.line[j].start;
3884 char *e = (char *)sub->list.line[j].end;
3885
Bram Moolenaar87953742013-06-05 18:52:40 +02003886 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003887 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003888 s == NULL ? "NULL" : s,
3889 e == NULL ? "NULL" : e);
3890 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003891}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003892
3893 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003894pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003895{
3896 static char buf[30];
3897
3898 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3899 buf[0] = NUL;
3900 else
3901 {
3902 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003903 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003904 }
3905 return buf;
3906}
3907
Bram Moolenaar5714b802013-05-28 22:03:20 +02003908#endif
3909
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003910// Used during execution: whether a match has been found.
Bram Moolenaar2338c322018-07-08 19:07:19 +02003911static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003912#ifdef FEAT_RELTIME
3913static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003914static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003915static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003916#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003917
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003918static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003919static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003920
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003921/*
3922 * Copy postponed invisible match info from "from" to "to".
3923 */
3924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003925copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003926{
3927 to->result = from->result;
3928 to->state = from->state;
3929 copy_sub(&to->subs.norm, &from->subs.norm);
3930#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003931 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003932 copy_sub(&to->subs.synt, &from->subs.synt);
3933#endif
3934 to->end = from->end;
3935}
3936
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003937 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003938clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003939{
3940 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003941 // Use 0xff to set lnum to -1
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003942 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003943 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003944 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003945 vim_memset(sub->list.line, 0,
3946 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003947 sub->in_use = 0;
3948}
3949
3950/*
3951 * Copy the submatches from "from" to "to".
3952 */
3953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003954copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003955{
3956 to->in_use = from->in_use;
3957 if (from->in_use > 0)
3958 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003959 // Copy the match start and end positions.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003960 if (REG_MULTI)
3961 mch_memmove(&to->list.multi[0],
3962 &from->list.multi[0],
3963 sizeof(struct multipos) * from->in_use);
3964 else
3965 mch_memmove(&to->list.line[0],
3966 &from->list.line[0],
3967 sizeof(struct linepos) * from->in_use);
3968 }
3969}
3970
3971/*
3972 * Like copy_sub() but exclude the main match.
3973 */
3974 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003975copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003976{
3977 if (to->in_use < from->in_use)
3978 to->in_use = from->in_use;
3979 if (from->in_use > 1)
3980 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003981 // Copy the match start and end positions.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003982 if (REG_MULTI)
3983 mch_memmove(&to->list.multi[1],
3984 &from->list.multi[1],
3985 sizeof(struct multipos) * (from->in_use - 1));
3986 else
3987 mch_memmove(&to->list.line[1],
3988 &from->list.line[1],
3989 sizeof(struct linepos) * (from->in_use - 1));
3990 }
3991}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003992
Bram Moolenaar428e9872013-05-30 17:05:39 +02003993/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003994 * Like copy_sub() but only do the end of the main match if \ze is present.
3995 */
3996 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003997copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003998{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003999 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004000 {
4001 if (REG_MULTI)
4002 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004003 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004004 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004005 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4006 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004007 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004008 }
4009 else
4010 {
4011 if (from->list.line[0].end != NULL)
4012 to->list.line[0].end = from->list.line[0].end;
4013 }
4014 }
4015}
4016
4017/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004018 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004019 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004020 */
4021 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004022sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004023{
4024 int i;
4025 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004026 linenr_T s1;
4027 linenr_T s2;
4028 char_u *sp1;
4029 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004030
4031 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4032 if (REG_MULTI)
4033 {
4034 for (i = 0; i < todo; ++i)
4035 {
4036 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004037 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004038 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004039 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004040 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004041 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004042 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004043 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004044 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004045 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004046 if (s1 != -1 && sub1->list.multi[i].start_col
4047 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004048 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004049
Bram Moolenaar0270f382018-07-17 05:43:58 +02004050 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004051 {
4052 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004053 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004054 else
4055 s1 = -1;
4056 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004057 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004058 else
4059 s2 = -1;
4060 if (s1 != s2)
4061 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004062 if (s1 != -1 && sub1->list.multi[i].end_col
4063 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004064 return FALSE;
4065 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004066 }
4067 }
4068 else
4069 {
4070 for (i = 0; i < todo; ++i)
4071 {
4072 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004073 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004075 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004076 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004077 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004078 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004079 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004080 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004081 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004082 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004083 {
4084 if (i < sub1->in_use)
4085 sp1 = sub1->list.line[i].end;
4086 else
4087 sp1 = NULL;
4088 if (i < sub2->in_use)
4089 sp2 = sub2->list.line[i].end;
4090 else
4091 sp2 = NULL;
4092 if (sp1 != sp2)
4093 return FALSE;
4094 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004095 }
4096 }
4097
4098 return TRUE;
4099}
4100
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004101#ifdef ENABLE_LOG
4102 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004103report_state(char *action,
4104 regsub_T *sub,
4105 nfa_state_T *state,
4106 int lid,
4107 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004108{
4109 int col;
4110
4111 if (sub->in_use <= 0)
4112 col = -1;
4113 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004114 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004115 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004116 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004117 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004118 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4119 action, abs(state->id), lid, state->c, code, col,
4120 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004121}
4122#endif
4123
Bram Moolenaar43e02982013-06-07 17:31:29 +02004124/*
4125 * Return TRUE if the same state is already in list "l" with the same
4126 * positions as "subs".
4127 */
4128 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004129has_state_with_pos(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004130 nfa_list_T *l, // runtime state list
4131 nfa_state_T *state, // state to update
4132 regsubs_T *subs, // pointers to subexpressions
4133 nfa_pim_T *pim) // postponed match or NULL
Bram Moolenaar43e02982013-06-07 17:31:29 +02004134{
4135 nfa_thread_T *thread;
4136 int i;
4137
4138 for (i = 0; i < l->n; ++i)
4139 {
4140 thread = &l->t[i];
4141 if (thread->state->id == state->id
4142 && sub_equal(&thread->subs.norm, &subs->norm)
4143#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004144 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004145 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004146#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004147 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004148 return TRUE;
4149 }
4150 return FALSE;
4151}
4152
4153/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004154 * Return TRUE if "one" and "two" are equal. That includes when both are not
4155 * set.
4156 */
4157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004158pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004159{
4160 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4161 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4162
4163 if (one_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004164 // one is unused: equal when two is also unused
Bram Moolenaar69b52452013-07-17 21:10:51 +02004165 return two_unused;
4166 if (two_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004167 // one is used and two is not: not equal
Bram Moolenaar69b52452013-07-17 21:10:51 +02004168 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004169 // compare the state id
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004170 if (one->state->id != two->state->id)
4171 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004172 // compare the position
Bram Moolenaar69b52452013-07-17 21:10:51 +02004173 if (REG_MULTI)
4174 return one->end.pos.lnum == two->end.pos.lnum
4175 && one->end.pos.col == two->end.pos.col;
4176 return one->end.ptr == two->end.ptr;
4177}
4178
4179/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004180 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4181 */
4182 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004183match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004184{
4185 nfa_state_T *state = startstate;
4186
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004187 // avoid too much recursion
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004188 if (depth > 10)
4189 return FALSE;
4190
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004191 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004192 {
4193 switch (state->c)
4194 {
4195 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004196 case NFA_MCLOSE:
4197 case NFA_END_INVISIBLE:
4198 case NFA_END_INVISIBLE_NEG:
4199 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004200 return TRUE;
4201
4202 case NFA_SPLIT:
4203 return match_follows(state->out, depth + 1)
4204 || match_follows(state->out1, depth + 1);
4205
4206 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004207 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004208 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004209 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004210 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004211 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004212 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004213 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004214 case NFA_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004215 // skip ahead to next state
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004216 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004217 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004218
4219 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004220 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004221 case NFA_IDENT:
4222 case NFA_SIDENT:
4223 case NFA_KWORD:
4224 case NFA_SKWORD:
4225 case NFA_FNAME:
4226 case NFA_SFNAME:
4227 case NFA_PRINT:
4228 case NFA_SPRINT:
4229 case NFA_WHITE:
4230 case NFA_NWHITE:
4231 case NFA_DIGIT:
4232 case NFA_NDIGIT:
4233 case NFA_HEX:
4234 case NFA_NHEX:
4235 case NFA_OCTAL:
4236 case NFA_NOCTAL:
4237 case NFA_WORD:
4238 case NFA_NWORD:
4239 case NFA_HEAD:
4240 case NFA_NHEAD:
4241 case NFA_ALPHA:
4242 case NFA_NALPHA:
4243 case NFA_LOWER:
4244 case NFA_NLOWER:
4245 case NFA_UPPER:
4246 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004247 case NFA_LOWER_IC:
4248 case NFA_NLOWER_IC:
4249 case NFA_UPPER_IC:
4250 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004251 case NFA_START_COLL:
4252 case NFA_START_NEG_COLL:
4253 case NFA_NEWL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004254 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004255 return FALSE;
4256
4257 default:
4258 if (state->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004259 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004260 return FALSE;
4261
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004262 // Others: zero-width or possibly zero-width, might still find
4263 // a match at the same position, keep looking.
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004264 break;
4265 }
4266 state = state->out;
4267 }
4268 return FALSE;
4269}
4270
4271
4272/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004273 * Return TRUE if "state" is already in list "l".
4274 */
4275 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004276state_in_list(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004277 nfa_list_T *l, // runtime state list
4278 nfa_state_T *state, // state to update
4279 regsubs_T *subs) // pointers to subexpressions
Bram Moolenaar43e02982013-06-07 17:31:29 +02004280{
4281 if (state->lastlist[nfa_ll_index] == l->id)
4282 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004283 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004284 return TRUE;
4285 }
4286 return FALSE;
4287}
4288
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004289// Offset used for "off" by addstate_here().
Bram Moolenaar16b35782016-09-09 20:29:50 +02004290#define ADDSTATE_HERE_OFFSET 10
4291
Bram Moolenaard05bf562013-06-30 23:24:08 +02004292/*
4293 * Add "state" and possibly what follows to state list ".".
4294 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004295 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004296 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004297 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004298addstate(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004299 nfa_list_T *l, // runtime state list
4300 nfa_state_T *state, // state to update
4301 regsubs_T *subs_arg, // pointers to subexpressions
4302 nfa_pim_T *pim, // postponed look-behind match
4303 int off_arg) // byte offset, when -1 go to next line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004305 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004306 int off = off_arg;
4307 int add_here = FALSE;
4308 int listindex = 0;
4309 int k;
4310 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004311 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004312 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004313 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004314 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004315 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004316 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004317 regsubs_T *subs = subs_arg;
4318 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004319#ifdef ENABLE_LOG
4320 int did_print = FALSE;
4321#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004322 static int depth = 0;
4323
4324 // This function is called recursively. When the depth is too much we run
4325 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004326 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004327 {
4328 --depth;
4329 return NULL;
4330 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004331
Bram Moolenaar16b35782016-09-09 20:29:50 +02004332 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4333 {
4334 add_here = TRUE;
4335 off = 0;
4336 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4337 }
4338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004339 switch (state->c)
4340 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004341 case NFA_NCLOSE:
4342 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004343 case NFA_MCLOSE1:
4344 case NFA_MCLOSE2:
4345 case NFA_MCLOSE3:
4346 case NFA_MCLOSE4:
4347 case NFA_MCLOSE5:
4348 case NFA_MCLOSE6:
4349 case NFA_MCLOSE7:
4350 case NFA_MCLOSE8:
4351 case NFA_MCLOSE9:
4352#ifdef FEAT_SYN_HL
4353 case NFA_ZCLOSE:
4354 case NFA_ZCLOSE1:
4355 case NFA_ZCLOSE2:
4356 case NFA_ZCLOSE3:
4357 case NFA_ZCLOSE4:
4358 case NFA_ZCLOSE5:
4359 case NFA_ZCLOSE6:
4360 case NFA_ZCLOSE7:
4361 case NFA_ZCLOSE8:
4362 case NFA_ZCLOSE9:
4363#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004364 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004365 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004366 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004367 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004368 // These nodes are not added themselves but their "out" and/or
4369 // "out1" may be added below.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004370 break;
4371
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004372 case NFA_BOL:
4373 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004374 // "^" won't match past end-of-line, don't bother trying.
4375 // Except when at the end of the line, or when we are going to the
4376 // next line for a look-behind match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004377 if (rex.input > rex.line
4378 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004379 && (nfa_endp == NULL
4380 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004381 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004382 goto skip_add;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004383 // FALLTHROUGH
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004384
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004385 case NFA_MOPEN1:
4386 case NFA_MOPEN2:
4387 case NFA_MOPEN3:
4388 case NFA_MOPEN4:
4389 case NFA_MOPEN5:
4390 case NFA_MOPEN6:
4391 case NFA_MOPEN7:
4392 case NFA_MOPEN8:
4393 case NFA_MOPEN9:
4394#ifdef FEAT_SYN_HL
4395 case NFA_ZOPEN:
4396 case NFA_ZOPEN1:
4397 case NFA_ZOPEN2:
4398 case NFA_ZOPEN3:
4399 case NFA_ZOPEN4:
4400 case NFA_ZOPEN5:
4401 case NFA_ZOPEN6:
4402 case NFA_ZOPEN7:
4403 case NFA_ZOPEN8:
4404 case NFA_ZOPEN9:
4405#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004406 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004407 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004408 // These nodes need to be added so that we can bail out when it
4409 // was added to this list before at the same position to avoid an
4410 // endless loop for "\(\)*"
Bram Moolenaar307aa162013-06-02 16:34:21 +02004411
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004412 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004413 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004414 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004415 // This state is already in the list, don't add it again,
4416 // unless it is an MOPEN that is used for a backreference or
4417 // when there is a PIM. For NFA_MATCH check the position,
4418 // lower position is preferred.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004419 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004420 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004421 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004422 // When called from addstate_here() do insert before
4423 // existing states.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004424 if (add_here)
4425 {
4426 for (k = 0; k < l->n && k < listindex; ++k)
4427 if (l->t[k].state->id == state->id)
4428 {
4429 found = TRUE;
4430 break;
4431 }
4432 }
4433 if (!add_here || found)
4434 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004435skip_add:
4436#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004437 nfa_set_code(state->c);
4438 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4439 abs(state->id), l->id, state->c, code,
4440 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004441#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004442 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004443 return subs;
4444 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004445 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004446
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004447 // Do not add the state again when it exists with the same
4448 // positions.
Bram Moolenaar69b52452013-07-17 21:10:51 +02004449 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004450 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004451 }
4452
Bram Moolenaar688b3982019-02-13 21:47:36 +01004453 // When there are backreferences or PIMs the number of states may
4454 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004455 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004456 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004457 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004458 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004459 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004460
Bram Moolenaar688b3982019-02-13 21:47:36 +01004461 if ((long)(newsize >> 10) >= p_mmp)
4462 {
4463 emsg(_(e_maxmempat));
4464 --depth;
4465 return NULL;
4466 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004467 if (subs != &temp_subs)
4468 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004469 // "subs" may point into the current array, need to make a
4470 // copy before it becomes invalid.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004471 copy_sub(&temp_subs.norm, &subs->norm);
4472#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004473 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004474 copy_sub(&temp_subs.synt, &subs->synt);
4475#endif
4476 subs = &temp_subs;
4477 }
4478
Bram Moolenaar688b3982019-02-13 21:47:36 +01004479 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004480 if (newt == NULL)
4481 {
4482 // out of memory
4483 --depth;
4484 return NULL;
4485 }
4486 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004487 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004488 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004489
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004490 // add the state to the list
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004491 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004492 thread = &l->t[l->n++];
4493 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004494 if (pim == NULL)
4495 thread->pim.result = NFA_PIM_UNUSED;
4496 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004497 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004498 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004499 l->has_pim = TRUE;
4500 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004501 copy_sub(&thread->subs.norm, &subs->norm);
4502#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004503 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004504 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004505#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004506#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004507 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004508 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004509#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004510 }
4511
4512#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004513 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004514 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004515#endif
4516 switch (state->c)
4517 {
4518 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 break;
4520
4521 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004522 // order matters here
Bram Moolenaar16b35782016-09-09 20:29:50 +02004523 subs = addstate(l, state->out, subs, pim, off_arg);
4524 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 break;
4526
Bram Moolenaar699c1202013-09-25 16:41:54 +02004527 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004528 case NFA_NOPEN:
4529 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004530 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004531 break;
4532
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004533 case NFA_MOPEN:
4534 case NFA_MOPEN1:
4535 case NFA_MOPEN2:
4536 case NFA_MOPEN3:
4537 case NFA_MOPEN4:
4538 case NFA_MOPEN5:
4539 case NFA_MOPEN6:
4540 case NFA_MOPEN7:
4541 case NFA_MOPEN8:
4542 case NFA_MOPEN9:
4543#ifdef FEAT_SYN_HL
4544 case NFA_ZOPEN:
4545 case NFA_ZOPEN1:
4546 case NFA_ZOPEN2:
4547 case NFA_ZOPEN3:
4548 case NFA_ZOPEN4:
4549 case NFA_ZOPEN5:
4550 case NFA_ZOPEN6:
4551 case NFA_ZOPEN7:
4552 case NFA_ZOPEN8:
4553 case NFA_ZOPEN9:
4554#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004555 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004556 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004557 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004558 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004559 sub = &subs->norm;
4560 }
4561#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004562 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004563 {
4564 subidx = state->c - NFA_ZOPEN;
4565 sub = &subs->synt;
4566 }
4567#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004568 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004569 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004570 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004571 sub = &subs->norm;
4572 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004573
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004574 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004575 save_ptr = NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004576 CLEAR_FIELD(save_multipos);
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004577
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004578 // Set the position (with "off" added) in the subexpression. Save
4579 // and restore it when it was in use. Otherwise fill any gap.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 if (REG_MULTI)
4581 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004582 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004583 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004584 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004585 save_in_use = -1;
4586 }
4587 else
4588 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004589 save_in_use = sub->in_use;
4590 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004591 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004592 sub->list.multi[i].start_lnum = -1;
4593 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004594 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004595 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004596 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004597 if (off == -1)
4598 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004599 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004600 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004601 }
4602 else
4603 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004604 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004605 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004606 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004607 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004608 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 }
4610 else
4611 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004612 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004613 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004614 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004615 save_in_use = -1;
4616 }
4617 else
4618 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004619 save_in_use = sub->in_use;
4620 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004621 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004622 sub->list.line[i].start = NULL;
4623 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004624 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004625 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004626 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004627 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004628 }
4629
Bram Moolenaar16b35782016-09-09 20:29:50 +02004630 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004631 if (subs == NULL)
4632 break;
4633 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004634#ifdef FEAT_SYN_HL
4635 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4636 sub = &subs->synt;
4637 else
4638#endif
4639 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004641 if (save_in_use == -1)
4642 {
4643 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004644 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004645 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004646 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004647 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004648 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004649 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 break;
4651
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004652 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004653 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004654 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004655 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004656 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004657 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004658 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004659 break;
4660 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004661 // FALLTHROUGH
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004662 case NFA_MCLOSE1:
4663 case NFA_MCLOSE2:
4664 case NFA_MCLOSE3:
4665 case NFA_MCLOSE4:
4666 case NFA_MCLOSE5:
4667 case NFA_MCLOSE6:
4668 case NFA_MCLOSE7:
4669 case NFA_MCLOSE8:
4670 case NFA_MCLOSE9:
4671#ifdef FEAT_SYN_HL
4672 case NFA_ZCLOSE:
4673 case NFA_ZCLOSE1:
4674 case NFA_ZCLOSE2:
4675 case NFA_ZCLOSE3:
4676 case NFA_ZCLOSE4:
4677 case NFA_ZCLOSE5:
4678 case NFA_ZCLOSE6:
4679 case NFA_ZCLOSE7:
4680 case NFA_ZCLOSE8:
4681 case NFA_ZCLOSE9:
4682#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004684 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004685 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004687 sub = &subs->norm;
4688 }
4689#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004690 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004691 {
4692 subidx = state->c - NFA_ZCLOSE;
4693 sub = &subs->synt;
4694 }
4695#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004696 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004697 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004698 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004699 sub = &subs->norm;
4700 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004702 // We don't fill in gaps here, there must have been an MOPEN that
4703 // has done that.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004704 save_in_use = sub->in_use;
4705 if (sub->in_use <= subidx)
4706 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004707 if (REG_MULTI)
4708 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004709 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004710 if (off == -1)
4711 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004712 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004713 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004714 }
4715 else
4716 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004717 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004718 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004719 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004720 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004721 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004722 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004723 }
4724 else
4725 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004726 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004727 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004728 // avoid compiler warnings
Bram Moolenaara80faa82020-04-12 19:37:17 +02004729 CLEAR_FIELD(save_multipos);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 }
4731
Bram Moolenaar16b35782016-09-09 20:29:50 +02004732 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004733 if (subs == NULL)
4734 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004735 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004736#ifdef FEAT_SYN_HL
4737 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4738 sub = &subs->synt;
4739 else
4740#endif
4741 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004742
4743 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004744 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004745 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004746 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004747 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004748 break;
4749 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004750 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004751 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004752}
4753
4754/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004755 * Like addstate(), but the new state(s) are put at position "*ip".
4756 * Used for zero-width matches, next state to use is the added one.
4757 * This makes sure the order of states to be tried does not change, which
4758 * matters for alternatives.
4759 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004760 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004761addstate_here(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004762 nfa_list_T *l, // runtime state list
4763 nfa_state_T *state, // state to update
4764 regsubs_T *subs, // pointers to subexpressions
4765 nfa_pim_T *pim, // postponed look-behind match
Bram Moolenaar05540972016-01-30 20:31:25 +01004766 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004767{
4768 int tlen = l->n;
4769 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004770 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004771 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004772
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004773 // First add the state(s) at the end, so that we know how many there are.
4774 // Pass the listidx as offset (avoids adding another argument to
4775 // addstate().
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004776 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4777 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004778 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004779
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004780 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004781 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004782 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004783
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004784 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004785 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004786 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004787 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004788 if (count == 1)
4789 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004790 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004791 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004792 }
4793 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004794 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004795 if (l->n + count - 1 >= l->len)
4796 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004797 // not enough space to move the new states, reallocate the list
4798 // and move the states to the right position
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004799 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004800 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004801 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004802
Bram Moolenaar688b3982019-02-13 21:47:36 +01004803 if ((long)(newsize >> 10) >= p_mmp)
4804 {
4805 emsg(_(e_maxmempat));
4806 return NULL;
4807 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004808 newl = alloc(newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004809 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004810 return NULL;
4811 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004812 mch_memmove(&(newl[0]),
4813 &(l->t[0]),
4814 sizeof(nfa_thread_T) * listidx);
4815 mch_memmove(&(newl[listidx]),
4816 &(l->t[l->n - count]),
4817 sizeof(nfa_thread_T) * count);
4818 mch_memmove(&(newl[listidx + count]),
4819 &(l->t[listidx + 1]),
4820 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4821 vim_free(l->t);
4822 l->t = newl;
4823 }
4824 else
4825 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004826 // make space for new states, then move them from the
4827 // end to the current position
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004828 mch_memmove(&(l->t[listidx + count]),
4829 &(l->t[listidx + 1]),
4830 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4831 mch_memmove(&(l->t[listidx]),
4832 &(l->t[l->n - 1]),
4833 sizeof(nfa_thread_T) * count);
4834 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004835 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004836 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004837 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004838
4839 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004840}
4841
4842/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004843 * Check character class "class" against current character c.
4844 */
4845 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004846check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004847{
4848 switch (class)
4849 {
4850 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004851 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004852 return OK;
4853 break;
4854 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004855 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004856 return OK;
4857 break;
4858 case NFA_CLASS_BLANK:
4859 if (c == ' ' || c == '\t')
4860 return OK;
4861 break;
4862 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004863 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004864 return OK;
4865 break;
4866 case NFA_CLASS_DIGIT:
4867 if (VIM_ISDIGIT(c))
4868 return OK;
4869 break;
4870 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004871 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 return OK;
4873 break;
4874 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004875 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876 return OK;
4877 break;
4878 case NFA_CLASS_PRINT:
4879 if (vim_isprintc(c))
4880 return OK;
4881 break;
4882 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004883 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884 return OK;
4885 break;
4886 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004887 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004888 return OK;
4889 break;
4890 case NFA_CLASS_UPPER:
4891 if (MB_ISUPPER(c))
4892 return OK;
4893 break;
4894 case NFA_CLASS_XDIGIT:
4895 if (vim_isxdigit(c))
4896 return OK;
4897 break;
4898 case NFA_CLASS_TAB:
4899 if (c == '\t')
4900 return OK;
4901 break;
4902 case NFA_CLASS_RETURN:
4903 if (c == '\r')
4904 return OK;
4905 break;
4906 case NFA_CLASS_BACKSPACE:
4907 if (c == '\b')
4908 return OK;
4909 break;
4910 case NFA_CLASS_ESCAPE:
4911 if (c == '\033')
4912 return OK;
4913 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004914 case NFA_CLASS_IDENT:
4915 if (vim_isIDc(c))
4916 return OK;
4917 break;
4918 case NFA_CLASS_KEYWORD:
4919 if (reg_iswordc(c))
4920 return OK;
4921 break;
4922 case NFA_CLASS_FNAME:
4923 if (vim_isfilec(c))
4924 return OK;
4925 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004926
4927 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004928 // should not be here :P
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004929 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004930 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004931 }
4932 return FAIL;
4933}
4934
Bram Moolenaar5714b802013-05-28 22:03:20 +02004935/*
4936 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004937 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004938 */
4939 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004940match_backref(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004941 regsub_T *sub, // pointers to subexpressions
Bram Moolenaar05540972016-01-30 20:31:25 +01004942 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004943 int *bytelen) // out: length of match in bytes
Bram Moolenaar5714b802013-05-28 22:03:20 +02004944{
4945 int len;
4946
4947 if (sub->in_use <= subidx)
4948 {
4949retempty:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004950 // backref was not set, match an empty string
Bram Moolenaar5714b802013-05-28 22:03:20 +02004951 *bytelen = 0;
4952 return TRUE;
4953 }
4954
4955 if (REG_MULTI)
4956 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004957 if (sub->list.multi[subidx].start_lnum < 0
4958 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004959 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004960 if (sub->list.multi[subidx].start_lnum == rex.lnum
4961 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004962 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004963 len = sub->list.multi[subidx].end_col
4964 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004965 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4966 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004967 {
4968 *bytelen = len;
4969 return TRUE;
4970 }
4971 }
4972 else
4973 {
4974 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004975 sub->list.multi[subidx].start_lnum,
4976 sub->list.multi[subidx].start_col,
4977 sub->list.multi[subidx].end_lnum,
4978 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004979 bytelen) == RA_MATCH)
4980 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004981 }
4982 }
4983 else
4984 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004985 if (sub->list.line[subidx].start == NULL
4986 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004987 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004988 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004989 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004990 {
4991 *bytelen = len;
4992 return TRUE;
4993 }
4994 }
4995 return FALSE;
4996}
4997
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004998#ifdef FEAT_SYN_HL
4999
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005000/*
5001 * Check for a match with \z subexpression "subidx".
5002 * Return TRUE if it matches.
5003 */
5004 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005005match_zref(
5006 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005007 int *bytelen) // out: length of match in bytes
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005008{
5009 int len;
5010
5011 cleanup_zsubexpr();
5012 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5013 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005014 // backref was not set, match an empty string
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005015 *bytelen = 0;
5016 return TRUE;
5017 }
5018
5019 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005020 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005021 {
5022 *bytelen = len;
5023 return TRUE;
5024 }
5025 return FALSE;
5026}
5027#endif
5028
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005029/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005030 * Save list IDs for all NFA states of "prog" into "list".
5031 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005032 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005033 */
5034 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005035nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005036{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005037 int i;
5038 nfa_state_T *p;
5039
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005040 // Order in the list is reverse, it's a bit faster that way.
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005041 p = &prog->state[0];
5042 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005044 list[i] = p->lastlist[1];
5045 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005046 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005047 }
5048}
5049
5050/*
5051 * Restore list IDs from "list" to all NFA states.
5052 */
5053 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005054nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005056 int i;
5057 nfa_state_T *p;
5058
5059 p = &prog->state[0];
5060 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005061 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005062 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005063 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064 }
5065}
5066
Bram Moolenaar423532e2013-05-29 21:14:42 +02005067 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005068nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005069{
5070 if (op == 1) return pos > val;
5071 if (op == 2) return pos < val;
5072 return val == pos;
5073}
5074
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005075static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005076
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005077/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005078 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005079 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5080 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005081 */
5082 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005083recursive_regmatch(
5084 nfa_state_T *state,
5085 nfa_pim_T *pim,
5086 nfa_regprog_T *prog,
5087 regsubs_T *submatch,
5088 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005089 int **listids,
5090 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005091{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005092 int save_reginput_col = (int)(rex.input - rex.line);
5093 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005094 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005095 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005096 save_se_T *save_nfa_endp = nfa_endp;
5097 save_se_T endpos;
5098 save_se_T *endposp = NULL;
5099 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005100 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005101
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005102 if (pim != NULL)
5103 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005104 // start at the position where the postponed match was
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005105 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005106 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005107 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005108 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005109 }
5110
Bram Moolenaardecd9542013-06-07 16:31:50 +02005111 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005112 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5113 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5114 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005115 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005116 // The recursive match must end at the current position. When "pim" is
5117 // not NULL it specifies the current position.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005118 endposp = &endpos;
5119 if (REG_MULTI)
5120 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005121 if (pim == NULL)
5122 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005123 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5124 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005125 }
5126 else
5127 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005128 }
5129 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005130 {
5131 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005132 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005133 else
5134 endpos.se_u.ptr = pim->end.ptr;
5135 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005136
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005137 // Go back the specified number of bytes, or as far as the
5138 // start of the previous line, to try matching "\@<=" or
5139 // not matching "\@<!". This is very inefficient, limit the number of
5140 // bytes if possible.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005141 if (state->val <= 0)
5142 {
5143 if (REG_MULTI)
5144 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005145 rex.line = reg_getline(--rex.lnum);
5146 if (rex.line == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005147 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005148 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005149 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005150 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151 }
5152 else
5153 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005154 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005155 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005156 // Not enough bytes in this line, go to end of
5157 // previous line.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005158 rex.line = reg_getline(--rex.lnum);
5159 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005160 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005161 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005162 rex.line = reg_getline(++rex.lnum);
5163 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005164 }
5165 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005166 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005167 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005168 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005169 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005170 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005171 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005172 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005173 }
5174 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005175 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005176 }
5177 }
5178
Bram Moolenaarf46da702013-06-02 22:37:42 +02005179#ifdef ENABLE_LOG
5180 if (log_fd != stderr)
5181 fclose(log_fd);
5182 log_fd = NULL;
5183#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005184 // Have to clear the lastlist field of the NFA nodes, so that
5185 // nfa_regmatch() and addstate() can run properly after recursion.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005186 if (nfa_ll_index == 1)
5187 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005188 // Already calling nfa_regmatch() recursively. Save the lastlist[1]
5189 // values and clear them.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005190 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005191 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005192 vim_free(*listids);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005193 *listids = ALLOC_MULT(int, prog->nstate);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005194 if (*listids == NULL)
5195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005196 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005197 return 0;
5198 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005199 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005200 }
5201 nfa_save_listids(prog, *listids);
5202 need_restore = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005203 // any value of rex.nfa_listid will do
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005204 }
5205 else
5206 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005207 // First recursive nfa_regmatch() call, switch to the second lastlist
5208 // entry. Make sure rex.nfa_listid is different from a previous
5209 // recursive call, because some states may still have this ID.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005210 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005211 if (rex.nfa_listid <= rex.nfa_alt_listid)
5212 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005213 }
5214
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005215 // Call nfa_regmatch() to check if the current concat matches at this
5216 // position. The concat ends with the node NFA_END_INVISIBLE
Bram Moolenaarf46da702013-06-02 22:37:42 +02005217 nfa_endp = endposp;
5218 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005219
5220 if (need_restore)
5221 nfa_restore_listids(prog, *listids);
5222 else
5223 {
5224 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005225 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005226 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005227
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005228 // restore position in input text
Bram Moolenaar0270f382018-07-17 05:43:58 +02005229 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005230 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005231 rex.line = reg_getline(rex.lnum);
5232 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005233 if (result != NFA_TOO_EXPENSIVE)
5234 {
5235 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005236 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005237 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005238 nfa_endp = save_nfa_endp;
5239
5240#ifdef ENABLE_LOG
5241 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5242 if (log_fd != NULL)
5243 {
5244 fprintf(log_fd, "****************************\n");
5245 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5246 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5247 fprintf(log_fd, "****************************\n");
5248 }
5249 else
5250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005251 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005252 log_fd = stderr;
5253 }
5254#endif
5255
5256 return result;
5257}
5258
Bram Moolenaara2d95102013-06-04 14:23:05 +02005259/*
5260 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005261 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005262 * NFA_ANY: 1
5263 * specific character: 99
5264 */
5265 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005266failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005267{
5268 int c = state->c;
5269 int l, r;
5270
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005271 // detect looping
Bram Moolenaara2d95102013-06-04 14:23:05 +02005272 if (depth > 4)
5273 return 1;
5274
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005275 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005276 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005277 case NFA_SPLIT:
5278 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005279 // avoid recursive stuff
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005280 return 1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005281 // two alternatives, use the lowest failure chance
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005282 l = failure_chance(state->out, depth + 1);
5283 r = failure_chance(state->out1, depth + 1);
5284 return l < r ? l : r;
5285
5286 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005287 // matches anything, unlikely to fail
Bram Moolenaara2d95102013-06-04 14:23:05 +02005288 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005289
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005290 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005291 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005292 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005293 // empty match works always
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005294 return 0;
5295
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005296 case NFA_START_INVISIBLE:
5297 case NFA_START_INVISIBLE_FIRST:
5298 case NFA_START_INVISIBLE_NEG:
5299 case NFA_START_INVISIBLE_NEG_FIRST:
5300 case NFA_START_INVISIBLE_BEFORE:
5301 case NFA_START_INVISIBLE_BEFORE_FIRST:
5302 case NFA_START_INVISIBLE_BEFORE_NEG:
5303 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5304 case NFA_START_PATTERN:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005305 // recursive regmatch is expensive, use low failure chance
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005306 return 5;
5307
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005308 case NFA_BOL:
5309 case NFA_EOL:
5310 case NFA_BOF:
5311 case NFA_EOF:
5312 case NFA_NEWL:
5313 return 99;
5314
5315 case NFA_BOW:
5316 case NFA_EOW:
5317 return 90;
5318
5319 case NFA_MOPEN:
5320 case NFA_MOPEN1:
5321 case NFA_MOPEN2:
5322 case NFA_MOPEN3:
5323 case NFA_MOPEN4:
5324 case NFA_MOPEN5:
5325 case NFA_MOPEN6:
5326 case NFA_MOPEN7:
5327 case NFA_MOPEN8:
5328 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005329#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005330 case NFA_ZOPEN:
5331 case NFA_ZOPEN1:
5332 case NFA_ZOPEN2:
5333 case NFA_ZOPEN3:
5334 case NFA_ZOPEN4:
5335 case NFA_ZOPEN5:
5336 case NFA_ZOPEN6:
5337 case NFA_ZOPEN7:
5338 case NFA_ZOPEN8:
5339 case NFA_ZOPEN9:
5340 case NFA_ZCLOSE:
5341 case NFA_ZCLOSE1:
5342 case NFA_ZCLOSE2:
5343 case NFA_ZCLOSE3:
5344 case NFA_ZCLOSE4:
5345 case NFA_ZCLOSE5:
5346 case NFA_ZCLOSE6:
5347 case NFA_ZCLOSE7:
5348 case NFA_ZCLOSE8:
5349 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005350#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005351 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005352 case NFA_MCLOSE1:
5353 case NFA_MCLOSE2:
5354 case NFA_MCLOSE3:
5355 case NFA_MCLOSE4:
5356 case NFA_MCLOSE5:
5357 case NFA_MCLOSE6:
5358 case NFA_MCLOSE7:
5359 case NFA_MCLOSE8:
5360 case NFA_MCLOSE9:
5361 case NFA_NCLOSE:
5362 return failure_chance(state->out, depth + 1);
5363
5364 case NFA_BACKREF1:
5365 case NFA_BACKREF2:
5366 case NFA_BACKREF3:
5367 case NFA_BACKREF4:
5368 case NFA_BACKREF5:
5369 case NFA_BACKREF6:
5370 case NFA_BACKREF7:
5371 case NFA_BACKREF8:
5372 case NFA_BACKREF9:
5373#ifdef FEAT_SYN_HL
5374 case NFA_ZREF1:
5375 case NFA_ZREF2:
5376 case NFA_ZREF3:
5377 case NFA_ZREF4:
5378 case NFA_ZREF5:
5379 case NFA_ZREF6:
5380 case NFA_ZREF7:
5381 case NFA_ZREF8:
5382 case NFA_ZREF9:
5383#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005384 // backreferences don't match in many places
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005385 return 94;
5386
5387 case NFA_LNUM_GT:
5388 case NFA_LNUM_LT:
5389 case NFA_COL_GT:
5390 case NFA_COL_LT:
5391 case NFA_VCOL_GT:
5392 case NFA_VCOL_LT:
5393 case NFA_MARK_GT:
5394 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005395 case NFA_VISUAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005396 // before/after positions don't match very often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005397 return 85;
5398
5399 case NFA_LNUM:
5400 return 90;
5401
5402 case NFA_CURSOR:
5403 case NFA_COL:
5404 case NFA_VCOL:
5405 case NFA_MARK:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005406 // specific positions rarely match
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005407 return 98;
5408
5409 case NFA_COMPOSING:
5410 return 95;
5411
5412 default:
5413 if (c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005414 // character match fails often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005415 return 95;
5416 }
5417
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005418 // something else, includes character classes
Bram Moolenaara2d95102013-06-04 14:23:05 +02005419 return 50;
5420}
5421
Bram Moolenaarf46da702013-06-02 22:37:42 +02005422/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005423 * Skip until the char "c" we know a match must start with.
5424 */
5425 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005426skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005427{
5428 char_u *s;
5429
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005430 // Used often, do some work to avoid call overhead.
Bram Moolenaara12a1612019-01-24 16:39:02 +01005431 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005432 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005433 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005434 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005435 if (s == NULL)
5436 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005437 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005438 return OK;
5439}
5440
5441/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005442 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005443 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005444 * Returns zero for no match, 1 for a match.
5445 */
5446 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005447find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005448{
5449 colnr_T col = startcol;
5450 int c1, c2;
5451 int len1, len2;
5452 int match;
5453
5454 for (;;)
5455 {
5456 match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005457 len2 = MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005458 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5459 {
5460 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005461 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar59de4172020-06-09 19:34:54 +02005462 if (c1 != c2 && (!rex.reg_ic || MB_CASEFOLD(c1) != MB_CASEFOLD(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005463 {
5464 match = FALSE;
5465 break;
5466 }
5467 len2 += MB_CHAR2LEN(c2);
5468 }
5469 if (match
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005470 // check that no composing char follows
Bram Moolenaar473de612013-06-08 18:19:48 +02005471 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005472 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005473 {
5474 cleanup_subexpr();
5475 if (REG_MULTI)
5476 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005477 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005478 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005479 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005480 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005481 }
5482 else
5483 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005484 rex.reg_startp[0] = rex.line + col;
5485 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005486 }
5487 return 1L;
5488 }
5489
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005490 // Try finding regstart after the current match.
5491 col += MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005492 if (skip_to_start(regstart, &col) == FAIL)
5493 break;
5494 }
5495 return 0L;
5496}
5497
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005498#ifdef FEAT_RELTIME
5499 static int
5500nfa_did_time_out()
5501{
5502 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5503 {
5504 if (nfa_timed_out != NULL)
5505 *nfa_timed_out = TRUE;
5506 return TRUE;
5507 }
5508 return FALSE;
5509}
5510#endif
5511
Bram Moolenaar473de612013-06-08 18:19:48 +02005512/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513 * Main matching routine.
5514 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005515 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005516 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005517 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005518 *
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005519 * Return TRUE if there is a match, FALSE if there is no match,
5520 * NFA_TOO_EXPENSIVE if we end up with too many states.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005521 * When there is a match "submatch" contains the positions.
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005522 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523 * Note: Caller must ensure that: start != NULL.
5524 */
5525 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005526nfa_regmatch(
5527 nfa_regprog_T *prog,
5528 nfa_state_T *start,
5529 regsubs_T *submatch,
5530 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531{
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005532 int result = FALSE;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005533 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005534 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005535 int go_to_nextline = FALSE;
5536 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005537 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005538 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005539 nfa_list_T *thislist;
5540 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005541 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005542 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005543 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005544 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005545 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005546 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005547 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005548 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005549#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005550 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005551#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005552
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005553 // Some patterns may take a long time to match, especially when using
5554 // recursive_regmatch(). Allow interrupting them with CTRL-C.
Bram Moolenaar41f12052013-08-25 17:01:42 +02005555 fast_breakcheck();
5556 if (got_int)
5557 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005558#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005559 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005560 return FALSE;
5561#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005562
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005563#ifdef NFA_REGEXP_DEBUG_LOG
5564 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5565 if (debug == NULL)
5566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005567 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005568 return FALSE;
5569 }
5570#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005571 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005573 // Allocate memory for the lists of nodes.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005574 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005575
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005576 list[0].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005577 list[0].len = prog->nstate + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005578 list[1].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005579 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005580 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005581 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582
5583#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005584 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005585 if (log_fd != NULL)
5586 {
5587 fprintf(log_fd, "**********************************\n");
5588 nfa_set_code(start->c);
5589 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5590 abs(start->id), code);
5591 fprintf(log_fd, "**********************************\n");
5592 }
5593 else
5594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005595 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 log_fd = stderr;
5597 }
5598#endif
5599
5600 thislist = &list[0];
5601 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005602 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005603 nextlist = &list[1];
5604 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005605 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005606#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005607 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005608#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005609 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005610
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005611 // Inline optimized code for addstate(thislist, start, m, 0) if we know
5612 // it's the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005613 if (toplevel)
5614 {
5615 if (REG_MULTI)
5616 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005617 m->norm.list.multi[0].start_lnum = rex.lnum;
5618 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005619 }
5620 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005621 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005622 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005623 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005624 }
5625 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005626 r = addstate(thislist, start, m, NULL, 0);
5627 if (r == NULL)
5628 {
5629 nfa_match = NFA_TOO_EXPENSIVE;
5630 goto theend;
5631 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005632
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005633#define ADD_STATE_IF_MATCH(state) \
5634 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005635 add_state = state->out; \
5636 add_off = clen; \
5637 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005638
5639 /*
5640 * Run for each character.
5641 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005642 for (;;)
5643 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005644 int curc;
5645 int clen;
5646
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005647 if (has_mbyte)
5648 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005649 curc = (*mb_ptr2char)(rex.input);
5650 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005651 }
5652 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005654 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005655 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005656 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005657 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005658 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005659 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005660 go_to_nextline = FALSE;
5661 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005662
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005663 // swap lists
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005664 thislist = &list[flag];
5665 nextlist = &list[flag ^= 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005666 nextlist->n = 0; // clear nextlist
Bram Moolenaar196ed142013-07-21 18:59:24 +02005667 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005668 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005669 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005670 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005671# ifdef FEAT_EVAL
5672 || nfa_fail_for_testing
5673# endif
5674 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005675 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005676 // too many states, retry with old engine
Bram Moolenaarfda37292014-11-05 14:27:36 +01005677 nfa_match = NFA_TOO_EXPENSIVE;
5678 goto theend;
5679 }
5680
Bram Moolenaar0270f382018-07-17 05:43:58 +02005681 thislist->id = rex.nfa_listid;
5682 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005683
5684#ifdef ENABLE_LOG
5685 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005686 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005687 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005688 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005689 {
5690 int i;
5691
5692 for (i = 0; i < thislist->n; i++)
5693 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5694 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005695 fprintf(log_fd, "\n");
5696#endif
5697
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005698#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005699 fprintf(debug, "\n-------------------\n");
5700#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005701 /*
5702 * If the state lists are empty we can stop.
5703 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005704 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005705 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005706
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005707 // compute nextlist
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005708 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005709 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005710 // If the list gets very long there probably is something wrong.
5711 // At least allow interrupting with CTRL-C.
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005712 fast_breakcheck();
5713 if (got_int)
5714 break;
5715#ifdef FEAT_RELTIME
5716 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5717 {
5718 nfa_time_count = 0;
5719 if (nfa_did_time_out())
5720 break;
5721 }
5722#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005723 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005724
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005725#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005726 nfa_set_code(t->state->c);
5727 fprintf(debug, "%s, ", code);
5728#endif
5729#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005730 {
5731 int col;
5732
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005733 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005734 col = -1;
5735 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005736 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005737 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005738 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005739 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005740 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005741 abs(t->state->id), (int)t->state->c, code, col,
5742 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005743 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005744#endif
5745
5746 /*
5747 * Handle the possible codes of the current state.
5748 * The most important is NFA_MATCH.
5749 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005750 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005751 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005752 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005753 switch (t->state->c)
5754 {
5755 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005756 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005757 // If the match ends before a composing characters and
5758 // rex.reg_icombine is not set, that is not really a match.
Bram Moolenaar6100d022016-10-02 16:51:57 +02005759 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005760 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005761
Bram Moolenaar963fee22013-05-26 21:47:28 +02005762 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005763 copy_sub(&submatch->norm, &t->subs.norm);
5764#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005765 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005766 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005767#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005768#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005769 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005770#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005771 // Found the left-most longest match, do not look at any other
5772 // states at this position. When the list of states is going
5773 // to be empty quit without advancing, so that "rex.input" is
5774 // correct.
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005775 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005776 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005777 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005778 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005779
5780 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005781 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005782 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005783 /*
5784 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005785 * NFA_START_INVISIBLE_BEFORE node.
5786 * They surround a zero-width group, used with "\@=", "\&",
5787 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005788 * If we got here, it means that the current "invisible" group
5789 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005790 * nfa_regmatch(). For a look-behind match only when it ends
5791 * in the position in "nfa_endp".
5792 * Submatches are stored in *m, and used in the parent call.
5793 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005794#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005795 if (nfa_endp != NULL)
5796 {
5797 if (REG_MULTI)
5798 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005799 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005800 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005801 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005802 nfa_endp->se_u.pos.col);
5803 else
5804 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005805 (int)(rex.input - rex.line),
5806 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005807 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005808#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005809 // If "nfa_endp" is set it's only a match if it ends at
5810 // "nfa_endp"
Bram Moolenaarf46da702013-06-02 22:37:42 +02005811 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005812 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5813 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005814 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005815 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005816 break;
5817
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005818 // do not set submatches for \@!
Bram Moolenaardecd9542013-06-07 16:31:50 +02005819 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005820 {
5821 copy_sub(&m->norm, &t->subs.norm);
5822#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005823 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005824 copy_sub(&m->synt, &t->subs.synt);
5825#endif
5826 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005827#ifdef ENABLE_LOG
5828 fprintf(log_fd, "Match found:\n");
5829 log_subsexpr(m);
5830#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005831 nfa_match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005832 // See comment above at "goto nextchar".
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005833 if (nextlist->n == 0)
5834 clen = 0;
5835 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005836
5837 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005838 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005839 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005840 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005841 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005842 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005843 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005844 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005845 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005846#ifdef ENABLE_LOG
5847 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5848 failure_chance(t->state->out, 0),
5849 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005850#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005851 // Do it directly if there already is a PIM or when
5852 // nfa_postprocess() detected it will work better.
Bram Moolenaara2947e22013-06-11 22:44:09 +02005853 if (t->pim.result != NFA_PIM_UNUSED
5854 || t->state->c == NFA_START_INVISIBLE_FIRST
5855 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5856 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5857 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005858 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005859 int in_use = m->norm.in_use;
5860
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005861 // Copy submatch info for the recursive call, opposite
5862 // of what happens on success below.
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005863 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005864#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005865 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005866 copy_sub_off(&m->synt, &t->subs.synt);
5867#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005868
Bram Moolenaara2d95102013-06-04 14:23:05 +02005869 /*
5870 * First try matching the invisible match, then what
5871 * follows.
5872 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005873 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005874 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005875 if (result == NFA_TOO_EXPENSIVE)
5876 {
5877 nfa_match = result;
5878 goto theend;
5879 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005880
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005881 // for \@! and \@<! it is a match when the result is
5882 // FALSE
Bram Moolenaardecd9542013-06-07 16:31:50 +02005883 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005884 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5885 || t->state->c
5886 == NFA_START_INVISIBLE_BEFORE_NEG
5887 || t->state->c
5888 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005889 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005890 // Copy submatch info from the recursive call
Bram Moolenaara2d95102013-06-04 14:23:05 +02005891 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005892#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005893 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005894 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005895#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005896 // If the pattern has \ze and it matched in the
5897 // sub pattern, use it.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005898 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005899
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005900 // t->state->out1 is the corresponding
5901 // END_INVISIBLE node; Add its out to the current
5902 // list (zero-width match).
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005903 add_here = TRUE;
5904 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005905 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005906 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005907 }
5908 else
5909 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005910 nfa_pim_T pim;
5911
Bram Moolenaara2d95102013-06-04 14:23:05 +02005912 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005913 * First try matching what follows. Only if a match
5914 * is found verify the invisible match matches. Add a
5915 * nfa_pim_T to the following states, it contains info
5916 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005917 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005918 pim.state = t->state;
5919 pim.result = NFA_PIM_TODO;
5920 pim.subs.norm.in_use = 0;
5921#ifdef FEAT_SYN_HL
5922 pim.subs.synt.in_use = 0;
5923#endif
5924 if (REG_MULTI)
5925 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005926 pim.end.pos.col = (int)(rex.input - rex.line);
5927 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005928 }
5929 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005930 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005931
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005932 // t->state->out1 is the corresponding END_INVISIBLE
5933 // node; Add its out to the current list (zero-width
5934 // match).
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005935 if (addstate_here(thislist, t->state->out1->out,
5936 &t->subs, &pim, &listidx) == NULL)
5937 {
5938 nfa_match = NFA_TOO_EXPENSIVE;
5939 goto theend;
5940 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005941 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005942 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005943 break;
5944
Bram Moolenaar87953742013-06-05 18:52:40 +02005945 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005946 {
5947 nfa_state_T *skip = NULL;
5948#ifdef ENABLE_LOG
5949 int skip_lid = 0;
5950#endif
5951
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005952 // There is no point in trying to match the pattern if the
5953 // output state is not going to be added to the list.
Bram Moolenaar43e02982013-06-07 17:31:29 +02005954 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5955 {
5956 skip = t->state->out1->out;
5957#ifdef ENABLE_LOG
5958 skip_lid = nextlist->id;
5959#endif
5960 }
5961 else if (state_in_list(nextlist,
5962 t->state->out1->out->out, &t->subs))
5963 {
5964 skip = t->state->out1->out->out;
5965#ifdef ENABLE_LOG
5966 skip_lid = nextlist->id;
5967#endif
5968 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005969 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005970 t->state->out1->out->out, &t->subs))
5971 {
5972 skip = t->state->out1->out->out;
5973#ifdef ENABLE_LOG
5974 skip_lid = thislist->id;
5975#endif
5976 }
5977 if (skip != NULL)
5978 {
5979#ifdef ENABLE_LOG
5980 nfa_set_code(skip->c);
5981 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5982 abs(skip->id), skip_lid, skip->c, code);
5983#endif
5984 break;
5985 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005986 // Copy submatch info to the recursive call, opposite of what
5987 // happens afterwards.
Bram Moolenaar699c1202013-09-25 16:41:54 +02005988 copy_sub_off(&m->norm, &t->subs.norm);
5989#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005990 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005991 copy_sub_off(&m->synt, &t->subs.synt);
5992#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005993
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005994 // First try matching the pattern.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005995 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005996 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005997 if (result == NFA_TOO_EXPENSIVE)
5998 {
5999 nfa_match = result;
6000 goto theend;
6001 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006002 if (result)
6003 {
6004 int bytelen;
6005
6006#ifdef ENABLE_LOG
6007 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6008 log_subsexpr(m);
6009#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006010 // Copy submatch info from the recursive call
Bram Moolenaar87953742013-06-05 18:52:40 +02006011 copy_sub_off(&t->subs.norm, &m->norm);
6012#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006013 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006014 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006015#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006016 // Now we need to skip over the matched text and then
6017 // continue with what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02006018 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006019 // TODO: multi-line match
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006020 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006021 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006022 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006023 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006024
6025#ifdef ENABLE_LOG
6026 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6027#endif
6028 if (bytelen == 0)
6029 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006030 // empty match, output of corresponding
6031 // NFA_END_PATTERN/NFA_SKIP to be used at current
6032 // position
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006033 add_here = TRUE;
6034 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006035 }
6036 else if (bytelen <= clen)
6037 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006038 // match current character, output of corresponding
6039 // NFA_END_PATTERN to be used at next position.
Bram Moolenaar87953742013-06-05 18:52:40 +02006040 add_state = t->state->out1->out->out;
6041 add_off = clen;
6042 }
6043 else
6044 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006045 // skip over the matched characters, set character
6046 // count in NFA_SKIP
Bram Moolenaar87953742013-06-05 18:52:40 +02006047 add_state = t->state->out1->out;
6048 add_off = bytelen;
6049 add_count = bytelen - clen;
6050 }
6051 }
6052 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006053 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006054
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006055 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006056 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006057 {
6058 add_here = TRUE;
6059 add_state = t->state->out;
6060 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006061 break;
6062
6063 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006064 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006065 {
6066 add_here = TRUE;
6067 add_state = t->state->out;
6068 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006069 break;
6070
6071 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006072 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006073
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006074 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006075 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006076 else if (has_mbyte)
6077 {
6078 int this_class;
6079
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006080 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006081 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006082 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006083 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006084 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006085 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006087 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006088 || (rex.input > rex.line
6089 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006090 result = FALSE;
6091 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006092 {
6093 add_here = TRUE;
6094 add_state = t->state->out;
6095 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006096 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097
6098 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006099 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006100 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006101 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 else if (has_mbyte)
6103 {
6104 int this_class, prev_class;
6105
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006106 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006107 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006108 prev_class = reg_prev_class();
6109 if (this_class == prev_class
6110 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006111 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006112 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006113 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6114 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006115 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006116 result = FALSE;
6117 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006118 {
6119 add_here = TRUE;
6120 add_state = t->state->out;
6121 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006122 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006123
Bram Moolenaar4b780632013-05-31 22:14:52 +02006124 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006125 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006126 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006127 {
6128 add_here = TRUE;
6129 add_state = t->state->out;
6130 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006131 break;
6132
6133 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006134 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006135 {
6136 add_here = TRUE;
6137 add_state = t->state->out;
6138 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006139 break;
6140
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006141 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006142 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006143 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006144 int len = 0;
6145 nfa_state_T *end;
6146 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006147 int cchars[MAX_MCO];
6148 int ccount = 0;
6149 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006150
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006151 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006152 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006153 if (utf_iscomposing(sta->c))
6154 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006155 // Only match composing character(s), ignore base
6156 // character. Used for ".{composing}" and "{composing}"
6157 // (no preceding character).
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006158 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006159 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006160 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006161 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006162 // If \Z was present, then ignore composing characters.
6163 // When ignoring the base character this always matches.
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006164 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006165 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006166 else
6167 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006168 while (sta->c != NFA_END_COMPOSING)
6169 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006170 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006171
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006172 // Check base character matches first, unless ignored.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006173 else if (len > 0 || mc == sta->c)
6174 {
6175 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006176 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006177 len += mb_char2len(mc);
6178 sta = sta->out;
6179 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006180
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006181 // We don't care about the order of composing characters.
6182 // Get them into cchars[] first.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006183 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006184 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006185 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006186 cchars[ccount++] = mc;
6187 len += mb_char2len(mc);
6188 if (ccount == MAX_MCO)
6189 break;
6190 }
6191
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006192 // Check that each composing char in the pattern matches a
6193 // composing char in the text. We do not check if all
6194 // composing chars are matched.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006195 result = OK;
6196 while (sta->c != NFA_END_COMPOSING)
6197 {
6198 for (j = 0; j < ccount; ++j)
6199 if (cchars[j] == sta->c)
6200 break;
6201 if (j == ccount)
6202 {
6203 result = FAIL;
6204 break;
6205 }
6206 sta = sta->out;
6207 }
6208 }
6209 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006210 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006211
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006212 end = t->state->out1; // NFA_END_COMPOSING
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006213 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006214 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006215 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006216
6217 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006218 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006219 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006220 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006221 go_to_nextline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006222 // Pass -1 for the offset, which means taking the position
6223 // at the start of the next line.
Bram Moolenaara2d95102013-06-04 14:23:05 +02006224 add_state = t->state->out;
6225 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006226 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006227 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006228 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006229 // match \n as if it is an ordinary character
Bram Moolenaara2d95102013-06-04 14:23:05 +02006230 add_state = t->state->out;
6231 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006232 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006233 break;
6234
Bram Moolenaar417bad22013-06-07 14:08:30 +02006235 case NFA_START_COLL:
6236 case NFA_START_NEG_COLL:
6237 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006238 // What follows is a list of characters, until NFA_END_COLL.
6239 // One of them must match or none of them must match.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006240 nfa_state_T *state;
6241 int result_if_matched;
6242 int c1, c2;
6243
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006244 // Never match EOL. If it's part of the collection it is added
6245 // as a separate state with an OR.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006246 if (curc == NUL)
6247 break;
6248
6249 state = t->state->out;
6250 result_if_matched = (t->state->c == NFA_START_COLL);
6251 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006252 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006253 if (state->c == NFA_END_COLL)
6254 {
6255 result = !result_if_matched;
6256 break;
6257 }
6258 if (state->c == NFA_RANGE_MIN)
6259 {
6260 c1 = state->val;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006261 state = state->out; // advance to NFA_RANGE_MAX
Bram Moolenaar417bad22013-06-07 14:08:30 +02006262 c2 = state->val;
6263#ifdef ENABLE_LOG
6264 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6265 curc, c1, c2);
6266#endif
6267 if (curc >= c1 && curc <= c2)
6268 {
6269 result = result_if_matched;
6270 break;
6271 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006272 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006273 {
Bram Moolenaar59de4172020-06-09 19:34:54 +02006274 int curc_low = MB_CASEFOLD(curc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02006275 int done = FALSE;
6276
6277 for ( ; c1 <= c2; ++c1)
Bram Moolenaar59de4172020-06-09 19:34:54 +02006278 if (MB_CASEFOLD(c1) == curc_low)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006279 {
6280 result = result_if_matched;
6281 done = TRUE;
6282 break;
6283 }
6284 if (done)
6285 break;
6286 }
6287 }
6288 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006289 : (curc == state->c
Bram Moolenaar59de4172020-06-09 19:34:54 +02006290 || (rex.reg_ic && MB_CASEFOLD(curc)
6291 == MB_CASEFOLD(state->c))))
Bram Moolenaar417bad22013-06-07 14:08:30 +02006292 {
6293 result = result_if_matched;
6294 break;
6295 }
6296 state = state->out;
6297 }
6298 if (result)
6299 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006300 // next state is in out of the NFA_END_COLL, out1 of
6301 // START points to the END state
Bram Moolenaar417bad22013-06-07 14:08:30 +02006302 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006303 add_off = clen;
6304 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006305 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006306 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307
6308 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006309 // Any char except '\0', (end of input) does not match.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006310 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006311 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006312 add_state = t->state->out;
6313 add_off = clen;
6314 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006315 break;
6316
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006317 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006318 // On a composing character skip over it. Otherwise do
6319 // nothing. Always matches.
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006320 if (enc_utf8 && utf_iscomposing(curc))
6321 {
6322 add_off = clen;
6323 }
6324 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006325 {
6326 add_here = TRUE;
6327 add_off = 0;
6328 }
6329 add_state = t->state->out;
6330 break;
6331
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006332 /*
6333 * Character classes like \a for alpha, \d for digit etc.
6334 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006335 case NFA_IDENT: // \i
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006336 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006337 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 break;
6339
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006340 case NFA_SIDENT: // \I
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006341 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006342 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006343 break;
6344
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006345 case NFA_KWORD: // \k
Bram Moolenaar0270f382018-07-17 05:43:58 +02006346 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006347 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006348 break;
6349
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006350 case NFA_SKWORD: // \K
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006351 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006352 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006353 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006354 break;
6355
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006356 case NFA_FNAME: // \f
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006357 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006358 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006359 break;
6360
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006361 case NFA_SFNAME: // \F
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006362 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006363 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006364 break;
6365
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006366 case NFA_PRINT: // \p
Bram Moolenaar0270f382018-07-17 05:43:58 +02006367 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006368 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 break;
6370
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006371 case NFA_SPRINT: // \P
Bram Moolenaar0270f382018-07-17 05:43:58 +02006372 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006373 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006374 break;
6375
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006376 case NFA_WHITE: // \s
Bram Moolenaar1c465442017-03-12 20:10:05 +01006377 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006378 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 break;
6380
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006381 case NFA_NWHITE: // \S
Bram Moolenaar1c465442017-03-12 20:10:05 +01006382 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006383 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 break;
6385
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006386 case NFA_DIGIT: // \d
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006387 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006388 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389 break;
6390
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006391 case NFA_NDIGIT: // \D
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006392 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006393 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394 break;
6395
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006396 case NFA_HEX: // \x
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006397 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006398 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 break;
6400
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006401 case NFA_NHEX: // \X
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006402 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006403 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006404 break;
6405
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006406 case NFA_OCTAL: // \o
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006407 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006408 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006409 break;
6410
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006411 case NFA_NOCTAL: // \O
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006412 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006413 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 break;
6415
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006416 case NFA_WORD: // \w
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006417 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006418 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006419 break;
6420
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006421 case NFA_NWORD: // \W
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006422 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006423 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006424 break;
6425
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006426 case NFA_HEAD: // \h
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006427 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006428 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006429 break;
6430
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006431 case NFA_NHEAD: // \H
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006432 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006433 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006434 break;
6435
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006436 case NFA_ALPHA: // \a
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006437 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006438 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006439 break;
6440
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006441 case NFA_NALPHA: // \A
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006442 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006443 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006444 break;
6445
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006446 case NFA_LOWER: // \l
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006447 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006448 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006449 break;
6450
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006451 case NFA_NLOWER: // \L
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006452 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006453 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006454 break;
6455
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006456 case NFA_UPPER: // \u
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006457 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006458 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006459 break;
6460
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006461 case NFA_NUPPER: // \U
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006462 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006463 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006464 break;
6465
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006466 case NFA_LOWER_IC: // [a-z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006467 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006468 ADD_STATE_IF_MATCH(t->state);
6469 break;
6470
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006471 case NFA_NLOWER_IC: // [^a-z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006472 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006473 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006474 ADD_STATE_IF_MATCH(t->state);
6475 break;
6476
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006477 case NFA_UPPER_IC: // [A-Z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006478 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006479 ADD_STATE_IF_MATCH(t->state);
6480 break;
6481
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006482 case NFA_NUPPER_IC: // ^[A-Z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006483 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006484 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006485 ADD_STATE_IF_MATCH(t->state);
6486 break;
6487
Bram Moolenaar5714b802013-05-28 22:03:20 +02006488 case NFA_BACKREF1:
6489 case NFA_BACKREF2:
6490 case NFA_BACKREF3:
6491 case NFA_BACKREF4:
6492 case NFA_BACKREF5:
6493 case NFA_BACKREF6:
6494 case NFA_BACKREF7:
6495 case NFA_BACKREF8:
6496 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006497#ifdef FEAT_SYN_HL
6498 case NFA_ZREF1:
6499 case NFA_ZREF2:
6500 case NFA_ZREF3:
6501 case NFA_ZREF4:
6502 case NFA_ZREF5:
6503 case NFA_ZREF6:
6504 case NFA_ZREF7:
6505 case NFA_ZREF8:
6506 case NFA_ZREF9:
6507#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006508 // \1 .. \9 \z1 .. \z9
Bram Moolenaar5714b802013-05-28 22:03:20 +02006509 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006510 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006511 int bytelen;
6512
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006513 if (t->state->c <= NFA_BACKREF9)
6514 {
6515 subidx = t->state->c - NFA_BACKREF1 + 1;
6516 result = match_backref(&t->subs.norm, subidx, &bytelen);
6517 }
6518#ifdef FEAT_SYN_HL
6519 else
6520 {
6521 subidx = t->state->c - NFA_ZREF1 + 1;
6522 result = match_zref(subidx, &bytelen);
6523 }
6524#endif
6525
Bram Moolenaar5714b802013-05-28 22:03:20 +02006526 if (result)
6527 {
6528 if (bytelen == 0)
6529 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006530 // empty match always works, output of NFA_SKIP to be
6531 // used next
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006532 add_here = TRUE;
6533 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006534 }
6535 else if (bytelen <= clen)
6536 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006537 // match current character, jump ahead to out of
6538 // NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006539 add_state = t->state->out->out;
6540 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006541 }
6542 else
6543 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006544 // skip over the matched characters, set character
6545 // count in NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006546 add_state = t->state->out;
6547 add_off = bytelen;
6548 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006549 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006550 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006551 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006552 }
6553 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006554 // character of previous matching \1 .. \9 or \@>
Bram Moolenaar5714b802013-05-28 22:03:20 +02006555 if (t->count - clen <= 0)
6556 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006557 // end of match, go to what follows
Bram Moolenaara2d95102013-06-04 14:23:05 +02006558 add_state = t->state->out;
6559 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006560 }
6561 else
6562 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006563 // add state again with decremented count
Bram Moolenaara2d95102013-06-04 14:23:05 +02006564 add_state = t->state;
6565 add_off = 0;
6566 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006567 }
6568 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006569
Bram Moolenaar423532e2013-05-29 21:14:42 +02006570 case NFA_LNUM:
6571 case NFA_LNUM_GT:
6572 case NFA_LNUM_LT:
6573 result = (REG_MULTI &&
6574 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006575 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006576 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006577 {
6578 add_here = TRUE;
6579 add_state = t->state->out;
6580 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006581 break;
6582
6583 case NFA_COL:
6584 case NFA_COL_GT:
6585 case NFA_COL_LT:
6586 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006587 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006588 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006589 {
6590 add_here = TRUE;
6591 add_state = t->state->out;
6592 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006593 break;
6594
6595 case NFA_VCOL:
6596 case NFA_VCOL_GT:
6597 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006598 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006599 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006600 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006601 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006602
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006603 // Bail out quickly when there can't be a match, avoid the
6604 // overhead of win_linetabsize() on long lines.
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006605 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006606 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006607 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006608 result = FALSE;
6609 if (op == 1 && col - 1 > t->state->val && col > 100)
6610 {
6611 int ts = wp->w_buffer->b_p_ts;
6612
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006613 // Guess that a character won't use more columns than
6614 // 'tabstop', with a minimum of 4.
Bram Moolenaaref795d12015-01-18 16:46:32 +01006615 if (ts < 4)
6616 ts = 4;
6617 result = col > t->state->val * ts;
6618 }
6619 if (!result)
6620 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006621 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006622 if (result)
6623 {
6624 add_here = TRUE;
6625 add_state = t->state->out;
6626 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006627 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006628 break;
6629
Bram Moolenaar044aa292013-06-04 21:27:38 +02006630 case NFA_MARK:
6631 case NFA_MARK_GT:
6632 case NFA_MARK_LT:
6633 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006634 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006635
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006636 // Compare the mark position to the match position.
6637 result = (pos != NULL // mark doesn't exist
6638 && pos->lnum > 0 // mark isn't set in reg_buf
Bram Moolenaar0270f382018-07-17 05:43:58 +02006639 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6640 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006641 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006642 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006643 ? t->state->c == NFA_MARK_GT
6644 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006645 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006646 ? t->state->c == NFA_MARK_GT
6647 : t->state->c == NFA_MARK_LT)));
6648 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006649 {
6650 add_here = TRUE;
6651 add_state = t->state->out;
6652 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006653 break;
6654 }
6655
Bram Moolenaar423532e2013-05-29 21:14:42 +02006656 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006657 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006658 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006659 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006660 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006661 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006662 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006663 {
6664 add_here = TRUE;
6665 add_state = t->state->out;
6666 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006667 break;
6668
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006669 case NFA_VISUAL:
6670 result = reg_match_visual();
6671 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006672 {
6673 add_here = TRUE;
6674 add_state = t->state->out;
6675 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006676 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006677
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006678 case NFA_MOPEN1:
6679 case NFA_MOPEN2:
6680 case NFA_MOPEN3:
6681 case NFA_MOPEN4:
6682 case NFA_MOPEN5:
6683 case NFA_MOPEN6:
6684 case NFA_MOPEN7:
6685 case NFA_MOPEN8:
6686 case NFA_MOPEN9:
6687#ifdef FEAT_SYN_HL
6688 case NFA_ZOPEN:
6689 case NFA_ZOPEN1:
6690 case NFA_ZOPEN2:
6691 case NFA_ZOPEN3:
6692 case NFA_ZOPEN4:
6693 case NFA_ZOPEN5:
6694 case NFA_ZOPEN6:
6695 case NFA_ZOPEN7:
6696 case NFA_ZOPEN8:
6697 case NFA_ZOPEN9:
6698#endif
6699 case NFA_NOPEN:
6700 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006701 // These states are only added to be able to bail out when
6702 // they are added again, nothing is to be done.
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006703 break;
6704
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006705 default: // regular character
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006706 {
6707 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006708
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006709#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006710 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006711 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006712#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006713 result = (c == curc);
6714
Bram Moolenaar6100d022016-10-02 16:51:57 +02006715 if (!result && rex.reg_ic)
Bram Moolenaar59de4172020-06-09 19:34:54 +02006716 result = MB_CASEFOLD(c) == MB_CASEFOLD(curc);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006717 // If rex.reg_icombine is not set only skip over the character
6718 // itself. When it is set skip over composing characters.
Bram Moolenaar6100d022016-10-02 16:51:57 +02006719 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006720 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006721 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006722 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006723 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006724
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006725 } // switch (t->state->c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006726
6727 if (add_state != NULL)
6728 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006729 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006730 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006731
6732 if (t->pim.result == NFA_PIM_UNUSED)
6733 pim = NULL;
6734 else
6735 pim = &t->pim;
6736
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006737 // Handle the postponed invisible match if the match might end
6738 // without advancing and before the end of the line.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006739 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006740 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006741 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006742 {
6743#ifdef ENABLE_LOG
6744 fprintf(log_fd, "\n");
6745 fprintf(log_fd, "==================================\n");
6746 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6747 fprintf(log_fd, "\n");
6748#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006749 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006750 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006751 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006752 // for \@! and \@<! it is a match when the result is
6753 // FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006754 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006755 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6756 || pim->state->c
6757 == NFA_START_INVISIBLE_BEFORE_NEG
6758 || pim->state->c
6759 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006760 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006761 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006762 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006763#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006764 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006765 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006766#endif
6767 }
6768 }
6769 else
6770 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006771 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772#ifdef ENABLE_LOG
6773 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006774 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006775 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6776 fprintf(log_fd, "\n");
6777#endif
6778 }
6779
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006780 // for \@! and \@<! it is a match when result is FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006781 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006782 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6783 || pim->state->c
6784 == NFA_START_INVISIBLE_BEFORE_NEG
6785 || pim->state->c
6786 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006787 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006788 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006789 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006790#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006791 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006792 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006793#endif
6794 }
6795 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006796 // look-behind match failed, don't add the state
Bram Moolenaara2d95102013-06-04 14:23:05 +02006797 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006798
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006799 // Postponed invisible match was handled, don't add it to
6800 // following states.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006801 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006802 }
6803
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006804 // If "pim" points into l->t it will become invalid when
6805 // adding the state causes the list to be reallocated. Make a
6806 // local copy to avoid that.
Bram Moolenaara951e352013-10-06 15:46:11 +02006807 if (pim == &t->pim)
6808 {
6809 copy_pim(&pim_copy, pim);
6810 pim = &pim_copy;
6811 }
6812
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006813 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006814 r = addstate_here(thislist, add_state, &t->subs,
6815 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006816 else
6817 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006818 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006819 if (add_count > 0)
6820 nextlist->t[nextlist->n - 1].count = add_count;
6821 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006822 if (r == NULL)
6823 {
6824 nfa_match = NFA_TOO_EXPENSIVE;
6825 goto theend;
6826 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006827 }
6828
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006829 } // for (thislist = thislist; thislist->state; thislist++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006830
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006831 // Look for the start of a match in the current position by adding the
6832 // start state to the list of states.
6833 // The first found match is the leftmost one, thus the order of states
6834 // matters!
6835 // Do not add the start state in recursive calls of nfa_regmatch(),
6836 // because recursive calls should only start in the first position.
6837 // Unless "nfa_endp" is not NULL, then we match the end position.
6838 // Also don't start a match past the first line.
Bram Moolenaar61602c52013-06-01 19:54:43 +02006839 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006840 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006841 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006842 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006843 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006844 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006845 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006846 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006847 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6848 || (rex.lnum == nfa_endp->se_u.pos.lnum
6849 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006850 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006851 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006852 {
6853#ifdef ENABLE_LOG
6854 fprintf(log_fd, "(---) STARTSTATE\n");
6855#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006856 // Inline optimized code for addstate() if we know the state is
6857 // the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006858 if (toplevel)
6859 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006860 int add = TRUE;
6861 int c;
6862
6863 if (prog->regstart != NUL && clen != 0)
6864 {
6865 if (nextlist->n == 0)
6866 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006867 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006868
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006869 // Nextlist is empty, we can skip ahead to the
6870 // character that must appear at the start.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006871 if (skip_to_start(prog->regstart, &col) == FAIL)
6872 break;
6873#ifdef ENABLE_LOG
6874 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006875 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006876#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006877 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006878 }
6879 else
6880 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006881 // Checking if the required start character matches is
6882 // cheaper than adding a state that won't match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02006883 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006884 if (c != prog->regstart && (!rex.reg_ic
Bram Moolenaar59de4172020-06-09 19:34:54 +02006885 || MB_CASEFOLD(c) != MB_CASEFOLD(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006886 {
6887#ifdef ENABLE_LOG
6888 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6889#endif
6890 add = FALSE;
6891 }
6892 }
6893 }
6894
6895 if (add)
6896 {
6897 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006898 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006899 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006900 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006901 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006902 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6903 {
6904 nfa_match = NFA_TOO_EXPENSIVE;
6905 goto theend;
6906 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006907 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006908 }
6909 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006910 {
6911 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6912 {
6913 nfa_match = NFA_TOO_EXPENSIVE;
6914 goto theend;
6915 }
6916 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006917 }
6918
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006919#ifdef ENABLE_LOG
6920 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006921 {
6922 int i;
6923
6924 for (i = 0; i < thislist->n; i++)
6925 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6926 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006927 fprintf(log_fd, "\n");
6928#endif
6929
6930nextchar:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006931 // Advance to the next character, or advance to the next line, or
6932 // finish.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006933 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006934 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006935 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006936 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006937 reg_nextline();
6938 else
6939 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006940
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006941 // Allow interrupting with CTRL-C.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006942 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006943 if (got_int)
6944 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006945#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006946 // Check for timeout once in a twenty times to avoid overhead.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006947 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6948 {
6949 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006950 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006951 break;
6952 }
6953#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006954 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006955
6956#ifdef ENABLE_LOG
6957 if (log_fd != stderr)
6958 fclose(log_fd);
6959 log_fd = NULL;
6960#endif
6961
6962theend:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006963 // Free memory
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006964 vim_free(list[0].t);
6965 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006966 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006967#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006968#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969 fclose(debug);
6970#endif
6971
Bram Moolenaar963fee22013-05-26 21:47:28 +02006972 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006973}
6974
6975/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006976 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006977 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006978 */
6979 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006980nfa_regtry(
6981 nfa_regprog_T *prog,
6982 colnr_T col,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006983 proftime_T *tm UNUSED, // timeout limit or NULL
6984 int *timed_out UNUSED) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006985{
6986 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006987 regsubs_T subs, m;
6988 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006989 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006990#ifdef ENABLE_LOG
6991 FILE *f;
6992#endif
6993
Bram Moolenaar0270f382018-07-17 05:43:58 +02006994 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006995#ifdef FEAT_RELTIME
6996 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006997 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006998 nfa_time_count = 0;
6999#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000
7001#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007002 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007003 if (f != NULL)
7004 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007005 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007006#ifdef DEBUG
7007 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7008#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007009 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007010 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007011 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007012 fprintf(f, "\n\n");
7013 fclose(f);
7014 }
7015 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007016 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017#endif
7018
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007019 clear_sub(&subs.norm);
7020 clear_sub(&m.norm);
7021#ifdef FEAT_SYN_HL
7022 clear_sub(&subs.synt);
7023 clear_sub(&m.synt);
7024#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007025
Bram Moolenaarfda37292014-11-05 14:27:36 +01007026 result = nfa_regmatch(prog, start, &subs, &m);
7027 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007029 else if (result == NFA_TOO_EXPENSIVE)
7030 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007031
7032 cleanup_subexpr();
7033 if (REG_MULTI)
7034 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007035 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007036 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007037 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7038 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007039
Bram Moolenaar6100d022016-10-02 16:51:57 +02007040 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7041 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042 }
7043
Bram Moolenaar6100d022016-10-02 16:51:57 +02007044 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007046 rex.reg_startpos[0].lnum = 0;
7047 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007049 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007050 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007051 // pattern has a \ze but it didn't match, use current end
Bram Moolenaar0270f382018-07-17 05:43:58 +02007052 rex.reg_endpos[0].lnum = rex.lnum;
7053 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007054 }
7055 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007056 // Use line number of "\ze".
Bram Moolenaar0270f382018-07-17 05:43:58 +02007057 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007058 }
7059 else
7060 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007061 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007062 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007063 rex.reg_startp[i] = subs.norm.list.line[i].start;
7064 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007065 }
7066
Bram Moolenaar6100d022016-10-02 16:51:57 +02007067 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007068 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007069 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007070 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007071 }
7072
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007073#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007074 // Package any found \z(...\) matches for export. Default is none.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007075 unref_extmatch(re_extmatch_out);
7076 re_extmatch_out = NULL;
7077
7078 if (prog->reghasz == REX_SET)
7079 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007080 cleanup_zsubexpr();
7081 re_extmatch_out = make_extmatch();
Bram Moolenaar7c77b342019-12-22 19:40:40 +01007082 if (re_extmatch_out == NULL)
7083 return 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007084 // Loop over \z1, \z2, etc. There is no \z0.
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007085 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007086 {
7087 if (REG_MULTI)
7088 {
7089 struct multipos *mpos = &subs.synt.list.multi[i];
7090
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007091 // Only accept single line matches that are valid.
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007092 if (mpos->start_lnum >= 0
7093 && mpos->start_lnum == mpos->end_lnum
7094 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007095 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007096 vim_strnsave(reg_getline(mpos->start_lnum)
7097 + mpos->start_col,
7098 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007099 }
7100 else
7101 {
7102 struct linepos *lpos = &subs.synt.list.line[i];
7103
7104 if (lpos->start != NULL && lpos->end != NULL)
7105 re_extmatch_out->matches[i] =
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007106 vim_strnsave(lpos->start, lpos->end - lpos->start);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007107 }
7108 }
7109 }
7110#endif
7111
Bram Moolenaar0270f382018-07-17 05:43:58 +02007112 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113}
7114
7115/*
7116 * Match a regexp against a string ("line" points to the string) or multiple
7117 * lines ("line" is NULL, use reg_getline()).
7118 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007119 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007120 */
7121 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007122nfa_regexec_both(
7123 char_u *line,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007124 colnr_T startcol, // column to start looking for match
7125 proftime_T *tm, // timeout limit or NULL
7126 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127{
7128 nfa_regprog_T *prog;
7129 long retval = 0L;
7130 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007131 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132
7133 if (REG_MULTI)
7134 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007135 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007136 line = reg_getline((linenr_T)0); // relative to the cursor
Bram Moolenaar6100d022016-10-02 16:51:57 +02007137 rex.reg_startpos = rex.reg_mmatch->startpos;
7138 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007139 }
7140 else
7141 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007142 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7143 rex.reg_startp = rex.reg_match->startp;
7144 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007145 }
7146
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007147 // Be paranoid...
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007148 if (prog == NULL || line == NULL)
7149 {
Bram Moolenaare83cca22020-09-07 18:53:21 +02007150 iemsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151 goto theend;
7152 }
7153
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007154 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007155 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007156 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007157 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007158 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007159
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007160 // If pattern contains "\Z" overrule value of rex.reg_icombine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007161 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007162 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163
Bram Moolenaar0270f382018-07-17 05:43:58 +02007164 rex.line = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007165 rex.lnum = 0; // relative to line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007166
Bram Moolenaar0270f382018-07-17 05:43:58 +02007167 rex.nfa_has_zend = prog->has_zend;
7168 rex.nfa_has_backref = prog->has_backref;
7169 rex.nfa_nsubexpr = prog->nsubexp;
7170 rex.nfa_listid = 1;
7171 rex.nfa_alt_listid = 2;
7172#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007173 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007174#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007175
Bram Moolenaard89616e2013-06-06 18:46:06 +02007176 if (prog->reganch && col > 0)
7177 return 0L;
7178
Bram Moolenaar0270f382018-07-17 05:43:58 +02007179 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007180#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007181 // Clear the external match subpointers if necessary.
Bram Moolenaar473de612013-06-08 18:19:48 +02007182 if (prog->reghasz == REX_SET)
7183 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007184 rex.nfa_has_zsubexpr = TRUE;
7185 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007186 }
7187 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007188 {
7189 rex.nfa_has_zsubexpr = FALSE;
7190 rex.need_clear_zsubexpr = FALSE;
7191 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007192#endif
7193
Bram Moolenaard89616e2013-06-06 18:46:06 +02007194 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007195 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007196 // Skip ahead until a character we know the match must start with.
7197 // When there is none there is no match.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007198 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007199 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007200
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007201 // If match_text is set it contains the full text that must match.
7202 // Nothing else to try. Doesn't handle combining chars well.
Bram Moolenaara12a1612019-01-24 16:39:02 +01007203 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007204 return find_match_text(col, prog->regstart, prog->match_text);
7205 }
7206
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007207 // If the start column is past the maximum column: no need to try.
Bram Moolenaar6100d022016-10-02 16:51:57 +02007208 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007209 goto theend;
7210
Bram Moolenaar0270f382018-07-17 05:43:58 +02007211 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7212 // it's accidentally used during execution.
7213 nstate = 0;
7214 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007215 {
7216 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007217 prog->state[i].lastlist[0] = 0;
7218 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219 }
7220
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007221 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007222
Bram Moolenaar0270f382018-07-17 05:43:58 +02007223#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007224 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007225#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007226
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007227theend:
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007228 // Make sure the end is never before the start. Can happen when \zs and
7229 // \ze are used.
7230 if (REG_MULTI)
7231 {
7232 lpos_T *start = &rex.reg_mmatch->startpos[0];
7233 lpos_T *end = &rex.reg_mmatch->endpos[0];
7234
7235 if (end->lnum < start->lnum
7236 || (end->lnum == start->lnum && end->col < start->col))
7237 rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
7238 }
7239 else
7240 {
7241 if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
7242 rex.reg_match->endp[0] = rex.reg_match->startp[0];
7243 }
7244
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007245 return retval;
7246}
7247
7248/*
7249 * Compile a regular expression into internal code for the NFA matcher.
7250 * Returns the program in allocated space. Returns NULL for an error.
7251 */
7252 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007253nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007254{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007255 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007256 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007257 int *postfix;
7258
7259 if (expr == NULL)
7260 return NULL;
7261
Bram Moolenaar0270f382018-07-17 05:43:58 +02007262#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007263 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007264#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007265 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007266
7267 init_class_tab();
7268
7269 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7270 return NULL;
7271
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007272 // Build postfix form of the regexp. Needed to build the NFA
7273 // (and count its size).
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007274 postfix = re2post();
7275 if (postfix == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007276 goto fail; // Cascaded (syntax?) error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007277
7278 /*
7279 * In order to build the NFA, we parse the input regexp twice:
7280 * 1. first pass to count size (so we can allocate space)
7281 * 2. second to emit code
7282 */
7283#ifdef ENABLE_LOG
7284 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007285 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007286
7287 if (f != NULL)
7288 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007289 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007290 fclose(f);
7291 }
7292 }
7293#endif
7294
7295 /*
7296 * PASS 1
7297 * Count number of NFA states in "nstate". Do not build the NFA.
7298 */
7299 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007300
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007301 // allocate the regprog with space for the compiled regexp
Bram Moolenaar16619a22013-06-11 18:42:36 +02007302 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007303 prog = alloc(prog_size);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007304 if (prog == NULL)
7305 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007306 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007307 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007308
7309 /*
7310 * PASS 2
7311 * Build the NFA
7312 */
7313 prog->start = post2nfa(postfix, post_ptr, FALSE);
7314 if (prog->start == NULL)
7315 goto fail;
7316
7317 prog->regflags = regflags;
7318 prog->engine = &nfa_regengine;
7319 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007320 prog->has_zend = rex.nfa_has_zend;
7321 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007322 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007323
Bram Moolenaara2947e22013-06-11 22:44:09 +02007324 nfa_postprocess(prog);
7325
Bram Moolenaard89616e2013-06-06 18:46:06 +02007326 prog->reganch = nfa_get_reganch(prog->start, 0);
7327 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007328 prog->match_text = nfa_get_match_text(prog->start);
7329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007330#ifdef ENABLE_LOG
7331 nfa_postfix_dump(expr, OK);
7332 nfa_dump(prog);
7333#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007334#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007335 // Remember whether this pattern has any \z specials in it.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007336 prog->reghasz = re_has_z;
7337#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007338 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007339#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007340 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007341#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007342
7343out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007344 VIM_CLEAR(post_start);
7345 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007346 state_ptr = NULL;
7347 return (regprog_T *)prog;
7348
7349fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007350 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007351#ifdef ENABLE_LOG
7352 nfa_postfix_dump(expr, FAIL);
7353#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007354#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007355 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007356#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007357 goto out;
7358}
7359
Bram Moolenaar473de612013-06-08 18:19:48 +02007360/*
7361 * Free a compiled regexp program, returned by nfa_regcomp().
7362 */
7363 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007364nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007365{
7366 if (prog != NULL)
7367 {
7368 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007369 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007370 vim_free(prog);
7371 }
7372}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007373
7374/*
7375 * Match a regexp against a string.
7376 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7377 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007378 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007379 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007380 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007381 */
7382 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007383nfa_regexec_nl(
7384 regmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007385 char_u *line, // string to match against
7386 colnr_T col, // column to start looking for match
Bram Moolenaar05540972016-01-30 20:31:25 +01007387 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007388{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007389 rex.reg_match = rmp;
7390 rex.reg_mmatch = NULL;
7391 rex.reg_maxline = 0;
7392 rex.reg_line_lbr = line_lbr;
7393 rex.reg_buf = curbuf;
7394 rex.reg_win = NULL;
7395 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007396 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007397 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007398 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007399}
7400
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007401
7402/*
7403 * Match a regexp against multiple lines.
7404 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7405 * Uses curbuf for line count and 'iskeyword'.
7406 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007407 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007408 * match otherwise.
7409 *
7410 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7411 *
7412 * ! Also NOTE : match may actually be in another line. e.g.:
7413 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7414 *
7415 * +-------------------------+
7416 * |a |
7417 * |b |
7418 * |c |
7419 * | |
7420 * +-------------------------+
7421 *
7422 * then nfa_regexec_multi() returns 3. while the original
7423 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7424 *
7425 * FIXME if this behavior is not compatible.
7426 */
7427 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007428nfa_regexec_multi(
7429 regmmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007430 win_T *win, // window in which to search or NULL
7431 buf_T *buf, // buffer in which to search
7432 linenr_T lnum, // nr of line to start looking for match
7433 colnr_T col, // column to start looking for match
7434 proftime_T *tm, // timeout limit or NULL
7435 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007436{
Bram Moolenaarf4140482020-02-15 23:06:45 +01007437 init_regexec_multi(rmp, win, buf, lnum);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007438 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007439}
7440
7441#ifdef DEBUG
7442# undef ENABLE_LOG
7443#endif