blob: 804d742ab3b63707a2f6e32c53fa8d6c21db578e [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 */
1323 /*FALLTHROUGH*/
1324
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 Moolenaar7cd4d9c2013-05-26 14:54:12 +02001525 int 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 Moolenaarfbc0d2e2013-05-19 19:40:29 +02002043 int 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);
3530 zend = alloc_state(NFA_ZEND, s1, NULL);
3531 s1->out= skip;
3532 patch(e.out, zend);
3533 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003534 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003535 else
3536 {
3537 patch(e.out, s1);
3538 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003539 if (before)
3540 {
3541 if (n <= 0)
3542 /* See if we can guess the maximum width, it avoids a
3543 * lot of pointless tries. */
3544 n = nfa_max_width(e.start, 0);
3545 s->val = n; /* store the count */
3546 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003547 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003548 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003549 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003550
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003551#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003552 case NFA_COMPOSING: /* char with composing char */
3553#if 0
3554 /* TODO */
3555 if (regflags & RF_ICOMBINE)
3556 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003557 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003558 }
3559#endif
3560 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003561#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003562
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003563 case NFA_MOPEN: /* \( \) Submatch */
3564 case NFA_MOPEN1:
3565 case NFA_MOPEN2:
3566 case NFA_MOPEN3:
3567 case NFA_MOPEN4:
3568 case NFA_MOPEN5:
3569 case NFA_MOPEN6:
3570 case NFA_MOPEN7:
3571 case NFA_MOPEN8:
3572 case NFA_MOPEN9:
3573#ifdef FEAT_SYN_HL
3574 case NFA_ZOPEN: /* \z( \) Submatch */
3575 case NFA_ZOPEN1:
3576 case NFA_ZOPEN2:
3577 case NFA_ZOPEN3:
3578 case NFA_ZOPEN4:
3579 case NFA_ZOPEN5:
3580 case NFA_ZOPEN6:
3581 case NFA_ZOPEN7:
3582 case NFA_ZOPEN8:
3583 case NFA_ZOPEN9:
3584#endif
3585 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 if (nfa_calc_size == TRUE)
3587 {
3588 nstate += 2;
3589 break;
3590 }
3591
3592 mopen = *p;
3593 switch (*p)
3594 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003595 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3596#ifdef FEAT_SYN_HL
3597 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3598 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3599 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3600 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3601 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3602 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3603 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3604 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3605 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3606 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3607#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003608#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003609 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003610#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003611 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003612 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003613 mclose = *p + NSUBEXP;
3614 break;
3615 }
3616
3617 /* Allow "NFA_MOPEN" as a valid postfix representation for
3618 * the empty regexp "". In this case, the NFA will be
3619 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3620 * empty groups of parenthesis, and empty mbyte chars */
3621 if (stackp == stack)
3622 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003623 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003625 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003626 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003628 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003629 patch(list1(&s->out), s1);
3630 PUSH(frag(s, list1(&s1->out)));
3631 break;
3632 }
3633
3634 /* At least one node was emitted before NFA_MOPEN, so
3635 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3636 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003637 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003638 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003639 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640
Bram Moolenaar525666f2013-06-02 16:40:55 +02003641 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003642 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003643 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003644 patch(e.out, s1);
3645
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003646#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003647 if (mopen == NFA_COMPOSING)
3648 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003649 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003650#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651
3652 PUSH(frag(s, list1(&s1->out)));
3653 break;
3654
Bram Moolenaar5714b802013-05-28 22:03:20 +02003655 case NFA_BACKREF1:
3656 case NFA_BACKREF2:
3657 case NFA_BACKREF3:
3658 case NFA_BACKREF4:
3659 case NFA_BACKREF5:
3660 case NFA_BACKREF6:
3661 case NFA_BACKREF7:
3662 case NFA_BACKREF8:
3663 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003664#ifdef FEAT_SYN_HL
3665 case NFA_ZREF1:
3666 case NFA_ZREF2:
3667 case NFA_ZREF3:
3668 case NFA_ZREF4:
3669 case NFA_ZREF5:
3670 case NFA_ZREF6:
3671 case NFA_ZREF7:
3672 case NFA_ZREF8:
3673 case NFA_ZREF9:
3674#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003675 if (nfa_calc_size == TRUE)
3676 {
3677 nstate += 2;
3678 break;
3679 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003680 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003681 if (s == NULL)
3682 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003683 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003684 if (s1 == NULL)
3685 goto theend;
3686 patch(list1(&s->out), s1);
3687 PUSH(frag(s, list1(&s1->out)));
3688 break;
3689
Bram Moolenaar423532e2013-05-29 21:14:42 +02003690 case NFA_LNUM:
3691 case NFA_LNUM_GT:
3692 case NFA_LNUM_LT:
3693 case NFA_VCOL:
3694 case NFA_VCOL_GT:
3695 case NFA_VCOL_LT:
3696 case NFA_COL:
3697 case NFA_COL_GT:
3698 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003699 case NFA_MARK:
3700 case NFA_MARK_GT:
3701 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003702 {
3703 int n = *++p; /* lnum, col or mark name */
3704
Bram Moolenaar423532e2013-05-29 21:14:42 +02003705 if (nfa_calc_size == TRUE)
3706 {
3707 nstate += 1;
3708 break;
3709 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003710 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003711 if (s == NULL)
3712 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003713 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003714 PUSH(frag(s, list1(&s->out)));
3715 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003716 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003717
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003718 case NFA_ZSTART:
3719 case NFA_ZEND:
3720 default:
3721 /* Operands */
3722 if (nfa_calc_size == TRUE)
3723 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003724 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003725 break;
3726 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003727 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003728 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003729 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003730 PUSH(frag(s, list1(&s->out)));
3731 break;
3732
3733 } /* switch(*p) */
3734
3735 } /* for(p = postfix; *p; ++p) */
3736
3737 if (nfa_calc_size == TRUE)
3738 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003739 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003740 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741 }
3742
3743 e = POP();
3744 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003745 {
3746 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747 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 +02003748 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749
3750 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003751 {
3752 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003753 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003754 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003755
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003756 matchstate = &state_ptr[istate++]; /* the match state */
3757 matchstate->c = NFA_MATCH;
3758 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003759 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760
3761 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003762 ret = e.start;
3763
3764theend:
3765 vim_free(stack);
3766 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767
3768#undef POP1
3769#undef PUSH1
3770#undef POP2
3771#undef PUSH2
3772#undef POP
3773#undef PUSH
3774}
3775
Bram Moolenaara2947e22013-06-11 22:44:09 +02003776/*
3777 * After building the NFA program, inspect it to add optimization hints.
3778 */
3779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003780nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003781{
3782 int i;
3783 int c;
3784
3785 for (i = 0; i < prog->nstate; ++i)
3786 {
3787 c = prog->state[i].c;
3788 if (c == NFA_START_INVISIBLE
3789 || c == NFA_START_INVISIBLE_NEG
3790 || c == NFA_START_INVISIBLE_BEFORE
3791 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3792 {
3793 int directly;
3794
3795 /* Do it directly when what follows is possibly the end of the
3796 * match. */
3797 if (match_follows(prog->state[i].out1->out, 0))
3798 directly = TRUE;
3799 else
3800 {
3801 int ch_invisible = failure_chance(prog->state[i].out, 0);
3802 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3803
3804 /* Postpone when the invisible match is expensive or has a
3805 * lower chance of failing. */
3806 if (c == NFA_START_INVISIBLE_BEFORE
3807 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3808 {
3809 /* "before" matches are very expensive when
3810 * unbounded, always prefer what follows then,
3811 * unless what follows will always match.
3812 * Otherwise strongly prefer what follows. */
3813 if (prog->state[i].val <= 0 && ch_follows > 0)
3814 directly = FALSE;
3815 else
3816 directly = ch_follows * 10 < ch_invisible;
3817 }
3818 else
3819 {
3820 /* normal invisible, first do the one with the
3821 * highest failure chance */
3822 directly = ch_follows < ch_invisible;
3823 }
3824 }
3825 if (directly)
3826 /* switch to the _FIRST state */
3827 ++prog->state[i].c;
3828 }
3829 }
3830}
3831
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003832/****************************************************************
3833 * NFA execution code.
3834 ****************************************************************/
3835
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003836typedef struct
3837{
3838 int in_use; /* number of subexpr with useful info */
3839
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003840 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003841 union
3842 {
3843 struct multipos
3844 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003845 linenr_T start_lnum;
3846 linenr_T end_lnum;
3847 colnr_T start_col;
3848 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003849 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003850 struct linepos
3851 {
3852 char_u *start;
3853 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003854 } line[NSUBEXP];
3855 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003856} regsub_T;
3857
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003858typedef struct
3859{
3860 regsub_T norm; /* \( .. \) matches */
3861#ifdef FEAT_SYN_HL
3862 regsub_T synt; /* \z( .. \) matches */
3863#endif
3864} regsubs_T;
3865
Bram Moolenaara2d95102013-06-04 14:23:05 +02003866/* nfa_pim_T stores a Postponed Invisible Match. */
3867typedef struct nfa_pim_S nfa_pim_T;
3868struct nfa_pim_S
3869{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003870 int result; /* NFA_PIM_*, see below */
3871 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003872 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003873 union
3874 {
3875 lpos_T pos;
3876 char_u *ptr;
3877 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003878};
3879
3880/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003881#define NFA_PIM_UNUSED 0 /* pim not used */
3882#define NFA_PIM_TODO 1 /* pim not done yet */
3883#define NFA_PIM_MATCH 2 /* pim executed, matches */
3884#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003885
3886
Bram Moolenaar963fee22013-05-26 21:47:28 +02003887/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003888typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003889{
3890 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003891 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003892 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3893 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003894 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003895} nfa_thread_T;
3896
Bram Moolenaar963fee22013-05-26 21:47:28 +02003897/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003898typedef struct
3899{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003900 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003901 int n; /* nr of states currently in "t" */
3902 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003903 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003904 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003905} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003906
Bram Moolenaar5714b802013-05-28 22:03:20 +02003907#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003908static void log_subsexpr(regsubs_T *subs);
3909static void log_subexpr(regsub_T *sub);
3910static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003911
3912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003913log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003914{
3915 log_subexpr(&subs->norm);
3916# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003917 if (nfa_has_zsubexpr)
3918 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003919# endif
3920}
3921
Bram Moolenaar5714b802013-05-28 22:03:20 +02003922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003923log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003924{
3925 int j;
3926
3927 for (j = 0; j < sub->in_use; j++)
3928 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003929 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003930 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003931 sub->list.multi[j].start_col,
3932 (int)sub->list.multi[j].start_lnum,
3933 sub->list.multi[j].end_col,
3934 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003935 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003936 {
3937 char *s = (char *)sub->list.line[j].start;
3938 char *e = (char *)sub->list.line[j].end;
3939
Bram Moolenaar87953742013-06-05 18:52:40 +02003940 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003941 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003942 s == NULL ? "NULL" : s,
3943 e == NULL ? "NULL" : e);
3944 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003945}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003946
3947 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003948pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003949{
3950 static char buf[30];
3951
3952 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3953 buf[0] = NUL;
3954 else
3955 {
3956 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3957 : (int)(pim->end.ptr - reginput));
3958 }
3959 return buf;
3960}
3961
Bram Moolenaar5714b802013-05-28 22:03:20 +02003962#endif
3963
Bram Moolenaar963fee22013-05-26 21:47:28 +02003964/* Used during execution: whether a match has been found. */
3965static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003966#ifdef FEAT_RELTIME
3967static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003968static int *nfa_timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003969static int nfa_time_count;
3970#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003971
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003972static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3973static void clear_sub(regsub_T *sub);
3974static void copy_sub(regsub_T *to, regsub_T *from);
3975static void copy_sub_off(regsub_T *to, regsub_T *from);
3976static void copy_ze_off(regsub_T *to, regsub_T *from);
3977static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3978static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3979static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3980static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3981static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3982static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3983static 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 +02003984
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003985/*
3986 * Copy postponed invisible match info from "from" to "to".
3987 */
3988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003989copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003990{
3991 to->result = from->result;
3992 to->state = from->state;
3993 copy_sub(&to->subs.norm, &from->subs.norm);
3994#ifdef FEAT_SYN_HL
3995 if (nfa_has_zsubexpr)
3996 copy_sub(&to->subs.synt, &from->subs.synt);
3997#endif
3998 to->end = from->end;
3999}
4000
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004001 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004002clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004003{
4004 if (REG_MULTI)
4005 /* Use 0xff to set lnum to -1 */
4006 vim_memset(sub->list.multi, 0xff,
4007 sizeof(struct multipos) * nfa_nsubexpr);
4008 else
4009 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4010 sub->in_use = 0;
4011}
4012
4013/*
4014 * Copy the submatches from "from" to "to".
4015 */
4016 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004017copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004018{
4019 to->in_use = from->in_use;
4020 if (from->in_use > 0)
4021 {
4022 /* Copy the match start and end positions. */
4023 if (REG_MULTI)
4024 mch_memmove(&to->list.multi[0],
4025 &from->list.multi[0],
4026 sizeof(struct multipos) * from->in_use);
4027 else
4028 mch_memmove(&to->list.line[0],
4029 &from->list.line[0],
4030 sizeof(struct linepos) * from->in_use);
4031 }
4032}
4033
4034/*
4035 * Like copy_sub() but exclude the main match.
4036 */
4037 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004038copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004039{
4040 if (to->in_use < from->in_use)
4041 to->in_use = from->in_use;
4042 if (from->in_use > 1)
4043 {
4044 /* Copy the match start and end positions. */
4045 if (REG_MULTI)
4046 mch_memmove(&to->list.multi[1],
4047 &from->list.multi[1],
4048 sizeof(struct multipos) * (from->in_use - 1));
4049 else
4050 mch_memmove(&to->list.line[1],
4051 &from->list.line[1],
4052 sizeof(struct linepos) * (from->in_use - 1));
4053 }
4054}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004055
Bram Moolenaar428e9872013-05-30 17:05:39 +02004056/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004057 * Like copy_sub() but only do the end of the main match if \ze is present.
4058 */
4059 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004060copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004061{
4062 if (nfa_has_zend)
4063 {
4064 if (REG_MULTI)
4065 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004066 if (from->list.multi[0].end_lnum >= 0)
4067 {
4068 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4069 to->list.multi[0].end_col = from->list.multi[0].end_col;
4070 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004071 }
4072 else
4073 {
4074 if (from->list.line[0].end != NULL)
4075 to->list.line[0].end = from->list.line[0].end;
4076 }
4077 }
4078}
4079
4080/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004081 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004082 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004083 */
4084 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004085sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004086{
4087 int i;
4088 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004089 linenr_T s1;
4090 linenr_T s2;
4091 char_u *sp1;
4092 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004093
4094 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4095 if (REG_MULTI)
4096 {
4097 for (i = 0; i < todo; ++i)
4098 {
4099 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004100 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004101 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004102 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004103 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004104 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004105 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004106 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004107 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004108 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004109 if (s1 != -1 && sub1->list.multi[i].start_col
4110 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004111 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004112
4113 if (nfa_has_backref)
4114 {
4115 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004116 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004117 else
4118 s1 = -1;
4119 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004120 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004121 else
4122 s2 = -1;
4123 if (s1 != s2)
4124 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004125 if (s1 != -1 && sub1->list.multi[i].end_col
4126 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004127 return FALSE;
4128 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004129 }
4130 }
4131 else
4132 {
4133 for (i = 0; i < todo; ++i)
4134 {
4135 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004136 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004137 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004138 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004139 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004140 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004141 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004142 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004143 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004144 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004145 if (nfa_has_backref)
4146 {
4147 if (i < sub1->in_use)
4148 sp1 = sub1->list.line[i].end;
4149 else
4150 sp1 = NULL;
4151 if (i < sub2->in_use)
4152 sp2 = sub2->list.line[i].end;
4153 else
4154 sp2 = NULL;
4155 if (sp1 != sp2)
4156 return FALSE;
4157 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004158 }
4159 }
4160
4161 return TRUE;
4162}
4163
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004164#ifdef ENABLE_LOG
4165 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004166report_state(char *action,
4167 regsub_T *sub,
4168 nfa_state_T *state,
4169 int lid,
4170 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004171{
4172 int col;
4173
4174 if (sub->in_use <= 0)
4175 col = -1;
4176 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004177 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004178 else
4179 col = (int)(sub->list.line[0].start - regline);
4180 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004181 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4182 action, abs(state->id), lid, state->c, code, col,
4183 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004184}
4185#endif
4186
Bram Moolenaar43e02982013-06-07 17:31:29 +02004187/*
4188 * Return TRUE if the same state is already in list "l" with the same
4189 * positions as "subs".
4190 */
4191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004192has_state_with_pos(
4193 nfa_list_T *l, /* runtime state list */
4194 nfa_state_T *state, /* state to update */
4195 regsubs_T *subs, /* pointers to subexpressions */
4196 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004197{
4198 nfa_thread_T *thread;
4199 int i;
4200
4201 for (i = 0; i < l->n; ++i)
4202 {
4203 thread = &l->t[i];
4204 if (thread->state->id == state->id
4205 && sub_equal(&thread->subs.norm, &subs->norm)
4206#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004207 && (!nfa_has_zsubexpr
4208 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004209#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004210 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004211 return TRUE;
4212 }
4213 return FALSE;
4214}
4215
4216/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004217 * Return TRUE if "one" and "two" are equal. That includes when both are not
4218 * set.
4219 */
4220 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004221pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004222{
4223 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4224 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4225
4226 if (one_unused)
4227 /* one is unused: equal when two is also unused */
4228 return two_unused;
4229 if (two_unused)
4230 /* one is used and two is not: not equal */
4231 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004232 /* compare the state id */
4233 if (one->state->id != two->state->id)
4234 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004235 /* compare the position */
4236 if (REG_MULTI)
4237 return one->end.pos.lnum == two->end.pos.lnum
4238 && one->end.pos.col == two->end.pos.col;
4239 return one->end.ptr == two->end.ptr;
4240}
4241
4242/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004243 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4244 */
4245 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004246match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004247{
4248 nfa_state_T *state = startstate;
4249
4250 /* avoid too much recursion */
4251 if (depth > 10)
4252 return FALSE;
4253
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004254 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004255 {
4256 switch (state->c)
4257 {
4258 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004259 case NFA_MCLOSE:
4260 case NFA_END_INVISIBLE:
4261 case NFA_END_INVISIBLE_NEG:
4262 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004263 return TRUE;
4264
4265 case NFA_SPLIT:
4266 return match_follows(state->out, depth + 1)
4267 || match_follows(state->out1, depth + 1);
4268
4269 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004270 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004271 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004272 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004273 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004274 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004276 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004277 case NFA_COMPOSING:
4278 /* skip ahead to next state */
4279 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004280 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004281
4282 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004283 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004284 case NFA_IDENT:
4285 case NFA_SIDENT:
4286 case NFA_KWORD:
4287 case NFA_SKWORD:
4288 case NFA_FNAME:
4289 case NFA_SFNAME:
4290 case NFA_PRINT:
4291 case NFA_SPRINT:
4292 case NFA_WHITE:
4293 case NFA_NWHITE:
4294 case NFA_DIGIT:
4295 case NFA_NDIGIT:
4296 case NFA_HEX:
4297 case NFA_NHEX:
4298 case NFA_OCTAL:
4299 case NFA_NOCTAL:
4300 case NFA_WORD:
4301 case NFA_NWORD:
4302 case NFA_HEAD:
4303 case NFA_NHEAD:
4304 case NFA_ALPHA:
4305 case NFA_NALPHA:
4306 case NFA_LOWER:
4307 case NFA_NLOWER:
4308 case NFA_UPPER:
4309 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004310 case NFA_LOWER_IC:
4311 case NFA_NLOWER_IC:
4312 case NFA_UPPER_IC:
4313 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004314 case NFA_START_COLL:
4315 case NFA_START_NEG_COLL:
4316 case NFA_NEWL:
4317 /* state will advance input */
4318 return FALSE;
4319
4320 default:
4321 if (state->c > 0)
4322 /* state will advance input */
4323 return FALSE;
4324
4325 /* Others: zero-width or possibly zero-width, might still find
4326 * a match at the same position, keep looking. */
4327 break;
4328 }
4329 state = state->out;
4330 }
4331 return FALSE;
4332}
4333
4334
4335/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004336 * Return TRUE if "state" is already in list "l".
4337 */
4338 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004339state_in_list(
4340 nfa_list_T *l, /* runtime state list */
4341 nfa_state_T *state, /* state to update */
4342 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004343{
4344 if (state->lastlist[nfa_ll_index] == l->id)
4345 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004346 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004347 return TRUE;
4348 }
4349 return FALSE;
4350}
4351
Bram Moolenaar16b35782016-09-09 20:29:50 +02004352/* Offset used for "off" by addstate_here(). */
4353#define ADDSTATE_HERE_OFFSET 10
4354
Bram Moolenaard05bf562013-06-30 23:24:08 +02004355/*
4356 * Add "state" and possibly what follows to state list ".".
4357 * Returns "subs_arg", possibly copied into temp_subs.
4358 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004359 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004360addstate(
4361 nfa_list_T *l, /* runtime state list */
4362 nfa_state_T *state, /* state to update */
4363 regsubs_T *subs_arg, /* pointers to subexpressions */
4364 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004365 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004366{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004367 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004368 int off = off_arg;
4369 int add_here = FALSE;
4370 int listindex = 0;
4371 int k;
4372 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004373 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004374 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004375 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004376 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004377 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004378 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004379 regsubs_T *subs = subs_arg;
4380 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004381#ifdef ENABLE_LOG
4382 int did_print = FALSE;
4383#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004384
Bram Moolenaar16b35782016-09-09 20:29:50 +02004385 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4386 {
4387 add_here = TRUE;
4388 off = 0;
4389 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4390 }
4391
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004392 switch (state->c)
4393 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004394 case NFA_NCLOSE:
4395 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004396 case NFA_MCLOSE1:
4397 case NFA_MCLOSE2:
4398 case NFA_MCLOSE3:
4399 case NFA_MCLOSE4:
4400 case NFA_MCLOSE5:
4401 case NFA_MCLOSE6:
4402 case NFA_MCLOSE7:
4403 case NFA_MCLOSE8:
4404 case NFA_MCLOSE9:
4405#ifdef FEAT_SYN_HL
4406 case NFA_ZCLOSE:
4407 case NFA_ZCLOSE1:
4408 case NFA_ZCLOSE2:
4409 case NFA_ZCLOSE3:
4410 case NFA_ZCLOSE4:
4411 case NFA_ZCLOSE5:
4412 case NFA_ZCLOSE6:
4413 case NFA_ZCLOSE7:
4414 case NFA_ZCLOSE8:
4415 case NFA_ZCLOSE9:
4416#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004417 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004418 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004419 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004420 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004421 /* These nodes are not added themselves but their "out" and/or
4422 * "out1" may be added below. */
4423 break;
4424
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004425 case NFA_BOL:
4426 case NFA_BOF:
4427 /* "^" won't match past end-of-line, don't bother trying.
4428 * Except when at the end of the line, or when we are going to the
4429 * next line for a look-behind match. */
4430 if (reginput > regline
4431 && *reginput != NUL
4432 && (nfa_endp == NULL
4433 || !REG_MULTI
4434 || reglnum == nfa_endp->se_u.pos.lnum))
4435 goto skip_add;
4436 /* FALLTHROUGH */
4437
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004438 case NFA_MOPEN1:
4439 case NFA_MOPEN2:
4440 case NFA_MOPEN3:
4441 case NFA_MOPEN4:
4442 case NFA_MOPEN5:
4443 case NFA_MOPEN6:
4444 case NFA_MOPEN7:
4445 case NFA_MOPEN8:
4446 case NFA_MOPEN9:
4447#ifdef FEAT_SYN_HL
4448 case NFA_ZOPEN:
4449 case NFA_ZOPEN1:
4450 case NFA_ZOPEN2:
4451 case NFA_ZOPEN3:
4452 case NFA_ZOPEN4:
4453 case NFA_ZOPEN5:
4454 case NFA_ZOPEN6:
4455 case NFA_ZOPEN7:
4456 case NFA_ZOPEN8:
4457 case NFA_ZOPEN9:
4458#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004459 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004460 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004461 /* These nodes need to be added so that we can bail out when it
4462 * was added to this list before at the same position to avoid an
4463 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004464
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004465 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004466 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004467 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004468 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004469 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004470 * when there is a PIM. For NFA_MATCH check the position,
4471 * lower position is preferred. */
4472 if (!nfa_has_backref && pim == NULL && !l->has_pim
4473 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004474 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004475 /* When called from addstate_here() do insert before
4476 * existing states. */
4477 if (add_here)
4478 {
4479 for (k = 0; k < l->n && k < listindex; ++k)
4480 if (l->t[k].state->id == state->id)
4481 {
4482 found = TRUE;
4483 break;
4484 }
4485 }
4486 if (!add_here || found)
4487 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004488skip_add:
4489#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004490 nfa_set_code(state->c);
4491 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4492 abs(state->id), l->id, state->c, code,
4493 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004494#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004495 return subs;
4496 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004497 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004498
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004499 /* Do not add the state again when it exists with the same
4500 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004501 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004502 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004503 }
4504
Bram Moolenaara0169122013-06-26 18:16:58 +02004505 /* When there are backreferences or PIMs the number of states may
4506 * be (a lot) bigger than anticipated. */
4507 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004508 {
4509 int newlen = l->len * 3 / 2 + 50;
4510
Bram Moolenaard05bf562013-06-30 23:24:08 +02004511 if (subs != &temp_subs)
4512 {
4513 /* "subs" may point into the current array, need to make a
4514 * copy before it becomes invalid. */
4515 copy_sub(&temp_subs.norm, &subs->norm);
4516#ifdef FEAT_SYN_HL
4517 if (nfa_has_zsubexpr)
4518 copy_sub(&temp_subs.synt, &subs->synt);
4519#endif
4520 subs = &temp_subs;
4521 }
4522
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004523 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004524 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4525 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004526 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004527
4528 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004529 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004530 thread = &l->t[l->n++];
4531 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004532 if (pim == NULL)
4533 thread->pim.result = NFA_PIM_UNUSED;
4534 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004535 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004536 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004537 l->has_pim = TRUE;
4538 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004539 copy_sub(&thread->subs.norm, &subs->norm);
4540#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004541 if (nfa_has_zsubexpr)
4542 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004543#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004544#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004545 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004546 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004547#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 }
4549
4550#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004551 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004552 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553#endif
4554 switch (state->c)
4555 {
4556 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 break;
4558
4559 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004560 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004561 subs = addstate(l, state->out, subs, pim, off_arg);
4562 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 break;
4564
Bram Moolenaar699c1202013-09-25 16:41:54 +02004565 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004566 case NFA_NOPEN:
4567 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004568 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004569 break;
4570
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004571 case NFA_MOPEN:
4572 case NFA_MOPEN1:
4573 case NFA_MOPEN2:
4574 case NFA_MOPEN3:
4575 case NFA_MOPEN4:
4576 case NFA_MOPEN5:
4577 case NFA_MOPEN6:
4578 case NFA_MOPEN7:
4579 case NFA_MOPEN8:
4580 case NFA_MOPEN9:
4581#ifdef FEAT_SYN_HL
4582 case NFA_ZOPEN:
4583 case NFA_ZOPEN1:
4584 case NFA_ZOPEN2:
4585 case NFA_ZOPEN3:
4586 case NFA_ZOPEN4:
4587 case NFA_ZOPEN5:
4588 case NFA_ZOPEN6:
4589 case NFA_ZOPEN7:
4590 case NFA_ZOPEN8:
4591 case NFA_ZOPEN9:
4592#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004593 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004594 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004595 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004596 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004597 sub = &subs->norm;
4598 }
4599#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004600 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004601 {
4602 subidx = state->c - NFA_ZOPEN;
4603 sub = &subs->synt;
4604 }
4605#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004606 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004607 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004608 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004609 sub = &subs->norm;
4610 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004612 /* avoid compiler warnings */
4613 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004614 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004615
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004616 /* Set the position (with "off" added) in the subexpression. Save
4617 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618 if (REG_MULTI)
4619 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004620 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004621 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004622 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004623 save_in_use = -1;
4624 }
4625 else
4626 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004627 save_in_use = sub->in_use;
4628 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004629 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004630 sub->list.multi[i].start_lnum = -1;
4631 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004632 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004633 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004634 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004635 if (off == -1)
4636 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004637 sub->list.multi[subidx].start_lnum = reglnum + 1;
4638 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004639 }
4640 else
4641 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004642 sub->list.multi[subidx].start_lnum = reglnum;
4643 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004644 (colnr_T)(reginput - regline + off);
4645 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004646 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647 }
4648 else
4649 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004650 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004651 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004652 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004653 save_in_use = -1;
4654 }
4655 else
4656 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004657 save_in_use = sub->in_use;
4658 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004659 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004660 sub->list.line[i].start = NULL;
4661 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004662 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004663 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004664 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004665 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004666 }
4667
Bram Moolenaar16b35782016-09-09 20:29:50 +02004668 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004669 /* "subs" may have changed, need to set "sub" again */
4670#ifdef FEAT_SYN_HL
4671 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4672 sub = &subs->synt;
4673 else
4674#endif
4675 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004676
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004677 if (save_in_use == -1)
4678 {
4679 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004680 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004681 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004682 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004683 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004684 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004685 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 break;
4687
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004688 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004689 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004690 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004691 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004692 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004693 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004694 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 break;
4696 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004697 case NFA_MCLOSE1:
4698 case NFA_MCLOSE2:
4699 case NFA_MCLOSE3:
4700 case NFA_MCLOSE4:
4701 case NFA_MCLOSE5:
4702 case NFA_MCLOSE6:
4703 case NFA_MCLOSE7:
4704 case NFA_MCLOSE8:
4705 case NFA_MCLOSE9:
4706#ifdef FEAT_SYN_HL
4707 case NFA_ZCLOSE:
4708 case NFA_ZCLOSE1:
4709 case NFA_ZCLOSE2:
4710 case NFA_ZCLOSE3:
4711 case NFA_ZCLOSE4:
4712 case NFA_ZCLOSE5:
4713 case NFA_ZCLOSE6:
4714 case NFA_ZCLOSE7:
4715 case NFA_ZCLOSE8:
4716 case NFA_ZCLOSE9:
4717#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004718 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004720 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004721 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004722 sub = &subs->norm;
4723 }
4724#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004725 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004726 {
4727 subidx = state->c - NFA_ZCLOSE;
4728 sub = &subs->synt;
4729 }
4730#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004731 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004732 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004733 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004734 sub = &subs->norm;
4735 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004736
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004737 /* We don't fill in gaps here, there must have been an MOPEN that
4738 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004739 save_in_use = sub->in_use;
4740 if (sub->in_use <= subidx)
4741 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004742 if (REG_MULTI)
4743 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004744 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004745 if (off == -1)
4746 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004747 sub->list.multi[subidx].end_lnum = reglnum + 1;
4748 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004749 }
4750 else
4751 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004752 sub->list.multi[subidx].end_lnum = reglnum;
4753 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004754 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004755 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004756 /* avoid compiler warnings */
4757 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004758 }
4759 else
4760 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004761 save_ptr = sub->list.line[subidx].end;
4762 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004763 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004764 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004765 }
4766
Bram Moolenaar16b35782016-09-09 20:29:50 +02004767 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004768 /* "subs" may have changed, need to set "sub" again */
4769#ifdef FEAT_SYN_HL
4770 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4771 sub = &subs->synt;
4772 else
4773#endif
4774 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004775
4776 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004777 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004778 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004779 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004780 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004781 break;
4782 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004783 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004784}
4785
4786/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004787 * Like addstate(), but the new state(s) are put at position "*ip".
4788 * Used for zero-width matches, next state to use is the added one.
4789 * This makes sure the order of states to be tried does not change, which
4790 * matters for alternatives.
4791 */
4792 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004793addstate_here(
4794 nfa_list_T *l, /* runtime state list */
4795 nfa_state_T *state, /* state to update */
4796 regsubs_T *subs, /* pointers to subexpressions */
4797 nfa_pim_T *pim, /* postponed look-behind match */
4798 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004799{
4800 int tlen = l->n;
4801 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004802 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004803
Bram Moolenaar16b35782016-09-09 20:29:50 +02004804 /* First add the state(s) at the end, so that we know how many there are.
4805 * Pass the listidx as offset (avoids adding another argument to
4806 * addstate(). */
4807 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004808
Bram Moolenaar4b417062013-05-25 20:19:50 +02004809 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004810 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004811 return;
4812
4813 /* re-order to put the new state at the current position */
4814 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004815 if (count == 0)
4816 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004817 if (count == 1)
4818 {
4819 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004820 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004821 }
4822 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004823 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004824 if (l->n + count - 1 >= l->len)
4825 {
4826 /* not enough space to move the new states, reallocate the list
4827 * and move the states to the right position */
4828 nfa_thread_T *newl;
4829
4830 l->len = l->len * 3 / 2 + 50;
4831 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4832 if (newl == NULL)
4833 return;
4834 mch_memmove(&(newl[0]),
4835 &(l->t[0]),
4836 sizeof(nfa_thread_T) * listidx);
4837 mch_memmove(&(newl[listidx]),
4838 &(l->t[l->n - count]),
4839 sizeof(nfa_thread_T) * count);
4840 mch_memmove(&(newl[listidx + count]),
4841 &(l->t[listidx + 1]),
4842 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4843 vim_free(l->t);
4844 l->t = newl;
4845 }
4846 else
4847 {
4848 /* make space for new states, then move them from the
4849 * end to the current position */
4850 mch_memmove(&(l->t[listidx + count]),
4851 &(l->t[listidx + 1]),
4852 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4853 mch_memmove(&(l->t[listidx]),
4854 &(l->t[l->n - 1]),
4855 sizeof(nfa_thread_T) * count);
4856 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004857 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004858 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004859 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004860}
4861
4862/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863 * Check character class "class" against current character c.
4864 */
4865 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004866check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004867{
4868 switch (class)
4869 {
4870 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004871 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004872 return OK;
4873 break;
4874 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004875 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004876 return OK;
4877 break;
4878 case NFA_CLASS_BLANK:
4879 if (c == ' ' || c == '\t')
4880 return OK;
4881 break;
4882 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004883 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884 return OK;
4885 break;
4886 case NFA_CLASS_DIGIT:
4887 if (VIM_ISDIGIT(c))
4888 return OK;
4889 break;
4890 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004891 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004892 return OK;
4893 break;
4894 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004895 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004896 return OK;
4897 break;
4898 case NFA_CLASS_PRINT:
4899 if (vim_isprintc(c))
4900 return OK;
4901 break;
4902 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004903 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004904 return OK;
4905 break;
4906 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004907 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908 return OK;
4909 break;
4910 case NFA_CLASS_UPPER:
4911 if (MB_ISUPPER(c))
4912 return OK;
4913 break;
4914 case NFA_CLASS_XDIGIT:
4915 if (vim_isxdigit(c))
4916 return OK;
4917 break;
4918 case NFA_CLASS_TAB:
4919 if (c == '\t')
4920 return OK;
4921 break;
4922 case NFA_CLASS_RETURN:
4923 if (c == '\r')
4924 return OK;
4925 break;
4926 case NFA_CLASS_BACKSPACE:
4927 if (c == '\b')
4928 return OK;
4929 break;
4930 case NFA_CLASS_ESCAPE:
4931 if (c == '\033')
4932 return OK;
4933 break;
4934
4935 default:
4936 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004937 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004938 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004939 }
4940 return FAIL;
4941}
4942
Bram Moolenaar5714b802013-05-28 22:03:20 +02004943/*
4944 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004945 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004946 */
4947 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004948match_backref(
4949 regsub_T *sub, /* pointers to subexpressions */
4950 int subidx,
4951 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004952{
4953 int len;
4954
4955 if (sub->in_use <= subidx)
4956 {
4957retempty:
4958 /* backref was not set, match an empty string */
4959 *bytelen = 0;
4960 return TRUE;
4961 }
4962
4963 if (REG_MULTI)
4964 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004965 if (sub->list.multi[subidx].start_lnum < 0
4966 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004967 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004968 if (sub->list.multi[subidx].start_lnum == reglnum
4969 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004970 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004971 len = sub->list.multi[subidx].end_col
4972 - sub->list.multi[subidx].start_col;
4973 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004974 reginput, &len) == 0)
4975 {
4976 *bytelen = len;
4977 return TRUE;
4978 }
4979 }
4980 else
4981 {
4982 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004983 sub->list.multi[subidx].start_lnum,
4984 sub->list.multi[subidx].start_col,
4985 sub->list.multi[subidx].end_lnum,
4986 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004987 bytelen) == RA_MATCH)
4988 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004989 }
4990 }
4991 else
4992 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004993 if (sub->list.line[subidx].start == NULL
4994 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004995 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004996 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4997 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004998 {
4999 *bytelen = len;
5000 return TRUE;
5001 }
5002 }
5003 return FALSE;
5004}
5005
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005006#ifdef FEAT_SYN_HL
5007
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005008static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005009
5010/*
5011 * Check for a match with \z subexpression "subidx".
5012 * Return TRUE if it matches.
5013 */
5014 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005015match_zref(
5016 int subidx,
5017 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005018{
5019 int len;
5020
5021 cleanup_zsubexpr();
5022 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5023 {
5024 /* backref was not set, match an empty string */
5025 *bytelen = 0;
5026 return TRUE;
5027 }
5028
5029 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5030 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5031 {
5032 *bytelen = len;
5033 return TRUE;
5034 }
5035 return FALSE;
5036}
5037#endif
5038
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005039/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005040 * Save list IDs for all NFA states of "prog" into "list".
5041 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005042 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005043 */
5044 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005045nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005046{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005047 int i;
5048 nfa_state_T *p;
5049
5050 /* Order in the list is reverse, it's a bit faster that way. */
5051 p = &prog->state[0];
5052 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005053 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005054 list[i] = p->lastlist[1];
5055 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005056 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005057 }
5058}
5059
5060/*
5061 * Restore list IDs from "list" to all NFA states.
5062 */
5063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005064nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005065{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005066 int i;
5067 nfa_state_T *p;
5068
5069 p = &prog->state[0];
5070 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005071 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005072 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005073 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005074 }
5075}
5076
Bram Moolenaar423532e2013-05-29 21:14:42 +02005077 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005078nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005079{
5080 if (op == 1) return pos > val;
5081 if (op == 2) return pos < val;
5082 return val == pos;
5083}
5084
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005085static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5086static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005087
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005089 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005090 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5091 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092 */
5093 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005094recursive_regmatch(
5095 nfa_state_T *state,
5096 nfa_pim_T *pim,
5097 nfa_regprog_T *prog,
5098 regsubs_T *submatch,
5099 regsubs_T *m,
5100 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005101{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005102 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005103 int save_reglnum = reglnum;
5104 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005105 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005106 save_se_T *save_nfa_endp = nfa_endp;
5107 save_se_T endpos;
5108 save_se_T *endposp = NULL;
5109 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005110 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005111
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005112 if (pim != NULL)
5113 {
5114 /* start at the position where the postponed match was */
5115 if (REG_MULTI)
5116 reginput = regline + pim->end.pos.col;
5117 else
5118 reginput = pim->end.ptr;
5119 }
5120
Bram Moolenaardecd9542013-06-07 16:31:50 +02005121 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005122 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5123 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5124 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005125 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005126 /* The recursive match must end at the current position. When "pim" is
5127 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005128 endposp = &endpos;
5129 if (REG_MULTI)
5130 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005131 if (pim == NULL)
5132 {
5133 endpos.se_u.pos.col = (int)(reginput - regline);
5134 endpos.se_u.pos.lnum = reglnum;
5135 }
5136 else
5137 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005138 }
5139 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005140 {
5141 if (pim == NULL)
5142 endpos.se_u.ptr = reginput;
5143 else
5144 endpos.se_u.ptr = pim->end.ptr;
5145 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005146
5147 /* Go back the specified number of bytes, or as far as the
5148 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005149 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005150 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005151 if (state->val <= 0)
5152 {
5153 if (REG_MULTI)
5154 {
5155 regline = reg_getline(--reglnum);
5156 if (regline == NULL)
5157 /* can't go before the first line */
5158 regline = reg_getline(++reglnum);
5159 }
5160 reginput = regline;
5161 }
5162 else
5163 {
5164 if (REG_MULTI && (int)(reginput - regline) < state->val)
5165 {
5166 /* Not enough bytes in this line, go to end of
5167 * previous line. */
5168 regline = reg_getline(--reglnum);
5169 if (regline == NULL)
5170 {
5171 /* can't go before the first line */
5172 regline = reg_getline(++reglnum);
5173 reginput = regline;
5174 }
5175 else
5176 reginput = regline + STRLEN(regline);
5177 }
5178 if ((int)(reginput - regline) >= state->val)
5179 {
5180 reginput -= state->val;
5181#ifdef FEAT_MBYTE
5182 if (has_mbyte)
5183 reginput -= mb_head_off(regline, reginput);
5184#endif
5185 }
5186 else
5187 reginput = regline;
5188 }
5189 }
5190
Bram Moolenaarf46da702013-06-02 22:37:42 +02005191#ifdef ENABLE_LOG
5192 if (log_fd != stderr)
5193 fclose(log_fd);
5194 log_fd = NULL;
5195#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005196 /* Have to clear the lastlist field of the NFA nodes, so that
5197 * nfa_regmatch() and addstate() can run properly after recursion. */
5198 if (nfa_ll_index == 1)
5199 {
5200 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5201 * values and clear them. */
5202 if (*listids == NULL)
5203 {
5204 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5205 if (*listids == NULL)
5206 {
5207 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5208 return 0;
5209 }
5210 }
5211 nfa_save_listids(prog, *listids);
5212 need_restore = TRUE;
5213 /* any value of nfa_listid will do */
5214 }
5215 else
5216 {
5217 /* First recursive nfa_regmatch() call, switch to the second lastlist
5218 * entry. Make sure nfa_listid is different from a previous recursive
5219 * call, because some states may still have this ID. */
5220 ++nfa_ll_index;
5221 if (nfa_listid <= nfa_alt_listid)
5222 nfa_listid = nfa_alt_listid;
5223 }
5224
5225 /* Call nfa_regmatch() to check if the current concat matches at this
5226 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005227 nfa_endp = endposp;
5228 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005229
5230 if (need_restore)
5231 nfa_restore_listids(prog, *listids);
5232 else
5233 {
5234 --nfa_ll_index;
5235 nfa_alt_listid = nfa_listid;
5236 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005237
5238 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005239 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005240 if (REG_MULTI)
5241 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005242 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005243 if (result != NFA_TOO_EXPENSIVE)
5244 {
5245 nfa_match = save_nfa_match;
5246 nfa_listid = save_nfa_listid;
5247 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005248 nfa_endp = save_nfa_endp;
5249
5250#ifdef ENABLE_LOG
5251 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5252 if (log_fd != NULL)
5253 {
5254 fprintf(log_fd, "****************************\n");
5255 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5256 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5257 fprintf(log_fd, "****************************\n");
5258 }
5259 else
5260 {
5261 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5262 log_fd = stderr;
5263 }
5264#endif
5265
5266 return result;
5267}
5268
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005269static int skip_to_start(int c, colnr_T *colp);
5270static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005271
5272/*
5273 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005274 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005275 * NFA_ANY: 1
5276 * specific character: 99
5277 */
5278 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005279failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005280{
5281 int c = state->c;
5282 int l, r;
5283
5284 /* detect looping */
5285 if (depth > 4)
5286 return 1;
5287
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005288 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005289 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005290 case NFA_SPLIT:
5291 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5292 /* avoid recursive stuff */
5293 return 1;
5294 /* two alternatives, use the lowest failure chance */
5295 l = failure_chance(state->out, depth + 1);
5296 r = failure_chance(state->out1, depth + 1);
5297 return l < r ? l : r;
5298
5299 case NFA_ANY:
5300 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005301 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005302
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005303 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005304 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005305 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005306 /* empty match works always */
5307 return 0;
5308
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005309 case NFA_START_INVISIBLE:
5310 case NFA_START_INVISIBLE_FIRST:
5311 case NFA_START_INVISIBLE_NEG:
5312 case NFA_START_INVISIBLE_NEG_FIRST:
5313 case NFA_START_INVISIBLE_BEFORE:
5314 case NFA_START_INVISIBLE_BEFORE_FIRST:
5315 case NFA_START_INVISIBLE_BEFORE_NEG:
5316 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5317 case NFA_START_PATTERN:
5318 /* recursive regmatch is expensive, use low failure chance */
5319 return 5;
5320
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005321 case NFA_BOL:
5322 case NFA_EOL:
5323 case NFA_BOF:
5324 case NFA_EOF:
5325 case NFA_NEWL:
5326 return 99;
5327
5328 case NFA_BOW:
5329 case NFA_EOW:
5330 return 90;
5331
5332 case NFA_MOPEN:
5333 case NFA_MOPEN1:
5334 case NFA_MOPEN2:
5335 case NFA_MOPEN3:
5336 case NFA_MOPEN4:
5337 case NFA_MOPEN5:
5338 case NFA_MOPEN6:
5339 case NFA_MOPEN7:
5340 case NFA_MOPEN8:
5341 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005342#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005343 case NFA_ZOPEN:
5344 case NFA_ZOPEN1:
5345 case NFA_ZOPEN2:
5346 case NFA_ZOPEN3:
5347 case NFA_ZOPEN4:
5348 case NFA_ZOPEN5:
5349 case NFA_ZOPEN6:
5350 case NFA_ZOPEN7:
5351 case NFA_ZOPEN8:
5352 case NFA_ZOPEN9:
5353 case NFA_ZCLOSE:
5354 case NFA_ZCLOSE1:
5355 case NFA_ZCLOSE2:
5356 case NFA_ZCLOSE3:
5357 case NFA_ZCLOSE4:
5358 case NFA_ZCLOSE5:
5359 case NFA_ZCLOSE6:
5360 case NFA_ZCLOSE7:
5361 case NFA_ZCLOSE8:
5362 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005363#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005364 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005365 case NFA_MCLOSE1:
5366 case NFA_MCLOSE2:
5367 case NFA_MCLOSE3:
5368 case NFA_MCLOSE4:
5369 case NFA_MCLOSE5:
5370 case NFA_MCLOSE6:
5371 case NFA_MCLOSE7:
5372 case NFA_MCLOSE8:
5373 case NFA_MCLOSE9:
5374 case NFA_NCLOSE:
5375 return failure_chance(state->out, depth + 1);
5376
5377 case NFA_BACKREF1:
5378 case NFA_BACKREF2:
5379 case NFA_BACKREF3:
5380 case NFA_BACKREF4:
5381 case NFA_BACKREF5:
5382 case NFA_BACKREF6:
5383 case NFA_BACKREF7:
5384 case NFA_BACKREF8:
5385 case NFA_BACKREF9:
5386#ifdef FEAT_SYN_HL
5387 case NFA_ZREF1:
5388 case NFA_ZREF2:
5389 case NFA_ZREF3:
5390 case NFA_ZREF4:
5391 case NFA_ZREF5:
5392 case NFA_ZREF6:
5393 case NFA_ZREF7:
5394 case NFA_ZREF8:
5395 case NFA_ZREF9:
5396#endif
5397 /* backreferences don't match in many places */
5398 return 94;
5399
5400 case NFA_LNUM_GT:
5401 case NFA_LNUM_LT:
5402 case NFA_COL_GT:
5403 case NFA_COL_LT:
5404 case NFA_VCOL_GT:
5405 case NFA_VCOL_LT:
5406 case NFA_MARK_GT:
5407 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005408 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005409 /* before/after positions don't match very often */
5410 return 85;
5411
5412 case NFA_LNUM:
5413 return 90;
5414
5415 case NFA_CURSOR:
5416 case NFA_COL:
5417 case NFA_VCOL:
5418 case NFA_MARK:
5419 /* specific positions rarely match */
5420 return 98;
5421
5422 case NFA_COMPOSING:
5423 return 95;
5424
5425 default:
5426 if (c > 0)
5427 /* character match fails often */
5428 return 95;
5429 }
5430
5431 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005432 return 50;
5433}
5434
Bram Moolenaarf46da702013-06-02 22:37:42 +02005435/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005436 * Skip until the char "c" we know a match must start with.
5437 */
5438 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005439skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005440{
5441 char_u *s;
5442
5443 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005444 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005445#ifdef FEAT_MBYTE
5446 && !has_mbyte
5447#endif
5448 )
5449 s = vim_strbyte(regline + *colp, c);
5450 else
5451 s = cstrchr(regline + *colp, c);
5452 if (s == NULL)
5453 return FAIL;
5454 *colp = (int)(s - regline);
5455 return OK;
5456}
5457
5458/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005459 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005460 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005461 * Returns zero for no match, 1 for a match.
5462 */
5463 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005464find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005465{
5466 colnr_T col = startcol;
5467 int c1, c2;
5468 int len1, len2;
5469 int match;
5470
5471 for (;;)
5472 {
5473 match = TRUE;
5474 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5475 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5476 {
5477 c1 = PTR2CHAR(match_text + len1);
5478 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005479 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005480 {
5481 match = FALSE;
5482 break;
5483 }
5484 len2 += MB_CHAR2LEN(c2);
5485 }
5486 if (match
5487#ifdef FEAT_MBYTE
5488 /* check that no composing char follows */
5489 && !(enc_utf8
5490 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5491#endif
5492 )
5493 {
5494 cleanup_subexpr();
5495 if (REG_MULTI)
5496 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005497 rex.reg_startpos[0].lnum = reglnum;
5498 rex.reg_startpos[0].col = col;
5499 rex.reg_endpos[0].lnum = reglnum;
5500 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005501 }
5502 else
5503 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005504 rex.reg_startp[0] = regline + col;
5505 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005506 }
5507 return 1L;
5508 }
5509
5510 /* Try finding regstart after the current match. */
5511 col += MB_CHAR2LEN(regstart); /* skip regstart */
5512 if (skip_to_start(regstart, &col) == FAIL)
5513 break;
5514 }
5515 return 0L;
5516}
5517
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005518#ifdef FEAT_RELTIME
5519 static int
5520nfa_did_time_out()
5521{
5522 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5523 {
5524 if (nfa_timed_out != NULL)
5525 *nfa_timed_out = TRUE;
5526 return TRUE;
5527 }
5528 return FALSE;
5529}
5530#endif
5531
Bram Moolenaar473de612013-06-08 18:19:48 +02005532/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 * Main matching routine.
5534 *
5535 * Run NFA to determine whether it matches reginput.
5536 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005537 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005538 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005539 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005540 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005541 * Note: Caller must ensure that: start != NULL.
5542 */
5543 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005544nfa_regmatch(
5545 nfa_regprog_T *prog,
5546 nfa_state_T *start,
5547 regsubs_T *submatch,
5548 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005549{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005551 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005553 int go_to_nextline = FALSE;
5554 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005555 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005556 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005557 nfa_list_T *thislist;
5558 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005559 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005560 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005561 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005562 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005563 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005564 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005565#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005566 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005567#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005568
Bram Moolenaar41f12052013-08-25 17:01:42 +02005569 /* Some patterns may take a long time to match, especially when using
5570 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5571 fast_breakcheck();
5572 if (got_int)
5573 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005574#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005575 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005576 return FALSE;
5577#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005578
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005579#ifdef NFA_REGEXP_DEBUG_LOG
5580 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5581 if (debug == NULL)
5582 {
5583 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5584 return FALSE;
5585 }
5586#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005587 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005588
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005589 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005590 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005591
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005592 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005593 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005594 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005595 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005596 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005598
5599#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005600 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005601 if (log_fd != NULL)
5602 {
5603 fprintf(log_fd, "**********************************\n");
5604 nfa_set_code(start->c);
5605 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5606 abs(start->id), code);
5607 fprintf(log_fd, "**********************************\n");
5608 }
5609 else
5610 {
5611 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5612 log_fd = stderr;
5613 }
5614#endif
5615
5616 thislist = &list[0];
5617 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005618 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005619 nextlist = &list[1];
5620 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005621 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005622#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005623 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005625 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005626
5627 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5628 * it's the first MOPEN. */
5629 if (toplevel)
5630 {
5631 if (REG_MULTI)
5632 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005633 m->norm.list.multi[0].start_lnum = reglnum;
5634 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005635 }
5636 else
5637 m->norm.list.line[0].start = reginput;
5638 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005639 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005640 }
5641 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005642 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005643
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005644#define ADD_STATE_IF_MATCH(state) \
5645 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005646 add_state = state->out; \
5647 add_off = clen; \
5648 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005649
5650 /*
5651 * Run for each character.
5652 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005653 for (;;)
5654 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005655 int curc;
5656 int clen;
5657
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005658#ifdef FEAT_MBYTE
5659 if (has_mbyte)
5660 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005661 curc = (*mb_ptr2char)(reginput);
5662 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005663 }
5664 else
5665#endif
5666 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005667 curc = *reginput;
5668 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005669 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005670 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005671 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005673 go_to_nextline = FALSE;
5674 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675
5676 /* swap lists */
5677 thislist = &list[flag];
5678 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005679 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005680 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005681 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005682 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5683 {
5684 /* too many states, retry with old engine */
5685 nfa_match = NFA_TOO_EXPENSIVE;
5686 goto theend;
5687 }
5688
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005689 thislist->id = nfa_listid;
5690 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005691
5692#ifdef ENABLE_LOG
5693 fprintf(log_fd, "------------------------------------------\n");
5694 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005695 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005697 {
5698 int i;
5699
5700 for (i = 0; i < thislist->n; i++)
5701 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5702 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703 fprintf(log_fd, "\n");
5704#endif
5705
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005706#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005707 fprintf(debug, "\n-------------------\n");
5708#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005709 /*
5710 * If the state lists are empty we can stop.
5711 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005712 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005713 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005714
5715 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005716 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005717 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005718 /* If the list gets very long there probably is something wrong.
5719 * At least allow interrupting with CTRL-C. */
5720 fast_breakcheck();
5721 if (got_int)
5722 break;
5723#ifdef FEAT_RELTIME
5724 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5725 {
5726 nfa_time_count = 0;
5727 if (nfa_did_time_out())
5728 break;
5729 }
5730#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005731 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005732
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005733#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005734 nfa_set_code(t->state->c);
5735 fprintf(debug, "%s, ", code);
5736#endif
5737#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005738 {
5739 int col;
5740
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005741 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005742 col = -1;
5743 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005744 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005745 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005746 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005747 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005748 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5749 abs(t->state->id), (int)t->state->c, code, col,
5750 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005752#endif
5753
5754 /*
5755 * Handle the possible codes of the current state.
5756 * The most important is NFA_MATCH.
5757 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005758 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005759 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005760 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005761 switch (t->state->c)
5762 {
5763 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005764 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005765#ifdef FEAT_MBYTE
5766 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005767 * rex.reg_icombine is not set, that is not really a match. */
5768 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005769 break;
5770#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005771 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005772 copy_sub(&submatch->norm, &t->subs.norm);
5773#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005774 if (nfa_has_zsubexpr)
5775 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005776#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005777#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005778 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005779#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005780 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005781 * states at this position. When the list of states is going
5782 * to be empty quit without advancing, so that "reginput" is
5783 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005784 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005785 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005786 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005787 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005788
5789 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005790 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005791 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005792 /*
5793 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005794 * NFA_START_INVISIBLE_BEFORE node.
5795 * They surround a zero-width group, used with "\@=", "\&",
5796 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005797 * If we got here, it means that the current "invisible" group
5798 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005799 * nfa_regmatch(). For a look-behind match only when it ends
5800 * in the position in "nfa_endp".
5801 * Submatches are stored in *m, and used in the parent call.
5802 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005803#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005804 if (nfa_endp != NULL)
5805 {
5806 if (REG_MULTI)
5807 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5808 (int)reglnum,
5809 (int)nfa_endp->se_u.pos.lnum,
5810 (int)(reginput - regline),
5811 nfa_endp->se_u.pos.col);
5812 else
5813 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5814 (int)(reginput - regline),
5815 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005816 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005817#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005818 /* If "nfa_endp" is set it's only a match if it ends at
5819 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005820 if (nfa_endp != NULL && (REG_MULTI
5821 ? (reglnum != nfa_endp->se_u.pos.lnum
5822 || (int)(reginput - regline)
5823 != nfa_endp->se_u.pos.col)
5824 : reginput != nfa_endp->se_u.ptr))
5825 break;
5826
5827 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005828 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005829 {
5830 copy_sub(&m->norm, &t->subs.norm);
5831#ifdef FEAT_SYN_HL
5832 if (nfa_has_zsubexpr)
5833 copy_sub(&m->synt, &t->subs.synt);
5834#endif
5835 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005836#ifdef ENABLE_LOG
5837 fprintf(log_fd, "Match found:\n");
5838 log_subsexpr(m);
5839#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005840 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005841 /* See comment above at "goto nextchar". */
5842 if (nextlist->n == 0)
5843 clen = 0;
5844 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005845
5846 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005847 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005848 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005849 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005850 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005851 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005852 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005853 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005854 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005855#ifdef ENABLE_LOG
5856 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5857 failure_chance(t->state->out, 0),
5858 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005859#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005860 /* Do it directly if there already is a PIM or when
5861 * nfa_postprocess() detected it will work better. */
5862 if (t->pim.result != NFA_PIM_UNUSED
5863 || t->state->c == NFA_START_INVISIBLE_FIRST
5864 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5865 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5866 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005867 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005868 int in_use = m->norm.in_use;
5869
Bram Moolenaar699c1202013-09-25 16:41:54 +02005870 /* Copy submatch info for the recursive call, opposite
5871 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005872 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005873#ifdef FEAT_SYN_HL
5874 if (nfa_has_zsubexpr)
5875 copy_sub_off(&m->synt, &t->subs.synt);
5876#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005877
Bram Moolenaara2d95102013-06-04 14:23:05 +02005878 /*
5879 * First try matching the invisible match, then what
5880 * follows.
5881 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005882 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005883 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005884 if (result == NFA_TOO_EXPENSIVE)
5885 {
5886 nfa_match = result;
5887 goto theend;
5888 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005889
Bram Moolenaardecd9542013-06-07 16:31:50 +02005890 /* for \@! and \@<! it is a match when the result is
5891 * FALSE */
5892 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005893 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5894 || t->state->c
5895 == NFA_START_INVISIBLE_BEFORE_NEG
5896 || t->state->c
5897 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005898 {
5899 /* Copy submatch info from the recursive call */
5900 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005901#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005902 if (nfa_has_zsubexpr)
5903 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005904#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005905 /* If the pattern has \ze and it matched in the
5906 * sub pattern, use it. */
5907 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005908
Bram Moolenaara2d95102013-06-04 14:23:05 +02005909 /* t->state->out1 is the corresponding
5910 * END_INVISIBLE node; Add its out to the current
5911 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005912 add_here = TRUE;
5913 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005914 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005915 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005916 }
5917 else
5918 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005919 nfa_pim_T pim;
5920
Bram Moolenaara2d95102013-06-04 14:23:05 +02005921 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005922 * First try matching what follows. Only if a match
5923 * is found verify the invisible match matches. Add a
5924 * nfa_pim_T to the following states, it contains info
5925 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005926 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005927 pim.state = t->state;
5928 pim.result = NFA_PIM_TODO;
5929 pim.subs.norm.in_use = 0;
5930#ifdef FEAT_SYN_HL
5931 pim.subs.synt.in_use = 0;
5932#endif
5933 if (REG_MULTI)
5934 {
5935 pim.end.pos.col = (int)(reginput - regline);
5936 pim.end.pos.lnum = reglnum;
5937 }
5938 else
5939 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005940
5941 /* t->state->out1 is the corresponding END_INVISIBLE
5942 * node; Add its out to the current list (zero-width
5943 * match). */
5944 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005945 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005946 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005947 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005948 break;
5949
Bram Moolenaar87953742013-06-05 18:52:40 +02005950 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005951 {
5952 nfa_state_T *skip = NULL;
5953#ifdef ENABLE_LOG
5954 int skip_lid = 0;
5955#endif
5956
5957 /* There is no point in trying to match the pattern if the
5958 * output state is not going to be added to the list. */
5959 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5960 {
5961 skip = t->state->out1->out;
5962#ifdef ENABLE_LOG
5963 skip_lid = nextlist->id;
5964#endif
5965 }
5966 else if (state_in_list(nextlist,
5967 t->state->out1->out->out, &t->subs))
5968 {
5969 skip = t->state->out1->out->out;
5970#ifdef ENABLE_LOG
5971 skip_lid = nextlist->id;
5972#endif
5973 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005974 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005975 t->state->out1->out->out, &t->subs))
5976 {
5977 skip = t->state->out1->out->out;
5978#ifdef ENABLE_LOG
5979 skip_lid = thislist->id;
5980#endif
5981 }
5982 if (skip != NULL)
5983 {
5984#ifdef ENABLE_LOG
5985 nfa_set_code(skip->c);
5986 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5987 abs(skip->id), skip_lid, skip->c, code);
5988#endif
5989 break;
5990 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005991 /* Copy submatch info to the recursive call, opposite of what
5992 * happens afterwards. */
5993 copy_sub_off(&m->norm, &t->subs.norm);
5994#ifdef FEAT_SYN_HL
5995 if (nfa_has_zsubexpr)
5996 copy_sub_off(&m->synt, &t->subs.synt);
5997#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005998
Bram Moolenaar87953742013-06-05 18:52:40 +02005999 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006000 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02006001 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006002 if (result == NFA_TOO_EXPENSIVE)
6003 {
6004 nfa_match = result;
6005 goto theend;
6006 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006007 if (result)
6008 {
6009 int bytelen;
6010
6011#ifdef ENABLE_LOG
6012 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6013 log_subsexpr(m);
6014#endif
6015 /* Copy submatch info from the recursive call */
6016 copy_sub_off(&t->subs.norm, &m->norm);
6017#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006018 if (nfa_has_zsubexpr)
6019 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006020#endif
6021 /* Now we need to skip over the matched text and then
6022 * continue with what follows. */
6023 if (REG_MULTI)
6024 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006025 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02006026 - (int)(reginput - regline);
6027 else
6028 bytelen = (int)(m->norm.list.line[0].end - reginput);
6029
6030#ifdef ENABLE_LOG
6031 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6032#endif
6033 if (bytelen == 0)
6034 {
6035 /* empty match, output of corresponding
6036 * NFA_END_PATTERN/NFA_SKIP to be used at current
6037 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006038 add_here = TRUE;
6039 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006040 }
6041 else if (bytelen <= clen)
6042 {
6043 /* match current character, output of corresponding
6044 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006045 add_state = t->state->out1->out->out;
6046 add_off = clen;
6047 }
6048 else
6049 {
6050 /* skip over the matched characters, set character
6051 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006052 add_state = t->state->out1->out;
6053 add_off = bytelen;
6054 add_count = bytelen - clen;
6055 }
6056 }
6057 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006058 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006059
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006060 case NFA_BOL:
6061 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006062 {
6063 add_here = TRUE;
6064 add_state = t->state->out;
6065 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006066 break;
6067
6068 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006069 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006070 {
6071 add_here = TRUE;
6072 add_state = t->state->out;
6073 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074 break;
6075
6076 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006077 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006079 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006080 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006081#ifdef FEAT_MBYTE
6082 else if (has_mbyte)
6083 {
6084 int this_class;
6085
6086 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006087 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006088 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006089 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006091 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006092 }
6093#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006094 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006095 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006096 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006097 result = FALSE;
6098 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006099 {
6100 add_here = TRUE;
6101 add_state = t->state->out;
6102 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006103 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006104
6105 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006106 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006107 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006108 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006109#ifdef FEAT_MBYTE
6110 else if (has_mbyte)
6111 {
6112 int this_class, prev_class;
6113
6114 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006115 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006116 prev_class = reg_prev_class();
6117 if (this_class == prev_class
6118 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006119 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006120 }
6121#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006122 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006123 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006124 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006125 result = FALSE;
6126 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006127 {
6128 add_here = TRUE;
6129 add_state = t->state->out;
6130 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006131 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006132
Bram Moolenaar4b780632013-05-31 22:14:52 +02006133 case NFA_BOF:
6134 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006135 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006136 {
6137 add_here = TRUE;
6138 add_state = t->state->out;
6139 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006140 break;
6141
6142 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006143 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006144 {
6145 add_here = TRUE;
6146 add_state = t->state->out;
6147 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006148 break;
6149
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006150#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006151 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006152 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006153 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006154 int len = 0;
6155 nfa_state_T *end;
6156 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006157 int cchars[MAX_MCO];
6158 int ccount = 0;
6159 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006160
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006161 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006162 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006163 if (utf_iscomposing(sta->c))
6164 {
6165 /* Only match composing character(s), ignore base
6166 * character. Used for ".{composing}" and "{composing}"
6167 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006168 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006169 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006170 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006172 /* If \Z was present, then ignore composing characters.
6173 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006174 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006175 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006176 else
6177 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006178 while (sta->c != NFA_END_COMPOSING)
6179 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006180 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006181
6182 /* Check base character matches first, unless ignored. */
6183 else if (len > 0 || mc == sta->c)
6184 {
6185 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006186 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006187 len += mb_char2len(mc);
6188 sta = sta->out;
6189 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006190
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006191 /* We don't care about the order of composing characters.
6192 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006193 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006194 {
6195 mc = mb_ptr2char(reginput + len);
6196 cchars[ccount++] = mc;
6197 len += mb_char2len(mc);
6198 if (ccount == MAX_MCO)
6199 break;
6200 }
6201
6202 /* Check that each composing char in the pattern matches a
6203 * composing char in the text. We do not check if all
6204 * composing chars are matched. */
6205 result = OK;
6206 while (sta->c != NFA_END_COMPOSING)
6207 {
6208 for (j = 0; j < ccount; ++j)
6209 if (cchars[j] == sta->c)
6210 break;
6211 if (j == ccount)
6212 {
6213 result = FAIL;
6214 break;
6215 }
6216 sta = sta->out;
6217 }
6218 }
6219 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006220 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006221
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006222 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006223 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006224 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006225 }
6226#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006227
6228 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006229 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6230 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006231 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006232 go_to_nextline = TRUE;
6233 /* Pass -1 for the offset, which means taking the position
6234 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006235 add_state = t->state->out;
6236 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006237 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006238 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006239 {
6240 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006241 add_state = t->state->out;
6242 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006243 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006244 break;
6245
Bram Moolenaar417bad22013-06-07 14:08:30 +02006246 case NFA_START_COLL:
6247 case NFA_START_NEG_COLL:
6248 {
6249 /* What follows is a list of characters, until NFA_END_COLL.
6250 * One of them must match or none of them must match. */
6251 nfa_state_T *state;
6252 int result_if_matched;
6253 int c1, c2;
6254
6255 /* Never match EOL. If it's part of the collection it is added
6256 * as a separate state with an OR. */
6257 if (curc == NUL)
6258 break;
6259
6260 state = t->state->out;
6261 result_if_matched = (t->state->c == NFA_START_COLL);
6262 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006263 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006264 if (state->c == NFA_END_COLL)
6265 {
6266 result = !result_if_matched;
6267 break;
6268 }
6269 if (state->c == NFA_RANGE_MIN)
6270 {
6271 c1 = state->val;
6272 state = state->out; /* advance to NFA_RANGE_MAX */
6273 c2 = state->val;
6274#ifdef ENABLE_LOG
6275 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6276 curc, c1, c2);
6277#endif
6278 if (curc >= c1 && curc <= c2)
6279 {
6280 result = result_if_matched;
6281 break;
6282 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006283 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006284 {
6285 int curc_low = MB_TOLOWER(curc);
6286 int done = FALSE;
6287
6288 for ( ; c1 <= c2; ++c1)
6289 if (MB_TOLOWER(c1) == curc_low)
6290 {
6291 result = result_if_matched;
6292 done = TRUE;
6293 break;
6294 }
6295 if (done)
6296 break;
6297 }
6298 }
6299 else if (state->c < 0 ? check_char_class(state->c, curc)
6300 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006301 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006302 == MB_TOLOWER(state->c))))
6303 {
6304 result = result_if_matched;
6305 break;
6306 }
6307 state = state->out;
6308 }
6309 if (result)
6310 {
6311 /* next state is in out of the NFA_END_COLL, out1 of
6312 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006313 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006314 add_off = clen;
6315 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006316 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006317 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006318
6319 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006320 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006321 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006322 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006323 add_state = t->state->out;
6324 add_off = clen;
6325 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006326 break;
6327
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006328 case NFA_ANY_COMPOSING:
6329 /* On a composing character skip over it. Otherwise do
6330 * nothing. Always matches. */
6331#ifdef FEAT_MBYTE
6332 if (enc_utf8 && utf_iscomposing(curc))
6333 {
6334 add_off = clen;
6335 }
6336 else
6337#endif
6338 {
6339 add_here = TRUE;
6340 add_off = 0;
6341 }
6342 add_state = t->state->out;
6343 break;
6344
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006345 /*
6346 * Character classes like \a for alpha, \d for digit etc.
6347 */
6348 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006349 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006350 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006351 break;
6352
6353 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006354 result = !VIM_ISDIGIT(curc) && 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_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006359 result = vim_iswordp_buf(reginput, rex.reg_buf);
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_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006364 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006365 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006366 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006367 break;
6368
6369 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006370 result = vim_isfilec(curc);
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_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006375 result = !VIM_ISDIGIT(curc) && 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_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006380 result = vim_isprintc(PTR2CHAR(reginput));
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_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006385 result = !VIM_ISDIGIT(curc) && 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_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006390 result = VIM_ISWHITE(curc);
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_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006395 result = curc != NUL && !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_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006400 result = ri_digit(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_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006405 result = curc != NUL && !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_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006410 result = ri_hex(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_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006415 result = curc != NUL && !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_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006420 result = ri_octal(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_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006425 result = curc != NUL && !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_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006430 result = ri_word(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_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006435 result = curc != NUL && !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_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006440 result = ri_head(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_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006445 result = curc != NUL && !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_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006450 result = ri_alpha(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_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006455 result = curc != NUL && !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_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006460 result = ri_lower(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_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006465 result = curc != NUL && !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_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006470 result = ri_upper(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_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006475 result = curc != NUL && !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
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006479 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006480 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006481 ADD_STATE_IF_MATCH(t->state);
6482 break;
6483
6484 case NFA_NLOWER_IC: /* [^a-z] */
6485 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006486 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006487 ADD_STATE_IF_MATCH(t->state);
6488 break;
6489
6490 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006491 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006492 ADD_STATE_IF_MATCH(t->state);
6493 break;
6494
6495 case NFA_NUPPER_IC: /* ^[A-Z] */
6496 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006497 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006498 ADD_STATE_IF_MATCH(t->state);
6499 break;
6500
Bram Moolenaar5714b802013-05-28 22:03:20 +02006501 case NFA_BACKREF1:
6502 case NFA_BACKREF2:
6503 case NFA_BACKREF3:
6504 case NFA_BACKREF4:
6505 case NFA_BACKREF5:
6506 case NFA_BACKREF6:
6507 case NFA_BACKREF7:
6508 case NFA_BACKREF8:
6509 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006510#ifdef FEAT_SYN_HL
6511 case NFA_ZREF1:
6512 case NFA_ZREF2:
6513 case NFA_ZREF3:
6514 case NFA_ZREF4:
6515 case NFA_ZREF5:
6516 case NFA_ZREF6:
6517 case NFA_ZREF7:
6518 case NFA_ZREF8:
6519 case NFA_ZREF9:
6520#endif
6521 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006522 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006523 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006524 int bytelen;
6525
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006526 if (t->state->c <= NFA_BACKREF9)
6527 {
6528 subidx = t->state->c - NFA_BACKREF1 + 1;
6529 result = match_backref(&t->subs.norm, subidx, &bytelen);
6530 }
6531#ifdef FEAT_SYN_HL
6532 else
6533 {
6534 subidx = t->state->c - NFA_ZREF1 + 1;
6535 result = match_zref(subidx, &bytelen);
6536 }
6537#endif
6538
Bram Moolenaar5714b802013-05-28 22:03:20 +02006539 if (result)
6540 {
6541 if (bytelen == 0)
6542 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006543 /* empty match always works, output of NFA_SKIP to be
6544 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006545 add_here = TRUE;
6546 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006547 }
6548 else if (bytelen <= clen)
6549 {
6550 /* match current character, jump ahead to out of
6551 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006552 add_state = t->state->out->out;
6553 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006554 }
6555 else
6556 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006557 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006558 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006559 add_state = t->state->out;
6560 add_off = bytelen;
6561 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006562 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006563 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006564 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006565 }
6566 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006567 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006568 if (t->count - clen <= 0)
6569 {
6570 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006571 add_state = t->state->out;
6572 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006573 }
6574 else
6575 {
6576 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006577 add_state = t->state;
6578 add_off = 0;
6579 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006580 }
6581 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006582
Bram Moolenaar423532e2013-05-29 21:14:42 +02006583 case NFA_LNUM:
6584 case NFA_LNUM_GT:
6585 case NFA_LNUM_LT:
6586 result = (REG_MULTI &&
6587 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006588 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006589 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006590 {
6591 add_here = TRUE;
6592 add_state = t->state->out;
6593 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006594 break;
6595
6596 case NFA_COL:
6597 case NFA_COL_GT:
6598 case NFA_COL_LT:
6599 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6600 (long_u)(reginput - regline) + 1);
6601 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006602 {
6603 add_here = TRUE;
6604 add_state = t->state->out;
6605 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006606 break;
6607
6608 case NFA_VCOL:
6609 case NFA_VCOL_GT:
6610 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006611 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006612 int op = t->state->c - NFA_VCOL;
6613 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006614 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006615
6616 /* Bail out quickly when there can't be a match, avoid the
6617 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006618 if (op != 1 && col > t->state->val
6619#ifdef FEAT_MBYTE
6620 * (has_mbyte ? MB_MAXBYTES : 1)
6621#endif
6622 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006623 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006624 result = FALSE;
6625 if (op == 1 && col - 1 > t->state->val && col > 100)
6626 {
6627 int ts = wp->w_buffer->b_p_ts;
6628
6629 /* Guess that a character won't use more columns than
6630 * 'tabstop', with a minimum of 4. */
6631 if (ts < 4)
6632 ts = 4;
6633 result = col > t->state->val * ts;
6634 }
6635 if (!result)
6636 result = nfa_re_num_cmp(t->state->val, op,
6637 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006638 if (result)
6639 {
6640 add_here = TRUE;
6641 add_state = t->state->out;
6642 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006643 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006644 break;
6645
Bram Moolenaar044aa292013-06-04 21:27:38 +02006646 case NFA_MARK:
6647 case NFA_MARK_GT:
6648 case NFA_MARK_LT:
6649 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006650 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006651
6652 /* Compare the mark position to the match position. */
6653 result = (pos != NULL /* mark doesn't exist */
6654 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006655 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006656 ? (pos->col == (colnr_T)(reginput - regline)
6657 ? t->state->c == NFA_MARK
6658 : (pos->col < (colnr_T)(reginput - regline)
6659 ? t->state->c == NFA_MARK_GT
6660 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006661 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006662 ? t->state->c == NFA_MARK_GT
6663 : t->state->c == NFA_MARK_LT)));
6664 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006665 {
6666 add_here = TRUE;
6667 add_state = t->state->out;
6668 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006669 break;
6670 }
6671
Bram Moolenaar423532e2013-05-29 21:14:42 +02006672 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006673 result = (rex.reg_win != NULL
6674 && (reglnum + rex.reg_firstlnum
6675 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006676 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006677 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006678 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006679 {
6680 add_here = TRUE;
6681 add_state = t->state->out;
6682 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006683 break;
6684
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006685 case NFA_VISUAL:
6686 result = reg_match_visual();
6687 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006688 {
6689 add_here = TRUE;
6690 add_state = t->state->out;
6691 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006692 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006693
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006694 case NFA_MOPEN1:
6695 case NFA_MOPEN2:
6696 case NFA_MOPEN3:
6697 case NFA_MOPEN4:
6698 case NFA_MOPEN5:
6699 case NFA_MOPEN6:
6700 case NFA_MOPEN7:
6701 case NFA_MOPEN8:
6702 case NFA_MOPEN9:
6703#ifdef FEAT_SYN_HL
6704 case NFA_ZOPEN:
6705 case NFA_ZOPEN1:
6706 case NFA_ZOPEN2:
6707 case NFA_ZOPEN3:
6708 case NFA_ZOPEN4:
6709 case NFA_ZOPEN5:
6710 case NFA_ZOPEN6:
6711 case NFA_ZOPEN7:
6712 case NFA_ZOPEN8:
6713 case NFA_ZOPEN9:
6714#endif
6715 case NFA_NOPEN:
6716 case NFA_ZSTART:
6717 /* These states are only added to be able to bail out when
6718 * they are added again, nothing is to be done. */
6719 break;
6720
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006721 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006722 {
6723 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006724
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006725#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006726 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006727 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006728#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006729 result = (c == curc);
6730
Bram Moolenaar6100d022016-10-02 16:51:57 +02006731 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006732 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006733#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006734 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006735 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006736 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006737 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006738#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006739 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006740 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006741 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006742
6743 } /* switch (t->state->c) */
6744
6745 if (add_state != NULL)
6746 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006747 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006748 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006749
6750 if (t->pim.result == NFA_PIM_UNUSED)
6751 pim = NULL;
6752 else
6753 pim = &t->pim;
6754
6755 /* Handle the postponed invisible match if the match might end
6756 * without advancing and before the end of the line. */
6757 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006758 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006759 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006760 {
6761#ifdef ENABLE_LOG
6762 fprintf(log_fd, "\n");
6763 fprintf(log_fd, "==================================\n");
6764 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6765 fprintf(log_fd, "\n");
6766#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006767 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006768 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006769 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006770 /* for \@! and \@<! it is a match when the result is
6771 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006772 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006773 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6774 || pim->state->c
6775 == NFA_START_INVISIBLE_BEFORE_NEG
6776 || pim->state->c
6777 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006778 {
6779 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006780 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006781#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006782 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006783 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006784#endif
6785 }
6786 }
6787 else
6788 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006789 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006790#ifdef ENABLE_LOG
6791 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006792 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006793 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6794 fprintf(log_fd, "\n");
6795#endif
6796 }
6797
Bram Moolenaardecd9542013-06-07 16:31:50 +02006798 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006799 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006800 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6801 || pim->state->c
6802 == NFA_START_INVISIBLE_BEFORE_NEG
6803 || pim->state->c
6804 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006805 {
6806 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006807 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006808#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006809 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006810 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006811#endif
6812 }
6813 else
6814 /* look-behind match failed, don't add the state */
6815 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006816
6817 /* Postponed invisible match was handled, don't add it to
6818 * following states. */
6819 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006820 }
6821
Bram Moolenaara951e352013-10-06 15:46:11 +02006822 /* If "pim" points into l->t it will become invalid when
6823 * adding the state causes the list to be reallocated. Make a
6824 * local copy to avoid that. */
6825 if (pim == &t->pim)
6826 {
6827 copy_pim(&pim_copy, pim);
6828 pim = &pim_copy;
6829 }
6830
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006831 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006832 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006833 else
6834 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006835 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006836 if (add_count > 0)
6837 nextlist->t[nextlist->n - 1].count = add_count;
6838 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006839 }
6840
6841 } /* for (thislist = thislist; thislist->state; thislist++) */
6842
Bram Moolenaare23febd2013-05-26 18:40:14 +02006843 /* Look for the start of a match in the current position by adding the
6844 * start state to the list of states.
6845 * The first found match is the leftmost one, thus the order of states
6846 * matters!
6847 * Do not add the start state in recursive calls of nfa_regmatch(),
6848 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006849 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006850 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006851 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006852 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006853 && reglnum == 0
6854 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006855 && (rex.reg_maxcol == 0
6856 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006857 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006858 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006859 ? (reglnum < nfa_endp->se_u.pos.lnum
6860 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006861 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006862 < nfa_endp->se_u.pos.col))
6863 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006864 {
6865#ifdef ENABLE_LOG
6866 fprintf(log_fd, "(---) STARTSTATE\n");
6867#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006868 /* Inline optimized code for addstate() if we know the state is
6869 * the first MOPEN. */
6870 if (toplevel)
6871 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006872 int add = TRUE;
6873 int c;
6874
6875 if (prog->regstart != NUL && clen != 0)
6876 {
6877 if (nextlist->n == 0)
6878 {
6879 colnr_T col = (colnr_T)(reginput - regline) + clen;
6880
6881 /* Nextlist is empty, we can skip ahead to the
6882 * character that must appear at the start. */
6883 if (skip_to_start(prog->regstart, &col) == FAIL)
6884 break;
6885#ifdef ENABLE_LOG
6886 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6887 col - ((colnr_T)(reginput - regline) + clen));
6888#endif
6889 reginput = regline + col - clen;
6890 }
6891 else
6892 {
6893 /* Checking if the required start character matches is
6894 * cheaper than adding a state that won't match. */
6895 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006896 if (c != prog->regstart && (!rex.reg_ic
6897 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006898 {
6899#ifdef ENABLE_LOG
6900 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6901#endif
6902 add = FALSE;
6903 }
6904 }
6905 }
6906
6907 if (add)
6908 {
6909 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006910 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006911 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006912 else
6913 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006914 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006915 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006916 }
6917 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006918 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006919 }
6920
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006921#ifdef ENABLE_LOG
6922 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006923 {
6924 int i;
6925
6926 for (i = 0; i < thislist->n; i++)
6927 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6928 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006929 fprintf(log_fd, "\n");
6930#endif
6931
6932nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006933 /* Advance to the next character, or advance to the next line, or
6934 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006935 if (clen != 0)
6936 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006937 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6938 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006939 reg_nextline();
6940 else
6941 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006942
6943 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006944 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006945 if (got_int)
6946 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006947#ifdef FEAT_RELTIME
6948 /* Check for timeout once in a twenty times to avoid overhead. */
6949 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6950 {
6951 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006952 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006953 break;
6954 }
6955#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006956 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006957
6958#ifdef ENABLE_LOG
6959 if (log_fd != stderr)
6960 fclose(log_fd);
6961 log_fd = NULL;
6962#endif
6963
6964theend:
6965 /* Free memory */
6966 vim_free(list[0].t);
6967 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006968 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006969#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006970#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006971 fclose(debug);
6972#endif
6973
Bram Moolenaar963fee22013-05-26 21:47:28 +02006974 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006975}
6976
6977/*
6978 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006979 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006980 */
6981 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006982nfa_regtry(
6983 nfa_regprog_T *prog,
6984 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006985 proftime_T *tm UNUSED, /* timeout limit or NULL */
6986 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006987{
6988 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006989 regsubs_T subs, m;
6990 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006991 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992#ifdef ENABLE_LOG
6993 FILE *f;
6994#endif
6995
6996 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006997#ifdef FEAT_RELTIME
6998 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006999 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007000 nfa_time_count = 0;
7001#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007002
7003#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007004 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007005 if (f != NULL)
7006 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007007 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007008#ifdef DEBUG
7009 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7010#endif
7011 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02007012 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007013 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 fprintf(f, "\n\n");
7015 fclose(f);
7016 }
7017 else
7018 EMSG(_("Could not open temporary log file for writing "));
7019#endif
7020
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007021 clear_sub(&subs.norm);
7022 clear_sub(&m.norm);
7023#ifdef FEAT_SYN_HL
7024 clear_sub(&subs.synt);
7025 clear_sub(&m.synt);
7026#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007027
Bram Moolenaarfda37292014-11-05 14:27:36 +01007028 result = nfa_regmatch(prog, start, &subs, &m);
7029 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007031 else if (result == NFA_TOO_EXPENSIVE)
7032 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007033
7034 cleanup_subexpr();
7035 if (REG_MULTI)
7036 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007037 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007038 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007039 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7040 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007041
Bram Moolenaar6100d022016-10-02 16:51:57 +02007042 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7043 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007044 }
7045
Bram Moolenaar6100d022016-10-02 16:51:57 +02007046 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007047 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007048 rex.reg_startpos[0].lnum = 0;
7049 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007050 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007051 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007052 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007053 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007054 rex.reg_endpos[0].lnum = reglnum;
7055 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007056 }
7057 else
7058 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007059 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007060 }
7061 else
7062 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007063 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007064 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007065 rex.reg_startp[i] = subs.norm.list.line[i].start;
7066 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007067 }
7068
Bram Moolenaar6100d022016-10-02 16:51:57 +02007069 if (rex.reg_startp[0] == NULL)
7070 rex.reg_startp[0] = regline + col;
7071 if (rex.reg_endp[0] == NULL)
7072 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007073 }
7074
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007075#ifdef FEAT_SYN_HL
7076 /* Package any found \z(...\) matches for export. Default is none. */
7077 unref_extmatch(re_extmatch_out);
7078 re_extmatch_out = NULL;
7079
7080 if (prog->reghasz == REX_SET)
7081 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007082 cleanup_zsubexpr();
7083 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007084 /* Loop over \z1, \z2, etc. There is no \z0. */
7085 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007086 {
7087 if (REG_MULTI)
7088 {
7089 struct multipos *mpos = &subs.synt.list.multi[i];
7090
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007091 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007092 if (mpos->start_lnum >= 0
7093 && mpos->start_lnum == mpos->end_lnum
7094 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007095 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007096 vim_strnsave(reg_getline(mpos->start_lnum)
7097 + mpos->start_col,
7098 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007099 }
7100 else
7101 {
7102 struct linepos *lpos = &subs.synt.list.line[i];
7103
7104 if (lpos->start != NULL && lpos->end != NULL)
7105 re_extmatch_out->matches[i] =
7106 vim_strnsave(lpos->start,
7107 (int)(lpos->end - lpos->start));
7108 }
7109 }
7110 }
7111#endif
7112
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007113 return 1 + reglnum;
7114}
7115
7116/*
7117 * Match a regexp against a string ("line" points to the string) or multiple
7118 * lines ("line" is NULL, use reg_getline()).
7119 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007120 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121 */
7122 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007123nfa_regexec_both(
7124 char_u *line,
7125 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007126 proftime_T *tm, /* timeout limit or NULL */
7127 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007128{
7129 nfa_regprog_T *prog;
7130 long retval = 0L;
7131 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007132 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007133
7134 if (REG_MULTI)
7135 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007136 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007137 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007138 rex.reg_startpos = rex.reg_mmatch->startpos;
7139 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140 }
7141 else
7142 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007143 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7144 rex.reg_startp = rex.reg_match->startp;
7145 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007146 }
7147
7148 /* Be paranoid... */
7149 if (prog == NULL || line == NULL)
7150 {
7151 EMSG(_(e_null));
7152 goto theend;
7153 }
7154
Bram Moolenaar6100d022016-10-02 16:51:57 +02007155 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007156 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007157 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007159 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007160
7161#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007162 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007164 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007165#endif
7166
7167 regline = line;
7168 reglnum = 0; /* relative to line */
7169
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007170 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007171 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007172 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007173 nfa_listid = 1;
7174 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007175 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007176
Bram Moolenaard89616e2013-06-06 18:46:06 +02007177 if (prog->reganch && col > 0)
7178 return 0L;
7179
Bram Moolenaar473de612013-06-08 18:19:48 +02007180 need_clear_subexpr = TRUE;
7181#ifdef FEAT_SYN_HL
7182 /* Clear the external match subpointers if necessary. */
7183 if (prog->reghasz == REX_SET)
7184 {
7185 nfa_has_zsubexpr = TRUE;
7186 need_clear_zsubexpr = TRUE;
7187 }
7188 else
7189 nfa_has_zsubexpr = FALSE;
7190#endif
7191
Bram Moolenaard89616e2013-06-06 18:46:06 +02007192 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007193 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007194 /* Skip ahead until a character we know the match must start with.
7195 * When there is none there is no match. */
7196 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007197 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007198
Bram Moolenaar473de612013-06-08 18:19:48 +02007199 /* If match_text is set it contains the full text that must match.
7200 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007201 if (prog->match_text != NULL
7202#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007203 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007204#endif
7205 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007206 return find_match_text(col, prog->regstart, prog->match_text);
7207 }
7208
Bram Moolenaard89616e2013-06-06 18:46:06 +02007209 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007210 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007211 goto theend;
7212
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007213 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007214 for (i = 0; i < nstate; ++i)
7215 {
7216 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007217 prog->state[i].lastlist[0] = 0;
7218 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007219 }
7220
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007221 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007222
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007223 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007225theend:
7226 return retval;
7227}
7228
7229/*
7230 * Compile a regular expression into internal code for the NFA matcher.
7231 * Returns the program in allocated space. Returns NULL for an error.
7232 */
7233 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007234nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007235{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007236 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007237 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007238 int *postfix;
7239
7240 if (expr == NULL)
7241 return NULL;
7242
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007243 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007244 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007245
7246 init_class_tab();
7247
7248 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7249 return NULL;
7250
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007251 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007252 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007253 postfix = re2post();
7254 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007255 {
7256 /* TODO: only give this error for debugging? */
7257 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007258 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007259 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007260 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007261
7262 /*
7263 * In order to build the NFA, we parse the input regexp twice:
7264 * 1. first pass to count size (so we can allocate space)
7265 * 2. second to emit code
7266 */
7267#ifdef ENABLE_LOG
7268 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007269 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007270
7271 if (f != NULL)
7272 {
7273 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7274 fclose(f);
7275 }
7276 }
7277#endif
7278
7279 /*
7280 * PASS 1
7281 * Count number of NFA states in "nstate". Do not build the NFA.
7282 */
7283 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007284
Bram Moolenaar16619a22013-06-11 18:42:36 +02007285 /* allocate the regprog with space for the compiled regexp */
7286 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007287 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7288 if (prog == NULL)
7289 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007290 state_ptr = prog->state;
7291
7292 /*
7293 * PASS 2
7294 * Build the NFA
7295 */
7296 prog->start = post2nfa(postfix, post_ptr, FALSE);
7297 if (prog->start == NULL)
7298 goto fail;
7299
7300 prog->regflags = regflags;
7301 prog->engine = &nfa_regengine;
7302 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007303 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007304 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007305 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007306
Bram Moolenaara2947e22013-06-11 22:44:09 +02007307 nfa_postprocess(prog);
7308
Bram Moolenaard89616e2013-06-06 18:46:06 +02007309 prog->reganch = nfa_get_reganch(prog->start, 0);
7310 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007311 prog->match_text = nfa_get_match_text(prog->start);
7312
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007313#ifdef ENABLE_LOG
7314 nfa_postfix_dump(expr, OK);
7315 nfa_dump(prog);
7316#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007317#ifdef FEAT_SYN_HL
7318 /* Remember whether this pattern has any \z specials in it. */
7319 prog->reghasz = re_has_z;
7320#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007321 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007322 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007323
7324out:
7325 vim_free(post_start);
7326 post_start = post_ptr = post_end = NULL;
7327 state_ptr = NULL;
7328 return (regprog_T *)prog;
7329
7330fail:
7331 vim_free(prog);
7332 prog = NULL;
7333#ifdef ENABLE_LOG
7334 nfa_postfix_dump(expr, FAIL);
7335#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007336 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007337 goto out;
7338}
7339
Bram Moolenaar473de612013-06-08 18:19:48 +02007340/*
7341 * Free a compiled regexp program, returned by nfa_regcomp().
7342 */
7343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007344nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007345{
7346 if (prog != NULL)
7347 {
7348 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007349 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007350 vim_free(prog);
7351 }
7352}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007353
7354/*
7355 * Match a regexp against a string.
7356 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7357 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007358 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007359 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007360 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007361 */
7362 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007363nfa_regexec_nl(
7364 regmatch_T *rmp,
7365 char_u *line, /* string to match against */
7366 colnr_T col, /* column to start looking for match */
7367 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007368{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007369 rex.reg_match = rmp;
7370 rex.reg_mmatch = NULL;
7371 rex.reg_maxline = 0;
7372 rex.reg_line_lbr = line_lbr;
7373 rex.reg_buf = curbuf;
7374 rex.reg_win = NULL;
7375 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007376#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007377 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007378#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007379 rex.reg_maxcol = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007380 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007381}
7382
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007383
7384/*
7385 * Match a regexp against multiple lines.
7386 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7387 * Uses curbuf for line count and 'iskeyword'.
7388 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007389 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007390 * match otherwise.
7391 *
7392 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7393 *
7394 * ! Also NOTE : match may actually be in another line. e.g.:
7395 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7396 *
7397 * +-------------------------+
7398 * |a |
7399 * |b |
7400 * |c |
7401 * | |
7402 * +-------------------------+
7403 *
7404 * then nfa_regexec_multi() returns 3. while the original
7405 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7406 *
7407 * FIXME if this behavior is not compatible.
7408 */
7409 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007410nfa_regexec_multi(
7411 regmmatch_T *rmp,
7412 win_T *win, /* window in which to search or NULL */
7413 buf_T *buf, /* buffer in which to search */
7414 linenr_T lnum, /* nr of line to start looking for match */
7415 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007416 proftime_T *tm, /* timeout limit or NULL */
7417 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007418{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007419 rex.reg_match = NULL;
7420 rex.reg_mmatch = rmp;
7421 rex.reg_buf = buf;
7422 rex.reg_win = win;
7423 rex.reg_firstlnum = lnum;
7424 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7425 rex.reg_line_lbr = FALSE;
7426 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007427#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007428 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007429#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007430 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007431
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007432 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007433}
7434
7435#ifdef DEBUG
7436# undef ENABLE_LOG
7437#endif