blob: 064d90a033bad7c416caef13599a8df34857e250 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar63d9e732019-12-05 21:10:38 +010032// Added to NFA_ANY - NFA_NUPPER_IC to include a NL.
Bram Moolenaar1cfad522013-08-14 12:06:49 +020033#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010039 NFA_EMPTY, // matches 0-length
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar63d9e732019-12-05 21:10:38 +010041 NFA_START_COLL, // [abc] start
42 NFA_END_COLL, // [abc] end
43 NFA_START_NEG_COLL, // [^abc] start
44 NFA_END_NEG_COLL, // [^abc] end (postfix only)
45 NFA_RANGE, // range of the two previous items
46 // (postfix only)
47 NFA_RANGE_MIN, // low end of a range
48 NFA_RANGE_MAX, // high end of a range
Bram Moolenaar417bad22013-06-07 14:08:30 +020049
Bram Moolenaar63d9e732019-12-05 21:10:38 +010050 NFA_CONCAT, // concatenate two previous items (postfix
51 // only)
52 NFA_OR, // \| (postfix only)
53 NFA_STAR, // greedy * (postfix only)
54 NFA_STAR_NONGREEDY, // non-greedy * (postfix only)
55 NFA_QUEST, // greedy \? (postfix only)
56 NFA_QUEST_NONGREEDY, // non-greedy \? (postfix only)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
Bram Moolenaar63d9e732019-12-05 21:10:38 +010058 NFA_BOL, // ^ Begin line
59 NFA_EOL, // $ End line
60 NFA_BOW, // \< Begin word
61 NFA_EOW, // \> End word
62 NFA_BOF, // \%^ Begin file
63 NFA_EOF, // \%$ End file
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020064 NFA_NEWL,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010065 NFA_ZSTART, // Used for \zs
66 NFA_ZEND, // Used for \ze
67 NFA_NOPEN, // Start of subexpression marked with \%(
68 NFA_NCLOSE, // End of subexpr. marked with \%( ... \)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020069 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaar63d9e732019-12-05 21:10:38 +010081 NFA_COMPOSING, // Next nodes in NFA are part of the
82 // composing multibyte char
83 NFA_END_COMPOSING, // End of a composing char in the NFA
84 NFA_ANY_COMPOSING, // \%C: Any composing characters.
85 NFA_OPT_CHARS, // \%[abc]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
Bram Moolenaar63d9e732019-12-05 21:10:38 +010087 // The following are used only in the postfix form, not in the NFA
88 NFA_PREV_ATOM_NO_WIDTH, // Used for \@=
89 NFA_PREV_ATOM_NO_WIDTH_NEG, // Used for \@!
90 NFA_PREV_ATOM_JUST_BEFORE, // Used for \@<=
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, // Used for \@<!
92 NFA_PREV_ATOM_LIKE_PATTERN, // Used for \@>
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020093
Bram Moolenaar63d9e732019-12-05 21:10:38 +010094 NFA_BACKREF1, // \1
95 NFA_BACKREF2, // \2
96 NFA_BACKREF3, // \3
97 NFA_BACKREF4, // \4
98 NFA_BACKREF5, // \5
99 NFA_BACKREF6, // \6
100 NFA_BACKREF7, // \7
101 NFA_BACKREF8, // \8
102 NFA_BACKREF9, // \9
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100104 NFA_ZREF1, // \z1
105 NFA_ZREF2, // \z2
106 NFA_ZREF3, // \z3
107 NFA_ZREF4, // \z4
108 NFA_ZREF5, // \z5
109 NFA_ZREF6, // \z6
110 NFA_ZREF7, // \z7
111 NFA_ZREF8, // \z8
112 NFA_ZREF9, // \z9
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200113#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100114 NFA_SKIP, // Skip characters
Bram Moolenaar5714b802013-05-28 22:03:20 +0200115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100162 // NFA_FIRST_NL
163 NFA_ANY, // Match any one character.
164 NFA_IDENT, // Match identifier char
165 NFA_SIDENT, // Match identifier char but no digit
166 NFA_KWORD, // Match keyword char
167 NFA_SKWORD, // Match word char but no digit
168 NFA_FNAME, // Match file name char
169 NFA_SFNAME, // Match file name char but no digit
170 NFA_PRINT, // Match printable char
171 NFA_SPRINT, // Match printable char but no digit
172 NFA_WHITE, // Match whitespace char
173 NFA_NWHITE, // Match non-whitespace char
174 NFA_DIGIT, // Match digit char
175 NFA_NDIGIT, // Match non-digit char
176 NFA_HEX, // Match hex char
177 NFA_NHEX, // Match non-hex char
178 NFA_OCTAL, // Match octal char
179 NFA_NOCTAL, // Match non-octal char
180 NFA_WORD, // Match word char
181 NFA_NWORD, // Match non-word char
182 NFA_HEAD, // Match head char
183 NFA_NHEAD, // Match non-head char
184 NFA_ALPHA, // Match alpha char
185 NFA_NALPHA, // Match non-alpha char
186 NFA_LOWER, // Match lowercase char
187 NFA_NLOWER, // Match non-lowercase char
188 NFA_UPPER, // Match uppercase char
189 NFA_NUPPER, // Match non-uppercase char
190 NFA_LOWER_IC, // Match [a-z]
191 NFA_NLOWER_IC, // Match [^a-z]
192 NFA_UPPER_IC, // Match [A-Z]
193 NFA_NUPPER_IC, // Match [^A-Z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100198 NFA_CURSOR, // Match cursor pos
199 NFA_LNUM, // Match line number
200 NFA_LNUM_GT, // Match > line number
201 NFA_LNUM_LT, // Match < line number
202 NFA_COL, // Match cursor column
203 NFA_COL_GT, // Match > cursor column
204 NFA_COL_LT, // Match < cursor column
205 NFA_VCOL, // Match cursor virtual column
206 NFA_VCOL_GT, // Match > cursor virtual column
207 NFA_VCOL_LT, // Match < cursor virtual column
208 NFA_MARK, // Match mark
209 NFA_MARK_GT, // Match > mark
210 NFA_MARK_LT, // Match < mark
211 NFA_VISUAL, // Match Visual area
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100213 // Character classes [:alnum:] etc
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
Bram Moolenaar221cd9f2019-01-31 15:34:40 +0100229 NFA_CLASS_ESCAPE,
230 NFA_CLASS_IDENT,
231 NFA_CLASS_KEYWORD,
232 NFA_CLASS_FNAME
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200233};
234
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100235// Keep in sync with classchars.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200236static int nfa_classcodes[] = {
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
241 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
242 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
243 NFA_UPPER, NFA_NUPPER
244};
245
Bram Moolenaar174a8482013-11-28 14:20:17 +0100246static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaara5483442019-02-17 20:17:02 +0100248static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %d");
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +0200249static char_u e_value_too_large[] = N_("E951: \\% value too large");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250
Bram Moolenaar0270f382018-07-17 05:43:58 +0200251// Variables only used in nfa_regcomp() and descendants.
252static int nfa_re_flags; // re_flags passed to nfa_regcomp()
253static int *post_start; // holds the postfix form of r.e.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200254static int *post_end;
255static int *post_ptr;
Bram Moolenaar66c50c52021-01-02 17:43:49 +0100256
257// Set when the pattern should use the NFA engine.
258// E.g. [[:upper:]] only allows 8bit characters for BT engine,
259// while NFA engine handles multibyte characters correctly.
260static int wants_nfa;
261
Bram Moolenaar0270f382018-07-17 05:43:58 +0200262static int nstate; // Number of states in the NFA.
263static int istate; // Index in the state vector, used in alloc_state()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200264
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100265// If not NULL match must end at this position
Bram Moolenaar307aa162013-06-02 16:34:21 +0200266static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200267
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100268// 0 for first call to nfa_regmatch(), 1 for recursive call.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200269static int nfa_ll_index = 0;
270
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100271static int realloc_post_list(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100272static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200273#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100274static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100276static int match_follows(nfa_state_T *startstate, int depth);
277static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200278
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100279// helper functions used when doing re2post() ... regatom() parsing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200280#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200281 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200282 return FAIL; \
283 *post_ptr++ = c; \
284 } while (0)
285
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286/*
287 * Initialize internal variables before NFA compilation.
288 * Return OK on success, FAIL otherwise.
289 */
290 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291nfa_regcomp_start(
292 char_u *expr,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100293 int re_flags) // see vim_regcomp()
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200294{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200295 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200296 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297
298 nstate = 0;
299 istate = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100300 // A reasonable estimation for maximum size
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200301 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100303 // Some items blow up in size, such as [A-z]. Add more space for that.
304 // When it is still not enough realloc_post_list() will be used.
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200305 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200306
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100307 // Size for postfix representation of expr.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200308 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200309
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200310 post_start = alloc(postfix_size);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311 if (post_start == NULL)
312 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200313 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200314 post_end = post_start + nstate_max;
Bram Moolenaar66c50c52021-01-02 17:43:49 +0100315 wants_nfa = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +0200316 rex.nfa_has_zend = FALSE;
317 rex.nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200318
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100319 // shared with BT engine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200320 regcomp_start(expr, re_flags);
321
322 return OK;
323}
324
325/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200326 * Figure out if the NFA state list starts with an anchor, must match at start
327 * of the line.
328 */
329 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100330nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200331{
332 nfa_state_T *p = start;
333
334 if (depth > 4)
335 return 0;
336
337 while (p != NULL)
338 {
339 switch (p->c)
340 {
341 case NFA_BOL:
342 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100343 return 1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200344
345 case NFA_ZSTART:
346 case NFA_ZEND:
347 case NFA_CURSOR:
348 case NFA_VISUAL:
349
350 case NFA_MOPEN:
351 case NFA_MOPEN1:
352 case NFA_MOPEN2:
353 case NFA_MOPEN3:
354 case NFA_MOPEN4:
355 case NFA_MOPEN5:
356 case NFA_MOPEN6:
357 case NFA_MOPEN7:
358 case NFA_MOPEN8:
359 case NFA_MOPEN9:
360 case NFA_NOPEN:
361#ifdef FEAT_SYN_HL
362 case NFA_ZOPEN:
363 case NFA_ZOPEN1:
364 case NFA_ZOPEN2:
365 case NFA_ZOPEN3:
366 case NFA_ZOPEN4:
367 case NFA_ZOPEN5:
368 case NFA_ZOPEN6:
369 case NFA_ZOPEN7:
370 case NFA_ZOPEN8:
371 case NFA_ZOPEN9:
372#endif
373 p = p->out;
374 break;
375
376 case NFA_SPLIT:
377 return nfa_get_reganch(p->out, depth + 1)
378 && nfa_get_reganch(p->out1, depth + 1);
379
380 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100381 return 0; // noooo
Bram Moolenaard89616e2013-06-06 18:46:06 +0200382 }
383 }
384 return 0;
385}
386
387/*
388 * Figure out if the NFA state list starts with a character which must match
389 * at start of the match.
390 */
391 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100392nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200393{
394 nfa_state_T *p = start;
395
396 if (depth > 4)
397 return 0;
398
399 while (p != NULL)
400 {
401 switch (p->c)
402 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100403 // all kinds of zero-width matches
Bram Moolenaard89616e2013-06-06 18:46:06 +0200404 case NFA_BOL:
405 case NFA_BOF:
406 case NFA_BOW:
407 case NFA_EOW:
408 case NFA_ZSTART:
409 case NFA_ZEND:
410 case NFA_CURSOR:
411 case NFA_VISUAL:
412 case NFA_LNUM:
413 case NFA_LNUM_GT:
414 case NFA_LNUM_LT:
415 case NFA_COL:
416 case NFA_COL_GT:
417 case NFA_COL_LT:
418 case NFA_VCOL:
419 case NFA_VCOL_GT:
420 case NFA_VCOL_LT:
421 case NFA_MARK:
422 case NFA_MARK_GT:
423 case NFA_MARK_LT:
424
425 case NFA_MOPEN:
426 case NFA_MOPEN1:
427 case NFA_MOPEN2:
428 case NFA_MOPEN3:
429 case NFA_MOPEN4:
430 case NFA_MOPEN5:
431 case NFA_MOPEN6:
432 case NFA_MOPEN7:
433 case NFA_MOPEN8:
434 case NFA_MOPEN9:
435 case NFA_NOPEN:
436#ifdef FEAT_SYN_HL
437 case NFA_ZOPEN:
438 case NFA_ZOPEN1:
439 case NFA_ZOPEN2:
440 case NFA_ZOPEN3:
441 case NFA_ZOPEN4:
442 case NFA_ZOPEN5:
443 case NFA_ZOPEN6:
444 case NFA_ZOPEN7:
445 case NFA_ZOPEN8:
446 case NFA_ZOPEN9:
447#endif
448 p = p->out;
449 break;
450
451 case NFA_SPLIT:
452 {
453 int c1 = nfa_get_regstart(p->out, depth + 1);
454 int c2 = nfa_get_regstart(p->out1, depth + 1);
455
456 if (c1 == c2)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100457 return c1; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200458 return 0;
459 }
460
461 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200462 if (p->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100463 return p->c; // yes!
Bram Moolenaard89616e2013-06-06 18:46:06 +0200464 return 0;
465 }
466 }
467 return 0;
468}
469
470/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200471 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200472 * else. If so return a string in allocated memory with what must match after
473 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200474 */
475 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100476nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200477{
478 nfa_state_T *p = start;
479 int len = 0;
480 char_u *ret;
481 char_u *s;
482
483 if (p->c != NFA_MOPEN)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100484 return NULL; // just in case
Bram Moolenaar473de612013-06-08 18:19:48 +0200485 p = p->out;
486 while (p->c > 0)
487 {
488 len += MB_CHAR2LEN(p->c);
489 p = p->out;
490 }
491 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
492 return NULL;
493
494 ret = alloc(len);
495 if (ret != NULL)
496 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100497 p = start->out->out; // skip first char, it goes into regstart
Bram Moolenaar473de612013-06-08 18:19:48 +0200498 s = ret;
499 while (p->c > 0)
500 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200501 if (has_mbyte)
502 s += (*mb_char2bytes)(p->c, s);
503 else
Bram Moolenaar473de612013-06-08 18:19:48 +0200504 *s++ = p->c;
505 p = p->out;
506 }
507 *s = NUL;
508 }
509 return ret;
510}
511
512/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200513 * Allocate more space for post_start. Called when
514 * running above the estimated number of states.
515 */
516 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100517realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200518{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200519 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100520 int new_max;
Bram Moolenaar16299b52013-05-30 18:45:23 +0200521 int *new_start;
522 int *old_start;
523
Bram Moolenaar38f08e72019-02-20 22:04:32 +0100524 // For weird patterns the number of states can be very high. Increasing by
525 // 50% seems a reasonable compromise between memory use and speed.
526 new_max = nstate_max * 3 / 2;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200527 new_start = ALLOC_MULT(int, new_max);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200528 if (new_start == NULL)
529 return FAIL;
530 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200531 old_start = post_start;
532 post_start = new_start;
533 post_ptr = new_start + (post_ptr - old_start);
534 post_end = post_start + new_max;
535 vim_free(old_start);
536 return OK;
537}
538
539/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540 * Search between "start" and "end" and try to recognize a
541 * character class in expanded form. For example [0-9].
542 * On success, return the id the character class to be emitted.
543 * On failure, return 0 (=FAIL)
544 * Start points to the first char of the range, while end should point
545 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200546 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
547 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200548 */
549 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100550nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200551{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200552# define CLASS_not 0x80
553# define CLASS_af 0x40
554# define CLASS_AF 0x20
555# define CLASS_az 0x10
556# define CLASS_AZ 0x08
557# define CLASS_o7 0x04
558# define CLASS_o9 0x02
559# define CLASS_underscore 0x01
560
561 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200562 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200563 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200564
565 if (extra_newl == TRUE)
566 newl = TRUE;
567
568 if (*end != ']')
569 return FAIL;
570 p = start;
571 if (*p == '^')
572 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200573 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200574 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200575 }
576
577 while (p < end)
578 {
579 if (p + 2 < end && *(p + 1) == '-')
580 {
581 switch (*p)
582 {
583 case '0':
584 if (*(p + 2) == '9')
585 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200586 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200587 break;
588 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200589 if (*(p + 2) == '7')
590 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200591 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 break;
593 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200594 return FAIL;
595
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200596 case 'a':
597 if (*(p + 2) == 'z')
598 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200599 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200600 break;
601 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200602 if (*(p + 2) == 'f')
603 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200604 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200605 break;
606 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200607 return FAIL;
608
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609 case 'A':
610 if (*(p + 2) == 'Z')
611 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200612 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200613 break;
614 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200615 if (*(p + 2) == 'F')
616 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200617 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200618 break;
619 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200620 return FAIL;
621
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200622 default:
623 return FAIL;
624 }
625 p += 3;
626 }
627 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
628 {
629 newl = TRUE;
630 p += 2;
631 }
632 else if (*p == '_')
633 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200634 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 p ++;
636 }
637 else if (*p == '\n')
638 {
639 newl = TRUE;
640 p ++;
641 }
642 else
643 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100644 } // while (p < end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200645
646 if (p != end)
647 return FAIL;
648
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200649 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200650 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200651
652 switch (config)
653 {
654 case CLASS_o9:
655 return extra_newl + NFA_DIGIT;
656 case CLASS_not | CLASS_o9:
657 return extra_newl + NFA_NDIGIT;
658 case CLASS_af | CLASS_AF | CLASS_o9:
659 return extra_newl + NFA_HEX;
660 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
661 return extra_newl + NFA_NHEX;
662 case CLASS_o7:
663 return extra_newl + NFA_OCTAL;
664 case CLASS_not | CLASS_o7:
665 return extra_newl + NFA_NOCTAL;
666 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
667 return extra_newl + NFA_WORD;
668 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
669 return extra_newl + NFA_NWORD;
670 case CLASS_az | CLASS_AZ | CLASS_underscore:
671 return extra_newl + NFA_HEAD;
672 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
673 return extra_newl + NFA_NHEAD;
674 case CLASS_az | CLASS_AZ:
675 return extra_newl + NFA_ALPHA;
676 case CLASS_not | CLASS_az | CLASS_AZ:
677 return extra_newl + NFA_NALPHA;
678 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200679 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200680 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200681 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200682 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200683 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200684 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200685 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200686 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200687 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200688}
689
690/*
691 * Produce the bytes for equivalence class "c".
692 * Currently only handles latin1, latin9 and utf-8.
693 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
694 * equivalent to 'a OR b OR c'
695 *
696 * NOTE! When changing this function, also update reg_equi_class()
697 */
698 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100699nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200700{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200701#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaara12a1612019-01-24 16:39:02 +0100702#define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200703
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200704 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
705 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200706 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200707#ifdef EBCDIC
708# define A_circumflex 0x62
709# define A_diaeresis 0x63
710# define A_grave 0x64
711# define A_acute 0x65
712# define A_virguilla 0x66
713# define A_ring 0x67
714# define C_cedilla 0x68
715# define E_acute 0x71
716# define E_circumflex 0x72
717# define E_diaeresis 0x73
718# define E_grave 0x74
719# define I_acute 0x75
720# define I_circumflex 0x76
721# define I_diaeresis 0x77
722# define I_grave 0x78
723# define N_virguilla 0x69
724# define O_circumflex 0xeb
725# define O_diaeresis 0xec
726# define O_grave 0xed
727# define O_acute 0xee
728# define O_virguilla 0xef
729# define O_slash 0x80
730# define U_circumflex 0xfb
731# define U_diaeresis 0xfc
732# define U_grave 0xfd
733# define U_acute 0xfe
734# define Y_acute 0xba
735# define a_grave 0x42
736# define a_acute 0x43
737# define a_circumflex 0x44
738# define a_virguilla 0x45
739# define a_diaeresis 0x46
740# define a_ring 0x47
741# define c_cedilla 0x48
742# define e_grave 0x51
743# define e_acute 0x52
744# define e_circumflex 0x53
745# define e_diaeresis 0x54
746# define i_grave 0x55
747# define i_acute 0x56
748# define i_circumflex 0x57
749# define i_diaeresis 0x58
750# define n_virguilla 0x49
751# define o_grave 0xcb
752# define o_acute 0xcc
753# define o_circumflex 0xcd
754# define o_virguilla 0xce
755# define o_diaeresis 0xcf
756# define o_slash 0x70
757# define u_grave 0xdb
758# define u_acute 0xdc
759# define u_circumflex 0xdd
760# define u_diaeresis 0xde
761# define y_acute 0x8d
762# define y_diaeresis 0xdf
763#else
764# define A_grave 0xc0
765# define A_acute 0xc1
766# define A_circumflex 0xc2
767# define A_virguilla 0xc3
768# define A_diaeresis 0xc4
769# define A_ring 0xc5
770# define C_cedilla 0xc7
771# define E_grave 0xc8
772# define E_acute 0xc9
773# define E_circumflex 0xca
774# define E_diaeresis 0xcb
775# define I_grave 0xcc
776# define I_acute 0xcd
777# define I_circumflex 0xce
778# define I_diaeresis 0xcf
779# define N_virguilla 0xd1
780# define O_grave 0xd2
781# define O_acute 0xd3
782# define O_circumflex 0xd4
783# define O_virguilla 0xd5
784# define O_diaeresis 0xd6
785# define O_slash 0xd8
786# define U_grave 0xd9
787# define U_acute 0xda
788# define U_circumflex 0xdb
789# define U_diaeresis 0xdc
790# define Y_acute 0xdd
791# define a_grave 0xe0
792# define a_acute 0xe1
793# define a_circumflex 0xe2
794# define a_virguilla 0xe3
795# define a_diaeresis 0xe4
796# define a_ring 0xe5
797# define c_cedilla 0xe7
798# define e_grave 0xe8
799# define e_acute 0xe9
800# define e_circumflex 0xea
801# define e_diaeresis 0xeb
802# define i_grave 0xec
803# define i_acute 0xed
804# define i_circumflex 0xee
805# define i_diaeresis 0xef
806# define n_virguilla 0xf1
807# define o_grave 0xf2
808# define o_acute 0xf3
809# define o_circumflex 0xf4
810# define o_virguilla 0xf5
811# define o_diaeresis 0xf6
812# define o_slash 0xf8
813# define u_grave 0xf9
814# define u_acute 0xfa
815# define u_circumflex 0xfb
816# define u_diaeresis 0xfc
817# define y_acute 0xfd
818# define y_diaeresis 0xff
819#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200820 switch (c)
821 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200822 case 'A': case A_grave: case A_acute: case A_circumflex:
823 case A_virguilla: case A_diaeresis: case A_ring:
824 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
825 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
826 CASEMBC(0x1ea2)
827 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
828 EMIT2(A_circumflex); EMIT2(A_virguilla);
829 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200830 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
831 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
832 EMITMBC(0x1ea2)
833 return OK;
834
835 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
836 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200837 return OK;
838
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200839 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
840 CASEMBC(0x10a) CASEMBC(0x10c)
841 EMIT2('C'); EMIT2(C_cedilla);
842 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200843 EMITMBC(0x10a) EMITMBC(0x10c)
844 return OK;
845
846 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200847 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200848 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
849 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200850 return OK;
851
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200852 case 'E': case E_grave: case E_acute: case E_circumflex:
853 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
854 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
855 CASEMBC(0x1eba) CASEMBC(0x1ebc)
856 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
857 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200858 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
859 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
860 EMITMBC(0x1ebc)
861 return OK;
862
863 case 'F': CASEMBC(0x1e1e)
864 EMIT2('F'); EMITMBC(0x1e1e)
865 return OK;
866
867 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200868 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
869 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200870 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
871 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
872 EMITMBC(0x1f4) EMITMBC(0x1e20)
873 return OK;
874
875 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200876 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200877 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
878 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200879 return OK;
880
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200881 case 'I': case I_grave: case I_acute: case I_circumflex:
882 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
883 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
884 CASEMBC(0x1cf) CASEMBC(0x1ec8)
885 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
886 EMIT2(I_circumflex); EMIT2(I_diaeresis);
887 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200888 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
889 EMITMBC(0x1cf) EMITMBC(0x1ec8)
890 return OK;
891
892 case 'J': CASEMBC(0x134)
893 EMIT2('J'); EMITMBC(0x134)
894 return OK;
895
896 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200897 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200898 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
899 EMITMBC(0x1e34)
900 return OK;
901
902 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200903 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200904 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
905 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
906 return OK;
907
908 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
909 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200910 return OK;
911
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200912 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
913 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
914 EMIT2('N'); EMIT2(N_virguilla);
915 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200917 return OK;
918
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200919 case 'O': case O_grave: case O_acute: case O_circumflex:
920 case O_virguilla: case O_diaeresis: case O_slash:
921 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
922 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
923 CASEMBC(0x1ec) CASEMBC(0x1ece)
924 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
925 EMIT2(O_circumflex); EMIT2(O_virguilla);
926 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200927 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
928 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
929 EMITMBC(0x1ec) EMITMBC(0x1ece)
930 return OK;
931
932 case 'P': case 0x1e54: case 0x1e56:
933 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
934 return OK;
935
936 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200937 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200938 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
939 EMITMBC(0x1e58) EMITMBC(0x1e5e)
940 return OK;
941
942 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200943 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200944 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
945 EMITMBC(0x160) EMITMBC(0x1e60)
946 return OK;
947
948 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200949 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200950 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
951 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200952 return OK;
953
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200954 case 'U': case U_grave: case U_acute: case U_diaeresis:
955 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
956 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
957 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
958 CASEMBC(0x1ee6)
959 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
960 EMIT2(U_diaeresis); EMIT2(U_circumflex);
961 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200962 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
963 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
964 EMITMBC(0x1ee6)
965 return OK;
966
967 case 'V': CASEMBC(0x1e7c)
968 EMIT2('V'); EMITMBC(0x1e7c)
969 return OK;
970
971 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200972 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200973 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
974 EMITMBC(0x1e84) EMITMBC(0x1e86)
975 return OK;
976
977 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
978 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200979 return OK;
980
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200981 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
982 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
983 CASEMBC(0x1ef8)
984 EMIT2('Y'); EMIT2(Y_acute);
985 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200986 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
987 EMITMBC(0x1ef8)
988 return OK;
989
990 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200991 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200992 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
993 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200994 return OK;
995
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200996 case 'a': case a_grave: case a_acute: case a_circumflex:
997 case a_virguilla: case a_diaeresis: case a_ring:
998 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
999 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1000 CASEMBC(0x1ea3)
1001 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1002 EMIT2(a_circumflex); EMIT2(a_virguilla);
1003 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001004 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1005 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1006 EMITMBC(0x1ea3)
1007 return OK;
1008
1009 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1010 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001011 return OK;
1012
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001013 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1014 CASEMBC(0x10b) CASEMBC(0x10d)
1015 EMIT2('c'); EMIT2(c_cedilla);
1016 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001017 EMITMBC(0x10b) EMITMBC(0x10d)
1018 return OK;
1019
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001020 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001021 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001022 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1023 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001024 return OK;
1025
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001026 case 'e': case e_grave: case e_acute: case e_circumflex:
1027 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1028 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1029 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1030 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1031 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1032 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001033 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1034 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1035 return OK;
1036
1037 case 'f': CASEMBC(0x1e1f)
1038 EMIT2('f'); EMITMBC(0x1e1f)
1039 return OK;
1040
1041 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001042 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1043 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001044 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1045 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1046 EMITMBC(0x1f5) EMITMBC(0x1e21)
1047 return OK;
1048
1049 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001050 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001051 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1052 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001053 return OK;
1054
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001055 case 'i': case i_grave: case i_acute: case i_circumflex:
1056 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1057 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1058 CASEMBC(0x1ec9)
1059 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1060 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1061 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001062 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1063 EMITMBC(0x1ec9)
1064 return OK;
1065
1066 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1067 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1068 return OK;
1069
1070 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001071 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001072 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1073 EMITMBC(0x1e35)
1074 return OK;
1075
1076 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001077 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001078 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1079 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1080 return OK;
1081
1082 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1083 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001084 return OK;
1085
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001086 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1087 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1088 CASEMBC(0x1e49)
1089 EMIT2('n'); EMIT2(n_virguilla);
1090 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001091 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1092 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001093 return OK;
1094
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001095 case 'o': case o_grave: case o_acute: case o_circumflex:
1096 case o_virguilla: case o_diaeresis: case o_slash:
1097 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1098 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1099 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1100 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1101 EMIT2(o_circumflex); EMIT2(o_virguilla);
1102 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001103 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1104 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1105 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1106 return OK;
1107
1108 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1109 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1110 return OK;
1111
1112 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001113 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001114 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1115 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1116 return OK;
1117
1118 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1121 EMITMBC(0x161) EMITMBC(0x1e61)
1122 return OK;
1123
1124 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001125 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001126 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1127 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001128 return OK;
1129
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001130 case 'u': case u_grave: case u_acute: case u_circumflex:
1131 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1132 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1133 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1134 CASEMBC(0x1ee7)
1135 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1136 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1137 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001138 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1139 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1140 EMITMBC(0x1ee7)
1141 return OK;
1142
1143 case 'v': CASEMBC(0x1e7d)
1144 EMIT2('v'); EMITMBC(0x1e7d)
1145 return OK;
1146
1147 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001148 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001149 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1150 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1151 return OK;
1152
1153 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1154 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001155 return OK;
1156
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001157 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1158 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1159 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1160 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1161 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1163 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001164 return OK;
1165
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001166 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001167 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1169 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1170 return OK;
1171
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001172 // default: character itself
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001173 }
1174 }
1175
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001176 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001177 return OK;
1178#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001179#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001180}
1181
1182/*
1183 * Code to parse regular expression.
1184 *
1185 * We try to reuse parsing functions in regexp.c to
1186 * minimize surprise and keep the syntax consistent.
1187 */
1188
1189/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190 * Parse the lowest level.
1191 *
1192 * An atom can be one of a long list of items. Many atoms match one character
1193 * in the text. It is often an ordinary character or a character class.
1194 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1195 * is only for syntax highlighting.
1196 *
1197 * atom ::= ordinary-atom
1198 * or \( pattern \)
1199 * or \%( pattern \)
1200 * or \z( pattern \)
1201 */
1202 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001203nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001204{
1205 int c;
1206 int charclass;
1207 int equiclass;
1208 int collclass;
1209 int got_coll_char;
1210 char_u *p;
1211 char_u *endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001213 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001214 int emit_range;
1215 int negated;
1216 int result;
1217 int startc = -1;
1218 int endc = -1;
1219 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001220 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001222 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 switch (c)
1224 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001225 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001226 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001227
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228 case Magic('^'):
1229 EMIT(NFA_BOL);
1230 break;
1231
1232 case Magic('$'):
1233 EMIT(NFA_EOL);
1234#if defined(FEAT_SYN_HL) || defined(PROTO)
1235 had_eol = TRUE;
1236#endif
1237 break;
1238
1239 case Magic('<'):
1240 EMIT(NFA_BOW);
1241 break;
1242
1243 case Magic('>'):
1244 EMIT(NFA_EOW);
1245 break;
1246
1247 case Magic('_'):
1248 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001249 if (c == NUL)
1250 EMSG_RET_FAIL(_(e_nul_found));
1251
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001252 if (c == '^') // "\_^" is start-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253 {
1254 EMIT(NFA_BOL);
1255 break;
1256 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001257 if (c == '$') // "\_$" is end-of-line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258 {
1259 EMIT(NFA_EOL);
1260#if defined(FEAT_SYN_HL) || defined(PROTO)
1261 had_eol = TRUE;
1262#endif
1263 break;
1264 }
1265
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001266 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001268 // "\_[" is collection plus newline
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001270 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001272 // "\_x" is character class plus newline
1273 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001274
1275 /*
1276 * Character classes.
1277 */
1278 case Magic('.'):
1279 case Magic('i'):
1280 case Magic('I'):
1281 case Magic('k'):
1282 case Magic('K'):
1283 case Magic('f'):
1284 case Magic('F'):
1285 case Magic('p'):
1286 case Magic('P'):
1287 case Magic('s'):
1288 case Magic('S'):
1289 case Magic('d'):
1290 case Magic('D'):
1291 case Magic('x'):
1292 case Magic('X'):
1293 case Magic('o'):
1294 case Magic('O'):
1295 case Magic('w'):
1296 case Magic('W'):
1297 case Magic('h'):
1298 case Magic('H'):
1299 case Magic('a'):
1300 case Magic('A'):
1301 case Magic('l'):
1302 case Magic('L'):
1303 case Magic('u'):
1304 case Magic('U'):
1305 p = vim_strchr(classchars, no_Magic(c));
1306 if (p == NULL)
1307 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001308 if (extra == NFA_ADD_NL)
1309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 semsg(_(e_ill_char_class), c);
Bram Moolenaar174a8482013-11-28 14:20:17 +01001311 rc_did_emsg = TRUE;
1312 return FAIL;
1313 }
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001314 siemsg("INTERNAL: Unknown character class char: %d", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001315 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001317
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001318 // When '.' is followed by a composing char ignore the dot, so that
1319 // the composing char is matched here.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1321 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001322 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001323 c = getchr();
1324 goto nfa_do_multibyte;
1325 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001326 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001327 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328 {
1329 EMIT(NFA_NEWL);
1330 EMIT(NFA_OR);
1331 regflags |= RF_HASNL;
1332 }
1333 break;
1334
1335 case Magic('n'):
1336 if (reg_string)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001337 // In a string "\n" matches a newline character.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001338 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339 else
1340 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001341 // In buffer text "\n" matches the end of a line.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001342 EMIT(NFA_NEWL);
1343 regflags |= RF_HASNL;
1344 }
1345 break;
1346
1347 case Magic('('):
1348 if (nfa_reg(REG_PAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001349 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001350 break;
1351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001352 case Magic('|'):
1353 case Magic('&'):
1354 case Magic(')'):
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001355 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001356 return FAIL;
1357
1358 case Magic('='):
1359 case Magic('?'):
1360 case Magic('+'):
1361 case Magic('@'):
1362 case Magic('*'):
1363 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 // these should follow an atom, not form an atom
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001365 semsg(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 return FAIL;
1367
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001368 case Magic('~'):
1369 {
1370 char_u *lp;
1371
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001372 // Previous substitute pattern.
1373 // Generated as "\%(pattern\)".
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001374 if (reg_prev_sub == NULL)
1375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001376 emsg(_(e_nopresub));
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001377 return FAIL;
1378 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001379 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001380 {
1381 EMIT(PTR2CHAR(lp));
1382 if (lp != reg_prev_sub)
1383 EMIT(NFA_CONCAT);
1384 }
1385 EMIT(NFA_NOPEN);
1386 break;
1387 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388
Bram Moolenaar428e9872013-05-30 17:05:39 +02001389 case Magic('1'):
1390 case Magic('2'):
1391 case Magic('3'):
1392 case Magic('4'):
1393 case Magic('5'):
1394 case Magic('6'):
1395 case Magic('7'):
1396 case Magic('8'):
1397 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001398 {
1399 int refnum = no_Magic(c) - '1';
1400
1401 if (!seen_endbrace(refnum + 1))
1402 return FAIL;
1403 EMIT(NFA_BACKREF1 + refnum);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001404 rex.nfa_has_backref = TRUE;
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001405 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001406 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407
1408 case Magic('z'):
1409 c = no_Magic(getchr());
1410 switch (c)
1411 {
1412 case 's':
1413 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001414 if (re_mult_next("\\zs") == FAIL)
1415 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 break;
1417 case 'e':
1418 EMIT(NFA_ZEND);
Bram Moolenaar0270f382018-07-17 05:43:58 +02001419 rex.nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001420 if (re_mult_next("\\ze") == FAIL)
1421 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001422 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001423#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001424 case '1':
1425 case '2':
1426 case '3':
1427 case '4':
1428 case '5':
1429 case '6':
1430 case '7':
1431 case '8':
1432 case '9':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001433 // \z1...\z9
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001434 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001435 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001436 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001437 // No need to set rex.nfa_has_backref, the sub-matches don't
1438 // change when \z1 .. \z9 matches or not.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001439 re_has_z = REX_USE;
1440 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441 case '(':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001442 // \z(
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001443 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001444 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001445 if (nfa_reg(REG_ZPAREN) == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001446 return FAIL; // cascaded error
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001447 re_has_z = REX_SET;
1448 break;
1449#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001451 semsg(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452 no_Magic(c));
1453 return FAIL;
1454 }
1455 break;
1456
1457 case Magic('%'):
1458 c = no_Magic(getchr());
1459 switch (c)
1460 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001461 // () without a back reference
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 case '(':
1463 if (nfa_reg(REG_NPAREN) == FAIL)
1464 return FAIL;
1465 EMIT(NFA_NOPEN);
1466 break;
1467
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001468 case 'd': // %d123 decimal
1469 case 'o': // %o123 octal
1470 case 'x': // %xab hex 2
1471 case 'u': // %uabcd hex 4
1472 case 'U': // %U1234abcd hex 8
Bram Moolenaar47196582013-05-25 22:04:23 +02001473 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001474 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475
Bram Moolenaar47196582013-05-25 22:04:23 +02001476 switch (c)
1477 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001478 case 'd': nr = getdecchrs(); break;
1479 case 'o': nr = getoctchrs(); break;
1480 case 'x': nr = gethexchrs(2); break;
1481 case 'u': nr = gethexchrs(4); break;
1482 case 'U': nr = gethexchrs(8); break;
1483 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001484 }
1485
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001486 if (nr < 0 || nr > INT_MAX)
Bram Moolenaar47196582013-05-25 22:04:23 +02001487 EMSG2_RET_FAIL(
1488 _("E678: Invalid character after %s%%[dxouU]"),
1489 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001490 // A NUL is stored in the text as NL
1491 // TODO: what if a composing character follows?
Bram Moolenaar595cad22013-09-22 13:57:24 +02001492 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001493 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 break;
1495
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001496 // Catch \%^ and \%$ regardless of where they appear in the
1497 // pattern -- regardless of whether or not it makes sense.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 case '^':
1499 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 break;
1501
1502 case '$':
1503 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 break;
1505
1506 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001507 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 break;
1509
1510 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001511 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001512 break;
1513
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001514 case 'C':
1515 EMIT(NFA_ANY_COMPOSING);
1516 break;
1517
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001519 {
1520 int n;
1521
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001522 // \%[abc]
Bram Moolenaard7986252013-06-17 21:33:41 +02001523 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001524 {
1525 if (c == NUL)
1526 EMSG2_RET_FAIL(_(e_missing_sb),
1527 reg_magic == MAGIC_ALL);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001528 // recursive call!
Bram Moolenaard7986252013-06-17 21:33:41 +02001529 if (nfa_regatom() == FAIL)
1530 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001531 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001532 getchr(); // get the ]
Bram Moolenaar2976c022013-06-05 21:30:37 +02001533 if (n == 0)
1534 EMSG2_RET_FAIL(_(e_empty_sb),
1535 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001536 EMIT(NFA_OPT_CHARS);
1537 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001538
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001539 // Emit as "\%(\%[abc]\)" to be able to handle
1540 // "\%[abc]*" which would cause the empty string to be
1541 // matched an unlimited number of times. NFA_NOPEN is
1542 // added only once at a position, while NFA_SPLIT is
1543 // added multiple times. This is more efficient than
1544 // not allowing NFA_SPLIT multiple times, it is used
1545 // a lot.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001546 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001547 break;
1548 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001549
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001551 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001552 long_u n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001553 int cmp = c;
1554
1555 if (c == '<' || c == '>')
1556 c = getchr();
1557 while (VIM_ISDIGIT(c))
1558 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001559 long_u tmp = n * 10 + (c - '0');
1560
1561 if (tmp < n)
1562 {
1563 // overflow.
1564 emsg(_(e_value_too_large));
1565 return FAIL;
1566 }
1567 n = tmp;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001568 c = getchr();
1569 }
1570 if (c == 'l' || c == 'c' || c == 'v')
1571 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001572 long_u limit = INT_MAX;
Bram Moolenaar9403a212019-02-13 18:35:06 +01001573
Bram Moolenaar423532e2013-05-29 21:14:42 +02001574 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001575 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001576 // \%{n}l \%{n}<l \%{n}>l
Bram Moolenaar423532e2013-05-29 21:14:42 +02001577 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001578 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001579 if (save_prev_at_start)
1580 at_start = TRUE;
1581 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001582 else if (c == 'c')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001583 // \%{n}c \%{n}<c \%{n}>c
Bram Moolenaar423532e2013-05-29 21:14:42 +02001584 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001585 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001586 else
Bram Moolenaar9403a212019-02-13 18:35:06 +01001587 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001588 // \%{n}v \%{n}<v \%{n}>v
Bram Moolenaar423532e2013-05-29 21:14:42 +02001589 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001590 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar9403a212019-02-13 18:35:06 +01001591 limit = INT_MAX / MB_MAXBYTES;
1592 }
1593 if (n >= limit)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001594 {
Bram Moolenaarc5acc0f2020-06-03 18:55:38 +02001595 emsg(_(e_value_too_large));
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001596 return FAIL;
1597 }
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001598 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001599 break;
1600 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001601 else if (c == '\'' && n == 0)
1602 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001603 // \%'m \%<'m \%>'m
Bram Moolenaar044aa292013-06-04 21:27:38 +02001604 EMIT(cmp == '<' ? NFA_MARK_LT :
1605 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001606 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001607 break;
1608 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001609 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 semsg(_("E867: (NFA) Unknown operator '\\%%%c'"),
Bram Moolenaar5714b802013-05-28 22:03:20 +02001611 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 return FAIL;
1613 }
1614 break;
1615
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001616 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001617collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001618 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001619 * [abc] uses NFA_START_COLL - NFA_END_COLL
1620 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1621 * Each character is produced as a regular state, using
1622 * NFA_CONCAT to bind them together.
1623 * Besides normal characters there can be:
1624 * - character classes NFA_CLASS_*
1625 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001626 */
1627
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 p = regparse;
1629 endp = skip_anyof(p);
1630 if (*endp == ']')
1631 {
1632 /*
1633 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001634 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 * and perform the necessary substitutions in the NFA.
1636 */
1637 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001638 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639 if (result != FAIL)
1640 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001641 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001642 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001643 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 EMIT(NFA_NEWL);
1645 EMIT(NFA_OR);
1646 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001647 else
1648 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001649 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001650 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 return OK;
1652 }
1653 /*
1654 * Failed to recognize a character class. Use the simple
1655 * version that turns [abc] into 'a' OR 'b' OR 'c'
1656 */
1657 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 negated = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001659 if (*regparse == '^') // negated range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001660 {
1661 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001662 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001663 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001664 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001665 else
1666 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001667 if (*regparse == '-')
1668 {
1669 startc = '-';
1670 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001671 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001672 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001673 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001674 // Emit the OR branches for each character in the []
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001675 emit_range = FALSE;
1676 while (regparse < endp)
1677 {
1678 oldstartc = startc;
1679 startc = -1;
1680 got_coll_char = FALSE;
1681 if (*regparse == '[')
1682 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001683 // Check for [: :], [= =], [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 equiclass = collclass = 0;
1685 charclass = get_char_class(&regparse);
1686 if (charclass == CLASS_NONE)
1687 {
1688 equiclass = get_equi_class(&regparse);
1689 if (equiclass == 0)
1690 collclass = get_coll_element(&regparse);
1691 }
1692
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001693 // Character class like [:alpha:]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001694 if (charclass != CLASS_NONE)
1695 {
1696 switch (charclass)
1697 {
1698 case CLASS_ALNUM:
1699 EMIT(NFA_CLASS_ALNUM);
1700 break;
1701 case CLASS_ALPHA:
1702 EMIT(NFA_CLASS_ALPHA);
1703 break;
1704 case CLASS_BLANK:
1705 EMIT(NFA_CLASS_BLANK);
1706 break;
1707 case CLASS_CNTRL:
1708 EMIT(NFA_CLASS_CNTRL);
1709 break;
1710 case CLASS_DIGIT:
1711 EMIT(NFA_CLASS_DIGIT);
1712 break;
1713 case CLASS_GRAPH:
1714 EMIT(NFA_CLASS_GRAPH);
1715 break;
1716 case CLASS_LOWER:
Bram Moolenaar66c50c52021-01-02 17:43:49 +01001717 wants_nfa = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001718 EMIT(NFA_CLASS_LOWER);
1719 break;
1720 case CLASS_PRINT:
1721 EMIT(NFA_CLASS_PRINT);
1722 break;
1723 case CLASS_PUNCT:
1724 EMIT(NFA_CLASS_PUNCT);
1725 break;
1726 case CLASS_SPACE:
1727 EMIT(NFA_CLASS_SPACE);
1728 break;
1729 case CLASS_UPPER:
Bram Moolenaar66c50c52021-01-02 17:43:49 +01001730 wants_nfa = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001731 EMIT(NFA_CLASS_UPPER);
1732 break;
1733 case CLASS_XDIGIT:
1734 EMIT(NFA_CLASS_XDIGIT);
1735 break;
1736 case CLASS_TAB:
1737 EMIT(NFA_CLASS_TAB);
1738 break;
1739 case CLASS_RETURN:
1740 EMIT(NFA_CLASS_RETURN);
1741 break;
1742 case CLASS_BACKSPACE:
1743 EMIT(NFA_CLASS_BACKSPACE);
1744 break;
1745 case CLASS_ESCAPE:
1746 EMIT(NFA_CLASS_ESCAPE);
1747 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01001748 case CLASS_IDENT:
1749 EMIT(NFA_CLASS_IDENT);
1750 break;
1751 case CLASS_KEYWORD:
1752 EMIT(NFA_CLASS_KEYWORD);
1753 break;
1754 case CLASS_FNAME:
1755 EMIT(NFA_CLASS_FNAME);
1756 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001757 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001758 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001759 continue;
1760 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001761 // Try equivalence class [=a=] and the like
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001762 if (equiclass != 0)
1763 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001764 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 if (result == FAIL)
1766 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001767 // should never happen
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1769 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001770 continue;
1771 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001772 // Try collating class like [. .]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 if (collclass != 0)
1774 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001775 startc = collclass; // allow [.a.]-x as a range
1776 // Will emit the proper atom at the end of the
1777 // while loop.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001778 }
1779 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001780 // Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1781 // start character.
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001782 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 {
1784 emit_range = TRUE;
1785 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001786 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001787 continue; // reading the end of the range
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 }
1789
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001790 // Now handle simple and escaped characters.
1791 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1792 // accepts "\t", "\e", etc., but only when the 'l' flag in
1793 // 'cpoptions' is not included.
1794 // Posix doesn't recognize backslash at all.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001795 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001796 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 && regparse + 1 <= endp
1798 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001799 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 && vim_strchr(REGEXP_ABBR, regparse[1])
1801 != NULL)
1802 )
1803 )
1804 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001805 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001807 if (*regparse == 'n')
Bram Moolenaara5483442019-02-17 20:17:02 +01001808 startc = (reg_string || emit_range
1809 || regparse[1] == '-') ? NL : NFA_NEWL;
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001810 else if (*regparse == 'd'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811 || *regparse == 'o'
1812 || *regparse == 'x'
1813 || *regparse == 'u'
1814 || *regparse == 'U'
1815 )
1816 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001817 // TODO(RE) This needs more testing
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818 startc = coll_get_char();
1819 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001820 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 }
1822 else
1823 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001824 // \r,\t,\e,\b
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 startc = backslash_trans(*regparse);
1826 }
1827 }
1828
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001829 // Normal printable char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001831 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001832
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001833 // Previous char was '-', so this char is end of range.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834 if (emit_range)
1835 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001836 endc = startc;
1837 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001839 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001840
1841 if (endc > startc + 2)
1842 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001843 // Emit a range instead of the sequence of
1844 // individual characters.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001845 if (startc == 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001846 // \x00 is translated to \x0a, start at \x01.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001847 EMIT(1);
1848 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001849 --post_ptr; // remove NFA_CONCAT
Bram Moolenaar417bad22013-06-07 14:08:30 +02001850 EMIT(endc);
1851 EMIT(NFA_RANGE);
1852 EMIT(NFA_CONCAT);
1853 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01001854 else if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855 || (*mb_char2len)(endc) > 1))
1856 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001857 // Emit the characters in the range.
1858 // "startc" was already emitted, so skip it.
1859 //
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001860 for (c = startc + 1; c <= endc; c++)
1861 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001862 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001863 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001864 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001865 }
1866 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001867 {
1868#ifdef EBCDIC
1869 int alpha_only = FALSE;
1870
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001871 // for alphabetical range skip the gaps
1872 // 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001873 if (isalpha(startc) && isalpha(endc))
1874 alpha_only = TRUE;
1875#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001876 // Emit the range. "startc" was already emitted, so
1877 // skip it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878 for (c = startc + 1; c <= endc; c++)
1879#ifdef EBCDIC
1880 if (!alpha_only || isalpha(startc))
1881#endif
1882 {
1883 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001884 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001886 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001887 emit_range = FALSE;
1888 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 }
1890 else
1891 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001892 // This char (startc) is not part of a range. Just
1893 // emit it.
1894 // Normally, simply emit startc. But if we get char
1895 // code=0 from a collating char, then replace it with
1896 // 0x0a.
1897 // This is needed to completely mimic the behaviour of
1898 // the backtracking engine.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001899 if (startc == NFA_NEWL)
1900 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001901 // Line break can't be matched as part of the
1902 // collection, add an OR below. But not for negated
1903 // range.
Bram Moolenaar417bad22013-06-07 14:08:30 +02001904 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001905 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001906 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001908 {
1909 if (got_coll_char == TRUE && startc == 0)
1910 EMIT(0x0a);
1911 else
1912 EMIT(startc);
1913 EMIT(NFA_CONCAT);
1914 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 }
1916
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001917 MB_PTR_ADV(regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001918 } // while (p < endp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001920 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001921 if (*regparse == '-') // if last, '-' is just a char
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001922 {
1923 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001924 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001927 // skip the trailing ]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001928 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001929 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001930
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001931 // Mark end of the collection.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001932 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001933 EMIT(NFA_END_NEG_COLL);
1934 else
1935 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001936
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001937 // \_[] also matches \n but it's not negated
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001938 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001939 {
1940 EMIT(reg_string ? NL : NFA_NEWL);
1941 EMIT(NFA_OR);
1942 }
1943
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944 return OK;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001945 } // if exists closing ]
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001946
1947 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001949 // FALLTHROUGH
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 default:
1952 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 int plen;
1954
1955nfa_do_multibyte:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001956 // plen is length of current char with composing chars
Bram Moolenaar47196582013-05-25 22:04:23 +02001957 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001958 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001959 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001961 int i = 0;
1962
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001963 // A base character plus composing characters, or just one
1964 // or more composing characters.
1965 // This requires creating a separate atom as if enclosing
1966 // the characters in (), where NFA_COMPOSING is the ( and
1967 // NFA_END_COMPOSING is the ). Note that right now we are
1968 // building the postfix form, not the NFA itself;
1969 // a composing char could be: a, b, c, NFA_COMPOSING
1970 // where 'b' and 'c' are chars with codes > 256.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001971 for (;;)
1972 {
1973 EMIT(c);
1974 if (i > 0)
1975 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001976 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001977 break;
1978 c = utf_ptr2char(old_regparse + i);
1979 }
1980 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 regparse = old_regparse + plen;
1982 }
1983 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001984 {
1985 c = no_Magic(c);
1986 EMIT(c);
1987 }
1988 return OK;
1989 }
1990 }
1991
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001992 return OK;
1993}
1994
1995/*
1996 * Parse something followed by possible [*+=].
1997 *
1998 * A piece is an atom, possibly followed by a multi, an indication of how many
1999 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2000 * characters: "", "a", "aa", etc.
2001 *
2002 * piece ::= atom
2003 * or atom multi
2004 */
2005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002006nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007{
2008 int i;
2009 int op;
2010 int ret;
2011 long minval, maxval;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002012 int greedy = TRUE; // Braces are prefixed with '-' ?
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002013 parse_state_T old_state;
2014 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002015 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002016 int old_post_pos;
2017 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018 int quest;
2019
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002020 // Save the current parse state, so that we can use it if <atom>{m,n} is
2021 // next.
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002022 save_parse_state(&old_state);
2023
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002024 // store current pos in the postfix form, for \{m,n} involving 0s
Bram Moolenaar16299b52013-05-30 18:45:23 +02002025 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002026
2027 ret = nfa_regatom();
2028 if (ret == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002029 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030
2031 op = peekchr();
2032 if (re_multi_type(op) == NOT_MULTI)
2033 return OK;
2034
2035 skipchr();
2036 switch (op)
2037 {
2038 case Magic('*'):
2039 EMIT(NFA_STAR);
2040 break;
2041
2042 case Magic('+'):
2043 /*
2044 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2045 * first and only submatch would be "aaa". But the backtracking
2046 * engine interprets the plus as "try matching one more time", and
2047 * a* matches a second time at the end of the input, the empty
2048 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002049 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002050 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002051 * In order to be consistent with the old engine, we replace
2052 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002054 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002055 curchr = -1;
2056 if (nfa_regatom() == FAIL)
2057 return FAIL;
2058 EMIT(NFA_STAR);
2059 EMIT(NFA_CONCAT);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002060 skipchr(); // skip the \+
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061 break;
2062
2063 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002064 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002065 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002066 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 switch(op)
2068 {
2069 case '=':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002070 // \@=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002071 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002072 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002073 case '!':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002074 // \@!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002075 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002076 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002078 op = no_Magic(getchr());
2079 if (op == '=')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002080 // \@<=
Bram Moolenaar61602c52013-06-01 19:54:43 +02002081 i = NFA_PREV_ATOM_JUST_BEFORE;
2082 else if (op == '!')
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002083 // \@<!
Bram Moolenaar61602c52013-06-01 19:54:43 +02002084 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2085 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002086 case '>':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002087 // \@>
Bram Moolenaar87953742013-06-05 18:52:40 +02002088 i = NFA_PREV_ATOM_LIKE_PATTERN;
2089 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002091 if (i == 0)
2092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002093 semsg(_("E869: (NFA) Unknown operator '\\@%c'"), op);
Bram Moolenaar61602c52013-06-01 19:54:43 +02002094 return FAIL;
2095 }
2096 EMIT(i);
2097 if (i == NFA_PREV_ATOM_JUST_BEFORE
2098 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2099 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100 break;
2101
2102 case Magic('?'):
2103 case Magic('='):
2104 EMIT(NFA_QUEST);
2105 break;
2106
2107 case Magic('{'):
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002108 // a{2,5} will expand to 'aaa?a?a?'
2109 // a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2110 // version of '?'
2111 // \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2112 // parenthesis have the same id
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002113
2114 greedy = TRUE;
2115 c2 = peekchr();
2116 if (c2 == '-' || c2 == Magic('-'))
2117 {
2118 skipchr();
2119 greedy = FALSE;
2120 }
2121 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002122 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002123
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002124 // <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2125 // <atom>*
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002126 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002127 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002128 if (greedy) // { { (match the braces)
2129 // \{}, \{0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002130 EMIT(NFA_STAR);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002131 else // { { (match the braces)
2132 // \{-}, \{-0,}
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002133 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002134 break;
2135 }
2136
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002137 // Special case: x{0} or x{-0}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002138 if (maxval == 0)
2139 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002140 // Ignore result of previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002141 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002142 // NFA_EMPTY is 0-length and works everywhere
Bram Moolenaar699c1202013-09-25 16:41:54 +02002143 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002144 return OK;
2145 }
2146
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002147 // The engine is very inefficient (uses too many states) when the
2148 // maximum is much larger than the minimum and when the maximum is
Bram Moolenaar66c50c52021-01-02 17:43:49 +01002149 // large. However, when maxval is MAX_LIMIT, it is okay, as this
2150 // will emit NFA_STAR.
2151 // Bail out if we can use the other engine, but only, when the
2152 // pattern does not need the NFA engine like (e.g. [[:upper:]]\{2,\}
2153 // does not work with with characters > 8 bit with the BT engine)
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002154 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaar66c50c52021-01-02 17:43:49 +01002155 && (maxval > 500 || maxval > minval + 200)
2156 && (maxval != MAX_LIMIT && minval < 200)
2157 && !wants_nfa)
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002158 return FAIL;
2159
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002160 // Ignore previous call to nfa_regatom()
Bram Moolenaar16299b52013-05-30 18:45:23 +02002161 post_ptr = post_start + my_post_start;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002162 // Save parse state after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002163 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002164
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2166 for (i = 0; i < maxval; i++)
2167 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002168 // Goto beginning of the repeated atom
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002169 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002170 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002171 if (nfa_regatom() == FAIL)
2172 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002173 // after "minval" times, atoms are optional
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002174 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002175 {
2176 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002177 {
2178 if (greedy)
2179 EMIT(NFA_STAR);
2180 else
2181 EMIT(NFA_STAR_NONGREEDY);
2182 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002183 else
2184 EMIT(quest);
2185 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002186 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002188 if (i + 1 > minval && maxval == MAX_LIMIT)
2189 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002190 }
2191
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002192 // Go to just after the repeated atom and the \{}
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002193 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194 curchr = -1;
2195
2196 break;
2197
2198
2199 default:
2200 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002201 } // end switch
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002202
2203 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002204 // Can't have a multi follow a multi.
Bram Moolenaar3c867da2018-06-23 14:34:28 +02002205 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002206
2207 return OK;
2208}
2209
2210/*
2211 * Parse one or more pieces, concatenated. It matches a match for the
2212 * first piece, followed by a match for the second piece, etc. Example:
2213 * "f[0-9]b", first matches "f", then a digit and then "b".
2214 *
2215 * concat ::= piece
2216 * or piece piece
2217 * or piece piece piece
2218 * etc.
2219 */
2220 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002221nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002222{
2223 int cont = TRUE;
2224 int first = TRUE;
2225
2226 while (cont)
2227 {
2228 switch (peekchr())
2229 {
2230 case NUL:
2231 case Magic('|'):
2232 case Magic('&'):
2233 case Magic(')'):
2234 cont = FALSE;
2235 break;
2236
2237 case Magic('Z'):
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002238 regflags |= RF_ICOMBINE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002239 skipchr_keepstart();
2240 break;
2241 case Magic('c'):
2242 regflags |= RF_ICASE;
2243 skipchr_keepstart();
2244 break;
2245 case Magic('C'):
2246 regflags |= RF_NOICASE;
2247 skipchr_keepstart();
2248 break;
2249 case Magic('v'):
2250 reg_magic = MAGIC_ALL;
2251 skipchr_keepstart();
2252 curchr = -1;
2253 break;
2254 case Magic('m'):
2255 reg_magic = MAGIC_ON;
2256 skipchr_keepstart();
2257 curchr = -1;
2258 break;
2259 case Magic('M'):
2260 reg_magic = MAGIC_OFF;
2261 skipchr_keepstart();
2262 curchr = -1;
2263 break;
2264 case Magic('V'):
2265 reg_magic = MAGIC_NONE;
2266 skipchr_keepstart();
2267 curchr = -1;
2268 break;
2269
2270 default:
2271 if (nfa_regpiece() == FAIL)
2272 return FAIL;
2273 if (first == FALSE)
2274 EMIT(NFA_CONCAT);
2275 else
2276 first = FALSE;
2277 break;
2278 }
2279 }
2280
2281 return OK;
2282}
2283
2284/*
2285 * Parse a branch, one or more concats, separated by "\&". It matches the
2286 * last concat, but only if all the preceding concats also match at the same
2287 * position. Examples:
2288 * "foobeep\&..." matches "foo" in "foobeep".
2289 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2290 *
2291 * branch ::= concat
2292 * or concat \& concat
2293 * or concat \& concat \& concat
2294 * etc.
2295 */
2296 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002297nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002298{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002299 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002300
Bram Moolenaar16299b52013-05-30 18:45:23 +02002301 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002302
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002303 // First branch, possibly the only one
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002304 if (nfa_regconcat() == FAIL)
2305 return FAIL;
2306
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002307 // Try next concats
Bram Moolenaar890dd052017-12-16 19:59:37 +01002308 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 {
2310 skipchr();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002311 // if concat is empty do emit a node
Bram Moolenaar890dd052017-12-16 19:59:37 +01002312 if (old_post_pos == (int)(post_ptr - post_start))
2313 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002314 EMIT(NFA_NOPEN);
2315 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002316 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002317 if (nfa_regconcat() == FAIL)
2318 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002319 // if concat is empty do emit a node
Bram Moolenaar16299b52013-05-30 18:45:23 +02002320 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002321 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002323 }
2324
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002325 // if a branch is empty, emit one node for it
Bram Moolenaar16299b52013-05-30 18:45:23 +02002326 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002327 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328
2329 return OK;
2330}
2331
2332/*
2333 * Parse a pattern, one or more branches, separated by "\|". It matches
2334 * anything that matches one of the branches. Example: "foo\|beep" matches
2335 * "foo" and matches "beep". If more than one branch matches, the first one
2336 * is used.
2337 *
2338 * pattern ::= branch
2339 * or branch \| branch
2340 * or branch \| branch \| branch
2341 * etc.
2342 */
2343 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002344nfa_reg(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 int paren) // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346{
2347 int parno = 0;
2348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349 if (paren == REG_PAREN)
2350 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002351 if (regnpar >= NSUBEXP) // Too many `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353 parno = regnpar++;
2354 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002355#ifdef FEAT_SYN_HL
2356 else if (paren == REG_ZPAREN)
2357 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002358 // Make a ZOPEN node.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002359 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002360 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002361 parno = regnzpar++;
2362 }
2363#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002364
2365 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002366 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002367
2368 while (peekchr() == Magic('|'))
2369 {
2370 skipchr();
2371 if (nfa_regbranch() == FAIL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002372 return FAIL; // cascaded error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002373 EMIT(NFA_OR);
2374 }
2375
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 // Check for proper termination.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2378 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002379 if (paren == REG_NPAREN)
2380 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2381 else
2382 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2383 }
2384 else if (paren == REG_NOPAREN && peekchr() != NUL)
2385 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002386 if (peekchr() == Magic(')'))
2387 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2388 else
2389 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2390 }
2391 /*
2392 * Here we set the flag allowing back references to this set of
2393 * parentheses.
2394 */
2395 if (paren == REG_PAREN)
2396 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002397 had_endbrace[parno] = TRUE; // have seen the close paren
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002398 EMIT(NFA_MOPEN + parno);
2399 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002400#ifdef FEAT_SYN_HL
2401 else if (paren == REG_ZPAREN)
2402 EMIT(NFA_ZOPEN + parno);
2403#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002404
2405 return OK;
2406}
2407
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002408#ifdef DEBUG
2409static char_u code[50];
2410
2411 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002412nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002413{
2414 int addnl = FALSE;
2415
2416 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2417 {
2418 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002419 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002420 }
2421
2422 STRCPY(code, "");
2423 switch (c)
2424 {
2425 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2426 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2427 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2428 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2429 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2430 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2431
Bram Moolenaar5714b802013-05-28 22:03:20 +02002432 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2433 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2434 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2435 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2436 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2437 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2438 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2439 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2440 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002441#ifdef FEAT_SYN_HL
2442 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2443 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2444 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2445 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2446 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2447 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2448 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2449 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2450 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2451#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002452 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2453
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002454 case NFA_PREV_ATOM_NO_WIDTH:
2455 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002456 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2457 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002458 case NFA_PREV_ATOM_JUST_BEFORE:
2459 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2460 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2461 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002462 case NFA_PREV_ATOM_LIKE_PATTERN:
2463 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2464
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002465 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2466 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002467 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002468 case NFA_START_INVISIBLE_FIRST:
2469 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002470 case NFA_START_INVISIBLE_NEG:
2471 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002472 case NFA_START_INVISIBLE_NEG_FIRST:
2473 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002474 case NFA_START_INVISIBLE_BEFORE:
2475 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002476 case NFA_START_INVISIBLE_BEFORE_FIRST:
2477 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002478 case NFA_START_INVISIBLE_BEFORE_NEG:
2479 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002480 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2481 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002482 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002484 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002485 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002487 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2488 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002489 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002490
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002491 case NFA_MOPEN:
2492 case NFA_MOPEN1:
2493 case NFA_MOPEN2:
2494 case NFA_MOPEN3:
2495 case NFA_MOPEN4:
2496 case NFA_MOPEN5:
2497 case NFA_MOPEN6:
2498 case NFA_MOPEN7:
2499 case NFA_MOPEN8:
2500 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002501 STRCPY(code, "NFA_MOPEN(x)");
2502 code[10] = c - NFA_MOPEN + '0';
2503 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002504 case NFA_MCLOSE:
2505 case NFA_MCLOSE1:
2506 case NFA_MCLOSE2:
2507 case NFA_MCLOSE3:
2508 case NFA_MCLOSE4:
2509 case NFA_MCLOSE5:
2510 case NFA_MCLOSE6:
2511 case NFA_MCLOSE7:
2512 case NFA_MCLOSE8:
2513 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002514 STRCPY(code, "NFA_MCLOSE(x)");
2515 code[11] = c - NFA_MCLOSE + '0';
2516 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002517#ifdef FEAT_SYN_HL
2518 case NFA_ZOPEN:
2519 case NFA_ZOPEN1:
2520 case NFA_ZOPEN2:
2521 case NFA_ZOPEN3:
2522 case NFA_ZOPEN4:
2523 case NFA_ZOPEN5:
2524 case NFA_ZOPEN6:
2525 case NFA_ZOPEN7:
2526 case NFA_ZOPEN8:
2527 case NFA_ZOPEN9:
2528 STRCPY(code, "NFA_ZOPEN(x)");
2529 code[10] = c - NFA_ZOPEN + '0';
2530 break;
2531 case NFA_ZCLOSE:
2532 case NFA_ZCLOSE1:
2533 case NFA_ZCLOSE2:
2534 case NFA_ZCLOSE3:
2535 case NFA_ZCLOSE4:
2536 case NFA_ZCLOSE5:
2537 case NFA_ZCLOSE6:
2538 case NFA_ZCLOSE7:
2539 case NFA_ZCLOSE8:
2540 case NFA_ZCLOSE9:
2541 STRCPY(code, "NFA_ZCLOSE(x)");
2542 code[11] = c - NFA_ZCLOSE + '0';
2543 break;
2544#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002545 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2546 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2547 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2548 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002549 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2550 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002551 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2552 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2553 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2554 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2555 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2556 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2557 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2558 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2559 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2560 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2561 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2562 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2563 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2564 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002565 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002566
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002567 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002568 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2569 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2570 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002571 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002572 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002573
2574 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2575 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2576 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2577 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2578 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2579 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2580 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2581
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002582 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2583 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2584 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2585 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2586 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2587 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2588 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2589 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2590 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2591 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2592 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2593 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2594 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2595 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2596 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2597 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01002598 case NFA_CLASS_IDENT: STRCPY(code, "NFA_CLASS_IDENT"); break;
2599 case NFA_CLASS_KEYWORD: STRCPY(code, "NFA_CLASS_KEYWORD"); break;
2600 case NFA_CLASS_FNAME: STRCPY(code, "NFA_CLASS_FNAME"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002601
2602 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2603 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2604 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2605 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2606 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2607 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2608 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2609 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2610 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2611 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2612 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2613 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2614 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2615 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2616 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2617 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2618 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2619 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2620 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2621 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2622 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2623 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2624 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2625 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2626 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2627 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2628 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002629 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2630 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2631 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2632 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002633
2634 default:
2635 STRCPY(code, "CHAR(x)");
2636 code[5] = c;
2637 }
2638
2639 if (addnl == TRUE)
2640 STRCAT(code, " + NEWLINE ");
2641
2642}
2643
2644#ifdef ENABLE_LOG
2645static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002646static 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 +02002647
2648/*
2649 * Print the postfix notation of the current regexp.
2650 */
2651 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002652nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002653{
2654 int *p;
2655 FILE *f;
2656
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002657 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002658 if (f != NULL)
2659 {
2660 fprintf(f, "\n-------------------------\n");
2661 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002662 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002663 else if (retval == OK)
2664 fprintf(f, ">>> NFA engine succeeded !\n");
2665 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002666 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002667 {
2668 nfa_set_code(*p);
2669 fprintf(f, "%s, ", code);
2670 }
2671 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002672 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002673 fprintf(f, "%d ", *p);
2674 fprintf(f, "\n\n");
2675 fclose(f);
2676 }
2677}
2678
2679/*
2680 * Print the NFA starting with a root node "state".
2681 */
2682 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002683nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002684{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002685 garray_T indent;
2686
2687 ga_init2(&indent, 1, 64);
2688 ga_append(&indent, '\0');
2689 nfa_print_state2(debugf, state, &indent);
2690 ga_clear(&indent);
2691}
2692
2693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002694nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002695{
2696 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697
2698 if (state == NULL)
2699 return;
2700
2701 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002702
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002703 // Output indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002704 p = (char_u *)indent->ga_data;
2705 if (indent->ga_len >= 3)
2706 {
2707 int last = indent->ga_len - 3;
2708 char_u save[2];
2709
2710 STRNCPY(save, &p[last], 2);
2711 STRNCPY(&p[last], "+-", 2);
2712 fprintf(debugf, " %s", p);
2713 STRNCPY(&p[last], save, 2);
2714 }
2715 else
2716 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002717
2718 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002719 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002720 code,
2721 state->c,
2722 abs(state->id),
2723 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002724 if (state->id < 0)
2725 return;
2726
2727 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002728
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002729 // grow indent for state->out
Bram Moolenaar152e7892013-05-25 12:28:11 +02002730 indent->ga_len -= 1;
2731 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002732 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002733 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002734 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002735 ga_append(indent, '\0');
2736
2737 nfa_print_state2(debugf, state->out, indent);
2738
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002739 // replace last part of indent for state->out1
Bram Moolenaar152e7892013-05-25 12:28:11 +02002740 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002741 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002742 ga_append(indent, '\0');
2743
2744 nfa_print_state2(debugf, state->out1, indent);
2745
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002746 // shrink indent
Bram Moolenaar152e7892013-05-25 12:28:11 +02002747 indent->ga_len -= 3;
2748 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002749}
2750
2751/*
2752 * Print the NFA state machine.
2753 */
2754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002755nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002757 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002758
2759 if (debugf != NULL)
2760 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002761 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002762
Bram Moolenaar473de612013-06-08 18:19:48 +02002763 if (prog->reganch)
2764 fprintf(debugf, "reganch: %d\n", prog->reganch);
2765 if (prog->regstart != NUL)
2766 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2767 prog->regstart, prog->regstart);
2768 if (prog->match_text != NULL)
2769 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002770
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002771 fclose(debugf);
2772 }
2773}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002774#endif // ENABLE_LOG
2775#endif // DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002776
2777/*
2778 * Parse r.e. @expr and convert it into postfix form.
2779 * Return the postfix string on success, NULL otherwise.
2780 */
2781 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002782re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002783{
2784 if (nfa_reg(REG_NOPAREN) == FAIL)
2785 return NULL;
2786 EMIT(NFA_MOPEN);
2787 return post_start;
2788}
2789
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002790// NB. Some of the code below is inspired by Russ's.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002791
2792/*
2793 * Represents an NFA state plus zero or one or two arrows exiting.
2794 * if c == MATCH, no arrows out; matching state.
2795 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2796 * If c < 256, labeled arrow with character c to out.
2797 */
2798
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002799static nfa_state_T *state_ptr; // points to nfa_prog->state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800
2801/*
2802 * Allocate and initialize nfa_state_T.
2803 */
2804 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002805alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002806{
2807 nfa_state_T *s;
2808
2809 if (istate >= nstate)
2810 return NULL;
2811
2812 s = &state_ptr[istate++];
2813
2814 s->c = c;
2815 s->out = out;
2816 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002817 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002818
2819 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002820 s->lastlist[0] = 0;
2821 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822
2823 return s;
2824}
2825
2826/*
2827 * A partially built NFA without the matching state filled in.
2828 * Frag_T.start points at the start state.
2829 * Frag_T.out is a list of places that need to be set to the
2830 * next state for this fragment.
2831 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002832
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002833// Since the out pointers in the list are always
2834// uninitialized, we use the pointers themselves
2835// as storage for the Ptrlists.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002836typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002837union Ptrlist
2838{
2839 Ptrlist *next;
2840 nfa_state_T *s;
2841};
2842
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002843struct Frag
2844{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002845 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002846 Ptrlist *out;
2847};
2848typedef struct Frag Frag_T;
2849
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002850/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002851 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002852 */
2853 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002854frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002856 Frag_T n;
2857
2858 n.start = start;
2859 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002860 return n;
2861}
2862
2863/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002864 * Create singleton list containing just outp.
2865 */
2866 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002867list1(
2868 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869{
2870 Ptrlist *l;
2871
2872 l = (Ptrlist *)outp;
2873 l->next = NULL;
2874 return l;
2875}
2876
2877/*
2878 * Patch the list of states at out to point to start.
2879 */
2880 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002881patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882{
2883 Ptrlist *next;
2884
2885 for (; l; l = next)
2886 {
2887 next = l->next;
2888 l->s = s;
2889 }
2890}
2891
2892
2893/*
2894 * Join the two lists l1 and l2, returning the combination.
2895 */
2896 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002897append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002898{
2899 Ptrlist *oldl1;
2900
2901 oldl1 = l1;
2902 while (l1->next)
2903 l1 = l1->next;
2904 l1->next = l2;
2905 return oldl1;
2906}
2907
2908/*
2909 * Stack used for transforming postfix form into NFA.
2910 */
2911static Frag_T empty;
2912
2913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002914st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002915{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002916#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917 FILE *df;
2918 int *p2;
2919
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002920 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002921 if (df)
2922 {
2923 fprintf(df, "Error popping the stack!\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002924# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002925 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002926# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002927 fprintf(df, "Postfix form is: ");
Bram Moolenaar0270f382018-07-17 05:43:58 +02002928# ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002929 for (p2 = postfix; p2 < end; p2++)
2930 {
2931 nfa_set_code(*p2);
2932 fprintf(df, "%s, ", code);
2933 }
2934 nfa_set_code(*p);
2935 fprintf(df, "\nCurrent position is: ");
2936 for (p2 = postfix; p2 <= p; p2 ++)
2937 {
2938 nfa_set_code(*p2);
2939 fprintf(df, "%s, ", code);
2940 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02002941# else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002942 for (p2 = postfix; p2 < end; p2++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943 fprintf(df, "%d, ", *p2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002944 fprintf(df, "\nCurrent position is: ");
2945 for (p2 = postfix; p2 <= p; p2 ++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002946 fprintf(df, "%d, ", *p2);
Bram Moolenaar0270f382018-07-17 05:43:58 +02002947# endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002948 fprintf(df, "\n--------------------------\n");
2949 fclose(df);
2950 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002951#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952 emsg(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953}
2954
2955/*
2956 * Push an item onto the stack.
2957 */
2958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002959st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002960{
2961 Frag_T *stackp = *p;
2962
2963 if (stackp >= stack_end)
2964 return;
2965 *stackp = s;
2966 *p = *p + 1;
2967}
2968
2969/*
2970 * Pop an item from the stack.
2971 */
2972 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002973st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002974{
2975 Frag_T *stackp;
2976
2977 *p = *p - 1;
2978 stackp = *p;
2979 if (stackp < stack)
2980 return empty;
2981 return **p;
2982}
2983
2984/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002985 * Estimate the maximum byte length of anything matching "state".
2986 * When unknown or unlimited return -1.
2987 */
2988 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002989nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002990{
2991 int l, r;
2992 nfa_state_T *state = startstate;
2993 int len = 0;
2994
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002995 // detect looping in a NFA_SPLIT
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002996 if (depth > 4)
2997 return -1;
2998
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002999 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003000 {
3001 switch (state->c)
3002 {
3003 case NFA_END_INVISIBLE:
3004 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003005 // the end, return what we have
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003006 return len;
3007
3008 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003009 // two alternatives, use the maximum
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003010 l = nfa_max_width(state->out, depth + 1);
3011 r = nfa_max_width(state->out1, depth + 1);
3012 if (l < 0 || r < 0)
3013 return -1;
3014 return len + (l > r ? l : r);
3015
3016 case NFA_ANY:
3017 case NFA_START_COLL:
3018 case NFA_START_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003019 // matches some character, including composing chars
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003020 if (enc_utf8)
3021 len += MB_MAXBYTES;
3022 else if (has_mbyte)
3023 len += 2;
3024 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003025 ++len;
3026 if (state->c != NFA_ANY)
3027 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003028 // skip over the characters
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003029 state = state->out1->out;
3030 continue;
3031 }
3032 break;
3033
3034 case NFA_DIGIT:
3035 case NFA_WHITE:
3036 case NFA_HEX:
3037 case NFA_OCTAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003038 // ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003039 ++len;
3040 break;
3041
3042 case NFA_IDENT:
3043 case NFA_SIDENT:
3044 case NFA_KWORD:
3045 case NFA_SKWORD:
3046 case NFA_FNAME:
3047 case NFA_SFNAME:
3048 case NFA_PRINT:
3049 case NFA_SPRINT:
3050 case NFA_NWHITE:
3051 case NFA_NDIGIT:
3052 case NFA_NHEX:
3053 case NFA_NOCTAL:
3054 case NFA_WORD:
3055 case NFA_NWORD:
3056 case NFA_HEAD:
3057 case NFA_NHEAD:
3058 case NFA_ALPHA:
3059 case NFA_NALPHA:
3060 case NFA_LOWER:
3061 case NFA_NLOWER:
3062 case NFA_UPPER:
3063 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003064 case NFA_LOWER_IC:
3065 case NFA_NLOWER_IC:
3066 case NFA_UPPER_IC:
3067 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003068 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003069 // possibly non-ascii
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003070 if (has_mbyte)
3071 len += 3;
3072 else
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003073 ++len;
3074 break;
3075
3076 case NFA_START_INVISIBLE:
3077 case NFA_START_INVISIBLE_NEG:
3078 case NFA_START_INVISIBLE_BEFORE:
3079 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003080 // zero-width, out1 points to the END state
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003081 state = state->out1->out;
3082 continue;
3083
3084 case NFA_BACKREF1:
3085 case NFA_BACKREF2:
3086 case NFA_BACKREF3:
3087 case NFA_BACKREF4:
3088 case NFA_BACKREF5:
3089 case NFA_BACKREF6:
3090 case NFA_BACKREF7:
3091 case NFA_BACKREF8:
3092 case NFA_BACKREF9:
3093#ifdef FEAT_SYN_HL
3094 case NFA_ZREF1:
3095 case NFA_ZREF2:
3096 case NFA_ZREF3:
3097 case NFA_ZREF4:
3098 case NFA_ZREF5:
3099 case NFA_ZREF6:
3100 case NFA_ZREF7:
3101 case NFA_ZREF8:
3102 case NFA_ZREF9:
3103#endif
3104 case NFA_NEWL:
3105 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003106 // unknown width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003107 return -1;
3108
3109 case NFA_BOL:
3110 case NFA_EOL:
3111 case NFA_BOF:
3112 case NFA_EOF:
3113 case NFA_BOW:
3114 case NFA_EOW:
3115 case NFA_MOPEN:
3116 case NFA_MOPEN1:
3117 case NFA_MOPEN2:
3118 case NFA_MOPEN3:
3119 case NFA_MOPEN4:
3120 case NFA_MOPEN5:
3121 case NFA_MOPEN6:
3122 case NFA_MOPEN7:
3123 case NFA_MOPEN8:
3124 case NFA_MOPEN9:
3125#ifdef FEAT_SYN_HL
3126 case NFA_ZOPEN:
3127 case NFA_ZOPEN1:
3128 case NFA_ZOPEN2:
3129 case NFA_ZOPEN3:
3130 case NFA_ZOPEN4:
3131 case NFA_ZOPEN5:
3132 case NFA_ZOPEN6:
3133 case NFA_ZOPEN7:
3134 case NFA_ZOPEN8:
3135 case NFA_ZOPEN9:
3136 case NFA_ZCLOSE:
3137 case NFA_ZCLOSE1:
3138 case NFA_ZCLOSE2:
3139 case NFA_ZCLOSE3:
3140 case NFA_ZCLOSE4:
3141 case NFA_ZCLOSE5:
3142 case NFA_ZCLOSE6:
3143 case NFA_ZCLOSE7:
3144 case NFA_ZCLOSE8:
3145 case NFA_ZCLOSE9:
3146#endif
3147 case NFA_MCLOSE:
3148 case NFA_MCLOSE1:
3149 case NFA_MCLOSE2:
3150 case NFA_MCLOSE3:
3151 case NFA_MCLOSE4:
3152 case NFA_MCLOSE5:
3153 case NFA_MCLOSE6:
3154 case NFA_MCLOSE7:
3155 case NFA_MCLOSE8:
3156 case NFA_MCLOSE9:
3157 case NFA_NOPEN:
3158 case NFA_NCLOSE:
3159
3160 case NFA_LNUM_GT:
3161 case NFA_LNUM_LT:
3162 case NFA_COL_GT:
3163 case NFA_COL_LT:
3164 case NFA_VCOL_GT:
3165 case NFA_VCOL_LT:
3166 case NFA_MARK_GT:
3167 case NFA_MARK_LT:
3168 case NFA_VISUAL:
3169 case NFA_LNUM:
3170 case NFA_CURSOR:
3171 case NFA_COL:
3172 case NFA_VCOL:
3173 case NFA_MARK:
3174
3175 case NFA_ZSTART:
3176 case NFA_ZEND:
3177 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003178 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003179 case NFA_START_PATTERN:
3180 case NFA_END_PATTERN:
3181 case NFA_COMPOSING:
3182 case NFA_END_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003183 // zero-width
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003184 break;
3185
3186 default:
3187 if (state->c < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003188 // don't know what this is
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003189 return -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003190 // normal character
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003191 len += MB_CHAR2LEN(state->c);
3192 break;
3193 }
3194
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003195 // normal way to continue
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003196 state = state->out;
3197 }
3198
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003199 // unrecognized, "cannot happen"
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003200 return -1;
3201}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003202
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003203/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 * Convert a postfix form into its equivalent NFA.
3205 * Return the NFA start state on success, NULL otherwise.
3206 */
3207 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003208post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003209{
3210 int *p;
3211 int mopen;
3212 int mclose;
3213 Frag_T *stack = NULL;
3214 Frag_T *stackp = NULL;
3215 Frag_T *stack_end = NULL;
3216 Frag_T e1;
3217 Frag_T e2;
3218 Frag_T e;
3219 nfa_state_T *s;
3220 nfa_state_T *s1;
3221 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003222 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223
3224 if (postfix == NULL)
3225 return NULL;
3226
Bram Moolenaar053bb602013-05-20 13:55:21 +02003227#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003228#define POP() st_pop(&stackp, stack); \
3229 if (stackp < stack) \
3230 { \
3231 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003232 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003233 return NULL; \
3234 }
3235
3236 if (nfa_calc_size == FALSE)
3237 {
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003238 // Allocate space for the stack. Max states on the stack: "nstate".
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003239 stack = ALLOC_MULT(Frag_T, nstate + 1);
Bram Moolenaarc57463c2018-12-26 22:04:41 +01003240 if (stack == NULL)
3241 return NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003242 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003243 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 }
3245
3246 for (p = postfix; p < end; ++p)
3247 {
3248 switch (*p)
3249 {
3250 case NFA_CONCAT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003251 // Concatenation.
3252 // Pay attention: this operator does not exist in the r.e. itself
3253 // (it is implicit, really). It is added when r.e. is translated
3254 // to postfix form in re2post().
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003255 if (nfa_calc_size == TRUE)
3256 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003257 // nstate += 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258 break;
3259 }
3260 e2 = POP();
3261 e1 = POP();
3262 patch(e1.out, e2.start);
3263 PUSH(frag(e1.start, e2.out));
3264 break;
3265
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266 case NFA_OR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003267 // Alternation
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 if (nfa_calc_size == TRUE)
3269 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003270 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271 break;
3272 }
3273 e2 = POP();
3274 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003275 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003277 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 PUSH(frag(s, append(e1.out, e2.out)));
3279 break;
3280
3281 case NFA_STAR:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003282 // Zero or more, prefer more
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 if (nfa_calc_size == TRUE)
3284 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003285 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 break;
3287 }
3288 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003289 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003290 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003291 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 patch(e.out, s);
3293 PUSH(frag(s, list1(&s->out1)));
3294 break;
3295
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003296 case NFA_STAR_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003297 // Zero or more, prefer zero
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003298 if (nfa_calc_size == TRUE)
3299 {
3300 nstate++;
3301 break;
3302 }
3303 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003304 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003305 if (s == NULL)
3306 goto theend;
3307 patch(e.out, s);
3308 PUSH(frag(s, list1(&s->out)));
3309 break;
3310
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003311 case NFA_QUEST:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003312 // one or zero atoms=> greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 if (nfa_calc_size == TRUE)
3314 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003315 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 break;
3317 }
3318 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003319 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003321 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322 PUSH(frag(s, append(e.out, list1(&s->out1))));
3323 break;
3324
3325 case NFA_QUEST_NONGREEDY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003326 // zero or one atoms => non-greedy match
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327 if (nfa_calc_size == TRUE)
3328 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003329 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003330 break;
3331 }
3332 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003333 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003335 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003336 PUSH(frag(s, append(e.out, list1(&s->out))));
3337 break;
3338
Bram Moolenaar417bad22013-06-07 14:08:30 +02003339 case NFA_END_COLL:
3340 case NFA_END_NEG_COLL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003341 // On the stack is the sequence starting with NFA_START_COLL or
3342 // NFA_START_NEG_COLL and all possible characters. Patch it to
3343 // add the output to the start.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003344 if (nfa_calc_size == TRUE)
3345 {
3346 nstate++;
3347 break;
3348 }
3349 e = POP();
3350 s = alloc_state(NFA_END_COLL, NULL, NULL);
3351 if (s == NULL)
3352 goto theend;
3353 patch(e.out, s);
3354 e.start->out1 = s;
3355 PUSH(frag(e.start, list1(&s->out)));
3356 break;
3357
3358 case NFA_RANGE:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003359 // Before this are two characters, the low and high end of a
3360 // range. Turn them into two states with MIN and MAX.
Bram Moolenaar417bad22013-06-07 14:08:30 +02003361 if (nfa_calc_size == TRUE)
3362 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003363 // nstate += 0;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003364 break;
3365 }
3366 e2 = POP();
3367 e1 = POP();
3368 e2.start->val = e2.start->c;
3369 e2.start->c = NFA_RANGE_MAX;
3370 e1.start->val = e1.start->c;
3371 e1.start->c = NFA_RANGE_MIN;
3372 patch(e1.out, e2.start);
3373 PUSH(frag(e1.start, e2.out));
3374 break;
3375
Bram Moolenaar699c1202013-09-25 16:41:54 +02003376 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003377 // 0-length, used in a repetition with max/min count of 0
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003378 if (nfa_calc_size == TRUE)
3379 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003380 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003381 break;
3382 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003383 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003384 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003385 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386 PUSH(frag(s, list1(&s->out)));
3387 break;
3388
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003389 case NFA_OPT_CHARS:
3390 {
3391 int n;
3392
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003393 // \%[abc] implemented as:
3394 // NFA_SPLIT
3395 // +-CHAR(a)
3396 // | +-NFA_SPLIT
3397 // | +-CHAR(b)
3398 // | | +-NFA_SPLIT
3399 // | | +-CHAR(c)
3400 // | | | +-next
3401 // | | +- next
3402 // | +- next
3403 // +- next
3404 n = *++p; // get number of characters
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003405 if (nfa_calc_size == TRUE)
3406 {
3407 nstate += n;
3408 break;
3409 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003410 s = NULL; // avoid compiler warning
3411 e1.out = NULL; // stores list with out1's
3412 s1 = NULL; // previous NFA_SPLIT to connect to
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003413 while (n-- > 0)
3414 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003415 e = POP(); // get character
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003416 s = alloc_state(NFA_SPLIT, e.start, NULL);
3417 if (s == NULL)
3418 goto theend;
3419 if (e1.out == NULL)
3420 e1 = e;
3421 patch(e.out, s1);
3422 append(e1.out, list1(&s->out1));
3423 s1 = s;
3424 }
3425 PUSH(frag(s, e1.out));
3426 break;
3427 }
3428
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003429 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003430 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003431 case NFA_PREV_ATOM_JUST_BEFORE:
3432 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003433 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003434 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003435 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3436 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003437 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003438 int start_state;
3439 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003440 int n = 0;
3441 nfa_state_T *zend;
3442 nfa_state_T *skip;
3443
Bram Moolenaardecd9542013-06-07 16:31:50 +02003444 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003445 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003446 case NFA_PREV_ATOM_NO_WIDTH:
3447 start_state = NFA_START_INVISIBLE;
3448 end_state = NFA_END_INVISIBLE;
3449 break;
3450 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3451 start_state = NFA_START_INVISIBLE_NEG;
3452 end_state = NFA_END_INVISIBLE_NEG;
3453 break;
3454 case NFA_PREV_ATOM_JUST_BEFORE:
3455 start_state = NFA_START_INVISIBLE_BEFORE;
3456 end_state = NFA_END_INVISIBLE;
3457 break;
3458 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3459 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3460 end_state = NFA_END_INVISIBLE_NEG;
3461 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003462 default: // NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaardecd9542013-06-07 16:31:50 +02003463 start_state = NFA_START_PATTERN;
3464 end_state = NFA_END_PATTERN;
3465 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003466 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003467
3468 if (before)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003469 n = *++p; // get the count
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003470
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003471 // The \@= operator: match the preceding atom with zero width.
3472 // The \@! operator: no match for the preceding atom.
3473 // The \@<= operator: match for the preceding atom.
3474 // The \@<! operator: no match for the preceding atom.
3475 // Surrounds the preceding atom with START_INVISIBLE and
3476 // END_INVISIBLE, similarly to MOPEN.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003477
3478 if (nfa_calc_size == TRUE)
3479 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003480 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003481 break;
3482 }
3483 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003484 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003486 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487
Bram Moolenaar87953742013-06-05 18:52:40 +02003488 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003490 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003491 if (pattern)
3492 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003493 // NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02003494 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003495 if (skip == NULL)
3496 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003497 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003498 if (zend == NULL)
3499 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003500 s1->out= skip;
3501 patch(e.out, zend);
3502 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003503 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003504 else
3505 {
3506 patch(e.out, s1);
3507 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003508 if (before)
3509 {
3510 if (n <= 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003511 // See if we can guess the maximum width, it avoids a
3512 // lot of pointless tries.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003513 n = nfa_max_width(e.start, 0);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003514 s->val = n; // store the count
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003515 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003516 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003517 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003518 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003520 case NFA_COMPOSING: // char with composing char
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003521#if 0
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003522 // TODO
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003523 if (regflags & RF_ICOMBINE)
3524 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003525 // use the base character only
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003526 }
3527#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003528 // FALLTHROUGH
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003529
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003530 case NFA_MOPEN: // \( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003531 case NFA_MOPEN1:
3532 case NFA_MOPEN2:
3533 case NFA_MOPEN3:
3534 case NFA_MOPEN4:
3535 case NFA_MOPEN5:
3536 case NFA_MOPEN6:
3537 case NFA_MOPEN7:
3538 case NFA_MOPEN8:
3539 case NFA_MOPEN9:
3540#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003541 case NFA_ZOPEN: // \z( \) Submatch
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003542 case NFA_ZOPEN1:
3543 case NFA_ZOPEN2:
3544 case NFA_ZOPEN3:
3545 case NFA_ZOPEN4:
3546 case NFA_ZOPEN5:
3547 case NFA_ZOPEN6:
3548 case NFA_ZOPEN7:
3549 case NFA_ZOPEN8:
3550 case NFA_ZOPEN9:
3551#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003552 case NFA_NOPEN: // \%( \) "Invisible Submatch"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003553 if (nfa_calc_size == TRUE)
3554 {
3555 nstate += 2;
3556 break;
3557 }
3558
3559 mopen = *p;
3560 switch (*p)
3561 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003562 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3563#ifdef FEAT_SYN_HL
3564 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3565 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3566 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3567 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3568 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3569 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3570 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3571 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3572 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3573 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3574#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003575 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003576 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003577 // NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 mclose = *p + NSUBEXP;
3579 break;
3580 }
3581
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003582 // Allow "NFA_MOPEN" as a valid postfix representation for
3583 // the empty regexp "". In this case, the NFA will be
3584 // NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3585 // empty groups of parenthesis, and empty mbyte chars
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 if (stackp == stack)
3587 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003588 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003589 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003590 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003591 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003592 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003593 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 patch(list1(&s->out), s1);
3595 PUSH(frag(s, list1(&s1->out)));
3596 break;
3597 }
3598
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003599 // At least one node was emitted before NFA_MOPEN, so
3600 // at least one node will be between NFA_MOPEN and NFA_MCLOSE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003601 e = POP();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003602 s = alloc_state(mopen, e.start, NULL); // `('
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003603 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003604 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003606 s1 = alloc_state(mclose, NULL, NULL); // `)'
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003607 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003608 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003609 patch(e.out, s1);
3610
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003611 if (mopen == NFA_COMPOSING)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003612 // COMPOSING->out1 = END_COMPOSING
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 patch(list1(&s->out1), s1);
3614
3615 PUSH(frag(s, list1(&s1->out)));
3616 break;
3617
Bram Moolenaar5714b802013-05-28 22:03:20 +02003618 case NFA_BACKREF1:
3619 case NFA_BACKREF2:
3620 case NFA_BACKREF3:
3621 case NFA_BACKREF4:
3622 case NFA_BACKREF5:
3623 case NFA_BACKREF6:
3624 case NFA_BACKREF7:
3625 case NFA_BACKREF8:
3626 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003627#ifdef FEAT_SYN_HL
3628 case NFA_ZREF1:
3629 case NFA_ZREF2:
3630 case NFA_ZREF3:
3631 case NFA_ZREF4:
3632 case NFA_ZREF5:
3633 case NFA_ZREF6:
3634 case NFA_ZREF7:
3635 case NFA_ZREF8:
3636 case NFA_ZREF9:
3637#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003638 if (nfa_calc_size == TRUE)
3639 {
3640 nstate += 2;
3641 break;
3642 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003643 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003644 if (s == NULL)
3645 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003646 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003647 if (s1 == NULL)
3648 goto theend;
3649 patch(list1(&s->out), s1);
3650 PUSH(frag(s, list1(&s1->out)));
3651 break;
3652
Bram Moolenaar423532e2013-05-29 21:14:42 +02003653 case NFA_LNUM:
3654 case NFA_LNUM_GT:
3655 case NFA_LNUM_LT:
3656 case NFA_VCOL:
3657 case NFA_VCOL_GT:
3658 case NFA_VCOL_LT:
3659 case NFA_COL:
3660 case NFA_COL_GT:
3661 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003662 case NFA_MARK:
3663 case NFA_MARK_GT:
3664 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003665 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003666 int n = *++p; // lnum, col or mark name
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003667
Bram Moolenaar423532e2013-05-29 21:14:42 +02003668 if (nfa_calc_size == TRUE)
3669 {
3670 nstate += 1;
3671 break;
3672 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003673 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003674 if (s == NULL)
3675 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003676 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003677 PUSH(frag(s, list1(&s->out)));
3678 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003679 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003680
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003681 case NFA_ZSTART:
3682 case NFA_ZEND:
3683 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003684 // Operands
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685 if (nfa_calc_size == TRUE)
3686 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003687 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 break;
3689 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003690 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003691 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003692 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003693 PUSH(frag(s, list1(&s->out)));
3694 break;
3695
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003696 } // switch(*p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003698 } // for(p = postfix; *p; ++p)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003699
3700 if (nfa_calc_size == TRUE)
3701 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003702 nstate++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003703 goto theend; // Return value when counting size is ignored anyway
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704 }
3705
3706 e = POP();
3707 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003708 {
3709 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003710 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 +02003711 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003712
3713 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003714 {
3715 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003716 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003717 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003718
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003719 matchstate = &state_ptr[istate++]; // the match state
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720 matchstate->c = NFA_MATCH;
3721 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003722 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003723
3724 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003725 ret = e.start;
3726
3727theend:
3728 vim_free(stack);
3729 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003730
3731#undef POP1
3732#undef PUSH1
3733#undef POP2
3734#undef PUSH2
3735#undef POP
3736#undef PUSH
3737}
3738
Bram Moolenaara2947e22013-06-11 22:44:09 +02003739/*
3740 * After building the NFA program, inspect it to add optimization hints.
3741 */
3742 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003743nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003744{
3745 int i;
3746 int c;
3747
3748 for (i = 0; i < prog->nstate; ++i)
3749 {
3750 c = prog->state[i].c;
3751 if (c == NFA_START_INVISIBLE
3752 || c == NFA_START_INVISIBLE_NEG
3753 || c == NFA_START_INVISIBLE_BEFORE
3754 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3755 {
3756 int directly;
3757
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003758 // Do it directly when what follows is possibly the end of the
3759 // match.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003760 if (match_follows(prog->state[i].out1->out, 0))
3761 directly = TRUE;
3762 else
3763 {
3764 int ch_invisible = failure_chance(prog->state[i].out, 0);
3765 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3766
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003767 // Postpone when the invisible match is expensive or has a
3768 // lower chance of failing.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003769 if (c == NFA_START_INVISIBLE_BEFORE
3770 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3771 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003772 // "before" matches are very expensive when
3773 // unbounded, always prefer what follows then,
3774 // unless what follows will always match.
3775 // Otherwise strongly prefer what follows.
Bram Moolenaara2947e22013-06-11 22:44:09 +02003776 if (prog->state[i].val <= 0 && ch_follows > 0)
3777 directly = FALSE;
3778 else
3779 directly = ch_follows * 10 < ch_invisible;
3780 }
3781 else
3782 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003783 // normal invisible, first do the one with the
3784 // highest failure chance
Bram Moolenaara2947e22013-06-11 22:44:09 +02003785 directly = ch_follows < ch_invisible;
3786 }
3787 }
3788 if (directly)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003789 // switch to the _FIRST state
Bram Moolenaara2947e22013-06-11 22:44:09 +02003790 ++prog->state[i].c;
3791 }
3792 }
3793}
3794
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003795/////////////////////////////////////////////////////////////////
3796// NFA execution code.
3797/////////////////////////////////////////////////////////////////
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003799typedef struct
3800{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003801 int in_use; // number of subexpr with useful info
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003802
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003803 // When REG_MULTI is TRUE list.multi is used, otherwise list.line.
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003804 union
3805 {
3806 struct multipos
3807 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003808 linenr_T start_lnum;
3809 linenr_T end_lnum;
3810 colnr_T start_col;
3811 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003812 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003813 struct linepos
3814 {
3815 char_u *start;
3816 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003817 } line[NSUBEXP];
3818 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003819} regsub_T;
3820
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003821typedef struct
3822{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003823 regsub_T norm; // \( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003824#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003825 regsub_T synt; // \z( .. \) matches
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003826#endif
3827} regsubs_T;
3828
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003829// nfa_pim_T stores a Postponed Invisible Match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02003830typedef struct nfa_pim_S nfa_pim_T;
3831struct nfa_pim_S
3832{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003833 int result; // NFA_PIM_*, see below
3834 nfa_state_T *state; // the invisible match start state
3835 regsubs_T subs; // submatch info, only party used
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003836 union
3837 {
3838 lpos_T pos;
3839 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003840 } end; // where the match must end
Bram Moolenaara2d95102013-06-04 14:23:05 +02003841};
3842
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003843// Values for done in nfa_pim_T.
3844#define NFA_PIM_UNUSED 0 // pim not used
3845#define NFA_PIM_TODO 1 // pim not done yet
3846#define NFA_PIM_MATCH 2 // pim executed, matches
3847#define NFA_PIM_NOMATCH 3 // pim executed, no match
Bram Moolenaara2d95102013-06-04 14:23:05 +02003848
3849
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003850// nfa_thread_T contains execution information of a NFA state
Bram Moolenaar4b417062013-05-25 20:19:50 +02003851typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003852{
3853 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003854 int count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003855 nfa_pim_T pim; // if pim.result != NFA_PIM_UNUSED: postponed
3856 // invisible match
3857 regsubs_T subs; // submatch info, only party used
Bram Moolenaar4b417062013-05-25 20:19:50 +02003858} nfa_thread_T;
3859
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003860// nfa_list_T contains the alternative NFA execution states.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003861typedef struct
3862{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003863 nfa_thread_T *t; // allocated array of states
3864 int n; // nr of states currently in "t"
3865 int len; // max nr of states in "t"
3866 int id; // ID of the list
3867 int has_pim; // TRUE when any state has a PIM
Bram Moolenaar4b417062013-05-25 20:19:50 +02003868} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003869
Bram Moolenaar5714b802013-05-28 22:03:20 +02003870#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003871static void log_subexpr(regsub_T *sub);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003872
3873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003874log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003875{
3876 log_subexpr(&subs->norm);
3877# ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003878 if (rex.nfa_has_zsubexpr)
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003879 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003880# endif
3881}
3882
Bram Moolenaar5714b802013-05-28 22:03:20 +02003883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003884log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003885{
3886 int j;
3887
3888 for (j = 0; j < sub->in_use; j++)
3889 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003890 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003891 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003892 sub->list.multi[j].start_col,
3893 (int)sub->list.multi[j].start_lnum,
3894 sub->list.multi[j].end_col,
3895 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003896 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003897 {
3898 char *s = (char *)sub->list.line[j].start;
3899 char *e = (char *)sub->list.line[j].end;
3900
Bram Moolenaar87953742013-06-05 18:52:40 +02003901 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003902 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003903 s == NULL ? "NULL" : s,
3904 e == NULL ? "NULL" : e);
3905 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003906}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003907
3908 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003909pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003910{
3911 static char buf[30];
3912
3913 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3914 buf[0] = NUL;
3915 else
3916 {
3917 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
Bram Moolenaar0270f382018-07-17 05:43:58 +02003918 : (int)(pim->end.ptr - rex.input));
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003919 }
3920 return buf;
3921}
3922
Bram Moolenaar5714b802013-05-28 22:03:20 +02003923#endif
3924
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003925// Used during execution: whether a match has been found.
Bram Moolenaar2338c322018-07-08 19:07:19 +02003926static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003927#ifdef FEAT_RELTIME
3928static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003929static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003930static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003931#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003932
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003933static void copy_sub(regsub_T *to, regsub_T *from);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003934static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003935
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003936/*
3937 * Copy postponed invisible match info from "from" to "to".
3938 */
3939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003940copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003941{
3942 to->result = from->result;
3943 to->state = from->state;
3944 copy_sub(&to->subs.norm, &from->subs.norm);
3945#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02003946 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003947 copy_sub(&to->subs.synt, &from->subs.synt);
3948#endif
3949 to->end = from->end;
3950}
3951
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003952 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003953clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003954{
3955 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003956 // Use 0xff to set lnum to -1
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003957 vim_memset(sub->list.multi, 0xff,
Bram Moolenaar0270f382018-07-17 05:43:58 +02003958 sizeof(struct multipos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003959 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02003960 vim_memset(sub->list.line, 0,
3961 sizeof(struct linepos) * rex.nfa_nsubexpr);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003962 sub->in_use = 0;
3963}
3964
3965/*
3966 * Copy the submatches from "from" to "to".
3967 */
3968 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003969copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003970{
3971 to->in_use = from->in_use;
3972 if (from->in_use > 0)
3973 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003974 // Copy the match start and end positions.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003975 if (REG_MULTI)
3976 mch_memmove(&to->list.multi[0],
3977 &from->list.multi[0],
3978 sizeof(struct multipos) * from->in_use);
3979 else
3980 mch_memmove(&to->list.line[0],
3981 &from->list.line[0],
3982 sizeof(struct linepos) * from->in_use);
3983 }
3984}
3985
3986/*
3987 * Like copy_sub() but exclude the main match.
3988 */
3989 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003990copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003991{
3992 if (to->in_use < from->in_use)
3993 to->in_use = from->in_use;
3994 if (from->in_use > 1)
3995 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003996 // Copy the match start and end positions.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003997 if (REG_MULTI)
3998 mch_memmove(&to->list.multi[1],
3999 &from->list.multi[1],
4000 sizeof(struct multipos) * (from->in_use - 1));
4001 else
4002 mch_memmove(&to->list.line[1],
4003 &from->list.line[1],
4004 sizeof(struct linepos) * (from->in_use - 1));
4005 }
4006}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004007
Bram Moolenaar428e9872013-05-30 17:05:39 +02004008/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004009 * Like copy_sub() but only do the end of the main match if \ze is present.
4010 */
4011 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004012copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004013{
Bram Moolenaar0270f382018-07-17 05:43:58 +02004014 if (rex.nfa_has_zend)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004015 {
4016 if (REG_MULTI)
4017 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004018 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004019 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004020 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4021 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004022 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004023 }
4024 else
4025 {
4026 if (from->list.line[0].end != NULL)
4027 to->list.line[0].end = from->list.line[0].end;
4028 }
4029 }
4030}
4031
4032/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004033 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004034 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004035 */
4036 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004037sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004038{
4039 int i;
4040 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004041 linenr_T s1;
4042 linenr_T s2;
4043 char_u *sp1;
4044 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004045
4046 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4047 if (REG_MULTI)
4048 {
4049 for (i = 0; i < todo; ++i)
4050 {
4051 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004052 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004053 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004054 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004055 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004056 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004057 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004058 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004059 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004060 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004061 if (s1 != -1 && sub1->list.multi[i].start_col
4062 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004063 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004064
Bram Moolenaar0270f382018-07-17 05:43:58 +02004065 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004066 {
4067 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004068 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004069 else
4070 s1 = -1;
4071 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004072 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004073 else
4074 s2 = -1;
4075 if (s1 != s2)
4076 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004077 if (s1 != -1 && sub1->list.multi[i].end_col
4078 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004079 return FALSE;
4080 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004081 }
4082 }
4083 else
4084 {
4085 for (i = 0; i < todo; ++i)
4086 {
4087 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004088 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004089 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004090 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004091 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004092 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004093 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004094 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004095 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004096 return FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004097 if (rex.nfa_has_backref)
Bram Moolenaaree482532014-05-13 15:56:51 +02004098 {
4099 if (i < sub1->in_use)
4100 sp1 = sub1->list.line[i].end;
4101 else
4102 sp1 = NULL;
4103 if (i < sub2->in_use)
4104 sp2 = sub2->list.line[i].end;
4105 else
4106 sp2 = NULL;
4107 if (sp1 != sp2)
4108 return FALSE;
4109 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004110 }
4111 }
4112
4113 return TRUE;
4114}
4115
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004116#ifdef ENABLE_LOG
4117 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004118report_state(char *action,
4119 regsub_T *sub,
4120 nfa_state_T *state,
4121 int lid,
4122 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004123{
4124 int col;
4125
4126 if (sub->in_use <= 0)
4127 col = -1;
4128 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004129 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004130 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02004131 col = (int)(sub->list.line[0].start - rex.line);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004132 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004133 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4134 action, abs(state->id), lid, state->c, code, col,
4135 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004136}
4137#endif
4138
Bram Moolenaar43e02982013-06-07 17:31:29 +02004139/*
4140 * Return TRUE if the same state is already in list "l" with the same
4141 * positions as "subs".
4142 */
4143 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004144has_state_with_pos(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004145 nfa_list_T *l, // runtime state list
4146 nfa_state_T *state, // state to update
4147 regsubs_T *subs, // pointers to subexpressions
4148 nfa_pim_T *pim) // postponed match or NULL
Bram Moolenaar43e02982013-06-07 17:31:29 +02004149{
4150 nfa_thread_T *thread;
4151 int i;
4152
4153 for (i = 0; i < l->n; ++i)
4154 {
4155 thread = &l->t[i];
4156 if (thread->state->id == state->id
4157 && sub_equal(&thread->subs.norm, &subs->norm)
4158#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004159 && (!rex.nfa_has_zsubexpr
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004160 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004161#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004162 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004163 return TRUE;
4164 }
4165 return FALSE;
4166}
4167
4168/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004169 * Return TRUE if "one" and "two" are equal. That includes when both are not
4170 * set.
4171 */
4172 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004173pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004174{
4175 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4176 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4177
4178 if (one_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004179 // one is unused: equal when two is also unused
Bram Moolenaar69b52452013-07-17 21:10:51 +02004180 return two_unused;
4181 if (two_unused)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004182 // one is used and two is not: not equal
Bram Moolenaar69b52452013-07-17 21:10:51 +02004183 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004184 // compare the state id
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004185 if (one->state->id != two->state->id)
4186 return FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004187 // compare the position
Bram Moolenaar69b52452013-07-17 21:10:51 +02004188 if (REG_MULTI)
4189 return one->end.pos.lnum == two->end.pos.lnum
4190 && one->end.pos.col == two->end.pos.col;
4191 return one->end.ptr == two->end.ptr;
4192}
4193
4194/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004195 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4196 */
4197 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004198match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004199{
4200 nfa_state_T *state = startstate;
4201
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004202 // avoid too much recursion
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004203 if (depth > 10)
4204 return FALSE;
4205
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004206 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004207 {
4208 switch (state->c)
4209 {
4210 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004211 case NFA_MCLOSE:
4212 case NFA_END_INVISIBLE:
4213 case NFA_END_INVISIBLE_NEG:
4214 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004215 return TRUE;
4216
4217 case NFA_SPLIT:
4218 return match_follows(state->out, depth + 1)
4219 || match_follows(state->out1, depth + 1);
4220
4221 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004222 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004223 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004224 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004225 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004226 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004227 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004228 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004229 case NFA_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004230 // skip ahead to next state
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004231 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004232 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004233
4234 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004235 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004236 case NFA_IDENT:
4237 case NFA_SIDENT:
4238 case NFA_KWORD:
4239 case NFA_SKWORD:
4240 case NFA_FNAME:
4241 case NFA_SFNAME:
4242 case NFA_PRINT:
4243 case NFA_SPRINT:
4244 case NFA_WHITE:
4245 case NFA_NWHITE:
4246 case NFA_DIGIT:
4247 case NFA_NDIGIT:
4248 case NFA_HEX:
4249 case NFA_NHEX:
4250 case NFA_OCTAL:
4251 case NFA_NOCTAL:
4252 case NFA_WORD:
4253 case NFA_NWORD:
4254 case NFA_HEAD:
4255 case NFA_NHEAD:
4256 case NFA_ALPHA:
4257 case NFA_NALPHA:
4258 case NFA_LOWER:
4259 case NFA_NLOWER:
4260 case NFA_UPPER:
4261 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004262 case NFA_LOWER_IC:
4263 case NFA_NLOWER_IC:
4264 case NFA_UPPER_IC:
4265 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004266 case NFA_START_COLL:
4267 case NFA_START_NEG_COLL:
4268 case NFA_NEWL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004269 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004270 return FALSE;
4271
4272 default:
4273 if (state->c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004274 // state will advance input
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275 return FALSE;
4276
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004277 // Others: zero-width or possibly zero-width, might still find
4278 // a match at the same position, keep looking.
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004279 break;
4280 }
4281 state = state->out;
4282 }
4283 return FALSE;
4284}
4285
4286
4287/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004288 * Return TRUE if "state" is already in list "l".
4289 */
4290 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004291state_in_list(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004292 nfa_list_T *l, // runtime state list
4293 nfa_state_T *state, // state to update
4294 regsubs_T *subs) // pointers to subexpressions
Bram Moolenaar43e02982013-06-07 17:31:29 +02004295{
4296 if (state->lastlist[nfa_ll_index] == l->id)
4297 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004298 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004299 return TRUE;
4300 }
4301 return FALSE;
4302}
4303
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004304// Offset used for "off" by addstate_here().
Bram Moolenaar16b35782016-09-09 20:29:50 +02004305#define ADDSTATE_HERE_OFFSET 10
4306
Bram Moolenaard05bf562013-06-30 23:24:08 +02004307/*
4308 * Add "state" and possibly what follows to state list ".".
4309 * Returns "subs_arg", possibly copied into temp_subs.
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004310 * Returns NULL when recursiveness is too deep.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004311 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004312 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004313addstate(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004314 nfa_list_T *l, // runtime state list
4315 nfa_state_T *state, // state to update
4316 regsubs_T *subs_arg, // pointers to subexpressions
4317 nfa_pim_T *pim, // postponed look-behind match
4318 int off_arg) // byte offset, when -1 go to next line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004319{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004320 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004321 int off = off_arg;
4322 int add_here = FALSE;
4323 int listindex = 0;
4324 int k;
4325 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004326 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004327 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004328 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004329 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004330 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004331 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004332 regsubs_T *subs = subs_arg;
4333 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004334#ifdef ENABLE_LOG
4335 int did_print = FALSE;
4336#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004337 static int depth = 0;
4338
4339 // This function is called recursively. When the depth is too much we run
4340 // out of stack and crash, limit recursiveness here.
Bram Moolenaar5382f122019-02-13 01:18:38 +01004341 if (++depth >= 5000 || subs == NULL)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004342 {
4343 --depth;
4344 return NULL;
4345 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004346
Bram Moolenaar16b35782016-09-09 20:29:50 +02004347 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4348 {
4349 add_here = TRUE;
4350 off = 0;
4351 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4352 }
4353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004354 switch (state->c)
4355 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004356 case NFA_NCLOSE:
4357 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004358 case NFA_MCLOSE1:
4359 case NFA_MCLOSE2:
4360 case NFA_MCLOSE3:
4361 case NFA_MCLOSE4:
4362 case NFA_MCLOSE5:
4363 case NFA_MCLOSE6:
4364 case NFA_MCLOSE7:
4365 case NFA_MCLOSE8:
4366 case NFA_MCLOSE9:
4367#ifdef FEAT_SYN_HL
4368 case NFA_ZCLOSE:
4369 case NFA_ZCLOSE1:
4370 case NFA_ZCLOSE2:
4371 case NFA_ZCLOSE3:
4372 case NFA_ZCLOSE4:
4373 case NFA_ZCLOSE5:
4374 case NFA_ZCLOSE6:
4375 case NFA_ZCLOSE7:
4376 case NFA_ZCLOSE8:
4377 case NFA_ZCLOSE9:
4378#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004379 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004380 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004381 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004382 case NFA_EMPTY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004383 // These nodes are not added themselves but their "out" and/or
4384 // "out1" may be added below.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004385 break;
4386
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004387 case NFA_BOL:
4388 case NFA_BOF:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004389 // "^" won't match past end-of-line, don't bother trying.
4390 // Except when at the end of the line, or when we are going to the
4391 // next line for a look-behind match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004392 if (rex.input > rex.line
4393 && *rex.input != NUL
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004394 && (nfa_endp == NULL
4395 || !REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02004396 || rex.lnum == nfa_endp->se_u.pos.lnum))
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004397 goto skip_add;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004398 // FALLTHROUGH
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004399
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004400 case NFA_MOPEN1:
4401 case NFA_MOPEN2:
4402 case NFA_MOPEN3:
4403 case NFA_MOPEN4:
4404 case NFA_MOPEN5:
4405 case NFA_MOPEN6:
4406 case NFA_MOPEN7:
4407 case NFA_MOPEN8:
4408 case NFA_MOPEN9:
4409#ifdef FEAT_SYN_HL
4410 case NFA_ZOPEN:
4411 case NFA_ZOPEN1:
4412 case NFA_ZOPEN2:
4413 case NFA_ZOPEN3:
4414 case NFA_ZOPEN4:
4415 case NFA_ZOPEN5:
4416 case NFA_ZOPEN6:
4417 case NFA_ZOPEN7:
4418 case NFA_ZOPEN8:
4419 case NFA_ZOPEN9:
4420#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004421 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004422 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004423 // These nodes need to be added so that we can bail out when it
4424 // was added to this list before at the same position to avoid an
4425 // endless loop for "\(\)*"
Bram Moolenaar307aa162013-06-02 16:34:21 +02004426
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004427 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004428 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004429 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004430 // This state is already in the list, don't add it again,
4431 // unless it is an MOPEN that is used for a backreference or
4432 // when there is a PIM. For NFA_MATCH check the position,
4433 // lower position is preferred.
Bram Moolenaar0270f382018-07-17 05:43:58 +02004434 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim
Bram Moolenaar9c235062014-05-13 16:44:29 +02004435 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004436 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004437 // When called from addstate_here() do insert before
4438 // existing states.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004439 if (add_here)
4440 {
4441 for (k = 0; k < l->n && k < listindex; ++k)
4442 if (l->t[k].state->id == state->id)
4443 {
4444 found = TRUE;
4445 break;
4446 }
4447 }
4448 if (!add_here || found)
4449 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004450skip_add:
4451#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004452 nfa_set_code(state->c);
4453 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4454 abs(state->id), l->id, state->c, code,
4455 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004456#endif
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004457 --depth;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004458 return subs;
4459 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004460 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004461
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004462 // Do not add the state again when it exists with the same
4463 // positions.
Bram Moolenaar69b52452013-07-17 21:10:51 +02004464 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004465 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004466 }
4467
Bram Moolenaar688b3982019-02-13 21:47:36 +01004468 // When there are backreferences or PIMs the number of states may
4469 // be (a lot) bigger than anticipated.
Bram Moolenaara0169122013-06-26 18:16:58 +02004470 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004471 {
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004472 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004473 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004474 nfa_thread_T *newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004475
Bram Moolenaar688b3982019-02-13 21:47:36 +01004476 if ((long)(newsize >> 10) >= p_mmp)
4477 {
4478 emsg(_(e_maxmempat));
4479 --depth;
4480 return NULL;
4481 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004482 if (subs != &temp_subs)
4483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004484 // "subs" may point into the current array, need to make a
4485 // copy before it becomes invalid.
Bram Moolenaard05bf562013-06-30 23:24:08 +02004486 copy_sub(&temp_subs.norm, &subs->norm);
4487#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004488 if (rex.nfa_has_zsubexpr)
Bram Moolenaard05bf562013-06-30 23:24:08 +02004489 copy_sub(&temp_subs.synt, &subs->synt);
4490#endif
4491 subs = &temp_subs;
4492 }
4493
Bram Moolenaar688b3982019-02-13 21:47:36 +01004494 newt = vim_realloc(l->t, newsize);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004495 if (newt == NULL)
4496 {
4497 // out of memory
4498 --depth;
4499 return NULL;
4500 }
4501 l->t = newt;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004502 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004503 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004504
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004505 // add the state to the list
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004506 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004507 thread = &l->t[l->n++];
4508 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004509 if (pim == NULL)
4510 thread->pim.result = NFA_PIM_UNUSED;
4511 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004512 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004513 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004514 l->has_pim = TRUE;
4515 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004516 copy_sub(&thread->subs.norm, &subs->norm);
4517#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02004518 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004519 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004520#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004521#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004522 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004523 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004524#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004525 }
4526
4527#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004528 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004529 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004530#endif
4531 switch (state->c)
4532 {
4533 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004534 break;
4535
4536 case NFA_SPLIT:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004537 // order matters here
Bram Moolenaar16b35782016-09-09 20:29:50 +02004538 subs = addstate(l, state->out, subs, pim, off_arg);
4539 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540 break;
4541
Bram Moolenaar699c1202013-09-25 16:41:54 +02004542 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004543 case NFA_NOPEN:
4544 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004545 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004546 break;
4547
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004548 case NFA_MOPEN:
4549 case NFA_MOPEN1:
4550 case NFA_MOPEN2:
4551 case NFA_MOPEN3:
4552 case NFA_MOPEN4:
4553 case NFA_MOPEN5:
4554 case NFA_MOPEN6:
4555 case NFA_MOPEN7:
4556 case NFA_MOPEN8:
4557 case NFA_MOPEN9:
4558#ifdef FEAT_SYN_HL
4559 case NFA_ZOPEN:
4560 case NFA_ZOPEN1:
4561 case NFA_ZOPEN2:
4562 case NFA_ZOPEN3:
4563 case NFA_ZOPEN4:
4564 case NFA_ZOPEN5:
4565 case NFA_ZOPEN6:
4566 case NFA_ZOPEN7:
4567 case NFA_ZOPEN8:
4568 case NFA_ZOPEN9:
4569#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004572 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004573 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004574 sub = &subs->norm;
4575 }
4576#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004577 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004578 {
4579 subidx = state->c - NFA_ZOPEN;
4580 sub = &subs->synt;
4581 }
4582#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004583 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004584 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004585 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004586 sub = &subs->norm;
4587 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004588
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004589 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004590 save_ptr = NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004591 CLEAR_FIELD(save_multipos);
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004592
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004593 // Set the position (with "off" added) in the subexpression. Save
4594 // and restore it when it was in use. Otherwise fill any gap.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004595 if (REG_MULTI)
4596 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004597 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004598 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004599 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004600 save_in_use = -1;
4601 }
4602 else
4603 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004604 save_in_use = sub->in_use;
4605 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004606 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004607 sub->list.multi[i].start_lnum = -1;
4608 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004609 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004610 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004611 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004612 if (off == -1)
4613 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004614 sub->list.multi[subidx].start_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004615 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004616 }
4617 else
4618 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004619 sub->list.multi[subidx].start_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004620 sub->list.multi[subidx].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004621 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004622 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004623 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004624 }
4625 else
4626 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004627 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004628 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004629 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004630 save_in_use = -1;
4631 }
4632 else
4633 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004634 save_in_use = sub->in_use;
4635 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004636 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004637 sub->list.line[i].start = NULL;
4638 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004639 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004640 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004641 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02004642 sub->list.line[subidx].start = rex.input + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004643 }
4644
Bram Moolenaar16b35782016-09-09 20:29:50 +02004645 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004646 if (subs == NULL)
4647 break;
4648 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004649#ifdef FEAT_SYN_HL
4650 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4651 sub = &subs->synt;
4652 else
4653#endif
4654 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004655
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004656 if (save_in_use == -1)
4657 {
4658 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004659 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004660 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004661 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004662 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004663 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004664 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004665 break;
4666
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004667 case NFA_MCLOSE:
Bram Moolenaar0270f382018-07-17 05:43:58 +02004668 if (rex.nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004669 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004670 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004671 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004672 // Do not overwrite the position set by \ze.
Bram Moolenaar16b35782016-09-09 20:29:50 +02004673 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004674 break;
4675 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004676 // FALLTHROUGH
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004677 case NFA_MCLOSE1:
4678 case NFA_MCLOSE2:
4679 case NFA_MCLOSE3:
4680 case NFA_MCLOSE4:
4681 case NFA_MCLOSE5:
4682 case NFA_MCLOSE6:
4683 case NFA_MCLOSE7:
4684 case NFA_MCLOSE8:
4685 case NFA_MCLOSE9:
4686#ifdef FEAT_SYN_HL
4687 case NFA_ZCLOSE:
4688 case NFA_ZCLOSE1:
4689 case NFA_ZCLOSE2:
4690 case NFA_ZCLOSE3:
4691 case NFA_ZCLOSE4:
4692 case NFA_ZCLOSE5:
4693 case NFA_ZCLOSE6:
4694 case NFA_ZCLOSE7:
4695 case NFA_ZCLOSE8:
4696 case NFA_ZCLOSE9:
4697#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004700 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004701 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004702 sub = &subs->norm;
4703 }
4704#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004705 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004706 {
4707 subidx = state->c - NFA_ZCLOSE;
4708 sub = &subs->synt;
4709 }
4710#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004711 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004712 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004713 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004714 sub = &subs->norm;
4715 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004716
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004717 // We don't fill in gaps here, there must have been an MOPEN that
4718 // has done that.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004719 save_in_use = sub->in_use;
4720 if (sub->in_use <= subidx)
4721 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004722 if (REG_MULTI)
4723 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004724 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004725 if (off == -1)
4726 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004727 sub->list.multi[subidx].end_lnum = rex.lnum + 1;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004728 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004729 }
4730 else
4731 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02004732 sub->list.multi[subidx].end_lnum = rex.lnum;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004733 sub->list.multi[subidx].end_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02004734 (colnr_T)(rex.input - rex.line + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004735 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004736 // avoid compiler warnings
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004737 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004738 }
4739 else
4740 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004741 save_ptr = sub->list.line[subidx].end;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004742 sub->list.line[subidx].end = rex.input + off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004743 // avoid compiler warnings
Bram Moolenaara80faa82020-04-12 19:37:17 +02004744 CLEAR_FIELD(save_multipos);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004745 }
4746
Bram Moolenaar16b35782016-09-09 20:29:50 +02004747 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004748 if (subs == NULL)
4749 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004750 // "subs" may have changed, need to set "sub" again
Bram Moolenaarebefd992013-08-14 14:18:40 +02004751#ifdef FEAT_SYN_HL
4752 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4753 sub = &subs->synt;
4754 else
4755#endif
4756 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004757
4758 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004759 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004760 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004761 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004762 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004763 break;
4764 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004765 --depth;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004766 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004767}
4768
4769/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004770 * Like addstate(), but the new state(s) are put at position "*ip".
4771 * Used for zero-width matches, next state to use is the added one.
4772 * This makes sure the order of states to be tried does not change, which
4773 * matters for alternatives.
4774 */
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004775 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004776addstate_here(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004777 nfa_list_T *l, // runtime state list
4778 nfa_state_T *state, // state to update
4779 regsubs_T *subs, // pointers to subexpressions
4780 nfa_pim_T *pim, // postponed look-behind match
Bram Moolenaar05540972016-01-30 20:31:25 +01004781 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004782{
4783 int tlen = l->n;
4784 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004785 int listidx = *ip;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004786 regsubs_T *r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004787
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004788 // First add the state(s) at the end, so that we know how many there are.
4789 // Pass the listidx as offset (avoids adding another argument to
4790 // addstate().
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004791 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
4792 if (r == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004793 return NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004794
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004795 // when "*ip" was at the end of the list, nothing to do
Bram Moolenaara2d95102013-06-04 14:23:05 +02004796 if (listidx + 1 == tlen)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004797 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004798
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004799 // re-order to put the new state at the current position
Bram Moolenaar4b417062013-05-25 20:19:50 +02004800 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004801 if (count == 0)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004802 return r; // no state got added
Bram Moolenaar428e9872013-05-30 17:05:39 +02004803 if (count == 1)
4804 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004805 // overwrite the current state
Bram Moolenaara2d95102013-06-04 14:23:05 +02004806 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004807 }
4808 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004809 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004810 if (l->n + count - 1 >= l->len)
4811 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004812 // not enough space to move the new states, reallocate the list
4813 // and move the states to the right position
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004814 int newlen = l->len * 3 / 2 + 50;
Bram Moolenaar688b3982019-02-13 21:47:36 +01004815 size_t newsize = newlen * sizeof(nfa_thread_T);
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004816 nfa_thread_T *newl;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004817
Bram Moolenaar688b3982019-02-13 21:47:36 +01004818 if ((long)(newsize >> 10) >= p_mmp)
4819 {
4820 emsg(_(e_maxmempat));
4821 return NULL;
4822 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004823 newl = alloc(newsize);
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004824 if (newl == NULL)
Bram Moolenaar15bbd6e2019-02-13 20:31:50 +01004825 return NULL;
4826 l->len = newlen;
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004827 mch_memmove(&(newl[0]),
4828 &(l->t[0]),
4829 sizeof(nfa_thread_T) * listidx);
4830 mch_memmove(&(newl[listidx]),
4831 &(l->t[l->n - count]),
4832 sizeof(nfa_thread_T) * count);
4833 mch_memmove(&(newl[listidx + count]),
4834 &(l->t[listidx + 1]),
4835 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4836 vim_free(l->t);
4837 l->t = newl;
4838 }
4839 else
4840 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004841 // make space for new states, then move them from the
4842 // end to the current position
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004843 mch_memmove(&(l->t[listidx + count]),
4844 &(l->t[listidx + 1]),
4845 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4846 mch_memmove(&(l->t[listidx]),
4847 &(l->t[l->n - 1]),
4848 sizeof(nfa_thread_T) * count);
4849 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004850 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004851 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004852 *ip = listidx - 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01004853
4854 return r;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004855}
4856
4857/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858 * Check character class "class" against current character c.
4859 */
4860 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004861check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004862{
4863 switch (class)
4864 {
4865 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004866 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004867 return OK;
4868 break;
4869 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004870 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004871 return OK;
4872 break;
4873 case NFA_CLASS_BLANK:
4874 if (c == ' ' || c == '\t')
4875 return OK;
4876 break;
4877 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004878 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879 return OK;
4880 break;
4881 case NFA_CLASS_DIGIT:
4882 if (VIM_ISDIGIT(c))
4883 return OK;
4884 break;
4885 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004886 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004887 return OK;
4888 break;
4889 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004890 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004891 return OK;
4892 break;
4893 case NFA_CLASS_PRINT:
4894 if (vim_isprintc(c))
4895 return OK;
4896 break;
4897 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004898 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004899 return OK;
4900 break;
4901 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004902 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004903 return OK;
4904 break;
4905 case NFA_CLASS_UPPER:
4906 if (MB_ISUPPER(c))
4907 return OK;
4908 break;
4909 case NFA_CLASS_XDIGIT:
4910 if (vim_isxdigit(c))
4911 return OK;
4912 break;
4913 case NFA_CLASS_TAB:
4914 if (c == '\t')
4915 return OK;
4916 break;
4917 case NFA_CLASS_RETURN:
4918 if (c == '\r')
4919 return OK;
4920 break;
4921 case NFA_CLASS_BACKSPACE:
4922 if (c == '\b')
4923 return OK;
4924 break;
4925 case NFA_CLASS_ESCAPE:
4926 if (c == '\033')
4927 return OK;
4928 break;
Bram Moolenaar221cd9f2019-01-31 15:34:40 +01004929 case NFA_CLASS_IDENT:
4930 if (vim_isIDc(c))
4931 return OK;
4932 break;
4933 case NFA_CLASS_KEYWORD:
4934 if (reg_iswordc(c))
4935 return OK;
4936 break;
4937 case NFA_CLASS_FNAME:
4938 if (vim_isfilec(c))
4939 return OK;
4940 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004941
4942 default:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004943 // should not be here :P
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004944 siemsg(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004945 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946 }
4947 return FAIL;
4948}
4949
Bram Moolenaar5714b802013-05-28 22:03:20 +02004950/*
4951 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004952 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004953 */
4954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004955match_backref(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004956 regsub_T *sub, // pointers to subexpressions
Bram Moolenaar05540972016-01-30 20:31:25 +01004957 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004958 int *bytelen) // out: length of match in bytes
Bram Moolenaar5714b802013-05-28 22:03:20 +02004959{
4960 int len;
4961
4962 if (sub->in_use <= subidx)
4963 {
4964retempty:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004965 // backref was not set, match an empty string
Bram Moolenaar5714b802013-05-28 22:03:20 +02004966 *bytelen = 0;
4967 return TRUE;
4968 }
4969
4970 if (REG_MULTI)
4971 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004972 if (sub->list.multi[subidx].start_lnum < 0
4973 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004974 goto retempty;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004975 if (sub->list.multi[subidx].start_lnum == rex.lnum
4976 && sub->list.multi[subidx].end_lnum == rex.lnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004977 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004978 len = sub->list.multi[subidx].end_col
4979 - sub->list.multi[subidx].start_col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02004980 if (cstrncmp(rex.line + sub->list.multi[subidx].start_col,
4981 rex.input, &len) == 0)
Bram Moolenaar580abea2013-06-14 20:31:28 +02004982 {
4983 *bytelen = len;
4984 return TRUE;
4985 }
4986 }
4987 else
4988 {
4989 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004990 sub->list.multi[subidx].start_lnum,
4991 sub->list.multi[subidx].start_col,
4992 sub->list.multi[subidx].end_lnum,
4993 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004994 bytelen) == RA_MATCH)
4995 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004996 }
4997 }
4998 else
4999 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005000 if (sub->list.line[subidx].start == NULL
5001 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005002 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005003 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005004 if (cstrncmp(sub->list.line[subidx].start, rex.input, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005005 {
5006 *bytelen = len;
5007 return TRUE;
5008 }
5009 }
5010 return FALSE;
5011}
5012
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005013#ifdef FEAT_SYN_HL
5014
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005015/*
5016 * Check for a match with \z subexpression "subidx".
5017 * Return TRUE if it matches.
5018 */
5019 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005020match_zref(
5021 int subidx,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005022 int *bytelen) // out: length of match in bytes
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005023{
5024 int len;
5025
5026 cleanup_zsubexpr();
5027 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5028 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005029 // backref was not set, match an empty string
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005030 *bytelen = 0;
5031 return TRUE;
5032 }
5033
5034 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005035 if (cstrncmp(re_extmatch_in->matches[subidx], rex.input, &len) == 0)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005036 {
5037 *bytelen = len;
5038 return TRUE;
5039 }
5040 return FALSE;
5041}
5042#endif
5043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005044/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005045 * Save list IDs for all NFA states of "prog" into "list".
5046 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005047 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005048 */
5049 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005050nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005052 int i;
5053 nfa_state_T *p;
5054
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005055 // Order in the list is reverse, it's a bit faster that way.
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005056 p = &prog->state[0];
5057 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005058 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005059 list[i] = p->lastlist[1];
5060 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005061 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005062 }
5063}
5064
5065/*
5066 * Restore list IDs from "list" to all NFA states.
5067 */
5068 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005069nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005070{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005071 int i;
5072 nfa_state_T *p;
5073
5074 p = &prog->state[0];
5075 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005076 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005077 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005078 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005079 }
5080}
5081
Bram Moolenaar423532e2013-05-29 21:14:42 +02005082 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005083nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005084{
5085 if (op == 1) return pos > val;
5086 if (op == 2) return pos < val;
5087 return val == pos;
5088}
5089
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005090static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005091
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005092/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005093 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005094 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5095 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005096 */
5097 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005098recursive_regmatch(
5099 nfa_state_T *state,
5100 nfa_pim_T *pim,
5101 nfa_regprog_T *prog,
5102 regsubs_T *submatch,
5103 regsubs_T *m,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005104 int **listids,
5105 int *listids_len)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005106{
Bram Moolenaar0270f382018-07-17 05:43:58 +02005107 int save_reginput_col = (int)(rex.input - rex.line);
5108 int save_reglnum = rex.lnum;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005109 int save_nfa_match = nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005110 int save_nfa_listid = rex.nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005111 save_se_T *save_nfa_endp = nfa_endp;
5112 save_se_T endpos;
5113 save_se_T *endposp = NULL;
5114 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005115 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005116
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005117 if (pim != NULL)
5118 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005119 // start at the position where the postponed match was
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005120 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005121 rex.input = rex.line + pim->end.pos.col;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005122 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005123 rex.input = pim->end.ptr;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005124 }
5125
Bram Moolenaardecd9542013-06-07 16:31:50 +02005126 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005127 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5128 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5129 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005130 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005131 // The recursive match must end at the current position. When "pim" is
5132 // not NULL it specifies the current position.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005133 endposp = &endpos;
5134 if (REG_MULTI)
5135 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005136 if (pim == NULL)
5137 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005138 endpos.se_u.pos.col = (int)(rex.input - rex.line);
5139 endpos.se_u.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005140 }
5141 else
5142 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005143 }
5144 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005145 {
5146 if (pim == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005147 endpos.se_u.ptr = rex.input;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005148 else
5149 endpos.se_u.ptr = pim->end.ptr;
5150 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005152 // Go back the specified number of bytes, or as far as the
5153 // start of the previous line, to try matching "\@<=" or
5154 // not matching "\@<!". This is very inefficient, limit the number of
5155 // bytes if possible.
Bram Moolenaarf46da702013-06-02 22:37:42 +02005156 if (state->val <= 0)
5157 {
5158 if (REG_MULTI)
5159 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005160 rex.line = reg_getline(--rex.lnum);
5161 if (rex.line == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005162 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005163 rex.line = reg_getline(++rex.lnum);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005164 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005165 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005166 }
5167 else
5168 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005169 if (REG_MULTI && (int)(rex.input - rex.line) < state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005170 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005171 // Not enough bytes in this line, go to end of
5172 // previous line.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005173 rex.line = reg_getline(--rex.lnum);
5174 if (rex.line == NULL)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005175 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005176 // can't go before the first line
Bram Moolenaar0270f382018-07-17 05:43:58 +02005177 rex.line = reg_getline(++rex.lnum);
5178 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005179 }
5180 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005181 rex.input = rex.line + STRLEN(rex.line);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005182 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005183 if ((int)(rex.input - rex.line) >= state->val)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005184 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005185 rex.input -= state->val;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005186 if (has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005187 rex.input -= mb_head_off(rex.line, rex.input);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005188 }
5189 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005190 rex.input = rex.line;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005191 }
5192 }
5193
Bram Moolenaarf46da702013-06-02 22:37:42 +02005194#ifdef ENABLE_LOG
5195 if (log_fd != stderr)
5196 fclose(log_fd);
5197 log_fd = NULL;
5198#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005199 // Have to clear the lastlist field of the NFA nodes, so that
5200 // nfa_regmatch() and addstate() can run properly after recursion.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005201 if (nfa_ll_index == 1)
5202 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005203 // Already calling nfa_regmatch() recursively. Save the lastlist[1]
5204 // values and clear them.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005205 if (*listids == NULL || *listids_len < prog->nstate)
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005206 {
Bram Moolenaar2338c322018-07-08 19:07:19 +02005207 vim_free(*listids);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005208 *listids = ALLOC_MULT(int, prog->nstate);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005209 if (*listids == NULL)
5210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005211 emsg(_("E878: (NFA) Could not allocate memory for branch traversal!"));
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005212 return 0;
5213 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02005214 *listids_len = prog->nstate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005215 }
5216 nfa_save_listids(prog, *listids);
5217 need_restore = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005218 // any value of rex.nfa_listid will do
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005219 }
5220 else
5221 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005222 // First recursive nfa_regmatch() call, switch to the second lastlist
5223 // entry. Make sure rex.nfa_listid is different from a previous
5224 // recursive call, because some states may still have this ID.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005225 ++nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005226 if (rex.nfa_listid <= rex.nfa_alt_listid)
5227 rex.nfa_listid = rex.nfa_alt_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005228 }
5229
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005230 // Call nfa_regmatch() to check if the current concat matches at this
5231 // position. The concat ends with the node NFA_END_INVISIBLE
Bram Moolenaarf46da702013-06-02 22:37:42 +02005232 nfa_endp = endposp;
5233 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005234
5235 if (need_restore)
5236 nfa_restore_listids(prog, *listids);
5237 else
5238 {
5239 --nfa_ll_index;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005240 rex.nfa_alt_listid = rex.nfa_listid;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005241 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005242
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005243 // restore position in input text
Bram Moolenaar0270f382018-07-17 05:43:58 +02005244 rex.lnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005245 if (REG_MULTI)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005246 rex.line = reg_getline(rex.lnum);
5247 rex.input = rex.line + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005248 if (result != NFA_TOO_EXPENSIVE)
5249 {
5250 nfa_match = save_nfa_match;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005251 rex.nfa_listid = save_nfa_listid;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005252 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005253 nfa_endp = save_nfa_endp;
5254
5255#ifdef ENABLE_LOG
5256 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5257 if (log_fd != NULL)
5258 {
5259 fprintf(log_fd, "****************************\n");
5260 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5261 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5262 fprintf(log_fd, "****************************\n");
5263 }
5264 else
5265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005266 emsg(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005267 log_fd = stderr;
5268 }
5269#endif
5270
5271 return result;
5272}
5273
Bram Moolenaara2d95102013-06-04 14:23:05 +02005274/*
5275 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005276 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005277 * NFA_ANY: 1
5278 * specific character: 99
5279 */
5280 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005281failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005282{
5283 int c = state->c;
5284 int l, r;
5285
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005286 // detect looping
Bram Moolenaara2d95102013-06-04 14:23:05 +02005287 if (depth > 4)
5288 return 1;
5289
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005290 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005291 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005292 case NFA_SPLIT:
5293 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005294 // avoid recursive stuff
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005295 return 1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005296 // two alternatives, use the lowest failure chance
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005297 l = failure_chance(state->out, depth + 1);
5298 r = failure_chance(state->out1, depth + 1);
5299 return l < r ? l : r;
5300
5301 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005302 // matches anything, unlikely to fail
Bram Moolenaara2d95102013-06-04 14:23:05 +02005303 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005304
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005305 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005306 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005307 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005308 // empty match works always
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005309 return 0;
5310
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005311 case NFA_START_INVISIBLE:
5312 case NFA_START_INVISIBLE_FIRST:
5313 case NFA_START_INVISIBLE_NEG:
5314 case NFA_START_INVISIBLE_NEG_FIRST:
5315 case NFA_START_INVISIBLE_BEFORE:
5316 case NFA_START_INVISIBLE_BEFORE_FIRST:
5317 case NFA_START_INVISIBLE_BEFORE_NEG:
5318 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5319 case NFA_START_PATTERN:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005320 // recursive regmatch is expensive, use low failure chance
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005321 return 5;
5322
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005323 case NFA_BOL:
5324 case NFA_EOL:
5325 case NFA_BOF:
5326 case NFA_EOF:
5327 case NFA_NEWL:
5328 return 99;
5329
5330 case NFA_BOW:
5331 case NFA_EOW:
5332 return 90;
5333
5334 case NFA_MOPEN:
5335 case NFA_MOPEN1:
5336 case NFA_MOPEN2:
5337 case NFA_MOPEN3:
5338 case NFA_MOPEN4:
5339 case NFA_MOPEN5:
5340 case NFA_MOPEN6:
5341 case NFA_MOPEN7:
5342 case NFA_MOPEN8:
5343 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005344#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005345 case NFA_ZOPEN:
5346 case NFA_ZOPEN1:
5347 case NFA_ZOPEN2:
5348 case NFA_ZOPEN3:
5349 case NFA_ZOPEN4:
5350 case NFA_ZOPEN5:
5351 case NFA_ZOPEN6:
5352 case NFA_ZOPEN7:
5353 case NFA_ZOPEN8:
5354 case NFA_ZOPEN9:
5355 case NFA_ZCLOSE:
5356 case NFA_ZCLOSE1:
5357 case NFA_ZCLOSE2:
5358 case NFA_ZCLOSE3:
5359 case NFA_ZCLOSE4:
5360 case NFA_ZCLOSE5:
5361 case NFA_ZCLOSE6:
5362 case NFA_ZCLOSE7:
5363 case NFA_ZCLOSE8:
5364 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005365#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005366 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005367 case NFA_MCLOSE1:
5368 case NFA_MCLOSE2:
5369 case NFA_MCLOSE3:
5370 case NFA_MCLOSE4:
5371 case NFA_MCLOSE5:
5372 case NFA_MCLOSE6:
5373 case NFA_MCLOSE7:
5374 case NFA_MCLOSE8:
5375 case NFA_MCLOSE9:
5376 case NFA_NCLOSE:
5377 return failure_chance(state->out, depth + 1);
5378
5379 case NFA_BACKREF1:
5380 case NFA_BACKREF2:
5381 case NFA_BACKREF3:
5382 case NFA_BACKREF4:
5383 case NFA_BACKREF5:
5384 case NFA_BACKREF6:
5385 case NFA_BACKREF7:
5386 case NFA_BACKREF8:
5387 case NFA_BACKREF9:
5388#ifdef FEAT_SYN_HL
5389 case NFA_ZREF1:
5390 case NFA_ZREF2:
5391 case NFA_ZREF3:
5392 case NFA_ZREF4:
5393 case NFA_ZREF5:
5394 case NFA_ZREF6:
5395 case NFA_ZREF7:
5396 case NFA_ZREF8:
5397 case NFA_ZREF9:
5398#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005399 // backreferences don't match in many places
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005400 return 94;
5401
5402 case NFA_LNUM_GT:
5403 case NFA_LNUM_LT:
5404 case NFA_COL_GT:
5405 case NFA_COL_LT:
5406 case NFA_VCOL_GT:
5407 case NFA_VCOL_LT:
5408 case NFA_MARK_GT:
5409 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005410 case NFA_VISUAL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005411 // before/after positions don't match very often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005412 return 85;
5413
5414 case NFA_LNUM:
5415 return 90;
5416
5417 case NFA_CURSOR:
5418 case NFA_COL:
5419 case NFA_VCOL:
5420 case NFA_MARK:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005421 // specific positions rarely match
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005422 return 98;
5423
5424 case NFA_COMPOSING:
5425 return 95;
5426
5427 default:
5428 if (c > 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005429 // character match fails often
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005430 return 95;
5431 }
5432
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005433 // something else, includes character classes
Bram Moolenaara2d95102013-06-04 14:23:05 +02005434 return 50;
5435}
5436
Bram Moolenaarf46da702013-06-02 22:37:42 +02005437/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005438 * Skip until the char "c" we know a match must start with.
5439 */
5440 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005441skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005442{
5443 char_u *s;
5444
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005445 // Used often, do some work to avoid call overhead.
Bram Moolenaara12a1612019-01-24 16:39:02 +01005446 if (!rex.reg_ic && !has_mbyte)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005447 s = vim_strbyte(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005448 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005449 s = cstrchr(rex.line + *colp, c);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005450 if (s == NULL)
5451 return FAIL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005452 *colp = (int)(s - rex.line);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005453 return OK;
5454}
5455
5456/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005457 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005458 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005459 * Returns zero for no match, 1 for a match.
5460 */
5461 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005462find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005463{
5464 colnr_T col = startcol;
5465 int c1, c2;
5466 int len1, len2;
5467 int match;
5468
5469 for (;;)
5470 {
5471 match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005472 len2 = MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005473 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5474 {
5475 c1 = PTR2CHAR(match_text + len1);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005476 c2 = PTR2CHAR(rex.line + col + len2);
Bram Moolenaar59de4172020-06-09 19:34:54 +02005477 if (c1 != c2 && (!rex.reg_ic || MB_CASEFOLD(c1) != MB_CASEFOLD(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005478 {
5479 match = FALSE;
5480 break;
5481 }
5482 len2 += MB_CHAR2LEN(c2);
5483 }
5484 if (match
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005485 // check that no composing char follows
Bram Moolenaar473de612013-06-08 18:19:48 +02005486 && !(enc_utf8
Bram Moolenaara12a1612019-01-24 16:39:02 +01005487 && utf_iscomposing(PTR2CHAR(rex.line + col + len2))))
Bram Moolenaar473de612013-06-08 18:19:48 +02005488 {
5489 cleanup_subexpr();
5490 if (REG_MULTI)
5491 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005492 rex.reg_startpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005493 rex.reg_startpos[0].col = col;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005494 rex.reg_endpos[0].lnum = rex.lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005495 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005496 }
5497 else
5498 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005499 rex.reg_startp[0] = rex.line + col;
5500 rex.reg_endp[0] = rex.line + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005501 }
5502 return 1L;
5503 }
5504
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005505 // Try finding regstart after the current match.
5506 col += MB_CHAR2LEN(regstart); // skip regstart
Bram Moolenaar473de612013-06-08 18:19:48 +02005507 if (skip_to_start(regstart, &col) == FAIL)
5508 break;
5509 }
5510 return 0L;
5511}
5512
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005513#ifdef FEAT_RELTIME
5514 static int
5515nfa_did_time_out()
5516{
5517 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5518 {
5519 if (nfa_timed_out != NULL)
5520 *nfa_timed_out = TRUE;
5521 return TRUE;
5522 }
5523 return FALSE;
5524}
5525#endif
5526
Bram Moolenaar473de612013-06-08 18:19:48 +02005527/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005528 * Main matching routine.
5529 *
Bram Moolenaar0270f382018-07-17 05:43:58 +02005530 * Run NFA to determine whether it matches rex.input.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005532 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005533 *
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005534 * Return TRUE if there is a match, FALSE if there is no match,
5535 * NFA_TOO_EXPENSIVE if we end up with too many states.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005536 * When there is a match "submatch" contains the positions.
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005537 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 * Note: Caller must ensure that: start != NULL.
5539 */
5540 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005541nfa_regmatch(
5542 nfa_regprog_T *prog,
5543 nfa_state_T *start,
5544 regsubs_T *submatch,
5545 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546{
Bram Moolenaarc6b1cc92019-05-03 11:21:05 +02005547 int result = FALSE;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005548 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005549 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005550 int go_to_nextline = FALSE;
5551 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005552 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005553 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005554 nfa_list_T *thislist;
5555 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005556 int *listids = NULL;
Bram Moolenaar2338c322018-07-08 19:07:19 +02005557 int listids_len = 0;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005558 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005559 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005560 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005561 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005562 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005563 regsubs_T *r;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005564#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005565 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005566#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005567
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005568 // Some patterns may take a long time to match, especially when using
5569 // recursive_regmatch(). Allow interrupting them with CTRL-C.
Bram Moolenaar41f12052013-08-25 17:01:42 +02005570 fast_breakcheck();
5571 if (got_int)
5572 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005573#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005574 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005575 return FALSE;
5576#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005577
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005578#ifdef NFA_REGEXP_DEBUG_LOG
5579 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5580 if (debug == NULL)
5581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005582 semsg("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005583 return FALSE;
5584 }
5585#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005586 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005587
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005588 // Allocate memory for the lists of nodes.
Bram Moolenaar0270f382018-07-17 05:43:58 +02005589 size = (prog->nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005590
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005591 list[0].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005592 list[0].len = prog->nstate + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005593 list[1].t = alloc(size);
Bram Moolenaar0270f382018-07-17 05:43:58 +02005594 list[1].len = prog->nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005595 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597
5598#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005599 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005600 if (log_fd != NULL)
5601 {
5602 fprintf(log_fd, "**********************************\n");
5603 nfa_set_code(start->c);
5604 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5605 abs(start->id), code);
5606 fprintf(log_fd, "**********************************\n");
5607 }
5608 else
5609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005610 emsg(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005611 log_fd = stderr;
5612 }
5613#endif
5614
5615 thislist = &list[0];
5616 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005617 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005618 nextlist = &list[1];
5619 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005620 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005621#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005622 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005623#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02005624 thislist->id = rex.nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005625
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005626 // Inline optimized code for addstate(thislist, start, m, 0) if we know
5627 // it's the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005628 if (toplevel)
5629 {
5630 if (REG_MULTI)
5631 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005632 m->norm.list.multi[0].start_lnum = rex.lnum;
5633 m->norm.list.multi[0].start_col = (colnr_T)(rex.input - rex.line);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005634 }
5635 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005636 m->norm.list.line[0].start = rex.input;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005637 m->norm.in_use = 1;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005638 r = addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005639 }
5640 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005641 r = addstate(thislist, start, m, NULL, 0);
5642 if (r == NULL)
5643 {
5644 nfa_match = NFA_TOO_EXPENSIVE;
5645 goto theend;
5646 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005647
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005648#define ADD_STATE_IF_MATCH(state) \
5649 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005650 add_state = state->out; \
5651 add_off = clen; \
5652 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005653
5654 /*
5655 * Run for each character.
5656 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005657 for (;;)
5658 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005659 int curc;
5660 int clen;
5661
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005662 if (has_mbyte)
5663 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005664 curc = (*mb_ptr2char)(rex.input);
5665 clen = (*mb_ptr2len)(rex.input);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005666 }
5667 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005668 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005669 curc = *rex.input;
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005670 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005673 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005674 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005675 go_to_nextline = FALSE;
5676 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005677
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005678 // swap lists
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005679 thislist = &list[flag];
5680 nextlist = &list[flag ^= 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005681 nextlist->n = 0; // clear nextlist
Bram Moolenaar196ed142013-07-21 18:59:24 +02005682 nextlist->has_pim = FALSE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02005683 ++rex.nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005684 if (prog->re_engine == AUTOMATIC_ENGINE
Bram Moolenaar0270f382018-07-17 05:43:58 +02005685 && (rex.nfa_listid >= NFA_MAX_STATES
Bram Moolenaar5ec74142018-06-23 17:14:41 +02005686# ifdef FEAT_EVAL
5687 || nfa_fail_for_testing
5688# endif
5689 ))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005690 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005691 // too many states, retry with old engine
Bram Moolenaarfda37292014-11-05 14:27:36 +01005692 nfa_match = NFA_TOO_EXPENSIVE;
5693 goto theend;
5694 }
5695
Bram Moolenaar0270f382018-07-17 05:43:58 +02005696 thislist->id = rex.nfa_listid;
5697 nextlist->id = rex.nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698
5699#ifdef ENABLE_LOG
5700 fprintf(log_fd, "------------------------------------------\n");
Bram Moolenaar0270f382018-07-17 05:43:58 +02005701 fprintf(log_fd, ">>> Reginput is \"%s\"\n", rex.input);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005702 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005704 {
5705 int i;
5706
5707 for (i = 0; i < thislist->n; i++)
5708 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5709 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710 fprintf(log_fd, "\n");
5711#endif
5712
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005713#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005714 fprintf(debug, "\n-------------------\n");
5715#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005716 /*
5717 * If the state lists are empty we can stop.
5718 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005719 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005720 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005722 // compute nextlist
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005723 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005724 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005725 // If the list gets very long there probably is something wrong.
5726 // At least allow interrupting with CTRL-C.
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005727 fast_breakcheck();
5728 if (got_int)
5729 break;
5730#ifdef FEAT_RELTIME
5731 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5732 {
5733 nfa_time_count = 0;
5734 if (nfa_did_time_out())
5735 break;
5736 }
5737#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005738 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005740#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741 nfa_set_code(t->state->c);
5742 fprintf(debug, "%s, ", code);
5743#endif
5744#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005745 {
5746 int col;
5747
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005748 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005749 col = -1;
5750 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005751 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005752 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005753 col = (int)(t->subs.norm.list.line[0].start - rex.line);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005754 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005755 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005756 abs(t->state->id), (int)t->state->c, code, col,
5757 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005758 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005759#endif
5760
5761 /*
5762 * Handle the possible codes of the current state.
5763 * The most important is NFA_MATCH.
5764 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005765 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005766 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005767 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005768 switch (t->state->c)
5769 {
5770 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005771 {
Bram Moolenaaref2dff52020-12-21 14:54:32 +01005772 // If the match is not at the start of the line, ends before a
5773 // composing characters and rex.reg_icombine is not set, that
5774 // is not really a match.
5775 if (enc_utf8 && !rex.reg_icombine
5776 && rex.input != rex.line && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005777 break;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005778
Bram Moolenaar963fee22013-05-26 21:47:28 +02005779 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005780 copy_sub(&submatch->norm, &t->subs.norm);
5781#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005782 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005783 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005784#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005785#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005786 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005787#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005788 // Found the left-most longest match, do not look at any other
5789 // states at this position. When the list of states is going
5790 // to be empty quit without advancing, so that "rex.input" is
5791 // correct.
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005792 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005793 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005794 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005795 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005796
5797 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005798 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005799 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005800 /*
5801 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005802 * NFA_START_INVISIBLE_BEFORE node.
5803 * They surround a zero-width group, used with "\@=", "\&",
5804 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005805 * If we got here, it means that the current "invisible" group
5806 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005807 * nfa_regmatch(). For a look-behind match only when it ends
5808 * in the position in "nfa_endp".
5809 * Submatches are stored in *m, and used in the parent call.
5810 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005811#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005812 if (nfa_endp != NULL)
5813 {
5814 if (REG_MULTI)
5815 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005816 (int)rex.lnum,
Bram Moolenaarf46da702013-06-02 22:37:42 +02005817 (int)nfa_endp->se_u.pos.lnum,
Bram Moolenaar0270f382018-07-17 05:43:58 +02005818 (int)(rex.input - rex.line),
Bram Moolenaarf46da702013-06-02 22:37:42 +02005819 nfa_endp->se_u.pos.col);
5820 else
5821 fprintf(log_fd, "Current col: %d, endp col: %d\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02005822 (int)(rex.input - rex.line),
5823 (int)(nfa_endp->se_u.ptr - rex.input));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005824 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005825#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005826 // If "nfa_endp" is set it's only a match if it ends at
5827 // "nfa_endp"
Bram Moolenaarf46da702013-06-02 22:37:42 +02005828 if (nfa_endp != NULL && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02005829 ? (rex.lnum != nfa_endp->se_u.pos.lnum
5830 || (int)(rex.input - rex.line)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005831 != nfa_endp->se_u.pos.col)
Bram Moolenaar0270f382018-07-17 05:43:58 +02005832 : rex.input != nfa_endp->se_u.ptr))
Bram Moolenaarf46da702013-06-02 22:37:42 +02005833 break;
5834
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005835 // do not set submatches for \@!
Bram Moolenaardecd9542013-06-07 16:31:50 +02005836 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005837 {
5838 copy_sub(&m->norm, &t->subs.norm);
5839#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005840 if (rex.nfa_has_zsubexpr)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005841 copy_sub(&m->synt, &t->subs.synt);
5842#endif
5843 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005844#ifdef ENABLE_LOG
5845 fprintf(log_fd, "Match found:\n");
5846 log_subsexpr(m);
5847#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005848 nfa_match = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005849 // See comment above at "goto nextchar".
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005850 if (nextlist->n == 0)
5851 clen = 0;
5852 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005853
5854 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005855 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005856 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005857 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005858 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005859 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005860 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005861 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005862 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005863#ifdef ENABLE_LOG
5864 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5865 failure_chance(t->state->out, 0),
5866 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005867#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005868 // Do it directly if there already is a PIM or when
5869 // nfa_postprocess() detected it will work better.
Bram Moolenaara2947e22013-06-11 22:44:09 +02005870 if (t->pim.result != NFA_PIM_UNUSED
5871 || t->state->c == NFA_START_INVISIBLE_FIRST
5872 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5873 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5874 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005875 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005876 int in_use = m->norm.in_use;
5877
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005878 // Copy submatch info for the recursive call, opposite
5879 // of what happens on success below.
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005880 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005881#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005882 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02005883 copy_sub_off(&m->synt, &t->subs.synt);
5884#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005885
Bram Moolenaara2d95102013-06-04 14:23:05 +02005886 /*
5887 * First try matching the invisible match, then what
5888 * follows.
5889 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005890 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02005891 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005892 if (result == NFA_TOO_EXPENSIVE)
5893 {
5894 nfa_match = result;
5895 goto theend;
5896 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005897
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005898 // for \@! and \@<! it is a match when the result is
5899 // FALSE
Bram Moolenaardecd9542013-06-07 16:31:50 +02005900 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005901 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5902 || t->state->c
5903 == NFA_START_INVISIBLE_BEFORE_NEG
5904 || t->state->c
5905 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005906 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005907 // Copy submatch info from the recursive call
Bram Moolenaara2d95102013-06-04 14:23:05 +02005908 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005909#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02005910 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005911 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005912#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005913 // If the pattern has \ze and it matched in the
5914 // sub pattern, use it.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005915 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005916
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005917 // t->state->out1 is the corresponding
5918 // END_INVISIBLE node; Add its out to the current
5919 // list (zero-width match).
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005920 add_here = TRUE;
5921 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005922 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005923 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005924 }
5925 else
5926 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005927 nfa_pim_T pim;
5928
Bram Moolenaara2d95102013-06-04 14:23:05 +02005929 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005930 * First try matching what follows. Only if a match
5931 * is found verify the invisible match matches. Add a
5932 * nfa_pim_T to the following states, it contains info
5933 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005934 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005935 pim.state = t->state;
5936 pim.result = NFA_PIM_TODO;
5937 pim.subs.norm.in_use = 0;
5938#ifdef FEAT_SYN_HL
5939 pim.subs.synt.in_use = 0;
5940#endif
5941 if (REG_MULTI)
5942 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02005943 pim.end.pos.col = (int)(rex.input - rex.line);
5944 pim.end.pos.lnum = rex.lnum;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005945 }
5946 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02005947 pim.end.ptr = rex.input;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005948
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005949 // t->state->out1 is the corresponding END_INVISIBLE
5950 // node; Add its out to the current list (zero-width
5951 // match).
Bram Moolenaar5567ad42019-02-12 23:05:46 +01005952 if (addstate_here(thislist, t->state->out1->out,
5953 &t->subs, &pim, &listidx) == NULL)
5954 {
5955 nfa_match = NFA_TOO_EXPENSIVE;
5956 goto theend;
5957 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005958 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005959 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005960 break;
5961
Bram Moolenaar87953742013-06-05 18:52:40 +02005962 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005963 {
5964 nfa_state_T *skip = NULL;
5965#ifdef ENABLE_LOG
5966 int skip_lid = 0;
5967#endif
5968
Bram Moolenaar63d9e732019-12-05 21:10:38 +01005969 // There is no point in trying to match the pattern if the
5970 // output state is not going to be added to the list.
Bram Moolenaar43e02982013-06-07 17:31:29 +02005971 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5972 {
5973 skip = t->state->out1->out;
5974#ifdef ENABLE_LOG
5975 skip_lid = nextlist->id;
5976#endif
5977 }
5978 else if (state_in_list(nextlist,
5979 t->state->out1->out->out, &t->subs))
5980 {
5981 skip = t->state->out1->out->out;
5982#ifdef ENABLE_LOG
5983 skip_lid = nextlist->id;
5984#endif
5985 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005986 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005987 t->state->out1->out->out, &t->subs))
5988 {
5989 skip = t->state->out1->out->out;
5990#ifdef ENABLE_LOG
5991 skip_lid = thislist->id;
5992#endif
5993 }
5994 if (skip != NULL)
5995 {
5996#ifdef ENABLE_LOG
5997 nfa_set_code(skip->c);
5998 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5999 abs(skip->id), skip_lid, skip->c, code);
6000#endif
6001 break;
6002 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006003 // Copy submatch info to the recursive call, opposite of what
6004 // happens afterwards.
Bram Moolenaar699c1202013-09-25 16:41:54 +02006005 copy_sub_off(&m->norm, &t->subs.norm);
6006#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006007 if (rex.nfa_has_zsubexpr)
Bram Moolenaar699c1202013-09-25 16:41:54 +02006008 copy_sub_off(&m->synt, &t->subs.synt);
6009#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02006010
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006011 // First try matching the pattern.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006012 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006013 submatch, m, &listids, &listids_len);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006014 if (result == NFA_TOO_EXPENSIVE)
6015 {
6016 nfa_match = result;
6017 goto theend;
6018 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006019 if (result)
6020 {
6021 int bytelen;
6022
6023#ifdef ENABLE_LOG
6024 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6025 log_subsexpr(m);
6026#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006027 // Copy submatch info from the recursive call
Bram Moolenaar87953742013-06-05 18:52:40 +02006028 copy_sub_off(&t->subs.norm, &m->norm);
6029#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006030 if (rex.nfa_has_zsubexpr)
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006031 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006032#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006033 // Now we need to skip over the matched text and then
6034 // continue with what follows.
Bram Moolenaar87953742013-06-05 18:52:40 +02006035 if (REG_MULTI)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006036 // TODO: multi-line match
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006037 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar0270f382018-07-17 05:43:58 +02006038 - (int)(rex.input - rex.line);
Bram Moolenaar87953742013-06-05 18:52:40 +02006039 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006040 bytelen = (int)(m->norm.list.line[0].end - rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02006041
6042#ifdef ENABLE_LOG
6043 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6044#endif
6045 if (bytelen == 0)
6046 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006047 // empty match, output of corresponding
6048 // NFA_END_PATTERN/NFA_SKIP to be used at current
6049 // position
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006050 add_here = TRUE;
6051 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006052 }
6053 else if (bytelen <= clen)
6054 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006055 // match current character, output of corresponding
6056 // NFA_END_PATTERN to be used at next position.
Bram Moolenaar87953742013-06-05 18:52:40 +02006057 add_state = t->state->out1->out->out;
6058 add_off = clen;
6059 }
6060 else
6061 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006062 // skip over the matched characters, set character
6063 // count in NFA_SKIP
Bram Moolenaar87953742013-06-05 18:52:40 +02006064 add_state = t->state->out1->out;
6065 add_off = bytelen;
6066 add_count = bytelen - clen;
6067 }
6068 }
6069 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006070 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006071
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006072 case NFA_BOL:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006073 if (rex.input == rex.line)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006074 {
6075 add_here = TRUE;
6076 add_state = t->state->out;
6077 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078 break;
6079
6080 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006081 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006082 {
6083 add_here = TRUE;
6084 add_state = t->state->out;
6085 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086 break;
6087
6088 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006089 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006091 if (curc == NUL)
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;
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 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006100 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006101 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006102 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006103 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006104 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006105 || (rex.input > rex.line
6106 && vim_iswordc_buf(rex.input[-1], 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
6115 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006116 result = TRUE;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006117 if (rex.input == rex.line)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006118 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006119 else if (has_mbyte)
6120 {
6121 int this_class, prev_class;
6122
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006123 // Get class of current and previous char (if it exists).
Bram Moolenaar0270f382018-07-17 05:43:58 +02006124 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 prev_class = reg_prev_class();
6126 if (this_class == prev_class
6127 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006128 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006129 }
Bram Moolenaar0270f382018-07-17 05:43:58 +02006130 else if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
6131 || (rex.input[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006132 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006133 result = FALSE;
6134 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006135 {
6136 add_here = TRUE;
6137 add_state = t->state->out;
6138 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006139 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006140
Bram Moolenaar4b780632013-05-31 22:14:52 +02006141 case NFA_BOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006142 if (rex.lnum == 0 && rex.input == rex.line
Bram Moolenaar6100d022016-10-02 16:51:57 +02006143 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006144 {
6145 add_here = TRUE;
6146 add_state = t->state->out;
6147 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006148 break;
6149
6150 case NFA_EOF:
Bram Moolenaar0270f382018-07-17 05:43:58 +02006151 if (rex.lnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006152 {
6153 add_here = TRUE;
6154 add_state = t->state->out;
6155 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006156 break;
6157
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006158 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006159 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006160 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006161 int len = 0;
6162 nfa_state_T *end;
6163 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006164 int cchars[MAX_MCO];
6165 int ccount = 0;
6166 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006168 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006169 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006170 if (utf_iscomposing(sta->c))
6171 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006172 // Only match composing character(s), ignore base
6173 // character. Used for ".{composing}" and "{composing}"
6174 // (no preceding character).
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006175 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006176 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006177 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006178 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006179 // If \Z was present, then ignore composing characters.
6180 // When ignoring the base character this always matches.
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006181 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006182 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006183 else
6184 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006185 while (sta->c != NFA_END_COMPOSING)
6186 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006187 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006188
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006189 // Check base character matches first, unless ignored.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006190 else if (len > 0 || mc == sta->c)
6191 {
6192 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006193 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006194 len += mb_char2len(mc);
6195 sta = sta->out;
6196 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006197
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006198 // We don't care about the order of composing characters.
6199 // Get them into cchars[] first.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006200 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006201 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006202 mc = mb_ptr2char(rex.input + len);
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006203 cchars[ccount++] = mc;
6204 len += mb_char2len(mc);
6205 if (ccount == MAX_MCO)
6206 break;
6207 }
6208
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006209 // Check that each composing char in the pattern matches a
6210 // composing char in the text. We do not check if all
6211 // composing chars are matched.
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006212 result = OK;
6213 while (sta->c != NFA_END_COMPOSING)
6214 {
6215 for (j = 0; j < ccount; ++j)
6216 if (cchars[j] == sta->c)
6217 break;
6218 if (j == ccount)
6219 {
6220 result = FAIL;
6221 break;
6222 }
6223 sta = sta->out;
6224 }
6225 }
6226 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006227 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006228
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006229 end = t->state->out1; // NFA_END_COMPOSING
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006230 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006231 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006232 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006233
6234 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006235 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006236 && rex.lnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006237 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006238 go_to_nextline = TRUE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006239 // Pass -1 for the offset, which means taking the position
6240 // at the start of the next line.
Bram Moolenaara2d95102013-06-04 14:23:05 +02006241 add_state = t->state->out;
6242 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006243 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006244 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006245 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006246 // match \n as if it is an ordinary character
Bram Moolenaara2d95102013-06-04 14:23:05 +02006247 add_state = t->state->out;
6248 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006249 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006250 break;
6251
Bram Moolenaar417bad22013-06-07 14:08:30 +02006252 case NFA_START_COLL:
6253 case NFA_START_NEG_COLL:
6254 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006255 // What follows is a list of characters, until NFA_END_COLL.
6256 // One of them must match or none of them must match.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006257 nfa_state_T *state;
6258 int result_if_matched;
6259 int c1, c2;
6260
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006261 // Never match EOL. If it's part of the collection it is added
6262 // as a separate state with an OR.
Bram Moolenaar417bad22013-06-07 14:08:30 +02006263 if (curc == NUL)
6264 break;
6265
6266 state = t->state->out;
6267 result_if_matched = (t->state->c == NFA_START_COLL);
6268 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006269 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006270 if (state->c == NFA_END_COLL)
6271 {
6272 result = !result_if_matched;
6273 break;
6274 }
6275 if (state->c == NFA_RANGE_MIN)
6276 {
6277 c1 = state->val;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006278 state = state->out; // advance to NFA_RANGE_MAX
Bram Moolenaar417bad22013-06-07 14:08:30 +02006279 c2 = state->val;
6280#ifdef ENABLE_LOG
6281 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6282 curc, c1, c2);
6283#endif
6284 if (curc >= c1 && curc <= c2)
6285 {
6286 result = result_if_matched;
6287 break;
6288 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006289 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006290 {
Bram Moolenaar59de4172020-06-09 19:34:54 +02006291 int curc_low = MB_CASEFOLD(curc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02006292 int done = FALSE;
6293
6294 for ( ; c1 <= c2; ++c1)
Bram Moolenaar59de4172020-06-09 19:34:54 +02006295 if (MB_CASEFOLD(c1) == curc_low)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006296 {
6297 result = result_if_matched;
6298 done = TRUE;
6299 break;
6300 }
6301 if (done)
6302 break;
6303 }
6304 }
6305 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006306 : (curc == state->c
Bram Moolenaar59de4172020-06-09 19:34:54 +02006307 || (rex.reg_ic && MB_CASEFOLD(curc)
6308 == MB_CASEFOLD(state->c))))
Bram Moolenaar417bad22013-06-07 14:08:30 +02006309 {
6310 result = result_if_matched;
6311 break;
6312 }
6313 state = state->out;
6314 }
6315 if (result)
6316 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006317 // next state is in out of the NFA_END_COLL, out1 of
6318 // START points to the END state
Bram Moolenaar417bad22013-06-07 14:08:30 +02006319 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006320 add_off = clen;
6321 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006322 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006323 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006324
6325 case NFA_ANY:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006326 // Any char except '\0', (end of input) does not match.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006327 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006328 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006329 add_state = t->state->out;
6330 add_off = clen;
6331 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006332 break;
6333
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006334 case NFA_ANY_COMPOSING:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006335 // On a composing character skip over it. Otherwise do
6336 // nothing. Always matches.
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006337 if (enc_utf8 && utf_iscomposing(curc))
6338 {
6339 add_off = clen;
6340 }
6341 else
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006342 {
6343 add_here = TRUE;
6344 add_off = 0;
6345 }
6346 add_state = t->state->out;
6347 break;
6348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006349 /*
6350 * Character classes like \a for alpha, \d for digit etc.
6351 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006352 case NFA_IDENT: // \i
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006353 result = vim_isIDc(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_SIDENT: // \I
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006358 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
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_KWORD: // \k
Bram Moolenaar0270f382018-07-17 05:43:58 +02006363 result = vim_iswordp_buf(rex.input, rex.reg_buf);
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_SKWORD: // \K
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006368 result = !VIM_ISDIGIT(curc)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006369 && vim_iswordp_buf(rex.input, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006370 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006371 break;
6372
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006373 case NFA_FNAME: // \f
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006374 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006375 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006376 break;
6377
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006378 case NFA_SFNAME: // \F
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006379 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006380 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 break;
6382
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006383 case NFA_PRINT: // \p
Bram Moolenaar0270f382018-07-17 05:43:58 +02006384 result = vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006385 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 break;
6387
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006388 case NFA_SPRINT: // \P
Bram Moolenaar0270f382018-07-17 05:43:58 +02006389 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006390 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391 break;
6392
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006393 case NFA_WHITE: // \s
Bram Moolenaar1c465442017-03-12 20:10:05 +01006394 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006395 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396 break;
6397
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006398 case NFA_NWHITE: // \S
Bram Moolenaar1c465442017-03-12 20:10:05 +01006399 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006400 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 break;
6402
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006403 case NFA_DIGIT: // \d
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006404 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006405 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006406 break;
6407
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006408 case NFA_NDIGIT: // \D
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006409 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006410 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006411 break;
6412
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006413 case NFA_HEX: // \x
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006415 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 break;
6417
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006418 case NFA_NHEX: // \X
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006420 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421 break;
6422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006423 case NFA_OCTAL: // \o
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006425 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006426 break;
6427
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006428 case NFA_NOCTAL: // \O
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006430 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431 break;
6432
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006433 case NFA_WORD: // \w
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006435 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006436 break;
6437
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006438 case NFA_NWORD: // \W
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006439 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006440 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006441 break;
6442
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006443 case NFA_HEAD: // \h
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006444 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006445 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006446 break;
6447
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006448 case NFA_NHEAD: // \H
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006449 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006450 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006451 break;
6452
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006453 case NFA_ALPHA: // \a
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006454 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006455 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006456 break;
6457
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006458 case NFA_NALPHA: // \A
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006459 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006460 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006461 break;
6462
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006463 case NFA_LOWER: // \l
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006464 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006465 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006466 break;
6467
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006468 case NFA_NLOWER: // \L
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006469 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006470 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006471 break;
6472
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006473 case NFA_UPPER: // \u
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006474 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006475 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006476 break;
6477
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006478 case NFA_NUPPER: // \U
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006479 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006480 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006481 break;
6482
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006483 case NFA_LOWER_IC: // [a-z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006484 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006485 ADD_STATE_IF_MATCH(t->state);
6486 break;
6487
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006488 case NFA_NLOWER_IC: // [^a-z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006489 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006490 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006491 ADD_STATE_IF_MATCH(t->state);
6492 break;
6493
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006494 case NFA_UPPER_IC: // [A-Z]
Bram Moolenaar6100d022016-10-02 16:51:57 +02006495 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006496 ADD_STATE_IF_MATCH(t->state);
6497 break;
6498
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006499 case NFA_NUPPER_IC: // ^[A-Z]
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006500 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006501 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006502 ADD_STATE_IF_MATCH(t->state);
6503 break;
6504
Bram Moolenaar5714b802013-05-28 22:03:20 +02006505 case NFA_BACKREF1:
6506 case NFA_BACKREF2:
6507 case NFA_BACKREF3:
6508 case NFA_BACKREF4:
6509 case NFA_BACKREF5:
6510 case NFA_BACKREF6:
6511 case NFA_BACKREF7:
6512 case NFA_BACKREF8:
6513 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006514#ifdef FEAT_SYN_HL
6515 case NFA_ZREF1:
6516 case NFA_ZREF2:
6517 case NFA_ZREF3:
6518 case NFA_ZREF4:
6519 case NFA_ZREF5:
6520 case NFA_ZREF6:
6521 case NFA_ZREF7:
6522 case NFA_ZREF8:
6523 case NFA_ZREF9:
6524#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006525 // \1 .. \9 \z1 .. \z9
Bram Moolenaar5714b802013-05-28 22:03:20 +02006526 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006527 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006528 int bytelen;
6529
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006530 if (t->state->c <= NFA_BACKREF9)
6531 {
6532 subidx = t->state->c - NFA_BACKREF1 + 1;
6533 result = match_backref(&t->subs.norm, subidx, &bytelen);
6534 }
6535#ifdef FEAT_SYN_HL
6536 else
6537 {
6538 subidx = t->state->c - NFA_ZREF1 + 1;
6539 result = match_zref(subidx, &bytelen);
6540 }
6541#endif
6542
Bram Moolenaar5714b802013-05-28 22:03:20 +02006543 if (result)
6544 {
6545 if (bytelen == 0)
6546 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006547 // empty match always works, output of NFA_SKIP to be
6548 // used next
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006549 add_here = TRUE;
6550 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006551 }
6552 else if (bytelen <= clen)
6553 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006554 // match current character, jump ahead to out of
6555 // NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006556 add_state = t->state->out->out;
6557 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006558 }
6559 else
6560 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006561 // skip over the matched characters, set character
6562 // count in NFA_SKIP
Bram Moolenaara2d95102013-06-04 14:23:05 +02006563 add_state = t->state->out;
6564 add_off = bytelen;
6565 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006566 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006567 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006568 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006569 }
6570 case NFA_SKIP:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006571 // character of previous matching \1 .. \9 or \@>
Bram Moolenaar5714b802013-05-28 22:03:20 +02006572 if (t->count - clen <= 0)
6573 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006574 // end of match, go to what follows
Bram Moolenaara2d95102013-06-04 14:23:05 +02006575 add_state = t->state->out;
6576 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006577 }
6578 else
6579 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006580 // add state again with decremented count
Bram Moolenaara2d95102013-06-04 14:23:05 +02006581 add_state = t->state;
6582 add_off = 0;
6583 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006584 }
6585 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006586
Bram Moolenaar423532e2013-05-29 21:14:42 +02006587 case NFA_LNUM:
6588 case NFA_LNUM_GT:
6589 case NFA_LNUM_LT:
6590 result = (REG_MULTI &&
6591 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006592 (long_u)(rex.lnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006593 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006594 {
6595 add_here = TRUE;
6596 add_state = t->state->out;
6597 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006598 break;
6599
6600 case NFA_COL:
6601 case NFA_COL_GT:
6602 case NFA_COL_LT:
6603 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006604 (long_u)(rex.input - rex.line) + 1);
Bram Moolenaar423532e2013-05-29 21:14:42 +02006605 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006606 {
6607 add_here = TRUE;
6608 add_state = t->state->out;
6609 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006610 break;
6611
6612 case NFA_VCOL:
6613 case NFA_VCOL_GT:
6614 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006615 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006616 int op = t->state->c - NFA_VCOL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02006617 colnr_T col = (colnr_T)(rex.input - rex.line);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006618 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006619
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006620 // Bail out quickly when there can't be a match, avoid the
6621 // overhead of win_linetabsize() on long lines.
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006622 if (op != 1 && col > t->state->val
Bram Moolenaara12a1612019-01-24 16:39:02 +01006623 * (has_mbyte ? MB_MAXBYTES : 1))
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006624 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006625 result = FALSE;
6626 if (op == 1 && col - 1 > t->state->val && col > 100)
6627 {
6628 int ts = wp->w_buffer->b_p_ts;
6629
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006630 // Guess that a character won't use more columns than
6631 // 'tabstop', with a minimum of 4.
Bram Moolenaaref795d12015-01-18 16:46:32 +01006632 if (ts < 4)
6633 ts = 4;
6634 result = col > t->state->val * ts;
6635 }
6636 if (!result)
6637 result = nfa_re_num_cmp(t->state->val, op,
Bram Moolenaar0270f382018-07-17 05:43:58 +02006638 (long_u)win_linetabsize(wp, rex.line, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006639 if (result)
6640 {
6641 add_here = TRUE;
6642 add_state = t->state->out;
6643 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006644 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006645 break;
6646
Bram Moolenaar044aa292013-06-04 21:27:38 +02006647 case NFA_MARK:
6648 case NFA_MARK_GT:
6649 case NFA_MARK_LT:
6650 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006651 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006652
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006653 // Compare the mark position to the match position.
6654 result = (pos != NULL // mark doesn't exist
6655 && pos->lnum > 0 // mark isn't set in reg_buf
Bram Moolenaar0270f382018-07-17 05:43:58 +02006656 && (pos->lnum == rex.lnum + rex.reg_firstlnum
6657 ? (pos->col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006658 ? t->state->c == NFA_MARK
Bram Moolenaar0270f382018-07-17 05:43:58 +02006659 : (pos->col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar044aa292013-06-04 21:27:38 +02006660 ? t->state->c == NFA_MARK_GT
6661 : t->state->c == NFA_MARK_LT))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006662 : (pos->lnum < rex.lnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006663 ? t->state->c == NFA_MARK_GT
6664 : t->state->c == NFA_MARK_LT)));
6665 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006666 {
6667 add_here = TRUE;
6668 add_state = t->state->out;
6669 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006670 break;
6671 }
6672
Bram Moolenaar423532e2013-05-29 21:14:42 +02006673 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006674 result = (rex.reg_win != NULL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006675 && (rex.lnum + rex.reg_firstlnum
Bram Moolenaar6100d022016-10-02 16:51:57 +02006676 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006677 && ((colnr_T)(rex.input - rex.line)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006678 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006679 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006680 {
6681 add_here = TRUE;
6682 add_state = t->state->out;
6683 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006684 break;
6685
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006686 case NFA_VISUAL:
6687 result = reg_match_visual();
6688 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006689 {
6690 add_here = TRUE;
6691 add_state = t->state->out;
6692 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006693 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006694
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006695 case NFA_MOPEN1:
6696 case NFA_MOPEN2:
6697 case NFA_MOPEN3:
6698 case NFA_MOPEN4:
6699 case NFA_MOPEN5:
6700 case NFA_MOPEN6:
6701 case NFA_MOPEN7:
6702 case NFA_MOPEN8:
6703 case NFA_MOPEN9:
6704#ifdef FEAT_SYN_HL
6705 case NFA_ZOPEN:
6706 case NFA_ZOPEN1:
6707 case NFA_ZOPEN2:
6708 case NFA_ZOPEN3:
6709 case NFA_ZOPEN4:
6710 case NFA_ZOPEN5:
6711 case NFA_ZOPEN6:
6712 case NFA_ZOPEN7:
6713 case NFA_ZOPEN8:
6714 case NFA_ZOPEN9:
6715#endif
6716 case NFA_NOPEN:
6717 case NFA_ZSTART:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006718 // These states are only added to be able to bail out when
6719 // they are added again, nothing is to be done.
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006720 break;
6721
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006722 default: // regular character
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006723 {
6724 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006725
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006726#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006727 if (c < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006728 siemsg("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006729#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006730 result = (c == curc);
6731
Bram Moolenaar6100d022016-10-02 16:51:57 +02006732 if (!result && rex.reg_ic)
Bram Moolenaar59de4172020-06-09 19:34:54 +02006733 result = MB_CASEFOLD(c) == MB_CASEFOLD(curc);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006734 // If rex.reg_icombine is not set only skip over the character
6735 // itself. When it is set skip over composing characters.
Bram Moolenaar6100d022016-10-02 16:51:57 +02006736 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006737 clen = utf_ptr2len(rex.input);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006738 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006739 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006740 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006741
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006742 } // switch (t->state->c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006743
6744 if (add_state != NULL)
6745 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006746 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006747 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006748
6749 if (t->pim.result == NFA_PIM_UNUSED)
6750 pim = NULL;
6751 else
6752 pim = &t->pim;
6753
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006754 // Handle the postponed invisible match if the match might end
6755 // without advancing and before the end of the line.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006756 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006758 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006759 {
6760#ifdef ENABLE_LOG
6761 fprintf(log_fd, "\n");
6762 fprintf(log_fd, "==================================\n");
6763 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6764 fprintf(log_fd, "\n");
6765#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006766 result = recursive_regmatch(pim->state, pim,
Bram Moolenaar2338c322018-07-08 19:07:19 +02006767 prog, submatch, m, &listids, &listids_len);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006768 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006769 // for \@! and \@<! it is a match when the result is
6770 // FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006771 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006772 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6773 || pim->state->c
6774 == NFA_START_INVISIBLE_BEFORE_NEG
6775 || pim->state->c
6776 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006777 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006778 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006779 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006780#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006781 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006782 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006783#endif
6784 }
6785 }
6786 else
6787 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006788 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006789#ifdef ENABLE_LOG
6790 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006791 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006792 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6793 fprintf(log_fd, "\n");
6794#endif
6795 }
6796
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006797 // for \@! and \@<! it is a match when result is FALSE
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006798 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006799 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6800 || pim->state->c
6801 == NFA_START_INVISIBLE_BEFORE_NEG
6802 || pim->state->c
6803 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006804 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006805 // Copy submatch info from the recursive call
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006806 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006807#ifdef FEAT_SYN_HL
Bram Moolenaar0270f382018-07-17 05:43:58 +02006808 if (rex.nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006809 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006810#endif
6811 }
6812 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006813 // look-behind match failed, don't add the state
Bram Moolenaara2d95102013-06-04 14:23:05 +02006814 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006815
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006816 // Postponed invisible match was handled, don't add it to
6817 // following states.
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006818 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006819 }
6820
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006821 // If "pim" points into l->t it will become invalid when
6822 // adding the state causes the list to be reallocated. Make a
6823 // local copy to avoid that.
Bram Moolenaara951e352013-10-06 15:46:11 +02006824 if (pim == &t->pim)
6825 {
6826 copy_pim(&pim_copy, pim);
6827 pim = &pim_copy;
6828 }
6829
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006830 if (add_here)
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006831 r = addstate_here(thislist, add_state, &t->subs,
6832 pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006833 else
6834 {
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006835 r = addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006836 if (add_count > 0)
6837 nextlist->t[nextlist->n - 1].count = add_count;
6838 }
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006839 if (r == NULL)
6840 {
6841 nfa_match = NFA_TOO_EXPENSIVE;
6842 goto theend;
6843 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006844 }
6845
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006846 } // for (thislist = thislist; thislist->state; thislist++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006847
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006848 // Look for the start of a match in the current position by adding the
6849 // start state to the list of states.
6850 // The first found match is the leftmost one, thus the order of states
6851 // matters!
6852 // Do not add the start state in recursive calls of nfa_regmatch(),
6853 // because recursive calls should only start in the first position.
6854 // Unless "nfa_endp" is not NULL, then we match the end position.
6855 // Also don't start a match past the first line.
Bram Moolenaar61602c52013-06-01 19:54:43 +02006856 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006857 && ((toplevel
Bram Moolenaar0270f382018-07-17 05:43:58 +02006858 && rex.lnum == 0
Bram Moolenaar61602c52013-06-01 19:54:43 +02006859 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006860 && (rex.reg_maxcol == 0
Bram Moolenaar0270f382018-07-17 05:43:58 +02006861 || (colnr_T)(rex.input - rex.line) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006862 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006863 && (REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006864 ? (rex.lnum < nfa_endp->se_u.pos.lnum
6865 || (rex.lnum == nfa_endp->se_u.pos.lnum
6866 && (int)(rex.input - rex.line)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006867 < nfa_endp->se_u.pos.col))
Bram Moolenaar0270f382018-07-17 05:43:58 +02006868 : rex.input < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006869 {
6870#ifdef ENABLE_LOG
6871 fprintf(log_fd, "(---) STARTSTATE\n");
6872#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006873 // Inline optimized code for addstate() if we know the state is
6874 // the first MOPEN.
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006875 if (toplevel)
6876 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006877 int add = TRUE;
6878 int c;
6879
6880 if (prog->regstart != NUL && clen != 0)
6881 {
6882 if (nextlist->n == 0)
6883 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02006884 colnr_T col = (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006885
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006886 // Nextlist is empty, we can skip ahead to the
6887 // character that must appear at the start.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006888 if (skip_to_start(prog->regstart, &col) == FAIL)
6889 break;
6890#ifdef ENABLE_LOG
6891 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
Bram Moolenaar0270f382018-07-17 05:43:58 +02006892 col - ((colnr_T)(rex.input - rex.line) + clen));
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006893#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02006894 rex.input = rex.line + col - clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006895 }
6896 else
6897 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006898 // Checking if the required start character matches is
6899 // cheaper than adding a state that won't match.
Bram Moolenaar0270f382018-07-17 05:43:58 +02006900 c = PTR2CHAR(rex.input + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006901 if (c != prog->regstart && (!rex.reg_ic
Bram Moolenaar59de4172020-06-09 19:34:54 +02006902 || MB_CASEFOLD(c) != MB_CASEFOLD(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006903 {
6904#ifdef ENABLE_LOG
6905 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6906#endif
6907 add = FALSE;
6908 }
6909 }
6910 }
6911
6912 if (add)
6913 {
6914 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006915 m->norm.list.multi[0].start_col =
Bram Moolenaar0270f382018-07-17 05:43:58 +02006916 (colnr_T)(rex.input - rex.line) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006917 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02006918 m->norm.list.line[0].start = rex.input + clen;
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006919 if (addstate(nextlist, start->out, m, NULL, clen) == NULL)
6920 {
6921 nfa_match = NFA_TOO_EXPENSIVE;
6922 goto theend;
6923 }
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006924 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006925 }
6926 else
Bram Moolenaar5567ad42019-02-12 23:05:46 +01006927 {
6928 if (addstate(nextlist, start, m, NULL, clen) == NULL)
6929 {
6930 nfa_match = NFA_TOO_EXPENSIVE;
6931 goto theend;
6932 }
6933 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006934 }
6935
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006936#ifdef ENABLE_LOG
6937 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006938 {
6939 int i;
6940
6941 for (i = 0; i < thislist->n; i++)
6942 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6943 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006944 fprintf(log_fd, "\n");
6945#endif
6946
6947nextchar:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006948 // Advance to the next character, or advance to the next line, or
6949 // finish.
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006950 if (clen != 0)
Bram Moolenaar0270f382018-07-17 05:43:58 +02006951 rex.input += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006952 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
Bram Moolenaar0270f382018-07-17 05:43:58 +02006953 && rex.lnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006954 reg_nextline();
6955 else
6956 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006957
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006958 // Allow interrupting with CTRL-C.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006959 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006960 if (got_int)
6961 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006962#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006963 // Check for timeout once in a twenty times to avoid overhead.
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006964 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6965 {
6966 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006967 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006968 break;
6969 }
6970#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006971 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006972
6973#ifdef ENABLE_LOG
6974 if (log_fd != stderr)
6975 fclose(log_fd);
6976 log_fd = NULL;
6977#endif
6978
6979theend:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01006980 // Free memory
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006981 vim_free(list[0].t);
6982 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006983 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006984#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006985#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006986 fclose(debug);
6987#endif
6988
Bram Moolenaar963fee22013-05-26 21:47:28 +02006989 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006990}
6991
6992/*
Bram Moolenaar0270f382018-07-17 05:43:58 +02006993 * Try match of "prog" with at rex.line["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006994 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006995 */
6996 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006997nfa_regtry(
6998 nfa_regprog_T *prog,
6999 colnr_T col,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007000 proftime_T *tm UNUSED, // timeout limit or NULL
7001 int *timed_out UNUSED) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007002{
7003 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007004 regsubs_T subs, m;
7005 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007006 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007007#ifdef ENABLE_LOG
7008 FILE *f;
7009#endif
7010
Bram Moolenaar0270f382018-07-17 05:43:58 +02007011 rex.input = rex.line + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007012#ifdef FEAT_RELTIME
7013 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007014 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007015 nfa_time_count = 0;
7016#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017
7018#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007019 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020 if (f != NULL)
7021 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007022 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007023#ifdef DEBUG
7024 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7025#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007026 fprintf(f, "\tInput text is \"%s\" \n", rex.input);
Bram Moolenaar87953742013-06-05 18:52:40 +02007027 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007028 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007029 fprintf(f, "\n\n");
7030 fclose(f);
7031 }
7032 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007033 emsg("Could not open temporary log file for writing");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007034#endif
7035
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007036 clear_sub(&subs.norm);
7037 clear_sub(&m.norm);
7038#ifdef FEAT_SYN_HL
7039 clear_sub(&subs.synt);
7040 clear_sub(&m.synt);
7041#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042
Bram Moolenaarfda37292014-11-05 14:27:36 +01007043 result = nfa_regmatch(prog, start, &subs, &m);
7044 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007046 else if (result == NFA_TOO_EXPENSIVE)
7047 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007048
7049 cleanup_subexpr();
7050 if (REG_MULTI)
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_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7055 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007056
Bram Moolenaar6100d022016-10-02 16:51:57 +02007057 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7058 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007059 }
7060
Bram Moolenaar6100d022016-10-02 16:51:57 +02007061 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007062 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007063 rex.reg_startpos[0].lnum = 0;
7064 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007065 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007066 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007067 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007068 // pattern has a \ze but it didn't match, use current end
Bram Moolenaar0270f382018-07-17 05:43:58 +02007069 rex.reg_endpos[0].lnum = rex.lnum;
7070 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007071 }
7072 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007073 // Use line number of "\ze".
Bram Moolenaar0270f382018-07-17 05:43:58 +02007074 rex.lnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007075 }
7076 else
7077 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007078 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007079 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007080 rex.reg_startp[i] = subs.norm.list.line[i].start;
7081 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007082 }
7083
Bram Moolenaar6100d022016-10-02 16:51:57 +02007084 if (rex.reg_startp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007085 rex.reg_startp[0] = rex.line + col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007086 if (rex.reg_endp[0] == NULL)
Bram Moolenaar0270f382018-07-17 05:43:58 +02007087 rex.reg_endp[0] = rex.input;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007088 }
7089
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007090#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007091 // Package any found \z(...\) matches for export. Default is none.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007092 unref_extmatch(re_extmatch_out);
7093 re_extmatch_out = NULL;
7094
7095 if (prog->reghasz == REX_SET)
7096 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007097 cleanup_zsubexpr();
7098 re_extmatch_out = make_extmatch();
Bram Moolenaar7c77b342019-12-22 19:40:40 +01007099 if (re_extmatch_out == NULL)
7100 return 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007101 // Loop over \z1, \z2, etc. There is no \z0.
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007102 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007103 {
7104 if (REG_MULTI)
7105 {
7106 struct multipos *mpos = &subs.synt.list.multi[i];
7107
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007108 // Only accept single line matches that are valid.
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007109 if (mpos->start_lnum >= 0
7110 && mpos->start_lnum == mpos->end_lnum
7111 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007112 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007113 vim_strnsave(reg_getline(mpos->start_lnum)
7114 + mpos->start_col,
7115 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007116 }
7117 else
7118 {
7119 struct linepos *lpos = &subs.synt.list.line[i];
7120
7121 if (lpos->start != NULL && lpos->end != NULL)
7122 re_extmatch_out->matches[i] =
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007123 vim_strnsave(lpos->start, lpos->end - lpos->start);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007124 }
7125 }
7126 }
7127#endif
7128
Bram Moolenaar0270f382018-07-17 05:43:58 +02007129 return 1 + rex.lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007130}
7131
7132/*
7133 * Match a regexp against a string ("line" points to the string) or multiple
7134 * lines ("line" is NULL, use reg_getline()).
7135 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007136 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007137 */
7138 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007139nfa_regexec_both(
7140 char_u *line,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007141 colnr_T startcol, // column to start looking for match
7142 proftime_T *tm, // timeout limit or NULL
7143 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007144{
7145 nfa_regprog_T *prog;
7146 long retval = 0L;
7147 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007148 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149
7150 if (REG_MULTI)
7151 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007152 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007153 line = reg_getline((linenr_T)0); // relative to the cursor
Bram Moolenaar6100d022016-10-02 16:51:57 +02007154 rex.reg_startpos = rex.reg_mmatch->startpos;
7155 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007156 }
7157 else
7158 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007159 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7160 rex.reg_startp = rex.reg_match->startp;
7161 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007162 }
7163
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007164 // Be paranoid...
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007165 if (prog == NULL || line == NULL)
7166 {
Bram Moolenaare83cca22020-09-07 18:53:21 +02007167 iemsg(_(e_null));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168 goto theend;
7169 }
7170
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007171 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007172 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007173 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007174 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007175 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007176
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007177 // If pattern contains "\Z" overrule value of rex.reg_icombine
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007178 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007179 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007180
Bram Moolenaar0270f382018-07-17 05:43:58 +02007181 rex.line = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007182 rex.lnum = 0; // relative to line
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007183
Bram Moolenaar0270f382018-07-17 05:43:58 +02007184 rex.nfa_has_zend = prog->has_zend;
7185 rex.nfa_has_backref = prog->has_backref;
7186 rex.nfa_nsubexpr = prog->nsubexp;
7187 rex.nfa_listid = 1;
7188 rex.nfa_alt_listid = 2;
7189#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007190 nfa_regengine.expr = prog->pattern;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007191#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007192
Bram Moolenaard89616e2013-06-06 18:46:06 +02007193 if (prog->reganch && col > 0)
7194 return 0L;
7195
Bram Moolenaar0270f382018-07-17 05:43:58 +02007196 rex.need_clear_subexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007197#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007198 // Clear the external match subpointers if necessary.
Bram Moolenaar473de612013-06-08 18:19:48 +02007199 if (prog->reghasz == REX_SET)
7200 {
Bram Moolenaar0270f382018-07-17 05:43:58 +02007201 rex.nfa_has_zsubexpr = TRUE;
7202 rex.need_clear_zsubexpr = TRUE;
Bram Moolenaar473de612013-06-08 18:19:48 +02007203 }
7204 else
Bram Moolenaar0270f382018-07-17 05:43:58 +02007205 {
7206 rex.nfa_has_zsubexpr = FALSE;
7207 rex.need_clear_zsubexpr = FALSE;
7208 }
Bram Moolenaar473de612013-06-08 18:19:48 +02007209#endif
7210
Bram Moolenaard89616e2013-06-06 18:46:06 +02007211 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007212 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007213 // Skip ahead until a character we know the match must start with.
7214 // When there is none there is no match.
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007215 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007216 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007217
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007218 // If match_text is set it contains the full text that must match.
7219 // Nothing else to try. Doesn't handle combining chars well.
Bram Moolenaara12a1612019-01-24 16:39:02 +01007220 if (prog->match_text != NULL && !rex.reg_icombine)
Bram Moolenaar473de612013-06-08 18:19:48 +02007221 return find_match_text(col, prog->regstart, prog->match_text);
7222 }
7223
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007224 // If the start column is past the maximum column: no need to try.
Bram Moolenaar6100d022016-10-02 16:51:57 +02007225 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007226 goto theend;
7227
Bram Moolenaar0270f382018-07-17 05:43:58 +02007228 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when
7229 // it's accidentally used during execution.
7230 nstate = 0;
7231 for (i = 0; i < prog->nstate; ++i)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007232 {
7233 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007234 prog->state[i].lastlist[0] = 0;
7235 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007236 }
7237
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007238 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007239
Bram Moolenaar0270f382018-07-17 05:43:58 +02007240#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007241 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007242#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007244theend:
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007245 if (retval > 0)
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007246 {
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007247 // Make sure the end is never before the start. Can happen when \zs and
7248 // \ze are used.
7249 if (REG_MULTI)
7250 {
7251 lpos_T *start = &rex.reg_mmatch->startpos[0];
7252 lpos_T *end = &rex.reg_mmatch->endpos[0];
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007253
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007254 if (end->lnum < start->lnum
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007255 || (end->lnum == start->lnum && end->col < start->col))
Bram Moolenaara3d10a52020-12-21 18:24:00 +01007256 rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
7257 }
7258 else
7259 {
7260 if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
7261 rex.reg_match->endp[0] = rex.reg_match->startp[0];
7262 }
Bram Moolenaara7a691c2020-12-09 16:36:04 +01007263 }
7264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007265 return retval;
7266}
7267
7268/*
7269 * Compile a regular expression into internal code for the NFA matcher.
7270 * Returns the program in allocated space. Returns NULL for an error.
7271 */
7272 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007273nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007274{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007275 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007276 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007277 int *postfix;
7278
7279 if (expr == NULL)
7280 return NULL;
7281
Bram Moolenaar0270f382018-07-17 05:43:58 +02007282#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007283 nfa_regengine.expr = expr;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007284#endif
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007285 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007286
7287 init_class_tab();
7288
7289 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7290 return NULL;
7291
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007292 // Build postfix form of the regexp. Needed to build the NFA
7293 // (and count its size).
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007294 postfix = re2post();
7295 if (postfix == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007296 goto fail; // Cascaded (syntax?) error
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007297
7298 /*
7299 * In order to build the NFA, we parse the input regexp twice:
7300 * 1. first pass to count size (so we can allocate space)
7301 * 2. second to emit code
7302 */
7303#ifdef ENABLE_LOG
7304 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007305 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007306
7307 if (f != NULL)
7308 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007309 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007310 fclose(f);
7311 }
7312 }
7313#endif
7314
7315 /*
7316 * PASS 1
7317 * Count number of NFA states in "nstate". Do not build the NFA.
7318 */
7319 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007320
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007321 // allocate the regprog with space for the compiled regexp
Bram Moolenaar16619a22013-06-11 18:42:36 +02007322 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007323 prog = alloc(prog_size);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007324 if (prog == NULL)
7325 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007326 state_ptr = prog->state;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007327 prog->re_in_use = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007328
7329 /*
7330 * PASS 2
7331 * Build the NFA
7332 */
7333 prog->start = post2nfa(postfix, post_ptr, FALSE);
7334 if (prog->start == NULL)
7335 goto fail;
7336
7337 prog->regflags = regflags;
7338 prog->engine = &nfa_regengine;
7339 prog->nstate = nstate;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007340 prog->has_zend = rex.nfa_has_zend;
7341 prog->has_backref = rex.nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007342 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007343
Bram Moolenaara2947e22013-06-11 22:44:09 +02007344 nfa_postprocess(prog);
7345
Bram Moolenaard89616e2013-06-06 18:46:06 +02007346 prog->reganch = nfa_get_reganch(prog->start, 0);
7347 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007348 prog->match_text = nfa_get_match_text(prog->start);
7349
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007350#ifdef ENABLE_LOG
7351 nfa_postfix_dump(expr, OK);
7352 nfa_dump(prog);
7353#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007354#ifdef FEAT_SYN_HL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007355 // Remember whether this pattern has any \z specials in it.
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007356 prog->reghasz = re_has_z;
7357#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007358 prog->pattern = vim_strsave(expr);
Bram Moolenaar0270f382018-07-17 05:43:58 +02007359#ifdef DEBUG
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007360 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007361#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007362
7363out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007364 VIM_CLEAR(post_start);
7365 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007366 state_ptr = NULL;
7367 return (regprog_T *)prog;
7368
7369fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007370 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007371#ifdef ENABLE_LOG
7372 nfa_postfix_dump(expr, FAIL);
7373#endif
Bram Moolenaar0270f382018-07-17 05:43:58 +02007374#ifdef DEBUG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007375 nfa_regengine.expr = NULL;
Bram Moolenaar0270f382018-07-17 05:43:58 +02007376#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007377 goto out;
7378}
7379
Bram Moolenaar473de612013-06-08 18:19:48 +02007380/*
7381 * Free a compiled regexp program, returned by nfa_regcomp().
7382 */
7383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007384nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007385{
7386 if (prog != NULL)
7387 {
7388 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007389 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007390 vim_free(prog);
7391 }
7392}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007393
7394/*
7395 * Match a regexp against a string.
7396 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7397 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007398 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007399 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007400 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007401 */
7402 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007403nfa_regexec_nl(
7404 regmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007405 char_u *line, // string to match against
7406 colnr_T col, // column to start looking for match
Bram Moolenaar05540972016-01-30 20:31:25 +01007407 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007408{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007409 rex.reg_match = rmp;
7410 rex.reg_mmatch = NULL;
7411 rex.reg_maxline = 0;
7412 rex.reg_line_lbr = line_lbr;
7413 rex.reg_buf = curbuf;
7414 rex.reg_win = NULL;
7415 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007416 rex.reg_icombine = FALSE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007417 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007418 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007419}
7420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007421
7422/*
7423 * Match a regexp against multiple lines.
7424 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7425 * Uses curbuf for line count and 'iskeyword'.
7426 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007427 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007428 * match otherwise.
7429 *
7430 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7431 *
7432 * ! Also NOTE : match may actually be in another line. e.g.:
7433 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7434 *
7435 * +-------------------------+
7436 * |a |
7437 * |b |
7438 * |c |
7439 * | |
7440 * +-------------------------+
7441 *
7442 * then nfa_regexec_multi() returns 3. while the original
7443 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7444 *
7445 * FIXME if this behavior is not compatible.
7446 */
7447 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007448nfa_regexec_multi(
7449 regmmatch_T *rmp,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01007450 win_T *win, // window in which to search or NULL
7451 buf_T *buf, // buffer in which to search
7452 linenr_T lnum, // nr of line to start looking for match
7453 colnr_T col, // column to start looking for match
7454 proftime_T *tm, // timeout limit or NULL
7455 int *timed_out) // flag set on timeout or NULL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007456{
Bram Moolenaarf4140482020-02-15 23:06:45 +01007457 init_regexec_multi(rmp, win, buf, lnum);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007458 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007459}
7460
7461#ifdef DEBUG
7462# undef ENABLE_LOG
7463#endif