blob: 120861a46b575b0c9031d1ac76573993f05a41dc [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);
314static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm);
315static long nfa_regexec_both(char_u *line, colnr_T col, proftime_T *tm);
316static 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);
319static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm);
320static 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 }
631 else
632 if (*(p + 2) == '7')
633 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200634 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 break;
636 }
637 case 'a':
638 if (*(p + 2) == 'z')
639 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 break;
642 }
643 else
644 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 }
649 case 'A':
650 if (*(p + 2) == 'Z')
651 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200652 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200653 break;
654 }
655 else
656 if (*(p + 2) == 'F')
657 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200658 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200659 break;
660 }
661 /* FALLTHROUGH */
662 default:
663 return FAIL;
664 }
665 p += 3;
666 }
667 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
668 {
669 newl = TRUE;
670 p += 2;
671 }
672 else if (*p == '_')
673 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200674 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200675 p ++;
676 }
677 else if (*p == '\n')
678 {
679 newl = TRUE;
680 p ++;
681 }
682 else
683 return FAIL;
684 } /* while (p < end) */
685
686 if (p != end)
687 return FAIL;
688
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200689 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200690 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200691
692 switch (config)
693 {
694 case CLASS_o9:
695 return extra_newl + NFA_DIGIT;
696 case CLASS_not | CLASS_o9:
697 return extra_newl + NFA_NDIGIT;
698 case CLASS_af | CLASS_AF | CLASS_o9:
699 return extra_newl + NFA_HEX;
700 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
701 return extra_newl + NFA_NHEX;
702 case CLASS_o7:
703 return extra_newl + NFA_OCTAL;
704 case CLASS_not | CLASS_o7:
705 return extra_newl + NFA_NOCTAL;
706 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
707 return extra_newl + NFA_WORD;
708 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
709 return extra_newl + NFA_NWORD;
710 case CLASS_az | CLASS_AZ | CLASS_underscore:
711 return extra_newl + NFA_HEAD;
712 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
713 return extra_newl + NFA_NHEAD;
714 case CLASS_az | CLASS_AZ:
715 return extra_newl + NFA_ALPHA;
716 case CLASS_not | CLASS_az | CLASS_AZ:
717 return extra_newl + NFA_NALPHA;
718 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200719 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200720 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200721 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200722 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200723 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200724 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200726 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200727 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728}
729
730/*
731 * Produce the bytes for equivalence class "c".
732 * Currently only handles latin1, latin9 and utf-8.
733 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
734 * equivalent to 'a OR b OR c'
735 *
736 * NOTE! When changing this function, also update reg_equi_class()
737 */
738 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100739nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200740{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200741#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
742#ifdef FEAT_MBYTE
743# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
744#else
745# define EMITMBC(c)
746#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747
748#ifdef FEAT_MBYTE
749 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
750 || STRCMP(p_enc, "iso-8859-15") == 0)
751#endif
752 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200753#ifdef EBCDIC
754# define A_circumflex 0x62
755# define A_diaeresis 0x63
756# define A_grave 0x64
757# define A_acute 0x65
758# define A_virguilla 0x66
759# define A_ring 0x67
760# define C_cedilla 0x68
761# define E_acute 0x71
762# define E_circumflex 0x72
763# define E_diaeresis 0x73
764# define E_grave 0x74
765# define I_acute 0x75
766# define I_circumflex 0x76
767# define I_diaeresis 0x77
768# define I_grave 0x78
769# define N_virguilla 0x69
770# define O_circumflex 0xeb
771# define O_diaeresis 0xec
772# define O_grave 0xed
773# define O_acute 0xee
774# define O_virguilla 0xef
775# define O_slash 0x80
776# define U_circumflex 0xfb
777# define U_diaeresis 0xfc
778# define U_grave 0xfd
779# define U_acute 0xfe
780# define Y_acute 0xba
781# define a_grave 0x42
782# define a_acute 0x43
783# define a_circumflex 0x44
784# define a_virguilla 0x45
785# define a_diaeresis 0x46
786# define a_ring 0x47
787# define c_cedilla 0x48
788# define e_grave 0x51
789# define e_acute 0x52
790# define e_circumflex 0x53
791# define e_diaeresis 0x54
792# define i_grave 0x55
793# define i_acute 0x56
794# define i_circumflex 0x57
795# define i_diaeresis 0x58
796# define n_virguilla 0x49
797# define o_grave 0xcb
798# define o_acute 0xcc
799# define o_circumflex 0xcd
800# define o_virguilla 0xce
801# define o_diaeresis 0xcf
802# define o_slash 0x70
803# define u_grave 0xdb
804# define u_acute 0xdc
805# define u_circumflex 0xdd
806# define u_diaeresis 0xde
807# define y_acute 0x8d
808# define y_diaeresis 0xdf
809#else
810# define A_grave 0xc0
811# define A_acute 0xc1
812# define A_circumflex 0xc2
813# define A_virguilla 0xc3
814# define A_diaeresis 0xc4
815# define A_ring 0xc5
816# define C_cedilla 0xc7
817# define E_grave 0xc8
818# define E_acute 0xc9
819# define E_circumflex 0xca
820# define E_diaeresis 0xcb
821# define I_grave 0xcc
822# define I_acute 0xcd
823# define I_circumflex 0xce
824# define I_diaeresis 0xcf
825# define N_virguilla 0xd1
826# define O_grave 0xd2
827# define O_acute 0xd3
828# define O_circumflex 0xd4
829# define O_virguilla 0xd5
830# define O_diaeresis 0xd6
831# define O_slash 0xd8
832# define U_grave 0xd9
833# define U_acute 0xda
834# define U_circumflex 0xdb
835# define U_diaeresis 0xdc
836# define Y_acute 0xdd
837# define a_grave 0xe0
838# define a_acute 0xe1
839# define a_circumflex 0xe2
840# define a_virguilla 0xe3
841# define a_diaeresis 0xe4
842# define a_ring 0xe5
843# define c_cedilla 0xe7
844# define e_grave 0xe8
845# define e_acute 0xe9
846# define e_circumflex 0xea
847# define e_diaeresis 0xeb
848# define i_grave 0xec
849# define i_acute 0xed
850# define i_circumflex 0xee
851# define i_diaeresis 0xef
852# define n_virguilla 0xf1
853# define o_grave 0xf2
854# define o_acute 0xf3
855# define o_circumflex 0xf4
856# define o_virguilla 0xf5
857# define o_diaeresis 0xf6
858# define o_slash 0xf8
859# define u_grave 0xf9
860# define u_acute 0xfa
861# define u_circumflex 0xfb
862# define u_diaeresis 0xfc
863# define y_acute 0xfd
864# define y_diaeresis 0xff
865#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200866 switch (c)
867 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200868 case 'A': case A_grave: case A_acute: case A_circumflex:
869 case A_virguilla: case A_diaeresis: case A_ring:
870 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
871 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
872 CASEMBC(0x1ea2)
873 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
874 EMIT2(A_circumflex); EMIT2(A_virguilla);
875 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200876 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
877 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
878 EMITMBC(0x1ea2)
879 return OK;
880
881 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
882 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200883 return OK;
884
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200885 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
886 CASEMBC(0x10a) CASEMBC(0x10c)
887 EMIT2('C'); EMIT2(C_cedilla);
888 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200889 EMITMBC(0x10a) EMITMBC(0x10c)
890 return OK;
891
892 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200893 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200894 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
895 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200896 return OK;
897
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200898 case 'E': case E_grave: case E_acute: case E_circumflex:
899 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
900 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
901 CASEMBC(0x1eba) CASEMBC(0x1ebc)
902 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
903 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200904 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
905 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
906 EMITMBC(0x1ebc)
907 return OK;
908
909 case 'F': CASEMBC(0x1e1e)
910 EMIT2('F'); EMITMBC(0x1e1e)
911 return OK;
912
913 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200914 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
915 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200916 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
917 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
918 EMITMBC(0x1f4) EMITMBC(0x1e20)
919 return OK;
920
921 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200922 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200923 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
924 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200925 return OK;
926
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200927 case 'I': case I_grave: case I_acute: case I_circumflex:
928 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
929 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
930 CASEMBC(0x1cf) CASEMBC(0x1ec8)
931 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
932 EMIT2(I_circumflex); EMIT2(I_diaeresis);
933 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200934 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
935 EMITMBC(0x1cf) EMITMBC(0x1ec8)
936 return OK;
937
938 case 'J': CASEMBC(0x134)
939 EMIT2('J'); EMITMBC(0x134)
940 return OK;
941
942 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200943 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200944 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
945 EMITMBC(0x1e34)
946 return OK;
947
948 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200949 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200950 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
951 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
952 return OK;
953
954 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
955 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200956 return OK;
957
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200958 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
959 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
960 EMIT2('N'); EMIT2(N_virguilla);
961 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200962 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200963 return OK;
964
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200965 case 'O': case O_grave: case O_acute: case O_circumflex:
966 case O_virguilla: case O_diaeresis: case O_slash:
967 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
968 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
969 CASEMBC(0x1ec) CASEMBC(0x1ece)
970 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
971 EMIT2(O_circumflex); EMIT2(O_virguilla);
972 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200973 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
974 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
975 EMITMBC(0x1ec) EMITMBC(0x1ece)
976 return OK;
977
978 case 'P': case 0x1e54: case 0x1e56:
979 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
980 return OK;
981
982 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200983 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200984 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
985 EMITMBC(0x1e58) EMITMBC(0x1e5e)
986 return OK;
987
988 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200989 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200990 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
991 EMITMBC(0x160) EMITMBC(0x1e60)
992 return OK;
993
994 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200995 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200996 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
997 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200998 return OK;
999
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001000 case 'U': case U_grave: case U_acute: case U_diaeresis:
1001 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
1002 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
1003 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
1004 CASEMBC(0x1ee6)
1005 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
1006 EMIT2(U_diaeresis); EMIT2(U_circumflex);
1007 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001008 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
1009 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
1010 EMITMBC(0x1ee6)
1011 return OK;
1012
1013 case 'V': CASEMBC(0x1e7c)
1014 EMIT2('V'); EMITMBC(0x1e7c)
1015 return OK;
1016
1017 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001018 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001019 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
1020 EMITMBC(0x1e84) EMITMBC(0x1e86)
1021 return OK;
1022
1023 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
1024 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001025 return OK;
1026
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001027 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1028 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1029 CASEMBC(0x1ef8)
1030 EMIT2('Y'); EMIT2(Y_acute);
1031 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001032 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1033 EMITMBC(0x1ef8)
1034 return OK;
1035
1036 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001037 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001038 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1039 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001040 return OK;
1041
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001042 case 'a': case a_grave: case a_acute: case a_circumflex:
1043 case a_virguilla: case a_diaeresis: case a_ring:
1044 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1045 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1046 CASEMBC(0x1ea3)
1047 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1048 EMIT2(a_circumflex); EMIT2(a_virguilla);
1049 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001050 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1051 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1052 EMITMBC(0x1ea3)
1053 return OK;
1054
1055 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1056 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001057 return OK;
1058
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001059 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1060 CASEMBC(0x10b) CASEMBC(0x10d)
1061 EMIT2('c'); EMIT2(c_cedilla);
1062 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001063 EMITMBC(0x10b) EMITMBC(0x10d)
1064 return OK;
1065
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001066 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001067 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001068 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1069 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001070 return OK;
1071
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001072 case 'e': case e_grave: case e_acute: case e_circumflex:
1073 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1074 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1075 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1076 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1077 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1078 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001079 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1080 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1081 return OK;
1082
1083 case 'f': CASEMBC(0x1e1f)
1084 EMIT2('f'); EMITMBC(0x1e1f)
1085 return OK;
1086
1087 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001088 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1089 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001090 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1091 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1092 EMITMBC(0x1f5) EMITMBC(0x1e21)
1093 return OK;
1094
1095 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001096 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001097 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1098 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001099 return OK;
1100
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001101 case 'i': case i_grave: case i_acute: case i_circumflex:
1102 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1103 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1104 CASEMBC(0x1ec9)
1105 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1106 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1107 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001108 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1109 EMITMBC(0x1ec9)
1110 return OK;
1111
1112 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1113 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1114 return OK;
1115
1116 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001117 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001118 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1119 EMITMBC(0x1e35)
1120 return OK;
1121
1122 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001123 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001124 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1125 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1126 return OK;
1127
1128 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1129 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001130 return OK;
1131
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001132 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1133 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1134 CASEMBC(0x1e49)
1135 EMIT2('n'); EMIT2(n_virguilla);
1136 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001137 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1138 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 return OK;
1140
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001141 case 'o': case o_grave: case o_acute: case o_circumflex:
1142 case o_virguilla: case o_diaeresis: case o_slash:
1143 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1144 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1145 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1146 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1147 EMIT2(o_circumflex); EMIT2(o_virguilla);
1148 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001149 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1150 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1151 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1152 return OK;
1153
1154 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1155 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1156 return OK;
1157
1158 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001159 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001160 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1161 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1162 return OK;
1163
1164 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001165 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001166 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1167 EMITMBC(0x161) EMITMBC(0x1e61)
1168 return OK;
1169
1170 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001171 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001172 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1173 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001174 return OK;
1175
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001176 case 'u': case u_grave: case u_acute: case u_circumflex:
1177 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1178 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1179 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1180 CASEMBC(0x1ee7)
1181 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1182 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1183 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001184 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1185 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1186 EMITMBC(0x1ee7)
1187 return OK;
1188
1189 case 'v': CASEMBC(0x1e7d)
1190 EMIT2('v'); EMITMBC(0x1e7d)
1191 return OK;
1192
1193 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001194 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001195 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1196 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1197 return OK;
1198
1199 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1200 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001201 return OK;
1202
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001203 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1204 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1205 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1206 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1207 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001208 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1209 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001210 return OK;
1211
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001212 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001213 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001214 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1215 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1216 return OK;
1217
1218 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001219 }
1220 }
1221
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001222 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001223 return OK;
1224#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001225#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001226}
1227
1228/*
1229 * Code to parse regular expression.
1230 *
1231 * We try to reuse parsing functions in regexp.c to
1232 * minimize surprise and keep the syntax consistent.
1233 */
1234
1235/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236 * Parse the lowest level.
1237 *
1238 * An atom can be one of a long list of items. Many atoms match one character
1239 * in the text. It is often an ordinary character or a character class.
1240 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1241 * is only for syntax highlighting.
1242 *
1243 * atom ::= ordinary-atom
1244 * or \( pattern \)
1245 * or \%( pattern \)
1246 * or \z( pattern \)
1247 */
1248 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001249nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001250{
1251 int c;
1252 int charclass;
1253 int equiclass;
1254 int collclass;
1255 int got_coll_char;
1256 char_u *p;
1257 char_u *endp;
1258#ifdef FEAT_MBYTE
1259 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260#endif
1261 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 int emit_range;
1263 int negated;
1264 int result;
1265 int startc = -1;
1266 int endc = -1;
1267 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001268 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001270 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271 switch (c)
1272 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001273 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001274 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001275
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001276 case Magic('^'):
1277 EMIT(NFA_BOL);
1278 break;
1279
1280 case Magic('$'):
1281 EMIT(NFA_EOL);
1282#if defined(FEAT_SYN_HL) || defined(PROTO)
1283 had_eol = TRUE;
1284#endif
1285 break;
1286
1287 case Magic('<'):
1288 EMIT(NFA_BOW);
1289 break;
1290
1291 case Magic('>'):
1292 EMIT(NFA_EOW);
1293 break;
1294
1295 case Magic('_'):
1296 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001297 if (c == NUL)
1298 EMSG_RET_FAIL(_(e_nul_found));
1299
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001300 if (c == '^') /* "\_^" is start-of-line */
1301 {
1302 EMIT(NFA_BOL);
1303 break;
1304 }
1305 if (c == '$') /* "\_$" is end-of-line */
1306 {
1307 EMIT(NFA_EOL);
1308#if defined(FEAT_SYN_HL) || defined(PROTO)
1309 had_eol = TRUE;
1310#endif
1311 break;
1312 }
1313
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001314 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315
1316 /* "\_[" is collection plus newline */
1317 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001318 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319
1320 /* "\_x" is character class plus newline */
1321 /*FALLTHROUGH*/
1322
1323 /*
1324 * Character classes.
1325 */
1326 case Magic('.'):
1327 case Magic('i'):
1328 case Magic('I'):
1329 case Magic('k'):
1330 case Magic('K'):
1331 case Magic('f'):
1332 case Magic('F'):
1333 case Magic('p'):
1334 case Magic('P'):
1335 case Magic('s'):
1336 case Magic('S'):
1337 case Magic('d'):
1338 case Magic('D'):
1339 case Magic('x'):
1340 case Magic('X'):
1341 case Magic('o'):
1342 case Magic('O'):
1343 case Magic('w'):
1344 case Magic('W'):
1345 case Magic('h'):
1346 case Magic('H'):
1347 case Magic('a'):
1348 case Magic('A'):
1349 case Magic('l'):
1350 case Magic('L'):
1351 case Magic('u'):
1352 case Magic('U'):
1353 p = vim_strchr(classchars, no_Magic(c));
1354 if (p == NULL)
1355 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001356 if (extra == NFA_ADD_NL)
1357 {
1358 EMSGN(_(e_ill_char_class), c);
1359 rc_did_emsg = TRUE;
1360 return FAIL;
1361 }
Bram Moolenaarde330112017-01-08 20:50:52 +01001362 IEMSGN("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001363 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 }
1365#ifdef FEAT_MBYTE
1366 /* When '.' is followed by a composing char ignore the dot, so that
1367 * the composing char is matched here. */
1368 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1369 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001370 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001371 c = getchr();
1372 goto nfa_do_multibyte;
1373 }
1374#endif
1375 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001376 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 {
1378 EMIT(NFA_NEWL);
1379 EMIT(NFA_OR);
1380 regflags |= RF_HASNL;
1381 }
1382 break;
1383
1384 case Magic('n'):
1385 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001386 /* In a string "\n" matches a newline character. */
1387 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001388 else
1389 {
1390 /* In buffer text "\n" matches the end of a line. */
1391 EMIT(NFA_NEWL);
1392 regflags |= RF_HASNL;
1393 }
1394 break;
1395
1396 case Magic('('):
1397 if (nfa_reg(REG_PAREN) == FAIL)
1398 return FAIL; /* cascaded error */
1399 break;
1400
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401 case Magic('|'):
1402 case Magic('&'):
1403 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001404 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 return FAIL;
1406
1407 case Magic('='):
1408 case Magic('?'):
1409 case Magic('+'):
1410 case Magic('@'):
1411 case Magic('*'):
1412 case Magic('{'):
1413 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001414 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 return FAIL;
1416
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001417 case Magic('~'):
1418 {
1419 char_u *lp;
1420
1421 /* Previous substitute pattern.
1422 * Generated as "\%(pattern\)". */
1423 if (reg_prev_sub == NULL)
1424 {
1425 EMSG(_(e_nopresub));
1426 return FAIL;
1427 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001428 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001429 {
1430 EMIT(PTR2CHAR(lp));
1431 if (lp != reg_prev_sub)
1432 EMIT(NFA_CONCAT);
1433 }
1434 EMIT(NFA_NOPEN);
1435 break;
1436 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437
Bram Moolenaar428e9872013-05-30 17:05:39 +02001438 case Magic('1'):
1439 case Magic('2'):
1440 case Magic('3'):
1441 case Magic('4'):
1442 case Magic('5'):
1443 case Magic('6'):
1444 case Magic('7'):
1445 case Magic('8'):
1446 case Magic('9'):
1447 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1448 nfa_has_backref = TRUE;
1449 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001450
1451 case Magic('z'):
1452 c = no_Magic(getchr());
1453 switch (c)
1454 {
1455 case 's':
1456 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001457 if (re_mult_next("\\zs") == FAIL)
1458 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001459 break;
1460 case 'e':
1461 EMIT(NFA_ZEND);
1462 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001463 if (re_mult_next("\\ze") == FAIL)
1464 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001465 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001466#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 case '1':
1468 case '2':
1469 case '3':
1470 case '4':
1471 case '5':
1472 case '6':
1473 case '7':
1474 case '8':
1475 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001476 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001477 if (reg_do_extmatch != REX_USE)
1478 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001479 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1480 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001481 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001482 re_has_z = REX_USE;
1483 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001484 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001485 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001486 if (reg_do_extmatch != REX_SET)
1487 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001488 if (nfa_reg(REG_ZPAREN) == FAIL)
1489 return FAIL; /* cascaded error */
1490 re_has_z = REX_SET;
1491 break;
1492#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001494 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 no_Magic(c));
1496 return FAIL;
1497 }
1498 break;
1499
1500 case Magic('%'):
1501 c = no_Magic(getchr());
1502 switch (c)
1503 {
1504 /* () without a back reference */
1505 case '(':
1506 if (nfa_reg(REG_NPAREN) == FAIL)
1507 return FAIL;
1508 EMIT(NFA_NOPEN);
1509 break;
1510
1511 case 'd': /* %d123 decimal */
1512 case 'o': /* %o123 octal */
1513 case 'x': /* %xab hex 2 */
1514 case 'u': /* %uabcd hex 4 */
1515 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001516 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001517 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518
Bram Moolenaar47196582013-05-25 22:04:23 +02001519 switch (c)
1520 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001521 case 'd': nr = getdecchrs(); break;
1522 case 'o': nr = getoctchrs(); break;
1523 case 'x': nr = gethexchrs(2); break;
1524 case 'u': nr = gethexchrs(4); break;
1525 case 'U': nr = gethexchrs(8); break;
1526 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001527 }
1528
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001529 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001530 EMSG2_RET_FAIL(
1531 _("E678: Invalid character after %s%%[dxouU]"),
1532 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001533 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001534 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001535 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 break;
1538
1539 /* Catch \%^ and \%$ regardless of where they appear in the
1540 * pattern -- regardless of whether or not it makes sense. */
1541 case '^':
1542 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 break;
1544
1545 case '$':
1546 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001547 break;
1548
1549 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001550 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 break;
1552
1553 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001554 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 break;
1556
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001557 case 'C':
1558 EMIT(NFA_ANY_COMPOSING);
1559 break;
1560
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001562 {
1563 int n;
1564
1565 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001566 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001567 {
1568 if (c == NUL)
1569 EMSG2_RET_FAIL(_(e_missing_sb),
1570 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001571 /* recursive call! */
1572 if (nfa_regatom() == FAIL)
1573 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001574 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001575 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001576 if (n == 0)
1577 EMSG2_RET_FAIL(_(e_empty_sb),
1578 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001579 EMIT(NFA_OPT_CHARS);
1580 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001581
1582 /* Emit as "\%(\%[abc]\)" to be able to handle
1583 * "\%[abc]*" which would cause the empty string to be
1584 * matched an unlimited number of times. NFA_NOPEN is
1585 * added only once at a position, while NFA_SPLIT is
1586 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001587 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001588 * a lot. */
1589 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001590 break;
1591 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001592
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001594 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001595 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001596 int cmp = c;
1597
1598 if (c == '<' || c == '>')
1599 c = getchr();
1600 while (VIM_ISDIGIT(c))
1601 {
1602 n = n * 10 + (c - '0');
1603 c = getchr();
1604 }
1605 if (c == 'l' || c == 'c' || c == 'v')
1606 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001607 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001608 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001609 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001610 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001611 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001612 if (save_prev_at_start)
1613 at_start = TRUE;
1614 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001615 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001616 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001617 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001618 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001619 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001620 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001621 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001622 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001623 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001624 break;
1625 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001626 else if (c == '\'' && n == 0)
1627 {
1628 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001629 EMIT(cmp == '<' ? NFA_MARK_LT :
1630 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001631 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001632 break;
1633 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001634 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001635 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1636 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 return FAIL;
1638 }
1639 break;
1640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001641 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001642collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001643 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001644 * [abc] uses NFA_START_COLL - NFA_END_COLL
1645 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1646 * Each character is produced as a regular state, using
1647 * NFA_CONCAT to bind them together.
1648 * Besides normal characters there can be:
1649 * - character classes NFA_CLASS_*
1650 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001651 */
1652
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 p = regparse;
1654 endp = skip_anyof(p);
1655 if (*endp == ']')
1656 {
1657 /*
1658 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001659 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001660 * and perform the necessary substitutions in the NFA.
1661 */
1662 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001663 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001664 if (result != FAIL)
1665 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001666 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001667 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001668 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001669 EMIT(NFA_NEWL);
1670 EMIT(NFA_OR);
1671 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001672 else
1673 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001674 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001675 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 return OK;
1677 }
1678 /*
1679 * Failed to recognize a character class. Use the simple
1680 * version that turns [abc] into 'a' OR 'b' OR 'c'
1681 */
1682 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001683 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 if (*regparse == '^') /* negated range */
1685 {
1686 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001687 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001688 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001690 else
1691 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001692 if (*regparse == '-')
1693 {
1694 startc = '-';
1695 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001696 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001697 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698 }
1699 /* Emit the OR branches for each character in the [] */
1700 emit_range = FALSE;
1701 while (regparse < endp)
1702 {
1703 oldstartc = startc;
1704 startc = -1;
1705 got_coll_char = FALSE;
1706 if (*regparse == '[')
1707 {
1708 /* Check for [: :], [= =], [. .] */
1709 equiclass = collclass = 0;
1710 charclass = get_char_class(&regparse);
1711 if (charclass == CLASS_NONE)
1712 {
1713 equiclass = get_equi_class(&regparse);
1714 if (equiclass == 0)
1715 collclass = get_coll_element(&regparse);
1716 }
1717
1718 /* Character class like [:alpha:] */
1719 if (charclass != CLASS_NONE)
1720 {
1721 switch (charclass)
1722 {
1723 case CLASS_ALNUM:
1724 EMIT(NFA_CLASS_ALNUM);
1725 break;
1726 case CLASS_ALPHA:
1727 EMIT(NFA_CLASS_ALPHA);
1728 break;
1729 case CLASS_BLANK:
1730 EMIT(NFA_CLASS_BLANK);
1731 break;
1732 case CLASS_CNTRL:
1733 EMIT(NFA_CLASS_CNTRL);
1734 break;
1735 case CLASS_DIGIT:
1736 EMIT(NFA_CLASS_DIGIT);
1737 break;
1738 case CLASS_GRAPH:
1739 EMIT(NFA_CLASS_GRAPH);
1740 break;
1741 case CLASS_LOWER:
1742 EMIT(NFA_CLASS_LOWER);
1743 break;
1744 case CLASS_PRINT:
1745 EMIT(NFA_CLASS_PRINT);
1746 break;
1747 case CLASS_PUNCT:
1748 EMIT(NFA_CLASS_PUNCT);
1749 break;
1750 case CLASS_SPACE:
1751 EMIT(NFA_CLASS_SPACE);
1752 break;
1753 case CLASS_UPPER:
1754 EMIT(NFA_CLASS_UPPER);
1755 break;
1756 case CLASS_XDIGIT:
1757 EMIT(NFA_CLASS_XDIGIT);
1758 break;
1759 case CLASS_TAB:
1760 EMIT(NFA_CLASS_TAB);
1761 break;
1762 case CLASS_RETURN:
1763 EMIT(NFA_CLASS_RETURN);
1764 break;
1765 case CLASS_BACKSPACE:
1766 EMIT(NFA_CLASS_BACKSPACE);
1767 break;
1768 case CLASS_ESCAPE:
1769 EMIT(NFA_CLASS_ESCAPE);
1770 break;
1771 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001772 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 continue;
1774 }
1775 /* Try equivalence class [=a=] and the like */
1776 if (equiclass != 0)
1777 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001778 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001779 if (result == FAIL)
1780 {
1781 /* should never happen */
1782 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1783 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001784 continue;
1785 }
1786 /* Try collating class like [. .] */
1787 if (collclass != 0)
1788 {
1789 startc = collclass; /* allow [.a.]-x as a range */
1790 /* Will emit the proper atom at the end of the
1791 * while loop. */
1792 }
1793 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001794 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1795 * start character. */
1796 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 {
1798 emit_range = TRUE;
1799 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001800 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 continue; /* reading the end of the range */
1802 }
1803
1804 /* Now handle simple and escaped characters.
1805 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1806 * accepts "\t", "\e", etc., but only when the 'l' flag in
1807 * 'cpoptions' is not included.
1808 * Posix doesn't recognize backslash at all.
1809 */
1810 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001811 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812 && regparse + 1 <= endp
1813 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001814 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001815 && vim_strchr(REGEXP_ABBR, regparse[1])
1816 != NULL)
1817 )
1818 )
1819 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001820 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001822 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823 startc = reg_string ? NL : NFA_NEWL;
1824 else
1825 if (*regparse == 'd'
1826 || *regparse == 'o'
1827 || *regparse == 'x'
1828 || *regparse == 'u'
1829 || *regparse == 'U'
1830 )
1831 {
1832 /* TODO(RE) This needs more testing */
1833 startc = coll_get_char();
1834 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001835 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836 }
1837 else
1838 {
1839 /* \r,\t,\e,\b */
1840 startc = backslash_trans(*regparse);
1841 }
1842 }
1843
1844 /* Normal printable char */
1845 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001846 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847
1848 /* Previous char was '-', so this char is end of range. */
1849 if (emit_range)
1850 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001851 endc = startc;
1852 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001853 if (startc > endc)
1854 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001855
1856 if (endc > startc + 2)
1857 {
1858 /* Emit a range instead of the sequence of
1859 * individual characters. */
1860 if (startc == 0)
1861 /* \x00 is translated to \x0a, start at \x01. */
1862 EMIT(1);
1863 else
1864 --post_ptr; /* remove NFA_CONCAT */
1865 EMIT(endc);
1866 EMIT(NFA_RANGE);
1867 EMIT(NFA_CONCAT);
1868 }
1869 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001870#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001871 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001872 || (*mb_char2len)(endc) > 1))
1873 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001874 /* Emit the characters in the range.
1875 * "startc" was already emitted, so skip it.
1876 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 for (c = startc + 1; c <= endc; c++)
1878 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001879 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001880 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001881 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 }
1883 else
1884#endif
1885 {
1886#ifdef EBCDIC
1887 int alpha_only = FALSE;
1888
1889 /* for alphabetical range skip the gaps
1890 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1891 if (isalpha(startc) && isalpha(endc))
1892 alpha_only = TRUE;
1893#endif
1894 /* Emit the range. "startc" was already emitted, so
1895 * skip it. */
1896 for (c = startc + 1; c <= endc; c++)
1897#ifdef EBCDIC
1898 if (!alpha_only || isalpha(startc))
1899#endif
1900 {
1901 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001902 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001905 emit_range = FALSE;
1906 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907 }
1908 else
1909 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001910 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001912 * Normally, simply emit startc. But if we get char
1913 * code=0 from a collating char, then replace it with
1914 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001915 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001916 * the backtracking engine. */
1917 if (startc == NFA_NEWL)
1918 {
1919 /* Line break can't be matched as part of the
1920 * collection, add an OR below. But not for negated
1921 * range. */
1922 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001923 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001924 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001925 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001926 {
1927 if (got_coll_char == TRUE && startc == 0)
1928 EMIT(0x0a);
1929 else
1930 EMIT(startc);
1931 EMIT(NFA_CONCAT);
1932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001933 }
1934
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001935 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 } /* while (p < endp) */
1937
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001938 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001939 if (*regparse == '-') /* if last, '-' is just a char */
1940 {
1941 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001942 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001944
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001945 /* skip the trailing ] */
1946 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001947 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001948
1949 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001951 EMIT(NFA_END_NEG_COLL);
1952 else
1953 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001954
1955 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001956 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001957 {
1958 EMIT(reg_string ? NL : NFA_NEWL);
1959 EMIT(NFA_OR);
1960 }
1961
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001963 } /* if exists closing ] */
1964
1965 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001967 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001968
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001969 default:
1970 {
1971#ifdef FEAT_MBYTE
1972 int plen;
1973
1974nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001975 /* plen is length of current char with composing chars */
1976 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001977 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001978 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001979 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001980 int i = 0;
1981
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001982 /* A base character plus composing characters, or just one
1983 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001984 * This requires creating a separate atom as if enclosing
1985 * the characters in (), where NFA_COMPOSING is the ( and
1986 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987 * building the postfix form, not the NFA itself;
1988 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001989 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001990 for (;;)
1991 {
1992 EMIT(c);
1993 if (i > 0)
1994 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001995 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001996 break;
1997 c = utf_ptr2char(old_regparse + i);
1998 }
1999 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002000 regparse = old_regparse + plen;
2001 }
2002 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002003#endif
2004 {
2005 c = no_Magic(c);
2006 EMIT(c);
2007 }
2008 return OK;
2009 }
2010 }
2011
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002012 return OK;
2013}
2014
2015/*
2016 * Parse something followed by possible [*+=].
2017 *
2018 * A piece is an atom, possibly followed by a multi, an indication of how many
2019 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2020 * characters: "", "a", "aa", etc.
2021 *
2022 * piece ::= atom
2023 * or atom multi
2024 */
2025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002026nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027{
2028 int i;
2029 int op;
2030 int ret;
2031 long minval, maxval;
2032 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002033 parse_state_T old_state;
2034 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002036 int old_post_pos;
2037 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 int quest;
2039
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002040 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2041 * next. */
2042 save_parse_state(&old_state);
2043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002045 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046
2047 ret = nfa_regatom();
2048 if (ret == FAIL)
2049 return FAIL; /* cascaded error */
2050
2051 op = peekchr();
2052 if (re_multi_type(op) == NOT_MULTI)
2053 return OK;
2054
2055 skipchr();
2056 switch (op)
2057 {
2058 case Magic('*'):
2059 EMIT(NFA_STAR);
2060 break;
2061
2062 case Magic('+'):
2063 /*
2064 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2065 * first and only submatch would be "aaa". But the backtracking
2066 * engine interprets the plus as "try matching one more time", and
2067 * a* matches a second time at the end of the input, the empty
2068 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002069 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002071 * In order to be consistent with the old engine, we replace
2072 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002074 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002075 curchr = -1;
2076 if (nfa_regatom() == FAIL)
2077 return FAIL;
2078 EMIT(NFA_STAR);
2079 EMIT(NFA_CONCAT);
2080 skipchr(); /* skip the \+ */
2081 break;
2082
2083 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002084 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002086 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 switch(op)
2088 {
2089 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002090 /* \@= */
2091 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002092 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002093 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002094 /* \@! */
2095 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002096 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002097 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002098 op = no_Magic(getchr());
2099 if (op == '=')
2100 /* \@<= */
2101 i = NFA_PREV_ATOM_JUST_BEFORE;
2102 else if (op == '!')
2103 /* \@<! */
2104 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2105 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002106 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002107 /* \@> */
2108 i = NFA_PREV_ATOM_LIKE_PATTERN;
2109 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002110 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002111 if (i == 0)
2112 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002113 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2114 return FAIL;
2115 }
2116 EMIT(i);
2117 if (i == NFA_PREV_ATOM_JUST_BEFORE
2118 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2119 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002120 break;
2121
2122 case Magic('?'):
2123 case Magic('='):
2124 EMIT(NFA_QUEST);
2125 break;
2126
2127 case Magic('{'):
2128 /* a{2,5} will expand to 'aaa?a?a?'
2129 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2130 * version of '?'
2131 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2132 * parenthesis have the same id
2133 */
2134
2135 greedy = TRUE;
2136 c2 = peekchr();
2137 if (c2 == '-' || c2 == Magic('-'))
2138 {
2139 skipchr();
2140 greedy = FALSE;
2141 }
2142 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002143 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002145 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2146 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002147 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002148 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002149 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002150 /* \{}, \{0,} */
2151 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002152 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002153 /* \{-}, \{-0,} */
2154 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002155 break;
2156 }
2157
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002158 /* Special case: x{0} or x{-0} */
2159 if (maxval == 0)
2160 {
2161 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002162 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002163 /* NFA_EMPTY is 0-length and works everywhere */
2164 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002165 return OK;
2166 }
2167
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002168 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002169 * maximum is much larger than the minimum and when the maximum is
2170 * large. Bail out if we can use the other engine. */
2171 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002172 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002173 return FAIL;
2174
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002175 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002176 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002177 /* Save parse state after the repeated atom and the \{} */
2178 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002179
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2181 for (i = 0; i < maxval; i++)
2182 {
2183 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002184 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002185 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002186 if (nfa_regatom() == FAIL)
2187 return FAIL;
2188 /* after "minval" times, atoms are optional */
2189 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002190 {
2191 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002192 {
2193 if (greedy)
2194 EMIT(NFA_STAR);
2195 else
2196 EMIT(NFA_STAR_NONGREEDY);
2197 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002198 else
2199 EMIT(quest);
2200 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002201 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002202 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002203 if (i + 1 > minval && maxval == MAX_LIMIT)
2204 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002205 }
2206
2207 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002208 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002209 curchr = -1;
2210
2211 break;
2212
2213
2214 default:
2215 break;
2216 } /* end switch */
2217
2218 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002221
2222 return OK;
2223}
2224
2225/*
2226 * Parse one or more pieces, concatenated. It matches a match for the
2227 * first piece, followed by a match for the second piece, etc. Example:
2228 * "f[0-9]b", first matches "f", then a digit and then "b".
2229 *
2230 * concat ::= piece
2231 * or piece piece
2232 * or piece piece piece
2233 * etc.
2234 */
2235 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002236nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002237{
2238 int cont = TRUE;
2239 int first = TRUE;
2240
2241 while (cont)
2242 {
2243 switch (peekchr())
2244 {
2245 case NUL:
2246 case Magic('|'):
2247 case Magic('&'):
2248 case Magic(')'):
2249 cont = FALSE;
2250 break;
2251
2252 case Magic('Z'):
2253#ifdef FEAT_MBYTE
2254 regflags |= RF_ICOMBINE;
2255#endif
2256 skipchr_keepstart();
2257 break;
2258 case Magic('c'):
2259 regflags |= RF_ICASE;
2260 skipchr_keepstart();
2261 break;
2262 case Magic('C'):
2263 regflags |= RF_NOICASE;
2264 skipchr_keepstart();
2265 break;
2266 case Magic('v'):
2267 reg_magic = MAGIC_ALL;
2268 skipchr_keepstart();
2269 curchr = -1;
2270 break;
2271 case Magic('m'):
2272 reg_magic = MAGIC_ON;
2273 skipchr_keepstart();
2274 curchr = -1;
2275 break;
2276 case Magic('M'):
2277 reg_magic = MAGIC_OFF;
2278 skipchr_keepstart();
2279 curchr = -1;
2280 break;
2281 case Magic('V'):
2282 reg_magic = MAGIC_NONE;
2283 skipchr_keepstart();
2284 curchr = -1;
2285 break;
2286
2287 default:
2288 if (nfa_regpiece() == FAIL)
2289 return FAIL;
2290 if (first == FALSE)
2291 EMIT(NFA_CONCAT);
2292 else
2293 first = FALSE;
2294 break;
2295 }
2296 }
2297
2298 return OK;
2299}
2300
2301/*
2302 * Parse a branch, one or more concats, separated by "\&". It matches the
2303 * last concat, but only if all the preceding concats also match at the same
2304 * position. Examples:
2305 * "foobeep\&..." matches "foo" in "foobeep".
2306 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2307 *
2308 * branch ::= concat
2309 * or concat \& concat
2310 * or concat \& concat \& concat
2311 * etc.
2312 */
2313 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002314nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002315{
2316 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002317 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002318
Bram Moolenaar16299b52013-05-30 18:45:23 +02002319 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002320
2321 /* First branch, possibly the only one */
2322 if (nfa_regconcat() == FAIL)
2323 return FAIL;
2324
2325 ch = peekchr();
2326 /* Try next concats */
2327 while (ch == Magic('&'))
2328 {
2329 skipchr();
2330 EMIT(NFA_NOPEN);
2331 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002332 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002333 if (nfa_regconcat() == FAIL)
2334 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002335 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002336 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002337 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002338 EMIT(NFA_CONCAT);
2339 ch = peekchr();
2340 }
2341
Bram Moolenaar699c1202013-09-25 16:41:54 +02002342 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002343 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002344 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345
2346 return OK;
2347}
2348
2349/*
2350 * Parse a pattern, one or more branches, separated by "\|". It matches
2351 * anything that matches one of the branches. Example: "foo\|beep" matches
2352 * "foo" and matches "beep". If more than one branch matches, the first one
2353 * is used.
2354 *
2355 * pattern ::= branch
2356 * or branch \| branch
2357 * or branch \| branch \| branch
2358 * etc.
2359 */
2360 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002361nfa_reg(
2362 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002363{
2364 int parno = 0;
2365
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366 if (paren == REG_PAREN)
2367 {
2368 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002370 parno = regnpar++;
2371 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002372#ifdef FEAT_SYN_HL
2373 else if (paren == REG_ZPAREN)
2374 {
2375 /* Make a ZOPEN node. */
2376 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002377 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002378 parno = regnzpar++;
2379 }
2380#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381
2382 if (nfa_regbranch() == FAIL)
2383 return FAIL; /* cascaded error */
2384
2385 while (peekchr() == Magic('|'))
2386 {
2387 skipchr();
2388 if (nfa_regbranch() == FAIL)
2389 return FAIL; /* cascaded error */
2390 EMIT(NFA_OR);
2391 }
2392
2393 /* Check for proper termination. */
2394 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2395 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396 if (paren == REG_NPAREN)
2397 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2398 else
2399 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2400 }
2401 else if (paren == REG_NOPAREN && peekchr() != NUL)
2402 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002403 if (peekchr() == Magic(')'))
2404 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2405 else
2406 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2407 }
2408 /*
2409 * Here we set the flag allowing back references to this set of
2410 * parentheses.
2411 */
2412 if (paren == REG_PAREN)
2413 {
2414 had_endbrace[parno] = TRUE; /* have seen the close paren */
2415 EMIT(NFA_MOPEN + parno);
2416 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002417#ifdef FEAT_SYN_HL
2418 else if (paren == REG_ZPAREN)
2419 EMIT(NFA_ZOPEN + parno);
2420#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421
2422 return OK;
2423}
2424
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425#ifdef DEBUG
2426static char_u code[50];
2427
2428 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002429nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002430{
2431 int addnl = FALSE;
2432
2433 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2434 {
2435 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002436 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002437 }
2438
2439 STRCPY(code, "");
2440 switch (c)
2441 {
2442 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2443 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2444 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2445 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2446 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2447 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2448
Bram Moolenaar5714b802013-05-28 22:03:20 +02002449 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2450 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2451 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2452 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2453 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2454 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2455 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2456 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2457 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002458#ifdef FEAT_SYN_HL
2459 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2460 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2461 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2462 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2463 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2464 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2465 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2466 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2467 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2468#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002469 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2470
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002471 case NFA_PREV_ATOM_NO_WIDTH:
2472 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002473 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2474 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002475 case NFA_PREV_ATOM_JUST_BEFORE:
2476 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2477 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2478 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002479 case NFA_PREV_ATOM_LIKE_PATTERN:
2480 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2481
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002482 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2483 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002485 case NFA_START_INVISIBLE_FIRST:
2486 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002487 case NFA_START_INVISIBLE_NEG:
2488 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002489 case NFA_START_INVISIBLE_NEG_FIRST:
2490 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002491 case NFA_START_INVISIBLE_BEFORE:
2492 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002493 case NFA_START_INVISIBLE_BEFORE_FIRST:
2494 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002495 case NFA_START_INVISIBLE_BEFORE_NEG:
2496 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002497 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2498 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002499 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002500 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002501 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002502 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002503
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002504 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2505 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002506 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002507
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002508 case NFA_MOPEN:
2509 case NFA_MOPEN1:
2510 case NFA_MOPEN2:
2511 case NFA_MOPEN3:
2512 case NFA_MOPEN4:
2513 case NFA_MOPEN5:
2514 case NFA_MOPEN6:
2515 case NFA_MOPEN7:
2516 case NFA_MOPEN8:
2517 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518 STRCPY(code, "NFA_MOPEN(x)");
2519 code[10] = c - NFA_MOPEN + '0';
2520 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002521 case NFA_MCLOSE:
2522 case NFA_MCLOSE1:
2523 case NFA_MCLOSE2:
2524 case NFA_MCLOSE3:
2525 case NFA_MCLOSE4:
2526 case NFA_MCLOSE5:
2527 case NFA_MCLOSE6:
2528 case NFA_MCLOSE7:
2529 case NFA_MCLOSE8:
2530 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 STRCPY(code, "NFA_MCLOSE(x)");
2532 code[11] = c - NFA_MCLOSE + '0';
2533 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002534#ifdef FEAT_SYN_HL
2535 case NFA_ZOPEN:
2536 case NFA_ZOPEN1:
2537 case NFA_ZOPEN2:
2538 case NFA_ZOPEN3:
2539 case NFA_ZOPEN4:
2540 case NFA_ZOPEN5:
2541 case NFA_ZOPEN6:
2542 case NFA_ZOPEN7:
2543 case NFA_ZOPEN8:
2544 case NFA_ZOPEN9:
2545 STRCPY(code, "NFA_ZOPEN(x)");
2546 code[10] = c - NFA_ZOPEN + '0';
2547 break;
2548 case NFA_ZCLOSE:
2549 case NFA_ZCLOSE1:
2550 case NFA_ZCLOSE2:
2551 case NFA_ZCLOSE3:
2552 case NFA_ZCLOSE4:
2553 case NFA_ZCLOSE5:
2554 case NFA_ZCLOSE6:
2555 case NFA_ZCLOSE7:
2556 case NFA_ZCLOSE8:
2557 case NFA_ZCLOSE9:
2558 STRCPY(code, "NFA_ZCLOSE(x)");
2559 code[11] = c - NFA_ZCLOSE + '0';
2560 break;
2561#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002562 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2563 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2564 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2565 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002566 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2567 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002568 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2569 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2570 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2571 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2572 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2573 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2574 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2575 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2576 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2577 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2578 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2579 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2580 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2581 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002582 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002584 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002585 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2586 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2587 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002588 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002589 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002590
2591 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2592 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2593 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2594 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2595 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2596 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2597 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2600 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2601 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2602 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2603 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2604 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2605 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2606 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2607 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2608 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2609 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2610 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2611 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2612 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2613 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2614 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2615
2616 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2617 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2618 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2619 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2620 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2621 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2622 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2623 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2624 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2625 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2626 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2627 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2628 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2629 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2630 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2631 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2632 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2633 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2634 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2635 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2636 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2637 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2638 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2639 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2640 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2641 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2642 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002643 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2644 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2645 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2646 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647
2648 default:
2649 STRCPY(code, "CHAR(x)");
2650 code[5] = c;
2651 }
2652
2653 if (addnl == TRUE)
2654 STRCAT(code, " + NEWLINE ");
2655
2656}
2657
2658#ifdef ENABLE_LOG
2659static FILE *log_fd;
2660
2661/*
2662 * Print the postfix notation of the current regexp.
2663 */
2664 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002665nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002666{
2667 int *p;
2668 FILE *f;
2669
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002670 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002671 if (f != NULL)
2672 {
2673 fprintf(f, "\n-------------------------\n");
2674 if (retval == FAIL)
2675 fprintf(f, ">>> NFA engine failed ... \n");
2676 else if (retval == OK)
2677 fprintf(f, ">>> NFA engine succeeded !\n");
2678 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002679 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002680 {
2681 nfa_set_code(*p);
2682 fprintf(f, "%s, ", code);
2683 }
2684 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002685 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002686 fprintf(f, "%d ", *p);
2687 fprintf(f, "\n\n");
2688 fclose(f);
2689 }
2690}
2691
2692/*
2693 * Print the NFA starting with a root node "state".
2694 */
2695 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002696nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002698 garray_T indent;
2699
2700 ga_init2(&indent, 1, 64);
2701 ga_append(&indent, '\0');
2702 nfa_print_state2(debugf, state, &indent);
2703 ga_clear(&indent);
2704}
2705
2706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002707nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002708{
2709 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002710
2711 if (state == NULL)
2712 return;
2713
2714 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002715
2716 /* Output indent */
2717 p = (char_u *)indent->ga_data;
2718 if (indent->ga_len >= 3)
2719 {
2720 int last = indent->ga_len - 3;
2721 char_u save[2];
2722
2723 STRNCPY(save, &p[last], 2);
2724 STRNCPY(&p[last], "+-", 2);
2725 fprintf(debugf, " %s", p);
2726 STRNCPY(&p[last], save, 2);
2727 }
2728 else
2729 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730
2731 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002732 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002733 code,
2734 state->c,
2735 abs(state->id),
2736 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002737 if (state->id < 0)
2738 return;
2739
2740 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002741
2742 /* grow indent for state->out */
2743 indent->ga_len -= 1;
2744 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002745 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002746 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002747 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002748 ga_append(indent, '\0');
2749
2750 nfa_print_state2(debugf, state->out, indent);
2751
2752 /* replace last part of indent for state->out1 */
2753 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002754 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002755 ga_append(indent, '\0');
2756
2757 nfa_print_state2(debugf, state->out1, indent);
2758
2759 /* shrink indent */
2760 indent->ga_len -= 3;
2761 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002762}
2763
2764/*
2765 * Print the NFA state machine.
2766 */
2767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002768nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002769{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002770 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002771
2772 if (debugf != NULL)
2773 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002774 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002775
Bram Moolenaar473de612013-06-08 18:19:48 +02002776 if (prog->reganch)
2777 fprintf(debugf, "reganch: %d\n", prog->reganch);
2778 if (prog->regstart != NUL)
2779 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2780 prog->regstart, prog->regstart);
2781 if (prog->match_text != NULL)
2782 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002783
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784 fclose(debugf);
2785 }
2786}
2787#endif /* ENABLE_LOG */
2788#endif /* DEBUG */
2789
2790/*
2791 * Parse r.e. @expr and convert it into postfix form.
2792 * Return the postfix string on success, NULL otherwise.
2793 */
2794 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002795re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002796{
2797 if (nfa_reg(REG_NOPAREN) == FAIL)
2798 return NULL;
2799 EMIT(NFA_MOPEN);
2800 return post_start;
2801}
2802
2803/* NB. Some of the code below is inspired by Russ's. */
2804
2805/*
2806 * Represents an NFA state plus zero or one or two arrows exiting.
2807 * if c == MATCH, no arrows out; matching state.
2808 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2809 * If c < 256, labeled arrow with character c to out.
2810 */
2811
2812static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2813
2814/*
2815 * Allocate and initialize nfa_state_T.
2816 */
2817 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002818alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002819{
2820 nfa_state_T *s;
2821
2822 if (istate >= nstate)
2823 return NULL;
2824
2825 s = &state_ptr[istate++];
2826
2827 s->c = c;
2828 s->out = out;
2829 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002830 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831
2832 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002833 s->lastlist[0] = 0;
2834 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002835
2836 return s;
2837}
2838
2839/*
2840 * A partially built NFA without the matching state filled in.
2841 * Frag_T.start points at the start state.
2842 * Frag_T.out is a list of places that need to be set to the
2843 * next state for this fragment.
2844 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002845
2846/* Since the out pointers in the list are always
2847 * uninitialized, we use the pointers themselves
2848 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002849typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002850union Ptrlist
2851{
2852 Ptrlist *next;
2853 nfa_state_T *s;
2854};
2855
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002856struct Frag
2857{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002858 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002859 Ptrlist *out;
2860};
2861typedef struct Frag Frag_T;
2862
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002863static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2864static Ptrlist *list1(nfa_state_T **outp);
2865static void patch(Ptrlist *l, nfa_state_T *s);
2866static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2867static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2868static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002869
2870/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002871 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002872 */
2873 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002874frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002876 Frag_T n;
2877
2878 n.start = start;
2879 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002880 return n;
2881}
2882
2883/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 * Create singleton list containing just outp.
2885 */
2886 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002887list1(
2888 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002889{
2890 Ptrlist *l;
2891
2892 l = (Ptrlist *)outp;
2893 l->next = NULL;
2894 return l;
2895}
2896
2897/*
2898 * Patch the list of states at out to point to start.
2899 */
2900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002901patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002902{
2903 Ptrlist *next;
2904
2905 for (; l; l = next)
2906 {
2907 next = l->next;
2908 l->s = s;
2909 }
2910}
2911
2912
2913/*
2914 * Join the two lists l1 and l2, returning the combination.
2915 */
2916 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002917append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918{
2919 Ptrlist *oldl1;
2920
2921 oldl1 = l1;
2922 while (l1->next)
2923 l1 = l1->next;
2924 l1->next = l2;
2925 return oldl1;
2926}
2927
2928/*
2929 * Stack used for transforming postfix form into NFA.
2930 */
2931static Frag_T empty;
2932
2933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002934st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002935{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002936#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002937 FILE *df;
2938 int *p2;
2939
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002940 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002941 if (df)
2942 {
2943 fprintf(df, "Error popping the stack!\n");
2944#ifdef DEBUG
2945 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2946#endif
2947 fprintf(df, "Postfix form is: ");
2948#ifdef DEBUG
2949 for (p2 = postfix; p2 < end; p2++)
2950 {
2951 nfa_set_code(*p2);
2952 fprintf(df, "%s, ", code);
2953 }
2954 nfa_set_code(*p);
2955 fprintf(df, "\nCurrent position is: ");
2956 for (p2 = postfix; p2 <= p; p2 ++)
2957 {
2958 nfa_set_code(*p2);
2959 fprintf(df, "%s, ", code);
2960 }
2961#else
2962 for (p2 = postfix; p2 < end; p2++)
2963 {
2964 fprintf(df, "%d, ", *p2);
2965 }
2966 fprintf(df, "\nCurrent position is: ");
2967 for (p2 = postfix; p2 <= p; p2 ++)
2968 {
2969 fprintf(df, "%d, ", *p2);
2970 }
2971#endif
2972 fprintf(df, "\n--------------------------\n");
2973 fclose(df);
2974 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002975#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 EMSG(_("E874: (NFA) Could not pop the stack !"));
2977}
2978
2979/*
2980 * Push an item onto the stack.
2981 */
2982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002983st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002984{
2985 Frag_T *stackp = *p;
2986
2987 if (stackp >= stack_end)
2988 return;
2989 *stackp = s;
2990 *p = *p + 1;
2991}
2992
2993/*
2994 * Pop an item from the stack.
2995 */
2996 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002997st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002998{
2999 Frag_T *stackp;
3000
3001 *p = *p - 1;
3002 stackp = *p;
3003 if (stackp < stack)
3004 return empty;
3005 return **p;
3006}
3007
3008/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003009 * Estimate the maximum byte length of anything matching "state".
3010 * When unknown or unlimited return -1.
3011 */
3012 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003013nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003014{
3015 int l, r;
3016 nfa_state_T *state = startstate;
3017 int len = 0;
3018
3019 /* detect looping in a NFA_SPLIT */
3020 if (depth > 4)
3021 return -1;
3022
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003023 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003024 {
3025 switch (state->c)
3026 {
3027 case NFA_END_INVISIBLE:
3028 case NFA_END_INVISIBLE_NEG:
3029 /* the end, return what we have */
3030 return len;
3031
3032 case NFA_SPLIT:
3033 /* two alternatives, use the maximum */
3034 l = nfa_max_width(state->out, depth + 1);
3035 r = nfa_max_width(state->out1, depth + 1);
3036 if (l < 0 || r < 0)
3037 return -1;
3038 return len + (l > r ? l : r);
3039
3040 case NFA_ANY:
3041 case NFA_START_COLL:
3042 case NFA_START_NEG_COLL:
3043 /* matches some character, including composing chars */
3044#ifdef FEAT_MBYTE
3045 if (enc_utf8)
3046 len += MB_MAXBYTES;
3047 else if (has_mbyte)
3048 len += 2;
3049 else
3050#endif
3051 ++len;
3052 if (state->c != NFA_ANY)
3053 {
3054 /* skip over the characters */
3055 state = state->out1->out;
3056 continue;
3057 }
3058 break;
3059
3060 case NFA_DIGIT:
3061 case NFA_WHITE:
3062 case NFA_HEX:
3063 case NFA_OCTAL:
3064 /* ascii */
3065 ++len;
3066 break;
3067
3068 case NFA_IDENT:
3069 case NFA_SIDENT:
3070 case NFA_KWORD:
3071 case NFA_SKWORD:
3072 case NFA_FNAME:
3073 case NFA_SFNAME:
3074 case NFA_PRINT:
3075 case NFA_SPRINT:
3076 case NFA_NWHITE:
3077 case NFA_NDIGIT:
3078 case NFA_NHEX:
3079 case NFA_NOCTAL:
3080 case NFA_WORD:
3081 case NFA_NWORD:
3082 case NFA_HEAD:
3083 case NFA_NHEAD:
3084 case NFA_ALPHA:
3085 case NFA_NALPHA:
3086 case NFA_LOWER:
3087 case NFA_NLOWER:
3088 case NFA_UPPER:
3089 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003090 case NFA_LOWER_IC:
3091 case NFA_NLOWER_IC:
3092 case NFA_UPPER_IC:
3093 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003094 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003095 /* possibly non-ascii */
3096#ifdef FEAT_MBYTE
3097 if (has_mbyte)
3098 len += 3;
3099 else
3100#endif
3101 ++len;
3102 break;
3103
3104 case NFA_START_INVISIBLE:
3105 case NFA_START_INVISIBLE_NEG:
3106 case NFA_START_INVISIBLE_BEFORE:
3107 case NFA_START_INVISIBLE_BEFORE_NEG:
3108 /* zero-width, out1 points to the END state */
3109 state = state->out1->out;
3110 continue;
3111
3112 case NFA_BACKREF1:
3113 case NFA_BACKREF2:
3114 case NFA_BACKREF3:
3115 case NFA_BACKREF4:
3116 case NFA_BACKREF5:
3117 case NFA_BACKREF6:
3118 case NFA_BACKREF7:
3119 case NFA_BACKREF8:
3120 case NFA_BACKREF9:
3121#ifdef FEAT_SYN_HL
3122 case NFA_ZREF1:
3123 case NFA_ZREF2:
3124 case NFA_ZREF3:
3125 case NFA_ZREF4:
3126 case NFA_ZREF5:
3127 case NFA_ZREF6:
3128 case NFA_ZREF7:
3129 case NFA_ZREF8:
3130 case NFA_ZREF9:
3131#endif
3132 case NFA_NEWL:
3133 case NFA_SKIP:
3134 /* unknown width */
3135 return -1;
3136
3137 case NFA_BOL:
3138 case NFA_EOL:
3139 case NFA_BOF:
3140 case NFA_EOF:
3141 case NFA_BOW:
3142 case NFA_EOW:
3143 case NFA_MOPEN:
3144 case NFA_MOPEN1:
3145 case NFA_MOPEN2:
3146 case NFA_MOPEN3:
3147 case NFA_MOPEN4:
3148 case NFA_MOPEN5:
3149 case NFA_MOPEN6:
3150 case NFA_MOPEN7:
3151 case NFA_MOPEN8:
3152 case NFA_MOPEN9:
3153#ifdef FEAT_SYN_HL
3154 case NFA_ZOPEN:
3155 case NFA_ZOPEN1:
3156 case NFA_ZOPEN2:
3157 case NFA_ZOPEN3:
3158 case NFA_ZOPEN4:
3159 case NFA_ZOPEN5:
3160 case NFA_ZOPEN6:
3161 case NFA_ZOPEN7:
3162 case NFA_ZOPEN8:
3163 case NFA_ZOPEN9:
3164 case NFA_ZCLOSE:
3165 case NFA_ZCLOSE1:
3166 case NFA_ZCLOSE2:
3167 case NFA_ZCLOSE3:
3168 case NFA_ZCLOSE4:
3169 case NFA_ZCLOSE5:
3170 case NFA_ZCLOSE6:
3171 case NFA_ZCLOSE7:
3172 case NFA_ZCLOSE8:
3173 case NFA_ZCLOSE9:
3174#endif
3175 case NFA_MCLOSE:
3176 case NFA_MCLOSE1:
3177 case NFA_MCLOSE2:
3178 case NFA_MCLOSE3:
3179 case NFA_MCLOSE4:
3180 case NFA_MCLOSE5:
3181 case NFA_MCLOSE6:
3182 case NFA_MCLOSE7:
3183 case NFA_MCLOSE8:
3184 case NFA_MCLOSE9:
3185 case NFA_NOPEN:
3186 case NFA_NCLOSE:
3187
3188 case NFA_LNUM_GT:
3189 case NFA_LNUM_LT:
3190 case NFA_COL_GT:
3191 case NFA_COL_LT:
3192 case NFA_VCOL_GT:
3193 case NFA_VCOL_LT:
3194 case NFA_MARK_GT:
3195 case NFA_MARK_LT:
3196 case NFA_VISUAL:
3197 case NFA_LNUM:
3198 case NFA_CURSOR:
3199 case NFA_COL:
3200 case NFA_VCOL:
3201 case NFA_MARK:
3202
3203 case NFA_ZSTART:
3204 case NFA_ZEND:
3205 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003206 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003207 case NFA_START_PATTERN:
3208 case NFA_END_PATTERN:
3209 case NFA_COMPOSING:
3210 case NFA_END_COMPOSING:
3211 /* zero-width */
3212 break;
3213
3214 default:
3215 if (state->c < 0)
3216 /* don't know what this is */
3217 return -1;
3218 /* normal character */
3219 len += MB_CHAR2LEN(state->c);
3220 break;
3221 }
3222
3223 /* normal way to continue */
3224 state = state->out;
3225 }
3226
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003227 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003228 return -1;
3229}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003230
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003231/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 * Convert a postfix form into its equivalent NFA.
3233 * Return the NFA start state on success, NULL otherwise.
3234 */
3235 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003236post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237{
3238 int *p;
3239 int mopen;
3240 int mclose;
3241 Frag_T *stack = NULL;
3242 Frag_T *stackp = NULL;
3243 Frag_T *stack_end = NULL;
3244 Frag_T e1;
3245 Frag_T e2;
3246 Frag_T e;
3247 nfa_state_T *s;
3248 nfa_state_T *s1;
3249 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003250 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251
3252 if (postfix == NULL)
3253 return NULL;
3254
Bram Moolenaar053bb602013-05-20 13:55:21 +02003255#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256#define POP() st_pop(&stackp, stack); \
3257 if (stackp < stack) \
3258 { \
3259 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003260 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003261 return NULL; \
3262 }
3263
3264 if (nfa_calc_size == FALSE)
3265 {
3266 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003267 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003268 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003269 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003270 }
3271
3272 for (p = postfix; p < end; ++p)
3273 {
3274 switch (*p)
3275 {
3276 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003277 /* Concatenation.
3278 * Pay attention: this operator does not exist in the r.e. itself
3279 * (it is implicit, really). It is added when r.e. is translated
3280 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 if (nfa_calc_size == TRUE)
3282 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003283 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 break;
3285 }
3286 e2 = POP();
3287 e1 = POP();
3288 patch(e1.out, e2.start);
3289 PUSH(frag(e1.start, e2.out));
3290 break;
3291
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 case NFA_OR:
3293 /* Alternation */
3294 if (nfa_calc_size == TRUE)
3295 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003296 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003297 break;
3298 }
3299 e2 = POP();
3300 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003301 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003302 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003303 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003304 PUSH(frag(s, append(e1.out, e2.out)));
3305 break;
3306
3307 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003308 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003309 if (nfa_calc_size == TRUE)
3310 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003311 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003312 break;
3313 }
3314 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003315 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003316 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003317 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003318 patch(e.out, s);
3319 PUSH(frag(s, list1(&s->out1)));
3320 break;
3321
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003322 case NFA_STAR_NONGREEDY:
3323 /* Zero or more, prefer zero */
3324 if (nfa_calc_size == TRUE)
3325 {
3326 nstate++;
3327 break;
3328 }
3329 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003330 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003331 if (s == NULL)
3332 goto theend;
3333 patch(e.out, s);
3334 PUSH(frag(s, list1(&s->out)));
3335 break;
3336
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003337 case NFA_QUEST:
3338 /* one or zero atoms=> greedy match */
3339 if (nfa_calc_size == TRUE)
3340 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003341 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003342 break;
3343 }
3344 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003345 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003346 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003347 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 PUSH(frag(s, append(e.out, list1(&s->out1))));
3349 break;
3350
3351 case NFA_QUEST_NONGREEDY:
3352 /* zero or one atoms => non-greedy match */
3353 if (nfa_calc_size == TRUE)
3354 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003355 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003356 break;
3357 }
3358 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003359 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003360 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003361 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003362 PUSH(frag(s, append(e.out, list1(&s->out))));
3363 break;
3364
Bram Moolenaar417bad22013-06-07 14:08:30 +02003365 case NFA_END_COLL:
3366 case NFA_END_NEG_COLL:
3367 /* On the stack is the sequence starting with NFA_START_COLL or
3368 * NFA_START_NEG_COLL and all possible characters. Patch it to
3369 * add the output to the start. */
3370 if (nfa_calc_size == TRUE)
3371 {
3372 nstate++;
3373 break;
3374 }
3375 e = POP();
3376 s = alloc_state(NFA_END_COLL, NULL, NULL);
3377 if (s == NULL)
3378 goto theend;
3379 patch(e.out, s);
3380 e.start->out1 = s;
3381 PUSH(frag(e.start, list1(&s->out)));
3382 break;
3383
3384 case NFA_RANGE:
3385 /* Before this are two characters, the low and high end of a
3386 * range. Turn them into two states with MIN and MAX. */
3387 if (nfa_calc_size == TRUE)
3388 {
3389 /* nstate += 0; */
3390 break;
3391 }
3392 e2 = POP();
3393 e1 = POP();
3394 e2.start->val = e2.start->c;
3395 e2.start->c = NFA_RANGE_MAX;
3396 e1.start->val = e1.start->c;
3397 e1.start->c = NFA_RANGE_MIN;
3398 patch(e1.out, e2.start);
3399 PUSH(frag(e1.start, e2.out));
3400 break;
3401
Bram Moolenaar699c1202013-09-25 16:41:54 +02003402 case NFA_EMPTY:
3403 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 if (nfa_calc_size == TRUE)
3405 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003406 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003407 break;
3408 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003409 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003410 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003411 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003412 PUSH(frag(s, list1(&s->out)));
3413 break;
3414
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003415 case NFA_OPT_CHARS:
3416 {
3417 int n;
3418
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003419 /* \%[abc] implemented as:
3420 * NFA_SPLIT
3421 * +-CHAR(a)
3422 * | +-NFA_SPLIT
3423 * | +-CHAR(b)
3424 * | | +-NFA_SPLIT
3425 * | | +-CHAR(c)
3426 * | | | +-next
3427 * | | +- next
3428 * | +- next
3429 * +- next
3430 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003431 n = *++p; /* get number of characters */
3432 if (nfa_calc_size == TRUE)
3433 {
3434 nstate += n;
3435 break;
3436 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003437 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003438 e1.out = NULL; /* stores list with out1's */
3439 s1 = NULL; /* previous NFA_SPLIT to connect to */
3440 while (n-- > 0)
3441 {
3442 e = POP(); /* get character */
3443 s = alloc_state(NFA_SPLIT, e.start, NULL);
3444 if (s == NULL)
3445 goto theend;
3446 if (e1.out == NULL)
3447 e1 = e;
3448 patch(e.out, s1);
3449 append(e1.out, list1(&s->out1));
3450 s1 = s;
3451 }
3452 PUSH(frag(s, e1.out));
3453 break;
3454 }
3455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003456 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003457 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003458 case NFA_PREV_ATOM_JUST_BEFORE:
3459 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003460 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003461 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003462 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3463 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003464 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003465 int start_state;
3466 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003467 int n = 0;
3468 nfa_state_T *zend;
3469 nfa_state_T *skip;
3470
Bram Moolenaardecd9542013-06-07 16:31:50 +02003471 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003472 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003473 case NFA_PREV_ATOM_NO_WIDTH:
3474 start_state = NFA_START_INVISIBLE;
3475 end_state = NFA_END_INVISIBLE;
3476 break;
3477 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3478 start_state = NFA_START_INVISIBLE_NEG;
3479 end_state = NFA_END_INVISIBLE_NEG;
3480 break;
3481 case NFA_PREV_ATOM_JUST_BEFORE:
3482 start_state = NFA_START_INVISIBLE_BEFORE;
3483 end_state = NFA_END_INVISIBLE;
3484 break;
3485 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3486 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3487 end_state = NFA_END_INVISIBLE_NEG;
3488 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003489 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003490 start_state = NFA_START_PATTERN;
3491 end_state = NFA_END_PATTERN;
3492 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003493 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003494
3495 if (before)
3496 n = *++p; /* get the count */
3497
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003498 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003499 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003500 * The \@<= operator: match for the preceding atom.
3501 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003503 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504
3505 if (nfa_calc_size == TRUE)
3506 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003507 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003508 break;
3509 }
3510 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003511 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003512 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003513 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514
Bram Moolenaar87953742013-06-05 18:52:40 +02003515 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003517 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003518 if (pattern)
3519 {
3520 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3521 skip = alloc_state(NFA_SKIP, NULL, NULL);
3522 zend = alloc_state(NFA_ZEND, s1, NULL);
3523 s1->out= skip;
3524 patch(e.out, zend);
3525 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003526 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003527 else
3528 {
3529 patch(e.out, s1);
3530 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003531 if (before)
3532 {
3533 if (n <= 0)
3534 /* See if we can guess the maximum width, it avoids a
3535 * lot of pointless tries. */
3536 n = nfa_max_width(e.start, 0);
3537 s->val = n; /* store the count */
3538 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003539 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003540 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003541 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003542
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003543#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003544 case NFA_COMPOSING: /* char with composing char */
3545#if 0
3546 /* TODO */
3547 if (regflags & RF_ICOMBINE)
3548 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003549 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003550 }
3551#endif
3552 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003553#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003554
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003555 case NFA_MOPEN: /* \( \) Submatch */
3556 case NFA_MOPEN1:
3557 case NFA_MOPEN2:
3558 case NFA_MOPEN3:
3559 case NFA_MOPEN4:
3560 case NFA_MOPEN5:
3561 case NFA_MOPEN6:
3562 case NFA_MOPEN7:
3563 case NFA_MOPEN8:
3564 case NFA_MOPEN9:
3565#ifdef FEAT_SYN_HL
3566 case NFA_ZOPEN: /* \z( \) Submatch */
3567 case NFA_ZOPEN1:
3568 case NFA_ZOPEN2:
3569 case NFA_ZOPEN3:
3570 case NFA_ZOPEN4:
3571 case NFA_ZOPEN5:
3572 case NFA_ZOPEN6:
3573 case NFA_ZOPEN7:
3574 case NFA_ZOPEN8:
3575 case NFA_ZOPEN9:
3576#endif
3577 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003578 if (nfa_calc_size == TRUE)
3579 {
3580 nstate += 2;
3581 break;
3582 }
3583
3584 mopen = *p;
3585 switch (*p)
3586 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003587 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3588#ifdef FEAT_SYN_HL
3589 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3590 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3591 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3592 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3593 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3594 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3595 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3596 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3597 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3598 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3599#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003600#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003601 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003602#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003603 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003604 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 mclose = *p + NSUBEXP;
3606 break;
3607 }
3608
3609 /* Allow "NFA_MOPEN" as a valid postfix representation for
3610 * the empty regexp "". In this case, the NFA will be
3611 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3612 * empty groups of parenthesis, and empty mbyte chars */
3613 if (stackp == stack)
3614 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003615 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003616 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003617 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003618 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003620 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 patch(list1(&s->out), s1);
3622 PUSH(frag(s, list1(&s1->out)));
3623 break;
3624 }
3625
3626 /* At least one node was emitted before NFA_MOPEN, so
3627 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3628 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003629 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003631 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003632
Bram Moolenaar525666f2013-06-02 16:40:55 +02003633 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003635 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003636 patch(e.out, s1);
3637
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003638#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003639 if (mopen == NFA_COMPOSING)
3640 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003642#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003643
3644 PUSH(frag(s, list1(&s1->out)));
3645 break;
3646
Bram Moolenaar5714b802013-05-28 22:03:20 +02003647 case NFA_BACKREF1:
3648 case NFA_BACKREF2:
3649 case NFA_BACKREF3:
3650 case NFA_BACKREF4:
3651 case NFA_BACKREF5:
3652 case NFA_BACKREF6:
3653 case NFA_BACKREF7:
3654 case NFA_BACKREF8:
3655 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003656#ifdef FEAT_SYN_HL
3657 case NFA_ZREF1:
3658 case NFA_ZREF2:
3659 case NFA_ZREF3:
3660 case NFA_ZREF4:
3661 case NFA_ZREF5:
3662 case NFA_ZREF6:
3663 case NFA_ZREF7:
3664 case NFA_ZREF8:
3665 case NFA_ZREF9:
3666#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003667 if (nfa_calc_size == TRUE)
3668 {
3669 nstate += 2;
3670 break;
3671 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003672 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003673 if (s == NULL)
3674 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003675 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003676 if (s1 == NULL)
3677 goto theend;
3678 patch(list1(&s->out), s1);
3679 PUSH(frag(s, list1(&s1->out)));
3680 break;
3681
Bram Moolenaar423532e2013-05-29 21:14:42 +02003682 case NFA_LNUM:
3683 case NFA_LNUM_GT:
3684 case NFA_LNUM_LT:
3685 case NFA_VCOL:
3686 case NFA_VCOL_GT:
3687 case NFA_VCOL_LT:
3688 case NFA_COL:
3689 case NFA_COL_GT:
3690 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003691 case NFA_MARK:
3692 case NFA_MARK_GT:
3693 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003694 {
3695 int n = *++p; /* lnum, col or mark name */
3696
Bram Moolenaar423532e2013-05-29 21:14:42 +02003697 if (nfa_calc_size == TRUE)
3698 {
3699 nstate += 1;
3700 break;
3701 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003702 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003703 if (s == NULL)
3704 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003705 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003706 PUSH(frag(s, list1(&s->out)));
3707 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003708 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003709
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003710 case NFA_ZSTART:
3711 case NFA_ZEND:
3712 default:
3713 /* Operands */
3714 if (nfa_calc_size == TRUE)
3715 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003716 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003717 break;
3718 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003719 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003721 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 PUSH(frag(s, list1(&s->out)));
3723 break;
3724
3725 } /* switch(*p) */
3726
3727 } /* for(p = postfix; *p; ++p) */
3728
3729 if (nfa_calc_size == TRUE)
3730 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003731 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003732 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003733 }
3734
3735 e = POP();
3736 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003737 {
3738 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 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 +02003740 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741
3742 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003743 {
3744 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003745 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003746 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003747
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003748 matchstate = &state_ptr[istate++]; /* the match state */
3749 matchstate->c = NFA_MATCH;
3750 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003751 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752
3753 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003754 ret = e.start;
3755
3756theend:
3757 vim_free(stack);
3758 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759
3760#undef POP1
3761#undef PUSH1
3762#undef POP2
3763#undef PUSH2
3764#undef POP
3765#undef PUSH
3766}
3767
Bram Moolenaara2947e22013-06-11 22:44:09 +02003768/*
3769 * After building the NFA program, inspect it to add optimization hints.
3770 */
3771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003772nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003773{
3774 int i;
3775 int c;
3776
3777 for (i = 0; i < prog->nstate; ++i)
3778 {
3779 c = prog->state[i].c;
3780 if (c == NFA_START_INVISIBLE
3781 || c == NFA_START_INVISIBLE_NEG
3782 || c == NFA_START_INVISIBLE_BEFORE
3783 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3784 {
3785 int directly;
3786
3787 /* Do it directly when what follows is possibly the end of the
3788 * match. */
3789 if (match_follows(prog->state[i].out1->out, 0))
3790 directly = TRUE;
3791 else
3792 {
3793 int ch_invisible = failure_chance(prog->state[i].out, 0);
3794 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3795
3796 /* Postpone when the invisible match is expensive or has a
3797 * lower chance of failing. */
3798 if (c == NFA_START_INVISIBLE_BEFORE
3799 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3800 {
3801 /* "before" matches are very expensive when
3802 * unbounded, always prefer what follows then,
3803 * unless what follows will always match.
3804 * Otherwise strongly prefer what follows. */
3805 if (prog->state[i].val <= 0 && ch_follows > 0)
3806 directly = FALSE;
3807 else
3808 directly = ch_follows * 10 < ch_invisible;
3809 }
3810 else
3811 {
3812 /* normal invisible, first do the one with the
3813 * highest failure chance */
3814 directly = ch_follows < ch_invisible;
3815 }
3816 }
3817 if (directly)
3818 /* switch to the _FIRST state */
3819 ++prog->state[i].c;
3820 }
3821 }
3822}
3823
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003824/****************************************************************
3825 * NFA execution code.
3826 ****************************************************************/
3827
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003828typedef struct
3829{
3830 int in_use; /* number of subexpr with useful info */
3831
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003832 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003833 union
3834 {
3835 struct multipos
3836 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003837 linenr_T start_lnum;
3838 linenr_T end_lnum;
3839 colnr_T start_col;
3840 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003841 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003842 struct linepos
3843 {
3844 char_u *start;
3845 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003846 } line[NSUBEXP];
3847 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003848} regsub_T;
3849
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003850typedef struct
3851{
3852 regsub_T norm; /* \( .. \) matches */
3853#ifdef FEAT_SYN_HL
3854 regsub_T synt; /* \z( .. \) matches */
3855#endif
3856} regsubs_T;
3857
Bram Moolenaara2d95102013-06-04 14:23:05 +02003858/* nfa_pim_T stores a Postponed Invisible Match. */
3859typedef struct nfa_pim_S nfa_pim_T;
3860struct nfa_pim_S
3861{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003862 int result; /* NFA_PIM_*, see below */
3863 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003864 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003865 union
3866 {
3867 lpos_T pos;
3868 char_u *ptr;
3869 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003870};
3871
3872/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003873#define NFA_PIM_UNUSED 0 /* pim not used */
3874#define NFA_PIM_TODO 1 /* pim not done yet */
3875#define NFA_PIM_MATCH 2 /* pim executed, matches */
3876#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003877
3878
Bram Moolenaar963fee22013-05-26 21:47:28 +02003879/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003880typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003881{
3882 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003883 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003884 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3885 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003886 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003887} nfa_thread_T;
3888
Bram Moolenaar963fee22013-05-26 21:47:28 +02003889/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003890typedef struct
3891{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003892 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003893 int n; /* nr of states currently in "t" */
3894 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003895 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003896 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003897} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003898
Bram Moolenaar5714b802013-05-28 22:03:20 +02003899#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003900static void log_subsexpr(regsubs_T *subs);
3901static void log_subexpr(regsub_T *sub);
3902static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003903
3904 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003905log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003906{
3907 log_subexpr(&subs->norm);
3908# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003909 if (nfa_has_zsubexpr)
3910 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003911# endif
3912}
3913
Bram Moolenaar5714b802013-05-28 22:03:20 +02003914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003915log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003916{
3917 int j;
3918
3919 for (j = 0; j < sub->in_use; j++)
3920 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003921 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003922 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003923 sub->list.multi[j].start_col,
3924 (int)sub->list.multi[j].start_lnum,
3925 sub->list.multi[j].end_col,
3926 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003927 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003928 {
3929 char *s = (char *)sub->list.line[j].start;
3930 char *e = (char *)sub->list.line[j].end;
3931
Bram Moolenaar87953742013-06-05 18:52:40 +02003932 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003933 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003934 s == NULL ? "NULL" : s,
3935 e == NULL ? "NULL" : e);
3936 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003937}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003938
3939 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003940pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003941{
3942 static char buf[30];
3943
3944 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3945 buf[0] = NUL;
3946 else
3947 {
3948 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3949 : (int)(pim->end.ptr - reginput));
3950 }
3951 return buf;
3952}
3953
Bram Moolenaar5714b802013-05-28 22:03:20 +02003954#endif
3955
Bram Moolenaar963fee22013-05-26 21:47:28 +02003956/* Used during execution: whether a match has been found. */
3957static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003958#ifdef FEAT_RELTIME
3959static proftime_T *nfa_time_limit;
3960static int nfa_time_count;
3961#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003962
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003963static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3964static void clear_sub(regsub_T *sub);
3965static void copy_sub(regsub_T *to, regsub_T *from);
3966static void copy_sub_off(regsub_T *to, regsub_T *from);
3967static void copy_ze_off(regsub_T *to, regsub_T *from);
3968static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3969static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3970static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3971static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3972static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3973static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3974static 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 +02003975
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003976/*
3977 * Copy postponed invisible match info from "from" to "to".
3978 */
3979 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003980copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003981{
3982 to->result = from->result;
3983 to->state = from->state;
3984 copy_sub(&to->subs.norm, &from->subs.norm);
3985#ifdef FEAT_SYN_HL
3986 if (nfa_has_zsubexpr)
3987 copy_sub(&to->subs.synt, &from->subs.synt);
3988#endif
3989 to->end = from->end;
3990}
3991
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003992 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003993clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003994{
3995 if (REG_MULTI)
3996 /* Use 0xff to set lnum to -1 */
3997 vim_memset(sub->list.multi, 0xff,
3998 sizeof(struct multipos) * nfa_nsubexpr);
3999 else
4000 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4001 sub->in_use = 0;
4002}
4003
4004/*
4005 * Copy the submatches from "from" to "to".
4006 */
4007 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004008copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004009{
4010 to->in_use = from->in_use;
4011 if (from->in_use > 0)
4012 {
4013 /* Copy the match start and end positions. */
4014 if (REG_MULTI)
4015 mch_memmove(&to->list.multi[0],
4016 &from->list.multi[0],
4017 sizeof(struct multipos) * from->in_use);
4018 else
4019 mch_memmove(&to->list.line[0],
4020 &from->list.line[0],
4021 sizeof(struct linepos) * from->in_use);
4022 }
4023}
4024
4025/*
4026 * Like copy_sub() but exclude the main match.
4027 */
4028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004029copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004030{
4031 if (to->in_use < from->in_use)
4032 to->in_use = from->in_use;
4033 if (from->in_use > 1)
4034 {
4035 /* Copy the match start and end positions. */
4036 if (REG_MULTI)
4037 mch_memmove(&to->list.multi[1],
4038 &from->list.multi[1],
4039 sizeof(struct multipos) * (from->in_use - 1));
4040 else
4041 mch_memmove(&to->list.line[1],
4042 &from->list.line[1],
4043 sizeof(struct linepos) * (from->in_use - 1));
4044 }
4045}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004046
Bram Moolenaar428e9872013-05-30 17:05:39 +02004047/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004048 * Like copy_sub() but only do the end of the main match if \ze is present.
4049 */
4050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004051copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004052{
4053 if (nfa_has_zend)
4054 {
4055 if (REG_MULTI)
4056 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004057 if (from->list.multi[0].end_lnum >= 0)
4058 {
4059 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4060 to->list.multi[0].end_col = from->list.multi[0].end_col;
4061 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004062 }
4063 else
4064 {
4065 if (from->list.line[0].end != NULL)
4066 to->list.line[0].end = from->list.line[0].end;
4067 }
4068 }
4069}
4070
4071/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004072 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004073 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004074 */
4075 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004076sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004077{
4078 int i;
4079 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004080 linenr_T s1;
4081 linenr_T s2;
4082 char_u *sp1;
4083 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004084
4085 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4086 if (REG_MULTI)
4087 {
4088 for (i = 0; i < todo; ++i)
4089 {
4090 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004091 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004092 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004093 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004094 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004095 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004096 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004097 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004098 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004099 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004100 if (s1 != -1 && sub1->list.multi[i].start_col
4101 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004102 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004103
4104 if (nfa_has_backref)
4105 {
4106 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004107 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004108 else
4109 s1 = -1;
4110 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004111 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004112 else
4113 s2 = -1;
4114 if (s1 != s2)
4115 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004116 if (s1 != -1 && sub1->list.multi[i].end_col
4117 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004118 return FALSE;
4119 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004120 }
4121 }
4122 else
4123 {
4124 for (i = 0; i < todo; ++i)
4125 {
4126 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004127 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004128 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004129 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004130 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004131 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004132 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004133 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004134 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004135 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004136 if (nfa_has_backref)
4137 {
4138 if (i < sub1->in_use)
4139 sp1 = sub1->list.line[i].end;
4140 else
4141 sp1 = NULL;
4142 if (i < sub2->in_use)
4143 sp2 = sub2->list.line[i].end;
4144 else
4145 sp2 = NULL;
4146 if (sp1 != sp2)
4147 return FALSE;
4148 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004149 }
4150 }
4151
4152 return TRUE;
4153}
4154
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004155#ifdef ENABLE_LOG
4156 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004157report_state(char *action,
4158 regsub_T *sub,
4159 nfa_state_T *state,
4160 int lid,
4161 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004162{
4163 int col;
4164
4165 if (sub->in_use <= 0)
4166 col = -1;
4167 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004168 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004169 else
4170 col = (int)(sub->list.line[0].start - regline);
4171 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004172 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4173 action, abs(state->id), lid, state->c, code, col,
4174 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004175}
4176#endif
4177
Bram Moolenaar43e02982013-06-07 17:31:29 +02004178/*
4179 * Return TRUE if the same state is already in list "l" with the same
4180 * positions as "subs".
4181 */
4182 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004183has_state_with_pos(
4184 nfa_list_T *l, /* runtime state list */
4185 nfa_state_T *state, /* state to update */
4186 regsubs_T *subs, /* pointers to subexpressions */
4187 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004188{
4189 nfa_thread_T *thread;
4190 int i;
4191
4192 for (i = 0; i < l->n; ++i)
4193 {
4194 thread = &l->t[i];
4195 if (thread->state->id == state->id
4196 && sub_equal(&thread->subs.norm, &subs->norm)
4197#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004198 && (!nfa_has_zsubexpr
4199 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004200#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004201 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004202 return TRUE;
4203 }
4204 return FALSE;
4205}
4206
4207/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004208 * Return TRUE if "one" and "two" are equal. That includes when both are not
4209 * set.
4210 */
4211 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004212pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004213{
4214 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4215 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4216
4217 if (one_unused)
4218 /* one is unused: equal when two is also unused */
4219 return two_unused;
4220 if (two_unused)
4221 /* one is used and two is not: not equal */
4222 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004223 /* compare the state id */
4224 if (one->state->id != two->state->id)
4225 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004226 /* compare the position */
4227 if (REG_MULTI)
4228 return one->end.pos.lnum == two->end.pos.lnum
4229 && one->end.pos.col == two->end.pos.col;
4230 return one->end.ptr == two->end.ptr;
4231}
4232
4233/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004234 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4235 */
4236 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004237match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004238{
4239 nfa_state_T *state = startstate;
4240
4241 /* avoid too much recursion */
4242 if (depth > 10)
4243 return FALSE;
4244
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004245 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004246 {
4247 switch (state->c)
4248 {
4249 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004250 case NFA_MCLOSE:
4251 case NFA_END_INVISIBLE:
4252 case NFA_END_INVISIBLE_NEG:
4253 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004254 return TRUE;
4255
4256 case NFA_SPLIT:
4257 return match_follows(state->out, depth + 1)
4258 || match_follows(state->out1, depth + 1);
4259
4260 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004261 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004262 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004263 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004264 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004265 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004266 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004267 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004268 case NFA_COMPOSING:
4269 /* skip ahead to next state */
4270 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004271 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004272
4273 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004274 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004275 case NFA_IDENT:
4276 case NFA_SIDENT:
4277 case NFA_KWORD:
4278 case NFA_SKWORD:
4279 case NFA_FNAME:
4280 case NFA_SFNAME:
4281 case NFA_PRINT:
4282 case NFA_SPRINT:
4283 case NFA_WHITE:
4284 case NFA_NWHITE:
4285 case NFA_DIGIT:
4286 case NFA_NDIGIT:
4287 case NFA_HEX:
4288 case NFA_NHEX:
4289 case NFA_OCTAL:
4290 case NFA_NOCTAL:
4291 case NFA_WORD:
4292 case NFA_NWORD:
4293 case NFA_HEAD:
4294 case NFA_NHEAD:
4295 case NFA_ALPHA:
4296 case NFA_NALPHA:
4297 case NFA_LOWER:
4298 case NFA_NLOWER:
4299 case NFA_UPPER:
4300 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004301 case NFA_LOWER_IC:
4302 case NFA_NLOWER_IC:
4303 case NFA_UPPER_IC:
4304 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004305 case NFA_START_COLL:
4306 case NFA_START_NEG_COLL:
4307 case NFA_NEWL:
4308 /* state will advance input */
4309 return FALSE;
4310
4311 default:
4312 if (state->c > 0)
4313 /* state will advance input */
4314 return FALSE;
4315
4316 /* Others: zero-width or possibly zero-width, might still find
4317 * a match at the same position, keep looking. */
4318 break;
4319 }
4320 state = state->out;
4321 }
4322 return FALSE;
4323}
4324
4325
4326/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004327 * Return TRUE if "state" is already in list "l".
4328 */
4329 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004330state_in_list(
4331 nfa_list_T *l, /* runtime state list */
4332 nfa_state_T *state, /* state to update */
4333 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004334{
4335 if (state->lastlist[nfa_ll_index] == l->id)
4336 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004337 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004338 return TRUE;
4339 }
4340 return FALSE;
4341}
4342
Bram Moolenaar16b35782016-09-09 20:29:50 +02004343/* Offset used for "off" by addstate_here(). */
4344#define ADDSTATE_HERE_OFFSET 10
4345
Bram Moolenaard05bf562013-06-30 23:24:08 +02004346/*
4347 * Add "state" and possibly what follows to state list ".".
4348 * Returns "subs_arg", possibly copied into temp_subs.
4349 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004350 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004351addstate(
4352 nfa_list_T *l, /* runtime state list */
4353 nfa_state_T *state, /* state to update */
4354 regsubs_T *subs_arg, /* pointers to subexpressions */
4355 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004356 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004357{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004358 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004359 int off = off_arg;
4360 int add_here = FALSE;
4361 int listindex = 0;
4362 int k;
4363 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004364 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004365 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004366 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004367 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004368 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004369 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004370 regsubs_T *subs = subs_arg;
4371 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004372#ifdef ENABLE_LOG
4373 int did_print = FALSE;
4374#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375
Bram Moolenaar16b35782016-09-09 20:29:50 +02004376 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4377 {
4378 add_here = TRUE;
4379 off = 0;
4380 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4381 }
4382
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004383 switch (state->c)
4384 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004385 case NFA_NCLOSE:
4386 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004387 case NFA_MCLOSE1:
4388 case NFA_MCLOSE2:
4389 case NFA_MCLOSE3:
4390 case NFA_MCLOSE4:
4391 case NFA_MCLOSE5:
4392 case NFA_MCLOSE6:
4393 case NFA_MCLOSE7:
4394 case NFA_MCLOSE8:
4395 case NFA_MCLOSE9:
4396#ifdef FEAT_SYN_HL
4397 case NFA_ZCLOSE:
4398 case NFA_ZCLOSE1:
4399 case NFA_ZCLOSE2:
4400 case NFA_ZCLOSE3:
4401 case NFA_ZCLOSE4:
4402 case NFA_ZCLOSE5:
4403 case NFA_ZCLOSE6:
4404 case NFA_ZCLOSE7:
4405 case NFA_ZCLOSE8:
4406 case NFA_ZCLOSE9:
4407#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004408 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004409 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004410 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004411 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004412 /* These nodes are not added themselves but their "out" and/or
4413 * "out1" may be added below. */
4414 break;
4415
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004416 case NFA_BOL:
4417 case NFA_BOF:
4418 /* "^" won't match past end-of-line, don't bother trying.
4419 * Except when at the end of the line, or when we are going to the
4420 * next line for a look-behind match. */
4421 if (reginput > regline
4422 && *reginput != NUL
4423 && (nfa_endp == NULL
4424 || !REG_MULTI
4425 || reglnum == nfa_endp->se_u.pos.lnum))
4426 goto skip_add;
4427 /* FALLTHROUGH */
4428
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004429 case NFA_MOPEN1:
4430 case NFA_MOPEN2:
4431 case NFA_MOPEN3:
4432 case NFA_MOPEN4:
4433 case NFA_MOPEN5:
4434 case NFA_MOPEN6:
4435 case NFA_MOPEN7:
4436 case NFA_MOPEN8:
4437 case NFA_MOPEN9:
4438#ifdef FEAT_SYN_HL
4439 case NFA_ZOPEN:
4440 case NFA_ZOPEN1:
4441 case NFA_ZOPEN2:
4442 case NFA_ZOPEN3:
4443 case NFA_ZOPEN4:
4444 case NFA_ZOPEN5:
4445 case NFA_ZOPEN6:
4446 case NFA_ZOPEN7:
4447 case NFA_ZOPEN8:
4448 case NFA_ZOPEN9:
4449#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004450 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004451 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004452 /* These nodes need to be added so that we can bail out when it
4453 * was added to this list before at the same position to avoid an
4454 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004455
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004456 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004457 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004458 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004459 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004460 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004461 * when there is a PIM. For NFA_MATCH check the position,
4462 * lower position is preferred. */
4463 if (!nfa_has_backref && pim == NULL && !l->has_pim
4464 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004465 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004466 /* When called from addstate_here() do insert before
4467 * existing states. */
4468 if (add_here)
4469 {
4470 for (k = 0; k < l->n && k < listindex; ++k)
4471 if (l->t[k].state->id == state->id)
4472 {
4473 found = TRUE;
4474 break;
4475 }
4476 }
4477 if (!add_here || found)
4478 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004479skip_add:
4480#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004481 nfa_set_code(state->c);
4482 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4483 abs(state->id), l->id, state->c, code,
4484 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004485#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004486 return subs;
4487 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004488 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004489
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004490 /* Do not add the state again when it exists with the same
4491 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004492 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004493 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004494 }
4495
Bram Moolenaara0169122013-06-26 18:16:58 +02004496 /* When there are backreferences or PIMs the number of states may
4497 * be (a lot) bigger than anticipated. */
4498 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004499 {
4500 int newlen = l->len * 3 / 2 + 50;
4501
Bram Moolenaard05bf562013-06-30 23:24:08 +02004502 if (subs != &temp_subs)
4503 {
4504 /* "subs" may point into the current array, need to make a
4505 * copy before it becomes invalid. */
4506 copy_sub(&temp_subs.norm, &subs->norm);
4507#ifdef FEAT_SYN_HL
4508 if (nfa_has_zsubexpr)
4509 copy_sub(&temp_subs.synt, &subs->synt);
4510#endif
4511 subs = &temp_subs;
4512 }
4513
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004514 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004515 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4516 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004517 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004518
4519 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004520 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004521 thread = &l->t[l->n++];
4522 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004523 if (pim == NULL)
4524 thread->pim.result = NFA_PIM_UNUSED;
4525 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004526 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004527 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004528 l->has_pim = TRUE;
4529 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004530 copy_sub(&thread->subs.norm, &subs->norm);
4531#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004532 if (nfa_has_zsubexpr)
4533 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004534#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004535#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004536 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004537 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004538#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 }
4540
4541#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004542 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004543 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004544#endif
4545 switch (state->c)
4546 {
4547 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 break;
4549
4550 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004551 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004552 subs = addstate(l, state->out, subs, pim, off_arg);
4553 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004554 break;
4555
Bram Moolenaar699c1202013-09-25 16:41:54 +02004556 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004557 case NFA_NOPEN:
4558 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004559 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 break;
4561
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004562 case NFA_MOPEN:
4563 case NFA_MOPEN1:
4564 case NFA_MOPEN2:
4565 case NFA_MOPEN3:
4566 case NFA_MOPEN4:
4567 case NFA_MOPEN5:
4568 case NFA_MOPEN6:
4569 case NFA_MOPEN7:
4570 case NFA_MOPEN8:
4571 case NFA_MOPEN9:
4572#ifdef FEAT_SYN_HL
4573 case NFA_ZOPEN:
4574 case NFA_ZOPEN1:
4575 case NFA_ZOPEN2:
4576 case NFA_ZOPEN3:
4577 case NFA_ZOPEN4:
4578 case NFA_ZOPEN5:
4579 case NFA_ZOPEN6:
4580 case NFA_ZOPEN7:
4581 case NFA_ZOPEN8:
4582 case NFA_ZOPEN9:
4583#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004584 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004585 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004586 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004587 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004588 sub = &subs->norm;
4589 }
4590#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004591 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004592 {
4593 subidx = state->c - NFA_ZOPEN;
4594 sub = &subs->synt;
4595 }
4596#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004597 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004598 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004599 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004600 sub = &subs->norm;
4601 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004602
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004603 /* avoid compiler warnings */
4604 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004605 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004606
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004607 /* Set the position (with "off" added) in the subexpression. Save
4608 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004609 if (REG_MULTI)
4610 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004611 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004612 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004613 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004614 save_in_use = -1;
4615 }
4616 else
4617 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004618 save_in_use = sub->in_use;
4619 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004620 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004621 sub->list.multi[i].start_lnum = -1;
4622 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004623 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004624 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004625 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004626 if (off == -1)
4627 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004628 sub->list.multi[subidx].start_lnum = reglnum + 1;
4629 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004630 }
4631 else
4632 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004633 sub->list.multi[subidx].start_lnum = reglnum;
4634 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004635 (colnr_T)(reginput - regline + off);
4636 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004637 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 }
4639 else
4640 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004641 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004642 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004643 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004644 save_in_use = -1;
4645 }
4646 else
4647 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004648 save_in_use = sub->in_use;
4649 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004650 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004651 sub->list.line[i].start = NULL;
4652 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004653 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004654 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004655 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004656 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004657 }
4658
Bram Moolenaar16b35782016-09-09 20:29:50 +02004659 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004660 /* "subs" may have changed, need to set "sub" again */
4661#ifdef FEAT_SYN_HL
4662 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4663 sub = &subs->synt;
4664 else
4665#endif
4666 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004667
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004668 if (save_in_use == -1)
4669 {
4670 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004671 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004672 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004673 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004674 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004675 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004676 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677 break;
4678
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004679 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004680 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004681 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004682 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004684 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004685 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004686 break;
4687 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004688 case NFA_MCLOSE1:
4689 case NFA_MCLOSE2:
4690 case NFA_MCLOSE3:
4691 case NFA_MCLOSE4:
4692 case NFA_MCLOSE5:
4693 case NFA_MCLOSE6:
4694 case NFA_MCLOSE7:
4695 case NFA_MCLOSE8:
4696 case NFA_MCLOSE9:
4697#ifdef FEAT_SYN_HL
4698 case NFA_ZCLOSE:
4699 case NFA_ZCLOSE1:
4700 case NFA_ZCLOSE2:
4701 case NFA_ZCLOSE3:
4702 case NFA_ZCLOSE4:
4703 case NFA_ZCLOSE5:
4704 case NFA_ZCLOSE6:
4705 case NFA_ZCLOSE7:
4706 case NFA_ZCLOSE8:
4707 case NFA_ZCLOSE9:
4708#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004709 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004710 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004711 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004713 sub = &subs->norm;
4714 }
4715#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004716 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004717 {
4718 subidx = state->c - NFA_ZCLOSE;
4719 sub = &subs->synt;
4720 }
4721#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004722 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004723 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004724 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004725 sub = &subs->norm;
4726 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004727
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004728 /* We don't fill in gaps here, there must have been an MOPEN that
4729 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004730 save_in_use = sub->in_use;
4731 if (sub->in_use <= subidx)
4732 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733 if (REG_MULTI)
4734 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004735 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004736 if (off == -1)
4737 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004738 sub->list.multi[subidx].end_lnum = reglnum + 1;
4739 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004740 }
4741 else
4742 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004743 sub->list.multi[subidx].end_lnum = reglnum;
4744 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004745 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004746 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004747 /* avoid compiler warnings */
4748 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004749 }
4750 else
4751 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004752 save_ptr = sub->list.line[subidx].end;
4753 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004754 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004755 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004756 }
4757
Bram Moolenaar16b35782016-09-09 20:29:50 +02004758 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004759 /* "subs" may have changed, need to set "sub" again */
4760#ifdef FEAT_SYN_HL
4761 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4762 sub = &subs->synt;
4763 else
4764#endif
4765 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004766
4767 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004768 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004769 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004770 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004771 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004772 break;
4773 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004774 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004775}
4776
4777/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004778 * Like addstate(), but the new state(s) are put at position "*ip".
4779 * Used for zero-width matches, next state to use is the added one.
4780 * This makes sure the order of states to be tried does not change, which
4781 * matters for alternatives.
4782 */
4783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004784addstate_here(
4785 nfa_list_T *l, /* runtime state list */
4786 nfa_state_T *state, /* state to update */
4787 regsubs_T *subs, /* pointers to subexpressions */
4788 nfa_pim_T *pim, /* postponed look-behind match */
4789 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004790{
4791 int tlen = l->n;
4792 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004793 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004794
Bram Moolenaar16b35782016-09-09 20:29:50 +02004795 /* First add the state(s) at the end, so that we know how many there are.
4796 * Pass the listidx as offset (avoids adding another argument to
4797 * addstate(). */
4798 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004799
Bram Moolenaar4b417062013-05-25 20:19:50 +02004800 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004801 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004802 return;
4803
4804 /* re-order to put the new state at the current position */
4805 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004806 if (count == 0)
4807 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004808 if (count == 1)
4809 {
4810 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004811 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004812 }
4813 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004814 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004815 if (l->n + count - 1 >= l->len)
4816 {
4817 /* not enough space to move the new states, reallocate the list
4818 * and move the states to the right position */
4819 nfa_thread_T *newl;
4820
4821 l->len = l->len * 3 / 2 + 50;
4822 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4823 if (newl == NULL)
4824 return;
4825 mch_memmove(&(newl[0]),
4826 &(l->t[0]),
4827 sizeof(nfa_thread_T) * listidx);
4828 mch_memmove(&(newl[listidx]),
4829 &(l->t[l->n - count]),
4830 sizeof(nfa_thread_T) * count);
4831 mch_memmove(&(newl[listidx + count]),
4832 &(l->t[listidx + 1]),
4833 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4834 vim_free(l->t);
4835 l->t = newl;
4836 }
4837 else
4838 {
4839 /* make space for new states, then move them from the
4840 * end to the current position */
4841 mch_memmove(&(l->t[listidx + count]),
4842 &(l->t[listidx + 1]),
4843 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4844 mch_memmove(&(l->t[listidx]),
4845 &(l->t[l->n - 1]),
4846 sizeof(nfa_thread_T) * count);
4847 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004848 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004849 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004850 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004851}
4852
4853/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004854 * Check character class "class" against current character c.
4855 */
4856 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004857check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004858{
4859 switch (class)
4860 {
4861 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004862 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004863 return OK;
4864 break;
4865 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004866 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004867 return OK;
4868 break;
4869 case NFA_CLASS_BLANK:
4870 if (c == ' ' || c == '\t')
4871 return OK;
4872 break;
4873 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004874 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004875 return OK;
4876 break;
4877 case NFA_CLASS_DIGIT:
4878 if (VIM_ISDIGIT(c))
4879 return OK;
4880 break;
4881 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004882 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004883 return OK;
4884 break;
4885 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004886 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004887 return OK;
4888 break;
4889 case NFA_CLASS_PRINT:
4890 if (vim_isprintc(c))
4891 return OK;
4892 break;
4893 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004894 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004895 return OK;
4896 break;
4897 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004898 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004899 return OK;
4900 break;
4901 case NFA_CLASS_UPPER:
4902 if (MB_ISUPPER(c))
4903 return OK;
4904 break;
4905 case NFA_CLASS_XDIGIT:
4906 if (vim_isxdigit(c))
4907 return OK;
4908 break;
4909 case NFA_CLASS_TAB:
4910 if (c == '\t')
4911 return OK;
4912 break;
4913 case NFA_CLASS_RETURN:
4914 if (c == '\r')
4915 return OK;
4916 break;
4917 case NFA_CLASS_BACKSPACE:
4918 if (c == '\b')
4919 return OK;
4920 break;
4921 case NFA_CLASS_ESCAPE:
4922 if (c == '\033')
4923 return OK;
4924 break;
4925
4926 default:
4927 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004928 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004929 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004930 }
4931 return FAIL;
4932}
4933
Bram Moolenaar5714b802013-05-28 22:03:20 +02004934/*
4935 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004936 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004937 */
4938 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004939match_backref(
4940 regsub_T *sub, /* pointers to subexpressions */
4941 int subidx,
4942 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004943{
4944 int len;
4945
4946 if (sub->in_use <= subidx)
4947 {
4948retempty:
4949 /* backref was not set, match an empty string */
4950 *bytelen = 0;
4951 return TRUE;
4952 }
4953
4954 if (REG_MULTI)
4955 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004956 if (sub->list.multi[subidx].start_lnum < 0
4957 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004958 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004959 if (sub->list.multi[subidx].start_lnum == reglnum
4960 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004961 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004962 len = sub->list.multi[subidx].end_col
4963 - sub->list.multi[subidx].start_col;
4964 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004965 reginput, &len) == 0)
4966 {
4967 *bytelen = len;
4968 return TRUE;
4969 }
4970 }
4971 else
4972 {
4973 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004974 sub->list.multi[subidx].start_lnum,
4975 sub->list.multi[subidx].start_col,
4976 sub->list.multi[subidx].end_lnum,
4977 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004978 bytelen) == RA_MATCH)
4979 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004980 }
4981 }
4982 else
4983 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004984 if (sub->list.line[subidx].start == NULL
4985 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004986 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004987 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4988 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004989 {
4990 *bytelen = len;
4991 return TRUE;
4992 }
4993 }
4994 return FALSE;
4995}
4996
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004997#ifdef FEAT_SYN_HL
4998
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004999static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005000
5001/*
5002 * Check for a match with \z subexpression "subidx".
5003 * Return TRUE if it matches.
5004 */
5005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005006match_zref(
5007 int subidx,
5008 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005009{
5010 int len;
5011
5012 cleanup_zsubexpr();
5013 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5014 {
5015 /* backref was not set, match an empty string */
5016 *bytelen = 0;
5017 return TRUE;
5018 }
5019
5020 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5021 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5022 {
5023 *bytelen = len;
5024 return TRUE;
5025 }
5026 return FALSE;
5027}
5028#endif
5029
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005030/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005031 * Save list IDs for all NFA states of "prog" into "list".
5032 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005033 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005034 */
5035 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005036nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005037{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005038 int i;
5039 nfa_state_T *p;
5040
5041 /* Order in the list is reverse, it's a bit faster that way. */
5042 p = &prog->state[0];
5043 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005044 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005045 list[i] = p->lastlist[1];
5046 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005047 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005048 }
5049}
5050
5051/*
5052 * Restore list IDs from "list" to all NFA states.
5053 */
5054 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005055nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005056{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005057 int i;
5058 nfa_state_T *p;
5059
5060 p = &prog->state[0];
5061 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005062 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005063 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005064 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005065 }
5066}
5067
Bram Moolenaar423532e2013-05-29 21:14:42 +02005068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005069nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005070{
5071 if (op == 1) return pos > val;
5072 if (op == 2) return pos < val;
5073 return val == pos;
5074}
5075
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005076static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5077static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005078
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005079/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005080 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005081 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5082 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005083 */
5084 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005085recursive_regmatch(
5086 nfa_state_T *state,
5087 nfa_pim_T *pim,
5088 nfa_regprog_T *prog,
5089 regsubs_T *submatch,
5090 regsubs_T *m,
5091 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005092{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005093 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005094 int save_reglnum = reglnum;
5095 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005096 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 save_se_T *save_nfa_endp = nfa_endp;
5098 save_se_T endpos;
5099 save_se_T *endposp = NULL;
5100 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005101 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005102
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005103 if (pim != NULL)
5104 {
5105 /* start at the position where the postponed match was */
5106 if (REG_MULTI)
5107 reginput = regline + pim->end.pos.col;
5108 else
5109 reginput = pim->end.ptr;
5110 }
5111
Bram Moolenaardecd9542013-06-07 16:31:50 +02005112 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02005113 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5114 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5115 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005116 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005117 /* The recursive match must end at the current position. When "pim" is
5118 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005119 endposp = &endpos;
5120 if (REG_MULTI)
5121 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005122 if (pim == NULL)
5123 {
5124 endpos.se_u.pos.col = (int)(reginput - regline);
5125 endpos.se_u.pos.lnum = reglnum;
5126 }
5127 else
5128 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005129 }
5130 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005131 {
5132 if (pim == NULL)
5133 endpos.se_u.ptr = reginput;
5134 else
5135 endpos.se_u.ptr = pim->end.ptr;
5136 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005137
5138 /* Go back the specified number of bytes, or as far as the
5139 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005140 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005141 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005142 if (state->val <= 0)
5143 {
5144 if (REG_MULTI)
5145 {
5146 regline = reg_getline(--reglnum);
5147 if (regline == NULL)
5148 /* can't go before the first line */
5149 regline = reg_getline(++reglnum);
5150 }
5151 reginput = regline;
5152 }
5153 else
5154 {
5155 if (REG_MULTI && (int)(reginput - regline) < state->val)
5156 {
5157 /* Not enough bytes in this line, go to end of
5158 * previous line. */
5159 regline = reg_getline(--reglnum);
5160 if (regline == NULL)
5161 {
5162 /* can't go before the first line */
5163 regline = reg_getline(++reglnum);
5164 reginput = regline;
5165 }
5166 else
5167 reginput = regline + STRLEN(regline);
5168 }
5169 if ((int)(reginput - regline) >= state->val)
5170 {
5171 reginput -= state->val;
5172#ifdef FEAT_MBYTE
5173 if (has_mbyte)
5174 reginput -= mb_head_off(regline, reginput);
5175#endif
5176 }
5177 else
5178 reginput = regline;
5179 }
5180 }
5181
Bram Moolenaarf46da702013-06-02 22:37:42 +02005182#ifdef ENABLE_LOG
5183 if (log_fd != stderr)
5184 fclose(log_fd);
5185 log_fd = NULL;
5186#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005187 /* Have to clear the lastlist field of the NFA nodes, so that
5188 * nfa_regmatch() and addstate() can run properly after recursion. */
5189 if (nfa_ll_index == 1)
5190 {
5191 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5192 * values and clear them. */
5193 if (*listids == NULL)
5194 {
5195 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5196 if (*listids == NULL)
5197 {
5198 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5199 return 0;
5200 }
5201 }
5202 nfa_save_listids(prog, *listids);
5203 need_restore = TRUE;
5204 /* any value of nfa_listid will do */
5205 }
5206 else
5207 {
5208 /* First recursive nfa_regmatch() call, switch to the second lastlist
5209 * entry. Make sure nfa_listid is different from a previous recursive
5210 * call, because some states may still have this ID. */
5211 ++nfa_ll_index;
5212 if (nfa_listid <= nfa_alt_listid)
5213 nfa_listid = nfa_alt_listid;
5214 }
5215
5216 /* Call nfa_regmatch() to check if the current concat matches at this
5217 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005218 nfa_endp = endposp;
5219 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005220
5221 if (need_restore)
5222 nfa_restore_listids(prog, *listids);
5223 else
5224 {
5225 --nfa_ll_index;
5226 nfa_alt_listid = nfa_listid;
5227 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005228
5229 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005230 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005231 if (REG_MULTI)
5232 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005233 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005234 if (result != NFA_TOO_EXPENSIVE)
5235 {
5236 nfa_match = save_nfa_match;
5237 nfa_listid = save_nfa_listid;
5238 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005239 nfa_endp = save_nfa_endp;
5240
5241#ifdef ENABLE_LOG
5242 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5243 if (log_fd != NULL)
5244 {
5245 fprintf(log_fd, "****************************\n");
5246 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5247 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5248 fprintf(log_fd, "****************************\n");
5249 }
5250 else
5251 {
5252 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5253 log_fd = stderr;
5254 }
5255#endif
5256
5257 return result;
5258}
5259
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005260static int skip_to_start(int c, colnr_T *colp);
5261static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005262
5263/*
5264 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005265 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005266 * NFA_ANY: 1
5267 * specific character: 99
5268 */
5269 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005270failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005271{
5272 int c = state->c;
5273 int l, r;
5274
5275 /* detect looping */
5276 if (depth > 4)
5277 return 1;
5278
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005279 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005280 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005281 case NFA_SPLIT:
5282 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5283 /* avoid recursive stuff */
5284 return 1;
5285 /* two alternatives, use the lowest failure chance */
5286 l = failure_chance(state->out, depth + 1);
5287 r = failure_chance(state->out1, depth + 1);
5288 return l < r ? l : r;
5289
5290 case NFA_ANY:
5291 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005292 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005293
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005294 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005295 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005296 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005297 /* empty match works always */
5298 return 0;
5299
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005300 case NFA_START_INVISIBLE:
5301 case NFA_START_INVISIBLE_FIRST:
5302 case NFA_START_INVISIBLE_NEG:
5303 case NFA_START_INVISIBLE_NEG_FIRST:
5304 case NFA_START_INVISIBLE_BEFORE:
5305 case NFA_START_INVISIBLE_BEFORE_FIRST:
5306 case NFA_START_INVISIBLE_BEFORE_NEG:
5307 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5308 case NFA_START_PATTERN:
5309 /* recursive regmatch is expensive, use low failure chance */
5310 return 5;
5311
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005312 case NFA_BOL:
5313 case NFA_EOL:
5314 case NFA_BOF:
5315 case NFA_EOF:
5316 case NFA_NEWL:
5317 return 99;
5318
5319 case NFA_BOW:
5320 case NFA_EOW:
5321 return 90;
5322
5323 case NFA_MOPEN:
5324 case NFA_MOPEN1:
5325 case NFA_MOPEN2:
5326 case NFA_MOPEN3:
5327 case NFA_MOPEN4:
5328 case NFA_MOPEN5:
5329 case NFA_MOPEN6:
5330 case NFA_MOPEN7:
5331 case NFA_MOPEN8:
5332 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005333#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005334 case NFA_ZOPEN:
5335 case NFA_ZOPEN1:
5336 case NFA_ZOPEN2:
5337 case NFA_ZOPEN3:
5338 case NFA_ZOPEN4:
5339 case NFA_ZOPEN5:
5340 case NFA_ZOPEN6:
5341 case NFA_ZOPEN7:
5342 case NFA_ZOPEN8:
5343 case NFA_ZOPEN9:
5344 case NFA_ZCLOSE:
5345 case NFA_ZCLOSE1:
5346 case NFA_ZCLOSE2:
5347 case NFA_ZCLOSE3:
5348 case NFA_ZCLOSE4:
5349 case NFA_ZCLOSE5:
5350 case NFA_ZCLOSE6:
5351 case NFA_ZCLOSE7:
5352 case NFA_ZCLOSE8:
5353 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005354#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005355 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005356 case NFA_MCLOSE1:
5357 case NFA_MCLOSE2:
5358 case NFA_MCLOSE3:
5359 case NFA_MCLOSE4:
5360 case NFA_MCLOSE5:
5361 case NFA_MCLOSE6:
5362 case NFA_MCLOSE7:
5363 case NFA_MCLOSE8:
5364 case NFA_MCLOSE9:
5365 case NFA_NCLOSE:
5366 return failure_chance(state->out, depth + 1);
5367
5368 case NFA_BACKREF1:
5369 case NFA_BACKREF2:
5370 case NFA_BACKREF3:
5371 case NFA_BACKREF4:
5372 case NFA_BACKREF5:
5373 case NFA_BACKREF6:
5374 case NFA_BACKREF7:
5375 case NFA_BACKREF8:
5376 case NFA_BACKREF9:
5377#ifdef FEAT_SYN_HL
5378 case NFA_ZREF1:
5379 case NFA_ZREF2:
5380 case NFA_ZREF3:
5381 case NFA_ZREF4:
5382 case NFA_ZREF5:
5383 case NFA_ZREF6:
5384 case NFA_ZREF7:
5385 case NFA_ZREF8:
5386 case NFA_ZREF9:
5387#endif
5388 /* backreferences don't match in many places */
5389 return 94;
5390
5391 case NFA_LNUM_GT:
5392 case NFA_LNUM_LT:
5393 case NFA_COL_GT:
5394 case NFA_COL_LT:
5395 case NFA_VCOL_GT:
5396 case NFA_VCOL_LT:
5397 case NFA_MARK_GT:
5398 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005399 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005400 /* before/after positions don't match very often */
5401 return 85;
5402
5403 case NFA_LNUM:
5404 return 90;
5405
5406 case NFA_CURSOR:
5407 case NFA_COL:
5408 case NFA_VCOL:
5409 case NFA_MARK:
5410 /* specific positions rarely match */
5411 return 98;
5412
5413 case NFA_COMPOSING:
5414 return 95;
5415
5416 default:
5417 if (c > 0)
5418 /* character match fails often */
5419 return 95;
5420 }
5421
5422 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005423 return 50;
5424}
5425
Bram Moolenaarf46da702013-06-02 22:37:42 +02005426/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005427 * Skip until the char "c" we know a match must start with.
5428 */
5429 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005430skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005431{
5432 char_u *s;
5433
5434 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005435 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005436#ifdef FEAT_MBYTE
5437 && !has_mbyte
5438#endif
5439 )
5440 s = vim_strbyte(regline + *colp, c);
5441 else
5442 s = cstrchr(regline + *colp, c);
5443 if (s == NULL)
5444 return FAIL;
5445 *colp = (int)(s - regline);
5446 return OK;
5447}
5448
5449/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005450 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005451 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005452 * Returns zero for no match, 1 for a match.
5453 */
5454 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005455find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005456{
5457 colnr_T col = startcol;
5458 int c1, c2;
5459 int len1, len2;
5460 int match;
5461
5462 for (;;)
5463 {
5464 match = TRUE;
5465 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5466 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5467 {
5468 c1 = PTR2CHAR(match_text + len1);
5469 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005470 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005471 {
5472 match = FALSE;
5473 break;
5474 }
5475 len2 += MB_CHAR2LEN(c2);
5476 }
5477 if (match
5478#ifdef FEAT_MBYTE
5479 /* check that no composing char follows */
5480 && !(enc_utf8
5481 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5482#endif
5483 )
5484 {
5485 cleanup_subexpr();
5486 if (REG_MULTI)
5487 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005488 rex.reg_startpos[0].lnum = reglnum;
5489 rex.reg_startpos[0].col = col;
5490 rex.reg_endpos[0].lnum = reglnum;
5491 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005492 }
5493 else
5494 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005495 rex.reg_startp[0] = regline + col;
5496 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005497 }
5498 return 1L;
5499 }
5500
5501 /* Try finding regstart after the current match. */
5502 col += MB_CHAR2LEN(regstart); /* skip regstart */
5503 if (skip_to_start(regstart, &col) == FAIL)
5504 break;
5505 }
5506 return 0L;
5507}
5508
5509/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005510 * Main matching routine.
5511 *
5512 * Run NFA to determine whether it matches reginput.
5513 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005514 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005515 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005516 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005517 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518 * Note: Caller must ensure that: start != NULL.
5519 */
5520 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005521nfa_regmatch(
5522 nfa_regprog_T *prog,
5523 nfa_state_T *start,
5524 regsubs_T *submatch,
5525 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005527 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005528 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005530 int go_to_nextline = FALSE;
5531 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005532 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005533 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005534 nfa_list_T *thislist;
5535 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005536 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005537 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005538 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005539 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005540 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005541 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005542#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005543 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005544#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005545
Bram Moolenaar41f12052013-08-25 17:01:42 +02005546 /* Some patterns may take a long time to match, especially when using
5547 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5548 fast_breakcheck();
5549 if (got_int)
5550 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005551#ifdef FEAT_RELTIME
5552 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5553 return FALSE;
5554#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005555
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005556#ifdef NFA_REGEXP_DEBUG_LOG
5557 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5558 if (debug == NULL)
5559 {
5560 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5561 return FALSE;
5562 }
5563#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005564 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005566 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005567 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005568
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005569 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005570 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005571 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005572 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005573 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005574 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005575
5576#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005577 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005578 if (log_fd != NULL)
5579 {
5580 fprintf(log_fd, "**********************************\n");
5581 nfa_set_code(start->c);
5582 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5583 abs(start->id), code);
5584 fprintf(log_fd, "**********************************\n");
5585 }
5586 else
5587 {
5588 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5589 log_fd = stderr;
5590 }
5591#endif
5592
5593 thislist = &list[0];
5594 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005595 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005596 nextlist = &list[1];
5597 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005598 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005600 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005601#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005602 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005603
5604 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5605 * it's the first MOPEN. */
5606 if (toplevel)
5607 {
5608 if (REG_MULTI)
5609 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005610 m->norm.list.multi[0].start_lnum = reglnum;
5611 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005612 }
5613 else
5614 m->norm.list.line[0].start = reginput;
5615 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005616 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005617 }
5618 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005619 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005620
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005621#define ADD_STATE_IF_MATCH(state) \
5622 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005623 add_state = state->out; \
5624 add_off = clen; \
5625 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005626
5627 /*
5628 * Run for each character.
5629 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005630 for (;;)
5631 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005632 int curc;
5633 int clen;
5634
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005635#ifdef FEAT_MBYTE
5636 if (has_mbyte)
5637 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005638 curc = (*mb_ptr2char)(reginput);
5639 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005640 }
5641 else
5642#endif
5643 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005644 curc = *reginput;
5645 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005646 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005647 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005648 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005649 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005650 go_to_nextline = FALSE;
5651 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005652
5653 /* swap lists */
5654 thislist = &list[flag];
5655 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005656 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005657 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005658 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005659 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5660 {
5661 /* too many states, retry with old engine */
5662 nfa_match = NFA_TOO_EXPENSIVE;
5663 goto theend;
5664 }
5665
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005666 thislist->id = nfa_listid;
5667 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005668
5669#ifdef ENABLE_LOG
5670 fprintf(log_fd, "------------------------------------------\n");
5671 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005672 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005673 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005674 {
5675 int i;
5676
5677 for (i = 0; i < thislist->n; i++)
5678 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5679 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005680 fprintf(log_fd, "\n");
5681#endif
5682
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005683#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005684 fprintf(debug, "\n-------------------\n");
5685#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005686 /*
5687 * If the state lists are empty we can stop.
5688 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005689 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005690 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005691
5692 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005693 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005694 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005695 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005696
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005697#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005698 nfa_set_code(t->state->c);
5699 fprintf(debug, "%s, ", code);
5700#endif
5701#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005702 {
5703 int col;
5704
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005705 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005706 col = -1;
5707 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005708 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005709 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005710 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005711 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005712 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5713 abs(t->state->id), (int)t->state->c, code, col,
5714 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005715 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005716#endif
5717
5718 /*
5719 * Handle the possible codes of the current state.
5720 * The most important is NFA_MATCH.
5721 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005722 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005723 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005724 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005725 switch (t->state->c)
5726 {
5727 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005728 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005729#ifdef FEAT_MBYTE
5730 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005731 * rex.reg_icombine is not set, that is not really a match. */
5732 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005733 break;
5734#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005735 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005736 copy_sub(&submatch->norm, &t->subs.norm);
5737#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005738 if (nfa_has_zsubexpr)
5739 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005740#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005741#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005742 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005743#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005744 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005745 * states at this position. When the list of states is going
5746 * to be empty quit without advancing, so that "reginput" is
5747 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005748 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005749 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005750 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005752
5753 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005754 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005755 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005756 /*
5757 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005758 * NFA_START_INVISIBLE_BEFORE node.
5759 * They surround a zero-width group, used with "\@=", "\&",
5760 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005761 * If we got here, it means that the current "invisible" group
5762 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005763 * nfa_regmatch(). For a look-behind match only when it ends
5764 * in the position in "nfa_endp".
5765 * Submatches are stored in *m, and used in the parent call.
5766 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005767#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005768 if (nfa_endp != NULL)
5769 {
5770 if (REG_MULTI)
5771 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5772 (int)reglnum,
5773 (int)nfa_endp->se_u.pos.lnum,
5774 (int)(reginput - regline),
5775 nfa_endp->se_u.pos.col);
5776 else
5777 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5778 (int)(reginput - regline),
5779 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005780 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005781#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005782 /* If "nfa_endp" is set it's only a match if it ends at
5783 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005784 if (nfa_endp != NULL && (REG_MULTI
5785 ? (reglnum != nfa_endp->se_u.pos.lnum
5786 || (int)(reginput - regline)
5787 != nfa_endp->se_u.pos.col)
5788 : reginput != nfa_endp->se_u.ptr))
5789 break;
5790
5791 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005792 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005793 {
5794 copy_sub(&m->norm, &t->subs.norm);
5795#ifdef FEAT_SYN_HL
5796 if (nfa_has_zsubexpr)
5797 copy_sub(&m->synt, &t->subs.synt);
5798#endif
5799 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005800#ifdef ENABLE_LOG
5801 fprintf(log_fd, "Match found:\n");
5802 log_subsexpr(m);
5803#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005804 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005805 /* See comment above at "goto nextchar". */
5806 if (nextlist->n == 0)
5807 clen = 0;
5808 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005809
5810 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005811 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005812 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005813 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005814 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005815 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005816 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005817 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005818 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005819#ifdef ENABLE_LOG
5820 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5821 failure_chance(t->state->out, 0),
5822 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005823#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005824 /* Do it directly if there already is a PIM or when
5825 * nfa_postprocess() detected it will work better. */
5826 if (t->pim.result != NFA_PIM_UNUSED
5827 || t->state->c == NFA_START_INVISIBLE_FIRST
5828 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5829 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5830 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005831 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005832 int in_use = m->norm.in_use;
5833
Bram Moolenaar699c1202013-09-25 16:41:54 +02005834 /* Copy submatch info for the recursive call, opposite
5835 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005836 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005837#ifdef FEAT_SYN_HL
5838 if (nfa_has_zsubexpr)
5839 copy_sub_off(&m->synt, &t->subs.synt);
5840#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005841
Bram Moolenaara2d95102013-06-04 14:23:05 +02005842 /*
5843 * First try matching the invisible match, then what
5844 * follows.
5845 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005846 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005847 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005848 if (result == NFA_TOO_EXPENSIVE)
5849 {
5850 nfa_match = result;
5851 goto theend;
5852 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005853
Bram Moolenaardecd9542013-06-07 16:31:50 +02005854 /* for \@! and \@<! it is a match when the result is
5855 * FALSE */
5856 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005857 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5858 || t->state->c
5859 == NFA_START_INVISIBLE_BEFORE_NEG
5860 || t->state->c
5861 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005862 {
5863 /* Copy submatch info from the recursive call */
5864 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005865#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005866 if (nfa_has_zsubexpr)
5867 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005868#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005869 /* If the pattern has \ze and it matched in the
5870 * sub pattern, use it. */
5871 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005872
Bram Moolenaara2d95102013-06-04 14:23:05 +02005873 /* t->state->out1 is the corresponding
5874 * END_INVISIBLE node; Add its out to the current
5875 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005876 add_here = TRUE;
5877 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005878 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005879 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005880 }
5881 else
5882 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005883 nfa_pim_T pim;
5884
Bram Moolenaara2d95102013-06-04 14:23:05 +02005885 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005886 * First try matching what follows. Only if a match
5887 * is found verify the invisible match matches. Add a
5888 * nfa_pim_T to the following states, it contains info
5889 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005890 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005891 pim.state = t->state;
5892 pim.result = NFA_PIM_TODO;
5893 pim.subs.norm.in_use = 0;
5894#ifdef FEAT_SYN_HL
5895 pim.subs.synt.in_use = 0;
5896#endif
5897 if (REG_MULTI)
5898 {
5899 pim.end.pos.col = (int)(reginput - regline);
5900 pim.end.pos.lnum = reglnum;
5901 }
5902 else
5903 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005904
5905 /* t->state->out1 is the corresponding END_INVISIBLE
5906 * node; Add its out to the current list (zero-width
5907 * match). */
5908 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005909 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005910 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005911 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005912 break;
5913
Bram Moolenaar87953742013-06-05 18:52:40 +02005914 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005915 {
5916 nfa_state_T *skip = NULL;
5917#ifdef ENABLE_LOG
5918 int skip_lid = 0;
5919#endif
5920
5921 /* There is no point in trying to match the pattern if the
5922 * output state is not going to be added to the list. */
5923 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5924 {
5925 skip = t->state->out1->out;
5926#ifdef ENABLE_LOG
5927 skip_lid = nextlist->id;
5928#endif
5929 }
5930 else if (state_in_list(nextlist,
5931 t->state->out1->out->out, &t->subs))
5932 {
5933 skip = t->state->out1->out->out;
5934#ifdef ENABLE_LOG
5935 skip_lid = nextlist->id;
5936#endif
5937 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005938 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005939 t->state->out1->out->out, &t->subs))
5940 {
5941 skip = t->state->out1->out->out;
5942#ifdef ENABLE_LOG
5943 skip_lid = thislist->id;
5944#endif
5945 }
5946 if (skip != NULL)
5947 {
5948#ifdef ENABLE_LOG
5949 nfa_set_code(skip->c);
5950 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5951 abs(skip->id), skip_lid, skip->c, code);
5952#endif
5953 break;
5954 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005955 /* Copy submatch info to the recursive call, opposite of what
5956 * happens afterwards. */
5957 copy_sub_off(&m->norm, &t->subs.norm);
5958#ifdef FEAT_SYN_HL
5959 if (nfa_has_zsubexpr)
5960 copy_sub_off(&m->synt, &t->subs.synt);
5961#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005962
Bram Moolenaar87953742013-06-05 18:52:40 +02005963 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005964 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005965 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005966 if (result == NFA_TOO_EXPENSIVE)
5967 {
5968 nfa_match = result;
5969 goto theend;
5970 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005971 if (result)
5972 {
5973 int bytelen;
5974
5975#ifdef ENABLE_LOG
5976 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5977 log_subsexpr(m);
5978#endif
5979 /* Copy submatch info from the recursive call */
5980 copy_sub_off(&t->subs.norm, &m->norm);
5981#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005982 if (nfa_has_zsubexpr)
5983 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005984#endif
5985 /* Now we need to skip over the matched text and then
5986 * continue with what follows. */
5987 if (REG_MULTI)
5988 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005989 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02005990 - (int)(reginput - regline);
5991 else
5992 bytelen = (int)(m->norm.list.line[0].end - reginput);
5993
5994#ifdef ENABLE_LOG
5995 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5996#endif
5997 if (bytelen == 0)
5998 {
5999 /* empty match, output of corresponding
6000 * NFA_END_PATTERN/NFA_SKIP to be used at current
6001 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006002 add_here = TRUE;
6003 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006004 }
6005 else if (bytelen <= clen)
6006 {
6007 /* match current character, output of corresponding
6008 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006009 add_state = t->state->out1->out->out;
6010 add_off = clen;
6011 }
6012 else
6013 {
6014 /* skip over the matched characters, set character
6015 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006016 add_state = t->state->out1->out;
6017 add_off = bytelen;
6018 add_count = bytelen - clen;
6019 }
6020 }
6021 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006022 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006023
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006024 case NFA_BOL:
6025 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006026 {
6027 add_here = TRUE;
6028 add_state = t->state->out;
6029 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006030 break;
6031
6032 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006033 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006034 {
6035 add_here = TRUE;
6036 add_state = t->state->out;
6037 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006038 break;
6039
6040 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006041 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006042
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006043 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006044 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006045#ifdef FEAT_MBYTE
6046 else if (has_mbyte)
6047 {
6048 int this_class;
6049
6050 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006051 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006052 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006053 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006055 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006056 }
6057#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006058 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006059 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006060 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006061 result = FALSE;
6062 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006063 {
6064 add_here = TRUE;
6065 add_state = t->state->out;
6066 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006068
6069 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006070 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006071 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006072 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006073#ifdef FEAT_MBYTE
6074 else if (has_mbyte)
6075 {
6076 int this_class, prev_class;
6077
6078 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006079 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006080 prev_class = reg_prev_class();
6081 if (this_class == prev_class
6082 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006083 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006084 }
6085#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006086 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006087 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006088 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006089 result = FALSE;
6090 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006091 {
6092 add_here = TRUE;
6093 add_state = t->state->out;
6094 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006095 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006096
Bram Moolenaar4b780632013-05-31 22:14:52 +02006097 case NFA_BOF:
6098 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006099 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006100 {
6101 add_here = TRUE;
6102 add_state = t->state->out;
6103 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006104 break;
6105
6106 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006107 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006108 {
6109 add_here = TRUE;
6110 add_state = t->state->out;
6111 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006112 break;
6113
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006114#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006116 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006117 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006118 int len = 0;
6119 nfa_state_T *end;
6120 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006121 int cchars[MAX_MCO];
6122 int ccount = 0;
6123 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006124
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006125 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006126 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006127 if (utf_iscomposing(sta->c))
6128 {
6129 /* Only match composing character(s), ignore base
6130 * character. Used for ".{composing}" and "{composing}"
6131 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006132 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006133 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006134 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006135 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006136 /* If \Z was present, then ignore composing characters.
6137 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006138 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006139 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006140 else
6141 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006142 while (sta->c != NFA_END_COMPOSING)
6143 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006144 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006145
6146 /* Check base character matches first, unless ignored. */
6147 else if (len > 0 || mc == sta->c)
6148 {
6149 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006150 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006151 len += mb_char2len(mc);
6152 sta = sta->out;
6153 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006154
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006155 /* We don't care about the order of composing characters.
6156 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006157 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006158 {
6159 mc = mb_ptr2char(reginput + len);
6160 cchars[ccount++] = mc;
6161 len += mb_char2len(mc);
6162 if (ccount == MAX_MCO)
6163 break;
6164 }
6165
6166 /* Check that each composing char in the pattern matches a
6167 * composing char in the text. We do not check if all
6168 * composing chars are matched. */
6169 result = OK;
6170 while (sta->c != NFA_END_COMPOSING)
6171 {
6172 for (j = 0; j < ccount; ++j)
6173 if (cchars[j] == sta->c)
6174 break;
6175 if (j == ccount)
6176 {
6177 result = FAIL;
6178 break;
6179 }
6180 sta = sta->out;
6181 }
6182 }
6183 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006184 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006185
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006186 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006187 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006189 }
6190#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006191
6192 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006193 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6194 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006195 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006196 go_to_nextline = TRUE;
6197 /* Pass -1 for the offset, which means taking the position
6198 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006199 add_state = t->state->out;
6200 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006201 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006202 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006203 {
6204 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006205 add_state = t->state->out;
6206 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006207 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006208 break;
6209
Bram Moolenaar417bad22013-06-07 14:08:30 +02006210 case NFA_START_COLL:
6211 case NFA_START_NEG_COLL:
6212 {
6213 /* What follows is a list of characters, until NFA_END_COLL.
6214 * One of them must match or none of them must match. */
6215 nfa_state_T *state;
6216 int result_if_matched;
6217 int c1, c2;
6218
6219 /* Never match EOL. If it's part of the collection it is added
6220 * as a separate state with an OR. */
6221 if (curc == NUL)
6222 break;
6223
6224 state = t->state->out;
6225 result_if_matched = (t->state->c == NFA_START_COLL);
6226 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006227 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006228 if (state->c == NFA_END_COLL)
6229 {
6230 result = !result_if_matched;
6231 break;
6232 }
6233 if (state->c == NFA_RANGE_MIN)
6234 {
6235 c1 = state->val;
6236 state = state->out; /* advance to NFA_RANGE_MAX */
6237 c2 = state->val;
6238#ifdef ENABLE_LOG
6239 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6240 curc, c1, c2);
6241#endif
6242 if (curc >= c1 && curc <= c2)
6243 {
6244 result = result_if_matched;
6245 break;
6246 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006247 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006248 {
6249 int curc_low = MB_TOLOWER(curc);
6250 int done = FALSE;
6251
6252 for ( ; c1 <= c2; ++c1)
6253 if (MB_TOLOWER(c1) == curc_low)
6254 {
6255 result = result_if_matched;
6256 done = TRUE;
6257 break;
6258 }
6259 if (done)
6260 break;
6261 }
6262 }
6263 else if (state->c < 0 ? check_char_class(state->c, curc)
6264 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006265 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006266 == MB_TOLOWER(state->c))))
6267 {
6268 result = result_if_matched;
6269 break;
6270 }
6271 state = state->out;
6272 }
6273 if (result)
6274 {
6275 /* next state is in out of the NFA_END_COLL, out1 of
6276 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006277 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006278 add_off = clen;
6279 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006280 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006281 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006282
6283 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006284 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006285 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006286 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006287 add_state = t->state->out;
6288 add_off = clen;
6289 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006290 break;
6291
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006292 case NFA_ANY_COMPOSING:
6293 /* On a composing character skip over it. Otherwise do
6294 * nothing. Always matches. */
6295#ifdef FEAT_MBYTE
6296 if (enc_utf8 && utf_iscomposing(curc))
6297 {
6298 add_off = clen;
6299 }
6300 else
6301#endif
6302 {
6303 add_here = TRUE;
6304 add_off = 0;
6305 }
6306 add_state = t->state->out;
6307 break;
6308
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006309 /*
6310 * Character classes like \a for alpha, \d for digit etc.
6311 */
6312 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006313 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006314 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006315 break;
6316
6317 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006318 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006319 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006320 break;
6321
6322 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006323 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006324 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006325 break;
6326
6327 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006328 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006329 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006330 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006331 break;
6332
6333 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006334 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006335 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006336 break;
6337
6338 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006339 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006340 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006341 break;
6342
6343 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006344 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006345 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006346 break;
6347
6348 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006349 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
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_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006354 result = VIM_ISWHITE(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_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006359 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006360 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006361 break;
6362
6363 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006364 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006365 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 break;
6367
6368 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006369 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006370 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006371 break;
6372
6373 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006374 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006375 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006376 break;
6377
6378 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006379 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006380 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006381 break;
6382
6383 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006384 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006385 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386 break;
6387
6388 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006389 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006390 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391 break;
6392
6393 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006394 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006395 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006396 break;
6397
6398 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006399 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006400 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 break;
6402
6403 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006404 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006405 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006406 break;
6407
6408 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006409 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006410 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006411 break;
6412
6413 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006414 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006415 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 break;
6417
6418 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006419 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006420 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006421 break;
6422
6423 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006424 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006425 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006426 break;
6427
6428 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006429 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006430 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006431 break;
6432
6433 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006434 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006435 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006436 break;
6437
6438 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006439 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006440 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006441 break;
6442
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006443 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006444 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006445 ADD_STATE_IF_MATCH(t->state);
6446 break;
6447
6448 case NFA_NLOWER_IC: /* [^a-z] */
6449 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006450 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006451 ADD_STATE_IF_MATCH(t->state);
6452 break;
6453
6454 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006455 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006456 ADD_STATE_IF_MATCH(t->state);
6457 break;
6458
6459 case NFA_NUPPER_IC: /* ^[A-Z] */
6460 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006461 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006462 ADD_STATE_IF_MATCH(t->state);
6463 break;
6464
Bram Moolenaar5714b802013-05-28 22:03:20 +02006465 case NFA_BACKREF1:
6466 case NFA_BACKREF2:
6467 case NFA_BACKREF3:
6468 case NFA_BACKREF4:
6469 case NFA_BACKREF5:
6470 case NFA_BACKREF6:
6471 case NFA_BACKREF7:
6472 case NFA_BACKREF8:
6473 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006474#ifdef FEAT_SYN_HL
6475 case NFA_ZREF1:
6476 case NFA_ZREF2:
6477 case NFA_ZREF3:
6478 case NFA_ZREF4:
6479 case NFA_ZREF5:
6480 case NFA_ZREF6:
6481 case NFA_ZREF7:
6482 case NFA_ZREF8:
6483 case NFA_ZREF9:
6484#endif
6485 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006486 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006487 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006488 int bytelen;
6489
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006490 if (t->state->c <= NFA_BACKREF9)
6491 {
6492 subidx = t->state->c - NFA_BACKREF1 + 1;
6493 result = match_backref(&t->subs.norm, subidx, &bytelen);
6494 }
6495#ifdef FEAT_SYN_HL
6496 else
6497 {
6498 subidx = t->state->c - NFA_ZREF1 + 1;
6499 result = match_zref(subidx, &bytelen);
6500 }
6501#endif
6502
Bram Moolenaar5714b802013-05-28 22:03:20 +02006503 if (result)
6504 {
6505 if (bytelen == 0)
6506 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006507 /* empty match always works, output of NFA_SKIP to be
6508 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006509 add_here = TRUE;
6510 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006511 }
6512 else if (bytelen <= clen)
6513 {
6514 /* match current character, jump ahead to out of
6515 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006516 add_state = t->state->out->out;
6517 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006518 }
6519 else
6520 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006521 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006522 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006523 add_state = t->state->out;
6524 add_off = bytelen;
6525 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006526 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006527 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006528 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006529 }
6530 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006531 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006532 if (t->count - clen <= 0)
6533 {
6534 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006535 add_state = t->state->out;
6536 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006537 }
6538 else
6539 {
6540 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006541 add_state = t->state;
6542 add_off = 0;
6543 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006544 }
6545 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006546
Bram Moolenaar423532e2013-05-29 21:14:42 +02006547 case NFA_LNUM:
6548 case NFA_LNUM_GT:
6549 case NFA_LNUM_LT:
6550 result = (REG_MULTI &&
6551 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006552 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006553 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006554 {
6555 add_here = TRUE;
6556 add_state = t->state->out;
6557 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006558 break;
6559
6560 case NFA_COL:
6561 case NFA_COL_GT:
6562 case NFA_COL_LT:
6563 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6564 (long_u)(reginput - regline) + 1);
6565 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006566 {
6567 add_here = TRUE;
6568 add_state = t->state->out;
6569 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006570 break;
6571
6572 case NFA_VCOL:
6573 case NFA_VCOL_GT:
6574 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006575 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006576 int op = t->state->c - NFA_VCOL;
6577 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006578 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006579
6580 /* Bail out quickly when there can't be a match, avoid the
6581 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006582 if (op != 1 && col > t->state->val
6583#ifdef FEAT_MBYTE
6584 * (has_mbyte ? MB_MAXBYTES : 1)
6585#endif
6586 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006587 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006588 result = FALSE;
6589 if (op == 1 && col - 1 > t->state->val && col > 100)
6590 {
6591 int ts = wp->w_buffer->b_p_ts;
6592
6593 /* Guess that a character won't use more columns than
6594 * 'tabstop', with a minimum of 4. */
6595 if (ts < 4)
6596 ts = 4;
6597 result = col > t->state->val * ts;
6598 }
6599 if (!result)
6600 result = nfa_re_num_cmp(t->state->val, op,
6601 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006602 if (result)
6603 {
6604 add_here = TRUE;
6605 add_state = t->state->out;
6606 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006607 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006608 break;
6609
Bram Moolenaar044aa292013-06-04 21:27:38 +02006610 case NFA_MARK:
6611 case NFA_MARK_GT:
6612 case NFA_MARK_LT:
6613 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006614 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006615
6616 /* Compare the mark position to the match position. */
6617 result = (pos != NULL /* mark doesn't exist */
6618 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006619 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006620 ? (pos->col == (colnr_T)(reginput - regline)
6621 ? t->state->c == NFA_MARK
6622 : (pos->col < (colnr_T)(reginput - regline)
6623 ? t->state->c == NFA_MARK_GT
6624 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006625 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006626 ? t->state->c == NFA_MARK_GT
6627 : t->state->c == NFA_MARK_LT)));
6628 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006629 {
6630 add_here = TRUE;
6631 add_state = t->state->out;
6632 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006633 break;
6634 }
6635
Bram Moolenaar423532e2013-05-29 21:14:42 +02006636 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006637 result = (rex.reg_win != NULL
6638 && (reglnum + rex.reg_firstlnum
6639 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006640 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006641 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006642 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006643 {
6644 add_here = TRUE;
6645 add_state = t->state->out;
6646 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006647 break;
6648
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006649 case NFA_VISUAL:
6650 result = reg_match_visual();
6651 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006652 {
6653 add_here = TRUE;
6654 add_state = t->state->out;
6655 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006656 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006657
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006658 case NFA_MOPEN1:
6659 case NFA_MOPEN2:
6660 case NFA_MOPEN3:
6661 case NFA_MOPEN4:
6662 case NFA_MOPEN5:
6663 case NFA_MOPEN6:
6664 case NFA_MOPEN7:
6665 case NFA_MOPEN8:
6666 case NFA_MOPEN9:
6667#ifdef FEAT_SYN_HL
6668 case NFA_ZOPEN:
6669 case NFA_ZOPEN1:
6670 case NFA_ZOPEN2:
6671 case NFA_ZOPEN3:
6672 case NFA_ZOPEN4:
6673 case NFA_ZOPEN5:
6674 case NFA_ZOPEN6:
6675 case NFA_ZOPEN7:
6676 case NFA_ZOPEN8:
6677 case NFA_ZOPEN9:
6678#endif
6679 case NFA_NOPEN:
6680 case NFA_ZSTART:
6681 /* These states are only added to be able to bail out when
6682 * they are added again, nothing is to be done. */
6683 break;
6684
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006685 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006686 {
6687 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006688
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006689#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006690 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006691 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006692#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006693 result = (c == curc);
6694
Bram Moolenaar6100d022016-10-02 16:51:57 +02006695 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006696 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006697#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006698 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006699 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006700 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006701 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006702#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006703 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006704 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006705 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006706
6707 } /* switch (t->state->c) */
6708
6709 if (add_state != NULL)
6710 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006711 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006712 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006713
6714 if (t->pim.result == NFA_PIM_UNUSED)
6715 pim = NULL;
6716 else
6717 pim = &t->pim;
6718
6719 /* Handle the postponed invisible match if the match might end
6720 * without advancing and before the end of the line. */
6721 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006722 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006723 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006724 {
6725#ifdef ENABLE_LOG
6726 fprintf(log_fd, "\n");
6727 fprintf(log_fd, "==================================\n");
6728 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6729 fprintf(log_fd, "\n");
6730#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006731 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006732 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006733 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006734 /* for \@! and \@<! it is a match when the result is
6735 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006736 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006737 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6738 || pim->state->c
6739 == NFA_START_INVISIBLE_BEFORE_NEG
6740 || pim->state->c
6741 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006742 {
6743 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006744 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006745#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006746 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006747 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006748#endif
6749 }
6750 }
6751 else
6752 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006753 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006754#ifdef ENABLE_LOG
6755 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006756 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006757 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6758 fprintf(log_fd, "\n");
6759#endif
6760 }
6761
Bram Moolenaardecd9542013-06-07 16:31:50 +02006762 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006763 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006764 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6765 || pim->state->c
6766 == NFA_START_INVISIBLE_BEFORE_NEG
6767 || pim->state->c
6768 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006769 {
6770 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006771 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006773 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006774 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006775#endif
6776 }
6777 else
6778 /* look-behind match failed, don't add the state */
6779 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006780
6781 /* Postponed invisible match was handled, don't add it to
6782 * following states. */
6783 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006784 }
6785
Bram Moolenaara951e352013-10-06 15:46:11 +02006786 /* If "pim" points into l->t it will become invalid when
6787 * adding the state causes the list to be reallocated. Make a
6788 * local copy to avoid that. */
6789 if (pim == &t->pim)
6790 {
6791 copy_pim(&pim_copy, pim);
6792 pim = &pim_copy;
6793 }
6794
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006795 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006796 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006797 else
6798 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006799 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006800 if (add_count > 0)
6801 nextlist->t[nextlist->n - 1].count = add_count;
6802 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006803 }
6804
6805 } /* for (thislist = thislist; thislist->state; thislist++) */
6806
Bram Moolenaare23febd2013-05-26 18:40:14 +02006807 /* Look for the start of a match in the current position by adding the
6808 * start state to the list of states.
6809 * The first found match is the leftmost one, thus the order of states
6810 * matters!
6811 * Do not add the start state in recursive calls of nfa_regmatch(),
6812 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006813 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006814 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006815 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006816 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006817 && reglnum == 0
6818 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006819 && (rex.reg_maxcol == 0
6820 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006821 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006822 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006823 ? (reglnum < nfa_endp->se_u.pos.lnum
6824 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006825 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006826 < nfa_endp->se_u.pos.col))
6827 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006828 {
6829#ifdef ENABLE_LOG
6830 fprintf(log_fd, "(---) STARTSTATE\n");
6831#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006832 /* Inline optimized code for addstate() if we know the state is
6833 * the first MOPEN. */
6834 if (toplevel)
6835 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006836 int add = TRUE;
6837 int c;
6838
6839 if (prog->regstart != NUL && clen != 0)
6840 {
6841 if (nextlist->n == 0)
6842 {
6843 colnr_T col = (colnr_T)(reginput - regline) + clen;
6844
6845 /* Nextlist is empty, we can skip ahead to the
6846 * character that must appear at the start. */
6847 if (skip_to_start(prog->regstart, &col) == FAIL)
6848 break;
6849#ifdef ENABLE_LOG
6850 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6851 col - ((colnr_T)(reginput - regline) + clen));
6852#endif
6853 reginput = regline + col - clen;
6854 }
6855 else
6856 {
6857 /* Checking if the required start character matches is
6858 * cheaper than adding a state that won't match. */
6859 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006860 if (c != prog->regstart && (!rex.reg_ic
6861 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006862 {
6863#ifdef ENABLE_LOG
6864 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6865#endif
6866 add = FALSE;
6867 }
6868 }
6869 }
6870
6871 if (add)
6872 {
6873 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006874 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006875 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006876 else
6877 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006878 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006879 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006880 }
6881 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006882 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006883 }
6884
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006885#ifdef ENABLE_LOG
6886 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006887 {
6888 int i;
6889
6890 for (i = 0; i < thislist->n; i++)
6891 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6892 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006893 fprintf(log_fd, "\n");
6894#endif
6895
6896nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006897 /* Advance to the next character, or advance to the next line, or
6898 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006899 if (clen != 0)
6900 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006901 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6902 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006903 reg_nextline();
6904 else
6905 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006906
6907 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006908 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006909 if (got_int)
6910 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006911#ifdef FEAT_RELTIME
6912 /* Check for timeout once in a twenty times to avoid overhead. */
6913 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6914 {
6915 nfa_time_count = 0;
6916 if (profile_passed_limit(nfa_time_limit))
6917 break;
6918 }
6919#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006920 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006921
6922#ifdef ENABLE_LOG
6923 if (log_fd != stderr)
6924 fclose(log_fd);
6925 log_fd = NULL;
6926#endif
6927
6928theend:
6929 /* Free memory */
6930 vim_free(list[0].t);
6931 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006932 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006933#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006934#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006935 fclose(debug);
6936#endif
6937
Bram Moolenaar963fee22013-05-26 21:47:28 +02006938 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006939}
6940
6941/*
6942 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006943 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006944 */
6945 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006946nfa_regtry(
6947 nfa_regprog_T *prog,
6948 colnr_T col,
6949 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006950{
6951 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006952 regsubs_T subs, m;
6953 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006954 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006955#ifdef ENABLE_LOG
6956 FILE *f;
6957#endif
6958
6959 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006960#ifdef FEAT_RELTIME
6961 nfa_time_limit = tm;
6962 nfa_time_count = 0;
6963#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006964
6965#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006966 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006967 if (f != NULL)
6968 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006969 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006970#ifdef DEBUG
6971 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6972#endif
6973 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006974 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006975 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006976 fprintf(f, "\n\n");
6977 fclose(f);
6978 }
6979 else
6980 EMSG(_("Could not open temporary log file for writing "));
6981#endif
6982
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006983 clear_sub(&subs.norm);
6984 clear_sub(&m.norm);
6985#ifdef FEAT_SYN_HL
6986 clear_sub(&subs.synt);
6987 clear_sub(&m.synt);
6988#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006989
Bram Moolenaarfda37292014-11-05 14:27:36 +01006990 result = nfa_regmatch(prog, start, &subs, &m);
6991 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006993 else if (result == NFA_TOO_EXPENSIVE)
6994 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006995
6996 cleanup_subexpr();
6997 if (REG_MULTI)
6998 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006999 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007001 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7002 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007003
Bram Moolenaar6100d022016-10-02 16:51:57 +02007004 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7005 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007006 }
7007
Bram Moolenaar6100d022016-10-02 16:51:57 +02007008 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007009 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007010 rex.reg_startpos[0].lnum = 0;
7011 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007012 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007013 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007015 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007016 rex.reg_endpos[0].lnum = reglnum;
7017 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007018 }
7019 else
7020 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007021 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022 }
7023 else
7024 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007025 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007026 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007027 rex.reg_startp[i] = subs.norm.list.line[i].start;
7028 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007029 }
7030
Bram Moolenaar6100d022016-10-02 16:51:57 +02007031 if (rex.reg_startp[0] == NULL)
7032 rex.reg_startp[0] = regline + col;
7033 if (rex.reg_endp[0] == NULL)
7034 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007035 }
7036
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007037#ifdef FEAT_SYN_HL
7038 /* Package any found \z(...\) matches for export. Default is none. */
7039 unref_extmatch(re_extmatch_out);
7040 re_extmatch_out = NULL;
7041
7042 if (prog->reghasz == REX_SET)
7043 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007044 cleanup_zsubexpr();
7045 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007046 /* Loop over \z1, \z2, etc. There is no \z0. */
7047 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007048 {
7049 if (REG_MULTI)
7050 {
7051 struct multipos *mpos = &subs.synt.list.multi[i];
7052
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007053 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007054 if (mpos->start_lnum >= 0
7055 && mpos->start_lnum == mpos->end_lnum
7056 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007057 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007058 vim_strnsave(reg_getline(mpos->start_lnum)
7059 + mpos->start_col,
7060 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007061 }
7062 else
7063 {
7064 struct linepos *lpos = &subs.synt.list.line[i];
7065
7066 if (lpos->start != NULL && lpos->end != NULL)
7067 re_extmatch_out->matches[i] =
7068 vim_strnsave(lpos->start,
7069 (int)(lpos->end - lpos->start));
7070 }
7071 }
7072 }
7073#endif
7074
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007075 return 1 + reglnum;
7076}
7077
7078/*
7079 * Match a regexp against a string ("line" points to the string) or multiple
7080 * lines ("line" is NULL, use reg_getline()).
7081 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007082 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007083 */
7084 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007085nfa_regexec_both(
7086 char_u *line,
7087 colnr_T startcol, /* column to start looking for match */
7088 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007089{
7090 nfa_regprog_T *prog;
7091 long retval = 0L;
7092 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007093 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007094
7095 if (REG_MULTI)
7096 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007097 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007098 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007099 rex.reg_startpos = rex.reg_mmatch->startpos;
7100 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007101 }
7102 else
7103 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007104 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7105 rex.reg_startp = rex.reg_match->startp;
7106 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007107 }
7108
7109 /* Be paranoid... */
7110 if (prog == NULL || line == NULL)
7111 {
7112 EMSG(_(e_null));
7113 goto theend;
7114 }
7115
Bram Moolenaar6100d022016-10-02 16:51:57 +02007116 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007117 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007118 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007119 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007120 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121
7122#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007123 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007124 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007125 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007126#endif
7127
7128 regline = line;
7129 reglnum = 0; /* relative to line */
7130
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007131 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007132 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007133 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007134 nfa_listid = 1;
7135 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007136 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007137
Bram Moolenaard89616e2013-06-06 18:46:06 +02007138 if (prog->reganch && col > 0)
7139 return 0L;
7140
Bram Moolenaar473de612013-06-08 18:19:48 +02007141 need_clear_subexpr = TRUE;
7142#ifdef FEAT_SYN_HL
7143 /* Clear the external match subpointers if necessary. */
7144 if (prog->reghasz == REX_SET)
7145 {
7146 nfa_has_zsubexpr = TRUE;
7147 need_clear_zsubexpr = TRUE;
7148 }
7149 else
7150 nfa_has_zsubexpr = FALSE;
7151#endif
7152
Bram Moolenaard89616e2013-06-06 18:46:06 +02007153 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007154 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007155 /* Skip ahead until a character we know the match must start with.
7156 * When there is none there is no match. */
7157 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007158 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007159
Bram Moolenaar473de612013-06-08 18:19:48 +02007160 /* If match_text is set it contains the full text that must match.
7161 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007162 if (prog->match_text != NULL
7163#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007164 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007165#endif
7166 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007167 return find_match_text(col, prog->regstart, prog->match_text);
7168 }
7169
Bram Moolenaard89616e2013-06-06 18:46:06 +02007170 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007171 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007172 goto theend;
7173
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007174 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007175 for (i = 0; i < nstate; ++i)
7176 {
7177 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007178 prog->state[i].lastlist[0] = 0;
7179 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007180 }
7181
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007182 retval = nfa_regtry(prog, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007183
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007184 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007185
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007186theend:
7187 return retval;
7188}
7189
7190/*
7191 * Compile a regular expression into internal code for the NFA matcher.
7192 * Returns the program in allocated space. Returns NULL for an error.
7193 */
7194 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007195nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007196{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007197 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007198 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007199 int *postfix;
7200
7201 if (expr == NULL)
7202 return NULL;
7203
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007204 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007205 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007206
7207 init_class_tab();
7208
7209 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7210 return NULL;
7211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007212 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007213 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007214 postfix = re2post();
7215 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007216 {
7217 /* TODO: only give this error for debugging? */
7218 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007219 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007220 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007221 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007222
7223 /*
7224 * In order to build the NFA, we parse the input regexp twice:
7225 * 1. first pass to count size (so we can allocate space)
7226 * 2. second to emit code
7227 */
7228#ifdef ENABLE_LOG
7229 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007230 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007231
7232 if (f != NULL)
7233 {
7234 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7235 fclose(f);
7236 }
7237 }
7238#endif
7239
7240 /*
7241 * PASS 1
7242 * Count number of NFA states in "nstate". Do not build the NFA.
7243 */
7244 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007245
Bram Moolenaar16619a22013-06-11 18:42:36 +02007246 /* allocate the regprog with space for the compiled regexp */
7247 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007248 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7249 if (prog == NULL)
7250 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007251 state_ptr = prog->state;
7252
7253 /*
7254 * PASS 2
7255 * Build the NFA
7256 */
7257 prog->start = post2nfa(postfix, post_ptr, FALSE);
7258 if (prog->start == NULL)
7259 goto fail;
7260
7261 prog->regflags = regflags;
7262 prog->engine = &nfa_regengine;
7263 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007264 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007265 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007266 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007267
Bram Moolenaara2947e22013-06-11 22:44:09 +02007268 nfa_postprocess(prog);
7269
Bram Moolenaard89616e2013-06-06 18:46:06 +02007270 prog->reganch = nfa_get_reganch(prog->start, 0);
7271 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007272 prog->match_text = nfa_get_match_text(prog->start);
7273
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007274#ifdef ENABLE_LOG
7275 nfa_postfix_dump(expr, OK);
7276 nfa_dump(prog);
7277#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007278#ifdef FEAT_SYN_HL
7279 /* Remember whether this pattern has any \z specials in it. */
7280 prog->reghasz = re_has_z;
7281#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007282 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007283 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007284
7285out:
7286 vim_free(post_start);
7287 post_start = post_ptr = post_end = NULL;
7288 state_ptr = NULL;
7289 return (regprog_T *)prog;
7290
7291fail:
7292 vim_free(prog);
7293 prog = NULL;
7294#ifdef ENABLE_LOG
7295 nfa_postfix_dump(expr, FAIL);
7296#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007297 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007298 goto out;
7299}
7300
Bram Moolenaar473de612013-06-08 18:19:48 +02007301/*
7302 * Free a compiled regexp program, returned by nfa_regcomp().
7303 */
7304 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007305nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007306{
7307 if (prog != NULL)
7308 {
7309 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007310 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007311 vim_free(prog);
7312 }
7313}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007314
7315/*
7316 * Match a regexp against a string.
7317 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7318 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007319 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007320 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007321 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007322 */
7323 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007324nfa_regexec_nl(
7325 regmatch_T *rmp,
7326 char_u *line, /* string to match against */
7327 colnr_T col, /* column to start looking for match */
7328 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007329{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007330 rex.reg_match = rmp;
7331 rex.reg_mmatch = NULL;
7332 rex.reg_maxline = 0;
7333 rex.reg_line_lbr = line_lbr;
7334 rex.reg_buf = curbuf;
7335 rex.reg_win = NULL;
7336 rex.reg_ic = rmp->rm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007337#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007338 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007339#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007340 rex.reg_maxcol = 0;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007341 return nfa_regexec_both(line, col, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007342}
7343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007344
7345/*
7346 * Match a regexp against multiple lines.
7347 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7348 * Uses curbuf for line count and 'iskeyword'.
7349 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007350 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007351 * match otherwise.
7352 *
7353 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7354 *
7355 * ! Also NOTE : match may actually be in another line. e.g.:
7356 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7357 *
7358 * +-------------------------+
7359 * |a |
7360 * |b |
7361 * |c |
7362 * | |
7363 * +-------------------------+
7364 *
7365 * then nfa_regexec_multi() returns 3. while the original
7366 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7367 *
7368 * FIXME if this behavior is not compatible.
7369 */
7370 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007371nfa_regexec_multi(
7372 regmmatch_T *rmp,
7373 win_T *win, /* window in which to search or NULL */
7374 buf_T *buf, /* buffer in which to search */
7375 linenr_T lnum, /* nr of line to start looking for match */
7376 colnr_T col, /* column to start looking for match */
7377 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007378{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007379 rex.reg_match = NULL;
7380 rex.reg_mmatch = rmp;
7381 rex.reg_buf = buf;
7382 rex.reg_win = win;
7383 rex.reg_firstlnum = lnum;
7384 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7385 rex.reg_line_lbr = FALSE;
7386 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007387#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007388 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007389#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007390 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007391
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007392 return nfa_regexec_both(NULL, col, tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007393}
7394
7395#ifdef DEBUG
7396# undef ENABLE_LOG
7397#endif