blob: e6296993d4315da6fa45b98b84c7ef21696edcdc [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static int nfa_regcomp_start(char_u *expr, int re_flags);
286static int nfa_get_reganch(nfa_state_T *start, int depth);
287static int nfa_get_regstart(nfa_state_T *start, int depth);
288static char_u *nfa_get_match_text(nfa_state_T *start);
289static int realloc_post_list(void);
290static int nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl);
291static int nfa_emit_equi_class(int c);
292static int nfa_regatom(void);
293static int nfa_regpiece(void);
294static int nfa_regconcat(void);
295static int nfa_regbranch(void);
296static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100298static void nfa_set_code(int c);
299static void nfa_postfix_dump(char_u *expr, int retval);
300static void nfa_print_state(FILE *debugf, nfa_state_T *state);
301static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
302static void nfa_dump(nfa_regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100304static int *re2post(void);
305static nfa_state_T *alloc_state(int c, nfa_state_T *out, nfa_state_T *out1);
306static void st_error(int *postfix, int *end, int *p);
307static int nfa_max_width(nfa_state_T *startstate, int depth);
308static nfa_state_T *post2nfa(int *postfix, int *end, int nfa_calc_size);
309static void nfa_postprocess(nfa_regprog_T *prog);
310static int check_char_class(int class, int c);
311static void nfa_save_listids(nfa_regprog_T *prog, int *list);
312static void nfa_restore_listids(nfa_regprog_T *prog, int *list);
313static int nfa_re_num_cmp(long_u val, int op, long_u pos);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200314static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm, int *timed_out);
315static long nfa_regexec_both(char_u *line, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100316static regprog_T *nfa_regcomp(char_u *expr, int re_flags);
317static void nfa_regfree(regprog_T *prog);
318static int nfa_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200319static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100320static int match_follows(nfa_state_T *startstate, int depth);
321static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
323/* helper functions used when doing re2post() ... regatom() parsing */
324#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200325 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326 return FAIL; \
327 *post_ptr++ = c; \
328 } while (0)
329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330/*
331 * Initialize internal variables before NFA compilation.
332 * Return OK on success, FAIL otherwise.
333 */
334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335nfa_regcomp_start(
336 char_u *expr,
337 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200339 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200340 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
342 nstate = 0;
343 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200344 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200345 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200348 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200349 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200350
351 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354 post_start = (int *)lalloc(postfix_size, TRUE);
355 if (post_start == NULL)
356 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200358 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200360 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200362 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200363 regcomp_start(expr, re_flags);
364
365 return OK;
366}
367
368/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200369 * Figure out if the NFA state list starts with an anchor, must match at start
370 * of the line.
371 */
372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100373nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200374{
375 nfa_state_T *p = start;
376
377 if (depth > 4)
378 return 0;
379
380 while (p != NULL)
381 {
382 switch (p->c)
383 {
384 case NFA_BOL:
385 case NFA_BOF:
386 return 1; /* yes! */
387
388 case NFA_ZSTART:
389 case NFA_ZEND:
390 case NFA_CURSOR:
391 case NFA_VISUAL:
392
393 case NFA_MOPEN:
394 case NFA_MOPEN1:
395 case NFA_MOPEN2:
396 case NFA_MOPEN3:
397 case NFA_MOPEN4:
398 case NFA_MOPEN5:
399 case NFA_MOPEN6:
400 case NFA_MOPEN7:
401 case NFA_MOPEN8:
402 case NFA_MOPEN9:
403 case NFA_NOPEN:
404#ifdef FEAT_SYN_HL
405 case NFA_ZOPEN:
406 case NFA_ZOPEN1:
407 case NFA_ZOPEN2:
408 case NFA_ZOPEN3:
409 case NFA_ZOPEN4:
410 case NFA_ZOPEN5:
411 case NFA_ZOPEN6:
412 case NFA_ZOPEN7:
413 case NFA_ZOPEN8:
414 case NFA_ZOPEN9:
415#endif
416 p = p->out;
417 break;
418
419 case NFA_SPLIT:
420 return nfa_get_reganch(p->out, depth + 1)
421 && nfa_get_reganch(p->out1, depth + 1);
422
423 default:
424 return 0; /* noooo */
425 }
426 }
427 return 0;
428}
429
430/*
431 * Figure out if the NFA state list starts with a character which must match
432 * at start of the match.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200436{
437 nfa_state_T *p = start;
438
439 if (depth > 4)
440 return 0;
441
442 while (p != NULL)
443 {
444 switch (p->c)
445 {
446 /* all kinds of zero-width matches */
447 case NFA_BOL:
448 case NFA_BOF:
449 case NFA_BOW:
450 case NFA_EOW:
451 case NFA_ZSTART:
452 case NFA_ZEND:
453 case NFA_CURSOR:
454 case NFA_VISUAL:
455 case NFA_LNUM:
456 case NFA_LNUM_GT:
457 case NFA_LNUM_LT:
458 case NFA_COL:
459 case NFA_COL_GT:
460 case NFA_COL_LT:
461 case NFA_VCOL:
462 case NFA_VCOL_GT:
463 case NFA_VCOL_LT:
464 case NFA_MARK:
465 case NFA_MARK_GT:
466 case NFA_MARK_LT:
467
468 case NFA_MOPEN:
469 case NFA_MOPEN1:
470 case NFA_MOPEN2:
471 case NFA_MOPEN3:
472 case NFA_MOPEN4:
473 case NFA_MOPEN5:
474 case NFA_MOPEN6:
475 case NFA_MOPEN7:
476 case NFA_MOPEN8:
477 case NFA_MOPEN9:
478 case NFA_NOPEN:
479#ifdef FEAT_SYN_HL
480 case NFA_ZOPEN:
481 case NFA_ZOPEN1:
482 case NFA_ZOPEN2:
483 case NFA_ZOPEN3:
484 case NFA_ZOPEN4:
485 case NFA_ZOPEN5:
486 case NFA_ZOPEN6:
487 case NFA_ZOPEN7:
488 case NFA_ZOPEN8:
489 case NFA_ZOPEN9:
490#endif
491 p = p->out;
492 break;
493
494 case NFA_SPLIT:
495 {
496 int c1 = nfa_get_regstart(p->out, depth + 1);
497 int c2 = nfa_get_regstart(p->out1, depth + 1);
498
499 if (c1 == c2)
500 return c1; /* yes! */
501 return 0;
502 }
503
504 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200505 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200506 return p->c; /* yes! */
507 return 0;
508 }
509 }
510 return 0;
511}
512
513/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200514 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200515 * else. If so return a string in allocated memory with what must match after
516 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200517 */
518 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100519nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200520{
521 nfa_state_T *p = start;
522 int len = 0;
523 char_u *ret;
524 char_u *s;
525
526 if (p->c != NFA_MOPEN)
527 return NULL; /* just in case */
528 p = p->out;
529 while (p->c > 0)
530 {
531 len += MB_CHAR2LEN(p->c);
532 p = p->out;
533 }
534 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
535 return NULL;
536
537 ret = alloc(len);
538 if (ret != NULL)
539 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200540 p = start->out->out; /* skip first char, it goes into regstart */
541 s = ret;
542 while (p->c > 0)
543 {
544#ifdef FEAT_MBYTE
545 if (has_mbyte)
546 s += (*mb_char2bytes)(p->c, s);
547 else
548#endif
549 *s++ = p->c;
550 p = p->out;
551 }
552 *s = NUL;
553 }
554 return ret;
555}
556
557/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 * Allocate more space for post_start. Called when
559 * running above the estimated number of states.
560 */
561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100562realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200563{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200564 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 int new_max = nstate_max + 1000;
566 int *new_start;
567 int *old_start;
568
569 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
570 if (new_start == NULL)
571 return FAIL;
572 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200573 old_start = post_start;
574 post_start = new_start;
575 post_ptr = new_start + (post_ptr - old_start);
576 post_end = post_start + new_max;
577 vim_free(old_start);
578 return OK;
579}
580
581/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 * Search between "start" and "end" and try to recognize a
583 * character class in expanded form. For example [0-9].
584 * On success, return the id the character class to be emitted.
585 * On failure, return 0 (=FAIL)
586 * Start points to the first char of the range, while end should point
587 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200588 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
589 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 */
591 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100592nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200594# define CLASS_not 0x80
595# define CLASS_af 0x40
596# define CLASS_AF 0x20
597# define CLASS_az 0x10
598# define CLASS_AZ 0x08
599# define CLASS_o7 0x04
600# define CLASS_o9 0x02
601# define CLASS_underscore 0x01
602
603 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200605 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200606
607 if (extra_newl == TRUE)
608 newl = TRUE;
609
610 if (*end != ']')
611 return FAIL;
612 p = start;
613 if (*p == '^')
614 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200615 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200616 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 }
618
619 while (p < end)
620 {
621 if (p + 2 < end && *(p + 1) == '-')
622 {
623 switch (*p)
624 {
625 case '0':
626 if (*(p + 2) == '9')
627 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200628 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 break;
630 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200631 if (*(p + 2) == '7')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200636 return FAIL;
637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 case 'a':
639 if (*(p + 2) == 'z')
640 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200641 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642 break;
643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 if (*(p + 2) == 'f')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200649 return FAIL;
650
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 case 'A':
652 if (*(p + 2) == 'Z')
653 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200654 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655 break;
656 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200657 if (*(p + 2) == 'F')
658 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200659 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 break;
661 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200662 return FAIL;
663
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200664 default:
665 return FAIL;
666 }
667 p += 3;
668 }
669 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
670 {
671 newl = TRUE;
672 p += 2;
673 }
674 else if (*p == '_')
675 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677 p ++;
678 }
679 else if (*p == '\n')
680 {
681 newl = TRUE;
682 p ++;
683 }
684 else
685 return FAIL;
686 } /* while (p < end) */
687
688 if (p != end)
689 return FAIL;
690
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200692 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200693
694 switch (config)
695 {
696 case CLASS_o9:
697 return extra_newl + NFA_DIGIT;
698 case CLASS_not | CLASS_o9:
699 return extra_newl + NFA_NDIGIT;
700 case CLASS_af | CLASS_AF | CLASS_o9:
701 return extra_newl + NFA_HEX;
702 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
703 return extra_newl + NFA_NHEX;
704 case CLASS_o7:
705 return extra_newl + NFA_OCTAL;
706 case CLASS_not | CLASS_o7:
707 return extra_newl + NFA_NOCTAL;
708 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
709 return extra_newl + NFA_WORD;
710 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
711 return extra_newl + NFA_NWORD;
712 case CLASS_az | CLASS_AZ | CLASS_underscore:
713 return extra_newl + NFA_HEAD;
714 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
715 return extra_newl + NFA_NHEAD;
716 case CLASS_az | CLASS_AZ:
717 return extra_newl + NFA_ALPHA;
718 case CLASS_not | CLASS_az | CLASS_AZ:
719 return extra_newl + NFA_NALPHA;
720 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200721 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200722 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200723 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200724 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200726 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200727 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200729 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730}
731
732/*
733 * Produce the bytes for equivalence class "c".
734 * Currently only handles latin1, latin9 and utf-8.
735 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
736 * equivalent to 'a OR b OR c'
737 *
738 * NOTE! When changing this function, also update reg_equi_class()
739 */
740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100741nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200742{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200743#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
744#ifdef FEAT_MBYTE
745# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
746#else
747# define EMITMBC(c)
748#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749
750#ifdef FEAT_MBYTE
751 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
752 || STRCMP(p_enc, "iso-8859-15") == 0)
753#endif
754 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200755#ifdef EBCDIC
756# define A_circumflex 0x62
757# define A_diaeresis 0x63
758# define A_grave 0x64
759# define A_acute 0x65
760# define A_virguilla 0x66
761# define A_ring 0x67
762# define C_cedilla 0x68
763# define E_acute 0x71
764# define E_circumflex 0x72
765# define E_diaeresis 0x73
766# define E_grave 0x74
767# define I_acute 0x75
768# define I_circumflex 0x76
769# define I_diaeresis 0x77
770# define I_grave 0x78
771# define N_virguilla 0x69
772# define O_circumflex 0xeb
773# define O_diaeresis 0xec
774# define O_grave 0xed
775# define O_acute 0xee
776# define O_virguilla 0xef
777# define O_slash 0x80
778# define U_circumflex 0xfb
779# define U_diaeresis 0xfc
780# define U_grave 0xfd
781# define U_acute 0xfe
782# define Y_acute 0xba
783# define a_grave 0x42
784# define a_acute 0x43
785# define a_circumflex 0x44
786# define a_virguilla 0x45
787# define a_diaeresis 0x46
788# define a_ring 0x47
789# define c_cedilla 0x48
790# define e_grave 0x51
791# define e_acute 0x52
792# define e_circumflex 0x53
793# define e_diaeresis 0x54
794# define i_grave 0x55
795# define i_acute 0x56
796# define i_circumflex 0x57
797# define i_diaeresis 0x58
798# define n_virguilla 0x49
799# define o_grave 0xcb
800# define o_acute 0xcc
801# define o_circumflex 0xcd
802# define o_virguilla 0xce
803# define o_diaeresis 0xcf
804# define o_slash 0x70
805# define u_grave 0xdb
806# define u_acute 0xdc
807# define u_circumflex 0xdd
808# define u_diaeresis 0xde
809# define y_acute 0x8d
810# define y_diaeresis 0xdf
811#else
812# define A_grave 0xc0
813# define A_acute 0xc1
814# define A_circumflex 0xc2
815# define A_virguilla 0xc3
816# define A_diaeresis 0xc4
817# define A_ring 0xc5
818# define C_cedilla 0xc7
819# define E_grave 0xc8
820# define E_acute 0xc9
821# define E_circumflex 0xca
822# define E_diaeresis 0xcb
823# define I_grave 0xcc
824# define I_acute 0xcd
825# define I_circumflex 0xce
826# define I_diaeresis 0xcf
827# define N_virguilla 0xd1
828# define O_grave 0xd2
829# define O_acute 0xd3
830# define O_circumflex 0xd4
831# define O_virguilla 0xd5
832# define O_diaeresis 0xd6
833# define O_slash 0xd8
834# define U_grave 0xd9
835# define U_acute 0xda
836# define U_circumflex 0xdb
837# define U_diaeresis 0xdc
838# define Y_acute 0xdd
839# define a_grave 0xe0
840# define a_acute 0xe1
841# define a_circumflex 0xe2
842# define a_virguilla 0xe3
843# define a_diaeresis 0xe4
844# define a_ring 0xe5
845# define c_cedilla 0xe7
846# define e_grave 0xe8
847# define e_acute 0xe9
848# define e_circumflex 0xea
849# define e_diaeresis 0xeb
850# define i_grave 0xec
851# define i_acute 0xed
852# define i_circumflex 0xee
853# define i_diaeresis 0xef
854# define n_virguilla 0xf1
855# define o_grave 0xf2
856# define o_acute 0xf3
857# define o_circumflex 0xf4
858# define o_virguilla 0xf5
859# define o_diaeresis 0xf6
860# define o_slash 0xf8
861# define u_grave 0xf9
862# define u_acute 0xfa
863# define u_circumflex 0xfb
864# define u_diaeresis 0xfc
865# define y_acute 0xfd
866# define y_diaeresis 0xff
867#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 switch (c)
869 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'A': case A_grave: case A_acute: case A_circumflex:
871 case A_virguilla: case A_diaeresis: case A_ring:
872 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
873 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
874 CASEMBC(0x1ea2)
875 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
876 EMIT2(A_circumflex); EMIT2(A_virguilla);
877 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200878 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
879 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
880 EMITMBC(0x1ea2)
881 return OK;
882
883 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
884 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 return OK;
886
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200887 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
888 CASEMBC(0x10a) CASEMBC(0x10c)
889 EMIT2('C'); EMIT2(C_cedilla);
890 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200891 EMITMBC(0x10a) EMITMBC(0x10c)
892 return OK;
893
894 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
897 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200898 return OK;
899
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200900 case 'E': case E_grave: case E_acute: case E_circumflex:
901 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
902 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
903 CASEMBC(0x1eba) CASEMBC(0x1ebc)
904 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
905 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200906 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
907 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
908 EMITMBC(0x1ebc)
909 return OK;
910
911 case 'F': CASEMBC(0x1e1e)
912 EMIT2('F'); EMITMBC(0x1e1e)
913 return OK;
914
915 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200916 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
917 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200918 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
919 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
920 EMITMBC(0x1f4) EMITMBC(0x1e20)
921 return OK;
922
923 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200924 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200925 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
926 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927 return OK;
928
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 case 'I': case I_grave: case I_acute: case I_circumflex:
930 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
931 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
932 CASEMBC(0x1cf) CASEMBC(0x1ec8)
933 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
934 EMIT2(I_circumflex); EMIT2(I_diaeresis);
935 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
937 EMITMBC(0x1cf) EMITMBC(0x1ec8)
938 return OK;
939
940 case 'J': CASEMBC(0x134)
941 EMIT2('J'); EMITMBC(0x134)
942 return OK;
943
944 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200945 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200946 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
947 EMITMBC(0x1e34)
948 return OK;
949
950 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200951 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200952 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
953 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
954 return OK;
955
956 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
957 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 return OK;
959
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200960 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
961 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
962 EMIT2('N'); EMIT2(N_virguilla);
963 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200964 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 return OK;
966
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200967 case 'O': case O_grave: case O_acute: case O_circumflex:
968 case O_virguilla: case O_diaeresis: case O_slash:
969 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
970 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
971 CASEMBC(0x1ec) CASEMBC(0x1ece)
972 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
973 EMIT2(O_circumflex); EMIT2(O_virguilla);
974 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200975 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
976 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
977 EMITMBC(0x1ec) EMITMBC(0x1ece)
978 return OK;
979
980 case 'P': case 0x1e54: case 0x1e56:
981 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
982 return OK;
983
984 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200986 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
987 EMITMBC(0x1e58) EMITMBC(0x1e5e)
988 return OK;
989
990 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200991 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200992 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
993 EMITMBC(0x160) EMITMBC(0x1e60)
994 return OK;
995
996 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200997 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200998 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
999 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return OK;
1001
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001002 case 'U': case U_grave: case U_acute: case U_diaeresis:
1003 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
1004 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
1005 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
1006 CASEMBC(0x1ee6)
1007 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
1008 EMIT2(U_diaeresis); EMIT2(U_circumflex);
1009 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
1011 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
1012 EMITMBC(0x1ee6)
1013 return OK;
1014
1015 case 'V': CASEMBC(0x1e7c)
1016 EMIT2('V'); EMITMBC(0x1e7c)
1017 return OK;
1018
1019 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001020 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001021 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
1022 EMITMBC(0x1e84) EMITMBC(0x1e86)
1023 return OK;
1024
1025 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
1026 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001027 return OK;
1028
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001029 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1030 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1031 CASEMBC(0x1ef8)
1032 EMIT2('Y'); EMIT2(Y_acute);
1033 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001034 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1035 EMITMBC(0x1ef8)
1036 return OK;
1037
1038 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001040 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1041 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'a': case a_grave: case a_acute: case a_circumflex:
1045 case a_virguilla: case a_diaeresis: case a_ring:
1046 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1047 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1048 CASEMBC(0x1ea3)
1049 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1050 EMIT2(a_circumflex); EMIT2(a_virguilla);
1051 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001052 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1053 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1054 EMITMBC(0x1ea3)
1055 return OK;
1056
1057 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1058 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001059 return OK;
1060
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001061 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1062 CASEMBC(0x10b) CASEMBC(0x10d)
1063 EMIT2('c'); EMIT2(c_cedilla);
1064 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001065 EMITMBC(0x10b) EMITMBC(0x10d)
1066 return OK;
1067
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001068 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001070 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1071 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001072 return OK;
1073
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001074 case 'e': case e_grave: case e_acute: case e_circumflex:
1075 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1076 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1077 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1078 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1079 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1080 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001081 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1082 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1083 return OK;
1084
1085 case 'f': CASEMBC(0x1e1f)
1086 EMIT2('f'); EMITMBC(0x1e1f)
1087 return OK;
1088
1089 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001090 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1091 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1093 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1094 EMITMBC(0x1f5) EMITMBC(0x1e21)
1095 return OK;
1096
1097 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001098 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001099 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1100 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001101 return OK;
1102
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001103 case 'i': case i_grave: case i_acute: case i_circumflex:
1104 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1105 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1106 CASEMBC(0x1ec9)
1107 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1108 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1109 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001110 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1111 EMITMBC(0x1ec9)
1112 return OK;
1113
1114 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1115 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1116 return OK;
1117
1118 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1121 EMITMBC(0x1e35)
1122 return OK;
1123
1124 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001125 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001126 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1127 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1128 return OK;
1129
1130 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1131 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 return OK;
1133
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001134 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1135 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1136 CASEMBC(0x1e49)
1137 EMIT2('n'); EMIT2(n_virguilla);
1138 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001139 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1140 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 return OK;
1142
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001143 case 'o': case o_grave: case o_acute: case o_circumflex:
1144 case o_virguilla: case o_diaeresis: case o_slash:
1145 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1146 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1147 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1148 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1149 EMIT2(o_circumflex); EMIT2(o_virguilla);
1150 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001151 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1152 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1153 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1154 return OK;
1155
1156 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1157 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1158 return OK;
1159
1160 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001161 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1163 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1164 return OK;
1165
1166 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001167 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1169 EMITMBC(0x161) EMITMBC(0x1e61)
1170 return OK;
1171
1172 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001173 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001174 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1175 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001176 return OK;
1177
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001178 case 'u': case u_grave: case u_acute: case u_circumflex:
1179 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1180 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1181 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1182 CASEMBC(0x1ee7)
1183 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1184 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1185 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001186 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1187 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1188 EMITMBC(0x1ee7)
1189 return OK;
1190
1191 case 'v': CASEMBC(0x1e7d)
1192 EMIT2('v'); EMITMBC(0x1e7d)
1193 return OK;
1194
1195 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001196 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001197 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1198 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1199 return OK;
1200
1201 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1202 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 return OK;
1204
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001205 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1206 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1207 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1208 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1209 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001210 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1211 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 return OK;
1213
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001214 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001215 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001216 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1217 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1218 return OK;
1219
1220 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 }
1222 }
1223
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001224 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 return OK;
1226#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001227#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228}
1229
1230/*
1231 * Code to parse regular expression.
1232 *
1233 * We try to reuse parsing functions in regexp.c to
1234 * minimize surprise and keep the syntax consistent.
1235 */
1236
1237/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 * Parse the lowest level.
1239 *
1240 * An atom can be one of a long list of items. Many atoms match one character
1241 * in the text. It is often an ordinary character or a character class.
1242 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1243 * is only for syntax highlighting.
1244 *
1245 * atom ::= ordinary-atom
1246 * or \( pattern \)
1247 * or \%( pattern \)
1248 * or \z( pattern \)
1249 */
1250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001251nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252{
1253 int c;
1254 int charclass;
1255 int equiclass;
1256 int collclass;
1257 int got_coll_char;
1258 char_u *p;
1259 char_u *endp;
1260#ifdef FEAT_MBYTE
1261 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262#endif
1263 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 int emit_range;
1265 int negated;
1266 int result;
1267 int startc = -1;
1268 int endc = -1;
1269 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001270 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 switch (c)
1274 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001275 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001276 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 case Magic('^'):
1279 EMIT(NFA_BOL);
1280 break;
1281
1282 case Magic('$'):
1283 EMIT(NFA_EOL);
1284#if defined(FEAT_SYN_HL) || defined(PROTO)
1285 had_eol = TRUE;
1286#endif
1287 break;
1288
1289 case Magic('<'):
1290 EMIT(NFA_BOW);
1291 break;
1292
1293 case Magic('>'):
1294 EMIT(NFA_EOW);
1295 break;
1296
1297 case Magic('_'):
1298 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001299 if (c == NUL)
1300 EMSG_RET_FAIL(_(e_nul_found));
1301
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001302 if (c == '^') /* "\_^" is start-of-line */
1303 {
1304 EMIT(NFA_BOL);
1305 break;
1306 }
1307 if (c == '$') /* "\_$" is end-of-line */
1308 {
1309 EMIT(NFA_EOL);
1310#if defined(FEAT_SYN_HL) || defined(PROTO)
1311 had_eol = TRUE;
1312#endif
1313 break;
1314 }
1315
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001316 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317
1318 /* "\_[" is collection plus newline */
1319 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001320 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321
1322 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001323 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001324
1325 /*
1326 * Character classes.
1327 */
1328 case Magic('.'):
1329 case Magic('i'):
1330 case Magic('I'):
1331 case Magic('k'):
1332 case Magic('K'):
1333 case Magic('f'):
1334 case Magic('F'):
1335 case Magic('p'):
1336 case Magic('P'):
1337 case Magic('s'):
1338 case Magic('S'):
1339 case Magic('d'):
1340 case Magic('D'):
1341 case Magic('x'):
1342 case Magic('X'):
1343 case Magic('o'):
1344 case Magic('O'):
1345 case Magic('w'):
1346 case Magic('W'):
1347 case Magic('h'):
1348 case Magic('H'):
1349 case Magic('a'):
1350 case Magic('A'):
1351 case Magic('l'):
1352 case Magic('L'):
1353 case Magic('u'):
1354 case Magic('U'):
1355 p = vim_strchr(classchars, no_Magic(c));
1356 if (p == NULL)
1357 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001358 if (extra == NFA_ADD_NL)
1359 {
1360 EMSGN(_(e_ill_char_class), c);
1361 rc_did_emsg = TRUE;
1362 return FAIL;
1363 }
Bram Moolenaarde330112017-01-08 20:50:52 +01001364 IEMSGN("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001365 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 }
1367#ifdef FEAT_MBYTE
1368 /* When '.' is followed by a composing char ignore the dot, so that
1369 * the composing char is matched here. */
1370 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1371 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001372 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001373 c = getchr();
1374 goto nfa_do_multibyte;
1375 }
1376#endif
1377 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001378 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001379 {
1380 EMIT(NFA_NEWL);
1381 EMIT(NFA_OR);
1382 regflags |= RF_HASNL;
1383 }
1384 break;
1385
1386 case Magic('n'):
1387 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001388 /* In a string "\n" matches a newline character. */
1389 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 else
1391 {
1392 /* In buffer text "\n" matches the end of a line. */
1393 EMIT(NFA_NEWL);
1394 regflags |= RF_HASNL;
1395 }
1396 break;
1397
1398 case Magic('('):
1399 if (nfa_reg(REG_PAREN) == FAIL)
1400 return FAIL; /* cascaded error */
1401 break;
1402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 case Magic('|'):
1404 case Magic('&'):
1405 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001406 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 return FAIL;
1408
1409 case Magic('='):
1410 case Magic('?'):
1411 case Magic('+'):
1412 case Magic('@'):
1413 case Magic('*'):
1414 case Magic('{'):
1415 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001416 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 return FAIL;
1418
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001419 case Magic('~'):
1420 {
1421 char_u *lp;
1422
1423 /* Previous substitute pattern.
1424 * Generated as "\%(pattern\)". */
1425 if (reg_prev_sub == NULL)
1426 {
1427 EMSG(_(e_nopresub));
1428 return FAIL;
1429 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001430 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001431 {
1432 EMIT(PTR2CHAR(lp));
1433 if (lp != reg_prev_sub)
1434 EMIT(NFA_CONCAT);
1435 }
1436 EMIT(NFA_NOPEN);
1437 break;
1438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439
Bram Moolenaar428e9872013-05-30 17:05:39 +02001440 case Magic('1'):
1441 case Magic('2'):
1442 case Magic('3'):
1443 case Magic('4'):
1444 case Magic('5'):
1445 case Magic('6'):
1446 case Magic('7'):
1447 case Magic('8'):
1448 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001449 {
1450 int refnum = no_Magic(c) - '1';
1451
1452 if (!seen_endbrace(refnum + 1))
1453 return FAIL;
1454 EMIT(NFA_BACKREF1 + refnum);
1455 nfa_has_backref = TRUE;
1456 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001457 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001458
1459 case Magic('z'):
1460 c = no_Magic(getchr());
1461 switch (c)
1462 {
1463 case 's':
1464 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001465 if (re_mult_next("\\zs") == FAIL)
1466 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 break;
1468 case 'e':
1469 EMIT(NFA_ZEND);
1470 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001471 if (re_mult_next("\\ze") == FAIL)
1472 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001473 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001474#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 case '1':
1476 case '2':
1477 case '3':
1478 case '4':
1479 case '5':
1480 case '6':
1481 case '7':
1482 case '8':
1483 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001484 /* \z1...\z9 */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001485 if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001486 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001487 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1488 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001489 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001490 re_has_z = REX_USE;
1491 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001493 /* \z( */
Bram Moolenaarbcf94422018-06-23 14:21:42 +02001494 if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001495 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001496 if (nfa_reg(REG_ZPAREN) == FAIL)
1497 return FAIL; /* cascaded error */
1498 re_has_z = REX_SET;
1499 break;
1500#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001502 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001503 no_Magic(c));
1504 return FAIL;
1505 }
1506 break;
1507
1508 case Magic('%'):
1509 c = no_Magic(getchr());
1510 switch (c)
1511 {
1512 /* () without a back reference */
1513 case '(':
1514 if (nfa_reg(REG_NPAREN) == FAIL)
1515 return FAIL;
1516 EMIT(NFA_NOPEN);
1517 break;
1518
1519 case 'd': /* %d123 decimal */
1520 case 'o': /* %o123 octal */
1521 case 'x': /* %xab hex 2 */
1522 case 'u': /* %uabcd hex 4 */
1523 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001524 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001525 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526
Bram Moolenaar47196582013-05-25 22:04:23 +02001527 switch (c)
1528 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001529 case 'd': nr = getdecchrs(); break;
1530 case 'o': nr = getoctchrs(); break;
1531 case 'x': nr = gethexchrs(2); break;
1532 case 'u': nr = gethexchrs(4); break;
1533 case 'U': nr = gethexchrs(8); break;
1534 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001535 }
1536
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001537 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001538 EMSG2_RET_FAIL(
1539 _("E678: Invalid character after %s%%[dxouU]"),
1540 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001541 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001542 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001543 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001544 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 break;
1546
1547 /* Catch \%^ and \%$ regardless of where they appear in the
1548 * pattern -- regardless of whether or not it makes sense. */
1549 case '^':
1550 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 break;
1552
1553 case '$':
1554 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 break;
1556
1557 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 break;
1560
1561 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001562 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 break;
1564
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001565 case 'C':
1566 EMIT(NFA_ANY_COMPOSING);
1567 break;
1568
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001569 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001570 {
1571 int n;
1572
1573 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001574 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001575 {
1576 if (c == NUL)
1577 EMSG2_RET_FAIL(_(e_missing_sb),
1578 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001579 /* recursive call! */
1580 if (nfa_regatom() == FAIL)
1581 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001582 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001583 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001584 if (n == 0)
1585 EMSG2_RET_FAIL(_(e_empty_sb),
1586 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001587 EMIT(NFA_OPT_CHARS);
1588 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001589
1590 /* Emit as "\%(\%[abc]\)" to be able to handle
1591 * "\%[abc]*" which would cause the empty string to be
1592 * matched an unlimited number of times. NFA_NOPEN is
1593 * added only once at a position, while NFA_SPLIT is
1594 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001595 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001596 * a lot. */
1597 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001598 break;
1599 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001601 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001602 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001603 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001604 int cmp = c;
1605
1606 if (c == '<' || c == '>')
1607 c = getchr();
1608 while (VIM_ISDIGIT(c))
1609 {
1610 n = n * 10 + (c - '0');
1611 c = getchr();
1612 }
1613 if (c == 'l' || c == 'c' || c == 'v')
1614 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001615 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001616 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001617 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001618 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001619 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001620 if (save_prev_at_start)
1621 at_start = TRUE;
1622 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001623 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001624 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001625 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001626 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001627 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001628 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001629 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001630 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001631#if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
1632 if (n > INT_MAX)
1633 {
1634 EMSG(_("E951: \\% value too large"));
1635 return FAIL;
1636 }
1637#endif
1638 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001639 break;
1640 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001641 else if (c == '\'' && n == 0)
1642 {
1643 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001644 EMIT(cmp == '<' ? NFA_MARK_LT :
1645 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001646 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001647 break;
1648 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001649 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001650 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1651 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001652 return FAIL;
1653 }
1654 break;
1655
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001656 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001657collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001659 * [abc] uses NFA_START_COLL - NFA_END_COLL
1660 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1661 * Each character is produced as a regular state, using
1662 * NFA_CONCAT to bind them together.
1663 * Besides normal characters there can be:
1664 * - character classes NFA_CLASS_*
1665 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 */
1667
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 p = regparse;
1669 endp = skip_anyof(p);
1670 if (*endp == ']')
1671 {
1672 /*
1673 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001674 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001675 * and perform the necessary substitutions in the NFA.
1676 */
1677 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001678 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001679 if (result != FAIL)
1680 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001681 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001683 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 EMIT(NFA_NEWL);
1685 EMIT(NFA_OR);
1686 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001687 else
1688 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001690 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 return OK;
1692 }
1693 /*
1694 * Failed to recognize a character class. Use the simple
1695 * version that turns [abc] into 'a' OR 'b' OR 'c'
1696 */
1697 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001699 if (*regparse == '^') /* negated range */
1700 {
1701 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001702 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001703 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001704 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001705 else
1706 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707 if (*regparse == '-')
1708 {
1709 startc = '-';
1710 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001711 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001712 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 }
1714 /* Emit the OR branches for each character in the [] */
1715 emit_range = FALSE;
1716 while (regparse < endp)
1717 {
1718 oldstartc = startc;
1719 startc = -1;
1720 got_coll_char = FALSE;
1721 if (*regparse == '[')
1722 {
1723 /* Check for [: :], [= =], [. .] */
1724 equiclass = collclass = 0;
1725 charclass = get_char_class(&regparse);
1726 if (charclass == CLASS_NONE)
1727 {
1728 equiclass = get_equi_class(&regparse);
1729 if (equiclass == 0)
1730 collclass = get_coll_element(&regparse);
1731 }
1732
1733 /* Character class like [:alpha:] */
1734 if (charclass != CLASS_NONE)
1735 {
1736 switch (charclass)
1737 {
1738 case CLASS_ALNUM:
1739 EMIT(NFA_CLASS_ALNUM);
1740 break;
1741 case CLASS_ALPHA:
1742 EMIT(NFA_CLASS_ALPHA);
1743 break;
1744 case CLASS_BLANK:
1745 EMIT(NFA_CLASS_BLANK);
1746 break;
1747 case CLASS_CNTRL:
1748 EMIT(NFA_CLASS_CNTRL);
1749 break;
1750 case CLASS_DIGIT:
1751 EMIT(NFA_CLASS_DIGIT);
1752 break;
1753 case CLASS_GRAPH:
1754 EMIT(NFA_CLASS_GRAPH);
1755 break;
1756 case CLASS_LOWER:
1757 EMIT(NFA_CLASS_LOWER);
1758 break;
1759 case CLASS_PRINT:
1760 EMIT(NFA_CLASS_PRINT);
1761 break;
1762 case CLASS_PUNCT:
1763 EMIT(NFA_CLASS_PUNCT);
1764 break;
1765 case CLASS_SPACE:
1766 EMIT(NFA_CLASS_SPACE);
1767 break;
1768 case CLASS_UPPER:
1769 EMIT(NFA_CLASS_UPPER);
1770 break;
1771 case CLASS_XDIGIT:
1772 EMIT(NFA_CLASS_XDIGIT);
1773 break;
1774 case CLASS_TAB:
1775 EMIT(NFA_CLASS_TAB);
1776 break;
1777 case CLASS_RETURN:
1778 EMIT(NFA_CLASS_RETURN);
1779 break;
1780 case CLASS_BACKSPACE:
1781 EMIT(NFA_CLASS_BACKSPACE);
1782 break;
1783 case CLASS_ESCAPE:
1784 EMIT(NFA_CLASS_ESCAPE);
1785 break;
1786 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001787 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 continue;
1789 }
1790 /* Try equivalence class [=a=] and the like */
1791 if (equiclass != 0)
1792 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001793 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 if (result == FAIL)
1795 {
1796 /* should never happen */
1797 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1798 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 continue;
1800 }
1801 /* Try collating class like [. .] */
1802 if (collclass != 0)
1803 {
1804 startc = collclass; /* allow [.a.]-x as a range */
1805 /* Will emit the proper atom at the end of the
1806 * while loop. */
1807 }
1808 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001809 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1810 * start character. */
1811 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812 {
1813 emit_range = TRUE;
1814 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001815 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 continue; /* reading the end of the range */
1817 }
1818
1819 /* Now handle simple and escaped characters.
1820 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1821 * accepts "\t", "\e", etc., but only when the 'l' flag in
1822 * 'cpoptions' is not included.
1823 * Posix doesn't recognize backslash at all.
1824 */
1825 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001826 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 && regparse + 1 <= endp
1828 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001829 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830 && vim_strchr(REGEXP_ABBR, regparse[1])
1831 != NULL)
1832 )
1833 )
1834 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001835 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001837 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 startc = reg_string ? NL : NFA_NEWL;
1839 else
1840 if (*regparse == 'd'
1841 || *regparse == 'o'
1842 || *regparse == 'x'
1843 || *regparse == 'u'
1844 || *regparse == 'U'
1845 )
1846 {
1847 /* TODO(RE) This needs more testing */
1848 startc = coll_get_char();
1849 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001850 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 }
1852 else
1853 {
1854 /* \r,\t,\e,\b */
1855 startc = backslash_trans(*regparse);
1856 }
1857 }
1858
1859 /* Normal printable char */
1860 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001861 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862
1863 /* Previous char was '-', so this char is end of range. */
1864 if (emit_range)
1865 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001866 endc = startc;
1867 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001869 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001870
1871 if (endc > startc + 2)
1872 {
1873 /* Emit a range instead of the sequence of
1874 * individual characters. */
1875 if (startc == 0)
1876 /* \x00 is translated to \x0a, start at \x01. */
1877 EMIT(1);
1878 else
1879 --post_ptr; /* remove NFA_CONCAT */
1880 EMIT(endc);
1881 EMIT(NFA_RANGE);
1882 EMIT(NFA_CONCAT);
1883 }
1884 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001886 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887 || (*mb_char2len)(endc) > 1))
1888 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001889 /* Emit the characters in the range.
1890 * "startc" was already emitted, so skip it.
1891 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892 for (c = startc + 1; c <= endc; c++)
1893 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001894 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001895 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 }
1898 else
1899#endif
1900 {
1901#ifdef EBCDIC
1902 int alpha_only = FALSE;
1903
1904 /* for alphabetical range skip the gaps
1905 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1906 if (isalpha(startc) && isalpha(endc))
1907 alpha_only = TRUE;
1908#endif
1909 /* Emit the range. "startc" was already emitted, so
1910 * skip it. */
1911 for (c = startc + 1; c <= endc; c++)
1912#ifdef EBCDIC
1913 if (!alpha_only || isalpha(startc))
1914#endif
1915 {
1916 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001917 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001918 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001920 emit_range = FALSE;
1921 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001922 }
1923 else
1924 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001925 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 * Normally, simply emit startc. But if we get char
1928 * code=0 from a collating char, then replace it with
1929 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001931 * the backtracking engine. */
1932 if (startc == NFA_NEWL)
1933 {
1934 /* Line break can't be matched as part of the
1935 * collection, add an OR below. But not for negated
1936 * range. */
1937 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001938 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001939 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001941 {
1942 if (got_coll_char == TRUE && startc == 0)
1943 EMIT(0x0a);
1944 else
1945 EMIT(startc);
1946 EMIT(NFA_CONCAT);
1947 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 }
1949
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001950 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 } /* while (p < endp) */
1952
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001953 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954 if (*regparse == '-') /* if last, '-' is just a char */
1955 {
1956 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001957 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001959
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 /* skip the trailing ] */
1961 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001962 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001963
1964 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001965 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001966 EMIT(NFA_END_NEG_COLL);
1967 else
1968 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001969
1970 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001971 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001972 {
1973 EMIT(reg_string ? NL : NFA_NEWL);
1974 EMIT(NFA_OR);
1975 }
1976
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001977 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001978 } /* if exists closing ] */
1979
1980 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001982 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001984 default:
1985 {
1986#ifdef FEAT_MBYTE
1987 int plen;
1988
1989nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001990 /* plen is length of current char with composing chars */
1991 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001992 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001993 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001994 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001995 int i = 0;
1996
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001997 /* A base character plus composing characters, or just one
1998 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001999 * This requires creating a separate atom as if enclosing
2000 * the characters in (), where NFA_COMPOSING is the ( and
2001 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 * building the postfix form, not the NFA itself;
2003 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002004 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002005 for (;;)
2006 {
2007 EMIT(c);
2008 if (i > 0)
2009 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002010 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002011 break;
2012 c = utf_ptr2char(old_regparse + i);
2013 }
2014 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002015 regparse = old_regparse + plen;
2016 }
2017 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018#endif
2019 {
2020 c = no_Magic(c);
2021 EMIT(c);
2022 }
2023 return OK;
2024 }
2025 }
2026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027 return OK;
2028}
2029
2030/*
2031 * Parse something followed by possible [*+=].
2032 *
2033 * A piece is an atom, possibly followed by a multi, an indication of how many
2034 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2035 * characters: "", "a", "aa", etc.
2036 *
2037 * piece ::= atom
2038 * or atom multi
2039 */
2040 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002041nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042{
2043 int i;
2044 int op;
2045 int ret;
2046 long minval, maxval;
2047 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002048 parse_state_T old_state;
2049 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002050 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002051 int old_post_pos;
2052 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 int quest;
2054
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002055 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2056 * next. */
2057 save_parse_state(&old_state);
2058
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002060 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061
2062 ret = nfa_regatom();
2063 if (ret == FAIL)
2064 return FAIL; /* cascaded error */
2065
2066 op = peekchr();
2067 if (re_multi_type(op) == NOT_MULTI)
2068 return OK;
2069
2070 skipchr();
2071 switch (op)
2072 {
2073 case Magic('*'):
2074 EMIT(NFA_STAR);
2075 break;
2076
2077 case Magic('+'):
2078 /*
2079 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2080 * first and only submatch would be "aaa". But the backtracking
2081 * engine interprets the plus as "try matching one more time", and
2082 * a* matches a second time at the end of the input, the empty
2083 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002084 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002086 * In order to be consistent with the old engine, we replace
2087 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002089 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090 curchr = -1;
2091 if (nfa_regatom() == FAIL)
2092 return FAIL;
2093 EMIT(NFA_STAR);
2094 EMIT(NFA_CONCAT);
2095 skipchr(); /* skip the \+ */
2096 break;
2097
2098 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002099 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002101 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102 switch(op)
2103 {
2104 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002105 /* \@= */
2106 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002107 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002108 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002109 /* \@! */
2110 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002111 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002113 op = no_Magic(getchr());
2114 if (op == '=')
2115 /* \@<= */
2116 i = NFA_PREV_ATOM_JUST_BEFORE;
2117 else if (op == '!')
2118 /* \@<! */
2119 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2120 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002121 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002122 /* \@> */
2123 i = NFA_PREV_ATOM_LIKE_PATTERN;
2124 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002125 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002126 if (i == 0)
2127 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002128 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2129 return FAIL;
2130 }
2131 EMIT(i);
2132 if (i == NFA_PREV_ATOM_JUST_BEFORE
2133 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2134 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002135 break;
2136
2137 case Magic('?'):
2138 case Magic('='):
2139 EMIT(NFA_QUEST);
2140 break;
2141
2142 case Magic('{'):
2143 /* a{2,5} will expand to 'aaa?a?a?'
2144 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2145 * version of '?'
2146 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2147 * parenthesis have the same id
2148 */
2149
2150 greedy = TRUE;
2151 c2 = peekchr();
2152 if (c2 == '-' || c2 == Magic('-'))
2153 {
2154 skipchr();
2155 greedy = FALSE;
2156 }
2157 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002158 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002160 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2161 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002162 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002164 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002165 /* \{}, \{0,} */
2166 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002167 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002168 /* \{-}, \{-0,} */
2169 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 break;
2171 }
2172
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 /* Special case: x{0} or x{-0} */
2174 if (maxval == 0)
2175 {
2176 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002177 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002178 /* NFA_EMPTY is 0-length and works everywhere */
2179 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 return OK;
2181 }
2182
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002183 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002184 * maximum is much larger than the minimum and when the maximum is
2185 * large. Bail out if we can use the other engine. */
2186 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002187 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002188 return FAIL;
2189
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002190 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002191 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002192 /* Save parse state after the repeated atom and the \{} */
2193 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002195 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2196 for (i = 0; i < maxval; i++)
2197 {
2198 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002199 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002200 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 if (nfa_regatom() == FAIL)
2202 return FAIL;
2203 /* after "minval" times, atoms are optional */
2204 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002205 {
2206 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002207 {
2208 if (greedy)
2209 EMIT(NFA_STAR);
2210 else
2211 EMIT(NFA_STAR_NONGREEDY);
2212 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002213 else
2214 EMIT(quest);
2215 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002216 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002218 if (i + 1 > minval && maxval == MAX_LIMIT)
2219 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 }
2221
2222 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002223 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 curchr = -1;
2225
2226 break;
2227
2228
2229 default:
2230 break;
2231 } /* end switch */
2232
2233 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236
2237 return OK;
2238}
2239
2240/*
2241 * Parse one or more pieces, concatenated. It matches a match for the
2242 * first piece, followed by a match for the second piece, etc. Example:
2243 * "f[0-9]b", first matches "f", then a digit and then "b".
2244 *
2245 * concat ::= piece
2246 * or piece piece
2247 * or piece piece piece
2248 * etc.
2249 */
2250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002251nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252{
2253 int cont = TRUE;
2254 int first = TRUE;
2255
2256 while (cont)
2257 {
2258 switch (peekchr())
2259 {
2260 case NUL:
2261 case Magic('|'):
2262 case Magic('&'):
2263 case Magic(')'):
2264 cont = FALSE;
2265 break;
2266
2267 case Magic('Z'):
2268#ifdef FEAT_MBYTE
2269 regflags |= RF_ICOMBINE;
2270#endif
2271 skipchr_keepstart();
2272 break;
2273 case Magic('c'):
2274 regflags |= RF_ICASE;
2275 skipchr_keepstart();
2276 break;
2277 case Magic('C'):
2278 regflags |= RF_NOICASE;
2279 skipchr_keepstart();
2280 break;
2281 case Magic('v'):
2282 reg_magic = MAGIC_ALL;
2283 skipchr_keepstart();
2284 curchr = -1;
2285 break;
2286 case Magic('m'):
2287 reg_magic = MAGIC_ON;
2288 skipchr_keepstart();
2289 curchr = -1;
2290 break;
2291 case Magic('M'):
2292 reg_magic = MAGIC_OFF;
2293 skipchr_keepstart();
2294 curchr = -1;
2295 break;
2296 case Magic('V'):
2297 reg_magic = MAGIC_NONE;
2298 skipchr_keepstart();
2299 curchr = -1;
2300 break;
2301
2302 default:
2303 if (nfa_regpiece() == FAIL)
2304 return FAIL;
2305 if (first == FALSE)
2306 EMIT(NFA_CONCAT);
2307 else
2308 first = FALSE;
2309 break;
2310 }
2311 }
2312
2313 return OK;
2314}
2315
2316/*
2317 * Parse a branch, one or more concats, separated by "\&". It matches the
2318 * last concat, but only if all the preceding concats also match at the same
2319 * position. Examples:
2320 * "foobeep\&..." matches "foo" in "foobeep".
2321 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2322 *
2323 * branch ::= concat
2324 * or concat \& concat
2325 * or concat \& concat \& concat
2326 * etc.
2327 */
2328 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002329nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002331 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002332
Bram Moolenaar16299b52013-05-30 18:45:23 +02002333 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334
2335 /* First branch, possibly the only one */
2336 if (nfa_regconcat() == FAIL)
2337 return FAIL;
2338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002340 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002341 {
2342 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002343 /* if concat is empty do emit a node */
2344 if (old_post_pos == (int)(post_ptr - post_start))
2345 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346 EMIT(NFA_NOPEN);
2347 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002348 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349 if (nfa_regconcat() == FAIL)
2350 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002351 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002352 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002353 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 }
2356
Bram Moolenaar699c1202013-09-25 16:41:54 +02002357 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002358 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002359 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002360
2361 return OK;
2362}
2363
2364/*
2365 * Parse a pattern, one or more branches, separated by "\|". It matches
2366 * anything that matches one of the branches. Example: "foo\|beep" matches
2367 * "foo" and matches "beep". If more than one branch matches, the first one
2368 * is used.
2369 *
2370 * pattern ::= branch
2371 * or branch \| branch
2372 * or branch \| branch \| branch
2373 * etc.
2374 */
2375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002376nfa_reg(
2377 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002378{
2379 int parno = 0;
2380
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 if (paren == REG_PAREN)
2382 {
2383 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385 parno = regnpar++;
2386 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002387#ifdef FEAT_SYN_HL
2388 else if (paren == REG_ZPAREN)
2389 {
2390 /* Make a ZOPEN node. */
2391 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002392 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002393 parno = regnzpar++;
2394 }
2395#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396
2397 if (nfa_regbranch() == FAIL)
2398 return FAIL; /* cascaded error */
2399
2400 while (peekchr() == Magic('|'))
2401 {
2402 skipchr();
2403 if (nfa_regbranch() == FAIL)
2404 return FAIL; /* cascaded error */
2405 EMIT(NFA_OR);
2406 }
2407
2408 /* Check for proper termination. */
2409 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2410 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002411 if (paren == REG_NPAREN)
2412 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2413 else
2414 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2415 }
2416 else if (paren == REG_NOPAREN && peekchr() != NUL)
2417 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002418 if (peekchr() == Magic(')'))
2419 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2420 else
2421 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2422 }
2423 /*
2424 * Here we set the flag allowing back references to this set of
2425 * parentheses.
2426 */
2427 if (paren == REG_PAREN)
2428 {
2429 had_endbrace[parno] = TRUE; /* have seen the close paren */
2430 EMIT(NFA_MOPEN + parno);
2431 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002432#ifdef FEAT_SYN_HL
2433 else if (paren == REG_ZPAREN)
2434 EMIT(NFA_ZOPEN + parno);
2435#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436
2437 return OK;
2438}
2439
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440#ifdef DEBUG
2441static char_u code[50];
2442
2443 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002444nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445{
2446 int addnl = FALSE;
2447
2448 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2449 {
2450 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002451 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 }
2453
2454 STRCPY(code, "");
2455 switch (c)
2456 {
2457 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2458 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2459 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2460 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2461 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2462 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2463
Bram Moolenaar5714b802013-05-28 22:03:20 +02002464 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2465 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2466 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2467 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2468 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2469 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2470 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2471 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2472 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002473#ifdef FEAT_SYN_HL
2474 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2475 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2476 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2477 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2478 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2479 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2480 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2481 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2482 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2483#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002484 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2485
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 case NFA_PREV_ATOM_NO_WIDTH:
2487 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002488 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2489 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002490 case NFA_PREV_ATOM_JUST_BEFORE:
2491 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2492 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2493 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002494 case NFA_PREV_ATOM_LIKE_PATTERN:
2495 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2496
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002497 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2498 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002500 case NFA_START_INVISIBLE_FIRST:
2501 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002502 case NFA_START_INVISIBLE_NEG:
2503 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002504 case NFA_START_INVISIBLE_NEG_FIRST:
2505 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002506 case NFA_START_INVISIBLE_BEFORE:
2507 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002508 case NFA_START_INVISIBLE_BEFORE_FIRST:
2509 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002510 case NFA_START_INVISIBLE_BEFORE_NEG:
2511 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002512 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2513 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002514 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002516 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002517 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2520 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002521 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002523 case NFA_MOPEN:
2524 case NFA_MOPEN1:
2525 case NFA_MOPEN2:
2526 case NFA_MOPEN3:
2527 case NFA_MOPEN4:
2528 case NFA_MOPEN5:
2529 case NFA_MOPEN6:
2530 case NFA_MOPEN7:
2531 case NFA_MOPEN8:
2532 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 STRCPY(code, "NFA_MOPEN(x)");
2534 code[10] = c - NFA_MOPEN + '0';
2535 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002536 case NFA_MCLOSE:
2537 case NFA_MCLOSE1:
2538 case NFA_MCLOSE2:
2539 case NFA_MCLOSE3:
2540 case NFA_MCLOSE4:
2541 case NFA_MCLOSE5:
2542 case NFA_MCLOSE6:
2543 case NFA_MCLOSE7:
2544 case NFA_MCLOSE8:
2545 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002546 STRCPY(code, "NFA_MCLOSE(x)");
2547 code[11] = c - NFA_MCLOSE + '0';
2548 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002549#ifdef FEAT_SYN_HL
2550 case NFA_ZOPEN:
2551 case NFA_ZOPEN1:
2552 case NFA_ZOPEN2:
2553 case NFA_ZOPEN3:
2554 case NFA_ZOPEN4:
2555 case NFA_ZOPEN5:
2556 case NFA_ZOPEN6:
2557 case NFA_ZOPEN7:
2558 case NFA_ZOPEN8:
2559 case NFA_ZOPEN9:
2560 STRCPY(code, "NFA_ZOPEN(x)");
2561 code[10] = c - NFA_ZOPEN + '0';
2562 break;
2563 case NFA_ZCLOSE:
2564 case NFA_ZCLOSE1:
2565 case NFA_ZCLOSE2:
2566 case NFA_ZCLOSE3:
2567 case NFA_ZCLOSE4:
2568 case NFA_ZCLOSE5:
2569 case NFA_ZCLOSE6:
2570 case NFA_ZCLOSE7:
2571 case NFA_ZCLOSE8:
2572 case NFA_ZCLOSE9:
2573 STRCPY(code, "NFA_ZCLOSE(x)");
2574 code[11] = c - NFA_ZCLOSE + '0';
2575 break;
2576#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002577 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2578 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2579 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2580 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002581 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2582 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002583 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2584 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2585 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2586 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2587 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2588 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2589 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2590 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2591 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2592 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2593 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2594 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2595 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2596 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002597 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002600 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2601 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2602 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002603 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002604 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002605
2606 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2607 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2608 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2609 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2610 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2611 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2612 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2615 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2616 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2617 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2618 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2619 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2620 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2621 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2622 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2623 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2624 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2625 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2626 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2627 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2628 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2629 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2630
2631 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2632 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2633 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2634 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2635 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2636 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2637 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2638 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2639 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2640 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2641 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2642 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2643 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2644 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2645 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2646 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2647 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2648 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2649 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2650 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2651 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2652 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2653 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2654 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2655 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2656 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2657 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002658 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2659 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2660 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2661 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662
2663 default:
2664 STRCPY(code, "CHAR(x)");
2665 code[5] = c;
2666 }
2667
2668 if (addnl == TRUE)
2669 STRCAT(code, " + NEWLINE ");
2670
2671}
2672
2673#ifdef ENABLE_LOG
2674static FILE *log_fd;
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002675static 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 +02002676
2677/*
2678 * Print the postfix notation of the current regexp.
2679 */
2680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002681nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002682{
2683 int *p;
2684 FILE *f;
2685
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002686 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002687 if (f != NULL)
2688 {
2689 fprintf(f, "\n-------------------------\n");
2690 if (retval == FAIL)
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02002691 fprintf(f, ">>> NFA engine failed... \n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002692 else if (retval == OK)
2693 fprintf(f, ">>> NFA engine succeeded !\n");
2694 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002695 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002696 {
2697 nfa_set_code(*p);
2698 fprintf(f, "%s, ", code);
2699 }
2700 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002701 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002702 fprintf(f, "%d ", *p);
2703 fprintf(f, "\n\n");
2704 fclose(f);
2705 }
2706}
2707
2708/*
2709 * Print the NFA starting with a root node "state".
2710 */
2711 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002712nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002713{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002714 garray_T indent;
2715
2716 ga_init2(&indent, 1, 64);
2717 ga_append(&indent, '\0');
2718 nfa_print_state2(debugf, state, &indent);
2719 ga_clear(&indent);
2720}
2721
2722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002723nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002724{
2725 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002726
2727 if (state == NULL)
2728 return;
2729
2730 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002731
2732 /* Output indent */
2733 p = (char_u *)indent->ga_data;
2734 if (indent->ga_len >= 3)
2735 {
2736 int last = indent->ga_len - 3;
2737 char_u save[2];
2738
2739 STRNCPY(save, &p[last], 2);
2740 STRNCPY(&p[last], "+-", 2);
2741 fprintf(debugf, " %s", p);
2742 STRNCPY(&p[last], save, 2);
2743 }
2744 else
2745 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002746
2747 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002748 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002749 code,
2750 state->c,
2751 abs(state->id),
2752 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002753 if (state->id < 0)
2754 return;
2755
2756 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002757
2758 /* grow indent for state->out */
2759 indent->ga_len -= 1;
2760 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002761 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002762 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002763 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002764 ga_append(indent, '\0');
2765
2766 nfa_print_state2(debugf, state->out, indent);
2767
2768 /* replace last part of indent for state->out1 */
2769 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002770 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002771 ga_append(indent, '\0');
2772
2773 nfa_print_state2(debugf, state->out1, indent);
2774
2775 /* shrink indent */
2776 indent->ga_len -= 3;
2777 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002778}
2779
2780/*
2781 * Print the NFA state machine.
2782 */
2783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002784nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002785{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002786 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002787
2788 if (debugf != NULL)
2789 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002790 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002791
Bram Moolenaar473de612013-06-08 18:19:48 +02002792 if (prog->reganch)
2793 fprintf(debugf, "reganch: %d\n", prog->reganch);
2794 if (prog->regstart != NUL)
2795 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2796 prog->regstart, prog->regstart);
2797 if (prog->match_text != NULL)
2798 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002799
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800 fclose(debugf);
2801 }
2802}
2803#endif /* ENABLE_LOG */
2804#endif /* DEBUG */
2805
2806/*
2807 * Parse r.e. @expr and convert it into postfix form.
2808 * Return the postfix string on success, NULL otherwise.
2809 */
2810 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002811re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812{
2813 if (nfa_reg(REG_NOPAREN) == FAIL)
2814 return NULL;
2815 EMIT(NFA_MOPEN);
2816 return post_start;
2817}
2818
2819/* NB. Some of the code below is inspired by Russ's. */
2820
2821/*
2822 * Represents an NFA state plus zero or one or two arrows exiting.
2823 * if c == MATCH, no arrows out; matching state.
2824 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2825 * If c < 256, labeled arrow with character c to out.
2826 */
2827
2828static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2829
2830/*
2831 * Allocate and initialize nfa_state_T.
2832 */
2833 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002834alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835{
2836 nfa_state_T *s;
2837
2838 if (istate >= nstate)
2839 return NULL;
2840
2841 s = &state_ptr[istate++];
2842
2843 s->c = c;
2844 s->out = out;
2845 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002846 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002847
2848 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002849 s->lastlist[0] = 0;
2850 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851
2852 return s;
2853}
2854
2855/*
2856 * A partially built NFA without the matching state filled in.
2857 * Frag_T.start points at the start state.
2858 * Frag_T.out is a list of places that need to be set to the
2859 * next state for this fragment.
2860 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002861
2862/* Since the out pointers in the list are always
2863 * uninitialized, we use the pointers themselves
2864 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002865typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002866union Ptrlist
2867{
2868 Ptrlist *next;
2869 nfa_state_T *s;
2870};
2871
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002872struct Frag
2873{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002874 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875 Ptrlist *out;
2876};
2877typedef struct Frag Frag_T;
2878
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002879static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2880static Ptrlist *list1(nfa_state_T **outp);
2881static void patch(Ptrlist *l, nfa_state_T *s);
2882static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2883static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2884static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002885
2886/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002887 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888 */
2889 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002890frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002892 Frag_T n;
2893
2894 n.start = start;
2895 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002896 return n;
2897}
2898
2899/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002900 * Create singleton list containing just outp.
2901 */
2902 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002903list1(
2904 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002905{
2906 Ptrlist *l;
2907
2908 l = (Ptrlist *)outp;
2909 l->next = NULL;
2910 return l;
2911}
2912
2913/*
2914 * Patch the list of states at out to point to start.
2915 */
2916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002917patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918{
2919 Ptrlist *next;
2920
2921 for (; l; l = next)
2922 {
2923 next = l->next;
2924 l->s = s;
2925 }
2926}
2927
2928
2929/*
2930 * Join the two lists l1 and l2, returning the combination.
2931 */
2932 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002933append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934{
2935 Ptrlist *oldl1;
2936
2937 oldl1 = l1;
2938 while (l1->next)
2939 l1 = l1->next;
2940 l1->next = l2;
2941 return oldl1;
2942}
2943
2944/*
2945 * Stack used for transforming postfix form into NFA.
2946 */
2947static Frag_T empty;
2948
2949 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002950st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002951{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002952#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002953 FILE *df;
2954 int *p2;
2955
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002956 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002957 if (df)
2958 {
2959 fprintf(df, "Error popping the stack!\n");
2960#ifdef DEBUG
2961 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2962#endif
2963 fprintf(df, "Postfix form is: ");
2964#ifdef DEBUG
2965 for (p2 = postfix; p2 < end; p2++)
2966 {
2967 nfa_set_code(*p2);
2968 fprintf(df, "%s, ", code);
2969 }
2970 nfa_set_code(*p);
2971 fprintf(df, "\nCurrent position is: ");
2972 for (p2 = postfix; p2 <= p; p2 ++)
2973 {
2974 nfa_set_code(*p2);
2975 fprintf(df, "%s, ", code);
2976 }
2977#else
2978 for (p2 = postfix; p2 < end; p2++)
2979 {
2980 fprintf(df, "%d, ", *p2);
2981 }
2982 fprintf(df, "\nCurrent position is: ");
2983 for (p2 = postfix; p2 <= p; p2 ++)
2984 {
2985 fprintf(df, "%d, ", *p2);
2986 }
2987#endif
2988 fprintf(df, "\n--------------------------\n");
2989 fclose(df);
2990 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002991#endif
Bram Moolenaar5efa0102018-06-22 21:42:30 +02002992 EMSG(_("E874: (NFA) Could not pop the stack!"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002993}
2994
2995/*
2996 * Push an item onto the stack.
2997 */
2998 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002999st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000{
3001 Frag_T *stackp = *p;
3002
3003 if (stackp >= stack_end)
3004 return;
3005 *stackp = s;
3006 *p = *p + 1;
3007}
3008
3009/*
3010 * Pop an item from the stack.
3011 */
3012 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003013st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003014{
3015 Frag_T *stackp;
3016
3017 *p = *p - 1;
3018 stackp = *p;
3019 if (stackp < stack)
3020 return empty;
3021 return **p;
3022}
3023
3024/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003025 * Estimate the maximum byte length of anything matching "state".
3026 * When unknown or unlimited return -1.
3027 */
3028 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003029nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003030{
3031 int l, r;
3032 nfa_state_T *state = startstate;
3033 int len = 0;
3034
3035 /* detect looping in a NFA_SPLIT */
3036 if (depth > 4)
3037 return -1;
3038
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003039 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003040 {
3041 switch (state->c)
3042 {
3043 case NFA_END_INVISIBLE:
3044 case NFA_END_INVISIBLE_NEG:
3045 /* the end, return what we have */
3046 return len;
3047
3048 case NFA_SPLIT:
3049 /* two alternatives, use the maximum */
3050 l = nfa_max_width(state->out, depth + 1);
3051 r = nfa_max_width(state->out1, depth + 1);
3052 if (l < 0 || r < 0)
3053 return -1;
3054 return len + (l > r ? l : r);
3055
3056 case NFA_ANY:
3057 case NFA_START_COLL:
3058 case NFA_START_NEG_COLL:
3059 /* matches some character, including composing chars */
3060#ifdef FEAT_MBYTE
3061 if (enc_utf8)
3062 len += MB_MAXBYTES;
3063 else if (has_mbyte)
3064 len += 2;
3065 else
3066#endif
3067 ++len;
3068 if (state->c != NFA_ANY)
3069 {
3070 /* skip over the characters */
3071 state = state->out1->out;
3072 continue;
3073 }
3074 break;
3075
3076 case NFA_DIGIT:
3077 case NFA_WHITE:
3078 case NFA_HEX:
3079 case NFA_OCTAL:
3080 /* ascii */
3081 ++len;
3082 break;
3083
3084 case NFA_IDENT:
3085 case NFA_SIDENT:
3086 case NFA_KWORD:
3087 case NFA_SKWORD:
3088 case NFA_FNAME:
3089 case NFA_SFNAME:
3090 case NFA_PRINT:
3091 case NFA_SPRINT:
3092 case NFA_NWHITE:
3093 case NFA_NDIGIT:
3094 case NFA_NHEX:
3095 case NFA_NOCTAL:
3096 case NFA_WORD:
3097 case NFA_NWORD:
3098 case NFA_HEAD:
3099 case NFA_NHEAD:
3100 case NFA_ALPHA:
3101 case NFA_NALPHA:
3102 case NFA_LOWER:
3103 case NFA_NLOWER:
3104 case NFA_UPPER:
3105 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003106 case NFA_LOWER_IC:
3107 case NFA_NLOWER_IC:
3108 case NFA_UPPER_IC:
3109 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003110 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003111 /* possibly non-ascii */
3112#ifdef FEAT_MBYTE
3113 if (has_mbyte)
3114 len += 3;
3115 else
3116#endif
3117 ++len;
3118 break;
3119
3120 case NFA_START_INVISIBLE:
3121 case NFA_START_INVISIBLE_NEG:
3122 case NFA_START_INVISIBLE_BEFORE:
3123 case NFA_START_INVISIBLE_BEFORE_NEG:
3124 /* zero-width, out1 points to the END state */
3125 state = state->out1->out;
3126 continue;
3127
3128 case NFA_BACKREF1:
3129 case NFA_BACKREF2:
3130 case NFA_BACKREF3:
3131 case NFA_BACKREF4:
3132 case NFA_BACKREF5:
3133 case NFA_BACKREF6:
3134 case NFA_BACKREF7:
3135 case NFA_BACKREF8:
3136 case NFA_BACKREF9:
3137#ifdef FEAT_SYN_HL
3138 case NFA_ZREF1:
3139 case NFA_ZREF2:
3140 case NFA_ZREF3:
3141 case NFA_ZREF4:
3142 case NFA_ZREF5:
3143 case NFA_ZREF6:
3144 case NFA_ZREF7:
3145 case NFA_ZREF8:
3146 case NFA_ZREF9:
3147#endif
3148 case NFA_NEWL:
3149 case NFA_SKIP:
3150 /* unknown width */
3151 return -1;
3152
3153 case NFA_BOL:
3154 case NFA_EOL:
3155 case NFA_BOF:
3156 case NFA_EOF:
3157 case NFA_BOW:
3158 case NFA_EOW:
3159 case NFA_MOPEN:
3160 case NFA_MOPEN1:
3161 case NFA_MOPEN2:
3162 case NFA_MOPEN3:
3163 case NFA_MOPEN4:
3164 case NFA_MOPEN5:
3165 case NFA_MOPEN6:
3166 case NFA_MOPEN7:
3167 case NFA_MOPEN8:
3168 case NFA_MOPEN9:
3169#ifdef FEAT_SYN_HL
3170 case NFA_ZOPEN:
3171 case NFA_ZOPEN1:
3172 case NFA_ZOPEN2:
3173 case NFA_ZOPEN3:
3174 case NFA_ZOPEN4:
3175 case NFA_ZOPEN5:
3176 case NFA_ZOPEN6:
3177 case NFA_ZOPEN7:
3178 case NFA_ZOPEN8:
3179 case NFA_ZOPEN9:
3180 case NFA_ZCLOSE:
3181 case NFA_ZCLOSE1:
3182 case NFA_ZCLOSE2:
3183 case NFA_ZCLOSE3:
3184 case NFA_ZCLOSE4:
3185 case NFA_ZCLOSE5:
3186 case NFA_ZCLOSE6:
3187 case NFA_ZCLOSE7:
3188 case NFA_ZCLOSE8:
3189 case NFA_ZCLOSE9:
3190#endif
3191 case NFA_MCLOSE:
3192 case NFA_MCLOSE1:
3193 case NFA_MCLOSE2:
3194 case NFA_MCLOSE3:
3195 case NFA_MCLOSE4:
3196 case NFA_MCLOSE5:
3197 case NFA_MCLOSE6:
3198 case NFA_MCLOSE7:
3199 case NFA_MCLOSE8:
3200 case NFA_MCLOSE9:
3201 case NFA_NOPEN:
3202 case NFA_NCLOSE:
3203
3204 case NFA_LNUM_GT:
3205 case NFA_LNUM_LT:
3206 case NFA_COL_GT:
3207 case NFA_COL_LT:
3208 case NFA_VCOL_GT:
3209 case NFA_VCOL_LT:
3210 case NFA_MARK_GT:
3211 case NFA_MARK_LT:
3212 case NFA_VISUAL:
3213 case NFA_LNUM:
3214 case NFA_CURSOR:
3215 case NFA_COL:
3216 case NFA_VCOL:
3217 case NFA_MARK:
3218
3219 case NFA_ZSTART:
3220 case NFA_ZEND:
3221 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003222 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003223 case NFA_START_PATTERN:
3224 case NFA_END_PATTERN:
3225 case NFA_COMPOSING:
3226 case NFA_END_COMPOSING:
3227 /* zero-width */
3228 break;
3229
3230 default:
3231 if (state->c < 0)
3232 /* don't know what this is */
3233 return -1;
3234 /* normal character */
3235 len += MB_CHAR2LEN(state->c);
3236 break;
3237 }
3238
3239 /* normal way to continue */
3240 state = state->out;
3241 }
3242
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003243 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003244 return -1;
3245}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003246
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003247/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003248 * Convert a postfix form into its equivalent NFA.
3249 * Return the NFA start state on success, NULL otherwise.
3250 */
3251 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003252post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003253{
3254 int *p;
3255 int mopen;
3256 int mclose;
3257 Frag_T *stack = NULL;
3258 Frag_T *stackp = NULL;
3259 Frag_T *stack_end = NULL;
3260 Frag_T e1;
3261 Frag_T e2;
3262 Frag_T e;
3263 nfa_state_T *s;
3264 nfa_state_T *s1;
3265 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003266 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267
3268 if (postfix == NULL)
3269 return NULL;
3270
Bram Moolenaar053bb602013-05-20 13:55:21 +02003271#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003272#define POP() st_pop(&stackp, stack); \
3273 if (stackp < stack) \
3274 { \
3275 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003276 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003277 return NULL; \
3278 }
3279
3280 if (nfa_calc_size == FALSE)
3281 {
3282 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003283 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003285 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003286 }
3287
3288 for (p = postfix; p < end; ++p)
3289 {
3290 switch (*p)
3291 {
3292 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003293 /* Concatenation.
3294 * Pay attention: this operator does not exist in the r.e. itself
3295 * (it is implicit, really). It is added when r.e. is translated
3296 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 if (nfa_calc_size == TRUE)
3298 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003299 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 break;
3301 }
3302 e2 = POP();
3303 e1 = POP();
3304 patch(e1.out, e2.start);
3305 PUSH(frag(e1.start, e2.out));
3306 break;
3307
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003308 case NFA_OR:
3309 /* Alternation */
3310 if (nfa_calc_size == TRUE)
3311 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003312 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003313 break;
3314 }
3315 e2 = POP();
3316 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003317 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003319 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 PUSH(frag(s, append(e1.out, e2.out)));
3321 break;
3322
3323 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003324 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325 if (nfa_calc_size == TRUE)
3326 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003327 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003328 break;
3329 }
3330 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003331 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003333 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003334 patch(e.out, s);
3335 PUSH(frag(s, list1(&s->out1)));
3336 break;
3337
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003338 case NFA_STAR_NONGREEDY:
3339 /* Zero or more, prefer zero */
3340 if (nfa_calc_size == TRUE)
3341 {
3342 nstate++;
3343 break;
3344 }
3345 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003346 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003347 if (s == NULL)
3348 goto theend;
3349 patch(e.out, s);
3350 PUSH(frag(s, list1(&s->out)));
3351 break;
3352
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003353 case NFA_QUEST:
3354 /* one or zero atoms=> greedy match */
3355 if (nfa_calc_size == TRUE)
3356 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003357 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003358 break;
3359 }
3360 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003361 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003363 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 PUSH(frag(s, append(e.out, list1(&s->out1))));
3365 break;
3366
3367 case NFA_QUEST_NONGREEDY:
3368 /* zero or one atoms => non-greedy match */
3369 if (nfa_calc_size == TRUE)
3370 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003371 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003372 break;
3373 }
3374 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003375 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003376 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003377 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003378 PUSH(frag(s, append(e.out, list1(&s->out))));
3379 break;
3380
Bram Moolenaar417bad22013-06-07 14:08:30 +02003381 case NFA_END_COLL:
3382 case NFA_END_NEG_COLL:
3383 /* On the stack is the sequence starting with NFA_START_COLL or
3384 * NFA_START_NEG_COLL and all possible characters. Patch it to
3385 * add the output to the start. */
3386 if (nfa_calc_size == TRUE)
3387 {
3388 nstate++;
3389 break;
3390 }
3391 e = POP();
3392 s = alloc_state(NFA_END_COLL, NULL, NULL);
3393 if (s == NULL)
3394 goto theend;
3395 patch(e.out, s);
3396 e.start->out1 = s;
3397 PUSH(frag(e.start, list1(&s->out)));
3398 break;
3399
3400 case NFA_RANGE:
3401 /* Before this are two characters, the low and high end of a
3402 * range. Turn them into two states with MIN and MAX. */
3403 if (nfa_calc_size == TRUE)
3404 {
3405 /* nstate += 0; */
3406 break;
3407 }
3408 e2 = POP();
3409 e1 = POP();
3410 e2.start->val = e2.start->c;
3411 e2.start->c = NFA_RANGE_MAX;
3412 e1.start->val = e1.start->c;
3413 e1.start->c = NFA_RANGE_MIN;
3414 patch(e1.out, e2.start);
3415 PUSH(frag(e1.start, e2.out));
3416 break;
3417
Bram Moolenaar699c1202013-09-25 16:41:54 +02003418 case NFA_EMPTY:
3419 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003420 if (nfa_calc_size == TRUE)
3421 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003422 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003423 break;
3424 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003425 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003426 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003427 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 PUSH(frag(s, list1(&s->out)));
3429 break;
3430
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003431 case NFA_OPT_CHARS:
3432 {
3433 int n;
3434
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003435 /* \%[abc] implemented as:
3436 * NFA_SPLIT
3437 * +-CHAR(a)
3438 * | +-NFA_SPLIT
3439 * | +-CHAR(b)
3440 * | | +-NFA_SPLIT
3441 * | | +-CHAR(c)
3442 * | | | +-next
3443 * | | +- next
3444 * | +- next
3445 * +- next
3446 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003447 n = *++p; /* get number of characters */
3448 if (nfa_calc_size == TRUE)
3449 {
3450 nstate += n;
3451 break;
3452 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003453 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003454 e1.out = NULL; /* stores list with out1's */
3455 s1 = NULL; /* previous NFA_SPLIT to connect to */
3456 while (n-- > 0)
3457 {
3458 e = POP(); /* get character */
3459 s = alloc_state(NFA_SPLIT, e.start, NULL);
3460 if (s == NULL)
3461 goto theend;
3462 if (e1.out == NULL)
3463 e1 = e;
3464 patch(e.out, s1);
3465 append(e1.out, list1(&s->out1));
3466 s1 = s;
3467 }
3468 PUSH(frag(s, e1.out));
3469 break;
3470 }
3471
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003472 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003473 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003474 case NFA_PREV_ATOM_JUST_BEFORE:
3475 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003476 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003477 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003478 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3479 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003480 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003481 int start_state;
3482 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003483 int n = 0;
3484 nfa_state_T *zend;
3485 nfa_state_T *skip;
3486
Bram Moolenaardecd9542013-06-07 16:31:50 +02003487 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003488 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003489 case NFA_PREV_ATOM_NO_WIDTH:
3490 start_state = NFA_START_INVISIBLE;
3491 end_state = NFA_END_INVISIBLE;
3492 break;
3493 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3494 start_state = NFA_START_INVISIBLE_NEG;
3495 end_state = NFA_END_INVISIBLE_NEG;
3496 break;
3497 case NFA_PREV_ATOM_JUST_BEFORE:
3498 start_state = NFA_START_INVISIBLE_BEFORE;
3499 end_state = NFA_END_INVISIBLE;
3500 break;
3501 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3502 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3503 end_state = NFA_END_INVISIBLE_NEG;
3504 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003505 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003506 start_state = NFA_START_PATTERN;
3507 end_state = NFA_END_PATTERN;
3508 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003509 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003510
3511 if (before)
3512 n = *++p; /* get the count */
3513
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003514 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003515 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003516 * The \@<= operator: match for the preceding atom.
3517 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003519 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520
3521 if (nfa_calc_size == TRUE)
3522 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003523 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 break;
3525 }
3526 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003527 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003528 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003529 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003530
Bram Moolenaar87953742013-06-05 18:52:40 +02003531 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003532 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003533 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003534 if (pattern)
3535 {
3536 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3537 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003538 if (skip == NULL)
3539 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003540 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003541 if (zend == NULL)
3542 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003543 s1->out= skip;
3544 patch(e.out, zend);
3545 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003546 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003547 else
3548 {
3549 patch(e.out, s1);
3550 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003551 if (before)
3552 {
3553 if (n <= 0)
3554 /* See if we can guess the maximum width, it avoids a
3555 * lot of pointless tries. */
3556 n = nfa_max_width(e.start, 0);
3557 s->val = n; /* store the count */
3558 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003559 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003560 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003561 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003562
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003563#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003564 case NFA_COMPOSING: /* char with composing char */
3565#if 0
3566 /* TODO */
3567 if (regflags & RF_ICOMBINE)
3568 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003569 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003570 }
3571#endif
3572 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003573#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003574
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003575 case NFA_MOPEN: /* \( \) Submatch */
3576 case NFA_MOPEN1:
3577 case NFA_MOPEN2:
3578 case NFA_MOPEN3:
3579 case NFA_MOPEN4:
3580 case NFA_MOPEN5:
3581 case NFA_MOPEN6:
3582 case NFA_MOPEN7:
3583 case NFA_MOPEN8:
3584 case NFA_MOPEN9:
3585#ifdef FEAT_SYN_HL
3586 case NFA_ZOPEN: /* \z( \) Submatch */
3587 case NFA_ZOPEN1:
3588 case NFA_ZOPEN2:
3589 case NFA_ZOPEN3:
3590 case NFA_ZOPEN4:
3591 case NFA_ZOPEN5:
3592 case NFA_ZOPEN6:
3593 case NFA_ZOPEN7:
3594 case NFA_ZOPEN8:
3595 case NFA_ZOPEN9:
3596#endif
3597 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003598 if (nfa_calc_size == TRUE)
3599 {
3600 nstate += 2;
3601 break;
3602 }
3603
3604 mopen = *p;
3605 switch (*p)
3606 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003607 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3608#ifdef FEAT_SYN_HL
3609 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3610 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3611 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3612 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3613 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3614 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3615 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3616 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3617 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3618 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3619#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003620#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003621 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003622#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003624 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003625 mclose = *p + NSUBEXP;
3626 break;
3627 }
3628
3629 /* Allow "NFA_MOPEN" as a valid postfix representation for
3630 * the empty regexp "". In this case, the NFA will be
3631 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3632 * empty groups of parenthesis, and empty mbyte chars */
3633 if (stackp == stack)
3634 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003635 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003637 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003638 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003639 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003640 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641 patch(list1(&s->out), s1);
3642 PUSH(frag(s, list1(&s1->out)));
3643 break;
3644 }
3645
3646 /* At least one node was emitted before NFA_MOPEN, so
3647 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3648 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003649 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003650 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003651 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003652
Bram Moolenaar525666f2013-06-02 16:40:55 +02003653 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003654 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003655 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003656 patch(e.out, s1);
3657
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003658#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003659 if (mopen == NFA_COMPOSING)
3660 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003661 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003662#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003663
3664 PUSH(frag(s, list1(&s1->out)));
3665 break;
3666
Bram Moolenaar5714b802013-05-28 22:03:20 +02003667 case NFA_BACKREF1:
3668 case NFA_BACKREF2:
3669 case NFA_BACKREF3:
3670 case NFA_BACKREF4:
3671 case NFA_BACKREF5:
3672 case NFA_BACKREF6:
3673 case NFA_BACKREF7:
3674 case NFA_BACKREF8:
3675 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003676#ifdef FEAT_SYN_HL
3677 case NFA_ZREF1:
3678 case NFA_ZREF2:
3679 case NFA_ZREF3:
3680 case NFA_ZREF4:
3681 case NFA_ZREF5:
3682 case NFA_ZREF6:
3683 case NFA_ZREF7:
3684 case NFA_ZREF8:
3685 case NFA_ZREF9:
3686#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003687 if (nfa_calc_size == TRUE)
3688 {
3689 nstate += 2;
3690 break;
3691 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003692 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003693 if (s == NULL)
3694 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003695 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003696 if (s1 == NULL)
3697 goto theend;
3698 patch(list1(&s->out), s1);
3699 PUSH(frag(s, list1(&s1->out)));
3700 break;
3701
Bram Moolenaar423532e2013-05-29 21:14:42 +02003702 case NFA_LNUM:
3703 case NFA_LNUM_GT:
3704 case NFA_LNUM_LT:
3705 case NFA_VCOL:
3706 case NFA_VCOL_GT:
3707 case NFA_VCOL_LT:
3708 case NFA_COL:
3709 case NFA_COL_GT:
3710 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003711 case NFA_MARK:
3712 case NFA_MARK_GT:
3713 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003714 {
3715 int n = *++p; /* lnum, col or mark name */
3716
Bram Moolenaar423532e2013-05-29 21:14:42 +02003717 if (nfa_calc_size == TRUE)
3718 {
3719 nstate += 1;
3720 break;
3721 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003722 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003723 if (s == NULL)
3724 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003725 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003726 PUSH(frag(s, list1(&s->out)));
3727 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003728 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003729
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003730 case NFA_ZSTART:
3731 case NFA_ZEND:
3732 default:
3733 /* Operands */
3734 if (nfa_calc_size == TRUE)
3735 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003736 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003737 break;
3738 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003739 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003740 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003741 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003742 PUSH(frag(s, list1(&s->out)));
3743 break;
3744
3745 } /* switch(*p) */
3746
3747 } /* for(p = postfix; *p; ++p) */
3748
3749 if (nfa_calc_size == TRUE)
3750 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003751 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003752 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003753 }
3754
3755 e = POP();
3756 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003757 {
3758 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 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 +02003760 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003761
3762 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003763 {
3764 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003765 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003766 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768 matchstate = &state_ptr[istate++]; /* the match state */
3769 matchstate->c = NFA_MATCH;
3770 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003771 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003772
3773 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003774 ret = e.start;
3775
3776theend:
3777 vim_free(stack);
3778 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779
3780#undef POP1
3781#undef PUSH1
3782#undef POP2
3783#undef PUSH2
3784#undef POP
3785#undef PUSH
3786}
3787
Bram Moolenaara2947e22013-06-11 22:44:09 +02003788/*
3789 * After building the NFA program, inspect it to add optimization hints.
3790 */
3791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003792nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003793{
3794 int i;
3795 int c;
3796
3797 for (i = 0; i < prog->nstate; ++i)
3798 {
3799 c = prog->state[i].c;
3800 if (c == NFA_START_INVISIBLE
3801 || c == NFA_START_INVISIBLE_NEG
3802 || c == NFA_START_INVISIBLE_BEFORE
3803 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3804 {
3805 int directly;
3806
3807 /* Do it directly when what follows is possibly the end of the
3808 * match. */
3809 if (match_follows(prog->state[i].out1->out, 0))
3810 directly = TRUE;
3811 else
3812 {
3813 int ch_invisible = failure_chance(prog->state[i].out, 0);
3814 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3815
3816 /* Postpone when the invisible match is expensive or has a
3817 * lower chance of failing. */
3818 if (c == NFA_START_INVISIBLE_BEFORE
3819 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3820 {
3821 /* "before" matches are very expensive when
3822 * unbounded, always prefer what follows then,
3823 * unless what follows will always match.
3824 * Otherwise strongly prefer what follows. */
3825 if (prog->state[i].val <= 0 && ch_follows > 0)
3826 directly = FALSE;
3827 else
3828 directly = ch_follows * 10 < ch_invisible;
3829 }
3830 else
3831 {
3832 /* normal invisible, first do the one with the
3833 * highest failure chance */
3834 directly = ch_follows < ch_invisible;
3835 }
3836 }
3837 if (directly)
3838 /* switch to the _FIRST state */
3839 ++prog->state[i].c;
3840 }
3841 }
3842}
3843
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003844/****************************************************************
3845 * NFA execution code.
3846 ****************************************************************/
3847
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003848typedef struct
3849{
3850 int in_use; /* number of subexpr with useful info */
3851
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003852 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003853 union
3854 {
3855 struct multipos
3856 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003857 linenr_T start_lnum;
3858 linenr_T end_lnum;
3859 colnr_T start_col;
3860 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003861 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003862 struct linepos
3863 {
3864 char_u *start;
3865 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003866 } line[NSUBEXP];
3867 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003868} regsub_T;
3869
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003870typedef struct
3871{
3872 regsub_T norm; /* \( .. \) matches */
3873#ifdef FEAT_SYN_HL
3874 regsub_T synt; /* \z( .. \) matches */
3875#endif
3876} regsubs_T;
3877
Bram Moolenaara2d95102013-06-04 14:23:05 +02003878/* nfa_pim_T stores a Postponed Invisible Match. */
3879typedef struct nfa_pim_S nfa_pim_T;
3880struct nfa_pim_S
3881{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003882 int result; /* NFA_PIM_*, see below */
3883 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003884 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003885 union
3886 {
3887 lpos_T pos;
3888 char_u *ptr;
3889 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003890};
3891
3892/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003893#define NFA_PIM_UNUSED 0 /* pim not used */
3894#define NFA_PIM_TODO 1 /* pim not done yet */
3895#define NFA_PIM_MATCH 2 /* pim executed, matches */
3896#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003897
3898
Bram Moolenaar963fee22013-05-26 21:47:28 +02003899/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003900typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003901{
3902 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003903 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003904 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3905 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003906 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003907} nfa_thread_T;
3908
Bram Moolenaar963fee22013-05-26 21:47:28 +02003909/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910typedef struct
3911{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003912 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003913 int n; /* nr of states currently in "t" */
3914 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003915 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003916 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003917} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918
Bram Moolenaar5714b802013-05-28 22:03:20 +02003919#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003920static void log_subsexpr(regsubs_T *subs);
3921static void log_subexpr(regsub_T *sub);
3922static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003923
3924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003925log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003926{
3927 log_subexpr(&subs->norm);
3928# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003929 if (nfa_has_zsubexpr)
3930 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003931# endif
3932}
3933
Bram Moolenaar5714b802013-05-28 22:03:20 +02003934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003935log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003936{
3937 int j;
3938
3939 for (j = 0; j < sub->in_use; j++)
3940 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003941 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003942 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003943 sub->list.multi[j].start_col,
3944 (int)sub->list.multi[j].start_lnum,
3945 sub->list.multi[j].end_col,
3946 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003947 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003948 {
3949 char *s = (char *)sub->list.line[j].start;
3950 char *e = (char *)sub->list.line[j].end;
3951
Bram Moolenaar87953742013-06-05 18:52:40 +02003952 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003953 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003954 s == NULL ? "NULL" : s,
3955 e == NULL ? "NULL" : e);
3956 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003957}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003958
3959 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003960pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003961{
3962 static char buf[30];
3963
3964 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3965 buf[0] = NUL;
3966 else
3967 {
3968 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3969 : (int)(pim->end.ptr - reginput));
3970 }
3971 return buf;
3972}
3973
Bram Moolenaar5714b802013-05-28 22:03:20 +02003974#endif
3975
Bram Moolenaar963fee22013-05-26 21:47:28 +02003976/* Used during execution: whether a match has been found. */
3977static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003978#ifdef FEAT_RELTIME
3979static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003980static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003981static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003982#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003983
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003984static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3985static void clear_sub(regsub_T *sub);
3986static void copy_sub(regsub_T *to, regsub_T *from);
3987static void copy_sub_off(regsub_T *to, regsub_T *from);
3988static void copy_ze_off(regsub_T *to, regsub_T *from);
3989static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3990static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3991static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3992static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3993static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3994static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3995static void addstate_here(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003996
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003997/*
3998 * Copy postponed invisible match info from "from" to "to".
3999 */
4000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004001copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004002{
4003 to->result = from->result;
4004 to->state = from->state;
4005 copy_sub(&to->subs.norm, &from->subs.norm);
4006#ifdef FEAT_SYN_HL
4007 if (nfa_has_zsubexpr)
4008 copy_sub(&to->subs.synt, &from->subs.synt);
4009#endif
4010 to->end = from->end;
4011}
4012
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004013 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004014clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004015{
4016 if (REG_MULTI)
4017 /* Use 0xff to set lnum to -1 */
4018 vim_memset(sub->list.multi, 0xff,
4019 sizeof(struct multipos) * nfa_nsubexpr);
4020 else
4021 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4022 sub->in_use = 0;
4023}
4024
4025/*
4026 * Copy the submatches from "from" to "to".
4027 */
4028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004029copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004030{
4031 to->in_use = from->in_use;
4032 if (from->in_use > 0)
4033 {
4034 /* Copy the match start and end positions. */
4035 if (REG_MULTI)
4036 mch_memmove(&to->list.multi[0],
4037 &from->list.multi[0],
4038 sizeof(struct multipos) * from->in_use);
4039 else
4040 mch_memmove(&to->list.line[0],
4041 &from->list.line[0],
4042 sizeof(struct linepos) * from->in_use);
4043 }
4044}
4045
4046/*
4047 * Like copy_sub() but exclude the main match.
4048 */
4049 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004050copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004051{
4052 if (to->in_use < from->in_use)
4053 to->in_use = from->in_use;
4054 if (from->in_use > 1)
4055 {
4056 /* Copy the match start and end positions. */
4057 if (REG_MULTI)
4058 mch_memmove(&to->list.multi[1],
4059 &from->list.multi[1],
4060 sizeof(struct multipos) * (from->in_use - 1));
4061 else
4062 mch_memmove(&to->list.line[1],
4063 &from->list.line[1],
4064 sizeof(struct linepos) * (from->in_use - 1));
4065 }
4066}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067
Bram Moolenaar428e9872013-05-30 17:05:39 +02004068/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004069 * Like copy_sub() but only do the end of the main match if \ze is present.
4070 */
4071 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004072copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004073{
4074 if (nfa_has_zend)
4075 {
4076 if (REG_MULTI)
4077 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004078 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004079 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004080 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4081 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004082 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004083 }
4084 else
4085 {
4086 if (from->list.line[0].end != NULL)
4087 to->list.line[0].end = from->list.line[0].end;
4088 }
4089 }
4090}
4091
4092/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004093 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004094 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004095 */
4096 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004097sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004098{
4099 int i;
4100 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004101 linenr_T s1;
4102 linenr_T s2;
4103 char_u *sp1;
4104 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004105
4106 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4107 if (REG_MULTI)
4108 {
4109 for (i = 0; i < todo; ++i)
4110 {
4111 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004112 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004113 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004114 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004115 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004116 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004117 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004118 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004119 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004120 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004121 if (s1 != -1 && sub1->list.multi[i].start_col
4122 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004123 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004124
4125 if (nfa_has_backref)
4126 {
4127 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004128 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004129 else
4130 s1 = -1;
4131 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004132 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004133 else
4134 s2 = -1;
4135 if (s1 != s2)
4136 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004137 if (s1 != -1 && sub1->list.multi[i].end_col
4138 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004139 return FALSE;
4140 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004141 }
4142 }
4143 else
4144 {
4145 for (i = 0; i < todo; ++i)
4146 {
4147 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004148 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004149 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004150 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004151 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004152 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004153 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004154 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004155 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004156 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004157 if (nfa_has_backref)
4158 {
4159 if (i < sub1->in_use)
4160 sp1 = sub1->list.line[i].end;
4161 else
4162 sp1 = NULL;
4163 if (i < sub2->in_use)
4164 sp2 = sub2->list.line[i].end;
4165 else
4166 sp2 = NULL;
4167 if (sp1 != sp2)
4168 return FALSE;
4169 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004170 }
4171 }
4172
4173 return TRUE;
4174}
4175
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004176#ifdef ENABLE_LOG
4177 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004178report_state(char *action,
4179 regsub_T *sub,
4180 nfa_state_T *state,
4181 int lid,
4182 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004183{
4184 int col;
4185
4186 if (sub->in_use <= 0)
4187 col = -1;
4188 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004189 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004190 else
4191 col = (int)(sub->list.line[0].start - regline);
4192 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004193 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4194 action, abs(state->id), lid, state->c, code, col,
4195 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004196}
4197#endif
4198
Bram Moolenaar43e02982013-06-07 17:31:29 +02004199/*
4200 * Return TRUE if the same state is already in list "l" with the same
4201 * positions as "subs".
4202 */
4203 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004204has_state_with_pos(
4205 nfa_list_T *l, /* runtime state list */
4206 nfa_state_T *state, /* state to update */
4207 regsubs_T *subs, /* pointers to subexpressions */
4208 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004209{
4210 nfa_thread_T *thread;
4211 int i;
4212
4213 for (i = 0; i < l->n; ++i)
4214 {
4215 thread = &l->t[i];
4216 if (thread->state->id == state->id
4217 && sub_equal(&thread->subs.norm, &subs->norm)
4218#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004219 && (!nfa_has_zsubexpr
4220 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004221#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004222 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004223 return TRUE;
4224 }
4225 return FALSE;
4226}
4227
4228/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004229 * Return TRUE if "one" and "two" are equal. That includes when both are not
4230 * set.
4231 */
4232 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004233pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004234{
4235 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4236 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4237
4238 if (one_unused)
4239 /* one is unused: equal when two is also unused */
4240 return two_unused;
4241 if (two_unused)
4242 /* one is used and two is not: not equal */
4243 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004244 /* compare the state id */
4245 if (one->state->id != two->state->id)
4246 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004247 /* compare the position */
4248 if (REG_MULTI)
4249 return one->end.pos.lnum == two->end.pos.lnum
4250 && one->end.pos.col == two->end.pos.col;
4251 return one->end.ptr == two->end.ptr;
4252}
4253
4254/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004255 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4256 */
4257 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004258match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004259{
4260 nfa_state_T *state = startstate;
4261
4262 /* avoid too much recursion */
4263 if (depth > 10)
4264 return FALSE;
4265
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004266 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004267 {
4268 switch (state->c)
4269 {
4270 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004271 case NFA_MCLOSE:
4272 case NFA_END_INVISIBLE:
4273 case NFA_END_INVISIBLE_NEG:
4274 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275 return TRUE;
4276
4277 case NFA_SPLIT:
4278 return match_follows(state->out, depth + 1)
4279 || match_follows(state->out1, depth + 1);
4280
4281 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004282 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004283 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004284 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004285 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004286 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004287 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004288 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004289 case NFA_COMPOSING:
4290 /* skip ahead to next state */
4291 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004292 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004293
4294 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004295 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004296 case NFA_IDENT:
4297 case NFA_SIDENT:
4298 case NFA_KWORD:
4299 case NFA_SKWORD:
4300 case NFA_FNAME:
4301 case NFA_SFNAME:
4302 case NFA_PRINT:
4303 case NFA_SPRINT:
4304 case NFA_WHITE:
4305 case NFA_NWHITE:
4306 case NFA_DIGIT:
4307 case NFA_NDIGIT:
4308 case NFA_HEX:
4309 case NFA_NHEX:
4310 case NFA_OCTAL:
4311 case NFA_NOCTAL:
4312 case NFA_WORD:
4313 case NFA_NWORD:
4314 case NFA_HEAD:
4315 case NFA_NHEAD:
4316 case NFA_ALPHA:
4317 case NFA_NALPHA:
4318 case NFA_LOWER:
4319 case NFA_NLOWER:
4320 case NFA_UPPER:
4321 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004322 case NFA_LOWER_IC:
4323 case NFA_NLOWER_IC:
4324 case NFA_UPPER_IC:
4325 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004326 case NFA_START_COLL:
4327 case NFA_START_NEG_COLL:
4328 case NFA_NEWL:
4329 /* state will advance input */
4330 return FALSE;
4331
4332 default:
4333 if (state->c > 0)
4334 /* state will advance input */
4335 return FALSE;
4336
4337 /* Others: zero-width or possibly zero-width, might still find
4338 * a match at the same position, keep looking. */
4339 break;
4340 }
4341 state = state->out;
4342 }
4343 return FALSE;
4344}
4345
4346
4347/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004348 * Return TRUE if "state" is already in list "l".
4349 */
4350 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004351state_in_list(
4352 nfa_list_T *l, /* runtime state list */
4353 nfa_state_T *state, /* state to update */
4354 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004355{
4356 if (state->lastlist[nfa_ll_index] == l->id)
4357 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004358 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004359 return TRUE;
4360 }
4361 return FALSE;
4362}
4363
Bram Moolenaar16b35782016-09-09 20:29:50 +02004364/* Offset used for "off" by addstate_here(). */
4365#define ADDSTATE_HERE_OFFSET 10
4366
Bram Moolenaard05bf562013-06-30 23:24:08 +02004367/*
4368 * Add "state" and possibly what follows to state list ".".
4369 * Returns "subs_arg", possibly copied into temp_subs.
4370 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004371 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004372addstate(
4373 nfa_list_T *l, /* runtime state list */
4374 nfa_state_T *state, /* state to update */
4375 regsubs_T *subs_arg, /* pointers to subexpressions */
4376 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004377 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004378{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004379 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004380 int off = off_arg;
4381 int add_here = FALSE;
4382 int listindex = 0;
4383 int k;
4384 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004385 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004386 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004387 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004388 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004389 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004390 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004391 regsubs_T *subs = subs_arg;
4392 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004393#ifdef ENABLE_LOG
4394 int did_print = FALSE;
4395#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396
Bram Moolenaar16b35782016-09-09 20:29:50 +02004397 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4398 {
4399 add_here = TRUE;
4400 off = 0;
4401 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4402 }
4403
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004404 switch (state->c)
4405 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004406 case NFA_NCLOSE:
4407 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004408 case NFA_MCLOSE1:
4409 case NFA_MCLOSE2:
4410 case NFA_MCLOSE3:
4411 case NFA_MCLOSE4:
4412 case NFA_MCLOSE5:
4413 case NFA_MCLOSE6:
4414 case NFA_MCLOSE7:
4415 case NFA_MCLOSE8:
4416 case NFA_MCLOSE9:
4417#ifdef FEAT_SYN_HL
4418 case NFA_ZCLOSE:
4419 case NFA_ZCLOSE1:
4420 case NFA_ZCLOSE2:
4421 case NFA_ZCLOSE3:
4422 case NFA_ZCLOSE4:
4423 case NFA_ZCLOSE5:
4424 case NFA_ZCLOSE6:
4425 case NFA_ZCLOSE7:
4426 case NFA_ZCLOSE8:
4427 case NFA_ZCLOSE9:
4428#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004429 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004430 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004431 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004432 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004433 /* These nodes are not added themselves but their "out" and/or
4434 * "out1" may be added below. */
4435 break;
4436
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004437 case NFA_BOL:
4438 case NFA_BOF:
4439 /* "^" won't match past end-of-line, don't bother trying.
4440 * Except when at the end of the line, or when we are going to the
4441 * next line for a look-behind match. */
4442 if (reginput > regline
4443 && *reginput != NUL
4444 && (nfa_endp == NULL
4445 || !REG_MULTI
4446 || reglnum == nfa_endp->se_u.pos.lnum))
4447 goto skip_add;
4448 /* FALLTHROUGH */
4449
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004450 case NFA_MOPEN1:
4451 case NFA_MOPEN2:
4452 case NFA_MOPEN3:
4453 case NFA_MOPEN4:
4454 case NFA_MOPEN5:
4455 case NFA_MOPEN6:
4456 case NFA_MOPEN7:
4457 case NFA_MOPEN8:
4458 case NFA_MOPEN9:
4459#ifdef FEAT_SYN_HL
4460 case NFA_ZOPEN:
4461 case NFA_ZOPEN1:
4462 case NFA_ZOPEN2:
4463 case NFA_ZOPEN3:
4464 case NFA_ZOPEN4:
4465 case NFA_ZOPEN5:
4466 case NFA_ZOPEN6:
4467 case NFA_ZOPEN7:
4468 case NFA_ZOPEN8:
4469 case NFA_ZOPEN9:
4470#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004471 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004472 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004473 /* These nodes need to be added so that we can bail out when it
4474 * was added to this list before at the same position to avoid an
4475 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004476
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004477 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004478 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004479 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004480 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004481 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004482 * when there is a PIM. For NFA_MATCH check the position,
4483 * lower position is preferred. */
4484 if (!nfa_has_backref && pim == NULL && !l->has_pim
4485 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004486 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004487 /* When called from addstate_here() do insert before
4488 * existing states. */
4489 if (add_here)
4490 {
4491 for (k = 0; k < l->n && k < listindex; ++k)
4492 if (l->t[k].state->id == state->id)
4493 {
4494 found = TRUE;
4495 break;
4496 }
4497 }
4498 if (!add_here || found)
4499 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004500skip_add:
4501#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004502 nfa_set_code(state->c);
4503 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4504 abs(state->id), l->id, state->c, code,
4505 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004506#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004507 return subs;
4508 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004509 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004510
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004511 /* Do not add the state again when it exists with the same
4512 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004513 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004514 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004515 }
4516
Bram Moolenaara0169122013-06-26 18:16:58 +02004517 /* When there are backreferences or PIMs the number of states may
4518 * be (a lot) bigger than anticipated. */
4519 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004520 {
4521 int newlen = l->len * 3 / 2 + 50;
4522
Bram Moolenaard05bf562013-06-30 23:24:08 +02004523 if (subs != &temp_subs)
4524 {
4525 /* "subs" may point into the current array, need to make a
4526 * copy before it becomes invalid. */
4527 copy_sub(&temp_subs.norm, &subs->norm);
4528#ifdef FEAT_SYN_HL
4529 if (nfa_has_zsubexpr)
4530 copy_sub(&temp_subs.synt, &subs->synt);
4531#endif
4532 subs = &temp_subs;
4533 }
4534
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004535 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004536 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4537 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004538 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004539
4540 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004541 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004542 thread = &l->t[l->n++];
4543 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004544 if (pim == NULL)
4545 thread->pim.result = NFA_PIM_UNUSED;
4546 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004547 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004548 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004549 l->has_pim = TRUE;
4550 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004551 copy_sub(&thread->subs.norm, &subs->norm);
4552#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004553 if (nfa_has_zsubexpr)
4554 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004555#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004556#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004557 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004558 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004559#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 }
4561
4562#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004563 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004564 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004565#endif
4566 switch (state->c)
4567 {
4568 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004569 break;
4570
4571 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004572 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004573 subs = addstate(l, state->out, subs, pim, off_arg);
4574 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004575 break;
4576
Bram Moolenaar699c1202013-09-25 16:41:54 +02004577 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578 case NFA_NOPEN:
4579 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004580 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004581 break;
4582
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004583 case NFA_MOPEN:
4584 case NFA_MOPEN1:
4585 case NFA_MOPEN2:
4586 case NFA_MOPEN3:
4587 case NFA_MOPEN4:
4588 case NFA_MOPEN5:
4589 case NFA_MOPEN6:
4590 case NFA_MOPEN7:
4591 case NFA_MOPEN8:
4592 case NFA_MOPEN9:
4593#ifdef FEAT_SYN_HL
4594 case NFA_ZOPEN:
4595 case NFA_ZOPEN1:
4596 case NFA_ZOPEN2:
4597 case NFA_ZOPEN3:
4598 case NFA_ZOPEN4:
4599 case NFA_ZOPEN5:
4600 case NFA_ZOPEN6:
4601 case NFA_ZOPEN7:
4602 case NFA_ZOPEN8:
4603 case NFA_ZOPEN9:
4604#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004605 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004606 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004607 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004608 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004609 sub = &subs->norm;
4610 }
4611#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004612 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004613 {
4614 subidx = state->c - NFA_ZOPEN;
4615 sub = &subs->synt;
4616 }
4617#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004618 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004619 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004620 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004621 sub = &subs->norm;
4622 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004623
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004624 /* avoid compiler warnings */
4625 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004626 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004627
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004628 /* Set the position (with "off" added) in the subexpression. Save
4629 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004630 if (REG_MULTI)
4631 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004632 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004633 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004634 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004635 save_in_use = -1;
4636 }
4637 else
4638 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004639 save_in_use = sub->in_use;
4640 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004641 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004642 sub->list.multi[i].start_lnum = -1;
4643 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004644 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004645 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004646 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004647 if (off == -1)
4648 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004649 sub->list.multi[subidx].start_lnum = reglnum + 1;
4650 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004651 }
4652 else
4653 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004654 sub->list.multi[subidx].start_lnum = reglnum;
4655 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004656 (colnr_T)(reginput - regline + off);
4657 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004658 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004659 }
4660 else
4661 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004662 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004663 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004664 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004665 save_in_use = -1;
4666 }
4667 else
4668 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004669 save_in_use = sub->in_use;
4670 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004671 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004672 sub->list.line[i].start = NULL;
4673 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004674 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004675 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004676 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004677 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004678 }
4679
Bram Moolenaar16b35782016-09-09 20:29:50 +02004680 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004681 /* "subs" may have changed, need to set "sub" again */
4682#ifdef FEAT_SYN_HL
4683 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4684 sub = &subs->synt;
4685 else
4686#endif
4687 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004689 if (save_in_use == -1)
4690 {
4691 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004692 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004693 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004694 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004695 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004696 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004697 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 break;
4699
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004700 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004701 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004702 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004703 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004704 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004705 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004706 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004707 break;
4708 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004709 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004710 case NFA_MCLOSE1:
4711 case NFA_MCLOSE2:
4712 case NFA_MCLOSE3:
4713 case NFA_MCLOSE4:
4714 case NFA_MCLOSE5:
4715 case NFA_MCLOSE6:
4716 case NFA_MCLOSE7:
4717 case NFA_MCLOSE8:
4718 case NFA_MCLOSE9:
4719#ifdef FEAT_SYN_HL
4720 case NFA_ZCLOSE:
4721 case NFA_ZCLOSE1:
4722 case NFA_ZCLOSE2:
4723 case NFA_ZCLOSE3:
4724 case NFA_ZCLOSE4:
4725 case NFA_ZCLOSE5:
4726 case NFA_ZCLOSE6:
4727 case NFA_ZCLOSE7:
4728 case NFA_ZCLOSE8:
4729 case NFA_ZCLOSE9:
4730#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004731 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004732 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004733 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004734 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004735 sub = &subs->norm;
4736 }
4737#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004738 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004739 {
4740 subidx = state->c - NFA_ZCLOSE;
4741 sub = &subs->synt;
4742 }
4743#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004744 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004745 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004746 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004747 sub = &subs->norm;
4748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004749
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004750 /* We don't fill in gaps here, there must have been an MOPEN that
4751 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004752 save_in_use = sub->in_use;
4753 if (sub->in_use <= subidx)
4754 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004755 if (REG_MULTI)
4756 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004757 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004758 if (off == -1)
4759 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004760 sub->list.multi[subidx].end_lnum = reglnum + 1;
4761 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004762 }
4763 else
4764 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004765 sub->list.multi[subidx].end_lnum = reglnum;
4766 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004767 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004768 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004769 /* avoid compiler warnings */
4770 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004771 }
4772 else
4773 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004774 save_ptr = sub->list.line[subidx].end;
4775 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004776 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004777 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 }
4779
Bram Moolenaar16b35782016-09-09 20:29:50 +02004780 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004781 /* "subs" may have changed, need to set "sub" again */
4782#ifdef FEAT_SYN_HL
4783 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4784 sub = &subs->synt;
4785 else
4786#endif
4787 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004788
4789 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004790 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004791 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004792 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004793 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004794 break;
4795 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004796 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004797}
4798
4799/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004800 * Like addstate(), but the new state(s) are put at position "*ip".
4801 * Used for zero-width matches, next state to use is the added one.
4802 * This makes sure the order of states to be tried does not change, which
4803 * matters for alternatives.
4804 */
4805 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004806addstate_here(
4807 nfa_list_T *l, /* runtime state list */
4808 nfa_state_T *state, /* state to update */
4809 regsubs_T *subs, /* pointers to subexpressions */
4810 nfa_pim_T *pim, /* postponed look-behind match */
4811 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004812{
4813 int tlen = l->n;
4814 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004815 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004816
Bram Moolenaar16b35782016-09-09 20:29:50 +02004817 /* First add the state(s) at the end, so that we know how many there are.
4818 * Pass the listidx as offset (avoids adding another argument to
4819 * addstate(). */
4820 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004821
Bram Moolenaar4b417062013-05-25 20:19:50 +02004822 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004823 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004824 return;
4825
4826 /* re-order to put the new state at the current position */
4827 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004828 if (count == 0)
4829 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004830 if (count == 1)
4831 {
4832 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004833 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004834 }
4835 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004836 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004837 if (l->n + count - 1 >= l->len)
4838 {
4839 /* not enough space to move the new states, reallocate the list
4840 * and move the states to the right position */
4841 nfa_thread_T *newl;
4842
4843 l->len = l->len * 3 / 2 + 50;
4844 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4845 if (newl == NULL)
4846 return;
4847 mch_memmove(&(newl[0]),
4848 &(l->t[0]),
4849 sizeof(nfa_thread_T) * listidx);
4850 mch_memmove(&(newl[listidx]),
4851 &(l->t[l->n - count]),
4852 sizeof(nfa_thread_T) * count);
4853 mch_memmove(&(newl[listidx + count]),
4854 &(l->t[listidx + 1]),
4855 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4856 vim_free(l->t);
4857 l->t = newl;
4858 }
4859 else
4860 {
4861 /* make space for new states, then move them from the
4862 * end to the current position */
4863 mch_memmove(&(l->t[listidx + count]),
4864 &(l->t[listidx + 1]),
4865 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4866 mch_memmove(&(l->t[listidx]),
4867 &(l->t[l->n - 1]),
4868 sizeof(nfa_thread_T) * count);
4869 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004870 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004871 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004872 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004873}
4874
4875/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876 * Check character class "class" against current character c.
4877 */
4878 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004879check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004880{
4881 switch (class)
4882 {
4883 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004884 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004885 return OK;
4886 break;
4887 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004888 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004889 return OK;
4890 break;
4891 case NFA_CLASS_BLANK:
4892 if (c == ' ' || c == '\t')
4893 return OK;
4894 break;
4895 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004896 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004897 return OK;
4898 break;
4899 case NFA_CLASS_DIGIT:
4900 if (VIM_ISDIGIT(c))
4901 return OK;
4902 break;
4903 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004904 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004905 return OK;
4906 break;
4907 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004908 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004909 return OK;
4910 break;
4911 case NFA_CLASS_PRINT:
4912 if (vim_isprintc(c))
4913 return OK;
4914 break;
4915 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004916 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004917 return OK;
4918 break;
4919 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004920 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004921 return OK;
4922 break;
4923 case NFA_CLASS_UPPER:
4924 if (MB_ISUPPER(c))
4925 return OK;
4926 break;
4927 case NFA_CLASS_XDIGIT:
4928 if (vim_isxdigit(c))
4929 return OK;
4930 break;
4931 case NFA_CLASS_TAB:
4932 if (c == '\t')
4933 return OK;
4934 break;
4935 case NFA_CLASS_RETURN:
4936 if (c == '\r')
4937 return OK;
4938 break;
4939 case NFA_CLASS_BACKSPACE:
4940 if (c == '\b')
4941 return OK;
4942 break;
4943 case NFA_CLASS_ESCAPE:
4944 if (c == '\033')
4945 return OK;
4946 break;
4947
4948 default:
4949 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004950 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004951 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004952 }
4953 return FAIL;
4954}
4955
Bram Moolenaar5714b802013-05-28 22:03:20 +02004956/*
4957 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004958 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004959 */
4960 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004961match_backref(
4962 regsub_T *sub, /* pointers to subexpressions */
4963 int subidx,
4964 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004965{
4966 int len;
4967
4968 if (sub->in_use <= subidx)
4969 {
4970retempty:
4971 /* backref was not set, match an empty string */
4972 *bytelen = 0;
4973 return TRUE;
4974 }
4975
4976 if (REG_MULTI)
4977 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004978 if (sub->list.multi[subidx].start_lnum < 0
4979 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004980 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004981 if (sub->list.multi[subidx].start_lnum == reglnum
4982 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004983 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004984 len = sub->list.multi[subidx].end_col
4985 - sub->list.multi[subidx].start_col;
4986 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004987 reginput, &len) == 0)
4988 {
4989 *bytelen = len;
4990 return TRUE;
4991 }
4992 }
4993 else
4994 {
4995 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004996 sub->list.multi[subidx].start_lnum,
4997 sub->list.multi[subidx].start_col,
4998 sub->list.multi[subidx].end_lnum,
4999 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02005000 bytelen) == RA_MATCH)
5001 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005002 }
5003 }
5004 else
5005 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005006 if (sub->list.line[subidx].start == NULL
5007 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005008 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005009 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
5010 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005011 {
5012 *bytelen = len;
5013 return TRUE;
5014 }
5015 }
5016 return FALSE;
5017}
5018
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005019#ifdef FEAT_SYN_HL
5020
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005021static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005022
5023/*
5024 * Check for a match with \z subexpression "subidx".
5025 * Return TRUE if it matches.
5026 */
5027 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005028match_zref(
5029 int subidx,
5030 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005031{
5032 int len;
5033
5034 cleanup_zsubexpr();
5035 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5036 {
5037 /* backref was not set, match an empty string */
5038 *bytelen = 0;
5039 return TRUE;
5040 }
5041
5042 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5043 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5044 {
5045 *bytelen = len;
5046 return TRUE;
5047 }
5048 return FALSE;
5049}
5050#endif
5051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005052/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005053 * Save list IDs for all NFA states of "prog" into "list".
5054 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005055 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005056 */
5057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005058nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005059{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005060 int i;
5061 nfa_state_T *p;
5062
5063 /* Order in the list is reverse, it's a bit faster that way. */
5064 p = &prog->state[0];
5065 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005066 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005067 list[i] = p->lastlist[1];
5068 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005069 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005070 }
5071}
5072
5073/*
5074 * Restore list IDs from "list" to all NFA states.
5075 */
5076 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005077nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005078{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005079 int i;
5080 nfa_state_T *p;
5081
5082 p = &prog->state[0];
5083 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005084 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005085 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005086 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005087 }
5088}
5089
Bram Moolenaar423532e2013-05-29 21:14:42 +02005090 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005091nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005092{
5093 if (op == 1) return pos > val;
5094 if (op == 2) return pos < val;
5095 return val == pos;
5096}
5097
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005098static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5099static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005100
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005101/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005102 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005103 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5104 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005105 */
5106 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005107recursive_regmatch(
5108 nfa_state_T *state,
5109 nfa_pim_T *pim,
5110 nfa_regprog_T *prog,
5111 regsubs_T *submatch,
5112 regsubs_T *m,
5113 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005114{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005115 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005116 int save_reglnum = reglnum;
5117 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005118 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005119 save_se_T *save_nfa_endp = nfa_endp;
5120 save_se_T endpos;
5121 save_se_T *endposp = NULL;
5122 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005123 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005124
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005125 if (pim != NULL)
5126 {
5127 /* start at the position where the postponed match was */
5128 if (REG_MULTI)
5129 reginput = regline + pim->end.pos.col;
5130 else
5131 reginput = pim->end.ptr;
5132 }
5133
Bram Moolenaardecd9542013-06-07 16:31:50 +02005134 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005135 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5136 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5137 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005138 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005139 /* The recursive match must end at the current position. When "pim" is
5140 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005141 endposp = &endpos;
5142 if (REG_MULTI)
5143 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005144 if (pim == NULL)
5145 {
5146 endpos.se_u.pos.col = (int)(reginput - regline);
5147 endpos.se_u.pos.lnum = reglnum;
5148 }
5149 else
5150 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151 }
5152 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005153 {
5154 if (pim == NULL)
5155 endpos.se_u.ptr = reginput;
5156 else
5157 endpos.se_u.ptr = pim->end.ptr;
5158 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005159
5160 /* Go back the specified number of bytes, or as far as the
5161 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005162 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005163 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005164 if (state->val <= 0)
5165 {
5166 if (REG_MULTI)
5167 {
5168 regline = reg_getline(--reglnum);
5169 if (regline == NULL)
5170 /* can't go before the first line */
5171 regline = reg_getline(++reglnum);
5172 }
5173 reginput = regline;
5174 }
5175 else
5176 {
5177 if (REG_MULTI && (int)(reginput - regline) < state->val)
5178 {
5179 /* Not enough bytes in this line, go to end of
5180 * previous line. */
5181 regline = reg_getline(--reglnum);
5182 if (regline == NULL)
5183 {
5184 /* can't go before the first line */
5185 regline = reg_getline(++reglnum);
5186 reginput = regline;
5187 }
5188 else
5189 reginput = regline + STRLEN(regline);
5190 }
5191 if ((int)(reginput - regline) >= state->val)
5192 {
5193 reginput -= state->val;
5194#ifdef FEAT_MBYTE
5195 if (has_mbyte)
5196 reginput -= mb_head_off(regline, reginput);
5197#endif
5198 }
5199 else
5200 reginput = regline;
5201 }
5202 }
5203
Bram Moolenaarf46da702013-06-02 22:37:42 +02005204#ifdef ENABLE_LOG
5205 if (log_fd != stderr)
5206 fclose(log_fd);
5207 log_fd = NULL;
5208#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005209 /* Have to clear the lastlist field of the NFA nodes, so that
5210 * nfa_regmatch() and addstate() can run properly after recursion. */
5211 if (nfa_ll_index == 1)
5212 {
5213 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5214 * values and clear them. */
5215 if (*listids == NULL)
5216 {
5217 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5218 if (*listids == NULL)
5219 {
5220 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5221 return 0;
5222 }
5223 }
5224 nfa_save_listids(prog, *listids);
5225 need_restore = TRUE;
5226 /* any value of nfa_listid will do */
5227 }
5228 else
5229 {
5230 /* First recursive nfa_regmatch() call, switch to the second lastlist
5231 * entry. Make sure nfa_listid is different from a previous recursive
5232 * call, because some states may still have this ID. */
5233 ++nfa_ll_index;
5234 if (nfa_listid <= nfa_alt_listid)
5235 nfa_listid = nfa_alt_listid;
5236 }
5237
5238 /* Call nfa_regmatch() to check if the current concat matches at this
5239 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005240 nfa_endp = endposp;
5241 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005242
5243 if (need_restore)
5244 nfa_restore_listids(prog, *listids);
5245 else
5246 {
5247 --nfa_ll_index;
5248 nfa_alt_listid = nfa_listid;
5249 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005250
5251 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005252 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005253 if (REG_MULTI)
5254 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005255 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005256 if (result != NFA_TOO_EXPENSIVE)
5257 {
5258 nfa_match = save_nfa_match;
5259 nfa_listid = save_nfa_listid;
5260 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005261 nfa_endp = save_nfa_endp;
5262
5263#ifdef ENABLE_LOG
5264 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5265 if (log_fd != NULL)
5266 {
5267 fprintf(log_fd, "****************************\n");
5268 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5269 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5270 fprintf(log_fd, "****************************\n");
5271 }
5272 else
5273 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005274 EMSG(_(e_log_open_failed));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005275 log_fd = stderr;
5276 }
5277#endif
5278
5279 return result;
5280}
5281
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005282static int skip_to_start(int c, colnr_T *colp);
5283static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005284
5285/*
5286 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005287 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005288 * NFA_ANY: 1
5289 * specific character: 99
5290 */
5291 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005292failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005293{
5294 int c = state->c;
5295 int l, r;
5296
5297 /* detect looping */
5298 if (depth > 4)
5299 return 1;
5300
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005301 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005302 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005303 case NFA_SPLIT:
5304 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5305 /* avoid recursive stuff */
5306 return 1;
5307 /* two alternatives, use the lowest failure chance */
5308 l = failure_chance(state->out, depth + 1);
5309 r = failure_chance(state->out1, depth + 1);
5310 return l < r ? l : r;
5311
5312 case NFA_ANY:
5313 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005314 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005315
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005316 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005317 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005318 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005319 /* empty match works always */
5320 return 0;
5321
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005322 case NFA_START_INVISIBLE:
5323 case NFA_START_INVISIBLE_FIRST:
5324 case NFA_START_INVISIBLE_NEG:
5325 case NFA_START_INVISIBLE_NEG_FIRST:
5326 case NFA_START_INVISIBLE_BEFORE:
5327 case NFA_START_INVISIBLE_BEFORE_FIRST:
5328 case NFA_START_INVISIBLE_BEFORE_NEG:
5329 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5330 case NFA_START_PATTERN:
5331 /* recursive regmatch is expensive, use low failure chance */
5332 return 5;
5333
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005334 case NFA_BOL:
5335 case NFA_EOL:
5336 case NFA_BOF:
5337 case NFA_EOF:
5338 case NFA_NEWL:
5339 return 99;
5340
5341 case NFA_BOW:
5342 case NFA_EOW:
5343 return 90;
5344
5345 case NFA_MOPEN:
5346 case NFA_MOPEN1:
5347 case NFA_MOPEN2:
5348 case NFA_MOPEN3:
5349 case NFA_MOPEN4:
5350 case NFA_MOPEN5:
5351 case NFA_MOPEN6:
5352 case NFA_MOPEN7:
5353 case NFA_MOPEN8:
5354 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005355#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005356 case NFA_ZOPEN:
5357 case NFA_ZOPEN1:
5358 case NFA_ZOPEN2:
5359 case NFA_ZOPEN3:
5360 case NFA_ZOPEN4:
5361 case NFA_ZOPEN5:
5362 case NFA_ZOPEN6:
5363 case NFA_ZOPEN7:
5364 case NFA_ZOPEN8:
5365 case NFA_ZOPEN9:
5366 case NFA_ZCLOSE:
5367 case NFA_ZCLOSE1:
5368 case NFA_ZCLOSE2:
5369 case NFA_ZCLOSE3:
5370 case NFA_ZCLOSE4:
5371 case NFA_ZCLOSE5:
5372 case NFA_ZCLOSE6:
5373 case NFA_ZCLOSE7:
5374 case NFA_ZCLOSE8:
5375 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005376#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005377 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005378 case NFA_MCLOSE1:
5379 case NFA_MCLOSE2:
5380 case NFA_MCLOSE3:
5381 case NFA_MCLOSE4:
5382 case NFA_MCLOSE5:
5383 case NFA_MCLOSE6:
5384 case NFA_MCLOSE7:
5385 case NFA_MCLOSE8:
5386 case NFA_MCLOSE9:
5387 case NFA_NCLOSE:
5388 return failure_chance(state->out, depth + 1);
5389
5390 case NFA_BACKREF1:
5391 case NFA_BACKREF2:
5392 case NFA_BACKREF3:
5393 case NFA_BACKREF4:
5394 case NFA_BACKREF5:
5395 case NFA_BACKREF6:
5396 case NFA_BACKREF7:
5397 case NFA_BACKREF8:
5398 case NFA_BACKREF9:
5399#ifdef FEAT_SYN_HL
5400 case NFA_ZREF1:
5401 case NFA_ZREF2:
5402 case NFA_ZREF3:
5403 case NFA_ZREF4:
5404 case NFA_ZREF5:
5405 case NFA_ZREF6:
5406 case NFA_ZREF7:
5407 case NFA_ZREF8:
5408 case NFA_ZREF9:
5409#endif
5410 /* backreferences don't match in many places */
5411 return 94;
5412
5413 case NFA_LNUM_GT:
5414 case NFA_LNUM_LT:
5415 case NFA_COL_GT:
5416 case NFA_COL_LT:
5417 case NFA_VCOL_GT:
5418 case NFA_VCOL_LT:
5419 case NFA_MARK_GT:
5420 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005421 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005422 /* before/after positions don't match very often */
5423 return 85;
5424
5425 case NFA_LNUM:
5426 return 90;
5427
5428 case NFA_CURSOR:
5429 case NFA_COL:
5430 case NFA_VCOL:
5431 case NFA_MARK:
5432 /* specific positions rarely match */
5433 return 98;
5434
5435 case NFA_COMPOSING:
5436 return 95;
5437
5438 default:
5439 if (c > 0)
5440 /* character match fails often */
5441 return 95;
5442 }
5443
5444 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005445 return 50;
5446}
5447
Bram Moolenaarf46da702013-06-02 22:37:42 +02005448/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005449 * Skip until the char "c" we know a match must start with.
5450 */
5451 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005452skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005453{
5454 char_u *s;
5455
5456 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005457 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005458#ifdef FEAT_MBYTE
5459 && !has_mbyte
5460#endif
5461 )
5462 s = vim_strbyte(regline + *colp, c);
5463 else
5464 s = cstrchr(regline + *colp, c);
5465 if (s == NULL)
5466 return FAIL;
5467 *colp = (int)(s - regline);
5468 return OK;
5469}
5470
5471/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005472 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005473 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005474 * Returns zero for no match, 1 for a match.
5475 */
5476 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005477find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005478{
5479 colnr_T col = startcol;
5480 int c1, c2;
5481 int len1, len2;
5482 int match;
5483
5484 for (;;)
5485 {
5486 match = TRUE;
5487 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5488 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5489 {
5490 c1 = PTR2CHAR(match_text + len1);
5491 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005492 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005493 {
5494 match = FALSE;
5495 break;
5496 }
5497 len2 += MB_CHAR2LEN(c2);
5498 }
5499 if (match
5500#ifdef FEAT_MBYTE
5501 /* check that no composing char follows */
5502 && !(enc_utf8
5503 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5504#endif
5505 )
5506 {
5507 cleanup_subexpr();
5508 if (REG_MULTI)
5509 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005510 rex.reg_startpos[0].lnum = reglnum;
5511 rex.reg_startpos[0].col = col;
5512 rex.reg_endpos[0].lnum = reglnum;
5513 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005514 }
5515 else
5516 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005517 rex.reg_startp[0] = regline + col;
5518 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005519 }
5520 return 1L;
5521 }
5522
5523 /* Try finding regstart after the current match. */
5524 col += MB_CHAR2LEN(regstart); /* skip regstart */
5525 if (skip_to_start(regstart, &col) == FAIL)
5526 break;
5527 }
5528 return 0L;
5529}
5530
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005531#ifdef FEAT_RELTIME
5532 static int
5533nfa_did_time_out()
5534{
5535 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5536 {
5537 if (nfa_timed_out != NULL)
5538 *nfa_timed_out = TRUE;
5539 return TRUE;
5540 }
5541 return FALSE;
5542}
5543#endif
5544
Bram Moolenaar473de612013-06-08 18:19:48 +02005545/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546 * Main matching routine.
5547 *
5548 * Run NFA to determine whether it matches reginput.
5549 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005550 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005551 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005553 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005554 * Note: Caller must ensure that: start != NULL.
5555 */
5556 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005557nfa_regmatch(
5558 nfa_regprog_T *prog,
5559 nfa_state_T *start,
5560 regsubs_T *submatch,
5561 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005564 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005566 int go_to_nextline = FALSE;
5567 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005568 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005569 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005570 nfa_list_T *thislist;
5571 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005573 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005574 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005575 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005576 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005577 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005578#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005579 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005580#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005581
Bram Moolenaar41f12052013-08-25 17:01:42 +02005582 /* Some patterns may take a long time to match, especially when using
5583 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5584 fast_breakcheck();
5585 if (got_int)
5586 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005587#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005588 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005589 return FALSE;
5590#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005591
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005592#ifdef NFA_REGEXP_DEBUG_LOG
5593 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5594 if (debug == NULL)
5595 {
Bram Moolenaar5efa0102018-06-22 21:42:30 +02005596 EMSG2("(NFA) COULD NOT OPEN %s!", NFA_REGEXP_DEBUG_LOG);
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005597 return FALSE;
5598 }
5599#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005600 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005601
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005602 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005603 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005604
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005605 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005606 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005607 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005608 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005609 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005611
5612#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005613 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614 if (log_fd != NULL)
5615 {
5616 fprintf(log_fd, "**********************************\n");
5617 nfa_set_code(start->c);
5618 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5619 abs(start->id), code);
5620 fprintf(log_fd, "**********************************\n");
5621 }
5622 else
5623 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005624 EMSG(_(e_log_open_failed));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005625 log_fd = stderr;
5626 }
5627#endif
5628
5629 thislist = &list[0];
5630 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005631 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005632 nextlist = &list[1];
5633 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005634 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005635#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005636 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005638 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005639
5640 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5641 * it's the first MOPEN. */
5642 if (toplevel)
5643 {
5644 if (REG_MULTI)
5645 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005646 m->norm.list.multi[0].start_lnum = reglnum;
5647 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005648 }
5649 else
5650 m->norm.list.line[0].start = reginput;
5651 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005652 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005653 }
5654 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005655 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005656
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005657#define ADD_STATE_IF_MATCH(state) \
5658 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005659 add_state = state->out; \
5660 add_off = clen; \
5661 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005662
5663 /*
5664 * Run for each character.
5665 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005666 for (;;)
5667 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005668 int curc;
5669 int clen;
5670
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671#ifdef FEAT_MBYTE
5672 if (has_mbyte)
5673 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005674 curc = (*mb_ptr2char)(reginput);
5675 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005676 }
5677 else
5678#endif
5679 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005680 curc = *reginput;
5681 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005682 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005683 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005684 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005685 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005686 go_to_nextline = FALSE;
5687 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005688
5689 /* swap lists */
5690 thislist = &list[flag];
5691 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005692 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005693 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005694 ++nfa_listid;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02005695 if (prog->re_engine == AUTOMATIC_ENGINE
5696 && (nfa_listid >= NFA_MAX_STATES || nfa_fail_for_testing))
Bram Moolenaarfda37292014-11-05 14:27:36 +01005697 {
5698 /* too many states, retry with old engine */
5699 nfa_match = NFA_TOO_EXPENSIVE;
5700 goto theend;
5701 }
5702
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005703 thislist->id = nfa_listid;
5704 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005705
5706#ifdef ENABLE_LOG
5707 fprintf(log_fd, "------------------------------------------\n");
5708 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005709 fprintf(log_fd, ">>> Advanced one character... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005710 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005711 {
5712 int i;
5713
5714 for (i = 0; i < thislist->n; i++)
5715 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5716 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005717 fprintf(log_fd, "\n");
5718#endif
5719
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005720#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005721 fprintf(debug, "\n-------------------\n");
5722#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005723 /*
5724 * If the state lists are empty we can stop.
5725 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005726 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005727 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005728
5729 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005730 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005731 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005732 /* If the list gets very long there probably is something wrong.
5733 * At least allow interrupting with CTRL-C. */
5734 fast_breakcheck();
5735 if (got_int)
5736 break;
5737#ifdef FEAT_RELTIME
5738 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5739 {
5740 nfa_time_count = 0;
5741 if (nfa_did_time_out())
5742 break;
5743 }
5744#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005745 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005746
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005747#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005748 nfa_set_code(t->state->c);
5749 fprintf(debug, "%s, ", code);
5750#endif
5751#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005752 {
5753 int col;
5754
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005755 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005756 col = -1;
5757 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005758 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005759 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005760 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005761 nfa_set_code(t->state->c);
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02005762 fprintf(log_fd, "(%d) char %d %s (start col %d)%s... \n",
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005763 abs(t->state->id), (int)t->state->c, code, col,
5764 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005765 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005766#endif
5767
5768 /*
5769 * Handle the possible codes of the current state.
5770 * The most important is NFA_MATCH.
5771 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005772 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005773 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005774 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005775 switch (t->state->c)
5776 {
5777 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005778 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005779#ifdef FEAT_MBYTE
5780 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005781 * rex.reg_icombine is not set, that is not really a match. */
5782 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005783 break;
5784#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005785 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005786 copy_sub(&submatch->norm, &t->subs.norm);
5787#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005788 if (nfa_has_zsubexpr)
5789 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005790#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005791#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005792 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005793#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005794 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005795 * states at this position. When the list of states is going
5796 * to be empty quit without advancing, so that "reginput" is
5797 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005798 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005799 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005800 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005801 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005802
5803 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005804 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005805 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005806 /*
5807 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005808 * NFA_START_INVISIBLE_BEFORE node.
5809 * They surround a zero-width group, used with "\@=", "\&",
5810 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005811 * If we got here, it means that the current "invisible" group
5812 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005813 * nfa_regmatch(). For a look-behind match only when it ends
5814 * in the position in "nfa_endp".
5815 * Submatches are stored in *m, and used in the parent call.
5816 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005817#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005818 if (nfa_endp != NULL)
5819 {
5820 if (REG_MULTI)
5821 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5822 (int)reglnum,
5823 (int)nfa_endp->se_u.pos.lnum,
5824 (int)(reginput - regline),
5825 nfa_endp->se_u.pos.col);
5826 else
5827 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5828 (int)(reginput - regline),
5829 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005830 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005831#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005832 /* If "nfa_endp" is set it's only a match if it ends at
5833 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005834 if (nfa_endp != NULL && (REG_MULTI
5835 ? (reglnum != nfa_endp->se_u.pos.lnum
5836 || (int)(reginput - regline)
5837 != nfa_endp->se_u.pos.col)
5838 : reginput != nfa_endp->se_u.ptr))
5839 break;
5840
5841 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005842 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005843 {
5844 copy_sub(&m->norm, &t->subs.norm);
5845#ifdef FEAT_SYN_HL
5846 if (nfa_has_zsubexpr)
5847 copy_sub(&m->synt, &t->subs.synt);
5848#endif
5849 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005850#ifdef ENABLE_LOG
5851 fprintf(log_fd, "Match found:\n");
5852 log_subsexpr(m);
5853#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005854 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005855 /* See comment above at "goto nextchar". */
5856 if (nextlist->n == 0)
5857 clen = 0;
5858 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005859
5860 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005861 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005862 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005863 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005864 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005865 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005866 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005867 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005868 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005869#ifdef ENABLE_LOG
5870 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5871 failure_chance(t->state->out, 0),
5872 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005873#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005874 /* Do it directly if there already is a PIM or when
5875 * nfa_postprocess() detected it will work better. */
5876 if (t->pim.result != NFA_PIM_UNUSED
5877 || t->state->c == NFA_START_INVISIBLE_FIRST
5878 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5879 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5880 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005881 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005882 int in_use = m->norm.in_use;
5883
Bram Moolenaar699c1202013-09-25 16:41:54 +02005884 /* Copy submatch info for the recursive call, opposite
5885 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005886 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005887#ifdef FEAT_SYN_HL
5888 if (nfa_has_zsubexpr)
5889 copy_sub_off(&m->synt, &t->subs.synt);
5890#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005891
Bram Moolenaara2d95102013-06-04 14:23:05 +02005892 /*
5893 * First try matching the invisible match, then what
5894 * follows.
5895 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005896 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005897 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005898 if (result == NFA_TOO_EXPENSIVE)
5899 {
5900 nfa_match = result;
5901 goto theend;
5902 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005903
Bram Moolenaardecd9542013-06-07 16:31:50 +02005904 /* for \@! and \@<! it is a match when the result is
5905 * FALSE */
5906 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005907 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5908 || t->state->c
5909 == NFA_START_INVISIBLE_BEFORE_NEG
5910 || t->state->c
5911 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005912 {
5913 /* Copy submatch info from the recursive call */
5914 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005915#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005916 if (nfa_has_zsubexpr)
5917 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005918#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005919 /* If the pattern has \ze and it matched in the
5920 * sub pattern, use it. */
5921 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005922
Bram Moolenaara2d95102013-06-04 14:23:05 +02005923 /* t->state->out1 is the corresponding
5924 * END_INVISIBLE node; Add its out to the current
5925 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005926 add_here = TRUE;
5927 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005928 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005929 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005930 }
5931 else
5932 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005933 nfa_pim_T pim;
5934
Bram Moolenaara2d95102013-06-04 14:23:05 +02005935 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005936 * First try matching what follows. Only if a match
5937 * is found verify the invisible match matches. Add a
5938 * nfa_pim_T to the following states, it contains info
5939 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005940 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005941 pim.state = t->state;
5942 pim.result = NFA_PIM_TODO;
5943 pim.subs.norm.in_use = 0;
5944#ifdef FEAT_SYN_HL
5945 pim.subs.synt.in_use = 0;
5946#endif
5947 if (REG_MULTI)
5948 {
5949 pim.end.pos.col = (int)(reginput - regline);
5950 pim.end.pos.lnum = reglnum;
5951 }
5952 else
5953 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005954
5955 /* t->state->out1 is the corresponding END_INVISIBLE
5956 * node; Add its out to the current list (zero-width
5957 * match). */
5958 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005959 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005960 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005961 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005962 break;
5963
Bram Moolenaar87953742013-06-05 18:52:40 +02005964 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005965 {
5966 nfa_state_T *skip = NULL;
5967#ifdef ENABLE_LOG
5968 int skip_lid = 0;
5969#endif
5970
5971 /* There is no point in trying to match the pattern if the
5972 * output state is not going to be added to the list. */
5973 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5974 {
5975 skip = t->state->out1->out;
5976#ifdef ENABLE_LOG
5977 skip_lid = nextlist->id;
5978#endif
5979 }
5980 else if (state_in_list(nextlist,
5981 t->state->out1->out->out, &t->subs))
5982 {
5983 skip = t->state->out1->out->out;
5984#ifdef ENABLE_LOG
5985 skip_lid = nextlist->id;
5986#endif
5987 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005988 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005989 t->state->out1->out->out, &t->subs))
5990 {
5991 skip = t->state->out1->out->out;
5992#ifdef ENABLE_LOG
5993 skip_lid = thislist->id;
5994#endif
5995 }
5996 if (skip != NULL)
5997 {
5998#ifdef ENABLE_LOG
5999 nfa_set_code(skip->c);
6000 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
6001 abs(skip->id), skip_lid, skip->c, code);
6002#endif
6003 break;
6004 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02006005 /* Copy submatch info to the recursive call, opposite of what
6006 * happens afterwards. */
6007 copy_sub_off(&m->norm, &t->subs.norm);
6008#ifdef FEAT_SYN_HL
6009 if (nfa_has_zsubexpr)
6010 copy_sub_off(&m->synt, &t->subs.synt);
6011#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02006012
Bram Moolenaar87953742013-06-05 18:52:40 +02006013 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006014 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02006015 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006016 if (result == NFA_TOO_EXPENSIVE)
6017 {
6018 nfa_match = result;
6019 goto theend;
6020 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006021 if (result)
6022 {
6023 int bytelen;
6024
6025#ifdef ENABLE_LOG
6026 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6027 log_subsexpr(m);
6028#endif
6029 /* Copy submatch info from the recursive call */
6030 copy_sub_off(&t->subs.norm, &m->norm);
6031#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006032 if (nfa_has_zsubexpr)
6033 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006034#endif
6035 /* Now we need to skip over the matched text and then
6036 * continue with what follows. */
6037 if (REG_MULTI)
6038 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006039 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02006040 - (int)(reginput - regline);
6041 else
6042 bytelen = (int)(m->norm.list.line[0].end - reginput);
6043
6044#ifdef ENABLE_LOG
6045 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6046#endif
6047 if (bytelen == 0)
6048 {
6049 /* empty match, output of corresponding
6050 * NFA_END_PATTERN/NFA_SKIP to be used at current
6051 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006052 add_here = TRUE;
6053 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006054 }
6055 else if (bytelen <= clen)
6056 {
6057 /* match current character, output of corresponding
6058 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006059 add_state = t->state->out1->out->out;
6060 add_off = clen;
6061 }
6062 else
6063 {
6064 /* skip over the matched characters, set character
6065 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006066 add_state = t->state->out1->out;
6067 add_off = bytelen;
6068 add_count = bytelen - clen;
6069 }
6070 }
6071 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006072 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006073
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074 case NFA_BOL:
6075 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006076 {
6077 add_here = TRUE;
6078 add_state = t->state->out;
6079 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006080 break;
6081
6082 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006083 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006084 {
6085 add_here = TRUE;
6086 add_state = t->state->out;
6087 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006088 break;
6089
6090 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006091 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006092
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006093 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006094 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006095#ifdef FEAT_MBYTE
6096 else if (has_mbyte)
6097 {
6098 int this_class;
6099
6100 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006101 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006103 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006104 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006105 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006106 }
6107#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006108 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006109 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006110 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006111 result = FALSE;
6112 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006113 {
6114 add_here = TRUE;
6115 add_state = t->state->out;
6116 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006118
6119 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006120 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006121 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006122 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006123#ifdef FEAT_MBYTE
6124 else if (has_mbyte)
6125 {
6126 int this_class, prev_class;
6127
6128 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006129 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006130 prev_class = reg_prev_class();
6131 if (this_class == prev_class
6132 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006133 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006134 }
6135#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006136 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006137 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006138 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006139 result = FALSE;
6140 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006141 {
6142 add_here = TRUE;
6143 add_state = t->state->out;
6144 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006146
Bram Moolenaar4b780632013-05-31 22:14:52 +02006147 case NFA_BOF:
6148 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006149 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006150 {
6151 add_here = TRUE;
6152 add_state = t->state->out;
6153 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006154 break;
6155
6156 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006157 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006158 {
6159 add_here = TRUE;
6160 add_state = t->state->out;
6161 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006162 break;
6163
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006164#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006165 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006166 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006167 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006168 int len = 0;
6169 nfa_state_T *end;
6170 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006171 int cchars[MAX_MCO];
6172 int ccount = 0;
6173 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006174
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006175 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006176 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006177 if (utf_iscomposing(sta->c))
6178 {
6179 /* Only match composing character(s), ignore base
6180 * character. Used for ".{composing}" and "{composing}"
6181 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006182 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006183 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006184 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006185 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006186 /* If \Z was present, then ignore composing characters.
6187 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006188 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006189 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006190 else
6191 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006192 while (sta->c != NFA_END_COMPOSING)
6193 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006194 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006195
6196 /* Check base character matches first, unless ignored. */
6197 else if (len > 0 || mc == sta->c)
6198 {
6199 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006200 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006201 len += mb_char2len(mc);
6202 sta = sta->out;
6203 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006204
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006205 /* We don't care about the order of composing characters.
6206 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006207 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006208 {
6209 mc = mb_ptr2char(reginput + len);
6210 cchars[ccount++] = mc;
6211 len += mb_char2len(mc);
6212 if (ccount == MAX_MCO)
6213 break;
6214 }
6215
6216 /* Check that each composing char in the pattern matches a
6217 * composing char in the text. We do not check if all
6218 * composing chars are matched. */
6219 result = OK;
6220 while (sta->c != NFA_END_COMPOSING)
6221 {
6222 for (j = 0; j < ccount; ++j)
6223 if (cchars[j] == sta->c)
6224 break;
6225 if (j == ccount)
6226 {
6227 result = FAIL;
6228 break;
6229 }
6230 sta = sta->out;
6231 }
6232 }
6233 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006234 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006235
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006236 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006237 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006238 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006239 }
6240#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006241
6242 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006243 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6244 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006245 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006246 go_to_nextline = TRUE;
6247 /* Pass -1 for the offset, which means taking the position
6248 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006249 add_state = t->state->out;
6250 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006251 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006252 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006253 {
6254 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006255 add_state = t->state->out;
6256 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006258 break;
6259
Bram Moolenaar417bad22013-06-07 14:08:30 +02006260 case NFA_START_COLL:
6261 case NFA_START_NEG_COLL:
6262 {
6263 /* What follows is a list of characters, until NFA_END_COLL.
6264 * One of them must match or none of them must match. */
6265 nfa_state_T *state;
6266 int result_if_matched;
6267 int c1, c2;
6268
6269 /* Never match EOL. If it's part of the collection it is added
6270 * as a separate state with an OR. */
6271 if (curc == NUL)
6272 break;
6273
6274 state = t->state->out;
6275 result_if_matched = (t->state->c == NFA_START_COLL);
6276 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006277 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006278 if (state->c == NFA_END_COLL)
6279 {
6280 result = !result_if_matched;
6281 break;
6282 }
6283 if (state->c == NFA_RANGE_MIN)
6284 {
6285 c1 = state->val;
6286 state = state->out; /* advance to NFA_RANGE_MAX */
6287 c2 = state->val;
6288#ifdef ENABLE_LOG
6289 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6290 curc, c1, c2);
6291#endif
6292 if (curc >= c1 && curc <= c2)
6293 {
6294 result = result_if_matched;
6295 break;
6296 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006297 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006298 {
6299 int curc_low = MB_TOLOWER(curc);
6300 int done = FALSE;
6301
6302 for ( ; c1 <= c2; ++c1)
6303 if (MB_TOLOWER(c1) == curc_low)
6304 {
6305 result = result_if_matched;
6306 done = TRUE;
6307 break;
6308 }
6309 if (done)
6310 break;
6311 }
6312 }
6313 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006314 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006315 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006316 == MB_TOLOWER(state->c))))
6317 {
6318 result = result_if_matched;
6319 break;
6320 }
6321 state = state->out;
6322 }
6323 if (result)
6324 {
6325 /* next state is in out of the NFA_END_COLL, out1 of
6326 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006327 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006328 add_off = clen;
6329 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006330 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006331 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006332
6333 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006334 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006335 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006336 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006337 add_state = t->state->out;
6338 add_off = clen;
6339 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006340 break;
6341
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006342 case NFA_ANY_COMPOSING:
6343 /* On a composing character skip over it. Otherwise do
6344 * nothing. Always matches. */
6345#ifdef FEAT_MBYTE
6346 if (enc_utf8 && utf_iscomposing(curc))
6347 {
6348 add_off = clen;
6349 }
6350 else
6351#endif
6352 {
6353 add_here = TRUE;
6354 add_off = 0;
6355 }
6356 add_state = t->state->out;
6357 break;
6358
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006359 /*
6360 * Character classes like \a for alpha, \d for digit etc.
6361 */
6362 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006363 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006364 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006365 break;
6366
6367 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006368 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006369 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006370 break;
6371
6372 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006373 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006374 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006375 break;
6376
6377 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006378 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006379 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006380 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 break;
6382
6383 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006384 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006385 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 break;
6387
6388 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006389 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006390 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391 break;
6392
6393 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006394 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006395 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396 break;
6397
6398 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006399 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006400 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 break;
6402
6403 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006404 result = VIM_ISWHITE(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
6408 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006409 result = curc != NUL && !VIM_ISWHITE(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
6413 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = ri_digit(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
6418 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = curc != NUL && !ri_digit(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
6423 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = ri_hex(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
6428 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = curc != NUL && !ri_hex(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
6433 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = ri_octal(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
6438 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006439 result = curc != NUL && !ri_octal(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
6443 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006444 result = ri_word(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
6448 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006449 result = curc != NUL && !ri_word(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
6453 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006454 result = ri_head(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
6458 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006459 result = curc != NUL && !ri_head(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
6463 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006464 result = ri_alpha(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
6468 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006469 result = curc != NUL && !ri_alpha(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
6473 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006474 result = ri_lower(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
6478 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006479 result = curc != NUL && !ri_lower(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
6483 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006484 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006485 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006486 break;
6487
6488 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006489 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006490 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006491 break;
6492
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006493 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006494 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006495 ADD_STATE_IF_MATCH(t->state);
6496 break;
6497
6498 case NFA_NLOWER_IC: /* [^a-z] */
6499 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006500 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006501 ADD_STATE_IF_MATCH(t->state);
6502 break;
6503
6504 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006505 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006506 ADD_STATE_IF_MATCH(t->state);
6507 break;
6508
6509 case NFA_NUPPER_IC: /* ^[A-Z] */
6510 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006511 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006512 ADD_STATE_IF_MATCH(t->state);
6513 break;
6514
Bram Moolenaar5714b802013-05-28 22:03:20 +02006515 case NFA_BACKREF1:
6516 case NFA_BACKREF2:
6517 case NFA_BACKREF3:
6518 case NFA_BACKREF4:
6519 case NFA_BACKREF5:
6520 case NFA_BACKREF6:
6521 case NFA_BACKREF7:
6522 case NFA_BACKREF8:
6523 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006524#ifdef FEAT_SYN_HL
6525 case NFA_ZREF1:
6526 case NFA_ZREF2:
6527 case NFA_ZREF3:
6528 case NFA_ZREF4:
6529 case NFA_ZREF5:
6530 case NFA_ZREF6:
6531 case NFA_ZREF7:
6532 case NFA_ZREF8:
6533 case NFA_ZREF9:
6534#endif
6535 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006536 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006537 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006538 int bytelen;
6539
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006540 if (t->state->c <= NFA_BACKREF9)
6541 {
6542 subidx = t->state->c - NFA_BACKREF1 + 1;
6543 result = match_backref(&t->subs.norm, subidx, &bytelen);
6544 }
6545#ifdef FEAT_SYN_HL
6546 else
6547 {
6548 subidx = t->state->c - NFA_ZREF1 + 1;
6549 result = match_zref(subidx, &bytelen);
6550 }
6551#endif
6552
Bram Moolenaar5714b802013-05-28 22:03:20 +02006553 if (result)
6554 {
6555 if (bytelen == 0)
6556 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006557 /* empty match always works, output of NFA_SKIP to be
6558 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006559 add_here = TRUE;
6560 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006561 }
6562 else if (bytelen <= clen)
6563 {
6564 /* match current character, jump ahead to out of
6565 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006566 add_state = t->state->out->out;
6567 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006568 }
6569 else
6570 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006571 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006572 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006573 add_state = t->state->out;
6574 add_off = bytelen;
6575 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006576 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006577 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006578 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006579 }
6580 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006581 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006582 if (t->count - clen <= 0)
6583 {
6584 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006585 add_state = t->state->out;
6586 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006587 }
6588 else
6589 {
6590 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006591 add_state = t->state;
6592 add_off = 0;
6593 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006594 }
6595 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006596
Bram Moolenaar423532e2013-05-29 21:14:42 +02006597 case NFA_LNUM:
6598 case NFA_LNUM_GT:
6599 case NFA_LNUM_LT:
6600 result = (REG_MULTI &&
6601 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006602 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006603 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006604 {
6605 add_here = TRUE;
6606 add_state = t->state->out;
6607 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006608 break;
6609
6610 case NFA_COL:
6611 case NFA_COL_GT:
6612 case NFA_COL_LT:
6613 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6614 (long_u)(reginput - regline) + 1);
6615 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006616 {
6617 add_here = TRUE;
6618 add_state = t->state->out;
6619 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006620 break;
6621
6622 case NFA_VCOL:
6623 case NFA_VCOL_GT:
6624 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006625 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006626 int op = t->state->c - NFA_VCOL;
6627 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006628 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006629
6630 /* Bail out quickly when there can't be a match, avoid the
6631 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006632 if (op != 1 && col > t->state->val
6633#ifdef FEAT_MBYTE
6634 * (has_mbyte ? MB_MAXBYTES : 1)
6635#endif
6636 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006637 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006638 result = FALSE;
6639 if (op == 1 && col - 1 > t->state->val && col > 100)
6640 {
6641 int ts = wp->w_buffer->b_p_ts;
6642
6643 /* Guess that a character won't use more columns than
6644 * 'tabstop', with a minimum of 4. */
6645 if (ts < 4)
6646 ts = 4;
6647 result = col > t->state->val * ts;
6648 }
6649 if (!result)
6650 result = nfa_re_num_cmp(t->state->val, op,
6651 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006652 if (result)
6653 {
6654 add_here = TRUE;
6655 add_state = t->state->out;
6656 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006657 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006658 break;
6659
Bram Moolenaar044aa292013-06-04 21:27:38 +02006660 case NFA_MARK:
6661 case NFA_MARK_GT:
6662 case NFA_MARK_LT:
6663 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006664 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006665
6666 /* Compare the mark position to the match position. */
6667 result = (pos != NULL /* mark doesn't exist */
6668 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006669 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006670 ? (pos->col == (colnr_T)(reginput - regline)
6671 ? t->state->c == NFA_MARK
6672 : (pos->col < (colnr_T)(reginput - regline)
6673 ? t->state->c == NFA_MARK_GT
6674 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006675 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006676 ? t->state->c == NFA_MARK_GT
6677 : t->state->c == NFA_MARK_LT)));
6678 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006679 {
6680 add_here = TRUE;
6681 add_state = t->state->out;
6682 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006683 break;
6684 }
6685
Bram Moolenaar423532e2013-05-29 21:14:42 +02006686 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006687 result = (rex.reg_win != NULL
6688 && (reglnum + rex.reg_firstlnum
6689 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006690 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006691 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006692 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006693 {
6694 add_here = TRUE;
6695 add_state = t->state->out;
6696 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006697 break;
6698
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006699 case NFA_VISUAL:
6700 result = reg_match_visual();
6701 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006702 {
6703 add_here = TRUE;
6704 add_state = t->state->out;
6705 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006706 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006707
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006708 case NFA_MOPEN1:
6709 case NFA_MOPEN2:
6710 case NFA_MOPEN3:
6711 case NFA_MOPEN4:
6712 case NFA_MOPEN5:
6713 case NFA_MOPEN6:
6714 case NFA_MOPEN7:
6715 case NFA_MOPEN8:
6716 case NFA_MOPEN9:
6717#ifdef FEAT_SYN_HL
6718 case NFA_ZOPEN:
6719 case NFA_ZOPEN1:
6720 case NFA_ZOPEN2:
6721 case NFA_ZOPEN3:
6722 case NFA_ZOPEN4:
6723 case NFA_ZOPEN5:
6724 case NFA_ZOPEN6:
6725 case NFA_ZOPEN7:
6726 case NFA_ZOPEN8:
6727 case NFA_ZOPEN9:
6728#endif
6729 case NFA_NOPEN:
6730 case NFA_ZSTART:
6731 /* These states are only added to be able to bail out when
6732 * they are added again, nothing is to be done. */
6733 break;
6734
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006735 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006736 {
6737 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006738
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006739#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006740 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006741 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006742#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006743 result = (c == curc);
6744
Bram Moolenaar6100d022016-10-02 16:51:57 +02006745 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006746 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006747#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006748 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006749 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006750 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006751 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006752#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006753 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006754 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006755 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006756
6757 } /* switch (t->state->c) */
6758
6759 if (add_state != NULL)
6760 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006761 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006762 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006763
6764 if (t->pim.result == NFA_PIM_UNUSED)
6765 pim = NULL;
6766 else
6767 pim = &t->pim;
6768
6769 /* Handle the postponed invisible match if the match might end
6770 * without advancing and before the end of the line. */
6771 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006773 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006774 {
6775#ifdef ENABLE_LOG
6776 fprintf(log_fd, "\n");
6777 fprintf(log_fd, "==================================\n");
6778 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6779 fprintf(log_fd, "\n");
6780#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006781 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006782 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006783 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006784 /* for \@! and \@<! it is a match when the result is
6785 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006786 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006787 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6788 || pim->state->c
6789 == NFA_START_INVISIBLE_BEFORE_NEG
6790 || pim->state->c
6791 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006792 {
6793 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006794 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006795#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006796 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006797 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006798#endif
6799 }
6800 }
6801 else
6802 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006803 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006804#ifdef ENABLE_LOG
6805 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006806 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006807 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6808 fprintf(log_fd, "\n");
6809#endif
6810 }
6811
Bram Moolenaardecd9542013-06-07 16:31:50 +02006812 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006813 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006814 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6815 || pim->state->c
6816 == NFA_START_INVISIBLE_BEFORE_NEG
6817 || pim->state->c
6818 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006819 {
6820 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006821 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006822#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006823 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006824 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006825#endif
6826 }
6827 else
6828 /* look-behind match failed, don't add the state */
6829 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006830
6831 /* Postponed invisible match was handled, don't add it to
6832 * following states. */
6833 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006834 }
6835
Bram Moolenaara951e352013-10-06 15:46:11 +02006836 /* If "pim" points into l->t it will become invalid when
6837 * adding the state causes the list to be reallocated. Make a
6838 * local copy to avoid that. */
6839 if (pim == &t->pim)
6840 {
6841 copy_pim(&pim_copy, pim);
6842 pim = &pim_copy;
6843 }
6844
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006845 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006846 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006847 else
6848 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006849 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006850 if (add_count > 0)
6851 nextlist->t[nextlist->n - 1].count = add_count;
6852 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006853 }
6854
6855 } /* for (thislist = thislist; thislist->state; thislist++) */
6856
Bram Moolenaare23febd2013-05-26 18:40:14 +02006857 /* Look for the start of a match in the current position by adding the
6858 * start state to the list of states.
6859 * The first found match is the leftmost one, thus the order of states
6860 * matters!
6861 * Do not add the start state in recursive calls of nfa_regmatch(),
6862 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006863 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006864 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006865 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006866 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006867 && reglnum == 0
6868 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006869 && (rex.reg_maxcol == 0
6870 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006871 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006872 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006873 ? (reglnum < nfa_endp->se_u.pos.lnum
6874 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006875 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006876 < nfa_endp->se_u.pos.col))
6877 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006878 {
6879#ifdef ENABLE_LOG
6880 fprintf(log_fd, "(---) STARTSTATE\n");
6881#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006882 /* Inline optimized code for addstate() if we know the state is
6883 * the first MOPEN. */
6884 if (toplevel)
6885 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006886 int add = TRUE;
6887 int c;
6888
6889 if (prog->regstart != NUL && clen != 0)
6890 {
6891 if (nextlist->n == 0)
6892 {
6893 colnr_T col = (colnr_T)(reginput - regline) + clen;
6894
6895 /* Nextlist is empty, we can skip ahead to the
6896 * character that must appear at the start. */
6897 if (skip_to_start(prog->regstart, &col) == FAIL)
6898 break;
6899#ifdef ENABLE_LOG
6900 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6901 col - ((colnr_T)(reginput - regline) + clen));
6902#endif
6903 reginput = regline + col - clen;
6904 }
6905 else
6906 {
6907 /* Checking if the required start character matches is
6908 * cheaper than adding a state that won't match. */
6909 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006910 if (c != prog->regstart && (!rex.reg_ic
6911 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006912 {
6913#ifdef ENABLE_LOG
6914 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6915#endif
6916 add = FALSE;
6917 }
6918 }
6919 }
6920
6921 if (add)
6922 {
6923 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006924 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006925 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006926 else
6927 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006928 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006929 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006930 }
6931 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006932 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006933 }
6934
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006935#ifdef ENABLE_LOG
6936 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006937 {
6938 int i;
6939
6940 for (i = 0; i < thislist->n; i++)
6941 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6942 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006943 fprintf(log_fd, "\n");
6944#endif
6945
6946nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006947 /* Advance to the next character, or advance to the next line, or
6948 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006949 if (clen != 0)
6950 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006951 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6952 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006953 reg_nextline();
6954 else
6955 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006956
6957 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006958 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006959 if (got_int)
6960 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006961#ifdef FEAT_RELTIME
6962 /* Check for timeout once in a twenty times to avoid overhead. */
6963 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6964 {
6965 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006966 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006967 break;
6968 }
6969#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006970 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006971
6972#ifdef ENABLE_LOG
6973 if (log_fd != stderr)
6974 fclose(log_fd);
6975 log_fd = NULL;
6976#endif
6977
6978theend:
6979 /* Free memory */
6980 vim_free(list[0].t);
6981 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006982 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006983#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006984#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006985 fclose(debug);
6986#endif
6987
Bram Moolenaar963fee22013-05-26 21:47:28 +02006988 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006989}
6990
6991/*
6992 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006993 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006994 */
6995 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006996nfa_regtry(
6997 nfa_regprog_T *prog,
6998 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006999 proftime_T *tm UNUSED, /* timeout limit or NULL */
7000 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007001{
7002 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007003 regsubs_T subs, m;
7004 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007005 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007006#ifdef ENABLE_LOG
7007 FILE *f;
7008#endif
7009
7010 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007011#ifdef FEAT_RELTIME
7012 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007013 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007014 nfa_time_count = 0;
7015#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007016
7017#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007018 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007019 if (f != NULL)
7020 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007021 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022#ifdef DEBUG
7023 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7024#endif
7025 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02007026 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007027 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007028 fprintf(f, "\n\n");
7029 fclose(f);
7030 }
7031 else
7032 EMSG(_("Could not open temporary log file for writing "));
7033#endif
7034
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007035 clear_sub(&subs.norm);
7036 clear_sub(&m.norm);
7037#ifdef FEAT_SYN_HL
7038 clear_sub(&subs.synt);
7039 clear_sub(&m.synt);
7040#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007041
Bram Moolenaarfda37292014-11-05 14:27:36 +01007042 result = nfa_regmatch(prog, start, &subs, &m);
7043 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007044 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007045 else if (result == NFA_TOO_EXPENSIVE)
7046 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007047
7048 cleanup_subexpr();
7049 if (REG_MULTI)
7050 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007051 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007052 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007053 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7054 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007055
Bram Moolenaar6100d022016-10-02 16:51:57 +02007056 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7057 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007058 }
7059
Bram Moolenaar6100d022016-10-02 16:51:57 +02007060 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007061 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007062 rex.reg_startpos[0].lnum = 0;
7063 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007064 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007065 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007066 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007067 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007068 rex.reg_endpos[0].lnum = reglnum;
7069 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007070 }
7071 else
7072 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007073 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007074 }
7075 else
7076 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007077 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007078 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007079 rex.reg_startp[i] = subs.norm.list.line[i].start;
7080 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007081 }
7082
Bram Moolenaar6100d022016-10-02 16:51:57 +02007083 if (rex.reg_startp[0] == NULL)
7084 rex.reg_startp[0] = regline + col;
7085 if (rex.reg_endp[0] == NULL)
7086 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007087 }
7088
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007089#ifdef FEAT_SYN_HL
7090 /* Package any found \z(...\) matches for export. Default is none. */
7091 unref_extmatch(re_extmatch_out);
7092 re_extmatch_out = NULL;
7093
7094 if (prog->reghasz == REX_SET)
7095 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007096 cleanup_zsubexpr();
7097 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007098 /* Loop over \z1, \z2, etc. There is no \z0. */
7099 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007100 {
7101 if (REG_MULTI)
7102 {
7103 struct multipos *mpos = &subs.synt.list.multi[i];
7104
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007105 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007106 if (mpos->start_lnum >= 0
7107 && mpos->start_lnum == mpos->end_lnum
7108 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007109 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007110 vim_strnsave(reg_getline(mpos->start_lnum)
7111 + mpos->start_col,
7112 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007113 }
7114 else
7115 {
7116 struct linepos *lpos = &subs.synt.list.line[i];
7117
7118 if (lpos->start != NULL && lpos->end != NULL)
7119 re_extmatch_out->matches[i] =
7120 vim_strnsave(lpos->start,
7121 (int)(lpos->end - lpos->start));
7122 }
7123 }
7124 }
7125#endif
7126
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127 return 1 + reglnum;
7128}
7129
7130/*
7131 * Match a regexp against a string ("line" points to the string) or multiple
7132 * lines ("line" is NULL, use reg_getline()).
7133 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007134 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007135 */
7136 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007137nfa_regexec_both(
7138 char_u *line,
7139 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007140 proftime_T *tm, /* timeout limit or NULL */
7141 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007142{
7143 nfa_regprog_T *prog;
7144 long retval = 0L;
7145 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007146 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007147
7148 if (REG_MULTI)
7149 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007150 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007152 rex.reg_startpos = rex.reg_mmatch->startpos;
7153 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007154 }
7155 else
7156 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007157 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7158 rex.reg_startp = rex.reg_match->startp;
7159 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007160 }
7161
7162 /* Be paranoid... */
7163 if (prog == NULL || line == NULL)
7164 {
7165 EMSG(_(e_null));
7166 goto theend;
7167 }
7168
Bram Moolenaar6100d022016-10-02 16:51:57 +02007169 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007171 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007172 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007173 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007174
7175#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007176 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007177 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007178 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007179#endif
7180
7181 regline = line;
7182 reglnum = 0; /* relative to line */
7183
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007184 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007185 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007186 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007187 nfa_listid = 1;
7188 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007189 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007190
Bram Moolenaard89616e2013-06-06 18:46:06 +02007191 if (prog->reganch && col > 0)
7192 return 0L;
7193
Bram Moolenaar473de612013-06-08 18:19:48 +02007194 need_clear_subexpr = TRUE;
7195#ifdef FEAT_SYN_HL
7196 /* Clear the external match subpointers if necessary. */
7197 if (prog->reghasz == REX_SET)
7198 {
7199 nfa_has_zsubexpr = TRUE;
7200 need_clear_zsubexpr = TRUE;
7201 }
7202 else
7203 nfa_has_zsubexpr = FALSE;
7204#endif
7205
Bram Moolenaard89616e2013-06-06 18:46:06 +02007206 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007207 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007208 /* Skip ahead until a character we know the match must start with.
7209 * When there is none there is no match. */
7210 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007211 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007212
Bram Moolenaar473de612013-06-08 18:19:48 +02007213 /* If match_text is set it contains the full text that must match.
7214 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007215 if (prog->match_text != NULL
7216#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007217 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007218#endif
7219 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007220 return find_match_text(col, prog->regstart, prog->match_text);
7221 }
7222
Bram Moolenaard89616e2013-06-06 18:46:06 +02007223 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007224 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007225 goto theend;
7226
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007227 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007228 for (i = 0; i < nstate; ++i)
7229 {
7230 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007231 prog->state[i].lastlist[0] = 0;
7232 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007233 }
7234
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007235 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007236
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007237 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007238
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007239theend:
7240 return retval;
7241}
7242
7243/*
7244 * Compile a regular expression into internal code for the NFA matcher.
7245 * Returns the program in allocated space. Returns NULL for an error.
7246 */
7247 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007248nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007249{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007250 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007251 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007252 int *postfix;
7253
7254 if (expr == NULL)
7255 return NULL;
7256
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007257 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007258 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007259
7260 init_class_tab();
7261
7262 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7263 return NULL;
7264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007265 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007266 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007267 postfix = re2post();
7268 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007269 {
7270 /* TODO: only give this error for debugging? */
7271 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007272 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007273 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007274 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007275
7276 /*
7277 * In order to build the NFA, we parse the input regexp twice:
7278 * 1. first pass to count size (so we can allocate space)
7279 * 2. second to emit code
7280 */
7281#ifdef ENABLE_LOG
7282 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007283 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007284
7285 if (f != NULL)
7286 {
Bram Moolenaar9b0c5c22018-06-20 20:37:36 +02007287 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\"... hold on !\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007288 fclose(f);
7289 }
7290 }
7291#endif
7292
7293 /*
7294 * PASS 1
7295 * Count number of NFA states in "nstate". Do not build the NFA.
7296 */
7297 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007298
Bram Moolenaar16619a22013-06-11 18:42:36 +02007299 /* allocate the regprog with space for the compiled regexp */
7300 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007301 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7302 if (prog == NULL)
7303 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007304 state_ptr = prog->state;
7305
7306 /*
7307 * PASS 2
7308 * Build the NFA
7309 */
7310 prog->start = post2nfa(postfix, post_ptr, FALSE);
7311 if (prog->start == NULL)
7312 goto fail;
7313
7314 prog->regflags = regflags;
7315 prog->engine = &nfa_regengine;
7316 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007317 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007318 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007319 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007320
Bram Moolenaara2947e22013-06-11 22:44:09 +02007321 nfa_postprocess(prog);
7322
Bram Moolenaard89616e2013-06-06 18:46:06 +02007323 prog->reganch = nfa_get_reganch(prog->start, 0);
7324 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007325 prog->match_text = nfa_get_match_text(prog->start);
7326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007327#ifdef ENABLE_LOG
7328 nfa_postfix_dump(expr, OK);
7329 nfa_dump(prog);
7330#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007331#ifdef FEAT_SYN_HL
7332 /* Remember whether this pattern has any \z specials in it. */
7333 prog->reghasz = re_has_z;
7334#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007335 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007336 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007337
7338out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007339 VIM_CLEAR(post_start);
7340 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007341 state_ptr = NULL;
7342 return (regprog_T *)prog;
7343
7344fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007345 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007346#ifdef ENABLE_LOG
7347 nfa_postfix_dump(expr, FAIL);
7348#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007349 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007350 goto out;
7351}
7352
Bram Moolenaar473de612013-06-08 18:19:48 +02007353/*
7354 * Free a compiled regexp program, returned by nfa_regcomp().
7355 */
7356 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007357nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007358{
7359 if (prog != NULL)
7360 {
7361 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007362 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007363 vim_free(prog);
7364 }
7365}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007366
7367/*
7368 * Match a regexp against a string.
7369 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7370 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007371 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007372 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007373 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007374 */
7375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007376nfa_regexec_nl(
7377 regmatch_T *rmp,
7378 char_u *line, /* string to match against */
7379 colnr_T col, /* column to start looking for match */
7380 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007381{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007382 rex.reg_match = rmp;
7383 rex.reg_mmatch = NULL;
7384 rex.reg_maxline = 0;
7385 rex.reg_line_lbr = line_lbr;
7386 rex.reg_buf = curbuf;
7387 rex.reg_win = NULL;
7388 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007389#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007390 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007391#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007392 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007393 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007394}
7395
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007396
7397/*
7398 * Match a regexp against multiple lines.
7399 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7400 * Uses curbuf for line count and 'iskeyword'.
7401 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007402 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007403 * match otherwise.
7404 *
7405 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7406 *
7407 * ! Also NOTE : match may actually be in another line. e.g.:
7408 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7409 *
7410 * +-------------------------+
7411 * |a |
7412 * |b |
7413 * |c |
7414 * | |
7415 * +-------------------------+
7416 *
7417 * then nfa_regexec_multi() returns 3. while the original
7418 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7419 *
7420 * FIXME if this behavior is not compatible.
7421 */
7422 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007423nfa_regexec_multi(
7424 regmmatch_T *rmp,
7425 win_T *win, /* window in which to search or NULL */
7426 buf_T *buf, /* buffer in which to search */
7427 linenr_T lnum, /* nr of line to start looking for match */
7428 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007429 proftime_T *tm, /* timeout limit or NULL */
7430 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007431{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007432 rex.reg_match = NULL;
7433 rex.reg_mmatch = rmp;
7434 rex.reg_buf = buf;
7435 rex.reg_win = win;
7436 rex.reg_firstlnum = lnum;
7437 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7438 rex.reg_line_lbr = FALSE;
7439 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007440#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007441 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007442#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007443 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007444
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007445 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007446}
7447
7448#ifdef DEBUG
7449# undef ENABLE_LOG
7450#endif