blob: a288ea47c84036ee769ae08c0c7f9d23087dab5c [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 Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar0270f382018-07-17 05:43:58 +0200250// Variables only used in nfa_regcomp() and descendants.
251static int nfa_re_flags; // re_flags passed to nfa_regcomp()
252static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200253static int *post_end;
254static int *post_ptr;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200255static int nstate; // Number of states in the NFA.
256static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200257
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100258// If not NULL match must end at this position
Bram Moolenaar307aa162013-06-02 16:34:21 +0200259static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200260
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100261// 0 for first call to nfa_regmatch(), 1 for recursive call.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200262static int nfa_ll_index = 0;
263
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100264static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100265static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100267static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100269static int match_follows(nfa_state_T *startstate, int depth);
270static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100272// helper functions used when doing re2post() ... regatom() parsing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275 return FAIL; \
276 *post_ptr++ = c; \
277 } while (0)
278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200279/*
280 * Initialize internal variables before NFA compilation.
281 * Return OK on success, FAIL otherwise.
282 */
283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100284nfa_regcomp_start(
285 char_u *expr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100286 int re_flags) // see vim_regcomp()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200288 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200289 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200290
291 nstate = 0;
292 istate = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100293 // A reasonable estimation for maximum size
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200294 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200295
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100296 // Some items blow up in size, such as [A-z]. Add more space for that.
297 // When it is still not enough realloc_post_list() will be used.
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200298 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200299
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100300 // Size for postfix representation of expr.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200301 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200302
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200303 post_start = alloc(postfix_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304 if (post_start == NULL)
305 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200307 post_end = post_start + nstate_max;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200308 rex.nfa_has_zend = FALSE;
309 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200310
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100311 // shared with BT engine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312 regcomp_start(expr, re_flags);
313
314 return OK;
315}
316
317/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200318 * Figure out if the NFA state list starts with an anchor, must match at start
319 * of the line.
320 */
321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200323{
324 nfa_state_T *p = start;
325
326 if (depth > 4)
327 return 0;
328
329 while (p != NULL)
330 {
331 switch (p->c)
332 {
333 case NFA_BOL:
334 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100335 return 1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200336
337 case NFA_ZSTART:
338 case NFA_ZEND:
339 case NFA_CURSOR:
340 case NFA_VISUAL:
341
342 case NFA_MOPEN:
343 case NFA_MOPEN1:
344 case NFA_MOPEN2:
345 case NFA_MOPEN3:
346 case NFA_MOPEN4:
347 case NFA_MOPEN5:
348 case NFA_MOPEN6:
349 case NFA_MOPEN7:
350 case NFA_MOPEN8:
351 case NFA_MOPEN9:
352 case NFA_NOPEN:
353#ifdef FEAT_SYN_HL
354 case NFA_ZOPEN:
355 case NFA_ZOPEN1:
356 case NFA_ZOPEN2:
357 case NFA_ZOPEN3:
358 case NFA_ZOPEN4:
359 case NFA_ZOPEN5:
360 case NFA_ZOPEN6:
361 case NFA_ZOPEN7:
362 case NFA_ZOPEN8:
363 case NFA_ZOPEN9:
364#endif
365 p = p->out;
366 break;
367
368 case NFA_SPLIT:
369 return nfa_get_reganch(p->out, depth + 1)
370 && nfa_get_reganch(p->out1, depth + 1);
371
372 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100373 return 0; // noooo
Bram Moolenaard89616e2013-06-06 18:46:06 +0200374 }
375 }
376 return 0;
377}
378
379/*
380 * Figure out if the NFA state list starts with a character which must match
381 * at start of the match.
382 */
383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100384nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200385{
386 nfa_state_T *p = start;
387
388 if (depth > 4)
389 return 0;
390
391 while (p != NULL)
392 {
393 switch (p->c)
394 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100395 // all kinds of zero-width matches
Bram Moolenaard89616e2013-06-06 18:46:06 +0200396 case NFA_BOL:
397 case NFA_BOF:
398 case NFA_BOW:
399 case NFA_EOW:
400 case NFA_ZSTART:
401 case NFA_ZEND:
402 case NFA_CURSOR:
403 case NFA_VISUAL:
404 case NFA_LNUM:
405 case NFA_LNUM_GT:
406 case NFA_LNUM_LT:
407 case NFA_COL:
408 case NFA_COL_GT:
409 case NFA_COL_LT:
410 case NFA_VCOL:
411 case NFA_VCOL_GT:
412 case NFA_VCOL_LT:
413 case NFA_MARK:
414 case NFA_MARK_GT:
415 case NFA_MARK_LT:
416
417 case NFA_MOPEN:
418 case NFA_MOPEN1:
419 case NFA_MOPEN2:
420 case NFA_MOPEN3:
421 case NFA_MOPEN4:
422 case NFA_MOPEN5:
423 case NFA_MOPEN6:
424 case NFA_MOPEN7:
425 case NFA_MOPEN8:
426 case NFA_MOPEN9:
427 case NFA_NOPEN:
428#ifdef FEAT_SYN_HL
429 case NFA_ZOPEN:
430 case NFA_ZOPEN1:
431 case NFA_ZOPEN2:
432 case NFA_ZOPEN3:
433 case NFA_ZOPEN4:
434 case NFA_ZOPEN5:
435 case NFA_ZOPEN6:
436 case NFA_ZOPEN7:
437 case NFA_ZOPEN8:
438 case NFA_ZOPEN9:
439#endif
440 p = p->out;
441 break;
442
443 case NFA_SPLIT:
444 {
445 int c1 = nfa_get_regstart(p->out, depth + 1);
446 int c2 = nfa_get_regstart(p->out1, depth + 1);
447
448 if (c1 == c2)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100449 return c1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200450 return 0;
451 }
452
453 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200454 if (p->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100455 return p->c; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200456 return 0;
457 }
458 }
459 return 0;
460}
461
462/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200463 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200464 * else. If so return a string in allocated memory with what must match after
465 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200466 */
467 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100468nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200469{
470 nfa_state_T *p = start;
471 int len = 0;
472 char_u *ret;
473 char_u *s;
474
475 if (p->c != NFA_MOPEN)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100476 return NULL; // just in case
Bram Moolenaar473de612013-06-08 18:19:48 +0200477 p = p->out;
478 while (p->c > 0)
479 {
480 len += MB_CHAR2LEN(p->c);
481 p = p->out;
482 }
483 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
484 return NULL;
485
486 ret = alloc(len);
487 if (ret != NULL)
488 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100489 p = start->out->out; // skip first char, it goes into regstart
Bram Moolenaar473de612013-06-08 18:19:48 +0200490 s = ret;
491 while (p->c > 0)
492 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200493 if (has_mbyte)
494 s += (*mb_char2bytes)(p->c, s);
495 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200496 *s++ = p->c;
497 p = p->out;
498 }
499 *s = NUL;
500 }
501 return ret;
502}
503
504/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200505 * Allocate more space for post_start. Called when
506 * running above the estimated number of states.
507 */
508 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100509realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200510{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200511 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100512 int new_max;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200513 int *new_start;
514 int *old_start;
515
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100516 // For weird patterns the number of states can be very high. Increasing by
517 // 50% seems a reasonable compromise between memory use and speed.
518 new_max = nstate_max * 3 / 2;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200519 new_start = ALLOC_MULT(int, new_max);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200520 if (new_start == NULL)
521 return FAIL;
522 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200523 old_start = post_start;
524 post_start = new_start;
525 post_ptr = new_start + (post_ptr - old_start);
526 post_end = post_start + new_max;
527 vim_free(old_start);
528 return OK;
529}
530
531/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200532 * Search between "start" and "end" and try to recognize a
533 * character class in expanded form. For example [0-9].
534 * On success, return the id the character class to be emitted.
535 * On failure, return 0 (=FAIL)
536 * Start points to the first char of the range, while end should point
537 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200538 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
539 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540 */
541 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100542nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200543{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200544# define CLASS_not 0x80
545# define CLASS_af 0x40
546# define CLASS_AF 0x20
547# define CLASS_az 0x10
548# define CLASS_AZ 0x08
549# define CLASS_o7 0x04
550# define CLASS_o9 0x02
551# define CLASS_underscore 0x01
552
553 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200554 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200555 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200556
557 if (extra_newl == TRUE)
558 newl = TRUE;
559
560 if (*end != ']')
561 return FAIL;
562 p = start;
563 if (*p == '^')
564 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200565 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200566 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200567 }
568
569 while (p < end)
570 {
571 if (p + 2 < end && *(p + 1) == '-')
572 {
573 switch (*p)
574 {
575 case '0':
576 if (*(p + 2) == '9')
577 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200578 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200579 break;
580 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200581 if (*(p + 2) == '7')
582 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200583 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200584 break;
585 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200586 return FAIL;
587
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200588 case 'a':
589 if (*(p + 2) == 'z')
590 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200591 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 break;
593 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200594 if (*(p + 2) == 'f')
595 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200596 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200597 break;
598 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200599 return FAIL;
600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200601 case 'A':
602 if (*(p + 2) == 'Z')
603 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200604 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200605 break;
606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 if (*(p + 2) == 'F')
608 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200609 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 break;
611 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200612 return FAIL;
613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200614 default:
615 return FAIL;
616 }
617 p += 3;
618 }
619 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
620 {
621 newl = TRUE;
622 p += 2;
623 }
624 else if (*p == '_')
625 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200626 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200627 p ++;
628 }
629 else if (*p == '\n')
630 {
631 newl = TRUE;
632 p ++;
633 }
634 else
635 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100636 } // while (p < end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200637
638 if (p != end)
639 return FAIL;
640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200642 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643
644 switch (config)
645 {
646 case CLASS_o9:
647 return extra_newl + NFA_DIGIT;
648 case CLASS_not | CLASS_o9:
649 return extra_newl + NFA_NDIGIT;
650 case CLASS_af | CLASS_AF | CLASS_o9:
651 return extra_newl + NFA_HEX;
652 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
653 return extra_newl + NFA_NHEX;
654 case CLASS_o7:
655 return extra_newl + NFA_OCTAL;
656 case CLASS_not | CLASS_o7:
657 return extra_newl + NFA_NOCTAL;
658 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
659 return extra_newl + NFA_WORD;
660 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
661 return extra_newl + NFA_NWORD;
662 case CLASS_az | CLASS_AZ | CLASS_underscore:
663 return extra_newl + NFA_HEAD;
664 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
665 return extra_newl + NFA_NHEAD;
666 case CLASS_az | CLASS_AZ:
667 return extra_newl + NFA_ALPHA;
668 case CLASS_not | CLASS_az | CLASS_AZ:
669 return extra_newl + NFA_NALPHA;
670 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200671 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200672 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200673 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200674 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200675 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200677 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200678 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200679 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680}
681
682/*
683 * Produce the bytes for equivalence class "c".
684 * Currently only handles latin1, latin9 and utf-8.
685 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
686 * equivalent to 'a OR b OR c'
687 *
688 * NOTE! When changing this function, also update reg_equi_class()
689 */
690 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100691nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200693#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100694#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200696 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
697 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200698 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200699#ifdef EBCDIC
700# define A_circumflex 0x62
701# define A_diaeresis 0x63
702# define A_grave 0x64
703# define A_acute 0x65
704# define A_virguilla 0x66
705# define A_ring 0x67
706# define C_cedilla 0x68
707# define E_acute 0x71
708# define E_circumflex 0x72
709# define E_diaeresis 0x73
710# define E_grave 0x74
711# define I_acute 0x75
712# define I_circumflex 0x76
713# define I_diaeresis 0x77
714# define I_grave 0x78
715# define N_virguilla 0x69
716# define O_circumflex 0xeb
717# define O_diaeresis 0xec
718# define O_grave 0xed
719# define O_acute 0xee
720# define O_virguilla 0xef
721# define O_slash 0x80
722# define U_circumflex 0xfb
723# define U_diaeresis 0xfc
724# define U_grave 0xfd
725# define U_acute 0xfe
726# define Y_acute 0xba
727# define a_grave 0x42
728# define a_acute 0x43
729# define a_circumflex 0x44
730# define a_virguilla 0x45
731# define a_diaeresis 0x46
732# define a_ring 0x47
733# define c_cedilla 0x48
734# define e_grave 0x51
735# define e_acute 0x52
736# define e_circumflex 0x53
737# define e_diaeresis 0x54
738# define i_grave 0x55
739# define i_acute 0x56
740# define i_circumflex 0x57
741# define i_diaeresis 0x58
742# define n_virguilla 0x49
743# define o_grave 0xcb
744# define o_acute 0xcc
745# define o_circumflex 0xcd
746# define o_virguilla 0xce
747# define o_diaeresis 0xcf
748# define o_slash 0x70
749# define u_grave 0xdb
750# define u_acute 0xdc
751# define u_circumflex 0xdd
752# define u_diaeresis 0xde
753# define y_acute 0x8d
754# define y_diaeresis 0xdf
755#else
756# define A_grave 0xc0
757# define A_acute 0xc1
758# define A_circumflex 0xc2
759# define A_virguilla 0xc3
760# define A_diaeresis 0xc4
761# define A_ring 0xc5
762# define C_cedilla 0xc7
763# define E_grave 0xc8
764# define E_acute 0xc9
765# define E_circumflex 0xca
766# define E_diaeresis 0xcb
767# define I_grave 0xcc
768# define I_acute 0xcd
769# define I_circumflex 0xce
770# define I_diaeresis 0xcf
771# define N_virguilla 0xd1
772# define O_grave 0xd2
773# define O_acute 0xd3
774# define O_circumflex 0xd4
775# define O_virguilla 0xd5
776# define O_diaeresis 0xd6
777# define O_slash 0xd8
778# define U_grave 0xd9
779# define U_acute 0xda
780# define U_circumflex 0xdb
781# define U_diaeresis 0xdc
782# define Y_acute 0xdd
783# define a_grave 0xe0
784# define a_acute 0xe1
785# define a_circumflex 0xe2
786# define a_virguilla 0xe3
787# define a_diaeresis 0xe4
788# define a_ring 0xe5
789# define c_cedilla 0xe7
790# define e_grave 0xe8
791# define e_acute 0xe9
792# define e_circumflex 0xea
793# define e_diaeresis 0xeb
794# define i_grave 0xec
795# define i_acute 0xed
796# define i_circumflex 0xee
797# define i_diaeresis 0xef
798# define n_virguilla 0xf1
799# define o_grave 0xf2
800# define o_acute 0xf3
801# define o_circumflex 0xf4
802# define o_virguilla 0xf5
803# define o_diaeresis 0xf6
804# define o_slash 0xf8
805# define u_grave 0xf9
806# define u_acute 0xfa
807# define u_circumflex 0xfb
808# define u_diaeresis 0xfc
809# define y_acute 0xfd
810# define y_diaeresis 0xff
811#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 switch (c)
813 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200814 case 'A': case A_grave: case A_acute: case A_circumflex:
815 case A_virguilla: case A_diaeresis: case A_ring:
816 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
817 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
818 CASEMBC(0x1ea2)
819 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
820 EMIT2(A_circumflex); EMIT2(A_virguilla);
821 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200822 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
823 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
824 EMITMBC(0x1ea2)
825 return OK;
826
827 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
828 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200829 return OK;
830
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200831 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
832 CASEMBC(0x10a) CASEMBC(0x10c)
833 EMIT2('C'); EMIT2(C_cedilla);
834 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200835 EMITMBC(0x10a) EMITMBC(0x10c)
836 return OK;
837
838 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200839 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200840 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
841 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200842 return OK;
843
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200844 case 'E': case E_grave: case E_acute: case E_circumflex:
845 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
846 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
847 CASEMBC(0x1eba) CASEMBC(0x1ebc)
848 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
849 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200850 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
851 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
852 EMITMBC(0x1ebc)
853 return OK;
854
855 case 'F': CASEMBC(0x1e1e)
856 EMIT2('F'); EMITMBC(0x1e1e)
857 return OK;
858
859 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200860 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
861 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200862 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
863 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
864 EMITMBC(0x1f4) EMITMBC(0x1e20)
865 return OK;
866
867 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200868 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200869 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
870 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200871 return OK;
872
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200873 case 'I': case I_grave: case I_acute: case I_circumflex:
874 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
875 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
876 CASEMBC(0x1cf) CASEMBC(0x1ec8)
877 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
878 EMIT2(I_circumflex); EMIT2(I_diaeresis);
879 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200880 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
881 EMITMBC(0x1cf) EMITMBC(0x1ec8)
882 return OK;
883
884 case 'J': CASEMBC(0x134)
885 EMIT2('J'); EMITMBC(0x134)
886 return OK;
887
888 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200889 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200890 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
891 EMITMBC(0x1e34)
892 return OK;
893
894 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
897 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
898 return OK;
899
900 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
901 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200902 return OK;
903
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200904 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
905 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
906 EMIT2('N'); EMIT2(N_virguilla);
907 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200908 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909 return OK;
910
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200911 case 'O': case O_grave: case O_acute: case O_circumflex:
912 case O_virguilla: case O_diaeresis: case O_slash:
913 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
914 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
915 CASEMBC(0x1ec) CASEMBC(0x1ece)
916 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
917 EMIT2(O_circumflex); EMIT2(O_virguilla);
918 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200919 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
920 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
921 EMITMBC(0x1ec) EMITMBC(0x1ece)
922 return OK;
923
924 case 'P': case 0x1e54: case 0x1e56:
925 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
926 return OK;
927
928 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200930 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
931 EMITMBC(0x1e58) EMITMBC(0x1e5e)
932 return OK;
933
934 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200935 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
937 EMITMBC(0x160) EMITMBC(0x1e60)
938 return OK;
939
940 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200941 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200942 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
943 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200944 return OK;
945
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200946 case 'U': case U_grave: case U_acute: case U_diaeresis:
947 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
948 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
949 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
950 CASEMBC(0x1ee6)
951 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
952 EMIT2(U_diaeresis); EMIT2(U_circumflex);
953 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200954 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
955 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
956 EMITMBC(0x1ee6)
957 return OK;
958
959 case 'V': CASEMBC(0x1e7c)
960 EMIT2('V'); EMITMBC(0x1e7c)
961 return OK;
962
963 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200964 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200965 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
966 EMITMBC(0x1e84) EMITMBC(0x1e86)
967 return OK;
968
969 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
970 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200971 return OK;
972
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200973 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
974 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
975 CASEMBC(0x1ef8)
976 EMIT2('Y'); EMIT2(Y_acute);
977 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200978 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
979 EMITMBC(0x1ef8)
980 return OK;
981
982 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200983 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200984 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
985 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200986 return OK;
987
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200988 case 'a': case a_grave: case a_acute: case a_circumflex:
989 case a_virguilla: case a_diaeresis: case a_ring:
990 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
991 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
992 CASEMBC(0x1ea3)
993 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
994 EMIT2(a_circumflex); EMIT2(a_virguilla);
995 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200996 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
997 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
998 EMITMBC(0x1ea3)
999 return OK;
1000
1001 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1002 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 return OK;
1004
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001005 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1006 CASEMBC(0x10b) CASEMBC(0x10d)
1007 EMIT2('c'); EMIT2(c_cedilla);
1008 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001009 EMITMBC(0x10b) EMITMBC(0x10d)
1010 return OK;
1011
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001012 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001013 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001014 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1015 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001016 return OK;
1017
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001018 case 'e': case e_grave: case e_acute: case e_circumflex:
1019 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1020 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1021 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1022 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1023 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1024 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001025 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1026 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1027 return OK;
1028
1029 case 'f': CASEMBC(0x1e1f)
1030 EMIT2('f'); EMITMBC(0x1e1f)
1031 return OK;
1032
1033 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001034 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1035 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001036 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1037 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1038 EMITMBC(0x1f5) EMITMBC(0x1e21)
1039 return OK;
1040
1041 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001042 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001043 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1044 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001045 return OK;
1046
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001047 case 'i': case i_grave: case i_acute: case i_circumflex:
1048 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1049 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1050 CASEMBC(0x1ec9)
1051 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1052 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1053 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001054 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1055 EMITMBC(0x1ec9)
1056 return OK;
1057
1058 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1059 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1060 return OK;
1061
1062 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001063 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001064 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1065 EMITMBC(0x1e35)
1066 return OK;
1067
1068 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001070 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1071 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1072 return OK;
1073
1074 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1075 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001076 return OK;
1077
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001078 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1079 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1080 CASEMBC(0x1e49)
1081 EMIT2('n'); EMIT2(n_virguilla);
1082 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001083 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1084 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001085 return OK;
1086
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001087 case 'o': case o_grave: case o_acute: case o_circumflex:
1088 case o_virguilla: case o_diaeresis: case o_slash:
1089 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1090 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1091 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1092 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1093 EMIT2(o_circumflex); EMIT2(o_virguilla);
1094 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001095 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1096 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1097 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1098 return OK;
1099
1100 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1101 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1102 return OK;
1103
1104 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001105 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001106 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1107 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1108 return OK;
1109
1110 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001111 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001112 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1113 EMITMBC(0x161) EMITMBC(0x1e61)
1114 return OK;
1115
1116 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001117 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001118 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1119 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120 return OK;
1121
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001122 case 'u': case u_grave: case u_acute: case u_circumflex:
1123 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1124 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1125 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1126 CASEMBC(0x1ee7)
1127 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1128 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1129 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001130 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1131 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1132 EMITMBC(0x1ee7)
1133 return OK;
1134
1135 case 'v': CASEMBC(0x1e7d)
1136 EMIT2('v'); EMITMBC(0x1e7d)
1137 return OK;
1138
1139 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001140 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001141 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1142 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1143 return OK;
1144
1145 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1146 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001147 return OK;
1148
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001149 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1150 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1151 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1152 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1153 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001154 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1155 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001156 return OK;
1157
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001158 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001159 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001160 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1161 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1162 return OK;
1163
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001164 // default: character itself
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001165 }
1166 }
1167
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169 return OK;
1170#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001171#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001172}
1173
1174/*
1175 * Code to parse regular expression.
1176 *
1177 * We try to reuse parsing functions in regexp.c to
1178 * minimize surprise and keep the syntax consistent.
1179 */
1180
1181/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001182 * Parse the lowest level.
1183 *
1184 * An atom can be one of a long list of items. Many atoms match one character
1185 * in the text. It is often an ordinary character or a character class.
1186 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1187 * is only for syntax highlighting.
1188 *
1189 * atom ::= ordinary-atom
1190 * or \( pattern \)
1191 * or \%( pattern \)
1192 * or \z( pattern \)
1193 */
1194 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001195nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001196{
1197 int c;
1198 int charclass;
1199 int equiclass;
1200 int collclass;
1201 int got_coll_char;
1202 char_u *p;
1203 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001204 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001205 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001206 int emit_range;
1207 int negated;
1208 int result;
1209 int startc = -1;
1210 int endc = -1;
1211 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001212 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001215 switch (c)
1216 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001217 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001218 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001220 case Magic('^'):
1221 EMIT(NFA_BOL);
1222 break;
1223
1224 case Magic('$'):
1225 EMIT(NFA_EOL);
1226#if defined(FEAT_SYN_HL) || defined(PROTO)
1227 had_eol = TRUE;
1228#endif
1229 break;
1230
1231 case Magic('<'):
1232 EMIT(NFA_BOW);
1233 break;
1234
1235 case Magic('>'):
1236 EMIT(NFA_EOW);
1237 break;
1238
1239 case Magic('_'):
1240 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001241 if (c == NUL)
1242 EMSG_RET_FAIL(_(e_nul_found));
1243
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001244 if (c == '^') // "\_^" is start-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245 {
1246 EMIT(NFA_BOL);
1247 break;
1248 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001249 if (c == '$') // "\_$" is end-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250 {
1251 EMIT(NFA_EOL);
1252#if defined(FEAT_SYN_HL) || defined(PROTO)
1253 had_eol = TRUE;
1254#endif
1255 break;
1256 }
1257
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001258 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001259
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001260 // "\_[" is collection plus newline
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001261 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001262 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001263
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001264 // "\_x" is character class plus newline
1265 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001266
1267 /*
1268 * Character classes.
1269 */
1270 case Magic('.'):
1271 case Magic('i'):
1272 case Magic('I'):
1273 case Magic('k'):
1274 case Magic('K'):
1275 case Magic('f'):
1276 case Magic('F'):
1277 case Magic('p'):
1278 case Magic('P'):
1279 case Magic('s'):
1280 case Magic('S'):
1281 case Magic('d'):
1282 case Magic('D'):
1283 case Magic('x'):
1284 case Magic('X'):
1285 case Magic('o'):
1286 case Magic('O'):
1287 case Magic('w'):
1288 case Magic('W'):
1289 case Magic('h'):
1290 case Magic('H'):
1291 case Magic('a'):
1292 case Magic('A'):
1293 case Magic('l'):
1294 case Magic('L'):
1295 case Magic('u'):
1296 case Magic('U'):
1297 p = vim_strchr(classchars, no_Magic(c));
1298 if (p == NULL)
1299 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001300 if (extra == NFA_ADD_NL)
1301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001302 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001303 rc_did_emsg = TRUE;
1304 return FAIL;
1305 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001306 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001307 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001308 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001309
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001310 // When '.' is followed by a composing char ignore the dot, so that
1311 // the composing char is matched here.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001312 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1313 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001314 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 c = getchr();
1316 goto nfa_do_multibyte;
1317 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001319 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 {
1321 EMIT(NFA_NEWL);
1322 EMIT(NFA_OR);
1323 regflags |= RF_HASNL;
1324 }
1325 break;
1326
1327 case Magic('n'):
1328 if (reg_string)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001329 // In a string "\n" matches a newline character.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001330 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 else
1332 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001333 // In buffer text "\n" matches the end of a line.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001334 EMIT(NFA_NEWL);
1335 regflags |= RF_HASNL;
1336 }
1337 break;
1338
1339 case Magic('('):
1340 if (nfa_reg(REG_PAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001341 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342 break;
1343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001344 case Magic('|'):
1345 case Magic('&'):
1346 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001347 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 return FAIL;
1349
1350 case Magic('='):
1351 case Magic('?'):
1352 case Magic('+'):
1353 case Magic('@'):
1354 case Magic('*'):
1355 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001356 // these should follow an atom, not form an atom
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 return FAIL;
1359
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001360 case Magic('~'):
1361 {
1362 char_u *lp;
1363
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 // Previous substitute pattern.
1365 // Generated as "\%(pattern\)".
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001366 if (reg_prev_sub == NULL)
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001369 return FAIL;
1370 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001371 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001372 {
1373 EMIT(PTR2CHAR(lp));
1374 if (lp != reg_prev_sub)
1375 EMIT(NFA_CONCAT);
1376 }
1377 EMIT(NFA_NOPEN);
1378 break;
1379 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001380
Bram Moolenaar428e9872013-05-30 17:05:39 +02001381 case Magic('1'):
1382 case Magic('2'):
1383 case Magic('3'):
1384 case Magic('4'):
1385 case Magic('5'):
1386 case Magic('6'):
1387 case Magic('7'):
1388 case Magic('8'):
1389 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001390 {
1391 int refnum = no_Magic(c) - '1';
1392
1393 if (!seen_endbrace(refnum + 1))
1394 return FAIL;
1395 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001396 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001397 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001398 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399
1400 case Magic('z'):
1401 c = no_Magic(getchr());
1402 switch (c)
1403 {
1404 case 's':
1405 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001406 if (re_mult_next("\\zs") == FAIL)
1407 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408 break;
1409 case 'e':
1410 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001411 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001412 if (re_mult_next("\\ze") == FAIL)
1413 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001414 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001415#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 case '1':
1417 case '2':
1418 case '3':
1419 case '4':
1420 case '5':
1421 case '6':
1422 case '7':
1423 case '8':
1424 case '9':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001425 // \z1...\z9
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001426 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001427 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001428 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001429 // No need to set rex.nfa_has_backref, the sub-matches don't
1430 // change when \z1 .. \z9 matches or not.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001431 re_has_z = REX_USE;
1432 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001433 case '(':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001434 // \z(
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001435 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001436 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001437 if (nfa_reg(REG_ZPAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001438 return FAIL; // cascaded error
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001439 re_has_z = REX_SET;
1440 break;
1441#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001442 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001443 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 no_Magic(c));
1445 return FAIL;
1446 }
1447 break;
1448
1449 case Magic('%'):
1450 c = no_Magic(getchr());
1451 switch (c)
1452 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001453 // () without a back reference
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001454 case '(':
1455 if (nfa_reg(REG_NPAREN) == FAIL)
1456 return FAIL;
1457 EMIT(NFA_NOPEN);
1458 break;
1459
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001460 case 'd': // %d123 decimal
1461 case 'o': // %o123 octal
1462 case 'x': // %xab hex 2
1463 case 'u': // %uabcd hex 4
1464 case 'U': // %U1234abcd hex 8
Bram Moolenaar47196582013-05-25 22:04:23 +02001465 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001466 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467
Bram Moolenaar47196582013-05-25 22:04:23 +02001468 switch (c)
1469 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001470 case 'd': nr = getdecchrs(); break;
1471 case 'o': nr = getoctchrs(); break;
1472 case 'x': nr = gethexchrs(2); break;
1473 case 'u': nr = gethexchrs(4); break;
1474 case 'U': nr = gethexchrs(8); break;
1475 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001476 }
1477
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001478 if (nr < 0 || nr > INT_MAX)
Bram Moolenaar47196582013-05-25 22:04:23 +02001479 EMSG2_RET_FAIL(
1480 _("E678: Invalid character after %s%%[dxouU]"),
1481 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001482 // A NUL is stored in the text as NL
1483 // TODO: what if a composing character follows?
Bram Moolenaar595cad22013-09-22 13:57:24 +02001484 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001485 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001486 break;
1487
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001488 // Catch \%^ and \%$ regardless of where they appear in the
1489 // pattern -- regardless of whether or not it makes sense.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001490 case '^':
1491 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 break;
1493
1494 case '$':
1495 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 break;
1497
1498 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001499 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 break;
1501
1502 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001503 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 break;
1505
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001506 case 'C':
1507 EMIT(NFA_ANY_COMPOSING);
1508 break;
1509
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001510 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001511 {
1512 int n;
1513
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001514 // \%[abc]
Bram Moolenaard7986252013-06-17 21:33:41 +02001515 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001516 {
1517 if (c == NUL)
1518 EMSG2_RET_FAIL(_(e_missing_sb),
1519 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001520 // recursive call!
Bram Moolenaard7986252013-06-17 21:33:41 +02001521 if (nfa_regatom() == FAIL)
1522 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001523 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001524 getchr(); // get the ]
Bram Moolenaar2976c022013-06-05 21:30:37 +02001525 if (n == 0)
1526 EMSG2_RET_FAIL(_(e_empty_sb),
1527 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001528 EMIT(NFA_OPT_CHARS);
1529 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001530
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001531 // Emit as "\%(\%[abc]\)" to be able to handle
1532 // "\%[abc]*" which would cause the empty string to be
1533 // matched an unlimited number of times. NFA_NOPEN is
1534 // added only once at a position, while NFA_SPLIT is
1535 // added multiple times. This is more efficient than
1536 // not allowing NFA_SPLIT multiple times, it is used
1537 // a lot.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001538 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001539 break;
1540 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001541
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001543 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001544 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001545 int cmp = c;
1546
1547 if (c == '<' || c == '>')
1548 c = getchr();
1549 while (VIM_ISDIGIT(c))
1550 {
1551 n = n * 10 + (c - '0');
1552 c = getchr();
1553 }
1554 if (c == 'l' || c == 'c' || c == 'v')
1555 {
Bram Moolenaar9403a212019-02-13 18:35:06 +01001556 int limit = INT_MAX;
1557
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001559 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001560 // \%{n}l \%{n}<l \%{n}>l
Bram Moolenaar423532e2013-05-29 21:14:42 +02001561 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001562 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001563 if (save_prev_at_start)
1564 at_start = TRUE;
1565 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001566 else if (c == 'c')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001567 // \%{n}c \%{n}<c \%{n}>c
Bram Moolenaar423532e2013-05-29 21:14:42 +02001568 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001569 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001570 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001571 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001572 // \%{n}v \%{n}<v \%{n}>v
Bram Moolenaar423532e2013-05-29 21:14:42 +02001573 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001574 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001575 limit = INT_MAX / MB_MAXBYTES;
1576 }
1577 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001579 emsg(_("E951: \\% value too large"));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001580 return FAIL;
1581 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001582 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001583 break;
1584 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001585 else if (c == '\'' && n == 0)
1586 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001587 // \%'m \%<'m \%>'m
Bram Moolenaar044aa292013-06-04 21:27:38 +02001588 EMIT(cmp == '<' ? NFA_MARK_LT :
1589 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001590 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001591 break;
1592 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001593 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001595 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596 return FAIL;
1597 }
1598 break;
1599
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001600 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001601collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001602 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001603 * [abc] uses NFA_START_COLL - NFA_END_COLL
1604 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1605 * Each character is produced as a regular state, using
1606 * NFA_CONCAT to bind them together.
1607 * Besides normal characters there can be:
1608 * - character classes NFA_CLASS_*
1609 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 */
1611
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 p = regparse;
1613 endp = skip_anyof(p);
1614 if (*endp == ']')
1615 {
1616 /*
1617 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001618 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 * and perform the necessary substitutions in the NFA.
1620 */
1621 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001622 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 if (result != FAIL)
1624 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001625 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001627 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 EMIT(NFA_NEWL);
1629 EMIT(NFA_OR);
1630 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001631 else
1632 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001633 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001634 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 return OK;
1636 }
1637 /*
1638 * Failed to recognize a character class. Use the simple
1639 * version that turns [abc] into 'a' OR 'b' OR 'c'
1640 */
1641 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 negated = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001643 if (*regparse == '^') // negated range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 {
1645 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001646 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001647 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001648 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001649 else
1650 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 if (*regparse == '-')
1652 {
1653 startc = '-';
1654 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001655 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001656 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001658 // Emit the OR branches for each character in the []
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 emit_range = FALSE;
1660 while (regparse < endp)
1661 {
1662 oldstartc = startc;
1663 startc = -1;
1664 got_coll_char = FALSE;
1665 if (*regparse == '[')
1666 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001667 // Check for [: :], [= =], [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 equiclass = collclass = 0;
1669 charclass = get_char_class(&regparse);
1670 if (charclass == CLASS_NONE)
1671 {
1672 equiclass = get_equi_class(&regparse);
1673 if (equiclass == 0)
1674 collclass = get_coll_element(&regparse);
1675 }
1676
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001677 // Character class like [:alpha:]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 if (charclass != CLASS_NONE)
1679 {
1680 switch (charclass)
1681 {
1682 case CLASS_ALNUM:
1683 EMIT(NFA_CLASS_ALNUM);
1684 break;
1685 case CLASS_ALPHA:
1686 EMIT(NFA_CLASS_ALPHA);
1687 break;
1688 case CLASS_BLANK:
1689 EMIT(NFA_CLASS_BLANK);
1690 break;
1691 case CLASS_CNTRL:
1692 EMIT(NFA_CLASS_CNTRL);
1693 break;
1694 case CLASS_DIGIT:
1695 EMIT(NFA_CLASS_DIGIT);
1696 break;
1697 case CLASS_GRAPH:
1698 EMIT(NFA_CLASS_GRAPH);
1699 break;
1700 case CLASS_LOWER:
1701 EMIT(NFA_CLASS_LOWER);
1702 break;
1703 case CLASS_PRINT:
1704 EMIT(NFA_CLASS_PRINT);
1705 break;
1706 case CLASS_PUNCT:
1707 EMIT(NFA_CLASS_PUNCT);
1708 break;
1709 case CLASS_SPACE:
1710 EMIT(NFA_CLASS_SPACE);
1711 break;
1712 case CLASS_UPPER:
1713 EMIT(NFA_CLASS_UPPER);
1714 break;
1715 case CLASS_XDIGIT:
1716 EMIT(NFA_CLASS_XDIGIT);
1717 break;
1718 case CLASS_TAB:
1719 EMIT(NFA_CLASS_TAB);
1720 break;
1721 case CLASS_RETURN:
1722 EMIT(NFA_CLASS_RETURN);
1723 break;
1724 case CLASS_BACKSPACE:
1725 EMIT(NFA_CLASS_BACKSPACE);
1726 break;
1727 case CLASS_ESCAPE:
1728 EMIT(NFA_CLASS_ESCAPE);
1729 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001730 case CLASS_IDENT:
1731 EMIT(NFA_CLASS_IDENT);
1732 break;
1733 case CLASS_KEYWORD:
1734 EMIT(NFA_CLASS_KEYWORD);
1735 break;
1736 case CLASS_FNAME:
1737 EMIT(NFA_CLASS_FNAME);
1738 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001739 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001740 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741 continue;
1742 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001743 // Try equivalence class [=a=] and the like
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001744 if (equiclass != 0)
1745 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001746 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 if (result == FAIL)
1748 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001749 // should never happen
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001750 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001752 continue;
1753 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001754 // Try collating class like [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001755 if (collclass != 0)
1756 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001757 startc = collclass; // allow [.a.]-x as a range
1758 // Will emit the proper atom at the end of the
1759 // while loop.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001760 }
1761 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001762 // Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1763 // start character.
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001764 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 {
1766 emit_range = TRUE;
1767 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001768 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001769 continue; // reading the end of the range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001770 }
1771
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001772 // Now handle simple and escaped characters.
1773 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1774 // accepts "\t", "\e", etc., but only when the 'l' flag in
1775 // 'cpoptions' is not included.
1776 // Posix doesn't recognize backslash at all.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001778 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 && regparse + 1 <= endp
1780 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001781 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001782 && vim_strchr(REGEXP_ABBR, regparse[1])
1783 != NULL)
1784 )
1785 )
1786 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001787 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001789 if (*regparse == 'n')
Bram Moolenaara5483442019-02-17 20:17:02 +01001790 startc = (reg_string || emit_range
1791 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001792 else if (*regparse == 'd'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001793 || *regparse == 'o'
1794 || *regparse == 'x'
1795 || *regparse == 'u'
1796 || *regparse == 'U'
1797 )
1798 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001799 // TODO(RE) This needs more testing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 startc = coll_get_char();
1801 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001802 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001803 }
1804 else
1805 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001806 // \r,\t,\e,\b
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001807 startc = backslash_trans(*regparse);
1808 }
1809 }
1810
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001811 // Normal printable char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001813 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001814
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001815 // Previous char was '-', so this char is end of range.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 if (emit_range)
1817 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001818 endc = startc;
1819 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001821 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001822
1823 if (endc > startc + 2)
1824 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001825 // Emit a range instead of the sequence of
1826 // individual characters.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001827 if (startc == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001828 // \x00 is translated to \x0a, start at \x01.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001829 EMIT(1);
1830 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 --post_ptr; // remove NFA_CONCAT
Bram Moolenaar417bad22013-06-07 14:08:30 +02001832 EMIT(endc);
1833 EMIT(NFA_RANGE);
1834 EMIT(NFA_CONCAT);
1835 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001836 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001837 || (*mb_char2len)(endc) > 1))
1838 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001839 // Emit the characters in the range.
1840 // "startc" was already emitted, so skip it.
1841 //
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001842 for (c = startc + 1; c <= endc; c++)
1843 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001844 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001845 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001846 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847 }
1848 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001849 {
1850#ifdef EBCDIC
1851 int alpha_only = FALSE;
1852
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001853 // for alphabetical range skip the gaps
1854 // 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 if (isalpha(startc) && isalpha(endc))
1856 alpha_only = TRUE;
1857#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001858 // Emit the range. "startc" was already emitted, so
1859 // skip it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001860 for (c = startc + 1; c <= endc; c++)
1861#ifdef EBCDIC
1862 if (!alpha_only || isalpha(startc))
1863#endif
1864 {
1865 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001866 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001867 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001869 emit_range = FALSE;
1870 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 }
1872 else
1873 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001874 // This char (startc) is not part of a range. Just
1875 // emit it.
1876 // Normally, simply emit startc. But if we get char
1877 // code=0 from a collating char, then replace it with
1878 // 0x0a.
1879 // This is needed to completely mimic the behaviour of
1880 // the backtracking engine.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001881 if (startc == NFA_NEWL)
1882 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001883 // Line break can't be matched as part of the
1884 // collection, add an OR below. But not for negated
1885 // range.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001886 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001887 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001888 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001890 {
1891 if (got_coll_char == TRUE && startc == 0)
1892 EMIT(0x0a);
1893 else
1894 EMIT(startc);
1895 EMIT(NFA_CONCAT);
1896 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 }
1898
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001899 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001900 } // while (p < endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001901
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001902 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001903 if (*regparse == '-') // if last, '-' is just a char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 {
1905 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001906 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001909 // skip the trailing ]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001910 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001911 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001912
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001913 // Mark end of the collection.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001915 EMIT(NFA_END_NEG_COLL);
1916 else
1917 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001918
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001919 // \_[] also matches \n but it's not negated
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001920 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001921 {
1922 EMIT(reg_string ? NL : NFA_NEWL);
1923 EMIT(NFA_OR);
1924 }
1925
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926 return OK;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001927 } // if exists closing ]
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001928
1929 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001931 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001932
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 default:
1934 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 int plen;
1936
1937nfa_do_multibyte:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001938 // plen is length of current char with composing chars
Bram Moolenaar47196582013-05-25 22:04:23 +02001939 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001940 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001941 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001942 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001943 int i = 0;
1944
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001945 // A base character plus composing characters, or just one
1946 // or more composing characters.
1947 // This requires creating a separate atom as if enclosing
1948 // the characters in (), where NFA_COMPOSING is the ( and
1949 // NFA_END_COMPOSING is the ). Note that right now we are
1950 // building the postfix form, not the NFA itself;
1951 // a composing char could be: a, b, c, NFA_COMPOSING
1952 // where 'b' and 'c' are chars with codes > 256.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001953 for (;;)
1954 {
1955 EMIT(c);
1956 if (i > 0)
1957 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001958 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001959 break;
1960 c = utf_ptr2char(old_regparse + i);
1961 }
1962 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001963 regparse = old_regparse + plen;
1964 }
1965 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 {
1967 c = no_Magic(c);
1968 EMIT(c);
1969 }
1970 return OK;
1971 }
1972 }
1973
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001974 return OK;
1975}
1976
1977/*
1978 * Parse something followed by possible [*+=].
1979 *
1980 * A piece is an atom, possibly followed by a multi, an indication of how many
1981 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1982 * characters: "", "a", "aa", etc.
1983 *
1984 * piece ::= atom
1985 * or atom multi
1986 */
1987 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001988nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989{
1990 int i;
1991 int op;
1992 int ret;
1993 long minval, maxval;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001994 int greedy = TRUE; // Braces are prefixed with '-' ?
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001995 parse_state_T old_state;
1996 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001997 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001998 int old_post_pos;
1999 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002000 int quest;
2001
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002002 // Save the current parse state, so that we can use it if <atom>{m,n} is
2003 // next.
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002004 save_parse_state(&old_state);
2005
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002006 // store current pos in the postfix form, for \{m,n} involving 0s
Bram Moolenaar16299b52013-05-30 18:45:23 +02002007 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008
2009 ret = nfa_regatom();
2010 if (ret == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002011 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002012
2013 op = peekchr();
2014 if (re_multi_type(op) == NOT_MULTI)
2015 return OK;
2016
2017 skipchr();
2018 switch (op)
2019 {
2020 case Magic('*'):
2021 EMIT(NFA_STAR);
2022 break;
2023
2024 case Magic('+'):
2025 /*
2026 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2027 * first and only submatch would be "aaa". But the backtracking
2028 * engine interprets the plus as "try matching one more time", and
2029 * a* matches a second time at the end of the input, the empty
2030 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002031 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002032 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002033 * In order to be consistent with the old engine, we replace
2034 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002036 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037 curchr = -1;
2038 if (nfa_regatom() == FAIL)
2039 return FAIL;
2040 EMIT(NFA_STAR);
2041 EMIT(NFA_CONCAT);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002042 skipchr(); // skip the \+
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 break;
2044
2045 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002046 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002047 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002048 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002049 switch(op)
2050 {
2051 case '=':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002052 // \@=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002053 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002055 case '!':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002056 // \@!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002057 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002058 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002060 op = no_Magic(getchr());
2061 if (op == '=')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002062 // \@<=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002063 i = NFA_PREV_ATOM_JUST_BEFORE;
2064 else if (op == '!')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002065 // \@<!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002066 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2067 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 case '>':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002069 // \@>
Bram Moolenaar87953742013-06-05 18:52:40 +02002070 i = NFA_PREV_ATOM_LIKE_PATTERN;
2071 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002073 if (i == 0)
2074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002075 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002076 return FAIL;
2077 }
2078 EMIT(i);
2079 if (i == NFA_PREV_ATOM_JUST_BEFORE
2080 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2081 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002082 break;
2083
2084 case Magic('?'):
2085 case Magic('='):
2086 EMIT(NFA_QUEST);
2087 break;
2088
2089 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002090 // a{2,5} will expand to 'aaa?a?a?'
2091 // a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2092 // version of '?'
2093 // \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2094 // parenthesis have the same id
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002095
2096 greedy = TRUE;
2097 c2 = peekchr();
2098 if (c2 == '-' || c2 == Magic('-'))
2099 {
2100 skipchr();
2101 greedy = FALSE;
2102 }
2103 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002104 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002105
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002106 // <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2107 // <atom>*
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002108 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002109 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002110 if (greedy) // { { (match the braces)
2111 // \{}, \{0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002112 EMIT(NFA_STAR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002113 else // { { (match the braces)
2114 // \{-}, \{-0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002115 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002116 break;
2117 }
2118
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002119 // Special case: x{0} or x{-0}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002120 if (maxval == 0)
2121 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002122 // Ignore result of previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002123 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002124 // NFA_EMPTY is 0-length and works everywhere
Bram Moolenaar699c1202013-09-25 16:41:54 +02002125 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002126 return OK;
2127 }
2128
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002129 // The engine is very inefficient (uses too many states) when the
2130 // maximum is much larger than the minimum and when the maximum is
2131 // large. Bail out if we can use the other engine.
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002132 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002133 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002134 return FAIL;
2135
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002136 // Ignore previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002137 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002138 // Save parse state after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002139 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002140
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002141 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2142 for (i = 0; i < maxval; i++)
2143 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002144 // Goto beginning of the repeated atom
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002145 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002146 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002147 if (nfa_regatom() == FAIL)
2148 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002149 // after "minval" times, atoms are optional
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002150 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002151 {
2152 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002153 {
2154 if (greedy)
2155 EMIT(NFA_STAR);
2156 else
2157 EMIT(NFA_STAR_NONGREEDY);
2158 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002159 else
2160 EMIT(quest);
2161 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002162 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002164 if (i + 1 > minval && maxval == MAX_LIMIT)
2165 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 }
2167
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002168 // Go to just after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002169 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 curchr = -1;
2171
2172 break;
2173
2174
2175 default:
2176 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002177 } // end switch
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178
2179 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002180 // Can't have a multi follow a multi.
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002181 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182
2183 return OK;
2184}
2185
2186/*
2187 * Parse one or more pieces, concatenated. It matches a match for the
2188 * first piece, followed by a match for the second piece, etc. Example:
2189 * "f[0-9]b", first matches "f", then a digit and then "b".
2190 *
2191 * concat ::= piece
2192 * or piece piece
2193 * or piece piece piece
2194 * etc.
2195 */
2196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002197nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002198{
2199 int cont = TRUE;
2200 int first = TRUE;
2201
2202 while (cont)
2203 {
2204 switch (peekchr())
2205 {
2206 case NUL:
2207 case Magic('|'):
2208 case Magic('&'):
2209 case Magic(')'):
2210 cont = FALSE;
2211 break;
2212
2213 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002214 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002215 skipchr_keepstart();
2216 break;
2217 case Magic('c'):
2218 regflags |= RF_ICASE;
2219 skipchr_keepstart();
2220 break;
2221 case Magic('C'):
2222 regflags |= RF_NOICASE;
2223 skipchr_keepstart();
2224 break;
2225 case Magic('v'):
2226 reg_magic = MAGIC_ALL;
2227 skipchr_keepstart();
2228 curchr = -1;
2229 break;
2230 case Magic('m'):
2231 reg_magic = MAGIC_ON;
2232 skipchr_keepstart();
2233 curchr = -1;
2234 break;
2235 case Magic('M'):
2236 reg_magic = MAGIC_OFF;
2237 skipchr_keepstart();
2238 curchr = -1;
2239 break;
2240 case Magic('V'):
2241 reg_magic = MAGIC_NONE;
2242 skipchr_keepstart();
2243 curchr = -1;
2244 break;
2245
2246 default:
2247 if (nfa_regpiece() == FAIL)
2248 return FAIL;
2249 if (first == FALSE)
2250 EMIT(NFA_CONCAT);
2251 else
2252 first = FALSE;
2253 break;
2254 }
2255 }
2256
2257 return OK;
2258}
2259
2260/*
2261 * Parse a branch, one or more concats, separated by "\&". It matches the
2262 * last concat, but only if all the preceding concats also match at the same
2263 * position. Examples:
2264 * "foobeep\&..." matches "foo" in "foobeep".
2265 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2266 *
2267 * branch ::= concat
2268 * or concat \& concat
2269 * or concat \& concat \& concat
2270 * etc.
2271 */
2272 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002273nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002275 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276
Bram Moolenaar16299b52013-05-30 18:45:23 +02002277 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002278
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002279 // First branch, possibly the only one
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002280 if (nfa_regconcat() == FAIL)
2281 return FAIL;
2282
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002283 // Try next concats
Bram Moolenaar890dd052017-12-16 19:59:37 +01002284 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 {
2286 skipchr();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002287 // if concat is empty do emit a node
Bram Moolenaar890dd052017-12-16 19:59:37 +01002288 if (old_post_pos == (int)(post_ptr - post_start))
2289 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002290 EMIT(NFA_NOPEN);
2291 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002292 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002293 if (nfa_regconcat() == FAIL)
2294 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002295 // if concat is empty do emit a node
Bram Moolenaar16299b52013-05-30 18:45:23 +02002296 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002297 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002298 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002299 }
2300
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002301 // if a branch is empty, emit one node for it
Bram Moolenaar16299b52013-05-30 18:45:23 +02002302 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002303 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304
2305 return OK;
2306}
2307
2308/*
2309 * Parse a pattern, one or more branches, separated by "\|". It matches
2310 * anything that matches one of the branches. Example: "foo\|beep" matches
2311 * "foo" and matches "beep". If more than one branch matches, the first one
2312 * is used.
2313 *
2314 * pattern ::= branch
2315 * or branch \| branch
2316 * or branch \| branch \| branch
2317 * etc.
2318 */
2319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002320nfa_reg(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002321 int paren) // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322{
2323 int parno = 0;
2324
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002325 if (paren == REG_PAREN)
2326 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002327 if (regnpar >= NSUBEXP) // Too many `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002329 parno = regnpar++;
2330 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002331#ifdef FEAT_SYN_HL
2332 else if (paren == REG_ZPAREN)
2333 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002334 // Make a ZOPEN node.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002335 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002336 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002337 parno = regnzpar++;
2338 }
2339#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340
2341 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002342 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002343
2344 while (peekchr() == Magic('|'))
2345 {
2346 skipchr();
2347 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002348 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349 EMIT(NFA_OR);
2350 }
2351
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002352 // Check for proper termination.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2354 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 if (paren == REG_NPAREN)
2356 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2357 else
2358 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2359 }
2360 else if (paren == REG_NOPAREN && peekchr() != NUL)
2361 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362 if (peekchr() == Magic(')'))
2363 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2364 else
2365 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2366 }
2367 /*
2368 * Here we set the flag allowing back references to this set of
2369 * parentheses.
2370 */
2371 if (paren == REG_PAREN)
2372 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002373 had_endbrace[parno] = TRUE; // have seen the close paren
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002374 EMIT(NFA_MOPEN + parno);
2375 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002376#ifdef FEAT_SYN_HL
2377 else if (paren == REG_ZPAREN)
2378 EMIT(NFA_ZOPEN + parno);
2379#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002380
2381 return OK;
2382}
2383
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384#ifdef DEBUG
2385static char_u code[50];
2386
2387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002388nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389{
2390 int addnl = FALSE;
2391
2392 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2393 {
2394 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002395 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396 }
2397
2398 STRCPY(code, "");
2399 switch (c)
2400 {
2401 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2402 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2403 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2404 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2405 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2406 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2407
Bram Moolenaar5714b802013-05-28 22:03:20 +02002408 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2409 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2410 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2411 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2412 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2413 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2414 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2415 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2416 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002417#ifdef FEAT_SYN_HL
2418 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2419 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2420 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2421 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2422 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2423 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2424 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2425 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2426 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2427#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002428 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2429
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430 case NFA_PREV_ATOM_NO_WIDTH:
2431 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002432 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2433 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002434 case NFA_PREV_ATOM_JUST_BEFORE:
2435 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2436 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2437 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002438 case NFA_PREV_ATOM_LIKE_PATTERN:
2439 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2440
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002441 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2442 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002444 case NFA_START_INVISIBLE_FIRST:
2445 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002446 case NFA_START_INVISIBLE_NEG:
2447 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002448 case NFA_START_INVISIBLE_NEG_FIRST:
2449 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002450 case NFA_START_INVISIBLE_BEFORE:
2451 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002452 case NFA_START_INVISIBLE_BEFORE_FIRST:
2453 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002454 case NFA_START_INVISIBLE_BEFORE_NEG:
2455 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002456 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2457 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002458 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002459 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002460 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002461 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002462
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002463 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2464 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002465 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002466
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002467 case NFA_MOPEN:
2468 case NFA_MOPEN1:
2469 case NFA_MOPEN2:
2470 case NFA_MOPEN3:
2471 case NFA_MOPEN4:
2472 case NFA_MOPEN5:
2473 case NFA_MOPEN6:
2474 case NFA_MOPEN7:
2475 case NFA_MOPEN8:
2476 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002477 STRCPY(code, "NFA_MOPEN(x)");
2478 code[10] = c - NFA_MOPEN + '0';
2479 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002480 case NFA_MCLOSE:
2481 case NFA_MCLOSE1:
2482 case NFA_MCLOSE2:
2483 case NFA_MCLOSE3:
2484 case NFA_MCLOSE4:
2485 case NFA_MCLOSE5:
2486 case NFA_MCLOSE6:
2487 case NFA_MCLOSE7:
2488 case NFA_MCLOSE8:
2489 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002490 STRCPY(code, "NFA_MCLOSE(x)");
2491 code[11] = c - NFA_MCLOSE + '0';
2492 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002493#ifdef FEAT_SYN_HL
2494 case NFA_ZOPEN:
2495 case NFA_ZOPEN1:
2496 case NFA_ZOPEN2:
2497 case NFA_ZOPEN3:
2498 case NFA_ZOPEN4:
2499 case NFA_ZOPEN5:
2500 case NFA_ZOPEN6:
2501 case NFA_ZOPEN7:
2502 case NFA_ZOPEN8:
2503 case NFA_ZOPEN9:
2504 STRCPY(code, "NFA_ZOPEN(x)");
2505 code[10] = c - NFA_ZOPEN + '0';
2506 break;
2507 case NFA_ZCLOSE:
2508 case NFA_ZCLOSE1:
2509 case NFA_ZCLOSE2:
2510 case NFA_ZCLOSE3:
2511 case NFA_ZCLOSE4:
2512 case NFA_ZCLOSE5:
2513 case NFA_ZCLOSE6:
2514 case NFA_ZCLOSE7:
2515 case NFA_ZCLOSE8:
2516 case NFA_ZCLOSE9:
2517 STRCPY(code, "NFA_ZCLOSE(x)");
2518 code[11] = c - NFA_ZCLOSE + '0';
2519 break;
2520#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002521 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2522 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2523 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2524 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002525 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2526 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002527 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2528 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2529 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2530 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2531 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2532 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2533 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2534 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2535 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2536 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2537 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2538 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2539 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2540 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002541 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002542
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002544 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2545 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2546 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002547 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002549
2550 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2551 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2552 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2553 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2554 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2555 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2556 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2557
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2559 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2560 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2561 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2562 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2563 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2564 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2565 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2566 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2567 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2568 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2569 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2570 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2571 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2572 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2573 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002574 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2575 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2576 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002577
2578 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2579 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2580 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2581 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2582 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2583 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2584 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2585 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2586 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2587 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2588 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2589 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2590 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2591 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2592 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2593 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2594 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2595 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2596 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2597 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2598 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2599 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2600 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2601 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2602 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2603 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2604 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002605 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2606 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2607 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2608 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002609
2610 default:
2611 STRCPY(code, "CHAR(x)");
2612 code[5] = c;
2613 }
2614
2615 if (addnl == TRUE)
2616 STRCAT(code, " + NEWLINE ");
2617
2618}
2619
2620#ifdef ENABLE_LOG
2621static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002622static 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 +02002623
2624/*
2625 * Print the postfix notation of the current regexp.
2626 */
2627 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002628nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002629{
2630 int *p;
2631 FILE *f;
2632
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002633 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002634 if (f != NULL)
2635 {
2636 fprintf(f, "\n-------------------------\n");
2637 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002638 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002639 else if (retval == OK)
2640 fprintf(f, ">>> NFA engine succeeded !\n");
2641 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002642 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002643 {
2644 nfa_set_code(*p);
2645 fprintf(f, "%s, ", code);
2646 }
2647 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002648 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002649 fprintf(f, "%d ", *p);
2650 fprintf(f, "\n\n");
2651 fclose(f);
2652 }
2653}
2654
2655/*
2656 * Print the NFA starting with a root node "state".
2657 */
2658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002659nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002660{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002661 garray_T indent;
2662
2663 ga_init2(&indent, 1, 64);
2664 ga_append(&indent, '\0');
2665 nfa_print_state2(debugf, state, &indent);
2666 ga_clear(&indent);
2667}
2668
2669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002670nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002671{
2672 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673
2674 if (state == NULL)
2675 return;
2676
2677 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002678
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002679 // Output indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002680 p = (char_u *)indent->ga_data;
2681 if (indent->ga_len >= 3)
2682 {
2683 int last = indent->ga_len - 3;
2684 char_u save[2];
2685
2686 STRNCPY(save, &p[last], 2);
2687 STRNCPY(&p[last], "+-", 2);
2688 fprintf(debugf, " %s", p);
2689 STRNCPY(&p[last], save, 2);
2690 }
2691 else
2692 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002693
2694 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002695 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002696 code,
2697 state->c,
2698 abs(state->id),
2699 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002700 if (state->id < 0)
2701 return;
2702
2703 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002704
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002705 // grow indent for state->out
Bram Moolenaar152e7892013-05-25 12:28:11 +02002706 indent->ga_len -= 1;
2707 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002708 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002709 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002710 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002711 ga_append(indent, '\0');
2712
2713 nfa_print_state2(debugf, state->out, indent);
2714
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002715 // replace last part of indent for state->out1
Bram Moolenaar152e7892013-05-25 12:28:11 +02002716 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002717 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002718 ga_append(indent, '\0');
2719
2720 nfa_print_state2(debugf, state->out1, indent);
2721
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002722 // shrink indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002723 indent->ga_len -= 3;
2724 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725}
2726
2727/*
2728 * Print the NFA state machine.
2729 */
2730 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002731nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002733 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002734
2735 if (debugf != NULL)
2736 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002737 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002738
Bram Moolenaar473de612013-06-08 18:19:48 +02002739 if (prog->reganch)
2740 fprintf(debugf, "reganch: %d\n", prog->reganch);
2741 if (prog->regstart != NUL)
2742 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2743 prog->regstart, prog->regstart);
2744 if (prog->match_text != NULL)
2745 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002746
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002747 fclose(debugf);
2748 }
2749}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002750#endif // ENABLE_LOG
2751#endif // DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752
2753/*
2754 * Parse r.e. @expr and convert it into postfix form.
2755 * Return the postfix string on success, NULL otherwise.
2756 */
2757 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002758re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002759{
2760 if (nfa_reg(REG_NOPAREN) == FAIL)
2761 return NULL;
2762 EMIT(NFA_MOPEN);
2763 return post_start;
2764}
2765
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002766// NB. Some of the code below is inspired by Russ's.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002767
2768/*
2769 * Represents an NFA state plus zero or one or two arrows exiting.
2770 * if c == MATCH, no arrows out; matching state.
2771 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2772 * If c < 256, labeled arrow with character c to out.
2773 */
2774
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002775static nfa_state_T *state_ptr; // points to nfa_prog->state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776
2777/*
2778 * Allocate and initialize nfa_state_T.
2779 */
2780 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002781alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002782{
2783 nfa_state_T *s;
2784
2785 if (istate >= nstate)
2786 return NULL;
2787
2788 s = &state_ptr[istate++];
2789
2790 s->c = c;
2791 s->out = out;
2792 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002793 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002794
2795 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002796 s->lastlist[0] = 0;
2797 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798
2799 return s;
2800}
2801
2802/*
2803 * A partially built NFA without the matching state filled in.
2804 * Frag_T.start points at the start state.
2805 * Frag_T.out is a list of places that need to be set to the
2806 * next state for this fragment.
2807 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002808
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002809// Since the out pointers in the list are always
2810// uninitialized, we use the pointers themselves
2811// as storage for the Ptrlists.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002813union Ptrlist
2814{
2815 Ptrlist *next;
2816 nfa_state_T *s;
2817};
2818
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002819struct Frag
2820{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002821 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 Ptrlist *out;
2823};
2824typedef struct Frag Frag_T;
2825
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002826/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002827 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002828 */
2829 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002830frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002832 Frag_T n;
2833
2834 n.start = start;
2835 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836 return n;
2837}
2838
2839/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002840 * Create singleton list containing just outp.
2841 */
2842 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002843list1(
2844 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845{
2846 Ptrlist *l;
2847
2848 l = (Ptrlist *)outp;
2849 l->next = NULL;
2850 return l;
2851}
2852
2853/*
2854 * Patch the list of states at out to point to start.
2855 */
2856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002857patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002858{
2859 Ptrlist *next;
2860
2861 for (; l; l = next)
2862 {
2863 next = l->next;
2864 l->s = s;
2865 }
2866}
2867
2868
2869/*
2870 * Join the two lists l1 and l2, returning the combination.
2871 */
2872 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002873append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874{
2875 Ptrlist *oldl1;
2876
2877 oldl1 = l1;
2878 while (l1->next)
2879 l1 = l1->next;
2880 l1->next = l2;
2881 return oldl1;
2882}
2883
2884/*
2885 * Stack used for transforming postfix form into NFA.
2886 */
2887static Frag_T empty;
2888
2889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002890st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002892#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 FILE *df;
2894 int *p2;
2895
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002896 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897 if (df)
2898 {
2899 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002900# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002902# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002903 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002904# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905 for (p2 = postfix; p2 < end; p2++)
2906 {
2907 nfa_set_code(*p2);
2908 fprintf(df, "%s, ", code);
2909 }
2910 nfa_set_code(*p);
2911 fprintf(df, "\nCurrent position is: ");
2912 for (p2 = postfix; p2 <= p; p2 ++)
2913 {
2914 nfa_set_code(*p2);
2915 fprintf(df, "%s, ", code);
2916 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002917# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918 for (p2 = postfix; p2 < end; p2++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002919 fprintf(df, "%d, ", *p2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002920 fprintf(df, "\nCurrent position is: ");
2921 for (p2 = postfix; p2 <= p; p2 ++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002922 fprintf(df, "%d, ", *p2);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002923# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002924 fprintf(df, "\n--------------------------\n");
2925 fclose(df);
2926 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002927#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002928 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002929}
2930
2931/*
2932 * Push an item onto the stack.
2933 */
2934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002935st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936{
2937 Frag_T *stackp = *p;
2938
2939 if (stackp >= stack_end)
2940 return;
2941 *stackp = s;
2942 *p = *p + 1;
2943}
2944
2945/*
2946 * Pop an item from the stack.
2947 */
2948 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002949st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950{
2951 Frag_T *stackp;
2952
2953 *p = *p - 1;
2954 stackp = *p;
2955 if (stackp < stack)
2956 return empty;
2957 return **p;
2958}
2959
2960/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002961 * Estimate the maximum byte length of anything matching "state".
2962 * When unknown or unlimited return -1.
2963 */
2964 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002965nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002966{
2967 int l, r;
2968 nfa_state_T *state = startstate;
2969 int len = 0;
2970
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002971 // detect looping in a NFA_SPLIT
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002972 if (depth > 4)
2973 return -1;
2974
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002975 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002976 {
2977 switch (state->c)
2978 {
2979 case NFA_END_INVISIBLE:
2980 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002981 // the end, return what we have
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002982 return len;
2983
2984 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002985 // two alternatives, use the maximum
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002986 l = nfa_max_width(state->out, depth + 1);
2987 r = nfa_max_width(state->out1, depth + 1);
2988 if (l < 0 || r < 0)
2989 return -1;
2990 return len + (l > r ? l : r);
2991
2992 case NFA_ANY:
2993 case NFA_START_COLL:
2994 case NFA_START_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002995 // matches some character, including composing chars
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002996 if (enc_utf8)
2997 len += MB_MAXBYTES;
2998 else if (has_mbyte)
2999 len += 2;
3000 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003001 ++len;
3002 if (state->c != NFA_ANY)
3003 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003004 // skip over the characters
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003005 state = state->out1->out;
3006 continue;
3007 }
3008 break;
3009
3010 case NFA_DIGIT:
3011 case NFA_WHITE:
3012 case NFA_HEX:
3013 case NFA_OCTAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003014 // ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003015 ++len;
3016 break;
3017
3018 case NFA_IDENT:
3019 case NFA_SIDENT:
3020 case NFA_KWORD:
3021 case NFA_SKWORD:
3022 case NFA_FNAME:
3023 case NFA_SFNAME:
3024 case NFA_PRINT:
3025 case NFA_SPRINT:
3026 case NFA_NWHITE:
3027 case NFA_NDIGIT:
3028 case NFA_NHEX:
3029 case NFA_NOCTAL:
3030 case NFA_WORD:
3031 case NFA_NWORD:
3032 case NFA_HEAD:
3033 case NFA_NHEAD:
3034 case NFA_ALPHA:
3035 case NFA_NALPHA:
3036 case NFA_LOWER:
3037 case NFA_NLOWER:
3038 case NFA_UPPER:
3039 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003040 case NFA_LOWER_IC:
3041 case NFA_NLOWER_IC:
3042 case NFA_UPPER_IC:
3043 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003044 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003045 // possibly non-ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003046 if (has_mbyte)
3047 len += 3;
3048 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003049 ++len;
3050 break;
3051
3052 case NFA_START_INVISIBLE:
3053 case NFA_START_INVISIBLE_NEG:
3054 case NFA_START_INVISIBLE_BEFORE:
3055 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003056 // zero-width, out1 points to the END state
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003057 state = state->out1->out;
3058 continue;
3059
3060 case NFA_BACKREF1:
3061 case NFA_BACKREF2:
3062 case NFA_BACKREF3:
3063 case NFA_BACKREF4:
3064 case NFA_BACKREF5:
3065 case NFA_BACKREF6:
3066 case NFA_BACKREF7:
3067 case NFA_BACKREF8:
3068 case NFA_BACKREF9:
3069#ifdef FEAT_SYN_HL
3070 case NFA_ZREF1:
3071 case NFA_ZREF2:
3072 case NFA_ZREF3:
3073 case NFA_ZREF4:
3074 case NFA_ZREF5:
3075 case NFA_ZREF6:
3076 case NFA_ZREF7:
3077 case NFA_ZREF8:
3078 case NFA_ZREF9:
3079#endif
3080 case NFA_NEWL:
3081 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003082 // unknown width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003083 return -1;
3084
3085 case NFA_BOL:
3086 case NFA_EOL:
3087 case NFA_BOF:
3088 case NFA_EOF:
3089 case NFA_BOW:
3090 case NFA_EOW:
3091 case NFA_MOPEN:
3092 case NFA_MOPEN1:
3093 case NFA_MOPEN2:
3094 case NFA_MOPEN3:
3095 case NFA_MOPEN4:
3096 case NFA_MOPEN5:
3097 case NFA_MOPEN6:
3098 case NFA_MOPEN7:
3099 case NFA_MOPEN8:
3100 case NFA_MOPEN9:
3101#ifdef FEAT_SYN_HL
3102 case NFA_ZOPEN:
3103 case NFA_ZOPEN1:
3104 case NFA_ZOPEN2:
3105 case NFA_ZOPEN3:
3106 case NFA_ZOPEN4:
3107 case NFA_ZOPEN5:
3108 case NFA_ZOPEN6:
3109 case NFA_ZOPEN7:
3110 case NFA_ZOPEN8:
3111 case NFA_ZOPEN9:
3112 case NFA_ZCLOSE:
3113 case NFA_ZCLOSE1:
3114 case NFA_ZCLOSE2:
3115 case NFA_ZCLOSE3:
3116 case NFA_ZCLOSE4:
3117 case NFA_ZCLOSE5:
3118 case NFA_ZCLOSE6:
3119 case NFA_ZCLOSE7:
3120 case NFA_ZCLOSE8:
3121 case NFA_ZCLOSE9:
3122#endif
3123 case NFA_MCLOSE:
3124 case NFA_MCLOSE1:
3125 case NFA_MCLOSE2:
3126 case NFA_MCLOSE3:
3127 case NFA_MCLOSE4:
3128 case NFA_MCLOSE5:
3129 case NFA_MCLOSE6:
3130 case NFA_MCLOSE7:
3131 case NFA_MCLOSE8:
3132 case NFA_MCLOSE9:
3133 case NFA_NOPEN:
3134 case NFA_NCLOSE:
3135
3136 case NFA_LNUM_GT:
3137 case NFA_LNUM_LT:
3138 case NFA_COL_GT:
3139 case NFA_COL_LT:
3140 case NFA_VCOL_GT:
3141 case NFA_VCOL_LT:
3142 case NFA_MARK_GT:
3143 case NFA_MARK_LT:
3144 case NFA_VISUAL:
3145 case NFA_LNUM:
3146 case NFA_CURSOR:
3147 case NFA_COL:
3148 case NFA_VCOL:
3149 case NFA_MARK:
3150
3151 case NFA_ZSTART:
3152 case NFA_ZEND:
3153 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003154 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003155 case NFA_START_PATTERN:
3156 case NFA_END_PATTERN:
3157 case NFA_COMPOSING:
3158 case NFA_END_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003159 // zero-width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003160 break;
3161
3162 default:
3163 if (state->c < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003164 // don't know what this is
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003165 return -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003166 // normal character
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003167 len += MB_CHAR2LEN(state->c);
3168 break;
3169 }
3170
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003171 // normal way to continue
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003172 state = state->out;
3173 }
3174
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003175 // unrecognized, "cannot happen"
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003176 return -1;
3177}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003178
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003179/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 * Convert a postfix form into its equivalent NFA.
3181 * Return the NFA start state on success, NULL otherwise.
3182 */
3183 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003184post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003185{
3186 int *p;
3187 int mopen;
3188 int mclose;
3189 Frag_T *stack = NULL;
3190 Frag_T *stackp = NULL;
3191 Frag_T *stack_end = NULL;
3192 Frag_T e1;
3193 Frag_T e2;
3194 Frag_T e;
3195 nfa_state_T *s;
3196 nfa_state_T *s1;
3197 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003198 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003199
3200 if (postfix == NULL)
3201 return NULL;
3202
Bram Moolenaar053bb602013-05-20 13:55:21 +02003203#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204#define POP() st_pop(&stackp, stack); \
3205 if (stackp < stack) \
3206 { \
3207 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003208 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209 return NULL; \
3210 }
3211
3212 if (nfa_calc_size == FALSE)
3213 {
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003214 // Allocate space for the stack. Max states on the stack: "nstate".
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003215 stack = ALLOC_MULT(Frag_T, nstate + 1);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003216 if (stack == NULL)
3217 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003219 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 }
3221
3222 for (p = postfix; p < end; ++p)
3223 {
3224 switch (*p)
3225 {
3226 case NFA_CONCAT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003227 // Concatenation.
3228 // Pay attention: this operator does not exist in the r.e. itself
3229 // (it is implicit, really). It is added when r.e. is translated
3230 // to postfix form in re2post().
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003231 if (nfa_calc_size == TRUE)
3232 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003233 // nstate += 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 break;
3235 }
3236 e2 = POP();
3237 e1 = POP();
3238 patch(e1.out, e2.start);
3239 PUSH(frag(e1.start, e2.out));
3240 break;
3241
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003242 case NFA_OR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003243 // Alternation
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 if (nfa_calc_size == TRUE)
3245 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003246 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 break;
3248 }
3249 e2 = POP();
3250 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003251 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003253 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003254 PUSH(frag(s, append(e1.out, e2.out)));
3255 break;
3256
3257 case NFA_STAR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003258 // Zero or more, prefer more
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259 if (nfa_calc_size == TRUE)
3260 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003261 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003262 break;
3263 }
3264 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003265 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003267 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 patch(e.out, s);
3269 PUSH(frag(s, list1(&s->out1)));
3270 break;
3271
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003272 case NFA_STAR_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003273 // Zero or more, prefer zero
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003274 if (nfa_calc_size == TRUE)
3275 {
3276 nstate++;
3277 break;
3278 }
3279 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003280 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003281 if (s == NULL)
3282 goto theend;
3283 patch(e.out, s);
3284 PUSH(frag(s, list1(&s->out)));
3285 break;
3286
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003287 case NFA_QUEST:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003288 // one or zero atoms=> greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 if (nfa_calc_size == TRUE)
3290 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003291 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 break;
3293 }
3294 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003295 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003297 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 PUSH(frag(s, append(e.out, list1(&s->out1))));
3299 break;
3300
3301 case NFA_QUEST_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003302 // zero or one atoms => non-greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003303 if (nfa_calc_size == TRUE)
3304 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003305 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003306 break;
3307 }
3308 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003309 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003311 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 PUSH(frag(s, append(e.out, list1(&s->out))));
3313 break;
3314
Bram Moolenaar417bad22013-06-07 14:08:30 +02003315 case NFA_END_COLL:
3316 case NFA_END_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003317 // On the stack is the sequence starting with NFA_START_COLL or
3318 // NFA_START_NEG_COLL and all possible characters. Patch it to
3319 // add the output to the start.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003320 if (nfa_calc_size == TRUE)
3321 {
3322 nstate++;
3323 break;
3324 }
3325 e = POP();
3326 s = alloc_state(NFA_END_COLL, NULL, NULL);
3327 if (s == NULL)
3328 goto theend;
3329 patch(e.out, s);
3330 e.start->out1 = s;
3331 PUSH(frag(e.start, list1(&s->out)));
3332 break;
3333
3334 case NFA_RANGE:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003335 // Before this are two characters, the low and high end of a
3336 // range. Turn them into two states with MIN and MAX.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003337 if (nfa_calc_size == TRUE)
3338 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003339 // nstate += 0;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003340 break;
3341 }
3342 e2 = POP();
3343 e1 = POP();
3344 e2.start->val = e2.start->c;
3345 e2.start->c = NFA_RANGE_MAX;
3346 e1.start->val = e1.start->c;
3347 e1.start->c = NFA_RANGE_MIN;
3348 patch(e1.out, e2.start);
3349 PUSH(frag(e1.start, e2.out));
3350 break;
3351
Bram Moolenaar699c1202013-09-25 16:41:54 +02003352 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003353 // 0-length, used in a repetition with max/min count of 0
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 if (nfa_calc_size == TRUE)
3355 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003356 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 break;
3358 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003359 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003361 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 PUSH(frag(s, list1(&s->out)));
3363 break;
3364
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003365 case NFA_OPT_CHARS:
3366 {
3367 int n;
3368
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003369 // \%[abc] implemented as:
3370 // NFA_SPLIT
3371 // +-CHAR(a)
3372 // | +-NFA_SPLIT
3373 // | +-CHAR(b)
3374 // | | +-NFA_SPLIT
3375 // | | +-CHAR(c)
3376 // | | | +-next
3377 // | | +- next
3378 // | +- next
3379 // +- next
3380 n = *++p; // get number of characters
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003381 if (nfa_calc_size == TRUE)
3382 {
3383 nstate += n;
3384 break;
3385 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003386 s = NULL; // avoid compiler warning
3387 e1.out = NULL; // stores list with out1's
3388 s1 = NULL; // previous NFA_SPLIT to connect to
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003389 while (n-- > 0)
3390 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003391 e = POP(); // get character
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003392 s = alloc_state(NFA_SPLIT, e.start, NULL);
3393 if (s == NULL)
3394 goto theend;
3395 if (e1.out == NULL)
3396 e1 = e;
3397 patch(e.out, s1);
3398 append(e1.out, list1(&s->out1));
3399 s1 = s;
3400 }
3401 PUSH(frag(s, e1.out));
3402 break;
3403 }
3404
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003405 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003406 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003407 case NFA_PREV_ATOM_JUST_BEFORE:
3408 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003409 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003410 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003411 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3412 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003413 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003414 int start_state;
3415 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003416 int n = 0;
3417 nfa_state_T *zend;
3418 nfa_state_T *skip;
3419
Bram Moolenaardecd9542013-06-07 16:31:50 +02003420 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003421 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003422 case NFA_PREV_ATOM_NO_WIDTH:
3423 start_state = NFA_START_INVISIBLE;
3424 end_state = NFA_END_INVISIBLE;
3425 break;
3426 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3427 start_state = NFA_START_INVISIBLE_NEG;
3428 end_state = NFA_END_INVISIBLE_NEG;
3429 break;
3430 case NFA_PREV_ATOM_JUST_BEFORE:
3431 start_state = NFA_START_INVISIBLE_BEFORE;
3432 end_state = NFA_END_INVISIBLE;
3433 break;
3434 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3435 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3436 end_state = NFA_END_INVISIBLE_NEG;
3437 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003438 default: // NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003439 start_state = NFA_START_PATTERN;
3440 end_state = NFA_END_PATTERN;
3441 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003442 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003443
3444 if (before)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003445 n = *++p; // get the count
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003446
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003447 // The \@= operator: match the preceding atom with zero width.
3448 // The \@! operator: no match for the preceding atom.
3449 // The \@<= operator: match for the preceding atom.
3450 // The \@<! operator: no match for the preceding atom.
3451 // Surrounds the preceding atom with START_INVISIBLE and
3452 // END_INVISIBLE, similarly to MOPEN.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003453
3454 if (nfa_calc_size == TRUE)
3455 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003456 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003457 break;
3458 }
3459 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003460 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003461 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003462 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003463
Bram Moolenaar87953742013-06-05 18:52:40 +02003464 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003465 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003466 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003467 if (pattern)
3468 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003469 // NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02003470 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003471 if (skip == NULL)
3472 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003473 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003474 if (zend == NULL)
3475 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003476 s1->out= skip;
3477 patch(e.out, zend);
3478 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003479 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003480 else
3481 {
3482 patch(e.out, s1);
3483 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003484 if (before)
3485 {
3486 if (n <= 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003487 // See if we can guess the maximum width, it avoids a
3488 // lot of pointless tries.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003489 n = nfa_max_width(e.start, 0);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003490 s->val = n; // store the count
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003491 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003492 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003494 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003495
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003496 case NFA_COMPOSING: // char with composing char
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003497#if 0
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003498 // TODO
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003499 if (regflags & RF_ICOMBINE)
3500 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003501 // use the base character only
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003502 }
3503#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003504 // FALLTHROUGH
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003505
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003506 case NFA_MOPEN: // \( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003507 case NFA_MOPEN1:
3508 case NFA_MOPEN2:
3509 case NFA_MOPEN3:
3510 case NFA_MOPEN4:
3511 case NFA_MOPEN5:
3512 case NFA_MOPEN6:
3513 case NFA_MOPEN7:
3514 case NFA_MOPEN8:
3515 case NFA_MOPEN9:
3516#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003517 case NFA_ZOPEN: // \z( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003518 case NFA_ZOPEN1:
3519 case NFA_ZOPEN2:
3520 case NFA_ZOPEN3:
3521 case NFA_ZOPEN4:
3522 case NFA_ZOPEN5:
3523 case NFA_ZOPEN6:
3524 case NFA_ZOPEN7:
3525 case NFA_ZOPEN8:
3526 case NFA_ZOPEN9:
3527#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003528 case NFA_NOPEN: // \%( \) "Invisible Submatch"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 if (nfa_calc_size == TRUE)
3530 {
3531 nstate += 2;
3532 break;
3533 }
3534
3535 mopen = *p;
3536 switch (*p)
3537 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003538 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3539#ifdef FEAT_SYN_HL
3540 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3541 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3542 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3543 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3544 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3545 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3546 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3547 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3548 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3549 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3550#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003551 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003552 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003553 // NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554 mclose = *p + NSUBEXP;
3555 break;
3556 }
3557
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003558 // Allow "NFA_MOPEN" as a valid postfix representation for
3559 // the empty regexp "". In this case, the NFA will be
3560 // NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3561 // empty groups of parenthesis, and empty mbyte chars
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003562 if (stackp == stack)
3563 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003564 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003565 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003566 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003567 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003568 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003569 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003570 patch(list1(&s->out), s1);
3571 PUSH(frag(s, list1(&s1->out)));
3572 break;
3573 }
3574
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003575 // At least one node was emitted before NFA_MOPEN, so
3576 // at least one node will be between NFA_MOPEN and NFA_MCLOSE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003577 e = POP();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003578 s = alloc_state(mopen, e.start, NULL); // `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003579 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003580 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003581
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003582 s1 = alloc_state(mclose, NULL, NULL); // `)'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003584 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003585 patch(e.out, s1);
3586
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003587 if (mopen == NFA_COMPOSING)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003588 // COMPOSING->out1 = END_COMPOSING
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589 patch(list1(&s->out1), s1);
3590
3591 PUSH(frag(s, list1(&s1->out)));
3592 break;
3593
Bram Moolenaar5714b802013-05-28 22:03:20 +02003594 case NFA_BACKREF1:
3595 case NFA_BACKREF2:
3596 case NFA_BACKREF3:
3597 case NFA_BACKREF4:
3598 case NFA_BACKREF5:
3599 case NFA_BACKREF6:
3600 case NFA_BACKREF7:
3601 case NFA_BACKREF8:
3602 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003603#ifdef FEAT_SYN_HL
3604 case NFA_ZREF1:
3605 case NFA_ZREF2:
3606 case NFA_ZREF3:
3607 case NFA_ZREF4:
3608 case NFA_ZREF5:
3609 case NFA_ZREF6:
3610 case NFA_ZREF7:
3611 case NFA_ZREF8:
3612 case NFA_ZREF9:
3613#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003614 if (nfa_calc_size == TRUE)
3615 {
3616 nstate += 2;
3617 break;
3618 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003619 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003620 if (s == NULL)
3621 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003622 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003623 if (s1 == NULL)
3624 goto theend;
3625 patch(list1(&s->out), s1);
3626 PUSH(frag(s, list1(&s1->out)));
3627 break;
3628
Bram Moolenaar423532e2013-05-29 21:14:42 +02003629 case NFA_LNUM:
3630 case NFA_LNUM_GT:
3631 case NFA_LNUM_LT:
3632 case NFA_VCOL:
3633 case NFA_VCOL_GT:
3634 case NFA_VCOL_LT:
3635 case NFA_COL:
3636 case NFA_COL_GT:
3637 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003638 case NFA_MARK:
3639 case NFA_MARK_GT:
3640 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003641 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003642 int n = *++p; // lnum, col or mark name
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003643
Bram Moolenaar423532e2013-05-29 21:14:42 +02003644 if (nfa_calc_size == TRUE)
3645 {
3646 nstate += 1;
3647 break;
3648 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003649 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003650 if (s == NULL)
3651 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003652 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003653 PUSH(frag(s, list1(&s->out)));
3654 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003655 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003656
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003657 case NFA_ZSTART:
3658 case NFA_ZEND:
3659 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003660 // Operands
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003661 if (nfa_calc_size == TRUE)
3662 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003663 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003664 break;
3665 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003666 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003667 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003668 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003669 PUSH(frag(s, list1(&s->out)));
3670 break;
3671
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003672 } // switch(*p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003673
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003674 } // for(p = postfix; *p; ++p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003675
3676 if (nfa_calc_size == TRUE)
3677 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003678 nstate++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003679 goto theend; // Return value when counting size is ignored anyway
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003680 }
3681
3682 e = POP();
3683 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003684 {
3685 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003686 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 +02003687 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688
3689 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003690 {
3691 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003692 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003693 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003695 matchstate = &state_ptr[istate++]; // the match state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696 matchstate->c = NFA_MATCH;
3697 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003698 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699
3700 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003701 ret = e.start;
3702
3703theend:
3704 vim_free(stack);
3705 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003706
3707#undef POP1
3708#undef PUSH1
3709#undef POP2
3710#undef PUSH2
3711#undef POP
3712#undef PUSH
3713}
3714
Bram Moolenaara2947e22013-06-11 22:44:09 +02003715/*
3716 * After building the NFA program, inspect it to add optimization hints.
3717 */
3718 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003719nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003720{
3721 int i;
3722 int c;
3723
3724 for (i = 0; i < prog->nstate; ++i)
3725 {
3726 c = prog->state[i].c;
3727 if (c == NFA_START_INVISIBLE
3728 || c == NFA_START_INVISIBLE_NEG
3729 || c == NFA_START_INVISIBLE_BEFORE
3730 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3731 {
3732 int directly;
3733
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003734 // Do it directly when what follows is possibly the end of the
3735 // match.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003736 if (match_follows(prog->state[i].out1->out, 0))
3737 directly = TRUE;
3738 else
3739 {
3740 int ch_invisible = failure_chance(prog->state[i].out, 0);
3741 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3742
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003743 // Postpone when the invisible match is expensive or has a
3744 // lower chance of failing.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003745 if (c == NFA_START_INVISIBLE_BEFORE
3746 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3747 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003748 // "before" matches are very expensive when
3749 // unbounded, always prefer what follows then,
3750 // unless what follows will always match.
3751 // Otherwise strongly prefer what follows.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003752 if (prog->state[i].val <= 0 && ch_follows > 0)
3753 directly = FALSE;
3754 else
3755 directly = ch_follows * 10 < ch_invisible;
3756 }
3757 else
3758 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003759 // normal invisible, first do the one with the
3760 // highest failure chance
Bram Moolenaara2947e22013-06-11 22:44:09 +02003761 directly = ch_follows < ch_invisible;
3762 }
3763 }
3764 if (directly)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003765 // switch to the _FIRST state
Bram Moolenaara2947e22013-06-11 22:44:09 +02003766 ++prog->state[i].c;
3767 }
3768 }
3769}
3770
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003771/////////////////////////////////////////////////////////////////
3772// NFA execution code.
3773/////////////////////////////////////////////////////////////////
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003774
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003775typedef struct
3776{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003777 int in_use; // number of subexpr with useful info
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003778
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003779 // When REG_MULTI is TRUE list.multi is used, otherwise list.line.
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003780 union
3781 {
3782 struct multipos
3783 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003784 linenr_T start_lnum;
3785 linenr_T end_lnum;
3786 colnr_T start_col;
3787 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003788 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003789 struct linepos
3790 {
3791 char_u *start;
3792 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003793 } line[NSUBEXP];
3794 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003795} regsub_T;
3796
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003797typedef struct
3798{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003799 regsub_T norm; // \( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003800#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003801 regsub_T synt; // \z( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003802#endif
3803} regsubs_T;
3804
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003805// nfa_pim_T stores a Postponed Invisible Match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02003806typedef struct nfa_pim_S nfa_pim_T;
3807struct nfa_pim_S
3808{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003809 int result; // NFA_PIM_*, see below
3810 nfa_state_T *state; // the invisible match start state
3811 regsubs_T subs; // submatch info, only party used
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003812 union
3813 {
3814 lpos_T pos;
3815 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003816 } end; // where the match must end
Bram Moolenaara2d95102013-06-04 14:23:05 +02003817};
3818
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003819// Values for done in nfa_pim_T.
3820#define NFA_PIM_UNUSED 0 // pim not used
3821#define NFA_PIM_TODO 1 // pim not done yet
3822#define NFA_PIM_MATCH 2 // pim executed, matches
3823#define NFA_PIM_NOMATCH 3 // pim executed, no match
Bram Moolenaara2d95102013-06-04 14:23:05 +02003824
3825
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003826// nfa_thread_T contains execution information of a NFA state
Bram Moolenaar4b417062013-05-25 20:19:50 +02003827typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003828{
3829 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003830 int count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003831 nfa_pim_T pim; // if pim.result != NFA_PIM_UNUSED: postponed
3832 // invisible match
3833 regsubs_T subs; // submatch info, only party used
Bram Moolenaar4b417062013-05-25 20:19:50 +02003834} nfa_thread_T;
3835
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003836// nfa_list_T contains the alternative NFA execution states.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003837typedef struct
3838{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003839 nfa_thread_T *t; // allocated array of states
3840 int n; // nr of states currently in "t"
3841 int len; // max nr of states in "t"
3842 int id; // ID of the list
3843 int has_pim; // TRUE when any state has a PIM
Bram Moolenaar4b417062013-05-25 20:19:50 +02003844} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003845
Bram Moolenaar5714b802013-05-28 22:03:20 +02003846#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003847static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003848
3849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003850log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003851{
3852 log_subexpr(&subs->norm);
3853# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003854 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003855 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003856# endif
3857}
3858
Bram Moolenaar5714b802013-05-28 22:03:20 +02003859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003860log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003861{
3862 int j;
3863
3864 for (j = 0; j < sub->in_use; j++)
3865 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003866 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003867 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003868 sub->list.multi[j].start_col,
3869 (int)sub->list.multi[j].start_lnum,
3870 sub->list.multi[j].end_col,
3871 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003872 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003873 {
3874 char *s = (char *)sub->list.line[j].start;
3875 char *e = (char *)sub->list.line[j].end;
3876
Bram Moolenaar87953742013-06-05 18:52:40 +02003877 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003878 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003879 s == NULL ? "NULL" : s,
3880 e == NULL ? "NULL" : e);
3881 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003882}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003883
3884 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003885pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003886{
3887 static char buf[30];
3888
3889 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3890 buf[0] = NUL;
3891 else
3892 {
3893 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003894 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003895 }
3896 return buf;
3897}
3898
Bram Moolenaar5714b802013-05-28 22:03:20 +02003899#endif
3900
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003901// Used during execution: whether a match has been found.
Bram Moolenaar2338c322018-07-08 19:07:19 +02003902static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003903#ifdef FEAT_RELTIME
3904static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003905static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003906static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003907#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003908
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003909static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003910static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003911
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003912/*
3913 * Copy postponed invisible match info from "from" to "to".
3914 */
3915 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003916copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003917{
3918 to->result = from->result;
3919 to->state = from->state;
3920 copy_sub(&to->subs.norm, &from->subs.norm);
3921#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003922 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003923 copy_sub(&to->subs.synt, &from->subs.synt);
3924#endif
3925 to->end = from->end;
3926}
3927
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003929clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003930{
3931 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003932 // Use 0xff to set lnum to -1
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003933 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003934 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003935 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003936 vim_memset(sub->list.line, 0,
3937 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003938 sub->in_use = 0;
3939}
3940
3941/*
3942 * Copy the submatches from "from" to "to".
3943 */
3944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003945copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003946{
3947 to->in_use = from->in_use;
3948 if (from->in_use > 0)
3949 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003950 // Copy the match start and end positions.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003951 if (REG_MULTI)
3952 mch_memmove(&to->list.multi[0],
3953 &from->list.multi[0],
3954 sizeof(struct multipos) * from->in_use);
3955 else
3956 mch_memmove(&to->list.line[0],
3957 &from->list.line[0],
3958 sizeof(struct linepos) * from->in_use);
3959 }
3960}
3961
3962/*
3963 * Like copy_sub() but exclude the main match.
3964 */
3965 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003966copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003967{
3968 if (to->in_use < from->in_use)
3969 to->in_use = from->in_use;
3970 if (from->in_use > 1)
3971 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003972 // Copy the match start and end positions.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003973 if (REG_MULTI)
3974 mch_memmove(&to->list.multi[1],
3975 &from->list.multi[1],
3976 sizeof(struct multipos) * (from->in_use - 1));
3977 else
3978 mch_memmove(&to->list.line[1],
3979 &from->list.line[1],
3980 sizeof(struct linepos) * (from->in_use - 1));
3981 }
3982}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003983
Bram Moolenaar428e9872013-05-30 17:05:39 +02003984/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003985 * Like copy_sub() but only do the end of the main match if \ze is present.
3986 */
3987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003988copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003989{
Bram Moolenaar0270f382018-07-17 05:43:58 +02003990 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02003991 {
3992 if (REG_MULTI)
3993 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003994 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003995 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003996 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
3997 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003998 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02003999 }
4000 else
4001 {
4002 if (from->list.line[0].end != NULL)
4003 to->list.line[0].end = from->list.line[0].end;
4004 }
4005 }
4006}
4007
4008/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004009 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004010 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004011 */
4012 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004013sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004014{
4015 int i;
4016 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004017 linenr_T s1;
4018 linenr_T s2;
4019 char_u *sp1;
4020 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004021
4022 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4023 if (REG_MULTI)
4024 {
4025 for (i = 0; i < todo; ++i)
4026 {
4027 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004028 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004029 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004030 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004031 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004032 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004034 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004035 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004036 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004037 if (s1 != -1 && sub1->list.multi[i].start_col
4038 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004039 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004040
Bram Moolenaar0270f382018-07-17 05:43:58 +02004041 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004042 {
4043 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004044 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004045 else
4046 s1 = -1;
4047 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004048 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004049 else
4050 s2 = -1;
4051 if (s1 != s2)
4052 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004053 if (s1 != -1 && sub1->list.multi[i].end_col
4054 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004055 return FALSE;
4056 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004057 }
4058 }
4059 else
4060 {
4061 for (i = 0; i < todo; ++i)
4062 {
4063 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004064 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004065 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004066 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004067 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004068 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004069 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004070 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004071 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004072 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004073 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004074 {
4075 if (i < sub1->in_use)
4076 sp1 = sub1->list.line[i].end;
4077 else
4078 sp1 = NULL;
4079 if (i < sub2->in_use)
4080 sp2 = sub2->list.line[i].end;
4081 else
4082 sp2 = NULL;
4083 if (sp1 != sp2)
4084 return FALSE;
4085 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004086 }
4087 }
4088
4089 return TRUE;
4090}
4091
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004092#ifdef ENABLE_LOG
4093 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004094report_state(char *action,
4095 regsub_T *sub,
4096 nfa_state_T *state,
4097 int lid,
4098 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004099{
4100 int col;
4101
4102 if (sub->in_use <= 0)
4103 col = -1;
4104 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004105 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004106 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004107 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004108 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004109 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4110 action, abs(state->id), lid, state->c, code, col,
4111 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004112}
4113#endif
4114
Bram Moolenaar43e02982013-06-07 17:31:29 +02004115/*
4116 * Return TRUE if the same state is already in list "l" with the same
4117 * positions as "subs".
4118 */
4119 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004120has_state_with_pos(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004121 nfa_list_T *l, // runtime state list
4122 nfa_state_T *state, // state to update
4123 regsubs_T *subs, // pointers to subexpressions
4124 nfa_pim_T *pim) // postponed match or NULL
Bram Moolenaar43e02982013-06-07 17:31:29 +02004125{
4126 nfa_thread_T *thread;
4127 int i;
4128
4129 for (i = 0; i < l->n; ++i)
4130 {
4131 thread = &l->t[i];
4132 if (thread->state->id == state->id
4133 && sub_equal(&thread->subs.norm, &subs->norm)
4134#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004135 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004136 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004137#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004138 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004139 return TRUE;
4140 }
4141 return FALSE;
4142}
4143
4144/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004145 * Return TRUE if "one" and "two" are equal. That includes when both are not
4146 * set.
4147 */
4148 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004149pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004150{
4151 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4152 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4153
4154 if (one_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004155 // one is unused: equal when two is also unused
Bram Moolenaar69b52452013-07-17 21:10:51 +02004156 return two_unused;
4157 if (two_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004158 // one is used and two is not: not equal
Bram Moolenaar69b52452013-07-17 21:10:51 +02004159 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004160 // compare the state id
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004161 if (one->state->id != two->state->id)
4162 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004163 // compare the position
Bram Moolenaar69b52452013-07-17 21:10:51 +02004164 if (REG_MULTI)
4165 return one->end.pos.lnum == two->end.pos.lnum
4166 && one->end.pos.col == two->end.pos.col;
4167 return one->end.ptr == two->end.ptr;
4168}
4169
4170/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004171 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4172 */
4173 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004174match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004175{
4176 nfa_state_T *state = startstate;
4177
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004178 // avoid too much recursion
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004179 if (depth > 10)
4180 return FALSE;
4181
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004182 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004183 {
4184 switch (state->c)
4185 {
4186 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004187 case NFA_MCLOSE:
4188 case NFA_END_INVISIBLE:
4189 case NFA_END_INVISIBLE_NEG:
4190 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004191 return TRUE;
4192
4193 case NFA_SPLIT:
4194 return match_follows(state->out, depth + 1)
4195 || match_follows(state->out1, depth + 1);
4196
4197 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004198 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004199 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004200 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004201 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004202 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004203 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004204 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004205 case NFA_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004206 // skip ahead to next state
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004207 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004208 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004209
4210 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004211 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004212 case NFA_IDENT:
4213 case NFA_SIDENT:
4214 case NFA_KWORD:
4215 case NFA_SKWORD:
4216 case NFA_FNAME:
4217 case NFA_SFNAME:
4218 case NFA_PRINT:
4219 case NFA_SPRINT:
4220 case NFA_WHITE:
4221 case NFA_NWHITE:
4222 case NFA_DIGIT:
4223 case NFA_NDIGIT:
4224 case NFA_HEX:
4225 case NFA_NHEX:
4226 case NFA_OCTAL:
4227 case NFA_NOCTAL:
4228 case NFA_WORD:
4229 case NFA_NWORD:
4230 case NFA_HEAD:
4231 case NFA_NHEAD:
4232 case NFA_ALPHA:
4233 case NFA_NALPHA:
4234 case NFA_LOWER:
4235 case NFA_NLOWER:
4236 case NFA_UPPER:
4237 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004238 case NFA_LOWER_IC:
4239 case NFA_NLOWER_IC:
4240 case NFA_UPPER_IC:
4241 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004242 case NFA_START_COLL:
4243 case NFA_START_NEG_COLL:
4244 case NFA_NEWL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004245 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004246 return FALSE;
4247
4248 default:
4249 if (state->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004250 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004251 return FALSE;
4252
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004253 // Others: zero-width or possibly zero-width, might still find
4254 // a match at the same position, keep looking.
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004255 break;
4256 }
4257 state = state->out;
4258 }
4259 return FALSE;
4260}
4261
4262
4263/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004264 * Return TRUE if "state" is already in list "l".
4265 */
4266 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004267state_in_list(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004268 nfa_list_T *l, // runtime state list
4269 nfa_state_T *state, // state to update
4270 regsubs_T *subs) // pointers to subexpressions
Bram Moolenaar43e02982013-06-07 17:31:29 +02004271{
4272 if (state->lastlist[nfa_ll_index] == l->id)
4273 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004274 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004275 return TRUE;
4276 }
4277 return FALSE;
4278}
4279
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004280// Offset used for "off" by addstate_here().
Bram Moolenaar16b35782016-09-09 20:29:50 +02004281#define ADDSTATE_HERE_OFFSET 10
4282
Bram Moolenaard05bf562013-06-30 23:24:08 +02004283/*
4284 * Add "state" and possibly what follows to state list ".".
4285 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004286 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004287 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004288 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004289addstate(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004290 nfa_list_T *l, // runtime state list
4291 nfa_state_T *state, // state to update
4292 regsubs_T *subs_arg, // pointers to subexpressions
4293 nfa_pim_T *pim, // postponed look-behind match
4294 int off_arg) // byte offset, when -1 go to next line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004295{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004296 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004297 int off = off_arg;
4298 int add_here = FALSE;
4299 int listindex = 0;
4300 int k;
4301 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004302 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004303 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004304 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004305 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004306 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004307 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004308 regsubs_T *subs = subs_arg;
4309 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004310#ifdef ENABLE_LOG
4311 int did_print = FALSE;
4312#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004313 static int depth = 0;
4314
4315 // This function is called recursively. When the depth is too much we run
4316 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004317 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004318 {
4319 --depth;
4320 return NULL;
4321 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004322
Bram Moolenaar16b35782016-09-09 20:29:50 +02004323 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4324 {
4325 add_here = TRUE;
4326 off = 0;
4327 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4328 }
4329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 switch (state->c)
4331 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004332 case NFA_NCLOSE:
4333 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004334 case NFA_MCLOSE1:
4335 case NFA_MCLOSE2:
4336 case NFA_MCLOSE3:
4337 case NFA_MCLOSE4:
4338 case NFA_MCLOSE5:
4339 case NFA_MCLOSE6:
4340 case NFA_MCLOSE7:
4341 case NFA_MCLOSE8:
4342 case NFA_MCLOSE9:
4343#ifdef FEAT_SYN_HL
4344 case NFA_ZCLOSE:
4345 case NFA_ZCLOSE1:
4346 case NFA_ZCLOSE2:
4347 case NFA_ZCLOSE3:
4348 case NFA_ZCLOSE4:
4349 case NFA_ZCLOSE5:
4350 case NFA_ZCLOSE6:
4351 case NFA_ZCLOSE7:
4352 case NFA_ZCLOSE8:
4353 case NFA_ZCLOSE9:
4354#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004355 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004356 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004357 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004358 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004359 // These nodes are not added themselves but their "out" and/or
4360 // "out1" may be added below.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004361 break;
4362
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004363 case NFA_BOL:
4364 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004365 // "^" won't match past end-of-line, don't bother trying.
4366 // Except when at the end of the line, or when we are going to the
4367 // next line for a look-behind match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004368 if (rex.input > rex.line
4369 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004370 && (nfa_endp == NULL
4371 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004372 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004373 goto skip_add;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004374 // FALLTHROUGH
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004375
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004376 case NFA_MOPEN1:
4377 case NFA_MOPEN2:
4378 case NFA_MOPEN3:
4379 case NFA_MOPEN4:
4380 case NFA_MOPEN5:
4381 case NFA_MOPEN6:
4382 case NFA_MOPEN7:
4383 case NFA_MOPEN8:
4384 case NFA_MOPEN9:
4385#ifdef FEAT_SYN_HL
4386 case NFA_ZOPEN:
4387 case NFA_ZOPEN1:
4388 case NFA_ZOPEN2:
4389 case NFA_ZOPEN3:
4390 case NFA_ZOPEN4:
4391 case NFA_ZOPEN5:
4392 case NFA_ZOPEN6:
4393 case NFA_ZOPEN7:
4394 case NFA_ZOPEN8:
4395 case NFA_ZOPEN9:
4396#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004397 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004398 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004399 // These nodes need to be added so that we can bail out when it
4400 // was added to this list before at the same position to avoid an
4401 // endless loop for "\(\)*"
Bram Moolenaar307aa162013-06-02 16:34:21 +02004402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004403 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004404 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004405 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004406 // This state is already in the list, don't add it again,
4407 // unless it is an MOPEN that is used for a backreference or
4408 // when there is a PIM. For NFA_MATCH check the position,
4409 // lower position is preferred.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004410 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004411 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004412 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004413 // When called from addstate_here() do insert before
4414 // existing states.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004415 if (add_here)
4416 {
4417 for (k = 0; k < l->n && k < listindex; ++k)
4418 if (l->t[k].state->id == state->id)
4419 {
4420 found = TRUE;
4421 break;
4422 }
4423 }
4424 if (!add_here || found)
4425 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004426skip_add:
4427#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004428 nfa_set_code(state->c);
4429 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4430 abs(state->id), l->id, state->c, code,
4431 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004432#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004433 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004434 return subs;
4435 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004436 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004437
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004438 // Do not add the state again when it exists with the same
4439 // positions.
Bram Moolenaar69b52452013-07-17 21:10:51 +02004440 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004441 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004442 }
4443
Bram Moolenaar688b3982019-02-13 21:47:36 +01004444 // When there are backreferences or PIMs the number of states may
4445 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004446 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004447 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004448 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004449 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004450 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004451
Bram Moolenaar688b3982019-02-13 21:47:36 +01004452 if ((long)(newsize >> 10) >= p_mmp)
4453 {
4454 emsg(_(e_maxmempat));
4455 --depth;
4456 return NULL;
4457 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004458 if (subs != &temp_subs)
4459 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004460 // "subs" may point into the current array, need to make a
4461 // copy before it becomes invalid.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004462 copy_sub(&temp_subs.norm, &subs->norm);
4463#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004464 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004465 copy_sub(&temp_subs.synt, &subs->synt);
4466#endif
4467 subs = &temp_subs;
4468 }
4469
Bram Moolenaar688b3982019-02-13 21:47:36 +01004470 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004471 if (newt == NULL)
4472 {
4473 // out of memory
4474 --depth;
4475 return NULL;
4476 }
4477 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004478 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004479 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004480
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004481 // add the state to the list
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004482 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004483 thread = &l->t[l->n++];
4484 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004485 if (pim == NULL)
4486 thread->pim.result = NFA_PIM_UNUSED;
4487 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004488 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004489 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004490 l->has_pim = TRUE;
4491 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004492 copy_sub(&thread->subs.norm, &subs->norm);
4493#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004494 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004495 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004496#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004497#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004498 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004499 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004500#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004501 }
4502
4503#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004504 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004505 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004506#endif
4507 switch (state->c)
4508 {
4509 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004510 break;
4511
4512 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004513 // order matters here
Bram Moolenaar16b35782016-09-09 20:29:50 +02004514 subs = addstate(l, state->out, subs, pim, off_arg);
4515 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004516 break;
4517
Bram Moolenaar699c1202013-09-25 16:41:54 +02004518 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 case NFA_NOPEN:
4520 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004521 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004522 break;
4523
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004524 case NFA_MOPEN:
4525 case NFA_MOPEN1:
4526 case NFA_MOPEN2:
4527 case NFA_MOPEN3:
4528 case NFA_MOPEN4:
4529 case NFA_MOPEN5:
4530 case NFA_MOPEN6:
4531 case NFA_MOPEN7:
4532 case NFA_MOPEN8:
4533 case NFA_MOPEN9:
4534#ifdef FEAT_SYN_HL
4535 case NFA_ZOPEN:
4536 case NFA_ZOPEN1:
4537 case NFA_ZOPEN2:
4538 case NFA_ZOPEN3:
4539 case NFA_ZOPEN4:
4540 case NFA_ZOPEN5:
4541 case NFA_ZOPEN6:
4542 case NFA_ZOPEN7:
4543 case NFA_ZOPEN8:
4544 case NFA_ZOPEN9:
4545#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004546 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004547 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004548 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004550 sub = &subs->norm;
4551 }
4552#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004553 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004554 {
4555 subidx = state->c - NFA_ZOPEN;
4556 sub = &subs->synt;
4557 }
4558#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004559 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004560 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004561 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004562 sub = &subs->norm;
4563 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004564
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004565 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004566 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004567 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004568
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004569 // Set the position (with "off" added) in the subexpression. Save
4570 // and restore it when it was in use. Otherwise fill any gap.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 if (REG_MULTI)
4572 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004573 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004574 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004575 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004576 save_in_use = -1;
4577 }
4578 else
4579 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004580 save_in_use = sub->in_use;
4581 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004582 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004583 sub->list.multi[i].start_lnum = -1;
4584 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004585 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004586 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004587 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004588 if (off == -1)
4589 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004590 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004591 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004592 }
4593 else
4594 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004595 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004596 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004597 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004598 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004599 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 }
4601 else
4602 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004603 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004604 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004605 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004606 save_in_use = -1;
4607 }
4608 else
4609 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004610 save_in_use = sub->in_use;
4611 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004612 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004613 sub->list.line[i].start = NULL;
4614 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004615 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004616 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004617 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004618 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004619 }
4620
Bram Moolenaar16b35782016-09-09 20:29:50 +02004621 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004622 if (subs == NULL)
4623 break;
4624 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004625#ifdef FEAT_SYN_HL
4626 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4627 sub = &subs->synt;
4628 else
4629#endif
4630 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004631
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004632 if (save_in_use == -1)
4633 {
4634 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004635 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004636 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004637 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004638 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004639 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004640 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004641 break;
4642
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004643 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004644 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004645 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004646 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004648 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004649 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004650 break;
4651 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004652 // FALLTHROUGH
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004653 case NFA_MCLOSE1:
4654 case NFA_MCLOSE2:
4655 case NFA_MCLOSE3:
4656 case NFA_MCLOSE4:
4657 case NFA_MCLOSE5:
4658 case NFA_MCLOSE6:
4659 case NFA_MCLOSE7:
4660 case NFA_MCLOSE8:
4661 case NFA_MCLOSE9:
4662#ifdef FEAT_SYN_HL
4663 case NFA_ZCLOSE:
4664 case NFA_ZCLOSE1:
4665 case NFA_ZCLOSE2:
4666 case NFA_ZCLOSE3:
4667 case NFA_ZCLOSE4:
4668 case NFA_ZCLOSE5:
4669 case NFA_ZCLOSE6:
4670 case NFA_ZCLOSE7:
4671 case NFA_ZCLOSE8:
4672 case NFA_ZCLOSE9:
4673#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004674 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004675 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004676 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004678 sub = &subs->norm;
4679 }
4680#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004681 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004682 {
4683 subidx = state->c - NFA_ZCLOSE;
4684 sub = &subs->synt;
4685 }
4686#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004687 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004688 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004689 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004690 sub = &subs->norm;
4691 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004692
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004693 // We don't fill in gaps here, there must have been an MOPEN that
4694 // has done that.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004695 save_in_use = sub->in_use;
4696 if (sub->in_use <= subidx)
4697 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 if (REG_MULTI)
4699 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004700 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004701 if (off == -1)
4702 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004703 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004704 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004705 }
4706 else
4707 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004708 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004709 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004710 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004711 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004712 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004713 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714 }
4715 else
4716 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004717 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004718 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004719 // avoid compiler warnings
Bram Moolenaard5638832016-09-09 17:59:50 +02004720 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004721 }
4722
Bram Moolenaar16b35782016-09-09 20:29:50 +02004723 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004724 if (subs == NULL)
4725 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004726 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004727#ifdef FEAT_SYN_HL
4728 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4729 sub = &subs->synt;
4730 else
4731#endif
4732 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733
4734 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004735 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004737 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004738 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004739 break;
4740 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004741 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004742 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743}
4744
4745/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004746 * Like addstate(), but the new state(s) are put at position "*ip".
4747 * Used for zero-width matches, next state to use is the added one.
4748 * This makes sure the order of states to be tried does not change, which
4749 * matters for alternatives.
4750 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004751 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004752addstate_here(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004753 nfa_list_T *l, // runtime state list
4754 nfa_state_T *state, // state to update
4755 regsubs_T *subs, // pointers to subexpressions
4756 nfa_pim_T *pim, // postponed look-behind match
Bram Moolenaar05540972016-01-30 20:31:25 +01004757 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004758{
4759 int tlen = l->n;
4760 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004761 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004762 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004763
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004764 // First add the state(s) at the end, so that we know how many there are.
4765 // Pass the listidx as offset (avoids adding another argument to
4766 // addstate().
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004767 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4768 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004769 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004770
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004771 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004772 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004773 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004774
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004775 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004776 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004777 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004778 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004779 if (count == 1)
4780 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004781 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004782 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004783 }
4784 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004785 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004786 if (l->n + count - 1 >= l->len)
4787 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004788 // not enough space to move the new states, reallocate the list
4789 // and move the states to the right position
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004790 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004791 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004792 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004793
Bram Moolenaar688b3982019-02-13 21:47:36 +01004794 if ((long)(newsize >> 10) >= p_mmp)
4795 {
4796 emsg(_(e_maxmempat));
4797 return NULL;
4798 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004799 newl = alloc(newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004800 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004801 return NULL;
4802 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004803 mch_memmove(&(newl[0]),
4804 &(l->t[0]),
4805 sizeof(nfa_thread_T) * listidx);
4806 mch_memmove(&(newl[listidx]),
4807 &(l->t[l->n - count]),
4808 sizeof(nfa_thread_T) * count);
4809 mch_memmove(&(newl[listidx + count]),
4810 &(l->t[listidx + 1]),
4811 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4812 vim_free(l->t);
4813 l->t = newl;
4814 }
4815 else
4816 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004817 // make space for new states, then move them from the
4818 // end to the current position
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004819 mch_memmove(&(l->t[listidx + count]),
4820 &(l->t[listidx + 1]),
4821 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4822 mch_memmove(&(l->t[listidx]),
4823 &(l->t[l->n - 1]),
4824 sizeof(nfa_thread_T) * count);
4825 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004826 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004827 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004828 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004829
4830 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004831}
4832
4833/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004834 * Check character class "class" against current character c.
4835 */
4836 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004837check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004838{
4839 switch (class)
4840 {
4841 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004842 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004843 return OK;
4844 break;
4845 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004846 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004847 return OK;
4848 break;
4849 case NFA_CLASS_BLANK:
4850 if (c == ' ' || c == '\t')
4851 return OK;
4852 break;
4853 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004854 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004855 return OK;
4856 break;
4857 case NFA_CLASS_DIGIT:
4858 if (VIM_ISDIGIT(c))
4859 return OK;
4860 break;
4861 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004862 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863 return OK;
4864 break;
4865 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004866 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004867 return OK;
4868 break;
4869 case NFA_CLASS_PRINT:
4870 if (vim_isprintc(c))
4871 return OK;
4872 break;
4873 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004874 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004875 return OK;
4876 break;
4877 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004878 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879 return OK;
4880 break;
4881 case NFA_CLASS_UPPER:
4882 if (MB_ISUPPER(c))
4883 return OK;
4884 break;
4885 case NFA_CLASS_XDIGIT:
4886 if (vim_isxdigit(c))
4887 return OK;
4888 break;
4889 case NFA_CLASS_TAB:
4890 if (c == '\t')
4891 return OK;
4892 break;
4893 case NFA_CLASS_RETURN:
4894 if (c == '\r')
4895 return OK;
4896 break;
4897 case NFA_CLASS_BACKSPACE:
4898 if (c == '\b')
4899 return OK;
4900 break;
4901 case NFA_CLASS_ESCAPE:
4902 if (c == '\033')
4903 return OK;
4904 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004905 case NFA_CLASS_IDENT:
4906 if (vim_isIDc(c))
4907 return OK;
4908 break;
4909 case NFA_CLASS_KEYWORD:
4910 if (reg_iswordc(c))
4911 return OK;
4912 break;
4913 case NFA_CLASS_FNAME:
4914 if (vim_isfilec(c))
4915 return OK;
4916 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917
4918 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004919 // should not be here :P
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004920 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004921 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922 }
4923 return FAIL;
4924}
4925
Bram Moolenaar5714b802013-05-28 22:03:20 +02004926/*
4927 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004928 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004929 */
4930 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004931match_backref(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004932 regsub_T *sub, // pointers to subexpressions
Bram Moolenaar05540972016-01-30 20:31:25 +01004933 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004934 int *bytelen) // out: length of match in bytes
Bram Moolenaar5714b802013-05-28 22:03:20 +02004935{
4936 int len;
4937
4938 if (sub->in_use <= subidx)
4939 {
4940retempty:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004941 // backref was not set, match an empty string
Bram Moolenaar5714b802013-05-28 22:03:20 +02004942 *bytelen = 0;
4943 return TRUE;
4944 }
4945
4946 if (REG_MULTI)
4947 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004948 if (sub->list.multi[subidx].start_lnum < 0
4949 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004950 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004951 if (sub->list.multi[subidx].start_lnum == rex.lnum
4952 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004953 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004954 len = sub->list.multi[subidx].end_col
4955 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004956 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4957 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004958 {
4959 *bytelen = len;
4960 return TRUE;
4961 }
4962 }
4963 else
4964 {
4965 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004966 sub->list.multi[subidx].start_lnum,
4967 sub->list.multi[subidx].start_col,
4968 sub->list.multi[subidx].end_lnum,
4969 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004970 bytelen) == RA_MATCH)
4971 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004972 }
4973 }
4974 else
4975 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004976 if (sub->list.line[subidx].start == NULL
4977 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004978 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004979 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02004980 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004981 {
4982 *bytelen = len;
4983 return TRUE;
4984 }
4985 }
4986 return FALSE;
4987}
4988
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004989#ifdef FEAT_SYN_HL
4990
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004991/*
4992 * Check for a match with \z subexpression "subidx".
4993 * Return TRUE if it matches.
4994 */
4995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004996match_zref(
4997 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004998 int *bytelen) // out: length of match in bytes
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004999{
5000 int len;
5001
5002 cleanup_zsubexpr();
5003 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5004 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005005 // backref was not set, match an empty string
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005006 *bytelen = 0;
5007 return TRUE;
5008 }
5009
5010 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005011 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005012 {
5013 *bytelen = len;
5014 return TRUE;
5015 }
5016 return FALSE;
5017}
5018#endif
5019
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005020/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005021 * Save list IDs for all NFA states of "prog" into "list".
5022 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005023 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005024 */
5025 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005026nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005027{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005028 int i;
5029 nfa_state_T *p;
5030
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005031 // Order in the list is reverse, it's a bit faster that way.
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005032 p = &prog->state[0];
5033 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005034 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005035 list[i] = p->lastlist[1];
5036 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005037 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005038 }
5039}
5040
5041/*
5042 * Restore list IDs from "list" to all NFA states.
5043 */
5044 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005045nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005046{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005047 int i;
5048 nfa_state_T *p;
5049
5050 p = &prog->state[0];
5051 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005052 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005053 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005054 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 }
5056}
5057
Bram Moolenaar423532e2013-05-29 21:14:42 +02005058 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005059nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005060{
5061 if (op == 1) return pos > val;
5062 if (op == 2) return pos < val;
5063 return val == pos;
5064}
5065
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005066static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005067
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005069 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005070 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5071 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005072 */
5073 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005074recursive_regmatch(
5075 nfa_state_T *state,
5076 nfa_pim_T *pim,
5077 nfa_regprog_T *prog,
5078 regsubs_T *submatch,
5079 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005080 int **listids,
5081 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005082{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005083 int save_reginput_col = (int)(rex.input - rex.line);
5084 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005085 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005086 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005087 save_se_T *save_nfa_endp = nfa_endp;
5088 save_se_T endpos;
5089 save_se_T *endposp = NULL;
5090 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005091 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005093 if (pim != NULL)
5094 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005095 // start at the position where the postponed match was
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005096 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005097 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005098 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005099 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005100 }
5101
Bram Moolenaardecd9542013-06-07 16:31:50 +02005102 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005103 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5104 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5105 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005106 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005107 // The recursive match must end at the current position. When "pim" is
5108 // not NULL it specifies the current position.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005109 endposp = &endpos;
5110 if (REG_MULTI)
5111 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005112 if (pim == NULL)
5113 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005114 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5115 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005116 }
5117 else
5118 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005119 }
5120 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005121 {
5122 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005123 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005124 else
5125 endpos.se_u.ptr = pim->end.ptr;
5126 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005127
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005128 // Go back the specified number of bytes, or as far as the
5129 // start of the previous line, to try matching "\@<=" or
5130 // not matching "\@<!". This is very inefficient, limit the number of
5131 // bytes if possible.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005132 if (state->val <= 0)
5133 {
5134 if (REG_MULTI)
5135 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005136 rex.line = reg_getline(--rex.lnum);
5137 if (rex.line == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005138 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005139 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005140 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005141 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005142 }
5143 else
5144 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005145 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005146 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005147 // Not enough bytes in this line, go to end of
5148 // previous line.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005149 rex.line = reg_getline(--rex.lnum);
5150 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005152 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005153 rex.line = reg_getline(++rex.lnum);
5154 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005155 }
5156 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005157 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005158 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005159 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005160 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005161 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005162 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005163 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005164 }
5165 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005166 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005167 }
5168 }
5169
Bram Moolenaarf46da702013-06-02 22:37:42 +02005170#ifdef ENABLE_LOG
5171 if (log_fd != stderr)
5172 fclose(log_fd);
5173 log_fd = NULL;
5174#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005175 // Have to clear the lastlist field of the NFA nodes, so that
5176 // nfa_regmatch() and addstate() can run properly after recursion.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005177 if (nfa_ll_index == 1)
5178 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005179 // Already calling nfa_regmatch() recursively. Save the lastlist[1]
5180 // values and clear them.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005181 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005182 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005183 vim_free(*listids);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005184 *listids = ALLOC_MULT(int, prog->nstate);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005185 if (*listids == NULL)
5186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005187 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005188 return 0;
5189 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005190 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005191 }
5192 nfa_save_listids(prog, *listids);
5193 need_restore = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005194 // any value of rex.nfa_listid will do
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005195 }
5196 else
5197 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005198 // First recursive nfa_regmatch() call, switch to the second lastlist
5199 // entry. Make sure rex.nfa_listid is different from a previous
5200 // recursive call, because some states may still have this ID.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005201 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005202 if (rex.nfa_listid <= rex.nfa_alt_listid)
5203 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005204 }
5205
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005206 // Call nfa_regmatch() to check if the current concat matches at this
5207 // position. The concat ends with the node NFA_END_INVISIBLE
Bram Moolenaarf46da702013-06-02 22:37:42 +02005208 nfa_endp = endposp;
5209 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005210
5211 if (need_restore)
5212 nfa_restore_listids(prog, *listids);
5213 else
5214 {
5215 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005216 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005217 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005218
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005219 // restore position in input text
Bram Moolenaar0270f382018-07-17 05:43:58 +02005220 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005221 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005222 rex.line = reg_getline(rex.lnum);
5223 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005224 if (result != NFA_TOO_EXPENSIVE)
5225 {
5226 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005227 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005228 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005229 nfa_endp = save_nfa_endp;
5230
5231#ifdef ENABLE_LOG
5232 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5233 if (log_fd != NULL)
5234 {
5235 fprintf(log_fd, "****************************\n");
5236 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5237 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5238 fprintf(log_fd, "****************************\n");
5239 }
5240 else
5241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005242 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005243 log_fd = stderr;
5244 }
5245#endif
5246
5247 return result;
5248}
5249
Bram Moolenaara2d95102013-06-04 14:23:05 +02005250/*
5251 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005252 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005253 * NFA_ANY: 1
5254 * specific character: 99
5255 */
5256 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005257failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005258{
5259 int c = state->c;
5260 int l, r;
5261
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005262 // detect looping
Bram Moolenaara2d95102013-06-04 14:23:05 +02005263 if (depth > 4)
5264 return 1;
5265
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005266 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005267 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005268 case NFA_SPLIT:
5269 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005270 // avoid recursive stuff
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005271 return 1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005272 // two alternatives, use the lowest failure chance
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005273 l = failure_chance(state->out, depth + 1);
5274 r = failure_chance(state->out1, depth + 1);
5275 return l < r ? l : r;
5276
5277 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005278 // matches anything, unlikely to fail
Bram Moolenaara2d95102013-06-04 14:23:05 +02005279 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005280
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005281 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005282 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005283 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005284 // empty match works always
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005285 return 0;
5286
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005287 case NFA_START_INVISIBLE:
5288 case NFA_START_INVISIBLE_FIRST:
5289 case NFA_START_INVISIBLE_NEG:
5290 case NFA_START_INVISIBLE_NEG_FIRST:
5291 case NFA_START_INVISIBLE_BEFORE:
5292 case NFA_START_INVISIBLE_BEFORE_FIRST:
5293 case NFA_START_INVISIBLE_BEFORE_NEG:
5294 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5295 case NFA_START_PATTERN:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005296 // recursive regmatch is expensive, use low failure chance
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005297 return 5;
5298
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005299 case NFA_BOL:
5300 case NFA_EOL:
5301 case NFA_BOF:
5302 case NFA_EOF:
5303 case NFA_NEWL:
5304 return 99;
5305
5306 case NFA_BOW:
5307 case NFA_EOW:
5308 return 90;
5309
5310 case NFA_MOPEN:
5311 case NFA_MOPEN1:
5312 case NFA_MOPEN2:
5313 case NFA_MOPEN3:
5314 case NFA_MOPEN4:
5315 case NFA_MOPEN5:
5316 case NFA_MOPEN6:
5317 case NFA_MOPEN7:
5318 case NFA_MOPEN8:
5319 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005320#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005321 case NFA_ZOPEN:
5322 case NFA_ZOPEN1:
5323 case NFA_ZOPEN2:
5324 case NFA_ZOPEN3:
5325 case NFA_ZOPEN4:
5326 case NFA_ZOPEN5:
5327 case NFA_ZOPEN6:
5328 case NFA_ZOPEN7:
5329 case NFA_ZOPEN8:
5330 case NFA_ZOPEN9:
5331 case NFA_ZCLOSE:
5332 case NFA_ZCLOSE1:
5333 case NFA_ZCLOSE2:
5334 case NFA_ZCLOSE3:
5335 case NFA_ZCLOSE4:
5336 case NFA_ZCLOSE5:
5337 case NFA_ZCLOSE6:
5338 case NFA_ZCLOSE7:
5339 case NFA_ZCLOSE8:
5340 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005341#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005342 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005343 case NFA_MCLOSE1:
5344 case NFA_MCLOSE2:
5345 case NFA_MCLOSE3:
5346 case NFA_MCLOSE4:
5347 case NFA_MCLOSE5:
5348 case NFA_MCLOSE6:
5349 case NFA_MCLOSE7:
5350 case NFA_MCLOSE8:
5351 case NFA_MCLOSE9:
5352 case NFA_NCLOSE:
5353 return failure_chance(state->out, depth + 1);
5354
5355 case NFA_BACKREF1:
5356 case NFA_BACKREF2:
5357 case NFA_BACKREF3:
5358 case NFA_BACKREF4:
5359 case NFA_BACKREF5:
5360 case NFA_BACKREF6:
5361 case NFA_BACKREF7:
5362 case NFA_BACKREF8:
5363 case NFA_BACKREF9:
5364#ifdef FEAT_SYN_HL
5365 case NFA_ZREF1:
5366 case NFA_ZREF2:
5367 case NFA_ZREF3:
5368 case NFA_ZREF4:
5369 case NFA_ZREF5:
5370 case NFA_ZREF6:
5371 case NFA_ZREF7:
5372 case NFA_ZREF8:
5373 case NFA_ZREF9:
5374#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005375 // backreferences don't match in many places
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005376 return 94;
5377
5378 case NFA_LNUM_GT:
5379 case NFA_LNUM_LT:
5380 case NFA_COL_GT:
5381 case NFA_COL_LT:
5382 case NFA_VCOL_GT:
5383 case NFA_VCOL_LT:
5384 case NFA_MARK_GT:
5385 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005386 case NFA_VISUAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005387 // before/after positions don't match very often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005388 return 85;
5389
5390 case NFA_LNUM:
5391 return 90;
5392
5393 case NFA_CURSOR:
5394 case NFA_COL:
5395 case NFA_VCOL:
5396 case NFA_MARK:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005397 // specific positions rarely match
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005398 return 98;
5399
5400 case NFA_COMPOSING:
5401 return 95;
5402
5403 default:
5404 if (c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005405 // character match fails often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005406 return 95;
5407 }
5408
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005409 // something else, includes character classes
Bram Moolenaara2d95102013-06-04 14:23:05 +02005410 return 50;
5411}
5412
Bram Moolenaarf46da702013-06-02 22:37:42 +02005413/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005414 * Skip until the char "c" we know a match must start with.
5415 */
5416 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005417skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005418{
5419 char_u *s;
5420
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005421 // Used often, do some work to avoid call overhead.
Bram Moolenaara12a1612019-01-24 16:39:02 +01005422 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005423 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005424 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005425 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005426 if (s == NULL)
5427 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005428 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005429 return OK;
5430}
5431
5432/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005433 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005434 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005435 * Returns zero for no match, 1 for a match.
5436 */
5437 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005438find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005439{
5440 colnr_T col = startcol;
5441 int c1, c2;
5442 int len1, len2;
5443 int match;
5444
5445 for (;;)
5446 {
5447 match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005448 len2 = MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005449 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5450 {
5451 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005452 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005453 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005454 {
5455 match = FALSE;
5456 break;
5457 }
5458 len2 += MB_CHAR2LEN(c2);
5459 }
5460 if (match
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005461 // check that no composing char follows
Bram Moolenaar473de612013-06-08 18:19:48 +02005462 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005463 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005464 {
5465 cleanup_subexpr();
5466 if (REG_MULTI)
5467 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005468 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005469 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005470 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005471 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005472 }
5473 else
5474 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005475 rex.reg_startp[0] = rex.line + col;
5476 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005477 }
5478 return 1L;
5479 }
5480
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005481 // Try finding regstart after the current match.
5482 col += MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005483 if (skip_to_start(regstart, &col) == FAIL)
5484 break;
5485 }
5486 return 0L;
5487}
5488
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005489#ifdef FEAT_RELTIME
5490 static int
5491nfa_did_time_out()
5492{
5493 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5494 {
5495 if (nfa_timed_out != NULL)
5496 *nfa_timed_out = TRUE;
5497 return TRUE;
5498 }
5499 return FALSE;
5500}
5501#endif
5502
Bram Moolenaar473de612013-06-08 18:19:48 +02005503/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005504 * Main matching routine.
5505 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005506 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005507 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005508 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005509 *
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005510 * Return TRUE if there is a match, FALSE if there is no match,
5511 * NFA_TOO_EXPENSIVE if we end up with too many states.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005512 * When there is a match "submatch" contains the positions.
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005513 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005514 * Note: Caller must ensure that: start != NULL.
5515 */
5516 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005517nfa_regmatch(
5518 nfa_regprog_T *prog,
5519 nfa_state_T *start,
5520 regsubs_T *submatch,
5521 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005522{
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005523 int result = FALSE;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005524 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005525 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005526 int go_to_nextline = FALSE;
5527 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005528 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005529 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005530 nfa_list_T *thislist;
5531 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005532 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005533 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005534 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005535 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005536 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005537 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005538 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005539 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005540#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005541 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005542#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005543
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005544 // Some patterns may take a long time to match, especially when using
5545 // recursive_regmatch(). Allow interrupting them with CTRL-C.
Bram Moolenaar41f12052013-08-25 17:01:42 +02005546 fast_breakcheck();
5547 if (got_int)
5548 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005549#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005550 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005551 return FALSE;
5552#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005553
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005554#ifdef NFA_REGEXP_DEBUG_LOG
5555 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5556 if (debug == NULL)
5557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005558 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005559 return FALSE;
5560 }
5561#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005562 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005564 // Allocate memory for the lists of nodes.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005565 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005566
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005567 list[0].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005568 list[0].len = prog->nstate + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005569 list[1].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005570 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005571 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005573
5574#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005575 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005576 if (log_fd != NULL)
5577 {
5578 fprintf(log_fd, "**********************************\n");
5579 nfa_set_code(start->c);
5580 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5581 abs(start->id), code);
5582 fprintf(log_fd, "**********************************\n");
5583 }
5584 else
5585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005586 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005587 log_fd = stderr;
5588 }
5589#endif
5590
5591 thislist = &list[0];
5592 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005593 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005594 nextlist = &list[1];
5595 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005596 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005598 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005600 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005601
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005602 // Inline optimized code for addstate(thislist, start, m, 0) if we know
5603 // it's the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005604 if (toplevel)
5605 {
5606 if (REG_MULTI)
5607 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005608 m->norm.list.multi[0].start_lnum = rex.lnum;
5609 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005610 }
5611 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005612 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005613 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005614 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005615 }
5616 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005617 r = addstate(thislist, start, m, NULL, 0);
5618 if (r == NULL)
5619 {
5620 nfa_match = NFA_TOO_EXPENSIVE;
5621 goto theend;
5622 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005623
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005624#define ADD_STATE_IF_MATCH(state) \
5625 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005626 add_state = state->out; \
5627 add_off = clen; \
5628 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005629
5630 /*
5631 * Run for each character.
5632 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005633 for (;;)
5634 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005635 int curc;
5636 int clen;
5637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005638 if (has_mbyte)
5639 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005640 curc = (*mb_ptr2char)(rex.input);
5641 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005642 }
5643 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005644 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005645 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005646 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005647 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005648 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005649 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005650 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005651 go_to_nextline = FALSE;
5652 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005654 // swap lists
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005655 thislist = &list[flag];
5656 nextlist = &list[flag ^= 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005657 nextlist->n = 0; // clear nextlist
Bram Moolenaar196ed142013-07-21 18:59:24 +02005658 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005659 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005660 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005661 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005662# ifdef FEAT_EVAL
5663 || nfa_fail_for_testing
5664# endif
5665 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005666 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005667 // too many states, retry with old engine
Bram Moolenaarfda37292014-11-05 14:27:36 +01005668 nfa_match = NFA_TOO_EXPENSIVE;
5669 goto theend;
5670 }
5671
Bram Moolenaar0270f382018-07-17 05:43:58 +02005672 thislist->id = rex.nfa_listid;
5673 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674
5675#ifdef ENABLE_LOG
5676 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005677 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005678 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005679 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005680 {
5681 int i;
5682
5683 for (i = 0; i < thislist->n; i++)
5684 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5685 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005686 fprintf(log_fd, "\n");
5687#endif
5688
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005689#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005690 fprintf(debug, "\n-------------------\n");
5691#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005692 /*
5693 * If the state lists are empty we can stop.
5694 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005695 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005696 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005697
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005698 // compute nextlist
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005699 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005700 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005701 // If the list gets very long there probably is something wrong.
5702 // At least allow interrupting with CTRL-C.
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005703 fast_breakcheck();
5704 if (got_int)
5705 break;
5706#ifdef FEAT_RELTIME
5707 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5708 {
5709 nfa_time_count = 0;
5710 if (nfa_did_time_out())
5711 break;
5712 }
5713#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005714 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005715
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005716#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005717 nfa_set_code(t->state->c);
5718 fprintf(debug, "%s, ", code);
5719#endif
5720#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005721 {
5722 int col;
5723
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005724 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005725 col = -1;
5726 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005727 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005728 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005729 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005730 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005731 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005732 abs(t->state->id), (int)t->state->c, code, col,
5733 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005734 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005735#endif
5736
5737 /*
5738 * Handle the possible codes of the current state.
5739 * The most important is NFA_MATCH.
5740 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005741 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005742 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005743 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005744 switch (t->state->c)
5745 {
5746 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005747 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005748 // If the match ends before a composing characters and
5749 // rex.reg_icombine is not set, that is not really a match.
Bram Moolenaar6100d022016-10-02 16:51:57 +02005750 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005751 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005752
Bram Moolenaar963fee22013-05-26 21:47:28 +02005753 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005754 copy_sub(&submatch->norm, &t->subs.norm);
5755#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005756 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005757 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005758#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005759#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005760 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005761#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005762 // Found the left-most longest match, do not look at any other
5763 // states at this position. When the list of states is going
5764 // to be empty quit without advancing, so that "rex.input" is
5765 // correct.
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005766 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005767 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005768 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005769 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005770
5771 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005772 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005773 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005774 /*
5775 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005776 * NFA_START_INVISIBLE_BEFORE node.
5777 * They surround a zero-width group, used with "\@=", "\&",
5778 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005779 * If we got here, it means that the current "invisible" group
5780 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005781 * nfa_regmatch(). For a look-behind match only when it ends
5782 * in the position in "nfa_endp".
5783 * Submatches are stored in *m, and used in the parent call.
5784 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005785#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005786 if (nfa_endp != NULL)
5787 {
5788 if (REG_MULTI)
5789 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005790 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005791 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005792 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005793 nfa_endp->se_u.pos.col);
5794 else
5795 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005796 (int)(rex.input - rex.line),
5797 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005798 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005799#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005800 // If "nfa_endp" is set it's only a match if it ends at
5801 // "nfa_endp"
Bram Moolenaarf46da702013-06-02 22:37:42 +02005802 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005803 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5804 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005805 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005806 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005807 break;
5808
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005809 // do not set submatches for \@!
Bram Moolenaardecd9542013-06-07 16:31:50 +02005810 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005811 {
5812 copy_sub(&m->norm, &t->subs.norm);
5813#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005814 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005815 copy_sub(&m->synt, &t->subs.synt);
5816#endif
5817 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005818#ifdef ENABLE_LOG
5819 fprintf(log_fd, "Match found:\n");
5820 log_subsexpr(m);
5821#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005822 nfa_match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005823 // See comment above at "goto nextchar".
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005824 if (nextlist->n == 0)
5825 clen = 0;
5826 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005827
5828 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005829 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005830 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005831 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005832 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005833 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005834 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005835 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005836 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005837#ifdef ENABLE_LOG
5838 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5839 failure_chance(t->state->out, 0),
5840 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005841#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005842 // Do it directly if there already is a PIM or when
5843 // nfa_postprocess() detected it will work better.
Bram Moolenaara2947e22013-06-11 22:44:09 +02005844 if (t->pim.result != NFA_PIM_UNUSED
5845 || t->state->c == NFA_START_INVISIBLE_FIRST
5846 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5847 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5848 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005849 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005850 int in_use = m->norm.in_use;
5851
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005852 // Copy submatch info for the recursive call, opposite
5853 // of what happens on success below.
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005854 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005855#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005856 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005857 copy_sub_off(&m->synt, &t->subs.synt);
5858#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005859
Bram Moolenaara2d95102013-06-04 14:23:05 +02005860 /*
5861 * First try matching the invisible match, then what
5862 * follows.
5863 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005864 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005865 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005866 if (result == NFA_TOO_EXPENSIVE)
5867 {
5868 nfa_match = result;
5869 goto theend;
5870 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005871
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005872 // for \@! and \@<! it is a match when the result is
5873 // FALSE
Bram Moolenaardecd9542013-06-07 16:31:50 +02005874 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005875 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5876 || t->state->c
5877 == NFA_START_INVISIBLE_BEFORE_NEG
5878 || t->state->c
5879 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005880 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005881 // Copy submatch info from the recursive call
Bram Moolenaara2d95102013-06-04 14:23:05 +02005882 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005883#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005884 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005885 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005886#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005887 // If the pattern has \ze and it matched in the
5888 // sub pattern, use it.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005889 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005890
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005891 // t->state->out1 is the corresponding
5892 // END_INVISIBLE node; Add its out to the current
5893 // list (zero-width match).
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005894 add_here = TRUE;
5895 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005896 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005897 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005898 }
5899 else
5900 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005901 nfa_pim_T pim;
5902
Bram Moolenaara2d95102013-06-04 14:23:05 +02005903 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005904 * First try matching what follows. Only if a match
5905 * is found verify the invisible match matches. Add a
5906 * nfa_pim_T to the following states, it contains info
5907 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005908 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005909 pim.state = t->state;
5910 pim.result = NFA_PIM_TODO;
5911 pim.subs.norm.in_use = 0;
5912#ifdef FEAT_SYN_HL
5913 pim.subs.synt.in_use = 0;
5914#endif
5915 if (REG_MULTI)
5916 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005917 pim.end.pos.col = (int)(rex.input - rex.line);
5918 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005919 }
5920 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005921 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005922
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005923 // t->state->out1 is the corresponding END_INVISIBLE
5924 // node; Add its out to the current list (zero-width
5925 // match).
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005926 if (addstate_here(thislist, t->state->out1->out,
5927 &t->subs, &pim, &listidx) == NULL)
5928 {
5929 nfa_match = NFA_TOO_EXPENSIVE;
5930 goto theend;
5931 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005933 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005934 break;
5935
Bram Moolenaar87953742013-06-05 18:52:40 +02005936 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005937 {
5938 nfa_state_T *skip = NULL;
5939#ifdef ENABLE_LOG
5940 int skip_lid = 0;
5941#endif
5942
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005943 // There is no point in trying to match the pattern if the
5944 // output state is not going to be added to the list.
Bram Moolenaar43e02982013-06-07 17:31:29 +02005945 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5946 {
5947 skip = t->state->out1->out;
5948#ifdef ENABLE_LOG
5949 skip_lid = nextlist->id;
5950#endif
5951 }
5952 else if (state_in_list(nextlist,
5953 t->state->out1->out->out, &t->subs))
5954 {
5955 skip = t->state->out1->out->out;
5956#ifdef ENABLE_LOG
5957 skip_lid = nextlist->id;
5958#endif
5959 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005960 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005961 t->state->out1->out->out, &t->subs))
5962 {
5963 skip = t->state->out1->out->out;
5964#ifdef ENABLE_LOG
5965 skip_lid = thislist->id;
5966#endif
5967 }
5968 if (skip != NULL)
5969 {
5970#ifdef ENABLE_LOG
5971 nfa_set_code(skip->c);
5972 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5973 abs(skip->id), skip_lid, skip->c, code);
5974#endif
5975 break;
5976 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005977 // Copy submatch info to the recursive call, opposite of what
5978 // happens afterwards.
Bram Moolenaar699c1202013-09-25 16:41:54 +02005979 copy_sub_off(&m->norm, &t->subs.norm);
5980#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005981 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005982 copy_sub_off(&m->synt, &t->subs.synt);
5983#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005984
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005985 // First try matching the pattern.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005986 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005987 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005988 if (result == NFA_TOO_EXPENSIVE)
5989 {
5990 nfa_match = result;
5991 goto theend;
5992 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005993 if (result)
5994 {
5995 int bytelen;
5996
5997#ifdef ENABLE_LOG
5998 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5999 log_subsexpr(m);
6000#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006001 // Copy submatch info from the recursive call
Bram Moolenaar87953742013-06-05 18:52:40 +02006002 copy_sub_off(&t->subs.norm, &m->norm);
6003#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006004 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006005 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006006#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006007 // Now we need to skip over the matched text and then
6008 // continue with what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02006009 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006010 // TODO: multi-line match
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006011 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006012 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006013 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006014 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006015
6016#ifdef ENABLE_LOG
6017 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6018#endif
6019 if (bytelen == 0)
6020 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006021 // empty match, output of corresponding
6022 // NFA_END_PATTERN/NFA_SKIP to be used at current
6023 // position
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006024 add_here = TRUE;
6025 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006026 }
6027 else if (bytelen <= clen)
6028 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006029 // match current character, output of corresponding
6030 // NFA_END_PATTERN to be used at next position.
Bram Moolenaar87953742013-06-05 18:52:40 +02006031 add_state = t->state->out1->out->out;
6032 add_off = clen;
6033 }
6034 else
6035 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006036 // skip over the matched characters, set character
6037 // count in NFA_SKIP
Bram Moolenaar87953742013-06-05 18:52:40 +02006038 add_state = t->state->out1->out;
6039 add_off = bytelen;
6040 add_count = bytelen - clen;
6041 }
6042 }
6043 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006044 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006045
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006046 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006047 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006048 {
6049 add_here = TRUE;
6050 add_state = t->state->out;
6051 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006052 break;
6053
6054 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006055 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006056 {
6057 add_here = TRUE;
6058 add_state = t->state->out;
6059 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006060 break;
6061
6062 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006063 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006064
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006065 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006066 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 else if (has_mbyte)
6068 {
6069 int this_class;
6070
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006071 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006072 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006073 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006074 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006075 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006076 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006077 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006078 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006079 || (rex.input > rex.line
6080 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006081 result = FALSE;
6082 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006083 {
6084 add_here = TRUE;
6085 add_state = t->state->out;
6086 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006087 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006088
6089 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006090 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006091 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006092 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 else if (has_mbyte)
6094 {
6095 int this_class, prev_class;
6096
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006097 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006098 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006099 prev_class = reg_prev_class();
6100 if (this_class == prev_class
6101 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006102 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006103 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006104 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6105 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006106 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006107 result = FALSE;
6108 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006109 {
6110 add_here = TRUE;
6111 add_state = t->state->out;
6112 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006113 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006114
Bram Moolenaar4b780632013-05-31 22:14:52 +02006115 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006116 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006117 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006118 {
6119 add_here = TRUE;
6120 add_state = t->state->out;
6121 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006122 break;
6123
6124 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006125 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006126 {
6127 add_here = TRUE;
6128 add_state = t->state->out;
6129 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006130 break;
6131
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006132 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006133 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006134 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006135 int len = 0;
6136 nfa_state_T *end;
6137 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006138 int cchars[MAX_MCO];
6139 int ccount = 0;
6140 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006141
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006142 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006143 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006144 if (utf_iscomposing(sta->c))
6145 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006146 // Only match composing character(s), ignore base
6147 // character. Used for ".{composing}" and "{composing}"
6148 // (no preceding character).
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006149 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006150 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006151 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006152 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006153 // If \Z was present, then ignore composing characters.
6154 // When ignoring the base character this always matches.
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006155 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006156 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006157 else
6158 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006159 while (sta->c != NFA_END_COMPOSING)
6160 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006161 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006162
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006163 // Check base character matches first, unless ignored.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006164 else if (len > 0 || mc == sta->c)
6165 {
6166 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006167 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006168 len += mb_char2len(mc);
6169 sta = sta->out;
6170 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006172 // We don't care about the order of composing characters.
6173 // Get them into cchars[] first.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006174 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006175 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006176 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006177 cchars[ccount++] = mc;
6178 len += mb_char2len(mc);
6179 if (ccount == MAX_MCO)
6180 break;
6181 }
6182
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006183 // Check that each composing char in the pattern matches a
6184 // composing char in the text. We do not check if all
6185 // composing chars are matched.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006186 result = OK;
6187 while (sta->c != NFA_END_COMPOSING)
6188 {
6189 for (j = 0; j < ccount; ++j)
6190 if (cchars[j] == sta->c)
6191 break;
6192 if (j == ccount)
6193 {
6194 result = FAIL;
6195 break;
6196 }
6197 sta = sta->out;
6198 }
6199 }
6200 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006201 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006202
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006203 end = t->state->out1; // NFA_END_COMPOSING
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006204 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006205 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006206 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006207
6208 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006209 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006210 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006211 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006212 go_to_nextline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006213 // Pass -1 for the offset, which means taking the position
6214 // at the start of the next line.
Bram Moolenaara2d95102013-06-04 14:23:05 +02006215 add_state = t->state->out;
6216 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006217 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006218 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006219 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006220 // match \n as if it is an ordinary character
Bram Moolenaara2d95102013-06-04 14:23:05 +02006221 add_state = t->state->out;
6222 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006223 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006224 break;
6225
Bram Moolenaar417bad22013-06-07 14:08:30 +02006226 case NFA_START_COLL:
6227 case NFA_START_NEG_COLL:
6228 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006229 // What follows is a list of characters, until NFA_END_COLL.
6230 // One of them must match or none of them must match.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006231 nfa_state_T *state;
6232 int result_if_matched;
6233 int c1, c2;
6234
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006235 // Never match EOL. If it's part of the collection it is added
6236 // as a separate state with an OR.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006237 if (curc == NUL)
6238 break;
6239
6240 state = t->state->out;
6241 result_if_matched = (t->state->c == NFA_START_COLL);
6242 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006243 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006244 if (state->c == NFA_END_COLL)
6245 {
6246 result = !result_if_matched;
6247 break;
6248 }
6249 if (state->c == NFA_RANGE_MIN)
6250 {
6251 c1 = state->val;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006252 state = state->out; // advance to NFA_RANGE_MAX
Bram Moolenaar417bad22013-06-07 14:08:30 +02006253 c2 = state->val;
6254#ifdef ENABLE_LOG
6255 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6256 curc, c1, c2);
6257#endif
6258 if (curc >= c1 && curc <= c2)
6259 {
6260 result = result_if_matched;
6261 break;
6262 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006263 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006264 {
6265 int curc_low = MB_TOLOWER(curc);
6266 int done = FALSE;
6267
6268 for ( ; c1 <= c2; ++c1)
6269 if (MB_TOLOWER(c1) == curc_low)
6270 {
6271 result = result_if_matched;
6272 done = TRUE;
6273 break;
6274 }
6275 if (done)
6276 break;
6277 }
6278 }
6279 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006280 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006281 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006282 == MB_TOLOWER(state->c))))
6283 {
6284 result = result_if_matched;
6285 break;
6286 }
6287 state = state->out;
6288 }
6289 if (result)
6290 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006291 // next state is in out of the NFA_END_COLL, out1 of
6292 // START points to the END state
Bram Moolenaar417bad22013-06-07 14:08:30 +02006293 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006294 add_off = clen;
6295 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006296 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006297 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006298
6299 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006300 // Any char except '\0', (end of input) does not match.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006301 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006302 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006303 add_state = t->state->out;
6304 add_off = clen;
6305 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006306 break;
6307
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006308 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006309 // On a composing character skip over it. Otherwise do
6310 // nothing. Always matches.
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006311 if (enc_utf8 && utf_iscomposing(curc))
6312 {
6313 add_off = clen;
6314 }
6315 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006316 {
6317 add_here = TRUE;
6318 add_off = 0;
6319 }
6320 add_state = t->state->out;
6321 break;
6322
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323 /*
6324 * Character classes like \a for alpha, \d for digit etc.
6325 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006326 case NFA_IDENT: // \i
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006327 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006328 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329 break;
6330
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006331 case NFA_SIDENT: // \I
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006332 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006333 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006334 break;
6335
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006336 case NFA_KWORD: // \k
Bram Moolenaar0270f382018-07-17 05:43:58 +02006337 result = vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006338 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006339 break;
6340
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006341 case NFA_SKWORD: // \K
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006342 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006343 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006344 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006345 break;
6346
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006347 case NFA_FNAME: // \f
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006348 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006349 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006350 break;
6351
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006352 case NFA_SFNAME: // \F
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006353 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006354 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006355 break;
6356
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006357 case NFA_PRINT: // \p
Bram Moolenaar0270f382018-07-17 05:43:58 +02006358 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006359 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006360 break;
6361
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006362 case NFA_SPRINT: // \P
Bram Moolenaar0270f382018-07-17 05:43:58 +02006363 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006364 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006365 break;
6366
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006367 case NFA_WHITE: // \s
Bram Moolenaar1c465442017-03-12 20:10:05 +01006368 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006369 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006370 break;
6371
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006372 case NFA_NWHITE: // \S
Bram Moolenaar1c465442017-03-12 20:10:05 +01006373 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006374 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006375 break;
6376
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006377 case NFA_DIGIT: // \d
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006378 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006379 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006380 break;
6381
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006382 case NFA_NDIGIT: // \D
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006383 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006384 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006385 break;
6386
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006387 case NFA_HEX: // \x
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006388 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006389 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006390 break;
6391
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006392 case NFA_NHEX: // \X
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006393 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006394 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006395 break;
6396
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006397 case NFA_OCTAL: // \o
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006398 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006399 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006400 break;
6401
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006402 case NFA_NOCTAL: // \O
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006403 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006404 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006405 break;
6406
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006407 case NFA_WORD: // \w
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006408 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006409 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006410 break;
6411
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006412 case NFA_NWORD: // \W
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006413 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006414 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006415 break;
6416
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006417 case NFA_HEAD: // \h
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006418 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006419 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006420 break;
6421
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006422 case NFA_NHEAD: // \H
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006423 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006424 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 break;
6426
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006427 case NFA_ALPHA: // \a
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006428 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006429 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006430 break;
6431
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006432 case NFA_NALPHA: // \A
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006433 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006434 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006435 break;
6436
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006437 case NFA_LOWER: // \l
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006438 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006439 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006440 break;
6441
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006442 case NFA_NLOWER: // \L
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006443 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006444 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006445 break;
6446
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006447 case NFA_UPPER: // \u
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006448 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006449 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006450 break;
6451
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006452 case NFA_NUPPER: // \U
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006453 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006454 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006455 break;
6456
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006457 case NFA_LOWER_IC: // [a-z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006458 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006459 ADD_STATE_IF_MATCH(t->state);
6460 break;
6461
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006462 case NFA_NLOWER_IC: // [^a-z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006463 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006464 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006465 ADD_STATE_IF_MATCH(t->state);
6466 break;
6467
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006468 case NFA_UPPER_IC: // [A-Z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006469 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006470 ADD_STATE_IF_MATCH(t->state);
6471 break;
6472
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006473 case NFA_NUPPER_IC: // ^[A-Z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006474 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006475 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006476 ADD_STATE_IF_MATCH(t->state);
6477 break;
6478
Bram Moolenaar5714b802013-05-28 22:03:20 +02006479 case NFA_BACKREF1:
6480 case NFA_BACKREF2:
6481 case NFA_BACKREF3:
6482 case NFA_BACKREF4:
6483 case NFA_BACKREF5:
6484 case NFA_BACKREF6:
6485 case NFA_BACKREF7:
6486 case NFA_BACKREF8:
6487 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006488#ifdef FEAT_SYN_HL
6489 case NFA_ZREF1:
6490 case NFA_ZREF2:
6491 case NFA_ZREF3:
6492 case NFA_ZREF4:
6493 case NFA_ZREF5:
6494 case NFA_ZREF6:
6495 case NFA_ZREF7:
6496 case NFA_ZREF8:
6497 case NFA_ZREF9:
6498#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006499 // \1 .. \9 \z1 .. \z9
Bram Moolenaar5714b802013-05-28 22:03:20 +02006500 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006501 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006502 int bytelen;
6503
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006504 if (t->state->c <= NFA_BACKREF9)
6505 {
6506 subidx = t->state->c - NFA_BACKREF1 + 1;
6507 result = match_backref(&t->subs.norm, subidx, &bytelen);
6508 }
6509#ifdef FEAT_SYN_HL
6510 else
6511 {
6512 subidx = t->state->c - NFA_ZREF1 + 1;
6513 result = match_zref(subidx, &bytelen);
6514 }
6515#endif
6516
Bram Moolenaar5714b802013-05-28 22:03:20 +02006517 if (result)
6518 {
6519 if (bytelen == 0)
6520 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006521 // empty match always works, output of NFA_SKIP to be
6522 // used next
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006523 add_here = TRUE;
6524 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006525 }
6526 else if (bytelen <= clen)
6527 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006528 // match current character, jump ahead to out of
6529 // NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006530 add_state = t->state->out->out;
6531 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006532 }
6533 else
6534 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006535 // skip over the matched characters, set character
6536 // count in NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006537 add_state = t->state->out;
6538 add_off = bytelen;
6539 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006540 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006541 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006542 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006543 }
6544 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006545 // character of previous matching \1 .. \9 or \@>
Bram Moolenaar5714b802013-05-28 22:03:20 +02006546 if (t->count - clen <= 0)
6547 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006548 // end of match, go to what follows
Bram Moolenaara2d95102013-06-04 14:23:05 +02006549 add_state = t->state->out;
6550 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006551 }
6552 else
6553 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006554 // add state again with decremented count
Bram Moolenaara2d95102013-06-04 14:23:05 +02006555 add_state = t->state;
6556 add_off = 0;
6557 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006558 }
6559 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006560
Bram Moolenaar423532e2013-05-29 21:14:42 +02006561 case NFA_LNUM:
6562 case NFA_LNUM_GT:
6563 case NFA_LNUM_LT:
6564 result = (REG_MULTI &&
6565 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006566 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006567 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006568 {
6569 add_here = TRUE;
6570 add_state = t->state->out;
6571 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006572 break;
6573
6574 case NFA_COL:
6575 case NFA_COL_GT:
6576 case NFA_COL_LT:
6577 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006578 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006579 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006580 {
6581 add_here = TRUE;
6582 add_state = t->state->out;
6583 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006584 break;
6585
6586 case NFA_VCOL:
6587 case NFA_VCOL_GT:
6588 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006589 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006590 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006591 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006592 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006593
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006594 // Bail out quickly when there can't be a match, avoid the
6595 // overhead of win_linetabsize() on long lines.
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006596 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006597 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006598 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006599 result = FALSE;
6600 if (op == 1 && col - 1 > t->state->val && col > 100)
6601 {
6602 int ts = wp->w_buffer->b_p_ts;
6603
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006604 // Guess that a character won't use more columns than
6605 // 'tabstop', with a minimum of 4.
Bram Moolenaaref795d12015-01-18 16:46:32 +01006606 if (ts < 4)
6607 ts = 4;
6608 result = col > t->state->val * ts;
6609 }
6610 if (!result)
6611 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006612 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006613 if (result)
6614 {
6615 add_here = TRUE;
6616 add_state = t->state->out;
6617 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006618 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006619 break;
6620
Bram Moolenaar044aa292013-06-04 21:27:38 +02006621 case NFA_MARK:
6622 case NFA_MARK_GT:
6623 case NFA_MARK_LT:
6624 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006625 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006626
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006627 // Compare the mark position to the match position.
6628 result = (pos != NULL // mark doesn't exist
6629 && pos->lnum > 0 // mark isn't set in reg_buf
Bram Moolenaar0270f382018-07-17 05:43:58 +02006630 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6631 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006632 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006633 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006634 ? t->state->c == NFA_MARK_GT
6635 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006636 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006637 ? t->state->c == NFA_MARK_GT
6638 : t->state->c == NFA_MARK_LT)));
6639 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006640 {
6641 add_here = TRUE;
6642 add_state = t->state->out;
6643 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006644 break;
6645 }
6646
Bram Moolenaar423532e2013-05-29 21:14:42 +02006647 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006648 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006649 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006650 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006651 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006652 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006653 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006654 {
6655 add_here = TRUE;
6656 add_state = t->state->out;
6657 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006658 break;
6659
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006660 case NFA_VISUAL:
6661 result = reg_match_visual();
6662 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006663 {
6664 add_here = TRUE;
6665 add_state = t->state->out;
6666 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006667 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006668
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006669 case NFA_MOPEN1:
6670 case NFA_MOPEN2:
6671 case NFA_MOPEN3:
6672 case NFA_MOPEN4:
6673 case NFA_MOPEN5:
6674 case NFA_MOPEN6:
6675 case NFA_MOPEN7:
6676 case NFA_MOPEN8:
6677 case NFA_MOPEN9:
6678#ifdef FEAT_SYN_HL
6679 case NFA_ZOPEN:
6680 case NFA_ZOPEN1:
6681 case NFA_ZOPEN2:
6682 case NFA_ZOPEN3:
6683 case NFA_ZOPEN4:
6684 case NFA_ZOPEN5:
6685 case NFA_ZOPEN6:
6686 case NFA_ZOPEN7:
6687 case NFA_ZOPEN8:
6688 case NFA_ZOPEN9:
6689#endif
6690 case NFA_NOPEN:
6691 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006692 // These states are only added to be able to bail out when
6693 // they are added again, nothing is to be done.
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006694 break;
6695
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006696 default: // regular character
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006697 {
6698 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006699
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006700#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006701 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006702 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006703#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006704 result = (c == curc);
6705
Bram Moolenaar6100d022016-10-02 16:51:57 +02006706 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006707 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006708 // If rex.reg_icombine is not set only skip over the character
6709 // itself. When it is set skip over composing characters.
Bram Moolenaar6100d022016-10-02 16:51:57 +02006710 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006711 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006712 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006713 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006714 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006715
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006716 } // switch (t->state->c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006717
6718 if (add_state != NULL)
6719 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006720 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006721 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006722
6723 if (t->pim.result == NFA_PIM_UNUSED)
6724 pim = NULL;
6725 else
6726 pim = &t->pim;
6727
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006728 // Handle the postponed invisible match if the match might end
6729 // without advancing and before the end of the line.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006730 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006731 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006732 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006733 {
6734#ifdef ENABLE_LOG
6735 fprintf(log_fd, "\n");
6736 fprintf(log_fd, "==================================\n");
6737 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6738 fprintf(log_fd, "\n");
6739#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006740 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006741 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006742 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006743 // for \@! and \@<! it is a match when the result is
6744 // FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006745 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006746 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6747 || pim->state->c
6748 == NFA_START_INVISIBLE_BEFORE_NEG
6749 || pim->state->c
6750 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006751 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006752 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006753 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006754#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006755 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006756 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757#endif
6758 }
6759 }
6760 else
6761 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006762 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006763#ifdef ENABLE_LOG
6764 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006765 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006766 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6767 fprintf(log_fd, "\n");
6768#endif
6769 }
6770
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006771 // for \@! and \@<! it is a match when result is FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006772 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006773 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6774 || pim->state->c
6775 == NFA_START_INVISIBLE_BEFORE_NEG
6776 || pim->state->c
6777 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006778 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006779 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006780 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006781#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006782 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006783 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006784#endif
6785 }
6786 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006787 // look-behind match failed, don't add the state
Bram Moolenaara2d95102013-06-04 14:23:05 +02006788 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006789
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006790 // Postponed invisible match was handled, don't add it to
6791 // following states.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006792 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006793 }
6794
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006795 // If "pim" points into l->t it will become invalid when
6796 // adding the state causes the list to be reallocated. Make a
6797 // local copy to avoid that.
Bram Moolenaara951e352013-10-06 15:46:11 +02006798 if (pim == &t->pim)
6799 {
6800 copy_pim(&pim_copy, pim);
6801 pim = &pim_copy;
6802 }
6803
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006804 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006805 r = addstate_here(thislist, add_state, &t->subs,
6806 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006807 else
6808 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006809 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006810 if (add_count > 0)
6811 nextlist->t[nextlist->n - 1].count = add_count;
6812 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006813 if (r == NULL)
6814 {
6815 nfa_match = NFA_TOO_EXPENSIVE;
6816 goto theend;
6817 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006818 }
6819
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006820 } // for (thislist = thislist; thislist->state; thislist++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006821
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006822 // Look for the start of a match in the current position by adding the
6823 // start state to the list of states.
6824 // The first found match is the leftmost one, thus the order of states
6825 // matters!
6826 // Do not add the start state in recursive calls of nfa_regmatch(),
6827 // because recursive calls should only start in the first position.
6828 // Unless "nfa_endp" is not NULL, then we match the end position.
6829 // Also don't start a match past the first line.
Bram Moolenaar61602c52013-06-01 19:54:43 +02006830 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006831 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006832 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006833 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006834 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006835 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006836 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006837 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006838 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6839 || (rex.lnum == nfa_endp->se_u.pos.lnum
6840 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006841 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006842 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006843 {
6844#ifdef ENABLE_LOG
6845 fprintf(log_fd, "(---) STARTSTATE\n");
6846#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006847 // Inline optimized code for addstate() if we know the state is
6848 // the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006849 if (toplevel)
6850 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006851 int add = TRUE;
6852 int c;
6853
6854 if (prog->regstart != NUL && clen != 0)
6855 {
6856 if (nextlist->n == 0)
6857 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006858 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006859
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006860 // Nextlist is empty, we can skip ahead to the
6861 // character that must appear at the start.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006862 if (skip_to_start(prog->regstart, &col) == FAIL)
6863 break;
6864#ifdef ENABLE_LOG
6865 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006866 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006867#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006868 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006869 }
6870 else
6871 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006872 // Checking if the required start character matches is
6873 // cheaper than adding a state that won't match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02006874 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006875 if (c != prog->regstart && (!rex.reg_ic
6876 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006877 {
6878#ifdef ENABLE_LOG
6879 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6880#endif
6881 add = FALSE;
6882 }
6883 }
6884 }
6885
6886 if (add)
6887 {
6888 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006889 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006890 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006891 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006892 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006893 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6894 {
6895 nfa_match = NFA_TOO_EXPENSIVE;
6896 goto theend;
6897 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006898 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006899 }
6900 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006901 {
6902 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6903 {
6904 nfa_match = NFA_TOO_EXPENSIVE;
6905 goto theend;
6906 }
6907 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006908 }
6909
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006910#ifdef ENABLE_LOG
6911 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006912 {
6913 int i;
6914
6915 for (i = 0; i < thislist->n; i++)
6916 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6917 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006918 fprintf(log_fd, "\n");
6919#endif
6920
6921nextchar:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006922 // Advance to the next character, or advance to the next line, or
6923 // finish.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006924 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006925 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006926 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006927 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006928 reg_nextline();
6929 else
6930 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006931
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006932 // Allow interrupting with CTRL-C.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006933 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006934 if (got_int)
6935 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006936#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006937 // Check for timeout once in a twenty times to avoid overhead.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006938 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6939 {
6940 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006941 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006942 break;
6943 }
6944#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006945 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006946
6947#ifdef ENABLE_LOG
6948 if (log_fd != stderr)
6949 fclose(log_fd);
6950 log_fd = NULL;
6951#endif
6952
6953theend:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006954 // Free memory
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006955 vim_free(list[0].t);
6956 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006957 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006958#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006959#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006960 fclose(debug);
6961#endif
6962
Bram Moolenaar963fee22013-05-26 21:47:28 +02006963 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006964}
6965
6966/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006967 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006968 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969 */
6970 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006971nfa_regtry(
6972 nfa_regprog_T *prog,
6973 colnr_T col,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006974 proftime_T *tm UNUSED, // timeout limit or NULL
6975 int *timed_out UNUSED) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006976{
6977 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006978 regsubs_T subs, m;
6979 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006980 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006981#ifdef ENABLE_LOG
6982 FILE *f;
6983#endif
6984
Bram Moolenaar0270f382018-07-17 05:43:58 +02006985 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006986#ifdef FEAT_RELTIME
6987 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006988 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006989 nfa_time_count = 0;
6990#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006991
6992#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006993 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006994 if (f != NULL)
6995 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006996 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997#ifdef DEBUG
6998 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6999#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007000 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007001 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007002 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007003 fprintf(f, "\n\n");
7004 fclose(f);
7005 }
7006 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007007 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007008#endif
7009
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007010 clear_sub(&subs.norm);
7011 clear_sub(&m.norm);
7012#ifdef FEAT_SYN_HL
7013 clear_sub(&subs.synt);
7014 clear_sub(&m.synt);
7015#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007016
Bram Moolenaarfda37292014-11-05 14:27:36 +01007017 result = nfa_regmatch(prog, start, &subs, &m);
7018 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007019 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007020 else if (result == NFA_TOO_EXPENSIVE)
7021 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022
7023 cleanup_subexpr();
7024 if (REG_MULTI)
7025 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007026 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007027 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007028 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7029 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007030
Bram Moolenaar6100d022016-10-02 16:51:57 +02007031 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7032 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033 }
7034
Bram Moolenaar6100d022016-10-02 16:51:57 +02007035 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007036 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007037 rex.reg_startpos[0].lnum = 0;
7038 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007039 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007040 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007041 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007042 // pattern has a \ze but it didn't match, use current end
Bram Moolenaar0270f382018-07-17 05:43:58 +02007043 rex.reg_endpos[0].lnum = rex.lnum;
7044 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045 }
7046 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007047 // Use line number of "\ze".
Bram Moolenaar0270f382018-07-17 05:43:58 +02007048 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007049 }
7050 else
7051 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007052 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007053 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007054 rex.reg_startp[i] = subs.norm.list.line[i].start;
7055 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007056 }
7057
Bram Moolenaar6100d022016-10-02 16:51:57 +02007058 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007059 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007060 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007061 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007062 }
7063
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007064#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007065 // Package any found \z(...\) matches for export. Default is none.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007066 unref_extmatch(re_extmatch_out);
7067 re_extmatch_out = NULL;
7068
7069 if (prog->reghasz == REX_SET)
7070 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007071 cleanup_zsubexpr();
7072 re_extmatch_out = make_extmatch();
Bram Moolenaar7c77b342019-12-22 19:40:40 +01007073 if (re_extmatch_out == NULL)
7074 return 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007075 // Loop over \z1, \z2, etc. There is no \z0.
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007076 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007077 {
7078 if (REG_MULTI)
7079 {
7080 struct multipos *mpos = &subs.synt.list.multi[i];
7081
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007082 // Only accept single line matches that are valid.
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007083 if (mpos->start_lnum >= 0
7084 && mpos->start_lnum == mpos->end_lnum
7085 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007086 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007087 vim_strnsave(reg_getline(mpos->start_lnum)
7088 + mpos->start_col,
7089 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007090 }
7091 else
7092 {
7093 struct linepos *lpos = &subs.synt.list.line[i];
7094
7095 if (lpos->start != NULL && lpos->end != NULL)
7096 re_extmatch_out->matches[i] =
7097 vim_strnsave(lpos->start,
7098 (int)(lpos->end - lpos->start));
7099 }
7100 }
7101 }
7102#endif
7103
Bram Moolenaar0270f382018-07-17 05:43:58 +02007104 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007105}
7106
7107/*
7108 * Match a regexp against a string ("line" points to the string) or multiple
7109 * lines ("line" is NULL, use reg_getline()).
7110 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007111 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007112 */
7113 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007114nfa_regexec_both(
7115 char_u *line,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007116 colnr_T startcol, // column to start looking for match
7117 proftime_T *tm, // timeout limit or NULL
7118 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007119{
7120 nfa_regprog_T *prog;
7121 long retval = 0L;
7122 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007123 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007124
7125 if (REG_MULTI)
7126 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007127 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007128 line = reg_getline((linenr_T)0); // relative to the cursor
Bram Moolenaar6100d022016-10-02 16:51:57 +02007129 rex.reg_startpos = rex.reg_mmatch->startpos;
7130 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007131 }
7132 else
7133 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007134 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7135 rex.reg_startp = rex.reg_match->startp;
7136 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007137 }
7138
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007139 // Be paranoid...
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140 if (prog == NULL || line == NULL)
7141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007142 emsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007143 goto theend;
7144 }
7145
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007146 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007147 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007148 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007150 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007152 // If pattern contains "\Z" overrule value of rex.reg_icombine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007153 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007154 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007155
Bram Moolenaar0270f382018-07-17 05:43:58 +02007156 rex.line = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007157 rex.lnum = 0; // relative to line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158
Bram Moolenaar0270f382018-07-17 05:43:58 +02007159 rex.nfa_has_zend = prog->has_zend;
7160 rex.nfa_has_backref = prog->has_backref;
7161 rex.nfa_nsubexpr = prog->nsubexp;
7162 rex.nfa_listid = 1;
7163 rex.nfa_alt_listid = 2;
7164#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007165 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007166#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007167
Bram Moolenaard89616e2013-06-06 18:46:06 +02007168 if (prog->reganch && col > 0)
7169 return 0L;
7170
Bram Moolenaar0270f382018-07-17 05:43:58 +02007171 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007172#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007173 // Clear the external match subpointers if necessary.
Bram Moolenaar473de612013-06-08 18:19:48 +02007174 if (prog->reghasz == REX_SET)
7175 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007176 rex.nfa_has_zsubexpr = TRUE;
7177 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007178 }
7179 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007180 {
7181 rex.nfa_has_zsubexpr = FALSE;
7182 rex.need_clear_zsubexpr = FALSE;
7183 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007184#endif
7185
Bram Moolenaard89616e2013-06-06 18:46:06 +02007186 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007187 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007188 // Skip ahead until a character we know the match must start with.
7189 // When there is none there is no match.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007190 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007191 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007192
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007193 // If match_text is set it contains the full text that must match.
7194 // Nothing else to try. Doesn't handle combining chars well.
Bram Moolenaara12a1612019-01-24 16:39:02 +01007195 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007196 return find_match_text(col, prog->regstart, prog->match_text);
7197 }
7198
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007199 // If the start column is past the maximum column: no need to try.
Bram Moolenaar6100d022016-10-02 16:51:57 +02007200 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007201 goto theend;
7202
Bram Moolenaar0270f382018-07-17 05:43:58 +02007203 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7204 // it's accidentally used during execution.
7205 nstate = 0;
7206 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007207 {
7208 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007209 prog->state[i].lastlist[0] = 0;
7210 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007211 }
7212
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007213 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007214
Bram Moolenaar0270f382018-07-17 05:43:58 +02007215#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007216 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007217#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007218
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219theend:
7220 return retval;
7221}
7222
7223/*
7224 * Compile a regular expression into internal code for the NFA matcher.
7225 * Returns the program in allocated space. Returns NULL for an error.
7226 */
7227 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007228nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007229{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007230 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007231 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007232 int *postfix;
7233
7234 if (expr == NULL)
7235 return NULL;
7236
Bram Moolenaar0270f382018-07-17 05:43:58 +02007237#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007238 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007239#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007240 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007241
7242 init_class_tab();
7243
7244 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7245 return NULL;
7246
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007247 // Build postfix form of the regexp. Needed to build the NFA
7248 // (and count its size).
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007249 postfix = re2post();
7250 if (postfix == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007251 goto fail; // Cascaded (syntax?) error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007252
7253 /*
7254 * In order to build the NFA, we parse the input regexp twice:
7255 * 1. first pass to count size (so we can allocate space)
7256 * 2. second to emit code
7257 */
7258#ifdef ENABLE_LOG
7259 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007260 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007261
7262 if (f != NULL)
7263 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007264 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007265 fclose(f);
7266 }
7267 }
7268#endif
7269
7270 /*
7271 * PASS 1
7272 * Count number of NFA states in "nstate". Do not build the NFA.
7273 */
7274 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007275
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007276 // allocate the regprog with space for the compiled regexp
Bram Moolenaar16619a22013-06-11 18:42:36 +02007277 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007278 prog = alloc(prog_size);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007279 if (prog == NULL)
7280 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007281 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007282 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007283
7284 /*
7285 * PASS 2
7286 * Build the NFA
7287 */
7288 prog->start = post2nfa(postfix, post_ptr, FALSE);
7289 if (prog->start == NULL)
7290 goto fail;
7291
7292 prog->regflags = regflags;
7293 prog->engine = &nfa_regengine;
7294 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007295 prog->has_zend = rex.nfa_has_zend;
7296 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007297 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007298
Bram Moolenaara2947e22013-06-11 22:44:09 +02007299 nfa_postprocess(prog);
7300
Bram Moolenaard89616e2013-06-06 18:46:06 +02007301 prog->reganch = nfa_get_reganch(prog->start, 0);
7302 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007303 prog->match_text = nfa_get_match_text(prog->start);
7304
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007305#ifdef ENABLE_LOG
7306 nfa_postfix_dump(expr, OK);
7307 nfa_dump(prog);
7308#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007309#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007310 // Remember whether this pattern has any \z specials in it.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007311 prog->reghasz = re_has_z;
7312#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007313 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007314#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007315 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007316#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007317
7318out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007319 VIM_CLEAR(post_start);
7320 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007321 state_ptr = NULL;
7322 return (regprog_T *)prog;
7323
7324fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007325 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007326#ifdef ENABLE_LOG
7327 nfa_postfix_dump(expr, FAIL);
7328#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007329#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007330 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007331#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007332 goto out;
7333}
7334
Bram Moolenaar473de612013-06-08 18:19:48 +02007335/*
7336 * Free a compiled regexp program, returned by nfa_regcomp().
7337 */
7338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007339nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007340{
7341 if (prog != NULL)
7342 {
7343 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007344 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007345 vim_free(prog);
7346 }
7347}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007348
7349/*
7350 * Match a regexp against a string.
7351 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7352 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007353 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007354 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007355 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007356 */
7357 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007358nfa_regexec_nl(
7359 regmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007360 char_u *line, // string to match against
7361 colnr_T col, // column to start looking for match
Bram Moolenaar05540972016-01-30 20:31:25 +01007362 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007363{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007364 rex.reg_match = rmp;
7365 rex.reg_mmatch = NULL;
7366 rex.reg_maxline = 0;
7367 rex.reg_line_lbr = line_lbr;
7368 rex.reg_buf = curbuf;
7369 rex.reg_win = NULL;
7370 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007371 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007372 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007373 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007374}
7375
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007376
7377/*
7378 * Match a regexp against multiple lines.
7379 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7380 * Uses curbuf for line count and 'iskeyword'.
7381 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007382 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007383 * match otherwise.
7384 *
7385 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7386 *
7387 * ! Also NOTE : match may actually be in another line. e.g.:
7388 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7389 *
7390 * +-------------------------+
7391 * |a |
7392 * |b |
7393 * |c |
7394 * | |
7395 * +-------------------------+
7396 *
7397 * then nfa_regexec_multi() returns 3. while the original
7398 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7399 *
7400 * FIXME if this behavior is not compatible.
7401 */
7402 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007403nfa_regexec_multi(
7404 regmmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007405 win_T *win, // window in which to search or NULL
7406 buf_T *buf, // buffer in which to search
7407 linenr_T lnum, // nr of line to start looking for match
7408 colnr_T col, // column to start looking for match
7409 proftime_T *tm, // timeout limit or NULL
7410 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007411{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007412 rex.reg_match = NULL;
7413 rex.reg_mmatch = rmp;
7414 rex.reg_buf = buf;
7415 rex.reg_win = win;
7416 rex.reg_firstlnum = lnum;
7417 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7418 rex.reg_line_lbr = FALSE;
7419 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007420 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007421 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007422
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007423 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007424}
7425
7426#ifdef DEBUG
7427# undef ENABLE_LOG
7428#endif