blob: fe0df3503ee7aaa8c474ac360662b02868a216cd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
Bram Moolenaarf446b482017-01-10 13:55:14 +010053 NFA_STAR, /* greedy * (postfix only) */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020054 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaare0ad3652015-01-27 12:59:55 +0100247/* re_flags passed to nfa_regcomp() */
248static int nfa_re_flags;
249
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200250/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200251static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200252
Bram Moolenaar428e9872013-05-30 17:05:39 +0200253/* NFA regexp \1 .. \9 encountered. */
254static int nfa_has_backref;
255
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257/* NFA regexp has \z( ), set zsubexpr. */
258static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200259#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200260
Bram Moolenaar963fee22013-05-26 21:47:28 +0200261/* Number of sub expressions actually being used during execution. 1 if only
262 * the whole match (subexpr 0) is used. */
263static int nfa_nsubexpr;
264
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200265static int *post_start; /* holds the postfix form of r.e. */
266static int *post_end;
267static int *post_ptr;
268
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200269static int nstate; /* Number of states in the NFA. Also used when
270 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200271static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaar307aa162013-06-02 16:34:21 +0200273/* If not NULL match must end at this position */
274static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200275
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200276/* listid is global, so that it increases on recursive calls to
277 * nfa_regmatch(), which means we don't have to clear the lastlist field of
278 * all the states. */
279static int nfa_listid;
280static int nfa_alt_listid;
281
282/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
283static int nfa_ll_index = 0;
284
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100285static int nfa_regcomp_start(char_u *expr, int re_flags);
286static int nfa_get_reganch(nfa_state_T *start, int depth);
287static int nfa_get_regstart(nfa_state_T *start, int depth);
288static char_u *nfa_get_match_text(nfa_state_T *start);
289static int realloc_post_list(void);
290static int nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl);
291static int nfa_emit_equi_class(int c);
292static int nfa_regatom(void);
293static int nfa_regpiece(void);
294static int nfa_regconcat(void);
295static int nfa_regbranch(void);
296static int nfa_reg(int paren);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200297#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100298static void nfa_set_code(int c);
299static void nfa_postfix_dump(char_u *expr, int retval);
300static void nfa_print_state(FILE *debugf, nfa_state_T *state);
301static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent);
302static void nfa_dump(nfa_regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200303#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100304static int *re2post(void);
305static nfa_state_T *alloc_state(int c, nfa_state_T *out, nfa_state_T *out1);
306static void st_error(int *postfix, int *end, int *p);
307static int nfa_max_width(nfa_state_T *startstate, int depth);
308static nfa_state_T *post2nfa(int *postfix, int *end, int nfa_calc_size);
309static void nfa_postprocess(nfa_regprog_T *prog);
310static int check_char_class(int class, int c);
311static void nfa_save_listids(nfa_regprog_T *prog, int *list);
312static void nfa_restore_listids(nfa_regprog_T *prog, int *list);
313static int nfa_re_num_cmp(long_u val, int op, long_u pos);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200314static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm, int *timed_out);
315static long nfa_regexec_both(char_u *line, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100316static regprog_T *nfa_regcomp(char_u *expr, int re_flags);
317static void nfa_regfree(regprog_T *prog);
318static int nfa_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200319static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100320static int match_follows(nfa_state_T *startstate, int depth);
321static int failure_chance(nfa_state_T *state, int depth);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322
323/* helper functions used when doing re2post() ... regatom() parsing */
324#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200325 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326 return FAIL; \
327 *post_ptr++ = c; \
328 } while (0)
329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200330/*
331 * Initialize internal variables before NFA compilation.
332 * Return OK on success, FAIL otherwise.
333 */
334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335nfa_regcomp_start(
336 char_u *expr,
337 int re_flags) /* see vim_regcomp() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200339 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200340 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200341
342 nstate = 0;
343 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200344 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200345 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200346
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200348 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200349 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200350
351 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200352 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354 post_start = (int *)lalloc(postfix_size, TRUE);
355 if (post_start == NULL)
356 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200358 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200360 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200362 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200363 regcomp_start(expr, re_flags);
364
365 return OK;
366}
367
368/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200369 * Figure out if the NFA state list starts with an anchor, must match at start
370 * of the line.
371 */
372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100373nfa_get_reganch(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200374{
375 nfa_state_T *p = start;
376
377 if (depth > 4)
378 return 0;
379
380 while (p != NULL)
381 {
382 switch (p->c)
383 {
384 case NFA_BOL:
385 case NFA_BOF:
386 return 1; /* yes! */
387
388 case NFA_ZSTART:
389 case NFA_ZEND:
390 case NFA_CURSOR:
391 case NFA_VISUAL:
392
393 case NFA_MOPEN:
394 case NFA_MOPEN1:
395 case NFA_MOPEN2:
396 case NFA_MOPEN3:
397 case NFA_MOPEN4:
398 case NFA_MOPEN5:
399 case NFA_MOPEN6:
400 case NFA_MOPEN7:
401 case NFA_MOPEN8:
402 case NFA_MOPEN9:
403 case NFA_NOPEN:
404#ifdef FEAT_SYN_HL
405 case NFA_ZOPEN:
406 case NFA_ZOPEN1:
407 case NFA_ZOPEN2:
408 case NFA_ZOPEN3:
409 case NFA_ZOPEN4:
410 case NFA_ZOPEN5:
411 case NFA_ZOPEN6:
412 case NFA_ZOPEN7:
413 case NFA_ZOPEN8:
414 case NFA_ZOPEN9:
415#endif
416 p = p->out;
417 break;
418
419 case NFA_SPLIT:
420 return nfa_get_reganch(p->out, depth + 1)
421 && nfa_get_reganch(p->out1, depth + 1);
422
423 default:
424 return 0; /* noooo */
425 }
426 }
427 return 0;
428}
429
430/*
431 * Figure out if the NFA state list starts with a character which must match
432 * at start of the match.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435nfa_get_regstart(nfa_state_T *start, int depth)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200436{
437 nfa_state_T *p = start;
438
439 if (depth > 4)
440 return 0;
441
442 while (p != NULL)
443 {
444 switch (p->c)
445 {
446 /* all kinds of zero-width matches */
447 case NFA_BOL:
448 case NFA_BOF:
449 case NFA_BOW:
450 case NFA_EOW:
451 case NFA_ZSTART:
452 case NFA_ZEND:
453 case NFA_CURSOR:
454 case NFA_VISUAL:
455 case NFA_LNUM:
456 case NFA_LNUM_GT:
457 case NFA_LNUM_LT:
458 case NFA_COL:
459 case NFA_COL_GT:
460 case NFA_COL_LT:
461 case NFA_VCOL:
462 case NFA_VCOL_GT:
463 case NFA_VCOL_LT:
464 case NFA_MARK:
465 case NFA_MARK_GT:
466 case NFA_MARK_LT:
467
468 case NFA_MOPEN:
469 case NFA_MOPEN1:
470 case NFA_MOPEN2:
471 case NFA_MOPEN3:
472 case NFA_MOPEN4:
473 case NFA_MOPEN5:
474 case NFA_MOPEN6:
475 case NFA_MOPEN7:
476 case NFA_MOPEN8:
477 case NFA_MOPEN9:
478 case NFA_NOPEN:
479#ifdef FEAT_SYN_HL
480 case NFA_ZOPEN:
481 case NFA_ZOPEN1:
482 case NFA_ZOPEN2:
483 case NFA_ZOPEN3:
484 case NFA_ZOPEN4:
485 case NFA_ZOPEN5:
486 case NFA_ZOPEN6:
487 case NFA_ZOPEN7:
488 case NFA_ZOPEN8:
489 case NFA_ZOPEN9:
490#endif
491 p = p->out;
492 break;
493
494 case NFA_SPLIT:
495 {
496 int c1 = nfa_get_regstart(p->out, depth + 1);
497 int c2 = nfa_get_regstart(p->out1, depth + 1);
498
499 if (c1 == c2)
500 return c1; /* yes! */
501 return 0;
502 }
503
504 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200505 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200506 return p->c; /* yes! */
507 return 0;
508 }
509 }
510 return 0;
511}
512
513/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200514 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200515 * else. If so return a string in allocated memory with what must match after
516 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200517 */
518 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +0100519nfa_get_match_text(nfa_state_T *start)
Bram Moolenaar473de612013-06-08 18:19:48 +0200520{
521 nfa_state_T *p = start;
522 int len = 0;
523 char_u *ret;
524 char_u *s;
525
526 if (p->c != NFA_MOPEN)
527 return NULL; /* just in case */
528 p = p->out;
529 while (p->c > 0)
530 {
531 len += MB_CHAR2LEN(p->c);
532 p = p->out;
533 }
534 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
535 return NULL;
536
537 ret = alloc(len);
538 if (ret != NULL)
539 {
Bram Moolenaar473de612013-06-08 18:19:48 +0200540 p = start->out->out; /* skip first char, it goes into regstart */
541 s = ret;
542 while (p->c > 0)
543 {
544#ifdef FEAT_MBYTE
545 if (has_mbyte)
546 s += (*mb_char2bytes)(p->c, s);
547 else
548#endif
549 *s++ = p->c;
550 p = p->out;
551 }
552 *s = NUL;
553 }
554 return ret;
555}
556
557/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 * Allocate more space for post_start. Called when
559 * running above the estimated number of states.
560 */
561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100562realloc_post_list(void)
Bram Moolenaar16299b52013-05-30 18:45:23 +0200563{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200564 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 int new_max = nstate_max + 1000;
566 int *new_start;
567 int *old_start;
568
569 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
570 if (new_start == NULL)
571 return FAIL;
572 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200573 old_start = post_start;
574 post_start = new_start;
575 post_ptr = new_start + (post_ptr - old_start);
576 post_end = post_start + new_max;
577 vim_free(old_start);
578 return OK;
579}
580
581/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 * Search between "start" and "end" and try to recognize a
583 * character class in expanded form. For example [0-9].
584 * On success, return the id the character class to be emitted.
585 * On failure, return 0 (=FAIL)
586 * Start points to the first char of the range, while end should point
587 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200588 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
589 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 */
591 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100592nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200594# define CLASS_not 0x80
595# define CLASS_af 0x40
596# define CLASS_AF 0x20
597# define CLASS_az 0x10
598# define CLASS_AZ 0x08
599# define CLASS_o7 0x04
600# define CLASS_o9 0x02
601# define CLASS_underscore 0x01
602
603 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200604 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200605 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200606
607 if (extra_newl == TRUE)
608 newl = TRUE;
609
610 if (*end != ']')
611 return FAIL;
612 p = start;
613 if (*p == '^')
614 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200615 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200616 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200617 }
618
619 while (p < end)
620 {
621 if (p + 2 < end && *(p + 1) == '-')
622 {
623 switch (*p)
624 {
625 case '0':
626 if (*(p + 2) == '9')
627 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200628 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200629 break;
630 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200631 if (*(p + 2) == '7')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200636 return FAIL;
637
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 case 'a':
639 if (*(p + 2) == 'z')
640 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200641 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200642 break;
643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 if (*(p + 2) == 'f')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200649 return FAIL;
650
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200651 case 'A':
652 if (*(p + 2) == 'Z')
653 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200654 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200655 break;
656 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200657 if (*(p + 2) == 'F')
658 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200659 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660 break;
661 }
Bram Moolenaarbb7943b2017-06-05 13:30:06 +0200662 return FAIL;
663
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200664 default:
665 return FAIL;
666 }
667 p += 3;
668 }
669 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
670 {
671 newl = TRUE;
672 p += 2;
673 }
674 else if (*p == '_')
675 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200676 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200677 p ++;
678 }
679 else if (*p == '\n')
680 {
681 newl = TRUE;
682 p ++;
683 }
684 else
685 return FAIL;
686 } /* while (p < end) */
687
688 if (p != end)
689 return FAIL;
690
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200691 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200692 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200693
694 switch (config)
695 {
696 case CLASS_o9:
697 return extra_newl + NFA_DIGIT;
698 case CLASS_not | CLASS_o9:
699 return extra_newl + NFA_NDIGIT;
700 case CLASS_af | CLASS_AF | CLASS_o9:
701 return extra_newl + NFA_HEX;
702 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
703 return extra_newl + NFA_NHEX;
704 case CLASS_o7:
705 return extra_newl + NFA_OCTAL;
706 case CLASS_not | CLASS_o7:
707 return extra_newl + NFA_NOCTAL;
708 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
709 return extra_newl + NFA_WORD;
710 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
711 return extra_newl + NFA_NWORD;
712 case CLASS_az | CLASS_AZ | CLASS_underscore:
713 return extra_newl + NFA_HEAD;
714 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
715 return extra_newl + NFA_NHEAD;
716 case CLASS_az | CLASS_AZ:
717 return extra_newl + NFA_ALPHA;
718 case CLASS_not | CLASS_az | CLASS_AZ:
719 return extra_newl + NFA_NALPHA;
720 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200721 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200722 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200723 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200724 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200726 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200727 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200729 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730}
731
732/*
733 * Produce the bytes for equivalence class "c".
734 * Currently only handles latin1, latin9 and utf-8.
735 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
736 * equivalent to 'a OR b OR c'
737 *
738 * NOTE! When changing this function, also update reg_equi_class()
739 */
740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100741nfa_emit_equi_class(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200742{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200743#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
744#ifdef FEAT_MBYTE
745# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
746#else
747# define EMITMBC(c)
748#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200749
750#ifdef FEAT_MBYTE
751 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
752 || STRCMP(p_enc, "iso-8859-15") == 0)
753#endif
754 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200755#ifdef EBCDIC
756# define A_circumflex 0x62
757# define A_diaeresis 0x63
758# define A_grave 0x64
759# define A_acute 0x65
760# define A_virguilla 0x66
761# define A_ring 0x67
762# define C_cedilla 0x68
763# define E_acute 0x71
764# define E_circumflex 0x72
765# define E_diaeresis 0x73
766# define E_grave 0x74
767# define I_acute 0x75
768# define I_circumflex 0x76
769# define I_diaeresis 0x77
770# define I_grave 0x78
771# define N_virguilla 0x69
772# define O_circumflex 0xeb
773# define O_diaeresis 0xec
774# define O_grave 0xed
775# define O_acute 0xee
776# define O_virguilla 0xef
777# define O_slash 0x80
778# define U_circumflex 0xfb
779# define U_diaeresis 0xfc
780# define U_grave 0xfd
781# define U_acute 0xfe
782# define Y_acute 0xba
783# define a_grave 0x42
784# define a_acute 0x43
785# define a_circumflex 0x44
786# define a_virguilla 0x45
787# define a_diaeresis 0x46
788# define a_ring 0x47
789# define c_cedilla 0x48
790# define e_grave 0x51
791# define e_acute 0x52
792# define e_circumflex 0x53
793# define e_diaeresis 0x54
794# define i_grave 0x55
795# define i_acute 0x56
796# define i_circumflex 0x57
797# define i_diaeresis 0x58
798# define n_virguilla 0x49
799# define o_grave 0xcb
800# define o_acute 0xcc
801# define o_circumflex 0xcd
802# define o_virguilla 0xce
803# define o_diaeresis 0xcf
804# define o_slash 0x70
805# define u_grave 0xdb
806# define u_acute 0xdc
807# define u_circumflex 0xdd
808# define u_diaeresis 0xde
809# define y_acute 0x8d
810# define y_diaeresis 0xdf
811#else
812# define A_grave 0xc0
813# define A_acute 0xc1
814# define A_circumflex 0xc2
815# define A_virguilla 0xc3
816# define A_diaeresis 0xc4
817# define A_ring 0xc5
818# define C_cedilla 0xc7
819# define E_grave 0xc8
820# define E_acute 0xc9
821# define E_circumflex 0xca
822# define E_diaeresis 0xcb
823# define I_grave 0xcc
824# define I_acute 0xcd
825# define I_circumflex 0xce
826# define I_diaeresis 0xcf
827# define N_virguilla 0xd1
828# define O_grave 0xd2
829# define O_acute 0xd3
830# define O_circumflex 0xd4
831# define O_virguilla 0xd5
832# define O_diaeresis 0xd6
833# define O_slash 0xd8
834# define U_grave 0xd9
835# define U_acute 0xda
836# define U_circumflex 0xdb
837# define U_diaeresis 0xdc
838# define Y_acute 0xdd
839# define a_grave 0xe0
840# define a_acute 0xe1
841# define a_circumflex 0xe2
842# define a_virguilla 0xe3
843# define a_diaeresis 0xe4
844# define a_ring 0xe5
845# define c_cedilla 0xe7
846# define e_grave 0xe8
847# define e_acute 0xe9
848# define e_circumflex 0xea
849# define e_diaeresis 0xeb
850# define i_grave 0xec
851# define i_acute 0xed
852# define i_circumflex 0xee
853# define i_diaeresis 0xef
854# define n_virguilla 0xf1
855# define o_grave 0xf2
856# define o_acute 0xf3
857# define o_circumflex 0xf4
858# define o_virguilla 0xf5
859# define o_diaeresis 0xf6
860# define o_slash 0xf8
861# define u_grave 0xf9
862# define u_acute 0xfa
863# define u_circumflex 0xfb
864# define u_diaeresis 0xfc
865# define y_acute 0xfd
866# define y_diaeresis 0xff
867#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200868 switch (c)
869 {
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200870 case 'A': case A_grave: case A_acute: case A_circumflex:
871 case A_virguilla: case A_diaeresis: case A_ring:
872 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104)
873 CASEMBC(0x1cd) CASEMBC(0x1de) CASEMBC(0x1e0)
874 CASEMBC(0x1ea2)
875 EMIT2('A'); EMIT2(A_grave); EMIT2(A_acute);
876 EMIT2(A_circumflex); EMIT2(A_virguilla);
877 EMIT2(A_diaeresis); EMIT2(A_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200878 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
879 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
880 EMITMBC(0x1ea2)
881 return OK;
882
883 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
884 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 return OK;
886
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200887 case 'C': case C_cedilla: CASEMBC(0x106) CASEMBC(0x108)
888 CASEMBC(0x10a) CASEMBC(0x10c)
889 EMIT2('C'); EMIT2(C_cedilla);
890 EMITMBC(0x106) EMITMBC(0x108)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200891 EMITMBC(0x10a) EMITMBC(0x10c)
892 return OK;
893
894 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200895 CASEMBC(0x1e0e) CASEMBC(0x1e10)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200896 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
897 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200898 return OK;
899
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200900 case 'E': case E_grave: case E_acute: case E_circumflex:
901 case E_diaeresis: CASEMBC(0x112) CASEMBC(0x114)
902 CASEMBC(0x116) CASEMBC(0x118) CASEMBC(0x11a)
903 CASEMBC(0x1eba) CASEMBC(0x1ebc)
904 EMIT2('E'); EMIT2(E_grave); EMIT2(E_acute);
905 EMIT2(E_circumflex); EMIT2(E_diaeresis);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200906 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
907 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
908 EMITMBC(0x1ebc)
909 return OK;
910
911 case 'F': CASEMBC(0x1e1e)
912 EMIT2('F'); EMITMBC(0x1e1e)
913 return OK;
914
915 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200916 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6)
917 CASEMBC(0x1f4) CASEMBC(0x1e20)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200918 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
919 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
920 EMITMBC(0x1f4) EMITMBC(0x1e20)
921 return OK;
922
923 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200924 CASEMBC(0x1e26) CASEMBC(0x1e28)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200925 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
926 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927 return OK;
928
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200929 case 'I': case I_grave: case I_acute: case I_circumflex:
930 case I_diaeresis: CASEMBC(0x128) CASEMBC(0x12a)
931 CASEMBC(0x12c) CASEMBC(0x12e) CASEMBC(0x130)
932 CASEMBC(0x1cf) CASEMBC(0x1ec8)
933 EMIT2('I'); EMIT2(I_grave); EMIT2(I_acute);
934 EMIT2(I_circumflex); EMIT2(I_diaeresis);
935 EMITMBC(0x128) EMITMBC(0x12a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200936 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
937 EMITMBC(0x1cf) EMITMBC(0x1ec8)
938 return OK;
939
940 case 'J': CASEMBC(0x134)
941 EMIT2('J'); EMITMBC(0x134)
942 return OK;
943
944 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200945 CASEMBC(0x1e34)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200946 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
947 EMITMBC(0x1e34)
948 return OK;
949
950 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200951 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200952 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
953 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
954 return OK;
955
956 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
957 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200958 return OK;
959
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200960 case 'N': case N_virguilla: CASEMBC(0x143) CASEMBC(0x145)
961 CASEMBC(0x147) CASEMBC(0x1e44) CASEMBC(0x1e48)
962 EMIT2('N'); EMIT2(N_virguilla);
963 EMITMBC(0x143) EMITMBC(0x145)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200964 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200965 return OK;
966
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200967 case 'O': case O_grave: case O_acute: case O_circumflex:
968 case O_virguilla: case O_diaeresis: case O_slash:
969 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150)
970 CASEMBC(0x1a0) CASEMBC(0x1d1) CASEMBC(0x1ea)
971 CASEMBC(0x1ec) CASEMBC(0x1ece)
972 EMIT2('O'); EMIT2(O_grave); EMIT2(O_acute);
973 EMIT2(O_circumflex); EMIT2(O_virguilla);
974 EMIT2(O_diaeresis); EMIT2(O_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200975 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
976 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
977 EMITMBC(0x1ec) EMITMBC(0x1ece)
978 return OK;
979
980 case 'P': case 0x1e54: case 0x1e56:
981 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
982 return OK;
983
984 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200985 CASEMBC(0x1e58) CASEMBC(0x1e5e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200986 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
987 EMITMBC(0x1e58) EMITMBC(0x1e5e)
988 return OK;
989
990 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200991 CASEMBC(0x160) CASEMBC(0x1e60)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200992 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
993 EMITMBC(0x160) EMITMBC(0x1e60)
994 return OK;
995
996 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +0200997 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200998 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
999 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001000 return OK;
1001
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001002 case 'U': case U_grave: case U_acute: case U_diaeresis:
1003 case U_circumflex: CASEMBC(0x168) CASEMBC(0x16a)
1004 CASEMBC(0x16c) CASEMBC(0x16e) CASEMBC(0x170)
1005 CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
1006 CASEMBC(0x1ee6)
1007 EMIT2('U'); EMIT2(U_grave); EMIT2(U_acute);
1008 EMIT2(U_diaeresis); EMIT2(U_circumflex);
1009 EMITMBC(0x168) EMITMBC(0x16a)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001010 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
1011 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
1012 EMITMBC(0x1ee6)
1013 return OK;
1014
1015 case 'V': CASEMBC(0x1e7c)
1016 EMIT2('V'); EMITMBC(0x1e7c)
1017 return OK;
1018
1019 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001020 CASEMBC(0x1e84) CASEMBC(0x1e86)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001021 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
1022 EMITMBC(0x1e84) EMITMBC(0x1e86)
1023 return OK;
1024
1025 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
1026 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001027 return OK;
1028
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001029 case 'Y': case Y_acute: CASEMBC(0x176) CASEMBC(0x178)
1030 CASEMBC(0x1e8e) CASEMBC(0x1ef2) CASEMBC(0x1ef6)
1031 CASEMBC(0x1ef8)
1032 EMIT2('Y'); EMIT2(Y_acute);
1033 EMITMBC(0x176) EMITMBC(0x178)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001034 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
1035 EMITMBC(0x1ef8)
1036 return OK;
1037
1038 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001039 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001040 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
1041 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001042 return OK;
1043
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001044 case 'a': case a_grave: case a_acute: case a_circumflex:
1045 case a_virguilla: case a_diaeresis: case a_ring:
1046 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105)
1047 CASEMBC(0x1ce) CASEMBC(0x1df) CASEMBC(0x1e1)
1048 CASEMBC(0x1ea3)
1049 EMIT2('a'); EMIT2(a_grave); EMIT2(a_acute);
1050 EMIT2(a_circumflex); EMIT2(a_virguilla);
1051 EMIT2(a_diaeresis); EMIT2(a_ring);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001052 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
1053 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
1054 EMITMBC(0x1ea3)
1055 return OK;
1056
1057 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1058 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001059 return OK;
1060
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001061 case 'c': case c_cedilla: CASEMBC(0x107) CASEMBC(0x109)
1062 CASEMBC(0x10b) CASEMBC(0x10d)
1063 EMIT2('c'); EMIT2(c_cedilla);
1064 EMITMBC(0x107) EMITMBC(0x109)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001065 EMITMBC(0x10b) EMITMBC(0x10d)
1066 return OK;
1067
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001068 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001069 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001070 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111)
1071 EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001072 return OK;
1073
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001074 case 'e': case e_grave: case e_acute: case e_circumflex:
1075 case e_diaeresis: CASEMBC(0x113) CASEMBC(0x115)
1076 CASEMBC(0x117) CASEMBC(0x119) CASEMBC(0x11b)
1077 CASEMBC(0x1ebb) CASEMBC(0x1ebd)
1078 EMIT2('e'); EMIT2(e_grave); EMIT2(e_acute);
1079 EMIT2(e_circumflex); EMIT2(e_diaeresis);
1080 EMITMBC(0x113) EMITMBC(0x115)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001081 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
1082 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
1083 return OK;
1084
1085 case 'f': CASEMBC(0x1e1f)
1086 EMIT2('f'); EMITMBC(0x1e1f)
1087 return OK;
1088
1089 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001090 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7)
1091 CASEMBC(0x1f5) CASEMBC(0x1e21)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
1093 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
1094 EMITMBC(0x1f5) EMITMBC(0x1e21)
1095 return OK;
1096
1097 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001098 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001099 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
1100 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001101 return OK;
1102
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001103 case 'i': case i_grave: case i_acute: case i_circumflex:
1104 case i_diaeresis: CASEMBC(0x129) CASEMBC(0x12b)
1105 CASEMBC(0x12d) CASEMBC(0x12f) CASEMBC(0x1d0)
1106 CASEMBC(0x1ec9)
1107 EMIT2('i'); EMIT2(i_grave); EMIT2(i_acute);
1108 EMIT2(i_circumflex); EMIT2(i_diaeresis);
1109 EMITMBC(0x129) EMITMBC(0x12b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001110 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
1111 EMITMBC(0x1ec9)
1112 return OK;
1113
1114 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1115 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
1116 return OK;
1117
1118 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001119 CASEMBC(0x1e35)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001120 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
1121 EMITMBC(0x1e35)
1122 return OK;
1123
1124 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001125 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001126 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1127 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1128 return OK;
1129
1130 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1131 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 return OK;
1133
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001134 case 'n': case n_virguilla: CASEMBC(0x144) CASEMBC(0x146)
1135 CASEMBC(0x148) CASEMBC(0x149) CASEMBC(0x1e45)
1136 CASEMBC(0x1e49)
1137 EMIT2('n'); EMIT2(n_virguilla);
1138 EMITMBC(0x144) EMITMBC(0x146)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001139 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1140 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001141 return OK;
1142
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001143 case 'o': case o_grave: case o_acute: case o_circumflex:
1144 case o_virguilla: case o_diaeresis: case o_slash:
1145 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151)
1146 CASEMBC(0x1a1) CASEMBC(0x1d2) CASEMBC(0x1eb)
1147 CASEMBC(0x1ed) CASEMBC(0x1ecf)
1148 EMIT2('o'); EMIT2(o_grave); EMIT2(o_acute);
1149 EMIT2(o_circumflex); EMIT2(o_virguilla);
1150 EMIT2(o_diaeresis); EMIT2(o_slash);
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001151 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1152 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1153 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1154 return OK;
1155
1156 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1157 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1158 return OK;
1159
1160 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001161 CASEMBC(0x1e59) CASEMBC(0x1e5f)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001162 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1163 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1164 return OK;
1165
1166 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001167 CASEMBC(0x161) CASEMBC(0x1e61)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001168 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1169 EMITMBC(0x161) EMITMBC(0x1e61)
1170 return OK;
1171
1172 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001173 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001174 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1175 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001176 return OK;
1177
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001178 case 'u': case u_grave: case u_acute: case u_circumflex:
1179 case u_diaeresis: CASEMBC(0x169) CASEMBC(0x16b)
1180 CASEMBC(0x16d) CASEMBC(0x16f) CASEMBC(0x171)
1181 CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1182 CASEMBC(0x1ee7)
1183 EMIT2('u'); EMIT2(u_grave); EMIT2(u_acute);
1184 EMIT2(u_circumflex); EMIT2(u_diaeresis);
1185 EMITMBC(0x169) EMITMBC(0x16b)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001186 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1187 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1188 EMITMBC(0x1ee7)
1189 return OK;
1190
1191 case 'v': CASEMBC(0x1e7d)
1192 EMIT2('v'); EMITMBC(0x1e7d)
1193 return OK;
1194
1195 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001196 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001197 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1198 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1199 return OK;
1200
1201 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1202 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001203 return OK;
1204
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001205 case 'y': case y_acute: case y_diaeresis: CASEMBC(0x177)
1206 CASEMBC(0x1e8f) CASEMBC(0x1e99) CASEMBC(0x1ef3)
1207 CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1208 EMIT2('y'); EMIT2(y_acute); EMIT2(y_diaeresis);
1209 EMITMBC(0x177)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001210 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1211 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001212 return OK;
1213
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001214 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
Bram Moolenaar2a6fa562016-04-04 20:55:59 +02001215 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001216 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1217 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1218 return OK;
1219
1220 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 }
1222 }
1223
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001224 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001225 return OK;
1226#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001227#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228}
1229
1230/*
1231 * Code to parse regular expression.
1232 *
1233 * We try to reuse parsing functions in regexp.c to
1234 * minimize surprise and keep the syntax consistent.
1235 */
1236
1237/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001238 * Parse the lowest level.
1239 *
1240 * An atom can be one of a long list of items. Many atoms match one character
1241 * in the text. It is often an ordinary character or a character class.
1242 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1243 * is only for syntax highlighting.
1244 *
1245 * atom ::= ordinary-atom
1246 * or \( pattern \)
1247 * or \%( pattern \)
1248 * or \z( pattern \)
1249 */
1250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001251nfa_regatom(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001252{
1253 int c;
1254 int charclass;
1255 int equiclass;
1256 int collclass;
1257 int got_coll_char;
1258 char_u *p;
1259 char_u *endp;
1260#ifdef FEAT_MBYTE
1261 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262#endif
1263 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001264 int emit_range;
1265 int negated;
1266 int result;
1267 int startc = -1;
1268 int endc = -1;
1269 int oldstartc = -1;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001270 int save_prev_at_start = prev_at_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001271
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 switch (c)
1274 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001275 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001276 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001277
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001278 case Magic('^'):
1279 EMIT(NFA_BOL);
1280 break;
1281
1282 case Magic('$'):
1283 EMIT(NFA_EOL);
1284#if defined(FEAT_SYN_HL) || defined(PROTO)
1285 had_eol = TRUE;
1286#endif
1287 break;
1288
1289 case Magic('<'):
1290 EMIT(NFA_BOW);
1291 break;
1292
1293 case Magic('>'):
1294 EMIT(NFA_EOW);
1295 break;
1296
1297 case Magic('_'):
1298 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001299 if (c == NUL)
1300 EMSG_RET_FAIL(_(e_nul_found));
1301
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001302 if (c == '^') /* "\_^" is start-of-line */
1303 {
1304 EMIT(NFA_BOL);
1305 break;
1306 }
1307 if (c == '$') /* "\_$" is end-of-line */
1308 {
1309 EMIT(NFA_EOL);
1310#if defined(FEAT_SYN_HL) || defined(PROTO)
1311 had_eol = TRUE;
1312#endif
1313 break;
1314 }
1315
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001316 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001317
1318 /* "\_[" is collection plus newline */
1319 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001320 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321
1322 /* "\_x" is character class plus newline */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001323 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001324
1325 /*
1326 * Character classes.
1327 */
1328 case Magic('.'):
1329 case Magic('i'):
1330 case Magic('I'):
1331 case Magic('k'):
1332 case Magic('K'):
1333 case Magic('f'):
1334 case Magic('F'):
1335 case Magic('p'):
1336 case Magic('P'):
1337 case Magic('s'):
1338 case Magic('S'):
1339 case Magic('d'):
1340 case Magic('D'):
1341 case Magic('x'):
1342 case Magic('X'):
1343 case Magic('o'):
1344 case Magic('O'):
1345 case Magic('w'):
1346 case Magic('W'):
1347 case Magic('h'):
1348 case Magic('H'):
1349 case Magic('a'):
1350 case Magic('A'):
1351 case Magic('l'):
1352 case Magic('L'):
1353 case Magic('u'):
1354 case Magic('U'):
1355 p = vim_strchr(classchars, no_Magic(c));
1356 if (p == NULL)
1357 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001358 if (extra == NFA_ADD_NL)
1359 {
1360 EMSGN(_(e_ill_char_class), c);
1361 rc_did_emsg = TRUE;
1362 return FAIL;
1363 }
Bram Moolenaarde330112017-01-08 20:50:52 +01001364 IEMSGN("INTERNAL: Unknown character class char: %ld", c);
Bram Moolenaar5714b802013-05-28 22:03:20 +02001365 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 }
1367#ifdef FEAT_MBYTE
1368 /* When '.' is followed by a composing char ignore the dot, so that
1369 * the composing char is matched here. */
1370 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1371 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001372 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001373 c = getchr();
1374 goto nfa_do_multibyte;
1375 }
1376#endif
1377 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001378 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001379 {
1380 EMIT(NFA_NEWL);
1381 EMIT(NFA_OR);
1382 regflags |= RF_HASNL;
1383 }
1384 break;
1385
1386 case Magic('n'):
1387 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001388 /* In a string "\n" matches a newline character. */
1389 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 else
1391 {
1392 /* In buffer text "\n" matches the end of a line. */
1393 EMIT(NFA_NEWL);
1394 regflags |= RF_HASNL;
1395 }
1396 break;
1397
1398 case Magic('('):
1399 if (nfa_reg(REG_PAREN) == FAIL)
1400 return FAIL; /* cascaded error */
1401 break;
1402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 case Magic('|'):
1404 case Magic('&'):
1405 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001406 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 return FAIL;
1408
1409 case Magic('='):
1410 case Magic('?'):
1411 case Magic('+'):
1412 case Magic('@'):
1413 case Magic('*'):
1414 case Magic('{'):
1415 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001416 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001417 return FAIL;
1418
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001419 case Magic('~'):
1420 {
1421 char_u *lp;
1422
1423 /* Previous substitute pattern.
1424 * Generated as "\%(pattern\)". */
1425 if (reg_prev_sub == NULL)
1426 {
1427 EMSG(_(e_nopresub));
1428 return FAIL;
1429 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001430 for (lp = reg_prev_sub; *lp != NUL; MB_CPTR_ADV(lp))
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001431 {
1432 EMIT(PTR2CHAR(lp));
1433 if (lp != reg_prev_sub)
1434 EMIT(NFA_CONCAT);
1435 }
1436 EMIT(NFA_NOPEN);
1437 break;
1438 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001439
Bram Moolenaar428e9872013-05-30 17:05:39 +02001440 case Magic('1'):
1441 case Magic('2'):
1442 case Magic('3'):
1443 case Magic('4'):
1444 case Magic('5'):
1445 case Magic('6'):
1446 case Magic('7'):
1447 case Magic('8'):
1448 case Magic('9'):
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +02001449 {
1450 int refnum = no_Magic(c) - '1';
1451
1452 if (!seen_endbrace(refnum + 1))
1453 return FAIL;
1454 EMIT(NFA_BACKREF1 + refnum);
1455 nfa_has_backref = TRUE;
1456 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02001457 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001458
1459 case Magic('z'):
1460 c = no_Magic(getchr());
1461 switch (c)
1462 {
1463 case 's':
1464 EMIT(NFA_ZSTART);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001465 if (re_mult_next("\\zs") == FAIL)
1466 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001467 break;
1468 case 'e':
1469 EMIT(NFA_ZEND);
1470 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001471 if (re_mult_next("\\ze") == FAIL)
1472 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001473 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001474#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 case '1':
1476 case '2':
1477 case '3':
1478 case '4':
1479 case '5':
1480 case '6':
1481 case '7':
1482 case '8':
1483 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001484 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001485 if (reg_do_extmatch != REX_USE)
1486 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001487 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1488 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001489 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001490 re_has_z = REX_USE;
1491 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001493 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001494 if (reg_do_extmatch != REX_SET)
1495 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001496 if (nfa_reg(REG_ZPAREN) == FAIL)
1497 return FAIL; /* cascaded error */
1498 re_has_z = REX_SET;
1499 break;
1500#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001501 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001502 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001503 no_Magic(c));
1504 return FAIL;
1505 }
1506 break;
1507
1508 case Magic('%'):
1509 c = no_Magic(getchr());
1510 switch (c)
1511 {
1512 /* () without a back reference */
1513 case '(':
1514 if (nfa_reg(REG_NPAREN) == FAIL)
1515 return FAIL;
1516 EMIT(NFA_NOPEN);
1517 break;
1518
1519 case 'd': /* %d123 decimal */
1520 case 'o': /* %o123 octal */
1521 case 'x': /* %xab hex 2 */
1522 case 'u': /* %uabcd hex 4 */
1523 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001524 {
Bram Moolenaar4c22a912017-11-02 22:29:38 +01001525 long nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526
Bram Moolenaar47196582013-05-25 22:04:23 +02001527 switch (c)
1528 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001529 case 'd': nr = getdecchrs(); break;
1530 case 'o': nr = getoctchrs(); break;
1531 case 'x': nr = gethexchrs(2); break;
1532 case 'u': nr = gethexchrs(4); break;
1533 case 'U': nr = gethexchrs(8); break;
1534 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001535 }
1536
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001537 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001538 EMSG2_RET_FAIL(
1539 _("E678: Invalid character after %s%%[dxouU]"),
1540 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001541 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001542 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001543 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001544 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 break;
1546
1547 /* Catch \%^ and \%$ regardless of where they appear in the
1548 * pattern -- regardless of whether or not it makes sense. */
1549 case '^':
1550 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 break;
1552
1553 case '$':
1554 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 break;
1556
1557 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001558 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 break;
1560
1561 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001562 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 break;
1564
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001565 case 'C':
1566 EMIT(NFA_ANY_COMPOSING);
1567 break;
1568
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001569 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001570 {
1571 int n;
1572
1573 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001574 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001575 {
1576 if (c == NUL)
1577 EMSG2_RET_FAIL(_(e_missing_sb),
1578 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001579 /* recursive call! */
1580 if (nfa_regatom() == FAIL)
1581 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001582 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001583 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001584 if (n == 0)
1585 EMSG2_RET_FAIL(_(e_empty_sb),
1586 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001587 EMIT(NFA_OPT_CHARS);
1588 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001589
1590 /* Emit as "\%(\%[abc]\)" to be able to handle
1591 * "\%[abc]*" which would cause the empty string to be
1592 * matched an unlimited number of times. NFA_NOPEN is
1593 * added only once at a position, while NFA_SPLIT is
1594 * added multiple times. This is more efficient than
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01001595 * not allowing NFA_SPLIT multiple times, it is used
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001596 * a lot. */
1597 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001598 break;
1599 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001601 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001602 {
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001603 long n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001604 int cmp = c;
1605
1606 if (c == '<' || c == '>')
1607 c = getchr();
1608 while (VIM_ISDIGIT(c))
1609 {
1610 n = n * 10 + (c - '0');
1611 c = getchr();
1612 }
1613 if (c == 'l' || c == 'c' || c == 'v')
1614 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001615 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001616 {
Bram Moolenaar044aa292013-06-04 21:27:38 +02001617 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001618 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001619 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001620 if (save_prev_at_start)
1621 at_start = TRUE;
1622 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001623 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001624 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001625 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001626 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001627 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001628 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001629 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001630 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01001631#if VIM_SIZEOF_INT < VIM_SIZEOF_LONG
1632 if (n > INT_MAX)
1633 {
1634 EMSG(_("E951: \\% value too large"));
1635 return FAIL;
1636 }
1637#endif
1638 EMIT((int)n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001639 break;
1640 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001641 else if (c == '\'' && n == 0)
1642 {
1643 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001644 EMIT(cmp == '<' ? NFA_MARK_LT :
1645 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001646 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001647 break;
1648 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001649 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001650 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1651 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001652 return FAIL;
1653 }
1654 break;
1655
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001656 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001657collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001659 * [abc] uses NFA_START_COLL - NFA_END_COLL
1660 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1661 * Each character is produced as a regular state, using
1662 * NFA_CONCAT to bind them together.
1663 * Besides normal characters there can be:
1664 * - character classes NFA_CLASS_*
1665 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 */
1667
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 p = regparse;
1669 endp = skip_anyof(p);
1670 if (*endp == ']')
1671 {
1672 /*
1673 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001674 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001675 * and perform the necessary substitutions in the NFA.
1676 */
1677 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001678 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001679 if (result != FAIL)
1680 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001681 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001683 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 EMIT(NFA_NEWL);
1685 EMIT(NFA_OR);
1686 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001687 else
1688 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001689 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001690 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691 return OK;
1692 }
1693 /*
1694 * Failed to recognize a character class. Use the simple
1695 * version that turns [abc] into 'a' OR 'b' OR 'c'
1696 */
1697 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001698 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001699 if (*regparse == '^') /* negated range */
1700 {
1701 negated = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001702 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001703 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001704 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001705 else
1706 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001707 if (*regparse == '-')
1708 {
1709 startc = '-';
1710 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001711 EMIT(NFA_CONCAT);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001712 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 }
1714 /* Emit the OR branches for each character in the [] */
1715 emit_range = FALSE;
1716 while (regparse < endp)
1717 {
1718 oldstartc = startc;
1719 startc = -1;
1720 got_coll_char = FALSE;
1721 if (*regparse == '[')
1722 {
1723 /* Check for [: :], [= =], [. .] */
1724 equiclass = collclass = 0;
1725 charclass = get_char_class(&regparse);
1726 if (charclass == CLASS_NONE)
1727 {
1728 equiclass = get_equi_class(&regparse);
1729 if (equiclass == 0)
1730 collclass = get_coll_element(&regparse);
1731 }
1732
1733 /* Character class like [:alpha:] */
1734 if (charclass != CLASS_NONE)
1735 {
1736 switch (charclass)
1737 {
1738 case CLASS_ALNUM:
1739 EMIT(NFA_CLASS_ALNUM);
1740 break;
1741 case CLASS_ALPHA:
1742 EMIT(NFA_CLASS_ALPHA);
1743 break;
1744 case CLASS_BLANK:
1745 EMIT(NFA_CLASS_BLANK);
1746 break;
1747 case CLASS_CNTRL:
1748 EMIT(NFA_CLASS_CNTRL);
1749 break;
1750 case CLASS_DIGIT:
1751 EMIT(NFA_CLASS_DIGIT);
1752 break;
1753 case CLASS_GRAPH:
1754 EMIT(NFA_CLASS_GRAPH);
1755 break;
1756 case CLASS_LOWER:
1757 EMIT(NFA_CLASS_LOWER);
1758 break;
1759 case CLASS_PRINT:
1760 EMIT(NFA_CLASS_PRINT);
1761 break;
1762 case CLASS_PUNCT:
1763 EMIT(NFA_CLASS_PUNCT);
1764 break;
1765 case CLASS_SPACE:
1766 EMIT(NFA_CLASS_SPACE);
1767 break;
1768 case CLASS_UPPER:
1769 EMIT(NFA_CLASS_UPPER);
1770 break;
1771 case CLASS_XDIGIT:
1772 EMIT(NFA_CLASS_XDIGIT);
1773 break;
1774 case CLASS_TAB:
1775 EMIT(NFA_CLASS_TAB);
1776 break;
1777 case CLASS_RETURN:
1778 EMIT(NFA_CLASS_RETURN);
1779 break;
1780 case CLASS_BACKSPACE:
1781 EMIT(NFA_CLASS_BACKSPACE);
1782 break;
1783 case CLASS_ESCAPE:
1784 EMIT(NFA_CLASS_ESCAPE);
1785 break;
1786 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001787 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 continue;
1789 }
1790 /* Try equivalence class [=a=] and the like */
1791 if (equiclass != 0)
1792 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001793 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 if (result == FAIL)
1795 {
1796 /* should never happen */
1797 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1798 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 continue;
1800 }
1801 /* Try collating class like [. .] */
1802 if (collclass != 0)
1803 {
1804 startc = collclass; /* allow [.a.]-x as a range */
1805 /* Will emit the proper atom at the end of the
1806 * while loop. */
1807 }
1808 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001809 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1810 * start character. */
1811 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812 {
1813 emit_range = TRUE;
1814 startc = oldstartc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001815 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001816 continue; /* reading the end of the range */
1817 }
1818
1819 /* Now handle simple and escaped characters.
1820 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1821 * accepts "\t", "\e", etc., but only when the 'l' flag in
1822 * 'cpoptions' is not included.
1823 * Posix doesn't recognize backslash at all.
1824 */
1825 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001826 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 && regparse + 1 <= endp
1828 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001829 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830 && vim_strchr(REGEXP_ABBR, regparse[1])
1831 != NULL)
1832 )
1833 )
1834 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001835 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001836
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001837 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001838 startc = reg_string ? NL : NFA_NEWL;
1839 else
1840 if (*regparse == 'd'
1841 || *regparse == 'o'
1842 || *regparse == 'x'
1843 || *regparse == 'u'
1844 || *regparse == 'U'
1845 )
1846 {
1847 /* TODO(RE) This needs more testing */
1848 startc = coll_get_char();
1849 got_coll_char = TRUE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001850 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001851 }
1852 else
1853 {
1854 /* \r,\t,\e,\b */
1855 startc = backslash_trans(*regparse);
1856 }
1857 }
1858
1859 /* Normal printable char */
1860 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001861 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862
1863 /* Previous char was '-', so this char is end of range. */
1864 if (emit_range)
1865 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001866 endc = startc;
1867 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02001869 EMSG_RET_FAIL(_(e_reverse_range));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001870
1871 if (endc > startc + 2)
1872 {
1873 /* Emit a range instead of the sequence of
1874 * individual characters. */
1875 if (startc == 0)
1876 /* \x00 is translated to \x0a, start at \x01. */
1877 EMIT(1);
1878 else
1879 --post_ptr; /* remove NFA_CONCAT */
1880 EMIT(endc);
1881 EMIT(NFA_RANGE);
1882 EMIT(NFA_CONCAT);
1883 }
1884 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001886 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887 || (*mb_char2len)(endc) > 1))
1888 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001889 /* Emit the characters in the range.
1890 * "startc" was already emitted, so skip it.
1891 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001892 for (c = startc + 1; c <= endc; c++)
1893 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001894 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001895 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001897 }
1898 else
1899#endif
1900 {
1901#ifdef EBCDIC
1902 int alpha_only = FALSE;
1903
1904 /* for alphabetical range skip the gaps
1905 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1906 if (isalpha(startc) && isalpha(endc))
1907 alpha_only = TRUE;
1908#endif
1909 /* Emit the range. "startc" was already emitted, so
1910 * skip it. */
1911 for (c = startc + 1; c <= endc; c++)
1912#ifdef EBCDIC
1913 if (!alpha_only || isalpha(startc))
1914#endif
1915 {
1916 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001917 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001918 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001920 emit_range = FALSE;
1921 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001922 }
1923 else
1924 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001925 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001927 * Normally, simply emit startc. But if we get char
1928 * code=0 from a collating char, then replace it with
1929 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001931 * the backtracking engine. */
1932 if (startc == NFA_NEWL)
1933 {
1934 /* Line break can't be matched as part of the
1935 * collection, add an OR below. But not for negated
1936 * range. */
1937 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001938 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001939 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001941 {
1942 if (got_coll_char == TRUE && startc == 0)
1943 EMIT(0x0a);
1944 else
1945 EMIT(startc);
1946 EMIT(NFA_CONCAT);
1947 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 }
1949
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001950 MB_PTR_ADV(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001951 } /* while (p < endp) */
1952
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001953 MB_PTR_BACK(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954 if (*regparse == '-') /* if last, '-' is just a char */
1955 {
1956 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001957 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001959
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001960 /* skip the trailing ] */
1961 regparse = endp;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001962 MB_PTR_ADV(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001963
1964 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001965 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001966 EMIT(NFA_END_NEG_COLL);
1967 else
1968 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001969
1970 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001971 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001972 {
1973 EMIT(reg_string ? NL : NFA_NEWL);
1974 EMIT(NFA_OR);
1975 }
1976
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001977 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001978 } /* if exists closing ] */
1979
1980 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001982 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001983
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001984 default:
1985 {
1986#ifdef FEAT_MBYTE
1987 int plen;
1988
1989nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001990 /* plen is length of current char with composing chars */
1991 if (enc_utf8 && ((*mb_char2len)(c)
Bram Moolenaarace95982017-03-29 17:30:27 +02001992 != (plen = utfc_ptr2len(old_regparse))
Bram Moolenaar47196582013-05-25 22:04:23 +02001993 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001994 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001995 int i = 0;
1996
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001997 /* A base character plus composing characters, or just one
1998 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001999 * This requires creating a separate atom as if enclosing
2000 * the characters in (), where NFA_COMPOSING is the ( and
2001 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 * building the postfix form, not the NFA itself;
2003 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002004 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002005 for (;;)
2006 {
2007 EMIT(c);
2008 if (i > 0)
2009 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02002010 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02002011 break;
2012 c = utf_ptr2char(old_regparse + i);
2013 }
2014 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002015 regparse = old_regparse + plen;
2016 }
2017 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002018#endif
2019 {
2020 c = no_Magic(c);
2021 EMIT(c);
2022 }
2023 return OK;
2024 }
2025 }
2026
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002027 return OK;
2028}
2029
2030/*
2031 * Parse something followed by possible [*+=].
2032 *
2033 * A piece is an atom, possibly followed by a multi, an indication of how many
2034 * times the atom can be matched. Example: "a*" matches any sequence of "a"
2035 * characters: "", "a", "aa", etc.
2036 *
2037 * piece ::= atom
2038 * or atom multi
2039 */
2040 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002041nfa_regpiece(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042{
2043 int i;
2044 int op;
2045 int ret;
2046 long minval, maxval;
2047 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002048 parse_state_T old_state;
2049 parse_state_T new_state;
Bram Moolenaar4c22a912017-11-02 22:29:38 +01002050 long c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002051 int old_post_pos;
2052 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002053 int quest;
2054
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002055 /* Save the current parse state, so that we can use it if <atom>{m,n} is
2056 * next. */
2057 save_parse_state(&old_state);
2058
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002060 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002061
2062 ret = nfa_regatom();
2063 if (ret == FAIL)
2064 return FAIL; /* cascaded error */
2065
2066 op = peekchr();
2067 if (re_multi_type(op) == NOT_MULTI)
2068 return OK;
2069
2070 skipchr();
2071 switch (op)
2072 {
2073 case Magic('*'):
2074 EMIT(NFA_STAR);
2075 break;
2076
2077 case Magic('+'):
2078 /*
2079 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
2080 * first and only submatch would be "aaa". But the backtracking
2081 * engine interprets the plus as "try matching one more time", and
2082 * a* matches a second time at the end of the input, the empty
2083 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002084 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002085 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002086 * In order to be consistent with the old engine, we replace
2087 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002088 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002089 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090 curchr = -1;
2091 if (nfa_regatom() == FAIL)
2092 return FAIL;
2093 EMIT(NFA_STAR);
2094 EMIT(NFA_CONCAT);
2095 skipchr(); /* skip the \+ */
2096 break;
2097
2098 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02002099 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002100 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02002101 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002102 switch(op)
2103 {
2104 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002105 /* \@= */
2106 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002107 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002108 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002109 /* \@! */
2110 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02002111 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002112 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02002113 op = no_Magic(getchr());
2114 if (op == '=')
2115 /* \@<= */
2116 i = NFA_PREV_ATOM_JUST_BEFORE;
2117 else if (op == '!')
2118 /* \@<! */
2119 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
2120 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002121 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02002122 /* \@> */
2123 i = NFA_PREV_ATOM_LIKE_PATTERN;
2124 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002125 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02002126 if (i == 0)
2127 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02002128 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
2129 return FAIL;
2130 }
2131 EMIT(i);
2132 if (i == NFA_PREV_ATOM_JUST_BEFORE
2133 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
2134 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002135 break;
2136
2137 case Magic('?'):
2138 case Magic('='):
2139 EMIT(NFA_QUEST);
2140 break;
2141
2142 case Magic('{'):
2143 /* a{2,5} will expand to 'aaa?a?a?'
2144 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
2145 * version of '?'
2146 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
2147 * parenthesis have the same id
2148 */
2149
2150 greedy = TRUE;
2151 c2 = peekchr();
2152 if (c2 == '-' || c2 == Magic('-'))
2153 {
2154 skipchr();
2155 greedy = FALSE;
2156 }
2157 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002158 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002159
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002160 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2161 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002162 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002163 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002164 if (greedy) /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002165 /* \{}, \{0,} */
2166 EMIT(NFA_STAR);
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002167 else /* { { (match the braces) */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002168 /* \{-}, \{-0,} */
2169 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 break;
2171 }
2172
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002173 /* Special case: x{0} or x{-0} */
2174 if (maxval == 0)
2175 {
2176 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002177 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002178 /* NFA_EMPTY is 0-length and works everywhere */
2179 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002180 return OK;
2181 }
2182
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002183 /* The engine is very inefficient (uses too many states) when the
Bram Moolenaara1d2c582015-02-10 18:18:17 +01002184 * maximum is much larger than the minimum and when the maximum is
2185 * large. Bail out if we can use the other engine. */
2186 if ((nfa_re_flags & RE_AUTO)
Bram Moolenaarf446b482017-01-10 13:55:14 +01002187 && (maxval > 500 || maxval > minval + 200))
Bram Moolenaare0ad3652015-01-27 12:59:55 +01002188 return FAIL;
2189
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002190 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002191 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002192 /* Save parse state after the repeated atom and the \{} */
2193 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002195 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2196 for (i = 0; i < maxval; i++)
2197 {
2198 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002199 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002200 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002201 if (nfa_regatom() == FAIL)
2202 return FAIL;
2203 /* after "minval" times, atoms are optional */
2204 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002205 {
2206 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002207 {
2208 if (greedy)
2209 EMIT(NFA_STAR);
2210 else
2211 EMIT(NFA_STAR_NONGREEDY);
2212 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002213 else
2214 EMIT(quest);
2215 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002216 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002217 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002218 if (i + 1 > minval && maxval == MAX_LIMIT)
2219 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 }
2221
2222 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002223 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 curchr = -1;
2225
2226 break;
2227
2228
2229 default:
2230 break;
2231 } /* end switch */
2232
2233 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002234 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002236
2237 return OK;
2238}
2239
2240/*
2241 * Parse one or more pieces, concatenated. It matches a match for the
2242 * first piece, followed by a match for the second piece, etc. Example:
2243 * "f[0-9]b", first matches "f", then a digit and then "b".
2244 *
2245 * concat ::= piece
2246 * or piece piece
2247 * or piece piece piece
2248 * etc.
2249 */
2250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002251nfa_regconcat(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252{
2253 int cont = TRUE;
2254 int first = TRUE;
2255
2256 while (cont)
2257 {
2258 switch (peekchr())
2259 {
2260 case NUL:
2261 case Magic('|'):
2262 case Magic('&'):
2263 case Magic(')'):
2264 cont = FALSE;
2265 break;
2266
2267 case Magic('Z'):
2268#ifdef FEAT_MBYTE
2269 regflags |= RF_ICOMBINE;
2270#endif
2271 skipchr_keepstart();
2272 break;
2273 case Magic('c'):
2274 regflags |= RF_ICASE;
2275 skipchr_keepstart();
2276 break;
2277 case Magic('C'):
2278 regflags |= RF_NOICASE;
2279 skipchr_keepstart();
2280 break;
2281 case Magic('v'):
2282 reg_magic = MAGIC_ALL;
2283 skipchr_keepstart();
2284 curchr = -1;
2285 break;
2286 case Magic('m'):
2287 reg_magic = MAGIC_ON;
2288 skipchr_keepstart();
2289 curchr = -1;
2290 break;
2291 case Magic('M'):
2292 reg_magic = MAGIC_OFF;
2293 skipchr_keepstart();
2294 curchr = -1;
2295 break;
2296 case Magic('V'):
2297 reg_magic = MAGIC_NONE;
2298 skipchr_keepstart();
2299 curchr = -1;
2300 break;
2301
2302 default:
2303 if (nfa_regpiece() == FAIL)
2304 return FAIL;
2305 if (first == FALSE)
2306 EMIT(NFA_CONCAT);
2307 else
2308 first = FALSE;
2309 break;
2310 }
2311 }
2312
2313 return OK;
2314}
2315
2316/*
2317 * Parse a branch, one or more concats, separated by "\&". It matches the
2318 * last concat, but only if all the preceding concats also match at the same
2319 * position. Examples:
2320 * "foobeep\&..." matches "foo" in "foobeep".
2321 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2322 *
2323 * branch ::= concat
2324 * or concat \& concat
2325 * or concat \& concat \& concat
2326 * etc.
2327 */
2328 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002329nfa_regbranch(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330{
Bram Moolenaar16299b52013-05-30 18:45:23 +02002331 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002332
Bram Moolenaar16299b52013-05-30 18:45:23 +02002333 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334
2335 /* First branch, possibly the only one */
2336 if (nfa_regconcat() == FAIL)
2337 return FAIL;
2338
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 /* Try next concats */
Bram Moolenaar890dd052017-12-16 19:59:37 +01002340 while (peekchr() == Magic('&'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002341 {
2342 skipchr();
Bram Moolenaar890dd052017-12-16 19:59:37 +01002343 /* if concat is empty do emit a node */
2344 if (old_post_pos == (int)(post_ptr - post_start))
2345 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002346 EMIT(NFA_NOPEN);
2347 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002348 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002349 if (nfa_regconcat() == FAIL)
2350 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002351 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002352 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002353 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 }
2356
Bram Moolenaar699c1202013-09-25 16:41:54 +02002357 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002358 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002359 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002360
2361 return OK;
2362}
2363
2364/*
2365 * Parse a pattern, one or more branches, separated by "\|". It matches
2366 * anything that matches one of the branches. Example: "foo\|beep" matches
2367 * "foo" and matches "beep". If more than one branch matches, the first one
2368 * is used.
2369 *
2370 * pattern ::= branch
2371 * or branch \| branch
2372 * or branch \| branch \| branch
2373 * etc.
2374 */
2375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002376nfa_reg(
2377 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002378{
2379 int parno = 0;
2380
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 if (paren == REG_PAREN)
2382 {
2383 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002384 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002385 parno = regnpar++;
2386 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002387#ifdef FEAT_SYN_HL
2388 else if (paren == REG_ZPAREN)
2389 {
2390 /* Make a ZOPEN node. */
2391 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002392 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002393 parno = regnzpar++;
2394 }
2395#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002396
2397 if (nfa_regbranch() == FAIL)
2398 return FAIL; /* cascaded error */
2399
2400 while (peekchr() == Magic('|'))
2401 {
2402 skipchr();
2403 if (nfa_regbranch() == FAIL)
2404 return FAIL; /* cascaded error */
2405 EMIT(NFA_OR);
2406 }
2407
2408 /* Check for proper termination. */
2409 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2410 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002411 if (paren == REG_NPAREN)
2412 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2413 else
2414 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2415 }
2416 else if (paren == REG_NOPAREN && peekchr() != NUL)
2417 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002418 if (peekchr() == Magic(')'))
2419 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2420 else
2421 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2422 }
2423 /*
2424 * Here we set the flag allowing back references to this set of
2425 * parentheses.
2426 */
2427 if (paren == REG_PAREN)
2428 {
2429 had_endbrace[parno] = TRUE; /* have seen the close paren */
2430 EMIT(NFA_MOPEN + parno);
2431 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002432#ifdef FEAT_SYN_HL
2433 else if (paren == REG_ZPAREN)
2434 EMIT(NFA_ZOPEN + parno);
2435#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436
2437 return OK;
2438}
2439
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002440#ifdef DEBUG
2441static char_u code[50];
2442
2443 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002444nfa_set_code(int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002445{
2446 int addnl = FALSE;
2447
2448 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2449 {
2450 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002451 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002452 }
2453
2454 STRCPY(code, "");
2455 switch (c)
2456 {
2457 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2458 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2459 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2460 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2461 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2462 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2463
Bram Moolenaar5714b802013-05-28 22:03:20 +02002464 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2465 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2466 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2467 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2468 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2469 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2470 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2471 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2472 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002473#ifdef FEAT_SYN_HL
2474 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2475 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2476 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2477 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2478 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2479 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2480 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2481 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2482 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2483#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002484 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2485
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002486 case NFA_PREV_ATOM_NO_WIDTH:
2487 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002488 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2489 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002490 case NFA_PREV_ATOM_JUST_BEFORE:
2491 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2492 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2493 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002494 case NFA_PREV_ATOM_LIKE_PATTERN:
2495 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2496
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002497 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2498 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002499 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002500 case NFA_START_INVISIBLE_FIRST:
2501 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002502 case NFA_START_INVISIBLE_NEG:
2503 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002504 case NFA_START_INVISIBLE_NEG_FIRST:
2505 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002506 case NFA_START_INVISIBLE_BEFORE:
2507 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002508 case NFA_START_INVISIBLE_BEFORE_FIRST:
2509 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002510 case NFA_START_INVISIBLE_BEFORE_NEG:
2511 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002512 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2513 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002514 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002515 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002516 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002517 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002518
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2520 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002521 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002523 case NFA_MOPEN:
2524 case NFA_MOPEN1:
2525 case NFA_MOPEN2:
2526 case NFA_MOPEN3:
2527 case NFA_MOPEN4:
2528 case NFA_MOPEN5:
2529 case NFA_MOPEN6:
2530 case NFA_MOPEN7:
2531 case NFA_MOPEN8:
2532 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002533 STRCPY(code, "NFA_MOPEN(x)");
2534 code[10] = c - NFA_MOPEN + '0';
2535 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002536 case NFA_MCLOSE:
2537 case NFA_MCLOSE1:
2538 case NFA_MCLOSE2:
2539 case NFA_MCLOSE3:
2540 case NFA_MCLOSE4:
2541 case NFA_MCLOSE5:
2542 case NFA_MCLOSE6:
2543 case NFA_MCLOSE7:
2544 case NFA_MCLOSE8:
2545 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002546 STRCPY(code, "NFA_MCLOSE(x)");
2547 code[11] = c - NFA_MCLOSE + '0';
2548 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002549#ifdef FEAT_SYN_HL
2550 case NFA_ZOPEN:
2551 case NFA_ZOPEN1:
2552 case NFA_ZOPEN2:
2553 case NFA_ZOPEN3:
2554 case NFA_ZOPEN4:
2555 case NFA_ZOPEN5:
2556 case NFA_ZOPEN6:
2557 case NFA_ZOPEN7:
2558 case NFA_ZOPEN8:
2559 case NFA_ZOPEN9:
2560 STRCPY(code, "NFA_ZOPEN(x)");
2561 code[10] = c - NFA_ZOPEN + '0';
2562 break;
2563 case NFA_ZCLOSE:
2564 case NFA_ZCLOSE1:
2565 case NFA_ZCLOSE2:
2566 case NFA_ZCLOSE3:
2567 case NFA_ZCLOSE4:
2568 case NFA_ZCLOSE5:
2569 case NFA_ZCLOSE6:
2570 case NFA_ZCLOSE7:
2571 case NFA_ZCLOSE8:
2572 case NFA_ZCLOSE9:
2573 STRCPY(code, "NFA_ZCLOSE(x)");
2574 code[11] = c - NFA_ZCLOSE + '0';
2575 break;
2576#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002577 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2578 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2579 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2580 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002581 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2582 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002583 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2584 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2585 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2586 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2587 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2588 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2589 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2590 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2591 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2592 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2593 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2594 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2595 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2596 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002597 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002598
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002600 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2601 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2602 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002603 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002604 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002605
2606 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2607 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2608 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2609 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2610 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2611 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2612 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2613
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002614 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2615 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2616 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2617 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2618 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2619 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2620 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2621 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2622 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2623 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2624 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2625 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2626 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2627 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2628 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2629 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2630
2631 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2632 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2633 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2634 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2635 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2636 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2637 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2638 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2639 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2640 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2641 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2642 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2643 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2644 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2645 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2646 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2647 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2648 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2649 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2650 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2651 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2652 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2653 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2654 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2655 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2656 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2657 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002658 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2659 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2660 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2661 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002662
2663 default:
2664 STRCPY(code, "CHAR(x)");
2665 code[5] = c;
2666 }
2667
2668 if (addnl == TRUE)
2669 STRCAT(code, " + NEWLINE ");
2670
2671}
2672
2673#ifdef ENABLE_LOG
2674static FILE *log_fd;
2675
2676/*
2677 * Print the postfix notation of the current regexp.
2678 */
2679 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002680nfa_postfix_dump(char_u *expr, int retval)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002681{
2682 int *p;
2683 FILE *f;
2684
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002685 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002686 if (f != NULL)
2687 {
2688 fprintf(f, "\n-------------------------\n");
2689 if (retval == FAIL)
2690 fprintf(f, ">>> NFA engine failed ... \n");
2691 else if (retval == OK)
2692 fprintf(f, ">>> NFA engine succeeded !\n");
2693 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002694 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002695 {
2696 nfa_set_code(*p);
2697 fprintf(f, "%s, ", code);
2698 }
2699 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002700 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002701 fprintf(f, "%d ", *p);
2702 fprintf(f, "\n\n");
2703 fclose(f);
2704 }
2705}
2706
2707/*
2708 * Print the NFA starting with a root node "state".
2709 */
2710 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002711nfa_print_state(FILE *debugf, nfa_state_T *state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002712{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002713 garray_T indent;
2714
2715 ga_init2(&indent, 1, 64);
2716 ga_append(&indent, '\0');
2717 nfa_print_state2(debugf, state, &indent);
2718 ga_clear(&indent);
2719}
2720
2721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002722nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent)
Bram Moolenaar152e7892013-05-25 12:28:11 +02002723{
2724 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725
2726 if (state == NULL)
2727 return;
2728
2729 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002730
2731 /* Output indent */
2732 p = (char_u *)indent->ga_data;
2733 if (indent->ga_len >= 3)
2734 {
2735 int last = indent->ga_len - 3;
2736 char_u save[2];
2737
2738 STRNCPY(save, &p[last], 2);
2739 STRNCPY(&p[last], "+-", 2);
2740 fprintf(debugf, " %s", p);
2741 STRNCPY(&p[last], save, 2);
2742 }
2743 else
2744 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002745
2746 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002747 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002748 code,
2749 state->c,
2750 abs(state->id),
2751 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752 if (state->id < 0)
2753 return;
2754
2755 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002756
2757 /* grow indent for state->out */
2758 indent->ga_len -= 1;
2759 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002760 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002761 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002762 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002763 ga_append(indent, '\0');
2764
2765 nfa_print_state2(debugf, state->out, indent);
2766
2767 /* replace last part of indent for state->out1 */
2768 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002769 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002770 ga_append(indent, '\0');
2771
2772 nfa_print_state2(debugf, state->out1, indent);
2773
2774 /* shrink indent */
2775 indent->ga_len -= 3;
2776 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002777}
2778
2779/*
2780 * Print the NFA state machine.
2781 */
2782 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002783nfa_dump(nfa_regprog_T *prog)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002784{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002785 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002786
2787 if (debugf != NULL)
2788 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002789 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002790
Bram Moolenaar473de612013-06-08 18:19:48 +02002791 if (prog->reganch)
2792 fprintf(debugf, "reganch: %d\n", prog->reganch);
2793 if (prog->regstart != NUL)
2794 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2795 prog->regstart, prog->regstart);
2796 if (prog->match_text != NULL)
2797 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002798
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002799 fclose(debugf);
2800 }
2801}
2802#endif /* ENABLE_LOG */
2803#endif /* DEBUG */
2804
2805/*
2806 * Parse r.e. @expr and convert it into postfix form.
2807 * Return the postfix string on success, NULL otherwise.
2808 */
2809 static int *
Bram Moolenaar05540972016-01-30 20:31:25 +01002810re2post(void)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811{
2812 if (nfa_reg(REG_NOPAREN) == FAIL)
2813 return NULL;
2814 EMIT(NFA_MOPEN);
2815 return post_start;
2816}
2817
2818/* NB. Some of the code below is inspired by Russ's. */
2819
2820/*
2821 * Represents an NFA state plus zero or one or two arrows exiting.
2822 * if c == MATCH, no arrows out; matching state.
2823 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2824 * If c < 256, labeled arrow with character c to out.
2825 */
2826
2827static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2828
2829/*
2830 * Allocate and initialize nfa_state_T.
2831 */
2832 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002833alloc_state(int c, nfa_state_T *out, nfa_state_T *out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834{
2835 nfa_state_T *s;
2836
2837 if (istate >= nstate)
2838 return NULL;
2839
2840 s = &state_ptr[istate++];
2841
2842 s->c = c;
2843 s->out = out;
2844 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002845 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002846
2847 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002848 s->lastlist[0] = 0;
2849 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002850
2851 return s;
2852}
2853
2854/*
2855 * A partially built NFA without the matching state filled in.
2856 * Frag_T.start points at the start state.
2857 * Frag_T.out is a list of places that need to be set to the
2858 * next state for this fragment.
2859 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002860
2861/* Since the out pointers in the list are always
2862 * uninitialized, we use the pointers themselves
2863 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002864typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002865union Ptrlist
2866{
2867 Ptrlist *next;
2868 nfa_state_T *s;
2869};
2870
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871struct Frag
2872{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002873 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002874 Ptrlist *out;
2875};
2876typedef struct Frag Frag_T;
2877
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002878static Frag_T frag(nfa_state_T *start, Ptrlist *out);
2879static Ptrlist *list1(nfa_state_T **outp);
2880static void patch(Ptrlist *l, nfa_state_T *s);
2881static Ptrlist *append(Ptrlist *l1, Ptrlist *l2);
2882static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end);
2883static Frag_T st_pop(Frag_T **p, Frag_T *stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884
2885/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002886 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002887 */
2888 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01002889frag(nfa_state_T *start, Ptrlist *out)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002890{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002891 Frag_T n;
2892
2893 n.start = start;
2894 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 return n;
2896}
2897
2898/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002899 * Create singleton list containing just outp.
2900 */
2901 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002902list1(
2903 nfa_state_T **outp)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002904{
2905 Ptrlist *l;
2906
2907 l = (Ptrlist *)outp;
2908 l->next = NULL;
2909 return l;
2910}
2911
2912/*
2913 * Patch the list of states at out to point to start.
2914 */
2915 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002916patch(Ptrlist *l, nfa_state_T *s)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002917{
2918 Ptrlist *next;
2919
2920 for (; l; l = next)
2921 {
2922 next = l->next;
2923 l->s = s;
2924 }
2925}
2926
2927
2928/*
2929 * Join the two lists l1 and l2, returning the combination.
2930 */
2931 static Ptrlist *
Bram Moolenaar05540972016-01-30 20:31:25 +01002932append(Ptrlist *l1, Ptrlist *l2)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002933{
2934 Ptrlist *oldl1;
2935
2936 oldl1 = l1;
2937 while (l1->next)
2938 l1 = l1->next;
2939 l1->next = l2;
2940 return oldl1;
2941}
2942
2943/*
2944 * Stack used for transforming postfix form into NFA.
2945 */
2946static Frag_T empty;
2947
2948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002949st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002951#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002952 FILE *df;
2953 int *p2;
2954
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002955 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956 if (df)
2957 {
2958 fprintf(df, "Error popping the stack!\n");
2959#ifdef DEBUG
2960 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2961#endif
2962 fprintf(df, "Postfix form is: ");
2963#ifdef DEBUG
2964 for (p2 = postfix; p2 < end; p2++)
2965 {
2966 nfa_set_code(*p2);
2967 fprintf(df, "%s, ", code);
2968 }
2969 nfa_set_code(*p);
2970 fprintf(df, "\nCurrent position is: ");
2971 for (p2 = postfix; p2 <= p; p2 ++)
2972 {
2973 nfa_set_code(*p2);
2974 fprintf(df, "%s, ", code);
2975 }
2976#else
2977 for (p2 = postfix; p2 < end; p2++)
2978 {
2979 fprintf(df, "%d, ", *p2);
2980 }
2981 fprintf(df, "\nCurrent position is: ");
2982 for (p2 = postfix; p2 <= p; p2 ++)
2983 {
2984 fprintf(df, "%d, ", *p2);
2985 }
2986#endif
2987 fprintf(df, "\n--------------------------\n");
2988 fclose(df);
2989 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002990#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002991 EMSG(_("E874: (NFA) Could not pop the stack !"));
2992}
2993
2994/*
2995 * Push an item onto the stack.
2996 */
2997 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002998st_push(Frag_T s, Frag_T **p, Frag_T *stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002999{
3000 Frag_T *stackp = *p;
3001
3002 if (stackp >= stack_end)
3003 return;
3004 *stackp = s;
3005 *p = *p + 1;
3006}
3007
3008/*
3009 * Pop an item from the stack.
3010 */
3011 static Frag_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003012st_pop(Frag_T **p, Frag_T *stack)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003013{
3014 Frag_T *stackp;
3015
3016 *p = *p - 1;
3017 stackp = *p;
3018 if (stackp < stack)
3019 return empty;
3020 return **p;
3021}
3022
3023/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003024 * Estimate the maximum byte length of anything matching "state".
3025 * When unknown or unlimited return -1.
3026 */
3027 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003028nfa_max_width(nfa_state_T *startstate, int depth)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003029{
3030 int l, r;
3031 nfa_state_T *state = startstate;
3032 int len = 0;
3033
3034 /* detect looping in a NFA_SPLIT */
3035 if (depth > 4)
3036 return -1;
3037
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003038 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003039 {
3040 switch (state->c)
3041 {
3042 case NFA_END_INVISIBLE:
3043 case NFA_END_INVISIBLE_NEG:
3044 /* the end, return what we have */
3045 return len;
3046
3047 case NFA_SPLIT:
3048 /* two alternatives, use the maximum */
3049 l = nfa_max_width(state->out, depth + 1);
3050 r = nfa_max_width(state->out1, depth + 1);
3051 if (l < 0 || r < 0)
3052 return -1;
3053 return len + (l > r ? l : r);
3054
3055 case NFA_ANY:
3056 case NFA_START_COLL:
3057 case NFA_START_NEG_COLL:
3058 /* matches some character, including composing chars */
3059#ifdef FEAT_MBYTE
3060 if (enc_utf8)
3061 len += MB_MAXBYTES;
3062 else if (has_mbyte)
3063 len += 2;
3064 else
3065#endif
3066 ++len;
3067 if (state->c != NFA_ANY)
3068 {
3069 /* skip over the characters */
3070 state = state->out1->out;
3071 continue;
3072 }
3073 break;
3074
3075 case NFA_DIGIT:
3076 case NFA_WHITE:
3077 case NFA_HEX:
3078 case NFA_OCTAL:
3079 /* ascii */
3080 ++len;
3081 break;
3082
3083 case NFA_IDENT:
3084 case NFA_SIDENT:
3085 case NFA_KWORD:
3086 case NFA_SKWORD:
3087 case NFA_FNAME:
3088 case NFA_SFNAME:
3089 case NFA_PRINT:
3090 case NFA_SPRINT:
3091 case NFA_NWHITE:
3092 case NFA_NDIGIT:
3093 case NFA_NHEX:
3094 case NFA_NOCTAL:
3095 case NFA_WORD:
3096 case NFA_NWORD:
3097 case NFA_HEAD:
3098 case NFA_NHEAD:
3099 case NFA_ALPHA:
3100 case NFA_NALPHA:
3101 case NFA_LOWER:
3102 case NFA_NLOWER:
3103 case NFA_UPPER:
3104 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003105 case NFA_LOWER_IC:
3106 case NFA_NLOWER_IC:
3107 case NFA_UPPER_IC:
3108 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02003109 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003110 /* possibly non-ascii */
3111#ifdef FEAT_MBYTE
3112 if (has_mbyte)
3113 len += 3;
3114 else
3115#endif
3116 ++len;
3117 break;
3118
3119 case NFA_START_INVISIBLE:
3120 case NFA_START_INVISIBLE_NEG:
3121 case NFA_START_INVISIBLE_BEFORE:
3122 case NFA_START_INVISIBLE_BEFORE_NEG:
3123 /* zero-width, out1 points to the END state */
3124 state = state->out1->out;
3125 continue;
3126
3127 case NFA_BACKREF1:
3128 case NFA_BACKREF2:
3129 case NFA_BACKREF3:
3130 case NFA_BACKREF4:
3131 case NFA_BACKREF5:
3132 case NFA_BACKREF6:
3133 case NFA_BACKREF7:
3134 case NFA_BACKREF8:
3135 case NFA_BACKREF9:
3136#ifdef FEAT_SYN_HL
3137 case NFA_ZREF1:
3138 case NFA_ZREF2:
3139 case NFA_ZREF3:
3140 case NFA_ZREF4:
3141 case NFA_ZREF5:
3142 case NFA_ZREF6:
3143 case NFA_ZREF7:
3144 case NFA_ZREF8:
3145 case NFA_ZREF9:
3146#endif
3147 case NFA_NEWL:
3148 case NFA_SKIP:
3149 /* unknown width */
3150 return -1;
3151
3152 case NFA_BOL:
3153 case NFA_EOL:
3154 case NFA_BOF:
3155 case NFA_EOF:
3156 case NFA_BOW:
3157 case NFA_EOW:
3158 case NFA_MOPEN:
3159 case NFA_MOPEN1:
3160 case NFA_MOPEN2:
3161 case NFA_MOPEN3:
3162 case NFA_MOPEN4:
3163 case NFA_MOPEN5:
3164 case NFA_MOPEN6:
3165 case NFA_MOPEN7:
3166 case NFA_MOPEN8:
3167 case NFA_MOPEN9:
3168#ifdef FEAT_SYN_HL
3169 case NFA_ZOPEN:
3170 case NFA_ZOPEN1:
3171 case NFA_ZOPEN2:
3172 case NFA_ZOPEN3:
3173 case NFA_ZOPEN4:
3174 case NFA_ZOPEN5:
3175 case NFA_ZOPEN6:
3176 case NFA_ZOPEN7:
3177 case NFA_ZOPEN8:
3178 case NFA_ZOPEN9:
3179 case NFA_ZCLOSE:
3180 case NFA_ZCLOSE1:
3181 case NFA_ZCLOSE2:
3182 case NFA_ZCLOSE3:
3183 case NFA_ZCLOSE4:
3184 case NFA_ZCLOSE5:
3185 case NFA_ZCLOSE6:
3186 case NFA_ZCLOSE7:
3187 case NFA_ZCLOSE8:
3188 case NFA_ZCLOSE9:
3189#endif
3190 case NFA_MCLOSE:
3191 case NFA_MCLOSE1:
3192 case NFA_MCLOSE2:
3193 case NFA_MCLOSE3:
3194 case NFA_MCLOSE4:
3195 case NFA_MCLOSE5:
3196 case NFA_MCLOSE6:
3197 case NFA_MCLOSE7:
3198 case NFA_MCLOSE8:
3199 case NFA_MCLOSE9:
3200 case NFA_NOPEN:
3201 case NFA_NCLOSE:
3202
3203 case NFA_LNUM_GT:
3204 case NFA_LNUM_LT:
3205 case NFA_COL_GT:
3206 case NFA_COL_LT:
3207 case NFA_VCOL_GT:
3208 case NFA_VCOL_LT:
3209 case NFA_MARK_GT:
3210 case NFA_MARK_LT:
3211 case NFA_VISUAL:
3212 case NFA_LNUM:
3213 case NFA_CURSOR:
3214 case NFA_COL:
3215 case NFA_VCOL:
3216 case NFA_MARK:
3217
3218 case NFA_ZSTART:
3219 case NFA_ZEND:
3220 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003221 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003222 case NFA_START_PATTERN:
3223 case NFA_END_PATTERN:
3224 case NFA_COMPOSING:
3225 case NFA_END_COMPOSING:
3226 /* zero-width */
3227 break;
3228
3229 default:
3230 if (state->c < 0)
3231 /* don't know what this is */
3232 return -1;
3233 /* normal character */
3234 len += MB_CHAR2LEN(state->c);
3235 break;
3236 }
3237
3238 /* normal way to continue */
3239 state = state->out;
3240 }
3241
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003242 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003243 return -1;
3244}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003245
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003246/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247 * Convert a postfix form into its equivalent NFA.
3248 * Return the NFA start state on success, NULL otherwise.
3249 */
3250 static nfa_state_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003251post2nfa(int *postfix, int *end, int nfa_calc_size)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003252{
3253 int *p;
3254 int mopen;
3255 int mclose;
3256 Frag_T *stack = NULL;
3257 Frag_T *stackp = NULL;
3258 Frag_T *stack_end = NULL;
3259 Frag_T e1;
3260 Frag_T e2;
3261 Frag_T e;
3262 nfa_state_T *s;
3263 nfa_state_T *s1;
3264 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003265 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003266
3267 if (postfix == NULL)
3268 return NULL;
3269
Bram Moolenaar053bb602013-05-20 13:55:21 +02003270#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003271#define POP() st_pop(&stackp, stack); \
3272 if (stackp < stack) \
3273 { \
3274 st_error(postfix, end, p); \
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003275 vim_free(stack); \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 return NULL; \
3277 }
3278
3279 if (nfa_calc_size == FALSE)
3280 {
3281 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003282 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003283 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003284 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003285 }
3286
3287 for (p = postfix; p < end; ++p)
3288 {
3289 switch (*p)
3290 {
3291 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003292 /* Concatenation.
3293 * Pay attention: this operator does not exist in the r.e. itself
3294 * (it is implicit, really). It is added when r.e. is translated
3295 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 if (nfa_calc_size == TRUE)
3297 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003298 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003299 break;
3300 }
3301 e2 = POP();
3302 e1 = POP();
3303 patch(e1.out, e2.start);
3304 PUSH(frag(e1.start, e2.out));
3305 break;
3306
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003307 case NFA_OR:
3308 /* Alternation */
3309 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 e2 = POP();
3315 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003316 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003317 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003318 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003319 PUSH(frag(s, append(e1.out, e2.out)));
3320 break;
3321
3322 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003323 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003324 if (nfa_calc_size == TRUE)
3325 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003326 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003327 break;
3328 }
3329 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003330 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003331 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003332 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 patch(e.out, s);
3334 PUSH(frag(s, list1(&s->out1)));
3335 break;
3336
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003337 case NFA_STAR_NONGREEDY:
3338 /* Zero or more, prefer zero */
3339 if (nfa_calc_size == TRUE)
3340 {
3341 nstate++;
3342 break;
3343 }
3344 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003345 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003346 if (s == NULL)
3347 goto theend;
3348 patch(e.out, s);
3349 PUSH(frag(s, list1(&s->out)));
3350 break;
3351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003352 case NFA_QUEST:
3353 /* one or zero atoms=> greedy match */
3354 if (nfa_calc_size == TRUE)
3355 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003356 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 break;
3358 }
3359 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003360 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003362 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003363 PUSH(frag(s, append(e.out, list1(&s->out1))));
3364 break;
3365
3366 case NFA_QUEST_NONGREEDY:
3367 /* zero or one atoms => non-greedy match */
3368 if (nfa_calc_size == TRUE)
3369 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003370 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003371 break;
3372 }
3373 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003374 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003375 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003376 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003377 PUSH(frag(s, append(e.out, list1(&s->out))));
3378 break;
3379
Bram Moolenaar417bad22013-06-07 14:08:30 +02003380 case NFA_END_COLL:
3381 case NFA_END_NEG_COLL:
3382 /* On the stack is the sequence starting with NFA_START_COLL or
3383 * NFA_START_NEG_COLL and all possible characters. Patch it to
3384 * add the output to the start. */
3385 if (nfa_calc_size == TRUE)
3386 {
3387 nstate++;
3388 break;
3389 }
3390 e = POP();
3391 s = alloc_state(NFA_END_COLL, NULL, NULL);
3392 if (s == NULL)
3393 goto theend;
3394 patch(e.out, s);
3395 e.start->out1 = s;
3396 PUSH(frag(e.start, list1(&s->out)));
3397 break;
3398
3399 case NFA_RANGE:
3400 /* Before this are two characters, the low and high end of a
3401 * range. Turn them into two states with MIN and MAX. */
3402 if (nfa_calc_size == TRUE)
3403 {
3404 /* nstate += 0; */
3405 break;
3406 }
3407 e2 = POP();
3408 e1 = POP();
3409 e2.start->val = e2.start->c;
3410 e2.start->c = NFA_RANGE_MAX;
3411 e1.start->val = e1.start->c;
3412 e1.start->c = NFA_RANGE_MIN;
3413 patch(e1.out, e2.start);
3414 PUSH(frag(e1.start, e2.out));
3415 break;
3416
Bram Moolenaar699c1202013-09-25 16:41:54 +02003417 case NFA_EMPTY:
3418 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003419 if (nfa_calc_size == TRUE)
3420 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003421 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003422 break;
3423 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003424 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003425 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003426 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003427 PUSH(frag(s, list1(&s->out)));
3428 break;
3429
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003430 case NFA_OPT_CHARS:
3431 {
3432 int n;
3433
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003434 /* \%[abc] implemented as:
3435 * NFA_SPLIT
3436 * +-CHAR(a)
3437 * | +-NFA_SPLIT
3438 * | +-CHAR(b)
3439 * | | +-NFA_SPLIT
3440 * | | +-CHAR(c)
3441 * | | | +-next
3442 * | | +- next
3443 * | +- next
3444 * +- next
3445 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003446 n = *++p; /* get number of characters */
3447 if (nfa_calc_size == TRUE)
3448 {
3449 nstate += n;
3450 break;
3451 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003452 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003453 e1.out = NULL; /* stores list with out1's */
3454 s1 = NULL; /* previous NFA_SPLIT to connect to */
3455 while (n-- > 0)
3456 {
3457 e = POP(); /* get character */
3458 s = alloc_state(NFA_SPLIT, e.start, NULL);
3459 if (s == NULL)
3460 goto theend;
3461 if (e1.out == NULL)
3462 e1 = e;
3463 patch(e.out, s1);
3464 append(e1.out, list1(&s->out1));
3465 s1 = s;
3466 }
3467 PUSH(frag(s, e1.out));
3468 break;
3469 }
3470
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003472 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003473 case NFA_PREV_ATOM_JUST_BEFORE:
3474 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003475 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003476 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003477 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3478 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003479 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003480 int start_state;
3481 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003482 int n = 0;
3483 nfa_state_T *zend;
3484 nfa_state_T *skip;
3485
Bram Moolenaardecd9542013-06-07 16:31:50 +02003486 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003487 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003488 case NFA_PREV_ATOM_NO_WIDTH:
3489 start_state = NFA_START_INVISIBLE;
3490 end_state = NFA_END_INVISIBLE;
3491 break;
3492 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3493 start_state = NFA_START_INVISIBLE_NEG;
3494 end_state = NFA_END_INVISIBLE_NEG;
3495 break;
3496 case NFA_PREV_ATOM_JUST_BEFORE:
3497 start_state = NFA_START_INVISIBLE_BEFORE;
3498 end_state = NFA_END_INVISIBLE;
3499 break;
3500 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3501 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3502 end_state = NFA_END_INVISIBLE_NEG;
3503 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003504 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003505 start_state = NFA_START_PATTERN;
3506 end_state = NFA_END_PATTERN;
3507 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003508 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003509
3510 if (before)
3511 n = *++p; /* get the count */
3512
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003513 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003514 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003515 * The \@<= operator: match for the preceding atom.
3516 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003517 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003518 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003519
3520 if (nfa_calc_size == TRUE)
3521 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003522 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003523 break;
3524 }
3525 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003526 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003527 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003528 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529
Bram Moolenaar87953742013-06-05 18:52:40 +02003530 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003532 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003533 if (pattern)
3534 {
3535 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3536 skip = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003537 if (skip == NULL)
3538 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003539 zend = alloc_state(NFA_ZEND, s1, NULL);
Bram Moolenaar983b3a52017-08-01 15:14:26 +02003540 if (zend == NULL)
3541 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003542 s1->out= skip;
3543 patch(e.out, zend);
3544 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003545 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003546 else
3547 {
3548 patch(e.out, s1);
3549 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003550 if (before)
3551 {
3552 if (n <= 0)
3553 /* See if we can guess the maximum width, it avoids a
3554 * lot of pointless tries. */
3555 n = nfa_max_width(e.start, 0);
3556 s->val = n; /* store the count */
3557 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003558 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003559 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003560 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003561
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003562#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003563 case NFA_COMPOSING: /* char with composing char */
3564#if 0
3565 /* TODO */
3566 if (regflags & RF_ICOMBINE)
3567 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003568 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003569 }
3570#endif
3571 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003572#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003573
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003574 case NFA_MOPEN: /* \( \) Submatch */
3575 case NFA_MOPEN1:
3576 case NFA_MOPEN2:
3577 case NFA_MOPEN3:
3578 case NFA_MOPEN4:
3579 case NFA_MOPEN5:
3580 case NFA_MOPEN6:
3581 case NFA_MOPEN7:
3582 case NFA_MOPEN8:
3583 case NFA_MOPEN9:
3584#ifdef FEAT_SYN_HL
3585 case NFA_ZOPEN: /* \z( \) Submatch */
3586 case NFA_ZOPEN1:
3587 case NFA_ZOPEN2:
3588 case NFA_ZOPEN3:
3589 case NFA_ZOPEN4:
3590 case NFA_ZOPEN5:
3591 case NFA_ZOPEN6:
3592 case NFA_ZOPEN7:
3593 case NFA_ZOPEN8:
3594 case NFA_ZOPEN9:
3595#endif
3596 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 if (nfa_calc_size == TRUE)
3598 {
3599 nstate += 2;
3600 break;
3601 }
3602
3603 mopen = *p;
3604 switch (*p)
3605 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003606 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3607#ifdef FEAT_SYN_HL
3608 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3609 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3610 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3611 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3612 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3613 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3614 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3615 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3616 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3617 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3618#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003619#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003620 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003621#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003622 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003623 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003624 mclose = *p + NSUBEXP;
3625 break;
3626 }
3627
3628 /* Allow "NFA_MOPEN" as a valid postfix representation for
3629 * the empty regexp "". In this case, the NFA will be
3630 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3631 * empty groups of parenthesis, and empty mbyte chars */
3632 if (stackp == stack)
3633 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003634 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003635 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003636 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003637 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003638 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003639 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003640 patch(list1(&s->out), s1);
3641 PUSH(frag(s, list1(&s1->out)));
3642 break;
3643 }
3644
3645 /* At least one node was emitted before NFA_MOPEN, so
3646 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3647 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003648 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003649 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003650 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003651
Bram Moolenaar525666f2013-06-02 16:40:55 +02003652 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003653 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003654 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003655 patch(e.out, s1);
3656
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003657#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003658 if (mopen == NFA_COMPOSING)
3659 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003660 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003661#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003662
3663 PUSH(frag(s, list1(&s1->out)));
3664 break;
3665
Bram Moolenaar5714b802013-05-28 22:03:20 +02003666 case NFA_BACKREF1:
3667 case NFA_BACKREF2:
3668 case NFA_BACKREF3:
3669 case NFA_BACKREF4:
3670 case NFA_BACKREF5:
3671 case NFA_BACKREF6:
3672 case NFA_BACKREF7:
3673 case NFA_BACKREF8:
3674 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003675#ifdef FEAT_SYN_HL
3676 case NFA_ZREF1:
3677 case NFA_ZREF2:
3678 case NFA_ZREF3:
3679 case NFA_ZREF4:
3680 case NFA_ZREF5:
3681 case NFA_ZREF6:
3682 case NFA_ZREF7:
3683 case NFA_ZREF8:
3684 case NFA_ZREF9:
3685#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003686 if (nfa_calc_size == TRUE)
3687 {
3688 nstate += 2;
3689 break;
3690 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003691 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003692 if (s == NULL)
3693 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003694 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003695 if (s1 == NULL)
3696 goto theend;
3697 patch(list1(&s->out), s1);
3698 PUSH(frag(s, list1(&s1->out)));
3699 break;
3700
Bram Moolenaar423532e2013-05-29 21:14:42 +02003701 case NFA_LNUM:
3702 case NFA_LNUM_GT:
3703 case NFA_LNUM_LT:
3704 case NFA_VCOL:
3705 case NFA_VCOL_GT:
3706 case NFA_VCOL_LT:
3707 case NFA_COL:
3708 case NFA_COL_GT:
3709 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003710 case NFA_MARK:
3711 case NFA_MARK_GT:
3712 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003713 {
3714 int n = *++p; /* lnum, col or mark name */
3715
Bram Moolenaar423532e2013-05-29 21:14:42 +02003716 if (nfa_calc_size == TRUE)
3717 {
3718 nstate += 1;
3719 break;
3720 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003721 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003722 if (s == NULL)
3723 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003724 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003725 PUSH(frag(s, list1(&s->out)));
3726 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003727 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003728
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 case NFA_ZSTART:
3730 case NFA_ZEND:
3731 default:
3732 /* Operands */
3733 if (nfa_calc_size == TRUE)
3734 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003735 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 break;
3737 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003738 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003739 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003740 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003741 PUSH(frag(s, list1(&s->out)));
3742 break;
3743
3744 } /* switch(*p) */
3745
3746 } /* for(p = postfix; *p; ++p) */
3747
3748 if (nfa_calc_size == TRUE)
3749 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003750 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003751 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752 }
3753
3754 e = POP();
3755 if (stackp != stack)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003756 {
3757 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003758 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 +02003759 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760
3761 if (istate >= nstate)
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003762 {
3763 vim_free(stack);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
Bram Moolenaar50ab9942015-04-13 15:28:12 +02003765 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003766
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767 matchstate = &state_ptr[istate++]; /* the match state */
3768 matchstate->c = NFA_MATCH;
3769 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003770 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003771
3772 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003773 ret = e.start;
3774
3775theend:
3776 vim_free(stack);
3777 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003778
3779#undef POP1
3780#undef PUSH1
3781#undef POP2
3782#undef PUSH2
3783#undef POP
3784#undef PUSH
3785}
3786
Bram Moolenaara2947e22013-06-11 22:44:09 +02003787/*
3788 * After building the NFA program, inspect it to add optimization hints.
3789 */
3790 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003791nfa_postprocess(nfa_regprog_T *prog)
Bram Moolenaara2947e22013-06-11 22:44:09 +02003792{
3793 int i;
3794 int c;
3795
3796 for (i = 0; i < prog->nstate; ++i)
3797 {
3798 c = prog->state[i].c;
3799 if (c == NFA_START_INVISIBLE
3800 || c == NFA_START_INVISIBLE_NEG
3801 || c == NFA_START_INVISIBLE_BEFORE
3802 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3803 {
3804 int directly;
3805
3806 /* Do it directly when what follows is possibly the end of the
3807 * match. */
3808 if (match_follows(prog->state[i].out1->out, 0))
3809 directly = TRUE;
3810 else
3811 {
3812 int ch_invisible = failure_chance(prog->state[i].out, 0);
3813 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3814
3815 /* Postpone when the invisible match is expensive or has a
3816 * lower chance of failing. */
3817 if (c == NFA_START_INVISIBLE_BEFORE
3818 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3819 {
3820 /* "before" matches are very expensive when
3821 * unbounded, always prefer what follows then,
3822 * unless what follows will always match.
3823 * Otherwise strongly prefer what follows. */
3824 if (prog->state[i].val <= 0 && ch_follows > 0)
3825 directly = FALSE;
3826 else
3827 directly = ch_follows * 10 < ch_invisible;
3828 }
3829 else
3830 {
3831 /* normal invisible, first do the one with the
3832 * highest failure chance */
3833 directly = ch_follows < ch_invisible;
3834 }
3835 }
3836 if (directly)
3837 /* switch to the _FIRST state */
3838 ++prog->state[i].c;
3839 }
3840 }
3841}
3842
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003843/****************************************************************
3844 * NFA execution code.
3845 ****************************************************************/
3846
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003847typedef struct
3848{
3849 int in_use; /* number of subexpr with useful info */
3850
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003851 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003852 union
3853 {
3854 struct multipos
3855 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003856 linenr_T start_lnum;
3857 linenr_T end_lnum;
3858 colnr_T start_col;
3859 colnr_T end_col;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003860 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003861 struct linepos
3862 {
3863 char_u *start;
3864 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003865 } line[NSUBEXP];
3866 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003867} regsub_T;
3868
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003869typedef struct
3870{
3871 regsub_T norm; /* \( .. \) matches */
3872#ifdef FEAT_SYN_HL
3873 regsub_T synt; /* \z( .. \) matches */
3874#endif
3875} regsubs_T;
3876
Bram Moolenaara2d95102013-06-04 14:23:05 +02003877/* nfa_pim_T stores a Postponed Invisible Match. */
3878typedef struct nfa_pim_S nfa_pim_T;
3879struct nfa_pim_S
3880{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003881 int result; /* NFA_PIM_*, see below */
3882 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003883 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003884 union
3885 {
3886 lpos_T pos;
3887 char_u *ptr;
3888 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003889};
3890
3891/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003892#define NFA_PIM_UNUSED 0 /* pim not used */
3893#define NFA_PIM_TODO 1 /* pim not done yet */
3894#define NFA_PIM_MATCH 2 /* pim executed, matches */
3895#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003896
3897
Bram Moolenaar963fee22013-05-26 21:47:28 +02003898/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003899typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003900{
3901 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003902 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003903 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3904 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003905 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003906} nfa_thread_T;
3907
Bram Moolenaar963fee22013-05-26 21:47:28 +02003908/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003909typedef struct
3910{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003911 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003912 int n; /* nr of states currently in "t" */
3913 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003914 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003915 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003916} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003917
Bram Moolenaar5714b802013-05-28 22:03:20 +02003918#ifdef ENABLE_LOG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003919static void log_subsexpr(regsubs_T *subs);
3920static void log_subexpr(regsub_T *sub);
3921static char *pim_info(nfa_pim_T *pim);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003922
3923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003924log_subsexpr(regsubs_T *subs)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003925{
3926 log_subexpr(&subs->norm);
3927# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003928 if (nfa_has_zsubexpr)
3929 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003930# endif
3931}
3932
Bram Moolenaar5714b802013-05-28 22:03:20 +02003933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003934log_subexpr(regsub_T *sub)
Bram Moolenaar5714b802013-05-28 22:03:20 +02003935{
3936 int j;
3937
3938 for (j = 0; j < sub->in_use; j++)
3939 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003940 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003941 j,
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01003942 sub->list.multi[j].start_col,
3943 (int)sub->list.multi[j].start_lnum,
3944 sub->list.multi[j].end_col,
3945 (int)sub->list.multi[j].end_lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003946 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003947 {
3948 char *s = (char *)sub->list.line[j].start;
3949 char *e = (char *)sub->list.line[j].end;
3950
Bram Moolenaar87953742013-06-05 18:52:40 +02003951 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003952 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003953 s == NULL ? "NULL" : s,
3954 e == NULL ? "NULL" : e);
3955 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003956}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003957
3958 static char *
Bram Moolenaar05540972016-01-30 20:31:25 +01003959pim_info(nfa_pim_T *pim)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003960{
3961 static char buf[30];
3962
3963 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3964 buf[0] = NUL;
3965 else
3966 {
3967 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3968 : (int)(pim->end.ptr - reginput));
3969 }
3970 return buf;
3971}
3972
Bram Moolenaar5714b802013-05-28 22:03:20 +02003973#endif
3974
Bram Moolenaar963fee22013-05-26 21:47:28 +02003975/* Used during execution: whether a match has been found. */
3976static int nfa_match;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003977#ifdef FEAT_RELTIME
3978static proftime_T *nfa_time_limit;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003979static int *nfa_timed_out;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01003980static int nfa_time_count;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01003981#endif
Bram Moolenaar4b417062013-05-25 20:19:50 +02003982
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003983static void copy_pim(nfa_pim_T *to, nfa_pim_T *from);
3984static void clear_sub(regsub_T *sub);
3985static void copy_sub(regsub_T *to, regsub_T *from);
3986static void copy_sub_off(regsub_T *to, regsub_T *from);
3987static void copy_ze_off(regsub_T *to, regsub_T *from);
3988static int sub_equal(regsub_T *sub1, regsub_T *sub2);
3989static int match_backref(regsub_T *sub, int subidx, int *bytelen);
3990static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim);
3991static int pim_equal(nfa_pim_T *one, nfa_pim_T *two);
3992static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs);
3993static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off);
3994static 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 +02003995
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003996/*
3997 * Copy postponed invisible match info from "from" to "to".
3998 */
3999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004000copy_pim(nfa_pim_T *to, nfa_pim_T *from)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004001{
4002 to->result = from->result;
4003 to->state = from->state;
4004 copy_sub(&to->subs.norm, &from->subs.norm);
4005#ifdef FEAT_SYN_HL
4006 if (nfa_has_zsubexpr)
4007 copy_sub(&to->subs.synt, &from->subs.synt);
4008#endif
4009 to->end = from->end;
4010}
4011
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004012 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004013clear_sub(regsub_T *sub)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004014{
4015 if (REG_MULTI)
4016 /* Use 0xff to set lnum to -1 */
4017 vim_memset(sub->list.multi, 0xff,
4018 sizeof(struct multipos) * nfa_nsubexpr);
4019 else
4020 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
4021 sub->in_use = 0;
4022}
4023
4024/*
4025 * Copy the submatches from "from" to "to".
4026 */
4027 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004028copy_sub(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004029{
4030 to->in_use = from->in_use;
4031 if (from->in_use > 0)
4032 {
4033 /* Copy the match start and end positions. */
4034 if (REG_MULTI)
4035 mch_memmove(&to->list.multi[0],
4036 &from->list.multi[0],
4037 sizeof(struct multipos) * from->in_use);
4038 else
4039 mch_memmove(&to->list.line[0],
4040 &from->list.line[0],
4041 sizeof(struct linepos) * from->in_use);
4042 }
4043}
4044
4045/*
4046 * Like copy_sub() but exclude the main match.
4047 */
4048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004049copy_sub_off(regsub_T *to, regsub_T *from)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004050{
4051 if (to->in_use < from->in_use)
4052 to->in_use = from->in_use;
4053 if (from->in_use > 1)
4054 {
4055 /* Copy the match start and end positions. */
4056 if (REG_MULTI)
4057 mch_memmove(&to->list.multi[1],
4058 &from->list.multi[1],
4059 sizeof(struct multipos) * (from->in_use - 1));
4060 else
4061 mch_memmove(&to->list.line[1],
4062 &from->list.line[1],
4063 sizeof(struct linepos) * (from->in_use - 1));
4064 }
4065}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004066
Bram Moolenaar428e9872013-05-30 17:05:39 +02004067/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02004068 * Like copy_sub() but only do the end of the main match if \ze is present.
4069 */
4070 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004071copy_ze_off(regsub_T *to, regsub_T *from)
Bram Moolenaarf2118842013-09-25 18:16:38 +02004072{
4073 if (nfa_has_zend)
4074 {
4075 if (REG_MULTI)
4076 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004077 if (from->list.multi[0].end_lnum >= 0)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004078 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004079 to->list.multi[0].end_lnum = from->list.multi[0].end_lnum;
4080 to->list.multi[0].end_col = from->list.multi[0].end_col;
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01004081 }
Bram Moolenaarf2118842013-09-25 18:16:38 +02004082 }
4083 else
4084 {
4085 if (from->list.line[0].end != NULL)
4086 to->list.line[0].end = from->list.line[0].end;
4087 }
4088 }
4089}
4090
4091/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004092 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02004093 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02004094 */
4095 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004096sub_equal(regsub_T *sub1, regsub_T *sub2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004097{
4098 int i;
4099 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004100 linenr_T s1;
4101 linenr_T s2;
4102 char_u *sp1;
4103 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004104
4105 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
4106 if (REG_MULTI)
4107 {
4108 for (i = 0; i < todo; ++i)
4109 {
4110 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004111 s1 = sub1->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004112 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004113 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004114 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004115 s2 = sub2->list.multi[i].start_lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004116 else
Bram Moolenaara0169122013-06-26 18:16:58 +02004117 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004118 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004119 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004120 if (s1 != -1 && sub1->list.multi[i].start_col
4121 != sub2->list.multi[i].start_col)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004122 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004123
4124 if (nfa_has_backref)
4125 {
4126 if (i < sub1->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004127 s1 = sub1->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004128 else
4129 s1 = -1;
4130 if (i < sub2->in_use)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004131 s2 = sub2->list.multi[i].end_lnum;
Bram Moolenaaree482532014-05-13 15:56:51 +02004132 else
4133 s2 = -1;
4134 if (s1 != s2)
4135 return FALSE;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004136 if (s1 != -1 && sub1->list.multi[i].end_col
4137 != sub2->list.multi[i].end_col)
Bram Moolenaaree482532014-05-13 15:56:51 +02004138 return FALSE;
4139 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004140 }
4141 }
4142 else
4143 {
4144 for (i = 0; i < todo; ++i)
4145 {
4146 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004147 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004148 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004149 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004150 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004151 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004152 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004153 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004154 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004155 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004156 if (nfa_has_backref)
4157 {
4158 if (i < sub1->in_use)
4159 sp1 = sub1->list.line[i].end;
4160 else
4161 sp1 = NULL;
4162 if (i < sub2->in_use)
4163 sp2 = sub2->list.line[i].end;
4164 else
4165 sp2 = NULL;
4166 if (sp1 != sp2)
4167 return FALSE;
4168 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004169 }
4170 }
4171
4172 return TRUE;
4173}
4174
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004175#ifdef ENABLE_LOG
4176 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004177report_state(char *action,
4178 regsub_T *sub,
4179 nfa_state_T *state,
4180 int lid,
4181 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004182{
4183 int col;
4184
4185 if (sub->in_use <= 0)
4186 col = -1;
4187 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004188 col = sub->list.multi[0].start_col;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004189 else
4190 col = (int)(sub->list.line[0].start - regline);
4191 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004192 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4193 action, abs(state->id), lid, state->c, code, col,
4194 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004195}
4196#endif
4197
Bram Moolenaar43e02982013-06-07 17:31:29 +02004198/*
4199 * Return TRUE if the same state is already in list "l" with the same
4200 * positions as "subs".
4201 */
4202 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004203has_state_with_pos(
4204 nfa_list_T *l, /* runtime state list */
4205 nfa_state_T *state, /* state to update */
4206 regsubs_T *subs, /* pointers to subexpressions */
4207 nfa_pim_T *pim) /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004208{
4209 nfa_thread_T *thread;
4210 int i;
4211
4212 for (i = 0; i < l->n; ++i)
4213 {
4214 thread = &l->t[i];
4215 if (thread->state->id == state->id
4216 && sub_equal(&thread->subs.norm, &subs->norm)
4217#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004218 && (!nfa_has_zsubexpr
4219 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004220#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004221 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004222 return TRUE;
4223 }
4224 return FALSE;
4225}
4226
4227/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004228 * Return TRUE if "one" and "two" are equal. That includes when both are not
4229 * set.
4230 */
4231 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004232pim_equal(nfa_pim_T *one, nfa_pim_T *two)
Bram Moolenaar69b52452013-07-17 21:10:51 +02004233{
4234 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4235 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4236
4237 if (one_unused)
4238 /* one is unused: equal when two is also unused */
4239 return two_unused;
4240 if (two_unused)
4241 /* one is used and two is not: not equal */
4242 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004243 /* compare the state id */
4244 if (one->state->id != two->state->id)
4245 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004246 /* compare the position */
4247 if (REG_MULTI)
4248 return one->end.pos.lnum == two->end.pos.lnum
4249 && one->end.pos.col == two->end.pos.col;
4250 return one->end.ptr == two->end.ptr;
4251}
4252
4253/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004254 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4255 */
4256 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004257match_follows(nfa_state_T *startstate, int depth)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004258{
4259 nfa_state_T *state = startstate;
4260
4261 /* avoid too much recursion */
4262 if (depth > 10)
4263 return FALSE;
4264
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004265 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004266 {
4267 switch (state->c)
4268 {
4269 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004270 case NFA_MCLOSE:
4271 case NFA_END_INVISIBLE:
4272 case NFA_END_INVISIBLE_NEG:
4273 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004274 return TRUE;
4275
4276 case NFA_SPLIT:
4277 return match_follows(state->out, depth + 1)
4278 || match_follows(state->out1, depth + 1);
4279
4280 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004281 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004282 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004283 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004284 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004285 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004286 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004287 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004288 case NFA_COMPOSING:
4289 /* skip ahead to next state */
4290 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004291 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004292
4293 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004294 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004295 case NFA_IDENT:
4296 case NFA_SIDENT:
4297 case NFA_KWORD:
4298 case NFA_SKWORD:
4299 case NFA_FNAME:
4300 case NFA_SFNAME:
4301 case NFA_PRINT:
4302 case NFA_SPRINT:
4303 case NFA_WHITE:
4304 case NFA_NWHITE:
4305 case NFA_DIGIT:
4306 case NFA_NDIGIT:
4307 case NFA_HEX:
4308 case NFA_NHEX:
4309 case NFA_OCTAL:
4310 case NFA_NOCTAL:
4311 case NFA_WORD:
4312 case NFA_NWORD:
4313 case NFA_HEAD:
4314 case NFA_NHEAD:
4315 case NFA_ALPHA:
4316 case NFA_NALPHA:
4317 case NFA_LOWER:
4318 case NFA_NLOWER:
4319 case NFA_UPPER:
4320 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004321 case NFA_LOWER_IC:
4322 case NFA_NLOWER_IC:
4323 case NFA_UPPER_IC:
4324 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004325 case NFA_START_COLL:
4326 case NFA_START_NEG_COLL:
4327 case NFA_NEWL:
4328 /* state will advance input */
4329 return FALSE;
4330
4331 default:
4332 if (state->c > 0)
4333 /* state will advance input */
4334 return FALSE;
4335
4336 /* Others: zero-width or possibly zero-width, might still find
4337 * a match at the same position, keep looking. */
4338 break;
4339 }
4340 state = state->out;
4341 }
4342 return FALSE;
4343}
4344
4345
4346/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004347 * Return TRUE if "state" is already in list "l".
4348 */
4349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004350state_in_list(
4351 nfa_list_T *l, /* runtime state list */
4352 nfa_state_T *state, /* state to update */
4353 regsubs_T *subs) /* pointers to subexpressions */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004354{
4355 if (state->lastlist[nfa_ll_index] == l->id)
4356 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004357 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004358 return TRUE;
4359 }
4360 return FALSE;
4361}
4362
Bram Moolenaar16b35782016-09-09 20:29:50 +02004363/* Offset used for "off" by addstate_here(). */
4364#define ADDSTATE_HERE_OFFSET 10
4365
Bram Moolenaard05bf562013-06-30 23:24:08 +02004366/*
4367 * Add "state" and possibly what follows to state list ".".
4368 * Returns "subs_arg", possibly copied into temp_subs.
4369 */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004370 static regsubs_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004371addstate(
4372 nfa_list_T *l, /* runtime state list */
4373 nfa_state_T *state, /* state to update */
4374 regsubs_T *subs_arg, /* pointers to subexpressions */
4375 nfa_pim_T *pim, /* postponed look-behind match */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004376 int off_arg) /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004377{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004378 int subidx;
Bram Moolenaar16b35782016-09-09 20:29:50 +02004379 int off = off_arg;
4380 int add_here = FALSE;
4381 int listindex = 0;
4382 int k;
4383 int found = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004384 nfa_thread_T *thread;
Bram Moolenaard5638832016-09-09 17:59:50 +02004385 struct multipos save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004386 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004387 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004388 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004389 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004390 regsubs_T *subs = subs_arg;
4391 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004392#ifdef ENABLE_LOG
4393 int did_print = FALSE;
4394#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004395
Bram Moolenaar16b35782016-09-09 20:29:50 +02004396 if (off_arg <= -ADDSTATE_HERE_OFFSET)
4397 {
4398 add_here = TRUE;
4399 off = 0;
4400 listindex = -(off_arg + ADDSTATE_HERE_OFFSET);
4401 }
4402
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004403 switch (state->c)
4404 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004405 case NFA_NCLOSE:
4406 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004407 case NFA_MCLOSE1:
4408 case NFA_MCLOSE2:
4409 case NFA_MCLOSE3:
4410 case NFA_MCLOSE4:
4411 case NFA_MCLOSE5:
4412 case NFA_MCLOSE6:
4413 case NFA_MCLOSE7:
4414 case NFA_MCLOSE8:
4415 case NFA_MCLOSE9:
4416#ifdef FEAT_SYN_HL
4417 case NFA_ZCLOSE:
4418 case NFA_ZCLOSE1:
4419 case NFA_ZCLOSE2:
4420 case NFA_ZCLOSE3:
4421 case NFA_ZCLOSE4:
4422 case NFA_ZCLOSE5:
4423 case NFA_ZCLOSE6:
4424 case NFA_ZCLOSE7:
4425 case NFA_ZCLOSE8:
4426 case NFA_ZCLOSE9:
4427#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004428 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004429 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004430 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004431 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004432 /* These nodes are not added themselves but their "out" and/or
4433 * "out1" may be added below. */
4434 break;
4435
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004436 case NFA_BOL:
4437 case NFA_BOF:
4438 /* "^" won't match past end-of-line, don't bother trying.
4439 * Except when at the end of the line, or when we are going to the
4440 * next line for a look-behind match. */
4441 if (reginput > regline
4442 && *reginput != NUL
4443 && (nfa_endp == NULL
4444 || !REG_MULTI
4445 || reglnum == nfa_endp->se_u.pos.lnum))
4446 goto skip_add;
4447 /* FALLTHROUGH */
4448
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004449 case NFA_MOPEN1:
4450 case NFA_MOPEN2:
4451 case NFA_MOPEN3:
4452 case NFA_MOPEN4:
4453 case NFA_MOPEN5:
4454 case NFA_MOPEN6:
4455 case NFA_MOPEN7:
4456 case NFA_MOPEN8:
4457 case NFA_MOPEN9:
4458#ifdef FEAT_SYN_HL
4459 case NFA_ZOPEN:
4460 case NFA_ZOPEN1:
4461 case NFA_ZOPEN2:
4462 case NFA_ZOPEN3:
4463 case NFA_ZOPEN4:
4464 case NFA_ZOPEN5:
4465 case NFA_ZOPEN6:
4466 case NFA_ZOPEN7:
4467 case NFA_ZOPEN8:
4468 case NFA_ZOPEN9:
4469#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004470 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004471 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004472 /* These nodes need to be added so that we can bail out when it
4473 * was added to this list before at the same position to avoid an
4474 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004475
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004476 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004477 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004478 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004479 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004480 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004481 * when there is a PIM. For NFA_MATCH check the position,
4482 * lower position is preferred. */
4483 if (!nfa_has_backref && pim == NULL && !l->has_pim
4484 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004485 {
Bram Moolenaar16b35782016-09-09 20:29:50 +02004486 /* When called from addstate_here() do insert before
4487 * existing states. */
4488 if (add_here)
4489 {
4490 for (k = 0; k < l->n && k < listindex; ++k)
4491 if (l->t[k].state->id == state->id)
4492 {
4493 found = TRUE;
4494 break;
4495 }
4496 }
4497 if (!add_here || found)
4498 {
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004499skip_add:
4500#ifdef ENABLE_LOG
Bram Moolenaar16b35782016-09-09 20:29:50 +02004501 nfa_set_code(state->c);
4502 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s pim: %s has_pim: %d found: %d\n",
4503 abs(state->id), l->id, state->c, code,
4504 pim == NULL ? "NULL" : "yes", l->has_pim, found);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004505#endif
Bram Moolenaar16b35782016-09-09 20:29:50 +02004506 return subs;
4507 }
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004508 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004509
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004510 /* Do not add the state again when it exists with the same
4511 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004512 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004513 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004514 }
4515
Bram Moolenaara0169122013-06-26 18:16:58 +02004516 /* When there are backreferences or PIMs the number of states may
4517 * be (a lot) bigger than anticipated. */
4518 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004519 {
4520 int newlen = l->len * 3 / 2 + 50;
4521
Bram Moolenaard05bf562013-06-30 23:24:08 +02004522 if (subs != &temp_subs)
4523 {
4524 /* "subs" may point into the current array, need to make a
4525 * copy before it becomes invalid. */
4526 copy_sub(&temp_subs.norm, &subs->norm);
4527#ifdef FEAT_SYN_HL
4528 if (nfa_has_zsubexpr)
4529 copy_sub(&temp_subs.synt, &subs->synt);
4530#endif
4531 subs = &temp_subs;
4532 }
4533
Bram Moolenaara1d2c582015-02-10 18:18:17 +01004534 /* TODO: check for vim_realloc() returning NULL. */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004535 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4536 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004537 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004538
4539 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004540 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004541 thread = &l->t[l->n++];
4542 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004543 if (pim == NULL)
4544 thread->pim.result = NFA_PIM_UNUSED;
4545 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004546 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004547 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004548 l->has_pim = TRUE;
4549 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004550 copy_sub(&thread->subs.norm, &subs->norm);
4551#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004552 if (nfa_has_zsubexpr)
4553 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004554#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004555#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004556 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004557 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004558#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559 }
4560
4561#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004562 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004563 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004564#endif
4565 switch (state->c)
4566 {
4567 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004568 break;
4569
4570 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004571 /* order matters here */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004572 subs = addstate(l, state->out, subs, pim, off_arg);
4573 subs = addstate(l, state->out1, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574 break;
4575
Bram Moolenaar699c1202013-09-25 16:41:54 +02004576 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004577 case NFA_NOPEN:
4578 case NFA_NCLOSE:
Bram Moolenaar16b35782016-09-09 20:29:50 +02004579 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004580 break;
4581
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004582 case NFA_MOPEN:
4583 case NFA_MOPEN1:
4584 case NFA_MOPEN2:
4585 case NFA_MOPEN3:
4586 case NFA_MOPEN4:
4587 case NFA_MOPEN5:
4588 case NFA_MOPEN6:
4589 case NFA_MOPEN7:
4590 case NFA_MOPEN8:
4591 case NFA_MOPEN9:
4592#ifdef FEAT_SYN_HL
4593 case NFA_ZOPEN:
4594 case NFA_ZOPEN1:
4595 case NFA_ZOPEN2:
4596 case NFA_ZOPEN3:
4597 case NFA_ZOPEN4:
4598 case NFA_ZOPEN5:
4599 case NFA_ZOPEN6:
4600 case NFA_ZOPEN7:
4601 case NFA_ZOPEN8:
4602 case NFA_ZOPEN9:
4603#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004604 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004605 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004606 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004607 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004608 sub = &subs->norm;
4609 }
4610#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004611 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004612 {
4613 subidx = state->c - NFA_ZOPEN;
4614 sub = &subs->synt;
4615 }
4616#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004617 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004618 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004619 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004620 sub = &subs->norm;
4621 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004622
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004623 /* avoid compiler warnings */
4624 save_ptr = NULL;
Bram Moolenaard5638832016-09-09 17:59:50 +02004625 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004626
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004627 /* Set the position (with "off" added) in the subexpression. Save
4628 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629 if (REG_MULTI)
4630 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004631 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004632 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004633 save_multipos = sub->list.multi[subidx];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004634 save_in_use = -1;
4635 }
4636 else
4637 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004638 save_in_use = sub->in_use;
4639 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004640 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004641 sub->list.multi[i].start_lnum = -1;
4642 sub->list.multi[i].end_lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004643 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004644 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004645 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004646 if (off == -1)
4647 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004648 sub->list.multi[subidx].start_lnum = reglnum + 1;
4649 sub->list.multi[subidx].start_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004650 }
4651 else
4652 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004653 sub->list.multi[subidx].start_lnum = reglnum;
4654 sub->list.multi[subidx].start_col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004655 (colnr_T)(reginput - regline + off);
4656 }
Bram Moolenaarc2b717e2015-09-29 15:06:14 +02004657 sub->list.multi[subidx].end_lnum = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004658 }
4659 else
4660 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004661 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004662 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004663 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004664 save_in_use = -1;
4665 }
4666 else
4667 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004668 save_in_use = sub->in_use;
4669 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004670 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004671 sub->list.line[i].start = NULL;
4672 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004673 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004674 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004675 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004676 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004677 }
4678
Bram Moolenaar16b35782016-09-09 20:29:50 +02004679 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004680 /* "subs" may have changed, need to set "sub" again */
4681#ifdef FEAT_SYN_HL
4682 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4683 sub = &subs->synt;
4684 else
4685#endif
4686 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004687
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004688 if (save_in_use == -1)
4689 {
4690 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004691 sub->list.multi[subidx] = save_multipos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004692 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004693 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004694 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004696 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004697 break;
4698
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004699 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004700 if (nfa_has_zend && (REG_MULTI
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004701 ? subs->norm.list.multi[0].end_lnum >= 0
Bram Moolenaar9be44812013-09-05 21:15:44 +02004702 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004703 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004704 /* Do not overwrite the position set by \ze. */
Bram Moolenaar16b35782016-09-09 20:29:50 +02004705 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004706 break;
4707 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004708 /* FALLTHROUGH */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004709 case NFA_MCLOSE1:
4710 case NFA_MCLOSE2:
4711 case NFA_MCLOSE3:
4712 case NFA_MCLOSE4:
4713 case NFA_MCLOSE5:
4714 case NFA_MCLOSE6:
4715 case NFA_MCLOSE7:
4716 case NFA_MCLOSE8:
4717 case NFA_MCLOSE9:
4718#ifdef FEAT_SYN_HL
4719 case NFA_ZCLOSE:
4720 case NFA_ZCLOSE1:
4721 case NFA_ZCLOSE2:
4722 case NFA_ZCLOSE3:
4723 case NFA_ZCLOSE4:
4724 case NFA_ZCLOSE5:
4725 case NFA_ZCLOSE6:
4726 case NFA_ZCLOSE7:
4727 case NFA_ZCLOSE8:
4728 case NFA_ZCLOSE9:
4729#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004731 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004732 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004733 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004734 sub = &subs->norm;
4735 }
4736#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004737 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004738 {
4739 subidx = state->c - NFA_ZCLOSE;
4740 sub = &subs->synt;
4741 }
4742#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004743 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004744 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004745 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004746 sub = &subs->norm;
4747 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004748
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004749 /* We don't fill in gaps here, there must have been an MOPEN that
4750 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004751 save_in_use = sub->in_use;
4752 if (sub->in_use <= subidx)
4753 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004754 if (REG_MULTI)
4755 {
Bram Moolenaard5638832016-09-09 17:59:50 +02004756 save_multipos = sub->list.multi[subidx];
Bram Moolenaar35b23862013-05-22 23:00:40 +02004757 if (off == -1)
4758 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004759 sub->list.multi[subidx].end_lnum = reglnum + 1;
4760 sub->list.multi[subidx].end_col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004761 }
4762 else
4763 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004764 sub->list.multi[subidx].end_lnum = reglnum;
4765 sub->list.multi[subidx].end_col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004766 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004767 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004768 /* avoid compiler warnings */
4769 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004770 }
4771 else
4772 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004773 save_ptr = sub->list.line[subidx].end;
4774 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004775 /* avoid compiler warnings */
Bram Moolenaard5638832016-09-09 17:59:50 +02004776 vim_memset(&save_multipos, 0, sizeof(save_multipos));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004777 }
4778
Bram Moolenaar16b35782016-09-09 20:29:50 +02004779 subs = addstate(l, state->out, subs, pim, off_arg);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004780 /* "subs" may have changed, need to set "sub" again */
4781#ifdef FEAT_SYN_HL
4782 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4783 sub = &subs->synt;
4784 else
4785#endif
4786 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004787
4788 if (REG_MULTI)
Bram Moolenaard5638832016-09-09 17:59:50 +02004789 sub->list.multi[subidx] = save_multipos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004790 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004791 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004792 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004793 break;
4794 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004795 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004796}
4797
4798/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004799 * Like addstate(), but the new state(s) are put at position "*ip".
4800 * Used for zero-width matches, next state to use is the added one.
4801 * This makes sure the order of states to be tried does not change, which
4802 * matters for alternatives.
4803 */
4804 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004805addstate_here(
4806 nfa_list_T *l, /* runtime state list */
4807 nfa_state_T *state, /* state to update */
4808 regsubs_T *subs, /* pointers to subexpressions */
4809 nfa_pim_T *pim, /* postponed look-behind match */
4810 int *ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004811{
4812 int tlen = l->n;
4813 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004814 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004815
Bram Moolenaar16b35782016-09-09 20:29:50 +02004816 /* First add the state(s) at the end, so that we know how many there are.
4817 * Pass the listidx as offset (avoids adding another argument to
4818 * addstate(). */
4819 addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004820
Bram Moolenaar4b417062013-05-25 20:19:50 +02004821 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004822 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004823 return;
4824
4825 /* re-order to put the new state at the current position */
4826 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004827 if (count == 0)
4828 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004829 if (count == 1)
4830 {
4831 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004832 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004833 }
4834 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004835 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004836 if (l->n + count - 1 >= l->len)
4837 {
4838 /* not enough space to move the new states, reallocate the list
4839 * and move the states to the right position */
4840 nfa_thread_T *newl;
4841
4842 l->len = l->len * 3 / 2 + 50;
4843 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4844 if (newl == NULL)
4845 return;
4846 mch_memmove(&(newl[0]),
4847 &(l->t[0]),
4848 sizeof(nfa_thread_T) * listidx);
4849 mch_memmove(&(newl[listidx]),
4850 &(l->t[l->n - count]),
4851 sizeof(nfa_thread_T) * count);
4852 mch_memmove(&(newl[listidx + count]),
4853 &(l->t[listidx + 1]),
4854 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4855 vim_free(l->t);
4856 l->t = newl;
4857 }
4858 else
4859 {
4860 /* make space for new states, then move them from the
4861 * end to the current position */
4862 mch_memmove(&(l->t[listidx + count]),
4863 &(l->t[listidx + 1]),
4864 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4865 mch_memmove(&(l->t[listidx]),
4866 &(l->t[l->n - 1]),
4867 sizeof(nfa_thread_T) * count);
4868 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004869 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004870 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004871 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004872}
4873
4874/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004875 * Check character class "class" against current character c.
4876 */
4877 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004878check_char_class(int class, int c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004879{
4880 switch (class)
4881 {
4882 case NFA_CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004883 if (c >= 1 && c < 128 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004884 return OK;
4885 break;
4886 case NFA_CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004887 if (c >= 1 && c < 128 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004888 return OK;
4889 break;
4890 case NFA_CLASS_BLANK:
4891 if (c == ' ' || c == '\t')
4892 return OK;
4893 break;
4894 case NFA_CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004895 if (c >= 1 && c <= 127 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004896 return OK;
4897 break;
4898 case NFA_CLASS_DIGIT:
4899 if (VIM_ISDIGIT(c))
4900 return OK;
4901 break;
4902 case NFA_CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02004903 if (c >= 1 && c <= 127 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004904 return OK;
4905 break;
4906 case NFA_CLASS_LOWER:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004907 if (MB_ISLOWER(c) && c != 170 && c != 186)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004908 return OK;
4909 break;
4910 case NFA_CLASS_PRINT:
4911 if (vim_isprintc(c))
4912 return OK;
4913 break;
4914 case NFA_CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02004915 if (c >= 1 && c < 128 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004916 return OK;
4917 break;
4918 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004919 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004920 return OK;
4921 break;
4922 case NFA_CLASS_UPPER:
4923 if (MB_ISUPPER(c))
4924 return OK;
4925 break;
4926 case NFA_CLASS_XDIGIT:
4927 if (vim_isxdigit(c))
4928 return OK;
4929 break;
4930 case NFA_CLASS_TAB:
4931 if (c == '\t')
4932 return OK;
4933 break;
4934 case NFA_CLASS_RETURN:
4935 if (c == '\r')
4936 return OK;
4937 break;
4938 case NFA_CLASS_BACKSPACE:
4939 if (c == '\b')
4940 return OK;
4941 break;
4942 case NFA_CLASS_ESCAPE:
4943 if (c == '\033')
4944 return OK;
4945 break;
4946
4947 default:
4948 /* should not be here :P */
Bram Moolenaarde330112017-01-08 20:50:52 +01004949 IEMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004950 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004951 }
4952 return FAIL;
4953}
4954
Bram Moolenaar5714b802013-05-28 22:03:20 +02004955/*
4956 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004957 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004958 */
4959 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004960match_backref(
4961 regsub_T *sub, /* pointers to subexpressions */
4962 int subidx,
4963 int *bytelen) /* out: length of match in bytes */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004964{
4965 int len;
4966
4967 if (sub->in_use <= subidx)
4968 {
4969retempty:
4970 /* backref was not set, match an empty string */
4971 *bytelen = 0;
4972 return TRUE;
4973 }
4974
4975 if (REG_MULTI)
4976 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004977 if (sub->list.multi[subidx].start_lnum < 0
4978 || sub->list.multi[subidx].end_lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004979 goto retempty;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004980 if (sub->list.multi[subidx].start_lnum == reglnum
4981 && sub->list.multi[subidx].end_lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004982 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004983 len = sub->list.multi[subidx].end_col
4984 - sub->list.multi[subidx].start_col;
4985 if (cstrncmp(regline + sub->list.multi[subidx].start_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004986 reginput, &len) == 0)
4987 {
4988 *bytelen = len;
4989 return TRUE;
4990 }
4991 }
4992 else
4993 {
4994 if (match_with_backref(
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01004995 sub->list.multi[subidx].start_lnum,
4996 sub->list.multi[subidx].start_col,
4997 sub->list.multi[subidx].end_lnum,
4998 sub->list.multi[subidx].end_col,
Bram Moolenaar580abea2013-06-14 20:31:28 +02004999 bytelen) == RA_MATCH)
5000 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005001 }
5002 }
5003 else
5004 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005005 if (sub->list.line[subidx].start == NULL
5006 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005007 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02005008 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
5009 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02005010 {
5011 *bytelen = len;
5012 return TRUE;
5013 }
5014 }
5015 return FALSE;
5016}
5017
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005018#ifdef FEAT_SYN_HL
5019
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005020static int match_zref(int subidx, int *bytelen);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005021
5022/*
5023 * Check for a match with \z subexpression "subidx".
5024 * Return TRUE if it matches.
5025 */
5026 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005027match_zref(
5028 int subidx,
5029 int *bytelen) /* out: length of match in bytes */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005030{
5031 int len;
5032
5033 cleanup_zsubexpr();
5034 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
5035 {
5036 /* backref was not set, match an empty string */
5037 *bytelen = 0;
5038 return TRUE;
5039 }
5040
5041 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
5042 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
5043 {
5044 *bytelen = len;
5045 return TRUE;
5046 }
5047 return FALSE;
5048}
5049#endif
5050
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005052 * Save list IDs for all NFA states of "prog" into "list".
5053 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005054 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005055 */
5056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005057nfa_save_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005058{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005059 int i;
5060 nfa_state_T *p;
5061
5062 /* Order in the list is reverse, it's a bit faster that way. */
5063 p = &prog->state[0];
5064 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005065 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005066 list[i] = p->lastlist[1];
5067 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005068 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005069 }
5070}
5071
5072/*
5073 * Restore list IDs from "list" to all NFA states.
5074 */
5075 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005076nfa_restore_listids(nfa_regprog_T *prog, int *list)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005077{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005078 int i;
5079 nfa_state_T *p;
5080
5081 p = &prog->state[0];
5082 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005083 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005084 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005085 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005086 }
5087}
5088
Bram Moolenaar423532e2013-05-29 21:14:42 +02005089 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005090nfa_re_num_cmp(long_u val, int op, long_u pos)
Bram Moolenaar423532e2013-05-29 21:14:42 +02005091{
5092 if (op == 1) return pos > val;
5093 if (op == 2) return pos < val;
5094 return val == pos;
5095}
5096
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005097static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids);
5098static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005099
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005100/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02005101 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005102 * "pim" is NULL or contains info about a Postponed Invisible Match (start
5103 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02005104 */
5105 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005106recursive_regmatch(
5107 nfa_state_T *state,
5108 nfa_pim_T *pim,
5109 nfa_regprog_T *prog,
5110 regsubs_T *submatch,
5111 regsubs_T *m,
5112 int **listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005113{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005114 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02005115 int save_reglnum = reglnum;
5116 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005117 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005118 save_se_T *save_nfa_endp = nfa_endp;
5119 save_se_T endpos;
5120 save_se_T *endposp = NULL;
5121 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005122 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005123
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005124 if (pim != NULL)
5125 {
5126 /* start at the position where the postponed match was */
5127 if (REG_MULTI)
5128 reginput = regline + pim->end.pos.col;
5129 else
5130 reginput = pim->end.ptr;
5131 }
5132
Bram Moolenaardecd9542013-06-07 16:31:50 +02005133 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01005134 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5135 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
5136 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005137 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005138 /* The recursive match must end at the current position. When "pim" is
5139 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005140 endposp = &endpos;
5141 if (REG_MULTI)
5142 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005143 if (pim == NULL)
5144 {
5145 endpos.se_u.pos.col = (int)(reginput - regline);
5146 endpos.se_u.pos.lnum = reglnum;
5147 }
5148 else
5149 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005150 }
5151 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005152 {
5153 if (pim == NULL)
5154 endpos.se_u.ptr = reginput;
5155 else
5156 endpos.se_u.ptr = pim->end.ptr;
5157 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005158
5159 /* Go back the specified number of bytes, or as far as the
5160 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005161 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005162 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005163 if (state->val <= 0)
5164 {
5165 if (REG_MULTI)
5166 {
5167 regline = reg_getline(--reglnum);
5168 if (regline == NULL)
5169 /* can't go before the first line */
5170 regline = reg_getline(++reglnum);
5171 }
5172 reginput = regline;
5173 }
5174 else
5175 {
5176 if (REG_MULTI && (int)(reginput - regline) < state->val)
5177 {
5178 /* Not enough bytes in this line, go to end of
5179 * previous line. */
5180 regline = reg_getline(--reglnum);
5181 if (regline == NULL)
5182 {
5183 /* can't go before the first line */
5184 regline = reg_getline(++reglnum);
5185 reginput = regline;
5186 }
5187 else
5188 reginput = regline + STRLEN(regline);
5189 }
5190 if ((int)(reginput - regline) >= state->val)
5191 {
5192 reginput -= state->val;
5193#ifdef FEAT_MBYTE
5194 if (has_mbyte)
5195 reginput -= mb_head_off(regline, reginput);
5196#endif
5197 }
5198 else
5199 reginput = regline;
5200 }
5201 }
5202
Bram Moolenaarf46da702013-06-02 22:37:42 +02005203#ifdef ENABLE_LOG
5204 if (log_fd != stderr)
5205 fclose(log_fd);
5206 log_fd = NULL;
5207#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005208 /* Have to clear the lastlist field of the NFA nodes, so that
5209 * nfa_regmatch() and addstate() can run properly after recursion. */
5210 if (nfa_ll_index == 1)
5211 {
5212 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5213 * values and clear them. */
5214 if (*listids == NULL)
5215 {
5216 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5217 if (*listids == NULL)
5218 {
5219 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5220 return 0;
5221 }
5222 }
5223 nfa_save_listids(prog, *listids);
5224 need_restore = TRUE;
5225 /* any value of nfa_listid will do */
5226 }
5227 else
5228 {
5229 /* First recursive nfa_regmatch() call, switch to the second lastlist
5230 * entry. Make sure nfa_listid is different from a previous recursive
5231 * call, because some states may still have this ID. */
5232 ++nfa_ll_index;
5233 if (nfa_listid <= nfa_alt_listid)
5234 nfa_listid = nfa_alt_listid;
5235 }
5236
5237 /* Call nfa_regmatch() to check if the current concat matches at this
5238 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005239 nfa_endp = endposp;
5240 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005241
5242 if (need_restore)
5243 nfa_restore_listids(prog, *listids);
5244 else
5245 {
5246 --nfa_ll_index;
5247 nfa_alt_listid = nfa_listid;
5248 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005249
5250 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005251 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005252 if (REG_MULTI)
5253 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005254 reginput = regline + save_reginput_col;
Bram Moolenaar6747fab2016-06-28 22:39:16 +02005255 if (result != NFA_TOO_EXPENSIVE)
5256 {
5257 nfa_match = save_nfa_match;
5258 nfa_listid = save_nfa_listid;
5259 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005260 nfa_endp = save_nfa_endp;
5261
5262#ifdef ENABLE_LOG
5263 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5264 if (log_fd != NULL)
5265 {
5266 fprintf(log_fd, "****************************\n");
5267 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5268 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5269 fprintf(log_fd, "****************************\n");
5270 }
5271 else
5272 {
Bram Moolenaarc1669272018-06-19 14:23:53 +02005273 EMSG(_("Could not open temporary log file for writing, displaying on stderr... "));
Bram Moolenaarf46da702013-06-02 22:37:42 +02005274 log_fd = stderr;
5275 }
5276#endif
5277
5278 return result;
5279}
5280
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005281static int skip_to_start(int c, colnr_T *colp);
5282static long find_match_text(colnr_T startcol, int regstart, char_u *match_text);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005283
5284/*
5285 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005286 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005287 * NFA_ANY: 1
5288 * specific character: 99
5289 */
5290 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005291failure_chance(nfa_state_T *state, int depth)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005292{
5293 int c = state->c;
5294 int l, r;
5295
5296 /* detect looping */
5297 if (depth > 4)
5298 return 1;
5299
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005300 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005301 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005302 case NFA_SPLIT:
5303 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5304 /* avoid recursive stuff */
5305 return 1;
5306 /* two alternatives, use the lowest failure chance */
5307 l = failure_chance(state->out, depth + 1);
5308 r = failure_chance(state->out1, depth + 1);
5309 return l < r ? l : r;
5310
5311 case NFA_ANY:
5312 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005313 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005314
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005315 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005316 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005317 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005318 /* empty match works always */
5319 return 0;
5320
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005321 case NFA_START_INVISIBLE:
5322 case NFA_START_INVISIBLE_FIRST:
5323 case NFA_START_INVISIBLE_NEG:
5324 case NFA_START_INVISIBLE_NEG_FIRST:
5325 case NFA_START_INVISIBLE_BEFORE:
5326 case NFA_START_INVISIBLE_BEFORE_FIRST:
5327 case NFA_START_INVISIBLE_BEFORE_NEG:
5328 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5329 case NFA_START_PATTERN:
5330 /* recursive regmatch is expensive, use low failure chance */
5331 return 5;
5332
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005333 case NFA_BOL:
5334 case NFA_EOL:
5335 case NFA_BOF:
5336 case NFA_EOF:
5337 case NFA_NEWL:
5338 return 99;
5339
5340 case NFA_BOW:
5341 case NFA_EOW:
5342 return 90;
5343
5344 case NFA_MOPEN:
5345 case NFA_MOPEN1:
5346 case NFA_MOPEN2:
5347 case NFA_MOPEN3:
5348 case NFA_MOPEN4:
5349 case NFA_MOPEN5:
5350 case NFA_MOPEN6:
5351 case NFA_MOPEN7:
5352 case NFA_MOPEN8:
5353 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005354#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005355 case NFA_ZOPEN:
5356 case NFA_ZOPEN1:
5357 case NFA_ZOPEN2:
5358 case NFA_ZOPEN3:
5359 case NFA_ZOPEN4:
5360 case NFA_ZOPEN5:
5361 case NFA_ZOPEN6:
5362 case NFA_ZOPEN7:
5363 case NFA_ZOPEN8:
5364 case NFA_ZOPEN9:
5365 case NFA_ZCLOSE:
5366 case NFA_ZCLOSE1:
5367 case NFA_ZCLOSE2:
5368 case NFA_ZCLOSE3:
5369 case NFA_ZCLOSE4:
5370 case NFA_ZCLOSE5:
5371 case NFA_ZCLOSE6:
5372 case NFA_ZCLOSE7:
5373 case NFA_ZCLOSE8:
5374 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005375#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005376 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005377 case NFA_MCLOSE1:
5378 case NFA_MCLOSE2:
5379 case NFA_MCLOSE3:
5380 case NFA_MCLOSE4:
5381 case NFA_MCLOSE5:
5382 case NFA_MCLOSE6:
5383 case NFA_MCLOSE7:
5384 case NFA_MCLOSE8:
5385 case NFA_MCLOSE9:
5386 case NFA_NCLOSE:
5387 return failure_chance(state->out, depth + 1);
5388
5389 case NFA_BACKREF1:
5390 case NFA_BACKREF2:
5391 case NFA_BACKREF3:
5392 case NFA_BACKREF4:
5393 case NFA_BACKREF5:
5394 case NFA_BACKREF6:
5395 case NFA_BACKREF7:
5396 case NFA_BACKREF8:
5397 case NFA_BACKREF9:
5398#ifdef FEAT_SYN_HL
5399 case NFA_ZREF1:
5400 case NFA_ZREF2:
5401 case NFA_ZREF3:
5402 case NFA_ZREF4:
5403 case NFA_ZREF5:
5404 case NFA_ZREF6:
5405 case NFA_ZREF7:
5406 case NFA_ZREF8:
5407 case NFA_ZREF9:
5408#endif
5409 /* backreferences don't match in many places */
5410 return 94;
5411
5412 case NFA_LNUM_GT:
5413 case NFA_LNUM_LT:
5414 case NFA_COL_GT:
5415 case NFA_COL_LT:
5416 case NFA_VCOL_GT:
5417 case NFA_VCOL_LT:
5418 case NFA_MARK_GT:
5419 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005420 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005421 /* before/after positions don't match very often */
5422 return 85;
5423
5424 case NFA_LNUM:
5425 return 90;
5426
5427 case NFA_CURSOR:
5428 case NFA_COL:
5429 case NFA_VCOL:
5430 case NFA_MARK:
5431 /* specific positions rarely match */
5432 return 98;
5433
5434 case NFA_COMPOSING:
5435 return 95;
5436
5437 default:
5438 if (c > 0)
5439 /* character match fails often */
5440 return 95;
5441 }
5442
5443 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005444 return 50;
5445}
5446
Bram Moolenaarf46da702013-06-02 22:37:42 +02005447/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005448 * Skip until the char "c" we know a match must start with.
5449 */
5450 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005451skip_to_start(int c, colnr_T *colp)
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005452{
5453 char_u *s;
5454
5455 /* Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02005456 if (!rex.reg_ic
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005457#ifdef FEAT_MBYTE
5458 && !has_mbyte
5459#endif
5460 )
5461 s = vim_strbyte(regline + *colp, c);
5462 else
5463 s = cstrchr(regline + *colp, c);
5464 if (s == NULL)
5465 return FAIL;
5466 *colp = (int)(s - regline);
5467 return OK;
5468}
5469
5470/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005471 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005472 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005473 * Returns zero for no match, 1 for a match.
5474 */
5475 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01005476find_match_text(colnr_T startcol, int regstart, char_u *match_text)
Bram Moolenaar473de612013-06-08 18:19:48 +02005477{
5478 colnr_T col = startcol;
5479 int c1, c2;
5480 int len1, len2;
5481 int match;
5482
5483 for (;;)
5484 {
5485 match = TRUE;
5486 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5487 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5488 {
5489 c1 = PTR2CHAR(match_text + len1);
5490 c2 = PTR2CHAR(regline + col + len2);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005491 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
Bram Moolenaar473de612013-06-08 18:19:48 +02005492 {
5493 match = FALSE;
5494 break;
5495 }
5496 len2 += MB_CHAR2LEN(c2);
5497 }
5498 if (match
5499#ifdef FEAT_MBYTE
5500 /* check that no composing char follows */
5501 && !(enc_utf8
5502 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5503#endif
5504 )
5505 {
5506 cleanup_subexpr();
5507 if (REG_MULTI)
5508 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005509 rex.reg_startpos[0].lnum = reglnum;
5510 rex.reg_startpos[0].col = col;
5511 rex.reg_endpos[0].lnum = reglnum;
5512 rex.reg_endpos[0].col = col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005513 }
5514 else
5515 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005516 rex.reg_startp[0] = regline + col;
5517 rex.reg_endp[0] = regline + col + len2;
Bram Moolenaar473de612013-06-08 18:19:48 +02005518 }
5519 return 1L;
5520 }
5521
5522 /* Try finding regstart after the current match. */
5523 col += MB_CHAR2LEN(regstart); /* skip regstart */
5524 if (skip_to_start(regstart, &col) == FAIL)
5525 break;
5526 }
5527 return 0L;
5528}
5529
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005530#ifdef FEAT_RELTIME
5531 static int
5532nfa_did_time_out()
5533{
5534 if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit))
5535 {
5536 if (nfa_timed_out != NULL)
5537 *nfa_timed_out = TRUE;
5538 return TRUE;
5539 }
5540 return FALSE;
5541}
5542#endif
5543
Bram Moolenaar473de612013-06-08 18:19:48 +02005544/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005545 * Main matching routine.
5546 *
5547 * Run NFA to determine whether it matches reginput.
5548 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005549 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005550 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005551 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005552 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005553 * Note: Caller must ensure that: start != NULL.
5554 */
5555 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005556nfa_regmatch(
5557 nfa_regprog_T *prog,
5558 nfa_state_T *start,
5559 regsubs_T *submatch,
5560 regsubs_T *m)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005561{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562 int result;
Bram Moolenaaraaf30472015-01-27 14:40:00 +01005563 size_t size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005565 int go_to_nextline = FALSE;
5566 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005567 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005568 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005569 nfa_list_T *thislist;
5570 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005571 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005572 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005573 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005574 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005575 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005576 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005577#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005578 FILE *debug;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005579#endif
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005580
Bram Moolenaar41f12052013-08-25 17:01:42 +02005581 /* Some patterns may take a long time to match, especially when using
5582 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5583 fast_breakcheck();
5584 if (got_int)
5585 return FALSE;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005586#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005587 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01005588 return FALSE;
5589#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005590
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005591#ifdef NFA_REGEXP_DEBUG_LOG
5592 debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
5593 if (debug == NULL)
5594 {
5595 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
5596 return FALSE;
5597 }
5598#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005599 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005600
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005601 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005602 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005603
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005604 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005605 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005606 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005607 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005608 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005609 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005610
5611#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005612 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005613 if (log_fd != NULL)
5614 {
5615 fprintf(log_fd, "**********************************\n");
5616 nfa_set_code(start->c);
5617 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5618 abs(start->id), code);
5619 fprintf(log_fd, "**********************************\n");
5620 }
5621 else
5622 {
5623 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5624 log_fd = stderr;
5625 }
5626#endif
5627
5628 thislist = &list[0];
5629 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005630 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005631 nextlist = &list[1];
5632 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005633 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005634#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005635 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005636#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005637 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005638
5639 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5640 * it's the first MOPEN. */
5641 if (toplevel)
5642 {
5643 if (REG_MULTI)
5644 {
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005645 m->norm.list.multi[0].start_lnum = reglnum;
5646 m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005647 }
5648 else
5649 m->norm.list.line[0].start = reginput;
5650 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005651 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005652 }
5653 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005654 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005655
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005656#define ADD_STATE_IF_MATCH(state) \
5657 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005658 add_state = state->out; \
5659 add_off = clen; \
5660 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005661
5662 /*
5663 * Run for each character.
5664 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005665 for (;;)
5666 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005667 int curc;
5668 int clen;
5669
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005670#ifdef FEAT_MBYTE
5671 if (has_mbyte)
5672 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005673 curc = (*mb_ptr2char)(reginput);
5674 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675 }
5676 else
5677#endif
5678 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005679 curc = *reginput;
5680 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005682 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005683 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005684 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005685 go_to_nextline = FALSE;
5686 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005687
5688 /* swap lists */
5689 thislist = &list[flag];
5690 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005691 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005692 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005693 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005694 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5695 {
5696 /* too many states, retry with old engine */
5697 nfa_match = NFA_TOO_EXPENSIVE;
5698 goto theend;
5699 }
5700
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005701 thislist->id = nfa_listid;
5702 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005703
5704#ifdef ENABLE_LOG
5705 fprintf(log_fd, "------------------------------------------\n");
5706 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005707 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005708 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005709 {
5710 int i;
5711
5712 for (i = 0; i < thislist->n; i++)
5713 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5714 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005715 fprintf(log_fd, "\n");
5716#endif
5717
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005718#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005719 fprintf(debug, "\n-------------------\n");
5720#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005721 /*
5722 * If the state lists are empty we can stop.
5723 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005724 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005725 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005726
5727 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005728 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005729 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005730 /* If the list gets very long there probably is something wrong.
5731 * At least allow interrupting with CTRL-C. */
5732 fast_breakcheck();
5733 if (got_int)
5734 break;
5735#ifdef FEAT_RELTIME
5736 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
5737 {
5738 nfa_time_count = 0;
5739 if (nfa_did_time_out())
5740 break;
5741 }
5742#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005743 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005744
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005745#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005746 nfa_set_code(t->state->c);
5747 fprintf(debug, "%s, ", code);
5748#endif
5749#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005750 {
5751 int col;
5752
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005753 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005754 col = -1;
5755 else if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01005756 col = t->subs.norm.list.multi[0].start_col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005757 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005758 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005759 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005760 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5761 abs(t->state->id), (int)t->state->c, code, col,
5762 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005763 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005764#endif
5765
5766 /*
5767 * Handle the possible codes of the current state.
5768 * The most important is NFA_MATCH.
5769 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005770 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005771 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005772 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005773 switch (t->state->c)
5774 {
5775 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005776 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005777#ifdef FEAT_MBYTE
5778 /* If the match ends before a composing characters and
Bram Moolenaar6100d022016-10-02 16:51:57 +02005779 * rex.reg_icombine is not set, that is not really a match. */
5780 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc))
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005781 break;
5782#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005783 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005784 copy_sub(&submatch->norm, &t->subs.norm);
5785#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005786 if (nfa_has_zsubexpr)
5787 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005788#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005789#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005790 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005791#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005792 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005793 * states at this position. When the list of states is going
5794 * to be empty quit without advancing, so that "reginput" is
5795 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005796 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005797 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005798 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005799 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005800
5801 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005802 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005803 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005804 /*
5805 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005806 * NFA_START_INVISIBLE_BEFORE node.
5807 * They surround a zero-width group, used with "\@=", "\&",
5808 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005809 * If we got here, it means that the current "invisible" group
5810 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005811 * nfa_regmatch(). For a look-behind match only when it ends
5812 * in the position in "nfa_endp".
5813 * Submatches are stored in *m, and used in the parent call.
5814 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005815#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005816 if (nfa_endp != NULL)
5817 {
5818 if (REG_MULTI)
5819 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5820 (int)reglnum,
5821 (int)nfa_endp->se_u.pos.lnum,
5822 (int)(reginput - regline),
5823 nfa_endp->se_u.pos.col);
5824 else
5825 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5826 (int)(reginput - regline),
5827 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005828 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005829#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005830 /* If "nfa_endp" is set it's only a match if it ends at
5831 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005832 if (nfa_endp != NULL && (REG_MULTI
5833 ? (reglnum != nfa_endp->se_u.pos.lnum
5834 || (int)(reginput - regline)
5835 != nfa_endp->se_u.pos.col)
5836 : reginput != nfa_endp->se_u.ptr))
5837 break;
5838
5839 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005840 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005841 {
5842 copy_sub(&m->norm, &t->subs.norm);
5843#ifdef FEAT_SYN_HL
5844 if (nfa_has_zsubexpr)
5845 copy_sub(&m->synt, &t->subs.synt);
5846#endif
5847 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005848#ifdef ENABLE_LOG
5849 fprintf(log_fd, "Match found:\n");
5850 log_subsexpr(m);
5851#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005852 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005853 /* See comment above at "goto nextchar". */
5854 if (nextlist->n == 0)
5855 clen = 0;
5856 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005857
5858 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005859 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005860 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005861 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005862 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005863 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005864 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005865 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005866 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005867#ifdef ENABLE_LOG
5868 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5869 failure_chance(t->state->out, 0),
5870 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005871#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005872 /* Do it directly if there already is a PIM or when
5873 * nfa_postprocess() detected it will work better. */
5874 if (t->pim.result != NFA_PIM_UNUSED
5875 || t->state->c == NFA_START_INVISIBLE_FIRST
5876 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5877 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5878 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005879 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005880 int in_use = m->norm.in_use;
5881
Bram Moolenaar699c1202013-09-25 16:41:54 +02005882 /* Copy submatch info for the recursive call, opposite
5883 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005884 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005885#ifdef FEAT_SYN_HL
5886 if (nfa_has_zsubexpr)
5887 copy_sub_off(&m->synt, &t->subs.synt);
5888#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005889
Bram Moolenaara2d95102013-06-04 14:23:05 +02005890 /*
5891 * First try matching the invisible match, then what
5892 * follows.
5893 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005894 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005895 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005896 if (result == NFA_TOO_EXPENSIVE)
5897 {
5898 nfa_match = result;
5899 goto theend;
5900 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005901
Bram Moolenaardecd9542013-06-07 16:31:50 +02005902 /* for \@! and \@<! it is a match when the result is
5903 * FALSE */
5904 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005905 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5906 || t->state->c
5907 == NFA_START_INVISIBLE_BEFORE_NEG
5908 || t->state->c
5909 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005910 {
5911 /* Copy submatch info from the recursive call */
5912 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005913#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005914 if (nfa_has_zsubexpr)
5915 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005916#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005917 /* If the pattern has \ze and it matched in the
5918 * sub pattern, use it. */
5919 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005920
Bram Moolenaara2d95102013-06-04 14:23:05 +02005921 /* t->state->out1 is the corresponding
5922 * END_INVISIBLE node; Add its out to the current
5923 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005924 add_here = TRUE;
5925 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005926 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005927 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005928 }
5929 else
5930 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005931 nfa_pim_T pim;
5932
Bram Moolenaara2d95102013-06-04 14:23:05 +02005933 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005934 * First try matching what follows. Only if a match
5935 * is found verify the invisible match matches. Add a
5936 * nfa_pim_T to the following states, it contains info
5937 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005938 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005939 pim.state = t->state;
5940 pim.result = NFA_PIM_TODO;
5941 pim.subs.norm.in_use = 0;
5942#ifdef FEAT_SYN_HL
5943 pim.subs.synt.in_use = 0;
5944#endif
5945 if (REG_MULTI)
5946 {
5947 pim.end.pos.col = (int)(reginput - regline);
5948 pim.end.pos.lnum = reglnum;
5949 }
5950 else
5951 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005952
5953 /* t->state->out1 is the corresponding END_INVISIBLE
5954 * node; Add its out to the current list (zero-width
5955 * match). */
5956 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005957 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005958 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005959 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005960 break;
5961
Bram Moolenaar87953742013-06-05 18:52:40 +02005962 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005963 {
5964 nfa_state_T *skip = NULL;
5965#ifdef ENABLE_LOG
5966 int skip_lid = 0;
5967#endif
5968
5969 /* There is no point in trying to match the pattern if the
5970 * output state is not going to be added to the list. */
5971 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5972 {
5973 skip = t->state->out1->out;
5974#ifdef ENABLE_LOG
5975 skip_lid = nextlist->id;
5976#endif
5977 }
5978 else if (state_in_list(nextlist,
5979 t->state->out1->out->out, &t->subs))
5980 {
5981 skip = t->state->out1->out->out;
5982#ifdef ENABLE_LOG
5983 skip_lid = nextlist->id;
5984#endif
5985 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005986 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005987 t->state->out1->out->out, &t->subs))
5988 {
5989 skip = t->state->out1->out->out;
5990#ifdef ENABLE_LOG
5991 skip_lid = thislist->id;
5992#endif
5993 }
5994 if (skip != NULL)
5995 {
5996#ifdef ENABLE_LOG
5997 nfa_set_code(skip->c);
5998 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5999 abs(skip->id), skip_lid, skip->c, code);
6000#endif
6001 break;
6002 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02006003 /* Copy submatch info to the recursive call, opposite of what
6004 * happens afterwards. */
6005 copy_sub_off(&m->norm, &t->subs.norm);
6006#ifdef FEAT_SYN_HL
6007 if (nfa_has_zsubexpr)
6008 copy_sub_off(&m->synt, &t->subs.synt);
6009#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02006010
Bram Moolenaar87953742013-06-05 18:52:40 +02006011 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006012 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02006013 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01006014 if (result == NFA_TOO_EXPENSIVE)
6015 {
6016 nfa_match = result;
6017 goto theend;
6018 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006019 if (result)
6020 {
6021 int bytelen;
6022
6023#ifdef ENABLE_LOG
6024 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
6025 log_subsexpr(m);
6026#endif
6027 /* Copy submatch info from the recursive call */
6028 copy_sub_off(&t->subs.norm, &m->norm);
6029#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006030 if (nfa_has_zsubexpr)
6031 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02006032#endif
6033 /* Now we need to skip over the matched text and then
6034 * continue with what follows. */
6035 if (REG_MULTI)
6036 /* TODO: multi-line match */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006037 bytelen = m->norm.list.multi[0].end_col
Bram Moolenaar87953742013-06-05 18:52:40 +02006038 - (int)(reginput - regline);
6039 else
6040 bytelen = (int)(m->norm.list.line[0].end - reginput);
6041
6042#ifdef ENABLE_LOG
6043 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
6044#endif
6045 if (bytelen == 0)
6046 {
6047 /* empty match, output of corresponding
6048 * NFA_END_PATTERN/NFA_SKIP to be used at current
6049 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006050 add_here = TRUE;
6051 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02006052 }
6053 else if (bytelen <= clen)
6054 {
6055 /* match current character, output of corresponding
6056 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02006057 add_state = t->state->out1->out->out;
6058 add_off = clen;
6059 }
6060 else
6061 {
6062 /* skip over the matched characters, set character
6063 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02006064 add_state = t->state->out1->out;
6065 add_off = bytelen;
6066 add_count = bytelen - clen;
6067 }
6068 }
6069 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02006070 }
Bram Moolenaar87953742013-06-05 18:52:40 +02006071
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006072 case NFA_BOL:
6073 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006074 {
6075 add_here = TRUE;
6076 add_state = t->state->out;
6077 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006078 break;
6079
6080 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006081 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006082 {
6083 add_here = TRUE;
6084 add_state = t->state->out;
6085 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006086 break;
6087
6088 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006089 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006090
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006091 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006092 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006093#ifdef FEAT_MBYTE
6094 else if (has_mbyte)
6095 {
6096 int this_class;
6097
6098 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006099 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006100 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006101 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006103 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006104 }
6105#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006106 else if (!vim_iswordc_buf(curc, rex.reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006107 || (reginput > regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006108 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006109 result = FALSE;
6110 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006111 {
6112 add_here = TRUE;
6113 add_state = t->state->out;
6114 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006116
6117 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02006118 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006119 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006120 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006121#ifdef FEAT_MBYTE
6122 else if (has_mbyte)
6123 {
6124 int this_class, prev_class;
6125
6126 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006127 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006128 prev_class = reg_prev_class();
6129 if (this_class == prev_class
6130 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02006131 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006132 }
6133#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02006134 else if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006135 || (reginput[0] != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006136 && vim_iswordc_buf(curc, rex.reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02006137 result = FALSE;
6138 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006139 {
6140 add_here = TRUE;
6141 add_state = t->state->out;
6142 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006143 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006144
Bram Moolenaar4b780632013-05-31 22:14:52 +02006145 case NFA_BOF:
6146 if (reglnum == 0 && reginput == regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02006147 && (!REG_MULTI || rex.reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006148 {
6149 add_here = TRUE;
6150 add_state = t->state->out;
6151 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006152 break;
6153
6154 case NFA_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006155 if (reglnum == rex.reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006156 {
6157 add_here = TRUE;
6158 add_state = t->state->out;
6159 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02006160 break;
6161
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006162#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006163 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006164 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006165 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006166 int len = 0;
6167 nfa_state_T *end;
6168 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006169 int cchars[MAX_MCO];
6170 int ccount = 0;
6171 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006172
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006173 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006174 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006175 if (utf_iscomposing(sta->c))
6176 {
6177 /* Only match composing character(s), ignore base
6178 * character. Used for ".{composing}" and "{composing}"
6179 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006180 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006181 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006182 if (rex.reg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006183 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006184 /* If \Z was present, then ignore composing characters.
6185 * When ignoring the base character this always matches. */
Bram Moolenaardff72ba2018-02-08 22:45:17 +01006186 if (sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006187 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006188 else
6189 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006190 while (sta->c != NFA_END_COMPOSING)
6191 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006192 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006193
6194 /* Check base character matches first, unless ignored. */
6195 else if (len > 0 || mc == sta->c)
6196 {
6197 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006198 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006199 len += mb_char2len(mc);
6200 sta = sta->out;
6201 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006202
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006203 /* We don't care about the order of composing characters.
6204 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006205 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006206 {
6207 mc = mb_ptr2char(reginput + len);
6208 cchars[ccount++] = mc;
6209 len += mb_char2len(mc);
6210 if (ccount == MAX_MCO)
6211 break;
6212 }
6213
6214 /* Check that each composing char in the pattern matches a
6215 * composing char in the text. We do not check if all
6216 * composing chars are matched. */
6217 result = OK;
6218 while (sta->c != NFA_END_COMPOSING)
6219 {
6220 for (j = 0; j < ccount; ++j)
6221 if (cchars[j] == sta->c)
6222 break;
6223 if (j == ccount)
6224 {
6225 result = FAIL;
6226 break;
6227 }
6228 sta = sta->out;
6229 }
6230 }
6231 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006232 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006233
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006234 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006235 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006236 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006237 }
6238#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006239
6240 case NFA_NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006241 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI
6242 && reglnum <= rex.reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006243 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006244 go_to_nextline = TRUE;
6245 /* Pass -1 for the offset, which means taking the position
6246 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006247 add_state = t->state->out;
6248 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006249 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006250 else if (curc == '\n' && rex.reg_line_lbr)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006251 {
6252 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006253 add_state = t->state->out;
6254 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006255 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006256 break;
6257
Bram Moolenaar417bad22013-06-07 14:08:30 +02006258 case NFA_START_COLL:
6259 case NFA_START_NEG_COLL:
6260 {
6261 /* What follows is a list of characters, until NFA_END_COLL.
6262 * One of them must match or none of them must match. */
6263 nfa_state_T *state;
6264 int result_if_matched;
6265 int c1, c2;
6266
6267 /* Never match EOL. If it's part of the collection it is added
6268 * as a separate state with an OR. */
6269 if (curc == NUL)
6270 break;
6271
6272 state = t->state->out;
6273 result_if_matched = (t->state->c == NFA_START_COLL);
6274 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006275 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006276 if (state->c == NFA_END_COLL)
6277 {
6278 result = !result_if_matched;
6279 break;
6280 }
6281 if (state->c == NFA_RANGE_MIN)
6282 {
6283 c1 = state->val;
6284 state = state->out; /* advance to NFA_RANGE_MAX */
6285 c2 = state->val;
6286#ifdef ENABLE_LOG
6287 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6288 curc, c1, c2);
6289#endif
6290 if (curc >= c1 && curc <= c2)
6291 {
6292 result = result_if_matched;
6293 break;
6294 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006295 if (rex.reg_ic)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006296 {
6297 int curc_low = MB_TOLOWER(curc);
6298 int done = FALSE;
6299
6300 for ( ; c1 <= c2; ++c1)
6301 if (MB_TOLOWER(c1) == curc_low)
6302 {
6303 result = result_if_matched;
6304 done = TRUE;
6305 break;
6306 }
6307 if (done)
6308 break;
6309 }
6310 }
6311 else if (state->c < 0 ? check_char_class(state->c, curc)
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006312 : (curc == state->c
Bram Moolenaar6100d022016-10-02 16:51:57 +02006313 || (rex.reg_ic && MB_TOLOWER(curc)
Bram Moolenaar417bad22013-06-07 14:08:30 +02006314 == MB_TOLOWER(state->c))))
6315 {
6316 result = result_if_matched;
6317 break;
6318 }
6319 state = state->out;
6320 }
6321 if (result)
6322 {
6323 /* next state is in out of the NFA_END_COLL, out1 of
6324 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006325 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006326 add_off = clen;
6327 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006329 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006330
6331 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006332 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006333 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006334 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006335 add_state = t->state->out;
6336 add_off = clen;
6337 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 break;
6339
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006340 case NFA_ANY_COMPOSING:
6341 /* On a composing character skip over it. Otherwise do
6342 * nothing. Always matches. */
6343#ifdef FEAT_MBYTE
6344 if (enc_utf8 && utf_iscomposing(curc))
6345 {
6346 add_off = clen;
6347 }
6348 else
6349#endif
6350 {
6351 add_here = TRUE;
6352 add_off = 0;
6353 }
6354 add_state = t->state->out;
6355 break;
6356
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006357 /*
6358 * Character classes like \a for alpha, \d for digit etc.
6359 */
6360 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006361 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006362 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006363 break;
6364
6365 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006366 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006367 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006368 break;
6369
6370 case NFA_KWORD: /* \k */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006371 result = vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006372 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006373 break;
6374
6375 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006376 result = !VIM_ISDIGIT(curc)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006377 && vim_iswordp_buf(reginput, rex.reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006378 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006379 break;
6380
6381 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006382 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006383 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 break;
6385
6386 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006387 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006388 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389 break;
6390
6391 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006392 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006393 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006394 break;
6395
6396 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006397 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006398 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 break;
6400
6401 case NFA_WHITE: /* \s */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006402 result = VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006403 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006404 break;
6405
6406 case NFA_NWHITE: /* \S */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006407 result = curc != NUL && !VIM_ISWHITE(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006408 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006409 break;
6410
6411 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006412 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006413 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 break;
6415
6416 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006417 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006418 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006419 break;
6420
6421 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006422 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006423 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006424 break;
6425
6426 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006427 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006428 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006429 break;
6430
6431 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006432 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006433 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006434 break;
6435
6436 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006437 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006438 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006439 break;
6440
6441 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006442 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006443 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006444 break;
6445
6446 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006447 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006448 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006449 break;
6450
6451 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006452 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006453 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006454 break;
6455
6456 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006457 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006458 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006459 break;
6460
6461 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006462 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006463 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006464 break;
6465
6466 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006467 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006468 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006469 break;
6470
6471 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006472 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006473 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006474 break;
6475
6476 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006477 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006478 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006479 break;
6480
6481 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006482 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006483 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006484 break;
6485
6486 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006487 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006488 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006489 break;
6490
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006491 case NFA_LOWER_IC: /* [a-z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006492 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006493 ADD_STATE_IF_MATCH(t->state);
6494 break;
6495
6496 case NFA_NLOWER_IC: /* [^a-z] */
6497 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006498 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006499 ADD_STATE_IF_MATCH(t->state);
6500 break;
6501
6502 case NFA_UPPER_IC: /* [A-Z] */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006503 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006504 ADD_STATE_IF_MATCH(t->state);
6505 break;
6506
6507 case NFA_NUPPER_IC: /* ^[A-Z] */
6508 result = curc != NUL
Bram Moolenaar6100d022016-10-02 16:51:57 +02006509 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc)));
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006510 ADD_STATE_IF_MATCH(t->state);
6511 break;
6512
Bram Moolenaar5714b802013-05-28 22:03:20 +02006513 case NFA_BACKREF1:
6514 case NFA_BACKREF2:
6515 case NFA_BACKREF3:
6516 case NFA_BACKREF4:
6517 case NFA_BACKREF5:
6518 case NFA_BACKREF6:
6519 case NFA_BACKREF7:
6520 case NFA_BACKREF8:
6521 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006522#ifdef FEAT_SYN_HL
6523 case NFA_ZREF1:
6524 case NFA_ZREF2:
6525 case NFA_ZREF3:
6526 case NFA_ZREF4:
6527 case NFA_ZREF5:
6528 case NFA_ZREF6:
6529 case NFA_ZREF7:
6530 case NFA_ZREF8:
6531 case NFA_ZREF9:
6532#endif
6533 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006534 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006535 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006536 int bytelen;
6537
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006538 if (t->state->c <= NFA_BACKREF9)
6539 {
6540 subidx = t->state->c - NFA_BACKREF1 + 1;
6541 result = match_backref(&t->subs.norm, subidx, &bytelen);
6542 }
6543#ifdef FEAT_SYN_HL
6544 else
6545 {
6546 subidx = t->state->c - NFA_ZREF1 + 1;
6547 result = match_zref(subidx, &bytelen);
6548 }
6549#endif
6550
Bram Moolenaar5714b802013-05-28 22:03:20 +02006551 if (result)
6552 {
6553 if (bytelen == 0)
6554 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006555 /* empty match always works, output of NFA_SKIP to be
6556 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006557 add_here = TRUE;
6558 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006559 }
6560 else if (bytelen <= clen)
6561 {
6562 /* match current character, jump ahead to out of
6563 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006564 add_state = t->state->out->out;
6565 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006566 }
6567 else
6568 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006569 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006570 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006571 add_state = t->state->out;
6572 add_off = bytelen;
6573 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006574 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006575 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006576 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006577 }
6578 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006579 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006580 if (t->count - clen <= 0)
6581 {
6582 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006583 add_state = t->state->out;
6584 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006585 }
6586 else
6587 {
6588 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006589 add_state = t->state;
6590 add_off = 0;
6591 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006592 }
6593 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006594
Bram Moolenaar423532e2013-05-29 21:14:42 +02006595 case NFA_LNUM:
6596 case NFA_LNUM_GT:
6597 case NFA_LNUM_LT:
6598 result = (REG_MULTI &&
6599 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
Bram Moolenaar6100d022016-10-02 16:51:57 +02006600 (long_u)(reglnum + rex.reg_firstlnum)));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006601 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006602 {
6603 add_here = TRUE;
6604 add_state = t->state->out;
6605 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006606 break;
6607
6608 case NFA_COL:
6609 case NFA_COL_GT:
6610 case NFA_COL_LT:
6611 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6612 (long_u)(reginput - regline) + 1);
6613 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006614 {
6615 add_here = TRUE;
6616 add_state = t->state->out;
6617 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006618 break;
6619
6620 case NFA_VCOL:
6621 case NFA_VCOL_GT:
6622 case NFA_VCOL_LT:
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006623 {
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006624 int op = t->state->c - NFA_VCOL;
6625 colnr_T col = (colnr_T)(reginput - regline);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006626 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006627
6628 /* Bail out quickly when there can't be a match, avoid the
6629 * overhead of win_linetabsize() on long lines. */
Bram Moolenaar4f36dc32015-03-05 17:16:06 +01006630 if (op != 1 && col > t->state->val
6631#ifdef FEAT_MBYTE
6632 * (has_mbyte ? MB_MAXBYTES : 1)
6633#endif
6634 )
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006635 break;
Bram Moolenaaref795d12015-01-18 16:46:32 +01006636 result = FALSE;
6637 if (op == 1 && col - 1 > t->state->val && col > 100)
6638 {
6639 int ts = wp->w_buffer->b_p_ts;
6640
6641 /* Guess that a character won't use more columns than
6642 * 'tabstop', with a minimum of 4. */
6643 if (ts < 4)
6644 ts = 4;
6645 result = col > t->state->val * ts;
6646 }
6647 if (!result)
6648 result = nfa_re_num_cmp(t->state->val, op,
6649 (long_u)win_linetabsize(wp, regline, col) + 1);
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006650 if (result)
6651 {
6652 add_here = TRUE;
6653 add_state = t->state->out;
6654 }
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006655 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006656 break;
6657
Bram Moolenaar044aa292013-06-04 21:27:38 +02006658 case NFA_MARK:
6659 case NFA_MARK_GT:
6660 case NFA_MARK_LT:
6661 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006662 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE);
Bram Moolenaar044aa292013-06-04 21:27:38 +02006663
6664 /* Compare the mark position to the match position. */
6665 result = (pos != NULL /* mark doesn't exist */
6666 && pos->lnum > 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006667 && (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006668 ? (pos->col == (colnr_T)(reginput - regline)
6669 ? t->state->c == NFA_MARK
6670 : (pos->col < (colnr_T)(reginput - regline)
6671 ? t->state->c == NFA_MARK_GT
6672 : t->state->c == NFA_MARK_LT))
Bram Moolenaar6100d022016-10-02 16:51:57 +02006673 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar044aa292013-06-04 21:27:38 +02006674 ? t->state->c == NFA_MARK_GT
6675 : t->state->c == NFA_MARK_LT)));
6676 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006677 {
6678 add_here = TRUE;
6679 add_state = t->state->out;
6680 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006681 break;
6682 }
6683
Bram Moolenaar423532e2013-05-29 21:14:42 +02006684 case NFA_CURSOR:
Bram Moolenaar6100d022016-10-02 16:51:57 +02006685 result = (rex.reg_win != NULL
6686 && (reglnum + rex.reg_firstlnum
6687 == rex.reg_win->w_cursor.lnum)
Bram Moolenaar423532e2013-05-29 21:14:42 +02006688 && ((colnr_T)(reginput - regline)
Bram Moolenaar6100d022016-10-02 16:51:57 +02006689 == rex.reg_win->w_cursor.col));
Bram Moolenaar423532e2013-05-29 21:14:42 +02006690 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006691 {
6692 add_here = TRUE;
6693 add_state = t->state->out;
6694 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006695 break;
6696
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006697 case NFA_VISUAL:
6698 result = reg_match_visual();
6699 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006700 {
6701 add_here = TRUE;
6702 add_state = t->state->out;
6703 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006704 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006705
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006706 case NFA_MOPEN1:
6707 case NFA_MOPEN2:
6708 case NFA_MOPEN3:
6709 case NFA_MOPEN4:
6710 case NFA_MOPEN5:
6711 case NFA_MOPEN6:
6712 case NFA_MOPEN7:
6713 case NFA_MOPEN8:
6714 case NFA_MOPEN9:
6715#ifdef FEAT_SYN_HL
6716 case NFA_ZOPEN:
6717 case NFA_ZOPEN1:
6718 case NFA_ZOPEN2:
6719 case NFA_ZOPEN3:
6720 case NFA_ZOPEN4:
6721 case NFA_ZOPEN5:
6722 case NFA_ZOPEN6:
6723 case NFA_ZOPEN7:
6724 case NFA_ZOPEN8:
6725 case NFA_ZOPEN9:
6726#endif
6727 case NFA_NOPEN:
6728 case NFA_ZSTART:
6729 /* These states are only added to be able to bail out when
6730 * they are added again, nothing is to be done. */
6731 break;
6732
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006733 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006734 {
6735 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006736
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006737#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006738 if (c < 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01006739 IEMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006740#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006741 result = (c == curc);
6742
Bram Moolenaar6100d022016-10-02 16:51:57 +02006743 if (!result && rex.reg_ic)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006744 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006745#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02006746 /* If rex.reg_icombine is not set only skip over the character
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006747 * itself. When it is set skip over composing characters. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006748 if (result && enc_utf8 && !rex.reg_icombine)
Bram Moolenaar2186ffa2015-05-04 10:33:15 +02006749 clen = utf_ptr2len(reginput);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006750#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006751 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006752 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006753 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006754
6755 } /* switch (t->state->c) */
6756
6757 if (add_state != NULL)
6758 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006759 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006760 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006761
6762 if (t->pim.result == NFA_PIM_UNUSED)
6763 pim = NULL;
6764 else
6765 pim = &t->pim;
6766
6767 /* Handle the postponed invisible match if the match might end
6768 * without advancing and before the end of the line. */
6769 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006770 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006771 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006772 {
6773#ifdef ENABLE_LOG
6774 fprintf(log_fd, "\n");
6775 fprintf(log_fd, "==================================\n");
6776 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6777 fprintf(log_fd, "\n");
6778#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006779 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006780 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006781 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006782 /* for \@! and \@<! it is a match when the result is
6783 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006784 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006785 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6786 || pim->state->c
6787 == NFA_START_INVISIBLE_BEFORE_NEG
6788 || pim->state->c
6789 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006790 {
6791 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006792 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006793#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006794 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006795 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006796#endif
6797 }
6798 }
6799 else
6800 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006801 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006802#ifdef ENABLE_LOG
6803 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006804 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006805 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6806 fprintf(log_fd, "\n");
6807#endif
6808 }
6809
Bram Moolenaardecd9542013-06-07 16:31:50 +02006810 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006811 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006812 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6813 || pim->state->c
6814 == NFA_START_INVISIBLE_BEFORE_NEG
6815 || pim->state->c
6816 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006817 {
6818 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006819 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006820#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006821 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006822 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006823#endif
6824 }
6825 else
6826 /* look-behind match failed, don't add the state */
6827 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006828
6829 /* Postponed invisible match was handled, don't add it to
6830 * following states. */
6831 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006832 }
6833
Bram Moolenaara951e352013-10-06 15:46:11 +02006834 /* If "pim" points into l->t it will become invalid when
6835 * adding the state causes the list to be reallocated. Make a
6836 * local copy to avoid that. */
6837 if (pim == &t->pim)
6838 {
6839 copy_pim(&pim_copy, pim);
6840 pim = &pim_copy;
6841 }
6842
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006843 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006844 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006845 else
6846 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006847 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006848 if (add_count > 0)
6849 nextlist->t[nextlist->n - 1].count = add_count;
6850 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006851 }
6852
6853 } /* for (thislist = thislist; thislist->state; thislist++) */
6854
Bram Moolenaare23febd2013-05-26 18:40:14 +02006855 /* Look for the start of a match in the current position by adding the
6856 * start state to the list of states.
6857 * The first found match is the leftmost one, thus the order of states
6858 * matters!
6859 * Do not add the start state in recursive calls of nfa_regmatch(),
6860 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006861 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006862 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006863 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006864 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006865 && reglnum == 0
6866 && clen != 0
Bram Moolenaar6100d022016-10-02 16:51:57 +02006867 && (rex.reg_maxcol == 0
6868 || (colnr_T)(reginput - regline) < rex.reg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006869 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006870 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006871 ? (reglnum < nfa_endp->se_u.pos.lnum
6872 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar2c7b9062018-02-04 18:22:46 +01006873 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006874 < nfa_endp->se_u.pos.col))
6875 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006876 {
6877#ifdef ENABLE_LOG
6878 fprintf(log_fd, "(---) STARTSTATE\n");
6879#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006880 /* Inline optimized code for addstate() if we know the state is
6881 * the first MOPEN. */
6882 if (toplevel)
6883 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006884 int add = TRUE;
6885 int c;
6886
6887 if (prog->regstart != NUL && clen != 0)
6888 {
6889 if (nextlist->n == 0)
6890 {
6891 colnr_T col = (colnr_T)(reginput - regline) + clen;
6892
6893 /* Nextlist is empty, we can skip ahead to the
6894 * character that must appear at the start. */
6895 if (skip_to_start(prog->regstart, &col) == FAIL)
6896 break;
6897#ifdef ENABLE_LOG
6898 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6899 col - ((colnr_T)(reginput - regline) + clen));
6900#endif
6901 reginput = regline + col - clen;
6902 }
6903 else
6904 {
6905 /* Checking if the required start character matches is
6906 * cheaper than adding a state that won't match. */
6907 c = PTR2CHAR(reginput + clen);
Bram Moolenaar6100d022016-10-02 16:51:57 +02006908 if (c != prog->regstart && (!rex.reg_ic
6909 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart)))
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006910 {
6911#ifdef ENABLE_LOG
6912 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6913#endif
6914 add = FALSE;
6915 }
6916 }
6917 }
6918
6919 if (add)
6920 {
6921 if (REG_MULTI)
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01006922 m->norm.list.multi[0].start_col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006923 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006924 else
6925 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006926 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006927 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006928 }
6929 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006930 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006931 }
6932
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006933#ifdef ENABLE_LOG
6934 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006935 {
6936 int i;
6937
6938 for (i = 0; i < thislist->n; i++)
6939 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6940 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006941 fprintf(log_fd, "\n");
6942#endif
6943
6944nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006945 /* Advance to the next character, or advance to the next line, or
6946 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006947 if (clen != 0)
6948 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006949 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6950 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006951 reg_nextline();
6952 else
6953 break;
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006954
6955 /* Allow interrupting with CTRL-C. */
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006956 line_breakcheck();
Bram Moolenaara20bcad2015-01-14 18:40:28 +01006957 if (got_int)
6958 break;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006959#ifdef FEAT_RELTIME
6960 /* Check for timeout once in a twenty times to avoid overhead. */
6961 if (nfa_time_limit != NULL && ++nfa_time_count == 20)
6962 {
6963 nfa_time_count = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006964 if (nfa_did_time_out())
Bram Moolenaar70781ee2015-02-03 16:49:24 +01006965 break;
6966 }
6967#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02006968 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006969
6970#ifdef ENABLE_LOG
6971 if (log_fd != stderr)
6972 fclose(log_fd);
6973 log_fd = NULL;
6974#endif
6975
6976theend:
6977 /* Free memory */
6978 vim_free(list[0].t);
6979 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006980 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006981#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006982#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006983 fclose(debug);
6984#endif
6985
Bram Moolenaar963fee22013-05-26 21:47:28 +02006986 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006987}
6988
6989/*
6990 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006991 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992 */
6993 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01006994nfa_regtry(
6995 nfa_regprog_T *prog,
6996 colnr_T col,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006997 proftime_T *tm UNUSED, /* timeout limit or NULL */
6998 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006999{
7000 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007001 regsubs_T subs, m;
7002 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007003 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007004#ifdef ENABLE_LOG
7005 FILE *f;
7006#endif
7007
7008 reginput = regline + col;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007009#ifdef FEAT_RELTIME
7010 nfa_time_limit = tm;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007011 nfa_timed_out = timed_out;
Bram Moolenaar70781ee2015-02-03 16:49:24 +01007012 nfa_time_count = 0;
7013#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014
7015#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007016 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007017 if (f != NULL)
7018 {
Bram Moolenaar87953742013-06-05 18:52:40 +02007019 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020#ifdef DEBUG
7021 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
7022#endif
7023 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02007024 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02007025 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007026 fprintf(f, "\n\n");
7027 fclose(f);
7028 }
7029 else
7030 EMSG(_("Could not open temporary log file for writing "));
7031#endif
7032
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007033 clear_sub(&subs.norm);
7034 clear_sub(&m.norm);
7035#ifdef FEAT_SYN_HL
7036 clear_sub(&subs.synt);
7037 clear_sub(&m.synt);
7038#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007039
Bram Moolenaarfda37292014-11-05 14:27:36 +01007040 result = nfa_regmatch(prog, start, &subs, &m);
7041 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007042 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01007043 else if (result == NFA_TOO_EXPENSIVE)
7044 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007045
7046 cleanup_subexpr();
7047 if (REG_MULTI)
7048 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007049 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007050 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007051 rex.reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum;
7052 rex.reg_startpos[i].col = subs.norm.list.multi[i].start_col;
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007053
Bram Moolenaar6100d022016-10-02 16:51:57 +02007054 rex.reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum;
7055 rex.reg_endpos[i].col = subs.norm.list.multi[i].end_col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007056 }
7057
Bram Moolenaar6100d022016-10-02 16:51:57 +02007058 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007059 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007060 rex.reg_startpos[0].lnum = 0;
7061 rex.reg_startpos[0].col = col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007062 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007063 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007064 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02007065 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007066 rex.reg_endpos[0].lnum = reglnum;
7067 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007068 }
7069 else
7070 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007071 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007072 }
7073 else
7074 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007075 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007076 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007077 rex.reg_startp[i] = subs.norm.list.line[i].start;
7078 rex.reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007079 }
7080
Bram Moolenaar6100d022016-10-02 16:51:57 +02007081 if (rex.reg_startp[0] == NULL)
7082 rex.reg_startp[0] = regline + col;
7083 if (rex.reg_endp[0] == NULL)
7084 rex.reg_endp[0] = reginput;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007085 }
7086
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007087#ifdef FEAT_SYN_HL
7088 /* Package any found \z(...\) matches for export. Default is none. */
7089 unref_extmatch(re_extmatch_out);
7090 re_extmatch_out = NULL;
7091
7092 if (prog->reghasz == REX_SET)
7093 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007094 cleanup_zsubexpr();
7095 re_extmatch_out = make_extmatch();
Bram Moolenaar5ad075c2015-11-24 15:18:32 +01007096 /* Loop over \z1, \z2, etc. There is no \z0. */
7097 for (i = 1; i < subs.synt.in_use; i++)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007098 {
7099 if (REG_MULTI)
7100 {
7101 struct multipos *mpos = &subs.synt.list.multi[i];
7102
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02007103 /* Only accept single line matches that are valid. */
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007104 if (mpos->start_lnum >= 0
7105 && mpos->start_lnum == mpos->end_lnum
7106 && mpos->end_col >= mpos->start_col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007107 re_extmatch_out->matches[i] =
Bram Moolenaar0cd040b2015-01-27 14:54:11 +01007108 vim_strnsave(reg_getline(mpos->start_lnum)
7109 + mpos->start_col,
7110 mpos->end_col - mpos->start_col);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007111 }
7112 else
7113 {
7114 struct linepos *lpos = &subs.synt.list.line[i];
7115
7116 if (lpos->start != NULL && lpos->end != NULL)
7117 re_extmatch_out->matches[i] =
7118 vim_strnsave(lpos->start,
7119 (int)(lpos->end - lpos->start));
7120 }
7121 }
7122 }
7123#endif
7124
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007125 return 1 + reglnum;
7126}
7127
7128/*
7129 * Match a regexp against a string ("line" points to the string) or multiple
7130 * lines ("line" is NULL, use reg_getline()).
7131 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007132 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007133 */
7134 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007135nfa_regexec_both(
7136 char_u *line,
7137 colnr_T startcol, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007138 proftime_T *tm, /* timeout limit or NULL */
7139 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007140{
7141 nfa_regprog_T *prog;
7142 long retval = 0L;
7143 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007144 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007145
7146 if (REG_MULTI)
7147 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007148 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007149 line = reg_getline((linenr_T)0); /* relative to the cursor */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007150 rex.reg_startpos = rex.reg_mmatch->startpos;
7151 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007152 }
7153 else
7154 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007155 prog = (nfa_regprog_T *)rex.reg_match->regprog;
7156 rex.reg_startp = rex.reg_match->startp;
7157 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007158 }
7159
7160 /* Be paranoid... */
7161 if (prog == NULL || line == NULL)
7162 {
7163 EMSG(_(e_null));
7164 goto theend;
7165 }
7166
Bram Moolenaar6100d022016-10-02 16:51:57 +02007167 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007168 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007169 rex.reg_ic = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007171 rex.reg_ic = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007172
7173#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007174 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007175 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02007176 rex.reg_icombine = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007177#endif
7178
7179 regline = line;
7180 reglnum = 0; /* relative to line */
7181
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007182 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007183 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007184 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007185 nfa_listid = 1;
7186 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007187 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007188
Bram Moolenaard89616e2013-06-06 18:46:06 +02007189 if (prog->reganch && col > 0)
7190 return 0L;
7191
Bram Moolenaar473de612013-06-08 18:19:48 +02007192 need_clear_subexpr = TRUE;
7193#ifdef FEAT_SYN_HL
7194 /* Clear the external match subpointers if necessary. */
7195 if (prog->reghasz == REX_SET)
7196 {
7197 nfa_has_zsubexpr = TRUE;
7198 need_clear_zsubexpr = TRUE;
7199 }
7200 else
7201 nfa_has_zsubexpr = FALSE;
7202#endif
7203
Bram Moolenaard89616e2013-06-06 18:46:06 +02007204 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02007205 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02007206 /* Skip ahead until a character we know the match must start with.
7207 * When there is none there is no match. */
7208 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007209 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007210
Bram Moolenaar473de612013-06-08 18:19:48 +02007211 /* If match_text is set it contains the full text that must match.
7212 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02007213 if (prog->match_text != NULL
7214#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007215 && !rex.reg_icombine
Bram Moolenaara940aa62013-06-08 23:30:04 +02007216#endif
7217 )
Bram Moolenaar473de612013-06-08 18:19:48 +02007218 return find_match_text(col, prog->regstart, prog->match_text);
7219 }
7220
Bram Moolenaard89616e2013-06-06 18:46:06 +02007221 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007222 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaard89616e2013-06-06 18:46:06 +02007223 goto theend;
7224
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007225 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007226 for (i = 0; i < nstate; ++i)
7227 {
7228 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02007229 prog->state[i].lastlist[0] = 0;
7230 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007231 }
7232
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007233 retval = nfa_regtry(prog, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007234
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007235 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007236
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007237theend:
7238 return retval;
7239}
7240
7241/*
7242 * Compile a regular expression into internal code for the NFA matcher.
7243 * Returns the program in allocated space. Returns NULL for an error.
7244 */
7245 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007246nfa_regcomp(char_u *expr, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007247{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007248 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007249 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007250 int *postfix;
7251
7252 if (expr == NULL)
7253 return NULL;
7254
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007255 nfa_regengine.expr = expr;
Bram Moolenaare0ad3652015-01-27 12:59:55 +01007256 nfa_re_flags = re_flags;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007257
7258 init_class_tab();
7259
7260 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7261 return NULL;
7262
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007263 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007264 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007265 postfix = re2post();
7266 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007267 {
7268 /* TODO: only give this error for debugging? */
7269 if (post_ptr >= post_end)
Bram Moolenaarde330112017-01-08 20:50:52 +01007270 IEMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007271 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007272 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007273
7274 /*
7275 * In order to build the NFA, we parse the input regexp twice:
7276 * 1. first pass to count size (so we can allocate space)
7277 * 2. second to emit code
7278 */
7279#ifdef ENABLE_LOG
7280 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007281 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007282
7283 if (f != NULL)
7284 {
7285 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7286 fclose(f);
7287 }
7288 }
7289#endif
7290
7291 /*
7292 * PASS 1
7293 * Count number of NFA states in "nstate". Do not build the NFA.
7294 */
7295 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007296
Bram Moolenaar16619a22013-06-11 18:42:36 +02007297 /* allocate the regprog with space for the compiled regexp */
7298 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007299 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7300 if (prog == NULL)
7301 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007302 state_ptr = prog->state;
7303
7304 /*
7305 * PASS 2
7306 * Build the NFA
7307 */
7308 prog->start = post2nfa(postfix, post_ptr, FALSE);
7309 if (prog->start == NULL)
7310 goto fail;
7311
7312 prog->regflags = regflags;
7313 prog->engine = &nfa_regengine;
7314 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007315 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007316 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007317 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007318
Bram Moolenaara2947e22013-06-11 22:44:09 +02007319 nfa_postprocess(prog);
7320
Bram Moolenaard89616e2013-06-06 18:46:06 +02007321 prog->reganch = nfa_get_reganch(prog->start, 0);
7322 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007323 prog->match_text = nfa_get_match_text(prog->start);
7324
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007325#ifdef ENABLE_LOG
7326 nfa_postfix_dump(expr, OK);
7327 nfa_dump(prog);
7328#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007329#ifdef FEAT_SYN_HL
7330 /* Remember whether this pattern has any \z specials in it. */
7331 prog->reghasz = re_has_z;
7332#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007333 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007334 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007335
7336out:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007337 VIM_CLEAR(post_start);
7338 post_ptr = post_end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007339 state_ptr = NULL;
7340 return (regprog_T *)prog;
7341
7342fail:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007343 VIM_CLEAR(prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007344#ifdef ENABLE_LOG
7345 nfa_postfix_dump(expr, FAIL);
7346#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007347 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007348 goto out;
7349}
7350
Bram Moolenaar473de612013-06-08 18:19:48 +02007351/*
7352 * Free a compiled regexp program, returned by nfa_regcomp().
7353 */
7354 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007355nfa_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02007356{
7357 if (prog != NULL)
7358 {
7359 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007360 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007361 vim_free(prog);
7362 }
7363}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007364
7365/*
7366 * Match a regexp against a string.
7367 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7368 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007369 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007370 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007371 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007372 */
7373 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007374nfa_regexec_nl(
7375 regmatch_T *rmp,
7376 char_u *line, /* string to match against */
7377 colnr_T col, /* column to start looking for match */
7378 int line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007379{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007380 rex.reg_match = rmp;
7381 rex.reg_mmatch = NULL;
7382 rex.reg_maxline = 0;
7383 rex.reg_line_lbr = line_lbr;
7384 rex.reg_buf = curbuf;
7385 rex.reg_win = NULL;
7386 rex.reg_ic = rmp->rm_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 = 0;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007391 return nfa_regexec_both(line, col, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007392}
7393
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007394
7395/*
7396 * Match a regexp against multiple lines.
7397 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7398 * Uses curbuf for line count and 'iskeyword'.
7399 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007400 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007401 * match otherwise.
7402 *
7403 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7404 *
7405 * ! Also NOTE : match may actually be in another line. e.g.:
7406 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7407 *
7408 * +-------------------------+
7409 * |a |
7410 * |b |
7411 * |c |
7412 * | |
7413 * +-------------------------+
7414 *
7415 * then nfa_regexec_multi() returns 3. while the original
7416 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7417 *
7418 * FIXME if this behavior is not compatible.
7419 */
7420 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01007421nfa_regexec_multi(
7422 regmmatch_T *rmp,
7423 win_T *win, /* window in which to search or NULL */
7424 buf_T *buf, /* buffer in which to search */
7425 linenr_T lnum, /* nr of line to start looking for match */
7426 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007427 proftime_T *tm, /* timeout limit or NULL */
7428 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007429{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007430 rex.reg_match = NULL;
7431 rex.reg_mmatch = rmp;
7432 rex.reg_buf = buf;
7433 rex.reg_win = win;
7434 rex.reg_firstlnum = lnum;
7435 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7436 rex.reg_line_lbr = FALSE;
7437 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007438#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02007439 rex.reg_icombine = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007440#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02007441 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007442
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007443 return nfa_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007444}
7445
7446#ifdef DEBUG
7447# undef ENABLE_LOG
7448#endif