blob: feb17bcaf57bf91872b55ff6740547c2f2e565f9 [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 Moolenaar5de820b2013-06-02 15:01:57 +02001485 if (reg_do_extmatch != REX_USE)
1486 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 Moolenaar5de820b2013-06-02 15:01:57 +02001494 if (reg_do_extmatch != REX_SET)
1495 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 Moolenaar021e1472013-05-30 19:18:31 +02001603 int 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 Moolenaard75799ab72013-06-05 11:05:17 +02001631 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001632 break;
1633 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001634 else if (c == '\'' && n == 0)
1635 {
1636 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001637 EMIT(cmp == '<' ? NFA_MARK_LT :
1638 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001639 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001640 break;
1641 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001642 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001643 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1644 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 return FAIL;
1646 }
1647 break;
1648
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001649 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001650collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001652 * [abc] uses NFA_START_COLL - NFA_END_COLL
1653 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1654 * Each character is produced as a regular state, using
1655 * NFA_CONCAT to bind them together.
1656 * Besides normal characters there can be:
1657 * - character classes NFA_CLASS_*
1658 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 */
1660
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 p = regparse;
1662 endp = skip_anyof(p);
1663 if (*endp == ']')
1664 {
1665 /*
1666 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001667 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 * and perform the necessary substitutions in the NFA.
1669 */
1670 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001671 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001672 if (result != FAIL)
1673 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001674 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001675 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001676 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 EMIT(NFA_NEWL);
1678 EMIT(NFA_OR);
1679 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001680 else
1681 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001683 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 return OK;
1685 }
1686 /*
1687 * Failed to recognize a character class. Use the simple
1688 * version that turns [abc] into 'a' OR 'b' OR 'c'
1689 */
1690 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001692 if (*regparse == '^') /* negated range */
1693 {
1694 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001695 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001696 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001697 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001698 else
1699 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001700 if (*regparse == '-')
1701 {
1702 startc = '-';
1703 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001704 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001705 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001706 }
1707 /* Emit the OR branches for each character in the [] */
1708 emit_range = FALSE;
1709 while (regparse < endp)
1710 {
1711 oldstartc = startc;
1712 startc = -1;
1713 got_coll_char = FALSE;
1714 if (*regparse == '[')
1715 {
1716 /* Check for [: :], [= =], [. .] */
1717 equiclass = collclass = 0;
1718 charclass = get_char_class(&regparse);
1719 if (charclass == CLASS_NONE)
1720 {
1721 equiclass = get_equi_class(&regparse);
1722 if (equiclass == 0)
1723 collclass = get_coll_element(&regparse);
1724 }
1725
1726 /* Character class like [:alpha:] */
1727 if (charclass != CLASS_NONE)
1728 {
1729 switch (charclass)
1730 {
1731 case CLASS_ALNUM:
1732 EMIT(NFA_CLASS_ALNUM);
1733 break;
1734 case CLASS_ALPHA:
1735 EMIT(NFA_CLASS_ALPHA);
1736 break;
1737 case CLASS_BLANK:
1738 EMIT(NFA_CLASS_BLANK);
1739 break;
1740 case CLASS_CNTRL:
1741 EMIT(NFA_CLASS_CNTRL);
1742 break;
1743 case CLASS_DIGIT:
1744 EMIT(NFA_CLASS_DIGIT);
1745 break;
1746 case CLASS_GRAPH:
1747 EMIT(NFA_CLASS_GRAPH);
1748 break;
1749 case CLASS_LOWER:
1750 EMIT(NFA_CLASS_LOWER);
1751 break;
1752 case CLASS_PRINT:
1753 EMIT(NFA_CLASS_PRINT);
1754 break;
1755 case CLASS_PUNCT:
1756 EMIT(NFA_CLASS_PUNCT);
1757 break;
1758 case CLASS_SPACE:
1759 EMIT(NFA_CLASS_SPACE);
1760 break;
1761 case CLASS_UPPER:
1762 EMIT(NFA_CLASS_UPPER);
1763 break;
1764 case CLASS_XDIGIT:
1765 EMIT(NFA_CLASS_XDIGIT);
1766 break;
1767 case CLASS_TAB:
1768 EMIT(NFA_CLASS_TAB);
1769 break;
1770 case CLASS_RETURN:
1771 EMIT(NFA_CLASS_RETURN);
1772 break;
1773 case CLASS_BACKSPACE:
1774 EMIT(NFA_CLASS_BACKSPACE);
1775 break;
1776 case CLASS_ESCAPE:
1777 EMIT(NFA_CLASS_ESCAPE);
1778 break;
1779 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001780 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 continue;
1782 }
1783 /* Try equivalence class [=a=] and the like */
1784 if (equiclass != 0)
1785 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001786 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001787 if (result == FAIL)
1788 {
1789 /* should never happen */
1790 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1791 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001792 continue;
1793 }
1794 /* Try collating class like [. .] */
1795 if (collclass != 0)
1796 {
1797 startc = collclass; /* allow [.a.]-x as a range */
1798 /* Will emit the proper atom at the end of the
1799 * while loop. */
1800 }
1801 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001802 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1803 * start character. */
1804 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805 {
1806 emit_range = TRUE;
1807 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001808 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809 continue; /* reading the end of the range */
1810 }
1811
1812 /* Now handle simple and escaped characters.
1813 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1814 * accepts "\t", "\e", etc., but only when the 'l' flag in
1815 * 'cpoptions' is not included.
1816 * Posix doesn't recognize backslash at all.
1817 */
1818 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001819 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001820 && regparse + 1 <= endp
1821 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001822 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823 && vim_strchr(REGEXP_ABBR, regparse[1])
1824 != NULL)
1825 )
1826 )
1827 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001828 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001830 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831 startc = reg_string ? NL : NFA_NEWL;
1832 else
1833 if (*regparse == 'd'
1834 || *regparse == 'o'
1835 || *regparse == 'x'
1836 || *regparse == 'u'
1837 || *regparse == 'U'
1838 )
1839 {
1840 /* TODO(RE) This needs more testing */
1841 startc = coll_get_char();
1842 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001843 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 }
1845 else
1846 {
1847 /* \r,\t,\e,\b */
1848 startc = backslash_trans(*regparse);
1849 }
1850 }
1851
1852 /* Normal printable char */
1853 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001854 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001855
1856 /* Previous char was '-', so this char is end of range. */
1857 if (emit_range)
1858 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001859 endc = startc;
1860 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001862 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001863
1864 if (endc > startc + 2)
1865 {
1866 /* Emit a range instead of the sequence of
1867 * individual characters. */
1868 if (startc == 0)
1869 /* \x00 is translated to \x0a, start at \x01. */
1870 EMIT(1);
1871 else
1872 --post_ptr; /* remove NFA_CONCAT */
1873 EMIT(endc);
1874 EMIT(NFA_RANGE);
1875 EMIT(NFA_CONCAT);
1876 }
1877 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001878#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001879 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 || (*mb_char2len)(endc) > 1))
1881 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001882 /* Emit the characters in the range.
1883 * "startc" was already emitted, so skip it.
1884 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885 for (c = startc + 1; c <= endc; c++)
1886 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001887 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001888 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890 }
1891 else
1892#endif
1893 {
1894#ifdef EBCDIC
1895 int alpha_only = FALSE;
1896
1897 /* for alphabetical range skip the gaps
1898 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1899 if (isalpha(startc) && isalpha(endc))
1900 alpha_only = TRUE;
1901#endif
1902 /* Emit the range. "startc" was already emitted, so
1903 * skip it. */
1904 for (c = startc + 1; c <= endc; c++)
1905#ifdef EBCDIC
1906 if (!alpha_only || isalpha(startc))
1907#endif
1908 {
1909 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001910 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001913 emit_range = FALSE;
1914 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 }
1916 else
1917 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001918 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001920 * Normally, simply emit startc. But if we get char
1921 * code=0 from a collating char, then replace it with
1922 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001924 * the backtracking engine. */
1925 if (startc == NFA_NEWL)
1926 {
1927 /* Line break can't be matched as part of the
1928 * collection, add an OR below. But not for negated
1929 * range. */
1930 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001931 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001934 {
1935 if (got_coll_char == TRUE && startc == 0)
1936 EMIT(0x0a);
1937 else
1938 EMIT(startc);
1939 EMIT(NFA_CONCAT);
1940 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 }
1942
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001943 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944 } /* while (p < endp) */
1945
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001946 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001947 if (*regparse == '-') /* if last, '-' is just a char */
1948 {
1949 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001950 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 /* skip the trailing ] */
1954 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001955 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001956
1957 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001959 EMIT(NFA_END_NEG_COLL);
1960 else
1961 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001962
1963 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001964 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001965 {
1966 EMIT(reg_string ? NL : NFA_NEWL);
1967 EMIT(NFA_OR);
1968 }
1969
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001970 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001971 } /* if exists closing ] */
1972
1973 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001974 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001975 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001977 default:
1978 {
1979#ifdef FEAT_MBYTE
1980 int plen;
1981
1982nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001983 /* plen is length of current char with composing chars */
1984 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001985 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001986 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001988 int i = 0;
1989
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001990 /* A base character plus composing characters, or just one
1991 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001992 * This requires creating a separate atom as if enclosing
1993 * the characters in (), where NFA_COMPOSING is the ( and
1994 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001995 * building the postfix form, not the NFA itself;
1996 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001997 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001998 for (;;)
1999 {
2000 EMIT(c);
2001 if (i > 0)
2002 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002003 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002004 break;
2005 c = utf_ptr2char(old_regparse + i);
2006 }
2007 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 regparse = old_regparse + plen;
2009 }
2010 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011#endif
2012 {
2013 c = no_Magic(c);
2014 EMIT(c);
2015 }
2016 return OK;
2017 }
2018 }
2019
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002020 return OK;
2021}
2022
2023/*
2024 * Parse something followed by possible [*+=].
2025 *
2026 * A piece is an atom, possibly followed by a multi, an indication of how many
2027 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2028 * characters: "", "a", "aa", etc.
2029 *
2030 * piece ::= atom
2031 * or atom multi
2032 */
2033 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002034nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035{
2036 int i;
2037 int op;
2038 int ret;
2039 long minval, maxval;
2040 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002041 parse_state_T old_state;
2042 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002043 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002044 int old_post_pos;
2045 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 int quest;
2047
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002048 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2049 * next. */
2050 save_parse_state(&old_state);
2051
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002052 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002053 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054
2055 ret = nfa_regatom();
2056 if (ret == FAIL)
2057 return FAIL; /* cascaded error */
2058
2059 op = peekchr();
2060 if (re_multi_type(op) == NOT_MULTI)
2061 return OK;
2062
2063 skipchr();
2064 switch (op)
2065 {
2066 case Magic('*'):
2067 EMIT(NFA_STAR);
2068 break;
2069
2070 case Magic('+'):
2071 /*
2072 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2073 * first and only submatch would be "aaa". But the backtracking
2074 * engine interprets the plus as "try matching one more time", and
2075 * a* matches a second time at the end of the input, the empty
2076 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002077 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002079 * In order to be consistent with the old engine, we replace
2080 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002081 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002082 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002083 curchr = -1;
2084 if (nfa_regatom() == FAIL)
2085 return FAIL;
2086 EMIT(NFA_STAR);
2087 EMIT(NFA_CONCAT);
2088 skipchr(); /* skip the \+ */
2089 break;
2090
2091 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002092 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002093 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002094 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002095 switch(op)
2096 {
2097 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002098 /* \@= */
2099 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002101 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002102 /* \@! */
2103 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002104 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002106 op = no_Magic(getchr());
2107 if (op == '=')
2108 /* \@<= */
2109 i = NFA_PREV_ATOM_JUST_BEFORE;
2110 else if (op == '!')
2111 /* \@<! */
2112 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2113 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002114 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002115 /* \@> */
2116 i = NFA_PREV_ATOM_LIKE_PATTERN;
2117 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002119 if (i == 0)
2120 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002121 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2122 return FAIL;
2123 }
2124 EMIT(i);
2125 if (i == NFA_PREV_ATOM_JUST_BEFORE
2126 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2127 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002128 break;
2129
2130 case Magic('?'):
2131 case Magic('='):
2132 EMIT(NFA_QUEST);
2133 break;
2134
2135 case Magic('{'):
2136 /* a{2,5} will expand to 'aaa?a?a?'
2137 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2138 * version of '?'
2139 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2140 * parenthesis have the same id
2141 */
2142
2143 greedy = TRUE;
2144 c2 = peekchr();
2145 if (c2 == '-' || c2 == Magic('-'))
2146 {
2147 skipchr();
2148 greedy = FALSE;
2149 }
2150 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002151 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002152
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002153 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2154 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002155 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002156 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002157 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002158 /* \{}, \{0,} */
2159 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002160 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002161 /* \{-}, \{-0,} */
2162 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 break;
2164 }
2165
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002166 /* Special case: x{0} or x{-0} */
2167 if (maxval == 0)
2168 {
2169 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002170 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002171 /* NFA_EMPTY is 0-length and works everywhere */
2172 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 return OK;
2174 }
2175
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002176 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002177 * maximum is much larger than the minimum and when the maximum is
2178 * large. Bail out if we can use the other engine. */
2179 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002180 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002181 return FAIL;
2182
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002183 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002184 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002185 /* Save parse state after the repeated atom and the \{} */
2186 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002188 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2189 for (i = 0; i < maxval; i++)
2190 {
2191 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002192 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002193 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194 if (nfa_regatom() == FAIL)
2195 return FAIL;
2196 /* after "minval" times, atoms are optional */
2197 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002198 {
2199 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002200 {
2201 if (greedy)
2202 EMIT(NFA_STAR);
2203 else
2204 EMIT(NFA_STAR_NONGREEDY);
2205 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002206 else
2207 EMIT(quest);
2208 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002209 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002210 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002211 if (i + 1 > minval && maxval == MAX_LIMIT)
2212 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002213 }
2214
2215 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002216 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 curchr = -1;
2218
2219 break;
2220
2221
2222 default:
2223 break;
2224 } /* end switch */
2225
2226 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002227 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002229
2230 return OK;
2231}
2232
2233/*
2234 * Parse one or more pieces, concatenated. It matches a match for the
2235 * first piece, followed by a match for the second piece, etc. Example:
2236 * "f[0-9]b", first matches "f", then a digit and then "b".
2237 *
2238 * concat ::= piece
2239 * or piece piece
2240 * or piece piece piece
2241 * etc.
2242 */
2243 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002244nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002245{
2246 int cont = TRUE;
2247 int first = TRUE;
2248
2249 while (cont)
2250 {
2251 switch (peekchr())
2252 {
2253 case NUL:
2254 case Magic('|'):
2255 case Magic('&'):
2256 case Magic(')'):
2257 cont = FALSE;
2258 break;
2259
2260 case Magic('Z'):
2261#ifdef FEAT_MBYTE
2262 regflags |= RF_ICOMBINE;
2263#endif
2264 skipchr_keepstart();
2265 break;
2266 case Magic('c'):
2267 regflags |= RF_ICASE;
2268 skipchr_keepstart();
2269 break;
2270 case Magic('C'):
2271 regflags |= RF_NOICASE;
2272 skipchr_keepstart();
2273 break;
2274 case Magic('v'):
2275 reg_magic = MAGIC_ALL;
2276 skipchr_keepstart();
2277 curchr = -1;
2278 break;
2279 case Magic('m'):
2280 reg_magic = MAGIC_ON;
2281 skipchr_keepstart();
2282 curchr = -1;
2283 break;
2284 case Magic('M'):
2285 reg_magic = MAGIC_OFF;
2286 skipchr_keepstart();
2287 curchr = -1;
2288 break;
2289 case Magic('V'):
2290 reg_magic = MAGIC_NONE;
2291 skipchr_keepstart();
2292 curchr = -1;
2293 break;
2294
2295 default:
2296 if (nfa_regpiece() == FAIL)
2297 return FAIL;
2298 if (first == FALSE)
2299 EMIT(NFA_CONCAT);
2300 else
2301 first = FALSE;
2302 break;
2303 }
2304 }
2305
2306 return OK;
2307}
2308
2309/*
2310 * Parse a branch, one or more concats, separated by "\&". It matches the
2311 * last concat, but only if all the preceding concats also match at the same
2312 * position. Examples:
2313 * "foobeep\&..." matches "foo" in "foobeep".
2314 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2315 *
2316 * branch ::= concat
2317 * or concat \& concat
2318 * or concat \& concat \& concat
2319 * etc.
2320 */
2321 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002322nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002323{
2324 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002325 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326
Bram Moolenaar16299b52013-05-30 18:45:23 +02002327 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002328
2329 /* First branch, possibly the only one */
2330 if (nfa_regconcat() == FAIL)
2331 return FAIL;
2332
2333 ch = peekchr();
2334 /* Try next concats */
2335 while (ch == Magic('&'))
2336 {
2337 skipchr();
2338 EMIT(NFA_NOPEN);
2339 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002340 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002341 if (nfa_regconcat() == FAIL)
2342 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002343 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002344 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002345 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346 EMIT(NFA_CONCAT);
2347 ch = peekchr();
2348 }
2349
Bram Moolenaar699c1202013-09-25 16:41:54 +02002350 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002351 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002352 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353
2354 return OK;
2355}
2356
2357/*
2358 * Parse a pattern, one or more branches, separated by "\|". It matches
2359 * anything that matches one of the branches. Example: "foo\|beep" matches
2360 * "foo" and matches "beep". If more than one branch matches, the first one
2361 * is used.
2362 *
2363 * pattern ::= branch
2364 * or branch \| branch
2365 * or branch \| branch \| branch
2366 * etc.
2367 */
2368 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369nfa_reg(
2370 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002371{
2372 int parno = 0;
2373
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002374 if (paren == REG_PAREN)
2375 {
2376 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002378 parno = regnpar++;
2379 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002380#ifdef FEAT_SYN_HL
2381 else if (paren == REG_ZPAREN)
2382 {
2383 /* Make a ZOPEN node. */
2384 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002385 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002386 parno = regnzpar++;
2387 }
2388#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002389
2390 if (nfa_regbranch() == FAIL)
2391 return FAIL; /* cascaded error */
2392
2393 while (peekchr() == Magic('|'))
2394 {
2395 skipchr();
2396 if (nfa_regbranch() == FAIL)
2397 return FAIL; /* cascaded error */
2398 EMIT(NFA_OR);
2399 }
2400
2401 /* Check for proper termination. */
2402 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2403 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002404 if (paren == REG_NPAREN)
2405 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2406 else
2407 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2408 }
2409 else if (paren == REG_NOPAREN && peekchr() != NUL)
2410 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002411 if (peekchr() == Magic(')'))
2412 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2413 else
2414 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2415 }
2416 /*
2417 * Here we set the flag allowing back references to this set of
2418 * parentheses.
2419 */
2420 if (paren == REG_PAREN)
2421 {
2422 had_endbrace[parno] = TRUE; /* have seen the close paren */
2423 EMIT(NFA_MOPEN + parno);
2424 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002425#ifdef FEAT_SYN_HL
2426 else if (paren == REG_ZPAREN)
2427 EMIT(NFA_ZOPEN + parno);
2428#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002429
2430 return OK;
2431}
2432
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433#ifdef DEBUG
2434static char_u code[50];
2435
2436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002437nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002438{
2439 int addnl = FALSE;
2440
2441 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2442 {
2443 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002444 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445 }
2446
2447 STRCPY(code, "");
2448 switch (c)
2449 {
2450 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2451 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2452 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2453 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2454 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2455 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2456
Bram Moolenaar5714b802013-05-28 22:03:20 +02002457 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2458 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2459 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2460 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2461 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2462 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2463 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2464 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2465 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002466#ifdef FEAT_SYN_HL
2467 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2468 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2469 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2470 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2471 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2472 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2473 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2474 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2475 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2476#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002477 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2478
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 case NFA_PREV_ATOM_NO_WIDTH:
2480 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002481 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2482 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002483 case NFA_PREV_ATOM_JUST_BEFORE:
2484 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2485 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2486 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002487 case NFA_PREV_ATOM_LIKE_PATTERN:
2488 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2489
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002490 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2491 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002492 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002493 case NFA_START_INVISIBLE_FIRST:
2494 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002495 case NFA_START_INVISIBLE_NEG:
2496 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002497 case NFA_START_INVISIBLE_NEG_FIRST:
2498 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002499 case NFA_START_INVISIBLE_BEFORE:
2500 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002501 case NFA_START_INVISIBLE_BEFORE_FIRST:
2502 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002503 case NFA_START_INVISIBLE_BEFORE_NEG:
2504 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002505 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2506 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002507 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002508 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002509 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002510 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002511
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002512 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2513 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002514 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002516 case NFA_MOPEN:
2517 case NFA_MOPEN1:
2518 case NFA_MOPEN2:
2519 case NFA_MOPEN3:
2520 case NFA_MOPEN4:
2521 case NFA_MOPEN5:
2522 case NFA_MOPEN6:
2523 case NFA_MOPEN7:
2524 case NFA_MOPEN8:
2525 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002526 STRCPY(code, "NFA_MOPEN(x)");
2527 code[10] = c - NFA_MOPEN + '0';
2528 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002529 case NFA_MCLOSE:
2530 case NFA_MCLOSE1:
2531 case NFA_MCLOSE2:
2532 case NFA_MCLOSE3:
2533 case NFA_MCLOSE4:
2534 case NFA_MCLOSE5:
2535 case NFA_MCLOSE6:
2536 case NFA_MCLOSE7:
2537 case NFA_MCLOSE8:
2538 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002539 STRCPY(code, "NFA_MCLOSE(x)");
2540 code[11] = c - NFA_MCLOSE + '0';
2541 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002542#ifdef FEAT_SYN_HL
2543 case NFA_ZOPEN:
2544 case NFA_ZOPEN1:
2545 case NFA_ZOPEN2:
2546 case NFA_ZOPEN3:
2547 case NFA_ZOPEN4:
2548 case NFA_ZOPEN5:
2549 case NFA_ZOPEN6:
2550 case NFA_ZOPEN7:
2551 case NFA_ZOPEN8:
2552 case NFA_ZOPEN9:
2553 STRCPY(code, "NFA_ZOPEN(x)");
2554 code[10] = c - NFA_ZOPEN + '0';
2555 break;
2556 case NFA_ZCLOSE:
2557 case NFA_ZCLOSE1:
2558 case NFA_ZCLOSE2:
2559 case NFA_ZCLOSE3:
2560 case NFA_ZCLOSE4:
2561 case NFA_ZCLOSE5:
2562 case NFA_ZCLOSE6:
2563 case NFA_ZCLOSE7:
2564 case NFA_ZCLOSE8:
2565 case NFA_ZCLOSE9:
2566 STRCPY(code, "NFA_ZCLOSE(x)");
2567 code[11] = c - NFA_ZCLOSE + '0';
2568 break;
2569#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002570 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2571 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2572 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2573 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002574 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2575 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002576 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2577 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2578 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2579 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2580 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2581 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2582 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2583 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2584 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2585 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2586 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2587 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2588 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2589 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002590 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002591
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002592 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002593 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2594 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2595 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002596 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002597 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002598
2599 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2600 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2601 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2602 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2603 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2604 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2605 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2606
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002607 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2608 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2609 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2610 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2611 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2612 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2613 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2614 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2615 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2616 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2617 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2618 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2619 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2620 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2621 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2622 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2623
2624 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2625 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2626 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2627 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2628 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2629 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2630 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2631 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2632 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2633 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2634 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2635 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2636 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2637 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2638 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2639 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2640 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2641 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2642 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2643 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2644 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2645 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2646 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2647 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2648 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2649 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2650 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002651 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2652 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2653 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2654 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002655
2656 default:
2657 STRCPY(code, "CHAR(x)");
2658 code[5] = c;
2659 }
2660
2661 if (addnl == TRUE)
2662 STRCAT(code, " + NEWLINE ");
2663
2664}
2665
2666#ifdef ENABLE_LOG
2667static FILE *log_fd;
2668
2669/*
2670 * Print the postfix notation of the current regexp.
2671 */
2672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002673nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002674{
2675 int *p;
2676 FILE *f;
2677
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002678 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002679 if (f != NULL)
2680 {
2681 fprintf(f, "\n-------------------------\n");
2682 if (retval == FAIL)
2683 fprintf(f, ">>> NFA engine failed ... \n");
2684 else if (retval == OK)
2685 fprintf(f, ">>> NFA engine succeeded !\n");
2686 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002687 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002688 {
2689 nfa_set_code(*p);
2690 fprintf(f, "%s, ", code);
2691 }
2692 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002693 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002694 fprintf(f, "%d ", *p);
2695 fprintf(f, "\n\n");
2696 fclose(f);
2697 }
2698}
2699
2700/*
2701 * Print the NFA starting with a root node "state".
2702 */
2703 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002704nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002705{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002706 garray_T indent;
2707
2708 ga_init2(&indent, 1, 64);
2709 ga_append(&indent, '\0');
2710 nfa_print_state2(debugf, state, &indent);
2711 ga_clear(&indent);
2712}
2713
2714 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002715nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002716{
2717 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002718
2719 if (state == NULL)
2720 return;
2721
2722 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002723
2724 /* Output indent */
2725 p = (char_u *)indent->ga_data;
2726 if (indent->ga_len >= 3)
2727 {
2728 int last = indent->ga_len - 3;
2729 char_u save[2];
2730
2731 STRNCPY(save, &p[last], 2);
2732 STRNCPY(&p[last], "+-", 2);
2733 fprintf(debugf, " %s", p);
2734 STRNCPY(&p[last], save, 2);
2735 }
2736 else
2737 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738
2739 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002740 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002741 code,
2742 state->c,
2743 abs(state->id),
2744 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002745 if (state->id < 0)
2746 return;
2747
2748 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002749
2750 /* grow indent for state->out */
2751 indent->ga_len -= 1;
2752 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002753 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002754 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002755 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002756 ga_append(indent, '\0');
2757
2758 nfa_print_state2(debugf, state->out, indent);
2759
2760 /* replace last part of indent for state->out1 */
2761 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002762 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002763 ga_append(indent, '\0');
2764
2765 nfa_print_state2(debugf, state->out1, indent);
2766
2767 /* shrink indent */
2768 indent->ga_len -= 3;
2769 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002770}
2771
2772/*
2773 * Print the NFA state machine.
2774 */
2775 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002776nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002777{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002778 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002779
2780 if (debugf != NULL)
2781 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002782 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002783
Bram Moolenaar473de612013-06-08 18:19:48 +02002784 if (prog->reganch)
2785 fprintf(debugf, "reganch: %d\n", prog->reganch);
2786 if (prog->regstart != NUL)
2787 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2788 prog->regstart, prog->regstart);
2789 if (prog->match_text != NULL)
2790 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002791
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002792 fclose(debugf);
2793 }
2794}
2795#endif /* ENABLE_LOG */
2796#endif /* DEBUG */
2797
2798/*
2799 * Parse r.e. @expr and convert it into postfix form.
2800 * Return the postfix string on success, NULL otherwise.
2801 */
2802 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002803re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002804{
2805 if (nfa_reg(REG_NOPAREN) == FAIL)
2806 return NULL;
2807 EMIT(NFA_MOPEN);
2808 return post_start;
2809}
2810
2811/* NB. Some of the code below is inspired by Russ's. */
2812
2813/*
2814 * Represents an NFA state plus zero or one or two arrows exiting.
2815 * if c == MATCH, no arrows out; matching state.
2816 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2817 * If c < 256, labeled arrow with character c to out.
2818 */
2819
2820static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2821
2822/*
2823 * Allocate and initialize nfa_state_T.
2824 */
2825 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002826alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002827{
2828 nfa_state_T *s;
2829
2830 if (istate >= nstate)
2831 return NULL;
2832
2833 s = &state_ptr[istate++];
2834
2835 s->c = c;
2836 s->out = out;
2837 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002838 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002839
2840 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002841 s->lastlist[0] = 0;
2842 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002843
2844 return s;
2845}
2846
2847/*
2848 * A partially built NFA without the matching state filled in.
2849 * Frag_T.start points at the start state.
2850 * Frag_T.out is a list of places that need to be set to the
2851 * next state for this fragment.
2852 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002853
2854/* Since the out pointers in the list are always
2855 * uninitialized, we use the pointers themselves
2856 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002857typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002858union Ptrlist
2859{
2860 Ptrlist *next;
2861 nfa_state_T *s;
2862};
2863
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002864struct Frag
2865{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002866 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002867 Ptrlist *out;
2868};
2869typedef struct Frag Frag_T;
2870
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002871static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2872static Ptrlist *list1(nfa_state_T **outp);
2873static void patch(Ptrlist *l, nfa_state_T *s);
2874static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2875static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2876static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002877
2878/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002879 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002880 */
2881 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002882frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002883{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002884 Frag_T n;
2885
2886 n.start = start;
2887 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002888 return n;
2889}
2890
2891/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002892 * Create singleton list containing just outp.
2893 */
2894 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002895list1(
2896 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897{
2898 Ptrlist *l;
2899
2900 l = (Ptrlist *)outp;
2901 l->next = NULL;
2902 return l;
2903}
2904
2905/*
2906 * Patch the list of states at out to point to start.
2907 */
2908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002909patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002910{
2911 Ptrlist *next;
2912
2913 for (; l; l = next)
2914 {
2915 next = l->next;
2916 l->s = s;
2917 }
2918}
2919
2920
2921/*
2922 * Join the two lists l1 and l2, returning the combination.
2923 */
2924 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002925append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926{
2927 Ptrlist *oldl1;
2928
2929 oldl1 = l1;
2930 while (l1->next)
2931 l1 = l1->next;
2932 l1->next = l2;
2933 return oldl1;
2934}
2935
2936/*
2937 * Stack used for transforming postfix form into NFA.
2938 */
2939static Frag_T empty;
2940
2941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002942st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002943{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002944#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002945 FILE *df;
2946 int *p2;
2947
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002948 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002949 if (df)
2950 {
2951 fprintf(df, "Error popping the stack!\n");
2952#ifdef DEBUG
2953 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2954#endif
2955 fprintf(df, "Postfix form is: ");
2956#ifdef DEBUG
2957 for (p2 = postfix; p2 < end; p2++)
2958 {
2959 nfa_set_code(*p2);
2960 fprintf(df, "%s, ", code);
2961 }
2962 nfa_set_code(*p);
2963 fprintf(df, "\nCurrent position is: ");
2964 for (p2 = postfix; p2 <= p; p2 ++)
2965 {
2966 nfa_set_code(*p2);
2967 fprintf(df, "%s, ", code);
2968 }
2969#else
2970 for (p2 = postfix; p2 < end; p2++)
2971 {
2972 fprintf(df, "%d, ", *p2);
2973 }
2974 fprintf(df, "\nCurrent position is: ");
2975 for (p2 = postfix; p2 <= p; p2 ++)
2976 {
2977 fprintf(df, "%d, ", *p2);
2978 }
2979#endif
2980 fprintf(df, "\n--------------------------\n");
2981 fclose(df);
2982 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002983#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002984 EMSG(_("E874: (NFA) Could not pop the stack !"));
2985}
2986
2987/*
2988 * Push an item onto the stack.
2989 */
2990 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002991st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992{
2993 Frag_T *stackp = *p;
2994
2995 if (stackp >= stack_end)
2996 return;
2997 *stackp = s;
2998 *p = *p + 1;
2999}
3000
3001/*
3002 * Pop an item from the stack.
3003 */
3004 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003005st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003006{
3007 Frag_T *stackp;
3008
3009 *p = *p - 1;
3010 stackp = *p;
3011 if (stackp < stack)
3012 return empty;
3013 return **p;
3014}
3015
3016/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003017 * Estimate the maximum byte length of anything matching "state".
3018 * When unknown or unlimited return -1.
3019 */
3020 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003021nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003022{
3023 int l, r;
3024 nfa_state_T *state = startstate;
3025 int len = 0;
3026
3027 /* detect looping in a NFA_SPLIT */
3028 if (depth > 4)
3029 return -1;
3030
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003031 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003032 {
3033 switch (state->c)
3034 {
3035 case NFA_END_INVISIBLE:
3036 case NFA_END_INVISIBLE_NEG:
3037 /* the end, return what we have */
3038 return len;
3039
3040 case NFA_SPLIT:
3041 /* two alternatives, use the maximum */
3042 l = nfa_max_width(state->out, depth + 1);
3043 r = nfa_max_width(state->out1, depth + 1);
3044 if (l < 0 || r < 0)
3045 return -1;
3046 return len + (l > r ? l : r);
3047
3048 case NFA_ANY:
3049 case NFA_START_COLL:
3050 case NFA_START_NEG_COLL:
3051 /* matches some character, including composing chars */
3052#ifdef FEAT_MBYTE
3053 if (enc_utf8)
3054 len += MB_MAXBYTES;
3055 else if (has_mbyte)
3056 len += 2;
3057 else
3058#endif
3059 ++len;
3060 if (state->c != NFA_ANY)
3061 {
3062 /* skip over the characters */
3063 state = state->out1->out;
3064 continue;
3065 }
3066 break;
3067
3068 case NFA_DIGIT:
3069 case NFA_WHITE:
3070 case NFA_HEX:
3071 case NFA_OCTAL:
3072 /* ascii */
3073 ++len;
3074 break;
3075
3076 case NFA_IDENT:
3077 case NFA_SIDENT:
3078 case NFA_KWORD:
3079 case NFA_SKWORD:
3080 case NFA_FNAME:
3081 case NFA_SFNAME:
3082 case NFA_PRINT:
3083 case NFA_SPRINT:
3084 case NFA_NWHITE:
3085 case NFA_NDIGIT:
3086 case NFA_NHEX:
3087 case NFA_NOCTAL:
3088 case NFA_WORD:
3089 case NFA_NWORD:
3090 case NFA_HEAD:
3091 case NFA_NHEAD:
3092 case NFA_ALPHA:
3093 case NFA_NALPHA:
3094 case NFA_LOWER:
3095 case NFA_NLOWER:
3096 case NFA_UPPER:
3097 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003098 case NFA_LOWER_IC:
3099 case NFA_NLOWER_IC:
3100 case NFA_UPPER_IC:
3101 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003102 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003103 /* possibly non-ascii */
3104#ifdef FEAT_MBYTE
3105 if (has_mbyte)
3106 len += 3;
3107 else
3108#endif
3109 ++len;
3110 break;
3111
3112 case NFA_START_INVISIBLE:
3113 case NFA_START_INVISIBLE_NEG:
3114 case NFA_START_INVISIBLE_BEFORE:
3115 case NFA_START_INVISIBLE_BEFORE_NEG:
3116 /* zero-width, out1 points to the END state */
3117 state = state->out1->out;
3118 continue;
3119
3120 case NFA_BACKREF1:
3121 case NFA_BACKREF2:
3122 case NFA_BACKREF3:
3123 case NFA_BACKREF4:
3124 case NFA_BACKREF5:
3125 case NFA_BACKREF6:
3126 case NFA_BACKREF7:
3127 case NFA_BACKREF8:
3128 case NFA_BACKREF9:
3129#ifdef FEAT_SYN_HL
3130 case NFA_ZREF1:
3131 case NFA_ZREF2:
3132 case NFA_ZREF3:
3133 case NFA_ZREF4:
3134 case NFA_ZREF5:
3135 case NFA_ZREF6:
3136 case NFA_ZREF7:
3137 case NFA_ZREF8:
3138 case NFA_ZREF9:
3139#endif
3140 case NFA_NEWL:
3141 case NFA_SKIP:
3142 /* unknown width */
3143 return -1;
3144
3145 case NFA_BOL:
3146 case NFA_EOL:
3147 case NFA_BOF:
3148 case NFA_EOF:
3149 case NFA_BOW:
3150 case NFA_EOW:
3151 case NFA_MOPEN:
3152 case NFA_MOPEN1:
3153 case NFA_MOPEN2:
3154 case NFA_MOPEN3:
3155 case NFA_MOPEN4:
3156 case NFA_MOPEN5:
3157 case NFA_MOPEN6:
3158 case NFA_MOPEN7:
3159 case NFA_MOPEN8:
3160 case NFA_MOPEN9:
3161#ifdef FEAT_SYN_HL
3162 case NFA_ZOPEN:
3163 case NFA_ZOPEN1:
3164 case NFA_ZOPEN2:
3165 case NFA_ZOPEN3:
3166 case NFA_ZOPEN4:
3167 case NFA_ZOPEN5:
3168 case NFA_ZOPEN6:
3169 case NFA_ZOPEN7:
3170 case NFA_ZOPEN8:
3171 case NFA_ZOPEN9:
3172 case NFA_ZCLOSE:
3173 case NFA_ZCLOSE1:
3174 case NFA_ZCLOSE2:
3175 case NFA_ZCLOSE3:
3176 case NFA_ZCLOSE4:
3177 case NFA_ZCLOSE5:
3178 case NFA_ZCLOSE6:
3179 case NFA_ZCLOSE7:
3180 case NFA_ZCLOSE8:
3181 case NFA_ZCLOSE9:
3182#endif
3183 case NFA_MCLOSE:
3184 case NFA_MCLOSE1:
3185 case NFA_MCLOSE2:
3186 case NFA_MCLOSE3:
3187 case NFA_MCLOSE4:
3188 case NFA_MCLOSE5:
3189 case NFA_MCLOSE6:
3190 case NFA_MCLOSE7:
3191 case NFA_MCLOSE8:
3192 case NFA_MCLOSE9:
3193 case NFA_NOPEN:
3194 case NFA_NCLOSE:
3195
3196 case NFA_LNUM_GT:
3197 case NFA_LNUM_LT:
3198 case NFA_COL_GT:
3199 case NFA_COL_LT:
3200 case NFA_VCOL_GT:
3201 case NFA_VCOL_LT:
3202 case NFA_MARK_GT:
3203 case NFA_MARK_LT:
3204 case NFA_VISUAL:
3205 case NFA_LNUM:
3206 case NFA_CURSOR:
3207 case NFA_COL:
3208 case NFA_VCOL:
3209 case NFA_MARK:
3210
3211 case NFA_ZSTART:
3212 case NFA_ZEND:
3213 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003214 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003215 case NFA_START_PATTERN:
3216 case NFA_END_PATTERN:
3217 case NFA_COMPOSING:
3218 case NFA_END_COMPOSING:
3219 /* zero-width */
3220 break;
3221
3222 default:
3223 if (state->c < 0)
3224 /* don't know what this is */
3225 return -1;
3226 /* normal character */
3227 len += MB_CHAR2LEN(state->c);
3228 break;
3229 }
3230
3231 /* normal way to continue */
3232 state = state->out;
3233 }
3234
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003235 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003236 return -1;
3237}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003238
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003239/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 * Convert a postfix form into its equivalent NFA.
3241 * Return the NFA start state on success, NULL otherwise.
3242 */
3243 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003244post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245{
3246 int *p;
3247 int mopen;
3248 int mclose;
3249 Frag_T *stack = NULL;
3250 Frag_T *stackp = NULL;
3251 Frag_T *stack_end = NULL;
3252 Frag_T e1;
3253 Frag_T e2;
3254 Frag_T e;
3255 nfa_state_T *s;
3256 nfa_state_T *s1;
3257 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003258 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003259
3260 if (postfix == NULL)
3261 return NULL;
3262
Bram Moolenaar053bb602013-05-20 13:55:21 +02003263#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003264#define POP() st_pop(&stackp, stack); \
3265 if (stackp < stack) \
3266 { \
3267 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003268 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003269 return NULL; \
3270 }
3271
3272 if (nfa_calc_size == FALSE)
3273 {
3274 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003275 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003277 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 }
3279
3280 for (p = postfix; p < end; ++p)
3281 {
3282 switch (*p)
3283 {
3284 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003285 /* Concatenation.
3286 * Pay attention: this operator does not exist in the r.e. itself
3287 * (it is implicit, really). It is added when r.e. is translated
3288 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 if (nfa_calc_size == TRUE)
3290 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003291 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 break;
3293 }
3294 e2 = POP();
3295 e1 = POP();
3296 patch(e1.out, e2.start);
3297 PUSH(frag(e1.start, e2.out));
3298 break;
3299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 case NFA_OR:
3301 /* Alternation */
3302 if (nfa_calc_size == TRUE)
3303 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003304 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003305 break;
3306 }
3307 e2 = POP();
3308 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003309 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003310 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003311 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 PUSH(frag(s, append(e1.out, e2.out)));
3313 break;
3314
3315 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003316 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003317 if (nfa_calc_size == TRUE)
3318 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003319 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003320 break;
3321 }
3322 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003323 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003324 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003325 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003326 patch(e.out, s);
3327 PUSH(frag(s, list1(&s->out1)));
3328 break;
3329
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003330 case NFA_STAR_NONGREEDY:
3331 /* Zero or more, prefer zero */
3332 if (nfa_calc_size == TRUE)
3333 {
3334 nstate++;
3335 break;
3336 }
3337 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003338 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003339 if (s == NULL)
3340 goto theend;
3341 patch(e.out, s);
3342 PUSH(frag(s, list1(&s->out)));
3343 break;
3344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003345 case NFA_QUEST:
3346 /* one or zero atoms=> greedy match */
3347 if (nfa_calc_size == TRUE)
3348 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003349 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003350 break;
3351 }
3352 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003353 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003354 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003355 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356 PUSH(frag(s, append(e.out, list1(&s->out1))));
3357 break;
3358
3359 case NFA_QUEST_NONGREEDY:
3360 /* zero or one atoms => non-greedy match */
3361 if (nfa_calc_size == TRUE)
3362 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003363 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003364 break;
3365 }
3366 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003367 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003369 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370 PUSH(frag(s, append(e.out, list1(&s->out))));
3371 break;
3372
Bram Moolenaar417bad22013-06-07 14:08:30 +02003373 case NFA_END_COLL:
3374 case NFA_END_NEG_COLL:
3375 /* On the stack is the sequence starting with NFA_START_COLL or
3376 * NFA_START_NEG_COLL and all possible characters. Patch it to
3377 * add the output to the start. */
3378 if (nfa_calc_size == TRUE)
3379 {
3380 nstate++;
3381 break;
3382 }
3383 e = POP();
3384 s = alloc_state(NFA_END_COLL, NULL, NULL);
3385 if (s == NULL)
3386 goto theend;
3387 patch(e.out, s);
3388 e.start->out1 = s;
3389 PUSH(frag(e.start, list1(&s->out)));
3390 break;
3391
3392 case NFA_RANGE:
3393 /* Before this are two characters, the low and high end of a
3394 * range. Turn them into two states with MIN and MAX. */
3395 if (nfa_calc_size == TRUE)
3396 {
3397 /* nstate += 0; */
3398 break;
3399 }
3400 e2 = POP();
3401 e1 = POP();
3402 e2.start->val = e2.start->c;
3403 e2.start->c = NFA_RANGE_MAX;
3404 e1.start->val = e1.start->c;
3405 e1.start->c = NFA_RANGE_MIN;
3406 patch(e1.out, e2.start);
3407 PUSH(frag(e1.start, e2.out));
3408 break;
3409
Bram Moolenaar699c1202013-09-25 16:41:54 +02003410 case NFA_EMPTY:
3411 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 if (nfa_calc_size == TRUE)
3413 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003414 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003415 break;
3416 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003417 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003418 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003419 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003420 PUSH(frag(s, list1(&s->out)));
3421 break;
3422
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003423 case NFA_OPT_CHARS:
3424 {
3425 int n;
3426
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003427 /* \%[abc] implemented as:
3428 * NFA_SPLIT
3429 * +-CHAR(a)
3430 * | +-NFA_SPLIT
3431 * | +-CHAR(b)
3432 * | | +-NFA_SPLIT
3433 * | | +-CHAR(c)
3434 * | | | +-next
3435 * | | +- next
3436 * | +- next
3437 * +- next
3438 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003439 n = *++p; /* get number of characters */
3440 if (nfa_calc_size == TRUE)
3441 {
3442 nstate += n;
3443 break;
3444 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003445 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003446 e1.out = NULL; /* stores list with out1's */
3447 s1 = NULL; /* previous NFA_SPLIT to connect to */
3448 while (n-- > 0)
3449 {
3450 e = POP(); /* get character */
3451 s = alloc_state(NFA_SPLIT, e.start, NULL);
3452 if (s == NULL)
3453 goto theend;
3454 if (e1.out == NULL)
3455 e1 = e;
3456 patch(e.out, s1);
3457 append(e1.out, list1(&s->out1));
3458 s1 = s;
3459 }
3460 PUSH(frag(s, e1.out));
3461 break;
3462 }
3463
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003464 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003465 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003466 case NFA_PREV_ATOM_JUST_BEFORE:
3467 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003468 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003469 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003470 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3471 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003473 int start_state;
3474 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003475 int n = 0;
3476 nfa_state_T *zend;
3477 nfa_state_T *skip;
3478
Bram Moolenaardecd9542013-06-07 16:31:50 +02003479 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003480 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003481 case NFA_PREV_ATOM_NO_WIDTH:
3482 start_state = NFA_START_INVISIBLE;
3483 end_state = NFA_END_INVISIBLE;
3484 break;
3485 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3486 start_state = NFA_START_INVISIBLE_NEG;
3487 end_state = NFA_END_INVISIBLE_NEG;
3488 break;
3489 case NFA_PREV_ATOM_JUST_BEFORE:
3490 start_state = NFA_START_INVISIBLE_BEFORE;
3491 end_state = NFA_END_INVISIBLE;
3492 break;
3493 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3494 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3495 end_state = NFA_END_INVISIBLE_NEG;
3496 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003497 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003498 start_state = NFA_START_PATTERN;
3499 end_state = NFA_END_PATTERN;
3500 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003501 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003502
3503 if (before)
3504 n = *++p; /* get the count */
3505
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003506 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003507 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003508 * The \@<= operator: match for the preceding atom.
3509 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003510 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003511 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003512
3513 if (nfa_calc_size == TRUE)
3514 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003515 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516 break;
3517 }
3518 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003519 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003521 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003522
Bram Moolenaar87953742013-06-05 18:52:40 +02003523 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003525 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003526 if (pattern)
3527 {
3528 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3529 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003530 if (skip == NULL)
3531 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003532 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003533 if (zend == NULL)
3534 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003535 s1->out= skip;
3536 patch(e.out, zend);
3537 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003538 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003539 else
3540 {
3541 patch(e.out, s1);
3542 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003543 if (before)
3544 {
3545 if (n <= 0)
3546 /* See if we can guess the maximum width, it avoids a
3547 * lot of pointless tries. */
3548 n = nfa_max_width(e.start, 0);
3549 s->val = n; /* store the count */
3550 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003551 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003552 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003553 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003554
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003555#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003556 case NFA_COMPOSING: /* char with composing char */
3557#if 0
3558 /* TODO */
3559 if (regflags & RF_ICOMBINE)
3560 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003561 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003562 }
3563#endif
3564 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003565#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003566
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003567 case NFA_MOPEN: /* \( \) Submatch */
3568 case NFA_MOPEN1:
3569 case NFA_MOPEN2:
3570 case NFA_MOPEN3:
3571 case NFA_MOPEN4:
3572 case NFA_MOPEN5:
3573 case NFA_MOPEN6:
3574 case NFA_MOPEN7:
3575 case NFA_MOPEN8:
3576 case NFA_MOPEN9:
3577#ifdef FEAT_SYN_HL
3578 case NFA_ZOPEN: /* \z( \) Submatch */
3579 case NFA_ZOPEN1:
3580 case NFA_ZOPEN2:
3581 case NFA_ZOPEN3:
3582 case NFA_ZOPEN4:
3583 case NFA_ZOPEN5:
3584 case NFA_ZOPEN6:
3585 case NFA_ZOPEN7:
3586 case NFA_ZOPEN8:
3587 case NFA_ZOPEN9:
3588#endif
3589 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003590 if (nfa_calc_size == TRUE)
3591 {
3592 nstate += 2;
3593 break;
3594 }
3595
3596 mopen = *p;
3597 switch (*p)
3598 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003599 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3600#ifdef FEAT_SYN_HL
3601 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3602 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3603 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3604 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3605 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3606 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3607 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3608 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3609 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3610 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3611#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003612#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003613 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003614#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003615 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003616 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617 mclose = *p + NSUBEXP;
3618 break;
3619 }
3620
3621 /* Allow "NFA_MOPEN" as a valid postfix representation for
3622 * the empty regexp "". In this case, the NFA will be
3623 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3624 * empty groups of parenthesis, and empty mbyte chars */
3625 if (stackp == stack)
3626 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003627 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003628 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003629 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003630 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003631 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003632 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003633 patch(list1(&s->out), s1);
3634 PUSH(frag(s, list1(&s1->out)));
3635 break;
3636 }
3637
3638 /* At least one node was emitted before NFA_MOPEN, so
3639 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3640 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003641 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003643 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644
Bram Moolenaar525666f2013-06-02 16:40:55 +02003645 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003646 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003647 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003648 patch(e.out, s1);
3649
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003650#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003651 if (mopen == NFA_COMPOSING)
3652 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003653 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003654#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655
3656 PUSH(frag(s, list1(&s1->out)));
3657 break;
3658
Bram Moolenaar5714b802013-05-28 22:03:20 +02003659 case NFA_BACKREF1:
3660 case NFA_BACKREF2:
3661 case NFA_BACKREF3:
3662 case NFA_BACKREF4:
3663 case NFA_BACKREF5:
3664 case NFA_BACKREF6:
3665 case NFA_BACKREF7:
3666 case NFA_BACKREF8:
3667 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003668#ifdef FEAT_SYN_HL
3669 case NFA_ZREF1:
3670 case NFA_ZREF2:
3671 case NFA_ZREF3:
3672 case NFA_ZREF4:
3673 case NFA_ZREF5:
3674 case NFA_ZREF6:
3675 case NFA_ZREF7:
3676 case NFA_ZREF8:
3677 case NFA_ZREF9:
3678#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003679 if (nfa_calc_size == TRUE)
3680 {
3681 nstate += 2;
3682 break;
3683 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003684 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003685 if (s == NULL)
3686 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003687 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003688 if (s1 == NULL)
3689 goto theend;
3690 patch(list1(&s->out), s1);
3691 PUSH(frag(s, list1(&s1->out)));
3692 break;
3693
Bram Moolenaar423532e2013-05-29 21:14:42 +02003694 case NFA_LNUM:
3695 case NFA_LNUM_GT:
3696 case NFA_LNUM_LT:
3697 case NFA_VCOL:
3698 case NFA_VCOL_GT:
3699 case NFA_VCOL_LT:
3700 case NFA_COL:
3701 case NFA_COL_GT:
3702 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003703 case NFA_MARK:
3704 case NFA_MARK_GT:
3705 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003706 {
3707 int n = *++p; /* lnum, col or mark name */
3708
Bram Moolenaar423532e2013-05-29 21:14:42 +02003709 if (nfa_calc_size == TRUE)
3710 {
3711 nstate += 1;
3712 break;
3713 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003714 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003715 if (s == NULL)
3716 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003717 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003718 PUSH(frag(s, list1(&s->out)));
3719 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003720 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003721
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 case NFA_ZSTART:
3723 case NFA_ZEND:
3724 default:
3725 /* Operands */
3726 if (nfa_calc_size == TRUE)
3727 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003728 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 break;
3730 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003731 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003732 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003733 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003734 PUSH(frag(s, list1(&s->out)));
3735 break;
3736
3737 } /* switch(*p) */
3738
3739 } /* for(p = postfix; *p; ++p) */
3740
3741 if (nfa_calc_size == TRUE)
3742 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003743 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003744 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003745 }
3746
3747 e = POP();
3748 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003749 {
3750 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003751 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 +02003752 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003753
3754 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003755 {
3756 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003758 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760 matchstate = &state_ptr[istate++]; /* the match state */
3761 matchstate->c = NFA_MATCH;
3762 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003763 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764
3765 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003766 ret = e.start;
3767
3768theend:
3769 vim_free(stack);
3770 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003771
3772#undef POP1
3773#undef PUSH1
3774#undef POP2
3775#undef PUSH2
3776#undef POP
3777#undef PUSH
3778}
3779
Bram Moolenaara2947e22013-06-11 22:44:09 +02003780/*
3781 * After building the NFA program, inspect it to add optimization hints.
3782 */
3783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003784nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003785{
3786 int i;
3787 int c;
3788
3789 for (i = 0; i < prog->nstate; ++i)
3790 {
3791 c = prog->state[i].c;
3792 if (c == NFA_START_INVISIBLE
3793 || c == NFA_START_INVISIBLE_NEG
3794 || c == NFA_START_INVISIBLE_BEFORE
3795 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3796 {
3797 int directly;
3798
3799 /* Do it directly when what follows is possibly the end of the
3800 * match. */
3801 if (match_follows(prog->state[i].out1->out, 0))
3802 directly = TRUE;
3803 else
3804 {
3805 int ch_invisible = failure_chance(prog->state[i].out, 0);
3806 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3807
3808 /* Postpone when the invisible match is expensive or has a
3809 * lower chance of failing. */
3810 if (c == NFA_START_INVISIBLE_BEFORE
3811 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3812 {
3813 /* "before" matches are very expensive when
3814 * unbounded, always prefer what follows then,
3815 * unless what follows will always match.
3816 * Otherwise strongly prefer what follows. */
3817 if (prog->state[i].val <= 0 && ch_follows > 0)
3818 directly = FALSE;
3819 else
3820 directly = ch_follows * 10 < ch_invisible;
3821 }
3822 else
3823 {
3824 /* normal invisible, first do the one with the
3825 * highest failure chance */
3826 directly = ch_follows < ch_invisible;
3827 }
3828 }
3829 if (directly)
3830 /* switch to the _FIRST state */
3831 ++prog->state[i].c;
3832 }
3833 }
3834}
3835
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836/****************************************************************
3837 * NFA execution code.
3838 ****************************************************************/
3839
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003840typedef struct
3841{
3842 int in_use; /* number of subexpr with useful info */
3843
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003844 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003845 union
3846 {
3847 struct multipos
3848 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003849 linenr_T start_lnum;
3850 linenr_T end_lnum;
3851 colnr_T start_col;
3852 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003853 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003854 struct linepos
3855 {
3856 char_u *start;
3857 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003858 } line[NSUBEXP];
3859 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003860} regsub_T;
3861
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003862typedef struct
3863{
3864 regsub_T norm; /* \( .. \) matches */
3865#ifdef FEAT_SYN_HL
3866 regsub_T synt; /* \z( .. \) matches */
3867#endif
3868} regsubs_T;
3869
Bram Moolenaara2d95102013-06-04 14:23:05 +02003870/* nfa_pim_T stores a Postponed Invisible Match. */
3871typedef struct nfa_pim_S nfa_pim_T;
3872struct nfa_pim_S
3873{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003874 int result; /* NFA_PIM_*, see below */
3875 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003876 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003877 union
3878 {
3879 lpos_T pos;
3880 char_u *ptr;
3881 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003882};
3883
3884/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003885#define NFA_PIM_UNUSED 0 /* pim not used */
3886#define NFA_PIM_TODO 1 /* pim not done yet */
3887#define NFA_PIM_MATCH 2 /* pim executed, matches */
3888#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003889
3890
Bram Moolenaar963fee22013-05-26 21:47:28 +02003891/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003892typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003893{
3894 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003895 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003896 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3897 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003898 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003899} nfa_thread_T;
3900
Bram Moolenaar963fee22013-05-26 21:47:28 +02003901/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003902typedef struct
3903{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003904 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003905 int n; /* nr of states currently in "t" */
3906 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003907 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003908 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003909} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910
Bram Moolenaar5714b802013-05-28 22:03:20 +02003911#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003912static void log_subsexpr(regsubs_T *subs);
3913static void log_subexpr(regsub_T *sub);
3914static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003915
3916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003917log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003918{
3919 log_subexpr(&subs->norm);
3920# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003921 if (nfa_has_zsubexpr)
3922 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003923# endif
3924}
3925
Bram Moolenaar5714b802013-05-28 22:03:20 +02003926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003927log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003928{
3929 int j;
3930
3931 for (j = 0; j < sub->in_use; j++)
3932 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003933 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003934 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003935 sub->list.multi[j].start_col,
3936 (int)sub->list.multi[j].start_lnum,
3937 sub->list.multi[j].end_col,
3938 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003939 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003940 {
3941 char *s = (char *)sub->list.line[j].start;
3942 char *e = (char *)sub->list.line[j].end;
3943
Bram Moolenaar87953742013-06-05 18:52:40 +02003944 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003945 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003946 s == NULL ? "NULL" : s,
3947 e == NULL ? "NULL" : e);
3948 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003949}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003950
3951 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003952pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003953{
3954 static char buf[30];
3955
3956 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3957 buf[0] = NUL;
3958 else
3959 {
3960 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3961 : (int)(pim->end.ptr - reginput));
3962 }
3963 return buf;
3964}
3965
Bram Moolenaar5714b802013-05-28 22:03:20 +02003966#endif
3967
Bram Moolenaar963fee22013-05-26 21:47:28 +02003968/* Used during execution: whether a match has been found. */
3969static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003970#ifdef FEAT_RELTIME
3971static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003972static int *nfa_timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003973static int nfa_time_count;
3974#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003975
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003976static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3977static void clear_sub(regsub_T *sub);
3978static void copy_sub(regsub_T *to, regsub_T *from);
3979static void copy_sub_off(regsub_T *to, regsub_T *from);
3980static void copy_ze_off(regsub_T *to, regsub_T *from);
3981static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3982static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3983static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3984static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3985static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3986static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3987static 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 +02003988
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003989/*
3990 * Copy postponed invisible match info from "from" to "to".
3991 */
3992 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003993copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003994{
3995 to->result = from->result;
3996 to->state = from->state;
3997 copy_sub(&to->subs.norm, &from->subs.norm);
3998#ifdef FEAT_SYN_HL
3999 if (nfa_has_zsubexpr)
4000 copy_sub(&to->subs.synt, &from->subs.synt);
4001#endif
4002 to->end = from->end;
4003}
4004
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004006clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004007{
4008 if (REG_MULTI)
4009 /* Use 0xff to set lnum to -1 */
4010 vim_memset(sub->list.multi, 0xff,
4011 sizeof(struct multipos) * nfa_nsubexpr);
4012 else
4013 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4014 sub->in_use = 0;
4015}
4016
4017/*
4018 * Copy the submatches from "from" to "to".
4019 */
4020 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004021copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004022{
4023 to->in_use = from->in_use;
4024 if (from->in_use > 0)
4025 {
4026 /* Copy the match start and end positions. */
4027 if (REG_MULTI)
4028 mch_memmove(&to->list.multi[0],
4029 &from->list.multi[0],
4030 sizeof(struct multipos) * from->in_use);
4031 else
4032 mch_memmove(&to->list.line[0],
4033 &from->list.line[0],
4034 sizeof(struct linepos) * from->in_use);
4035 }
4036}
4037
4038/*
4039 * Like copy_sub() but exclude the main match.
4040 */
4041 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004042copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004043{
4044 if (to->in_use < from->in_use)
4045 to->in_use = from->in_use;
4046 if (from->in_use > 1)
4047 {
4048 /* Copy the match start and end positions. */
4049 if (REG_MULTI)
4050 mch_memmove(&to->list.multi[1],
4051 &from->list.multi[1],
4052 sizeof(struct multipos) * (from->in_use - 1));
4053 else
4054 mch_memmove(&to->list.line[1],
4055 &from->list.line[1],
4056 sizeof(struct linepos) * (from->in_use - 1));
4057 }
4058}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004059
Bram Moolenaar428e9872013-05-30 17:05:39 +02004060/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004061 * Like copy_sub() but only do the end of the main match if \ze is present.
4062 */
4063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004064copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004065{
4066 if (nfa_has_zend)
4067 {
4068 if (REG_MULTI)
4069 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004070 if (from->list.multi[0].end_lnum >= 0)
4071 {
4072 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4073 to->list.multi[0].end_col = from->list.multi[0].end_col;
4074 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004075 }
4076 else
4077 {
4078 if (from->list.line[0].end != NULL)
4079 to->list.line[0].end = from->list.line[0].end;
4080 }
4081 }
4082}
4083
4084/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004085 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004086 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004087 */
4088 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004089sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004090{
4091 int i;
4092 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004093 linenr_T s1;
4094 linenr_T s2;
4095 char_u *sp1;
4096 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004097
4098 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4099 if (REG_MULTI)
4100 {
4101 for (i = 0; i < todo; ++i)
4102 {
4103 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004104 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004105 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004106 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004107 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004108 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004109 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004110 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004111 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004112 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004113 if (s1 != -1 && sub1->list.multi[i].start_col
4114 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004115 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004116
4117 if (nfa_has_backref)
4118 {
4119 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004120 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004121 else
4122 s1 = -1;
4123 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004124 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004125 else
4126 s2 = -1;
4127 if (s1 != s2)
4128 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004129 if (s1 != -1 && sub1->list.multi[i].end_col
4130 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004131 return FALSE;
4132 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004133 }
4134 }
4135 else
4136 {
4137 for (i = 0; i < todo; ++i)
4138 {
4139 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004140 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004141 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004142 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004143 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004144 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004145 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004146 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004147 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004148 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004149 if (nfa_has_backref)
4150 {
4151 if (i < sub1->in_use)
4152 sp1 = sub1->list.line[i].end;
4153 else
4154 sp1 = NULL;
4155 if (i < sub2->in_use)
4156 sp2 = sub2->list.line[i].end;
4157 else
4158 sp2 = NULL;
4159 if (sp1 != sp2)
4160 return FALSE;
4161 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004162 }
4163 }
4164
4165 return TRUE;
4166}
4167
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004168#ifdef ENABLE_LOG
4169 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004170report_state(char *action,
4171 regsub_T *sub,
4172 nfa_state_T *state,
4173 int lid,
4174 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004175{
4176 int col;
4177
4178 if (sub->in_use <= 0)
4179 col = -1;
4180 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004181 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004182 else
4183 col = (int)(sub->list.line[0].start - regline);
4184 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004185 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4186 action, abs(state->id), lid, state->c, code, col,
4187 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004188}
4189#endif
4190
Bram Moolenaar43e02982013-06-07 17:31:29 +02004191/*
4192 * Return TRUE if the same state is already in list "l" with the same
4193 * positions as "subs".
4194 */
4195 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004196has_state_with_pos(
4197 nfa_list_T *l, /* runtime state list */
4198 nfa_state_T *state, /* state to update */
4199 regsubs_T *subs, /* pointers to subexpressions */
4200 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004201{
4202 nfa_thread_T *thread;
4203 int i;
4204
4205 for (i = 0; i < l->n; ++i)
4206 {
4207 thread = &l->t[i];
4208 if (thread->state->id == state->id
4209 && sub_equal(&thread->subs.norm, &subs->norm)
4210#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004211 && (!nfa_has_zsubexpr
4212 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004213#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004214 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004215 return TRUE;
4216 }
4217 return FALSE;
4218}
4219
4220/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004221 * Return TRUE if "one" and "two" are equal. That includes when both are not
4222 * set.
4223 */
4224 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004225pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004226{
4227 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4228 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4229
4230 if (one_unused)
4231 /* one is unused: equal when two is also unused */
4232 return two_unused;
4233 if (two_unused)
4234 /* one is used and two is not: not equal */
4235 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004236 /* compare the state id */
4237 if (one->state->id != two->state->id)
4238 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004239 /* compare the position */
4240 if (REG_MULTI)
4241 return one->end.pos.lnum == two->end.pos.lnum
4242 && one->end.pos.col == two->end.pos.col;
4243 return one->end.ptr == two->end.ptr;
4244}
4245
4246/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004247 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4248 */
4249 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004250match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004251{
4252 nfa_state_T *state = startstate;
4253
4254 /* avoid too much recursion */
4255 if (depth > 10)
4256 return FALSE;
4257
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004258 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004259 {
4260 switch (state->c)
4261 {
4262 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004263 case NFA_MCLOSE:
4264 case NFA_END_INVISIBLE:
4265 case NFA_END_INVISIBLE_NEG:
4266 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004267 return TRUE;
4268
4269 case NFA_SPLIT:
4270 return match_follows(state->out, depth + 1)
4271 || match_follows(state->out1, depth + 1);
4272
4273 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004274 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004276 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004277 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004278 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004279 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004280 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004281 case NFA_COMPOSING:
4282 /* skip ahead to next state */
4283 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004284 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004285
4286 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004287 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004288 case NFA_IDENT:
4289 case NFA_SIDENT:
4290 case NFA_KWORD:
4291 case NFA_SKWORD:
4292 case NFA_FNAME:
4293 case NFA_SFNAME:
4294 case NFA_PRINT:
4295 case NFA_SPRINT:
4296 case NFA_WHITE:
4297 case NFA_NWHITE:
4298 case NFA_DIGIT:
4299 case NFA_NDIGIT:
4300 case NFA_HEX:
4301 case NFA_NHEX:
4302 case NFA_OCTAL:
4303 case NFA_NOCTAL:
4304 case NFA_WORD:
4305 case NFA_NWORD:
4306 case NFA_HEAD:
4307 case NFA_NHEAD:
4308 case NFA_ALPHA:
4309 case NFA_NALPHA:
4310 case NFA_LOWER:
4311 case NFA_NLOWER:
4312 case NFA_UPPER:
4313 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004314 case NFA_LOWER_IC:
4315 case NFA_NLOWER_IC:
4316 case NFA_UPPER_IC:
4317 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004318 case NFA_START_COLL:
4319 case NFA_START_NEG_COLL:
4320 case NFA_NEWL:
4321 /* state will advance input */
4322 return FALSE;
4323
4324 default:
4325 if (state->c > 0)
4326 /* state will advance input */
4327 return FALSE;
4328
4329 /* Others: zero-width or possibly zero-width, might still find
4330 * a match at the same position, keep looking. */
4331 break;
4332 }
4333 state = state->out;
4334 }
4335 return FALSE;
4336}
4337
4338
4339/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004340 * Return TRUE if "state" is already in list "l".
4341 */
4342 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004343state_in_list(
4344 nfa_list_T *l, /* runtime state list */
4345 nfa_state_T *state, /* state to update */
4346 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004347{
4348 if (state->lastlist[nfa_ll_index] == l->id)
4349 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004350 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004351 return TRUE;
4352 }
4353 return FALSE;
4354}
4355
Bram Moolenaar16b35782016-09-09 20:29:50 +02004356/* Offset used for "off" by addstate_here(). */
4357#define ADDSTATE_HERE_OFFSET 10
4358
Bram Moolenaard05bf562013-06-30 23:24:08 +02004359/*
4360 * Add "state" and possibly what follows to state list ".".
4361 * Returns "subs_arg", possibly copied into temp_subs.
4362 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004363 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004364addstate(
4365 nfa_list_T *l, /* runtime state list */
4366 nfa_state_T *state, /* state to update */
4367 regsubs_T *subs_arg, /* pointers to subexpressions */
4368 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004369 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004370{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004371 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004372 int off = off_arg;
4373 int add_here = FALSE;
4374 int listindex = 0;
4375 int k;
4376 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004377 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004378 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004379 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004380 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004381 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004382 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004383 regsubs_T *subs = subs_arg;
4384 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004385#ifdef ENABLE_LOG
4386 int did_print = FALSE;
4387#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004388
Bram Moolenaar16b35782016-09-09 20:29:50 +02004389 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4390 {
4391 add_here = TRUE;
4392 off = 0;
4393 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4394 }
4395
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004396 switch (state->c)
4397 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004398 case NFA_NCLOSE:
4399 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004400 case NFA_MCLOSE1:
4401 case NFA_MCLOSE2:
4402 case NFA_MCLOSE3:
4403 case NFA_MCLOSE4:
4404 case NFA_MCLOSE5:
4405 case NFA_MCLOSE6:
4406 case NFA_MCLOSE7:
4407 case NFA_MCLOSE8:
4408 case NFA_MCLOSE9:
4409#ifdef FEAT_SYN_HL
4410 case NFA_ZCLOSE:
4411 case NFA_ZCLOSE1:
4412 case NFA_ZCLOSE2:
4413 case NFA_ZCLOSE3:
4414 case NFA_ZCLOSE4:
4415 case NFA_ZCLOSE5:
4416 case NFA_ZCLOSE6:
4417 case NFA_ZCLOSE7:
4418 case NFA_ZCLOSE8:
4419 case NFA_ZCLOSE9:
4420#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004421 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004422 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004423 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004424 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004425 /* These nodes are not added themselves but their "out" and/or
4426 * "out1" may be added below. */
4427 break;
4428
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004429 case NFA_BOL:
4430 case NFA_BOF:
4431 /* "^" won't match past end-of-line, don't bother trying.
4432 * Except when at the end of the line, or when we are going to the
4433 * next line for a look-behind match. */
4434 if (reginput > regline
4435 && *reginput != NUL
4436 && (nfa_endp == NULL
4437 || !REG_MULTI
4438 || reglnum == nfa_endp->se_u.pos.lnum))
4439 goto skip_add;
4440 /* FALLTHROUGH */
4441
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004442 case NFA_MOPEN1:
4443 case NFA_MOPEN2:
4444 case NFA_MOPEN3:
4445 case NFA_MOPEN4:
4446 case NFA_MOPEN5:
4447 case NFA_MOPEN6:
4448 case NFA_MOPEN7:
4449 case NFA_MOPEN8:
4450 case NFA_MOPEN9:
4451#ifdef FEAT_SYN_HL
4452 case NFA_ZOPEN:
4453 case NFA_ZOPEN1:
4454 case NFA_ZOPEN2:
4455 case NFA_ZOPEN3:
4456 case NFA_ZOPEN4:
4457 case NFA_ZOPEN5:
4458 case NFA_ZOPEN6:
4459 case NFA_ZOPEN7:
4460 case NFA_ZOPEN8:
4461 case NFA_ZOPEN9:
4462#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004463 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004464 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004465 /* These nodes need to be added so that we can bail out when it
4466 * was added to this list before at the same position to avoid an
4467 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004468
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004470 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004471 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004472 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004473 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004474 * when there is a PIM. For NFA_MATCH check the position,
4475 * lower position is preferred. */
4476 if (!nfa_has_backref && pim == NULL && !l->has_pim
4477 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004478 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004479 /* When called from addstate_here() do insert before
4480 * existing states. */
4481 if (add_here)
4482 {
4483 for (k = 0; k < l->n && k < listindex; ++k)
4484 if (l->t[k].state->id == state->id)
4485 {
4486 found = TRUE;
4487 break;
4488 }
4489 }
4490 if (!add_here || found)
4491 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004492skip_add:
4493#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004494 nfa_set_code(state->c);
4495 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4496 abs(state->id), l->id, state->c, code,
4497 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004498#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004499 return subs;
4500 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004501 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004502
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004503 /* Do not add the state again when it exists with the same
4504 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004505 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004506 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004507 }
4508
Bram Moolenaara0169122013-06-26 18:16:58 +02004509 /* When there are backreferences or PIMs the number of states may
4510 * be (a lot) bigger than anticipated. */
4511 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004512 {
4513 int newlen = l->len * 3 / 2 + 50;
4514
Bram Moolenaard05bf562013-06-30 23:24:08 +02004515 if (subs != &temp_subs)
4516 {
4517 /* "subs" may point into the current array, need to make a
4518 * copy before it becomes invalid. */
4519 copy_sub(&temp_subs.norm, &subs->norm);
4520#ifdef FEAT_SYN_HL
4521 if (nfa_has_zsubexpr)
4522 copy_sub(&temp_subs.synt, &subs->synt);
4523#endif
4524 subs = &temp_subs;
4525 }
4526
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004527 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004528 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4529 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004530 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004531
4532 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004533 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004534 thread = &l->t[l->n++];
4535 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004536 if (pim == NULL)
4537 thread->pim.result = NFA_PIM_UNUSED;
4538 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004539 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004540 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004541 l->has_pim = TRUE;
4542 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004543 copy_sub(&thread->subs.norm, &subs->norm);
4544#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004545 if (nfa_has_zsubexpr)
4546 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004547#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004548#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004549 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004550 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004551#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004552 }
4553
4554#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004555 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004556 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557#endif
4558 switch (state->c)
4559 {
4560 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561 break;
4562
4563 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004565 subs = addstate(l, state->out, subs, pim, off_arg);
4566 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004567 break;
4568
Bram Moolenaar699c1202013-09-25 16:41:54 +02004569 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 case NFA_NOPEN:
4571 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004572 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004573 break;
4574
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004575 case NFA_MOPEN:
4576 case NFA_MOPEN1:
4577 case NFA_MOPEN2:
4578 case NFA_MOPEN3:
4579 case NFA_MOPEN4:
4580 case NFA_MOPEN5:
4581 case NFA_MOPEN6:
4582 case NFA_MOPEN7:
4583 case NFA_MOPEN8:
4584 case NFA_MOPEN9:
4585#ifdef FEAT_SYN_HL
4586 case NFA_ZOPEN:
4587 case NFA_ZOPEN1:
4588 case NFA_ZOPEN2:
4589 case NFA_ZOPEN3:
4590 case NFA_ZOPEN4:
4591 case NFA_ZOPEN5:
4592 case NFA_ZOPEN6:
4593 case NFA_ZOPEN7:
4594 case NFA_ZOPEN8:
4595 case NFA_ZOPEN9:
4596#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004597 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004598 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004599 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004601 sub = &subs->norm;
4602 }
4603#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004604 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004605 {
4606 subidx = state->c - NFA_ZOPEN;
4607 sub = &subs->synt;
4608 }
4609#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004610 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004611 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004612 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004613 sub = &subs->norm;
4614 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004615
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004616 /* avoid compiler warnings */
4617 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004618 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004619
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004620 /* Set the position (with "off" added) in the subexpression. Save
4621 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004622 if (REG_MULTI)
4623 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004624 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004626 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004627 save_in_use = -1;
4628 }
4629 else
4630 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004631 save_in_use = sub->in_use;
4632 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004633 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004634 sub->list.multi[i].start_lnum = -1;
4635 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004636 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004637 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004638 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004639 if (off == -1)
4640 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004641 sub->list.multi[subidx].start_lnum = reglnum + 1;
4642 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004643 }
4644 else
4645 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004646 sub->list.multi[subidx].start_lnum = reglnum;
4647 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004648 (colnr_T)(reginput - regline + off);
4649 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004650 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651 }
4652 else
4653 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004654 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004655 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004656 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004657 save_in_use = -1;
4658 }
4659 else
4660 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004661 save_in_use = sub->in_use;
4662 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004663 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004664 sub->list.line[i].start = NULL;
4665 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004666 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004667 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004668 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004669 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004670 }
4671
Bram Moolenaar16b35782016-09-09 20:29:50 +02004672 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004673 /* "subs" may have changed, need to set "sub" again */
4674#ifdef FEAT_SYN_HL
4675 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4676 sub = &subs->synt;
4677 else
4678#endif
4679 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004680
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004681 if (save_in_use == -1)
4682 {
4683 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004684 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004685 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004686 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004687 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004688 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004689 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004690 break;
4691
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004692 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004693 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004694 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004695 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004696 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004697 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004698 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699 break;
4700 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004701 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004702 case NFA_MCLOSE1:
4703 case NFA_MCLOSE2:
4704 case NFA_MCLOSE3:
4705 case NFA_MCLOSE4:
4706 case NFA_MCLOSE5:
4707 case NFA_MCLOSE6:
4708 case NFA_MCLOSE7:
4709 case NFA_MCLOSE8:
4710 case NFA_MCLOSE9:
4711#ifdef FEAT_SYN_HL
4712 case NFA_ZCLOSE:
4713 case NFA_ZCLOSE1:
4714 case NFA_ZCLOSE2:
4715 case NFA_ZCLOSE3:
4716 case NFA_ZCLOSE4:
4717 case NFA_ZCLOSE5:
4718 case NFA_ZCLOSE6:
4719 case NFA_ZCLOSE7:
4720 case NFA_ZCLOSE8:
4721 case NFA_ZCLOSE9:
4722#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004723 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004724 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004725 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004726 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004727 sub = &subs->norm;
4728 }
4729#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004730 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004731 {
4732 subidx = state->c - NFA_ZCLOSE;
4733 sub = &subs->synt;
4734 }
4735#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004736 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004737 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004738 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004739 sub = &subs->norm;
4740 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004741
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004742 /* We don't fill in gaps here, there must have been an MOPEN that
4743 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004744 save_in_use = sub->in_use;
4745 if (sub->in_use <= subidx)
4746 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004747 if (REG_MULTI)
4748 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004749 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004750 if (off == -1)
4751 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004752 sub->list.multi[subidx].end_lnum = reglnum + 1;
4753 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004754 }
4755 else
4756 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004757 sub->list.multi[subidx].end_lnum = reglnum;
4758 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004759 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004760 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004761 /* avoid compiler warnings */
4762 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004763 }
4764 else
4765 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004766 save_ptr = sub->list.line[subidx].end;
4767 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004768 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004769 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004770 }
4771
Bram Moolenaar16b35782016-09-09 20:29:50 +02004772 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004773 /* "subs" may have changed, need to set "sub" again */
4774#ifdef FEAT_SYN_HL
4775 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4776 sub = &subs->synt;
4777 else
4778#endif
4779 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004780
4781 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004782 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004783 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004784 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004785 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004786 break;
4787 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004788 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004789}
4790
4791/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004792 * Like addstate(), but the new state(s) are put at position "*ip".
4793 * Used for zero-width matches, next state to use is the added one.
4794 * This makes sure the order of states to be tried does not change, which
4795 * matters for alternatives.
4796 */
4797 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004798addstate_here(
4799 nfa_list_T *l, /* runtime state list */
4800 nfa_state_T *state, /* state to update */
4801 regsubs_T *subs, /* pointers to subexpressions */
4802 nfa_pim_T *pim, /* postponed look-behind match */
4803 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004804{
4805 int tlen = l->n;
4806 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004807 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004808
Bram Moolenaar16b35782016-09-09 20:29:50 +02004809 /* First add the state(s) at the end, so that we know how many there are.
4810 * Pass the listidx as offset (avoids adding another argument to
4811 * addstate(). */
4812 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004813
Bram Moolenaar4b417062013-05-25 20:19:50 +02004814 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004815 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004816 return;
4817
4818 /* re-order to put the new state at the current position */
4819 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004820 if (count == 0)
4821 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004822 if (count == 1)
4823 {
4824 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004825 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004826 }
4827 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004828 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004829 if (l->n + count - 1 >= l->len)
4830 {
4831 /* not enough space to move the new states, reallocate the list
4832 * and move the states to the right position */
4833 nfa_thread_T *newl;
4834
4835 l->len = l->len * 3 / 2 + 50;
4836 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4837 if (newl == NULL)
4838 return;
4839 mch_memmove(&(newl[0]),
4840 &(l->t[0]),
4841 sizeof(nfa_thread_T) * listidx);
4842 mch_memmove(&(newl[listidx]),
4843 &(l->t[l->n - count]),
4844 sizeof(nfa_thread_T) * count);
4845 mch_memmove(&(newl[listidx + count]),
4846 &(l->t[listidx + 1]),
4847 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4848 vim_free(l->t);
4849 l->t = newl;
4850 }
4851 else
4852 {
4853 /* make space for new states, then move them from the
4854 * end to the current position */
4855 mch_memmove(&(l->t[listidx + count]),
4856 &(l->t[listidx + 1]),
4857 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4858 mch_memmove(&(l->t[listidx]),
4859 &(l->t[l->n - 1]),
4860 sizeof(nfa_thread_T) * count);
4861 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004862 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004863 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004864 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004865}
4866
4867/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004868 * Check character class "class" against current character c.
4869 */
4870 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004871check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872{
4873 switch (class)
4874 {
4875 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004876 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004877 return OK;
4878 break;
4879 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004880 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004881 return OK;
4882 break;
4883 case NFA_CLASS_BLANK:
4884 if (c == ' ' || c == '\t')
4885 return OK;
4886 break;
4887 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004888 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004889 return OK;
4890 break;
4891 case NFA_CLASS_DIGIT:
4892 if (VIM_ISDIGIT(c))
4893 return OK;
4894 break;
4895 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004896 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004897 return OK;
4898 break;
4899 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004900 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901 return OK;
4902 break;
4903 case NFA_CLASS_PRINT:
4904 if (vim_isprintc(c))
4905 return OK;
4906 break;
4907 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004908 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004909 return OK;
4910 break;
4911 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004912 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004913 return OK;
4914 break;
4915 case NFA_CLASS_UPPER:
4916 if (MB_ISUPPER(c))
4917 return OK;
4918 break;
4919 case NFA_CLASS_XDIGIT:
4920 if (vim_isxdigit(c))
4921 return OK;
4922 break;
4923 case NFA_CLASS_TAB:
4924 if (c == '\t')
4925 return OK;
4926 break;
4927 case NFA_CLASS_RETURN:
4928 if (c == '\r')
4929 return OK;
4930 break;
4931 case NFA_CLASS_BACKSPACE:
4932 if (c == '\b')
4933 return OK;
4934 break;
4935 case NFA_CLASS_ESCAPE:
4936 if (c == '\033')
4937 return OK;
4938 break;
4939
4940 default:
4941 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004942 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004943 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004944 }
4945 return FAIL;
4946}
4947
Bram Moolenaar5714b802013-05-28 22:03:20 +02004948/*
4949 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004950 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004951 */
4952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004953match_backref(
4954 regsub_T *sub, /* pointers to subexpressions */
4955 int subidx,
4956 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004957{
4958 int len;
4959
4960 if (sub->in_use <= subidx)
4961 {
4962retempty:
4963 /* backref was not set, match an empty string */
4964 *bytelen = 0;
4965 return TRUE;
4966 }
4967
4968 if (REG_MULTI)
4969 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004970 if (sub->list.multi[subidx].start_lnum < 0
4971 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004972 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004973 if (sub->list.multi[subidx].start_lnum == reglnum
4974 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004975 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004976 len = sub->list.multi[subidx].end_col
4977 - sub->list.multi[subidx].start_col;
4978 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004979 reginput, &len) == 0)
4980 {
4981 *bytelen = len;
4982 return TRUE;
4983 }
4984 }
4985 else
4986 {
4987 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004988 sub->list.multi[subidx].start_lnum,
4989 sub->list.multi[subidx].start_col,
4990 sub->list.multi[subidx].end_lnum,
4991 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004992 bytelen) == RA_MATCH)
4993 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004994 }
4995 }
4996 else
4997 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004998 if (sub->list.line[subidx].start == NULL
4999 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005000 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005001 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
5002 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005003 {
5004 *bytelen = len;
5005 return TRUE;
5006 }
5007 }
5008 return FALSE;
5009}
5010
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005011#ifdef FEAT_SYN_HL
5012
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005013static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005014
5015/*
5016 * Check for a match with \z subexpression "subidx".
5017 * Return TRUE if it matches.
5018 */
5019 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005020match_zref(
5021 int subidx,
5022 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005023{
5024 int len;
5025
5026 cleanup_zsubexpr();
5027 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5028 {
5029 /* backref was not set, match an empty string */
5030 *bytelen = 0;
5031 return TRUE;
5032 }
5033
5034 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5035 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5036 {
5037 *bytelen = len;
5038 return TRUE;
5039 }
5040 return FALSE;
5041}
5042#endif
5043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005044/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005045 * Save list IDs for all NFA states of "prog" into "list".
5046 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005047 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005048 */
5049 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005050nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005052 int i;
5053 nfa_state_T *p;
5054
5055 /* Order in the list is reverse, it's a bit faster that way. */
5056 p = &prog->state[0];
5057 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005058 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005059 list[i] = p->lastlist[1];
5060 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005061 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005062 }
5063}
5064
5065/*
5066 * Restore list IDs from "list" to all NFA states.
5067 */
5068 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005069nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005070{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005071 int i;
5072 nfa_state_T *p;
5073
5074 p = &prog->state[0];
5075 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005076 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005077 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005078 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005079 }
5080}
5081
Bram Moolenaar423532e2013-05-29 21:14:42 +02005082 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005083nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005084{
5085 if (op == 1) return pos > val;
5086 if (op == 2) return pos < val;
5087 return val == pos;
5088}
5089
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005090static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5091static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005092
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005093/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005094 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005095 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5096 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 */
5098 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005099recursive_regmatch(
5100 nfa_state_T *state,
5101 nfa_pim_T *pim,
5102 nfa_regprog_T *prog,
5103 regsubs_T *submatch,
5104 regsubs_T *m,
5105 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005106{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005107 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005108 int save_reglnum = reglnum;
5109 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005110 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005111 save_se_T *save_nfa_endp = nfa_endp;
5112 save_se_T endpos;
5113 save_se_T *endposp = NULL;
5114 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005115 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005116
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005117 if (pim != NULL)
5118 {
5119 /* start at the position where the postponed match was */
5120 if (REG_MULTI)
5121 reginput = regline + pim->end.pos.col;
5122 else
5123 reginput = pim->end.ptr;
5124 }
5125
Bram Moolenaardecd9542013-06-07 16:31:50 +02005126 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005127 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5128 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5129 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005130 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005131 /* The recursive match must end at the current position. When "pim" is
5132 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005133 endposp = &endpos;
5134 if (REG_MULTI)
5135 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005136 if (pim == NULL)
5137 {
5138 endpos.se_u.pos.col = (int)(reginput - regline);
5139 endpos.se_u.pos.lnum = reglnum;
5140 }
5141 else
5142 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005143 }
5144 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005145 {
5146 if (pim == NULL)
5147 endpos.se_u.ptr = reginput;
5148 else
5149 endpos.se_u.ptr = pim->end.ptr;
5150 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151
5152 /* Go back the specified number of bytes, or as far as the
5153 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005154 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005155 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005156 if (state->val <= 0)
5157 {
5158 if (REG_MULTI)
5159 {
5160 regline = reg_getline(--reglnum);
5161 if (regline == NULL)
5162 /* can't go before the first line */
5163 regline = reg_getline(++reglnum);
5164 }
5165 reginput = regline;
5166 }
5167 else
5168 {
5169 if (REG_MULTI && (int)(reginput - regline) < state->val)
5170 {
5171 /* Not enough bytes in this line, go to end of
5172 * previous line. */
5173 regline = reg_getline(--reglnum);
5174 if (regline == NULL)
5175 {
5176 /* can't go before the first line */
5177 regline = reg_getline(++reglnum);
5178 reginput = regline;
5179 }
5180 else
5181 reginput = regline + STRLEN(regline);
5182 }
5183 if ((int)(reginput - regline) >= state->val)
5184 {
5185 reginput -= state->val;
5186#ifdef FEAT_MBYTE
5187 if (has_mbyte)
5188 reginput -= mb_head_off(regline, reginput);
5189#endif
5190 }
5191 else
5192 reginput = regline;
5193 }
5194 }
5195
Bram Moolenaarf46da702013-06-02 22:37:42 +02005196#ifdef ENABLE_LOG
5197 if (log_fd != stderr)
5198 fclose(log_fd);
5199 log_fd = NULL;
5200#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005201 /* Have to clear the lastlist field of the NFA nodes, so that
5202 * nfa_regmatch() and addstate() can run properly after recursion. */
5203 if (nfa_ll_index == 1)
5204 {
5205 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5206 * values and clear them. */
5207 if (*listids == NULL)
5208 {
5209 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5210 if (*listids == NULL)
5211 {
5212 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5213 return 0;
5214 }
5215 }
5216 nfa_save_listids(prog, *listids);
5217 need_restore = TRUE;
5218 /* any value of nfa_listid will do */
5219 }
5220 else
5221 {
5222 /* First recursive nfa_regmatch() call, switch to the second lastlist
5223 * entry. Make sure nfa_listid is different from a previous recursive
5224 * call, because some states may still have this ID. */
5225 ++nfa_ll_index;
5226 if (nfa_listid <= nfa_alt_listid)
5227 nfa_listid = nfa_alt_listid;
5228 }
5229
5230 /* Call nfa_regmatch() to check if the current concat matches at this
5231 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005232 nfa_endp = endposp;
5233 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005234
5235 if (need_restore)
5236 nfa_restore_listids(prog, *listids);
5237 else
5238 {
5239 --nfa_ll_index;
5240 nfa_alt_listid = nfa_listid;
5241 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005242
5243 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005244 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005245 if (REG_MULTI)
5246 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005247 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005248 if (result != NFA_TOO_EXPENSIVE)
5249 {
5250 nfa_match = save_nfa_match;
5251 nfa_listid = save_nfa_listid;
5252 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005253 nfa_endp = save_nfa_endp;
5254
5255#ifdef ENABLE_LOG
5256 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5257 if (log_fd != NULL)
5258 {
5259 fprintf(log_fd, "****************************\n");
5260 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5261 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5262 fprintf(log_fd, "****************************\n");
5263 }
5264 else
5265 {
5266 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5267 log_fd = stderr;
5268 }
5269#endif
5270
5271 return result;
5272}
5273
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005274static int skip_to_start(int c, colnr_T *colp);
5275static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005276
5277/*
5278 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005279 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005280 * NFA_ANY: 1
5281 * specific character: 99
5282 */
5283 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005284failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005285{
5286 int c = state->c;
5287 int l, r;
5288
5289 /* detect looping */
5290 if (depth > 4)
5291 return 1;
5292
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005293 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005294 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005295 case NFA_SPLIT:
5296 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5297 /* avoid recursive stuff */
5298 return 1;
5299 /* two alternatives, use the lowest failure chance */
5300 l = failure_chance(state->out, depth + 1);
5301 r = failure_chance(state->out1, depth + 1);
5302 return l < r ? l : r;
5303
5304 case NFA_ANY:
5305 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005306 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005307
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005308 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005309 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005310 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005311 /* empty match works always */
5312 return 0;
5313
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005314 case NFA_START_INVISIBLE:
5315 case NFA_START_INVISIBLE_FIRST:
5316 case NFA_START_INVISIBLE_NEG:
5317 case NFA_START_INVISIBLE_NEG_FIRST:
5318 case NFA_START_INVISIBLE_BEFORE:
5319 case NFA_START_INVISIBLE_BEFORE_FIRST:
5320 case NFA_START_INVISIBLE_BEFORE_NEG:
5321 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5322 case NFA_START_PATTERN:
5323 /* recursive regmatch is expensive, use low failure chance */
5324 return 5;
5325
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005326 case NFA_BOL:
5327 case NFA_EOL:
5328 case NFA_BOF:
5329 case NFA_EOF:
5330 case NFA_NEWL:
5331 return 99;
5332
5333 case NFA_BOW:
5334 case NFA_EOW:
5335 return 90;
5336
5337 case NFA_MOPEN:
5338 case NFA_MOPEN1:
5339 case NFA_MOPEN2:
5340 case NFA_MOPEN3:
5341 case NFA_MOPEN4:
5342 case NFA_MOPEN5:
5343 case NFA_MOPEN6:
5344 case NFA_MOPEN7:
5345 case NFA_MOPEN8:
5346 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005347#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005348 case NFA_ZOPEN:
5349 case NFA_ZOPEN1:
5350 case NFA_ZOPEN2:
5351 case NFA_ZOPEN3:
5352 case NFA_ZOPEN4:
5353 case NFA_ZOPEN5:
5354 case NFA_ZOPEN6:
5355 case NFA_ZOPEN7:
5356 case NFA_ZOPEN8:
5357 case NFA_ZOPEN9:
5358 case NFA_ZCLOSE:
5359 case NFA_ZCLOSE1:
5360 case NFA_ZCLOSE2:
5361 case NFA_ZCLOSE3:
5362 case NFA_ZCLOSE4:
5363 case NFA_ZCLOSE5:
5364 case NFA_ZCLOSE6:
5365 case NFA_ZCLOSE7:
5366 case NFA_ZCLOSE8:
5367 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005368#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005369 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005370 case NFA_MCLOSE1:
5371 case NFA_MCLOSE2:
5372 case NFA_MCLOSE3:
5373 case NFA_MCLOSE4:
5374 case NFA_MCLOSE5:
5375 case NFA_MCLOSE6:
5376 case NFA_MCLOSE7:
5377 case NFA_MCLOSE8:
5378 case NFA_MCLOSE9:
5379 case NFA_NCLOSE:
5380 return failure_chance(state->out, depth + 1);
5381
5382 case NFA_BACKREF1:
5383 case NFA_BACKREF2:
5384 case NFA_BACKREF3:
5385 case NFA_BACKREF4:
5386 case NFA_BACKREF5:
5387 case NFA_BACKREF6:
5388 case NFA_BACKREF7:
5389 case NFA_BACKREF8:
5390 case NFA_BACKREF9:
5391#ifdef FEAT_SYN_HL
5392 case NFA_ZREF1:
5393 case NFA_ZREF2:
5394 case NFA_ZREF3:
5395 case NFA_ZREF4:
5396 case NFA_ZREF5:
5397 case NFA_ZREF6:
5398 case NFA_ZREF7:
5399 case NFA_ZREF8:
5400 case NFA_ZREF9:
5401#endif
5402 /* backreferences don't match in many places */
5403 return 94;
5404
5405 case NFA_LNUM_GT:
5406 case NFA_LNUM_LT:
5407 case NFA_COL_GT:
5408 case NFA_COL_LT:
5409 case NFA_VCOL_GT:
5410 case NFA_VCOL_LT:
5411 case NFA_MARK_GT:
5412 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005413 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005414 /* before/after positions don't match very often */
5415 return 85;
5416
5417 case NFA_LNUM:
5418 return 90;
5419
5420 case NFA_CURSOR:
5421 case NFA_COL:
5422 case NFA_VCOL:
5423 case NFA_MARK:
5424 /* specific positions rarely match */
5425 return 98;
5426
5427 case NFA_COMPOSING:
5428 return 95;
5429
5430 default:
5431 if (c > 0)
5432 /* character match fails often */
5433 return 95;
5434 }
5435
5436 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005437 return 50;
5438}
5439
Bram Moolenaarf46da702013-06-02 22:37:42 +02005440/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005441 * Skip until the char "c" we know a match must start with.
5442 */
5443 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005444skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005445{
5446 char_u *s;
5447
5448 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005449 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005450#ifdef FEAT_MBYTE
5451 && !has_mbyte
5452#endif
5453 )
5454 s = vim_strbyte(regline + *colp, c);
5455 else
5456 s = cstrchr(regline + *colp, c);
5457 if (s == NULL)
5458 return FAIL;
5459 *colp = (int)(s - regline);
5460 return OK;
5461}
5462
5463/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005464 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005465 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005466 * Returns zero for no match, 1 for a match.
5467 */
5468 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005469find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005470{
5471 colnr_T col = startcol;
5472 int c1, c2;
5473 int len1, len2;
5474 int match;
5475
5476 for (;;)
5477 {
5478 match = TRUE;
5479 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5480 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5481 {
5482 c1 = PTR2CHAR(match_text + len1);
5483 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005484 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005485 {
5486 match = FALSE;
5487 break;
5488 }
5489 len2 += MB_CHAR2LEN(c2);
5490 }
5491 if (match
5492#ifdef FEAT_MBYTE
5493 /* check that no composing char follows */
5494 && !(enc_utf8
5495 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5496#endif
5497 )
5498 {
5499 cleanup_subexpr();
5500 if (REG_MULTI)
5501 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005502 rex.reg_startpos[0].lnum = reglnum;
5503 rex.reg_startpos[0].col = col;
5504 rex.reg_endpos[0].lnum = reglnum;
5505 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005506 }
5507 else
5508 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005509 rex.reg_startp[0] = regline + col;
5510 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005511 }
5512 return 1L;
5513 }
5514
5515 /* Try finding regstart after the current match. */
5516 col += MB_CHAR2LEN(regstart); /* skip regstart */
5517 if (skip_to_start(regstart, &col) == FAIL)
5518 break;
5519 }
5520 return 0L;
5521}
5522
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005523#ifdef FEAT_RELTIME
5524 static int
5525nfa_did_time_out()
5526{
5527 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5528 {
5529 if (nfa_timed_out != NULL)
5530 *nfa_timed_out = TRUE;
5531 return TRUE;
5532 }
5533 return FALSE;
5534}
5535#endif
5536
Bram Moolenaar473de612013-06-08 18:19:48 +02005537/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538 * Main matching routine.
5539 *
5540 * Run NFA to determine whether it matches reginput.
5541 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005542 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005543 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005545 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546 * Note: Caller must ensure that: start != NULL.
5547 */
5548 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005549nfa_regmatch(
5550 nfa_regprog_T *prog,
5551 nfa_state_T *start,
5552 regsubs_T *submatch,
5553 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005554{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005555 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005556 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005558 int go_to_nextline = FALSE;
5559 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005560 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005561 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005562 nfa_list_T *thislist;
5563 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005565 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005566 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005567 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005568 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005569 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005570#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005571 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005573
Bram Moolenaar41f12052013-08-25 17:01:42 +02005574 /* Some patterns may take a long time to match, especially when using
5575 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5576 fast_breakcheck();
5577 if (got_int)
5578 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005579#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005580 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005581 return FALSE;
5582#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005583
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005584#ifdef NFA_REGEXP_DEBUG_LOG
5585 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5586 if (debug == NULL)
5587 {
5588 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5589 return FALSE;
5590 }
5591#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005592 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005593
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005594 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005595 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005596
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005597 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005598 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005599 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005600 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005601 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005602 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005603
5604#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005605 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005606 if (log_fd != NULL)
5607 {
5608 fprintf(log_fd, "**********************************\n");
5609 nfa_set_code(start->c);
5610 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5611 abs(start->id), code);
5612 fprintf(log_fd, "**********************************\n");
5613 }
5614 else
5615 {
5616 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5617 log_fd = stderr;
5618 }
5619#endif
5620
5621 thislist = &list[0];
5622 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005623 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624 nextlist = &list[1];
5625 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005626 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005627#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005628 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005629#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005630 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005631
5632 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5633 * it's the first MOPEN. */
5634 if (toplevel)
5635 {
5636 if (REG_MULTI)
5637 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005638 m->norm.list.multi[0].start_lnum = reglnum;
5639 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005640 }
5641 else
5642 m->norm.list.line[0].start = reginput;
5643 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005644 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005645 }
5646 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005647 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005648
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005649#define ADD_STATE_IF_MATCH(state) \
5650 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005651 add_state = state->out; \
5652 add_off = clen; \
5653 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005654
5655 /*
5656 * Run for each character.
5657 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005658 for (;;)
5659 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005660 int curc;
5661 int clen;
5662
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005663#ifdef FEAT_MBYTE
5664 if (has_mbyte)
5665 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005666 curc = (*mb_ptr2char)(reginput);
5667 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005668 }
5669 else
5670#endif
5671 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 curc = *reginput;
5673 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005675 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005676 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005677 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005678 go_to_nextline = FALSE;
5679 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680
5681 /* swap lists */
5682 thislist = &list[flag];
5683 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005684 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005685 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005686 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005687 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5688 {
5689 /* too many states, retry with old engine */
5690 nfa_match = NFA_TOO_EXPENSIVE;
5691 goto theend;
5692 }
5693
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005694 thislist->id = nfa_listid;
5695 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696
5697#ifdef ENABLE_LOG
5698 fprintf(log_fd, "------------------------------------------\n");
5699 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005700 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005702 {
5703 int i;
5704
5705 for (i = 0; i < thislist->n; i++)
5706 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5707 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005708 fprintf(log_fd, "\n");
5709#endif
5710
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005711#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005712 fprintf(debug, "\n-------------------\n");
5713#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005714 /*
5715 * If the state lists are empty we can stop.
5716 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005717 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005718 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005719
5720 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005721 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005722 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005723 /* If the list gets very long there probably is something wrong.
5724 * At least allow interrupting with CTRL-C. */
5725 fast_breakcheck();
5726 if (got_int)
5727 break;
5728#ifdef FEAT_RELTIME
5729 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5730 {
5731 nfa_time_count = 0;
5732 if (nfa_did_time_out())
5733 break;
5734 }
5735#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005736 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005737
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005738#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005739 nfa_set_code(t->state->c);
5740 fprintf(debug, "%s, ", code);
5741#endif
5742#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005743 {
5744 int col;
5745
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005746 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005747 col = -1;
5748 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005749 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005750 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005751 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005752 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005753 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5754 abs(t->state->id), (int)t->state->c, code, col,
5755 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005756 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005757#endif
5758
5759 /*
5760 * Handle the possible codes of the current state.
5761 * The most important is NFA_MATCH.
5762 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005763 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005764 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005765 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005766 switch (t->state->c)
5767 {
5768 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005769 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005770#ifdef FEAT_MBYTE
5771 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005772 * rex.reg_icombine is not set, that is not really a match. */
5773 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005774 break;
5775#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005776 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005777 copy_sub(&submatch->norm, &t->subs.norm);
5778#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005779 if (nfa_has_zsubexpr)
5780 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005781#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005782#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005783 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005784#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005785 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005786 * states at this position. When the list of states is going
5787 * to be empty quit without advancing, so that "reginput" is
5788 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005789 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005790 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005791 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005792 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005793
5794 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005795 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005796 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005797 /*
5798 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005799 * NFA_START_INVISIBLE_BEFORE node.
5800 * They surround a zero-width group, used with "\@=", "\&",
5801 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005802 * If we got here, it means that the current "invisible" group
5803 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005804 * nfa_regmatch(). For a look-behind match only when it ends
5805 * in the position in "nfa_endp".
5806 * Submatches are stored in *m, and used in the parent call.
5807 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005808#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005809 if (nfa_endp != NULL)
5810 {
5811 if (REG_MULTI)
5812 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5813 (int)reglnum,
5814 (int)nfa_endp->se_u.pos.lnum,
5815 (int)(reginput - regline),
5816 nfa_endp->se_u.pos.col);
5817 else
5818 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5819 (int)(reginput - regline),
5820 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005821 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005822#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005823 /* If "nfa_endp" is set it's only a match if it ends at
5824 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005825 if (nfa_endp != NULL && (REG_MULTI
5826 ? (reglnum != nfa_endp->se_u.pos.lnum
5827 || (int)(reginput - regline)
5828 != nfa_endp->se_u.pos.col)
5829 : reginput != nfa_endp->se_u.ptr))
5830 break;
5831
5832 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005833 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005834 {
5835 copy_sub(&m->norm, &t->subs.norm);
5836#ifdef FEAT_SYN_HL
5837 if (nfa_has_zsubexpr)
5838 copy_sub(&m->synt, &t->subs.synt);
5839#endif
5840 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005841#ifdef ENABLE_LOG
5842 fprintf(log_fd, "Match found:\n");
5843 log_subsexpr(m);
5844#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005845 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005846 /* See comment above at "goto nextchar". */
5847 if (nextlist->n == 0)
5848 clen = 0;
5849 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005850
5851 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005852 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005853 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005854 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005855 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005856 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005857 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005858 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005859 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005860#ifdef ENABLE_LOG
5861 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5862 failure_chance(t->state->out, 0),
5863 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005864#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005865 /* Do it directly if there already is a PIM or when
5866 * nfa_postprocess() detected it will work better. */
5867 if (t->pim.result != NFA_PIM_UNUSED
5868 || t->state->c == NFA_START_INVISIBLE_FIRST
5869 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5870 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5871 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005872 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005873 int in_use = m->norm.in_use;
5874
Bram Moolenaar699c1202013-09-25 16:41:54 +02005875 /* Copy submatch info for the recursive call, opposite
5876 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005877 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005878#ifdef FEAT_SYN_HL
5879 if (nfa_has_zsubexpr)
5880 copy_sub_off(&m->synt, &t->subs.synt);
5881#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005882
Bram Moolenaara2d95102013-06-04 14:23:05 +02005883 /*
5884 * First try matching the invisible match, then what
5885 * follows.
5886 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005887 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005888 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005889 if (result == NFA_TOO_EXPENSIVE)
5890 {
5891 nfa_match = result;
5892 goto theend;
5893 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005894
Bram Moolenaardecd9542013-06-07 16:31:50 +02005895 /* for \@! and \@<! it is a match when the result is
5896 * FALSE */
5897 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005898 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5899 || t->state->c
5900 == NFA_START_INVISIBLE_BEFORE_NEG
5901 || t->state->c
5902 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005903 {
5904 /* Copy submatch info from the recursive call */
5905 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005906#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005907 if (nfa_has_zsubexpr)
5908 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005909#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005910 /* If the pattern has \ze and it matched in the
5911 * sub pattern, use it. */
5912 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005913
Bram Moolenaara2d95102013-06-04 14:23:05 +02005914 /* t->state->out1 is the corresponding
5915 * END_INVISIBLE node; Add its out to the current
5916 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005917 add_here = TRUE;
5918 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005919 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005920 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005921 }
5922 else
5923 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005924 nfa_pim_T pim;
5925
Bram Moolenaara2d95102013-06-04 14:23:05 +02005926 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005927 * First try matching what follows. Only if a match
5928 * is found verify the invisible match matches. Add a
5929 * nfa_pim_T to the following states, it contains info
5930 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005931 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005932 pim.state = t->state;
5933 pim.result = NFA_PIM_TODO;
5934 pim.subs.norm.in_use = 0;
5935#ifdef FEAT_SYN_HL
5936 pim.subs.synt.in_use = 0;
5937#endif
5938 if (REG_MULTI)
5939 {
5940 pim.end.pos.col = (int)(reginput - regline);
5941 pim.end.pos.lnum = reglnum;
5942 }
5943 else
5944 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005945
5946 /* t->state->out1 is the corresponding END_INVISIBLE
5947 * node; Add its out to the current list (zero-width
5948 * match). */
5949 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005950 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005951 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005952 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005953 break;
5954
Bram Moolenaar87953742013-06-05 18:52:40 +02005955 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005956 {
5957 nfa_state_T *skip = NULL;
5958#ifdef ENABLE_LOG
5959 int skip_lid = 0;
5960#endif
5961
5962 /* There is no point in trying to match the pattern if the
5963 * output state is not going to be added to the list. */
5964 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5965 {
5966 skip = t->state->out1->out;
5967#ifdef ENABLE_LOG
5968 skip_lid = nextlist->id;
5969#endif
5970 }
5971 else if (state_in_list(nextlist,
5972 t->state->out1->out->out, &t->subs))
5973 {
5974 skip = t->state->out1->out->out;
5975#ifdef ENABLE_LOG
5976 skip_lid = nextlist->id;
5977#endif
5978 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005979 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005980 t->state->out1->out->out, &t->subs))
5981 {
5982 skip = t->state->out1->out->out;
5983#ifdef ENABLE_LOG
5984 skip_lid = thislist->id;
5985#endif
5986 }
5987 if (skip != NULL)
5988 {
5989#ifdef ENABLE_LOG
5990 nfa_set_code(skip->c);
5991 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5992 abs(skip->id), skip_lid, skip->c, code);
5993#endif
5994 break;
5995 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005996 /* Copy submatch info to the recursive call, opposite of what
5997 * happens afterwards. */
5998 copy_sub_off(&m->norm, &t->subs.norm);
5999#ifdef FEAT_SYN_HL
6000 if (nfa_has_zsubexpr)
6001 copy_sub_off(&m->synt, &t->subs.synt);
6002#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02006003
Bram Moolenaar87953742013-06-05 18:52:40 +02006004 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006005 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02006006 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006007 if (result == NFA_TOO_EXPENSIVE)
6008 {
6009 nfa_match = result;
6010 goto theend;
6011 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006012 if (result)
6013 {
6014 int bytelen;
6015
6016#ifdef ENABLE_LOG
6017 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6018 log_subsexpr(m);
6019#endif
6020 /* Copy submatch info from the recursive call */
6021 copy_sub_off(&t->subs.norm, &m->norm);
6022#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006023 if (nfa_has_zsubexpr)
6024 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006025#endif
6026 /* Now we need to skip over the matched text and then
6027 * continue with what follows. */
6028 if (REG_MULTI)
6029 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006030 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02006031 - (int)(reginput - regline);
6032 else
6033 bytelen = (int)(m->norm.list.line[0].end - reginput);
6034
6035#ifdef ENABLE_LOG
6036 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6037#endif
6038 if (bytelen == 0)
6039 {
6040 /* empty match, output of corresponding
6041 * NFA_END_PATTERN/NFA_SKIP to be used at current
6042 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006043 add_here = TRUE;
6044 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006045 }
6046 else if (bytelen <= clen)
6047 {
6048 /* match current character, output of corresponding
6049 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006050 add_state = t->state->out1->out->out;
6051 add_off = clen;
6052 }
6053 else
6054 {
6055 /* skip over the matched characters, set character
6056 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006057 add_state = t->state->out1->out;
6058 add_off = bytelen;
6059 add_count = bytelen - clen;
6060 }
6061 }
6062 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006063 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006064
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006065 case NFA_BOL:
6066 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006067 {
6068 add_here = TRUE;
6069 add_state = t->state->out;
6070 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006071 break;
6072
6073 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006074 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006075 {
6076 add_here = TRUE;
6077 add_state = t->state->out;
6078 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006079 break;
6080
6081 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006082 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006083
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006084 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006085 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086#ifdef FEAT_MBYTE
6087 else if (has_mbyte)
6088 {
6089 int this_class;
6090
6091 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006092 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006094 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006095 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006096 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097 }
6098#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006099 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006100 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006101 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006102 result = FALSE;
6103 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006104 {
6105 add_here = TRUE;
6106 add_state = t->state->out;
6107 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006108 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006109
6110 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006111 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006112 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006113 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006114#ifdef FEAT_MBYTE
6115 else if (has_mbyte)
6116 {
6117 int this_class, prev_class;
6118
6119 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006120 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006121 prev_class = reg_prev_class();
6122 if (this_class == prev_class
6123 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006124 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 }
6126#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006127 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006128 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006129 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006130 result = FALSE;
6131 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006132 {
6133 add_here = TRUE;
6134 add_state = t->state->out;
6135 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006136 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006137
Bram Moolenaar4b780632013-05-31 22:14:52 +02006138 case NFA_BOF:
6139 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006140 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006141 {
6142 add_here = TRUE;
6143 add_state = t->state->out;
6144 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006145 break;
6146
6147 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006148 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006149 {
6150 add_here = TRUE;
6151 add_state = t->state->out;
6152 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006153 break;
6154
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006155#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006156 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006157 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006158 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006159 int len = 0;
6160 nfa_state_T *end;
6161 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006162 int cchars[MAX_MCO];
6163 int ccount = 0;
6164 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006165
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006166 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006167 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006168 if (utf_iscomposing(sta->c))
6169 {
6170 /* Only match composing character(s), ignore base
6171 * character. Used for ".{composing}" and "{composing}"
6172 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006173 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006174 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006175 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006176 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006177 /* If \Z was present, then ignore composing characters.
6178 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006179 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006180 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006181 else
6182 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006183 while (sta->c != NFA_END_COMPOSING)
6184 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006185 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006186
6187 /* Check base character matches first, unless ignored. */
6188 else if (len > 0 || mc == sta->c)
6189 {
6190 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006191 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006192 len += mb_char2len(mc);
6193 sta = sta->out;
6194 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006195
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006196 /* We don't care about the order of composing characters.
6197 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006198 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006199 {
6200 mc = mb_ptr2char(reginput + len);
6201 cchars[ccount++] = mc;
6202 len += mb_char2len(mc);
6203 if (ccount == MAX_MCO)
6204 break;
6205 }
6206
6207 /* Check that each composing char in the pattern matches a
6208 * composing char in the text. We do not check if all
6209 * composing chars are matched. */
6210 result = OK;
6211 while (sta->c != NFA_END_COMPOSING)
6212 {
6213 for (j = 0; j < ccount; ++j)
6214 if (cchars[j] == sta->c)
6215 break;
6216 if (j == ccount)
6217 {
6218 result = FAIL;
6219 break;
6220 }
6221 sta = sta->out;
6222 }
6223 }
6224 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006225 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006226
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006227 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006228 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006229 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006230 }
6231#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006232
6233 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006234 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6235 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006236 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006237 go_to_nextline = TRUE;
6238 /* Pass -1 for the offset, which means taking the position
6239 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006240 add_state = t->state->out;
6241 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006242 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006243 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006244 {
6245 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006246 add_state = t->state->out;
6247 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006248 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006249 break;
6250
Bram Moolenaar417bad22013-06-07 14:08:30 +02006251 case NFA_START_COLL:
6252 case NFA_START_NEG_COLL:
6253 {
6254 /* What follows is a list of characters, until NFA_END_COLL.
6255 * One of them must match or none of them must match. */
6256 nfa_state_T *state;
6257 int result_if_matched;
6258 int c1, c2;
6259
6260 /* Never match EOL. If it's part of the collection it is added
6261 * as a separate state with an OR. */
6262 if (curc == NUL)
6263 break;
6264
6265 state = t->state->out;
6266 result_if_matched = (t->state->c == NFA_START_COLL);
6267 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006268 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006269 if (state->c == NFA_END_COLL)
6270 {
6271 result = !result_if_matched;
6272 break;
6273 }
6274 if (state->c == NFA_RANGE_MIN)
6275 {
6276 c1 = state->val;
6277 state = state->out; /* advance to NFA_RANGE_MAX */
6278 c2 = state->val;
6279#ifdef ENABLE_LOG
6280 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6281 curc, c1, c2);
6282#endif
6283 if (curc >= c1 && curc <= c2)
6284 {
6285 result = result_if_matched;
6286 break;
6287 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006288 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006289 {
6290 int curc_low = MB_TOLOWER(curc);
6291 int done = FALSE;
6292
6293 for ( ; c1 <= c2; ++c1)
6294 if (MB_TOLOWER(c1) == curc_low)
6295 {
6296 result = result_if_matched;
6297 done = TRUE;
6298 break;
6299 }
6300 if (done)
6301 break;
6302 }
6303 }
6304 else if (state->c < 0 ? check_char_class(state->c, curc)
6305 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006306 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006307 == MB_TOLOWER(state->c))))
6308 {
6309 result = result_if_matched;
6310 break;
6311 }
6312 state = state->out;
6313 }
6314 if (result)
6315 {
6316 /* next state is in out of the NFA_END_COLL, out1 of
6317 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006318 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006319 add_off = clen;
6320 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006321 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006322 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006323
6324 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006325 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006326 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006327 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006328 add_state = t->state->out;
6329 add_off = clen;
6330 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006331 break;
6332
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006333 case NFA_ANY_COMPOSING:
6334 /* On a composing character skip over it. Otherwise do
6335 * nothing. Always matches. */
6336#ifdef FEAT_MBYTE
6337 if (enc_utf8 && utf_iscomposing(curc))
6338 {
6339 add_off = clen;
6340 }
6341 else
6342#endif
6343 {
6344 add_here = TRUE;
6345 add_off = 0;
6346 }
6347 add_state = t->state->out;
6348 break;
6349
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006350 /*
6351 * Character classes like \a for alpha, \d for digit etc.
6352 */
6353 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006354 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006355 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006356 break;
6357
6358 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006359 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006360 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006361 break;
6362
6363 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006364 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006365 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 break;
6367
6368 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006369 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006370 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006371 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006372 break;
6373
6374 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006375 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006376 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006377 break;
6378
6379 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006380 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006381 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006382 break;
6383
6384 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006385 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006386 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006387 break;
6388
6389 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006390 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006391 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006392 break;
6393
6394 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006395 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006396 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006397 break;
6398
6399 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006400 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006401 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006402 break;
6403
6404 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006405 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006406 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006407 break;
6408
6409 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006410 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006411 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006412 break;
6413
6414 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006415 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006416 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006417 break;
6418
6419 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006420 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006421 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006422 break;
6423
6424 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006425 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006426 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006427 break;
6428
6429 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006430 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006431 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006432 break;
6433
6434 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006435 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006436 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006437 break;
6438
6439 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006440 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006441 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006442 break;
6443
6444 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006445 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006446 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006447 break;
6448
6449 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006450 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006451 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006452 break;
6453
6454 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006455 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006456 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006457 break;
6458
6459 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006460 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006461 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006462 break;
6463
6464 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006465 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006466 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006467 break;
6468
6469 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006470 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006471 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006472 break;
6473
6474 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006475 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006476 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006477 break;
6478
6479 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006480 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006481 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006482 break;
6483
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006484 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006485 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006486 ADD_STATE_IF_MATCH(t->state);
6487 break;
6488
6489 case NFA_NLOWER_IC: /* [^a-z] */
6490 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006491 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006492 ADD_STATE_IF_MATCH(t->state);
6493 break;
6494
6495 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006496 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006497 ADD_STATE_IF_MATCH(t->state);
6498 break;
6499
6500 case NFA_NUPPER_IC: /* ^[A-Z] */
6501 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006502 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006503 ADD_STATE_IF_MATCH(t->state);
6504 break;
6505
Bram Moolenaar5714b802013-05-28 22:03:20 +02006506 case NFA_BACKREF1:
6507 case NFA_BACKREF2:
6508 case NFA_BACKREF3:
6509 case NFA_BACKREF4:
6510 case NFA_BACKREF5:
6511 case NFA_BACKREF6:
6512 case NFA_BACKREF7:
6513 case NFA_BACKREF8:
6514 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006515#ifdef FEAT_SYN_HL
6516 case NFA_ZREF1:
6517 case NFA_ZREF2:
6518 case NFA_ZREF3:
6519 case NFA_ZREF4:
6520 case NFA_ZREF5:
6521 case NFA_ZREF6:
6522 case NFA_ZREF7:
6523 case NFA_ZREF8:
6524 case NFA_ZREF9:
6525#endif
6526 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006527 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006528 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006529 int bytelen;
6530
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006531 if (t->state->c <= NFA_BACKREF9)
6532 {
6533 subidx = t->state->c - NFA_BACKREF1 + 1;
6534 result = match_backref(&t->subs.norm, subidx, &bytelen);
6535 }
6536#ifdef FEAT_SYN_HL
6537 else
6538 {
6539 subidx = t->state->c - NFA_ZREF1 + 1;
6540 result = match_zref(subidx, &bytelen);
6541 }
6542#endif
6543
Bram Moolenaar5714b802013-05-28 22:03:20 +02006544 if (result)
6545 {
6546 if (bytelen == 0)
6547 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006548 /* empty match always works, output of NFA_SKIP to be
6549 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006550 add_here = TRUE;
6551 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006552 }
6553 else if (bytelen <= clen)
6554 {
6555 /* match current character, jump ahead to out of
6556 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006557 add_state = t->state->out->out;
6558 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006559 }
6560 else
6561 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006562 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006563 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006564 add_state = t->state->out;
6565 add_off = bytelen;
6566 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006567 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006568 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006569 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006570 }
6571 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006572 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006573 if (t->count - clen <= 0)
6574 {
6575 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006576 add_state = t->state->out;
6577 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006578 }
6579 else
6580 {
6581 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006582 add_state = t->state;
6583 add_off = 0;
6584 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006585 }
6586 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006587
Bram Moolenaar423532e2013-05-29 21:14:42 +02006588 case NFA_LNUM:
6589 case NFA_LNUM_GT:
6590 case NFA_LNUM_LT:
6591 result = (REG_MULTI &&
6592 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006593 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006594 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006595 {
6596 add_here = TRUE;
6597 add_state = t->state->out;
6598 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006599 break;
6600
6601 case NFA_COL:
6602 case NFA_COL_GT:
6603 case NFA_COL_LT:
6604 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6605 (long_u)(reginput - regline) + 1);
6606 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006607 {
6608 add_here = TRUE;
6609 add_state = t->state->out;
6610 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006611 break;
6612
6613 case NFA_VCOL:
6614 case NFA_VCOL_GT:
6615 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006616 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006617 int op = t->state->c - NFA_VCOL;
6618 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006619 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006620
6621 /* Bail out quickly when there can't be a match, avoid the
6622 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006623 if (op != 1 && col > t->state->val
6624#ifdef FEAT_MBYTE
6625 * (has_mbyte ? MB_MAXBYTES : 1)
6626#endif
6627 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006628 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006629 result = FALSE;
6630 if (op == 1 && col - 1 > t->state->val && col > 100)
6631 {
6632 int ts = wp->w_buffer->b_p_ts;
6633
6634 /* Guess that a character won't use more columns than
6635 * 'tabstop', with a minimum of 4. */
6636 if (ts < 4)
6637 ts = 4;
6638 result = col > t->state->val * ts;
6639 }
6640 if (!result)
6641 result = nfa_re_num_cmp(t->state->val, op,
6642 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006643 if (result)
6644 {
6645 add_here = TRUE;
6646 add_state = t->state->out;
6647 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006648 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006649 break;
6650
Bram Moolenaar044aa292013-06-04 21:27:38 +02006651 case NFA_MARK:
6652 case NFA_MARK_GT:
6653 case NFA_MARK_LT:
6654 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006655 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006656
6657 /* Compare the mark position to the match position. */
6658 result = (pos != NULL /* mark doesn't exist */
6659 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006660 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006661 ? (pos->col == (colnr_T)(reginput - regline)
6662 ? t->state->c == NFA_MARK
6663 : (pos->col < (colnr_T)(reginput - regline)
6664 ? t->state->c == NFA_MARK_GT
6665 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006666 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006667 ? t->state->c == NFA_MARK_GT
6668 : t->state->c == NFA_MARK_LT)));
6669 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006670 {
6671 add_here = TRUE;
6672 add_state = t->state->out;
6673 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006674 break;
6675 }
6676
Bram Moolenaar423532e2013-05-29 21:14:42 +02006677 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006678 result = (rex.reg_win != NULL
6679 && (reglnum + rex.reg_firstlnum
6680 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006681 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006682 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006683 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006684 {
6685 add_here = TRUE;
6686 add_state = t->state->out;
6687 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006688 break;
6689
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006690 case NFA_VISUAL:
6691 result = reg_match_visual();
6692 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006693 {
6694 add_here = TRUE;
6695 add_state = t->state->out;
6696 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006697 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006698
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006699 case NFA_MOPEN1:
6700 case NFA_MOPEN2:
6701 case NFA_MOPEN3:
6702 case NFA_MOPEN4:
6703 case NFA_MOPEN5:
6704 case NFA_MOPEN6:
6705 case NFA_MOPEN7:
6706 case NFA_MOPEN8:
6707 case NFA_MOPEN9:
6708#ifdef FEAT_SYN_HL
6709 case NFA_ZOPEN:
6710 case NFA_ZOPEN1:
6711 case NFA_ZOPEN2:
6712 case NFA_ZOPEN3:
6713 case NFA_ZOPEN4:
6714 case NFA_ZOPEN5:
6715 case NFA_ZOPEN6:
6716 case NFA_ZOPEN7:
6717 case NFA_ZOPEN8:
6718 case NFA_ZOPEN9:
6719#endif
6720 case NFA_NOPEN:
6721 case NFA_ZSTART:
6722 /* These states are only added to be able to bail out when
6723 * they are added again, nothing is to be done. */
6724 break;
6725
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006726 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006727 {
6728 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006729
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006730#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006731 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006732 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006733#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006734 result = (c == curc);
6735
Bram Moolenaar6100d022016-10-02 16:51:57 +02006736 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006737 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006738#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006739 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006740 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006741 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006742 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006743#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006744 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006745 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006746 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006747
6748 } /* switch (t->state->c) */
6749
6750 if (add_state != NULL)
6751 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006752 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006753 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006754
6755 if (t->pim.result == NFA_PIM_UNUSED)
6756 pim = NULL;
6757 else
6758 pim = &t->pim;
6759
6760 /* Handle the postponed invisible match if the match might end
6761 * without advancing and before the end of the line. */
6762 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006763 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006764 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006765 {
6766#ifdef ENABLE_LOG
6767 fprintf(log_fd, "\n");
6768 fprintf(log_fd, "==================================\n");
6769 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6770 fprintf(log_fd, "\n");
6771#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006772 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006773 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006774 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006775 /* for \@! and \@<! it is a match when the result is
6776 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006777 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006778 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6779 || pim->state->c
6780 == NFA_START_INVISIBLE_BEFORE_NEG
6781 || pim->state->c
6782 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006783 {
6784 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006785 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006786#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006787 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006788 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006789#endif
6790 }
6791 }
6792 else
6793 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006794 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006795#ifdef ENABLE_LOG
6796 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006797 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006798 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6799 fprintf(log_fd, "\n");
6800#endif
6801 }
6802
Bram Moolenaardecd9542013-06-07 16:31:50 +02006803 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006804 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006805 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6806 || pim->state->c
6807 == NFA_START_INVISIBLE_BEFORE_NEG
6808 || pim->state->c
6809 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006810 {
6811 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006812 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006813#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006814 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006815 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006816#endif
6817 }
6818 else
6819 /* look-behind match failed, don't add the state */
6820 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006821
6822 /* Postponed invisible match was handled, don't add it to
6823 * following states. */
6824 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006825 }
6826
Bram Moolenaara951e352013-10-06 15:46:11 +02006827 /* If "pim" points into l->t it will become invalid when
6828 * adding the state causes the list to be reallocated. Make a
6829 * local copy to avoid that. */
6830 if (pim == &t->pim)
6831 {
6832 copy_pim(&pim_copy, pim);
6833 pim = &pim_copy;
6834 }
6835
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006836 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006837 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006838 else
6839 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006840 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006841 if (add_count > 0)
6842 nextlist->t[nextlist->n - 1].count = add_count;
6843 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006844 }
6845
6846 } /* for (thislist = thislist; thislist->state; thislist++) */
6847
Bram Moolenaare23febd2013-05-26 18:40:14 +02006848 /* Look for the start of a match in the current position by adding the
6849 * start state to the list of states.
6850 * The first found match is the leftmost one, thus the order of states
6851 * matters!
6852 * Do not add the start state in recursive calls of nfa_regmatch(),
6853 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006854 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006855 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006856 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006857 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006858 && reglnum == 0
6859 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006860 && (rex.reg_maxcol == 0
6861 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006862 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006863 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006864 ? (reglnum < nfa_endp->se_u.pos.lnum
6865 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006866 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006867 < nfa_endp->se_u.pos.col))
6868 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006869 {
6870#ifdef ENABLE_LOG
6871 fprintf(log_fd, "(---) STARTSTATE\n");
6872#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006873 /* Inline optimized code for addstate() if we know the state is
6874 * the first MOPEN. */
6875 if (toplevel)
6876 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006877 int add = TRUE;
6878 int c;
6879
6880 if (prog->regstart != NUL && clen != 0)
6881 {
6882 if (nextlist->n == 0)
6883 {
6884 colnr_T col = (colnr_T)(reginput - regline) + clen;
6885
6886 /* Nextlist is empty, we can skip ahead to the
6887 * character that must appear at the start. */
6888 if (skip_to_start(prog->regstart, &col) == FAIL)
6889 break;
6890#ifdef ENABLE_LOG
6891 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6892 col - ((colnr_T)(reginput - regline) + clen));
6893#endif
6894 reginput = regline + col - clen;
6895 }
6896 else
6897 {
6898 /* Checking if the required start character matches is
6899 * cheaper than adding a state that won't match. */
6900 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006901 if (c != prog->regstart && (!rex.reg_ic
6902 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006903 {
6904#ifdef ENABLE_LOG
6905 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6906#endif
6907 add = FALSE;
6908 }
6909 }
6910 }
6911
6912 if (add)
6913 {
6914 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006915 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006916 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006917 else
6918 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006919 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006920 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006921 }
6922 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006923 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006924 }
6925
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006926#ifdef ENABLE_LOG
6927 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006928 {
6929 int i;
6930
6931 for (i = 0; i < thislist->n; i++)
6932 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6933 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006934 fprintf(log_fd, "\n");
6935#endif
6936
6937nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006938 /* Advance to the next character, or advance to the next line, or
6939 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006940 if (clen != 0)
6941 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006942 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6943 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006944 reg_nextline();
6945 else
6946 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006947
6948 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006949 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006950 if (got_int)
6951 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006952#ifdef FEAT_RELTIME
6953 /* Check for timeout once in a twenty times to avoid overhead. */
6954 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6955 {
6956 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006957 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006958 break;
6959 }
6960#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006961 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006962
6963#ifdef ENABLE_LOG
6964 if (log_fd != stderr)
6965 fclose(log_fd);
6966 log_fd = NULL;
6967#endif
6968
6969theend:
6970 /* Free memory */
6971 vim_free(list[0].t);
6972 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006973 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006974#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006975#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006976 fclose(debug);
6977#endif
6978
Bram Moolenaar963fee22013-05-26 21:47:28 +02006979 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006980}
6981
6982/*
6983 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006984 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006985 */
6986 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006987nfa_regtry(
6988 nfa_regprog_T *prog,
6989 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006990 proftime_T *tm UNUSED, /* timeout limit or NULL */
6991 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992{
6993 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006994 regsubs_T subs, m;
6995 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006996 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997#ifdef ENABLE_LOG
6998 FILE *f;
6999#endif
7000
7001 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007002#ifdef FEAT_RELTIME
7003 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007004 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007005 nfa_time_count = 0;
7006#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007007
7008#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007009 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007010 if (f != NULL)
7011 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007012 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007013#ifdef DEBUG
7014 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7015#endif
7016 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02007017 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007018 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007019 fprintf(f, "\n\n");
7020 fclose(f);
7021 }
7022 else
7023 EMSG(_("Could not open temporary log file for writing "));
7024#endif
7025
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007026 clear_sub(&subs.norm);
7027 clear_sub(&m.norm);
7028#ifdef FEAT_SYN_HL
7029 clear_sub(&subs.synt);
7030 clear_sub(&m.synt);
7031#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007032
Bram Moolenaarfda37292014-11-05 14:27:36 +01007033 result = nfa_regmatch(prog, start, &subs, &m);
7034 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007035 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007036 else if (result == NFA_TOO_EXPENSIVE)
7037 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007038
7039 cleanup_subexpr();
7040 if (REG_MULTI)
7041 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007042 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007043 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007044 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7045 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007046
Bram Moolenaar6100d022016-10-02 16:51:57 +02007047 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7048 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007049 }
7050
Bram Moolenaar6100d022016-10-02 16:51:57 +02007051 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007052 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007053 rex.reg_startpos[0].lnum = 0;
7054 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007055 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007056 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007057 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007058 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007059 rex.reg_endpos[0].lnum = reglnum;
7060 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007061 }
7062 else
7063 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007064 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007065 }
7066 else
7067 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007068 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007069 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007070 rex.reg_startp[i] = subs.norm.list.line[i].start;
7071 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007072 }
7073
Bram Moolenaar6100d022016-10-02 16:51:57 +02007074 if (rex.reg_startp[0] == NULL)
7075 rex.reg_startp[0] = regline + col;
7076 if (rex.reg_endp[0] == NULL)
7077 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007078 }
7079
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007080#ifdef FEAT_SYN_HL
7081 /* Package any found \z(...\) matches for export. Default is none. */
7082 unref_extmatch(re_extmatch_out);
7083 re_extmatch_out = NULL;
7084
7085 if (prog->reghasz == REX_SET)
7086 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007087 cleanup_zsubexpr();
7088 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007089 /* Loop over \z1, \z2, etc. There is no \z0. */
7090 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007091 {
7092 if (REG_MULTI)
7093 {
7094 struct multipos *mpos = &subs.synt.list.multi[i];
7095
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007096 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007097 if (mpos->start_lnum >= 0
7098 && mpos->start_lnum == mpos->end_lnum
7099 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007100 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007101 vim_strnsave(reg_getline(mpos->start_lnum)
7102 + mpos->start_col,
7103 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007104 }
7105 else
7106 {
7107 struct linepos *lpos = &subs.synt.list.line[i];
7108
7109 if (lpos->start != NULL && lpos->end != NULL)
7110 re_extmatch_out->matches[i] =
7111 vim_strnsave(lpos->start,
7112 (int)(lpos->end - lpos->start));
7113 }
7114 }
7115 }
7116#endif
7117
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007118 return 1 + reglnum;
7119}
7120
7121/*
7122 * Match a regexp against a string ("line" points to the string) or multiple
7123 * lines ("line" is NULL, use reg_getline()).
7124 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007125 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007126 */
7127 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007128nfa_regexec_both(
7129 char_u *line,
7130 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007131 proftime_T *tm, /* timeout limit or NULL */
7132 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007133{
7134 nfa_regprog_T *prog;
7135 long retval = 0L;
7136 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007137 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007138
7139 if (REG_MULTI)
7140 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007141 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007142 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007143 rex.reg_startpos = rex.reg_mmatch->startpos;
7144 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007145 }
7146 else
7147 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007148 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7149 rex.reg_startp = rex.reg_match->startp;
7150 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151 }
7152
7153 /* Be paranoid... */
7154 if (prog == NULL || line == NULL)
7155 {
7156 EMSG(_(e_null));
7157 goto theend;
7158 }
7159
Bram Moolenaar6100d022016-10-02 16:51:57 +02007160 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007161 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007162 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007164 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007165
7166#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007167 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007169 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170#endif
7171
7172 regline = line;
7173 reglnum = 0; /* relative to line */
7174
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007175 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007176 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007177 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007178 nfa_listid = 1;
7179 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007180 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007181
Bram Moolenaard89616e2013-06-06 18:46:06 +02007182 if (prog->reganch && col > 0)
7183 return 0L;
7184
Bram Moolenaar473de612013-06-08 18:19:48 +02007185 need_clear_subexpr = TRUE;
7186#ifdef FEAT_SYN_HL
7187 /* Clear the external match subpointers if necessary. */
7188 if (prog->reghasz == REX_SET)
7189 {
7190 nfa_has_zsubexpr = TRUE;
7191 need_clear_zsubexpr = TRUE;
7192 }
7193 else
7194 nfa_has_zsubexpr = FALSE;
7195#endif
7196
Bram Moolenaard89616e2013-06-06 18:46:06 +02007197 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007198 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007199 /* Skip ahead until a character we know the match must start with.
7200 * When there is none there is no match. */
7201 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007202 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007203
Bram Moolenaar473de612013-06-08 18:19:48 +02007204 /* If match_text is set it contains the full text that must match.
7205 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007206 if (prog->match_text != NULL
7207#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007208 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007209#endif
7210 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007211 return find_match_text(col, prog->regstart, prog->match_text);
7212 }
7213
Bram Moolenaard89616e2013-06-06 18:46:06 +02007214 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007215 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007216 goto theend;
7217
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007218 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219 for (i = 0; i < nstate; ++i)
7220 {
7221 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007222 prog->state[i].lastlist[0] = 0;
7223 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007224 }
7225
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007226 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007227
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007228 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007229
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007230theend:
7231 return retval;
7232}
7233
7234/*
7235 * Compile a regular expression into internal code for the NFA matcher.
7236 * Returns the program in allocated space. Returns NULL for an error.
7237 */
7238 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007239nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007240{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007241 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007242 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007243 int *postfix;
7244
7245 if (expr == NULL)
7246 return NULL;
7247
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007248 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007249 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007250
7251 init_class_tab();
7252
7253 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7254 return NULL;
7255
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007256 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007257 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007258 postfix = re2post();
7259 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007260 {
7261 /* TODO: only give this error for debugging? */
7262 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007263 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007264 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007265 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007266
7267 /*
7268 * In order to build the NFA, we parse the input regexp twice:
7269 * 1. first pass to count size (so we can allocate space)
7270 * 2. second to emit code
7271 */
7272#ifdef ENABLE_LOG
7273 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007274 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007275
7276 if (f != NULL)
7277 {
7278 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7279 fclose(f);
7280 }
7281 }
7282#endif
7283
7284 /*
7285 * PASS 1
7286 * Count number of NFA states in "nstate". Do not build the NFA.
7287 */
7288 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007289
Bram Moolenaar16619a22013-06-11 18:42:36 +02007290 /* allocate the regprog with space for the compiled regexp */
7291 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007292 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7293 if (prog == NULL)
7294 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007295 state_ptr = prog->state;
7296
7297 /*
7298 * PASS 2
7299 * Build the NFA
7300 */
7301 prog->start = post2nfa(postfix, post_ptr, FALSE);
7302 if (prog->start == NULL)
7303 goto fail;
7304
7305 prog->regflags = regflags;
7306 prog->engine = &nfa_regengine;
7307 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007308 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007309 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007310 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007311
Bram Moolenaara2947e22013-06-11 22:44:09 +02007312 nfa_postprocess(prog);
7313
Bram Moolenaard89616e2013-06-06 18:46:06 +02007314 prog->reganch = nfa_get_reganch(prog->start, 0);
7315 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007316 prog->match_text = nfa_get_match_text(prog->start);
7317
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007318#ifdef ENABLE_LOG
7319 nfa_postfix_dump(expr, OK);
7320 nfa_dump(prog);
7321#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007322#ifdef FEAT_SYN_HL
7323 /* Remember whether this pattern has any \z specials in it. */
7324 prog->reghasz = re_has_z;
7325#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007326 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007327 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007328
7329out:
7330 vim_free(post_start);
7331 post_start = post_ptr = post_end = NULL;
7332 state_ptr = NULL;
7333 return (regprog_T *)prog;
7334
7335fail:
7336 vim_free(prog);
7337 prog = NULL;
7338#ifdef ENABLE_LOG
7339 nfa_postfix_dump(expr, FAIL);
7340#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007341 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007342 goto out;
7343}
7344
Bram Moolenaar473de612013-06-08 18:19:48 +02007345/*
7346 * Free a compiled regexp program, returned by nfa_regcomp().
7347 */
7348 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007349nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007350{
7351 if (prog != NULL)
7352 {
7353 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007354 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007355 vim_free(prog);
7356 }
7357}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007358
7359/*
7360 * Match a regexp against a string.
7361 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7362 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007363 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007364 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007365 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007366 */
7367 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007368nfa_regexec_nl(
7369 regmatch_T *rmp,
7370 char_u *line, /* string to match against */
7371 colnr_T col, /* column to start looking for match */
7372 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007373{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007374 rex.reg_match = rmp;
7375 rex.reg_mmatch = NULL;
7376 rex.reg_maxline = 0;
7377 rex.reg_line_lbr = line_lbr;
7378 rex.reg_buf = curbuf;
7379 rex.reg_win = NULL;
7380 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007381#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007382 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007383#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007384 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007385 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007386}
7387
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007388
7389/*
7390 * Match a regexp against multiple lines.
7391 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7392 * Uses curbuf for line count and 'iskeyword'.
7393 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007394 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007395 * match otherwise.
7396 *
7397 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7398 *
7399 * ! Also NOTE : match may actually be in another line. e.g.:
7400 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7401 *
7402 * +-------------------------+
7403 * |a |
7404 * |b |
7405 * |c |
7406 * | |
7407 * +-------------------------+
7408 *
7409 * then nfa_regexec_multi() returns 3. while the original
7410 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7411 *
7412 * FIXME if this behavior is not compatible.
7413 */
7414 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007415nfa_regexec_multi(
7416 regmmatch_T *rmp,
7417 win_T *win, /* window in which to search or NULL */
7418 buf_T *buf, /* buffer in which to search */
7419 linenr_T lnum, /* nr of line to start looking for match */
7420 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007421 proftime_T *tm, /* timeout limit or NULL */
7422 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007423{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007424 rex.reg_match = NULL;
7425 rex.reg_mmatch = rmp;
7426 rex.reg_buf = buf;
7427 rex.reg_win = win;
7428 rex.reg_firstlnum = lnum;
7429 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7430 rex.reg_line_lbr = FALSE;
7431 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007432#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007433 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007434#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007435 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007436
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007437 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007438}
7439
7440#ifdef DEBUG
7441# undef ENABLE_LOG
7442#endif